Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 33 additions & 53 deletions drivers/block/null_blk/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,25 +421,15 @@ static int nullb_update_nr_hw_queues(struct nullb_device *dev,
static int nullb_apply_submit_queues(struct nullb_device *dev,
unsigned int submit_queues)
{
int ret;

mutex_lock(&lock);
ret = nullb_update_nr_hw_queues(dev, submit_queues, dev->poll_queues);
mutex_unlock(&lock);

return ret;
guard(mutex)(&lock);
return nullb_update_nr_hw_queues(dev, submit_queues, dev->poll_queues);
}

static int nullb_apply_poll_queues(struct nullb_device *dev,
unsigned int poll_queues)
{
int ret;

mutex_lock(&lock);
ret = nullb_update_nr_hw_queues(dev, dev->submit_queues, poll_queues);
mutex_unlock(&lock);

return ret;
guard(mutex)(&lock);
return nullb_update_nr_hw_queues(dev, dev->submit_queues, poll_queues);
}

NULLB_DEVICE_ATTR(size, ulong, NULL);
Expand Down Expand Up @@ -493,15 +483,15 @@ static ssize_t nullb_device_power_store(struct config_item *item,
return ret;

ret = count;
mutex_lock(&lock);
guard(mutex)(&lock);
if (!dev->power && newp) {
if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
goto out;
return ret;

ret = null_add_dev(dev);
if (ret) {
clear_bit(NULLB_DEV_FL_UP, &dev->flags);
goto out;
return ret;
}

set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
Expand All @@ -515,8 +505,6 @@ static ssize_t nullb_device_power_store(struct config_item *item,
clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
}

out:
mutex_unlock(&lock);
return ret;
}

Expand Down Expand Up @@ -707,10 +695,9 @@ nullb_group_drop_item(struct config_group *group, struct config_item *item)
struct nullb_device *dev = to_nullb_device(item);

if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
mutex_lock(&lock);
guard(mutex)(&lock);
dev->power = false;
null_del_dev(dev->nullb);
mutex_unlock(&lock);
}
nullb_del_fault_config(dev);
config_item_put(item);
Expand Down Expand Up @@ -1205,7 +1192,7 @@ blk_status_t null_handle_discard(struct nullb_device *dev,
size_t n = nr_sectors << SECTOR_SHIFT;
size_t temp;

spin_lock_irq(&nullb->lock);
guard(spinlock_irq)(&nullb->lock);
while (n > 0) {
temp = min_t(size_t, n, dev->blocksize);
null_free_sector(nullb, sector, false);
Expand All @@ -1214,7 +1201,6 @@ blk_status_t null_handle_discard(struct nullb_device *dev,
sector += temp >> SECTOR_SHIFT;
n -= temp;
}
spin_unlock_irq(&nullb->lock);

return BLK_STS_OK;
}
Expand All @@ -1226,7 +1212,7 @@ static blk_status_t null_handle_flush(struct nullb *nullb)
if (!null_cache_active(nullb))
return 0;

spin_lock_irq(&nullb->lock);
guard(spinlock_irq)(&nullb->lock);
while (true) {
err = null_make_cache_space(nullb,
nullb->dev->cache_size * 1024 * 1024);
Expand All @@ -1235,7 +1221,6 @@ static blk_status_t null_handle_flush(struct nullb *nullb)
}

WARN_ON(!radix_tree_empty(&nullb->dev->cache));
spin_unlock_irq(&nullb->lock);
return errno_to_blk_status(err);
}

Expand Down Expand Up @@ -1292,7 +1277,7 @@ static blk_status_t null_handle_data_transfer(struct nullb_cmd *cmd,
struct req_iterator iter;
struct bio_vec bvec;

spin_lock_irq(&nullb->lock);
guard(spinlock_irq)(&nullb->lock);
rq_for_each_segment(bvec, rq, iter) {
len = bvec.bv_len;
if (transferred_bytes + len > max_bytes)
Expand All @@ -1307,7 +1292,6 @@ static blk_status_t null_handle_data_transfer(struct nullb_cmd *cmd,
if (transferred_bytes >= max_bytes)
break;
}
spin_unlock_irq(&nullb->lock);

return err;
}
Expand Down Expand Up @@ -1592,11 +1576,11 @@ static int null_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
int nr = 0;
struct request *rq;

spin_lock(&nq->poll_lock);
list_splice_init(&nq->poll_list, &list);
list_for_each_entry(rq, &list, queuelist)
blk_mq_set_request_complete(rq);
spin_unlock(&nq->poll_lock);
scoped_guard(spinlock, &nq->poll_lock) {
list_splice_init(&nq->poll_list, &list);
list_for_each_entry(rq, &list, queuelist)
blk_mq_set_request_complete(rq);
}

while (!list_empty(&list)) {
struct nullb_cmd *cmd;
Expand Down Expand Up @@ -1624,14 +1608,12 @@ static enum blk_eh_timer_return null_timeout_rq(struct request *rq)
if (hctx->type == HCTX_TYPE_POLL) {
struct nullb_queue *nq = hctx->driver_data;

spin_lock(&nq->poll_lock);
/* The request may have completed meanwhile. */
if (blk_mq_request_completed(rq)) {
spin_unlock(&nq->poll_lock);
return BLK_EH_DONE;
scoped_guard(spinlock, &nq->poll_lock) {
/* The request may have completed meanwhile. */
if (blk_mq_request_completed(rq))
return BLK_EH_DONE;
list_del_init(&rq->queuelist);
}
list_del_init(&rq->queuelist);
spin_unlock(&nq->poll_lock);
}

pr_info("rq %p timed out\n", rq);
Expand Down Expand Up @@ -1692,9 +1674,9 @@ static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
blk_mq_start_request(rq);

if (is_poll) {
spin_lock(&nq->poll_lock);
list_add_tail(&rq->queuelist, &nq->poll_list);
spin_unlock(&nq->poll_lock);
scoped_guard(spinlock, &nq->poll_lock) {
list_add_tail(&rq->queuelist, &nq->poll_list);
}
return BLK_STS_OK;
}
if (cmd->fake_timeout)
Expand Down Expand Up @@ -2081,14 +2063,13 @@ static struct nullb *null_find_dev_by_name(const char *name)
{
struct nullb *nullb = NULL, *nb;

mutex_lock(&lock);
guard(mutex)(&lock);
list_for_each_entry(nb, &nullb_list, list) {
if (strcmp(nb->disk_name, name) == 0) {
nullb = nb;
break;
}
}
mutex_unlock(&lock);

return nullb;
}
Expand All @@ -2101,10 +2082,9 @@ static int null_create_dev(void)
dev = null_alloc_dev();
if (!dev)
return -ENOMEM;

mutex_lock(&lock);
ret = null_add_dev(dev);
mutex_unlock(&lock);
scoped_guard(mutex, &lock) {
ret = null_add_dev(dev);
}
if (ret) {
null_free_dev(dev);
return ret;
Expand Down Expand Up @@ -2202,12 +2182,12 @@ static void __exit null_exit(void)

unregister_blkdev(null_major, "nullb");

mutex_lock(&lock);
while (!list_empty(&nullb_list)) {
nullb = list_entry(nullb_list.next, struct nullb, list);
null_destroy_dev(nullb);
scoped_guard(mutex, &lock) {
while (!list_empty(&nullb_list)) {
nullb = list_entry(nullb_list.next, struct nullb, list);
null_destroy_dev(nullb);
}
}
mutex_unlock(&lock);

if (tag_set.ops)
blk_mq_free_tag_set(&tag_set);
Expand Down
1 change: 1 addition & 0 deletions drivers/block/null_blk/null_blk.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <linux/fault-inject.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
#include <linux/cleanup.h>

struct nullb_cmd {
blk_status_t error;
Expand Down
19 changes: 4 additions & 15 deletions drivers/block/null_blk/zoned.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,12 @@ static blk_status_t null_open_zone(struct nullb_device *dev,
}

if (dev->need_zone_res_mgmt) {
spin_lock(&dev->zone_res_lock);
guard(spinlock)(&dev->zone_res_lock);

switch (zone->cond) {
case BLK_ZONE_COND_EMPTY:
ret = null_check_zone_resources(dev, zone);
if (ret != BLK_STS_OK) {
spin_unlock(&dev->zone_res_lock);
return ret;
}
break;
Expand All @@ -487,7 +486,6 @@ static blk_status_t null_open_zone(struct nullb_device *dev,
case BLK_ZONE_COND_CLOSED:
ret = null_check_zone_resources(dev, zone);
if (ret != BLK_STS_OK) {
spin_unlock(&dev->zone_res_lock);
return ret;
}
dev->nr_zones_closed--;
Expand All @@ -497,8 +495,6 @@ static blk_status_t null_open_zone(struct nullb_device *dev,
}

dev->nr_zones_exp_open++;

spin_unlock(&dev->zone_res_lock);
}

zone->cond = BLK_ZONE_COND_EXP_OPEN;
Expand Down Expand Up @@ -526,7 +522,7 @@ static blk_status_t null_close_zone(struct nullb_device *dev,
}

if (dev->need_zone_res_mgmt) {
spin_lock(&dev->zone_res_lock);
guard(spinlock)(&dev->zone_res_lock);

switch (zone->cond) {
case BLK_ZONE_COND_IMP_OPEN:
Expand All @@ -542,7 +538,6 @@ static blk_status_t null_close_zone(struct nullb_device *dev,
if (zone->wp > zone->start)
dev->nr_zones_closed++;

spin_unlock(&dev->zone_res_lock);
}

if (zone->wp == zone->start)
Expand All @@ -562,17 +557,15 @@ static blk_status_t null_finish_zone(struct nullb_device *dev,
return BLK_STS_IOERR;

if (dev->need_zone_res_mgmt) {
spin_lock(&dev->zone_res_lock);
guard(spinlock)(&dev->zone_res_lock);

switch (zone->cond) {
case BLK_ZONE_COND_FULL:
/* Finish operation on full is not an error */
spin_unlock(&dev->zone_res_lock);
return BLK_STS_OK;
case BLK_ZONE_COND_EMPTY:
ret = null_check_zone_resources(dev, zone);
if (ret != BLK_STS_OK) {
spin_unlock(&dev->zone_res_lock);
return ret;
}
break;
Expand All @@ -585,13 +578,11 @@ static blk_status_t null_finish_zone(struct nullb_device *dev,
case BLK_ZONE_COND_CLOSED:
ret = null_check_zone_resources(dev, zone);
if (ret != BLK_STS_OK) {
spin_unlock(&dev->zone_res_lock);
return ret;
}
dev->nr_zones_closed--;
break;
default:
spin_unlock(&dev->zone_res_lock);
return BLK_STS_IOERR;
}

Expand All @@ -611,7 +602,7 @@ static blk_status_t null_reset_zone(struct nullb_device *dev,
return BLK_STS_IOERR;

if (dev->need_zone_res_mgmt) {
spin_lock(&dev->zone_res_lock);
guard(spinlock)(&dev->zone_res_lock);

switch (zone->cond) {
case BLK_ZONE_COND_IMP_OPEN:
Expand All @@ -627,11 +618,9 @@ static blk_status_t null_reset_zone(struct nullb_device *dev,
case BLK_ZONE_COND_FULL:
break;
default:
spin_unlock(&dev->zone_res_lock);
return BLK_STS_IOERR;
}

spin_unlock(&dev->zone_res_lock);
}

zone->cond = BLK_ZONE_COND_EMPTY;
Expand Down
Loading