From patchwork Fri Jul 19 09:33:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13737063 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 04C19C3DA5D for ; Fri, 19 Jul 2024 09:37:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E6A26B00A2; Fri, 19 Jul 2024 05:37:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 470606B00A3; Fri, 19 Jul 2024 05:37:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2EADB6B00A4; Fri, 19 Jul 2024 05:37:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 04C8E6B00A2 for ; Fri, 19 Jul 2024 05:37:20 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8283312054E for ; Fri, 19 Jul 2024 09:37:20 +0000 (UTC) X-FDA: 82355999040.10.18D8B37 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf18.hostedemail.com (Postfix) with ESMTP id DEAF31C000A for ; Fri, 19 Jul 2024 09:37:17 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721381818; 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=6HZ76OCiCzCb7kobPjl1SjyFk8/6OI4OMPuKoiBoKXQ=; b=EkxZ34ob3vJKCTVls5PNTBsZmRgp2Mr7DQXxvn2ivOXXaJKPXn5k5WMb2Ij83kn0/rvIai aEI8H68x4CT2SIZQIffCExTkA7gF2NnClzR/nyH23fMUs6wXeznDP8+8Twdn0qw4y2Eon5 Tdtgt+aemZ/bMZgJ0ArBDHJjFdhBSdM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721381818; a=rsa-sha256; cv=none; b=3KM8Vui6nRu7mLU//mXvRTp/fMmnRBVi36o+4drSCFAz7Gj5eBGaCO8TEe6ScGwnJ3qAFy sey7GnzAYd9XyFhuWbv2W5dknonq63RixXGeOnGzqOVNTFSQGlnvt6CVmjQGyy8SPiGv47 8jQrEhaTOYMK+PUwksM5iKH1P+07NpE= Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4WQPfl6g7Bzdj4Q; Fri, 19 Jul 2024 17:35:27 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 9DB601402E2; Fri, 19 Jul 2024 17:37:12 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) 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, 19 Jul 2024 17:37:12 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [RFC v11 07/14] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Fri, 19 Jul 2024 17:33:31 +0800 Message-ID: <20240719093338.55117-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240719093338.55117-1-linyunsheng@huawei.com> References: <20240719093338.55117-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: DEAF31C000A X-Stat-Signature: fy6pihurm48ayr1kz4megpio3mu4oy54 X-HE-Tag: 1721381837-68521 X-HE-Meta: U2FsdGVkX19lMlsCnpq+VO+Isf6Vr9VaLH3Ps4RpnvsOSZbSxQCqJSsDWkwrMCgXGjjSDtnCukAQsuC375K8hw5BHVEgXFIPgkVeJmksnwrzu6H5PUV9Uqsl1MBYNznnN3vt9mpyRIBu6ghbvzky3rPyhRDt0yseIoSy/C6kMW2UEfr9wprooqrvHjCQul8zVJAUA6Ava9u6bSyjlArMwp4gC6gzetOw8h6q9M7P00sKcOQhYkif+rKjstQQqxy5jCKQjVGQW6DfFzXDzJswl6zp9ApKROVNZuRbWAXsPz4R0hl2ZOfafURdOQK+JdHZtn/EO6yUjeq6bjqUGsAhRqm00H9tAPYd8AodpqvaQTfmX95aq64urarFDXKECSLSS4iAuYs2qNlzhC1ey2BLIsxdiSv/aKfKNqAUe08hVsC9tTeyrwUfucKXk31gACT257QKjoerBrSO4zD2eKfkflPrg5nOKpJw+Eu0JKBLWCCtQ0KaD6c5M6Nn5AkA8E64QMijS35HK3oDdTjLGc+m6E/0ekxUNveUHHGfF4KnL75zjYgGaHBacXcii4OycK+Bl7iGOr2l+xIJThCvwdncNYPaycSjmiFBXLcc5oCq6tyY2NCUezk2UbvAe/r5Ylu8rKYFlVwQ1dlSlHpiwkx43IGyMfJncrifmV0NX7YNdNr8jOV+g3nuugL1A70JoztjoufQ4Omrt51rqyGsk0O5VyljN1kZeOu9X7WXUznM6OuMo2zx1gC1WeP1A34hvVWhycG46SIC7izaQFZhsaomHAejrSytzBvfuIZKYwnKqhCXSfII0uHun40PsZ7yBLZK+iU8rJkvMb8/3QalQFgAiqWopLrTBCMnlyaD642+K8iQivrlnwTNjIuUoR6rGjZDFudt+qMZj+q8i8nuc2uiOn6tO6l3W8HTdWMzZQIRZCokOJnNGlrRKb4iNcJFQi6U9t0g9FZqpE9eECOROCo 2JdCzGtj aXuaGBzngYD2gF97d+FZbVGpZcWL8p4ZNKL0EXY8692IOp57csB5jsZNNTeHVRvMxjpG0fdx1S/mcWifZ9X89kz9ZPybtcQpFg29mJBh/9HDKXdxFAA1jhEbWpcPkfVmmAnmgC0CCNyzR/3na3m/5BYWFNYFZ2UN7UNtAMN7yGQVIeavAefN1A59HDbujrx3scDvdAH0bsb7dgYyZbIM+5Vyjq1czULvh8VCeAhdwyNNqPh59vOrI5EgXmg+2s5XBd/84DiUMaeDZT7Eo5qaCKe8wkCwDd5QWzu8d 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 | 49 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 49 +++++++++++++++------------------ 3 files changed, 77 insertions(+), 37 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 ef1572f11248..12a16f8e8ad0 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -3,19 +3,64 @@ #ifndef _LINUX_PAGE_FRAG_CACHE_H #define _LINUX_PAGE_FRAG_CACHE_H +#include +#include #include #include #include #include +#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 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); + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + return (unsigned long)va | order | + (pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +#else + return (unsigned long)va | + (pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +#endif +} + +static inline unsigned long encoded_page_order(unsigned long encoded_va) +{ +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + return encoded_va & PAGE_FRAG_CACHE_ORDER_MASK; +#else + return 0; +#endif +} + +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 b12496f05c4a..7928e5d50711 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -22,7 +22,7 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp_mask) { - unsigned int page_size = PAGE_FRAG_CACHE_MAX_SIZE; + unsigned long order = PAGE_FRAG_CACHE_MAX_ORDER; struct page *page = NULL; gfp_t gfp = gfp_mask; @@ -35,28 +35,27 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, if (unlikely(!page)) { page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); if (unlikely(!page)) { - nc->va = NULL; + nc->encoded_va = 0; return NULL; } - page_size = PAGE_SIZE; + order = 0; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - nc->size = page_size; -#endif - nc->va = page_address(page); + 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); @@ -73,36 +72,30 @@ 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 = PAGE_SIZE; - 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; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - + size = page_frag_cache_page_size(encoded_va); remaining = nc->remaining & align_mask; if (unlikely(remaining < fragsz)) { if (unlikely(fragsz > PAGE_SIZE)) { @@ -118,13 +111,15 @@ 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))) { + VM_BUG_ON(compound_order(page) != + encoded_page_order(encoded_va)); + free_unref_page(page, encoded_page_order(encoded_va)); goto refill; } @@ -141,7 +136,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);