* [PATCH v2] slab: fix the kernel-doc of kmalloc_flex() on counter overflow
@ 2026-11-29 6:04 Arina Kharlamova via B4 Relay
2026-11-29 7:40 ` Manuel Ebner
0 siblings, 1 reply; 2+ messages in thread
From: Arina Kharlamova via B4 Relay @ 2026-11-29 6:04 UTC (permalink / raw)
To: Jonathan Corbet, Shuah Khan, Randy Dunlap, Kees Cook,
Gustavo A. R. Silva, Vlastimil Babka, Harry Yoo, Hao Li,
Christoph Lameter, David Rientjes, Roman Gushchin
Cc: workflows, linux-doc, linux-kernel, linux-hardening, linux-mm,
Arina Kharlamova
From: Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
Commit fa5c82f4d2bb ("slab.h: disable completely broken overflow handling
in flex allocations") removed the counter overflow check from
__alloc_flex(), but the kernel-doc and deprecated.rst still say that the
allocation fails if COUNT does not fit in the __counted_by() counter.
Update them to match the code. While at it, fix the Returns: line of
overflows_flex_counter_type(), where true and false are swapped.
Fixes: fa5c82f4d2bb ("slab.h: disable completely broken overflow handling in flex allocations")
Signed-off-by: Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
---
Found by checks derived from the spec2patch kernel API specifications,
with the help of an AI coding assistant (Claude); I reviewed the
analysis and the fix.
---
Changes in v2:
- Shorten the changelog and the comments (Harry Yoo)
- Rebase on slab/for-next
- Link to v1: https://patch.msgid.link/20261124-slab-doc-v1-1-9f14dfce47c8@mbzuai.ac.ae
---
Documentation/process/deprecated.rst | 7 +++----
include/linux/overflow.h | 5 +++--
include/linux/slab.h | 10 ++++------
3 files changed, 10 insertions(+), 12 deletions(-)
diff --git a/Documentation/process/deprecated.rst b/Documentation/process/deprecated.rst
index 22a5e62c92ea..ea392cf36fb8 100644
--- a/Documentation/process/deprecated.rst
+++ b/Documentation/process/deprecated.rst
@@ -405,7 +405,6 @@ become, respectively::
__auto_type ptr = kmalloc_obj(struct foo [, gfp] );
The argument gfp is optional, the default value is GFP_KERNEL.
-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`.
+If `ptr->flex_member` is annotated with __counted_by(), the caller must
+make sure that `count` fits in the counter member associated with
+`flex_member`.
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index d0f04436343e..2eb5490c2f2c 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -566,8 +566,9 @@ static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtr
* @FAM: Member name of the FAM within @TYPE.
* @COUNT: Value to check against the __counted_by annotated @FAM's counter.
*
- * Returns: true if @COUNT can be represented in the @FAM's counter. When
- * @FAM is not annotated with __counted_by(), always returns true.
+ * Returns: true if @COUNT cannot be represented in the @FAM's counter.
+ * When @FAM is not annotated with __counted_by(), @COUNT is checked
+ * against size_t.
*/
#define overflows_flex_counter_type(TYPE, FAM, COUNT) \
(overflows_type(COUNT, typeof_flex_counter(((TYPE *)NULL)->FAM)))
diff --git a/include/linux/slab.h b/include/linux/slab.h
index ad645967a3ee..5d67144c5a81 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -1092,9 +1092,8 @@ void *kmalloc_nolock(size_t size, gfp_t gfp_flags, int node);
* @COUNT: how many @FAM elements to allocate space for.
*
* Returns: Newly allocated pointer to @TYPE with @COUNT-many trailing
- * @FAM elements, or NULL on failure or if @COUNT cannot be represented
- * by the member of @TYPE that counts the @FAM elements (annotated via
- * __counted_by()).
+ * @FAM elements, or NULL on failure. @COUNT is not checked against the
+ * type of the __counted_by() counter of @FAM.
*/
#define __alloc_flex(KMALLOC, GFP, TYPE, FAM, COUNT) \
({ \
@@ -1137,9 +1136,8 @@ void *kmalloc_nolock(size_t size, gfp_t gfp_flags, int node);
* @...: 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
- * will immediately fail if @COUNT is larger than what the type of the
- * struct's counter variable can represent.
+ * failure. If @FAM has been annotated with __counted_by(), the caller
+ * must make sure that @COUNT fits in the struct's counter variable.
*/
#define kmalloc_flex(VAR_OR_TYPE, FAM, COUNT, ...) \
__alloc_flex(kmalloc, default_gfp(__VA_ARGS__), typeof(VAR_OR_TYPE), FAM, COUNT)
---
base-commit: 0b0985aeb9979c5fa5c3b872cecdf97ed4ed2b3e
change-id: 20261124-slab-doc-1894ca04d006
Best regards,
--
Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] slab: fix the kernel-doc of kmalloc_flex() on counter overflow
2026-11-29 6:04 [PATCH v2] slab: fix the kernel-doc of kmalloc_flex() on counter overflow Arina Kharlamova via B4 Relay
@ 2026-11-29 7:40 ` Manuel Ebner
0 siblings, 0 replies; 2+ messages in thread
From: Manuel Ebner @ 2026-11-29 7:40 UTC (permalink / raw)
To: arina.kharlamova, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva, Vlastimil Babka, Harry Yoo,
Hao Li, Christoph Lameter, David Rientjes, Roman Gushchin
Cc: workflows, linux-doc, linux-kernel, linux-hardening, linux-mm
Hi Arina,
On Sun, 2026-11-29 at 10:04 +0400, Arina Kharlamova via B4 Relay wrote:
> From: Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
>
> Commit fa5c82f4d2bb ("slab.h: disable completely broken overflow handling
> in flex allocations") removed the counter overflow check from
> __alloc_flex(), but the kernel-doc and deprecated.rst still say that the
> allocation fails if COUNT does not fit in the __counted_by() counter.
>
> Update them to match the code. While at it, fix the Returns: line of
> overflows_flex_counter_type(), where true and false are swapped.
>
> Fixes: fa5c82f4d2bb ("slab.h: disable completely broken overflow handling in flex allocations")
> Signed-off-by: Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
> ---
> Found by checks derived from the spec2patch kernel API specifications,
> with the help of an AI coding assistant (Claude); I reviewed the
> analysis and the fix.
> ---
> Changes in v2:
> - Shorten the changelog and the comments (Harry Yoo)
> - Rebase on slab/for-next
> - Link to v1: https://patch.msgid.link/20261124-slab-doc-v1-1-9f14dfce47c8@mbzuai.ac.ae
> ---
> Documentation/process/deprecated.rst | 7 +++----
> include/linux/overflow.h | 5 +++--
> include/linux/slab.h | 10 ++++------
> 3 files changed, 10 insertions(+), 12 deletions(-)
>
> diff --git a/Documentation/process/deprecated.rst b/Documentation/process/deprecated.rst
> index 22a5e62c92ea..ea392cf36fb8 100644
> --- a/Documentation/process/deprecated.rst
> +++ b/Documentation/process/deprecated.rst
> @@ -405,7 +405,6 @@ become, respectively::
> __auto_type ptr = kmalloc_obj(struct foo [, gfp] );
>
> The argument gfp is optional, the default value is GFP_KERNEL.
could you add this diff in you patch?
https://lore.kernel.org/all/20260909141255.1367539-2-manuelebnerli@mailbox.org/
Else looks good to me.
Reviewed-by: Manuel Ebner <manuelebnerli@mailbox.org>
Thanks
Manuel
> -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`.
> +If `ptr->flex_member` is annotated with __counted_by(), the caller must
> +make sure that `count` fits in the counter member associated with
> +`flex_member`.
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
>
> [...]
>
> Best regards,
> --
> Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-29 7:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 6:04 [PATCH v2] slab: fix the kernel-doc of kmalloc_flex() on counter overflow Arina Kharlamova via B4 Relay
2026-11-29 7:40 ` Manuel Ebner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®