Skip to content

Commit e97424f

Browse files
keithbuschaxboe
authored andcommitted
block: fix leaking page ref on truncated direct io
The size being added to a bio from an iov is aligned to a block size after the pages were gotten. If the new aligned size truncates the last page, its reference was being leaked. Ensure all pages that were not added to the bio have their reference released. Since this essentially requires doing the same that bio_put_pages(), and there was only one caller for that function, this patch makes the put_page() loop common for everyone. Fixes: b1a000d ("block: relax direct io memory alignment") Reported-by: Al Viro <[email protected]> Signed-off-by: Keith Busch <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 34cdb8c commit e97424f

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

block/bio.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,14 +1151,6 @@ void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
11511151
bio_set_flag(bio, BIO_CLONED);
11521152
}
11531153

1154-
static void bio_put_pages(struct page **pages, size_t size, size_t off)
1155-
{
1156-
size_t i, nr = DIV_ROUND_UP(size + (off & ~PAGE_MASK), PAGE_SIZE);
1157-
1158-
for (i = 0; i < nr; i++)
1159-
put_page(pages[i]);
1160-
}
1161-
11621154
static int bio_iov_add_page(struct bio *bio, struct page *page,
11631155
unsigned int len, unsigned int offset)
11641156
{
@@ -1207,7 +1199,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
12071199
struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
12081200
struct page **pages = (struct page **)bv;
12091201
ssize_t size, left;
1210-
unsigned len, i;
1202+
unsigned len, i = 0;
12111203
size_t offset;
12121204
int ret = 0;
12131205

@@ -1228,10 +1220,16 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
12281220
*/
12291221
size = iov_iter_get_pages(iter, pages, UINT_MAX - bio->bi_iter.bi_size,
12301222
nr_pages, &offset);
1231-
if (size > 0)
1223+
if (size > 0) {
1224+
nr_pages = DIV_ROUND_UP(offset + size, PAGE_SIZE);
12321225
size = ALIGN_DOWN(size, bdev_logical_block_size(bio->bi_bdev));
1233-
if (unlikely(size <= 0))
1234-
return size ? size : -EFAULT;
1226+
} else
1227+
nr_pages = 0;
1228+
1229+
if (unlikely(size <= 0)) {
1230+
ret = size ? size : -EFAULT;
1231+
goto out;
1232+
}
12351233

12361234
for (left = size, i = 0; left > 0; left -= len, i++) {
12371235
struct page *page = pages[i];
@@ -1240,17 +1238,19 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
12401238
if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
12411239
ret = bio_iov_add_zone_append_page(bio, page, len,
12421240
offset);
1243-
if (ret) {
1244-
bio_put_pages(pages + i, left, offset);
1241+
if (ret)
12451242
break;
1246-
}
12471243
} else
12481244
bio_iov_add_page(bio, page, len, offset);
12491245

12501246
offset = 0;
12511247
}
12521248

12531249
iov_iter_advance(iter, size - left);
1250+
out:
1251+
while (i < nr_pages)
1252+
put_page(pages[i++]);
1253+
12541254
return ret;
12551255
}
12561256

0 commit comments

Comments
 (0)