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.
Unified · Split
| @@ -15,6 +15,10 @@ · 14 unmodified lines | ||
| 15 | 15 | |
| 16 | 16 | typedef u32 depot_stack_handle_t; |
| 17 | 17 | |
| 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 | + | |
| 18 | 22 | depot_stack_handle_t stack_depot_save(unsigned long *entries, |
| 19 | 23 | unsigned int nr_entries, gfp_t gfp_flags); |
| 20 | 24 | |
| @@ -248,17 +248,28 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, · 247 unmodified lines | ||
| 248 | 248 | EXPORT_SYMBOL_GPL(stack_depot_fetch); |
| 249 | 249 | |
| 250 | 250 | /** |
| 251 | − * stack_depot_save - Save a stack trace from an array | |
| 251 | + * __stack_depot_save - Save a stack trace from an array | |
| 252 | 252 | * |
| 253 | 253 | * @entries: Pointer to storage array |
| 254 | 254 | * @nr_entries: Size of the storage array |
| 255 | 255 | * @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). | |
| 256 | 267 | * |
| 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. | |
| 258 | 269 | */ |
| 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) | |
| 262 | 273 | { |
| 263 | 274 | struct stack_record *found = NULL, **bucket; |
| 264 | 275 | depot_stack_handle_t retval = 0; |
| @@ -291,7 +302,7 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, · 26 unmodified lines | ||
| 291 | 302 | * The smp_load_acquire() here pairs with smp_store_release() to |
| 292 | 303 | * |next_slab_inited| in depot_alloc_stack() and init_stack_slab(). |
| 293 | 304 | */ |
| 294 | − if (unlikely(!smp_load_acquire(&next_slab_inited))) { | |
| 305 | + if (unlikely(can_alloc && !smp_load_acquire(&next_slab_inited))) { | |
| 295 | 306 | /* |
| 296 | 307 | * Zero out zone modifiers, as we don't have specific zone |
| 297 | 308 | * requirements. Keep the flags related to allocation in atomic |
| @@ -339,6 +350,26 @@ exit: · 41 unmodified lines | ||
| 339 | 350 | fast_exit: |
| 340 | 351 | return retval; |
| 341 | 352 | } |
| 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 | +} | |
| 342 | 373 | EXPORT_SYMBOL_GPL(stack_depot_save); |
| 343 | 374 | |
| 344 | 375 | static inline int in_irqentry_text(unsigned long ptr) |