From patchwork Wed Jul 31 12:44:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13748711 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 7C0ABC49EA1 for ; Wed, 31 Jul 2024 12:51:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 920196B0096; Wed, 31 Jul 2024 08:51:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F6BC6B0098; Wed, 31 Jul 2024 08:51:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 795A16B0099; Wed, 31 Jul 2024 08:51:04 -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 5C2F36B0096 for ; Wed, 31 Jul 2024 08:51:04 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CD21C4030A for ; Wed, 31 Jul 2024 12:51:03 +0000 (UTC) X-FDA: 82400032806.06.A7709B2 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf23.hostedemail.com (Postfix) with ESMTP id 65618140012 for ; Wed, 31 Jul 2024 12:51:01 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf23.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722430216; a=rsa-sha256; cv=none; b=A0uNhegGNFSSWhtccHhtrHUUdanSaeY1VtHWJQqcwwMFVxhTyc40P6Ljj+bKzdXx1/hztP Cx5agB43YyhVaJGsT5yPfSVbvYNqeRcWxIjygxTW/l6S0CJFtVhfmFTzTINe5t9ps2V1NA 79eYBrIXEXiDgpbXNKLlqIstBiIlgkY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf23.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 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=1722430216; 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=60pAXD0ZYXW1c2nPlAqP1x/rIedEAPxoWXNVAg0gsp8=; b=dwR2HclIERq0zvNKVV3JicNLmnGa6dYlm5GSGfqbEDFyBM86oS2trCS7lh/eNnGXEgyyCG jj7S7XFPi+1kNMVUwJ4rDPjie70VkFGpWvJwGaztBZCMGoh53CQ47HtPDJ8U/HBWfZYFGw CuqiLMB3ENl50LNRUPNNbUPZwVQ0ea4= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4WYsKq4M81zQnC9; Wed, 31 Jul 2024 20:46:39 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id AAEEE140360; Wed, 31 Jul 2024 20:50:58 +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; Wed, 31 Jul 2024 20:50:58 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v12 08/14] mm: page_frag: some minor refactoring before adding new API Date: Wed, 31 Jul 2024 20:44:58 +0800 Message-ID: <20240731124505.2903877-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240731124505.2903877-1-linyunsheng@huawei.com> References: <20240731124505.2903877-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: rspam12 X-Rspamd-Queue-Id: 65618140012 X-Stat-Signature: dscyfmoz7kicb4xe7tm39rewuny6guor X-Rspam-User: X-HE-Tag: 1722430261-830268 X-HE-Meta: U2FsdGVkX188heXBbkv9Aj9UYIAeydWBFhT44hk0wuMmb10cg3iXH3DJJIhTJEMTF40vrKZe1bHcfYAopxDCDd6oyeoKP1ByIsAzADjuw5AUCKeOr0zJ2iKG/q9T6wHJRFM/v0biQHwzcTxsBVDT/T93GTzOLiKFhG5p4NE5oqIyOeTQESxrFVPN79DU1bpnbJAdEU1fCQnH1RaiH2oo3AGcCFUdWhJE3iKkXN/u5itgB4sRG10h7qraXGkBWKwO64U8fEB2wZo09bZlCsEfQhpRf/SA5YZadYGhA3TIsaeTS2Xs8ADftiJi37+ovqDkCF3hYt544exyVoE5h7zIQ3Ng1pyOLQ+day8KPH/utjsDF57UKw5flEglq6SU8pqxLCJg3KJZTjhmMsje62OAOL9fPWdIFQNkCNBBiazcFFCzliuTc+o4/g9qaTMuGLCxjBLJEiTpckvQOFTkAVLEWdT2w191l/TA+fn4epGxqZjZNdBslhhmg49FJxWCVcFYDvRNN7GiFKe5h06cpFuDw6MBzbe2nbjmyshtvemxhto8Vyh5xJjvRpHRU5pCL4HOW8FgsTAQG/a8wn9LtUR9sCbla3dAQzmO4wOr6oz+1x8l3C1Vb856uWDUs7NfA0KlZDJ37m2/g8tEFAkEMJEzCzQfiRagz3wVD4Uo10F2X/V7nYKAMPqpsNbIvaeIF0olGGQn8Lu4Yu5/cMj2WAAiPX+2Srw0NmuPC/PAhviBQgGN/sGeEsYsPZThDUFdhlhgGyUZi+QlkK5D5/e+C3ev//SVnV6wvp5AnbwpSZI+3oWUUQ9AszV3FdgvgLTPwBgg2A3krlAjzk7YkCVKcMiLBkfBrjoSUF9ZcgniIGb5Lhd8W7OvAqmqLpkk20brF4GDDAJqjWU/FyioAwvweHbOigeupVsrTc38KoMk9oZD4YeBo49xm9ySO9HwcT3QzFehk+5Z642soTzqF7aJdNS 0JwS3pQk M2YW3J8n3TQWYN/uPrERNuwU8oUpEs1prQVxOG0uUz8ODkqR6PR2jEWduodvXs9l0qK/xsoNFnLlkcrtK87scqL09coQp1QZbTvcd5BibuA+1QjJpamsQx0CuvVM1VPTroxvJJOgqarV7z0wZTq8VqSoXzvcdIimQ7kCIsEZw+0bTHNpP/BHYvsj+Kki/+bW1Rd+Nu+Pvrx/Wn3ijaTX2gFdtjEl1KpXChG8nHzAJfpk2teSJHbetJmKt2YrRDmLARnWwyIPBworrSUDufxe5fmU4I083TXqTeWyW 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_reload(), so that the new API can make use of them. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 2 +- mm/page_frag_cache.c | 138 ++++++++++++++++++-------------- 2 files changed, 81 insertions(+), 59 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 4ce924eaf1b1..0abffdd10a1c 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -52,7 +52,7 @@ static inline void *encoded_page_address(unsigned long encoded_va) static inline void page_frag_cache_init(struct page_frag_cache *nc) { - nc->encoded_va = 0; + memset(nc, 0, sizeof(*nc)); } static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 2544b292375a..aa6eef55bb9c 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -19,8 +19,27 @@ #include #include "internal.h" -static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, - gfp_t gfp_mask) +static bool __page_frag_cache_reuse(unsigned long encoded_va, + unsigned int pagecnt_bias) +{ + struct page *page; + + page = virt_to_page((void *)encoded_va); + if (!page_ref_sub_and_test(page, pagecnt_bias)) + return false; + + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { + free_unref_page(page, encoded_page_order(encoded_va)); + return false; + } + + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + return true; +} + +static bool __page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) { unsigned long order = PAGE_FRAG_CACHE_MAX_ORDER; struct page *page = NULL; @@ -35,8 +54,8 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, if (unlikely(!page)) { page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); if (unlikely(!page)) { - nc->encoded_va = 0; - return NULL; + memset(nc, 0, sizeof(*nc)); + return false; } order = 0; @@ -45,7 +64,33 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, nc->encoded_va = encode_aligned_va(page_address(page), order, page_is_pfmemalloc(page)); - return page; + /* Even if we own the page, we do not use atomic_set(). + * This would break get_page_unless_zero() users. + */ + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + + return true; +} + +/* Reload cache by reusing the old cache if it is possible, or + * refilling from the page allocator. + */ +static bool __page_frag_cache_reload(struct page_frag_cache *nc, + gfp_t gfp_mask) +{ + if (likely(nc->encoded_va)) { + if (__page_frag_cache_reuse(nc->encoded_va, nc->pagecnt_bias)) + goto out; + } + + if (unlikely(!__page_frag_cache_refill(nc, gfp_mask))) + return false; + +out: + /* reset page count bias and remaining to start of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->remaining = page_frag_cache_page_size(nc->encoded_va); + return true; } void page_frag_cache_drain(struct page_frag_cache *nc) @@ -55,7 +100,7 @@ void page_frag_cache_drain(struct page_frag_cache *nc) __page_frag_cache_drain(virt_to_head_page((void *)nc->encoded_va), nc->pagecnt_bias); - nc->encoded_va = 0; + memset(nc, 0, sizeof(*nc)); } EXPORT_SYMBOL(page_frag_cache_drain); @@ -73,67 +118,44 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int align_mask) { unsigned long encoded_va = nc->encoded_va; - unsigned int size, remaining; - struct page *page; - - if (unlikely(!encoded_va)) { -refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) - return NULL; - - encoded_va = nc->encoded_va; - size = page_frag_cache_page_size(encoded_va); - - /* Even if we own the page, we do not use atomic_set(). - * This would break get_page_unless_zero() users. - */ - page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); - - /* reset page count bias and remaining to start of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->remaining = size; - } else { - size = page_frag_cache_page_size(encoded_va); - } + unsigned int remaining; remaining = nc->remaining & align_mask; - if (unlikely(remaining < fragsz)) { - if (unlikely(fragsz > PAGE_SIZE)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - return NULL; - } - - page = virt_to_page((void *)encoded_va); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; - - if (unlikely(encoded_page_pfmemalloc(encoded_va))) { - free_unref_page(page, encoded_page_order(encoded_va)); - goto refill; - } + /* As we have ensured remaining is zero when initiating and draining old + * cache, 'remaining >= fragsz' checking is enough to indicate there is + * enough available space for the new fragment allocation. + */ + if (likely(remaining >= fragsz)) { + nc->pagecnt_bias--; + nc->remaining = remaining - fragsz; - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + return encoded_page_address(encoded_va) + + (page_frag_cache_page_size(encoded_va) - remaining); + } - /* reset page count bias and remaining to start of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - remaining = size; + if (unlikely(fragsz > PAGE_SIZE)) { + /* + * The caller is trying to allocate a fragment with + * fragsz > PAGE_SIZE but the cache isn't big enough to satisfy + * the request, this may happen in low memory conditions. We don't + * release the cache page because it could make memory pressure + * worse so we simply return NULL here. + */ + return NULL; } + if (unlikely(!__page_frag_cache_reload(nc, gfp_mask))) + return NULL; + + /* As the we are allocating fragment from cache by count-up way, the offset + * of allocated fragment from the just reloaded cache is zero, so remaining + * aligning and offset calculation are not needed. + */ nc->pagecnt_bias--; - nc->remaining = remaining - fragsz; + nc->remaining -= fragsz; - return encoded_page_address(encoded_va) + (size - remaining); + return encoded_page_address(nc->encoded_va); } EXPORT_SYMBOL(__page_frag_alloc_va_align);