Skip to content

Commit acdd9f8

Browse files
Hugh Dickinstorvalds
authored andcommitted
huge tmpfs: SGP_NOALLOC to stop collapse_file() on race
khugepaged's collapse_file() currently uses SGP_NOHUGE to tell shmem_getpage() not to try allocating a huge page, in the very unlikely event that a racing hole-punch removes the swapped or fallocated page as soon as i_pages lock is dropped. We want to consolidate shmem's huge decisions, removing SGP_HUGE and SGP_NOHUGE; but cannot quite persuade ourselves that it's okay to regress the protection in this case - Yang Shi points out that the huge page would remain indefinitely, charged to root instead of the intended memcg. collapse_file() should not even allocate a small page in this case: why proceed if someone is punching a hole? SGP_READ is almost the right flag here, except that it optimizes away from a fallocated page, with NULL to tell caller to fill with zeroes (like a hole); whereas collapse_file()'s sequence relies on using a cache page. Add SGP_NOALLOC just for this. There are too many consecutive "if (page"s there in shmem_getpage_gfp(): group it better; and fix the outdated "bring it back from swap" comment. Link: https://lkml.kernel.org/r/1355343b-acf-4653-ef79-6aee40214ac5@google.com Signed-off-by: Hugh Dickins <hughd@google.com> Reviewed-by: Yang Shi <shy828301@gmail.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Miaohe Lin <linmiaohe@huawei.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Rik van Riel <riel@surriel.com> Cc: Shakeel Butt <shakeelb@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c852023 commit acdd9f8

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

include/linux/shmem_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
9494
/* Flag allocation requirements to shmem_getpage */
9595
enum sgp_type {
9696
SGP_READ, /* don't exceed i_size, don't allocate page */
97+
SGP_NOALLOC, /* similar, but fail on hole or use fallocated page */
9798
SGP_CACHE, /* don't exceed i_size, may allocate page */
9899
SGP_NOHUGE, /* like SGP_CACHE, but no huge pages */
99100
SGP_HUGE, /* like SGP_CACHE, huge pages preferred */

mm/khugepaged.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ static void collapse_file(struct mm_struct *mm,
17211721
xas_unlock_irq(&xas);
17221722
/* swap in or instantiate fallocated page */
17231723
if (shmem_getpage(mapping->host, index, &page,
1724-
SGP_NOHUGE)) {
1724+
SGP_NOALLOC)) {
17251725
result = SCAN_FAIL;
17261726
goto xa_unlocked;
17271727
}

mm/shmem.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,26 +1854,31 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
18541854
return error;
18551855
}
18561856

1857-
if (page)
1857+
if (page) {
18581858
hindex = page->index;
1859-
if (page && sgp == SGP_WRITE)
1860-
mark_page_accessed(page);
1861-
1862-
/* fallocated page? */
1863-
if (page && !PageUptodate(page)) {
1859+
if (sgp == SGP_WRITE)
1860+
mark_page_accessed(page);
1861+
if (PageUptodate(page))
1862+
goto out;
1863+
/* fallocated page */
18641864
if (sgp != SGP_READ)
18651865
goto clear;
18661866
unlock_page(page);
18671867
put_page(page);
1868-
page = NULL;
1869-
hindex = index;
18701868
}
1871-
if (page || sgp == SGP_READ)
1872-
goto out;
18731869

18741870
/*
1875-
* Fast cache lookup did not find it:
1876-
* bring it back from swap or allocate.
1871+
* SGP_READ: succeed on hole, with NULL page, letting caller zero.
1872+
* SGP_NOALLOC: fail on hole, with NULL page, letting caller fail.
1873+
*/
1874+
*pagep = NULL;
1875+
if (sgp == SGP_READ)
1876+
return 0;
1877+
if (sgp == SGP_NOALLOC)
1878+
return -ENOENT;
1879+
1880+
/*
1881+
* Fast cache lookup and swap lookup did not find it: allocate.
18771882
*/
18781883

18791884
if (vma && userfaultfd_missing(vma)) {

0 commit comments

Comments
 (0)