Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: hch@lst.de (Christoph Hellwig)
Subject: [PATCH v2] NVMe: eliminate potential deadlock by nvme_get_ns_from_disk invoking nvme_free_ns
Date: Wed, 4 May 2016 11:50:04 +0200	[thread overview]
Message-ID: <20160504095004.GA15483@lst.de> (raw)
In-Reply-To: <1462324114-3716-1-git-send-email-shhuiw@foxmail.com>

On Wed, May 04, 2016@09:08:34AM +0800, Wang Sheng-Hui wrote:
> Release dev_list_lock before enter nvme_free_ns from
> nvme_get_ns_from_disk to avoid potential deadlock.
> 
> Signed-off-by: Wang Sheng-Hui <shhuiw at foxmail.com>
> ---
>  drivers/nvme/host/core.c | 15 ++++++---------
>  1 file changed, 6 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
> index 643f457..ab12892 100644
> --- a/drivers/nvme/host/core.c
> +++ b/drivers/nvme/host/core.c
> @@ -87,20 +87,17 @@ static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
>  	spin_lock(&dev_list_lock);
>  	ns = disk->private_data;
>  	if (ns) {
> +		if (!try_module_get(ns->ctrl->ops->module)) {
> +			spin_unlock(&dev_list_lock);
> +			kref_put(&ns->kref, nvme_free_ns);
> +			return NULL;
> +		}
>  		if (!kref_get_unless_zero(&ns->kref))
> +			ns = NULL;
>  	}
>  	spin_unlock(&dev_list_lock);
>  
>  	return ns;
>  }

This is incorrect - if kref_get_unless_zero fails we now fail to to to
drop the reference again.  What about this variant instead?

	ns = disk->private_data;
	if (ns) {
		if (!kref_get_unless_zero(&ns->kref))
			ns = NULL;
		else if (!try_module_get(ns->ctrl->ops->module))
			goto out_put_ns;
	}
	spin_unlock(&dev_list_lock);
	return ns;

out_put_ns:
	spin_unlock(&dev_list_lock);
	kref_put(&ns->kref, nvme_free_ns);
	return NULL;

  reply	other threads:[~2016-05-04  9:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-04  1:08 [PATCH v2] NVMe: eliminate potential deadlock by nvme_get_ns_from_disk invoking nvme_free_ns Wang Sheng-Hui
2016-05-04  9:50 ` Christoph Hellwig [this message]
2016-05-04 14:44   ` Busch, Keith

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=20160504095004.GA15483@lst.de \
    --to=hch@lst.de \
    /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