Skip to content

Commit 4373151

Browse files
laoarakpm00
authored andcommitted
mm/util: deduplicate code in {kstrdup,kstrndup,kmemdup_nul}
These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Link: https://lkml.kernel.org/r/20241007144911.27693-7-laoar.shao@gmail.com Suggested-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Cc: Simon Horman <horms@kernel.org> Cc: Matthew Wilcox <willy@infradead.org> Cc: Alejandro Colomar <alx@kernel.org> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com> Cc: Andy Shevchenko <andy.shevchenko@gmail.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christian Brauner <brauner@kernel.org> Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Cc: David Airlie <airlied@gmail.com> Cc: Eric Biederman <ebiederm@xmission.com> Cc: Eric Paris <eparis@redhat.com> Cc: James Morris <jmorris@namei.org> Cc: Jan Kara <jack@suse.cz> Cc: Justin Stitt <justinstitt@google.com> Cc: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Cc: Matus Jokay <matus.jokay@stuba.sk> Cc: Maxime Ripard <mripard@kernel.org> Cc: Ondrej Mosnacek <omosnace@redhat.com> Cc: Paul Moore <paul@paul-moore.com> Cc: Quentin Monnet <qmo@kernel.org> Cc: "Serge E. Hallyn" <serge@hallyn.com> Cc: Stephen Smalley <stephen.smalley.work@gmail.com> Cc: Steven Rostedt (Google) <rostedt@goodmis.org> Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Cc: Thomas Zimmermann <tzimmermann@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 44ff630 commit 4373151

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

mm/util.c

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,41 @@ void kfree_const(const void *x)
4545
EXPORT_SYMBOL(kfree_const);
4646

4747
/**
48-
* kstrdup - allocate space for and copy an existing string
49-
* @s: the string to duplicate
48+
* __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated.
49+
* @s: The data to copy
50+
* @len: The size of the data, not including the NUL terminator
5051
* @gfp: the GFP mask used in the kmalloc() call when allocating memory
5152
*
52-
* Return: newly allocated copy of @s or %NULL in case of error
53+
* Return: newly allocated copy of @s with NUL-termination or %NULL in
54+
* case of error
5355
*/
54-
noinline
55-
char *kstrdup(const char *s, gfp_t gfp)
56+
static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp)
5657
{
57-
size_t len;
5858
char *buf;
5959

60-
if (!s)
60+
/* '+1' for the NUL terminator */
61+
buf = kmalloc_track_caller(len + 1, gfp);
62+
if (!buf)
6163
return NULL;
6264

63-
len = strlen(s) + 1;
64-
buf = kmalloc_track_caller(len, gfp);
65-
if (buf) {
66-
memcpy(buf, s, len);
67-
/*
68-
* During memcpy(), the string might be updated to a new value,
69-
* which could be longer than the string when strlen() is
70-
* called. Therefore, we need to add a NUL terminator.
71-
*/
72-
buf[len - 1] = '\0';
73-
}
65+
memcpy(buf, s, len);
66+
/* Ensure the buf is always NUL-terminated, regardless of @s. */
67+
buf[len] = '\0';
7468
return buf;
7569
}
70+
71+
/**
72+
* kstrdup - allocate space for and copy an existing string
73+
* @s: the string to duplicate
74+
* @gfp: the GFP mask used in the kmalloc() call when allocating memory
75+
*
76+
* Return: newly allocated copy of @s or %NULL in case of error
77+
*/
78+
noinline
79+
char *kstrdup(const char *s, gfp_t gfp)
80+
{
81+
return s ? __kmemdup_nul(s, strlen(s), gfp) : NULL;
82+
}
7683
EXPORT_SYMBOL(kstrdup);
7784

7885
/**
@@ -107,19 +114,7 @@ EXPORT_SYMBOL(kstrdup_const);
107114
*/
108115
char *kstrndup(const char *s, size_t max, gfp_t gfp)
109116
{
110-
size_t len;
111-
char *buf;
112-
113-
if (!s)
114-
return NULL;
115-
116-
len = strnlen(s, max);
117-
buf = kmalloc_track_caller(len+1, gfp);
118-
if (buf) {
119-
memcpy(buf, s, len);
120-
buf[len] = '\0';
121-
}
122-
return buf;
117+
return s ? __kmemdup_nul(s, strnlen(s, max), gfp) : NULL;
123118
}
124119
EXPORT_SYMBOL(kstrndup);
125120

@@ -193,17 +188,7 @@ EXPORT_SYMBOL(kvmemdup);
193188
*/
194189
char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
195190
{
196-
char *buf;
197-
198-
if (!s)
199-
return NULL;
200-
201-
buf = kmalloc_track_caller(len + 1, gfp);
202-
if (buf) {
203-
memcpy(buf, s, len);
204-
buf[len] = '\0';
205-
}
206-
return buf;
191+
return s ? __kmemdup_nul(s, len, gfp) : NULL;
207192
}
208193
EXPORT_SYMBOL(kmemdup_nul);
209194

0 commit comments

Comments
 (0)