All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ceph: fix write_begin optimization when write is beyond EOF
@ 2021-06-11 19:59 Jeff Layton
  2021-06-11 20:48 ` Matthew Wilcox
  2021-06-12  0:11 ` [PATCH v2] " Jeff Layton
  0 siblings, 2 replies; 11+ messages in thread
From: Jeff Layton @ 2021-06-11 19:59 UTC (permalink / raw)
  To: ceph-devel
  Cc: linux-cachefs, pfmeec, willy, dhowells, idryomov, stable, Andrew W Elble

It's not sufficient to skip reading when the pos is beyond the EOF.
There may be data at the head of the page that we need to fill in
before the write. Only elide the read if the pos is beyond the last page
in the file.

Cc: <stable@vger.kernel.org> # v5.10 .. v5.12
Fixes: 1cc1699070bd ("ceph: fold ceph_update_writeable_page into ceph_write_begin")
Reported-by: Andrew W Elble <aweits@rit.edu>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/ceph/addr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Note to stable maintainers: This is needed in v5.10.z - v5.12.z. In
v5.13, we've moved to using the new netfs_read_helper code so this isn't
necessary there.

I also now have a simple testcase for this that I'll submit to xfstests
early next week.

diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index 26e66436f005..e636fb8275e1 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -1353,11 +1353,11 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
 		/*
 		 * In some cases we don't need to read at all:
 		 * - full page write
-		 * - write that lies completely beyond EOF
+		 * - write that lies in a page that is completely beyond EOF
 		 * - write that covers the the page from start to EOF or beyond it
 		 */
 		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
