From patchwork Sun Apr 7 13:08:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620163 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 796FCCD11C2 for ; Sun, 7 Apr 2024 13:11:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0925A6B0085; Sun, 7 Apr 2024 09:11:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0422C6B0087; Sun, 7 Apr 2024 09:11:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4C016B0088; Sun, 7 Apr 2024 09:11:06 -0400 (EDT) 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 C1D406B0085 for ; Sun, 7 Apr 2024 09:11:06 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7A7811C0909 for ; Sun, 7 Apr 2024 13:11:06 +0000 (UTC) X-FDA: 81982771332.07.5412CED Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf30.hostedemail.com (Postfix) with ESMTP id 6B8CB80007 for ; Sun, 7 Apr 2024 13:11:03 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf30.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=1712495464; 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=Llfd5SBGX8YoKGV+hiygI6ajDjsvLtHpnsL6cS+MhDs=; b=8GkIWR2qosx6qV/RLvaVz1JyHalmMGomWi4SueXocCANQ0DziKPTtITukjpXZZm6a1Q6ZQ 4Q5APC5KgWHLjjVR9YUuRxYVwkc0TOIWIn/wB5uJIrm7FuiehP03xROOycNLHnrnnqiH/d crUW6lALeMTjaiErlb2c4YiwQs2tgfE= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf30.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=1712495464; a=rsa-sha256; cv=none; b=BOoIEmxdcHc8BJKDesVHrc/RjVqEDcCPq3CoAv8b99HFkgqu1iEmSB9BKARvUG+9fzihNl JHT/FouAPxZHLg6TI6QJ6WgBYtzvo74d/2sTHwxsKa3Fn/wvr6TSUbfmtXI93KYTD1v+if m5luuFIlaTv5T9lWjYfWO6f8h3R5Dlw= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VCCHs0L3jzbfLg; Sun, 7 Apr 2024 21:10:01 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id DC2AA1404F7; Sun, 7 Apr 2024 21:10:57 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:10:57 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , David Howells , Andrew Morton , Subject: [PATCH net-next v1 01/12] mm: Move the page fragment allocator from page_alloc into its own file Date: Sun, 7 Apr 2024 21:08:38 +0800 Message-ID: <20240407130850.19625-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Stat-Signature: xkasoriuxob3rxqcj4kxta96hpkur3q1 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6B8CB80007 X-HE-Tag: 1712495463-696652 X-HE-Meta: U2FsdGVkX18qWjmlLrsqBuUci102Hz6726Ff7DAbKPIylh5Bq3EG94DgtszGmxKaCiwWjOKmDW1JA0qSsB5eIdD3X+f8aGY+jnlf0mxRXuBiAXv0pPyDTCh8tZcPq++o2Wh9965NlBZd6GEHxgsZCMFPNLIvjl463TwdBYTHswZdqKZZeKNBoVuHNWYsqF9Yy+qIGtLZqkBOulBaSE6CmIdVfthoERpgELpZzM7ecl21WuMIzoHDElndkhbpCS7KlBP74F3tIVbQ1SsTNJlzOJL/Fo9bTfOgpFebs6ogHsgINjLOmLyLK0L/znJIBoc6Wu6y8We09EFZI9MpWjAp2f549eLcWPZSdDW58kKKdWlVlPWvrTUeeSSeN6in+K8XWNNhs7jnoXl9xv++r6hauBbR9cKrQQ0NyPi320NQn17xVyZbrL+iyRmkmv6N4YNZTcbeAiPNOzuSj7kpXUuAV6mUZk9c49npzFWu1HuEXUR+dNJPdzCEc9GT1nEsoDhc4n4G4BCD8QhC2kBkquW/clPc8iCQBINaF5nr6LWNbsxkdgvsMhDqKJbYCHDFjPFiVtpV6TUf+iYFLtoxS1d45p9lOBa0cECy0UXOro26lYKXHKXibNRgwxQ7wqwoiHmeSFBBtjKWDU9kRIiOasY++jfbixXPnndrTKXUNeu/bV8U50gO9QpfWE40Tj+CyaEyG08WN1v717Ss7HX6Jnx+HNDnV8ZjKyWj34FlC/C5w8k8ZYhedQueE5egvgpn1nhjdBwG3lN16a3mZSRpSSrr/dyNMQlf56Q2RTpqPI26ML81rgUznK31wMvdadLL+oKeEs+TRuaifiSSw1bEkZjHKMaybu/z3aYdHzu8sQyLNsDKSYTBW2vKvp2H2gvUkqH2VNaKhR4FNDU= 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: Inspired by [1], but use free_unref_page() to replace free_the_page() instead of __free_pages(), use VM_BUG_ON() to catch that we can use free_unref_page() directly, also add its own header file. As the API is only used by the networking, it may make sense to move it to the networking directory like the page_pool does in the future if we can make the free_unref_page() callable outside of the mm subsystem. And we can utilize that to decouple the 'struct page' in the networking subsystem in the future. 1. https://lore.kernel.org/all/20230411160902.4134381-3-dhowells@redhat.com/ CC: David Howells Signed-off-by: Yunsheng Lin --- include/linux/gfp.h | 22 ----- include/linux/mm_types.h | 18 ---- include/linux/page_frag_cache.h | 47 ++++++++++ include/linux/skbuff.h | 1 + mm/Makefile | 1 + mm/page_alloc.c | 136 ----------------------------- mm/page_frag_cache.c | 149 ++++++++++++++++++++++++++++++++ 7 files changed, 198 insertions(+), 176 deletions(-) create mode 100644 include/linux/page_frag_cache.h create mode 100644 mm/page_frag_cache.c diff --git a/include/linux/gfp.h b/include/linux/gfp.h index c775ea3c6015..5afeab2b906f 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -310,28 +310,6 @@ __meminit void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask) __al extern void __free_pages(struct page *page, unsigned int order); extern void free_pages(unsigned long addr, unsigned int order); -struct page_frag_cache; -void page_frag_cache_drain(struct page_frag_cache *nc); -extern 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); - -static inline void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align) -{ - WARN_ON_ONCE(!is_power_of_2(align)); - return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); -} - -static inline void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) -{ - return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); -} - -extern void page_frag_free(void *addr); - #define __free_page(page) __free_pages((page), 0) #define free_page(addr) free_pages((addr), 0) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 5240bd7bca33..78a92b4475a7 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -504,9 +504,6 @@ static_assert(sizeof(struct ptdesc) <= sizeof(struct page)); */ #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) -#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) -#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) - /* * page_private can be used on tail pages. However, PagePrivate is only * checked by the VM on the head page. So page_private on the tail pages @@ -525,21 +522,6 @@ static inline void *folio_get_private(struct folio *folio) return folio->private; } -struct page_frag_cache { - void * va; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; -#else - __u32 offset; -#endif - /* we maintain a pagecount bias, so that we dont dirty cache line - * containing page->_refcount every time we allocate a fragment. - */ - unsigned int pagecnt_bias; - bool pfmemalloc; -}; - typedef unsigned long vm_flags_t; /* diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h new file mode 100644 index 000000000000..04810d8d6a7d --- /dev/null +++ b/include/linux/page_frag_cache.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_PAGE_FRAG_CACHE_H +#define _LINUX_PAGE_FRAG_CACHE_H + +#include + +#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) +#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) + +struct page_frag_cache { + void *va; +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + __u16 offset; + __u16 size; +#else + __u32 offset; +#endif + /* we maintain a pagecount bias, so that we dont dirty cache line + * containing page->_refcount every time we allocate a fragment. + */ + unsigned int pagecnt_bias; + bool pfmemalloc; +}; + +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); + +static inline void *page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); +} + +static inline void *page_frag_alloc(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask) +{ + return page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); +} + +void page_frag_free(void *addr); + +#endif diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 7dfb906d92f7..76c5be5b1a8c 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #if IS_ENABLED(CONFIG_NF_CONNTRACK) #include diff --git a/mm/Makefile b/mm/Makefile index 4abb40b911ec..146c481c006f 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -59,6 +59,7 @@ page-alloc-$(CONFIG_SHUFFLE_PAGE_ALLOCATOR) += shuffle.o memory-hotplug-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-y += page-alloc.o +obj-y += page_frag_cache.o obj-y += init-mm.o obj-y += memblock.o obj-y += $(memory-hotplug-y) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 14d39f34d336..2308360d78eb 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4675,142 +4675,6 @@ void free_pages(unsigned long addr, unsigned int order) EXPORT_SYMBOL(free_pages); -/* - * Page Fragment: - * An arbitrary-length arbitrary-offset area of memory which resides - * within a 0 or higher order page. Multiple fragments within that page - * are individually refcounted, in the page's reference counter. - * - * The page_frag functions below provide a simple allocation framework for - * page fragments. This is used by the network stack and network device - * drivers to provide a backing region of memory for use as either an - * sk_buff->head, or to be used in the "frags" portion of skb_shared_info. - */ -static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, - gfp_t gfp_mask) -{ - struct page *page = NULL; - gfp_t gfp = gfp_mask; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | - __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; - page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); - nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; -#endif - if (unlikely(!page)) - page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); - - nc->va = page ? page_address(page) : NULL; - - return page; -} - -void page_frag_cache_drain(struct page_frag_cache *nc) -{ - if (!nc->va) - return; - - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va = NULL; -} -EXPORT_SYMBOL(page_frag_cache_drain); - -void __page_frag_cache_drain(struct page *page, unsigned int count) -{ - VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); - - if (page_ref_sub_and_test(page, count)) - free_the_page(page, compound_order(page)); -} -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 size = PAGE_SIZE; - struct page *page; - int offset; - - if (unlikely(!nc->va)) { -refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) - return NULL; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - /* 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 offset to start of new frag */ - nc->pfmemalloc = page_is_pfmemalloc(page); - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; - } - - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { - page = virt_to_page(nc->va); - - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; - - if (unlikely(nc->pfmemalloc)) { - free_the_page(page, compound_order(page)); - goto refill; - } - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); - - /* reset page count bias and offset to start of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { - /* - * 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; - } - } - - nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; - - return nc->va + offset; -} -EXPORT_SYMBOL(__page_frag_alloc_align); - -/* - * Frees a page fragment allocated out of either a compound or order 0 page. - */ -void page_frag_free(void *addr) -{ - struct page *page = virt_to_head_page(addr); - - if (unlikely(put_page_testzero(page))) - free_the_page(page, compound_order(page)); -} -EXPORT_SYMBOL(page_frag_free); - static void *make_alloc_exact(unsigned long addr, unsigned int order, size_t size) { diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c new file mode 100644 index 000000000000..a0f90ba25200 --- /dev/null +++ b/mm/page_frag_cache.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Page fragment allocator + * + * Page Fragment: + * An arbitrary-length arbitrary-offset area of memory which resides within a + * 0 or higher order page. Multiple fragments within that page are + * individually refcounted, in the page's reference counter. + * + * The page_frag functions provide a simple allocation framework for page + * fragments. This is used by the network stack and network device drivers to + * provide a backing region of memory for use as either an sk_buff->head, or to + * be used in the "frags" portion of skb_shared_info. + */ + +#include +#include +#include +#include +#include "internal.h" + +static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) +{ + struct page *page = NULL; + gfp_t gfp = gfp_mask; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | + __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; + page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, + PAGE_FRAG_CACHE_MAX_ORDER); + nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; +#endif + if (unlikely(!page)) + page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + + nc->va = page ? page_address(page) : NULL; + + return page; +} + +void page_frag_cache_drain(struct page_frag_cache *nc) +{ + if (!nc->va) + return; + + __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); + nc->va = NULL; +} +EXPORT_SYMBOL(page_frag_cache_drain); + +void __page_frag_cache_drain(struct page *page, unsigned int count) +{ + VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); + + /* ensure we can call free_unref_page() directly as we are bypassing + * the pcp_allowed_order() checking. + */ + VM_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); + + if (page_ref_sub_and_test(page, count)) + free_unref_page(page, compound_order(page)); +} +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 size = PAGE_SIZE; + struct page *page; + int offset; + + if (unlikely(!nc->va)) { +refill: + page = __page_frag_cache_refill(nc, gfp_mask); + if (!page) + return NULL; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + /* 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 offset to start of new frag */ + nc->pfmemalloc = page_is_pfmemalloc(page); + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset = size; + } + + offset = nc->offset - fragsz; + if (unlikely(offset < 0)) { + page = virt_to_page(nc->va); + + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + goto refill; + + if (unlikely(nc->pfmemalloc)) { + free_unref_page(page, compound_order(page)); + goto refill; + } + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + + /* reset page count bias and offset to start of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + offset = size - fragsz; + if (unlikely(offset < 0)) { + /* + * 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; + } + } + + nc->pagecnt_bias--; + offset &= align_mask; + nc->offset = offset; + + return nc->va + offset; +} +EXPORT_SYMBOL(__page_frag_alloc_align); + +/* + * Frees a page fragment allocated out of either a compound or order 0 page. + */ +void page_frag_free(void *addr) +{ + struct page *page = virt_to_head_page(addr); + + if (unlikely(put_page_testzero(page))) + free_unref_page(page, compound_order(page)); +} +EXPORT_SYMBOL(page_frag_free); From patchwork Sun Apr 7 13:08:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620164 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 D5EA8CD1296 for ; Sun, 7 Apr 2024 13:11:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F12866B0087; Sun, 7 Apr 2024 09:11:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC2A96B0088; Sun, 7 Apr 2024 09:11:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CED356B0089; Sun, 7 Apr 2024 09:11:07 -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 B0A9B6B0087 for ; Sun, 7 Apr 2024 09:11:07 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4D80F1A02DF for ; Sun, 7 Apr 2024 13:11:07 +0000 (UTC) X-FDA: 81982771374.05.4129DCB Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf01.hostedemail.com (Postfix) with ESMTP id 040A140003 for ; Sun, 7 Apr 2024 13:11:03 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712495465; 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=6cbmJ3Bqjx3XmshEl1h+C2PeMxJYAdoZFxbtmgbCWYA=; b=E/AJTVZQGhpqyWy+bN+L5GaEvKKiA+8Et4SR2I6zbyqFeIx2UJ9DdK2iyYgBAgg4FhBe/+ YJs8DTokTWO7eaDamLUKCgQ/JE/kf1hjxfd+E8Ve1j21VNF9zKEh01j99fII5CdqE7wMeF NUeFy1FylP28sIDIFDEX9ximi1O++0w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712495465; a=rsa-sha256; cv=none; b=WmEHXhXMSOfg1tPjoi6uj/0Hl+ui7sW5SkSHxlJHSJLBDNXAIhPh3clTrTWzH/zobCLPEU 1XsndpGKfj5YS92M5RKO3qF9/dOx7Zzw/N49zgxfcyMB+EZMDRhn0fHcfRtnULjo3AoHDV 9Hy4l3ss8yW6Djkp3naqgA5fh9wdMFk= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4VCCFw0Q1Tz1QCR6; Sun, 7 Apr 2024 21:08:20 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 8DA4B1404F7; Sun, 7 Apr 2024 21:10:59 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:10:59 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v1 02/12] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Sun, 7 Apr 2024 21:08:39 +0800 Message-ID: <20240407130850.19625-3-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: 040A140003 X-Rspam-User: X-Stat-Signature: hii5adfmu71eifpcy1693i3etzigoqeb X-Rspamd-Server: rspam03 X-HE-Tag: 1712495463-118671 X-HE-Meta: U2FsdGVkX19Kz3nPlQHgJgBRV2SZuu3LEoNwhddcEAifeEMXMy2KS9i+yTH6lpdPbBr2+JsUhADyY4IUn+/n/ABTzewxPiXvyIzLFaAeRNQHDZp2CCQcUFATOe0jWfbcf8x2qVgiuK6yuGNad/oYr4aNMa2LZs3KUOmYXnm9wKuRCRNpuLDBXVrpmvjS36Wk05MhAyVCRH4xXHHK3ZZ+Bxxp/iyu5uiAKVe7sCkrQyRe58BKYna71SfO0dmlZYSDU/FUQGK5qqa4aSNWblfxs9EbNCKJIJv+IMcBRhDDmmIdo6e2uEBGIw/Xr2w6IBtPPiu7UrezoAPowq0hDMtLUTJ7Jud8IrQiBZ5n/Q9sq//mt76cv2SSg4YngUFwxQ8Vwlf2VoUF8R0rJQbz8hfgYM1lYsvZvWSzJt660BlTho+EwrQ9uDB/AVIxpT+4Sweu/XwcbCiR7zL+6klhhP7k6zhoC2NmkKkvM5J7kT+7QTG6oFI0ebmvmxVzGqZ1zeTqAYeXRL/jX86D3VIvj3kUJbva4PuWjBX5Gka1FNFvn7OhKZOmeU6L2E3VkLD30QrN7C9NmPk41IyWk4QO8QqE9Vz9wa0k7SzYZAR6dl0ItnXiJO5p1pXeAPhbkanK2sTS3KHzq3BJhug3ld74Gpts+cY//pOjZOSq6UETKeFIjKB9wDBs2e98ysJEgm/aT9wZFfqwXeQnUjiTN+RGYnKfx4QhTcX8xLfCvYtzLIKZvNlvE0Ocl7LDkKRCL17xAea+2Fy7jKyomC536a6c1ViLr9KL1mMeH+Q17IhYuZLg0mMb2Xg0ZWNtYgHBWjuh6z6WO1jPvZsaCamEA4oONm5rUq6+t4jVP5Hxaqhpmz8tbl8s5/7Muv6b5uR8K1EovUY7zzN8MJKz+E99YDsSavH2Vi3CS/A2iR6nRWd4DY6yyVgxe1InBWsKYtIVFUQbZtV4DuRwWjsukyNb0z0JlR3 DLA== 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: We are above to use page_frag_alloc_*() API to not just allocate memory for skb->data, but also use them to do the memory allocation for skb frag too. Currently the implementation of page_frag in mm subsystem is running the offset as a countdown rather than count-up value, there may have several advantages to that as mentioned in [1], but it may have some disadvantages, for example, it may disable skb frag coaleasing and more correct cache prefetching We have a trade-off to make in order to have a unified implementation and API for page_frag, so use a initial zero offset in this patch, and the following patch will try to make some optimization to aovid the disadvantages as much as possible. 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5c.camel@gmail.com/ CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/page_frag_cache.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index a0f90ba25200..3e3e88d9af90 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -67,9 +67,8 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { - unsigned int size = PAGE_SIZE; + unsigned int size, offset; struct page *page; - int offset; if (unlikely(!nc->va)) { refill: @@ -77,10 +76,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, if (!page) return NULL; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* Even if we own the page, we do not use atomic_set(). * This would break get_page_unless_zero() users. */ @@ -89,11 +84,18 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pfmemalloc = page_is_pfmemalloc(page); nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; + nc->offset = 0; } - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#else + size = PAGE_SIZE; +#endif + + offset = ALIGN(nc->offset, -align_mask); + if (unlikely(offset + fragsz > size)) { page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -104,17 +106,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* OK, page count is 0, we can safely set it */ set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { + offset = 0; + if (unlikely(fragsz > size)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -129,8 +127,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; } From patchwork Sun Apr 7 13:08:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620165 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 6519CCD128A for ; Sun, 7 Apr 2024 13:11:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 611446B0088; Sun, 7 Apr 2024 09:11:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C1956B0089; Sun, 7 Apr 2024 09:11:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B1CE6B008A; Sun, 7 Apr 2024 09:11:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 31C1C6B0088 for ; Sun, 7 Apr 2024 09:11:09 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DF7F81A0580 for ; Sun, 7 Apr 2024 13:11:08 +0000 (UTC) X-FDA: 81982771416.07.52381F0 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf28.hostedemail.com (Postfix) with ESMTP id 054DDC0018 for ; Sun, 7 Apr 2024 13:11:05 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712495466; a=rsa-sha256; cv=none; b=4RQnt6D3JFC+FwnbPD/NUHNSURvUO4EVjgBVcBOmoDQc+GvvztJLTeQawJ5OEPwvs4VVB6 boRzw6rVqi6vLNve26P0I64P/ZBcSfAI2+cq7hVHLITiHQZzH5HD/ZUB4sHxqlh0OwzCFY ueWHkXxyEvojALOMYfVs8eo6V/2PySo= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712495466; 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=csHs/6EOnMLYAL1ZoQ/O7qgdOuwkAt68E5NLMmNzJf8=; b=NMmn9IQ+NJ2ND98jh0TqlrBN3cSo0Uelq0hGUtQ/zrDKh/7DJuzDLPcc4z7oRxllZrGHvH 521+pL4uKMK40wd5E5VtDNNnhFMMX2cLu78NjqFPGNXGdO5bUZ7JWenehW37cNWD/zJOu8 SM7gQ0L3cwaMhOJqi2hnSDOYolonPbA= Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4VCCGT43NLz1ym6R; Sun, 7 Apr 2024 21:08:49 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 1C8211A0172; Sun, 7 Apr 2024 21:11:02 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:01 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Andrew Morton , Eric Dumazet , David Howells , Marc Dionne , , Subject: [PATCH net-next v1 03/12] mm: page_frag: change page_frag_alloc_* API to accept align param Date: Sun, 7 Apr 2024 21:08:40 +0800 Message-ID: <20240407130850.19625-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 054DDC0018 X-Stat-Signature: 8tz8hr3q8mju3ytennwzx9h3j84iyjzy X-HE-Tag: 1712495465-224210 X-HE-Meta: U2FsdGVkX19ootbCBp5kVHtMvGwI5dlXLBt7IoAZBfroopnqcy6p8rtrk1jTWgBMyqWAJT7if3S6KA+xCythkHpamnpa5X5neSIy2UKugyuWiVV9F8PeJSn8GDrLjg7kczUTe4d0+RtKrSZRRtXQ7ROt5dAqaeu7vS8bTzedsEXQzXXe7GPWKAbiikN3VsWzG7IyZ1ODWRxzUDxs6us8WV35zC6Rs1VGMvGGJ7x2+fOieptu2cHwSj2RGhbzjfKOy8f9Isox3RAuxC8vgp7ncklPTx9TzvmQcq3+pniOkNKBtmzC43yC5glXDhZ515yBSPt5kfC+29WtG4Fvzl+/k1BLdj2S4bWp32yC8Ak2t5gbz4kgDJxbJZpf+g+4WbN2ME+rjRh4vqHxY8uWz24bb0bICrMTI/8Ukt4NxTrKplgEpuuiUyvIiiBf7W8W95qYEtux6bdD2I2AAF++Mz5tuQrm6X5ZrzcPwiTPO2gC7p0hO6NCXJO2nc2+Ik+nON5vkqBnC73xPyOCnToOkQ+iAI8fOQi69e0vg4pg5lThFA7GsmFEReAiovdYB07waluh1/uIi9ZX6x+2IaETVGW7vfSABbRvrTUM05IBreN4/rfdySDrzkTQ75heRLJOUeS10RNWdfC37yJpp7rvsxqHazlZtLIIHzgwulyovSYM3Q9yGwu88k/3jQq1m5A0V6u7ui6JNmOEslvNmRmLGTKRar9bD6qA+CD29y3u9Ft73ELQ8032ih0Wr7Lf79TYzFI4CDvPSRr7leXu6cJSM9RxsVr5KRLlLqUiwYC+17PpsBOvaVwLPYFULwaNEEDjwqWbF+R+6jn6Pfq70lXOme6HUpHM1zNvUwcaOJ7P5qOKXg8t0Iuuz60KjLLI4r/LFlfVoL7PmneT/I5m3Y2y/uPzVZW3wAYM5MTR/x6vi1TGS5slXZZvzYykTlL8ImSvnNX4VrruAhoJCcCyxb8g5En OLRSp0ON glpqlMbOGgtGqJ8+uUCnmYXEiYqEhPk556cvtq1KLd09QVw6YpxKwno8FhdyPO0AJiUv88pMcfK23yVMY0w0KMTzgd5m8uJmDPaem//wAeD0fqh8Yb8g6/Cx63HwI2WR7QH1G2eoLcOZS2CGhDQ3SJYHnzhVhiiU6etFzmbtiZ4LXsjlkk1/xa4uEuTs2UmgJrBar 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: When page_frag_alloc_* API doesn't need data alignment, the ALIGN() operation is unnecessary, so change page_frag_alloc_* API to accept align param instead of align_mask param, and do the ALIGN()'ing in the inline helper when needed. Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 20 ++++++++++++-------- include/linux/skbuff.h | 12 ++++++------ mm/page_frag_cache.c | 9 ++++----- net/core/skbuff.c | 12 +++++------- net/rxrpc/txbuf.c | 5 +++-- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 04810d8d6a7d..cc0ede0912f3 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -25,21 +25,25 @@ struct page_frag_cache { 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_alloc(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask); + +static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align) +{ + nc->offset = ALIGN(nc->offset, align); + + return page_frag_alloc(nc, fragsz, gfp_mask); +} static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) { WARN_ON_ONCE(!is_power_of_2(align)); - return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); -} -static inline void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) -{ - return page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); + return __page_frag_alloc_align(nc, fragsz, gfp_mask, align); } void page_frag_free(void *addr); diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 76c5be5b1a8c..2ef14dde5bbc 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3268,7 +3268,7 @@ static inline void skb_queue_purge(struct sk_buff_head *list) unsigned int skb_rbtree_purge(struct rb_root *root); void skb_errqueue_purge(struct sk_buff_head *list); -void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); +void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align); /** * netdev_alloc_frag - allocate a page fragment @@ -3279,14 +3279,14 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); */ static inline void *netdev_alloc_frag(unsigned int fragsz) { - return __netdev_alloc_frag_align(fragsz, ~0u); + return __netdev_alloc_frag_align(fragsz, 1u); } static inline void *netdev_alloc_frag_align(unsigned int fragsz, unsigned int align) { WARN_ON_ONCE(!is_power_of_2(align)); - return __netdev_alloc_frag_align(fragsz, -align); + return __netdev_alloc_frag_align(fragsz, align); } struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length, @@ -3346,18 +3346,18 @@ static inline void skb_free_frag(void *addr) page_frag_free(addr); } -void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); +void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align); static inline void *napi_alloc_frag(unsigned int fragsz) { - return __napi_alloc_frag_align(fragsz, ~0u); + return __napi_alloc_frag_align(fragsz, 1u); } static inline void *napi_alloc_frag_align(unsigned int fragsz, unsigned int align) { WARN_ON_ONCE(!is_power_of_2(align)); - return __napi_alloc_frag_align(fragsz, -align); + return __napi_alloc_frag_align(fragsz, align); } struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int length); diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 3e3e88d9af90..39c744c892ed 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -63,9 +63,8 @@ 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) +void *page_frag_alloc(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask) { unsigned int size, offset; struct page *page; @@ -94,7 +93,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, size = PAGE_SIZE; #endif - offset = ALIGN(nc->offset, -align_mask); + offset = nc->offset; if (unlikely(offset + fragsz > size)) { page = virt_to_page(nc->va); @@ -131,7 +130,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return nc->va + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(page_frag_alloc); /* * Frees a page fragment allocated out of either a compound or order 0 page. diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 2a5ce6667bbb..e5196c284b33 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -305,18 +305,17 @@ void napi_get_frags_check(struct napi_struct *napi) local_bh_enable(); } -void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) +void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align) { struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); fragsz = SKB_DATA_ALIGN(fragsz); - return __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, - align_mask); + return __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align); } EXPORT_SYMBOL(__napi_alloc_frag_align); -void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) +void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align) { void *data; @@ -324,15 +323,14 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) if (in_hardirq() || irqs_disabled()) { struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); - data = __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, - align_mask); + data = __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align); } else { struct napi_alloc_cache *nc; local_bh_disable(); nc = this_cpu_ptr(&napi_alloc_cache); data = __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, - align_mask); + align); local_bh_enable(); } return data; diff --git a/net/rxrpc/txbuf.c b/net/rxrpc/txbuf.c index e0679658d9de..eb640875bf07 100644 --- a/net/rxrpc/txbuf.c +++ b/net/rxrpc/txbuf.c @@ -32,9 +32,10 @@ struct rxrpc_txbuf *rxrpc_alloc_data_txbuf(struct rxrpc_call *call, size_t data_ hoff = round_up(sizeof(*whdr), data_align) - sizeof(*whdr); total = hoff + sizeof(*whdr) + data_size; + data_align = max_t(size_t, data_align, L1_CACHE_BYTES); mutex_lock(&call->conn->tx_data_alloc_lock); - buf = __page_frag_alloc_align(&call->conn->tx_data_alloc, total, gfp, - ~(data_align - 1) & ~(L1_CACHE_BYTES - 1)); + buf = page_frag_alloc_align(&call->conn->tx_data_alloc, total, gfp, + data_align); mutex_unlock(&call->conn->tx_data_alloc_lock); if (!buf) { kfree(txb); From patchwork Sun Apr 7 13:08: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: 13620166 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 34837CD128A for ; Sun, 7 Apr 2024 13:11:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF2DB6B0092; Sun, 7 Apr 2024 09:11:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA3086B0093; Sun, 7 Apr 2024 09:11:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 943766B0095; Sun, 7 Apr 2024 09:11:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 72C9B6B0092 for ; Sun, 7 Apr 2024 09:11:16 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 09FD8A0D04 for ; Sun, 7 Apr 2024 13:11:16 +0000 (UTC) X-FDA: 81982771752.30.B6890E8 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf26.hostedemail.com (Postfix) with ESMTP id 09419140020 for ; Sun, 7 Apr 2024 13:11:12 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; spf=pass (imf26.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712495474; a=rsa-sha256; cv=none; b=nxvcHMsm0ZsUQzG/wSrwXlAVGQIKCVQ18BlUUF/DQUmLcArFIdpNk7khKcDwx0pn6IFGDQ HPXfqFKNkGt/XdyTISHNNeHfEcNc1mQCI7mI1msien6O0/YOrI9yYZaw0uHcaVhzSK+9d1 A3sMRF6ZkXEQIlvu6+wMcuXf7LDece4= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; spf=pass (imf26.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712495474; 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=WjLE9YKwhDp+R6UGiiOCfAsJfSu/p6iheoGnxOdrzt4=; b=Y7ycFPDRCckEjWDtN66dEybCM/JdUtSFph+Gl9VyJ/9czvY6PwIx9orrUv5rGGp528RbR8 nrsaWvz1aJSf8wsOownk44zsUN6OPQuub7n4LRxZxK7NbT1pxJbLBPZtullRFQTZ0JRQ48 pGBnW2WHxFX5XWDmfRfhWM7QjoS4VIo= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VCCFc3t0szXktf; Sun, 7 Apr 2024 21:08:04 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 61F791404F7; Sun, 7 Apr 2024 21:11:08 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:07 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Jeroen de Borst , Praveen Kaligineedi , Shailend Chand , Eric Dumazet , Jesse Brandeburg , Tony Nguyen , Sunil Goutham , Geetha sowjanya , Subbaraya Sundeep , hariprasad , Felix Fietkau , Sean Wang , Mark Lee , Lorenzo Bianconi , Matthias Brugger , AngeloGioacchino Del Regno , Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Chaitanya Kulkarni , "Michael S. Tsirkin" , Jason Wang , Andrew Morton , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Howells , Marc Dionne , Chuck Lever , Jeff Layton , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey , Trond Myklebust , Anna Schumaker , , , , , , , , , , Subject: [PATCH net-next v1 04/12] mm: page_frag: add '_va' suffix to page_frag API Date: Sun, 7 Apr 2024 21:08:41 +0800 Message-ID: <20240407130850.19625-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 09419140020 X-Stat-Signature: chykinjwkmt331fmr37ioaf9qkcnxzdm X-HE-Tag: 1712495472-944506 X-HE-Meta: U2FsdGVkX18uvRuE6vWgFiFrrqSJS9KaZ9MMv7t5kZPED8QBtk/clRrrhOOUK4rOVhfxwXA+DsJOceuigUZdEpuEJ+Tb6b6+/sarRlHxyswYIptDEMUBbG7I2+MxcQA7AJI9IUDa/s1HKJC/5XMJdgG2Lw1FSiCoCTarDxzYqKf1YhfiTMK8PmGcHwHbvZ4V35doVxaEfw4rzMu5ZPfcaMyVQnAYmUtgJM/miTfTWCb22llHOxqtOKQHzIWDeGcXjvzN/6uvXit7DATGIiqPaf+B7iRsLnQ+aXf4tXXXhvE+sVUNpadC0Q1MvBxif8b/VwQHBB510CeFdXnUhqCIqNbLdWUmg/7B+n8VYHH/w04dPSIA9UNJ9zs8RV5vVdcE3vQNwCdjdrThkJ9U9Et2hUMuteW5MzczjuMHYiFH8is6EnZMjTc6q5vqcAZUlPezVLIZQ90/1xDWWVYYt1cHr0jrUtH9iGSHzlUPMwy1OSTziokCvzNLBsWxKFyFTDwqG08ALsi90JjI4g6o0MBDQXXbT6nRbYUmIiSpcC8eOUfxyd/R418w1ifxIO+qd+p434bqeSdp8qSXiupoQwJSVjAZ4pIpTbL7i+H0ZD9VgMxUbKwFX+ZTsfIPGhvvtaj31C8Y4bDpHzQ1MiFa+Mj527cPkEmJP9VzepQdQU7/YMHJJGHrjP7NZi7NLtPDgDCN9b7XJOB8GfgkkMc6Vc00QUQn1ojBNlFGj0KqJ6+1jRIKOFisWbRswaRcbKWywzZuQ5pFAWfe/Q++SWcICwdWiVec4NdomaZFIi8qhNUzgBWC4EzkQuLT4wZAw/s5/efllHI7eN1KPJ8zFyG1+xtcjq/2v7+UorqbfjFPw9PRHc4B+NQtaiHMH8irMbOoCcYJpz+eWiQ+bYgaxd6FVxxcU3Ij79PGVw+fgce+atj/EqXuW07Pj/pgMxIE127yRHkMifB3SbXYztY3w/HlHyO zpwC3WJF adJCsrGDrrH4gQNwXIe073OE8JFdP5u+M3kZXxuYlPDh8H3yi4CF18gm/K6hSwpoutdMuK1tmBuy6KVsQe23qb0SoiX2bbVXvAb8zjvdq+talmjWd0R+4DraqJnNA6FT6bbcVR3SkP87R5oqpb7NaPO9/Y0bp/ByBDc0YGPdlTBKgtEAmzCmuC9H9jWkNYncrHOZ1uwhNvbCMJ73DjsGYbDgZ3GgyFqyQzsfKwpAKt4vgfWZd/LEqUyFJezvBYM18BS2+gLfhtYiaS9g= 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: Currently most of the API for page_frag API is returning 'virtual address' as output or expecting 'virtual address' as input, in order to differentiate the API handling between 'virtual address' and 'struct page', add '_va' suffix to the corresponding API mirroring the page_pool_alloc_va() API of the page_pool. Signed-off-by: Yunsheng Lin --- drivers/net/ethernet/google/gve/gve_rx.c | 4 ++-- drivers/net/ethernet/intel/ice/ice_txrx.c | 2 +- drivers/net/ethernet/intel/ice/ice_txrx.h | 2 +- drivers/net/ethernet/intel/ice/ice_txrx_lib.c | 2 +- .../net/ethernet/intel/ixgbevf/ixgbevf_main.c | 4 ++-- .../marvell/octeontx2/nic/otx2_common.c | 2 +- drivers/net/ethernet/mediatek/mtk_wed_wo.c | 4 ++-- drivers/nvme/host/tcp.c | 8 +++---- drivers/nvme/target/tcp.c | 22 ++++++++--------- drivers/vhost/net.c | 6 ++--- include/linux/page_frag_cache.h | 24 ++++++++++--------- include/linux/skbuff.h | 2 +- kernel/bpf/cpumap.c | 2 +- mm/page_frag_cache.c | 10 ++++---- net/core/skbuff.c | 15 ++++++------ net/core/xdp.c | 2 +- net/rxrpc/txbuf.c | 15 ++++++------ net/sunrpc/svcsock.c | 4 ++-- 18 files changed, 67 insertions(+), 63 deletions(-) diff --git a/drivers/net/ethernet/google/gve/gve_rx.c b/drivers/net/ethernet/google/gve/gve_rx.c index cd727e55ae0f..820874c1c570 100644 --- a/drivers/net/ethernet/google/gve/gve_rx.c +++ b/drivers/net/ethernet/google/gve/gve_rx.c @@ -687,7 +687,7 @@ static int gve_xdp_redirect(struct net_device *dev, struct gve_rx_ring *rx, total_len = headroom + SKB_DATA_ALIGN(len) + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); - frame = page_frag_alloc(&rx->page_cache, total_len, GFP_ATOMIC); + frame = page_frag_alloc_va(&rx->page_cache, total_len, GFP_ATOMIC); if (!frame) { u64_stats_update_begin(&rx->statss); rx->xdp_alloc_fails++; @@ -700,7 +700,7 @@ static int gve_xdp_redirect(struct net_device *dev, struct gve_rx_ring *rx, err = xdp_do_redirect(dev, &new, xdp_prog); if (err) - page_frag_free(frame); + page_frag_free_va(frame); return err; } diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.c b/drivers/net/ethernet/intel/ice/ice_txrx.c index 8bb743f78fcb..399b317c509d 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx.c +++ b/drivers/net/ethernet/intel/ice/ice_txrx.c @@ -126,7 +126,7 @@ ice_unmap_and_free_tx_buf(struct ice_tx_ring *ring, struct ice_tx_buf *tx_buf) dev_kfree_skb_any(tx_buf->skb); break; case ICE_TX_BUF_XDP_TX: - page_frag_free(tx_buf->raw_buf); + page_frag_free_va(tx_buf->raw_buf); break; case ICE_TX_BUF_XDP_XMIT: xdp_return_frame(tx_buf->xdpf); diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.h b/drivers/net/ethernet/intel/ice/ice_txrx.h index feba314a3fe4..6379f57d8228 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx.h +++ b/drivers/net/ethernet/intel/ice/ice_txrx.h @@ -148,7 +148,7 @@ static inline int ice_skb_pad(void) * @ICE_TX_BUF_DUMMY: dummy Flow Director packet, unmap and kfree() * @ICE_TX_BUF_FRAG: mapped skb OR &xdp_buff frag, only unmap DMA * @ICE_TX_BUF_SKB: &sk_buff, unmap and consume_skb(), update stats - * @ICE_TX_BUF_XDP_TX: &xdp_buff, unmap and page_frag_free(), stats + * @ICE_TX_BUF_XDP_TX: &xdp_buff, unmap and page_frag_free_va(), stats * @ICE_TX_BUF_XDP_XMIT: &xdp_frame, unmap and xdp_return_frame(), stats * @ICE_TX_BUF_XSK_TX: &xdp_buff on XSk queue, xsk_buff_free(), stats */ diff --git a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c index df072ce767b1..c34cc02ad578 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c +++ b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c @@ -288,7 +288,7 @@ ice_clean_xdp_tx_buf(struct device *dev, struct ice_tx_buf *tx_buf, switch (tx_buf->type) { case ICE_TX_BUF_XDP_TX: - page_frag_free(tx_buf->raw_buf); + page_frag_free_va(tx_buf->raw_buf); break; case ICE_TX_BUF_XDP_XMIT: xdp_return_frame_bulk(tx_buf->xdpf, bq); diff --git a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c index 3161a13079fe..c35b8f675b48 100644 --- a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c +++ b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c @@ -303,7 +303,7 @@ static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector, /* free the skb */ if (ring_is_xdp(tx_ring)) - page_frag_free(tx_buffer->data); + page_frag_free_va(tx_buffer->data); else napi_consume_skb(tx_buffer->skb, napi_budget); @@ -2413,7 +2413,7 @@ static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring) /* Free all the Tx ring sk_buffs */ if (ring_is_xdp(tx_ring)) - page_frag_free(tx_buffer->data); + page_frag_free_va(tx_buffer->data); else dev_kfree_skb_any(tx_buffer->skb); diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c index a85ac039d779..8eb5820b8a70 100644 --- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c @@ -553,7 +553,7 @@ static int __otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool, *dma = dma_map_single_attrs(pfvf->dev, buf, pool->rbsize, DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); if (unlikely(dma_mapping_error(pfvf->dev, *dma))) { - page_frag_free(buf); + page_frag_free_va(buf); return -ENOMEM; } diff --git a/drivers/net/ethernet/mediatek/mtk_wed_wo.c b/drivers/net/ethernet/mediatek/mtk_wed_wo.c index 7063c78bd35f..c4228719f8a4 100644 --- a/drivers/net/ethernet/mediatek/mtk_wed_wo.c +++ b/drivers/net/ethernet/mediatek/mtk_wed_wo.c @@ -142,8 +142,8 @@ mtk_wed_wo_queue_refill(struct mtk_wed_wo *wo, struct mtk_wed_wo_queue *q, dma_addr_t addr; void *buf; - buf = page_frag_alloc(&q->cache, q->buf_size, - GFP_ATOMIC | GFP_DMA32); + buf = page_frag_alloc_va(&q->cache, q->buf_size, + GFP_ATOMIC | GFP_DMA32); if (!buf) break; diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index fdbcdcedcee9..79eddd74bfbb 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -500,7 +500,7 @@ static void nvme_tcp_exit_request(struct blk_mq_tag_set *set, { struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); - page_frag_free(req->pdu); + page_frag_free_va(req->pdu); } static int nvme_tcp_init_request(struct blk_mq_tag_set *set, @@ -514,7 +514,7 @@ static int nvme_tcp_init_request(struct blk_mq_tag_set *set, struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx]; u8 hdgst = nvme_tcp_hdgst_len(queue); - req->pdu = page_frag_alloc(&queue->pf_cache, + req->pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(struct nvme_tcp_cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!req->pdu) @@ -1331,7 +1331,7 @@ static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl) { struct nvme_tcp_request *async = &ctrl->async_req; - page_frag_free(async->pdu); + page_frag_free_va(async->pdu); } static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) @@ -1340,7 +1340,7 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) struct nvme_tcp_request *async = &ctrl->async_req; u8 hdgst = nvme_tcp_hdgst_len(queue); - async->pdu = page_frag_alloc(&queue->pf_cache, + async->pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(struct nvme_tcp_cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!async->pdu) diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c index a5422e2c979a..ea356ce22672 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -1462,24 +1462,24 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue, c->queue = queue; c->req.port = queue->port->nport; - c->cmd_pdu = page_frag_alloc(&queue->pf_cache, + c->cmd_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->cmd_pdu) return -ENOMEM; c->req.cmd = &c->cmd_pdu->cmd; - c->rsp_pdu = page_frag_alloc(&queue->pf_cache, + c->rsp_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->rsp_pdu) goto out_free_cmd; c->req.cqe = &c->rsp_pdu->cqe; - c->data_pdu = page_frag_alloc(&queue->pf_cache, + c->data_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->data_pdu) goto out_free_rsp; - c->r2t_pdu = page_frag_alloc(&queue->pf_cache, + c->r2t_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->r2t_pdu) goto out_free_data; @@ -1494,20 +1494,20 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue, return 0; out_free_data: - page_frag_free(c->data_pdu); + page_frag_free_va(c->data_pdu); out_free_rsp: - page_frag_free(c->rsp_pdu); + page_frag_free_va(c->rsp_pdu); out_free_cmd: - page_frag_free(c->cmd_pdu); + page_frag_free_va(c->cmd_pdu); return -ENOMEM; } static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c) { - page_frag_free(c->r2t_pdu); - page_frag_free(c->data_pdu); - page_frag_free(c->rsp_pdu); - page_frag_free(c->cmd_pdu); + page_frag_free_va(c->r2t_pdu); + page_frag_free_va(c->data_pdu); + page_frag_free_va(c->rsp_pdu); + page_frag_free_va(c->cmd_pdu); } static int nvmet_tcp_alloc_cmds(struct nvmet_tcp_queue *queue) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index c64ded183f8d..96d5ca299552 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -682,8 +682,8 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, return -ENOSPC; buflen += SKB_DATA_ALIGN(len + pad); - buf = page_frag_alloc_align(&net->pf_cache, buflen, GFP_KERNEL, - SMP_CACHE_BYTES); + buf = page_frag_alloc_va_align(&net->pf_cache, buflen, GFP_KERNEL, + SMP_CACHE_BYTES); if (unlikely(!buf)) return -ENOMEM; @@ -730,7 +730,7 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, return 0; err: - page_frag_free(buf); + page_frag_free_va(buf); return ret; } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index cc0ede0912f3..9d5d86b2d3ab 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -25,27 +25,29 @@ struct page_frag_cache { 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(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask); +void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask); -static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align) +static inline void *__page_frag_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, + gfp_t gfp_mask, + unsigned int align) { nc->offset = ALIGN(nc->offset, align); - return page_frag_alloc(nc, fragsz, gfp_mask); + return page_frag_alloc_va(nc, fragsz, gfp_mask); } -static inline void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align) +static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, + gfp_t gfp_mask, + unsigned int align) { WARN_ON_ONCE(!is_power_of_2(align)); - return __page_frag_alloc_align(nc, fragsz, gfp_mask, align); + return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } -void page_frag_free(void *addr); +void page_frag_free_va(void *addr); #endif diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 2ef14dde5bbc..ccd0244f0f39 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3343,7 +3343,7 @@ static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev, static inline void skb_free_frag(void *addr) { - page_frag_free(addr); + page_frag_free_va(addr); } void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align); diff --git a/kernel/bpf/cpumap.c b/kernel/bpf/cpumap.c index a8e34416e960..3a6a237e7dd3 100644 --- a/kernel/bpf/cpumap.c +++ b/kernel/bpf/cpumap.c @@ -322,7 +322,7 @@ static int cpu_map_kthread_run(void *data) /* Bring struct page memory area to curr CPU. Read by * build_skb_around via page_is_pfmemalloc(), and when - * freed written by page_frag_free call. + * freed written by page_frag_free_va call. */ prefetchw(page); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 39c744c892ed..7f639af4e518 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -63,8 +63,8 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); -void *page_frag_alloc(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask) +void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask) { unsigned int size, offset; struct page *page; @@ -130,16 +130,16 @@ void *page_frag_alloc(struct page_frag_cache *nc, unsigned int fragsz, return nc->va + offset; } -EXPORT_SYMBOL(page_frag_alloc); +EXPORT_SYMBOL(page_frag_alloc_va); /* * Frees a page fragment allocated out of either a compound or order 0 page. */ -void page_frag_free(void *addr) +void page_frag_free_va(void *addr) { struct page *page = virt_to_head_page(addr); if (unlikely(put_page_testzero(page))) free_unref_page(page, compound_order(page)); } -EXPORT_SYMBOL(page_frag_free); +EXPORT_SYMBOL(page_frag_free_va); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index e5196c284b33..2c10ebd133ac 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -311,7 +311,7 @@ void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align) fragsz = SKB_DATA_ALIGN(fragsz); - return __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align); + return __page_frag_alloc_va_align(&nc->page, fragsz, GFP_ATOMIC, align); } EXPORT_SYMBOL(__napi_alloc_frag_align); @@ -323,14 +323,15 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align) if (in_hardirq() || irqs_disabled()) { struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); - data = __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align); + data = __page_frag_alloc_va_align(nc, fragsz, GFP_ATOMIC, + align); } else { struct napi_alloc_cache *nc; local_bh_disable(); nc = this_cpu_ptr(&napi_alloc_cache); - data = __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, - align); + data = __page_frag_alloc_va_align(&nc->page, fragsz, GFP_ATOMIC, + align); local_bh_enable(); } return data; @@ -740,12 +741,12 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, if (in_hardirq() || irqs_disabled()) { nc = this_cpu_ptr(&netdev_alloc_cache); - data = page_frag_alloc(nc, len, gfp_mask); + data = page_frag_alloc_va(nc, len, gfp_mask); pfmemalloc = nc->pfmemalloc; } else { local_bh_disable(); nc = this_cpu_ptr(&napi_alloc_cache.page); - data = page_frag_alloc(nc, len, gfp_mask); + data = page_frag_alloc_va(nc, len, gfp_mask); pfmemalloc = nc->pfmemalloc; local_bh_enable(); } @@ -833,7 +834,7 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len) } else { len = SKB_HEAD_ALIGN(len); - data = page_frag_alloc(&nc->page, len, gfp_mask); + data = page_frag_alloc_va(&nc->page, len, gfp_mask); pfmemalloc = nc->page.pfmemalloc; } diff --git a/net/core/xdp.c b/net/core/xdp.c index 41693154e426..245a2d011aeb 100644 --- a/net/core/xdp.c +++ b/net/core/xdp.c @@ -391,7 +391,7 @@ void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, page_pool_put_full_page(page->pp, page, napi_direct); break; case MEM_TYPE_PAGE_SHARED: - page_frag_free(data); + page_frag_free_va(data); break; case MEM_TYPE_PAGE_ORDER0: page = virt_to_page(data); /* Assumes order0 page*/ diff --git a/net/rxrpc/txbuf.c b/net/rxrpc/txbuf.c index eb640875bf07..f2fa98360789 100644 --- a/net/rxrpc/txbuf.c +++ b/net/rxrpc/txbuf.c @@ -34,8 +34,8 @@ struct rxrpc_txbuf *rxrpc_alloc_data_txbuf(struct rxrpc_call *call, size_t data_ data_align = max_t(size_t, data_align, L1_CACHE_BYTES); mutex_lock(&call->conn->tx_data_alloc_lock); - buf = page_frag_alloc_align(&call->conn->tx_data_alloc, total, gfp, - data_align); + buf = page_frag_alloc_va_align(&call->conn->tx_data_alloc, total, gfp, + data_align); mutex_unlock(&call->conn->tx_data_alloc_lock); if (!buf) { kfree(txb); @@ -97,17 +97,18 @@ struct rxrpc_txbuf *rxrpc_alloc_ack_txbuf(struct rxrpc_call *call, size_t sack_s if (!txb) return NULL; - buf = page_frag_alloc(&call->local->tx_alloc, - sizeof(*whdr) + sizeof(*ack) + 1 + 3 + sizeof(*trailer), gfp); + buf = page_frag_alloc_va(&call->local->tx_alloc, + sizeof(*whdr) + sizeof(*ack) + 1 + 3 + sizeof(*trailer), gfp); if (!buf) { kfree(txb); return NULL; } if (sack_size) { - buf2 = page_frag_alloc(&call->local->tx_alloc, sack_size, gfp); + buf2 = page_frag_alloc_va(&call->local->tx_alloc, sack_size, + gfp); if (!buf2) { - page_frag_free(buf); + page_frag_free_va(buf); kfree(txb); return NULL; } @@ -181,7 +182,7 @@ static void rxrpc_free_txbuf(struct rxrpc_txbuf *txb) rxrpc_txbuf_free); for (i = 0; i < txb->nr_kvec; i++) if (txb->kvec[i].iov_base) - page_frag_free(txb->kvec[i].iov_base); + page_frag_free_va(txb->kvec[i].iov_base); kfree(txb); atomic_dec(&rxrpc_nr_txbuf); } diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c index 545017a3daa4..055ed38cef97 100644 --- a/net/sunrpc/svcsock.c +++ b/net/sunrpc/svcsock.c @@ -1231,8 +1231,8 @@ static int svc_tcp_sendmsg(struct svc_sock *svsk, struct svc_rqst *rqstp, /* The stream record marker is copied into a temporary page * fragment buffer so that it can be included in rq_bvec. */ - buf = page_frag_alloc(&svsk->sk_frag_cache, sizeof(marker), - GFP_KERNEL); + buf = page_frag_alloc_va(&svsk->sk_frag_cache, sizeof(marker), + GFP_KERNEL); if (!buf) return -ENOMEM; memcpy(buf, &marker, sizeof(marker)); From patchwork Sun Apr 7 13:08:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620167 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 82CFECD129A for ; Sun, 7 Apr 2024 13:11:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E2716B0093; Sun, 7 Apr 2024 09:11:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 946F76B0096; Sun, 7 Apr 2024 09:11:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80C896B0098; Sun, 7 Apr 2024 09:11:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 660796B0093 for ; Sun, 7 Apr 2024 09:11:17 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2CDBCC0ADF for ; Sun, 7 Apr 2024 13:11:17 +0000 (UTC) X-FDA: 81982771794.25.669368D Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf03.hostedemail.com (Postfix) with ESMTP id DEB7F20017 for ; Sun, 7 Apr 2024 13:11:13 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.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=1712495475; a=rsa-sha256; cv=none; b=jojuoYAouzoofT9NrcLW0F7vjK/q5cHOb87dQrzVb9+1V7Z/+ZLh3OobV76bExz0+5Taa0 8T9ArBz8gOJAxQjfNhRSzmffIFrfGryKIaIz2pN5w8KnQQsXn8Sleo72aiMwijCPaPZnfI Lew8ivTevAtE5GKnGbqEgN/wzvFAXDE= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.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=1712495475; 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=LAWRg82LuO3Rutlr6lQsMdGczkdPOra0hMaVHt1I+Uw=; b=Pz5ZGV/lxdjGqoZlnxJXwu7dcVFl0KVn5tcGj4mMiIYTC71qD7+yY1bPtiw30O6EazpaM3 wpT953Rk2upp2NM481NRF5s2s0qy+rE/K7P01yIkg92n0Ffp7/Y2olxSgTpGI9+7IXRset sEXCt5hGQ+4UJoeA5+hZXPksUg2hVXM= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VCCFf2sgGzXkvV; Sun, 7 Apr 2024 21:08:06 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 462851404F7; Sun, 7 Apr 2024 21:11:10 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:10 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Andrew Morton , Eric Dumazet , Subject: [PATCH net-next v1 05/12] mm: page_frag: add two inline helper for page_frag API Date: Sun, 7 Apr 2024 21:08:42 +0800 Message-ID: <20240407130850.19625-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: DEB7F20017 X-Stat-Signature: 4amwssw5sona8skmj54guhtsgy8dgqua X-Rspam-User: X-HE-Tag: 1712495473-633819 X-HE-Meta: U2FsdGVkX18ok7crDYIa1YrsrI1Km9ltOfpEgXc6tyiGKg+niUN47PG/bc00WzToRATTpviTLcWa6r8/TYl6rlGaLL/zFzHJPPxPjt4pVco9M631raGUr+lOpMak9jG7GBOyKUURaFRU+cdMZpOfyeYn59NtinXwtYt0qzJcGH4RobthGeEO7hDeZoYVkHz8pzcky6LcgAITcA8ZIhYdHdoxHawbxlfDcK1NeJd2xCb8x7vAZ3v6w2fpFPjqMkzfENZfqXEhNG0uRsU1tSLPli8M9KqLwMRX93Gg44bR7J1oqbenYjosVpYAi8hwyQGWPxvJ7UnutcH89ZcGWw3qFMyUTuYiPd2JWC35c+OMxKaPCL3ELNWxJSt3KuMrvbU9wNZeJthdku6WLIytsD3+sR13j9tMSn6xe2YVdv4E5/gqKOnkkRQzXHYSrs4XzEwmho0S2RQ7Gc37vCkPrIWWe/n0IuOTjROTjp/BqQmq5hSKw33JorDj21/TaZoA8mqcnHCXz0OMamIOEqMCd96IYYjR2rmmDzdD2izxTUZ9+Sgwh+jolvKfW+axFrSmUaoDz5Te7HUyml4UeEcpvB/7OPbQDhMcgSZy3ufhjtfM0/LG7ZdRiuOXCybe2UnfJMsD9jBh02PiBJWbcGpN+DV1ZafGnMZmYYpHcEyYaHlsrPoIL764NnHrgLOBsBrXvRp59JvChEaFy9pCQryvqY8DN4OMohbfSz0/gfygl92XN2d05+u9SU7e2LmuSYTbXREw96cBlLXnEUivYS9CHfui4EAMITT1NFgJuOirqR2feoBlEwImseG20+ebjQi7MCz6sKlqRaTVRqdLi7ejeGdDwERSqQRWN30FiH7cgf9rS+Gs2FgaWI+Sy2j3Ko7tYBEnUj7+AqrMiRmJ029dC78LrqENVfhzeLGYVfXuzDZSEZ14H2xI8qWF+j57GDkFxh8n3l1JpgzWA+Snnl6ihvU 7cko4sie wKn4Q1AUv9Ry1DcyhAs/+vXiTOm3eMG2pH3ppojR0fAf7IekIcRDtYFFdkmI6S8sUdqDV125hj3u9+TDn47Y/kXIvMfxPWHgz33t0YFImMwumh5Me9YbSs+7oar4WxeyNmgYx8ZDZMAH0Z5WRF7kU0hobHDP6kT1IiP1e+96k6z7ZUPcviG1/uZ3szud8KWBiE0vk 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: Add two inline helpers for page_frag API to avoid calling accessing the field of 'struct page_frag_cache'. Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 10 ++++++++++ net/core/skbuff.c | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 9d5d86b2d3ab..fe5faa80b6c3 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -23,6 +23,16 @@ struct page_frag_cache { bool pfmemalloc; }; +static inline void page_frag_cache_init(struct page_frag_cache *nc) +{ + nc->va = NULL; +} + +static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) +{ + return !!nc->pfmemalloc; +} + 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_va(struct page_frag_cache *nc, unsigned int fragsz, diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 2c10ebd133ac..4ad4db7403ba 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -742,12 +742,12 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, if (in_hardirq() || irqs_disabled()) { nc = this_cpu_ptr(&netdev_alloc_cache); data = page_frag_alloc_va(nc, len, gfp_mask); - pfmemalloc = nc->pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(nc); } else { local_bh_disable(); nc = this_cpu_ptr(&napi_alloc_cache.page); data = page_frag_alloc_va(nc, len, gfp_mask); - pfmemalloc = nc->pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(nc); local_bh_enable(); } From patchwork Sun Apr 7 13:08:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620168 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 02BD8CD128A for ; Sun, 7 Apr 2024 13:11:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43D826B0096; Sun, 7 Apr 2024 09:11:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C7E86B0098; Sun, 7 Apr 2024 09:11:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CBF16B0099; Sun, 7 Apr 2024 09:11:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EB5056B0096 for ; Sun, 7 Apr 2024 09:11:18 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A718D40B10 for ; Sun, 7 Apr 2024 13:11:18 +0000 (UTC) X-FDA: 81982771836.12.13F7044 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf23.hostedemail.com (Postfix) with ESMTP id 04622140009 for ; Sun, 7 Apr 2024 13:11:15 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712495476; 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=qN8xvUp/Pz6qhwsNImm1ke4lu/qTGOMKjtF+7ZY9FQE=; b=VwfPKNi2JSxAu3Hr4Tr4Tvs+UL9FkXYyynExTw4csq9s3eQMo7VPiJxUl3tjNrE6xrrG2a n6uNDSpYG3Q9lqhYTA/5AwLZPiBmBwFtm9i3Y3ZJizLJ4imz32P6UaHiJP7ltpR+97vxau gu4uAflvyEcRumxKpl3ukGz5WmM4gV4= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712495476; a=rsa-sha256; cv=none; b=fKRAvumrlXU21J987NEk+5GtFY9Zk9AJX6J4lmY1oG6i87ZKrX9tSUr9xCT5RnVgxl+Xd1 wPhNDKmATqRUkBgufkbq52x+CB0LhEvg2EiA3OkYBeYgw1CSAvLviElEOX4sUE5/JTNPeC e4acELnOla59yptKq+OqfnihDq7x6Qg= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4VCCGg1PZQzNn76; Sun, 7 Apr 2024 21:08:59 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 2B7ED1404F7; Sun, 7 Apr 2024 21:11:12 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:11 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Andrew Morton , Subject: [PATCH net-next v1 06/12] mm: page_frag: reuse MSB of 'size' field for pfmemalloc Date: Sun, 7 Apr 2024 21:08:43 +0800 Message-ID: <20240407130850.19625-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: 04622140009 X-Rspam-User: X-Stat-Signature: 4oy85abjt736jj74cg8mkaq11q5cknma X-Rspamd-Server: rspam01 X-HE-Tag: 1712495475-530509 X-HE-Meta: U2FsdGVkX18md98p+AbgwqP/FM4yDjfYfFc3apoDwRwEQeFOw0EvYmSDNbEeQvCtvccqWC21ae7qazDW53CKyRAa58vHZ1hatNLUyQ+jDoZIw/bZvA/HU4i15QN/zCKJwc8DEWckNCRmVNbWxjvtA/ukDgbMiFv5YwDsyfu8sJyTELzoJg6dNZqlTHkMZSQ5fup2RrneYXQTBAO+OfjDLI4tnxip1Orl9WKNajBxWFZNFXJdyNS/mSRGHNWjmH4f2GYUpZ1WBxA9q+JPJ+kBKgWe6mqUSuQCPqUvqd0RLct/lsoeLqn+28stOT3c54LKpPHPNCvHjl2gtJSiOOTwz7wLAGG9r+Tolt3FSajbNEtw+KEd6kxMlvFsKIG9fNCOkx4qkhSWvlVKMhzgoLIqpeTsZ6uHPcUfWr3N2EkBVwFaXj17N1wx2Hjo6c0VBDYTd4TJSO9CnEoTh/MKWB6pc2f0dWbFyN+kDDOf8jSLjHIZrVjeEj5k7MVRNRdaCg97O1CAK7XgztPG3phgmgUXbYZZkVgoJQ9wtH+QJI48soeLjVXZZu0KnjskL4ecJ6xoWZeDcdGwbHgvlzV5hVXBETKSxf/2fBq73z6XZ33kWFYICitwq40UbuJJHFsXccupXw17mN7TkrbIn0CJSPsMjbfx6+22LDp+PWzLlf1BDiHSnSgevTsJXBG8SLlO4E/u8gKoyQ+JCRlEoI2/WnrCsEn0LcGijAVViqijaWoHZJKsYhVhBpxeoGh6USaW9Abz1Ggy2ZFH6NvRh4b7AyxC05WjFtBbkQ+3vvAHg+XgkPB4uD/1wxXhb1WEfeHhKwbKFpy4dYHhzjEK9ce4SefLHEH6CRPuKh3cfwAUm9RCHsKcw6FiezFHT+KZtiYulF9Kr5nAXtr1qzitRIN/uqYpWU2/Vtm1MCmu6Qhn1xJ4Nd8U0SH15czSzDOYzksTMa4oirdOsjemuzxreKSx3ny AtvX1IZI BbDzYXf00Y4GgtUObIfyS9HcZmYmliRQ2fOn7eKH4TLSmbAOjA79ZW0hlQFwp/mGLEiQV2rxZkuKA5Ryp50uibXdb0ivatahu/5ew1Mm6z/KA/Gvb38cGVIoarALs1MKTCqTvRGgJZs4B6htpmNotAU2KoG2MYHJBAlJIVw/ScLG8RaHORx0Ze680BgM5rUM95nbZ 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: The '(PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)' case is for the system with page size less than 32KB, which is 0x8000 bytes requiring 16 bits space, change 'size' to 'size_mask' to avoid using the MSB, and change 'pfmemalloc' field to reuse the that MSB, so that we remove the orginal space needed by 'pfmemalloc'. For another case, the MSB of 'offset' is reused for 'pfmemalloc'. Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 13 ++++++++----- mm/page_frag_cache.c | 5 +++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index fe5faa80b6c3..40a7d6da9ef0 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -12,15 +12,16 @@ struct page_frag_cache { void *va; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) __u16 offset; - __u16 size; + __u16 size_mask:15; + __u16 pfmemalloc:1; #else - __u32 offset; + __u32 offset:31; + __u32 pfmemalloc:1; #endif /* we maintain a pagecount bias, so that we dont dirty cache line * containing page->_refcount every time we allocate a fragment. */ unsigned int pagecnt_bias; - bool pfmemalloc; }; static inline void page_frag_cache_init(struct page_frag_cache *nc) @@ -43,7 +44,9 @@ static inline void *__page_frag_alloc_va_align(struct page_frag_cache *nc, gfp_t gfp_mask, unsigned int align) { - nc->offset = ALIGN(nc->offset, align); + unsigned int offset = nc->offset; + + nc->offset = ALIGN(offset, align); return page_frag_alloc_va(nc, fragsz, gfp_mask); } @@ -53,7 +56,7 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, gfp_t gfp_mask, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align >= PAGE_SIZE); return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 7f639af4e518..a02e57a439f0 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -29,7 +29,8 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER); - nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; + nc->size_mask = page ? PAGE_FRAG_CACHE_MAX_SIZE - 1 : PAGE_SIZE - 1; + VM_BUG_ON(page && nc->size_mask != PAGE_FRAG_CACHE_MAX_SIZE - 1); #endif if (unlikely(!page)) page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); @@ -88,7 +89,7 @@ void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; + size = nc->size_mask + 1; #else size = PAGE_SIZE; #endif From patchwork Sun Apr 7 13:08: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: 13620169 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 2CBCECD1296 for ; Sun, 7 Apr 2024 13:11:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 268076B0099; Sun, 7 Apr 2024 09:11:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2198F6B009B; Sun, 7 Apr 2024 09:11:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0939D6B009C; Sun, 7 Apr 2024 09:11:21 -0400 (EDT) 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 DAE316B0099 for ; Sun, 7 Apr 2024 09:11:21 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A515D160875 for ; Sun, 7 Apr 2024 13:11:21 +0000 (UTC) X-FDA: 81982771962.12.CE6AAB5 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf26.hostedemail.com (Postfix) with ESMTP id D4394140014 for ; Sun, 7 Apr 2024 13:11:18 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; spf=pass (imf26.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712495479; 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=+Z1sNexvcfsuU49xW1KJHXwKoxZNEqa6JavsGGMquZ0=; b=rrw2cq+zh0lpiyBeBYoGfj9RjuA4VMvpK0h9QfuQUsBCvQwIQZpQ8IIWmPGjYZ1QKp1CIJ zz5VPj6S/H5Br7wVhN9AfDb/0k4n7hqB9p2yAM3+QkH8Js0pd6P+b4T06hFaKOT6qfyg7v Wr3BVx91HSWTZga1Dpi6IFT/BBkYaQg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712495479; a=rsa-sha256; cv=none; b=Hqo1PClP3NHca8amjOOl2nia/K2Ec+KwpgldeM2NulrT6hnA9z2Uj35aiqRa6sxA/q8JT9 EaKC3QlK0co1uKPH6+18lX1Ces6DUhIkXD4KOz0sXnusTrs4brwRylynvj3ZTq/qsiQW4C 5eaEAvCh5fMzT9kl6BGi2AuqMYvCqXQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; spf=pass (imf26.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4VCCJR5XMdz1GCqF; Sun, 7 Apr 2024 21:10:31 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id B1C0E1A0188; Sun, 7 Apr 2024 21:11:14 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:14 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Andrew Morton , Subject: [PATCH net-next v1 07/12] mm: page_frag: reuse existing bit field of 'va' for pagecnt_bias Date: Sun, 7 Apr 2024 21:08:44 +0800 Message-ID: <20240407130850.19625-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: D4394140014 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 81x86uf59e6uwh39xshnwtbkpzd6ua4a X-HE-Tag: 1712495478-242462 X-HE-Meta: U2FsdGVkX18ozuN4GxKH88/vLPeEoc4bvvcQs1J3ZcI+NVyLf7wSC2MEuFiHp+I+6aW1tGRsRRZKG8R9CpaM/eSH4P04iKcs6XpnoTSXlyp/6jxg6lZt9bbsNpPoV83AH3Et0mTLQgUUo/AmMmlQ49sJY6dGumTGTVxzxkmleWO6s1Swuw8LJKCAaz+cS1W7Ae5zOPShvz/5tjvfOl6yMCcDKrnMZtdDU4Lj0rYrCSWzz53f0BDkbc6ZnEN+Xpb/GkujsMlu2C+kElg6prUWR0FmpQaidSG9PvsL0Ag2EsUBwSbWy703rOWxlQakB9jRyuKUb1yAldc2X5W4F5kaJa2INVbUlnNzHBfVdlrUEASNNf2BCRL8dDL5UAjoIQtLWCs+heBW7AlTwTUyVmgh3pD33HC65C8BrZCPEoHAOM/EM19eAKV/dGSM6xOaISM/5VkwFKTIwA1JXVotA6nx/de5OzVUgSpYn2yeCaVVDemZuQOJbvAjnpLIIkF3LkFdG3gp8ys383hZyOsiWKrGuDrCVmpVoo5rXxQ4m/67QnZUAW26WgYyLetEC3HMS89obvntkqjrdnX86JkTvZT/2nFqPKFju9wlDXlbT+BnXNaocpsQH2TQIZ9VceeMSrDsHz9nL3KbSWHIN1Ow9+2iIHKZN/QYu4cCmUy8XYyhf/SbF5j6ktgk9/gt/6MPUV7pZoG2IaWmSOIPmX0JhovRKGqgvqJuUIimqBp0rbUbjlgU3bi6glyq2dYo0GO5ky+9fUihekksysg4RM3kMwJPkSFUPMP/YonG9Bg2LrOlOgo8LVf3OujS1V5zmm3gFMfJu2aSd4zHil2KO+Q1yziLYETs68/Pa8ChtLGYEXeVjzlNo8vQh1FpqQC3L7MNi7ro9xu5DCN75aXo1EDzEuSu5D5x/uUvhNFqkv6EvBAyGEs2ryb+GAw97E+tT2F1btOeACwdHucourjswrN5oeh UEQC+4MA smC6IHVf39mO9MDFWWk0ibjYJ59/JtieSDfYNzWKEqghWIDjSSHdOMydWFlP7OGyrp2idzRTxXTs7n2UyJiIsUjNwgOEYMh98tp0MifuRCgaKq/2v5fAYIthqcW1FKpJNygjm8HMmmr8cjRvuNPd2+IOdwlbmL61MAMdA/6V/JRO9rHSI6sIMuMipTt1jBr/UwuIN 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: As alignment of 'va' is always aligned with the order of the page allocated, we can reuse the LSB bits for the pagecount bias, and remove the orginal space needed by 'pagecnt_bias'. Also limit the 'fragsz' to be at least the size of 'usigned int' to match the limited pagecnt_bias. Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 20 +++++++---- mm/page_frag_cache.c | 63 +++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 40a7d6da9ef0..a97a1ac017d6 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -9,7 +9,18 @@ #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) struct page_frag_cache { - void *va; + union { + void *va; + /* we maintain a pagecount bias, so that we dont dirty cache + * line containing page->_refcount every time we allocate a + * fragment. As 'va' is always aligned with the order of the + * page allocated, we can reuse the LSB bits for the pagecount + * bias, and its bit width happens to be indicated by the + * 'size_mask' below. + */ + unsigned long pagecnt_bias; + + }; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) __u16 offset; __u16 size_mask:15; @@ -18,10 +29,6 @@ struct page_frag_cache { __u32 offset:31; __u32 pfmemalloc:1; #endif - /* we maintain a pagecount bias, so that we dont dirty cache line - * containing page->_refcount every time we allocate a fragment. - */ - unsigned int pagecnt_bias; }; static inline void page_frag_cache_init(struct page_frag_cache *nc) @@ -56,7 +63,8 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, gfp_t gfp_mask, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align) || align >= PAGE_SIZE); + WARN_ON_ONCE(!is_power_of_2(align) || align >= PAGE_SIZE || + fragsz < sizeof(unsigned int)); return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index a02e57a439f0..ae1393d0619a 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -18,8 +18,8 @@ #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_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) { struct page *page = NULL; gfp_t gfp = gfp_mask; @@ -35,9 +35,26 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, if (unlikely(!page)) page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); - nc->va = page ? page_address(page) : NULL; + if (unlikely(!page)) { + nc->va = NULL; + return false; + } + + nc->va = page_address(page); - return page; +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + VM_BUG_ON(nc->pagecnt_bias & nc->size_mask); + page_ref_add(page, nc->size_mask - 1); + nc->pagecnt_bias |= nc->size_mask; +#else + VM_BUG_ON(nc->pagecnt_bias & (PAGE_SIZE - 1)); + page_ref_add(page, PAGE_SIZE - 2); + nc->pagecnt_bias |= (PAGE_SIZE - 1); +#endif + + nc->pfmemalloc = page_is_pfmemalloc(page); + nc->offset = 0; + return true; } void page_frag_cache_drain(struct page_frag_cache *nc) @@ -67,38 +84,31 @@ EXPORT_SYMBOL(__page_frag_cache_drain); void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask) { - unsigned int size, offset; + unsigned long size_mask; + unsigned int offset; struct page *page; + void *va; if (unlikely(!nc->va)) { refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) + if (!__page_frag_cache_refill(nc, gfp_mask)) return NULL; - - /* 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 offset to start of new frag */ - nc->pfmemalloc = page_is_pfmemalloc(page); - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = 0; } #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size_mask + 1; + size_mask = nc->size_mask; #else - size = PAGE_SIZE; + size_mask = PAGE_SIZE - 1; #endif + va = (void *)((unsigned long)nc->va & ~size_mask); offset = nc->offset; - if (unlikely(offset + fragsz > size)) { - page = virt_to_page(nc->va); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + if (unlikely(offset + fragsz > (size_mask + 1))) { + page = virt_to_page(va); + + if (!page_ref_sub_and_test(page, nc->pagecnt_bias & size_mask)) goto refill; if (unlikely(nc->pfmemalloc)) { @@ -107,12 +117,11 @@ void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, } /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + set_page_count(page, size_mask); + nc->pagecnt_bias |= size_mask; - /* reset page count bias and offset to start of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; offset = 0; - if (unlikely(fragsz > size)) { + if (unlikely(fragsz > (size_mask + 1))) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -129,7 +138,7 @@ void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, nc->pagecnt_bias--; nc->offset = offset + fragsz; - return nc->va + offset; + return va + offset; } EXPORT_SYMBOL(page_frag_alloc_va); From patchwork Sun Apr 7 13:08:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620170 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 84CA4CD129A for ; Sun, 7 Apr 2024 13:11:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D65B56B009C; Sun, 7 Apr 2024 09:11:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D13EE6B009D; Sun, 7 Apr 2024 09:11:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB6786B009E; Sun, 7 Apr 2024 09:11:24 -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 9E5E36B009C for ; Sun, 7 Apr 2024 09:11:24 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6F9431608A0 for ; Sun, 7 Apr 2024 13:11:24 +0000 (UTC) X-FDA: 81982772088.18.DAAED07 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf11.hostedemail.com (Postfix) with ESMTP id E332240007 for ; Sun, 7 Apr 2024 13:11:21 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.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=1712495482; 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=CAHPxwty7eLu8Z1l4SI6yGL3kVGtu+SoL+tGqBKsr0M=; b=GJJY5YDr3Vn4YFa7LVgOaxL9jOFAiRSxDxYeipQn2+MKD5vorCypQfNhnL5nNwYDNm4s7S NGBjRiwfCDRakxbn3hBH/O7fFQGWxKbSLJRgPtkEbZNGeHedgx8GJkTrcyA6b0DacyT+QE IFq9u67d7KeUeye1w7nyGPB+BTfjuLc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.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=1712495482; a=rsa-sha256; cv=none; b=Mt5M+EGMm5ZZvVUC1qhUmkZYkW17NNd21hHZKNBrt4JeQCOCrnCqAMquWMUMRMTE5YSslS lH28BbGGtOMxld7d3Kuf/5Ls/M1pRoKRtgdHrmfJ4fecF8RacOc2haNSADIXPVxqFHWtJu l4qPQHnUNmb5BfqrWACylLymmOIuxGE= Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4VCCJW4f0Xz1GGCR; Sun, 7 Apr 2024 21:10:35 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 9352D18005F; Sun, 7 Apr 2024 21:11:18 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:18 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Andrew Morton , Subject: [PATCH net-next v1 09/12] mm: page_frag: introduce prepare/commit API for page_frag Date: Sun, 7 Apr 2024 21:08:46 +0800 Message-ID: <20240407130850.19625-10-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Stat-Signature: a4f8ri34pgcwje6r9qdwts7qoqag46o3 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E332240007 X-HE-Tag: 1712495481-18535 X-HE-Meta: U2FsdGVkX1/DEwuh9G/Si9vmuUxKf2bwfx+ntqtOlkbcTRON+p4HN9opBftWmTD0fnch6hQ8yL6diZNGKyIOu6kTISh8Xs34+Sp5gkVvEZQlNbxyGpVOzpwL0hzRBri5tpnvJqz3ja8vN/ZjjkaMWMuy8b9T4INAbBTg1GMmC55jvCrQMKugbFNndlsZrkgnAc31YdBwmEaz66CNiBX4SJb4viXiVG6Ze5i7Ed8q9sRi4E7iVH5RT872OEkceWZZjqgllufKlSgleomxQgI27sU+ceVkYybTW8/GRUngZTZ8K3zip3kgs+pwpZjX+bt3Qlqoov3RwfBO2uHMIfcqyGNd9h0hMj1u1JJXE3zVYoz4qbJ2H+woZc5Vd0goZ+/DAFKIvv9dCdFtkSltvOBUH2uEFWen0NjSin6dlIqzP80bQVlNpaHYjl+TAaJGn1VAe1hR0XqnZ5UpKSGwjAcSZmPlzfqNeWNV78CnVgWaLZUL9zYjiFODIlPnhdgq16E8k7mI1Bx96+gANth7ZASaJNwICKGSLcxx9Cpt1MlAwWx65jdPhP8b4CKX7fipto/2lO+ZPB4j0YQ+XY11EGGhbUAwSMWROEtVuirwmLDo8ir/h8pSs4AO/WzrZKW9vnjxMgr9cIzo5lbFM/e0aDbmNjXeONamtBhVVedZTnpKK1IzTpBjNd+cFLyVevItpBwI18bIAruPCI1XizdJDhO7sg8Wkq69aX61SJ4HNS1R2n+zeaT6fozbbZk1ZMPsu1EL9AiiwmDWjQv5paFOfFQI9Y0BadYGYX93pXycnHUuzbpqZmniwuTPr84VI1y7MNav0fxhYvF9aRuwvJ08qUaXHtwkL4JpmgDzg4MlzHjIBVbMT02+gwXzXN1fHbmQnshixS0ZvQQXwpP5+onURmxqLJEKpc5O/eVahH8SpUa8h4qrCdVnx0MnUzwxZkdvJySm5qPZPlyn5SUt6iR06N9 4RJ+w7f/ dMXZNXT2qE2rNz9x3bcsTZLNCXWBTJ7HstrCERXQ+5b9yd7iLIs1ES7lF4OwjNf6M2mlaK24OVqJgwvfPS9QQLuMChzAuOz1UgPhQUBkq7PTiMOwwbtMixKjiP0AcTvx8UIuwctF5FuPiDAEks7C7dKhdpgwxS62bSfRmjElEXS05jhHcXDmCqoII6JWv+6EIRUv7x/pcemQ8SdsOr2P+0tr5Rg/pAp5fzLqQyEatnzfMo87EvN5lwtsfZQ== 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: There are many use cases that need minimum memory in order for forward progressing, but can do better if there is more memory available. Currently skb_page_frag_refill() API is used to solve the above usecases, as mentioned in [1], its implementation is similar to the one in mm subsystem. To unify those two page_frag implementations, introduce a prepare API to ensure minimum memory is satisfied and return how much the actual memory is available to the caller. And the caller can decide how much memory to use by calling commit API, or not calling the commit API if deciding to not use any memory. Note it seems hard to decide which header files for caling virt_to_page() in the inline helper, so macro is used instead of inline helper to avoid dealing with that. 1. https://lore.kernel.org/all/20240228093013.8263-1-linyunsheng@huawei.com/ Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 141 +++++++++++++++++++++++++++++++- mm/page_frag_cache.c | 13 ++- 2 files changed, 144 insertions(+), 10 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index a97a1ac017d6..28185969cd2c 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -43,8 +43,25 @@ 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_va(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask); +void *page_frag_cache_refill(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask); + +static inline void *page_frag_alloc_va(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask) +{ + unsigned int offset; + void *va; + + va = page_frag_cache_refill(nc, fragsz, gfp_mask); + if (unlikely(!va)) + return NULL; + + offset = nc->offset; + nc->pagecnt_bias--; + nc->offset = offset + fragsz; + + return va + offset; +} static inline void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, @@ -69,6 +86,126 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } +static inline void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *size, + gfp_t gfp_mask) +{ + void *va; + + va = page_frag_cache_refill(nc, *size, gfp_mask); + if (unlikely(!va)) + return NULL; + + *offset = nc->offset; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + *size = nc->size_mask - *offset + 1; +#else + *size = PAGE_SIZE - *offset; +#endif + + return va + *offset; +} + +static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *size, + unsigned int align, + gfp_t gfp_mask) +{ + WARN_ON_ONCE(!is_power_of_2(align) || align >= PAGE_SIZE || + *size < sizeof(unsigned int)); + + *offset = nc->offset; + nc->offset = ALIGN(*offset, align); + return page_frag_alloc_va_prepare(nc, offset, size, gfp_mask); +} + +static inline void *__page_frag_alloc_pg_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *size, + gfp_t gfp_mask) +{ + void *va; + + va = page_frag_cache_refill(nc, *size, gfp_mask); + if (unlikely(!va)) + return NULL; + + *offset = nc->offset; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + *size = nc->size_mask - *offset + 1; +#else + *size = PAGE_SIZE - *offset; +#endif + + return va; +} + +#define page_frag_alloc_pg_prepare(nc, offset, size, gfp) \ +({ \ + struct page *__page = NULL; \ + void *__va; \ + \ + __va = __page_frag_alloc_pg_prepare(nc, offset, size, gfp); \ + if (likely(__va)) \ + __page = virt_to_page(__va); \ + \ + __page; \ +}) + +static inline void *__page_frag_alloc_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *size, + void **va, gfp_t gfp_mask) +{ + void *nc_va; + + nc_va = page_frag_cache_refill(nc, *size, gfp_mask); + if (unlikely(!nc_va)) + return NULL; + + *offset = nc->offset; + *va = nc_va + *offset; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + *size = nc->size_mask - *offset + 1; +#else + *size = PAGE_SIZE - *offset; +#endif + + return nc_va; +} + +#define page_frag_alloc_prepare(nc, offset, size, va, gfp) \ +({ \ + struct page *__page = NULL; \ + void *__va; \ + \ + __va = __page_frag_alloc_prepare(nc, offset, size, va, gfp); \ + if (likely(__va)) \ + __page = virt_to_page(__va); \ + \ + __page; \ +}) + +static inline void page_frag_alloc_commit(struct page_frag_cache *nc, + unsigned int offset, + unsigned int size) +{ + nc->pagecnt_bias--; + nc->offset = offset + size; +} + +static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, + unsigned int offset, + unsigned int size) +{ + nc->offset = offset + size; +} + void page_frag_free_va(void *addr); #endif diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index ae1393d0619a..cbd0ed82a596 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -81,8 +81,8 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); -void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask) +void *page_frag_cache_refill(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask) { unsigned long size_mask; unsigned int offset; @@ -120,7 +120,7 @@ void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, set_page_count(page, size_mask); nc->pagecnt_bias |= size_mask; - offset = 0; + nc->offset = 0; if (unlikely(fragsz > (size_mask + 1))) { /* * The caller is trying to allocate a fragment @@ -135,12 +135,9 @@ void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, } } - nc->pagecnt_bias--; - nc->offset = offset + fragsz; - - return va + offset; + return va; } -EXPORT_SYMBOL(page_frag_alloc_va); +EXPORT_SYMBOL(page_frag_cache_refill); /* * Frees a page fragment allocated out of either a compound or order 0 page. From patchwork Sun Apr 7 13:08:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620171 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 C7E5FCD11C2 for ; Sun, 7 Apr 2024 13:11:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5EA016B009E; Sun, 7 Apr 2024 09:11:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 572A26B009F; Sun, 7 Apr 2024 09:11:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43A686B00A0; Sun, 7 Apr 2024 09:11:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 26A706B009E for ; Sun, 7 Apr 2024 09:11:33 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E5D89C0AD3 for ; Sun, 7 Apr 2024 13:11:32 +0000 (UTC) X-FDA: 81982772424.01.BB09176 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf03.hostedemail.com (Postfix) with ESMTP id 6F9A020012 for ; Sun, 7 Apr 2024 13:11:29 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.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=1712495491; 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=li2k89W2YA223dg/YDNZv2vgDgEeU+CpLNgHU6ZpwWQ=; b=Xy2gx3v5ptPx53ZAzC5lTjiRCVz3enYDvHzm01oY6R2c7yprFj2qAdZn24e2pWo+Lfj8qj nS0OUKsb1ExKGGX8eTVtxP6X/e05Hw3Qds96U5LInVEwrQ7RU7yrF807rJHaL8dwfvx1PT /rLH3uDpUds5lB1+1XBHH98QpFlFqWk= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.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=1712495491; a=rsa-sha256; cv=none; b=S8NxQegZWaq8IrnNSgLwZ1u2Ps4w9sSPRi2O9A0TUUPN+o4Wx+uBd0cSNQrSWoZkPuR3UC uOH0g9mjFOMfzsRt3EIhZgihJ7GCib4mBspzwQZrHiw7xvY/041T+AxYMUj+an7ahGMU6p qj5Zo9X2H03wFu6Kf9zaEtrvdp1dJ6E= Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4VCCJh3KRmz1GG52; Sun, 7 Apr 2024 21:10:44 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 661961A0172; Sun, 7 Apr 2024 21:11:27 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:27 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Andrew Morton , Subject: [PATCH net-next v1 11/12] mm: page_frag: add a test module for page_frag Date: Sun, 7 Apr 2024 21:08:48 +0800 Message-ID: <20240407130850.19625-12-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 6F9A020012 X-Stat-Signature: m1975yfyq55pgjw9x5akdqz4akk65stj X-HE-Tag: 1712495489-908871 X-HE-Meta: U2FsdGVkX18EUP0vkzwj5Yh5W8eQBL6Kgf94lV/UpaEeKA7SfyTvygeob9bvpJn6snZLlutop9i0lPNsdsiS5MC2RMbR7TnlzS3EU+oAom7Bl0WNp3CIWVHbtQ2Vpo8c1B/8mW5mGxUEGgGmuCJTSlnlnXzwDfcarK58zYDQJFi/mNJRXD1MBak34SSwuhBz2SUzRYg8FG2UsX3wuOzTA0mTZqB8eHGJ7bfpncPVWjaULu+ncBkKHCc8j06sPP4QyHSQZNcKaiiuUJotKz2BvgFusIqJr3WGaj9AVq4F5ngr3el0UKgLJJ8viENAklt49/TCBhqCGaDf+DKzQLZxPHFmUjOrr+Urm3l6RnZQtZfpd9g0vV2AIHwSM5bbT7BBzO4mawaT62FSoe1xG42cDlHoFD7PrfF2UMU4osvSN+Hx/HlwMhS2y4YCMsuoB+FHZ6qtRSrxKPtZ2jfeTT+Y28xK5eFVrHXN6QPeudGKenTviy5p+DQ2brS1dGgO5hO4o9Nrz91+RgW/6wwNtyOI43Df0vUK93DhlqmBcMbr6yW+c2OC09ZIxO6VVkcKT4v6GyoQH3kyoFB1+2pBXBHVTms/fEOzZO1e0Z1XMyxLPW2jKBq1L/vQo+Wd3zWYPCwtFuwmn10QDn8lo6Ys0amEejlrdLTRwmHzIkKj2Sxbx1hYr9fwMmJjMIyscvV60cbun1n2KJvCskp+d6JwktFX0M7lVhUlK2v1E5j1kcbAFH6whQiTcCFlZL08IVj6IMyIijzbgVdzeCqCcwKA4Mtl4LB9EirvS6SIgwS9ajwm/k7y/Cll5jgKZ+jdmiaK/4Unf1K4jTtuvuXaVbyP6/Noo4S8m0F2NpY8hUJ5jXrU1nzPNdMfiBvCgfoMqpVggyuozZUCDqvTbt5p5lHxswFeAsJwCIfMDOC45Rvo2wgCVlYMdNynfIAPNRFKR8kmSdN1zWAfEi1y7XATpyjs+Tp IzlUUqt1 rnYwFWurr+px93d+WnY1VMlEfQ+5HSv+3tvbyb2nz1apDw7evXRonZQFiY+KP69vYldrX9V+FPdamgP4MGAHmwR5HE7Hdrq0SPc92yA932KBAwSkgMGTiUdhTcU3hELSZCtmsm8Z+3H7aQeKQU3h/tu1+H6hZIJYZ+sJR5oTVW5WUrDnnJ4/dmFPfgU+vVz188xFhmxfFBM/SJUvItHHSdrMFPqqvLRZqW0nP 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: Basing on the lib/objpool.c, change it to something like a ptrpool, so that we can utilize that to test the correctness and performance of the page_frag. The testing is done by ensuring that the fragments allocated from a frag_frag_cache instance is pushed into a ptrpool instance in a kthread binded to the first cpu, and a kthread binded to the current node will pop the fragmemt from the ptrpool and call page_frag_alloc_va() to free the fragmemt. We may refactor out the common part between objpool and ptrpool if this ptrpool thing turns out to be helpful for other place. Signed-off-by: Yunsheng Lin --- mm/Kconfig.debug | 8 + mm/Makefile | 1 + mm/page_frag_test.c | 366 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 375 insertions(+) create mode 100644 mm/page_frag_test.c diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index afc72fde0f03..1ebcd45f47d4 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -142,6 +142,14 @@ config DEBUG_PAGE_REF kernel code. However the runtime performance overhead is virtually nil until the tracepoints are actually enabled. +config DEBUG_PAGE_FRAG_TEST + tristate "Test module for page_frag" + default n + depends on m && DEBUG_KERNEL + help + This builds the "page_frag_test" module that is used to test the + correctness and performance of page_frag's implementation. + config DEBUG_RODATA_TEST bool "Testcase for the marking rodata read-only" depends on STRICT_KERNEL_RWX diff --git a/mm/Makefile b/mm/Makefile index 146c481c006f..8b62f5de48a7 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -102,6 +102,7 @@ obj-$(CONFIG_MEMORY_FAILURE) += memory-failure.o obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o obj-$(CONFIG_DEBUG_RODATA_TEST) += rodata_test.o +obj-$(CONFIG_DEBUG_PAGE_FRAG_TEST) += page_frag_test.o obj-$(CONFIG_DEBUG_VM_PGTABLE) += debug_vm_pgtable.o obj-$(CONFIG_PAGE_OWNER) += page_owner.o obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c new file mode 100644 index 000000000000..e311c2e7ff49 --- /dev/null +++ b/mm/page_frag_test.c @@ -0,0 +1,366 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Test module for page_frag cache + * + * Copyright: linyunsheng@huawei.com + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OBJPOOL_NR_OBJECT_MAX BIT(24) + +struct objpool_slot { + u32 head; + u32 tail; + u32 last; + u32 mask; + void *entries[]; +} __packed; + +struct objpool_head { + int nr_cpus; + int capacity; + struct objpool_slot **cpu_slots; +}; + +/* initialize percpu objpool_slot */ +static void objpool_init_percpu_slot(struct objpool_head *pool, + struct objpool_slot *slot) +{ + /* initialize elements of percpu objpool_slot */ + slot->mask = pool->capacity - 1; +} + +/* allocate and initialize percpu slots */ +static int objpool_init_percpu_slots(struct objpool_head *pool, + int nr_objs, gfp_t gfp) +{ + int i; + + for (i = 0; i < pool->nr_cpus; i++) { + struct objpool_slot *slot; + int size; + + /* skip the cpu node which could never be present */ + if (!cpu_possible(i)) + continue; + + size = struct_size(slot, entries, pool->capacity); + + /* + * here we allocate percpu-slot & objs together in a single + * allocation to make it more compact, taking advantage of + * warm caches and TLB hits. in default vmalloc is used to + * reduce the pressure of kernel slab system. as we know, + * mimimal size of vmalloc is one page since vmalloc would + * always align the requested size to page size + */ + if (gfp & GFP_ATOMIC) + slot = kmalloc_node(size, gfp, cpu_to_node(i)); + else + slot = __vmalloc_node(size, sizeof(void *), gfp, + cpu_to_node(i), + __builtin_return_address(0)); + if (!slot) + return -ENOMEM; + + memset(slot, 0, size); + pool->cpu_slots[i] = slot; + + objpool_init_percpu_slot(pool, slot); + } + + return 0; +} + +/* cleanup all percpu slots of the object pool */ +static void objpool_fini_percpu_slots(struct objpool_head *pool) +{ + int i; + + if (!pool->cpu_slots) + return; + + for (i = 0; i < pool->nr_cpus; i++) + kvfree(pool->cpu_slots[i]); + kfree(pool->cpu_slots); +} + +/* initialize object pool and pre-allocate objects */ +static int objpool_init(struct objpool_head *pool, int nr_objs, gfp_t gfp) +{ + int rc, capacity, slot_size; + + /* check input parameters */ + if (nr_objs <= 0 || nr_objs > OBJPOOL_NR_OBJECT_MAX) + return -EINVAL; + + /* calculate capacity of percpu objpool_slot */ + capacity = roundup_pow_of_two(nr_objs); + if (!capacity) + return -EINVAL; + + gfp = gfp & ~__GFP_ZERO; + + /* initialize objpool pool */ + memset(pool, 0, sizeof(struct objpool_head)); + pool->nr_cpus = nr_cpu_ids; + pool->capacity = capacity; + slot_size = pool->nr_cpus * sizeof(struct objpool_slot *); + pool->cpu_slots = kzalloc(slot_size, gfp); + if (!pool->cpu_slots) + return -ENOMEM; + + /* initialize per-cpu slots */ + rc = objpool_init_percpu_slots(pool, nr_objs, gfp); + if (rc) + objpool_fini_percpu_slots(pool); + + return rc; +} + +/* adding object to slot, abort if the slot was already full */ +static int objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu) +{ + struct objpool_slot *slot = pool->cpu_slots[cpu]; + u32 head, tail; + + /* loading tail and head as a local snapshot, tail first */ + tail = READ_ONCE(slot->tail); + + do { + head = READ_ONCE(slot->head); + /* slot is full */ + if (unlikely(tail - head >= pool->capacity)) + return -ENOSPC; + } while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1)); + + /* now the tail position is reserved for the given obj */ + WRITE_ONCE(slot->entries[tail & slot->mask], obj); + /* update sequence to make this obj available for pop() */ + smp_store_release(&slot->last, tail + 1); + + return 0; +} + +/* reclaim an object to object pool */ +static int objpool_push(void *obj, struct objpool_head *pool) +{ + unsigned long flags; + int rc; + + /* disable local irq to avoid preemption & interruption */ + raw_local_irq_save(flags); + rc = objpool_try_add_slot(obj, pool, raw_smp_processor_id()); + raw_local_irq_restore(flags); + + return rc; +} + +/* try to retrieve object from slot */ +static void *objpool_try_get_slot(struct objpool_head *pool, int cpu) +{ + struct objpool_slot *slot = pool->cpu_slots[cpu]; + /* load head snapshot, other cpus may change it */ + u32 head = smp_load_acquire(&slot->head); + + while (head != READ_ONCE(slot->last)) { + void *obj; + + /* + * data visibility of 'last' and 'head' could be out of + * order since memory updating of 'last' and 'head' are + * performed in push() and pop() independently + * + * before any retrieving attempts, pop() must guarantee + * 'last' is behind 'head', that is to say, there must + * be available objects in slot, which could be ensured + * by condition 'last != head && last - head <= nr_objs' + * that is equivalent to 'last - head - 1 < nr_objs' as + * 'last' and 'head' are both unsigned int32 + */ + if (READ_ONCE(slot->last) - head - 1 >= pool->capacity) { + head = READ_ONCE(slot->head); + continue; + } + + /* obj must be retrieved before moving forward head */ + obj = READ_ONCE(slot->entries[head & slot->mask]); + + /* move head forward to mark it's consumption */ + if (try_cmpxchg_release(&slot->head, &head, head + 1)) + return obj; + } + + return NULL; +} + +/* allocate an object from object pool */ +static void *objpool_pop(struct objpool_head *pool) +{ + void *obj = NULL; + unsigned long flags; + int i, cpu; + + /* disable local irq to avoid preemption & interruption */ + raw_local_irq_save(flags); + + cpu = raw_smp_processor_id(); + for (i = 0; i < num_possible_cpus(); i++) { + obj = objpool_try_get_slot(pool, cpu); + if (obj) + break; + cpu = cpumask_next_wrap(cpu, cpu_possible_mask, -1, 1); + } + raw_local_irq_restore(flags); + + return obj; +} + +/* release whole objpool forcely */ +static void objpool_free(struct objpool_head *pool) +{ + if (!pool->cpu_slots) + return; + + /* release percpu slots */ + objpool_fini_percpu_slots(pool); +} + +static struct objpool_head ptr_pool; +static int nr_objs = 512; +static int nr_test = 5120000; +static atomic_t nthreads; +static struct completion wait; +struct page_frag_cache test_frag; + +module_param(nr_test, int, 0600); +MODULE_PARM_DESC(nr_test, "number of iterations to test"); + +static int page_frag_pop_thread(void *arg) +{ + struct objpool_head *pool = arg; + int nr = nr_test; + + pr_info("page_frag pop test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + void *obj = objpool_pop(pool); + + if (obj) { + nr--; + page_frag_free_va(obj); + } else { + cond_resched(); + } + } + + if (atomic_dec_and_test(&nthreads)) + complete(&wait); + + pr_info("page_frag pop test thread exits on cpu %d\n", + smp_processor_id()); + + return 0; +} + +static int page_frag_push_thread(void *arg) +{ + struct objpool_head *pool = arg; + int nr = nr_test; + + pr_info("page_frag push test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + unsigned int size = get_random_u32(); + void *va; + int ret; + + size = clamp(size, sizeof(unsigned int), PAGE_SIZE); + va = page_frag_alloc_va(&test_frag, size, GFP_KERNEL); + if (!va) + continue; + + ret = objpool_push(va, pool); + if (ret) { + page_frag_free_va(va); + cond_resched(); + } else { + nr--; + } + } + + pr_info("page_frag push test thread exits on cpu %d\n", + smp_processor_id()); + + if (atomic_dec_and_test(&nthreads)) + complete(&wait); + + return 0; +} + +static int __init page_frag_test_init(void) +{ + struct task_struct *tsk_push, *tsk_pop; + ktime_t start; + u64 duration; + int ret; + + page_frag_cache_init(&test_frag); + atomic_set(&nthreads, 2); + init_completion(&wait); + + ret = objpool_init(&ptr_pool, nr_objs, GFP_KERNEL); + if (ret) + return ret; + + tsk_push = kthread_create_on_cpu(page_frag_push_thread, &ptr_pool, + cpumask_first(cpu_online_mask), + "page_frag_push"); + if (IS_ERR(tsk_push)) + return PTR_ERR(tsk_push); + + tsk_pop = kthread_create(page_frag_pop_thread, &ptr_pool, + "page_frag_pop"); + if (IS_ERR(tsk_pop)) { + kthread_stop(tsk_push); + return PTR_ERR(tsk_pop); + } + + start = ktime_get(); + wake_up_process(tsk_push); + wake_up_process(tsk_pop); + + pr_info("waiting for test to complete\n"); + wait_for_completion(&wait); + + duration = (u64)ktime_us_delta(ktime_get(), start); + pr_info("%d of iterations took: %lluus\n", nr_test, duration); + + objpool_free(&ptr_pool); + page_frag_cache_drain(&test_frag); + + return -EAGAIN; +} + +static void __exit page_frag_test_exit(void) +{ +} + +module_init(page_frag_test_init); +module_exit(page_frag_test_exit); + +MODULE_LICENSE("GPL"); From patchwork Sun Apr 7 13:08:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13620172 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 3A203CD11C2 for ; Sun, 7 Apr 2024 13:11:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C04A26B00A0; Sun, 7 Apr 2024 09:11:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB58F6B00A1; Sun, 7 Apr 2024 09:11:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7E6B6B00A2; Sun, 7 Apr 2024 09:11:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 7F3B96B00A0 for ; Sun, 7 Apr 2024 09:11:36 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4E3B81C0B70 for ; Sun, 7 Apr 2024 13:11:36 +0000 (UTC) X-FDA: 81982772592.10.2596819 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf28.hostedemail.com (Postfix) with ESMTP id 9CB44C0019 for ; Sun, 7 Apr 2024 13:11:33 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf28.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=1712495494; 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=8NOAjzHCdx/nChQp2qbRibFGFBQGtXhuoUEohni6+ns=; b=vTmgeqnTkFHE345wD5p2EJjn2OClaK567y/7N0R0T9cHkIWMxRZas5o0q15dwWBih62kOn mjKr9bJoHnH6kHpHE2tvzaaPQaFXApDBWPT+P1u5mKWnayPfsNY2R2S6BR23ORdNHCwRkY Tm7y7VS1RImkX090OxYQCHtTJZRJIGs= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf28.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712495494; a=rsa-sha256; cv=none; b=kCKt78dfbL9LkXDZJOrEe0ediZxWqN7smlzbNHvvPhwiXKQn+HX1MwYw2EB83AsfG1RE8Q 7N4vhmc5xgZ2AFgkJyYQRKhYIsiMuJRhaLt60oZ2oTluZ7T21ZD3UwUIFj87PfvNNieSF+ 5DOs2X7TzSLOb5rQgDIpPCZ53klDeUE= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VCCGF5qDXzwQcX; Sun, 7 Apr 2024 21:08:37 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 27C061404F7; Sun, 7 Apr 2024 21:11:30 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 21:11:29 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jonathan Corbet , Andrew Morton , , Subject: [PATCH net-next v1 12/12] mm: page_frag: update documentation and maintainer for page_frag Date: Sun, 7 Apr 2024 21:08:49 +0800 Message-ID: <20240407130850.19625-13-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240407130850.19625-1-linyunsheng@huawei.com> References: <20240407130850.19625-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: 9CB44C0019 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: o7hsr99s8x4mjnrp7wq1kunig7u8d5t4 X-HE-Tag: 1712495493-100114 X-HE-Meta: U2FsdGVkX181WwM9emYy9UD+e4DmbgtqJSuxmJrLnYaw1ogx2p/wD4RjMniHqQpIJXUB92puiGYALxlQFP1xvXWBH9kIXWqJ1YcE8hXKmeZPfkDcZyGtZZY/Bv8E/j4b9XKKeyCnHP2o44V/GaoTySyRTE0L31UD0qjr16vjigzto7qjj2p4BGvozds0N8dU718ww0EmBuyTspolZNNLw2A3Xq5jL1As4+OmiZOblWP/gmGWT9BSaRmsj3Ufh/4qFz8avaqOldgNmpydkv3LzsrRRoY6qvfktCVz9Ib0MoZTLzE3KAWP+8BihXQ4IEdqo6s3yhHm7z8YqgSpxRYWbuJ+Fi4WFRz1cXZTfEKfYTBIXZ/6GkoAfXkpzK8Ee+W/gQ6pTvRzOUgB4m5A0X2Y9NLHqxZ0wUEsZCXAgJ7aQkYrt2C9Fa1iRkgeVfXN3zv+TV5ETmUb2NtI1vMBLoEZ5XI02jtIc5sTTIK11UU20Sp7Sxv+ApXqNeMJ8t1SZ6CpwcMuNkt9JZNr6nZQQbwc/HSlBVgjmdL4jTl65eEtgL2W/HIyHVPV1Cjsw+02p6+mCeFH658wD9qV+GOAzg9pa/BsARea12SXCXKFN3eaMbyQlfz/hHatgT4pw6EWtUFJLHn84iFiJS0+yscjPnSWP0AxqFqyELOLlyi/d82s2R7qKGXnB8U8Af/alsOmu/qjb0i6I1A+a8jWRUSROte89deqgdJ0V4DD4DR2oCtQmqq7wOXHfcHQoP7iosB+pvzl4aUPJ52KO+7wEexvuucysH9+hVtrqGZBO2FVogRb3A7Y07A59nWMSyJ+JWc6yU4F+PZihyq1M5Fc+BsEnmk5AYZ9ZPDItGWthxMYjeaZUAikx3BC35jyYld7GfdrQ4eVEeUIF6242GIgKD1QZwzMkg1B6H/RAdpcgeR+gyahQGPUUoIgt+fo9e2gIJCcpzS9GeK7fYWG6e3N7QzfC7E olQxvrWD PKgXygeIHmhANXNJ2W1ez30InJalfUe2KL1EvNcnJChRhAxEXT4eIcJelLfJ2GTugsCU9ThYIGwAiP1O0kpDHo8+rnilN5Vrhj2NheYRpj8zpPoas7zc2xjCnLeoh2pDGtbcLuykniI6ILanqkIu4GUPNUZhdizMR5nv972zvI3ijqzsyAIRw9bCfpvHZZmo+1lcQT9E7uoHYRWkHC8oAUXVHUyDwT6hbzbfSYwqf53RoF9LiBZTQuRWdCvuiamTu/2oIbQcXaH0L9DK4QHljklRDBJIahvgT3x3GaDSqFTLERr9tnJWC9mj6hJEFZlFl3tez5EBGK4MpsDS42cnT6CVv0eqtgnJT4YZlPdWs8kXhnKPbFKCNKGAC2LD6v3pXe6DOhDx/XGycKPMbcysqORjdY23ptLoMw/VibemRsiW/e8TDMXnaDocc190bNwZx+KcHfcJ3t3cuYPwWpi5rHci3yC4mqxodzl/gkSMWJhieko1dJkB5lJj7fg== 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: Update documentation about design, implementation and API usages for page_frag. Also update MAINTAINERS for page_frag. Alexander seems to be the orginal author for page_frag, we can add him to the MAINTAINERS later if we have an ack from him. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- Documentation/mm/page_frags.rst | 115 ++++++++++++++++++---------- MAINTAINERS | 10 +++ include/linux/page_frag_cache.h | 128 ++++++++++++++++++++++++++++++++ mm/page_frag_cache.c | 51 ++++++++++--- 4 files changed, 256 insertions(+), 48 deletions(-) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 503ca6cdb804..77256dfb58bf 100644 --- a/Documentation/mm/page_frags.rst +++ b/Documentation/mm/page_frags.rst @@ -1,43 +1,80 @@ +.. SPDX-License-Identifier: GPL-2.0 + ============== Page fragments ============== -A page fragment is an arbitrary-length arbitrary-offset area of memory -which resides within a 0 or higher order compound page. Multiple -fragments within that page are individually refcounted, in the page's -reference counter. - -The page_frag functions, page_frag_alloc and page_frag_free, provide a -simple allocation framework for page fragments. This is used by the -network stack and network device drivers to provide a backing region of -memory for use as either an sk_buff->head, or to be used in the "frags" -portion of skb_shared_info. - -In order to make use of the page fragment APIs a backing page fragment -cache is needed. This provides a central point for the fragment allocation -and tracks allows multiple calls to make use of a cached page. The -advantage to doing this is that multiple calls to get_page can be avoided -which can be expensive at allocation time. However due to the nature of -this caching it is required that any calls to the cache be protected by -either a per-cpu limitation, or a per-cpu limitation and forcing interrupts -to be disabled when executing the fragment allocation. - -The network stack uses two separate caches per CPU to handle fragment -allocation. The netdev_alloc_cache is used by callers making use of the -netdev_alloc_frag and __netdev_alloc_skb calls. The napi_alloc_cache is -used by callers of the __napi_alloc_frag and napi_alloc_skb calls. The -main difference between these two calls is the context in which they may be -called. The "netdev" prefixed functions are usable in any context as these -functions will disable interrupts, while the "napi" prefixed functions are -only usable within the softirq context. - -Many network device drivers use a similar methodology for allocating page -fragments, but the page fragments are cached at the ring or descriptor -level. In order to enable these cases it is necessary to provide a generic -way of tearing down a page cache. For this reason __page_frag_cache_drain -was implemented. It allows for freeing multiple references from a single -page via a single call. The advantage to doing this is that it allows for -cleaning up the multiple references that were added to a page in order to -avoid calling get_page per allocation. - -Alexander Duyck, Nov 29, 2016. +.. kernel-doc:: mm/page_frag_cache.c + :doc: page_frag allocator + +Architecture overview +===================== + +.. code-block:: none + + +----------------------+ + | page_frag API caller | + +----------------------+ + ^ + | + | + | + v + +----------------------------------------------+ + | request page fragment | + +----------------------------------------------+ + ^ ^ + | | + | Cache empty or not enough | + | | + v | + +--------------------------------+ | + | refill cache with order 3 page | | + +--------------------------------+ | + ^ ^ | + | | | + | | Refill failed | + | | | Cache is enough + | | | + | v | + | +----------------------------------+ | + | | refill cache with order 0 page | | + | +----------------------------------+ | + | ^ | + | Refill succeed | | + | | Refill succeed | + | | | + v v v + +----------------------------------------------+ + | allocate fragment from cache | + +----------------------------------------------+ + +API interface +============= +As the design and implementation of page_frag API, the allocation side does not +allow concurrent calling, it is assumed that the caller must ensure there is not +concurrent alloc calling to the same page_frag_cache instance by using it's own +lock or rely on some lockless guarantee like NAPI softirq. + +Depending on different use cases, callers expecting to deal with va, page or +both va and page for them may call page_frag_alloc_va(), page_frag_alloc_pg(), +or page_frag_alloc() accordingly. + +There is also a use case that need minimum memory in order for forward +progressing, but can do better if there is more memory available. Introduce +page_frag_alloc_prepare() and page_frag_alloc_commit() related API, the caller +requests the minimum memory it need and the prepare API will return the maximum +size of the fragment returned, caller need to report back to the page_frag core +how much memory it actually use by calling commit API, or not calling the commit +API if deciding to not use any memory. + +.. kernel-doc:: include/linux/page_frag_cache.h + :identifiers: page_frag_cache_init page_frag_cache_is_pfmemalloc + page_frag_alloc_va __page_frag_alloc_va_align + page_frag_alloc_va_align page_frag_alloc_va_prepare + page_frag_alloc_va_prepare_align page_frag_alloc_pg_prepare + page_frag_alloc_prepare page_frag_alloc_commit + page_frag_alloc_commit_noref page_frag_free_va + +.. kernel-doc:: mm/page_frag_cache.c + :identifiers: page_frag_cache_drain diff --git a/MAINTAINERS b/MAINTAINERS index 4745ea94d463..2f84aba59428 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16683,6 +16683,16 @@ F: mm/page-writeback.c F: mm/readahead.c F: mm/truncate.c +PAGE FRAG +M: Yunsheng Lin +L: linux-mm@kvack.org +L: netdev@vger.kernel.org +S: Supported +F: Documentation/mm/page_frags.rst +F: include/linux/page_frag_cache.h +F: mm/page_frag_cache.c +F: mm/page_frag_test.c + PAGE POOL M: Jesper Dangaard Brouer M: Ilias Apalodimas diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 28185969cd2c..d8edbecdd179 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -31,11 +31,23 @@ struct page_frag_cache { #endif }; +/** + * page_frag_cache_init() - Init page_frag cache. + * @nc: page_frag cache from which to init + * + * Inline helper to init the page_frag cache. + */ static inline void page_frag_cache_init(struct page_frag_cache *nc) { nc->va = NULL; } +/** + * page_frag_cache_is_pfmemalloc() - Check for pfmemalloc. + * @nc: page_frag cache from which to check + * + * Used to check if the current page in page_frag cache is pfmemalloc'ed. + */ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { return !!nc->pfmemalloc; @@ -46,6 +58,17 @@ void __page_frag_cache_drain(struct page *page, unsigned int count); void *page_frag_cache_refill(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask); +/** + * page_frag_alloc_va() - Alloc a page fragment. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Get a page fragment from page_frag cache. + * + * Return: + * Return va of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask) { @@ -63,6 +86,19 @@ static inline void *page_frag_alloc_va(struct page_frag_cache *nc, return va + offset; } +/** + * __page_frag_alloc_va_align() - Alloc a page fragment with aligning + * requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align: the requested aligning requirement + * + * Get a page fragment from page_frag cache with aligning requirement. + * + * Return: + * Return va of the page fragment, otherwise return NULL. + */ static inline void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, @@ -75,6 +111,19 @@ static inline void *__page_frag_alloc_va_align(struct page_frag_cache *nc, return page_frag_alloc_va(nc, fragsz, gfp_mask); } +/** + * page_frag_alloc_va_align() - Alloc a page fragment with aligning requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align: the requested aligning requirement + * + * WARN_ON_ONCE() checking for align and fragsz before getting a page fragment + * from page_frag cache with aligning requirement. + * + * Return: + * Return va of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, @@ -86,6 +135,19 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } +/** + * page_frag_alloc_va_prepare() - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @size: in as the requested size, out as the available size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Prepare a page fragment with minimum size of ‘size’, 'size' is also used to + * report the maximum size of the page fragment the caller can use. + * + * Return: + * Return va of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *offset, unsigned int *size, @@ -108,6 +170,21 @@ static inline void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, return va + *offset; } +/** + * page_frag_alloc_va_prepare_align() - Prepare allocing a page fragment with + * aligning requirement. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @size: in as the requested size, out as the available size + * @align: the requested aligning requirement + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Prepare an aligned page fragment with minimum size of ‘size’, 'size' is also + * used to report the maximum size of the page fragment the caller can use. + * + * Return: + * Return va of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cache *nc, unsigned int *offset, unsigned int *size, @@ -144,6 +221,19 @@ static inline void *__page_frag_alloc_pg_prepare(struct page_frag_cache *nc, return va; } +/** + * page_frag_alloc_pg_prepare - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @size: in as the requested size, out as the available size + * @gfp: the allocation gfp to use when cache need to be refilled + * + * Prepare a page fragment with minimum size of ‘size’, 'size' is also used to + * report the maximum size of the page fragment the caller can use. + * + * Return: + * Return the page fragment, otherwise return NULL. + */ #define page_frag_alloc_pg_prepare(nc, offset, size, gfp) \ ({ \ struct page *__page = NULL; \ @@ -179,6 +269,21 @@ static inline void *__page_frag_alloc_prepare(struct page_frag_cache *nc, return nc_va; } +/** + * page_frag_alloc_prepare - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @size: in as the requested size, out as the available size + * @va: out as the va of the returned page fragment + * @gfp: the allocation gfp to use when cache need to be refilled + * + * Prepare a page fragment with minimum size of ‘size’, 'size' is also used to + * report the maximum size of the page fragment. Return both 'page' and 'va' of + * the fragment to the caller. + * + * Return: + * Return the page fragment, otherwise return NULL. + */ #define page_frag_alloc_prepare(nc, offset, size, va, gfp) \ ({ \ struct page *__page = NULL; \ @@ -191,6 +296,14 @@ static inline void *__page_frag_alloc_prepare(struct page_frag_cache *nc, __page; \ }) +/** + * page_frag_alloc_commit - Commit allocing a page fragment. + * @nc: page_frag cache from which to commit + * @offset: offset of the page fragment + * @size: size of the page fragment has been used + * + * Commit the alloc preparing by passing offset and the actual used size. + */ static inline void page_frag_alloc_commit(struct page_frag_cache *nc, unsigned int offset, unsigned int size) @@ -199,6 +312,17 @@ static inline void page_frag_alloc_commit(struct page_frag_cache *nc, nc->offset = offset + size; } +/** + * page_frag_alloc_commit_noref - Commit allocing a page fragment without taking + * page refcount. + * @nc: page_frag cache from which to commit + * @offset: offset of the page fragment + * @size: size of the page fragment has been used + * + * Commit the alloc preparing by passing offset and the actual used size, but + * not taking page refcount. Mostly used for fragmemt coaleasing case when the + * current fragmemt can share the same refcount with previous fragmemt. + */ static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, unsigned int offset, unsigned int size) @@ -206,6 +330,10 @@ static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, nc->offset = offset + size; } +/** + * page_frag_free_va - Free a page fragment by va. + * @addr: va of page fragment to be freed + */ void page_frag_free_va(void *addr); #endif diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index cbd0ed82a596..0c76ec006c22 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -1,15 +1,44 @@ // SPDX-License-Identifier: GPL-2.0-only -/* Page fragment allocator + +/** + * DOC: page_frag allocator + * + * A page fragment is an arbitrary-length arbitrary-offset area of memory which + * resides within a 0 or higher order compound page. Multiple fragments within + * that page are individually refcounted, in the page's reference counter. + * + * The page_frag functions, page_frag_alloc* and page_frag_free*, provide a + * simple allocation framework for page fragments. This is used by the network + * stack and network device drivers to provide a backing region of memory for + * use as either an sk_buff->head, or to be used in the "frags" portion of + * skb_shared_info. * - * Page Fragment: - * An arbitrary-length arbitrary-offset area of memory which resides within a - * 0 or higher order page. Multiple fragments within that page are - * individually refcounted, in the page's reference counter. + * In order to make use of the page fragment APIs a backing page fragment cache + * is needed. This provides a central point for the fragment allocation and + * tracks allows multiple calls to make use of a cached page. The advantage to + * doing this is that multiple calls to get_page can be avoided which can be + * expensive at allocation time. However due to the nature of this caching it + * is required that any calls to the cache be protected by either a per-cpu + * limitation, or a per-cpu limitation and forcing interrupts to be disabled + * when executing the fragment allocation. * - * The page_frag functions provide a simple allocation framework for page - * fragments. This is used by the network stack and network device drivers to - * provide a backing region of memory for use as either an sk_buff->head, or to - * be used in the "frags" portion of skb_shared_info. + * The network stack uses two separate caches per CPU to handle fragment + * allocation. The netdev_alloc_cache is used by callers making use of the + * netdev_alloc_frag and __netdev_alloc_skb calls. The napi_alloc_cache is + * used by callers of the __napi_alloc_frag and napi_alloc_skb calls. The + * main difference between these two calls is the context in which they may be + * called. The "netdev" prefixed functions are usable in any context as these + * functions will disable interrupts, while the "napi" prefixed functions are + * only usable within the softirq context. + * + * Many network device drivers use a similar methodology for allocating page + * fragments, but the page fragments are cached at the ring or descriptor + * level. In order to enable these cases it is necessary to provide a generic + * way of tearing down a page cache. For this reason __page_frag_cache_drain + * was implemented. It allows for freeing multiple references from a single + * page via a single call. The advantage to doing this is that it allows for + * cleaning up the multiple references that were added to a page in order to + * avoid calling get_page per allocation. */ #include @@ -57,6 +86,10 @@ static bool __page_frag_cache_refill(struct page_frag_cache *nc, return true; } +/** + * page_frag_cache_drain - Drain the current page from page_frag cache. + * @nc: page_frag cache from which to drain + */ void page_frag_cache_drain(struct page_frag_cache *nc) { if (!nc->va)