From nobody Tue Feb 10 16:18:46 2026 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 40C5218BBA8; Fri, 23 Aug 2024 15:06:50 +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=1724425612; cv=none; b=F2dzK2PBhrmHksavlhRd2W6GtPVm2pi9bmV9QhaM52VnXiq1e+aqutq2E/UlyxXnp0OPlnub+xLa8HTp+yJEKRpEBVCgO4XBtZe3asPALTO2bcU7x4nQZRMhSSe3v1I5OnTZ/8Q73nlKzZuLOc6dHqV12PauRfyd6MFIUhfzrg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724425612; c=relaxed/simple; bh=Ew/RwZXarMMJdl4/2ROvQD33NsVQ0Q5grvaSYfxY8ik=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zv3xjgqLwKWcPtJHiI1eT5l8Y9EV1VjEOZ0OWh/y1YCZe9N4w2s3WFUeWT/OBrMRRoy9h2TYLDFwLq30sPb+/iQ99eGtGkN2tfmhq2Gl40WOIiBkGR6cdsfEsRrzTCdPvgFkVPpkH8dnv1ee0b/R89QbL964gtXcKZTG67kqwI8= 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.44]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Wr3FQ2vzwz20mBR; Fri, 23 Aug 2024 23:02:02 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 7490C1402E1; Fri, 23 Aug 2024 23:06:46 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 23 Aug 2024 23:06:46 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v14 05/11] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Fri, 23 Aug 2024 23:00:33 +0800 Message-ID: <20240823150040.1567062-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240823150040.1567062-1-linyunsheng@huawei.com> References: <20240823150040.1567062-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: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) 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. 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/mm_types_task.h | 19 ++++--- include/linux/page_frag_cache.h | 98 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 64 +++++++++++---------- 3 files changed, 140 insertions(+), 41 deletions(-) diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h index cdc1e3696439..a8635460e027 100644 --- a/include/linux/mm_types_task.h +++ b/include/linux/mm_types_task.h @@ -50,18 +50,21 @@ struct page_frag { #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) + /* encoded_page consists of the virtual address, pfmemalloc bit and order + * of a page. + */ + unsigned long encoded_page; + + /* we maintain a pagecount bias, so that we dont dirty cache line + * containing page->_refcount every time we allocate a fragment. + */ +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <=3D 32) __u16 offset; - __u16 size; + __u16 pagecnt_bias; #else __u32 offset; + __u32 pagecnt_bias; #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 /* Track pages that require TLB flushes */ diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cach= e.h index 0a52f7a179c8..1ed0d1b7014b 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -3,24 +3,114 @@ #ifndef _LINUX_PAGE_FRAG_CACHE_H #define _LINUX_PAGE_FRAG_CACHE_H =20 +#include +#include #include +#include #include #include =20 +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) +/* Use a full byte here to enable assembler optimization as the shift + * operation is usually expecting a byte. + */ +#define PAGE_FRAG_CACHE_ORDER_MASK GENMASK(7, 0) +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 8 +#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT BIT(PAGE_FRAG_CACHE_PFMEMALLOC_SHI= FT) +#else +/* Compiler should be able to figure out we don't read things as any value + * ANDed with 0 is 0. + */ +#define PAGE_FRAG_CACHE_ORDER_MASK 0 +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 0 +#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT BIT(PAGE_FRAG_CACHE_PFMEMALLOC_SHI= FT) +#endif + +static inline unsigned long page_pool_encode_page(struct page *page, + unsigned int order, + bool pfmemalloc) +{ + BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_FRAG_CACHE_ORDER_MASK); + BUILD_BUG_ON(PAGE_FRAG_CACHE_PFMEMALLOC_BIT >=3D PAGE_SIZE); + + return (unsigned long)page_address(page) | + (order & PAGE_FRAG_CACHE_ORDER_MASK) | + ((unsigned long)pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +} + +static inline unsigned long page_pool_encoded_page_order(unsigned long enc= oded_page) +{ + return encoded_page & PAGE_FRAG_CACHE_ORDER_MASK; +} + +static inline bool page_pool_encoded_page_pfmemalloc(unsigned long encoded= _page) +{ + return !!(encoded_page & PAGE_FRAG_CACHE_PFMEMALLOC_BIT); +} + +static inline void *page_pool_encoded_page_address(unsigned long encoded_p= age) +{ + return (void *)(encoded_page & PAGE_MASK); +} + +static inline struct page *page_pool_encoded_page_ptr(unsigned long encode= d_page) +{ + return virt_to_page((void *)encoded_page); +} + static inline void page_frag_cache_init(struct page_frag_cache *nc) { - nc->va =3D NULL; + nc->encoded_page =3D 0; } =20 static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *n= c) { - return !!nc->pfmemalloc; + return page_pool_encoded_page_pfmemalloc(nc->encoded_page); +} + +static inline unsigned int page_frag_cache_page_size(unsigned long encoded= _page) +{ + return PAGE_SIZE << page_pool_encoded_page_order(encoded_page); } =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_cache_prepare(struct page_frag_cache *nc, unsigned int f= ragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask); + +static inline void __page_frag_cache_commit(struct page_frag_cache *nc, + struct page_frag *pfrag, bool referenced, + unsigned int used_sz) +{ + if (referenced) { + VM_BUG_ON(!nc->pagecnt_bias); + nc->pagecnt_bias--; + } + + /* Committed offset might be bigger than the current offset due to alignm= ent */ + VM_BUG_ON(nc->offset > pfrag->offset); + + VM_BUG_ON(used_sz > pfrag->size); + VM_BUG_ON(pfrag->offset + pfrag->size > page_frag_cache_page_size(nc->enc= oded_page)); + + nc->offset =3D pfrag->offset + used_sz; +} + +static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, un= signed int fragsz, + gfp_t gfp_mask, unsigned int align_mask) +{ + struct page_frag page_frag; + void *va; + + va =3D __page_frag_cache_prepare(nc, fragsz, &page_frag, gfp_mask, align_= mask); + if (unlikely(!va)) + return NULL; + + __page_frag_cache_commit(nc, &page_frag, true, fragsz); + + return va; +} =20 static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 4c8e04379cb3..2865bff7199d 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) { + unsigned long order =3D PAGE_FRAG_CACHE_MAX_ORDER; struct page *page =3D NULL; gfp_t gfp =3D gfp_mask; =20 @@ -30,23 +31,31 @@ 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)) { + nc->encoded_page =3D 0; + return NULL; + } + + order =3D 0; + } =20 - nc->va =3D page ? page_address(page) : NULL; + nc->encoded_page =3D page_pool_encode_page(page, order, + page_is_pfmemalloc(page)); =20 return page; } =20 void page_frag_cache_drain(struct page_frag_cache *nc) { - if (!nc->va) + if (!nc->encoded_page) return; =20 - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va =3D NULL; + __page_frag_cache_drain(page_pool_encoded_page_ptr(nc->encoded_page), + nc->pagecnt_bias); + nc->encoded_page =3D 0; } EXPORT_SYMBOL(page_frag_cache_drain); =20 @@ -59,35 +68,32 @@ void __page_frag_cache_drain(struct page *page, unsigne= d 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_cache_prepare(struct page_frag_cache *nc, unsigned int f= ragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask) { -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - unsigned int size =3D nc->size; -#else - unsigned int size =3D PAGE_SIZE; -#endif - unsigned int offset; + unsigned long encoded_page =3D nc->encoded_page; + unsigned int size, offset; struct page *page; =20 - if (unlikely(!nc->va)) { + size =3D page_frag_cache_page_size(encoded_page); + page =3D page_pool_encoded_page_ptr(encoded_page); + + if (unlikely(!encoded_page)) { refill: page =3D __page_frag_cache_refill(nc, gfp_mask); 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 + encoded_page =3D nc->encoded_page; + size =3D page_frag_cache_page_size(encoded_page); + /* Even if we own the page, we do not use atomic_set(). * This would break get_page_unless_zero() users. */ page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); =20 /* 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; } @@ -107,13 +113,12 @@ void *__page_frag_alloc_align(struct page_frag_cache = *nc, return NULL; } =20 - page =3D virt_to_page(nc->va); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; =20 - if (unlikely(nc->pfmemalloc)) { - free_unref_page(page, compound_order(page)); + if (unlikely(page_pool_encoded_page_pfmemalloc(encoded_page))) { + free_unref_page(page, + page_pool_encoded_page_order(encoded_page)); goto refill; } =20 @@ -125,12 +130,13 @@ void *__page_frag_alloc_align(struct page_frag_cache = *nc, offset =3D 0; } =20 - nc->pagecnt_bias--; - nc->offset =3D offset + fragsz; + pfrag->page =3D page; + pfrag->offset =3D offset; + pfrag->size =3D size - offset; =20 - return nc->va + offset; + return page_pool_encoded_page_address(encoded_page) + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_cache_prepare); =20 /* * Frees a page fragment allocated out of either a compound or order 0 pag= e. --=20 2.33.0