From nobody Thu Sep 19 23:20:50 2024 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C2D5383AC; Sun, 7 Apr 2024 13:11:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495463; cv=none; b=dbhp9lVKtBaD0sTKXokwsk//pmRYG3X8fH2switfbYv8LvJkMr90uVYqQqTpKN0+rXU+w5Y3qqkCey3ua1dkUNK7cJgDh6V04VVVIijyvCZ6Pfx1QetWjkV1AqmdkEABoVK/b6HnzzWaLShj3DWqiyp7JFcgyoMs08HI8tWtocw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495463; c=relaxed/simple; bh=sCycIupzR6Yh5XXiIugQkeK+hUAM6TNGBUayt9Bg/OY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XSH7hfe5Q49ekd+JoROiVpOY8CYPcPOKFg9SV4IchK1knbRhJ5m5VIQwliUuxcXQyD66MJhsf2qnnNZWsyWukW/Qhzx8LNi/RG3MiQF1g+IUwjDy2b84PgXxEwNzfhk377kUz81Nce2dHcwZ09IXv85GgOZd4SWr0WAHSiabaDk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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); =20 -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 fra= gsz, - 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) =20 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) <=3D sizeof(struct = page)); */ #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) =20 -#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 *fo= lio) return folio->private; } =20 -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; =20 /* diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cach= e.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 fra= gsz, + 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) +=3D shuffle.o memory-hotplug-$(CONFIG_MEMORY_HOTPLUG) +=3D memory_hotplug.o =20 obj-y +=3D page-alloc.o +obj-y +=3D page_frag_cache.o obj-y +=3D init-mm.o obj-y +=3D memblock.o obj-y +=3D $(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 or= der) =20 EXPORT_SYMBOL(free_pages); =20 -/* - * 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 =3D NULL; - gfp_t gfp =3D gfp_mask; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - gfp_mask =3D (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | - __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; - page =3D alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); - nc->size =3D page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; -#endif - if (unlikely(!page)) - page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); - - nc->va =3D 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 =3D 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) =3D=3D 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 =3D PAGE_SIZE; - struct page *page; - int offset; - - if (unlikely(!nc->va)) { -refill: - page =3D __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 =3D 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 =3D page_is_pfmemalloc(page); - nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset =3D size; - } - - offset =3D nc->offset - fragsz; - if (unlikely(offset < 0)) { - page =3D 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 =3D 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 =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset =3D 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 &=3D align_mask; - nc->offset =3D offset; - - return nc->va + offset; -} -EXPORT_SYMBOL(__page_frag_alloc_align); - -/* - * Frees a page fragment allocated out of either a compound or order 0 pag= e. - */ -void page_frag_free(void *addr) -{ - struct page *page =3D 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 with= in 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 driver= s 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 =3D NULL; + gfp_t gfp =3D gfp_mask; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + gfp_mask =3D (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | + __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; + page =3D alloc_pages_node(NUMA_NO_NODE, gfp_mask, + PAGE_FRAG_CACHE_MAX_ORDER); + nc->size =3D page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; +#endif + if (unlikely(!page)) + page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); + + nc->va =3D 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 =3D 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) =3D=3D 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 =3D PAGE_SIZE; + struct page *page; + int offset; + + if (unlikely(!nc->va)) { +refill: + page =3D __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 =3D 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 =3D page_is_pfmemalloc(page); + nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset =3D size; + } + + offset =3D nc->offset - fragsz; + if (unlikely(offset < 0)) { + page =3D 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 =3D 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 =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; + offset =3D 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 &=3D align_mask; + nc->offset =3D offset; + + return nc->va + offset; +} +EXPORT_SYMBOL(__page_frag_alloc_align); + +/* + * Frees a page fragment allocated out of either a compound or order 0 pag= e. + */ +void page_frag_free(void *addr) +{ + struct page *page =3D virt_to_head_page(addr); + + if (unlikely(put_page_testzero(page))) + free_unref_page(page, compound_order(page)); +} +EXPORT_SYMBOL(page_frag_free); --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF97239855; Sun, 7 Apr 2024 13:11:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.255 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495464; cv=none; b=PTrgj7ETNAh3R8EbpnWydUvm1M2iwnCq82i53AxcQKuMkPn2q9tWBvYrxmZ2YV2xRULAvsubrSvm+4/KqB4GT+TTos+jywDVZpX2khjLo5/XNqVOT/2h1jSRiUUYCHNE1nyOU5NPRj5xkF2/YWxstYQR1wLJdtaCgpFKXV4t85c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495464; c=relaxed/simple; bh=ckzJsKLlvTZAa8riV8iVnTe/Y30hT3rAXMB8iLio1u0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=owEMGkwoLeIadgs3arf1I6c3aoGaFZxyWy7pF0YD6AuckvblUmHdO9RxxtY5qbb/yz5Lg3pOZK3/+Vy4h+keDJLNQ3c9VXVh/onHFfPj2TS8EC2bPqoJDOi2x7+MRRtqWsDSZ+WFdU5XoN3BFhhiTWcs24/+Q6s9PI+K6DECYG8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.255 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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.cam= el@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 =3D PAGE_SIZE; + unsigned int size, offset; struct page *page; - int offset; =20 if (unlikely(!nc->va)) { refill: @@ -77,10 +76,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, if (!page) return NULL; =20 -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size =3D 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 *n= c, /* reset page count bias and offset to start of new frag */ nc->pfmemalloc =3D page_is_pfmemalloc(page); nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset =3D size; + nc->offset =3D 0; } =20 - offset =3D 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 =3D nc->size; +#else + size =3D PAGE_SIZE; +#endif + + offset =3D ALIGN(nc->offset, -align_mask); + if (unlikely(offset + fragsz > size)) { page =3D virt_to_page(nc->va); =20 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; } =20 -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size; -#endif /* OK, page count is 0, we can safely set it */ set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); =20 /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset =3D size - fragsz; - if (unlikely(offset < 0)) { + offset =3D 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 *n= c, } =20 nc->pagecnt_bias--; - offset &=3D align_mask; - nc->offset =3D offset; + nc->offset =3D offset + fragsz; =20 return nc->va + offset; } --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79E0F3A267; Sun, 7 Apr 2024 13:11:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.190 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495467; cv=none; b=DmZOEepeOniX5w8lTBHyjUMrH/I3E3DaIKNApoWIcnil4867UdkzcSKMoGAzxr/fJx2a50beHixXp1CFHDMn+jylEbOS5XOVzgoHafjDhxPXjs4G1qqguvAqCI3Xgh8/KfCQc9kPVbt6rMjc0TPMgEN62Tvi7/2EPLbqGq11tTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495467; c=relaxed/simple; bh=AnePHUh3T6UCcCAQ+5WxCR2SiVsSzk+IcmWCs8HWTWo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gYFhYluQdH1daOvmpsFAvixyTWZ50+NJGhtqkRPh+vi3X3wYIW4TDErLmcTbTF+S1eHFLDjYabne7tuVuJbMk8Fdm8GET2CqloTjS4YKV/zHyZiMP+SLcthzAcmcLbMqivJ/qOeotD8mPKA8ThUbfi+64vN0qxE0eNED3TNT66U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.190 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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_cach= e.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 { =20 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 fra= gsz, - 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 =3D ALIGN(nc->offset, align); + + return page_frag_alloc(nc, fragsz, gfp_mask); +} =20 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); -} =20 -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); } =20 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_hea= d *list) unsigned int skb_rbtree_purge(struct rb_root *root); void skb_errqueue_purge(struct sk_buff_head *list); =20 -void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_ma= sk); +void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align); =20 /** * 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); } =20 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); } =20 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int le= ngth, @@ -3346,18 +3346,18 @@ static inline void skb_free_frag(void *addr) page_frag_free(addr); } =20 -void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask= ); +void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align); =20 static inline void *napi_alloc_frag(unsigned int fragsz) { - return __napi_alloc_frag_align(fragsz, ~0u); + return __napi_alloc_frag_align(fragsz, 1u); } =20 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); } =20 struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int leng= th); 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); =20 -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 =3D PAGE_SIZE; #endif =20 - offset =3D ALIGN(nc->offset, -align_mask); + offset =3D nc->offset; if (unlikely(offset + fragsz > size)) { page =3D virt_to_page(nc->va); =20 @@ -131,7 +130,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *n= c, =20 return nc->va + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(page_frag_alloc); =20 /* * Frees a page fragment allocated out of either a compound or order 0 pag= e. 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(); } =20 -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 =3D this_cpu_ptr(&napi_alloc_cache); =20 fragsz =3D SKB_DATA_ALIGN(fragsz); =20 - 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); =20 -void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_ma= sk) +void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align) { void *data; =20 @@ -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 =3D this_cpu_ptr(&netdev_alloc_cache); =20 - data =3D __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, - align_mask); + data =3D __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align); } else { struct napi_alloc_cache *nc; =20 local_bh_disable(); nc =3D this_cpu_ptr(&napi_alloc_cache); data =3D __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 =3D round_up(sizeof(*whdr), data_align) - sizeof(*whdr); total =3D hoff + sizeof(*whdr) + data_size; =20 + data_align =3D max_t(size_t, data_align, L1_CACHE_BYTES); mutex_lock(&call->conn->tx_data_alloc_lock); - buf =3D __page_frag_alloc_align(&call->conn->tx_data_alloc, total, gfp, - ~(data_align - 1) & ~(L1_CACHE_BYTES - 1)); + buf =3D 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); --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 830443BBF2; Sun, 7 Apr 2024 13:11:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495474; cv=none; b=NXK80u+6v//DNAfuetYzQw6IxGynERxgAaoKGrkPSUwsCFRKv9v6n4/RK0gMQUmCLtQiXr9S73WIRRq/XL7ZphBmjHGnv6JVIk2JJWT8iPvfwy6O2m0W7Dwl82fQBqo++xzCguS0WXcP75bnCRzwdBPNhMvVrKLyw4+X/cTOeJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495474; c=relaxed/simple; bh=/V9lmCwlWEX1E/SszmQxz+bH7MEuE6WfJOQyLmMGDYI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Hb/jYC/xg3epBsb4YZlKU4MYO4rm2puvKaptpZ4rNThF89k3ZLpKnevDrLmlyjySqPlyOqPBvWNWDyM8KzPlLlE10oITK9JCbVhY7NhQlIXxFwAG2zYY/lfqWFmT7sV0jClfaPA33NLziBBA7lqT7/jY1J42TFQwHJZG9QDjXbY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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/etherne= t/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, str= uct gve_rx_ring *rx, =20 total_len =3D headroom + SKB_DATA_ALIGN(len) + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); - frame =3D page_frag_alloc(&rx->page_cache, total_len, GFP_ATOMIC); + frame =3D 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, str= uct gve_rx_ring *rx, =20 err =3D xdp_do_redirect(dev, &new, xdp_prog); if (err) - page_frag_free(frame); + page_frag_free_va(frame); =20 return err; } diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.c b/drivers/net/ethern= et/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, str= uct 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/ethern= et/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/et= hernet/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, =20 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/ne= t/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_vecto= r *q_vector, =20 /* 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); =20 @@ -2413,7 +2413,7 @@ static void ixgbevf_clean_tx_ring(struct ixgbevf_ring= *tx_ring) =20 /* 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); =20 diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c b/dri= vers/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, str= uct otx2_pool *pool, *dma =3D 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; } =20 diff --git a/drivers/net/ethernet/mediatek/mtk_wed_wo.c b/drivers/net/ether= net/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 m= tk_wed_wo_queue *q, dma_addr_t addr; void *buf; =20 - buf =3D page_frag_alloc(&q->cache, q->buf_size, - GFP_ATOMIC | GFP_DMA32); + buf =3D page_frag_alloc_va(&q->cache, q->buf_size, + GFP_ATOMIC | GFP_DMA32); if (!buf) break; =20 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 =3D blk_mq_rq_to_pdu(rq); =20 - page_frag_free(req->pdu); + page_frag_free_va(req->pdu); } =20 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 =3D &ctrl->queues[queue_idx]; u8 hdgst =3D nvme_tcp_hdgst_len(queue); =20 - req->pdu =3D page_frag_alloc(&queue->pf_cache, + req->pdu =3D 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_c= trl *ctrl) { struct nvme_tcp_request *async =3D &ctrl->async_req; =20 - page_frag_free(async->pdu); + page_frag_free_va(async->pdu); } =20 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_c= trl *ctrl) struct nvme_tcp_request *async =3D &ctrl->async_req; u8 hdgst =3D nvme_tcp_hdgst_len(queue); =20 - async->pdu =3D page_frag_alloc(&queue->pf_cache, + async->pdu =3D 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_que= ue *queue, c->queue =3D queue; c->req.port =3D queue->port->nport; =20 - c->cmd_pdu =3D page_frag_alloc(&queue->pf_cache, + c->cmd_pdu =3D 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 =3D &c->cmd_pdu->cmd; =20 - c->rsp_pdu =3D page_frag_alloc(&queue->pf_cache, + c->rsp_pdu =3D 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 =3D &c->rsp_pdu->cqe; =20 - c->data_pdu =3D page_frag_alloc(&queue->pf_cache, + c->data_pdu =3D page_frag_alloc_va(&queue->pf_cache, sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->data_pdu) goto out_free_rsp; =20 - c->r2t_pdu =3D page_frag_alloc(&queue->pf_cache, + c->r2t_pdu =3D 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_que= ue *queue, =20 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; } =20 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); } =20 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_virtque= ue *nvq, return -ENOSPC; =20 buflen +=3D SKB_DATA_ALIGN(len + pad); - buf =3D page_frag_alloc_align(&net->pf_cache, buflen, GFP_KERNEL, - SMP_CACHE_BYTES); + buf =3D page_frag_alloc_va_align(&net->pf_cache, buflen, GFP_KERNEL, + SMP_CACHE_BYTES); if (unlikely(!buf)) return -ENOMEM; =20 @@ -730,7 +730,7 @@ static int vhost_net_build_xdp(struct vhost_net_virtque= ue *nvq, return 0; =20 err: - page_frag_free(buf); + page_frag_free_va(buf); return ret; } =20 diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cach= e.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 { =20 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); =20 -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 =3D ALIGN(nc->offset, align); =20 - return page_frag_alloc(nc, fragsz, gfp_mask); + return page_frag_alloc_va(nc, fragsz, gfp_mask); } =20 -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)); =20 - return __page_frag_alloc_align(nc, fragsz, gfp_mask, align); + return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } =20 -void page_frag_free(void *addr); +void page_frag_free_va(void *addr); =20 #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_ali= gn(struct net_device *dev, =20 static inline void skb_free_frag(void *addr) { - page_frag_free(addr); + page_frag_free_va(addr); } =20 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) =20 /* 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); =20 -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, uns= igned int fragsz, =20 return nc->va + offset; } -EXPORT_SYMBOL(page_frag_alloc); +EXPORT_SYMBOL(page_frag_alloc_va); =20 /* * Frees a page fragment allocated out of either a compound or order 0 pag= e. */ -void page_frag_free(void *addr) +void page_frag_free_va(void *addr) { struct page *page =3D virt_to_head_page(addr); =20 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, unsi= gned int align) =20 fragsz =3D SKB_DATA_ALIGN(fragsz); =20 - 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); =20 @@ -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 =3D this_cpu_ptr(&netdev_alloc_cache); =20 - data =3D __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align); + data =3D __page_frag_alloc_va_align(nc, fragsz, GFP_ATOMIC, + align); } else { struct napi_alloc_cache *nc; =20 local_bh_disable(); nc =3D this_cpu_ptr(&napi_alloc_cache); - data =3D __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, - align); + data =3D __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, =20 if (in_hardirq() || irqs_disabled()) { nc =3D this_cpu_ptr(&netdev_alloc_cache); - data =3D page_frag_alloc(nc, len, gfp_mask); + data =3D page_frag_alloc_va(nc, len, gfp_mask); pfmemalloc =3D nc->pfmemalloc; } else { local_bh_disable(); nc =3D this_cpu_ptr(&napi_alloc_cache.page); - data =3D page_frag_alloc(nc, len, gfp_mask); + data =3D page_frag_alloc_va(nc, len, gfp_mask); pfmemalloc =3D nc->pfmemalloc; local_bh_enable(); } @@ -833,7 +834,7 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi= , unsigned int len) } else { len =3D SKB_HEAD_ALIGN(len); =20 - data =3D page_frag_alloc(&nc->page, len, gfp_mask); + data =3D page_frag_alloc_va(&nc->page, len, gfp_mask); pfmemalloc =3D nc->page.pfmemalloc; } =20 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 =3D 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_c= all *call, size_t data_ =20 data_align =3D max_t(size_t, data_align, L1_CACHE_BYTES); mutex_lock(&call->conn->tx_data_alloc_lock); - buf =3D page_frag_alloc_align(&call->conn->tx_data_alloc, total, gfp, - data_align); + buf =3D 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; =20 - buf =3D page_frag_alloc(&call->local->tx_alloc, - sizeof(*whdr) + sizeof(*ack) + 1 + 3 + sizeof(*trailer), gfp); + buf =3D page_frag_alloc_va(&call->local->tx_alloc, + sizeof(*whdr) + sizeof(*ack) + 1 + 3 + sizeof(*trailer), gfp); if (!buf) { kfree(txb); return NULL; } =20 if (sack_size) { - buf2 =3D page_frag_alloc(&call->local->tx_alloc, sack_size, gfp); + buf2 =3D 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 =3D 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, str= uct 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 =3D page_frag_alloc(&svsk->sk_frag_cache, sizeof(marker), - GFP_KERNEL); + buf =3D page_frag_alloc_va(&svsk->sk_frag_cache, sizeof(marker), + GFP_KERNEL); if (!buf) return -ENOMEM; memcpy(buf, &marker, sizeof(marker)); --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3970D3BBEA; Sun, 7 Apr 2024 13:11:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495473; cv=none; b=dNpHEF3tcufmYZzAuS/5jBwYxVV2+Z3q2EPWWY4BMUfSi90ZJkUEMdxgomrYyKku3yhKGuHOFg5zlKYdN3N22TMpGXMPe9Gd5VR85iREF3/pB8JW6GryFUQfwi4FpjrEGCmLXrhtdFEagg1iC4qpl5o12mDEg1LhLqSWwE1U0Kw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495473; c=relaxed/simple; bh=hDg7jA1fPAoBoi0a6T0CNtFlVqTTcsoVa4Nj+L71Yvs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XpjQV6T8CbeL71vZ8HfyuMUq+mnLwkWnGhFQ56ADJlZEqoUe1S8wgNAvMLvl2o6hfnHRJ/yV/4YhwE8aDW1BG4ePU3UWXtoVFU5GPbCj0XatF5zd5p8Rx1vo1o1aVsFA77zivEDhX3w4+2OGs2F+o/YamkSv2viYZcdgHK/Fs0s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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_cach= e.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; }; =20 +static inline void page_frag_cache_init(struct page_frag_cache *nc) +{ + nc->va =3D NULL; +} + +static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *n= c) +{ + 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 =3D this_cpu_ptr(&netdev_alloc_cache); data =3D page_frag_alloc_va(nc, len, gfp_mask); - pfmemalloc =3D nc->pfmemalloc; + pfmemalloc =3D page_frag_cache_is_pfmemalloc(nc); } else { local_bh_disable(); nc =3D this_cpu_ptr(&napi_alloc_cache.page); data =3D page_frag_alloc_va(nc, len, gfp_mask); - pfmemalloc =3D nc->pfmemalloc; + pfmemalloc =3D page_frag_cache_is_pfmemalloc(nc); local_bh_enable(); } =20 --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB4093D3AC; Sun, 7 Apr 2024 13:11:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495476; cv=none; b=Y2ImguY/cgEdABNNEpZUIVpeBn2UBCvZQ43n+Eit9Tf5qKeSxmwJzn9VbsQd2NuXvoDtrreaVPVmxgGuBiHkuC06LI3a31CL3NgwkZHhW/JjZvNOEeMpuB3ENw0nezYH2qaK0/dR76oR8kXsdOjL0e1toL3423YiupKUmevZEII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495476; c=relaxed/simple; bh=dvnuWSgw2pdf+CQ3hiV4nkuRSA4Mui25VpqM8+ZL28w=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=B3Rr/zZtxPo5TCYwvY1ysG/20bLP+IWOROKYUqyTO+c8wO0fefCVNzP79w6H1LuTJBNhOx/n+8vSnVORVd1fSzbx+4PHQfYV5RtOnyPzVvpDiC7gbGYM+xBLiifU15MhhsaeedK+Ks1mC11FzGL024vfQAWQseMArWiJlfiStDA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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_cach= e.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; }; =20 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 pag= e_frag_cache *nc, gfp_t gfp_mask, unsigned int align) { - nc->offset =3D ALIGN(nc->offset, align); + unsigned int offset =3D nc->offset; + + nc->offset =3D ALIGN(offset, align); =20 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 >=3D PAGE_SIZE); =20 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 =3D alloc_pages_node(NUMA_NO_NODE, gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER); - nc->size =3D page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; + nc->size_mask =3D page ? PAGE_FRAG_CACHE_MAX_SIZE - 1 : PAGE_SIZE - 1; + VM_BUG_ON(page && nc->size_mask !=3D PAGE_FRAG_CACHE_MAX_SIZE - 1); #endif if (unlikely(!page)) page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); @@ -88,7 +89,7 @@ void *page_frag_alloc_va(struct page_frag_cache *nc, unsi= gned int fragsz, =20 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size; + size =3D nc->size_mask + 1; #else size =3D PAGE_SIZE; #endif --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9F9E3839D; Sun, 7 Apr 2024 13:11:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495478; cv=none; b=EJ9kgJww2rc+kMIVggfcmPrRtSU+oVuGnMPcYgTQYP0pYZ+6NcJ4cNj3/94i6v5QJDdTWBLqFRr4RqRFEF5FH88PTMnGLjg2zY9RLGn1LWsMLz+2jvmF+KJDYsWhwTc2S8sDJxyvrkeVU+clyhT64Wvs25pK3evumCu2poqAS/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495478; c=relaxed/simple; bh=WCE8RXU0X7aMBPY//3QOWVCfbjWVSvTLXrqCWiLgSow=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DnEUoNbffHpbKQEAb1elQ9RWPwngzGb/b26QaLSMNVG4E1B24vvR09rZ0qX9hTFE0eK3/G0D5zT/fkQ4rKK1oeJVZEk/vGuA4hGjdeRgJTaQrkv3IRt71RyOkpFq3Pkdv6CQHiuPuKHVUELjdZusoIlHFNtwMu6dD2Wkmzd51bA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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_cach= e.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) =20 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; }; =20 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 >=3D PAGE_SIZE); + WARN_ON_ONCE(!is_power_of_2(align) || align >=3D PAGE_SIZE || + fragsz < sizeof(unsigned int)); =20 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" =20 -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 =3D NULL; gfp_t gfp =3D gfp_mask; @@ -35,9 +35,26 @@ static struct page *__page_frag_cache_refill(struct page= _frag_cache *nc, if (unlikely(!page)) page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); =20 - nc->va =3D page ? page_address(page) : NULL; + if (unlikely(!page)) { + nc->va =3D NULL; + return false; + } + + nc->va =3D page_address(page); =20 - 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 |=3D nc->size_mask; +#else + VM_BUG_ON(nc->pagecnt_bias & (PAGE_SIZE - 1)); + page_ref_add(page, PAGE_SIZE - 2); + nc->pagecnt_bias |=3D (PAGE_SIZE - 1); +#endif + + nc->pfmemalloc =3D page_is_pfmemalloc(page); + nc->offset =3D 0; + return true; } =20 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; =20 if (unlikely(!nc->va)) { refill: - page =3D __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 =3D page_is_pfmemalloc(page); - nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset =3D 0; } =20 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size_mask + 1; + size_mask =3D nc->size_mask; #else - size =3D PAGE_SIZE; + size_mask =3D PAGE_SIZE - 1; #endif =20 + va =3D (void *)((unsigned long)nc->va & ~size_mask); offset =3D nc->offset; - if (unlikely(offset + fragsz > size)) { - page =3D virt_to_page(nc->va); =20 - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + if (unlikely(offset + fragsz > (size_mask + 1))) { + page =3D virt_to_page(va); + + if (!page_ref_sub_and_test(page, nc->pagecnt_bias & size_mask)) goto refill; =20 if (unlikely(nc->pfmemalloc)) { @@ -107,12 +117,11 @@ void *page_frag_alloc_va(struct page_frag_cache *nc, = unsigned int fragsz, } =20 /* 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 |=3D size_mask; =20 - /* reset page count bias and offset to start of new frag */ - nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; offset =3D 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, un= signed int fragsz, nc->pagecnt_bias--; nc->offset =3D offset + fragsz; =20 - return nc->va + offset; + return va + offset; } EXPORT_SYMBOL(page_frag_alloc_va); =20 --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C88D3F9DB; Sun, 7 Apr 2024 13:11:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495480; cv=none; b=QIEN90S1P7BpcxnghdUw94TY7Dxr1MfMbjNEZl+WST8z4PvhfNxmriE9o33a+74xsG6k1uzA0W5xWvgDoPQycL3Ier+9loKoeTkjlJ6H5D1DpdU4Ch4ciouM4IVbpwyaPop6e0sWGPBhZb0Vvrhx4N9vvZEt/re1iyFZYBgH7Jg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495480; c=relaxed/simple; bh=77TAy3kea4Va3Is1I0qN4USj8ONZ6KixwSmbOIBXyLo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MqE6BDc0YFgfxKak7+9bGk/OU4yb2yfdnLdOAYvfhSyrggwO6yI8Da16WREjsEYDZP6Nq5sR8Ls6N4+t1KC45a9Gn0ZdowDIpXR4AUGLT85lW09RZlbx4ZBG2efi5G0qttg+VHCqCjsuvv2UeeoP0DmhlxZvXRqominnErJwd64= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4VCCG32pMPz1h5vr; Sun, 7 Apr 2024 21:08:27 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 8B7501401E0; Sun, 7 Apr 2024 21:11:16 +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:16 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Eric Dumazet Subject: [PATCH net-next v1 08/12] net: introduce the skb_copy_to_va_nocache() helper Date: Sun, 7 Apr 2024 21:08:45 +0800 Message-ID: <20240407130850.19625-9-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" introduce the skb_copy_to_va_nocache() helper to avoid calling virt_to_page() and skb_copy_to_page_nocache(). Signed-off-by: Yunsheng Lin --- include/net/sock.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index a495330c5c49..d214aeca72ad 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2272,6 +2272,21 @@ static inline int skb_copy_to_page_nocache(struct so= ck *sk, struct iov_iter *fro return 0; } =20 +static inline int skb_copy_to_va_nocache(struct sock *sk, struct iov_iter = *from, + struct sk_buff *skb, char *va, int copy) +{ + int err; + + err =3D skb_do_copy_data_nocache(sk, skb, from, va, copy, skb->len); + if (err) + return err; + + skb_len_add(skb, copy); + sk_wmem_queued_add(sk, copy); + sk_mem_charge(sk, copy); + return 0; +} + /** * sk_wmem_alloc_get - returns write allocations * @sk: socket --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A91E240BEB; Sun, 7 Apr 2024 13:11:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495482; cv=none; b=u6S327AucpM27bvW0PtBaiZqSW3RTnZq3ykssV3H1a4NUjdIOMu74XKZkJewi3xqpsY2pkMz7/SPaRVxyY3NJXWY7SKs0RYyP/pl36hT2OrI7D7ZcTX0jAoo4nnYk4e3FkCr8fm0hAEc/p+zWlKAQaQNlNJDDku4t1ldlfqTSeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495482; c=relaxed/simple; bh=pzP6X31Z4/s7y8HXW1cwy6Xr+yD0+ZY3i/X90DuBceg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mtJRkGijbJ1ol2v1F8ZVJIucdrF/UB0ywJ+QFnPJKvXpwWb4ZeqqQ0aeYQqheHcleR0DWnCZRMtOTFKQJ/CLk1SipbEofcxYZAAWonQYEvW6e4frY/z7hfEVw9tl9u7bTh5PAZ471RyXzE8BHAYh8hpsDhVMTTYf9gX6wQCbp8k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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_cach= e.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) =20 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 frag= sz, + 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 =3D page_frag_cache_refill(nc, fragsz, gfp_mask); + if (unlikely(!va)) + return NULL; + + offset =3D nc->offset; + nc->pagecnt_bias--; + nc->offset =3D offset + fragsz; + + return va + offset; +} =20 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 pag= e_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } =20 +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 =3D page_frag_cache_refill(nc, *size, gfp_mask); + if (unlikely(!va)) + return NULL; + + *offset =3D nc->offset; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + *size =3D nc->size_mask - *offset + 1; +#else + *size =3D PAGE_SIZE - *offset; +#endif + + return va + *offset; +} + +static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cach= e *nc, + unsigned int *offset, + unsigned int *size, + unsigned int align, + gfp_t gfp_mask) +{ + WARN_ON_ONCE(!is_power_of_2(align) || align >=3D PAGE_SIZE || + *size < sizeof(unsigned int)); + + *offset =3D nc->offset; + nc->offset =3D 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 *n= c, + unsigned int *offset, + unsigned int *size, + gfp_t gfp_mask) +{ + void *va; + + va =3D page_frag_cache_refill(nc, *size, gfp_mask); + if (unlikely(!va)) + return NULL; + + *offset =3D nc->offset; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + *size =3D nc->size_mask - *offset + 1; +#else + *size =3D PAGE_SIZE - *offset; +#endif + + return va; +} + +#define page_frag_alloc_pg_prepare(nc, offset, size, gfp) \ +({ \ + struct page *__page =3D NULL; \ + void *__va; \ + \ + __va =3D __page_frag_alloc_pg_prepare(nc, offset, size, gfp); \ + if (likely(__va)) \ + __page =3D 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 =3D page_frag_cache_refill(nc, *size, gfp_mask); + if (unlikely(!nc_va)) + return NULL; + + *offset =3D nc->offset; + *va =3D nc_va + *offset; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + *size =3D nc->size_mask - *offset + 1; +#else + *size =3D PAGE_SIZE - *offset; +#endif + + return nc_va; +} + +#define page_frag_alloc_prepare(nc, offset, size, va, gfp) \ +({ \ + struct page *__page =3D NULL; \ + void *__va; \ + \ + __va =3D __page_frag_alloc_prepare(nc, offset, size, va, gfp); \ + if (likely(__va)) \ + __page =3D 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 =3D offset + size; +} + +static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, + unsigned int offset, + unsigned int size) +{ + nc->offset =3D offset + size; +} + void page_frag_free_va(void *addr); =20 #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); =20 -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 frag= sz, + 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, un= signed int fragsz, set_page_count(page, size_mask); nc->pagecnt_bias |=3D size_mask; =20 - offset =3D 0; + nc->offset =3D 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, u= nsigned int fragsz, } } =20 - nc->pagecnt_bias--; - nc->offset =3D offset + fragsz; - - return va + offset; + return va; } -EXPORT_SYMBOL(page_frag_alloc_va); +EXPORT_SYMBOL(page_frag_cache_refill); =20 /* * Frees a page fragment allocated out of either a compound or order 0 pag= e. --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C9E738FA1 for ; Sun, 7 Apr 2024 13:11:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495491; cv=none; b=FV4uJRPPRzWjk44axEmIQya4cOn91w2gaJJS814sbt1qPLq5GFwNQCAUchxq4dMz3q2sx+hKjXWClJuA4svtc2C7PeE5n6fMv6Bycx3N37eyJKSvluWB5YQq+eG/NroT/yUXrmWYMrPNXR1uS3bSvulT7UdjsFSK0nncdzwLNSA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495491; c=relaxed/simple; bh=D53KaAadA19cU037H5cHFE9wDhzuFoqApBSLZSPvI7c=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=by2W3qVIT8lRuexqyoj3UgN/wI1nOCgGnpQ/Pl6N6eUK4f/QZlkEpxPxRIO1j5aFJQh0pyVqHKu21FqV1j9F5DHSoG+MiBjFq+Dz/Q6xHilJZ90Gb0pAxNY8eBgJ4y+EXq03MEsvKliJbDDp/4/WUe+LmKdJTV6JvKqphmxxbnQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4VCCJQ4MGXz1wqFj; Sun, 7 Apr 2024 21:10:30 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id BEEFC18005F; Sun, 7 Apr 2024 21:11:24 +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:24 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Ayush Sawal , Eric Dumazet , Willem de Bruijn , Jason Wang , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , John Fastabend , Jakub Sitnicki , David Ahern , Matthieu Baerts , Mat Martineau , Geliang Tang , Boris Pismenny , , Subject: [PATCH net-next v1 10/12] net: replace page_frag with page_frag_cache Date: Sun, 7 Apr 2024 21:08:47 +0800 Message-ID: <20240407130850.19625-11-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> Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" Use the newly introduced prepare/commit API to replace page_frag with page_frag_cache for sk_page_frag(). Signed-off-by: Yunsheng Lin --- .../chelsio/inline_crypto/chtls/chtls.h | 3 - .../chelsio/inline_crypto/chtls/chtls_io.c | 101 ++++--------- .../chelsio/inline_crypto/chtls/chtls_main.c | 3 - drivers/net/tun.c | 34 ++--- include/linux/sched.h | 4 +- include/net/sock.h | 14 +- kernel/exit.c | 3 +- kernel/fork.c | 2 +- net/core/skbuff.c | 32 ++-- net/core/skmsg.c | 22 +-- net/core/sock.c | 46 ++++-- net/ipv4/ip_output.c | 35 +++-- net/ipv4/tcp.c | 35 ++--- net/ipv4/tcp_output.c | 28 ++-- net/ipv6/ip6_output.c | 35 +++-- net/kcm/kcmsock.c | 30 ++-- net/mptcp/protocol.c | 74 ++++++---- net/tls/tls_device.c | 139 ++++++++++-------- 18 files changed, 342 insertions(+), 298 deletions(-) diff --git a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls.h b/dri= vers/net/ethernet/chelsio/inline_crypto/chtls/chtls.h index 7ff82b6778ba..fe2b6a8ef718 100644 --- a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls.h +++ b/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls.h @@ -234,7 +234,6 @@ struct chtls_dev { struct list_head list_node; struct list_head rcu_node; struct list_head na_node; - unsigned int send_page_order; int max_host_sndbuf; u32 round_robin_cnt; struct key_map kmap; @@ -453,8 +452,6 @@ enum { =20 /* The ULP mode/submode of an skbuff */ #define skb_ulp_mode(skb) (ULP_SKB_CB(skb)->ulp_mode) -#define TCP_PAGE(sk) (sk->sk_frag.page) -#define TCP_OFF(sk) (sk->sk_frag.offset) =20 static inline struct chtls_dev *to_chtls_dev(struct tls_toe_device *tlsdev) { diff --git a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_io.c b/= drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_io.c index d567e42e1760..8f2dfbe9d3a4 100644 --- a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_io.c +++ b/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_io.c @@ -825,12 +825,6 @@ void skb_entail(struct sock *sk, struct sk_buff *skb, = int flags) ULP_SKB_CB(skb)->flags =3D flags; __skb_queue_tail(&csk->txq, skb); sk->sk_wmem_queued +=3D skb->truesize; - - if (TCP_PAGE(sk) && TCP_OFF(sk)) { - put_page(TCP_PAGE(sk)); - TCP_PAGE(sk) =3D NULL; - TCP_OFF(sk) =3D 0; - } } =20 static struct sk_buff *get_tx_skb(struct sock *sk, int size) @@ -882,16 +876,12 @@ static void push_frames_if_head(struct sock *sk) chtls_push_frames(csk, 1); } =20 -static int chtls_skb_copy_to_page_nocache(struct sock *sk, - struct iov_iter *from, - struct sk_buff *skb, - struct page *page, - int off, int copy) +static int chtls_skb_copy_to_va_nocache(struct sock *sk, struct iov_iter *= from, + struct sk_buff *skb, char *va, int copy) { int err; =20 - err =3D skb_do_copy_data_nocache(sk, skb, from, page_address(page) + - off, copy, skb->len); + err =3D skb_do_copy_data_nocache(sk, skb, from, va, copy, skb->len); if (err) return err; =20 @@ -1114,82 +1104,45 @@ int chtls_sendmsg(struct sock *sk, struct msghdr *m= sg, size_t size) if (err) goto do_fault; } else { + struct page_frag_cache *pfrag =3D &sk->sk_frag; int i =3D skb_shinfo(skb)->nr_frags; - struct page *page =3D TCP_PAGE(sk); - int pg_size =3D PAGE_SIZE; - int off =3D TCP_OFF(sk); - bool merge; - - if (page) - pg_size =3D page_size(page); - if (off < pg_size && - skb_can_coalesce(skb, i, page, off)) { + unsigned int offset, size; + bool merge =3D false; + struct page *page; + void *va; + + size =3D 32U; + page =3D page_frag_alloc_prepare(pfrag, &offset, &size, + &va, sk->sk_allocation); + if (unlikely(!page)) + goto wait_for_memory; + + if (skb_can_coalesce(skb, i, page, offset)) merge =3D true; - goto copy; - } - merge =3D false; - if (i =3D=3D (is_tls_tx(csk) ? (MAX_SKB_FRAGS - 1) : - MAX_SKB_FRAGS)) + else if (i =3D=3D (is_tls_tx(csk) ? (MAX_SKB_FRAGS - 1) : + MAX_SKB_FRAGS)) goto new_buf; =20 - if (page && off =3D=3D pg_size) { - put_page(page); - TCP_PAGE(sk) =3D page =3D NULL; - pg_size =3D PAGE_SIZE; - } - - if (!page) { - gfp_t gfp =3D sk->sk_allocation; - int order =3D cdev->send_page_order; - - if (order) { - page =3D alloc_pages(gfp | __GFP_COMP | - __GFP_NOWARN | - __GFP_NORETRY, - order); - if (page) - pg_size <<=3D order; - } - if (!page) { - page =3D alloc_page(gfp); - pg_size =3D PAGE_SIZE; - } - if (!page) - goto wait_for_memory; - off =3D 0; - } -copy: - if (copy > pg_size - off) - copy =3D pg_size - off; + copy =3D min_t(int, copy, size); if (is_tls_tx(csk)) copy =3D min_t(int, copy, csk->tlshws.txleft); =20 - err =3D chtls_skb_copy_to_page_nocache(sk, &msg->msg_iter, - skb, page, - off, copy); - if (unlikely(err)) { - if (!TCP_PAGE(sk)) { - TCP_PAGE(sk) =3D page; - TCP_OFF(sk) =3D 0; - } + err =3D chtls_skb_copy_to_va_nocache(sk, &msg->msg_iter, + skb, va, copy); + if (unlikely(err)) goto do_fault; - } + /* Update the skb. */ if (merge) { skb_frag_size_add( &skb_shinfo(skb)->frags[i - 1], copy); + page_frag_alloc_commit_noref(pfrag, offset, + copy); } else { - skb_fill_page_desc(skb, i, page, off, copy); - if (off + copy < pg_size) { - /* space left keep page */ - get_page(page); - TCP_PAGE(sk) =3D page; - } else { - TCP_PAGE(sk) =3D NULL; - } + skb_fill_page_desc(skb, i, page, offset, copy); + page_frag_alloc_commit(pfrag, offset, copy); } - TCP_OFF(sk) =3D off + copy; } if (unlikely(skb->len =3D=3D mss)) tx_skb_finalize(skb); diff --git a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_main.c = b/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_main.c index 455a54708be4..ba88b2fc7cd8 100644 --- a/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_main.c +++ b/drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_main.c @@ -34,7 +34,6 @@ static DEFINE_MUTEX(notify_mutex); static RAW_NOTIFIER_HEAD(listen_notify_list); static struct proto chtls_cpl_prot, chtls_cpl_protv6; struct request_sock_ops chtls_rsk_ops, chtls_rsk_opsv6; -static uint send_page_order =3D (14 - PAGE_SHIFT < 0) ? 0 : 14 - PAGE_SHIF= T; =20 static void register_listen_notifier(struct notifier_block *nb) { @@ -273,8 +272,6 @@ static void *chtls_uld_add(const struct cxgb4_lld_info = *info) INIT_WORK(&cdev->deferq_task, process_deferq); spin_lock_init(&cdev->listen_lock); spin_lock_init(&cdev->idr_lock); - cdev->send_page_order =3D min_t(uint, get_order(32768), - send_page_order); cdev->max_host_sndbuf =3D 48 * 1024; =20 if (lldi->vr->key.size) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 0b3f21cba552..5939dfacb6e2 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1598,7 +1598,8 @@ static bool tun_can_build_skb(struct tun_struct *tun,= struct tun_file *tfile, } =20 static struct sk_buff *__tun_build_skb(struct tun_file *tfile, - struct page_frag *alloc_frag, char *buf, + struct page_frag_cache *alloc_frag, + char *buf, unsigned int offset, int buflen, int len, int pad) { struct sk_buff *skb =3D build_skb(buf, buflen); @@ -1609,9 +1610,7 @@ static struct sk_buff *__tun_build_skb(struct tun_fil= e *tfile, skb_reserve(skb, pad); skb_put(skb, len); skb_set_owner_w(skb, tfile->socket.sk); - - get_page(alloc_frag->page); - alloc_frag->offset +=3D buflen; + page_frag_alloc_commit(alloc_frag, offset, buflen); =20 return skb; } @@ -1660,9 +1659,10 @@ static struct sk_buff *tun_build_skb(struct tun_stru= ct *tun, struct virtio_net_hdr *hdr, int len, int *skb_xdp) { - struct page_frag *alloc_frag =3D ¤t->task_frag; + struct page_frag_cache *alloc_frag =3D ¤t->task_frag; struct bpf_prog *xdp_prog; int buflen =3D SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); + unsigned int offset, size; char *buf; size_t copied; int pad =3D TUN_RX_PAD; @@ -1675,14 +1675,13 @@ static struct sk_buff *tun_build_skb(struct tun_str= uct *tun, buflen +=3D SKB_DATA_ALIGN(len + pad); rcu_read_unlock(); =20 - alloc_frag->offset =3D ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES); - if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) + size =3D buflen; + buf =3D page_frag_alloc_va_prepare_align(alloc_frag, &offset, &size, + SMP_CACHE_BYTES, GFP_KERNEL); + if (unlikely(!buf)) return ERR_PTR(-ENOMEM); =20 - buf =3D (char *)page_address(alloc_frag->page) + alloc_frag->offset; - copied =3D copy_page_from_iter(alloc_frag->page, - alloc_frag->offset + pad, - len, from); + copied =3D copy_from_iter(buf + pad, len, from); if (copied !=3D len) return ERR_PTR(-EFAULT); =20 @@ -1692,8 +1691,8 @@ static struct sk_buff *tun_build_skb(struct tun_struc= t *tun, */ if (hdr->gso_type || !xdp_prog) { *skb_xdp =3D 1; - return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, - pad); + return __tun_build_skb(tfile, alloc_frag, buf, offset, buflen, + len, pad); } =20 *skb_xdp =3D 0; @@ -1710,13 +1709,12 @@ static struct sk_buff *tun_build_skb(struct tun_str= uct *tun, =20 act =3D bpf_prog_run_xdp(xdp_prog, &xdp); if (act =3D=3D XDP_REDIRECT || act =3D=3D XDP_TX) { - get_page(alloc_frag->page); - alloc_frag->offset +=3D buflen; + page_frag_alloc_commit(alloc_frag, offset, buflen); } err =3D tun_xdp_act(tun, xdp_prog, &xdp, act); if (err < 0) { if (act =3D=3D XDP_REDIRECT || act =3D=3D XDP_TX) - put_page(alloc_frag->page); + page_frag_free_va(buf); goto out; } =20 @@ -1731,8 +1729,8 @@ static struct sk_buff *tun_build_skb(struct tun_struc= t *tun, rcu_read_unlock(); local_bh_enable(); =20 - return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad); - + return __tun_build_skb(tfile, alloc_frag, buf, offset, buflen, len, + pad); out: rcu_read_unlock(); local_bh_enable(); diff --git a/include/linux/sched.h b/include/linux/sched.h index 3c2abbc587b4..55c4b5fbe845 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -45,6 +44,7 @@ #include #include #include +#include #include =20 /* task_struct member predeclarations (sorted alphabetically): */ @@ -1338,7 +1338,7 @@ struct task_struct { /* Cache last used pipe for splice(): */ struct pipe_inode_info *splice_pipe; =20 - struct page_frag task_frag; + struct page_frag_cache task_frag; =20 #ifdef CONFIG_TASK_DELAY_ACCT struct task_delay_info *delays; diff --git a/include/net/sock.h b/include/net/sock.h index d214aeca72ad..a9c76fa623ce 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -461,7 +461,7 @@ struct sock { struct sk_buff_head sk_write_queue; u32 sk_dst_pending_confirm; u32 sk_pacing_status; /* see enum sk_pacing */ - struct page_frag sk_frag; + struct page_frag_cache sk_frag; struct timer_list sk_timer; =20 unsigned long sk_pacing_rate; /* bytes per second */ @@ -2573,7 +2573,7 @@ static inline void sk_stream_moderate_sndbuf(struct s= ock *sk) * Return: a per task page_frag if context allows that, * otherwise a per socket one. */ -static inline struct page_frag *sk_page_frag(struct sock *sk) +static inline struct page_frag_cache *sk_page_frag(struct sock *sk) { if (sk->sk_use_task_frag) return ¤t->task_frag; @@ -2581,7 +2581,15 @@ static inline struct page_frag *sk_page_frag(struct = sock *sk) return &sk->sk_frag; } =20 -bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag); +struct page *sk_page_frag_alloc_prepare(struct sock *sk, + struct page_frag_cache *pfrag, + unsigned int *size, + unsigned int *offset, void **va); + +struct page *sk_page_frag_alloc_pg_prepare(struct sock *sk, + struct page_frag_cache *pfrag, + unsigned int *size, + unsigned int *offset); =20 /* * Default write policy as shown to user space via poll/select/SIGIO diff --git a/kernel/exit.c b/kernel/exit.c index 41a12630cbbc..8203275fd5ff 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -913,8 +913,7 @@ void __noreturn do_exit(long code) if (tsk->splice_pipe) free_pipe_info(tsk->splice_pipe); =20 - if (tsk->task_frag.page) - put_page(tsk->task_frag.page); + page_frag_cache_drain(&tsk->task_frag); =20 exit_task_stack_account(tsk); =20 diff --git a/kernel/fork.c b/kernel/fork.c index 39a5046c2f0b..8e5abc30c47a 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1158,10 +1158,10 @@ static struct task_struct *dup_task_struct(struct t= ask_struct *orig, int node) tsk->btrace_seq =3D 0; #endif tsk->splice_pipe =3D NULL; - tsk->task_frag.page =3D NULL; tsk->wake_q.next =3D NULL; tsk->worker_private =3D NULL; =20 + page_frag_cache_init(&tsk->task_frag); kcov_task_init(tsk); kmsan_task_create(tsk); kmap_local_fork(tsk); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 4ad4db7403ba..01a02324970a 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -2990,23 +2990,25 @@ static void sock_spd_release(struct splice_pipe_des= c *spd, unsigned int i) put_page(spd->pages[i]); } =20 -static struct page *linear_to_page(struct page *page, unsigned int *len, - unsigned int *offset, - struct sock *sk) +static struct page *linear_to_page(struct page_frag_cache *pfrag, + struct page *page, unsigned int *offset, + unsigned int *len, struct sock *sk) { - struct page_frag *pfrag =3D sk_page_frag(sk); + unsigned int new_len, new_offset; + struct page *frag_page; + void *va; =20 - if (!sk_page_frag_refill(sk, pfrag)) + frag_page =3D sk_page_frag_alloc_prepare(sk, pfrag, &new_offset, + &new_len, &va); + if (!frag_page) return NULL; =20 - *len =3D min_t(unsigned int, *len, pfrag->size - pfrag->offset); + *len =3D min_t(unsigned int, *len, new_len); =20 - memcpy(page_address(pfrag->page) + pfrag->offset, - page_address(page) + *offset, *len); - *offset =3D pfrag->offset; - pfrag->offset +=3D *len; + memcpy(va, page_address(page) + *offset, *len); + *offset =3D new_offset; =20 - return pfrag->page; + return frag_page; } =20 static bool spd_can_coalesce(const struct splice_pipe_desc *spd, @@ -3028,19 +3030,23 @@ static bool spd_fill_page(struct splice_pipe_desc *= spd, bool linear, struct sock *sk) { + struct page_frag_cache *pfrag =3D sk_page_frag(sk); + if (unlikely(spd->nr_pages =3D=3D MAX_SKB_FRAGS)) return true; =20 if (linear) { - page =3D linear_to_page(page, len, &offset, sk); + page =3D linear_to_page(pfrag, page, &offset, len, sk); if (!page) return true; } if (spd_can_coalesce(spd, page, offset)) { spd->partial[spd->nr_pages - 1].len +=3D *len; + page_frag_alloc_commit_noref(pfrag, offset, *len); return false; } - get_page(page); + + page_frag_alloc_commit(pfrag, offset, *len); spd->pages[spd->nr_pages] =3D page; spd->partial[spd->nr_pages].len =3D *len; spd->partial[spd->nr_pages].offset =3D offset; diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 4d75ef9d24bf..803f3903c019 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -27,23 +27,25 @@ static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, = int elem_first_coalesce) int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len, int elem_first_coalesce) { - struct page_frag *pfrag =3D sk_page_frag(sk); + struct page_frag_cache *pfrag =3D sk_page_frag(sk); u32 osize =3D msg->sg.size; int ret =3D 0; =20 len -=3D msg->sg.size; while (len > 0) { + unsigned int frag_offset, frag_len; struct scatterlist *sge; - u32 orig_offset; + struct page *page; int use, i; =20 - if (!sk_page_frag_refill(sk, pfrag)) { + page =3D sk_page_frag_alloc_pg_prepare(sk, pfrag, &frag_offset, + &frag_len); + if (!page) { ret =3D -ENOMEM; goto msg_trim; } =20 - orig_offset =3D pfrag->offset; - use =3D min_t(int, len, pfrag->size - orig_offset); + use =3D min_t(int, len, frag_len); if (!sk_wmem_schedule(sk, use)) { ret =3D -ENOMEM; goto msg_trim; @@ -54,9 +56,10 @@ int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, in= t len, sge =3D &msg->sg.data[i]; =20 if (sk_msg_try_coalesce_ok(msg, elem_first_coalesce) && - sg_page(sge) =3D=3D pfrag->page && - sge->offset + sge->length =3D=3D orig_offset) { + sg_page(sge) =3D=3D page && + sge->offset + sge->length =3D=3D frag_offset) { sge->length +=3D use; + page_frag_alloc_commit_noref(pfrag, frag_offset, use); } else { if (sk_msg_full(msg)) { ret =3D -ENOSPC; @@ -65,14 +68,13 @@ int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, i= nt len, =20 sge =3D &msg->sg.data[msg->sg.end]; sg_unmark_end(sge); - sg_set_page(sge, pfrag->page, use, orig_offset); - get_page(pfrag->page); + sg_set_page(sge, page, use, frag_offset); + page_frag_alloc_commit(pfrag, frag_offset, use); sk_msg_iter_next(msg, end); } =20 sk_mem_charge(sk, use); msg->sg.size +=3D use; - pfrag->offset +=3D use; len -=3D use; } =20 diff --git a/net/core/sock.c b/net/core/sock.c index fe9195186c13..a8318c7f6391 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -2191,10 +2191,7 @@ static void __sk_destruct(struct rcu_head *head) pr_debug("%s: optmem leakage (%d bytes) detected\n", __func__, atomic_read(&sk->sk_omem_alloc)); =20 - if (sk->sk_frag.page) { - put_page(sk->sk_frag.page); - sk->sk_frag.page =3D NULL; - } + page_frag_cache_drain(&sk->sk_frag); =20 /* We do not need to acquire sk->sk_peer_lock, we are the last user. */ put_cred(sk->sk_peer_cred); @@ -2935,16 +2932,43 @@ bool skb_page_frag_refill(unsigned int sz, struct p= age_frag *pfrag, gfp_t gfp) } EXPORT_SYMBOL(skb_page_frag_refill); =20 -bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag) +struct page *sk_page_frag_alloc_prepare(struct sock *sk, + struct page_frag_cache *pfrag, + unsigned int *offset, + unsigned int *size, void **va) { - if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation))) - return true; + struct page *page; + + *size =3D 32U; + page =3D page_frag_alloc_prepare(pfrag, offset, size, va, + sk->sk_allocation); + if (likely(page)) + return page; =20 sk_enter_memory_pressure(sk); sk_stream_moderate_sndbuf(sk); - return false; + return NULL; +} +EXPORT_SYMBOL(sk_page_frag_alloc_prepare); + +struct page *sk_page_frag_alloc_pg_prepare(struct sock *sk, + struct page_frag_cache *pfrag, + unsigned int *offset, + unsigned int *size) +{ + struct page *page; + + *size =3D 32U; + page =3D page_frag_alloc_pg_prepare(pfrag, offset, size, + sk->sk_allocation); + if (likely(page)) + return page; + + sk_enter_memory_pressure(sk); + sk_stream_moderate_sndbuf(sk); + return NULL; } -EXPORT_SYMBOL(sk_page_frag_refill); +EXPORT_SYMBOL(sk_page_frag_alloc_pg_prepare); =20 void __lock_sock(struct sock *sk) __releases(&sk->sk_lock.slock) @@ -3478,8 +3502,8 @@ void sock_init_data_uid(struct socket *sock, struct s= ock *sk, kuid_t uid) sk->sk_error_report =3D sock_def_error_report; sk->sk_destruct =3D sock_def_destruct; =20 - sk->sk_frag.page =3D NULL; - sk->sk_frag.offset =3D 0; + page_frag_cache_init(&sk->sk_frag); + sk->sk_peek_off =3D -1; =20 sk->sk_peer_pid =3D NULL; diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 1fe794967211..28b66922e298 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -952,7 +952,7 @@ static int __ip_append_data(struct sock *sk, struct flowi4 *fl4, struct sk_buff_head *queue, struct inet_cork *cork, - struct page_frag *pfrag, + struct page_frag_cache *pfrag, int getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb), void *from, int length, int transhdrlen, @@ -1228,31 +1228,40 @@ static int __ip_append_data(struct sock *sk, wmem_alloc_delta +=3D copy; } else if (!zc) { int i =3D skb_shinfo(skb)->nr_frags; + unsigned int frag_offset, frag_size; + struct page *page; + void *va; =20 err =3D -ENOMEM; - if (!sk_page_frag_refill(sk, pfrag)) + page =3D sk_page_frag_alloc_prepare(sk, pfrag, + &frag_offset, + &frag_size, &va); + if (!page) goto error; =20 skb_zcopy_downgrade_managed(skb); - if (!skb_can_coalesce(skb, i, pfrag->page, - pfrag->offset)) { + copy =3D min_t(int, copy, frag_size); + + if (!skb_can_coalesce(skb, i, page, frag_offset)) { err =3D -EMSGSIZE; if (i =3D=3D MAX_SKB_FRAGS) goto error; =20 - __skb_fill_page_desc(skb, i, pfrag->page, - pfrag->offset, 0); + __skb_fill_page_desc(skb, i, page, frag_offset, + copy); skb_shinfo(skb)->nr_frags =3D ++i; - get_page(pfrag->page); + page_frag_alloc_commit(pfrag, frag_offset, + copy); + } else { + skb_frag_size_add( + &skb_shinfo(skb)->frags[i - 1], copy); + page_frag_alloc_commit_noref(pfrag, frag_offset, + copy); } - copy =3D min_t(int, copy, pfrag->size - pfrag->offset); - if (getfrag(from, - page_address(pfrag->page) + pfrag->offset, - offset, copy, skb->len, skb) < 0) + + if (getfrag(from, va, offset, copy, skb->len, skb) < 0) goto error_efault; =20 - pfrag->offset +=3D copy; - skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); skb_len_add(skb, copy); wmem_alloc_delta +=3D copy; } else { diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 92ee60492314..3e4966dfab04 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1177,13 +1177,17 @@ int tcp_sendmsg_locked(struct sock *sk, struct msgh= dr *msg, size_t size) if (zc =3D=3D 0) { bool merge =3D true; int i =3D skb_shinfo(skb)->nr_frags; - struct page_frag *pfrag =3D sk_page_frag(sk); - - if (!sk_page_frag_refill(sk, pfrag)) + struct page_frag_cache *pfrag =3D sk_page_frag(sk); + unsigned int offset, size; + struct page *page; + void *va; + + page =3D sk_page_frag_alloc_prepare(sk, pfrag, &offset, + &size, &va); + if (!page) goto wait_for_space; =20 - if (!skb_can_coalesce(skb, i, pfrag->page, - pfrag->offset)) { + if (!skb_can_coalesce(skb, i, page, offset)) { if (i >=3D READ_ONCE(sysctl_max_skb_frags)) { tcp_mark_push(tp, skb); goto new_segment; @@ -1191,7 +1195,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr= *msg, size_t size) merge =3D false; } =20 - copy =3D min_t(int, copy, pfrag->size - pfrag->offset); + copy =3D min_t(int, copy, size); =20 if (unlikely(skb_zcopy_pure(skb) || skb_zcopy_managed(skb))) { if (tcp_downgrade_zcopy_pure(sk, skb)) @@ -1203,22 +1207,19 @@ int tcp_sendmsg_locked(struct sock *sk, struct msgh= dr *msg, size_t size) if (!copy) goto wait_for_space; =20 - err =3D skb_copy_to_page_nocache(sk, &msg->msg_iter, skb, - pfrag->page, - pfrag->offset, - copy); + err =3D skb_copy_to_va_nocache(sk, &msg->msg_iter, skb, + va, copy); if (err) goto do_error; =20 /* Update the skb. */ if (merge) { skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + page_frag_alloc_commit_noref(pfrag, offset, copy); } else { - skb_fill_page_desc(skb, i, pfrag->page, - pfrag->offset, copy); - page_ref_inc(pfrag->page); + skb_fill_page_desc(skb, i, page, offset, copy); + page_frag_alloc_commit(pfrag, offset, copy); } - pfrag->offset +=3D copy; } else if (zc =3D=3D MSG_ZEROCOPY) { /* First append to a fragless skb builds initial * pure zerocopy skb @@ -3101,11 +3102,7 @@ int tcp_disconnect(struct sock *sk, int flags) =20 WARN_ON(inet->inet_num && !icsk->icsk_bind_hash); =20 - if (sk->sk_frag.page) { - put_page(sk->sk_frag.page); - sk->sk_frag.page =3D NULL; - sk->sk_frag.offset =3D 0; - } + page_frag_cache_drain(&sk->sk_frag); sk_error_report(sk); return 0; } diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 9282fafc0e61..ba54d3fd13cc 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -3929,9 +3929,12 @@ static int tcp_send_syn_data(struct sock *sk, struct= sk_buff *syn) struct inet_connection_sock *icsk =3D inet_csk(sk); struct tcp_sock *tp =3D tcp_sk(sk); struct tcp_fastopen_request *fo =3D tp->fastopen_req; - struct page_frag *pfrag =3D sk_page_frag(sk); + struct page_frag_cache *pfrag =3D sk_page_frag(sk); + unsigned int offset, size; struct sk_buff *syn_data; int space, err =3D 0; + struct page *page; + void *va; =20 tp->rx_opt.mss_clamp =3D tp->advmss; /* If MSS is not cached */ if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie)) @@ -3950,30 +3953,31 @@ static int tcp_send_syn_data(struct sock *sk, struc= t sk_buff *syn) =20 space =3D min_t(size_t, space, fo->size); =20 - if (space && - !skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE), - pfrag, sk->sk_allocation)) - goto fallback; + if (space) { + size =3D min_t(size_t, space, PAGE_SIZE); + page =3D page_frag_alloc_prepare(pfrag, &offset, &size, &va, + sk->sk_allocation); + if (!page) + goto fallback; + } + syn_data =3D tcp_stream_alloc_skb(sk, sk->sk_allocation, false); if (!syn_data) goto fallback; memcpy(syn_data->cb, syn->cb, sizeof(syn->cb)); if (space) { - space =3D min_t(size_t, space, pfrag->size - pfrag->offset); + space =3D min_t(size_t, space, size); space =3D tcp_wmem_schedule(sk, space); } if (space) { - space =3D copy_page_from_iter(pfrag->page, pfrag->offset, - space, &fo->data->msg_iter); + space =3D _copy_from_iter(va, space, &fo->data->msg_iter); if (unlikely(!space)) { tcp_skb_tsorted_anchor_cleanup(syn_data); kfree_skb(syn_data); goto fallback; } - skb_fill_page_desc(syn_data, 0, pfrag->page, - pfrag->offset, space); - page_ref_inc(pfrag->page); - pfrag->offset +=3D space; + skb_fill_page_desc(syn_data, 0, page, offset, space); + page_frag_alloc_commit(pfrag, offset, space); skb_len_add(syn_data, space); skb_zcopy_set(syn_data, fo->uarg, NULL); } diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index b9dd3a66e423..95a4dbf1a7b1 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1404,7 +1404,7 @@ static int __ip6_append_data(struct sock *sk, struct sk_buff_head *queue, struct inet_cork_full *cork_full, struct inet6_cork *v6_cork, - struct page_frag *pfrag, + struct page_frag_cache *pfrag, int getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb), void *from, size_t length, int transhdrlen, @@ -1745,32 +1745,41 @@ static int __ip6_append_data(struct sock *sk, copy =3D err; wmem_alloc_delta +=3D copy; } else if (!zc) { + unsigned int frag_offset, frag_size; int i =3D skb_shinfo(skb)->nr_frags; + struct page *page; + void *va; =20 err =3D -ENOMEM; - if (!sk_page_frag_refill(sk, pfrag)) + page =3D sk_page_frag_alloc_prepare(sk, pfrag, + &frag_offset, + &frag_size, &va); + if (!page) goto error; =20 skb_zcopy_downgrade_managed(skb); - if (!skb_can_coalesce(skb, i, pfrag->page, - pfrag->offset)) { + copy =3D min_t(int, copy, frag_size); + + if (!skb_can_coalesce(skb, i, page, frag_offset)) { err =3D -EMSGSIZE; if (i =3D=3D MAX_SKB_FRAGS) goto error; =20 - __skb_fill_page_desc(skb, i, pfrag->page, - pfrag->offset, 0); + __skb_fill_page_desc(skb, i, page, frag_offset, + copy); skb_shinfo(skb)->nr_frags =3D ++i; - get_page(pfrag->page); + page_frag_alloc_commit(pfrag, frag_offset, + copy); + } else { + skb_frag_size_add( + &skb_shinfo(skb)->frags[i - 1], copy); + page_frag_alloc_commit_noref(pfrag, frag_offset, + copy); } - copy =3D min_t(int, copy, pfrag->size - pfrag->offset); - if (getfrag(from, - page_address(pfrag->page) + pfrag->offset, - offset, copy, skb->len, skb) < 0) + + if (getfrag(from, va, offset, copy, skb->len, skb) < 0) goto error_efault; =20 - pfrag->offset +=3D copy; - skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); skb->len +=3D copy; skb->data_len +=3D copy; skb->truesize +=3D copy; diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c index 2f191e50d4fc..6b837e85b683 100644 --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c @@ -803,13 +803,17 @@ static int kcm_sendmsg(struct socket *sock, struct ms= ghdr *msg, size_t len) while (msg_data_left(msg)) { bool merge =3D true; int i =3D skb_shinfo(skb)->nr_frags; - struct page_frag *pfrag =3D sk_page_frag(sk); - - if (!sk_page_frag_refill(sk, pfrag)) + struct page_frag_cache *pfrag =3D sk_page_frag(sk); + unsigned int offset, size; + struct page *page; + void *va; + + page =3D sk_page_frag_alloc_prepare(sk, pfrag, &offset, &size, + &va); + if (!page) goto wait_for_memory; =20 - if (!skb_can_coalesce(skb, i, pfrag->page, - pfrag->offset)) { + if (!skb_can_coalesce(skb, i, page, offset)) { if (i =3D=3D MAX_SKB_FRAGS) { struct sk_buff *tskb; =20 @@ -850,15 +854,12 @@ static int kcm_sendmsg(struct socket *sock, struct ms= ghdr *msg, size_t len) if (head !=3D skb) head->truesize +=3D copy; } else { - copy =3D min_t(int, msg_data_left(msg), - pfrag->size - pfrag->offset); + copy =3D min_t(int, msg_data_left(msg), size); if (!sk_wmem_schedule(sk, copy)) goto wait_for_memory; =20 - err =3D skb_copy_to_page_nocache(sk, &msg->msg_iter, skb, - pfrag->page, - pfrag->offset, - copy); + err =3D skb_copy_to_va_nocache(sk, &msg->msg_iter, skb, + va, copy); if (err) goto out_error; =20 @@ -866,13 +867,12 @@ static int kcm_sendmsg(struct socket *sock, struct ms= ghdr *msg, size_t len) if (merge) { skb_frag_size_add( &skb_shinfo(skb)->frags[i - 1], copy); + page_frag_alloc_commit_noref(pfrag, offset, copy); } else { - skb_fill_page_desc(skb, i, pfrag->page, - pfrag->offset, copy); - get_page(pfrag->page); + skb_fill_page_desc(skb, i, page, offset, copy); + page_frag_alloc_commit(pfrag, offset, copy); } =20 - pfrag->offset +=3D copy; } =20 copied +=3D copy; diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index 995b53cd021c..8f8dbd96d60d 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -957,17 +957,16 @@ static bool mptcp_skb_can_collapse_to(u64 write_seq, } =20 /* we can append data to the given data frag if: - * - there is space available in the backing page_frag - * - the data frag tail matches the current page_frag free offset + * - the data frag tail matches the current page and offset * - the data frag end sequence number matches the current write seq */ static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, - const struct page_frag *pfrag, + const struct page *page, + const unsigned int offset, const struct mptcp_data_frag *df) { - return df && pfrag->page =3D=3D df->page && - pfrag->size - pfrag->offset > 0 && - pfrag->offset =3D=3D (df->offset + df->data_len) && + return df && page =3D=3D df->page && + offset =3D=3D (df->offset + df->data_len) && df->data_seq + df->data_len =3D=3D msk->write_seq; } =20 @@ -1082,30 +1081,36 @@ static void mptcp_enter_memory_pressure(struct sock= *sk) /* ensure we get enough memory for the frag hdr, beyond some minimal amoun= t of * data */ -static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfra= g) +static struct page *mptcp_page_frag_alloc_prepare(struct sock *sk, + struct page_frag_cache *pfrag, + unsigned int *offset, + unsigned int *size, void **va) { - if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), - pfrag, sk->sk_allocation))) - return true; + struct page *page; + + page =3D page_frag_alloc_prepare(pfrag, offset, size, va, + sk->sk_allocation); + if (likely(page)) + return page; =20 mptcp_enter_memory_pressure(sk); - return false; + return NULL; } =20 static struct mptcp_data_frag * -mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfra= g, - int orig_offset) +mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page *page, + unsigned int orig_offset) { int offset =3D ALIGN(orig_offset, sizeof(long)); struct mptcp_data_frag *dfrag; =20 - dfrag =3D (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); + dfrag =3D (struct mptcp_data_frag *)(page_to_virt(page) + offset); dfrag->data_len =3D 0; dfrag->data_seq =3D msk->write_seq; dfrag->overhead =3D offset - orig_offset + sizeof(struct mptcp_data_frag); dfrag->offset =3D offset + sizeof(struct mptcp_data_frag); dfrag->already_sent =3D 0; - dfrag->page =3D pfrag->page; + dfrag->page =3D page; =20 return dfrag; } @@ -1788,7 +1793,7 @@ static u32 mptcp_send_limit(const struct sock *sk) static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) { struct mptcp_sock *msk =3D mptcp_sk(sk); - struct page_frag *pfrag; + struct page_frag_cache *pfrag; size_t copied =3D 0; int ret =3D 0; long timeo; @@ -1827,9 +1832,12 @@ static int mptcp_sendmsg(struct sock *sk, struct msg= hdr *msg, size_t len) while (msg_data_left(msg)) { int total_ts, frag_truesize =3D 0; struct mptcp_data_frag *dfrag; - bool dfrag_collapsed; - size_t psize, offset; + bool dfrag_collapsed =3D false; + unsigned int offset, size; + struct page *page; + size_t psize; u32 copy_limit; + void *va; =20 /* ensure fitting the notsent_lowat() constraint */ copy_limit =3D mptcp_send_limit(sk); @@ -1840,21 +1848,31 @@ static int mptcp_sendmsg(struct sock *sk, struct ms= ghdr *msg, size_t len) * page allocator */ dfrag =3D mptcp_pending_tail(sk); - dfrag_collapsed =3D mptcp_frag_can_collapse_to(msk, pfrag, dfrag); + size =3D 32U; + page =3D mptcp_page_frag_alloc_prepare(sk, pfrag, &offset, &size, + &va); + if (!page) + goto wait_for_memory; + + dfrag_collapsed =3D mptcp_frag_can_collapse_to(msk, page, offset, + dfrag); if (!dfrag_collapsed) { - if (!mptcp_page_frag_refill(sk, pfrag)) + size =3D 32U + sizeof(struct mptcp_data_frag); + page =3D mptcp_page_frag_alloc_prepare(sk, pfrag, &offset, + &size, &va); + if (!page) goto wait_for_memory; =20 - dfrag =3D mptcp_carve_data_frag(msk, pfrag, pfrag->offset); + dfrag =3D mptcp_carve_data_frag(msk, page, offset); frag_truesize =3D dfrag->overhead; + va +=3D dfrag->overhead; } =20 /* we do not bound vs wspace, to allow a single packet. * memory accounting will prevent execessive memory usage * anyway */ - offset =3D dfrag->offset + dfrag->data_len; - psize =3D pfrag->size - offset; + psize =3D size - frag_truesize; psize =3D min_t(size_t, psize, msg_data_left(msg)); psize =3D min_t(size_t, psize, copy_limit); total_ts =3D psize + frag_truesize; @@ -1862,8 +1880,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msgh= dr *msg, size_t len) if (!sk_wmem_schedule(sk, total_ts)) goto wait_for_memory; =20 - ret =3D do_copy_data_nocache(sk, psize, &msg->msg_iter, - page_address(dfrag->page) + offset); + ret =3D do_copy_data_nocache(sk, psize, &msg->msg_iter, va); if (ret) goto do_error; =20 @@ -1872,7 +1889,6 @@ static int mptcp_sendmsg(struct sock *sk, struct msgh= dr *msg, size_t len) copied +=3D psize; dfrag->data_len +=3D psize; frag_truesize +=3D psize; - pfrag->offset +=3D frag_truesize; WRITE_ONCE(msk->write_seq, msk->write_seq + psize); =20 /* charge data on mptcp pending queue to the msk socket @@ -1880,11 +1896,15 @@ static int mptcp_sendmsg(struct sock *sk, struct ms= ghdr *msg, size_t len) */ sk_wmem_queued_add(sk, frag_truesize); if (!dfrag_collapsed) { - get_page(dfrag->page); + page_frag_alloc_commit(pfrag, offset, frag_truesize); list_add_tail(&dfrag->list, &msk->rtx_queue); if (!msk->first_pending) WRITE_ONCE(msk->first_pending, dfrag); + } else { + page_frag_alloc_commit_noref(pfrag, offset, + frag_truesize); } + pr_debug("msk=3D%p dfrag at seq=3D%llu len=3D%u sent=3D%u new=3D%d", msk, dfrag->data_seq, dfrag->data_len, dfrag->already_sent, !dfrag_collapsed); diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c index bf8ed36b1ad6..437faac3305c 100644 --- a/net/tls/tls_device.c +++ b/net/tls/tls_device.c @@ -255,25 +255,43 @@ static void tls_device_resync_tx(struct sock *sk, str= uct tls_context *tls_ctx, clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags); } =20 -static void tls_append_frag(struct tls_record_info *record, - struct page_frag *pfrag, - int size) +static void tls_append_pfrag(struct tls_record_info *record, + struct page_frag_cache *pfrag, struct page *page, + unsigned int offset, unsigned int size) { skb_frag_t *frag; =20 frag =3D &record->frags[record->num_frags - 1]; - if (skb_frag_page(frag) =3D=3D pfrag->page && - skb_frag_off(frag) + skb_frag_size(frag) =3D=3D pfrag->offset) { + if (skb_frag_page(frag) =3D=3D page && + skb_frag_off(frag) + skb_frag_size(frag) =3D=3D offset) { skb_frag_size_add(frag, size); + page_frag_alloc_commit_noref(pfrag, offset, size); } else { ++frag; - skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset, - size); + skb_frag_fill_page_desc(frag, page, offset, size); ++record->num_frags; - get_page(pfrag->page); + page_frag_alloc_commit(pfrag, offset, size); + } + + record->len +=3D size; +} + +static void tls_append_page(struct tls_record_info *record, struct page *p= age, + unsigned int offset, unsigned int size) +{ + skb_frag_t *frag; + + frag =3D &record->frags[record->num_frags - 1]; + if (skb_frag_page(frag) =3D=3D page && + skb_frag_off(frag) + skb_frag_size(frag) =3D=3D offset) { + skb_frag_size_add(frag, size); + } else { + ++frag; + skb_frag_fill_page_desc(frag, page, offset, size); + ++record->num_frags; + get_page(page); } =20 - pfrag->offset +=3D size; record->len +=3D size; } =20 @@ -314,11 +332,12 @@ static int tls_push_record(struct sock *sk, static void tls_device_record_close(struct sock *sk, struct tls_context *ctx, struct tls_record_info *record, - struct page_frag *pfrag, + struct page_frag_cache *pfrag, unsigned char record_type) { struct tls_prot_info *prot =3D &ctx->prot_info; - struct page_frag dummy_tag_frag; + unsigned int offset, size; + struct page *page; =20 /* append tag * device will fill in the tag, we just need to append a placeholder @@ -326,13 +345,14 @@ static void tls_device_record_close(struct sock *sk, * increases frag count) * if we can't allocate memory now use the dummy page */ - if (unlikely(pfrag->size - pfrag->offset < prot->tag_size) && - !skb_page_frag_refill(prot->tag_size, pfrag, sk->sk_allocation)) { - dummy_tag_frag.page =3D dummy_page; - dummy_tag_frag.offset =3D 0; - pfrag =3D &dummy_tag_frag; + size =3D prot->tag_size; + page =3D page_frag_alloc_pg_prepare(pfrag, &offset, &size, + sk->sk_allocation); + if (unlikely(!page)) { + tls_append_page(record, dummy_page, 0, prot->tag_size); + } else { + tls_append_pfrag(record, pfrag, page, offset, prot->tag_size); } - tls_append_frag(record, pfrag, prot->tag_size); =20 /* fill prepend */ tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]), @@ -340,23 +360,33 @@ static void tls_device_record_close(struct sock *sk, record_type); } =20 -static int tls_create_new_record(struct tls_offload_context_tx *offload_ct= x, - struct page_frag *pfrag, +static int tls_create_new_record(struct sock *sk, + struct tls_offload_context_tx *offload_ctx, + struct page_frag_cache *pfrag, size_t prepend_size) { struct tls_record_info *record; + unsigned int offset, size; + struct page *page; skb_frag_t *frag; =20 + size =3D prepend_size; + page =3D page_frag_alloc_pg_prepare(pfrag, &offset, &size, + sk->sk_allocation); + if (!page) { + READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk); + sk_stream_moderate_sndbuf(sk); + return -ENOMEM; + } + record =3D kmalloc(sizeof(*record), GFP_KERNEL); if (!record) return -ENOMEM; =20 frag =3D &record->frags[0]; - skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset, - prepend_size); + skb_frag_fill_page_desc(frag, page, offset, prepend_size); =20 - get_page(pfrag->page); - pfrag->offset +=3D prepend_size; + page_frag_alloc_commit(pfrag, offset, prepend_size); =20 record->num_frags =3D 1; record->len =3D prepend_size; @@ -364,33 +394,21 @@ static int tls_create_new_record(struct tls_offload_c= ontext_tx *offload_ctx, return 0; } =20 -static int tls_do_allocation(struct sock *sk, - struct tls_offload_context_tx *offload_ctx, - struct page_frag *pfrag, - size_t prepend_size) +static struct page *tls_do_allocation(struct sock *sk, + struct tls_offload_context_tx *ctx, + struct page_frag_cache *pfrag, + size_t prepend_size, unsigned int *offset, + unsigned int *size, void **va) { - int ret; - - if (!offload_ctx->open_record) { - if (unlikely(!skb_page_frag_refill(prepend_size, pfrag, - sk->sk_allocation))) { - READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk); - sk_stream_moderate_sndbuf(sk); - return -ENOMEM; - } + if (!ctx->open_record) { + int ret; =20 - ret =3D tls_create_new_record(offload_ctx, pfrag, prepend_size); + ret =3D tls_create_new_record(sk, ctx, pfrag, prepend_size); if (ret) - return ret; - - if (pfrag->size > pfrag->offset) - return 0; + return NULL; } =20 - if (!sk_page_frag_refill(sk, pfrag)) - return -ENOMEM; - - return 0; + return sk_page_frag_alloc_prepare(sk, pfrag, offset, size, va); } =20 static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter = *i) @@ -427,8 +445,8 @@ static int tls_push_data(struct sock *sk, struct tls_prot_info *prot =3D &tls_ctx->prot_info; struct tls_offload_context_tx *ctx =3D tls_offload_ctx_tx(tls_ctx); struct tls_record_info *record; + struct page_frag_cache *pfrag; int tls_push_record_flags; - struct page_frag *pfrag; size_t orig_size =3D size; u32 max_open_record_len; bool more =3D false; @@ -465,8 +483,13 @@ static int tls_push_data(struct sock *sk, max_open_record_len =3D TLS_MAX_PAYLOAD_SIZE + prot->prepend_size; do { - rc =3D tls_do_allocation(sk, ctx, pfrag, prot->prepend_size); - if (unlikely(rc)) { + unsigned int frag_offset, frag_size; + struct page *page; + void *va; + + page =3D tls_do_allocation(sk, ctx, pfrag, prot->prepend_size, + &frag_offset, &frag_size, &va); + if (unlikely(!page)) { rc =3D sk_stream_wait_memory(sk, &timeo); if (!rc) continue; @@ -494,8 +517,8 @@ static int tls_push_data(struct sock *sk, =20 copy =3D min_t(size_t, size, max_open_record_len - record->len); if (copy && (flags & MSG_SPLICE_PAGES)) { - struct page_frag zc_pfrag; - struct page **pages =3D &zc_pfrag.page; + struct page *splice_page; + struct page **pages =3D &splice_page; size_t off; =20 rc =3D iov_iter_extract_pages(iter, &pages, @@ -507,24 +530,22 @@ static int tls_push_data(struct sock *sk, } copy =3D rc; =20 - if (WARN_ON_ONCE(!sendpage_ok(zc_pfrag.page))) { + if (WARN_ON_ONCE(!sendpage_ok(splice_page))) { iov_iter_revert(iter, copy); rc =3D -EIO; goto handle_error; } =20 - zc_pfrag.offset =3D off; - zc_pfrag.size =3D copy; - tls_append_frag(record, &zc_pfrag, copy); + tls_append_page(record, splice_page, off, copy); } else if (copy) { - copy =3D min_t(size_t, copy, pfrag->size - pfrag->offset); + copy =3D min_t(size_t, copy, frag_size); =20 - rc =3D tls_device_copy_data(page_address(pfrag->page) + - pfrag->offset, copy, - iter); + rc =3D tls_device_copy_data(va, copy, iter); if (rc) goto handle_error; - tls_append_frag(record, pfrag, copy); + + tls_append_pfrag(record, pfrag, page, frag_offset, + copy); } =20 size -=3D copy; --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86D9538F9B; Sun, 7 Apr 2024 13:11:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495491; cv=none; b=AaDRiXsNWJBl58pVnnPoid6A2rMwolZ4wCk76KrWB8//4PadR2ME+OMTaWx4nC52amXx80btdcZqO0aYYH52wCvFtvB7D1Vu9jmFf2tOhPsoObx9Jb0p4UiMwracGbDFCYIyDFWgFB+os1dnA6+cicBQS3EFkO42KchNs+Nxg78= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495491; c=relaxed/simple; bh=Bp7IreIK7SyJ9cJt8kOTpaanWwtNLojIjlShgciOf/4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UWWldwP1sI36SUpLJ5ndVgDFkzu1xrAocPiMkN7H7zpYRpD+iO6T6yv2aE6smDfWuDMYG8N4rvg3bVJwYxoO+6pUyIwWd+c4za7pmkiF7HdQvPneN+xdzk7KVjeY1teQ5ROiPQRpQvgwNDLSC2COMuv58aCHeug38dcWY9LFH34= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" 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. =20 +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) +=3D memory-failure.o obj-$(CONFIG_HWPOISON_INJECT) +=3D hwpoison-inject.o obj-$(CONFIG_DEBUG_KMEMLEAK) +=3D kmemleak.o obj-$(CONFIG_DEBUG_RODATA_TEST) +=3D rodata_test.o +obj-$(CONFIG_DEBUG_PAGE_FRAG_TEST) +=3D page_frag_test.o obj-$(CONFIG_DEBUG_VM_PGTABLE) +=3D debug_vm_pgtable.o obj-$(CONFIG_PAGE_OWNER) +=3D page_owner.o obj-$(CONFIG_MEMORY_ISOLATION) +=3D 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 =3D 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 =3D 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 =3D 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 =3D kmalloc_node(size, gfp, cpu_to_node(i)); + else + slot =3D __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] =3D 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 =3D 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 <=3D 0 || nr_objs > OBJPOOL_NR_OBJECT_MAX) + return -EINVAL; + + /* calculate capacity of percpu objpool_slot */ + capacity =3D roundup_pow_of_two(nr_objs); + if (!capacity) + return -EINVAL; + + gfp =3D gfp & ~__GFP_ZERO; + + /* initialize objpool pool */ + memset(pool, 0, sizeof(struct objpool_head)); + pool->nr_cpus =3D nr_cpu_ids; + pool->capacity =3D capacity; + slot_size =3D pool->nr_cpus * sizeof(struct objpool_slot *); + pool->cpu_slots =3D kzalloc(slot_size, gfp); + if (!pool->cpu_slots) + return -ENOMEM; + + /* initialize per-cpu slots */ + rc =3D 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 =3D pool->cpu_slots[cpu]; + u32 head, tail; + + /* loading tail and head as a local snapshot, tail first */ + tail =3D READ_ONCE(slot->tail); + + do { + head =3D READ_ONCE(slot->head); + /* slot is full */ + if (unlikely(tail - head >=3D 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 =3D 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 =3D pool->cpu_slots[cpu]; + /* load head snapshot, other cpus may change it */ + u32 head =3D smp_load_acquire(&slot->head); + + while (head !=3D 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 !=3D head && last - head <=3D 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 >=3D pool->capacity) { + head =3D READ_ONCE(slot->head); + continue; + } + + /* obj must be retrieved before moving forward head */ + obj =3D 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 =3D NULL; + unsigned long flags; + int i, cpu; + + /* disable local irq to avoid preemption & interruption */ + raw_local_irq_save(flags); + + cpu =3D raw_smp_processor_id(); + for (i =3D 0; i < num_possible_cpus(); i++) { + obj =3D objpool_try_get_slot(pool, cpu); + if (obj) + break; + cpu =3D 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 =3D 512; +static int nr_test =3D 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 =3D arg; + int nr =3D nr_test; + + pr_info("page_frag pop test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + void *obj =3D 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 =3D arg; + int nr =3D nr_test; + + pr_info("page_frag push test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + unsigned int size =3D get_random_u32(); + void *va; + int ret; + + size =3D clamp(size, sizeof(unsigned int), PAGE_SIZE); + va =3D page_frag_alloc_va(&test_frag, size, GFP_KERNEL); + if (!va) + continue; + + ret =3D 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 =3D objpool_init(&ptr_pool, nr_objs, GFP_KERNEL); + if (ret) + return ret; + + tsk_push =3D 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 =3D 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 =3D 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 =3D (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"); --=20 2.33.0 From nobody Thu Sep 19 23:20:50 2024 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2162487BF; Sun, 7 Apr 2024 13:11:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495496; cv=none; b=Y7jM1zv5vVOriL5pra2kB1JRCdV/T3tNT45rQJLLcayvFQCtNXzvqgOxq+HFvZCSj4+EnJIFiQoSgwtx7cP2yOFavSDAOkdEnFJ2NNCUaJTFeeIA+p0KutXfyqr0EP4/mdtSUy/0apQ2G0/kDUkieczdywWsDhPom512oKkm3jw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712495496; c=relaxed/simple; bh=SEey+bss8z2u+WwX2dsVTGkvGPe6bvz4cljBhfIUzAs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=k5gfHIh0fTzjAqOMq2UYsicwtdXukLNRExguLzEw+n9gVJj2wImKSUnVfb/Dj9nHMZ3622EotGCecSdv/03pk0i8rh6lNJUSq6iAfwpMhlLe7+ye/lmdsruLtXRlze0pYwAEp83nexGBcNPQR1PhPat2V02VE/uv/grBerYHfO0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500005.china.huawei.com (7.185.36.74) 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 + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Page fragments =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 -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 +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. 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 +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +As the design and implementation of page_frag API, the allocation side doe= s 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_p= g(), +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 ca= ller +requests the minimum memory it need and the prepare API will return the ma= ximum +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_prepa= re + 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 =20 +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_cach= e.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 }; =20 +/** + * 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 =3D NULL; } =20 +/** + * 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 *n= c) { 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 frag= sz, gfp_t gfp_mask); =20 +/** + * 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; } =20 +/** + * __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 p= age_frag_cache *nc, return page_frag_alloc_va(nc, fragsz, gfp_mask); } =20 +/** + * page_frag_alloc_va_align() - Alloc a page fragment with aligning requir= ement. + * @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 frag= ment + * 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 pag= e_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, align); } =20 +/** + * 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 =E2=80=98size=E2=80=99, 's= ize' 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; } =20 +/** + * page_frag_alloc_va_prepare_align() - Prepare allocing a page fragment w= ith + * 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 =E2=80=98size=E2= =80=99, '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_cach= e *nc, unsigned int *offset, unsigned int *size, @@ -144,6 +221,19 @@ static inline void *__page_frag_alloc_pg_prepare(struc= t page_frag_cache *nc, return va; } =20 +/** + * 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 =E2=80=98size=E2=80=99, 's= ize' 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 =3D NULL; \ @@ -179,6 +269,21 @@ static inline void *__page_frag_alloc_prepare(struct p= age_frag_cache *nc, return nc_va; } =20 +/** + * 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 =E2=80=98size=E2=80=99, 's= ize' is also used to + * report the maximum size of the page fragment. Return both 'page' and 'v= a' 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 =3D NULL; \ @@ -191,6 +296,14 @@ static inline void *__page_frag_alloc_prepare(struct p= age_frag_cache *nc, __page; \ }) =20 +/** + * 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 =3D offset + size; } =20 +/** + * 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 =3D offset + size; } =20 +/** + * 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); =20 #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 w= ithin + * 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 ne= twork + * 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 with= in 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 a= nd + * tracks allows multiple calls to make use of a cached page. The advanta= ge 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 cachin= g 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 disabl= ed + * 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 driver= s 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 ma= y be + * called. The "netdev" prefixed functions are usable in any context as t= hese + * 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 pa= ge + * 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 gen= eric + * way of tearing down a page cache. For this reason __page_frag_cache_dr= ain + * was implemented. It allows for freeing multiple references from a sing= le + * 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. */ =20 #include @@ -57,6 +86,10 @@ static bool __page_frag_cache_refill(struct page_frag_ca= che *nc, return true; } =20 +/** + * 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) --=20 2.33.0