linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ming Lei <ming.lei@redhat.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org,
	ZiyangZhang <ZiyangZhang@linux.alibaba.com>,
	Christoph Hellwig <hch@lst.de>, Ming Lei <ming.lei@redhat.com>
Subject: [PATCH V2 1/5] ublk_drv: avoid to leak ublk device in case that add_disk fails
Date: Wed, 27 Jul 2022 22:16:24 +0800	[thread overview]
Message-ID: <20220727141628.985429-2-ming.lei@redhat.com> (raw)
In-Reply-To: <20220727141628.985429-1-ming.lei@redhat.com>

->free_disk is only called after disk is added successfully, so
not hold ublk device reference count until add_disk is done.

Fixes: 6d9e6dfdf3b2 ("ublk: defer disk allocation")
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 drivers/block/ublk_drv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 255b2de46a24..b30d6c3355e8 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -215,8 +215,11 @@ static void ublk_free_disk(struct gendisk *disk)
 {
 	struct ublk_device *ub = disk->private_data;
 
-	clear_bit(UB_STATE_USED, &ub->state);
-	put_device(&ub->cdev_dev);
+	/* only called for added/used disk */
+	if (test_bit(UB_STATE_USED, &ub->state)) {
+		clear_bit(UB_STATE_USED, &ub->state);
+		put_device(&ub->cdev_dev);
+	}
 }
 
 static const struct block_device_operations ub_fops = {
@@ -1181,12 +1184,12 @@ static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd)
 
 	ub->dev_info.ublksrv_pid = ublksrv_pid;
 	ub->ub_disk = disk;
-	get_device(&ub->cdev_dev);
 	ret = add_disk(disk);
 	if (ret) {
 		put_disk(disk);
 		goto out_unlock;
 	}
+	get_device(&ub->cdev_dev);
 	set_bit(UB_STATE_USED, &ub->state);
 	ub->dev_info.state = UBLK_S_DEV_LIVE;
 out_unlock:
-- 
2.31.1


  reply	other threads:[~2022-07-27 14:16 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-27 14:16 [PATCH V2 0/5] ublk_drv: add generic mechanism to get/set parameters Ming Lei
2022-07-27 14:16 ` Ming Lei [this message]
2022-07-27 16:21   ` [PATCH V2 1/5] ublk_drv: avoid to leak ublk device in case that add_disk fails Christoph Hellwig
2022-07-28  0:18     ` Ming Lei
2022-07-28  2:57       ` Ziyang Zhang
2022-07-27 14:16 ` [PATCH V2 2/5] ublk_drv: cancel device even though disk isn't up Ming Lei
2022-07-27 14:16 ` [PATCH V2 3/5] ublk_drv: add SET_PARAM/GET_PARAM control command Ming Lei
2022-07-27 16:22   ` Christoph Hellwig
2022-07-28  1:56     ` Ming Lei
2022-07-28  4:38       ` Ming Lei
2022-07-27 14:16 ` [PATCH V2 4/5] ublk_drv: add two parameter types Ming Lei
2022-07-27 14:16 ` [PATCH V2 5/5] ublk_drv: cleanup ublksrv_ctrl_dev_info Ming Lei

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=20220727141628.985429-2-ming.lei@redhat.com \
    --to=ming.lei@redhat.com \
    --cc=ZiyangZhang@linux.alibaba.com \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=linux-block@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).