linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Matias Bjørling" <mb@lightnvm.io>
To: axboe@fb.com
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Igor Konopko" <igor.j.konopko@intel.com>,
	"Matias Bjørling" <mb@lightnvm.io>
Subject: [GIT PULL 23/26] lightnvm: track inflight target creations
Date: Sat,  4 May 2019 20:38:08 +0200	[thread overview]
Message-ID: <20190504183811.18725-24-mb@lightnvm.io> (raw)
In-Reply-To: <20190504183811.18725-1-mb@lightnvm.io>

From: Igor Konopko <igor.j.konopko@intel.com>

When creation process is still in progress, target is not yet on
targets list. This causes a chance for removing whole lightnvm
subsystem by calling nvm_unregister() in the meantime and finally by
causing kernel panic inside target init function.

This patch changes the behaviour by adding kref variable which tracks
all the users of nvm_dev structure. When nvm_dev is allocated, kref
value is set to 1. Then before every target creation the value is
increased and decreased after target removal. The extra reference
is decreased when nvm subsystem is unregistered.

Signed-off-by: Igor Konopko <igor.j.konopko@intel.com>
Reviewed-by: Javier González <javier@javigon.com>
Signed-off-by: Matias Bjørling <mb@lightnvm.io>
---
 drivers/lightnvm/core.c  | 41 ++++++++++++++++++++++++++++++----------
 include/linux/lightnvm.h |  1 +
 2 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/drivers/lightnvm/core.c b/drivers/lightnvm/core.c
index e2abe88a139c..0e9f7996ff1d 100644
--- a/drivers/lightnvm/core.c
+++ b/drivers/lightnvm/core.c
@@ -45,6 +45,8 @@ struct nvm_dev_map {
 	int num_ch;
 };
 
+static void nvm_free(struct kref *ref);
+
 static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
 {
 	struct nvm_target *tgt;
@@ -501,6 +503,7 @@ static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
 	}
 	__nvm_remove_target(t, true);
 	mutex_unlock(&dev->mlock);
+	kref_put(&dev->ref, nvm_free);
 
 	return 0;
 }
@@ -1094,15 +1097,16 @@ static int nvm_core_init(struct nvm_dev *dev)
 	return ret;
 }
 
-static void nvm_free(struct nvm_dev *dev)
+static void nvm_free(struct kref *ref)
 {
-	if (!dev)
-		return;
+	struct nvm_dev *dev = container_of(ref, struct nvm_dev, ref);
 
 	if (dev->dma_pool)
 		dev->ops->destroy_dma_pool(dev->dma_pool);
 
-	nvm_unregister_map(dev);
+	if (dev->rmap)
+		nvm_unregister_map(dev);
+
 	kfree(dev->lun_map);
 	kfree(dev);
 }
@@ -1139,7 +1143,13 @@ static int nvm_init(struct nvm_dev *dev)
 
 struct nvm_dev *nvm_alloc_dev(int node)
 {
-	return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
+	struct nvm_dev *dev;
+
+	dev = kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
+	if (dev)
+		kref_init(&dev->ref);
+
+	return dev;
 }
 EXPORT_SYMBOL(nvm_alloc_dev);
 
@@ -1147,12 +1157,16 @@ int nvm_register(struct nvm_dev *dev)
 {
 	int ret, exp_pool_size;
 
-	if (!dev->q || !dev->ops)
+	if (!dev->q || !dev->ops) {
+		kref_put(&dev->ref, nvm_free);
 		return -EINVAL;
+	}
 
 	ret = nvm_init(dev);
-	if (ret)
+	if (ret) {
+		kref_put(&dev->ref, nvm_free);
 		return ret;
+	}
 
 	exp_pool_size = max_t(int, PAGE_SIZE,
 			      (NVM_MAX_VLBA * (sizeof(u64) + dev->geo.sos)));
@@ -1162,7 +1176,7 @@ int nvm_register(struct nvm_dev *dev)
 						  exp_pool_size);
 	if (!dev->dma_pool) {
 		pr_err("nvm: could not create dma pool\n");
-		nvm_free(dev);
+		kref_put(&dev->ref, nvm_free);
 		return -ENOMEM;
 	}
 
