Skip to content

Commit 1cd96c2

Browse files
Boaz HarroshJens Axboe
authored andcommitted
block: WARN in __blk_put_request() for potential bio leak
Put a WARN_ON in __blk_put_request if it is about to leak bio(s). This is a serious bug that can happen in error handling code paths. For this to work I have fixed a couple of places in block/ where request->bio != NULL ownership was not honored. And a small cleanup at sg_io() while at it. Signed-off-by: Boaz Harrosh <bharrosh@panasas.com> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
1 parent f028f3b commit 1cd96c2

3 files changed

Lines changed: 9 additions & 17 deletions

File tree

‎block/blk-core.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ void __blk_put_request(struct request_queue *q, struct request *req)
10621062

10631063
elv_completed_request(q, req);
10641064

1065+
/* this is a bio leak */
1066+
WARN_ON(req->bio != NULL);
1067+
10651068
/*
10661069
* Request may not have originated from ll_rw_blk. if not,
10671070
* it didn't come out of our reserved rq pools

‎block/blk-merge.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ static int attempt_merge(struct request_queue *q, struct request *req,
403403
if (blk_rq_cpu_valid(next))
404404
req->cpu = next->cpu;
405405

406+
/* owner-ship of bio passed from next to req */
407+
next->bio = NULL;
406408
__blk_put_request(q, next);
407409
return 1;
408410
}

‎block/scsi_ioctl.c‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,10 @@ static int blk_fill_sghdr_rq(struct request_queue *q, struct request *rq,
214214
return 0;
215215
}
216216

217-
/*
218-
* unmap a request that was previously mapped to this sg_io_hdr. handles
219-
* both sg and non-sg sg_io_hdr.
220-
*/
221-
static int blk_unmap_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr)
222-
{
223-
blk_rq_unmap_user(rq->bio);
224-
blk_put_request(rq);
225-
return 0;
226-
}
227-
228217
static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
229218
struct bio *bio)
230219
{
231-
int r, ret = 0;
220+
int ret = 0;
232221

233222
/*
234223
* fill in all the output members
@@ -253,12 +242,10 @@ static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
253242
ret = -EFAULT;
254243
}
255244

256-
rq->bio = bio;
257-
r = blk_unmap_sghdr_rq(rq, hdr);
258-
if (ret)
259-
r = ret;
245+
blk_rq_unmap_user(bio);
246+
blk_put_request(rq);
260247

261-
return r;
248+
return ret;
262249
}
263250

264251
static int sg_io(struct request_queue *q, struct gendisk *bd_disk,

0 commit comments

Comments
 (0)