gpu/buddy: add __counted_by_ptr attribute to roots
PATCH 1/1 gpu/buddy: add __counted_by_ptr attribute to roots
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.
--- include/linux/gpu_buddy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/gpu_buddy.h +1 -1
@@ -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.
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Quoted 12 lines · Bill Wendling
In 'struct
gpu_buddy', the 'roots' field points to an array of pointers to 'structgpu_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)'.
Quoted 7 lines
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.
Would it make sense to also do this for *_scoreboard and free_tree, if we add a new field n_orders?
Quoted 17 lines
--- include/linux/gpu_buddy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/gpu_buddy.h +1
-1@@ -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.
Quoted 18 lines · Matthew Auld
Quoted 16 lines · Bill Wendling
In 'struct
gpu_buddy', the 'roots' field points to an array of pointers to 'structgpu_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.
Quoted 9 lines
Quoted 3 lines
Reviewed-by Matthew Auld <[email protected]>Would it make sense to also do this for
*_scoreboardandfree_tree, if we add a new fieldn_orders?
Sure! There's already a 'max_order'. I assume that 'n_orders' would be more dynamic?
-bw
Quoted 18 lines
Quoted 17 lines
--- include/linux/gpu_buddy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/gpu_buddy.h +1
-1@@ -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.
Matthew Auld
maintainerQuoted 23 lines · Bill Wendling
Quoted 22 lines · Matthew Auld
Quoted 19 lines · Bill Wendling
In 'struct
gpu_buddy', the 'roots' field points to an array of pointers to 'structgpu_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.
Quoted 7 lines
Quoted 5 lines
Reviewed-by Matthew Auld <[email protected]>Would it make sense to also do this for
*_scoreboardandfree_tree, if we add a new fieldn_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?
Quoted 21 lines
-bw
Quoted 18 lines
Quoted 17 lines
--- include/linux/gpu_buddy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/gpu_buddy.h +1
-1@@ -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.
Quoted 26 lines · Matthew Auld
Quoted 25 lines · Bill Wendling
Quoted 24 lines · Matthew Auld
Quoted 12 lines · Bill Wendling
In 'struct
gpu_buddy', the 'roots' field points to an array of pointers to 'structgpu_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)'.Quoted 7 lines
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.Reviewed-by Matthew Auld <[email protected]>
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Would it make sense to also do this for
*_scoreboardandfree_tree, if we add a new fieldn_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?
Well, yes*.
* The original Clang design of __counted_by (and associated) attributes accepted context-free expressions. However, GCC isn't able to handle expressions and we weren't able to agree on a syntax that was acceptable to both compiler teams. Thus, we won't be supporting expressions in these attributes, unless something miraculous happens.
-bw
Quoted 22 lines
Quoted 21 lines
-bw
Quoted 18 lines
Quoted 17 lines
--- include/linux/gpu_buddy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/gpu_buddy.h +1
-1@@ -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.