From nobody Wed Feb 11 05:04:57 2026 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 205044D8C1 for ; Wed, 24 Jul 2024 07:09:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721804988; cv=none; b=Efv08QBSRPz4eajCedf8h1hDpO+fDD5ginyzHdwW7F6/nQC6lHyF/wRL6KkDhDET/ZC99geyqoZdcekCcovvg8K1lc+xFun72jnJWtsDqT5HuVFmcXriYhNzn0Rc8QXZ9A7UAysWO7h8Xk4ZUEJrD18DkWQIKBjdBoRrIKGWiV0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721804988; c=relaxed/simple; bh=/FSCp9MYs7Fjl/w2a2kuAhE78vlHDZzXvzlaAb03DBs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WS3GkGsBQcz3JlcaVre0H43rpXESnEsLX/qP0KFfgxCTTjiuo7AdKHFwfz5j1ht0CNVqm24f7zIhaQFH9W5c+IPev4Wn5AEJZEnWZzg4RK5+twQ2XG81n7Wuq2XlSAFQTOWaFemderNU/fuoXyzvuy7muZp4R2IuCklhLqeqYAA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=eOXCl17L; arc=none smtp.client-ip=115.124.30.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="eOXCl17L" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1721804978; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=IgdtO0YJm1XtjSUXDNrZ8xElYsmGF7utZpmG/kiJcWg=; b=eOXCl17Lur8cOw1XteLn83KcttdQoDxMCd8YMW9IwOccdzL9YIQ9tCOdLG5EfasqJpmEt7d0pQOcWRa6YcwbZGvjpbhlYDmKKf4KumlGvmbNLnpS9eIstEv/rt9qOScg5J5fAsOEUtXEldl6+0OzbbFJmZV9AxW9ynYFsubnNSs= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045220184;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0WBDDM2T_1721804659; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WBDDM2T_1721804659) by smtp.aliyun-inc.com; Wed, 24 Jul 2024 15:04:19 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, ziy@nvidia.com, ioworker0@gmail.com, da.gomez@samsung.com, p.raghav@samsung.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/3] mm: shmem: add file length arg in shmem_get_folio() path Date: Wed, 24 Jul 2024 15:03:58 +0800 Message-Id: <70972d294797b377bf24a7290659e9057b978287.1721720891.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniel Gomez In preparation for large folio in the write and fallocate paths, add file length argument in shmem_get_folio() path to be able to calculate the folio order based on the file size. Use of order-0 (PAGE_SIZE) for read, page cache read, and vm fault. This enables high order folios in the write and fallocate path once the folio order is calculated based on the length. Signed-off-by: Daniel Gomez Signed-off-by: Baolin Wang --- fs/xfs/scrub/xfile.c | 6 +++--- fs/xfs/xfs_buf_mem.c | 3 ++- include/linux/shmem_fs.h | 2 +- mm/khugepaged.c | 3 ++- mm/shmem.c | 28 ++++++++++++++++------------ mm/userfaultfd.c | 2 +- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/fs/xfs/scrub/xfile.c b/fs/xfs/scrub/xfile.c index d848222f802b..d814d9d786d3 100644 --- a/fs/xfs/scrub/xfile.c +++ b/fs/xfs/scrub/xfile.c @@ -127,7 +127,7 @@ xfile_load( unsigned int offset; =20 if (shmem_get_folio(inode, pos >> PAGE_SHIFT, &folio, - SGP_READ) < 0) + SGP_READ, PAGE_SIZE) < 0) break; if (!folio) { /* @@ -197,7 +197,7 @@ xfile_store( unsigned int offset; =20 if (shmem_get_folio(inode, pos >> PAGE_SHIFT, &folio, - SGP_CACHE) < 0) + SGP_CACHE, PAGE_SIZE) < 0) break; if (filemap_check_wb_err(inode->i_mapping, 0)) { folio_unlock(folio); @@ -268,7 +268,7 @@ xfile_get_folio( =20 pflags =3D memalloc_nofs_save(); error =3D shmem_get_folio(inode, pos >> PAGE_SHIFT, &folio, - (flags & XFILE_ALLOC) ? SGP_CACHE : SGP_READ); + (flags & XFILE_ALLOC) ? SGP_CACHE : SGP_READ, PAGE_SIZE); memalloc_nofs_restore(pflags); if (error) return ERR_PTR(error); diff --git a/fs/xfs/xfs_buf_mem.c b/fs/xfs/xfs_buf_mem.c index 9bb2d24de709..784c81d35a1f 100644 --- a/fs/xfs/xfs_buf_mem.c +++ b/fs/xfs/xfs_buf_mem.c @@ -149,7 +149,8 @@ xmbuf_map_page( return -ENOMEM; } =20 - error =3D shmem_get_folio(inode, pos >> PAGE_SHIFT, &folio, SGP_CACHE); + error =3D shmem_get_folio(inode, pos >> PAGE_SHIFT, &folio, SGP_CACHE, + PAGE_SIZE); if (error) return error; =20 diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 1564d7d3ca61..34beaca2f853 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -144,7 +144,7 @@ enum sgp_type { }; =20 int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **fol= iop, - enum sgp_type sgp); + enum sgp_type sgp, size_t len); struct folio *shmem_read_folio_gfp(struct address_space *mapping, pgoff_t index, gfp_t gfp); =20 diff --git a/mm/khugepaged.c b/mm/khugepaged.c index a5ec03ef8722..3c9dbebbdf38 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1867,7 +1867,8 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, xas_unlock_irq(&xas); /* swap in or instantiate fallocated page */ if (shmem_get_folio(mapping->host, index, - &folio, SGP_NOALLOC)) { + &folio, SGP_NOALLOC, + PAGE_SIZE)) { result =3D SCAN_FAIL; goto xa_unlocked; } diff --git a/mm/shmem.c b/mm/shmem.c index db8f74cac1a2..92ed09527682 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -980,7 +980,7 @@ static struct folio *shmem_get_partial_folio(struct ino= de *inode, pgoff_t index) * (although in some cases this is just a waste of time). */ folio =3D NULL; - shmem_get_folio(inode, index, &folio, SGP_READ); + shmem_get_folio(inode, index, &folio, SGP_READ, PAGE_SIZE); return folio; } =20 @@ -2094,7 +2094,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, */ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, struct folio **foliop, enum sgp_type sgp, gfp_t gfp, - struct vm_fault *vmf, vm_fault_t *fault_type) + struct vm_fault *vmf, vm_fault_t *fault_type, size_t len) { struct vm_area_struct *vma =3D vmf ? vmf->vma : NULL; struct mm_struct *fault_mm; @@ -2297,10 +2297,10 @@ static int shmem_get_folio_gfp(struct inode *inode,= pgoff_t index, * Return: 0 if successful, else a negative error code. */ int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **fol= iop, - enum sgp_type sgp) + enum sgp_type sgp, size_t len) { return shmem_get_folio_gfp(inode, index, foliop, sgp, - mapping_gfp_mask(inode->i_mapping), NULL, NULL); + mapping_gfp_mask(inode->i_mapping), NULL, NULL, len); } EXPORT_SYMBOL_GPL(shmem_get_folio); =20 @@ -2395,7 +2395,7 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf) =20 WARN_ON_ONCE(vmf->page !=3D NULL); err =3D shmem_get_folio_gfp(inode, vmf->pgoff, &folio, SGP_CACHE, - gfp, vmf, &ret); + gfp, vmf, &ret, PAGE_SIZE); if (err) return vmf_error(err); if (folio) { @@ -2895,6 +2895,9 @@ shmem_write_begin(struct file *file, struct address_s= pace *mapping, struct folio *folio; int ret =3D 0; =20 + if (!mapping_large_folio_support(mapping)) + len =3D min_t(size_t, len, PAGE_SIZE - offset_in_page(pos)); + /* i_rwsem is held by caller */ if (unlikely(info->seals & (F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { @@ -2904,7 +2907,7 @@ shmem_write_begin(struct file *file, struct address_s= pace *mapping, return -EPERM; } =20 - ret =3D shmem_get_folio(inode, index, &folio, SGP_WRITE); + ret =3D shmem_get_folio(inode, index, &folio, SGP_WRITE, len); if (ret) return ret; =20 @@ -2975,7 +2978,7 @@ static ssize_t shmem_file_read_iter(struct kiocb *ioc= b, struct iov_iter *to) break; } =20 - error =3D shmem_get_folio(inode, index, &folio, SGP_READ); + error =3D shmem_get_folio(inode, index, &folio, SGP_READ, PAGE_SIZE); if (error) { if (error =3D=3D -EINVAL) error =3D 0; @@ -3152,7 +3155,7 @@ static ssize_t shmem_file_splice_read(struct file *in= , loff_t *ppos, break; =20 error =3D shmem_get_folio(inode, *ppos / PAGE_SIZE, &folio, - SGP_READ); + SGP_READ, PAGE_SIZE); if (error) { if (error =3D=3D -EINVAL) error =3D 0; @@ -3339,7 +3342,8 @@ static long shmem_fallocate(struct file *file, int mo= de, loff_t offset, error =3D -ENOMEM; else error =3D shmem_get_folio(inode, index, &folio, - SGP_FALLOC); + SGP_FALLOC, + (end - index) << PAGE_SHIFT); if (error) { info->fallocend =3D undo_fallocend; /* Remove the !uptodate folios we added */ @@ -3690,7 +3694,7 @@ static int shmem_symlink(struct mnt_idmap *idmap, str= uct inode *dir, } else { inode_nohighmem(inode); inode->i_mapping->a_ops =3D &shmem_aops; - error =3D shmem_get_folio(inode, 0, &folio, SGP_WRITE); + error =3D shmem_get_folio(inode, 0, &folio, SGP_WRITE, PAGE_SIZE); if (error) goto out_remove_offset; inode->i_op =3D &shmem_symlink_inode_operations; @@ -3736,7 +3740,7 @@ static const char *shmem_get_link(struct dentry *dent= ry, struct inode *inode, return ERR_PTR(-ECHILD); } } else { - error =3D shmem_get_folio(inode, 0, &folio, SGP_READ); + error =3D shmem_get_folio(inode, 0, &folio, SGP_READ, PAGE_SIZE); if (error) return ERR_PTR(error); if (!folio) @@ -5209,7 +5213,7 @@ struct folio *shmem_read_folio_gfp(struct address_spa= ce *mapping, int error; =20 error =3D shmem_get_folio_gfp(inode, index, &folio, SGP_CACHE, - gfp, NULL, NULL); + gfp, NULL, NULL, PAGE_SIZE); if (error) return ERR_PTR(error); =20 diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index e54e5c8907fa..c275e34c435a 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -391,7 +391,7 @@ static int mfill_atomic_pte_continue(pmd_t *dst_pmd, struct page *page; int ret; =20 - ret =3D shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC); + ret =3D shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC, PAGE_SIZE); /* Our caller expects us to return -EFAULT if we failed to find folio */ if (ret =3D=3D -ENOENT) ret =3D -EFAULT; --=20 2.39.3 From nobody Wed Feb 11 05:04:58 2026 Received: from out30-113.freemail.mail.aliyun.com (out30-113.freemail.mail.aliyun.com [115.124.30.113]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1560543AA1 for ; Wed, 24 Jul 2024 07:04:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.113 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721804672; cv=none; b=FctLyAgGKzcE2/wM2xyvmQ9V51d5D5u0ljeYext5X780qjQ6VkOH8S75Ahesdu2JN+NrNMTohyVKycTTKOgin93ODene4LFcycRDdgs/sri0FDn2A5HX5l/iKUZ0rt2SsD5I7iqil22mgQfKdzudLnvhthnAjHAkasWxID45mQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721804672; c=relaxed/simple; bh=fCiHkjMmxgKbcT4qE8kEtbnUUlIyom051hu5UsrLiD0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DKmX43rCKQFl/6IhTQwxQEbKJhCwPlHwVKqHd+Y6b9dI0lwtF0UKfa4Nr+6feVvf54oaVGUrV34rU29fQ7WCRykuqKijz3V/vsIZM0Vb9wV8kKa1tWr15LUuxmAPsdKrLjfYyEGTj1Wv9MmF4jd3hRYPkucHftLMA4VYjAtJtOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=Ua/zCN0e; arc=none smtp.client-ip=115.124.30.113 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="Ua/zCN0e" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1721804662; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=GJHtzrbfRGkXA1beo6I8v9VjRBWlqaeEznsY5RTbsHE=; b=Ua/zCN0eLIbXjFO4dYelpVgtUBvPbYS8e9bA9zRjsFI5JRwgNw+ayGQK0Tpk0xdQHnNIusorkzZ4GE2iqW7BgJsZT7CdeOwea/R2Kwtk20drU3yrR9Zozl+Z1dERZfemT0nsmpKzz/YgluJFAcsd1ZZeYZcWEM4RHOZb96Biq2A= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R551e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045220184;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0WBDNN8B_1721804660; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WBDNN8B_1721804660) by smtp.aliyun-inc.com; Wed, 24 Jul 2024 15:04:20 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, ziy@nvidia.com, ioworker0@gmail.com, da.gomez@samsung.com, p.raghav@samsung.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/3] mm: shmem: add large folio support to the write and fallocate paths Date: Wed, 24 Jul 2024 15:03:59 +0800 Message-Id: <05a31373ee1aa40a6a85d4897324a400686e2fb1.1721720891.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniel Gomez Add large folio support for shmem write and fallocate paths matching the same high order preference mechanism used in the iomap buffered IO path as used in __filemap_get_folio(). Add shmem_mapping_size_order() to get a hint for the order of the folio based on the file size which takes care of the mapping requirements. Swap does not support high order folios for now, so make it order-0 in case swap is enabled. If the top level huge page (controlled by '/sys/kernel/mm/transparent_hugep= age/shmem_enabled') is enabled, we just allow PMD sized THP to keep interface backward compatibility. Co-developed-by: Baolin Wang Signed-off-by: Daniel Gomez Signed-off-by: Baolin Wang --- include/linux/shmem_fs.h | 4 +-- mm/huge_memory.c | 2 +- mm/shmem.c | 57 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 34beaca2f853..fb0771218f1b 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -113,11 +113,11 @@ int shmem_unuse(unsigned int type); #ifdef CONFIG_TRANSPARENT_HUGEPAGE unsigned long shmem_allowable_huge_orders(struct inode *inode, struct vm_area_struct *vma, pgoff_t index, - bool shmem_huge_force); + bool shmem_huge_force, size_t len); #else static inline unsigned long shmem_allowable_huge_orders(struct inode *inod= e, struct vm_area_struct *vma, pgoff_t index, - bool shmem_huge_force) + bool shmem_huge_force, size_t len) { return 0; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index e555fcdd19d4..a8fc3b9e4034 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -162,7 +162,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area= _struct *vma, if (!in_pf && shmem_file(vma->vm_file)) return shmem_allowable_huge_orders(file_inode(vma->vm_file), vma, vma->vm_pgoff, - !enforce_sysfs); + !enforce_sysfs, PAGE_SIZE); =20 if (!vma_is_anonymous(vma)) { /* diff --git a/mm/shmem.c b/mm/shmem.c index 92ed09527682..cc0c1b790267 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1630,10 +1630,47 @@ static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t l= imit_gfp) return result; } =20 +/** + * shmem_mapping_size_order - Get maximum folio order for the given file s= ize. + * @mapping: Target address_space. + * @index: The page index. + * @size: The suggested size of the folio to create. + * + * This returns a high order for folios (when supported) based on the file= size + * which the mapping currently allows at the given index. The index is rel= evant + * due to alignment considerations the mapping might have. The returned or= der + * may be less than the size passed. + * + * Like __filemap_get_folio order calculation. + * + * Return: The order. + */ +static inline unsigned int +shmem_mapping_size_order(struct address_space *mapping, pgoff_t index, + size_t size, struct shmem_sb_info *sbinfo) +{ + unsigned int order =3D ilog2(size); + + if ((order <=3D PAGE_SHIFT) || + (!mapping_large_folio_support(mapping) || !sbinfo->noswap)) + return 0; + + order -=3D PAGE_SHIFT; + + /* If we're not aligned, allocate a smaller folio */ + if (index & ((1UL << order) - 1)) + order =3D __ffs(index); + + order =3D min_t(size_t, order, MAX_PAGECACHE_ORDER); + + /* Order-1 not supported due to THP dependency */ + return (order =3D=3D 1) ? 0 : order; +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE unsigned long shmem_allowable_huge_orders(struct inode *inode, struct vm_area_struct *vma, pgoff_t index, - bool shmem_huge_force) + bool shmem_huge_force, size_t len) { unsigned long mask =3D READ_ONCE(huge_shmem_orders_always); unsigned long within_size_orders =3D READ_ONCE(huge_shmem_orders_within_s= ize); @@ -1659,10 +1696,20 @@ unsigned long shmem_allowable_huge_orders(struct in= ode *inode, vma, vm_flags); if (!vma || !vma_is_anon_shmem(vma)) { /* - * For tmpfs, we now only support PMD sized THP if huge page - * is enabled, otherwise fallback to order 0. + * For tmpfs, if top level huge page is enabled, we just allow + * PMD size THP to keep interface backward compatibility. + */ + if (global_huge) + return BIT(HPAGE_PMD_ORDER); + + /* + * Otherwise, get a highest order hint based on the size of + * write and fallocate paths, then will try each allowable + * huge orders. */ - return global_huge ? BIT(HPAGE_PMD_ORDER) : 0; + order =3D shmem_mapping_size_order(inode->i_mapping, index, + len, SHMEM_SB(inode->i_sb)); + return BIT(order + 1) - 1; } =20 /* @@ -2174,7 +2221,7 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, } =20 /* Find hugepage orders that are allowed for anonymous shmem and tmpfs. */ - orders =3D shmem_allowable_huge_orders(inode, vma, index, false); + orders =3D shmem_allowable_huge_orders(inode, vma, index, false, len); if (orders > 0) { gfp_t huge_gfp; =20 --=20 2.39.3 From nobody Wed Feb 11 05:04:58 2026 Received: from out199-13.us.a.mail.aliyun.com (out199-13.us.a.mail.aliyun.com [47.90.199.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB1A37C0B7 for ; Wed, 24 Jul 2024 07:04:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=47.90.199.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721804682; cv=none; b=cd2QTwg6x3FurqJPeJwUfTk9abidfY8VxbbfpTsPPQEZAhgtRYOEFVmv55IxeWBBaXFP8j9UUx8tG4rHJa2oW/HJgLpXWMmgBPTns+sHHYOCkAom4AREVAVwSpRQBjFQe2MoZleEf3Fe9TAtPC8my4klmIOHxOZlrJXjEnAUCS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721804682; c=relaxed/simple; bh=1eEI2J/7NFOfsTYeA5VjrGvkQpPcv9rU6r1M/49RL84=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SYx3OxIXNySgIaFvbjm/R6rMfUEq6sHvZElqgeYiZ0eTgRBBnRa/lUwCvf/gTAxXbDYprLzgMb5NQO5N/H6bYm7bUCV1/ladIylmGgU3lPZ9PZZ9SO+4ItPTqahn7PU0LMo2riMI1mGi0wn1viRUYQr7gIQCfSp2Xp8K6Y8DLfw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=tY+ggRWe; arc=none smtp.client-ip=47.90.199.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="tY+ggRWe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1721804663; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=tdqWP64rTR+e1Asc5O6zn2fOj7NNlZAEMcoj2njyV9U=; b=tY+ggRWeZ7iwXAAcHSVxs1nslPchK6vv5cwzoJ0+vkrp6BtYWmlSvbfOjNAJRF7KjVLshyFhluJTpafbbUFYtSOW3vGGJauo0quUGz3EHZ27prrpo/Cewxr1mP7R6oDmUapkZ7L/Y1B74X6WXzmr5KJmZvCm2eDHTMehGcHV9F8= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067112;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0WBDDM3B_1721804661; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WBDDM3B_1721804661) by smtp.aliyun-inc.com; Wed, 24 Jul 2024 15:04:21 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, ziy@nvidia.com, ioworker0@gmail.com, da.gomez@samsung.com, p.raghav@samsung.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 3/3] mm: shmem: use mTHP interface to control huge orders for tmpfs Date: Wed, 24 Jul 2024 15:04:00 +0800 Message-Id: <03c77c20a8a6ccdd90678dcc6bf7d4aeaa9d29ad.1721720891.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For the huge orders allowed by writable mmap() faults on tmpfs, the mTHP interface is used to control the allowable huge orders, while 'huge_shmem_orders_inherit' maintains backward compatibility with top-level interface. For the huge orders allowed by write() and fallocate() paths on tmpfs, getting a highest order hint based on the size of write and fallocate paths, then will try each allowable huge orders filtered by the mTHP interfaces if set. Signed-off-by: Baolin Wang --- mm/memory.c | 4 ++-- mm/shmem.c | 42 ++++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 802d0d8a40f9..3a7f43c66db7 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4877,10 +4877,10 @@ vm_fault_t finish_fault(struct vm_fault *vmf) =20 /* * Using per-page fault to maintain the uffd semantics, and same - * approach also applies to non-anonymous-shmem faults to avoid + * approach also applies to non shmem/tmpfs faults to avoid * inflating the RSS of the process. */ - if (!vma_is_anon_shmem(vma) || unlikely(userfaultfd_armed(vma))) { + if (!vma_is_shmem(vma) || unlikely(userfaultfd_armed(vma))) { nr_pages =3D 1; } else if (nr_pages > 1) { pgoff_t idx =3D folio_page_idx(folio, page); diff --git a/mm/shmem.c b/mm/shmem.c index cc0c1b790267..8e60cc566196 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1692,26 +1692,6 @@ unsigned long shmem_allowable_huge_orders(struct ino= de *inode, if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED)) return 0; =20 - global_huge =3D shmem_huge_global_enabled(inode, index, shmem_huge_force, - vma, vm_flags); - if (!vma || !vma_is_anon_shmem(vma)) { - /* - * For tmpfs, if top level huge page is enabled, we just allow - * PMD size THP to keep interface backward compatibility. - */ - if (global_huge) - return BIT(HPAGE_PMD_ORDER); - - /* - * Otherwise, get a highest order hint based on the size of - * write and fallocate paths, then will try each allowable - * huge orders. - */ - order =3D shmem_mapping_size_order(inode->i_mapping, index, - len, SHMEM_SB(inode->i_sb)); - return BIT(order + 1) - 1; - } - /* * Following the 'deny' semantics of the top level, force the huge * option off from all mounts. @@ -1742,9 +1722,31 @@ unsigned long shmem_allowable_huge_orders(struct ino= de *inode, if (vm_flags & VM_HUGEPAGE) mask |=3D READ_ONCE(huge_shmem_orders_madvise); =20 + global_huge =3D shmem_huge_global_enabled(inode, index, shmem_huge_force, + vma, vm_flags); if (global_huge) mask |=3D READ_ONCE(huge_shmem_orders_inherit); =20 + /* + * For the huge orders allowed by writable mmap() faults on tmpfs, + * the mTHP interface is used to control the allowable huge orders, + * while 'huge_shmem_orders_inherit' maintains backward compatibility + * with top-level interface. + * + * For the huge orders allowed by write() and fallocate() paths on tmpfs, + * get a highest order hint based on the size of write and fallocate + * paths, then will try each allowable huge orders filtered by the mTHP + * interfaces if set. + */ + if (!vma && !global_huge) { + int highest_order =3D shmem_mapping_size_order(inode->i_mapping, index, = len, + SHMEM_SB(inode->i_sb)); + + if (!mask) + return highest_order > 0 ? BIT(highest_order + 1) - 1 : 0; + + mask &=3D BIT(highest_order + 1) - 1; + } return orders & mask; } =20 --=20 2.39.3