linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Jens Axboe <axboe@kernel.dk>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	Chris Zankel <chris@zankel.net>,
	Max Filippov <jcmvbkbc@gmail.com>,
	Philipp Reisner <philipp.reisner@linbit.com>,
	Lars Ellenberg <lars.ellenberg@linbit.com>,
	Jim Paris <jim@jtan.com>,
	Joshua Morris <josh.h.morris@us.ibm.com>,
	Philip Kelleher <pjk1939@linux.ibm.com>,
	Minchan Kim <minchan@kernel.org>, Nitin Gupta <ngupta@vflare.org>,
	Matias Bjorling <mb@lightnvm.io>, Coly Li <colyli@suse.de>,
	Mike Snitzer <snitzer@redhat.com>, Song Liu <song@kernel.org>,
	Maxim Levitsky <maximlevitsky@gmail.com>,
	Alex Dubov <oakad@yahoo.com>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	Dan Williams <dan.j.williams@intel.com>,
	Vishal Verma <vishal.l.verma@intel.com>,
	Dave Jiang <dave.jiang@intel.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Christian Borntraeger <borntraeger@de.ibm.com>
Cc: linux-block@vger.kernel.org, dm-devel@redhat.com,
	linux-m68k@lists.linux-m68k.org, linux-xtensa@linux-xtensa.org,
	drbd-dev@lists.linbit.com, linuxppc-dev@lists.ozlabs.org,
	linux-bcache@vger.kernel.org, linux-raid@vger.kernel.org,
	linux-mmc@vger.kernel.org, nvdimm@lists.linux.dev,
	linux-nvme@lists.infradead.org, linux-s390@vger.kernel.org
Subject: [PATCH 06/26] brd: convert to blk_alloc_disk/blk_cleanup_disk
Date: Fri, 21 May 2021 07:50:56 +0200	[thread overview]
Message-ID: <20210521055116.1053587-7-hch@lst.de> (raw)
In-Reply-To: <20210521055116.1053587-1-hch@lst.de>

Convert the brd driver to use the blk_alloc_disk and blk_cleanup_disk
helpers to simplify gendisk and request_queue allocation.  This also
allows to remove the request_queue pointer in struct request_queue,
and to simplify the initialization as blk_cleanup_disk can be called
on any disk returned from blk_alloc_disk.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/block/brd.c | 94 ++++++++++++++++-----------------------------
 1 file changed, 33 insertions(+), 61 deletions(-)

diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 7562cf30b14e..95694113e38e 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -38,9 +38,7 @@
  * device).
  */
 struct brd_device {
-	int		brd_number;
-
-	struct request_queue	*brd_queue;
+	int			brd_number;
 	struct gendisk		*brd_disk;
 	struct list_head	brd_list;
 
@@ -372,7 +370,7 @@ static LIST_HEAD(brd_devices);
 static DEFINE_MUTEX(brd_devices_mutex);
 static struct dentry *brd_debugfs_dir;
 
-static struct brd_device *brd_alloc(int i)
+static int brd_alloc(int i)
 {
 	struct brd_device *brd;
 	struct gendisk *disk;
@@ -380,64 +378,55 @@ static struct brd_device *brd_alloc(int i)
 
 	brd = kzalloc(sizeof(*brd), GFP_KERNEL);
 	if (!brd)
-		goto out;
+		return -ENOMEM;
 	brd->brd_number		= i;
 	spin_lock_init(&brd->brd_lock);
 	INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
 
-	brd->brd_queue = blk_alloc_queue(NUMA_NO_NODE);
-	if (!brd->brd_queue)
-		goto out_free_dev;
-
 	snprintf(buf, DISK_NAME_LEN, "ram%d", i);
 	if (!IS_ERR_OR_NULL(brd_debugfs_dir))
 		debugfs_create_u64(buf, 0444, brd_debugfs_dir,
 				&brd->brd_nr_pages);
 
-	/* This is so fdisk will align partitions on 4k, because of
-	 * direct_access API needing 4k alignment, returning a PFN
-	 * (This is only a problem on very small devices <= 4M,
-	 *  otherwise fdisk will align on 1M. Regardless this call
-	 *  is harmless)
-	 */
-	blk_queue_physical_block_size(brd->brd_queue, PAGE_SIZE);
-	disk = brd->brd_disk = alloc_disk(max_part);
+	disk = brd->brd_disk = blk_alloc_disk(NUMA_NO_NODE);
 	if (!disk)
-		goto out_free_queue;
+		goto out_free_dev;
+
 	disk->major		= RAMDISK_MAJOR;
 	disk->first_minor	= i * max_part;
+	disk->minors		= max_part;
 	disk->fops		= &brd_fops;
 	disk->private_data	= brd;
 	disk->flags		= GENHD_FL_EXT_DEVT;
 	strlcpy(disk->disk_name, buf, DISK_NAME_LEN);
 	set_capacity(disk, rd_size * 2);
+	
+	/*
+	 * This is so fdisk will align partitions on 4k, because of
+	 * direct_access API needing 4k alignment, returning a PFN
+	 * (This is only a problem on very small devices <= 4M,
+	 *  otherwise fdisk will align on 1M. Regardless this call
+	 *  is harmless)
+	 */
+	blk_queue_physical_block_size(disk->queue, PAGE_SIZE);
 
 	/* Tell the block layer that this is not a rotational device */
-	blk_queue_flag_set(QUEUE_FLAG_NONROT, brd->brd_queue);
-	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, brd->brd_queue);
+	blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
+	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
+	add_disk(disk);
+	list_add_tail(&brd->brd_list, &brd_devices);
 
-	return brd;
+	return 0;
 
-out_free_queue:
-	blk_cleanup_queue(brd->brd_queue);
 out_free_dev:
 	kfree(brd);
-out:
-	return NULL;
-}
-
-static void brd_free(struct brd_device *brd)
-{
-	put_disk(brd->brd_disk);
-	blk_cleanup_queue(brd->brd_queue);
-	brd_free_pages(brd);
-	kfree(brd);
+	return -ENOMEM;
 }
 
 static void brd_probe(dev_t dev)
 {
-	struct brd_device *brd;
 	int i = MINOR(dev) / max_part;
+	struct brd_device *brd;
 
 	mutex_lock(&brd_devices_mutex);
 	list_for_each_entry(brd, &brd_devices, brd_list) {
@@ -445,13 +434,7 @@ static void brd_probe(dev_t dev)
 			goto out_unlock;
 	}
 
-	brd = brd_alloc(i);
-	if (brd) {
-		brd->brd_disk->queue = brd->brd_queue;
-		add_disk(brd->brd_disk);
-		list_add_tail(&brd->brd_list, &brd_devices);
-	}
-
+	brd_alloc(i);
 out_unlock:
 	mutex_unlock(&brd_devices_mutex);
 }
