From nobody Tue Feb 10 12:57:53 2026 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 A12EC8614D; Wed, 8 May 2024 13:36:59 +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=1715175421; cv=none; b=us4AWQS7eKC/cSneOHgcq3j/iWgA2Em+hbJJQa/SsGcAgQTkCd3j4L/8rDgoY+HdAWIvYlNc0vvHszNgtkiLMmRbGnCN5xxUZI7vbVphLDrUOtgi8k3cI0qj//7eW7d68sDiQgkjIgCb+1ExsDqYfhKYm7JAFvkyX6zN689+dig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715175421; c=relaxed/simple; bh=t8kYAIW2U8ao1A2w0BdbR0I96PSYyvvBz8AT9+ffVW8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aj1CPETO7YA5m6GHnTrXQrj3O6erUAk8cST48YtGdJtdr0Z8hs7LWbtHyIzt16cRmBFKDI7u7QLtrt4q1Ph8hQcd36djksZB+Q4roSIsBJnTBKczoG8fQ/pCDAR0Ay8i2+EYoSPV5e33tyFGFsARjKmarLt7lbcKdYuJzMMTOJk= 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.48]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4VZGLn55CNz1RBtL; Wed, 8 May 2024 21:33:37 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id A857D18007E; Wed, 8 May 2024 21:36: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; Wed, 8 May 2024 21:36:57 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 08/13] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Wed, 8 May 2024 21:34:03 +0800 Message-ID: <20240508133408.54708-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-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: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) Content-Type: text/plain; charset="utf-8" Currently there is one 'struct page_frag' for every 'struct sock' and 'struct task_struct', we are about to replace the 'struct page_frag' with 'struct page_frag_cache' for them. Before begin the replacing, we need to ensure the size of 'struct page_frag_cache' is not bigger than the size of 'struct page_frag', as there may be tens of thousands of 'struct sock' and 'struct task_struct' instances in the system. By or'ing the page order & pfmemalloc with lower bits of 'va' instead of using 'u16' or 'u32' for page size and 'u8' for pfmemalloc, we are able to avoid 3 or 5 bytes space waste. And page address & pfmemalloc & order is unchanged for the same page in the same 'page_frag_cache' instance, it makes sense to fit them together. Also, it is better to replace 'offset' with 'remaining', which is the remaining size for the cache in a 'page_frag_cache' instance, we are able to do a single 'fragsz > remaining' checking for the case of cache being enough, which should be the fast path if we ensure size is zoro when 'va' =3D=3D NULL by memset'ing 'struct page_frag_cache' in page_frag_cache_init() and page_frag_cache_drain(). After this patch, the size of 'struct page_frag_cache' should be the same as the size of 'struct page_frag'. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 62 +++++++++++++++++---- mm/page_frag_cache.c | 98 ++++++++++++++++++++------------- 2 files changed, 111 insertions(+), 49 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cach= e.h index 024ff73a7ea4..88e91ee57b91 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -8,29 +8,67 @@ #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) =20 +/* + * struct encoded_va - a nonexistent type marking this pointer + * + * An 'encoded_va' pointer is a pointer to a aligned virtual address, whic= h is + * at least aligned to PAGE_SIZE, that means there are at least 12 lower b= its + * space available for other purposes. + * + * Currently we use the lower 8 bits and bit 9 for the order and PFMEMALLOC + * flag of the page this 'va' is corresponding to. + * + * Use the supplied helper functions to endcode/decode the pointer and bit= s. + */ +struct encoded_va; + +#define PAGE_FRAG_CACHE_ORDER_MASK GENMASK(7, 0) +#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT BIT(8) +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 8 + +static inline struct encoded_va *encode_aligned_va(void *va, + unsigned int order, + bool pfmemalloc) +{ + return (struct encoded_va *)((unsigned long)va | order | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +} + +static inline unsigned long encoded_page_order(struct encoded_va *encoded_= va) +{ + return PAGE_FRAG_CACHE_ORDER_MASK & (unsigned long)encoded_va; +} + +static inline bool encoded_page_pfmemalloc(struct encoded_va *encoded_va) +{ + return PAGE_FRAG_CACHE_PFMEMALLOC_BIT & (unsigned long)encoded_va; +} + +static inline void *encoded_page_address(struct encoded_va *encoded_va) +{ + return (void *)((unsigned long)encoded_va & PAGE_MASK); +} + struct page_frag_cache { - void *va; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; + struct encoded_va *encoded_va; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <=3D 32) + u16 pagecnt_bias; + u16 remaining; #else - __u32 offset; + u32 pagecnt_bias; + u32 remaining; #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) { - nc->va =3D NULL; + memset(nc, 0, sizeof(*nc)); } =20 static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *n= c) { - return !!nc->pfmemalloc; + return encoded_page_pfmemalloc(nc->encoded_va); } =20 void page_frag_cache_drain(struct page_frag_cache *nc); diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index c0ecfa733727..4542d72e7b01 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -22,6 +22,7 @@ static struct page *__page_frag_cache_refill(struct page_= frag_cache *nc, gfp_t gfp_mask) { struct page *page =3D NULL; + unsigned int size, order; gfp_t gfp =3D gfp_mask; =20 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) @@ -32,23 +33,41 @@ static struct page *__page_frag_cache_refill(struct pag= e_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; #endif - if (unlikely(!page)) + if (unlikely(!page)) { page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); + if (unlikely(!page)) + goto alloc_failed; + + size =3D PAGE_SIZE; + order =3D 0; + } else { + size =3D PAGE_FRAG_CACHE_MAX_SIZE; + order =3D PAGE_FRAG_CACHE_MAX_ORDER; + } =20 - nc->va =3D page ? page_address(page) : NULL; + nc->encoded_va =3D encode_aligned_va(page_address(page), order, + page_is_pfmemalloc(page)); + nc->remaining =3D size; + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; =20 return page; + +alloc_failed: + nc->encoded_va =3D NULL; + nc->remaining =3D 0; + return NULL; } =20 void page_frag_cache_drain(struct page_frag_cache *nc) { - if (!nc->va) + if (!nc->encoded_va) return; =20 - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va =3D NULL; + __page_frag_cache_drain(virt_to_head_page(nc->encoded_va), + nc->pagecnt_bias); + memset(nc, 0, sizeof(*nc)); } EXPORT_SYMBOL(page_frag_cache_drain); =20 @@ -65,35 +84,32 @@ void *__page_frag_alloc_va_align(struct page_frag_cache= *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { - unsigned int size, offset; + unsigned int remaining, page_size; + struct encoded_va *encoded_va; +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + unsigned long page_order; +#endif struct page *page; =20 - if (unlikely(!nc->va)) { -refill: - page =3D __page_frag_cache_refill(nc, gfp_mask); - if (!page) - 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; - } - +alloc_fragment: + remaining =3D nc->remaining & align_mask; + encoded_va =3D nc->encoded_va; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size; + page_order =3D encoded_page_order(encoded_va); + page_size =3D PAGE_SIZE << page_order; #else - size =3D PAGE_SIZE; + page_size =3D PAGE_SIZE; #endif =20 - offset =3D __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); - if (unlikely(offset + fragsz > size)) { + if (unlikely(fragsz > remaining)) { + if (unlikely(!encoded_va)) { + page =3D __page_frag_cache_refill(nc, gfp_mask); + if (page) + goto alloc_fragment; + + return NULL; + } + /* fragsz is not supposed to be bigger than PAGE_SIZE as we are * allowing order 3 page allocation to fail easily under low * memory condition. @@ -101,14 +117,22 @@ void *__page_frag_alloc_va_align(struct page_frag_cac= he *nc, if (WARN_ON_ONCE(fragsz > PAGE_SIZE)) return NULL; =20 - page =3D virt_to_page(nc->va); + page =3D virt_to_page(encoded_va); + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) { + page =3D __page_frag_cache_refill(nc, gfp_mask); + if (page) + goto alloc_fragment; =20 - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; + return NULL; + } =20 - if (unlikely(nc->pfmemalloc)) { + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { free_unref_page(page, compound_order(page)); - goto refill; + page =3D __page_frag_cache_refill(nc, gfp_mask); + if (page) + goto alloc_fragment; + + return NULL; } =20 /* OK, page count is 0, we can safely set it */ @@ -116,13 +140,13 @@ void *__page_frag_alloc_va_align(struct page_frag_cac= he *nc, =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; + remaining =3D page_size; } =20 + nc->remaining =3D remaining - fragsz; nc->pagecnt_bias--; - nc->offset =3D offset + fragsz; =20 - return nc->va + offset; + return encoded_page_address(encoded_va) + (page_size - remaining); } EXPORT_SYMBOL(__page_frag_alloc_va_align); =20 --=20 2.33.0