From patchwork Sat Oct 12 11:23:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13833397 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 361B6CF2566 for ; Sat, 12 Oct 2024 11:29:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE3176B0093; Sat, 12 Oct 2024 07:29:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC2D36B0095; Sat, 12 Oct 2024 07:29:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 934966B0096; Sat, 12 Oct 2024 07:29:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6C7E66B0093 for ; Sat, 12 Oct 2024 07:29:54 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8D3131A162F for ; Sat, 12 Oct 2024 11:29:44 +0000 (UTC) X-FDA: 82664730624.11.F67C0B6 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf09.hostedemail.com (Postfix) with ESMTP id D71B314001A for ; Sat, 12 Oct 2024 11:29:48 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728732489; a=rsa-sha256; cv=none; b=MOBAp2JZRA2zW5pZqPu/pM7kV5Holf8CoZmCAe01Cs+cdLDDQAENeZtfn6cbh8TfAWaxFs Q2HTiire13FnfYG7u+KIbPTrcXxrSoJeiafr9MBIihA3d5urQtld+M9liX2MvuoAT1QwO8 TZaR8OYJ+02Ao58t7SB7sNNCVzgRFOA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728732489; 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; bh=Niu+mzOHXZyTyKqZ4DWZXjUhv2MwboY9bqDPAygTHsM=; b=BsO8Hh8DXX6FP2llBSWCpYkEXfkoX6RzYAQ3ihgk5BXnZ7UfzNqmmActzH1y3N84/gy6o2 zFmklZzCWWqXblrEh1IbebBAaXL9/9Y3Y0SPvtpHEGMguEIQaNqriVMfTGp0WAGznWQxWz 0hYFbJSmOeiRLu4XQknbcC6rTPM5LJg= Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4XQh786Rw1zpWd8; Sat, 12 Oct 2024 19:27:48 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 1AD061402CB; Sat, 12 Oct 2024 19:29:48 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Sat, 12 Oct 2024 19:29:47 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v21 07/14] mm: page_frag: some minor refactoring before adding new API Date: Sat, 12 Oct 2024 19:23:13 +0800 Message-ID: <20241012112320.2503906-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241012112320.2503906-1-linyunsheng@huawei.com> References: <20241012112320.2503906-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: D71B314001A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: nc1eahwe3dutw8hwfgns7onurqbcu3dn X-HE-Tag: 1728732588-302169 X-HE-Meta: U2FsdGVkX1+A7fZD+MJ1s0vsjLcAZWyUAFz3wk6iHrK5JZMYtMoMLf8b/YIFNYaQ6q75Rjl/rX6Q4MAIPYLSZWB/7hUOgLUOrQO0l2u3To+8DJbqEMfUwZaFWSMg6JDD/gqv033Oke4P61neTSwJY58BnM+TekLEdGkf3ku80JgTiNXp7/lW8PgjTzIC7OIuq5eraMMq0qP3glE7Rw3hE4WKk7Fh8ZvNAANjQXSR5w7f25OgNKIwRBWfIyN4ZVho00VKpmTvrjLSahuOEo1UTDvev5cnegiitt9ZBpxmdRN+GSEb1ZtHhWvhN1IA/SC3s5R6NwbKC7DduV6PbZ0+9kGNxqZCNKydLQkxSutYD1ZrJ67pYlz6F2u1nWTHbJTiW19GgwfPPcQxo7QZhYe2xG9PUJtVJcz1TKS/Iq1txDBHHXs8ymntHhZ/X2Sci00Df+EDbE4DO0CAt4YfdCcf/zrdxKdGAMPkJOJ60/63TDw+vZ63Pum8wfls308SfjfECysXVq27s3RF7y9YfO5LzIJWcc+EGRAy0n/RU7K3Y2F2DkwLVz+8je7iSbMHNjTU4BuaR86kuKboStw1bpbdwsexE2G9gL4251GDiEQPtNMoD72jN1nORCVCbfVWoIgaRJWB85nFW511A3DOwrd6RsdP2cQ+CxW+sr3Uibmk5YIoo1FTEG/aDY9NOm8l5AqqeZiJ8mN0Jp/xAx+ArZJ2LnNUsprhLxrR0CxpB370t2FVtOXujTx7l1xa2b92hg81SQdApLesRrpQ5tg9NWgRFpA5COUtSvvcH8GnUCd4zgD4i62aQ2hcb/xz5cIO7dSimUTsny3Wn9oOkNxgudm31tUA70MvkWhgCy5EzNNfR+r6cMoGzZz0FLxRQK3657ejCEhlHxaKkmbU5fntim0/+Jh5pS5Yaw0jeeJU9IpfQ4ErJrfyej/yulkXKzwfiMXZzN2AMMjptax4c5NoPkk rUDUNT3/ YsL+p4QeR2cW2Rh145fZi+vud4Rhiy/4JfpGqeZfuyo+UeqQ2nEibSmC5RA3CMG11W3BMVCpEhTkenQLujEgowzpCBWiL/twRMVJqvB2yf+k2oDZbMNJFrqDjIlDS/uxs+DGrTkYqXE6RbKJBkA4J2ZpwTCBNkjT6kZevINEDqJAqabLoycY4J2TXyHgapFmVk/dOUeQNGSqUEuMw3WnCKPNXoFwESTWIkZFBG4Bcuv5cOaRVx/Go1M6qHl0p0oIwo6urq2h/ezf2ycZoJlYS/ezMlurh4yTT0PNyhFZ26PMvsiH/6DsDRylLkK3pC6TTgchWdgrS/Uuy978pr9IcdI7lL8aHg5bUEcfmZxnqKCYMjd2hgJKrGruLARvMQHP5SZ4VCd3T4BgZQ+XvXQJVDu8hfQUVXEADwiEgiW2u5syHHwTqcQxqayKrzTcMw+JULzIX 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: Refactor common codes from __page_frag_alloc_va_align() to __page_frag_cache_prepare() and __page_frag_cache_commit(), so that the new API can make use of them. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 36 +++++++++++++++++++++++++++-- mm/page_frag_cache.c | 40 ++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 41a91df82631..feed99d0cddb 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -39,8 +40,39 @@ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); -void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask, unsigned int align_mask); +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask); +unsigned int __page_frag_cache_commit_noref(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz); + +static inline unsigned int __page_frag_cache_commit(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz) +{ + VM_BUG_ON(!nc->pagecnt_bias); + nc->pagecnt_bias--; + + return __page_frag_cache_commit_noref(nc, pfrag, used_sz); +} + +static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask) +{ + struct page_frag page_frag; + void *va; + + va = __page_frag_cache_prepare(nc, fragsz, &page_frag, gfp_mask, + align_mask); + if (unlikely(!va)) + return NULL; + + __page_frag_cache_commit(nc, &page_frag, fragsz); + + return va; +} static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index a36fd09bf275..a852523bc8ca 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -90,9 +90,31 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); -void *__page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask) +unsigned int __page_frag_cache_commit_noref(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz) +{ + unsigned int orig_offset; + + VM_BUG_ON(used_sz > pfrag->size); + VM_BUG_ON(pfrag->page != encoded_page_decode_page(nc->encoded_page)); + VM_BUG_ON(pfrag->offset + pfrag->size > + (PAGE_SIZE << encoded_page_decode_order(nc->encoded_page))); + + /* pfrag->offset might be bigger than the nc->offset due to alignment */ + VM_BUG_ON(nc->offset > pfrag->offset); + + orig_offset = nc->offset; + nc->offset = pfrag->offset + used_sz; + + /* Return true size back to caller considering the offset alignment */ + return nc->offset - orig_offset; +} +EXPORT_SYMBOL(__page_frag_cache_commit_noref); + +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask) { unsigned long encoded_page = nc->encoded_page; unsigned int size, offset; @@ -114,6 +136,8 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; nc->offset = 0; + } else { + page = encoded_page_decode_page(encoded_page); } size = PAGE_SIZE << encoded_page_decode_order(encoded_page); @@ -132,8 +156,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return NULL; } - page = encoded_page_decode_page(encoded_page); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; @@ -148,15 +170,17 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset = 0; offset = 0; } - nc->pagecnt_bias--; - nc->offset = offset + fragsz; + pfrag->page = page; + pfrag->offset = offset; + pfrag->size = size - offset; return encoded_page_decode_virt(encoded_page) + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_cache_prepare); /* * Frees a page fragment allocated out of either a compound or order 0 page.