@@ -1184,6 +1198,7 @@ void nvm_unregister(struct nvm_dev *dev)
 		if (t->dev->parent != dev)
 			continue;
 		__nvm_remove_target(t, false);
+		kref_put(&dev->ref, nvm_free);
 	}
 	mutex_unlock(&dev->mlock);
 
@@ -1191,13 +1206,14 @@ void nvm_unregister(struct nvm_dev *dev)
 	list_del(&dev->devices);
 	up_write(&nvm_lock);
 
-	nvm_free(dev);
+	kref_put(&dev->ref, nvm_free);
 }
 EXPORT_SYMBOL(nvm_unregister);
 
 static int __nvm_configure_create(struct nvm_ioctl_create *create)
 {
 	struct nvm_dev *dev;
+	int ret;
 
 	down_write(&nvm_lock);
 	dev = nvm_find_nvm_dev(create->dev);
@@ -1208,7 +1224,12 @@ static int __nvm_configure_create(struct nvm_ioctl_create *create)
 		return -EINVAL;
 	}
 
-	return nvm_create_tgt(dev, create);
+	kref_get(&dev->ref);
+	ret = nvm_create_tgt(dev, create);
+	if (ret)
+		kref_put(&dev->ref, nvm_free);
+
+	return ret;
 }
 
 static long nvm_ioctl_info(struct file *file, void __user *arg)
diff --git a/include/linux/lightnvm.h b/include/linux/lightnvm.h
index d3b02708e5f0..4d0d5655c7b2 100644
--- a/include/linux/lightnvm.h
+++ b/include/linux/lightnvm.h
@@ -428,6 +428,7 @@ struct nvm_dev {
 	char name[DISK_NAME_LEN];
 	void *private_data;
 
+	struct kref ref;
 	void *rmap;
 
 	struct mutex mlock;
-- 
2.19.1


  parent reply	other threads:[~2019-05-04 18:39 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-04 18:37 [GIT PULL 00/26] lightnvm updates for 5.2 Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 01/26] lightnvm: pblk: line reference fix in GC Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 02/26] lightnvm: pblk: rollback on error during gc read Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 03/26] lightnvm: pblk: reduce L2P memory footprint Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 04/26] lightnvm: pblk: remove unused smeta_ssec field Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 05/26] lightnvm: pblk: gracefully handle GC vmalloc fail Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 06/26] lightnvm: pblk: fix race during put line Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 07/26] lightnvm: pblk: ensure that erase is chunk aligned Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 08/26] lightnvm: pblk: cleanly fail when there is not enough memory Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 09/26] lightnvm: pblk: set proper read status in bio Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 10/26] lightnvm: Inherit mdts from the parent nvme device Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 11/26] lightnvm: pblk: fix bio leak when bio is split Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 12/26] lightnvm: pblk: set propper line as data_line after gc Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 13/26] lightnvm: prevent race condition on pblk remove Matias Bjørling
2019-05-04 18:37 ` [GIT PULL 14/26] lightnvm: pblk: fix lock order in pblk_rb_tear_down_check Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 15/26] lightnvm: pblk: kick writer on write recovery path Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 16/26] lightnvm: pblk: fix update line wp in OOB recovery Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 17/26] lightnvm: pblk: propagate errors when reading meta Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 18/26] lightnvm: pblk: wait for inflight IOs in recovery Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 19/26] lightnvm: pblk: remove internal IO timeout Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 20/26] lightnvm: pblk: GC error handling Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 21/26] lightnvm: pblk: IO path reorganization Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 22/26] lightnvm: pblk: recover only written metadata Matias Bjørling
2019-05-04 18:38 ` Matias Bjørling [this message]
2019-05-04 18:38 ` [GIT PULL 24/26] lightnvm: do not remove instance under global lock Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 25/26] lightnvm: pblk: simplify partial read path Matias Bjørling
2019-05-04 18:38 ` [GIT PULL 26/26] lightnvm: pblk: use nvm_rq_to_ppa_list() Matias Bjørling
2019-05-06 16:20 ` [GIT PULL 00/26] lightnvm updates for 5.2 Jens Axboe

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=20190504183811.18725-24-mb@lightnvm.io \
    --to=mb@lightnvm.io \
    --cc=axboe@fb.com \
    --cc=igor.j.konopko@intel.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).