Skip to contentSkip to conversation

ceph: fix blindly expanding the readahead windows

PATCH Superseded [email protected] 3 participants 5 messages May 9, 2023 – May 10, 2023

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 ceph: fix blindly expanding the readahead windows

From: Xiubo Li <[email protected]>

Blindly expanding the readahead windows will cause unneccessary pagecache thrashing and also will introdue 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.

Fixes 49870056005c ("ceph: convert ceph_readpages to ceph_readahead")

URL: https://lore.kernel.org/ceph-devel/[email protected]
URL: https://www.spinics.net/lists/ceph-users/msg76183.html

---

V4:
- two small cleanup from Ilya's comments. Thanks

 fs/ceph/addr.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
fs/ceph/addr.c +21 -7
index ca4dc6450887..683ba9fbd590 100644
@@ -188,16 +188,30 @@ static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
 	struct inode *inode = rreq->inode;
 	struct ceph_inode_info *ci = ceph_inode(inode);
 	struct ceph_file_layout *lo = &ci->i_layout;
+	unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
+	unsigned long max_len = max_pages << PAGE_SHIFT;
+	loff_t end = rreq->start + rreq->len, new_end;
 	u32 blockoff;
-	u64 blockno;
 
-	/* Expand the start downward */
-	blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
-	rreq->start = blockno * lo->stripe_unit;
-	rreq->len += blockoff;
+	/* Readahead is disabled */
+	if (!max_pages)
+		return;
 
-	/* Now, round up the length to the next block */
-	rreq->len = roundup(rreq->len, lo->stripe_unit);
+	/*
+	 * Try to expand the length forward by rounding  up it to the next
+	 * block, but do not exceed the file size, unless the original
+	 * request already exceeds it.
+	 */
+	new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
+	if (new_end > end && new_end <= rreq->start + max_len)
+		rreq->len = new_end - rreq->start;
+
+	/* Try to expand the start downward */
+	div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
+	if (rreq->len + blockoff <= max_len) {
+		rreq->start -= blockoff;
+		rreq->len += blockoff;
+	}
 }
 
 static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
Signature · 1 line
2.40.0
#1 lore raw
Quoted 15 lines · On Tue, 2023-05-09 at 08:57 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

Blindly expanding the readahead windows will cause unneccessary pagecache thrashing and also will introdue 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.

Fixes 49870056005c ("ceph: convert ceph_readpages to ceph_readahead")
Quoted 10 lines

URL: https://lore.kernel.org/ceph-devel/[email protected]
URL: https://www.spinics.net/lists/ceph-users/msg76183.html

---

V4:
- two small cleanup from Ilya's comments. Thanks

(cc'ing Steve French since he was asking me about ceph readahead yesterday)

FWIW, the original idea here was to try to read whole OSD objects when we can. I can see that that may have been overzealous though, so ramping up the size more slowly makes sense.

Quoted 22 lines
 fs/ceph/addr.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
fs/ceph/addr.c +4 -5
index ca4dc6450887..683ba9fbd590 100644
@@ -188,16 +188,30 @@ static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
 	struct inode *inode = rreq->inode;
 	struct ceph_inode_info *ci = ceph_inode(inode);
 	struct ceph_file_layout *lo = &ci->i_layout;
+	unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
+	unsigned long max_len = max_pages << PAGE_SHIFT;
+	loff_t end = rreq->start + rreq->len, new_end;
 	u32 blockoff;
-	u64 blockno;
 
-	/* Expand the start downward */
-	blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
-	rreq->start = blockno * lo->stripe_unit;
-	rreq->len += blockoff;
+	/* Readahead is disabled */

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 204

Quoted 10 lines
fs/ceph/addr.c +7 -2
+	if (!max_pages)
+		return;
 
-	/* Now, round up the length to the next block */
-	rreq->len = roundup(rreq->len, lo->stripe_unit);
+	/*
+	 * Try to expand the length forward by rounding  up it to the next
+	 * block, but do not exceed the file size, unless the original
+	 * request already exceeds it.
+	 */

Do you really need to clamp this to the i_size? Is it ever possible for the client to be out of date as to the real file size? If so then you might end up with a short read when there is actually more data.

I guess by doing this, you're trying to avoid having to call the OSD back after a short read and get a zero-length read when the file is shorter than the requested read?

Quoted 13 lines
fs/ceph/addr.c +10 -0
+	new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
+	if (new_end > end && new_end <= rreq->start + max_len)
+		rreq->len = new_end - rreq->start;
+
+	/* Try to expand the start downward */
+	div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
+	if (rreq->len + blockoff <= max_len) {
+		rreq->start -= blockoff;
+		rreq->len += blockoff;
+	}
 }
 
 static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
