block: ensure bios return from blk_get_request are properly initialized
authorChristoph Hellwig <hch@lst.de>
Tue, 19 Jul 2016 09:31:50 +0000 (11:31 +0200)
committerJens Axboe <axboe@fb.com>
Wed, 20 Jul 2016 23:38:30 +0000 (17:38 -0600)
blk_get_request is used for BLOCK_PC and similar passthrough requests.
Currently we always need to call blk_rq_set_block_pc or an open coded
version of it to allow appending bios using the request mapping helpers
later on, which is a somewhat awkward API.  Instead move the
initialization part of blk_rq_set_block_pc into blk_get_request, so that
we always have a safe to use request.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@fb.com>
block/blk-core.c
block/blk-mq.c
drivers/block/virtio_blk.c
drivers/nvme/host/core.c

index dd32563..4d87949 100644 (file)
@@ -1294,10 +1294,15 @@ static struct request *blk_old_get_request(struct request_queue *q, int rw,
 
        spin_lock_irq(q->queue_lock);
        rq = get_request(q, rw, 0, NULL, gfp_mask);
-       if (IS_ERR(rq))
+       if (IS_ERR(rq)) {
                spin_unlock_irq(q->queue_lock);
-       /* q->queue_lock is unlocked at this point */
+               return rq;
+       }
 
+       /* q->queue_lock is unlocked at this point */
+       rq->__data_len = 0;
+       rq->__sector = (sector_t) -1;
+       rq->bio = rq->biotail = NULL;
        return rq;
 }
 
@@ -1377,9 +1382,6 @@ EXPORT_SYMBOL(blk_make_request);
 void blk_rq_set_block_pc(struct request *rq)
 {
        rq->cmd_type = REQ_TYPE_BLOCK_PC;
-       rq->__data_len = 0;
-       rq->__sector = (sector_t) -1;
-       rq->bio = rq->biotail = NULL;
        memset(rq->__cmd, 0, sizeof(rq->__cmd));
 }
 EXPORT_SYMBOL(blk_rq_set_block_pc);
index 7aa60c4..8f4fac8 100644 (file)
@@ -263,6 +263,10 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
                blk_queue_exit(q);
                return ERR_PTR(-EWOULDBLOCK);
        }
+
+       rq->__data_len = 0;
+       rq->__sector = (sector_t) -1;
+       rq->bio = rq->biotail = NULL;
        return rq;
 }
 EXPORT_SYMBOL(blk_mq_alloc_request);
index a85a143..1523e05 100644 (file)
@@ -243,7 +243,6 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)
        req = blk_get_request(q, READ, GFP_KERNEL);
        if (IS_ERR(req))
                return PTR_ERR(req);
-       blk_rq_set_block_pc(req);
        req->cmd_type = REQ_TYPE_DRV_PRIV;
 
        err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
index cf4b750..9f0ec3b 100644 (file)
@@ -222,10 +222,6 @@ struct request *nvme_alloc_request(struct request_queue *q,
 
        req->cmd_type = REQ_TYPE_DRV_PRIV;
        req->cmd_flags |= REQ_FAILFAST_DRIVER;
-       req->__data_len = 0;
-       req->__sector = (sector_t) -1;
-       req->bio = req->biotail = NULL;
-
        req->cmd = (unsigned char *)cmd;
        req->cmd_len = sizeof(struct nvme_command);