From: Vernon Yang Date: Mon, 8 Sep 2025 12:31:28 +0000 (+0800) Subject: mm: shmem: fix too little space for tmpfs only fallback 4KB X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=04a6e5fdcdf489437d7f553d8dcec3cf497b0d94;p=users%2Fjedix%2Flinux-maple.git mm: shmem: fix too little space for tmpfs only fallback 4KB When the system memory is sufficient, allocating memory is always successful, but when tmpfs size is low (e.g. 1MB), it falls back directly from 2MB to 4KB, and other small granularity (8KB ~ 1024KB) will not be tried. Therefore add check whether the remaining space of tmpfs is sufficient for allocation. If there is too little space left, try smaller large folio. Link: https://lkml.kernel.org/r/20250908123128.900254-1-vernon2gm@gmail.com Fixes: acd7ccb284b8 ("mm: shmem: add large folio support for tmpfs") Signed-off-by: Vernon Yang Cc: Baolin Wang Cc: Daniel Gomez Cc: Hugh Dickins Signed-off-by: Andrew Morton --- diff --git a/mm/shmem.c b/mm/shmem.c index 8147a99a4b075..c750bcae0ee36 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1820,6 +1820,7 @@ static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault unsigned long orders) { struct vm_area_struct *vma = vmf ? vmf->vma : NULL; + struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); pgoff_t aligned_index; unsigned long pages; int order; @@ -1835,6 +1836,18 @@ static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault while (orders) { pages = 1UL << order; aligned_index = round_down(index, pages); + + /* + * Check whether the remaining space of tmpfs is sufficient for + * allocation. If there is too little space left, try smaller + * large folio. + */ + if (sbinfo->max_blocks && percpu_counter_read(&sbinfo->used_blocks) + + pages > sbinfo->max_blocks) { + order = next_order(&orders, order); + continue; + } + /* * Check for conflict before waiting on a huge allocation. * Conflict might be that a huge page has just been allocated