Re: [PATCH] dax/bus: avoid deadlock when reading mapping attributes

From: Guixiong Wei

Date: Sun Dec 06 2026 - 10:02:54 EST



> From: "Alison Schofield"<alison.schofield@xxxxxxxxx>
> Date:  Thu, Dec 3, 2026, 10:22
> Subject:  Re: [PATCH] dax/bus: avoid deadlock when reading mapping attributes
> To: "Guixiong Wei"<weiguixiong@xxxxxxxxxxxxx>
> Cc: "Dan Williams"<djbw@xxxxxxxxxx>, "Vishal Verma"<vishal.l.verma@xxxxxxxxx>, "Dave Jiang"<dave.jiang@xxxxxxxxx>, <nvdimm@xxxxxxxxxxxxxxx>, <linux-cxl@xxxxxxxxxxxxxxx>, <linux-kernel@xxxxxxxxxxxxxxx>, <stable@xxxxxxxxxxxxxxx>
> On Fri, Oct 30, 2026 at 01:06:40PM +0800, Guixiong Wei wrote:
> > Reading a mapping's start, end, or page_offset attribute takes
> > dax_region_rwsem while holding a kernfs active reference. When
> > size_store() shrinks a device, it holds the rwsem while unregistering
> > mappings. Unregistration waits in kernfs_drain() for active references
> > to be released, so a concurrent attribute read can deadlock:
> > 
> >   Reader                         size_store()
> >   kernfs_get_active()
> >                                  down_write(dax_region_rwsem)
> >                                  device_unregister(mapping)
> >                                  kernfs_drain()
> >   down_write(dax_region_rwsem)
> > 
> > Setting range_id to -1 before unregistering does not break the cycle
> > because get_dax_range() checks it only after acquiring the rwsem.
> > 
> > Use down_write_trylock() and restart the read on contention. Returning
> > from the show callback allows kernfs to drop the active reference, so
> > mapping unregistration can finish. Keep active protection throughout
> > the callback to preserve the existing teardown synchronization.
> > 
> > The same inversion existed with the region device lock before it was
> > replaced by dax_region_rwsem, so the problem dates back to the
> > introduction of mapping devices.
> 
> Hi Guixiong,
> 
> Thanks for tracking this down. The change breaks the deadlock you describe,
> but I’d like to see a revision before taking it. IIUC the trylock and
> syscall restart can spin whenever the global dax_region_rwsem is contended,
> even during ordinary reads of unrelated mappings. AIO and io_uring can also
> turn the restart into -EINTR, so contention that previously caused a read
> to wait can now appear as an error.

Agreed. I will drop the trylock and restart_syscall().
> 
> Please look at the approach in 79c61899b5ee ("net-sysfs: remove rtnl_trylock
> from device attributes")? It uses `sysfs_break_active_protection()` so the
> callback can wait for the lock without blocking attribute removal. Would
> that work here with the lifetime of the mapping device and its parent, and
> the range_id check under the rwsem? If the callback needs to retain active
> protection, please explain what it protects. Does get_dax_range() need the
> write side of the rwsem?
Yes. get_device() on the mapping while the kernfs active reference is
still held, then break that reference, then take dax_region_rwsem for
read. The read side is enough. range_id is checked under the rwsem,
and the range fields are copied out before it is dropped. range_id < 0
returns -ENXIO, the existing error from get_dax_range().

The extra device reference keeps the mapping, and the mapping's
reference to its parent dev_dax, alive across put_device() in
unregister_dax_mapping(). That reference is dropped after the rwsem.
The callback does not need to keep the kernfs active reference.

> 
> There also seesm to be another instance of the same lock inversion-
> unregister_dev_dax() holds the rwsem across device_del() and that can drain
> a size, mapping, or align store that is itself waiting for the rwsem. Could
> you confirm that path and say whether it can use the same fix or should be
> handled in a follow-up?
Confirmed. device_del() drains those stores while dax_region_rwsem is
held, and the stores are waiting for that rwsem. The same sequence
applies there: pin the dev_dax, break active protection, then take
the rwsem. I would handle that as a follow-up. Please say if you want
it in this patch.

> 
> Please include how you reproduced the hang and tested the fix.

  daxctl reconfigure-device --mode=devdax --force dax0.0
  daxctl disable-device dax0.0
  daxctl reconfigure-device dax0.0 -s 1G

Readers looped on mapping0/{start,end,page_offset}, which is what
daxctl list -M reads. size_store() returns -EBUSY while device_dax is
bound, so the device was disabled first. This then hung:

  daxctl reconfigure-device dax0.0 -s 0

  daxctl:  size_store -> devm_release_action -> device_unregister
           -> kernfs_drain
  readers: start_show / end_show / pgoff_show -> device lock

Thanks,
Guixiong