Skip to content

Commit

Permalink
blk-mq: allow blk_mq_init_commands() to return failure
Browse files Browse the repository at this point in the history
If drivers do dynamic allocation in the hardware command init
path, then we need to be able to handle and return failures.

And if they do allocations or mappings in the init command path,
then we need a cleanup function to free up that space at exit
time. So add blk_mq_free_commands() as the cleanup function.

This is required for the mtip32xx driver conversion to blk-mq.

Signed-off-by: Jens Axboe <axboe@fb.com>
  • Loading branch information
Jens Axboe committed Mar 14, 2014
1 parent 89f8b33 commit 95363ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
52 changes: 45 additions & 7 deletions block/blk-mq.c
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,46 @@ static void blk_mq_hctx_notify(void *data, unsigned long action,
blk_mq_put_ctx(ctx);
}

static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
void (*init)(void *, struct blk_mq_hw_ctx *,
static int blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
int (*init)(void *, struct blk_mq_hw_ctx *,
struct request *, unsigned int),
void *data)
{
unsigned int i;
int ret = 0;

for (i = 0; i < hctx->queue_depth; i++) {
struct request *rq = hctx->rqs[i];

ret = init(data, hctx, rq, i);
if (ret)
break;
}

return ret;
}

int blk_mq_init_commands(struct request_queue *q,
int (*init)(void *, struct blk_mq_hw_ctx *,
struct request *, unsigned int),
void *data)
{
struct blk_mq_hw_ctx *hctx;
unsigned int i;
int ret = 0;

queue_for_each_hw_ctx(q, hctx, i) {
ret = blk_mq_init_hw_commands(hctx, init, data);
if (ret)
break;
}

return ret;
}
EXPORT_SYMBOL(blk_mq_init_commands);

static void blk_mq_free_hw_commands(struct blk_mq_hw_ctx *hctx,
void (*free)(void *, struct blk_mq_hw_ctx *,
struct request *, unsigned int),
void *data)
{
Expand All @@ -1068,22 +1106,22 @@ static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
for (i = 0; i < hctx->queue_depth; i++) {
struct request *rq = hctx->rqs[i];

init(data, hctx, rq, i);
free(data, hctx, rq, i);
}
}

void blk_mq_init_commands(struct request_queue *q,
void (*init)(void *, struct blk_mq_hw_ctx *,
void blk_mq_free_commands(struct request_queue *q,
void (*free)(void *, struct blk_mq_hw_ctx *,
struct request *, unsigned int),
void *data)
{
struct blk_mq_hw_ctx *hctx;
unsigned int i;

queue_for_each_hw_ctx(q, hctx, i)
blk_mq_init_hw_commands(hctx, init, data);
blk_mq_free_hw_commands(hctx, free, data);
}
EXPORT_SYMBOL(blk_mq_init_commands);
EXPORT_SYMBOL(blk_mq_free_commands);

static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
{
Expand Down
3 changes: 2 additions & 1 deletion drivers/block/virtio_blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,14 @@ static struct blk_mq_reg virtio_mq_reg = {
.flags = BLK_MQ_F_SHOULD_MERGE,
};

static void virtblk_init_vbr(void *data, struct blk_mq_hw_ctx *hctx,
static int virtblk_init_vbr(void *data, struct blk_mq_hw_ctx *hctx,
struct request *rq, unsigned int nr)
{
struct virtio_blk *vblk = data;
struct virtblk_req *vbr = rq->special;

sg_init_table(vbr->sg, vblk->sg_elems);
return 0;
}

static int virtblk_probe(struct virtio_device *vdev)
Expand Down
3 changes: 2 additions & 1 deletion include/linux/blk-mq.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ enum {
struct request_queue *blk_mq_init_queue(struct blk_mq_reg *, void *);
int blk_mq_register_disk(struct gendisk *);
void blk_mq_unregister_disk(struct gendisk *);
void blk_mq_init_commands(struct request_queue *, void (*init)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
int blk_mq_init_commands(struct request_queue *, int (*init)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
void blk_mq_free_commands(struct request_queue *, void (*free)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);

void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);

Expand Down

0 comments on commit 95363ef

Please sign in to comment.