Re: [PATCH v12 06/14] unwind_user/deferred: Add deferred unwinding interface
From: Linus Torvalds
Date: Wed Jul 02 2025 - 14:22:09 EST
On Wed, 2 Jul 2025 at 10:49, Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
>
> To still be able to use a 32 bit cmpxchg (for racing with an NMI), we
> could break this number up into two 32 bit words. One with the CPU that
> it was created on, and one with the per_cpu counter:
Do you actually even need a cpu number at all?
If this is per-thread, maybe just a per-thread counter would be good?
And you already *have* that per-thread thing, in
'current->unwind_info'.
And the work is using task_work, so the worker callback is also per-thread.
Also, is racing with NMI even a thing for the sequence number? I would
expect that the only thing that NMI would race with is the 'pending'
field, not the sequence number.
IOW, I think the logic could be
- check 'pending' non-atomically, just because it's cheap
- do a try_cmpxchg() on pending to actually deal with nmi races
Actually, there are no SMP issues, just instruction atomicity - so a
'local_try_cmpxchg() would be sufficient, but that's a 'long' not a
'u32' ;^(
- now you are exclusive for that thread, you're done, no more need
for any atomic counter or percpu things
And then the next step is to just say "pending is the low bit of the
id word" and having a separate 31-bit counter that gets incremented by
"get_cookie()".
So then you end up with something like
// New name for 'get_timestamp()'
get_current_cookie() { return current->unwind_info.cookie; }
// New name for 'get_cookie()':
// 31-bit counter by just leaving bit #0 alone
get_new_cookie() { current->unwind_info.cookie += 2; }
and then unwind_deferred_request() would do something like
unwind_deferred_request()
{
int old, new;
if (current->unwind_info.id)
return 1;
guard(irqsave)();
// For NMI, if we race with 'get_new_cookie()'
// we don't care if we get the old or the new one
old = 0; new = get_current_cookie() | 1;
if (!try_cmpxchg(¤t->unwind_info.id, &old, new))
return 1;
.. now schedule the thing with that cookie set.
Hmm?
But I didn't actually look at the users, so maybe I'm missing some
reason why you want to have a separate per-cpu value.
Or maybe I missed something else entirely, and the above is complete
garbage and the ramblings of a insane mind.
It happens.
Off to get more coffee.
Linus