diff mbox series

[v3,06/11] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul}

Message ID 20240621022959.9124-7-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Improve the copy of task comm | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 844 this patch: 847
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 3 of 3 maintainers
netdev/build_clang fail Errors and warnings before: 857 this patch: 861
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 1012 this patch: 1015
netdev/checkpatch warning CHECK: Please don't use multiple blank lines WARNING: line length of 84 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 8 this patch: 8
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc

Commit Message

Yafang Shao June 21, 2024, 2:29 a.m. UTC
These three functions follow the same pattern. To deduplicate the code,
let's introduce a common help __kstrndup().

Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 mm/internal.h | 24 ++++++++++++++++++++++++
 mm/util.c     | 27 ++++-----------------------
 2 files changed, 28 insertions(+), 23 deletions(-)

Comments

Simon Horman June 21, 2024, 1:51 p.m. UTC | #1
On Fri, Jun 21, 2024 at 10:29:54AM +0800, Yafang Shao wrote:
> These three functions follow the same pattern. To deduplicate the code,
> let's introduce a common help __kstrndup().
> 
> Suggested-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>

Hi Yafang Shao,

Some minor nits from my side.

> ---
>  mm/internal.h | 24 ++++++++++++++++++++++++
>  mm/util.c     | 27 ++++-----------------------
>  2 files changed, 28 insertions(+), 23 deletions(-)
> 
> diff --git a/mm/internal.h b/mm/internal.h
> index b2c75b12014e..fd87f685739b 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -1521,4 +1521,28 @@ static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
>  void workingset_update_node(struct xa_node *node);
>  extern struct list_lru shadow_nodes;
>  
> +/**
> + * __kstrndup - Create a NUL-terminated string from @s, which might be unterminated.
> + * @s: The data to stringify
> + * @len: The size of the data, including the null terminator
> + * @gfp: the GFP mask used in the kmalloc() call when allocating memory
> + *
> + * Return: newly allocated copy of @s with NUL-termination or %NULL in
> + * case of error
> + */
> +static __always_inline char *__kstrndup(const char *s, size_t len, gfp_t gfp)
> +{
> +	char *buf;
> +
> +	buf = kmalloc_track_caller(len, gfp);
> +	if (!buf)
> +		return NULL;
> +
> +	memcpy(buf, s, len);
> +	/* Ensure the buf is always NUL-terminated, regardless of @s. */
> +	buf[len - 1] = '\0';
> +	return buf;
> +}
> +
> +

nit: One blank line is enough.

>  #endif	/* __MM_INTERNAL_H */
> diff --git a/mm/util.c b/mm/util.c
> index 41c7875572ed..d9135c5fdf7f 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -58,17 +58,8 @@ char *kstrdup(const char *s, gfp_t gfp)
>  	if (!s)
>  		return NULL;
>  
> -	len = strlen(s) + 1;
> -	buf = kmalloc_track_caller(len, gfp);
> -	if (buf) {
> -		memcpy(buf, s, len);
> -		/* During memcpy(), the string might be updated to a new value,
> -		 * which could be longer than the string when strlen() is
> -		 * called. Therefore, we need to add a null termimator.
> -		 */
> -		buf[len - 1] = '\0';
> -	}
> -	return buf;

nit: The local variable buf is now unused, and should be removed from kstrdup().
     Likewise for kstrndup() and kmemdup_nul()

     Flagged by W=1 builds with gcc-13 and clang-18, and Smatch.

> +	len = strlen(s);
> +	return __kstrndup(s, len + 1, gfp);
>  }
>  EXPORT_SYMBOL(kstrdup);
>  
> @@ -111,12 +102,7 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp)
>  		return NULL;
>  
>  	len = strnlen(s, max);
> -	buf = kmalloc_track_caller(len+1, gfp);
> -	if (buf) {
> -		memcpy(buf, s, len);
> -		buf[len] = '\0';
> -	}
> -	return buf;
> +	return __kstrndup(s, len + 1, gfp);
>  }
>  EXPORT_SYMBOL(kstrndup);
>  
> @@ -195,12 +181,7 @@ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
>  	if (!s)
>  		return NULL;
>  
> -	buf = kmalloc_track_caller(len + 1, gfp);
> -	if (buf) {
> -		memcpy(buf, s, len);
> -		buf[len] = '\0';
> -	}
> -	return buf;
> +	return __kstrndup(s, len + 1, gfp);
>  }
>  EXPORT_SYMBOL(kmemdup_nul);
>  
> -- 
> 2.39.1
> 
>
Matthew Wilcox June 21, 2024, 1:57 p.m. UTC | #2
On Fri, Jun 21, 2024 at 10:29:54AM +0800, Yafang Shao wrote:
> +++ b/mm/internal.h

