Skip to content

Commit a492f07

Browse files
Joe Lawrenceaxboe
authored andcommitted
block,scsi: fixup blk_get_request dead queue scenarios
The blk_get_request function may fail in low-memory conditions or during device removal (even if __GFP_WAIT is set). To distinguish between these errors, modify the blk_get_request call stack to return the appropriate ERR_PTR. Verify that all callers check the return status and consider IS_ERR instead of a simple NULL pointer check. For consistency, make a similar change to the blk_mq_alloc_request leg of blk_get_request. It may fail if the queue is dead, or the caller was unwilling to wait. Signed-off-by: Joe Lawrence <joe.lawrence@stratus.com> Acked-by: Jiri Kosina <jkosina@suse.cz> [for pktdvd] Acked-by: Boaz Harrosh <bharrosh@panasas.com> [for osd] Reviewed-by: Jeff Moyer <jmoyer@redhat.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent eb571ee commit a492f07

20 files changed

Lines changed: 55 additions & 51 deletions

File tree

‎block/blk-core.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,9 @@ static struct io_context *rq_ioc(struct bio *bio)
933933
* Get a free request from @q. This function may fail under memory
934934
* pressure or if @q is dead.
935935
*
936-
* Must be callled with @q->queue_lock held and,
937-
* Returns %NULL on failure, with @q->queue_lock held.
938-
* Returns !%NULL on success, with @q->queue_lock *not held*.
936+
* Must be called with @q->queue_lock held and,
937+
* Returns ERR_PTR on failure, with @q->queue_lock held.
938+
* Returns request pointer on success, with @q->queue_lock *not held*.
939939
*/
940940
static struct request *__get_request(struct request_list *rl, int rw_flags,
941941
struct bio *bio, gfp_t gfp_mask)
@@ -949,7 +949,7 @@ static struct request *__get_request(struct request_list *rl, int rw_flags,
949949
int may_queue;
950950

951951
if (unlikely(blk_queue_dying(q)))
952-
return NULL;
952+
return ERR_PTR(-ENODEV);
953953

954954
may_queue = elv_may_queue(q, rw_flags);
955955
if (may_queue == ELV_MQUEUE_NO)
@@ -974,7 +974,7 @@ static struct request *__get_request(struct request_list *rl, int rw_flags,
974974
* process is not a "batcher", and not
975975
* exempted by the IO scheduler
976976
*/
977-
return NULL;
977+
return ERR_PTR(-ENOMEM);
978978
}
979979
}
980980
}
@@ -992,7 +992,7 @@ static struct request *__get_request(struct request_list *rl, int rw_flags,
992992
* allocated with any setting of ->nr_requests
993993
*/
994994
if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
995-
return NULL;
995+
return ERR_PTR(-ENOMEM);
996996

997997
q->nr_rqs[is_sync]++;
998998
rl->count[is_sync]++;
@@ -1097,7 +1097,7 @@ static struct request *__get_request(struct request_list *rl, int rw_flags,
10971097
rq_starved:
10981098
if (unlikely(rl->count[is_sync] == 0))
10991099
rl->starved[is_sync] = 1;
1100-
return NULL;
1100+
return ERR_PTR(-ENOMEM);
11011101
}
11021102

11031103
/**
@@ -1110,9 +1110,9 @@ static struct request *__get_request(struct request_list *rl, int rw_flags,
11101110
* Get a free request from @q. If %__GFP_WAIT is set in @gfp_mask, this
11111111
* function keeps retrying under memory pressure and fails iff @q is dead.
11121112
*
1113-
* Must be callled with @q->queue_lock held and,
1114-
* Returns %NULL on failure, with @q->queue_lock held.
1115-
* Returns !%NULL on success, with @q->queue_lock *not held*.
1113+
* Must be called with @q->queue_lock held and,
1114+
* Returns ERR_PTR on failure, with @q->queue_lock held.
1115+
* Returns request pointer on success, with @q->queue_lock *not held*.
11161116
*/
11171117
static struct request *get_request(struct request_queue *q, int rw_flags,
11181118
struct bio *bio, gfp_t gfp_mask)
@@ -1125,12 +1125,12 @@ static struct request *get_request(struct request_queue *q, int rw_flags,
11251125
rl = blk_get_rl(q, bio); /* transferred to @rq on success */
11261126
retry:
11271127
rq = __get_request(rl, rw_flags, bio, gfp_mask);
1128-
if (rq)
1128+
if (!IS_ERR(rq))
11291129
return rq;
11301130

11311131
if (!(gfp_mask & __GFP_WAIT) || unlikely(blk_queue_dying(q))) {
11321132
blk_put_rl(rl);
1133-
return NULL;
1133+
return rq;
11341134
}
11351135

11361136
/* wait on @rl and retry */
@@ -1167,7 +1167,7 @@ static struct request *blk_old_get_request(struct request_queue *q, int rw,
11671167

11681168
spin_lock_irq(q->queue_lock);
11691169
rq = get_request(q, rw, NULL, gfp_mask);
1170-
if (!rq)
1170+
if (IS_ERR(rq))
11711171
spin_unlock_irq(q->queue_lock);
11721172
/* q->queue_lock is unlocked at this point */
11731173

@@ -1219,8 +1219,8 @@ struct request *blk_make_request(struct request_queue *q, struct bio *bio,
12191219
{
12201220
struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
12211221

1222-
if (unlikely(!rq))
1223-
return ERR_PTR(-ENOMEM);
1222+
if (IS_ERR(rq))
1223+
return rq;
12241224

12251225
blk_rq_set_block_pc(rq);
12261226

@@ -1615,8 +1615,8 @@ void blk_queue_bio(struct request_queue *q, struct bio *bio)
16151615
* Returns with the queue unlocked.
16161616
*/
16171617
req = get_request(q, rw_flags, bio, GFP_NOIO);
1618-
if (unlikely(!req)) {
1619-
bio_endio(bio, -ENODEV); /* @q is dead */
1618+
if (IS_ERR(req)) {
1619+
bio_endio(bio, PTR_ERR(req)); /* @q is dead */
16201620
goto out_unlock;
16211621
}
16221622

‎block/blk-mq.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,11 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
218218
struct blk_mq_hw_ctx *hctx;
219219
struct request *rq;
220220
struct blk_mq_alloc_data alloc_data;
221+
int ret;
221222

222-
if (blk_mq_queue_enter(q))
223-
return NULL;
223+
ret = blk_mq_queue_enter(q);
224+
if (ret)
225+
return ERR_PTR(ret);
224226

225227
ctx = blk_mq_get_ctx(q);
226228
hctx = q->mq_ops->map_queue(q, ctx->cpu);
@@ -240,6 +242,8 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
240242
ctx = alloc_data.ctx;
241243
}
242244
blk_mq_put_ctx(ctx);
245+
if (!rq)
246+
return ERR_PTR(-EWOULDBLOCK);
243247
return rq;
244248
}
245249
EXPORT_SYMBOL(blk_mq_alloc_request);

‎block/bsg.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr, fmode_t has_write_perm,
270270
* map scatter-gather elements separately and string them to request
271271
*/
272272
rq = blk_get_request(q, rw, GFP_KERNEL);
273-
if (!rq)
274-
return ERR_PTR(-ENOMEM);
273+
if (IS_ERR(rq))
274+
return rq;
275275
blk_rq_set_block_pc(rq);
276276

277277
ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, bd, has_write_perm);
@@ -285,8 +285,8 @@ bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr, fmode_t has_write_perm,
285285
}
286286

287287
next_rq = blk_get_request(q, READ, GFP_KERNEL);
288-
if (!next_rq) {
289-
ret = -ENOMEM;
288+
if (IS_ERR(next_rq)) {
289+
ret = PTR_ERR(next_rq);
290290
goto out;
291291
}
292292
rq->next_rq = next_rq;

‎block/scsi_ioctl.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ static int sg_io(struct request_queue *q, struct gendisk *bd_disk,
318318
at_head = 1;
319319

320320
rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
321-
if (!rq)
322-
return -ENOMEM;
321+
if (IS_ERR(rq))
322+
return PTR_ERR(rq);
323323
blk_rq_set_block_pc(rq);
324324

325325
if (blk_fill_sghdr_rq(q, rq, hdr, mode)) {
@@ -448,8 +448,8 @@ int sg_scsi_ioctl(struct request_queue *q, struct gendisk *disk, fmode_t mode,
448448
}
449449

450450
rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
451-
if (!rq) {
452-
err = -ENODEV;
451+
if (IS_ERR(rq)) {
452+
err = PTR_ERR(rq);
453453
goto error_free_buffer;
454454
}
455455

@@ -539,8 +539,8 @@ static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
539539
int err;
540540

541541
rq = blk_get_request(q, WRITE, __GFP_WAIT);
542-
if (!rq)
543-
return -ENODEV;
542+
if (IS_ERR(rq))
543+
return PTR_ERR(rq);
544544
blk_rq_set_block_pc(rq);
545545
rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
546546
rq->cmd[0] = cmd;

‎drivers/block/paride/pd.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ static int pd_special_command(struct pd_unit *disk,
722722
int err = 0;
723723

724724
rq = blk_get_request(disk->gd->queue, READ, __GFP_WAIT);
725-
if (!rq)
726-
return -ENODEV;
725+
if (IS_ERR(rq))
726+
return PTR_ERR(rq);
727727

728728
rq->cmd_type = REQ_TYPE_SPECIAL;
729729
rq->special = func;

‎drivers/block/pktcdvd.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *
704704

705705
rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
706706
WRITE : READ, __GFP_WAIT);
707-
if (!rq)
708-
return -ENODEV;
707+
if (IS_ERR(rq))
708+
return PTR_ERR(rq);
709709
blk_rq_set_block_pc(rq);
710710

711711
if (cgc->buflen) {

‎drivers/block/sx8.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ static struct carm_request *carm_get_special(struct carm_host *host)
568568
return NULL;
569569

570570
rq = blk_get_request(host->oob_q, WRITE /* bogus */, GFP_KERNEL);
571-
if (!rq) {
571+
if (IS_ERR(rq)) {
572572
spin_lock_irqsave(&host->lock, flags);
573573
carm_put_request(host, crq);
574574
spin_unlock_irqrestore(&host->lock, flags);

‎drivers/cdrom/cdrom.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,8 +2180,8 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
21802180
len = nr * CD_FRAMESIZE_RAW;
21812181

21822182
rq = blk_get_request(q, READ, GFP_KERNEL);
2183-
if (!rq) {
2184-
ret = -ENOMEM;
2183+
if (IS_ERR(rq)) {
2184+
ret = PTR_ERR(rq);
21852185
break;
21862186
}
21872187
blk_rq_set_block_pc(rq);

‎drivers/ide/ide-park.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
4646
* timeout has expired, so power management will be reenabled.
4747
*/
4848
rq = blk_get_request(q, READ, GFP_NOWAIT);
49-
if (unlikely(!rq))
49+
if (IS_ERR(rq))
5050
goto out;
5151

5252
rq->cmd[0] = REQ_UNPARK_HEADS;

‎drivers/scsi/device_handler/scsi_dh_alua.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static struct request *get_alua_req(struct scsi_device *sdev,
115115

116116
rq = blk_get_request(q, rw, GFP_NOIO);
117117

118-
if (!rq) {
118+
if (IS_ERR(rq)) {
119119
sdev_printk(KERN_INFO, sdev,
120120
"%s: blk_get_request failed\n", __func__);
121121
return NULL;

0 commit comments

Comments
 (0)