Skip to content

Commit 743a275

Browse files
Panky-codesbrauner
authored andcommitted
filemap: cap PTE range to be created to allowed zero fill in folio_map_range()
Usually the page cache does not extend beyond the size of the inode, therefore, no PTEs are created for folios that extend beyond the size. But with LBS support, we might extend page cache beyond the size of the inode as we need to guarantee folios of minimum order. While doing a read, do_fault_around() can create PTEs for pages that lie beyond the EOF leading to incorrect error return when accessing a page beyond the mapped file. Cap the PTE range to be created for the page cache up to the end of file(EOF) in filemap_map_pages() so that return error codes are consistent with POSIX[1] for LBS configurations. generic/749 has been created to trigger this edge case. This also fixes generic/749 for tmpfs with huge=always on systems with 4k base page size. [1](from mmap(2)) SIGBUS Attempted access to a page of the buffer that lies beyond the end of the mapped file. For an explanation of the treatment of the bytes in the page that corresponds to the end of a mapped file that is not a multiple of the page size, see NOTES. Signed-off-by: Luis Chamberlain <[email protected]> Signed-off-by: Pankaj Raghav <[email protected]> Link: https://lore.kernel.org/r/[email protected] Tested-by: David Howells <[email protected]> Acked-by: Darrick J. Wong <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Reviewed-by: Daniel Gomez <[email protected]> Reviewed-by: Dave Chinner <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent e220917 commit 743a275

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

mm/filemap.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3608,7 +3608,7 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
36083608
struct vm_area_struct *vma = vmf->vma;
36093609
struct file *file = vma->vm_file;
36103610
struct address_space *mapping = file->f_mapping;
3611-
pgoff_t last_pgoff = start_pgoff;
3611+
pgoff_t file_end, last_pgoff = start_pgoff;
36123612
unsigned long addr;
36133613
XA_STATE(xas, &mapping->i_pages, start_pgoff);
36143614
struct folio *folio;
@@ -3634,6 +3634,10 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
36343634
goto out;
36353635
}
36363636

3637+
file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE) - 1;
3638+
if (end_pgoff > file_end)
3639+
end_pgoff = file_end;
3640+
36373641
folio_type = mm_counter_file(folio);
36383642
do {
36393643
unsigned long end;

0 commit comments

Comments
 (0)