From patchwork Wed Jul 24 07:03:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13740626 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E57AC3DA63 for ; Wed, 24 Jul 2024 07:04:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC91A6B008A; Wed, 24 Jul 2024 03:04:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A78A46B008C; Wed, 24 Jul 2024 03:04:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91A1C6B0092; Wed, 24 Jul 2024 03:04:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6AC206B008A for ; Wed, 24 Jul 2024 03:04:35 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E5D2DA06E6 for ; Wed, 24 Jul 2024 07:04:34 +0000 (UTC) X-FDA: 82373758068.03.DF7786A Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) by imf19.hostedemail.com (Postfix) with ESMTP id 818A31A0009 for ; Wed, 24 Jul 2024 07:04:31 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="Ua/zCN0e"; spf=pass (imf19.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721804610; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GJHtzrbfRGkXA1beo6I8v9VjRBWlqaeEznsY5RTbsHE=; b=L3c/udbIKTvCiFHR3ish+pWe9Rk0169aw0CJ0e3cQViwKmhDsXs8gnS5THJYpSH/geURBZ PBBSO0mJGC3E7LXEjf8oLNnlcNWBv6fMd/IxkEM161nw+zd0IMy8LltYw4rq6ID94CVNXr mHidwKvAWuAtSHU5swA+elwliMHUREQ= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b="Ua/zCN0e"; spf=pass (imf19.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721804610; a=rsa-sha256; cv=none; b=aumnpRxC5DQREPMWTpmd7Q0CFMHuuSyzap7AQVZbm+yXh9NObpJBGZ32+TmTZoF42nShQv WXYrI8wPU+s5lmS9WvA24GR1BtLP9ycaYtPCgTRBQ/+r4qATMs9f08vdnLqPyq7t42Q35H XRzIAfoJTc1ESUDpONlivyZFg5By/Tg= 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: MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 818A31A0009 X-Stat-Signature: tmcxq4grpbqpetpdwi3qzy38tx5og69s X-Rspam-User: X-HE-Tag: 1721804671-893732 X-HE-Meta: U2FsdGVkX189iCtmd6n9OBuhHKK1hy496x7j+oEkduePldMXar+IE760QaE+CBuiC7aw0UJOsB7DN5PpQ75f6Ns5fV7HpO8Hl3x4cr1j0vr0ubHLZ+gQo88p4y1oNKlVbE4IzAeodO0mtW3vl2avbB/W7rLslkh4BmN0p5FseZyMkp7ALB28vLRxBwSip6qsKfRzrKNz0bFjYJNnrVk5ZX5OtAVJkvIocWWxyVWnqY3hmUMdKVV05LFjOXoNYcWnC7hT8Rs2FDFFtyT3WlmSSISBi3NT8l6Tj2CbL7s2yMRUIN0rVFe6g24KQCNgeyyAt/jjx4+z6fqKqXRlWMJz8SiwljFK+6MvD8T6jmye27evNPtJuxK4/0+YYP3uGKO1xk1/EkOCVSbQpsOaRfbr1hUK3rFVZCfBbfvOzTRpBYd9XMR31AMCDA4mhxh7Optiv3KeXCpA9YB65UWt0VK7+VFCjHoSwo89oKl9ILEDATBJgxaMznD+ZLTTeNARqtuGs2in8tmP40PRq8KQ0likrjNICzOMxNEHxiUu2hAql2OOlawbqrvwH9/U4XA4/poRmwwyxKykhIJ+Krqfr+pIzdv35jTvuOYkuddDHO+l5y6j0oVCdUZqcegKpnfwxfCgnKK6HRQpOftZFYS8GLet1LVnxO9qClqoJ/tCeiY/LgxfcF59p2AyXLhst/Mt3M9u+GWmnRLgC7p/FDKJ5TNki1sgCY6OoLjGKYqBt+uQOttaJAWlTgeR63wd059ak1wYBrDqJEnO2800dskNL5MotEnf70y86mBEHe7oePWxAQEf5z/b2ieCrBvnmFAia3xIZHboHMvehIKBcHi/tXUPDT+HlMFk7jilwIZaM2hjN3nA3KoONWMma+67aXdxdrHn+qV+esU+iALsFXCrxIcFenSURoGdxFwk26ZGun9T3hxYCTbBRCXb1am7qL4btYyKBvzfAObXKu4KtJz9n6y MoRmwW/y ffo3Iz4H9IwK6qya9ue7M00TtVwoWf3aauyG8q81xdaHMxqBLGYJzo+NGnVrLjb2fdy/MBqQ0tg0Yun3KahwhN6e8XLwReHRlDZ0PF0WDlyTtZMImM1US2+sIDCeVWC+7zZXlOeg0BvC20UsUfVodCngHGvdRRTymU8qycphexVhpWn/VxPbqlpr7/LenDMJLMQiVFOgK+YJEspaupAUXKAmt3d5XgVRu6cbX X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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_hugepage/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 *inode, 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); 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 limit_gfp) return result; } +/** + * shmem_mapping_size_order - Get maximum folio order for the given file size. + * @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 relevant + * due to alignment considerations the mapping might have. The returned order + * 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 = ilog2(size); + + if ((order <= PAGE_SHIFT) || + (!mapping_large_folio_support(mapping) || !sbinfo->noswap)) + return 0; + + order -= PAGE_SHIFT; + + /* If we're not aligned, allocate a smaller folio */ + if (index & ((1UL << order) - 1)) + order = __ffs(index); + + order = min_t(size_t, order, MAX_PAGECACHE_ORDER); + + /* Order-1 not supported due to THP dependency */ + return (order == 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 = READ_ONCE(huge_shmem_orders_always); unsigned long within_size_orders = READ_ONCE(huge_shmem_orders_within_size); @@ -1659,10 +1696,20 @@ unsigned long shmem_allowable_huge_orders(struct inode *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 = shmem_mapping_size_order(inode->i_mapping, index, + len, SHMEM_SB(inode->i_sb)); + return BIT(order + 1) - 1; } /* @@ -2174,7 +2221,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, } /* Find hugepage orders that are allowed for anonymous shmem and tmpfs. */ - orders = shmem_allowable_huge_orders(inode, vma, index, false); + orders = shmem_allowable_huge_orders(inode, vma, index, false, len); if (orders > 0) { gfp_t huge_gfp;