From patchwork Thu Nov 14 12:15:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13875058 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 CFECED65C7E for ; Thu, 14 Nov 2024 12:22:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E8BF6B0085; Thu, 14 Nov 2024 07:22:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 471176B0088; Thu, 14 Nov 2024 07:22:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29AD36B0089; Thu, 14 Nov 2024 07:22:46 -0500 (EST) 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 0EBB66B0085 for ; Thu, 14 Nov 2024 07:22:46 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 90E4A1C6940 for ; Thu, 14 Nov 2024 12:22:45 +0000 (UTC) X-FDA: 82784612946.30.EB9345D Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf20.hostedemail.com (Postfix) with ESMTP id D44811C000C for ; Thu, 14 Nov 2024 12:21:47 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.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=1731586900; a=rsa-sha256; cv=none; b=DyerxaSceC8P9d+5ooZld+RrFCqF5IhOjUh85s1F9y6twqF8UEWEia0eJarA2dbqNjGVWl sAPc4IJJn9RW9UfSbRj158SSVWw5ANM9AzzF1Ux9EbZxkDUYXLnV9ttlsvXLwLApybGZOr 5ky18tcD1jqz2USf0v1+6FxKmxtKvm0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.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=1731586900; 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=NTawWnndFfTWOUnQwGiomK+iGs/jev6MczYVUwnpxtXjhyfk3iF5jwU6gl7Fu2x4IPkqsn J153g6c6M976HX9nQIb4MXtjRHFL23sveCjOTK3X51sRlGyw2dx9rYEL6mt06K4UUxtZp+ Kyk07NzvcNZ1p9o/0UzaR01War2UD+4= Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Xpzk46HcFzDrDS; Thu, 14 Nov 2024 20:19:56 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 7F9541800F2; Thu, 14 Nov 2024 20:22:37 +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; Thu, 14 Nov 2024 20:22:37 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM Subject: [PATCH net-next v1 01/10] mm: page_frag: some minor refactoring before adding new API Date: Thu, 14 Nov 2024 20:15:56 +0800 Message-ID: <20241114121606.3434517-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241114121606.3434517-1-linyunsheng@huawei.com> References: <20241114121606.3434517-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Queue-Id: D44811C000C X-Rspamd-Server: rspam11 X-Stat-Signature: osgnigj1wntojntbndnauzioqqxsuthw X-HE-Tag: 1731586907-787288 X-HE-Meta: U2FsdGVkX1//Kaf24PzFsfKCWCR3kOl5GPWv0By8iY0nGAz1vkxrbhbC33BfVZ4AHr2TuK+FI81NQ7dImlBaxm/EGjaIG3AUcaB8x+/R2VYwAjwf7zqczLLJqnXFB8TLOewqx8Kx7ZAn0MHTooeUq+8ZUy2wItNDdgCaLUXRt8nwFXQW/9s0efFQopFHQKWLnNfJOaLeG0Qaxb0h4DGrKDK3kRAOTzWQdNoD3ef+43HXX3UBn7+aBdamXDCX6oN5ZuwG73PG+GWb2QmgFBrPrmT1kdBzYC+LAaUPelTo/1Vrhkz8LbwYgXfTxO4iJTG6Q1WMVP12PZLXJsv2pR5YCsjOtZy59TieQcGnrN/IbvdO43HNlPm3nh6tz/6kOPID5uYFB1Vf5Od56DpZS9AzGVWASj9Zj0WSb7BXpHvnoOUy5mF9iwh9vrgM5kxTlxnuSYBlQ7W5rbMjC2z7RXwKkYyhqwaTg9wxSZswz4G6pdU6fiEkT0+Lb9/MygwTjUPwd54pkHeUo/eSEGpe2Bd6GNyYcOUcsvp1AxAkAo0S+IonZ2muab7bLJ0VKnGyc/rc+Roeapyz5E8NufSp0WYBfUz6gr38tglHef3rFcWxud3ZYecDupHch727pEgdZV7JGiIVf0nomjT34Z8uBVL9732wj5x6oe0BP+NHhqbk6zpFmV0mGyFfQlHoIyzfPZac5dAhwW8puj73/JhxalNNGdTNc9j7upwy2pFq5I0SxxlHDxKTDkXWx4lqy1Tn3g715a2KpiK2+orB88vUTZuJRR6dAoQcAaQgPDEpIjsNHFiX5VvC6ktXZ6SvoWIwJ9Bl0RdUysArcrbc286AiTTB0qx6Uy7jCbk1ZRh/x7J+ZQE7lcn4091Ez+svY+sPbtNNVt+TnLGTui/uGWWE4GG+0HBHg2+59vlGV2ZXDyMtjKkAO4nf2JZD3b3xrEXf4+R4g6mxsbtamX/gOQEGbac LLpPLWej reGwk7VXUgneE+2aH36h0mgfhQxJ5kO/vhENHIJ/0B/0B+rgkpcm3MNFctOJ4hgttBFiiEfo56glxy8nv05Gfne31qskepbZYc1brY7WWqKFUngoRW8coJWsdklByLqrq45WycZye2gis79CUNJYqovuX/yYrGpFsqa357d9WPPLwg/j9yzauTI1W9WxT8MtICcy0s9KwwCvoqWKCCVKyiOWZTKhy/dcXPmjvgOC+EK023QjM2gGEiYBmZlHz3XHO1nD0VWZdJVbSCR92q8Ym5ryq6SWmkKFFUlavFrIH3AMvPY9TiXHU3/LQN/pgS1PILOkj26O7FJBGPQlu/S/DKYAbuD6PJAYj5zv39jcymuTCNqQmmsbzcInJmwniHsEIjm4CCVOCWaoDGiN9kBWInrHhcQ== 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.