Skip to content

Commit bd2df8d

Browse files
JackieLiu1axboe
authored andcommitted
block: free copied pages when blk_rq_map_kern() fails
bio_copy_kern() allocates pages that are normally freed by the bio completion callback. If blk_rq_append_bio() rejects the bio, however, blk_rq_map_kern() only drops the bio reference. Since bio_put() does not free pages referenced by the bio vectors, those pages leak. This can happen when the bio exceeds the queue segment constraints or when a later mapping cannot be merged into a request built by earlier calls. Track whether the buffer was copied and free those pages before dropping the rejected bio. Fixes: 3a5a392 ("block: allow blk_rq_map_kern to append to requests") Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Jackie Liu <liuyun01@kylinos.cn> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://patch.msgid.link/20260715073518.96042-1-liu.yun@linux.dev Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent daff723 commit bd2df8d

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

block/blk-map.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ int blk_rq_map_kern(struct request *rq, void *kbuf, unsigned int len,
653653
gfp_t gfp_mask)
654654
{
655655
unsigned long addr = (unsigned long) kbuf;
656+
bool do_copy;
656657
struct bio *bio;
657658
int ret;
658659

@@ -661,7 +662,8 @@ int blk_rq_map_kern(struct request *rq, void *kbuf, unsigned int len,
661662
if (!len || !kbuf)
662663
return -EINVAL;
663664

664-
if (!blk_rq_aligned(rq->q, addr, len) || object_is_on_stack(kbuf))
665+
do_copy = !blk_rq_aligned(rq->q, addr, len) || object_is_on_stack(kbuf);
666+
if (do_copy)
665667
bio = bio_copy_kern(rq, kbuf, len, gfp_mask);
666668
else
667669
bio = bio_map_kern(rq, kbuf, len, gfp_mask);
@@ -670,8 +672,11 @@ int blk_rq_map_kern(struct request *rq, void *kbuf, unsigned int len,
670672
return PTR_ERR(bio);
671673

672674
ret = blk_rq_append_bio(rq, bio);
673-
if (unlikely(ret))
675+
if (unlikely(ret)) {
676+
if (do_copy)
677+
bio_free_pages(bio);
674678
blk_mq_map_bio_put(bio);
679+
}
675680
return ret;
676681
}
677682
EXPORT_SYMBOL(blk_rq_map_kern);

0 commit comments

Comments
 (0)