mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dan Williams <dan.j.williams@intel.com>
To: linux-nvdimm@ml01.01.org
Cc: Ross Zwisler <ross.zwisler@linux.intel.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 05/13] libnvdimm, blk: use devm_add_action to release bdev resources
Date: Wed, 23 Mar 2016 18:25:47 -0700	[thread overview]
Message-ID: <20160324012547.21436.51123.stgit@dwillia2-desk3.jf.intel.com> (raw)
In-Reply-To: <20160324012520.21436.22505.stgit@dwillia2-desk3.jf.intel.com>

Register a callback to clean up the request_queue and put the gendisk at
driver disable time.

Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 drivers/nvdimm/blk.c |   77 +++++++++++++++++++++++---------------------------
 1 file changed, 36 insertions(+), 41 deletions(-)

diff --git a/drivers/nvdimm/blk.c b/drivers/nvdimm/blk.c
index c8215dc356cc..27ff32a5e9cf 100644
--- a/drivers/nvdimm/blk.c
+++ b/drivers/nvdimm/blk.c
@@ -22,8 +22,6 @@
 #include "nd.h"
 
 struct nd_blk_device {
-	struct request_queue *queue;
-	struct gendisk *disk;
 	struct nd_namespace_blk *nsblk;
 	struct nd_blk_region *ndbr;
 	size_t disk_size;
@@ -235,29 +233,47 @@ static const struct block_device_operations nd_blk_fops = {
 	.revalidate_disk = nvdimm_revalidate_disk,
 };
 
-static int nd_blk_attach_disk(struct nd_namespace_common *ndns,
-		struct nd_blk_device *blk_dev)
+static void nd_blk_release_queue(void *q)
+{
+	blk_cleanup_queue(q);
+}
+
+static void nd_blk_release_disk(void *disk)
+{
+	del_gendisk(disk);
+	put_disk(disk);
+}
+
+static int nd_blk_attach_disk(struct device *dev,
+		struct nd_namespace_common *ndns, struct nd_blk_device *blk_dev)
 {
 	resource_size_t available_disk_size;
+	struct request_queue *q;
 	struct gendisk *disk;
 	u64 internal_nlba;
 
 	internal_nlba = div_u64(blk_dev->disk_size, blk_dev->internal_lbasize);
 	available_disk_size = internal_nlba * blk_dev->sector_size;
 
-	blk_dev->queue = blk_alloc_queue(GFP_KERNEL);
-	if (!blk_dev->queue)
+	q = blk_alloc_queue(GFP_KERNEL);
+	if (!q)
+		return -ENOMEM;
+	if (devm_add_action(dev, nd_blk_release_queue, q)) {
+		blk_cleanup_queue(q);
 		return -ENOMEM;
+	}
 
-	blk_queue_make_request(blk_dev->queue, nd_blk_make_request);
-	blk_queue_max_hw_sectors(blk_dev->queue, UINT_MAX);
-	blk_queue_bounce_limit(blk_dev->queue, BLK_BOUNCE_ANY);
-	blk_queue_logical_block_size(blk_dev->queue, blk_dev->sector_size);
-	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, blk_dev->queue);
+	blk_queue_make_request(q, nd_blk_make_request);
+	blk_queue_max_hw_sectors(q, UINT_MAX);
+	blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
+	blk_queue_logical_block_size(q, blk_dev->sector_size);
+	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
 
-	disk = blk_dev->disk = alloc_disk(0);
-	if (!disk) {
-		blk_cleanup_queue(blk_dev->queue);
+	disk = alloc_disk(0);
+	if (!disk)
+		return -ENOMEM;
+	if (devm_add_action(dev, nd_blk_release_disk, disk)) {
+		put_disk(disk);
 		return -ENOMEM;
 	}
 
@@ -265,7 +281,7 @@ static int nd_blk_attach_disk(struct nd_namespace_common *ndns,
 	disk->first_minor	= 0;
 	disk->fops		= &nd_blk_fops;
 	disk->private_data	= blk_dev;
-	disk->queue		= blk_dev->queue;
+	disk->queue		= q;
 	disk->flags		= GENHD_FL_EXT_DEVT;
 	nvdimm_namespace_disk_name(ndns, disk->disk_name);
 	set_capacity(disk, 0);
@@ -274,12 +290,8 @@ static int nd_blk_attach_disk(struct nd_namespace_common *ndns,
 	if (nd_blk_meta_size(blk_dev)) {
 		int rc = nd_integrity_init(disk, nd_blk_meta_size(blk_dev));
 
-		if (rc) {
-			del_gendisk(disk);
-			put_disk(disk);
-			blk_cleanup_queue(blk_dev->queue);
+		if (rc)
 			return rc;
-		}
 	}
 
 	set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
@@ -292,13 +304,12 @@ static int nd_blk_probe(struct device *dev)
 	struct nd_namespace_common *ndns;
 	struct nd_namespace_blk *nsblk;
 	struct nd_blk_device *blk_dev;
-	int rc;
 
 	ndns = nvdimm_namespace_common_probe(dev);
 	if (IS_ERR(ndns))
 		return PTR_ERR(ndns);
 
-	blk_dev = kzalloc(sizeof(*blk_dev), GFP_KERNEL);
+	blk_dev = devm_kzalloc(dev, sizeof(*blk_dev), GFP_KERNEL);
 	if (!blk_dev)
 		return -ENOMEM;
 
@@ -313,34 +324,18 @@ static int nd_blk_probe(struct device *dev)
 
 	ndns->rw_bytes = nd_blk_rw_bytes;
 	if (is_nd_btt(dev))
-		rc = nvdimm_namespace_attach_btt(ndns);
+		return nvdimm_namespace_attach_btt(ndns);
 	else if (nd_btt_probe(dev, ndns, blk_dev) == 0) {
 		/* we'll come back as btt-blk */
-		rc = -ENXIO;
+		return -ENXIO;
 	} else
-		rc = nd_blk_attach_disk(ndns, blk_dev);
-	if (rc)
-		kfree(blk_dev);
-	return rc;
-}
-
-static void nd_blk_detach_disk(struct nd_blk_device *blk_dev)
-{
-	del_gendisk(blk_dev->disk);
-	put_disk(blk_dev->disk);
-	blk_cleanup_queue(blk_dev->queue);
+		return nd_blk_attach_disk(dev, ndns, blk_dev);
 }
 
 static int nd_blk_remove(struct device *dev)
 {
-	struct nd_blk_device *blk_dev = dev_get_drvdata(dev);
-
 	if (is_nd_btt(dev))
 		nvdimm_namespace_detach_btt(to_nd_btt(dev));
-	else
-		nd_blk_detach_disk(blk_dev);
-	kfree(blk_dev);
-
 	return 0;
 }
 

  parent reply	other threads:[~2016-03-24  1:26 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-24  1:25 [PATCH 00/13] prep for device-dax, untangle pfn-device setup Dan Williams
2016-03-24  1:25 ` [PATCH 01/13] libnvdimm, pfn: fix nvdimm_namespace_add_poison() vs section alignment Dan Williams
2016-03-24 10:10   ` Johannes Thumshirn
2016-03-24 14:48     ` Dan Williams
2016-03-24  1:25 ` [PATCH 02/13] libnvdimm, pmem: kill pmem->ndns Dan Williams
2016-03-24 10:31   ` Johannes Thumshirn
2016-03-24  1:25 ` [PATCH 03/13] libnvdimm, pfn, convert nd_pfn_probe() to devm Dan Williams
2016-03-24 10:45   ` Johannes Thumshirn
2016-03-24  1:25 ` [PATCH 04/13] libnvdimm, btt, convert nd_btt_probe() " Dan Williams
2016-03-24 11:05   ` Johannes Thumshirn
2016-03-24  1:25 ` Dan Williams [this message]
2016-03-24 11:48   ` [PATCH 05/13] libnvdimm, blk: use devm_add_action to release bdev resources Johannes Thumshirn
2016-03-24 15:14     ` Dan Williams
2016-03-24 15:15       ` Johannes Thumshirn
2016-03-24 15:21         ` Dan Williams
2016-03-24  1:25 ` [PATCH 06/13] libnvdimm, blk: use ->queuedata for driver private data Dan Williams
2016-03-24 11:51   ` Johannes Thumshirn
2016-03-24  1:25 ` [PATCH 07/13] libnvdimm, pmem: " Dan Williams
2016-03-24 12:06   ` Johannes Thumshirn
2016-03-24  1:26 ` [PATCH 08/13] libnvdimm, blk: move i/o infrastructure to nd_namespace_blk Dan Williams
2016-03-24 12:22   ` Johannes Thumshirn
2016-03-24 15:21     ` Dan Williams
2016-03-24 15:22       ` Johannes Thumshirn
2016-03-25 22:02   ` [PATCH v2] " Dan Williams
2016-03-24  1:26 ` [PATCH 09/13] libnvdimm, pmem: use devm_add_action to release bdev resources Dan Williams
2016-03-24 12:35   ` Johannes Thumshirn
2016-03-24  1:26 ` [PATCH 10/13] libnvdimm, pmem: clean up resource print / request Dan Williams
2016-03-24 13:42   ` Johannes Thumshirn
2016-03-24  1:26 ` [PATCH 11/13] libnvdimm, pmem, pfn: make pmem_rw_bytes generic and refactor pfn setup Dan Williams
2016-03-24 13:50   ` Johannes Thumshirn
2016-03-25 22:13   ` [PATCH v2] " Dan Williams
2016-03-24  1:26 ` [PATCH 12/13] libnvdimm, pmem, pfn: move pfn setup to the core Dan Williams
2016-03-24 14:36   ` Johannes Thumshirn
2016-03-24 15:26     ` Dan Williams
2016-03-25 22:15   ` [PATCH v2] " Dan Williams
2016-03-29  8:30     ` Johannes Thumshirn
2016-03-24  1:26 ` [PATCH 13/13] libnvdimm, pmem: kill ->pmem_queue and ->pmem_disk Dan Williams
2016-03-24 14:38   ` Johannes Thumshirn

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=20160324012547.21436.51123.stgit@dwillia2-desk3.jf.intel.com \
    --to=dan.j.williams@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvdimm@ml01.01.org \
    --cc=ross.zwisler@linux.intel.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®