From: Zizhi Wo <wozizhi@huaweicloud.com>
To: axboe@kernel.dk, dlemoal@kernel.org, nilay@linux.ibm.com,
kch@nvidia.com, johannes.thumshirn@wdc.com, kbusch@kernel.org,
bvanassche@acm.org, linux-block@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, yangerkun@huawei.com,
chengzhihao1@huawei.com, wozizhi@huawei.com
Subject: [PATCH V5 5/9] null_blk: free zones array on device power-off
Date: Tue, 14 Jul 2026 12:18:01 +0800 [thread overview]
Message-ID: <20260714041805.1088702-6-wozizhi@huaweicloud.com> (raw)
In-Reply-To: <20260714041805.1088702-1-wozizhi@huaweicloud.com>
From: Zizhi Wo <wozizhi@huawei.com>
null_init_zoned_dev() allocates dev->zones when a zoned device is powered
on, but null_del_dev() never frees it on power-off; dev->zones is only
freed later in null_free_dev(), when the configfs directory is removed. If
the device is powered off and then on again, null_init_zoned_dev()
allocates a new array and overwrites the dev->zones pointer, leaking the
previous allocation each power cycle.
Free dev->zones in null_del_dev() via null_free_zoned_dev() to solve it.
And calling null_free_zoned_dev() in null_free_dev() is no longer necessary
because every caller already invokes null_del_dev() first: via
nullb_group_drop_item() before nullb_device_release(), in the
null_add_dev() error path of null_create_dev(), and in null_destroy_dev().
Remove the redundant call.
And take &lock around zone_cond_store() in the two store wrappers to
serialize dev->zones check-and-deref against its alloc/free, which already
run under &lock. The reason there was no problem before is that only
nullb_device_release() or null_exit() frees the dev->zones, which
guarantees that subsequent users won't access the configfs interface.
Fixes: ca4b2a011948 ("null_blk: add zone support")
Assisted-by: Claude-Code:GLM-5.2
Signed-off-by: Zizhi Wo <wozizhi@huawei.com>
---
drivers/block/null_blk/main.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index df85189f0b69..e063c931dfca 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -579,8 +579,13 @@ static ssize_t nullb_device_zone_readonly_store(struct config_item *item,
const char *page, size_t count)
{
struct nullb_device *dev = to_nullb_device(item);
+ ssize_t ret;
+
+ mutex_lock(&lock);
+ ret = zone_cond_store(dev, page, count, BLK_ZONE_COND_READONLY);
+ mutex_unlock(&lock);
- return zone_cond_store(dev, page, count, BLK_ZONE_COND_READONLY);
+ return ret;
}
CONFIGFS_ATTR_WO(nullb_device_, zone_readonly);
@@ -588,8 +593,13 @@ static ssize_t nullb_device_zone_offline_store(struct config_item *item,
const char *page, size_t count)
{
struct nullb_device *dev = to_nullb_device(item);
+ ssize_t ret;
- return zone_cond_store(dev, page, count, BLK_ZONE_COND_OFFLINE);
+ mutex_lock(&lock);
+ ret = zone_cond_store(dev, page, count, BLK_ZONE_COND_OFFLINE);
+ mutex_unlock(&lock);
+
+ return ret;
}
CONFIGFS_ATTR_WO(nullb_device_, zone_offline);
@@ -836,7 +846,6 @@ static void null_free_dev(struct nullb_device *dev)
if (!dev)
return;
- null_free_zoned_dev(dev);
badblocks_exit(&dev->badblocks);
kfree(dev);
}
@@ -1777,6 +1786,7 @@ static void null_del_dev(struct nullb *nullb)
}
put_disk(nullb->disk);
+ null_free_zoned_dev(dev);
if (nullb->tag_set == &nullb->__tag_set)
blk_mq_free_tag_set(nullb->tag_set);
kfree(nullb->queues);
--
2.52.0
next prev parent reply other threads:[~2026-07-14 4:26 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-14 4:17 [PATCH V5 0/9] null_blk: fix init/exit races and memleaks Zizhi Wo
2026-07-14 4:17 ` [PATCH V5 1/9] null_blk: use DEFINE_MUTEX for the file-scope mutex Zizhi Wo
2026-07-14 4:17 ` [PATCH V5 2/9] null_blk: register configfs subsystem after creating default devices Zizhi Wo
2026-07-14 4:17 ` [PATCH V5 3/9] null_blk: move unregister_blkdev() after destroying dev in null_exit() Zizhi Wo
2026-07-14 4:18 ` [PATCH V5 4/9] null_blk: free global tag_set on init error path Zizhi Wo
2026-07-14 4:18 ` Zizhi Wo [this message]
2026-07-15 14:14 ` [PATCH V5 5/9] null_blk: free zones array on device power-off Nilay Shroff
2026-07-14 4:18 ` [PATCH V5 6/9] null_blk: clean up null_del_dev() to use cached dev pointer Zizhi Wo
2026-07-14 4:18 ` [PATCH V5 7/9] null_blk: reject per-device queue resize for shared tag set Zizhi Wo
2026-07-15 13:42 ` Nilay Shroff
2026-07-14 4:18 ` [PATCH V5 8/9] null_blk: serialize configfs attribute stores with device setup Zizhi Wo
2026-07-14 4:18 ` [PATCH V5 9/9] null_blk: serialize configfs attribute shows with the file-scope lock Zizhi Wo
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=20260714041805.1088702-6-wozizhi@huaweicloud.com \
--to=wozizhi@huaweicloud.com \
--cc=axboe@kernel.dk \
--cc=bvanassche@acm.org \
--cc=chengzhihao1@huawei.com \
--cc=dlemoal@kernel.org \
--cc=johannes.thumshirn@wdc.com \
--cc=kbusch@kernel.org \
--cc=kch@nvidia.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nilay@linux.ibm.com \
--cc=wozizhi@huawei.com \
--cc=yangerkun@huawei.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