Why are you putting __kstrndup in a header file when it's only used
in util.c?

Also, I think this function is actually __kmemdup_nul(), not
__kstrndup().
Yafang Shao June 23, 2024, 2:26 a.m. UTC | #3
On Fri, Jun 21, 2024 at 9:51 PM Simon Horman <horms@kernel.org> wrote:
>
> On Fri, Jun 21, 2024 at 10:29:54AM +0800, Yafang Shao wrote:
> > These three functions follow the same pattern. To deduplicate the code,
> > let's introduce a common help __kstrndup().
> >
> > Suggested-by: Andrew Morton <akpm@linux-foundation.org>
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
>
> Hi Yafang Shao,
>
> Some minor nits from my side.
>
> > ---
> >  mm/internal.h | 24 ++++++++++++++++++++++++
> >  mm/util.c     | 27 ++++-----------------------
> >  2 files changed, 28 insertions(+), 23 deletions(-)
> >
> > diff --git a/mm/internal.h b/mm/internal.h
> > index b2c75b12014e..fd87f685739b 100644
> > --- a/mm/internal.h
> > +++ b/mm/internal.h
> > @@ -1521,4 +1521,28 @@ static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
> >  void workingset_update_node(struct xa_node *node);
> >  extern struct list_lru shadow_nodes;
> >
> > +/**
> > + * __kstrndup - Create a NUL-terminated string from @s, which might be unterminated.
> > + * @s: The data to stringify
> > + * @len: The size of the data, including the null terminator
> > + * @gfp: the GFP mask used in the kmalloc() call when allocating memory
> > + *
> > + * Return: newly allocated copy of @s with NUL-termination or %NULL in
> > + * case of error
> > + */
> > +static __always_inline char *__kstrndup(const char *s, size_t len, gfp_t gfp)
> > +{
> > +     char *buf;
> > +
> > +     buf = kmalloc_track_caller(len, gfp);
> > +     if (!buf)
> > +             return NULL;
> > +
> > +     memcpy(buf, s, len);
> > +     /* Ensure the buf is always NUL-terminated, regardless of @s. */
> > +     buf[len - 1] = '\0';
> > +     return buf;
> > +}
> > +
> > +
>
> nit: One blank line is enough.

Ah, will change it.

>
> >  #endif       /* __MM_INTERNAL_H */
> > diff --git a/mm/util.c b/mm/util.c
> > index 41c7875572ed..d9135c5fdf7f 100644
> > --- a/mm/util.c
> > +++ b/mm/util.c
> > @@ -58,17 +58,8 @@ char *kstrdup(const char *s, gfp_t gfp)
> >       if (!s)
> >               return NULL;
> >
> > -     len = strlen(s) + 1;
> > -     buf = kmalloc_track_caller(len, gfp);
> > -     if (buf) {
> > -             memcpy(buf, s, len);
> > -             /* During memcpy(), the string might be updated to a new value,
> > -              * which could be longer than the string when strlen() is
> > -              * called. Therefore, we need to add a null termimator.
> > -              */
> > -             buf[len - 1] = '\0';
> > -     }
> > -     return buf;
>
> nit: The local variable buf is now unused, and should be removed from kstrdup().
>      Likewise for kstrndup() and kmemdup_nul()
>
>      Flagged by W=1 builds with gcc-13 and clang-18, and Smatch.

I missed that. Thanks for pointing it out.
Yafang Shao June 23, 2024, 2:29 a.m. UTC | #4
On Fri, Jun 21, 2024 at 9:57 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Fri, Jun 21, 2024 at 10:29:54AM +0800, Yafang Shao wrote:
> > +++ b/mm/internal.h
>
> Why are you putting __kstrndup in a header file when it's only used
> in util.c?

I want to make it always inlined. However, it is not recommended to
define an inline function in a .c file, right ?

>
> Also, I think this function is actually __kmemdup_nul(), not
> __kstrndup().
>

