linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] mm: Stop filemap_read() from grabbing a superfluous page
@ 2021-10-20 10:10 David Howells
  2021-10-20 10:20 ` Jeff Layton
  2021-10-20 11:06 ` Matthew Wilcox
  0 siblings, 2 replies; 3+ messages in thread
From: David Howells @ 2021-10-20 10:10 UTC (permalink / raw)
  To: kent.overstreet, willy
  Cc: Andrew Morton, Jeff Layton, linux-mm, linux-fsdevel, dhowells,
	linux-kernel

Under some circumstances, filemap_read() will allocate sufficient pages to
read to the end of the file, call readahead/readpages on them and copy the
data over - and then it will allocate another page at the EOF and call
readpage on that and then ignore it.  This is unnecessary and a waste of
time and resources.

filemap_read() *does* check for this, but only after it has already done
the allocation and I/O.  Fix this by checking before calling
filemap_get_pages() also.

Changes:
 v2) Break out of the loop immediately rather than going to put_pages (the
     pvec is unoccupied).  Setting isize is then unnecessary.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Kent Overstreet <kent.overstreet@gmail.com>
cc: Matthew Wilcox (Oracle) <willy@infradead.org>
cc: Andrew Morton <akpm@linux-foundation.org>
cc: Jeff Layton <jlayton@redhat.com>
cc: linux-mm@kvack.org
cc: linux-fsdevel@vger.kernel.org
Link: https://lore.kernel.org/r/160588481358.3465195.16552616179674485179.stgit@warthog.procyon.org.uk/
Link: https://lore.kernel.org/r/163456863216.2614702.6384850026368833133.stgit@warthog.procyon.org.uk/
---

 mm/filemap.c |    3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/filemap.c b/mm/filemap.c
index dae481293b5d..e50be519f6a4 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2625,6 +2625,9 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
 		if ((iocb->ki_flags & IOCB_WAITQ) && already_read)
 			iocb->ki_flags |= IOCB_NOWAIT;
 
+		if (unlikely(iocb->ki_pos >= i_size_read(inode)))
+			break;
+
 		error = filemap_get_pages(iocb, iter, &pvec);
 		if (error < 0)
 			break;




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

* Re: [PATCH v2] mm: Stop filemap_read() from grabbing a superfluous page
  2021-10-20 10:10 [PATCH v2] mm: Stop filemap_read() from grabbing a superfluous page David Howells
@ 2021-10-20 10:20 ` Jeff Layton
  2021-10-20 11:06 ` Matthew Wilcox
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff Layton @ 2021-10-20 10:20 UTC (permalink / raw)
  To: David Howells, kent.overstreet, willy
  Cc: Andrew Morton, linux-mm, linux-fsdevel, linux-kernel

On Wed, 2021-10-20 at 11:10 +0100, David Howells wrote:
> Under some circumstances, filemap_read() will allocate sufficient pages to
> read to the end of the file, call readahead/readpages on them and copy the
> data over - and then it will allocate another page at the EOF and call
> readpage on that and then ignore it.  This is unnecessary and a waste of
> time and resources.
> 
> filemap_read() *does* check for this, but only after it has already done
> the allocation and I/O.  Fix this by checking before calling
> filemap_get_pages() also.
> 
> Changes:
>  v2) Break out of the loop immediately rather than going to put_pages (the
>      pvec is unoccupied).  Setting isize is then unnecessary.
> 
> Signed-off-by: David Howells <dhowells@redhat.com>
> cc: Kent Overstreet <kent.overstreet@gmail.com>
> cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> cc: Andrew Morton <akpm@linux-foundation.org>
> cc: Jeff Layton <jlayton@redhat.com>
> cc: linux-mm@kvack.org
> cc: linux-fsdevel@vger.kernel.org
> Link: https://lore.kernel.org/r/160588481358.3465195.16552616179674485179.stgit@warthog.procyon.org.uk/
> Link: https://lore.kernel.org/r/163456863216.2614702.6384850026368833133.stgit@warthog.procyon.org.uk/
> ---
> 
>  mm/filemap.c |    3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/mm/filemap.c b/mm/filemap.c
> index dae481293b5d..e50be519f6a4 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -2625,6 +2625,9 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
>  		if ((iocb->ki_flags & IOCB_WAITQ) && already_read)
>  			iocb->ki_flags |= IOCB_NOWAIT;
>  
> +		if (unlikely(iocb->ki_pos >= i_size_read(inode)))
> +			break;
> +
>  		error = filemap_get_pages(iocb, iter, &pvec);
>  		if (error < 0)
>  			break;
> 
> 

Even better.

Acked-by: Jeff Layton <jlayton@kernel.org>



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

* Re: [PATCH v2] mm: Stop filemap_read() from grabbing a superfluous page
  2021-10-20 10:10 [PATCH v2] mm: Stop filemap_read() from grabbing a superfluous page David Howells
  2021-10-20 10:20 ` Jeff Layton
@ 2021-10-20 11:06 ` Matthew Wilcox
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Wilcox @ 2021-10-20 11:06 UTC (permalink / raw)
  To: David Howells
  Cc: kent.overstreet, Andrew Morton, Jeff Layton, linux-mm,
	linux-fsdevel, linux-kernel

On Wed, Oct 20, 2021 at 11:10:31AM +0100, David Howells wrote:
> Under some circumstances, filemap_read() will allocate sufficient pages to
> read to the end of the file, call readahead/readpages on them and copy the
> data over - and then it will allocate another page at the EOF and call
> readpage on that and then ignore it.  This is unnecessary and a waste of
> time and resources.
> 
> filemap_read() *does* check for this, but only after it has already done
> the allocation and I/O.  Fix this by checking before calling
> filemap_get_pages() also.
> 
> Changes:
>  v2) Break out of the loop immediately rather than going to put_pages (the
>      pvec is unoccupied).  Setting isize is then unnecessary.
> 
> Signed-off-by: David Howells <dhowells@redhat.com>
> cc: Kent Overstreet <kent.overstreet@gmail.com>
> cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> cc: Andrew Morton <akpm@linux-foundation.org>
> cc: Jeff Layton <jlayton@redhat.com>
> cc: linux-mm@kvack.org
> cc: linux-fsdevel@vger.kernel.org
> Link: https://lore.kernel.org/r/160588481358.3465195.16552616179674485179.stgit@warthog.procyon.org.uk/
> Link: https://lore.kernel.org/r/163456863216.2614702.6384850026368833133.stgit@warthog.procyon.org.uk/

Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>


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

end of thread, other threads:[~2021-10-20 11:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-20 10:10 [PATCH v2] mm: Stop filemap_read() from grabbing a superfluous page David Howells
2021-10-20 10:20 ` Jeff Layton
2021-10-20 11:06 ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).