Skip to content

Commit 74207de

Browse files
Kiryl Shutsemauakpm00
authored andcommitted
mm/memory: do not populate page table entries beyond i_size
Patch series "Fix SIGBUS semantics with large folios", v3. Accessing memory within a VMA, but beyond i_size rounded up to the next page size, is supposed to generate SIGBUS. Darrick reported[1] an xfstests regression in v6.18-rc1. generic/749 failed due to missing SIGBUS. This was caused by my recent changes that try to fault in the whole folio where possible: 19773df ("mm/fault: try to map the entire file folio in finish_fault()") 357b927 ("mm/filemap: map entire large folio faultaround") These changes did not consider i_size when setting up PTEs, leading to xfstest breakage. However, the problem has been present in the kernel for a long time - since huge tmpfs was introduced in 2016. The kernel happily maps PMD-sized folios as PMD without checking i_size. And huge=always tmpfs allocates PMD-size folios on any writes. I considered this corner case when I implemented a large tmpfs, and my conclusion was that no one in their right mind should rely on receiving a SIGBUS signal when accessing beyond i_size. I cannot imagine how it could be useful for the workload. But apparently filesystem folks care a lot about preserving strict SIGBUS semantics. Generic/749 was introduced last year with reference to POSIX, but no real workloads were mentioned. It also acknowledged the tmpfs deviation from the test case. POSIX indeed says[3]: References within the address range starting at pa and continuing for len bytes to whole pages following the end of an object shall result in delivery of a SIGBUS signal. The patchset fixes the regression introduced by recent changes as well as more subtle SIGBUS breakage due to split failure on truncation. This patch (of 2): Accesses within VMA, but beyond i_size rounded up to PAGE_SIZE are supposed to generate SIGBUS. Recent changes attempted to fault in full folio where possible. They did not respect i_size, which led to populating PTEs beyond i_size and breaking SIGBUS semantics. Darrick reported generic/749 breakage because of this. However, the problem existed before the recent changes. With huge=always tmpfs, any write to a file leads to PMD-size allocation. Following the fault-in of the folio will install PMD mapping regardless of i_size. Fix filemap_map_pages() and finish_fault() to not install: - PTEs beyond i_size; - PMD mappings across i_size; Make an exception for shmem/tmpfs that for long time intentionally mapped with PMDs across i_size. Link: https://lkml.kernel.org/r/20251027115636.82382-1-kirill@shutemov.name Link: https://lkml.kernel.org/r/20251027115636.82382-2-kirill@shutemov.name Signed-off-by: Kiryl Shutsemau <kas@kernel.org> Fixes: 6795801 ("xfs: Support large folios") Reported-by: "Darrick J. Wong" <djwong@kernel.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Christian Brauner <brauner@kernel.org> Cc: Dave Chinner <david@fromorbit.com> Cc: David Hildenbrand <david@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Rik van Riel <riel@surriel.com> Cc: Shakeel Butt <shakeel.butt@linux.dev> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 895b4c0 commit 74207de

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

mm/filemap.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,7 +3681,8 @@ static struct folio *next_uptodate_folio(struct xa_state *xas,
36813681
static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
36823682
struct folio *folio, unsigned long start,
36833683
unsigned long addr, unsigned int nr_pages,
3684-
unsigned long *rss, unsigned short *mmap_miss)
3684+
unsigned long *rss, unsigned short *mmap_miss,
3685+
bool can_map_large)
36853686
{
36863687
unsigned int ref_from_caller = 1;
36873688
vm_fault_t ret = 0;
@@ -3696,7 +3697,7 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
36963697
* The folio must not cross VMA or page table boundary.
36973698
*/
36983699
addr0 = addr - start * PAGE_SIZE;
3699-
if (folio_within_vma(folio, vmf->vma) &&
3700+
if (can_map_large && folio_within_vma(folio, vmf->vma) &&
37003701
(addr0 & PMD_MASK) == ((addr0 + folio_size(folio) - 1) & PMD_MASK)) {
37013702
vmf->pte -= start;
37023703
page -= start;
@@ -3811,13 +3812,27 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
38113812
unsigned long rss = 0;
38123813
unsigned int nr_pages = 0, folio_type;
38133814
unsigned short mmap_miss = 0, mmap_miss_saved;
3815+
bool can_map_large;
38143816

38153817
rcu_read_lock();
38163818
folio = next_uptodate_folio(&xas, mapping, end_pgoff);
38173819
if (!folio)
38183820
goto out;
38193821

3820-
if (filemap_map_pmd(vmf, folio, start_pgoff)) {
3822+
file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE) - 1;
3823+
end_pgoff = min(end_pgoff, file_end);
3824+
3825+
/*
3826+
* Do not allow to map with PTEs beyond i_size and with PMD
3827+
* across i_size to preserve SIGBUS semantics.
3828+
*
3829+
* Make an exception for shmem/tmpfs that for long time
3830+
* intentionally mapped with PMDs across i_size.
3831+
*/
3832+
can_map_large = shmem_mapping(mapping) ||
3833+
file_end >= folio_next_index(folio);
3834+
3835+
if (can_map_large && filemap_map_pmd(vmf, folio, start_pgoff)) {
38213836
ret = VM_FAULT_NOPAGE;
38223837
goto out;
38233838
}
@@ -3830,10 +3845,6 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
38303845
goto out;
38313846
}
38323847

3833-
file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE) - 1;
3834-
if (end_pgoff > file_end)
3835-
end_pgoff = file_end;
3836-
38373848
folio_type = mm_counter_file(folio);
38383849
do {
38393850
unsigned long end;
@@ -3850,7 +3861,8 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
38503861
else
38513862
ret |= filemap_map_folio_range(vmf, folio,
38523863
xas.xa_index - folio->index, addr,
3853-
nr_pages, &rss, &mmap_miss);
3864+
nr_pages, &rss, &mmap_miss,
3865+
can_map_large);
38543866

38553867
folio_unlock(folio);
38563868
} while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);

mm/memory.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <linux/gfp.h>
6666
#include <linux/migrate.h>
6767
#include <linux/string.h>
68+
#include <linux/shmem_fs.h>
6869
#include <linux/memory-tiers.h>
6970
#include <linux/debugfs.h>
7071
#include <linux/userfaultfd_k.h>
@@ -5501,8 +5502,25 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
55015502
return ret;
55025503
}
55035504

5505+
if (!needs_fallback && vma->vm_file) {
5506+
struct address_space *mapping = vma->vm_file->f_mapping;
5507+
pgoff_t file_end;
5508+
5509+
file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
5510+
5511+
/*
5512+
* Do not allow to map with PTEs beyond i_size and with PMD
5513+
* across i_size to preserve SIGBUS semantics.
5514+
*
5515+
* Make an exception for shmem/tmpfs that for long time
5516+
* intentionally mapped with PMDs across i_size.
5517+
*/
5518+
needs_fallback = !shmem_mapping(mapping) &&
5519+
file_end < folio_next_index(folio);
5520+
}
5521+
55045522
if (pmd_none(*vmf->pmd)) {
5505-
if (folio_test_pmd_mappable(folio)) {
5523+
if (!needs_fallback && folio_test_pmd_mappable(folio)) {
55065524
ret = do_set_pmd(vmf, folio, page);
55075525
if (ret != VM_FAULT_FALLBACK)
55085526
return ret;

0 commit comments

Comments
 (0)