Good suggestion. Will use __kmemdup_nul() instead.
Matthew Wilcox June 23, 2024, 3:11 a.m. UTC | #5
On Sun, Jun 23, 2024 at 10:29:30AM +0800, Yafang Shao wrote:
> On Fri, Jun 21, 2024 at 9:57 PM Matthew Wilcox <willy@infradead.org> wrote:
> >
> > On Fri, Jun 21, 2024 at 10:29:54AM +0800, Yafang Shao wrote:
> > > +++ b/mm/internal.h
> >
> > Why are you putting __kstrndup in a header file when it's only used
> > in util.c?
> 
> I want to make it always inlined. However, it is not recommended to
> define an inline function in a .c file, right ?

I'm not aware of any such recommendation.  Better than putting it in
a .h file that everybody has to look at but nobody uses.
Yafang Shao June 23, 2024, 6 a.m. UTC | #6
On Sun, Jun 23, 2024 at 11:11 AM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Sun, Jun 23, 2024 at 10:29:30AM +0800, Yafang Shao wrote:
> > On Fri, Jun 21, 2024 at 9:57 PM Matthew Wilcox <willy@infradead.org> wrote:
> > >
> > > On Fri, Jun 21, 2024 at 10:29:54AM +0800, Yafang Shao wrote:
> > > > +++ b/mm/internal.h
> > >
> > > Why are you putting __kstrndup in a header file when it's only used
> > > in util.c?
> >
> > I want to make it always inlined. However, it is not recommended to
> > define an inline function in a .c file, right ?
>
> I'm not aware of any such recommendation.  Better than putting it in
> a .h file that everybody has to look at but nobody uses.

Understood.
Will change it.
diff mbox series

Patch

diff --git a/mm/internal.h b/mm/internal.h
index b2c75b12014e..fd87f685739b 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1521,4 +1521,28 @@  static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
 void workingset_update_node(struct xa_node *node);
 extern struct list_lru shadow_nodes;
 
+/**
+ * __kstrndup - Create a NUL-terminated string from @s, which might be unterminated.
+ * @s: The data to stringify
+ * @len: The size of the data, including the null terminator
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Return: newly allocated copy of @s with NUL-termination or %NULL in
+ * case of error
+ */
+static __always_inline char *__kstrndup(const char *s, size_t len, gfp_t gfp)
+{
+	char *buf;
+
+	buf = kmalloc_track_caller(len, gfp);
+	if (!buf)
+		return NULL;
+
+	memcpy(buf, s, len);
+	/* Ensure the buf is always NUL-terminated, regardless of @s. */
+	buf[len - 1] = '\0';
+	return buf;
+}
+
+
 #endif	/* __MM_INTERNAL_H */
diff --git a/mm/util.c b/mm/util.c
index 41c7875572ed..d9135c5fdf7f 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -58,17 +58,8 @@  char *kstrdup(const char *s, gfp_t gfp)
 	if (!s)
 		return NULL;
 
-	len = strlen(s) + 1;
-	buf = kmalloc_track_caller(len, gfp);
-	if (buf) {
-		memcpy(buf, s, len);
-		/* During memcpy(), the string might be updated to a new value,
-		 * which could be longer than the string when strlen() is
-		 * called. Therefore, we need to add a null termimator.
-		 */
-		buf[len - 1] = '\0';
-	}
-	return buf;
+	len = strlen(s);
+	return __kstrndup(s, len + 1, gfp);
 }
 EXPORT_SYMBOL(kstrdup);
 
@@ -111,12 +102,7 @@  char *kstrndup(const char *s, size_t max, gfp_t gfp)
 		return NULL;
 
 	len = strnlen(s, max);
-	buf = kmalloc_track_caller(len+1, gfp);
-	if (buf) {
-		memcpy(buf, s, len);
-		buf[len] = '\0';
-	}
-	return buf;
+	return __kstrndup(s, len + 1, gfp);
 }
 EXPORT_SYMBOL(kstrndup);
 
@@ -195,12 +181,7 @@  char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
 	if (!s)
 		return NULL;
 
-	buf = kmalloc_track_caller(len + 1, gfp);
-	if (buf) {
-		memcpy(buf, s, len);
-		buf[len] = '\0';
-	}
-	return buf;
+	return __kstrndup(s, len + 1, gfp);
 }
 EXPORT_SYMBOL(kmemdup_nul);