* [PATCH] block: free copied pages when blk_rq_map_kern() fails
@ 2026-07-15 7:35 Jackie Liu
2026-07-16 9:23 ` Christoph Hellwig
2026-07-16 12:12 ` Jens Axboe
0 siblings, 2 replies; 3+ messages in thread
From: Jackie Liu @ 2026-07-15 7:35 UTC (permalink / raw)
To: axboe; +Cc: linux-block
From: Jackie Liu <liuyun01@kylinos.cn>
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: 3a5a39276d2a ("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>
---
block/blk-map.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/block/blk-map.c b/block/blk-map.c
index 768549f19f97..d1d6bbe0ecf1 100644
--- a/block/blk-map.c
+++ b/block/blk-map.c
@@ -653,6 +653,7 @@ int blk_rq_map_kern(struct request *rq, void *kbuf, unsigned int len,
gfp_t gfp_mask)
{
unsigned long addr = (unsigned long) kbuf;
+ bool do_copy;
struct bio *bio;
int ret;
@@ -661,7 +662,8 @@ int blk_rq_map_kern(struct request *rq, void *kbuf, unsigned int len,
if (!len || !kbuf)
return -EINVAL;
- if (!blk_rq_aligned(rq->q, addr, len) || object_is_on_stack(kbuf))
+ do_copy = !blk_rq_aligned(rq->q, addr, len) || object_is_on_stack(kbuf);
+ if (do_copy)
bio = bio_copy_kern(rq, kbuf, len, gfp_mask);
else
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,
return PTR_ERR(bio);
ret = blk_rq_append_bio(rq, bio);
- if (unlikely(ret))
+ if (unlikely(ret)) {
+ if (do_copy)
+ bio_free_pages(bio);
blk_mq_map_bio_put(bio);
+ }
return ret;
}
EXPORT_SYMBOL(blk_rq_map_kern);
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] block: free copied pages when blk_rq_map_kern() fails
2026-07-15 7:35 [PATCH] block: free copied pages when blk_rq_map_kern() fails Jackie Liu
@ 2026-07-16 9:23 ` Christoph Hellwig
2026-07-16 12:12 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2026-07-16 9:23 UTC (permalink / raw)
To: Jackie Liu; +Cc: axboe, linux-block
Looks good:
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] block: free copied pages when blk_rq_map_kern() fails
2026-07-15 7:35 [PATCH] block: free copied pages when blk_rq_map_kern() fails Jackie Liu
2026-07-16 9:23 ` Christoph Hellwig
@ 2026-07-16 12:12 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2026-07-16 12:12 UTC (permalink / raw)
To: Jackie Liu; +Cc: linux-block
On Wed, 15 Jul 2026 15:35:18 +0800, Jackie Liu wrote:
> 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.
>
> [...]
Applied, thanks!
[1/1] block: free copied pages when blk_rq_map_kern() fails
commit: bd2df8dc72201f626d66ca97f2afc8b7f2740713
Best regards,
--
Jens Axboe
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-16 12:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 7:35 [PATCH] block: free copied pages when blk_rq_map_kern() fails Jackie Liu
2026-07-16 9:23 ` Christoph Hellwig
2026-07-16 12:12 ` Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox