Skip to contentSkip to conversation

ceph: fix blindly expanding the readahead windows

PATCH Superseded [email protected] 2 participants 5 messages May 11, 2023 – May 12, 2023

Subsystems (2)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover ceph: fix blindly expanding the readahead windows

From: Xiubo Li <[email protected]>

V5:
- Fixed the file ra_pages issue about the posix_fadvise

Xiubo Li (2):
ceph: add a dedicated private data for netfs rreq
ceph: fix blindly expanding the readahead windows

 fs/ceph/addr.c  | 83 ++++++++++++++++++++++++++++++++++++++-----------
 fs/ceph/super.h | 13 ++++++++
 2 files changed, 78 insertions(+), 18 deletions(-)
Signature · 1 line
2.40.0
#1 lore raw

PATCH v5 1/2 ceph: add a dedicated private data for netfs rreq

From: Xiubo Li <[email protected]>

We need to save the 'f_ra.ra_pages' to expand the readahead window later.

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

---
 fs/ceph/addr.c  | 43 ++++++++++++++++++++++++++++++++-----------
 fs/ceph/super.h | 13 +++++++++++++
 2 files changed, 45 insertions(+), 11 deletions(-)
fs/ceph/addr.c +32 -11
index 3b20873733af..db55fce13324 100644
@@ -404,18 +404,27 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
 {
 	struct inode *inode = rreq->inode;
 	int got = 0, want = CEPH_CAP_FILE_CACHE;
+	struct ceph_netfs_request_data *priv;
 	int ret = 0;
 
 	if (rreq->origin != NETFS_READAHEAD)
 		return 0;
 
+	priv = kzalloc(sizeof(*priv), GFP_NOFS);
+	if (!priv)
+		return -ENOMEM;
+
 	if (file) {
 		struct ceph_rw_context *rw_ctx;
 		struct ceph_file_info *fi = file->private_data;
 
 		rw_ctx = ceph_find_rw_context(fi);
-		if (rw_ctx)
+		if (rw_ctx) {
+			kfree(priv);
 			return 0;
+		}
+		priv->file_ra_pages = file->f_ra.ra_pages;
+		priv->file_ra_disabled = !!(file->f_mode & FMODE_RANDOM);
 	}
 
 	/*
@@ -425,27 +434,39 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
 	ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
 	if (ret < 0) {
 		dout("start_read %p, error getting cap\n", inode);
-		return ret;
+		goto out;
 	}
 
 	if (!(got & want)) {
 		dout("start_read %p, no cache cap\n", inode);
-		return -EACCES;
+		ret = -EACCES;
+		goto out;
+	}
+	if (ret == 0) {
+		ret = -EACCES;
+		goto out;
 	}
-	if (ret == 0)
-		return -EACCES;
 
-	rreq->netfs_priv = (void *)(uintptr_t)got;
-	return 0;
+	priv->caps = got;
+	rreq->netfs_priv = priv;
+
+out:
+	if (ret)
+		kfree(priv);
+
+	return ret;
 }
 
 static void ceph_netfs_free_request(struct netfs_io_request *rreq)
 {
-	struct ceph_inode_info *ci = ceph_inode(rreq->inode);
-	int got = (uintptr_t)rreq->netfs_priv;
+	struct ceph_netfs_request_data *priv = rreq->netfs_priv;
+
+	if (!priv)
+		return;
 
-	if (got)
-		ceph_put_cap_refs(ci, got);
+	ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
+	kfree(priv);
+	rreq->netfs_priv = NULL;
 }
 
 const struct netfs_request_ops ceph_netfs_ops = {
fs/ceph/super.h +13 -0
index a226d36b3ecb..1233f53f6e0b 100644
@@ -470,6 +470,19 @@ struct ceph_inode_info {
 #endif
 };
 
+struct ceph_netfs_request_data {
+	int caps;
+
+	/*
+	 * Maximum size of a file readahead request.
+	 * The posix_fadvise could update the bdi's default ra_pages.
+	 */
+	unsigned int file_ra_pages;
+
+	/* Set it if posix_fadvise disables file readahead entirely */
+	bool file_ra_disabled;
+};
+
 static inline struct ceph_inode_info *
 ceph_inode(const struct inode *inode)
 {
Signature · 1 line
2.40.0
#2 lore raw

Re: [PATCH v5 1/2] ceph: add a dedicated private data for netfs rreq

Quoted 33 lines · On Thu, May 11, 2023 at 11:03:34AM +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

We need to save the 'f_ra.ra_pages' to expand the readahead window later.

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

---
 fs/ceph/addr.c  | 43 ++++++++++++++++++++++++++++++++-----------
 fs/ceph/super.h | 13 +++++++++++++
 2 files changed, 45 insertions(+), 11 deletions(-)
fs/ceph/addr.c +4 -0
index 3b20873733af..db55fce13324 100644
@@ -404,18 +404,27 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
 {
 	struct inode *inode = rreq->inode;
 	int got = 0, want = CEPH_CAP_FILE_CACHE;
+	struct ceph_netfs_request_data *priv;
 	int ret = 0;
 
 	if (rreq->origin != NETFS_READAHEAD)
 		return 0;
 
+	priv = kzalloc(sizeof(*priv), GFP_NOFS);
+	if (!priv)
+		return -ENOMEM;

fs/ceph/addr.c static int ceph_init_request(struct netfs_io_request *rreq, struct file *file) · line 424

Quoted 10 lines
fs/ceph/addr.c +3 -1
+
 	if (file) {
 		struct ceph_rw_context *rw_ctx;
 		struct ceph_file_info *fi = file->private_data;
 
 		rw_ctx = ceph_find_rw_context(fi);
-		if (rw_ctx)
+		if (rw_ctx) {
+			kfree(priv);
 			return 0;

This is not working. When reaching here by read() system call, we always have non-null rw_ctx. (As I read the code and also verified with gdb) ceph_add_rw_context() is invoked in ceph_read_iter().

fs/ceph/addr.c static int ceph_init_request(struct netfs_io_request *rreq, struct file *file) · line 427

Quoted 3 lines
fs/ceph/addr.c +3 -0
+		}
+		priv->file_ra_pages = file->f_ra.ra_pages;
+		priv->file_ra_disabled = !!(file->f_mode & FMODE_RANDOM);

'!!' is not needed. From coding-style.rst: "When using bool types the !! construction is not needed, which eliminates a class of bugs".

Thanks
Hu Weiwen

Quoted 80 lines
fs/ceph/addr.c +22 -10
 	}
 
 	/*
@@ -425,27 +434,39 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
 	ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
 	if (ret < 0) {
 		dout("start_read %p, error getting cap\n", inode);
-		return ret;
+		goto out;
 	}
 
 	if (!(got & want)) {
 		dout("start_read %p, no cache cap\n", inode);
-		return -EACCES;
+		ret = -EACCES;
+		goto out;
+	}
+	if (ret == 0) {
+		ret = -EACCES;
+		goto out;
 	}
-	if (ret == 0)
-		return -EACCES;
 
-	rreq->netfs_priv = (void *)(uintptr_t)got;
-	return 0;
+	priv->caps = got;
+	rreq->netfs_priv = priv;
+
+out:
+	if (ret)
+		kfree(priv);
+
+	return ret;
 }
 
 static void ceph_netfs_free_request(struct netfs_io_request *rreq)
 {
-	struct ceph_inode_info *ci = ceph_inode(rreq->inode);
-	int got = (uintptr_t)rreq->netfs_priv;
+	struct ceph_netfs_request_data *priv = rreq->netfs_priv;
+
+	if (!priv)
+		return;
 
-	if (got)
-		ceph_put_cap_refs(ci, got);
+	ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
+	kfree(priv);
+	rreq->netfs_priv = NULL;
 }
 
 const struct netfs_request_ops ceph_netfs_ops = {
fs/ceph/super.h +13 -0
index a226d36b3ecb..1233f53f6e0b 100644
@@ -470,6 +470,19 @@ struct ceph_inode_info {
 #endif
 };
 
+struct ceph_netfs_request_data {
+	int caps;
+
+	/*
+	 * Maximum size of a file readahead request.
+	 * The posix_fadvise could update the bdi's default ra_pages.
+	 */
+	unsigned int file_ra_pages;
+
+	/* Set it if posix_fadvise disables file readahead entirely */
+	bool file_ra_disabled;
+};
+
 static inline struct ceph_inode_info *
 ceph_inode(const struct inode *inode)
 {
Signature · 1 line
2.40.0
author
#3 lore raw

Re: [PATCH v5 1/2] ceph: add a dedicated private data for netfs rreq

Quoted 39 lines · 胡玮文
Quoted 38 lines · On Thu, May 11, 2023 at 11:03:34AM +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

We need to save the 'f_ra.ra_pages' to expand the readahead window later.

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

---
  fs/ceph/addr.c  | 43 ++++++++++++++++++++++++++++++++-----------
  fs/ceph/super.h | 13 +++++++++++++
  2 files changed, 45 insertions(+), 11 deletions(-)
fs/ceph/addr.c +5 -0
index 3b20873733af..db55fce13324 100644
@@ -404,18 +404,27 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
  {
  	struct inode *inode = rreq->inode;
  	int got = 0, want = CEPH_CAP_FILE_CACHE;
+	struct ceph_netfs_request_data *priv;
  	int ret = 0;
 
  	if (rreq->origin != NETFS_READAHEAD)
  		return 0;
 
+	priv = kzalloc(sizeof(*priv), GFP_NOFS);
+	if (!priv)
+		return -ENOMEM;
+
  	if (file) {
  		struct ceph_rw_context *rw_ctx;
  		struct ceph_file_info *fi = file->private_data;
 
Quoted 8 lines

fs/ceph/addr.c static int ceph_init_request(struct netfs_io_request *rreq, struct file *file) · line 424

Quoted 5 lines
fs/ceph/addr.c +2 -1
  		rw_ctx = ceph_find_rw_context(fi);
-		if (rw_ctx)
+		if (rw_ctx) {
+			kfree(priv);
  			return 0;

This is not working. When reaching here by read() system call, we always have non-null rw_ctx. (As I read the code and also verified with gdb) ceph_add_rw_context() is invoked in ceph_read_iter().

This should work:

fs/ceph/addr.c +0 -0
index e1bf90059112..d3e37e01c3d0 100644
@@ -444,13 +444,14 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)

                struct ceph_rw_context *rw_ctx; struct ceph_file_info *fi = file->private_data;

+               priv->file_ra_pages = file->f_ra.ra_pages;
+               priv->file_ra_disabled = file->f_mode & FMODE_RANDOM; +
                rw_ctx = ceph_find_rw_context(fi);
                if (rw_ctx) {
-                       kfree(priv);
+                       rreq->netfs_priv = priv;
                        return 0;
                }
-               priv->file_ra_pages = file->f_ra.ra_pages;
-               priv->file_ra_disabled = !!(file->f_mode & FMODE_RANDOM); }

        /*

Thanks

- Xiubo

Quoted 89 lines

fs/ceph/addr.c static int ceph_init_request(struct netfs_io_request *rreq, struct file *file) · line 427

Quoted 3 lines
fs/ceph/addr.c +3 -0
+		}
+		priv->file_ra_pages = file->f_ra.ra_pages;
+		priv->file_ra_disabled = !!(file->f_mode & FMODE_RANDOM);

'!!' is not needed. From coding-style.rst: "When using bool types the !! construction is not needed, which eliminates a class of bugs".

Thanks
Hu Weiwen

Quoted 80 lines
fs/ceph/addr.c +22 -10
  	}
 
  	/*
@@ -425,27 +434,39 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
  	ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
  	if (ret < 0) {
  		dout("start_read %p, error getting cap\n", inode);
-		return ret;
+		goto out;
  	}
 
  	if (!(got & want)) {
  		dout("start_read %p, no cache cap\n", inode);
-		return -EACCES;
+		ret = -EACCES;
+		goto out;
+	}
+	if (ret == 0) {
+		ret = -EACCES;
+		goto out;
  	}
-	if (ret == 0)
-		return -EACCES;
 
-	rreq->netfs_priv = (void *)(uintptr_t)got;
-	return 0;
+	priv->caps = got;
+	rreq->netfs_priv = priv;
+
+out:
+	if (ret)
+		kfree(priv);
+
+	return ret;
  }
 
  static void ceph_netfs_free_request(struct netfs_io_request *rreq)
  {
-	struct ceph_inode_info *ci = ceph_inode(rreq->inode);
-	int got = (uintptr_t)rreq->netfs_priv;
+	struct ceph_netfs_request_data *priv = rreq->netfs_priv;
+
+	if (!priv)
+		return;
 
-	if (got)
-		ceph_put_cap_refs(ci, got);
+	ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
+	kfree(priv);
+	rreq->netfs_priv = NULL;
  }
 
  const struct netfs_request_ops ceph_netfs_ops = {
fs/ceph/super.h +13 -0
index a226d36b3ecb..1233f53f6e0b 100644
@@ -470,6 +470,19 @@ struct ceph_inode_info {
  #endif
  };
 
+struct ceph_netfs_request_data {
+	int caps;
+
+	/*
+	 * Maximum size of a file readahead request.
+	 * The posix_fadvise could update the bdi's default ra_pages.
+	 */
+	unsigned int file_ra_pages;
+
+	/* Set it if posix_fadvise disables file readahead entirely */
+	bool file_ra_disabled;
+};
+
  static inline struct ceph_inode_info *
  ceph_inode(const struct inode *inode)
  {
Signature · 1 line
2.40.0
#4 lore raw

PATCH v5 2/2 ceph: fix blindly expanding the readahead windows

In reply to [email protected]

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.

The posix_fadvise may change the maximum size of a file readahead.

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

---
 fs/ceph/addr.c | 40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)
fs/ceph/addr.c +33 -7
index db55fce13324..e1bf90059112 100644
@@ -188,16 +188,42 @@ 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;
+	loff_t end = rreq->start + rreq->len, new_end;
+	struct ceph_netfs_request_data *priv = rreq->netfs_priv;
+	unsigned long max_len;
 	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;
+	if (priv) {
+		/* Readahead is disabled by posix_fadvise POSIX_FADV_RANDOM */
+		if (priv->file_ra_disabled)
+			max_pages = 0;
+		else
+			max_pages = priv->file_ra_pages;
+
+	}
+
+	/* Readahead is disabled */
+	if (!max_pages)
+		return;
 
-	/* Now, round up the length to the next block */
-	rreq->len = roundup(rreq->len, lo->stripe_unit);
+	max_len = max_pages << PAGE_SHIFT;
+
+	/*
+	 * 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