From patchwork Fri Dec 6 12:25:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13897121 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 737BDE77179 for ; Fri, 6 Dec 2024 12:32:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 005766B0261; Fri, 6 Dec 2024 07:32:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EF7F46B0269; Fri, 6 Dec 2024 07:32:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D53316B026A; Fri, 6 Dec 2024 07:32:36 -0500 (EST) 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 B40EA6B0261 for ; Fri, 6 Dec 2024 07:32:36 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 05C0D1C86AA for ; Fri, 6 Dec 2024 12:32:27 +0000 (UTC) X-FDA: 82864471704.25.0AB1A3B Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf12.hostedemail.com (Postfix) with ESMTP id 306F34001C for ; Fri, 6 Dec 2024 12:32:17 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf12.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 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=1733488338; 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=3xiqcNUxqGifLj5ro6FSlzRyYoA7Pm6KCQOaDJzaKxk=; b=cadUgWk6UD0iKuaqEirVT7Rj7Dp9mAdpL+sadhRTbMK6bEGqqC+je50cne7IboNp27Ndgo PQ3RNdEk99zJl4McbbtO0FbSHJA66B+xC12gmpM4M7i2qwfKry39cz+G69szKBKZrRHa+P aY1ROvPKnJBLxt3elgcJxRUyAbua/hE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf12.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733488338; a=rsa-sha256; cv=none; b=hiJkzJjeNExrw2bm2bginH3XVW7QYJi1yx8vhSKHXhtbJcfXroE2VxT4QKtzncQ5Xvtv2k cLlbT14T0Igbi8aGL+hLuir4ylM5tXVE/jS169bbeJvBBMB/8oc8wFtSDAKWeNVEuGYlJp n/UDh9JMejOGPFF87d7qwxtf7u7DAw4= Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Y4VvZ743kz1kvGW; Fri, 6 Dec 2024 20:30:02 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 507741401DC; Fri, 6 Dec 2024 20:32: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, 6 Dec 2024 20:32:22 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM Subject: [PATCH net-next v2 01/10] mm: page_frag: some minor refactoring before adding new API Date: Fri, 6 Dec 2024 20:25:24 +0800 Message-ID: <20241206122533.3589947-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241206122533.3589947-1-linyunsheng@huawei.com> References: <20241206122533.3589947-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: 306F34001C X-Stat-Signature: 6y7uawpf1685eog8b19ibjh7iircy6xj X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1733488337-181340 X-HE-Meta: U2FsdGVkX18+32zoLHwA/XIjoaM0WnAWCQPT3fRpPkM2/iPjCakyB7/rhK+g1vAD3pcDH+W86nbOleGTCk8+2H5Xmml2VDd3nJ48F7NcZ2ShhkdIwIg3B888E76bQztZqi/dqX1w1YpZG2hushClP6C5ne9P9b/QldZAv7rykO4liwa+if72O/MTVnRp0Ltw5JnGxuA0ZEr0kI7l3GFT0OOWTEMjIl/5WbSAsHSyb7Au34NNyYx5PIL1//PlJ9i/Pgrjm54armw0prtUIM+OvG5pDzOcs2Fp4RkfSOqh9SzQ5bGGIKmTYpHJHCL83JdjsOCpYP8vXcr2y3nIBtAVytCW+KsJI+dRpQN3wxv+56vd0SylfE90SkyaZoITgH5MDANcuEprvgTQ4RiezXF4aAKBxUAXbOmgwUyOm3VPGZo0UbJBiZiCx8PbKgjxEpUq0DUfxklTS+oExNUswxWvne7iDgTm6Wn/SXxjK1Y60FGFS9mbnGn4WW7iGabpx1a2yvte3G5ek/wYYwvBfQtCTQAqGpriNzC4fG6dSE/WsBcYfsQ9gRsxmvvnlIk+EU2ZCGgqQTVj+3eD57I74XyD3kyBvaMufLYP+/+Qi1LlzfXWulKtLEG0dKChyikj8ZC4GxZwLqdB9op7Bw/bZdpm2t4vW+9URjtMhE9Aa0rU/LsUdThOCyT+o0F4HrO2A3wws96cVXwXgOg28PwXUW0RTBF9RzZ4AWJOzIEQE/wnD8My4X7mv5HqfNn/yMvu8ryBBjwDIr5ymiYsCHKKJsFXtpj9yR3Mvv6KBBoof+9DcHEdAElSgtOij3nOwMg0maO5n8GWHE7TPZJNhKzzxgYuB77STg6FtHEOPPMWAJxBG4g7ZzrXHJ6/i3HbLaiMGveVvLl+rrFNj0CW9wkSdZoIb4vLj2+1ZkDPo4TO0YiWZpRaMgI9n1QWRqvqonLSpnCBHUWMZkoetOzlMBFdTqH btvESa6d zmTP6lMt7NoVCoRoe7bYQQ8o7UpANfWrh4H6c/ryDLcuvHuXrntJyPWn4p/hfPPl0ZEUHlR6Y6LE+Lh4IOZIVPZljvftJN/s4VjHks8+YDihgCQnH8qAE7MERypmrsiwuXDF5E5f8Aj/eVUZNRx/8bUVr9clgrwXxf2sfoE0ri0m7WRIONBqZs35TXagmjNV8b3fjFuef788RZ4RoMalyWlLkvOnou5QPRt8w9xg36cZ2aJJY3ZgplM9UvJzw9wUbArrHjq12mqw3N59Uve8p2Je87odC4Yq7L5YxGxVlDhzrb5VM+pc3rkdp9qAwSNrfwcX6/hNiPxzsTtwJNztaKb0lhpliIUTC2Tn+eyX0K3qiM+5bnylrkBjBbEmjorNz3Szl0/uJbnc2mCzpJFNMCAWvhA== 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 CC: Andrew Morton CC: Linux-MM Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 34 ++++++++++++++++++++++++++-- mm/page_frag_cache.c | 40 ++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 41a91df82631..5ae97f93a0a1 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,37 @@ 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 (likely(va)) + __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 3f7a203d35c6..f55d34cf7d43 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.