Re: [PATCH v4 30/39] unwind_user/deferred: Make unwind deferral requests NMI-safe

From: Peter Zijlstra
Date: Thu Jan 23 2025 - 03:42:34 EST


On Wed, Jan 22, 2025 at 02:52:57PM -0800, Josh Poimboeuf wrote:
> On Wed, Jan 22, 2025 at 03:24:18PM +0100, Peter Zijlstra wrote:
> > On Tue, Jan 21, 2025 at 06:31:22PM -0800, Josh Poimboeuf wrote:
> > > +static int unwind_deferred_request_nmi(struct unwind_work *work, u64 *cookie)
> > > +{
> > > + struct unwind_task_info *info = &current->unwind_info;
> > > + bool inited_cookie = false;
> > > + int ret;
> > > +
> > > + *cookie = info->cookie;
> > > + if (!*cookie) {
> > > + /*
> > > + * This is the first unwind request since the most recent entry
> > > + * from user. Initialize the task cookie.
> > > + *
> > > + * Don't write to info->cookie directly, otherwise it may get
> > > + * cleared if the NMI occurred in the kernel during early entry
> > > + * or late exit before the task work gets to run. Instead, use
> > > + * info->nmi_cookie which gets synced later by get_cookie().
> > > + */
> > > + if (!info->nmi_cookie) {
> > > + u64 cpu = raw_smp_processor_id();
> > > + u64 ctx_ctr;
> > > +
> > > + ctx_ctr = __this_cpu_inc_return(unwind_ctx_ctr);
> >
> > __this_cpu_inc_return() is *NOT* NMI safe IIRC.
>
> Hm, I guess I was only looking at x86.

:-), right so x86 is rather special here, the various RISC platforms are
what you should be looking at, eg. arm64.