mm/slab: call __kmalloc_noprof() when size isn't builtin constant in kmalloc_array_noprof()
Subsystems (2)
PATCH 1/1 mm/slab: call __kmalloc_noprof() when size isn't builtin constant in kmalloc_array_noprof()
From: Ye Bin <[email protected]>
If 'n' or 'size' isn't builtin constant just call __kmalloc_noprof() in kmalloc_array_noprof().
--- include/linux/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +1 -1
@@ -947,7 +947,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t sizreturn NULL ;if (__builtin_constant_p (n) &&__builtin_constant_p (size))return kmalloc_noprof (bytes, flags); -return kmalloc_noprof (bytes, flags); +return __kmalloc_noprof (bytes, flags); }#define kmalloc_array (...) alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))
Signature · 1 line
2.34.1
Quoted 16 lines · Ye Bin
From: Ye Bin <[email protected]>
If 'n' or 'size' isn't builtin constant just call
__kmalloc_noprof()inkmalloc_array_noprof().Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")Signed-off-by Ye Bin <[email protected]>--- include/linux/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/slab.h +0
-0
include/linux/slab.h static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz · line 950
Quoted 5 lines
include/linux/slab.h +1
-1@@ -947,7 +947,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t sizreturn NULL ;if (__builtin_constant_p (n) &&__builtin_constant_p (size))return kmalloc_noprof (bytes, flags); -return kmalloc_noprof (bytes, flags); +return __kmalloc_noprof (bytes, flags);
Can we simply call kmalloc_noprof(bytes, flags) once instead?
The compiler should know 'bytes' is a constant if both 'n' and 'size' are constants.
Quoted 5 lines
include/linux/slab.h +0
-0}#define kmalloc_array (...) alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))Signature · 1 line
2.34.1
Signature · 2 lines
Cheers, Harry
Quoted 25 lines · Harry Yoo
Quoted 16 lines · Ye Bin
From: Ye Bin <[email protected]>
If 'n' or 'size' isn't builtin constant just call
__kmalloc_noprof()inkmalloc_array_noprof().Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")Signed-off-by Ye Bin <[email protected]>--- include/linux/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/slab.h +0
-0include/linux/slab.h static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz · line 950
Quoted 5 lines
include/linux/slab.h +1
-1@@ -947,7 +947,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t sizreturn NULL ;if (__builtin_constant_p (n) &&__builtin_constant_p (size))return kmalloc_noprof (bytes, flags); -return kmalloc_noprof (bytes, flags); +return __kmalloc_noprof (bytes, flags);Can we simply call
kmalloc_noprof(bytes, flags)once instead?
Quoted 2 lines
The compiler should know 'bytes' is a constant if both 'n' and 'size' are constants.
Right, applied like this, hope it's ok?
commit a6553ee4b00ba4b1466a81fb00fa4679b7ce5339 (HEAD -> slab/for-6.15/fixes-cleanups)
Author: Ye Bin <[email protected]> Date: Sat Mar 1 16:37:20 2025 +0800
mm/slab: call kmalloc_noprof() unconditionally in kmalloc_array_noprof()
If 'n' or 'size' isn't builtin constant, we used to call __kmalloc() before commit 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends"), which inadvertedly changed both paths to kmalloc_noprof().
As Harry Yoo points out we can just call kmalloc_noprof() unconditionally. If the compiler knows n and size are constants it doesn't guarantee that bytes will be also seen as constant, and that is the important test in kmalloc_noprof() here, so we can just defer to it always.
[ [email protected]: change as Harry suggested and adjust commit log ]
include/linux/slab.h +0 -2
@@ -941,8 +941,6 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t sizif (unlikely (check_mul_overflow (n, size, &bytes)))return NULL ; -if (__builtin_constant_p (n) &&__builtin_constant_p (size)) -return kmalloc_noprof (bytes, flags);return kmalloc_noprof (bytes, flags); }#define kmalloc_array (...) alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))
Harry Yoo
Reviewed-byQuoted 49 lines · Vlastimil Babka
Quoted 25 lines · Harry Yoo
Quoted 16 lines · Ye Bin
From: Ye Bin <[email protected]>
If 'n' or 'size' isn't builtin constant just call
__kmalloc_noprof()inkmalloc_array_noprof().Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")Signed-off-by Ye Bin <[email protected]>--- include/linux/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/slab.h +0
-0include/linux/slab.h static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz · line 950
Quoted 5 lines
include/linux/slab.h +1
-1@@ -947,7 +947,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t sizreturn NULL ;if (__builtin_constant_p (n) &&__builtin_constant_p (size))return kmalloc_noprof (bytes, flags); -return kmalloc_noprof (bytes, flags); +return __kmalloc_noprof (bytes, flags);Can we simply call
kmalloc_noprof(bytes, flags)once instead?Quoted 2 lines
The compiler should know 'bytes' is a constant if both 'n' and 'size' are constants.
Right, applied like this, hope it's ok?
commit
a6553ee4b00ba4b1466a81fb00fa4679b7ce5339(HEAD -> slab/for-6.15/fixes-cleanups)Author: Ye Bin <[email protected]> Date: Sat Mar 1 16:37:20 2025 +0800mm/slab: call
kmalloc_noprof()unconditionally inkmalloc_array_noprof()If 'n' or 'size' isn't builtin constant, we used to call
__kmalloc()before commit7bd230a26648("mm/slab: enable slab allocation tagging for kmalloc and friends"), which inadvertedly changed both paths tokmalloc_noprof().As Harry Yoo points out we can just call
kmalloc_noprof()unconditionally. If the compiler knows n and size are constants it doesn't guarantee that bytes will be also seen as constant, and that is the important test inkmalloc_noprof()here, so we can just defer to it always.
Quoted 5 lines
[ [email protected]: change as Harry suggested and adjust commit log ]
Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")Signed-off-by Ye Bin <[email protected]>Signed-off-by Vlastimil Babka <[email protected]>
Looks good to me, thanks!
FWIW,
Signature · 2 lines
Cheers, Harry
Quoted 14 lines
include/linux/slab.h +0
-2@@ -941,8 +941,6 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t sizif (unlikely (check_mul_overflow (n, size, &bytes)))return NULL ; -if (__builtin_constant_p (n) &&__builtin_constant_p (size)) -return kmalloc_noprof (bytes, flags);return kmalloc_noprof (bytes, flags); }#define kmalloc_array (...) alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))