Skip to content

Commit 66e0c4f

Browse files
committed
ceph: fix memory leak in ceph_direct_read_write()
The bvecs array which is allocated in iter_get_bvecs_alloc() is leaked and pages remain pinned if ceph_alloc_sparse_ext_map() fails. There is no need to delay the allocation of sparse_ext map until after the bvecs array is set up, so fix this by moving sparse_ext allocation a bit earlier. Also, make a similar adjustment in __ceph_sync_read() for consistency (a leak of the same kind in __ceph_sync_read() has been addressed differently). Cc: [email protected] Fixes: 03bc06c ("ceph: add new mount option to enable sparse reads") Signed-off-by: Ilya Dryomov <[email protected]> Reviewed-by: Alex Markuze <[email protected]>
1 parent 9abee47 commit 66e0c4f

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

fs/ceph/file.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,16 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
11161116
len = read_off + read_len - off;
11171117
more = len < iov_iter_count(to);
11181118

1119+
op = &req->r_ops[0];
1120+
if (sparse) {
1121+
extent_cnt = __ceph_sparse_read_ext_count(inode, read_len);
1122+
ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
1123+
if (ret) {
1124+
ceph_osdc_put_request(req);
1125+
break;
1126+
}
1127+
}
1128+
11191129
num_pages = calc_pages_for(read_off, read_len);
11201130
page_off = offset_in_page(off);
11211131
pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
@@ -1129,16 +1139,6 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
11291139
offset_in_page(read_off),
11301140
false, true);
11311141

1132-
op = &req->r_ops[0];
1133-
if (sparse) {
1134-
extent_cnt = __ceph_sparse_read_ext_count(inode, read_len);
1135-
ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
1136-
if (ret) {
1137-
ceph_osdc_put_request(req);
1138-
break;
1139-
}
1140-
}
1141-
11421142
ceph_osdc_start_request(osdc, req);
11431143
ret = ceph_osdc_wait_request(osdc, req);
11441144

@@ -1551,6 +1551,16 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
15511551
break;
15521552
}
15531553

1554+
op = &req->r_ops[0];
1555+
if (sparse) {
1556+
extent_cnt = __ceph_sparse_read_ext_count(inode, size);
1557+
ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
1558+
if (ret) {
1559+
ceph_osdc_put_request(req);
1560+
break;
1561+
}
1562+
}
1563+
15541564
len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages);
15551565
if (len < 0) {
15561566
ceph_osdc_put_request(req);
@@ -1560,6 +1570,8 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
15601570
if (len != size)
15611571
osd_req_op_extent_update(req, 0, len);
15621572

1573+
osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
1574+
15631575
/*
15641576
* To simplify error handling, allow AIO when IO within i_size
15651577
* or IO can be satisfied by single OSD request.
@@ -1591,17 +1603,6 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
15911603
req->r_mtime = mtime;
15921604
}
15931605

1594-
osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
1595-
op = &req->r_ops[0];
1596-
if (sparse) {
1597-
extent_cnt = __ceph_sparse_read_ext_count(inode, size);
1598-
ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
1599-
if (ret) {
1600-
ceph_osdc_put_request(req);
1601-
break;
1602-
}
1603-
}
1604-
16051606
if (aio_req) {
16061607
aio_req->total_len += len;
16071608
aio_req->num_reqs++;

0 commit comments

Comments
 (0)