From patchwork Fri Oct 18 10:53:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13841627 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 14988D2FFF3 for ; Fri, 18 Oct 2024 11:00:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E9C86B0093; Fri, 18 Oct 2024 07:00:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79A726B0095; Fri, 18 Oct 2024 07:00:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52C316B0096; Fri, 18 Oct 2024 07:00:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1E40D6B0093 for ; Fri, 18 Oct 2024 07:00:29 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 613FA1C75E6 for ; Fri, 18 Oct 2024 11:00:15 +0000 (UTC) X-FDA: 82686428832.01.226406E Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf13.hostedemail.com (Postfix) with ESMTP id BFD8A20025 for ; Fri, 18 Oct 2024 11:00:14 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf13.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 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=1729249065; 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=TvP9ARPISz/1rjJ8l+srYabVCOX0/3M72/Wlt4HjuSMFvF4snxo2JMRRWnar1FVVwO0/dd z4oc6wFyzC7pOOwffyMBR7IROS+z5oUJcceF81DVuhMs488NF6xEiQ5WtO+ztkpiTo/tIW jdULaVGndnzj+Ko3B0R6q/KkNK3EjDs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729249065; a=rsa-sha256; cv=none; b=o7G3gKBQwMcTW/2rdjLL4efi2hl1XNl9xgg7dHlElbY+36AUbLH//NLZp5oov7i5rPsBCy yGH8WCw8T254MAAgan7Dc5Ql4xsiS7dN+Okry8sGm/qb/aBrwb8ttKcWuSX1BSvyL409QS FtooBKWfG9VWAy5Pl51GMygM1HaxXRw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf13.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4XVMCF73jbz2DdtP; Fri, 18 Oct 2024 18:59:05 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id C48D11402E2; Fri, 18 Oct 2024 19:00:22 +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; Fri, 18 Oct 2024 19:00:22 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v22 07/14] mm: page_frag: some minor refactoring before adding new API Date: Fri, 18 Oct 2024 18:53:44 +0800 Message-ID: <20241018105351.1960345-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241018105351.1960345-1-linyunsheng@huawei.com> References: <20241018105351.1960345-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: BFD8A20025 X-Stat-Signature: tffsscr4n897phoh4u53f6tgyokwhpnq X-Rspam-User: X-HE-Tag: 1729249214-636853 X-HE-Meta: U2FsdGVkX18rPcZaPdthlNdqSfzWiggilgyiYXkRt+6scHmyH41uhu08tc8eT/Q/g0GWm7aJ3dwyVQfVcSCTtUXwk1p1fmrhVnFrOWmrK+g7lMulGT7L7U4qFb4kW9VD5OeUyv65CjrmdNcDQu0gZCvN5rDyH2Zmx+CaUwySKdSoEwDgTJyx5fVx4YrrLXZjKn3/aNc9LFvJaC4P7ZCrKfXmD/1SvVZc5gTtdV2RfqfSCaVHlBenyVlDRauXxWJ2vHXmYONXaj7KB5GQ9hTwFTViT+Tf+h6A9SfAyTktq2DlBfo4+i4E0w3XpIq66QptQNwOAGqgCguqj7/4B0kWV0S/dy0k+CJjOgilcaIOR9XXDb8y3gEjspZkirqoGzq6HVMafPZH/aV59ueDR+oO0HAILo+pcRa//rarVnJXbzYxZPXxdRHjC0qfenJQcJjsiia1zg5icVLGLG0w545VAx/x/GWyOYSStXMFf/i+pCia0qoYnf29IugHxHb7z29dSjLICW6E0IaBcZ84bhx7oMuCAyAaFLLbb2KWbzGLGZF4hBoPoZ0vvG1AeeYS+q7KEo4SyLU6Fp8v29rveXLWx4gjNEfO4WIqXcQg5f4BSSZvv7hBKKtxTAOWCv4hTuVLWF7ejDpPShXeA32ot98zcXp8N0reAeTH7iyepC3d7sL7XKQRaJnyLNQURBlyc41HkBwBgmopdUUOkJHg/+h2LwTHCxycWVpQa4LjP4SDDP2ipX2/CMu58+ledibKLNjeBHUAQO6lV63XOBOI3v4qDhJjllfJCFcDHVmBEHkLAwebaLJ69ZOxebJaYqjpsNPO+WISWkvsdFm3ETB3GgVdnD/BXNXGvybJ9X5ZH87LeltGbRaIK0p5l4rbAY2htBuIvPM3TFhSasqpmZtDjUoRFwbRqgRkU9RF0qKzVcet7OErzOhOOd2nk47tgR1kF+GY4q0RMGcsvtm8K+V+Uiq XMYFRMJl BV68WmMqY3umWaL5vbqwnbUyx4eCpieig6laft3BRoXTxFa8ZjoDbeBG6wOC2U/v64DFamip+4PgKD4s61VQBY5pdUtuMLcTDD7+52auitEmZz3LxJEZD1rQecuxnIeXZC5XuA8x7VWvSPKuPISknkWtC9ZToLI4tw1ZiINVBU3rHGo5hmsCqCP9uImPptCZCO1TzzLnTa8rK6UFPvnLGgFS793etNrNZMjDRFpJJKa27r20hCoz6HmV3GWUUjAwuOiHj+/BU+N73zEHU86YGeI7B7joP13ZaXKdByATEDEvHyX3bi96486pxDRQbe+Jne5NzLbrCe9mSyG3UPhTowKySnTkYCn+TXWrCbh+5SqpcqxXMgMrj7hTS0MPHuo2Uk/he8jT8sCeoavxk81TEzw8PKGzz92Jx/eInPbPn+8Wf1QllS9dpPYeSMzalIqgQzpIZ 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.