Signature · 1 line
Jeff Layton <[email protected]>
author
#2 lore raw
Quoted 58 lines · Jeff Layton
Quoted 15 lines · On Tue, 2023-05-09 at 08:57 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

Blindly expanding the readahead windows will cause unneccessary pagecache thrashing and also will introdue 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.

Fixes 49870056005c ("ceph: convert ceph_readpages to ceph_readahead")
Quoted 10 lines

URL: https://lore.kernel.org/ceph-devel/[email protected]
URL: https://www.spinics.net/lists/ceph-users/msg76183.html

---

V4:
- two small cleanup from Ilya's comments. Thanks

(cc'ing Steve French since he was asking me about ceph readahead yesterday)

FWIW, the original idea here was to try to read whole OSD objects when we can. I can see that that may have been overzealous though, so ramping up the size more slowly makes sense.

Quoted 25 lines
  fs/ceph/addr.c | 28 +++++++++++++++++++++-------
  1 file changed, 21 insertions(+), 7 deletions(-)
fs/ceph/addr.c +6 -5
index ca4dc6450887..683ba9fbd590 100644
@@ -188,16 +188,30 @@ static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
  	struct inode *inode = rreq->inode;
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_file_layout *lo = &ci->i_layout;
+	unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
+	unsigned long max_len = max_pages << PAGE_SHIFT;
+	loff_t end = rreq->start + rreq->len, new_end;
  	u32 blockoff;
-	u64 blockno;
 
-	/* Expand the start downward */
-	blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
-	rreq->start = blockno * lo->stripe_unit;
-	rreq->len += blockoff;
+	/* Readahead is disabled */
+	if (!max_pages)
+		return;
 
Quoted 7 lines

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 204

Quoted 7 lines
fs/ceph/addr.c +5 -2
-	/* Now, round up the length to the next block */
-	rreq->len = roundup(rreq->len, lo->stripe_unit);
+	/*
+	 * Try to expand the length forward by rounding  up it to the next
+	 * block, but do not exceed the file size, unless the original
+	 * request already exceeds it.
+	 */

Hi Jeff,

Quoted 7 lines

Do you really need to clamp this to the i_size? Is it ever possible for the client to be out of date as to the real file size? If so then you might end up with a short read when there is actually more data.

I guess by doing this, you're trying to avoid having to call the OSD back after a short read and get a zero-length read when the file is shorter than the requested read?

This is folded from Weiwen's another fix https://patchwork.kernel.org/project/ceph-devel/patch/[email protected]/.

For small files use case this may really could cause unnecessary network workload and inefficient usage of the page cache.

Thanks

- Xiubo

Quoted 13 lines
Quoted 13 lines
fs/ceph/addr.c +10 -0
+	new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
+	if (new_end > end && new_end <= rreq->start + max_len)
+		rreq->len = new_end - rreq->start;
+
+	/* Try to expand the start downward */
+	div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
+	if (rreq->len + blockoff <= max_len) {
+		rreq->start -= blockoff;
+		rreq->len += blockoff;
+	}
  }
 
  static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
#3 lore raw
In reply to [email protected]
Quoted 4 lines · On Tue, May 09, 2023 at 08:57:03AM +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

Blindly expanding the readahead windows will cause unneccessary pagecache thrashing and also will introdue the network workload.

                                    ^^^^^^^^
                                    introduce
Quoted 29 lines

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.

Fixes 49870056005c ("ceph: convert ceph_readpages to ceph_readahead")

URL: https://lore.kernel.org/ceph-devel/[email protected]
URL: https://www.spinics.net/lists/ceph-users/msg76183.html

---

V4:
- two small cleanup from Ilya's comments. Thanks

 fs/ceph/addr.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
fs/ceph/addr.c +0 -0
index ca4dc6450887..683ba9fbd590 100644

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 191

Quoted 4 lines
fs/ceph/addr.c +1 -0
@@ -188,16 +188,30 @@ static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
 	struct inode *inode = rreq->inode;
 	struct ceph_inode_info *ci = ceph_inode(inode);
 	struct ceph_file_layout *lo = &ci->i_layout;
+	unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;

I think it is better to use ractl->ra->ra_pages' instead of inode->i_sb->s_bdi->ra_pages'. So that we can consider per-request ra size config, e.g., posix_fadvise(POSIX_FADV_SEQUENTIAL) will double the ra_pages.

