From patchwork Wed May 15 05:57:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Gomez X-Patchwork-Id: 13664610 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 A44C0C25B75 for ; Wed, 15 May 2024 05:58:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41DC86B03DB; Wed, 15 May 2024 01:57:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CF446B03DD; Wed, 15 May 2024 01:57:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21FFC6B03DC; Wed, 15 May 2024 01:57:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E73786B03DA for ; Wed, 15 May 2024 01:57:45 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A9D9F1C1881 for ; Wed, 15 May 2024 05:57:45 +0000 (UTC) X-FDA: 82119573690.12.299E843 Received: from mailout1.w1.samsung.com (mailout1.w1.samsung.com [210.118.77.11]) by imf01.hostedemail.com (Postfix) with ESMTP id 7096D40007 for ; Wed, 15 May 2024 05:57:42 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=samsung.com header.s=mail20170921 header.b=SONL4RGQ; dmarc=pass (policy=none) header.from=samsung.com; spf=pass (imf01.hostedemail.com: domain of da.gomez@samsung.com designates 210.118.77.11 as permitted sender) smtp.mailfrom=da.gomez@samsung.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715752662; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SA7vsxMb4nPPaawAJ345Tbdd6TJyw9vmKosmLEJCqb4=; b=Y+HYhtu3SdNVy0tsm7OCH9OVP9MFDH55w7YtALYwIWS/2v1UQB359OHhfeAIoptYS1yNAt CRYR2GkiBTeTrWlPALFsTNAahgF5//7hP55wZ25BOqM4Eki3zpWX24iHkC/t1YBNIFKfdV t56W77/ULCwiN6sWWP9aHFyZNYC8kjM= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=samsung.com header.s=mail20170921 header.b=SONL4RGQ; dmarc=pass (policy=none) header.from=samsung.com; spf=pass (imf01.hostedemail.com: domain of da.gomez@samsung.com designates 210.118.77.11 as permitted sender) smtp.mailfrom=da.gomez@samsung.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715752662; a=rsa-sha256; cv=none; b=sOUAdH8YxFE0TMYKhY5xjHt0oZScp4dMPJ2pnPMFDwSDpnSiFlFYrLzTuMQNvu1YNMtP4r v2IS/oXzxU2QQ/cKssyEYax4VnWwaJ7+5ZJ7awq4yGQwO4+jcsNdvDdIBEDKYQyr/9w+u1 nV1Ttv7c/7+IxQYq46+YIqeF1UUF6Xw= Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20240515055741euoutp01e0140bc2a10abd443dfc4e8de0f0eeb5~Pk-RzVCif1508115081euoutp01O for ; Wed, 15 May 2024 05:57:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20240515055741euoutp01e0140bc2a10abd443dfc4e8de0f0eeb5~Pk-RzVCif1508115081euoutp01O DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1715752661; bh=SA7vsxMb4nPPaawAJ345Tbdd6TJyw9vmKosmLEJCqb4=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=SONL4RGQ+3aW3BkT0h8/QH6hsaAX39vkj1HmfbOJsXx4+FG0cBs7IWKZDx2H4DvD9 D+BoEZCaxUDJODjN/sJEd/u6Scd+J7FkcMoGZKyY2WYTjlU9aFRg3TraxXWA70yTqz c+bkaCxk3K/DrjoJGBNQ+uai9RzZsu82Mle5zQQs= Received: from eusmges1new.samsung.com (unknown [203.254.199.242]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20240515055740eucas1p25652a13d9ff1daf6170721db7d20973d~Pk-RhccWA1242112421eucas1p2K; Wed, 15 May 2024 05:57:40 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1new.samsung.com (EUCPMTA) with SMTP id 62.CC.09624.4DE44466; Wed, 15 May 2024 06:57:40 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20240515055740eucas1p1bf112e73a7009a0f9b2bbf09c989a51b~Pk-RFZpih2872228722eucas1p1q; Wed, 15 May 2024 05:57:40 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20240515055740eusmtrp1f762462ac90672e4ece458eafc17f138~Pk-RDcVyU0411404114eusmtrp1E; Wed, 15 May 2024 05:57:40 +0000 (GMT) X-AuditID: cbfec7f2-bfbff70000002598-6c-66444ed47f04 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id EF.F1.09010.4DE44466; Wed, 15 May 2024 06:57:40 +0100 (BST) Received: from CAMSVWEXC01.scsc.local (unknown [106.1.227.71]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20240515055740eusmtip1c1c5c7fdc67df03cf35207303023806f~Pk-Q1q2AV0512205122eusmtip1d; Wed, 15 May 2024 05:57:40 +0000 (GMT) Received: from CAMSVWEXC02.scsc.local (2002:6a01:e348::6a01:e348) by CAMSVWEXC01.scsc.local (2002:6a01:e347::6a01:e347) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 15 May 2024 06:57:39 +0100 Received: from CAMSVWEXC02.scsc.local ([::1]) by CAMSVWEXC02.scsc.local ([fe80::3c08:6c51:fa0a:6384%13]) with mapi id 15.00.1497.012; Wed, 15 May 2024 06:57:39 +0100 From: Daniel Gomez To: "hughd@google.com" , "akpm@linux-foundation.org" , "willy@infradead.org" , "jack@suse.cz" , "mcgrof@kernel.org" CC: "linux-mm@kvack.org" , "linux-xfs@vger.kernel.org" , "djwong@kernel.org" , "Pankaj Raghav" , "dagmcr@gmail.com" , "yosryahmed@google.com" , "baolin.wang@linux.alibaba.com" , "ritesh.list@gmail.com" , "lsf-pc@lists.linux-foundation.org" , "david@redhat.com" , "chandan.babu@oracle.com" , "linux-kernel@vger.kernel.org" , "brauner@kernel.org" , Daniel Gomez Subject: [PATCH 12/12] shmem: add large folio support to the write and fallocate paths Thread-Topic: [PATCH 12/12] shmem: add large folio support to the write and fallocate paths Thread-Index: AQHapozKay8vuuiPlUac1QtltK7AQQ== Date: Wed, 15 May 2024 05:57:38 +0000 Message-ID: <20240515055719.32577-13-da.gomez@samsung.com> In-Reply-To: <20240515055719.32577-1-da.gomez@samsung.com> Accept-Language: en-US, en-GB Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [106.210.248.161] MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA02Se0xTZxjG851zenporB5KtV/qxIBhQ9Ai0cUPZpRkGI9x2WayZRMv0K0H RKFAD53oNMGRzdkwws2pBSayMC4SScq9Qo2AKBQwchNELi6tl3YBKZJVIFTawxb++z3v877f 877JR+GSWYGcilenshq1MsGfFBH1He96tw98Hhm748pwMCqs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tkb DZfkvzubtKTnS4ffxyNfPvqGrpj3WrHl6L7JNifXc9SdcT7zRTgkW8n5TnioxpaVtfcWTs9f +Z5xz9ZZU6xf5v31ObTrrpzcuocMQv/3rorfHbd4maFL228+jQC+F099nFZ01hZqLfNf9qLQ s7fpZJxNTcHaONfnj2/9VmIpzkg01GIuKk4EAM2Wut0BBAAA X-CMS-MailID: 20240515055740eucas1p1bf112e73a7009a0f9b2bbf09c989a51b X-Msg-Generator: CA X-RootMTR: 20240515055740eucas1p1bf112e73a7009a0f9b2bbf09c989a51b X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20240515055740eucas1p1bf112e73a7009a0f9b2bbf09c989a51b References: <20240515055719.32577-1-da.gomez@samsung.com> X-Rspamd-Queue-Id: 7096D40007 X-Stat-Signature: fwxcimbbsmjron9cj94yyxdxqhnf6zke X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1715752662-550447 X-HE-Meta: U2FsdGVkX1+GEeLGakuxXm03DM8PNZwmuu3PXPf8TZLPymKyHSOj18ONyuUwCh8cRygACL0L+RR7D0h7/99O00UPkkpLhHJ1TqxF47Y8PSbK//P+y2CGJ/am0N4AdMukWBXUv0KkYxl1KofASUbTQsBu4RCrj9Cfwy4wIAsMDIc7Hcy7A5Oglz4GxCQXMcDEWZ929MNVAANWEXHa+9Yu2FWcVxkrt0oqHuRYUhDCAigT5f5QYYdMTrRSknRCsrG1zouEU5wVVDZxua5p0RVVWarB5FGdeqjPDD0AJklI7NxouVbEb1JdXwXjjT/6C7VDtzt1oHwiOt4UQDWRCo4bNMU2zpZw2fYTA8BR7HseADji2Th8tg02uKQNV0gfpJQmIPZraUlNBynhBlzsdDMbTGspRXiNcRsM/wQ0FZPsjKyKz3BZTmxF0wse/bqSu74V+CIZCWODIn2E/GdpzIdzDqE7bwdFqQs9vTfSBawq8v/vts0SyLPDTNb7J0b5ofv20ZvmdE2IHIgIoqC0ljPFUNFwL4A8I2tKRB/yd2dJ3hAPo/D7tjh8ZjV1AtBky3cEZNkD3U8xoKy8jl1Ao1xObVX2/to7+ONSOgIh8X/HUUSKs2zIdg8y/0MndwCWIMVPxCgoIbnVjhVs3ANZLk9j0U6PfDq7ddpeFRMe6WEKoWio433rGJmeDvAqIoPsI5zsiQiQidAnalJEQkGJ1JVBrKt25xUqaN5xi1i2AQhNbnzN4d4Q/uM6jKdOYQN+/EezyMdWz4G9RU/URjP96E7uw9TOArDxqER4EhinXDljbOm+SYSPNvE+hR4ww0+WOUdfl3GZiLrTrtvMt3uoDX4FO8qqBc5XygwAnOlmw9YJ595UdLfYXjQwNb/NyOe4wkY1b8mQVPford5hZMlaMyaZpKM7Mr5gQmU8korVG5bcpBJSkqLJyXaow46it+FA4V1/usC6BlFDf4X5pqW8Qqm S2Vl6D2p v0iYNX6xOyTJJFG71hdShD6RnpZux5sJB3dQ/gynG+pXVrUghP4frLntIYfHLB6t4u6Xd7K9GPICsDnq2PYn1rA+2RNS55K8SUYrd/9+UH59XeKDJVgCn3Y3IIeOCJYjIJyFjLrijgHls/qAshHt/2hyYtvoRVH77LLFojUo3SVgUl+DkJqbhFKVvw6TBhJUdayPme6eP46xPfSLGWNPIUoxwVL6tv1p7iuS1fgKYe38Q3UM= 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: 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. Skip high order folio allocation loop when reclaim path returns with no space left (ENOSPC). Add __GFP_COMP flag for high order folios allocation path to fix a memory leak. Signed-off-by: Daniel Gomez --- mm/shmem.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index fcd2c9befe19..9308a334a940 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1836,23 +1836,63 @@ static struct folio *shmem_alloc_folio(gfp_t gfp, struct shmem_inode_info *info, struct page *page; mpol = shmem_get_pgoff_policy(info, index, order, &ilx); - page = alloc_pages_mpol(gfp, order, mpol, ilx, numa_node_id()); + page = alloc_pages_mpol(gfp | __GFP_COMP, order, mpol, ilx, + numa_node_id()); mpol_cond_put(mpol); return page_rmappable_folio(page); } +/** + * 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; +} + static struct folio *shmem_alloc_and_add_folio(gfp_t gfp, struct inode *inode, pgoff_t index, struct mm_struct *fault_mm, bool huge, size_t len) { struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); - unsigned int order = 0; + unsigned int order = shmem_mapping_size_order(mapping, index, len, + SHMEM_SB(inode->i_sb)); struct folio *folio; long pages; int error; +neworder: if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) huge = false; @@ -1937,6 +1977,11 @@ static struct folio *shmem_alloc_and_add_folio(gfp_t gfp, unlock: folio_unlock(folio); folio_put(folio); + if ((error != -ENOSPC) && (order > 0)) { + if (--order == 1) + order = 0; + goto neworder; + } return ERR_PTR(error); }