History

lib/stackdepot: introduce __stack_depot_save()

Add __stack_depot_save(), which provides more fine-grained control over stackdepot's memory allocation behaviour, in case stackdepot runs out of "stack slabs". Normally stackdepot uses alloc_pages() in case it runs out of space; passing can_alloc==false to __stack_depot_save() prohibits this, at the cost of more likely failure to record a stack trace.

Marco Elver committed 5 years ago

Commit 11ac25c1 parent 7f2b8812 files · +41 −6Browse files

Unified · Split

2 files
  1. include/linux/stackdepot.h +4 −0
  2. lib/stackdepot.c +37 −6
include/linux/stackdepot.h+4−0
@@ -15,6 +15,10 @@ · 14 unmodified lines
1515
1616 typedef u32 depot_stack_handle_t;
1717
18+depot_stack_handle_t __stack_depot_save(unsigned long *entries,
19+ unsigned int nr_entries,
20+ gfp_t gfp_flags, bool can_alloc);
21+
1822 depot_stack_handle_t stack_depot_save(unsigned long *entries,
1923 unsigned int nr_entries, gfp_t gfp_flags);
2024
lib/stackdepot.c+37−6
@@ -248,17 +248,28 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, · 247 unmodified lines
248248 EXPORT_SYMBOL_GPL(stack_depot_fetch);
249249
250250 /**
251− * stack_depot_save - Save a stack trace from an array
251+ * __stack_depot_save - Save a stack trace from an array
252252 *
253253 * @entries: Pointer to storage array
254254 * @nr_entries: Size of the storage array
255255 * @alloc_flags: Allocation gfp flags
256+ * @can_alloc: Allocate stack slabs (increased chance of failure if false)
257+ *
258+ * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is
259+ * %true, is allowed to replenish the stack slab pool in case no space is left
260+ * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids
261+ * any allocations and will fail if no space is left to store the stack trace.
262+ *
263+ * Context: Any context, but setting @can_alloc to %false is required if
264+ * alloc_pages() cannot be used from the current context. Currently
265+ * this is the case from contexts where neither %GFP_ATOMIC nor
266+ * %GFP_NOWAIT can be used (NMI, raw_spin_lock).
256267 *
257− * Return: The handle of the stack struct stored in depot
268+ * Return: The handle of the stack struct stored in depot, 0 on failure.
258269 */
259−depot_stack_handle_t stack_depot_save(unsigned long *entries,
260− unsigned int nr_entries,
261− gfp_t alloc_flags)
270+depot_stack_handle_t __stack_depot_save(unsigned long *entries,
271+ unsigned int nr_entries,
272+ gfp_t alloc_flags, bool can_alloc)
262273 {
263274 struct stack_record *found = NULL, **bucket;
264275 depot_stack_handle_t retval = 0;
@@ -291,7 +302,7 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, · 26 unmodified lines
291302 * The smp_load_acquire() here pairs with smp_store_release() to
292303 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
293304 */
294− if (unlikely(!smp_load_acquire(&next_slab_inited))) {
305+ if (unlikely(can_alloc && !smp_load_acquire(&next_slab_inited))) {
295306 /*
296307 * Zero out zone modifiers, as we don't have specific zone
297308 * requirements. Keep the flags related to allocation in atomic
@@ -339,6 +350,26 @@ exit: · 41 unmodified lines
339350 fast_exit:
340351 return retval;
341352 }
353+EXPORT_SYMBOL_GPL(__stack_depot_save);
354+
355+/**
356+ * stack_depot_save - Save a stack trace from an array
357+ *
358+ * @entries: Pointer to storage array
359+ * @nr_entries: Size of the storage array
360+ * @alloc_flags: Allocation gfp flags
361+ *
362+ * Context: Contexts where allocations via alloc_pages() are allowed.
363+ * See __stack_depot_save() for more details.
364+ *
365+ * Return: The handle of the stack struct stored in depot, 0 on failure.
366+ */
367+depot_stack_handle_t stack_depot_save(unsigned long *entries,
368+ unsigned int nr_entries,
369+ gfp_t alloc_flags)
370+{
371+ return __stack_depot_save(entries, nr_entries, alloc_flags, true);
372+}
342373 EXPORT_SYMBOL_GPL(stack_depot_save);
343374
344375 static inline int in_irqentry_text(unsigned long ptr)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t