@@ -460,7 +443,9 @@ static void brd_del_one(struct brd_device *brd)
 {
 	list_del(&brd->brd_list);
 	del_gendisk(brd->brd_disk);
-	brd_free(brd);
+	blk_cleanup_disk(brd->brd_disk);
+	brd_free_pages(brd);
+	kfree(brd);
 }
 
 static inline void brd_check_and_reset_par(void)
@@ -485,7 +470,7 @@ static inline void brd_check_and_reset_par(void)
 static int __init brd_init(void)
 {
 	struct brd_device *brd, *next;
-	int i;
+	int err, i;
 
 	/*
 	 * brd module now has a feature to instantiate underlying device
@@ -511,22 +496,11 @@ static int __init brd_init(void)
 
 	mutex_lock(&brd_devices_mutex);
 	for (i = 0; i < rd_nr; i++) {
-		brd = brd_alloc(i);
-		if (!brd)
+		err = brd_alloc(i);
+		if (err)
 			goto out_free;
-		list_add_tail(&brd->brd_list, &brd_devices);
 	}
 
-	/* point of no return */
-
-	list_for_each_entry(brd, &brd_devices, brd_list) {
-		/*
-		 * associate with queue just before adding disk for
-		 * avoiding to mess up failure path
-		 */
-		brd->brd_disk->queue = brd->brd_queue;
-		add_disk(brd->brd_disk);
-	}
 	mutex_unlock(&brd_devices_mutex);
 
 	pr_info("brd: module loaded\n");
@@ -535,15 +509,13 @@ static int __init brd_init(void)
 out_free:
 	debugfs_remove_recursive(brd_debugfs_dir);
 
-	list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
-		list_del(&brd->brd_list);
-		brd_free(brd);
-	}
+	list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
+		brd_del_one(brd);
 	mutex_unlock(&brd_devices_mutex);
 	unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
 
 	pr_info("brd: module NOT loaded !!!\n");
-	return -ENOMEM;
+	return err;
 }
 
 static void __exit brd_exit(void)
