Backport the one-arg k*alloc_obj() APIs
cover Backport the one-arg k*alloc_obj() APIs
Two of my recent commits failed to build when cherry-picked to 6.18 solely because of their use of kzalloc_obj(size). The two-argument kzalloc_obj(size, gfp) was already backported to 6.18, but not the one-argument "overload".
It seems likely that this is a very common issue. These are core kernel APIs that almost everyone uses.
Thus, this series adds the single-argument version to 6.18, in a backport-friendly way by omitting the flex counter support.
After this is applied, dd015b566d50 and 696c030e1e34 will be clean cherry-picks, so please cherry-pick those afterwards.
Kees Cook (1):
slab: Introduce kmalloc_flex() and family
Linus Torvalds (2):
add default_gfp() helper macro and use it in the new *alloc_obj()
helpers
default_gfp(): avoid using the "newfangled" __VA_OPT__ trick
Randy Dunlap (1):
slab: recognize @GFP parameter as optional in kernel-doc
Documentation/process/deprecated.rst | 7 +++ include/linux/gfp.h | 4 ++ include/linux/slab.h | 78 +++++++++++++++++++++------- 3 files changed, 71 insertions(+), 18 deletions(-)
base-commit: e46dc0adfe39724bcf52cea47b8f9c9aed86a394
Signature · 1 line
2.55.0
PATCH 1/4 slab: Introduce kmalloc_flex() and family
From: Kees Cook <[email protected]>
commit e4c8b46b924eb8de66c6f0accc9cdd0c2e8fa23b upstream.
As done for kmalloc_obj*(), introduce a type-aware allocator for flexible arrays, which may also have "counted_by" annotations:
ptr = kmalloc(struct_size(ptr, flex_member, count), gfp);
becomes:
ptr = kmalloc_flex(*ptr, flex_member, count, gfp);
The internal use of __flex_counter() allows for automatically setting the counter member of a struct's flexible array member when it has been annotated with __counted_by(), avoiding any missed early size initializations while __counted_by() annotations are added to the kernel. Additionally, this also checks for "too large" allocations based on the type size of the counter variable. For example:
if (count > type_max(ptr->flex_counter)) fail...; size = struct_size(ptr, flex_member, count); ptr = kmalloc(size, gfp); if (!ptr) fail...; ptr->flex_counter = count;
becomes (n.b. unchanged from earlier example):
ptr = kmalloc_flex(*ptr, flex_member, count, gfp); if (!ptr) fail...; ptr->flex_counter = count;
Note that manual initialization of the flexible array counter is still required (at some point) after allocation as not all compiler versions support the __counted_by annotation yet. But doing it internally makes sure they cannot be missed when __counted_by _is_ available, meaning that the bounds checker will not trip due to the lack of "early enough" initializations that used to work before enabling the stricter bounds checking. For example:
ptr = kmalloc_flex(*ptr, flex_member, count, gfp); fill(ptr->flex, count); ptr->flex_count = count;
This works correctly before adding a __counted_by annotation (since nothing is checking ptr->flex accesses against ptr->flex_count). After adding the annotation, the bounds sanitizer would trip during fill() because ptr->flex_count wasn't set yet. But with kmalloc_flex() setting ptr->flex_count internally at allocation time, the existing code works without needing to move the ptr->flex_count assignment before the call to fill(). (This has been a stumbling block for __counted_by adoption.)
[Backport-notes: Removed the actual flex counter handling. That's a new
feature, which isn't necessary for just adding the new allocation APIs to get backports to apply cleanly. Also, the allocation-time overflow check in the upstream commit was reverted upstream.]
--- Documentation/process/deprecated.rst | 7 +++++ include/linux/slab.h | 42 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+)
Documentation/process/deprecated.rst +7 -0
@@ -387,6 +387,7 @@ allocations. For example, these open coded assignments:: ptr = kzalloc(sizeof(*ptr), gfp); ptr = kmalloc_array(count, sizeof(*ptr), gfp); ptr = kcalloc(count, sizeof(*ptr), gfp); + ptr = kmalloc(struct_size(ptr, flex_member, count), gfp); ptr = kmalloc(sizeof(struct foo, gfp); become, respectively::
@@ -395,4 +396,10 @@ become, respectively:: ptr = kzalloc_obj(*ptr, gfp); ptr = kmalloc_objs(*ptr, count, gfp); ptr = kzalloc_objs(*ptr, count, gfp); + ptr = kmalloc_flex(*ptr, flex_member, count, gfp); __auto_type ptr = kmalloc_obj(struct foo, gfp); + +If `ptr->flex_member` is annotated with __counted_by(), the allocation +will automatically fail if `count` is larger than the maximum +representable value that can be stored in the counter member associated +with `flex_member`.
include/linux/slab.h +42 -0
71 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 2/4 add default_gfp() helper macro and use it in the new *alloc_obj() helpers
In reply to Eric BiggersFrom: Linus Torvalds <[email protected]>
commit e19e1b480ac73c3e62ffebbca1174f0f511f43e7 upstream.
Most simple allocations use GFP_KERNEL, and with the new allocation helpers being introduced, let's just take advantage of that to simplify that default case.
It's a numbers game:
git grep 'alloc_obj(' |
sed 's/.*\(GFP_[_A-Z]*\).*/\1/' |
sort | uniq -c | sort -n | tail
shows that about 90% of all those new allocator instances just use that standard GFP_KERNEL.
Those helpers are already macros, and we can easily just make it be the default case when the gfp argument is missing.
And yes, we could do that for all the legacy interfaces too, but let's keep it to just the new ones at least for now, since those all got converted recently anyway, so this is not any "extra" noise outside of that limited conversion.
And, in fact, I want to do this before doing the -rc1 release, exactly so that we don't get extra merge conflicts.
--- include/linux/gfp.h | 4 ++++ include/linux/slab.h | 48 ++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 24 deletions(-)
include/linux/gfp.h +4 -0
@@ -13,6 +13,10 @@struct vm_area_struct ;struct mempolicy ; +/* Helper macro to avoid gfp flags if they are the default one */ +#define __default_gfp (a,...) a +#define default_gfp (...) __default_gfp(__VA_ARGS__ __VA_OPT__(,) GFP_KERNEL) +/* Convert GFP flags to their corresponding migrate type */ #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)#define GFP_MOVABLE_SHIFT 3
include/linux/slab.h +24 -24
72 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 3/4 default_gfp(): avoid using the "newfangled" __VA_OPT__ trick
In reply to Eric BiggersFrom: Linus Torvalds <[email protected]>
commit 551d44200152cb26f75d2ef990aeb6185b7e37fd upstream.
The default_gfp() helper that I added is not wrong, but it turns out that it causes unnecessary headaches for 'sparse' which doesn't support the use of __VA_OPT__ (introduced in C++20 and C23, and supported by gcc and clang for a long time).
We do already use __VA_OPT__ in some other cases in the kernel (drm/xe and btrfs), but it has been fairly limited. Now it triggers for pretty much everything, and sparse ends up not working at all.
We can use the traditional gcc ',##__VA_ARGS__' syntax instead: it may not be the "C standard" way and is slightly less natural in this context, but it is the traditional model for this and avoids the sparse problem.
--- include/linux/gfp.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/gfp.h +2 -2
@@ -14,8 +14,8 @@ struct vm_area_struct;struct mempolicy ;/* Helper macro to avoid gfp flags if they are the default one */ -#define __default_gfp (a,...) a -#define default_gfp (...) __default_gfp(__VA_ARGS__ __VA_OPT__(,) GFP_KERNEL) +#define __default_gfp (a,b,...) b +#define default_gfp (...) __default_gfp(,##__VA_ARGS__,GFP_KERNEL)/* Convert GFP flags to their corresponding migrate type */ #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
Signature · 1 line
2.55.0
PATCH 4/4 slab: recognize @GFP parameter as optional in kernel-doc
In reply to Eric BiggersFrom: Randy Dunlap <[email protected]>
commit 7b5f5865fb11e60edd03c5e063e2d228b7062317 upstream.
Since the @GFP parameter in kmalloc_obj() etc. is now optional, change the kernel-doc to indicate that it is optional. This avoids kernel-doc warnings:
WARNING: include/linux/slab.h:1101 Excess function parameter 'GFP' description in 'kmalloc_obj' WARNING: include/linux/slab.h:1113 Excess function parameter 'GFP' description in 'kmalloc_objs' WARNING: include/linux/slab.h:1128 Excess function parameter 'GFP' description in 'kmalloc_flex'
--- include/linux/slab.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/slab.h +3 -3
@@ -1006,7 +1006,7 @@ void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node);/** * kmalloc_obj - Allocate a single instance of the given type * @VAR_OR_TYPE: Variable or type to allocate. -* @GFP: GFP flags for the allocation. +* @...: optional GFP flags for the allocation (GFP_KERNEL when not specified). * * Returns: newly allocated pointer to a @VAR_OR_TYPE on success, or NULL * on failure.
@@ -1018,7 +1018,7 @@ void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node);* kmalloc_objs - Allocate an array of the given type * @VAR_OR_TYPE: Variable or type to allocate an array of. * @COUNT: How many elements in the array. -* @GFP: GFP flags for the allocation. +* @...: optional GFP flags for the allocation (GFP_KERNEL when not specified). * * Returns: newly allocated pointer to array of @VAR_OR_TYPE on success, * or NULL on failure.
@@ -1031,7 +1031,7 @@ void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node);* @VAR_OR_TYPE: Variable or type to allocate (with its flex array). * @FAM: The name of the flexible array member of the structure. * @COUNT: How many flexible array member elements are desired. -* @GFP: GFP flags for the allocation. +* @...: optional GFP flags for the allocation (GFP_KERNEL when not specified). * * Returns: newly allocated pointer to @VAR_OR_TYPE on success, NULL on * failure. If @FAM has been annotated with __counted_by(), the allocation
Signature · 1 line
2.55.0
Greg KH
maintainerRe: [PATCH 6.18 4/4] slab: recognize @GFP parameter as optional in kernel-doc
Quoted 18 lines · Eric Biggers
From: Randy Dunlap <[email protected]>
commit
7b5f5865fb11e60edd03c5e063e2d228b7062317upstream.Since the @GFP parameter in
kmalloc_obj()etc. is now optional, change the kernel-doc to indicate that it is optional. This avoids kernel-doc warnings:WARNING: include/linux/slab.h:1101 Excess function parameter 'GFP' description in 'kmalloc_obj' WARNING: include/linux/slab.h:1113 Excess function parameter 'GFP' description in 'kmalloc_objs' WARNING: include/linux/slab.h:1128 Excess function parameter 'GFP' description in 'kmalloc_flex'Fixes e19e1b480ac7 ("add default_gfp() helper macro and use it in the new *alloc_obj() helpers")Signed-off-by Randy Dunlap <[email protected]>Signed-off-by Vlastimil Babka (SUSE) <[email protected]>Signed-off-by Eric Biggers <[email protected]>
Quoted 3 lines
--- include/linux/slab.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Also added to 7.1.y to keep in sync, thanks!
greg k-h
Quoted 2 lines · Eric Biggers
After this is applied,
dd015b566d50and696c030e1e34will be clean cherry-picks, so please cherry-pick those afterwards.
Queued both fscrypt follow-ups for 6.18, thanks.
Signature · 2 lines
Thanks, Sasha