[RFC PATCH 20/76] mm: Stop generic_file_buffered_read() from grabbing a superfluous page

From: David Howells
Date: Fri Nov 20 2020 - 10:07:39 EST


Under some circumstances, generic_file_buffered_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.

Catch the overallocation in the "no_cached_page:" part and prevent it from
happening.

Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
---

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

diff --git a/mm/filemap.c b/mm/filemap.c
index cfb753955e36..5a63aa1dd71e 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2447,6 +2447,8 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb,
* Ok, it wasn't cached, so we need to create a new
* page..
*/
+ if ((index << PAGE_SHIFT) >= i_size_read(inode))
+ goto out;
page = page_cache_alloc(mapping);
if (!page) {
error = -ENOMEM;