-		    (pos >= i_size_read(inode)) ||
+		    (index > (i_size_read(inode) / PAGE_SIZE)) ||
 		    (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {
 			zero_user_segments(page, 0, pos_in_page,
 					   pos_in_page + len, PAGE_SIZE);
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-11 19:59 [PATCH] ceph: fix write_begin optimization when write is beyond EOF Jeff Layton
@ 2021-06-11 20:48 ` Matthew Wilcox
  2021-06-11 22:20   ` Jeff Layton
  2021-06-12  0:11 ` [PATCH v2] " Jeff Layton
  1 sibling, 1 reply; 11+ messages in thread
From: Matthew Wilcox @ 2021-06-11 20:48 UTC (permalink / raw)
  To: Jeff Layton
  Cc: ceph-devel, linux-cachefs, pfmeec, dhowells, idryomov, stable,
	Andrew W Elble

On Fri, Jun 11, 2021 at 03:59:04PM -0400, Jeff Layton wrote:
>  		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
> -		    (pos >= i_size_read(inode)) ||
> +		    (index > (i_size_read(inode) / PAGE_SIZE)) ||

I think that wants to be ((i_size_read(inode) - 1) / PAGE_SIZE)

If your file is 4096 bytes long, that means bytes 0-4095 contain data.
Except that i_size can be 0, so ...

		if ((offset == 0 && len == PAGE_SIZE) || i_size == 0 ||
		    (index > (i_size - 1) / PAGE_SIZE) ||
		    (offset == 0 && pos + len >= i_size))
  			zero_user_segments(page, 0, pos_in_page,
  					   pos_in_page + len, PAGE_SIZE);


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-11 20:48 ` Matthew Wilcox
@ 2021-06-11 22:20   ` Jeff Layton
  0 siblings, 0 replies; 11+ messages in thread
From: Jeff Layton @ 2021-06-11 22:20 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: ceph-devel, linux-cachefs, pfmeec, dhowells, idryomov, stable,
	Andrew W Elble

On Fri, 2021-06-11 at 21:48 +0100, Matthew Wilcox wrote:
> On Fri, Jun 11, 2021 at 03:59:04PM -0400, Jeff Layton wrote:
> >  		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
> > -		    (pos >= i_size_read(inode)) ||
> > +		    (index > (i_size_read(inode) / PAGE_SIZE)) ||
> 
> I think that wants to be ((i_size_read(inode) - 1) / PAGE_SIZE)
> 
> If your file is 4096 bytes long, that means bytes 0-4095 contain data.
> Except that i_size can be 0, so ...
> 
> 		if ((offset == 0 && len == PAGE_SIZE) || i_size == 0 ||
> 		    (index > (i_size - 1) / PAGE_SIZE) ||
> 		    (offset == 0 && pos + len >= i_size))
>   			zero_user_segments(page, 0, pos_in_page,
>   					   pos_in_page + len, PAGE_SIZE);
> 

Oh, right -- I'll fix that and send a v2. Sorry for the noise!

-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-11 19:59 [PATCH] ceph: fix write_begin optimization when write is beyond EOF Jeff Layton
  2021-06-11 20:48 ` Matthew Wilcox
@ 2021-06-12  0:11 ` Jeff Layton
  2021-06-12 13:36   ` Matthew Wilcox
  1 sibling, 1 reply; 11+ messages in thread
From: Jeff Layton @ 2021-06-12  0:11 UTC (permalink / raw)
  To: ceph-devel
  Cc: linux-cachefs, pfmeec, willy, dhowells, idryomov, stable, Andrew W Elble

It's not sufficient to skip reading when the pos is beyond the EOF.
There may be data at the head of the page that we need to fill in
before the write. Only elide the read if the pos is beyond the last page
in the file.

Cc: <stable@vger.kernel.org> # v5.10+
Fixes: 1cc1699070bd ("ceph: fold ceph_update_writeable_page into ceph_write_begin")
Reported-by: Andrew W Elble <aweits@rit.edu>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/ceph/addr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

This version fixes the one-off bug that Willy pointed out in v1.

Note that v5.13 has been converted to use the new netfs read helper lib,xi
so this fix is for v5.10.z through v5.12.z.

diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index 26e66436f005..813ab4256dbb 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -1353,11 +1353,11 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
 		/*
 		 * In some cases we don't need to read at all:
 		 * - full page write
-		 * - write that lies completely beyond EOF
+		 * - write that lies in a page that is completely beyond EOF
 		 * - write that covers the the page from start to EOF or beyond it
 		 */
 		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
-		    (pos >= i_size_read(inode)) ||
+		    (index > (i_size_read(inode) - 1) / PAGE_SIZE) ||
 		    (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {
 			zero_user_segments(page, 0, pos_in_page,
 					   pos_in_page + len, PAGE_SIZE);
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-12  0:11 ` [PATCH v2] " Jeff Layton
@ 2021-06-12 13:36   ` Matthew Wilcox
  2021-06-12 18:35     ` [PATCH v3] " Jeff Layton
  0 siblings, 1 reply; 11+ messages in thread
From: Matthew Wilcox @ 2021-06-12 13:36 UTC (permalink / raw)
  To: Jeff Layton
  Cc: ceph-devel, linux-cachefs, pfmeec, dhowells, idryomov, stable,
	Andrew W Elble

On Fri, Jun 11, 2021 at 08:11:41PM -0400, Jeff Layton wrote:
>  		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
> -		    (pos >= i_size_read(inode)) ||
> +		    (index > (i_size_read(inode) - 1) / PAGE_SIZE) ||
>  		    (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {

You missed the (i_size == 0) case.  And I really would factor out
reading i_size into a local variable.

>  			zero_user_segments(page, 0, pos_in_page,
>  					   pos_in_page + len, PAGE_SIZE);
> -- 
> 2.31.1
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-12 13:36   ` Matthew Wilcox
@ 2021-06-12 18:35     ` Jeff Layton
  2021-06-13 11:04       ` Matthew Wilcox
  0 siblings, 1 reply; 11+ messages in thread
From: Jeff Layton @ 2021-06-12 18:35 UTC (permalink / raw)
  To: ceph-devel
  Cc: linux-cachefs, pfmeec, willy, dhowells, idryomov, stable, Andrew W Elble

It's not sufficient to skip reading when the pos is beyond the EOF.
There may be data at the head of the page that we need to fill in
before the write.

Add a new helper function that corrects and clarifies the logic.

Cc: <stable@vger.kernel.org> # v5.10+
Fixes: 1cc1699070bd ("ceph: fold ceph_update_writeable_page into ceph_write_begin")
Reported-by: Andrew W Elble <aweits@rit.edu>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/ceph/addr.c | 60 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 13 deletions(-)

Willy pointed out that I had missed the i_size == 0 case in my earlier
patch. Also, the whole condition was getting a bit messy. This factors
it out into a new helper (and we can maybe copy this helper into netfs
code).

diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index 26e66436f005..ba53e9a3f0c1 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -1302,6 +1302,51 @@ ceph_find_incompatible(struct page *page)
 	return NULL;
 }
 
+/**
+ * prep_noread_page - prep a page for writing without reading first
+ * @page: page being prepared
+ * @pos: starting position for the write
+ * @len: length of write
+ *
+ * In some cases we don't need to read at all:
+ * - full page write
+ * - file is currently zero-length
+ * - write that lies in a page that is completely beyond EOF
+ * - write that covers the the page from start to EOF or beyond it
+ *
+ * If any of these criteria are met, then zero out the unwritten parts
+ * of the page and return true. Otherwise, return false.
+ */
+static bool prep_noread_page(struct page *page, loff_t pos, unsigned int len)
+{
+	struct inode *inode = page->mapping->host;
+	loff_t i_size = i_size_read(inode);
+	pgoff_t index = pos / PAGE_SIZE;
+	int pos_in_page = pos & ~PAGE_MASK;
+
+	/* full page write */
+	if (pos_in_page == 0 && len == PAGE_SIZE)
+		goto zero_out;
+
+	/* zero-length file */
+	if (i_size == 0)
+		goto zero_out;
+
+	/* position beyond last page in the file */
+	if (index > ((i_size - 1) / PAGE_SIZE))
+		goto zero_out;
+
+	/* write that covers the the page from start to EOF or beyond it */
+	if (pos_in_page == 0 && (pos + len) >= i_size)
+		goto zero_out;
+
+	return false;
+zero_out:
+	zero_user_segments(page, 0, pos_in_page,
+			   pos_in_page + len, PAGE_SIZE);
+	return true;
+}
+
 /*
  * We are only allowed to write into/dirty the page if the page is
  * clean, or already dirty within the same snap context.
@@ -1315,7 +1360,6 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
 	struct ceph_snap_context *snapc;
 	struct page *page = NULL;
 	pgoff_t index = pos >> PAGE_SHIFT;
-	int pos_in_page = pos & ~PAGE_MASK;
 	int r = 0;
 
 	dout("write_begin file %p inode %p page %p %d~%d\n", file, inode, page, (int)pos, (int)len);
@@ -1350,19 +1394,9 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
 			break;
 		}
 
-		/*
-		 * In some cases we don't need to read at all:
-		 * - full page write
-		 * - write that lies completely beyond EOF
-		 * - write that covers the the page from start to EOF or beyond it
-		 */
-		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
-		    (pos >= i_size_read(inode)) ||
-		    (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {
-			zero_user_segments(page, 0, pos_in_page,
-					   pos_in_page + len, PAGE_SIZE);
+		/* No need to read in some cases */
+		if (prep_noread_page(page, pos, len))
 			break;
-		}
 
 		/*
 		 * We need to read it. If we get back -EINPROGRESS, then the page was
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-12 18:35     ` [PATCH v3] " Jeff Layton
@ 2021-06-13 11:04       ` Matthew Wilcox
  2021-06-13 11:36         ` [PATCH v4] " Jeff Layton
  0 siblings, 1 reply; 11+ messages in thread
From: Matthew Wilcox @ 2021-06-13 11:04 UTC (permalink / raw)
  To: Jeff Layton
  Cc: ceph-devel, linux-cachefs, pfmeec, dhowells, idryomov, stable,
	Andrew W Elble

On Sat, Jun 12, 2021 at 02:35:31PM -0400, Jeff Layton wrote:
>  
> +/**
> + * prep_noread_page - prep a page for writing without reading first
> + * @page: page being prepared
> + * @pos: starting position for the write
> + * @len: length of write
> + *
> + * In some cases we don't need to read at all:
> + * - full page write
> + * - file is currently zero-length
> + * - write that lies in a page that is completely beyond EOF
> + * - write that covers the the page from start to EOF or beyond it
> + *
> + * If any of these criteria are met, then zero out the unwritten parts
> + * of the page and return true. Otherwise, return false.
> + */
> +static bool prep_noread_page(struct page *page, loff_t pos, unsigned int len)
> +{
> +	struct inode *inode = page->mapping->host;
> +	loff_t i_size = i_size_read(inode);
> +	pgoff_t index = pos / PAGE_SIZE;
> +	int pos_in_page = pos & ~PAGE_MASK;

Like the helper.  A couple of minor tweaks ...

	size_t offset = offset_in_page(pos);

> +	/* full page write */
> +	if (pos_in_page == 0 && len == PAGE_SIZE)
> +		goto zero_out;

At some point, we're going to need to pass the full len to
->write_begin, so that we can decide whether it's worth allocating
more than a single page.  Could you make 'len' here size_t, and
check for len >= PAGE_SIZE?

(with the current code, the offset of 0 is a redundant check, but
I'd rather see this future-proofed).


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-13 11:04       ` Matthew Wilcox
@ 2021-06-13 11:36         ` Jeff Layton
  2021-06-13 12:02           ` Jeff Layton
  0 siblings, 1 reply; 11+ messages in thread
From: Jeff Layton @ 2021-06-13 11:36 UTC (permalink / raw)
  To: ceph-devel
  Cc: linux-cachefs, pfmeec, willy, dhowells, idryomov, stable, Andrew W Elble

It's not sufficient to skip reading when the pos is beyond the EOF.
There may be data at the head of the page that we need to fill in
before the write.

Add a new helper function that corrects and clarifies the logic.

Cc: <stable@vger.kernel.org> # v5.10+
Cc: Matthew Wilcox <willy@infradead.org>
Fixes: 1cc1699070bd ("ceph: fold ceph_update_writeable_page into ceph_write_begin")
Reported-by: Andrew W Elble <aweits@rit.edu>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/ceph/addr.c | 63 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 13 deletions(-)

This version just has a couple of future-proofing tweaks that Willy
suggested.

diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index 26e66436f005..b20a17cfec42 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -1302,6 +1302,54 @@ ceph_find_incompatible(struct page *page)
 	return NULL;
 }
 
+/**
+ * prep_noread_page - prep a page for writing without reading first
+ * @page: page being prepared
+ * @pos: starting position for the write
+ * @len: length of write
+ *
+ * In some cases we don't need to read at all:
+ * - full page write
+ * - file is currently zero-length
+ * - write that lies in a page that is completely beyond EOF
+ * - write that covers the the page from start to EOF or beyond it
+ *
+ * If any of these criteria are met, then zero out the unwritten parts
+ * of the page and return true. Otherwise, return false.
+ */
+static bool prep_noread_page(struct page *page, loff_t pos, size_t len)
+{
+	struct inode *inode = page->mapping->host;
+	loff_t i_size = i_size_read(inode);
+	pgoff_t index = pos / PAGE_SIZE;
+	size_t offset = offset_in_page(pos);
+
+	/* clamp length to end of the current page */
+	if (len > PAGE_SIZE)
+		len = PAGE_SIZE - offset;
+
+	/* full page write */
+	if (offset == 0 && len == PAGE_SIZE)
+		goto zero_out;
+
+	/* zero-length file */
+	if (i_size == 0)
+		goto zero_out;
+
+	/* position beyond last page in the file */
+	if (index > ((i_size - 1) / PAGE_SIZE))
+		goto zero_out;
+
+	/* write that covers the the page from start to EOF or beyond it */
+	if (offset == 0 && (pos + len) >= i_size)
+		goto zero_out;
+
+	return false;
+zero_out:
+	zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);
+	return true;
+}
+
 /*
  * We are only allowed to write into/dirty the page if the page is
  * clean, or already dirty within the same snap context.
@@ -1315,7 +1363,6 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
 	struct ceph_snap_context *snapc;
 	struct page *page = NULL;
 	pgoff_t index = pos >> PAGE_SHIFT;
-	int pos_in_page = pos & ~PAGE_MASK;
 	int r = 0;
 
 	dout("write_begin file %p inode %p page %p %d~%d\n", file, inode, page, (int)pos, (int)len);
@@ -1350,19 +1397,9 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
 			break;
 		}
 
-		/*
-		 * In some cases we don't need to read at all:
-		 * - full page write
-		 * - write that lies completely beyond EOF
-		 * - write that covers the the page from start to EOF or beyond it
-		 */
-		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
-		    (pos >= i_size_read(inode)) ||
-		    (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {
-			zero_user_segments(page, 0, pos_in_page,
-					   pos_in_page + len, PAGE_SIZE);
+		/* No need to read in some cases */
+		if (prep_noread_page(page, pos, len))
 			break;
-		}
 
 		/*
 		 * We need to read it. If we get back -EINPROGRESS, then the page was
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v4] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-13 11:36         ` [PATCH v4] " Jeff Layton
@ 2021-06-13 12:02           ` Jeff Layton
  2021-06-13 15:15             ` Matthew Wilcox
  0 siblings, 1 reply; 11+ messages in thread
From: Jeff Layton @ 2021-06-13 12:02 UTC (permalink / raw)
  To: ceph-devel
  Cc: linux-cachefs, pfmeec, willy, dhowells, idryomov, stable, Andrew W Elble

On Sun, 2021-06-13 at 07:36 -0400, Jeff Layton wrote:
> It's not sufficient to skip reading when the pos is beyond the EOF.
> There may be data at the head of the page that we need to fill in
> before the write.
> 
> Add a new helper function that corrects and clarifies the logic.
> 
> Cc: <stable@vger.kernel.org> # v5.10+
> Cc: Matthew Wilcox <willy@infradead.org>
> Fixes: 1cc1699070bd ("ceph: fold ceph_update_writeable_page into ceph_write_begin")
> Reported-by: Andrew W Elble <aweits@rit.edu>
> Signed-off-by: Jeff Layton <jlayton@kernel.org>
> ---
>  fs/ceph/addr.c | 63 +++++++++++++++++++++++++++++++++++++++-----------
>  1 file changed, 50 insertions(+), 13 deletions(-)
> 
> This version just has a couple of future-proofing tweaks that Willy
> suggested.
> 
> diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
> index 26e66436f005..b20a17cfec42 100644
> --- a/fs/ceph/addr.c
> +++ b/fs/ceph/addr.c
> @@ -1302,6 +1302,54 @@ ceph_find_incompatible(struct page *page)
>  	return NULL;
>  }
>  
> +/**
> + * prep_noread_page - prep a page for writing without reading first
> + * @page: page being prepared
> + * @pos: starting position for the write
> + * @len: length of write
> + *
> + * In some cases we don't need to read at all:
> + * - full page write
> + * - file is currently zero-length
> + * - write that lies in a page that is completely beyond EOF
> + * - write that covers the the page from start to EOF or beyond it
> + *
> + * If any of these criteria are met, then zero out the unwritten parts
> + * of the page and return true. Otherwise, return false.
> + */
> +static bool prep_noread_page(struct page *page, loff_t pos, size_t len)
> +{
> +	struct inode *inode = page->mapping->host;
> +	loff_t i_size = i_size_read(inode);
> +	pgoff_t index = pos / PAGE_SIZE;
> +	size_t offset = offset_in_page(pos);
> +
> +	/* clamp length to end of the current page */
> +	if (len > PAGE_SIZE)
> +		len = PAGE_SIZE - offset;

Actually, I think this should be:

	len = min(len, PAGE_SIZE - offset);

Otherwise, len could still go beyond the end of the page.

> +
> +	/* full page write */
> +	if (offset == 0 && len == PAGE_SIZE)
> +		goto zero_out;
> +
> +	/* zero-length file */
> +	if (i_size == 0)
> +		goto zero_out;
> +
> +	/* position beyond last page in the file */
> +	if (index > ((i_size - 1) / PAGE_SIZE))
> +		goto zero_out;
> +
> +	/* write that covers the the page from start to EOF or beyond it */
> +	if (offset == 0 && (pos + len) >= i_size)
> +		goto zero_out;
> +
> +	return false;
> +zero_out:
> +	zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);
> +	return true;
> +}
> +
>  /*
>   * We are only allowed to write into/dirty the page if the page is
>   * clean, or already dirty within the same snap context.
> @@ -1315,7 +1363,6 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
>  	struct ceph_snap_context *snapc;
>  	struct page *page = NULL;
>  	pgoff_t index = pos >> PAGE_SHIFT;
> -	int pos_in_page = pos & ~PAGE_MASK;
>  	int r = 0;
>  
>  	dout("write_begin file %p inode %p page %p %d~%d\n", file, inode, page, (int)pos, (int)len);
> @@ -1350,19 +1397,9 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping,
>  			break;
>  		}
>  
> -		/*
> -		 * In some cases we don't need to read at all:
> -		 * - full page write
> -		 * - write that lies completely beyond EOF
> -		 * - write that covers the the page from start to EOF or beyond it
> -		 */
> -		if ((pos_in_page == 0 && len == PAGE_SIZE) ||
> -		    (pos >= i_size_read(inode)) ||
> -		    (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {
> -			zero_user_segments(page, 0, pos_in_page,
> -					   pos_in_page + len, PAGE_SIZE);
> +		/* No need to read in some cases */
> +		if (prep_noread_page(page, pos, len))
>  			break;
> -		}
>  
>  		/*
>  		 * We need to read it. If we get back -EINPROGRESS, then the page was

-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-13 12:02           ` Jeff Layton
@ 2021-06-13 15:15             ` Matthew Wilcox
  2021-06-13 15:25               ` Jeff Layton
  0 siblings, 1 reply; 11+ messages in thread
From: Matthew Wilcox @ 2021-06-13 15:15 UTC (permalink / raw)
  To: Jeff Layton
  Cc: ceph-devel, linux-cachefs, pfmeec, dhowells, idryomov, stable,
	Andrew W Elble

On Sun, Jun 13, 2021 at 08:02:12AM -0400, Jeff Layton wrote:
> > +	/* clamp length to end of the current page */
> > +	if (len > PAGE_SIZE)
> > +		len = PAGE_SIZE - offset;
> 
> Actually, I think this should be:
> 
> 	len = min(len, PAGE_SIZE - offset);
> 
> Otherwise, len could still go beyond the end of the page.

I don't understand why you want to clamp length instead of just coping
with len being > PAGE_SIZE.

> > +
> > +	/* full page write */
> > +	if (offset == 0 && len == PAGE_SIZE)
> > +		goto zero_out;

That becomes >=.

> > +	/* zero-length file */
> > +	if (i_size == 0)
> > +		goto zero_out;
> > +
> > +	/* position beyond last page in the file */
> > +	if (index > ((i_size - 1) / PAGE_SIZE))
> > +		goto zero_out;
> > +
> > +	/* write that covers the the page from start to EOF or beyond it */
> > +	if (offset == 0 && (pos + len) >= i_size)
> > +		goto zero_out;

That doesn't need any change.

> > +	return false;
> > +zero_out:
> > +	zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);

That also doesn't need any change.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4] ceph: fix write_begin optimization when write is beyond EOF
  2021-06-13 15:15             ` Matthew Wilcox
@ 2021-06-13 15:25               ` Jeff Layton
  0 siblings, 0 replies; 11+ messages in thread
From: Jeff Layton @ 2021-06-13 15:25 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: ceph-devel, linux-cachefs, pfmeec, dhowells, idryomov, stable,
	Andrew W Elble

On Sun, 2021-06-13 at 16:15 +0100, Matthew Wilcox wrote:
> On Sun, Jun 13, 2021 at 08:02:12AM -0400, Jeff Layton wrote:
> > > +	/* clamp length to end of the current page */
> > > +	if (len > PAGE_SIZE)
> > > +		len = PAGE_SIZE - offset;
> > 
> > Actually, I think this should be:
> > 
> > 	len = min(len, PAGE_SIZE - offset);
> > 
> > Otherwise, len could still go beyond the end of the page.
> 
> I don't understand why you want to clamp length instead of just coping
> with len being > PAGE_SIZE.
> 
> > > +
> > > +	/* full page write */
> > > +	if (offset == 0 && len == PAGE_SIZE)
> > > +		goto zero_out;
> 
> That becomes >=.
> 
> > > +	/* zero-length file */
> > > +	if (i_size == 0)
> > > +		goto zero_out;
> > > +
> > > +	/* position beyond last page in the file */
> > > +	if (index > ((i_size - 1) / PAGE_SIZE))
> > > +		goto zero_out;
> > > +
> > > +	/* write that covers the the page from start to EOF or beyond it */
> > > +	if (offset == 0 && (pos + len) >= i_size)
> > > +		goto zero_out;
> 
> That doesn't need any change.
> 
> > > +	return false;
> > > +zero_out:
> > > +	zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);
> 
> That also doesn't need any change.
> 

Won't it though? offset+len will could be beyond the end of the page at
that point. Hmm I guess zero_user_segments does this:

        if (start2 >= end2)
                start2 = end2 = 0;

...so that makes the second segment copy a no-op.

Ok, fair enough -- I'll get rid of the clamping and just allow len to be
longer than PAGE_SIZE in the checks.

Thanks,
-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-06-13 15:25 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-11 19:59 [PATCH] ceph: fix write_begin optimization when write is beyond EOF Jeff Layton
2021-06-11 20:48 ` Matthew Wilcox
2021-06-11 22:20   ` Jeff Layton
2021-06-12  0:11 ` [PATCH v2] " Jeff Layton
2021-06-12 13:36   ` Matthew Wilcox
2021-06-12 18:35     ` [PATCH v3] " Jeff Layton
2021-06-13 11:04       ` Matthew Wilcox
2021-06-13 11:36         ` [PATCH v4] " Jeff Layton
2021-06-13 12:02           ` Jeff Layton
2021-06-13 15:15             ` Matthew Wilcox
2021-06-13 15:25               ` Jeff Layton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.