freenode
Kernel & Low-Level

Hazard pointer series fixes race, draws Linus on ptr_eq

Mathieu Desnoyers posts hazptr updates with a try-acquire fast path; Torvalds rejects hiding pointer values to preserve address dependencies.

Mathieu Desnoyers has posted a four-part hazard pointer update that fixes a race in the two-phase synchronize path, adds a try-acquire fast path, and introduces a ptr_eq helper meant to keep compilers from breaking address dependencies. The race was identified by Boqun Feng.

Hazard pointers are a kernel synchronization primitive in the RCU family: readers publish pointers they are using so reclaim can wait safely without the full RCU grace-period machinery. The first change closes a window between synchronize and detach in the two-phase wildcard scheme. A later change lets the common acquire path attempt a slot without wildcards, falling back to a slow path and overflow list so both readers and synchronize still make forward progress. Wildcards then appear only briefly on the overflow path in non-preemptible regions, simplifying what per-CPU scans must expect.

Supporting that path is ptr_eq, a comparison helper documented for RCU-style dereference rules. Its initial form used optimizer barriers on both pointer operands so a later dependent load would not be rewritten into an unsafe form. Linus Torvalds objected that this hides the wrong thing: the values are valid, and only the comparison result needs shielding. Hiding the pointers forces reloads and extra register pressure for no gain. He preferred a plain equality when the compiler already honors address dependencies, architecture-specific compare sequences next, and the hide-variable approach only as a last resort.

Desnoyers agreed on that order but cautioned against assuming current Clang is safe for the hazptr acquire load-and-recheck pattern, citing still-open compiler dependency cases. Side discussion noted lockdep and Rust revocable patterns that pin fixed addresses; those remain workable when readers are rare, though a steady stream of the same published value can stall synchronize without further period-flip design.

The series sits on prior hazptr work in the RCU development tree and is aimed at making the fast path cheaper without weakening reclaim guarantees.