From patchwork Tue Oct 8 11:20:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13826287 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 A423BCEF152 for ; Tue, 8 Oct 2024 11:27:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 975716B0096; Tue, 8 Oct 2024 07:27:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D6BB6B0099; Tue, 8 Oct 2024 07:27:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79FFE6B009A; Tue, 8 Oct 2024 07:27:17 -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 4C7316B0096 for ; Tue, 8 Oct 2024 07:27:17 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DB785A1BB4 for ; Tue, 8 Oct 2024 11:27:14 +0000 (UTC) X-FDA: 82650208914.22.F599572 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf01.hostedemail.com (Postfix) with ESMTP id 8FE9740013 for ; Tue, 8 Oct 2024 11:27:14 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 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=1728386701; 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=uTAfBw0JOsAgFu+1MYVD4eHI4b1Z4Ik4BXo0D+lUKKM=; b=6m3PwGZlvV53RGNk9Fhp/CTuB65VveqYySe4I/S/BUm4DXgzynKE+Wxbz4MkJ/RxrrQeLT WAhPFkjjz/aiqJN4Fl+4RW+Fg1KY2SdOQYynK38fccEDi9fPKvLKo6yiBXwagKLLz2XaZ8 aopXvFC2939RSu5XC0ZncHXTFz/MQBc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728386701; a=rsa-sha256; cv=none; b=We8wrishnlKGgTM4E0oGWbfF3mI3w1fNd3Vi668+vwu/QBC5rakXlMD4/RcXAZIJCe+ARz mpZ2HtdnL6e86RU4zeP3u2ssDlN7YZR12gJH5NVNqB+LG059ebJ/h/f/rWcQ/KHdXf8zQK nMVY7ZmNCEnaS9e9v28Cpo9O5SWhCPA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4XNDGr3VFCzySww; Tue, 8 Oct 2024 19:25:56 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id B86AC140202; Tue, 8 Oct 2024 19:27:11 +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; Tue, 8 Oct 2024 19:27:11 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v20 07/14] mm: page_frag: some minor refactoring before adding new API Date: Tue, 8 Oct 2024 19:20:41 +0800 Message-ID: <20241008112049.2279307-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241008112049.2279307-1-linyunsheng@huawei.com> References: <20241008112049.2279307-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: 8FE9740013 X-Stat-Signature: 9cn55pmjmutewxam1i5y7hw4ynos5hat X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728386834-881070 X-HE-Meta: U2FsdGVkX1/i0g61HKWKIr/GPTHpkauHXQF69MwoFcyDbXZi+5Vs1TlvwqzECgmyd5cW8cQ1mqCCEkWH+TzmbLXrvteWw25uJnhGbUJ4N3aeg9SG2/lz18JIQTxo11mycJEDLFoW//lQUp8BTfPDJVUZJjQRJS8WFLYe7bbIO3Roxzt3Pz7Uk22nDv70zd0b1iAhANpVRLEn8y96Bv6k25NZyA7pwaxCuvOf3o5xGeYu1/UPL9Qosl6y0nDTbwfxaGkavLymOtIy6Z7gGROabuP5ZS3WB68LB/rnVP/DQiXPJqt+WpuUpcdnIqxtnmdTvWOU4il3lOjfglvuqArwsS9OPL/2z0SFHBfzFOt3gFmRFfZt5d7ldGXQtu/J5tk9PPonWuYVEB54W7ssFwb2eRTEhO2zNf88fwfvu4g4XuJdy3SqM81nQUmE3b95w6ExjbFKxmC/U6g2nXBIlHxJGh2nTDFFZY/oNF9DTsq9xMvG03eh/n9uvM9dEi/Hnwb1z+onlF2EebSmuPCVJ+9BOVmAM+vJshbiTCwChm5jvLCUzF1oMX+e+PRagIMfGI7YhyCx6ZJrEgzRL3+ydPJJOD366jvv5K4q9tBrluMNvcNGjtXCwaufATZKB8WnVphT2SSkYb1jA5kOjWuLkKGGFZEBfPc4Y2qcVPgVbksgCyEPB2by9jOJW05GsS5hYaHCQ0IITyn0il5GOku71eE33IzlPooFHWIVBoV7SLyJW7envh+oOtgxPDTL7Cwg0fL7ewb05ny692P4cPQqo4T5fLrgybTSByWIoOMrQoJur0yDhBJfKLIn+INa4w8WbS0zai4BwPRCEWR5+GDF3HyiIrTJcy+2zSnyA1VKvFSKRzDfcOyxFr9AJZqeWNAqMZqVI3cAW/JvHrEL9+HceOWXtMgabDU8xlfwa8ua3c1jOKx9b/mGDKzIOC9MsC+XQrTDF/O8EEB/ynq6G8EuhIw TdhKSP0g pfNfR6GWZtVl5TmSsvARB2GdZ6RkqDjClf8rVeBZiY+VWIChucusHOVuLwS8WFJ2556KPHG0uo5aQBeXaVGSMA1TrlxBYtCSbctxme5m+QtF7NkjWYYrI5wUQtOO+ZXtIuSXMaTAupWRH7JcopSZgt3DI4PpOtRO+ILiJzs4kjfqc/Hn3eOJqZvCaPPxQ6JgwiCuPrbZZTJHCEEHMSCDJFz2Rh7fIb14kgW95U5zO20ZhKMINFK9fuCs3DlJIxsuEbYpeAL5u8usiQ3vIxXnan4//fxzZXwZahxUC 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 dba2268e451a..a6cb32b1d1ca 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 4bff4de58808..e17f4a530af2 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -95,9 +95,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 != page_frag_encoded_page_ptr(nc->encoded_page)); + VM_BUG_ON(pfrag->offset + pfrag->size > + page_frag_cache_page_size(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; @@ -119,6 +141,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 = page_frag_encoded_page_ptr(encoded_page); } size = page_frag_cache_page_size(encoded_page); @@ -137,8 +161,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return NULL; } - page = page_frag_encoded_page_ptr(encoded_page); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; @@ -153,15 +175,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 page_frag_encoded_page_address(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.