Re: [PATCH 19/28] hazptr: Permit detaching hazard pointers from contexts

From: Boqun Feng

Date: Sat Sep 19 2026 - 07:43:48 EST


On Sat, Sep 19, 2026 at 12:42:48PM +0100, Boqun Feng wrote:
> On Fri, Sep 18, 2026 at 05:00:47PM -0700, Paul E. McKenney wrote:
> > From: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
> >
> > Provide a new hazptr_detach() function that detaches a given hazard
> > pointer from its acquisition context. This context might be a task or
> > an interrupt handler.
> >
> > [ paulmck: s/hazptr_detach_from_task/hazptr_detach/ per Mathieu. ]
> >
> > Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
> > Signed-off-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
> > Cc: Boqun Feng <boqun@xxxxxxxxxx>
> > Cc: <rcu@xxxxxxxxxxxxxxx>
> > Cc: <lkmm@xxxxxxxxxxxxxxx>
> > ---
> > include/linux/hazptr.h | 55 ++++++++++++++++++++++++++++--------------
> > 1 file changed, 37 insertions(+), 18 deletions(-)
> >
> > diff --git a/include/linux/hazptr.h b/include/linux/hazptr.h
> > index b121f7779cda..8197a51b9f7a 100644
> > --- a/include/linux/hazptr.h
> > +++ b/include/linux/hazptr.h
> > @@ -98,6 +98,41 @@ bool hazptr_slot_is_backup(struct hazptr_ctx *ctx, struct hazptr_slot *slot)
> > return slot == &ctx->backup_slot.slot;
> > }
> >
> > +/* Internal helper. */
> > +static inline
> > +void hazptr_promote_to_backup_slot(struct hazptr_ctx *ctx, struct hazptr_slot *slot)
> > +{
> > + struct hazptr_slot *backup_slot;
> > +
> > + backup_slot = hazptr_chain_backup_slot(ctx);
> > + /*
> > + * Move hazard pointer from the per-CPU slot to the
> > + * backup slot. This requires hazard pointer
> > + * synchronize to iterate on per-CPU slots with
> > + * load-acquire before iterating on the overflow list.
> > + */
> > + WRITE_ONCE(backup_slot->addr, slot->addr);
> > + /*
> > + * store-release orders store to backup slot addr before
> > + * store to per-CPU slot addr.
> > + */
> > + smp_store_release(&slot->addr, NULL);
> > + /* Use the backup slot for context. */
> > + ctx->slot = backup_slot;
> > +}
> > +
>
> Probably needs a function doc for hazptr_detach(), how about the
> following?
>

Obivioulsy I'm missing patch #24, never mind then :)

Regards,
Boqun

> /*
> * hazptr_detach: Detach the hazptr from its acquisition context.
> *
> * By default hazptr_acquire() expects to be released from its
> * acquisition context. However there are cases where the acquisition
> * context sends (in other words, transfers the ownership of) the hazptr
> * to another context, e.g.
> *
> * hazptr_acquire(ctx, gp);
> * hazptr_detach(ctx);
> * defer_work->hp = ctx;
> * queue_work(..., defer_work);
> * <in defer_work->func()>
> * hazptr_release(ctx);
> *
> * Use hazptr_detach() before sending the hazptr across contexts.
> *
> * It's safe to detach an "already-detatched" hazptr.
> */
>
> Thoughts?
>
> Regards,
> Boqun
>
> > +static inline
> > +void hazptr_detach(struct hazptr_ctx *ctx)
> > +{
> > + struct hazptr_slot *slot;
> > +
> > + guard(preempt)();
> > + slot = ctx->slot;
> > + if (unlikely(hazptr_slot_is_backup(ctx, slot)))
> > + return;
> > + hazptr_promote_to_backup_slot(ctx, slot);
> > +}
> > +
> > static inline
> > void hazptr_note_context_switch(void)
> > {
> > @@ -106,27 +141,11 @@ void hazptr_note_context_switch(void)
> >
> > for (idx = 0; idx < NR_HAZPTR_PERCPU_SLOTS; idx++) {
> > struct hazptr_slot_item *item = &percpu_slots->items[idx];
> > - struct hazptr_slot *slot = &item->slot, *backup_slot;
> > - struct hazptr_ctx *ctx;
> > + struct hazptr_slot *slot = &item->slot;
> >
> > if (!slot->addr)
> > continue;
> > - ctx = item->ctx.ctx;
> > - backup_slot = hazptr_chain_backup_slot(ctx);
> > - /*
> > - * Move hazard pointer from the per-CPU slot to the
> > - * backup slot. This requires hazard pointer
> > - * synchronize to iterate on per-CPU slots with
> > - * load-acquire before iterating on the overflow list.
> > - */
> > - WRITE_ONCE(backup_slot->addr, slot->addr);
> > - /*
> > - * store-release orders store to backup slot addr before
> > - * store to per-CPU slot addr.
> > - */
> > - smp_store_release(&slot->addr, NULL);
> > - /* Use the backup slot for context. */
> > - ctx->slot = backup_slot;
> > + hazptr_promote_to_backup_slot(item->ctx.ctx, slot);
> > }
> > }
> >
> > --
> > 2.40.1
> >