lib, stackdepot: check stackdepot handle before accessing slabs
Subsystems (2)
cover lib, stackdepot: check stackdepot handle before accessing slabs
This RFC patch series addresses suggestion discussed in an earlier RFC [1]. Since earlier RFC was about SLUB subsystem, and current changes only involve stackdepot, I am submitting the patches in a new thread.
The changes of this patch set are as follows:
PATCH-1: Checks validity of a stackdepot handle before proceeding to access stackdepot slab/objects.
PATCH-2: Adds a helper in stackdepot, to allow users to print stack entries just by specifying the stackdepot handle.
[1] https://lore.kernel.org/lkml/[email protected]/
Imran Khan (2):
lib, stackdepot: check stackdepot handle before accessing slabs. lib, stackdepot: Add helper to print stack entries.
lib/stackdepot.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
Signature · 1 line
2.30.2
RFC PATCH 1/2 lib, stackdepot: check stackdepot handle before accessing slabs.
stack_depot_save allocates slabs that will be used for storing objects in future.If this slab allocation fails we may get to a situation where space allocation for a new stack_record fails, causing stack_depot_save to return 0 as handle.
If user of this handle ends up invoking stack_depot_fetch with this handle value, current implementation of stack_depot_fetch will end up using slab from wrong index.
To avoid this check handle value at the beginning.
Also issue a warning for nil handle values and when slab allocation for stackdepot fails for the first time.
--- lib/stackdepot.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/stackdepot.c +6 -0
@@ -232,6 +232,10 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle,struct stack_record *stack; *entries =NULL ; +if (!handle) { +WARN (1 ,"stack depot handle is absent.\n" ); +return 0 ; + }if (parts.slabindex > depot_index) {WARN (1 ,"slab index %d out of bounds (%d) for stack id %08x\n" , parts.slabindex, depot_index, handle);
@@ -303,6 +307,8 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, page =alloc_pages (alloc_flags,STACK_ALLOC_ORDER );if (page) prealloc =page_address (page); +else +WARN_ONCE (1 ,"slab allocation for stack depot failed.\n" ); }raw_spin_lock_irqsave (&depot_lock, flags);
Signature · 1 line
2.30.2
Re: [RFC PATCH 1/2] lib, stackdepot: check stackdepot handle before accessing slabs.
+CC linux-mm, Alex
Quoted 11 lines · Imran Khan
stack_depot_saveallocates slabs that will be used for storing objects in future.If this slab allocation fails we may get to a situation where space allocation for a newstack_recordfails, causingstack_depot_saveto return 0 as handle.
If user of this handle ends up invokingstack_depot_fetchwith this handle value, current implementation ofstack_depot_fetchwill end up using slab from wrong index.
To avoid this check handle value at the beginning.
Also issue a warning for nil handle values and when slab allocation for stackdepot fails for the first time.
Quoted 2 lines
Signed-off-by Imran Khan <[email protected]>Suggested-by Vlastimil Babka <[email protected]>
Agree but without the warnings please, especially the "stack depot handle is absent" one. It's just something that can happen e.g. in GFP_NOWAIT contexts and no need to spam dmesg.
Quoted 29 lines
--- lib/stackdepot.c | 6 ++++++ 1 file changed, 6 insertions(+)lib/stackdepot.c +6
-0@@ -232,6 +232,10 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle,struct stack_record *stack; *entries =NULL ; +if (!handle) { +WARN (1 ,"stack depot handle is absent.\n" ); +return 0 ; + }if (parts.slabindex > depot_index) {WARN (1 ,"slab index %d out of bounds (%d) for stack id %08x\n" , parts.slabindex, depot_index, handle);@@ -303,6 +307,8 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, page =alloc_pages (alloc_flags,STACK_ALLOC_ORDER );if (page) prealloc =page_address (page); +else +WARN_ONCE (1 ,"slab allocation for stack depot failed.\n" ); }raw_spin_lock_irqsave (&depot_lock, flags);
Re: [RFC PATCH 1/2] lib, stackdepot: check stackdepot handle before accessing slabs.
Quoted 14 lines · Vlastimil Babka
+CC linux-mm, Alex
Quoted 11 lines · Imran Khan
stack_depot_saveallocates slabs that will be used for storing objects in future.If this slab allocation fails we may get to a situation where space allocation for a newstack_recordfails, causingstack_depot_saveto return 0 as handle.
If user of this handle ends up invokingstack_depot_fetchwith this handle value, current implementation ofstack_depot_fetchwill end up using slab from wrong index.
To avoid this check handle value at the beginning.
Also issue a warning for nil handle values and when slab allocation for stackdepot fails for the first time.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Imran Khan <[email protected]>Suggested-by Vlastimil Babka <[email protected]>Agree but without the warnings please, especially the "stack depot handle is absent" one. It's just something that can happen e.g. in
GFP_NOWAITcontexts and no need to spam dmesg.
Okay. I have removed warnings in v2 of patch set.
Quoted 27 lines
Quoted 27 lines
--- lib/stackdepot.c | 6 ++++++ 1 file changed, 6 insertions(+)lib/stackdepot.c +6
-0@@ -232,6 +232,10 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle,struct stack_record *stack; *entries =NULL ; +if (!handle) { +WARN (1 ,"stack depot handle is absent.\n" ); +return 0 ; + }if (parts.slabindex > depot_index) {WARN (1 ,"slab index %d out of bounds (%d) for stack id %08x\n" , parts.slabindex, depot_index, handle);@@ -303,6 +307,8 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, page =alloc_pages (alloc_flags,STACK_ALLOC_ORDER );if (page) prealloc =page_address (page); +else +WARN_ONCE (1 ,"slab allocation for stack depot failed.\n" ); }
Quoted 3 lines
lib/stackdepot.c depot_stack_handle_t stack_depot_save(unsigned long *entries, · line 314
Quoted 2 lines
lib/stackdepot.c +0
-0raw_spin_lock_irqsave (&depot_lock, flags);
Thanks for reviewing this.
--Imran
RFC PATCH 2/2 lib, stackdepot: Add helper to print stack entries.
In reply to Imran KhanTo print a stack entries, users of stackdepot, first use stack_depot_fetch to get a list of stack entries and then use stack_trace_print to print this list. Provide a helper in stackdepot to print stack entries based on stackdepot handle.
--- lib/stackdepot.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
lib/stackdepot.c +17 -0
@@ -214,6 +214,23 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,return NULL ; } +/** +* stack_depot_print - print stack entries from a depot +* +* @handle: Stack depot handle which was returned from +* stack_depot_save(). +* +*/ +void stack_depot_print (depot_stack_handle_t stack) +{ +unsigned long *entries; +unsigned int nr_entries; + + nr_entries =stack_depot_fetch (stack, &entries); +stack_trace_print (entries, nr_entries,0 ); +} +EXPORT_SYMBOL_GPL (stack_depot_print); +/** * stack_depot_fetch - Fetch stack entries from a depot *
Signature · 1 line
2.30.2
Re: [RFC PATCH 2/2] lib, stackdepot: Add helper to print stack entries.
Quoted 8 lines · Imran Khan
To print a stack entries, users of stackdepot, first use
stack_depot_fetchto get a list of stack entries and then usestack_trace_printto print this list. Provide a helper in stackdepot to print stack entries based on stackdepot handle.Signed-off-by Imran Khan <[email protected]>Suggested-by Vlastimil Babka <[email protected]>
You should convert existing users together with the patch that introduces the helper. I think print_stack() in mm/kasan/report.c, and __dump_page_owner() could use this.
Quoted 3 lines
--- lib/stackdepot.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
It's missing a declaration in include/linux/stackdepot.h Perhaps it could be all be a static inline there anyway.
Quoted 29 lines
lib/stackdepot.c +17
-0@@ -214,6 +214,23 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,return NULL ; } +/** +* stack_depot_print - print stack entries from a depot +* +* @handle: Stack depot handle which was returned from +* stack_depot_save(). +* +*/ +void stack_depot_print (depot_stack_handle_t stack) +{ +unsigned long *entries; +unsigned int nr_entries; + + nr_entries =stack_depot_fetch (stack, &entries); +stack_trace_print (entries, nr_entries,0 ); +} +EXPORT_SYMBOL_GPL (stack_depot_print); +/** * stack_depot_fetch - Fetch stack entries from a depot *
Re: [RFC PATCH 2/2] lib, stackdepot: Add helper to print stack entries.
Hi Vlastimil,
Quoted 7 lines · Vlastimil Babka
Quoted 6 lines · Imran Khan
To print a stack entries, users of stackdepot, first use
stack_depot_fetchto get a list of stack entries and then usestack_trace_printto print this list. Provide a helper in stackdepot to print stack entries based on stackdepot handle.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Imran Khan <[email protected]>Suggested-by Vlastimil Babka <[email protected]>You should convert existing users together with the patch that introduces the helper. I think
print_stack()inmm/kasan/report.c, and__dump_page_owner()could use this.
Okay. I have done this in v2 of the patch set. BTW I also see some users (one place in page owner, rest all in some gpu drivers) of stack_depot_fetch + stack_trace_snprintf. Could you please let me know if it would be okay to add a helper corresponding to stack_trace_snprintf as well.
Quoted 7 lines
Quoted 3 lines
--- lib/stackdepot.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)It's missing a declaration in
include/linux/stackdepot.hPerhaps it could be all be a static inline there anyway.
Agree. I have added missing declaration now. I have not made it inline though.
Quoted 21 lines
Quoted 21 lines
lib/stackdepot.c +13
-0@@ -214,6 +214,23 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,return NULL ; } +/** +* stack_depot_print - print stack entries from a depot +* +* @handle: Stack depot handle which was returned from +* stack_depot_save(). +* +*/ +void stack_depot_print (depot_stack_handle_t stack) +{ +unsigned long *entries; +unsigned int nr_entries; + + nr_entries =stack_depot_fetch (stack, &entries);
Quoted 9 lines
lib/stackdepot.c static inline struct stack_record *find_stack(struct stack_record *bucket, · line 236
Quoted 8 lines
lib/stackdepot.c +4
-0+stack_trace_print (entries, nr_entries,0 ); +} +EXPORT_SYMBOL_GPL (stack_depot_print); +/** * stack_depot_fetch - Fetch stack entries from a depot *
Thanks again for the review.
--Imran
Re: [RFC PATCH 2/2] lib, stackdepot: Add helper to print stack entries.
Quoted 18 lines · On 9/2/21 02:11, [email protected] wrote:
Hi Vlastimil,
Quoted 7 lines · Vlastimil Babka
Quoted 6 lines · Imran Khan
To print a stack entries, users of stackdepot, first use
stack_depot_fetchto get a list of stack entries and then usestack_trace_printto print this list. Provide a helper in stackdepot to print stack entries based on stackdepot handle.Quoted 7 lines
Quoted 2 lines
Signed-off-by Imran Khan <[email protected]>Suggested-by Vlastimil Babka <[email protected]>You should convert existing users together with the patch that introduces the helper. I think
print_stack()inmm/kasan/report.c, and__dump_page_owner()could use this.
Quoted 4 lines
Okay. I have done this in v2 of the patch set. BTW I also see some users (one place in page owner, rest all in some gpu drivers) of
stack_depot_fetch
+stack_trace_snprintf. Could you please let me know if it would be okay to add a helper corresponding tostack_trace_snprintfas well.
Yeah looks like that would make sense too, i915 even has this as __print_depot_stack(). Thanks.