Skip to contentSkip to conversation

mm/slab: call __kmalloc_noprof() when size isn't builtin constant in kmalloc_array_noprof()

PATCH In review Ye Bin 3 participants 4 messages Mar 1, 2025 – Mar 4, 2025

Subsystems (2)
lore
Share & tools
lore

Ye Bin

author
#0 lore raw

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().

Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +1 -1
index 3b03b31831a9..0edb8363fc4f 100644
@@ -947,7 +947,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz
 		return 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
#1 lore raw
Quoted 16 lines · Ye Bin

From: Ye Bin <[email protected]>

If 'n' or 'size' isn't builtin constant just call __kmalloc_noprof() in kmalloc_array_noprof().

Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +0 -0
index 3b03b31831a9..0edb8363fc4f 100644

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 siz
 		return 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
#2 lore raw
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() in kmalloc_array_noprof().

Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +0 -0
index 3b03b31831a9..0edb8363fc4f 100644

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 siz
 		return 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 ]

Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")
include/linux/slab.h +0 -2
index 09eedaecf120..ab05a143d09a 100644
@@ -941,8 +941,6 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz
 
        if (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__))
Reviewed-by
#3 lore raw
Quoted 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() in kmalloc_array_noprof().

Fixes 7bd230a26648 ("mm/slab: enable slab allocation tagging for kmalloc and friends")
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +0 -0
index 3b03b31831a9..0edb8363fc4f 100644

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 siz
 		return 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.

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")

Looks good to me, thanks!

FWIW,

Signature · 2 lines
Cheers,
Harry
Quoted 14 lines
include/linux/slab.h +0 -2
index 09eedaecf120..ab05a143d09a 100644
@@ -941,8 +941,6 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz
 
        if (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__))