From patchwork Tue May 28 12:55:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13676636 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 B2836C25B78 for ; Tue, 28 May 2024 12:59:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 088996B00A1; Tue, 28 May 2024 08:59:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB81E6B00A2; Tue, 28 May 2024 08:59:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7E306B00A3; Tue, 28 May 2024 08:59:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BDE336B00A1 for ; Tue, 28 May 2024 08:59:14 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 73EACA04FC for ; Tue, 28 May 2024 12:59:14 +0000 (UTC) X-FDA: 82167810228.04.4F50638 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf08.hostedemail.com (Postfix) with ESMTP id F3F17160006 for ; Tue, 28 May 2024 12:59:11 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 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=1716901152; a=rsa-sha256; cv=none; b=FysgWBhxWMw8onj2uc0LckQPb28vbl7s3cWolXPdYX9TlH+5bmlXH+LofbOwLA0nsV/is3 JB2W1mdw8ivxvFVuH8Am5uol0gIDaG94bbQjjBVv2OhWb+HG64xLt5hQQaAak2SMD6qbAa Q6pRIlydolg5ap+I6LZ8xZdLngtkfgU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 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=1716901152; 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=840vYFfv5FcAIVy1coQCA8JSVKRICZHkYWz9OA2GvK4=; b=SPCqki/WDQ3fe7r6SA9eNXL+VMuzHvBmXY8DUA4OqSTjM8upwV8X1sswpr/tP5upsgKSva ZeLjx10G4ySEdTpkd0fL8CXRQCfBOtQD/Od5Y0nNpiGj/VdOjncJuTjNkMfzDW+caFeO5d CddhlTcPhV/qN3k22L9n+S9pOqfvyTI= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4VpXYQ5gdgz1S6cn; Tue, 28 May 2024 20:55:22 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id D895B18007F; Tue, 28 May 2024 20:59: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; Tue, 28 May 2024 20:59:08 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v5 08/13] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Tue, 28 May 2024 20:55:58 +0800 Message-ID: <20240528125604.63048-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240528125604.63048-1-linyunsheng@huawei.com> References: <20240528125604.63048-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: F3F17160006 X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: zzgkg5bt3au1uucmase7u8s166zw65ym X-HE-Tag: 1716901151-111069 X-HE-Meta: U2FsdGVkX1+JiZevvoh+iN6vU4woFpAcHBzE9W/EqqhnuR4a4YTfzEGxttNJ02Xy15Lt8fWLjGLiACcSrAVvOpAkGS0+b3yZGJk8JbDByid7P7OKzl3g1lOB5fxJmFY1fOb7AmC8Q3+YjuPufb5MTPGoTrfZEJgkv11qDyS+nHB/EsL0mh7p5Cwvg4iskFaEukBN7VNylw9KjzyM2GB9Hd1m2tvZlTyv3wXUGBXRufp0VkMASfPzNMIlhWoj9Lwz/QKcbKxmfPwoGARNobb8MxzS5LYsQ3171bgnLNYlvyR+rd094owrklBuIunLCbmgqAoCQs/blG94TKk8uzJgVfT4qHNBjpYo53OTVBtiHZAmLFIjS6rBjdltySu/bbwDwymzZAlcLdZoMMJ6yoWiLuNbz+s6qe4RspTP2vimLPNBrMkpUQCgv12HZFUY0DXEJjM2GStUI4boH8VoE82Rp6y3gQUySz6UcfRhHVE2cTaHmRxdBmylz1NRWU9iWCPUYe/eju/PLIUodXLyJ+JH5iP4gorCrqtnXb4vMQYM1AQPhZ5rPy+WXAWyAlyso3LqfklFmuiqRlGcfl6x9FRnbt+CbRTpkDOiDMJ01qWr/p4ysgMGTMnJ8kNWUBPevIojtac+0K1mP4IRPRIh1lr2qUGh8U0GWBQ6rBRBYtZPHJOkmvVX6x7yZ0yUnXGc8VKmtlky0vyki1v9d+pP/iJQYxM+5MdRg1RJ5ZL7xkv83VDO8qgmYCY3npoAs+Z5vnBWLFMyfWZf7dkiBqI1p7BS8n42Nk6cbguCp10bVKk4TMgapQ3saG8GjeR7kroYGJqKqBKSee61h1kNLqucOkmmiU7PXDzp6g8317lriNsacuIp+4Lfqimn/teJCLVSKp9h8SEF8DdmTk8OdmUEubjrqcjYJk3W7NdTtTrfPbGV6kwvIZL4dRAT0AU51Xc/HDaPykJuL9frICCn3DzCqRD vzZWRSEM r8kWxVd3DXEvIvu2QtURH784IAQD5vNnrbEBalTObaF2vKCaQzJpkbOhIfq75pzZKGfvloteKUmG59QWtkjDAtmY6HglC3nTd0zk/9AwpvX/y+/+QVX6pafgjCDlLXRYdiOdy/6QVZOG0x49KS0CgR6GRBxslfvMIx4pppu6Z5BtEnORW1cewDnRHLhds3tk5EbZg6u2vrA6Tv0a1nL0sYLCOMFgKP0FSbdiN58hVn6Y4nyXPPzHt/FjApkbcYkymZd1YjFlInAsbkgWg1Tpkdbhqd2QuoGdi5xUS 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. 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 not being enough, which should be the fast path if we ensure size is zoro when 'va' == 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 | 82 ++++++++++++++++++++++++++++----- mm/page_frag_cache.c | 77 +++++++++++++++++-------------- 2 files changed, 112 insertions(+), 47 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 6ac3a25089d1..5228e3025ba1 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -8,29 +8,87 @@ #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; +/* + * struct encoded_va - a nonexistent type marking this pointer + * + * An 'encoded_va' pointer is a pointer to a aligned virtual address, which is + * at least aligned to PAGE_SIZE, that means there are at least 12 lower bits + * 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 bits. + */ +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) +{ +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + return (struct encoded_va *)((unsigned long)va | order | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +#else + return (struct encoded_va *)((unsigned long)va | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +#endif +} + +static inline unsigned long encoded_page_order(struct encoded_va *encoded_va) +{ #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; + return PAGE_FRAG_CACHE_ORDER_MASK & (unsigned long)encoded_va; +#else + return 0; +#endif +} + +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 { + struct encoded_va *encoded_va; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 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; }; static inline void page_frag_cache_init(struct page_frag_cache *nc) { - nc->va = NULL; + memset(nc, 0, sizeof(*nc)); } 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(struct encoded_va *encoded_va) +{ + return PAGE_SIZE << encoded_page_order(encoded_va); +} + +static inline unsigned int __page_frag_cache_page_offset(struct encoded_va *encoded_va, + unsigned int remaining) +{ + return page_frag_cache_page_size(encoded_va) - remaining; } 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 7a8d8e3a4bbf..7842f0f2f672 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -23,6 +23,7 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, { struct page *page = NULL; gfp_t gfp = gfp_mask; + unsigned int order; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* Ensure free_unref_page() can be used to free the page fragment */ @@ -32,23 +33,41 @@ 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)) { + memset(nc, 0, sizeof(*nc)); + return NULL; + } - nc->va = page ? page_address(page) : NULL; + order = 0; + nc->remaining = PAGE_SIZE; + } else { + order = PAGE_FRAG_CACHE_MAX_ORDER; + nc->remaining = PAGE_FRAG_CACHE_MAX_SIZE; + } + /* 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 of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + 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(nc->encoded_va), + nc->pagecnt_bias); + memset(nc, 0, sizeof(*nc)); } EXPORT_SYMBOL(page_frag_cache_drain); @@ -65,50 +84,37 @@ 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; + struct encoded_va *encoded_va = nc->encoded_va; + unsigned int remaining; struct page *page; - int offset; - if (unlikely(!nc->va)) { + if (unlikely(!encoded_va)) { refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) + if (unlikely(!__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 = page_is_pfmemalloc(page); - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = 0; + encoded_va = nc->encoded_va; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - - offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); - if (unlikely(offset + fragsz > size)) { - page = virt_to_page(nc->va); - + remaining = nc->remaining & align_mask; + if (unlikely(fragsz > remaining)) { + page = virt_to_page(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; } /* 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 */ + /* reset page count bias and remaining of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = 0; + remaining = page_frag_cache_page_size(encoded_va); if (WARN_ON_ONCE(fragsz > PAGE_SIZE)) { /* @@ -125,9 +131,10 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, } nc->pagecnt_bias--; - nc->offset = offset + fragsz; + nc->remaining = remaining - fragsz; - return nc->va + offset; + return encoded_page_address(encoded_va) + + __page_frag_cache_page_offset(encoded_va, remaining); } EXPORT_SYMBOL(__page_frag_alloc_va_align);