Re: [PATCH v4 12/22] lib/stackdepot: use read/write lock

From: Andi Kleen
Date: Fri Jan 12 2024 - 20:24:27 EST


On Fri, Jan 12, 2024 at 11:15:05PM +0100, Marco Elver wrote:
> + /*
> + * Stack traces of size 0 are never saved, and we can simply use
> + * the size field as an indicator if this is a new unused stack
> + * record in the freelist.
> + */
> + stack->size = 0;

I would use WRITE_ONCE here too, at least for TSan.

> + return NULL;
> +
> + /*
> + * We maintain the invariant that the elements in front are least
> + * recently used, and are therefore more likely to be associated with an
> + * RCU grace period in the past. Consequently it is sufficient to only
> + * check the first entry.
> + */
> + stack = list_first_entry(&free_stacks, struct stack_record, free_list);
> + if (stack->size && !poll_state_synchronize_rcu(stack->rcu_state))

READ_ONCE (also for TSan, and might be safer long term in case the
compiler considers some fancy code transformation)

> + return NULL;
>
> + stack = depot_pop_free();
> + if (WARN_ON(!stack))

Won't you get nesting problems here if this triggers due to the print?
I assume the nmi safe printk won't consider it like an NMI.

> counters[DEPOT_COUNTER_FREELIST_SIZE]++;
> counters[DEPOT_COUNTER_FREES]++;
> counters[DEPOT_COUNTER_INUSE]--;
> +
> + printk_deferred_exit();

Ah this handles the WARN_ON? Should be ok then.

-Andi