-- 
2.30.2


  parent reply	other threads:[~2021-05-21  5:52 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-21  5:50 simplify gendisk and request_queue allocation for bio based drivers Christoph Hellwig
2021-05-21  5:50 ` [PATCH 01/26] block: refactor device number setup in __device_add_disk Christoph Hellwig
2021-05-21 17:16   ` [dm-devel] " Luis Chamberlain
2021-05-24  7:20     ` Christoph Hellwig
2021-05-23  7:46   ` Hannes Reinecke
2021-05-24  7:22     ` Christoph Hellwig
2021-05-21  5:50 ` [PATCH 02/26] block: move the DISK_MAX_PARTS sanity check into __device_add_disk Christoph Hellwig
2021-05-21 17:18   ` [dm-devel] " Luis Chamberlain
2021-05-23  7:48   ` Hannes Reinecke
2021-05-21  5:50 ` [PATCH 03/26] block: automatically enable GENHD_FL_EXT_DEVT Christoph Hellwig
2021-05-21 17:22   ` [dm-devel] " Luis Chamberlain
2021-05-23  7:50   ` Hannes Reinecke
2021-05-21  5:50 ` [PATCH 04/26] block: add a flag to make put_disk on partially initalized disks safer Christoph Hellwig
2021-05-21 17:28   ` [dm-devel] " Luis Chamberlain
2021-05-23  7:54   ` Hannes Reinecke
2021-05-21  5:50 ` [PATCH 05/26] block: add blk_alloc_disk and blk_cleanup_disk APIs Christoph Hellwig
2021-05-21 17:44   ` [dm-devel] " Luis Chamberlain
2021-05-24  7:24     ` Christoph Hellwig
2021-05-23  7:55   ` Hannes Reinecke
2021-05-21  5:50 ` Christoph Hellwig [this message]
2021-05-23  7:58   ` [PATCH 06/26] brd: convert to blk_alloc_disk/blk_cleanup_disk Hannes Reinecke
2021-05-24  7:24     ` Christoph Hellwig
2021-05-21  5:50 ` [PATCH 07/26] drbd: " Christoph Hellwig
2021-05-23  7:59   ` Hannes Reinecke
2021-05-21  5:50 ` [PATCH 08/26] pktcdvd: " Christoph Hellwig
2021-05-23  8:00   ` Hannes Reinecke
2021-05-21  5:50 ` [PATCH 09/26] rsxx: " Christoph Hellwig
2021-05-23  8:01   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 10/26] zram: " Christoph Hellwig
2021-05-23  8:01   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 11/26] lightnvm: " Christoph Hellwig
2021-05-23  8:02   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 12/26] bcache: " Christoph Hellwig
2021-05-21  6:15   ` Coly Li
2021-05-21  6:23     ` Christoph Hellwig
2021-05-23 16:19       ` Coly Li
2021-05-23  8:04   ` Hannes Reinecke
2021-05-23 16:20   ` Coly Li
2021-05-21  5:51 ` [PATCH 13/26] dm: " Christoph Hellwig
2021-05-23  8:10   ` Hannes Reinecke
2021-05-24  7:25     ` Christoph Hellwig
2021-05-21  5:51 ` [PATCH 14/26] md: " Christoph Hellwig
2021-05-23  8:12   ` Hannes Reinecke
2021-05-24  7:26     ` Christoph Hellwig
2021-05-24  8:27       ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 15/26] nvdimm-blk: " Christoph Hellwig
2021-05-23  8:13   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 16/26] nvdimm-btt: " Christoph Hellwig
2021-05-23  8:14   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 17/26] nvdimm-pmem: " Christoph Hellwig
2021-05-23  8:14   ` Hannes Reinecke
2021-06-07  4:43   ` Dan Williams
2021-05-21  5:51 ` [PATCH 18/26] nvme-multipath: " Christoph Hellwig
2021-05-23  8:20   ` Hannes Reinecke
2021-05-24  7:29     ` Christoph Hellwig
2021-05-21  5:51 ` [PATCH 19/26] nfblock: " Christoph Hellwig
2021-05-21  8:37   ` Geert Uytterhoeven
2021-05-23  8:21   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 20/26] simdisk: " Christoph Hellwig
2021-05-23  8:22   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 21/26] n64cart: convert to blk_alloc_disk Christoph Hellwig
2021-05-23  8:22   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 22/26] ps3vram: convert to blk_alloc_disk/blk_cleanup_disk Christoph Hellwig
2021-05-23  8:23   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 23/26] dcssblk: " Christoph Hellwig
2021-05-23  8:23   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 24/26] xpram: " Christoph Hellwig
2021-05-23  8:24   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 25/26] null_blk: " Christoph Hellwig
2021-05-23  8:25   ` Hannes Reinecke
2021-05-21  5:51 ` [PATCH 26/26] block: unexport blk_alloc_queue Christoph Hellwig
2021-05-23  8:26   ` Hannes Reinecke
2021-05-25 22:41 ` simplify gendisk and request_queue allocation for bio based drivers Ulf Hansson
2021-05-26  4:49   ` Christoph Hellwig
2021-05-26  8:07     ` Ulf Hansson
2021-06-01 13:48 ` 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=20210521055116.1053587-7-hch@lst.de \
    --to=hch@lst.de \
    --cc=axboe@kernel.dk \
    --cc=borntraeger@de.ibm.com \
    --cc=chris@zankel.net \
    --cc=colyli@suse.de \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dm-devel@redhat.com \
    --cc=drbd-dev@lists.linbit.com \
    --cc=geert@linux-m68k.org \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=jcmvbkbc@gmail.com \
    --cc=jim@jtan.com \
    --cc=josh.h.morris@us.ibm.com \
    --cc=lars.ellenberg@linbit.com \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-m68k@lists.linux-m68k.org \
    --cc=linux-mmc@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-xtensa@linux-xtensa.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maximlevitsky@gmail.com \
    --cc=mb@lightnvm.io \
    --cc=minchan@kernel.org \
    --cc=ngupta@vflare.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=oakad@yahoo.com \
    --cc=philipp.reisner@linbit.com \
    --cc=pjk1939@linux.ibm.com \
    --cc=snitzer@redhat.com \
    --cc=song@kernel.org \
    --cc=ulf.hansson@linaro.org \
    --cc=vishal.l.verma@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;
as well as URLs for NNTP newsgroup(s).