mm/util: Use kmalloc buckets for kmemdup_nul()
Subsystems (2)
PATCH 1/1 mm/util: Use kmalloc buckets for kmemdup_nul()
The use of the kmemdup_nul()-family of allocations are explicitly for allocating NUL terminated strings, so these would be best separated from typed allocations, as they are their own set of arbitrarily sized allocations. They are not as risky as userspace controlled allocations, but these would be good to separate as well.
# grep memdup_nul /proc/slabinfo | cut -c-25 memdup_nul-8k 0 memdup_nul-4k 0 memdup_nul-2k 0 memdup_nul-1k 0 memdup_nul-512 28 memdup_nul-256 0 memdup_nul-192 60 memdup_nul-128 60 memdup_nul-96 60 memdup_nul-64 180 memdup_nul-32 960 memdup_nul-16 1860 memdup_nul-8 1980
---
Cc: Vlastimil Babka <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Lorenzo Stoakes <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: <[email protected]>
--- mm/util.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
mm/util.c +7 -5
@@ -34,6 +34,9 @@#include "internal.h" #include "swap.h" +static kmem_buckets *user_buckets__ro_after_init ; +static kmem_buckets *nul_buckets__ro_after_init ; +/** * kfree_const - conditionally free memory * @x: pointer to the memory
@@ -61,7 +64,7 @@ static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp)char *buf;/* '+1' for the NUL terminator */ - buf =kmalloc_track_caller (len +1 , gfp); + buf =kmem_buckets_alloc_track_caller (nul_buckets, len +1 , gfp);if (!buf)return NULL ;
@@ -195,15 +198,14 @@ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) }EXPORT_SYMBOL (kmemdup_nul); -static kmem_buckets *user_buckets__ro_after_init ; - -static int __init init_user_buckets (void ) +static int __init init_buckets (void ) { user_buckets =kmem_buckets_create ("memdup_user" ,0 ,0 ,INT_MAX ,NULL ); + nul_buckets =kmem_buckets_create ("memdup_nul" ,0 ,0 ,INT_MAX ,NULL );return 0 ; } -subsys_initcall (init_user_buckets); +subsys_initcall (init_buckets);/** * memdup_user - duplicate memory region from user space
Signature · 1 line
2.34.1
Quoted 24 lines · Kees Cook
The use of the kmemdup_nul()-family of allocations are explicitly for allocating NUL terminated strings, so these would be best separated from typed allocations, as they are their own set of arbitrarily sized allocations. They are not as risky as userspace controlled allocations, but these would be good to separate as well.
# grep memdup_nul /proc/slabinfo | cut -c-25 memdup_nul-8k 0 memdup_nul-4k 0 memdup_nul-2k 0 memdup_nul-1k 0 memdup_nul-512 28 memdup_nul-256 0 memdup_nul-192 60 memdup_nul-128 60 memdup_nul-96 60 memdup_nul-64 180 memdup_nul-32 960 memdup_nul-16 1860 memdup_nul-8 1980Suggested-by Harry Yoo <[email protected]>Signed-off-by Kees Cook <[email protected]>
Quoted 11 lines
---
Cc: Vlastimil Babka <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Lorenzo Stoakes <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: <[email protected]>
Quoted 49 lines
--- mm/util.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)mm/util.c +7
-6@@ -34,6 +34,9 @@#include "internal.h" #include "swap.h" +static kmem_buckets *user_buckets__ro_after_init ; +static kmem_buckets *nul_buckets__ro_after_init ; +/** * kfree_const - conditionally free memory * @x: pointer to the memory @@ -61,7 +64,7 @@ static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp)char *buf;/* '+1' for the NUL terminator */ - buf =kmalloc_track_caller (len +1 , gfp); + buf =kmem_buckets_alloc_track_caller (nul_buckets, len +1 , gfp);if (!buf)return NULL ;@@ -195,15 +198,14 @@ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) }EXPORT_SYMBOL (kmemdup_nul); -static kmem_buckets *user_buckets__ro_after_init ; - -static int __init init_user_buckets (void ) +static int __init init_buckets (void ) { user_buckets =kmem_buckets_create ("memdup_user" ,0 ,0 ,INT_MAX ,NULL ); + nul_buckets =kmem_buckets_create ("memdup_nul" ,0 ,0 ,INT_MAX ,NULL );return 0 ; } -subsys_initcall (init_user_buckets); +subsys_initcall (init_buckets);/** * memdup_user - duplicate memory region from user space -- 2.34.1
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Kees,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Kees-Cook/mm-util-Use-kmalloc-buckets-for-kmemdup_nul/20260521-204404 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20260521124026.work.036-kees%40kernel.org patch subject: [PATCH] mm/util: Use kmalloc buckets for kmemdup_nul() config: s390-allnoconfig (https://download.01.org/0day-ci/archive/20260522/[email protected]/config) compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project 5bac06718f502014fade905512f1d26d578a18f3) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260522/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
mm/util.c:37:22: warning: variable 'user_buckets' set but not used [-Wunused-but-set-global]
37 | static kmem_buckets *user_buckets __ro_after_init;
| ^
>> mm/util.c:38:22: warning: variable 'nul_buckets' set but not used [-Wunused-but-set-global]
38 | static kmem_buckets *nul_buckets __ro_after_init;
| ^2 warnings generated.
vim +/nul_buckets +38 mm/util.c
36
> 37 static kmem_buckets *user_buckets __ro_after_init;
> 38 static kmem_buckets *nul_buckets __ro_after_init;
39 --
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki