mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alison Schofield <alison.schofield@intel.com>
To: Guixiong Wei <weiguixiong@bytedance.com>
Cc: Dan Williams <djbw@kernel.org>,
	Vishal Verma <vishal.l.verma@intel.com>,
	Dave Jiang <dave.jiang@intel.com>, <nvdimm@lists.linux.dev>,
	<linux-cxl@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<stable@vger.kernel.org>
Subject: Re: [PATCH] dax/bus: avoid deadlock when reading mapping attributes
Date: Wed, 2 Dec 2026 18:21:45 -0800	[thread overview]
Message-ID: <axDSOfkSmZk6QBl8@aschofie-mobl2.lan> (raw)
In-Reply-To: <20261030050639.3462-2-weiguixiong@bytedance.com>

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
> 

      reply	other threads:[~2026-12-03  2:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30  5:06 Guixiong Wei
2026-12-03  2:21 ` Alison Schofield [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axDSOfkSmZk6QBl8@aschofie-mobl2.lan \
    --to=alison.schofield@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=djbw@kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=vishal.l.verma@intel.com \
    --cc=weiguixiong@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®