Skip to contentSkip to conversation

mm/filemap: fix infinite loop in generic_file_buffered_read()

PATCH New Suraj Jitindar Singh 1 participant 1 message Sep 19, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/filemap: fix infinite loop in generic_file_buffered_read()

From: Kent Overstreet <[email protected]>

commit 3644e2d2dda78e21edd8f5415b6d7ab03f5f54f3 upstream.

If iter->count is 0 and iocb->ki_pos is page aligned, this causes nr_pages to be 0.

Then in generic_file_buffered_read_get_pages() find_get_pages_contig() returns 0 - because we asked for 0 pages, so we call generic_file_buffered_read_no_cached_page() which attempts to add a page to the page cache, which fails with -EEXIST, and then we loop. Oops...

---
 mm/filemap.c | 3 +++
 1 file changed, 3 insertions(+)
mm/filemap.c +3 -0
index 3a983bc1a71c..3b0d8c6dd587 100644
@@ -2203,6 +2203,9 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb,
 
 	if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
 		return 0;
+	if (unlikely(!iov_iter_count(iter)))
+		return 0;
+
 	iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
 
 	index = *ppos >> PAGE_SHIFT;
Signature · 1 line
2.34.1