* [PATCH] gpu/buddy: add __counted_by_ptr attribute to roots
@ 2026-09-25 20:44 Bill Wendling
2026-09-28 12:20 ` Matthew Auld
0 siblings, 1 reply; 4+ messages in thread
From: Bill Wendling @ 2026-09-25 20:44 UTC (permalink / raw)
To: Matthew Auld, Arun Pravin
Cc: Joel Fernandes, Kees Cook, Gustavo A. R. Silva, dri-devel,
linux-kernel, linux-hardening, Bill Wendling,
codemender-patching+linux
In 'struct gpu_buddy', the 'roots' field points to an array of pointers
to 'struct gpu_buddy_block'. The number of allocated roots is tracked
by the 'n_roots' field.
Annotate the 'roots' pointer with the '__counted_by_ptr' attribute
referencing 'n_roots' to enable compile-time and runtime bounds-checking
via KASAN and '__builtin_dynamic_object_size'.
In 'gpu_buddy_init', 'mm->n_roots' is initialized first, and 'mm->roots' is
subsequently allocated with 'kmalloc_objs(struct gpu_buddy_block *,
mm->n_roots)'.
Since the bounds associated with 'roots' ('n_roots') are fully set prior
to any array allocation or access and remain invariant, this annotation
will not cause runtime panics or false-positive bounds checks.
Cc: codemender-patching+linux@google.com
Assisted-by: LLM
Signed-off-by: Bill Wendling <morbo@google.com>
---
include/linux/gpu_buddy.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/gpu_buddy.h b/include/linux/gpu_buddy.h
index 2c36124bb696..d7249e500ab6 100644
--- a/include/linux/gpu_buddy.h
+++ b/include/linux/gpu_buddy.h
@@ -172,7 +172,7 @@ struct gpu_buddy {
* a power of two, with each root being the largest power-of-two
* that fits in the remaining space.
*/
- struct gpu_buddy_block **roots;
+ struct gpu_buddy_block **roots __counted_by_ptr(n_roots);
/*
* Per-order free block scoreboard: free_scoreboard[order] holds the
* number of blocks of that order currently in the free state.
--
2.56.0.rc1.315.gc6ed9934b7-goog
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] gpu/buddy: add __counted_by_ptr attribute to roots
2026-09-25 20:44 [PATCH] gpu/buddy: add __counted_by_ptr attribute to roots Bill Wendling
@ 2026-09-28 12:20 ` Matthew Auld
2026-09-28 18:44 ` Bill Wendling
0 siblings, 1 reply; 4+ messages in thread
From: Matthew Auld @ 2026-09-28 12:20 UTC (permalink / raw)
To: Bill Wendling, Arun Pravin
Cc: Joel Fernandes, Kees Cook, Gustavo A. R. Silva, dri-devel,
linux-kernel, linux-hardening, codemender-patching+linux
On 25/09/2026 21:44, Bill Wendling wrote:
> In 'struct gpu_buddy', the 'roots' field points to an array of pointers
> to 'struct gpu_buddy_block'. The number of allocated roots is tracked
> by the 'n_roots' field.
>
> Annotate the 'roots' pointer with the '__counted_by_ptr' attribute
> referencing 'n_roots' to enable compile-time and runtime bounds-checking
> via KASAN and '__builtin_dynamic_object_size'.
>
> In 'gpu_buddy_init', 'mm->n_roots' is initialized first, and 'mm->roots' is
> subsequently allocated with 'kmalloc_objs(struct gpu_buddy_block *,
> mm->n_roots)'.
>
> Since the bounds associated with 'roots' ('n_roots') are fully set prior
> to any array allocation or access and remain invariant, this annotation
> will not cause runtime panics or false-positive bounds checks.
>
> Cc: codemender-patching+linux@google.com
> Assisted-by: LLM
> Signed-off-by: Bill Wendling <morbo@google.com>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Would it make sense to also do this for *_scoreboard and free_tree, if
we add a new field n_orders?
> ---
> include/linux/gpu_buddy.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/gpu_buddy.h b/include/linux/gpu_buddy.h
> index 2c36124bb696..d7249e500ab6 100644
> --- a/include/linux/gpu_buddy.h
> +++ b/include/linux/gpu_buddy.h
> @@ -172,7 +172,7 @@ struct gpu_buddy {
> * a power of two, with each root being the largest power-of-two
> * that fits in the remaining space.
> */
> - struct gpu_buddy_block **roots;
> + struct gpu_buddy_block **roots __counted_by_ptr(n_roots);
> /*
> * Per-order free block scoreboard: free_scoreboard[order] holds the
> * number of blocks of that order currently in the free state.
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] gpu/buddy: add __counted_by_ptr attribute to roots
2026-09-28 12:20 ` Matthew Auld
@ 2026-09-28 18:44 ` Bill Wendling
2026-09-29 8:36 ` Matthew Auld
0 siblings, 1 reply; 4+ messages in thread
From: Bill Wendling @ 2026-09-28 18:44 UTC (permalink / raw)
To: Matthew Auld
Cc: Arun Pravin, Joel Fernandes, Kees Cook, Gustavo A. R. Silva,
dri-devel, linux-kernel, linux-hardening,
codemender-patching+linux
On Mon, Sep 28, 2026 at 5:20 AM Matthew Auld <matthew.auld@intel.com> wrote:
>
> On 25/09/2026 21:44, Bill Wendling wrote:
> > In 'struct gpu_buddy', the 'roots' field points to an array of pointers
> > to 'struct gpu_buddy_block'. The number of allocated roots is tracked
> > by the 'n_roots' field.
> >
> > Annotate the 'roots' pointer with the '__counted_by_ptr' attribute
> > referencing 'n_roots' to enable compile-time and runtime bounds-checking
> > via KASAN and '__builtin_dynamic_object_size'.
> >
> > In 'gpu_buddy_init', 'mm->n_roots' is initialized first, and 'mm->roots' is
> > subsequently allocated with 'kmalloc_objs(struct gpu_buddy_block *,
> > mm->n_roots)'.
> >
> > Since the bounds associated with 'roots' ('n_roots') are fully set prior
> > to any array allocation or access and remain invariant, this annotation
> > will not cause runtime panics or false-positive bounds checks.
> >
> > Cc: codemender-patching+linux@google.com
> > Assisted-by: LLM
> > Signed-off-by: Bill Wendling <morbo@google.com>
>
> Reviewed-by: Matthew Auld <matthew.auld@intel.com>
>
> Would it make sense to also do this for *_scoreboard and free_tree, if
> we add a new field n_orders?
>
Sure! There's already a 'max_order'. I assume that 'n_orders' would be
more dynamic?
-bw
> > ---
> > include/linux/gpu_buddy.h | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/include/linux/gpu_buddy.h b/include/linux/gpu_buddy.h
> > index 2c36124bb696..d7249e500ab6 100644
> > --- a/include/linux/gpu_buddy.h
> > +++ b/include/linux/gpu_buddy.h
> > @@ -172,7 +172,7 @@ struct gpu_buddy {
> > * a power of two, with each root being the largest power-of-two
> > * that fits in the remaining space.
> > */
> > - struct gpu_buddy_block **roots;
> > + struct gpu_buddy_block **roots __counted_by_ptr(n_roots);
> > /*
> > * Per-order free block scoreboard: free_scoreboard[order] holds the
> > * number of blocks of that order currently in the free state.
>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] gpu/buddy: add __counted_by_ptr attribute to roots
2026-09-28 18:44 ` Bill Wendling
@ 2026-09-29 8:36 ` Matthew Auld
0 siblings, 0 replies; 4+ messages in thread
From: Matthew Auld @ 2026-09-29 8:36 UTC (permalink / raw)
To: Bill Wendling
Cc: Arun Pravin, Joel Fernandes, Kees Cook, Gustavo A. R. Silva,
dri-devel, linux-kernel, linux-hardening,
codemender-patching+linux
On 28/09/2026 19:44, Bill Wendling wrote:
> On Mon, Sep 28, 2026 at 5:20 AM Matthew Auld <matthew.auld@intel.com> wrote:
>>
>> On 25/09/2026 21:44, Bill Wendling wrote:
>>> In 'struct gpu_buddy', the 'roots' field points to an array of pointers
>>> to 'struct gpu_buddy_block'. The number of allocated roots is tracked
>>> by the 'n_roots' field.
>>>
>>> Annotate the 'roots' pointer with the '__counted_by_ptr' attribute
>>> referencing 'n_roots' to enable compile-time and runtime bounds-checking
>>> via KASAN and '__builtin_dynamic_object_size'.
>>>
>>> In 'gpu_buddy_init', 'mm->n_roots' is initialized first, and 'mm->roots' is
>>> subsequently allocated with 'kmalloc_objs(struct gpu_buddy_block *,
>>> mm->n_roots)'.
>>>
>>> Since the bounds associated with 'roots' ('n_roots') are fully set prior
>>> to any array allocation or access and remain invariant, this annotation
>>> will not cause runtime panics or false-positive bounds checks.
>>>
>>> Cc: codemender-patching+linux@google.com
>>> Assisted-by: LLM
>>> Signed-off-by: Bill Wendling <morbo@google.com>
>>
>> Reviewed-by: Matthew Auld <matthew.auld@intel.com>
>>
>> Would it make sense to also do this for *_scoreboard and free_tree, if
>> we add a new field n_orders?
>>
> Sure! There's already a 'max_order'. I assume that 'n_orders' would be
> more dynamic?
Yeah, I think just mm.n_orders = max_order + 1. I assume the
__counted_by_ptr() only works with a field?
>
> -bw
>
>>> ---
>>> include/linux/gpu_buddy.h | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/include/linux/gpu_buddy.h b/include/linux/gpu_buddy.h
>>> index 2c36124bb696..d7249e500ab6 100644
>>> --- a/include/linux/gpu_buddy.h
>>> +++ b/include/linux/gpu_buddy.h
>>> @@ -172,7 +172,7 @@ struct gpu_buddy {
>>> * a power of two, with each root being the largest power-of-two
>>> * that fits in the remaining space.
>>> */
>>> - struct gpu_buddy_block **roots;
>>> + struct gpu_buddy_block **roots __counted_by_ptr(n_roots);
>>> /*
>>> * Per-order free block scoreboard: free_scoreboard[order] holds the
>>> * number of blocks of that order currently in the free state.
>>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-09-29 8:36 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-25 20:44 [PATCH] gpu/buddy: add __counted_by_ptr attribute to roots Bill Wendling
2026-09-28 12:20 ` Matthew Auld
2026-09-28 18:44 ` Bill Wendling
2026-09-29 8:36 ` Matthew Auld
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®