mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] dax/bus: avoid deadlock when reading mapping attributes
@ 2026-10-30  5:06 Guixiong Wei
  2026-12-03  2:21 ` Alison Schofield
  0 siblings, 1 reply; 2+ messages in thread
From: Guixiong Wei @ 2026-10-30  5:06 UTC (permalink / raw)
  To: Dan Williams
  Cc: Vishal Verma, Dave Jiang, Alison Schofield, nvdimm, linux-cxl,
	linux-kernel, Guixiong Wei, stable

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.

Fixes: 0b07ce872a9e ("device-dax: introduce 'mapping' devices")
Cc: stable@vger.kernel.org # 5.10+
Signed-off-by: Guixiong Wei <weiguixiong@bytedance.com>
---
 drivers/dax/bus.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
index e40c25401cf06..823d60353fc83 100644
--- a/drivers/dax/bus.c
+++ b/drivers/dax/bus.c
@@ -3,6 +3,7 @@
 #include <linux/memremap.h>
 #include <linux/device.h>
 #include <linux/mutex.h>
+#include <linux/sched/signal.h>
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/dax.h>
@@ -719,14 +720,17 @@ static struct dev_dax_range *get_dax_range(struct device *dev)
 {
 	struct dax_mapping *mapping = to_dax_mapping(dev);
 	struct dev_dax *dev_dax = to_dev_dax(dev->parent);
-	int rc;
 
-	rc = down_write_killable(&dax_region_rwsem);
-	if (rc)
-		return NULL;
+	/*
+	 * Mapping removal drains this attribute with dax_region_rwsem held.
+	 * Restart the read on contention to release the kernfs active
+	 * reference without waiting for the rwsem.
+	 */
+	if (!down_write_trylock(&dax_region_rwsem))
+		return ERR_PTR(restart_syscall());
 	if (mapping->range_id < 0) {
 		up_write(&dax_region_rwsem);
-		return NULL;
+		return ERR_PTR(-ENXIO);
 	}
 
 	return &dev_dax->ranges[mapping->range_id];
@@ -744,8 +748,8 @@ static ssize_t start_show(struct device *dev,
 	ssize_t rc;
 
 	dax_range = get_dax_range(dev);
-	if (!dax_range)
-		return -ENXIO;
+	if (IS_ERR(dax_range))
+		return PTR_ERR(dax_range);
 	rc = sysfs_emit(buf, "%#llx\n", dax_range->range.start);
 	put_dax_range();
 
@@ -760,8 +764,8 @@ static ssize_t end_show(struct device *dev,
 	ssize_t rc;
 
 	dax_range = get_dax_range(dev);
-	if (!dax_range)
-		return -ENXIO;
+	if (IS_ERR(dax_range))
+		return PTR_ERR(dax_range);
 	rc = sysfs_emit(buf, "%#llx\n", dax_range->range.end);
 	put_dax_range();
 
@@ -776,8 +780,8 @@ static ssize_t pgoff_show(struct device *dev,
 	ssize_t rc;
 
 	dax_range = get_dax_range(dev);
-	if (!dax_range)
-		return -ENXIO;
+	if (IS_ERR(dax_range))
+		return PTR_ERR(dax_range);
 	rc = sysfs_emit(buf, "%#lx\n", dax_range->pgoff);
 	put_dax_range();
 
-- 
2.55.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] dax/bus: avoid deadlock when reading mapping attributes
  2026-10-30  5:06 [PATCH] dax/bus: avoid deadlock when reading mapping attributes Guixiong Wei
@ 2026-12-03  2:21 ` Alison Schofield
  0 siblings, 0 replies; 2+ messages in thread
From: Alison Schofield @ 2026-12-03  2:21 UTC (permalink / raw)
  To: Guixiong Wei
  Cc: Dan Williams, Vishal Verma, Dave Jiang, nvdimm, linux-cxl,
	linux-kernel, stable

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.

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?

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?

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

-- Alison



> 
> Fixes: 0b07ce872a9e ("device-dax: introduce 'mapping' devices")
> Cc: stable@vger.kernel.org # 5.10+
> Signed-off-by: Guixiong Wei <weiguixiong@bytedance.com>
> ---
>  drivers/dax/bus.c | 26 +++++++++++++++-----------
>  1 file changed, 15 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
> index e40c25401cf06..823d60353fc83 100644
> --- a/drivers/dax/bus.c
> +++ b/drivers/dax/bus.c
> @@ -3,6 +3,7 @@
>  #include <linux/memremap.h>
>  #include <linux/device.h>
>  #include <linux/mutex.h>
> +#include <linux/sched/signal.h>
>  #include <linux/list.h>
>  #include <linux/slab.h>
>  #include <linux/dax.h>
> @@ -719,14 +720,17 @@ static struct dev_dax_range *get_dax_range(struct device *dev)
>  {
>  	struct dax_mapping *mapping = to_dax_mapping(dev);
>  	struct dev_dax *dev_dax = to_dev_dax(dev->parent);
> -	int rc;
>  
> -	rc = down_write_killable(&dax_region_rwsem);
> -	if (rc)
> -		return NULL;
> +	/*
> +	 * Mapping removal drains this attribute with dax_region_rwsem held.
> +	 * Restart the read on contention to release the kernfs active
> +	 * reference without waiting for the rwsem.
> +	 */
> +	if (!down_write_trylock(&dax_region_rwsem))
> +		return ERR_PTR(restart_syscall());
>  	if (mapping->range_id < 0) {
>  		up_write(&dax_region_rwsem);
> -		return NULL;
> +		return ERR_PTR(-ENXIO);
>  	}
>  
>  	return &dev_dax->ranges[mapping->range_id];
> @@ -744,8 +748,8 @@ static ssize_t start_show(struct device *dev,
>  	ssize_t rc;
>  
>  	dax_range = get_dax_range(dev);
> -	if (!dax_range)
> -		return -ENXIO;
> +	if (IS_ERR(dax_range))
> +		return PTR_ERR(dax_range);
>  	rc = sysfs_emit(buf, "%#llx\n", dax_range->range.start);
>  	put_dax_range();
>  
> @@ -760,8 +764,8 @@ static ssize_t end_show(struct device *dev,
>  	ssize_t rc;
>  
>  	dax_range = get_dax_range(dev);
> -	if (!dax_range)
> -		return -ENXIO;
> +	if (IS_ERR(dax_range))
> +		return PTR_ERR(dax_range);
>  	rc = sysfs_emit(buf, "%#llx\n", dax_range->range.end);
>  	put_dax_range();
>  
> @@ -776,8 +780,8 @@ static ssize_t pgoff_show(struct device *dev,
>  	ssize_t rc;
>  
>  	dax_range = get_dax_range(dev);
> -	if (!dax_range)
> -		return -ENXIO;
> +	if (IS_ERR(dax_range))
> +		return PTR_ERR(dax_range);
>  	rc = sysfs_emit(buf, "%#lx\n", dax_range->pgoff);
>  	put_dax_range();
>  
> -- 
> 2.55.0
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  2:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-30  5:06 [PATCH] dax/bus: avoid deadlock when reading mapping attributes Guixiong Wei
2026-12-03  2:21 ` Alison Schofield

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®