Skip to content

Commit dc94bb8

Browse files
lxbszidryomov
authored andcommitted
ceph: fix blindly expanding the readahead windows
Blindly expanding the readahead windows will cause unneccessary pagecache thrashing and also will introduce the network workload. We should disable expanding the windows if the readahead is disabled and also shouldn't expand the windows too much. Expanding forward firstly instead of expanding backward for possible sequential reads. Bound `rreq->len` to the actual file size to restore the previous page cache usage. The posix_fadvise may change the maximum size of a file readahead. Cc: [email protected] Fixes: 4987005 ("ceph: convert ceph_readpages to ceph_readahead") Link: https://lore.kernel.org/ceph-devel/[email protected] Link: https://www.spinics.net/lists/ceph-users/msg76183.html Signed-off-by: Xiubo Li <[email protected]> Reviewed-and-tested-by: Hu Weiwen <[email protected]> Reviewed-by: Milind Changire <[email protected]> Signed-off-by: Ilya Dryomov <[email protected]>
1 parent 23ee27d commit dc94bb8

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

fs/ceph/addr.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,42 @@ static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
187187
struct inode *inode = rreq->inode;
188188
struct ceph_inode_info *ci = ceph_inode(inode);
189189
struct ceph_file_layout *lo = &ci->i_layout;
190+
unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
191+
loff_t end = rreq->start + rreq->len, new_end;
192+
struct ceph_netfs_request_data *priv = rreq->netfs_priv;
193+
unsigned long max_len;
190194
u32 blockoff;
191-
u64 blockno;
192195

193-
/* Expand the start downward */
194-
blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
195-
rreq->start = blockno * lo->stripe_unit;
196-
rreq->len += blockoff;
196+
if (priv) {
197+
/* Readahead is disabled by posix_fadvise POSIX_FADV_RANDOM */
198+
if (priv->file_ra_disabled)
199+
max_pages = 0;
200+
else
201+
max_pages = priv->file_ra_pages;
202+
203+
}
204+
205+
/* Readahead is disabled */
206+
if (!max_pages)
207+
return;
208+
209+
max_len = max_pages << PAGE_SHIFT;
197210

198-
/* Now, round up the length to the next block */
199-
rreq->len = roundup(rreq->len, lo->stripe_unit);
211+
/*
212+
* Try to expand the length forward by rounding up it to the next
213+
* block, but do not exceed the file size, unless the original
214+
* request already exceeds it.
215+
*/
216+
new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
217+
if (new_end > end && new_end <= rreq->start + max_len)
218+
rreq->len = new_end - rreq->start;
219+
220+
/* Try to expand the start downward */
221+
div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
222+
if (rreq->len + blockoff <= max_len) {
223+
rreq->start -= blockoff;
224+
rreq->len += blockoff;
225+
}
200226
}
201227

202228
static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)

0 commit comments

Comments
 (0)