From patchwork Wed Jul 31 12:44:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13748710 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CC93BC3DA64 for ; Wed, 31 Jul 2024 12:51:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6136D6B0092; Wed, 31 Jul 2024 08:51:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C32F6B0095; Wed, 31 Jul 2024 08:51:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B1306B0096; Wed, 31 Jul 2024 08:51:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 265DB6B0092 for ; Wed, 31 Jul 2024 08:51:02 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CD38EC013D for ; Wed, 31 Jul 2024 12:51:01 +0000 (UTC) X-FDA: 82400032722.16.0C6AA6D Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf05.hostedemail.com (Postfix) with ESMTP id 6F1C2100003 for ; Wed, 31 Jul 2024 12:50:59 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722430214; a=rsa-sha256; cv=none; b=kgMrHLGlbYrMjcfVZz7J3ZkZHkeutsl9BIeACJRW9cAh5/JKm3yy7k37Ptiekm8cjuJRKS B0PjSMolLl0HXi0mDH8O3yospIcvTX+oZbs26WGoLueBuvo66Ni6PeEkp3jYk7ySFXOU34 HF+xzJXhCuOsabbqDESy55h/pV+d5qY= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722430214; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wbxN6HFmPApZpCmj35iTaQWSjGHuVH+23z1ZHCdtx+U=; b=knJb4ONh2024J3dkJXnaH1EAbt9auiD9MKYaaTwSvavnWSlnC60H1z3a8W3712lfMGKRGf s/XjWN6wPpCt0mtIzXordinIVoTZvvhYla9jYVOWlQmLA6/dA6rvlphHzni88aPlbcwoVh zuSjqZj5LryyreBryvr1sI/WIrDBWLA= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4WYsQY0d0rzxW2B; Wed, 31 Jul 2024 20:50:45 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id C7689140360; Wed, 31 Jul 2024 20:50:56 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 31 Jul 2024 20:50:56 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v12 07/14] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Wed, 31 Jul 2024 20:44:57 +0800 Message-ID: <20240731124505.2903877-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240731124505.2903877-1-linyunsheng@huawei.com> References: <20240731124505.2903877-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 6F1C2100003 X-Stat-Signature: mfwmhbamtc9pjzpck1t79dw3uxk8ut9z X-Rspam-User: X-HE-Tag: 1722430259-618635 X-HE-Meta: U2FsdGVkX19BrmB/SSTNVOGmmg9k9yvSFOLXx9R+QQKmxw3bGJpAqlGCZ2pgcK+svE9LltnZdziNOg4Ybi/AViJDWu4400FJfISS3uwUqWBZTmWLveeWtEcPWbHZcBayi5Un0qCUqOg8WkR2TpR8akMcVdyDYSfgs1r4agsugJC1NIBEt5YIOYAipO5VeAQ5/bt4h3ce0HrsdCgOG2nvEzP6YpY032RnsQZMETLQGxQrsXM7oujyN/NMnER0xPyQ1Z8VmjQq8taeuA+dCLDz9IV9gZmeRwW2ww/iQLwIkJ5OolzKpqwmyorAqfwg6tZBTjBRtNG1aC5PzJSCqWWHD8BU+03awP/hMNvpViGfPyJDRDe/Fucn/VLW2IHjODY0aiF5zsyF1l2O+nsbVwLbkAYtXX8/4fx3S2GfmBEFXXa3dncRV2oILFYcjotk4HWtO5+BFPENioBUuEfKwVY1QgxcmnI+Dl37dfmTuLgbe0+4yR5SOXpC5JQl/kEL3OaRUPd2zos1S0kuv+hpusNJb/vFkuGlDz86CQify/zoUu9pVxXIUa0ThMRaajEpqNWom9TbN7+4aM2GWnOftIe1uVsoDZ1L5J9mNmKcdY6FccpGy6V8JwICuH0vyPkcBTDbkdnNzfQ0SuyIm3vZXE7GRy66UL97aC7b1lhYO+OrAsa6H3BvgGOrzp24l/7ohAieP1catNVdviXQzCeHflV9sr/cciJvthUV6b4f2ban+uf4gag6ZMmTdbFae0KBBoQI0yEwxSsunig6lzhlne5aXdermEvTW8tXY3jQnyV9t4fqlQbTKF8n7mR7QLuqFfUe7+q7pm+EdfJYgjJtzwD9a/nrdTpYsrVmxU0TEzij0xgUX03HrWXcWcl2uRVN7x6ZLgXDZUUWYCUx+7oDxyeychuM7uqTZpZGaVzcvkGSReYfqrboPj9tqKbDFIdRqhHw9BCuf6kbUHbSjZEPTKI O36ODv30 5OOqCncubhgBxGJzHDA8w7vrtzjEXHma3oAczW14Lpg3JAijxJhOynX6npRFZiUe2mDUTJfosfAuR6mGsJNKUOeeGRRFb4C4KTHgK4hEv/zn89gLRTfZaa5atNW3XQUMcvOA4sRLtU7xAJ6/pZCzVbUs3w4OmTnHZgfGfqF3+io3Z2XoLHCM2nSOedLGMGm6ESLMAsU4yVv96SquOCp5HiPkEnyBzCcdKmzR7AxYpnKKGOnruk1zT2QWc9b0eTOLGUyznFHmx9bopRSP0khWfv6q/Q3mkMjd5s74A X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Currently 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 | 16 +++++----- include/linux/page_frag_cache.h | 52 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 49 +++++++++++++++++-------------- 3 files changed, 85 insertions(+), 32 deletions(-) diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h index b1c54b2b9308..f2610112a642 100644 --- a/include/linux/mm_types_task.h +++ b/include/linux/mm_types_task.h @@ -50,18 +50,18 @@ 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_va consists of the virtual address, pfmemalloc bit and order + * of a page. + */ + unsigned long encoded_va; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32) __u16 remaining; - __u16 size; + __u16 pagecnt_bias; #else __u32 remaining; + __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; }; /* Track pages that require TLB flushes */ diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 7c9125a9aed3..4ce924eaf1b1 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -3,18 +3,66 @@ #ifndef _LINUX_PAGE_FRAG_CACHE_H #define _LINUX_PAGE_FRAG_CACHE_H +#include +#include #include #include #include +#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_BIT BIT(8) +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 8 +#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_BIT BIT(0) +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 0 +#endif + +static inline unsigned long encode_aligned_va(void *va, 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_SHIFT >= PAGE_SHIFT); + + return (unsigned long)va | (order & PAGE_FRAG_CACHE_ORDER_MASK) | + ((unsigned long)pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +} + +static inline unsigned long encoded_page_order(unsigned long encoded_va) +{ + return encoded_va & PAGE_FRAG_CACHE_ORDER_MASK; +} + +static inline bool encoded_page_pfmemalloc(unsigned long encoded_va) +{ + return !!(encoded_va & PAGE_FRAG_CACHE_PFMEMALLOC_BIT); +} + +static inline void *encoded_page_address(unsigned long encoded_va) +{ + return (void *)(encoded_va & PAGE_MASK); +} + static inline void page_frag_cache_init(struct page_frag_cache *nc) { - nc->va = NULL; + nc->encoded_va = 0; } static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { - return !!nc->pfmemalloc; + return encoded_page_pfmemalloc(nc->encoded_va); +} + +static inline unsigned int page_frag_cache_page_size(unsigned long encoded_va) +{ + return PAGE_SIZE << encoded_page_order(encoded_va); } 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 70fb6dead624..2544b292375a 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 = PAGE_FRAG_CACHE_MAX_ORDER; struct page *page = NULL; gfp_t gfp = gfp_mask; @@ -30,23 +31,31 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER); - nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; #endif - if (unlikely(!page)) + if (unlikely(!page)) { page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + if (unlikely(!page)) { + nc->encoded_va = 0; + return NULL; + } - nc->va = page ? page_address(page) : NULL; + order = 0; + } + + nc->encoded_va = encode_aligned_va(page_address(page), order, + page_is_pfmemalloc(page)); return page; } void page_frag_cache_drain(struct page_frag_cache *nc) { - if (!nc->va) + if (!nc->encoded_va) return; - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va = NULL; + __page_frag_cache_drain(virt_to_head_page((void *)nc->encoded_va), + nc->pagecnt_bias); + nc->encoded_va = 0; } EXPORT_SYMBOL(page_frag_cache_drain); @@ -63,33 +72,29 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - unsigned int size = nc->size; -#else - unsigned int size = PAGE_SIZE; -#endif - unsigned int remaining; + unsigned long encoded_va = nc->encoded_va; + unsigned int size, remaining; struct page *page; - if (unlikely(!nc->va)) { + if (unlikely(!encoded_va)) { refill: page = __page_frag_cache_refill(nc, gfp_mask); if (!page) return NULL; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif + encoded_va = nc->encoded_va; + size = page_frag_cache_page_size(encoded_va); + /* Even if we own the page, we do not use atomic_set(). * This would break get_page_unless_zero() users. */ page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); /* reset page count bias and remaining to start of new frag */ - nc->pfmemalloc = page_is_pfmemalloc(page); nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; nc->remaining = size; + } else { + size = page_frag_cache_page_size(encoded_va); } remaining = nc->remaining & align_mask; @@ -107,13 +112,13 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, return NULL; } - page = virt_to_page(nc->va); + page = virt_to_page((void *)encoded_va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; - if (unlikely(nc->pfmemalloc)) { - free_unref_page(page, compound_order(page)); + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { + free_unref_page(page, encoded_page_order(encoded_va)); goto refill; } @@ -128,7 +133,7 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, nc->pagecnt_bias--; nc->remaining = remaining - fragsz; - return nc->va + (size - remaining); + return encoded_page_address(encoded_va) + (size - remaining); } EXPORT_SYMBOL(__page_frag_alloc_va_align);