Re: [PATCH] kasan: Remove kasan_record_aux_stack_noalloc().
From: Sebastian Andrzej Siewior
Date: Fri Nov 22 2024 - 10:29:34 EST
On 2024-11-22 16:01:29 [+0100], Marco Elver wrote:
> > Do we need to update the comment saying that it must not be used from
> > NMI or do we make it jump over the locked section in the NMI case?
>
> Good point. It was meant to also be usable from NMI, because it's very
> likely to succeed, and should just take the lock-less fast path once the
> stack is in the depot.
>
> But I think we need a fix like this for initial saving of a stack trace:
>
>
> diff --git a/lib/stackdepot.c b/lib/stackdepot.c
> index 5ed34cc963fc..245d5b416699 100644
> --- a/lib/stackdepot.c
> +++ b/lib/stackdepot.c
> @@ -630,7 +630,15 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries,
> prealloc = page_address(page);
> }
>
> - raw_spin_lock_irqsave(&pool_lock, flags);
> + if (in_nmi()) {
> + /* We can never allocate in NMI context. */
> + WARN_ON_ONCE(can_alloc);
> + /* Best effort; bail if we fail to take the lock. */
> + if (!raw_spin_trylock_irqsave(&pool_lock, flags))
> + goto exit;
> + } else {
> + raw_spin_lock_irqsave(&pool_lock, flags);
> + }
> printk_deferred_enter();
>
> /* Try to find again, to avoid concurrently inserting duplicates. */
>
>
> If that looks reasonable, I'll turn it into a patch.
Yes, looks reasonable.
Sebastian