But `ractl' is not passed to this function. Can we just add this argument? ceph seems to be the only implementation of expand_readahead, so it should be easy. Or since this patch will be backported, maybe we should keep it simple, and write another patch for this?

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 198

Quoted 12 lines
fs/ceph/addr.c +5 -5
+	unsigned long max_len = max_pages << PAGE_SHIFT;
+	loff_t end = rreq->start + rreq->len, new_end;
 	u32 blockoff;
-	u64 blockno;
 
-	/* Expand the start downward */
-	blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
-	rreq->start = blockno * lo->stripe_unit;
-	rreq->len += blockoff;
+	/* Readahead is disabled */
+	if (!max_pages)
+		return;

If we have access to ractl here, we can also skip expanding on ractl->file->f_mode & FMODE_RANDOM', which is set by posix_fadvise(POSIX_FADV_RANDOM)'.

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 201

Quoted 5 lines
fs/ceph/addr.c +2 -2
-	/* Now, round up the length to the next block */
-	rreq->len = roundup(rreq->len, lo->stripe_unit);
+	/*
+	 * Try to expand the length forward by rounding  up it to the next

^^ an extra space

Quoted 19 lines
fs/ceph/addr.c +13 -0
+	 * block, but do not exceed the file size, unless the original
+	 * request already exceeds it.
+	 */
+	new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
+	if (new_end > end && new_end <= rreq->start + max_len)
+		rreq->len = new_end - rreq->start;
+
+	/* Try to expand the start downward */
+	div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
+	if (rreq->len + blockoff <= max_len) {
+		rreq->start -= blockoff;
+		rreq->len += blockoff;
+	}
 }
 
 static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
Signature · 1 line
2.40.0
author
#4 lore raw
Quoted 32 lines · 胡玮文
Quoted 4 lines · On Tue, May 09, 2023 at 08:57:03AM +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

Blindly expanding the readahead windows will cause unneccessary pagecache thrashing and also will introdue the network workload.

                                     ^^^^^^^^
                                     introduce
Quoted 24 lines

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.

Fixes 49870056005c ("ceph: convert ceph_readpages to ceph_readahead")

URL: https://lore.kernel.org/ceph-devel/[email protected]
URL: https://www.spinics.net/lists/ceph-users/msg76183.html

---

V4:
- two small cleanup from Ilya's comments. Thanks

  fs/ceph/addr.c | 28 +++++++++++++++++++++-------
  1 file changed, 21 insertions(+), 7 deletions(-)
Quoted 13 lines

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 191

Quoted 9 lines
fs/ceph/addr.c +1 -0
index ca4dc6450887..683ba9fbd590 100644
@@ -188,16 +188,30 @@ static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
  	struct inode *inode = rreq->inode;
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_file_layout *lo = &ci->i_layout;
+	unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;

I think it is better to use ractl->ra->ra_pages' instead of inode->i_sb->s_bdi->ra_pages'. So that we can consider per-request ra size config, e.g., posix_fadvise(POSIX_FADV_SEQUENTIAL) will double the ra_pages.

Yeah, good catch.

Quoted 4 lines

But `ractl' is not passed to this function. Can we just add this argument? ceph seems to be the only implementation of expand_readahead, so it should be easy. Or since this patch will be backported, maybe we should keep it simple, and write another patch for this?

I think we should fix it together with this. It should be easy.

We can just store the "file->f_ra->ra_pages" in ceph_init_request() instead, because each rreq will be related to a dedicated file.

Quoted 5 lines
Quoted 5 lines
fs/ceph/addr.c +2 -1
+	unsigned long max_len = max_pages << PAGE_SHIFT;
+	loff_t end = rreq->start + rreq->len, new_end;
  	u32 blockoff;
-	u64 blockno;
 
Quoted 10 lines

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 198

Quoted 7 lines
fs/ceph/addr.c +3 -4
-	/* Expand the start downward */
-	blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
-	rreq->start = blockno * lo->stripe_unit;
-	rreq->len += blockoff;
+	/* Readahead is disabled */
+	if (!max_pages)
+		return;

If we have access to ractl here, we can also skip expanding on ractl->file->f_mode & FMODE_RANDOM', which is set by posix_fadvise(POSIX_FADV_RANDOM)'.

Correct, because the "page_cache_sync_ra()' will do the right thing and we can skip expanding it here in ceph.

Thanks

- Xiubo

Quoted 27 lines

fs/ceph/addr.c static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq) · line 201

Quoted 6 lines
fs/ceph/addr.c +2 -2
-	/* Now, round up the length to the next block */
-	rreq->len = roundup(rreq->len, lo->stripe_unit);
+	/*
+	 * Try to expand the length forward by rounding  up it to the next

^^ an extra space

Quoted 19 lines
fs/ceph/addr.c +13 -0
+	 * block, but do not exceed the file size, unless the original
+	 * request already exceeds it.
+	 */
+	new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
+	if (new_end > end && new_end <= rreq->start + max_len)
+		rreq->len = new_end - rreq->start;
+
+	/* Try to expand the start downward */
+	div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
+	if (rreq->len + blockoff <= max_len) {
+		rreq->start -= blockoff;
+		rreq->len += blockoff;
+	}
  }
 
  static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
Signature · 1 line
2.40.0