From patchwork Tue Jul 9 13:27:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13727976 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 423D8C2BD09 for ; Tue, 9 Jul 2024 13:31:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6C0D6B00C1; Tue, 9 Jul 2024 09:31:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCD276B00C4; Tue, 9 Jul 2024 09:31:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A20FF6B00C3; Tue, 9 Jul 2024 09:31:20 -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 82B846B00C1 for ; Tue, 9 Jul 2024 09:31:20 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 24F7DC18A6 for ; Tue, 9 Jul 2024 13:31:20 +0000 (UTC) X-FDA: 82320300720.06.B10BD25 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf28.hostedemail.com (Postfix) with ESMTP id 962B2C000C for ; Tue, 9 Jul 2024 13:31:16 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=pass (imf28.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=1720531837; 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=/jkaXi9oCnUjsT51NW+o3Y7tDuYi8+zxDiHSk9e0aug=; b=vUeJWJ+wWe8mD9x3Mbi/dfCdgrPe0eABwtR3lOiVQ7EWC4uOSZr2QRVQFECZqtHq3/dAG4 VAXxMjdXRus7q4zC/wcxvUxi+wQJWzxqbvf4OKPgOE0mtXK1iuSOqH7xL289ZHO27h5+ce SNzKhzExtLLpBc2NraLGzLec4HT5Q0g= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=pass (imf28.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=1720531837; a=rsa-sha256; cv=none; b=H9Zzwb9TQeJJYeFg7w82UqiZOI66/vG4V/TA2yywCLeFBWQ+E8TJJnRCmibhjAhVgL5jh4 QZhQPxttijQo8UyuLFxKNEtiLOz9LVpQQR74ecfsr4BOfCkU5N0tXqN+LT4LeQ8i3fs0vx E6n5nZN9lEJc9OOEGN2X9omGKmOQSL4= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4WJMLv0NVRzcpK3; Tue, 9 Jul 2024 21:30:47 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 33A0B180064; Tue, 9 Jul 2024 21:31:13 +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; Tue, 9 Jul 2024 21:31:12 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v10 07/15] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Tue, 9 Jul 2024 21:27:32 +0800 Message-ID: <20240709132741.47751-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240709132741.47751-1-linyunsheng@huawei.com> References: <20240709132741.47751-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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 962B2C000C X-Stat-Signature: xzq785mftnx5mnkqjewhf47b7cr5ouq9 X-Rspam-User: X-HE-Tag: 1720531876-743926 X-HE-Meta: U2FsdGVkX1+t/U1TX9ex06k1ZDHIzNlh8+8T1hIMmQzVYNMY4LVRgxlrKzfuphWx6h65ugkAaKR/HUiFc5EhcSFTj15fPAJwDKUKq6pz8MkiWOAUnwPgQNQB57otNOFWqJ5qJF01mvJSov87TsKFbotEHj0qmNPacQWn03TP7IFx5a/zow8oyDz+qqHtPFcmX0ZwoE8P2ZQg1y2H9fNbQQ0fS4sG9HNKC3mTaE93rKtZxwpy3IfAH1MwyP9ZECdW3svK5HPjdEJqjEUtYSWJ5HCd1c6sTvsomOcMV5s6nkRpCqLRObRI6036bBbRVkUuo9cQ91a+D+aYL9WgPU7K+8T0uuHBbV5WjU+PNxLdgfkI+3o/ob6kCjwyUgAwhp72o4B0MmuQkYXW92I2cJYlFVrg/a3CG+0o1SXpIBcsjVM1VLt/IsX2u/lTswoZ5H/0qFcahtOa79sT9jgcyZ1ExrV9KuR6EWqPCNvLvBzjTug5lbi+caSYrur5kWM7S32F3Wr7a+PMHrcYZ1XYM6VKD3zI8g+y0kRVyx3B40b45tRwhiPirZ2RpXdwZU5u/AfAaFsTiWsCJSuUYEqbPZ5Y+IVwwU41eewg8eiSG+iXKRJwfc79vkDkSf3sKeM0XBMuujfX6zYKFw31H2gLpaEgFPWkTiJHI4btiewbJ7evmsvhlF9UFJ/0HKe3PBbTK5Q0P+b2aT2kEVzoZX6si/PrOHoJdDVpPMKwDlwwu0Nsu89Ss9Sd+YLu/1qZ4Ai9aA87DE6L9LGjVLNnkpgKu7y/dJ7ALxLmi8mPMXHCV15W21Ee6V2dCHGhgs6a+U+YHLaRPnti6gZ7b2DpVM4IGiVFgNmj9s7vwK3DLQBLW2NMvizpoAwUstinHdigsBsQN5yaxTnyClDoztCRnTqKvJE0mSSWiYEDDulLX65Gh1f6tVTo1M32wqPKnXcQ9hrExvivahn5oTHqN14KBZ5dRaY sXZFmT3d F9FGBalkKlh9UImS9oyLug8yIa4G0hq6ep5s9RXRoIa8V08U0n0ukKi9T/EG6JkntzbvR+dEi+G0XBvIDZy7RPA/EyLi8bQ5ReE/HYQSJBMhphn0h5VyE8cIubedySWxI2WT3+qAlWzSRZBWJD7JDip1qpU10ROY7Jmc3/+HBgFx1iUBT53AiTNUI1Rls92fmMud1IZL9txB/CSBL0myXtbfUenhONcajG3rFIJsIzwDNJYbDe2CSIStKwuaOKs5weUzQVg/LnoJ5upjc6tdyueAcoARD5DRuA3Ia 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/page_frag_cache.h | 65 ++++++++++++++++++++++++++++----- mm/page_frag_cache.c | 47 +++++++++++------------- 2 files changed, 77 insertions(+), 35 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 0ba96b7b64ad..87c3eb728f95 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -4,6 +4,8 @@ #define _LINUX_PAGE_FRAG_CACHE_H #include +#include +#include #include #include #include @@ -11,29 +13,72 @@ #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; +#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); +} + +struct page_frag_cache { + /* 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; }; 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 373f3bc29fcb..02e4ec92f948 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,46 +72,44 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { + unsigned long encoded_va = nc->encoded_va; int aligned_remaining, remaining; - unsigned int size = PAGE_SIZE; + unsigned int size; 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); aligned_remaining = nc->remaining & align_mask; remaining = aligned_remaining - fragsz; if (unlikely(remaining < 0)) { - 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; } @@ -142,7 +139,7 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, nc->pagecnt_bias--; nc->remaining = remaining; - return nc->va + (size - aligned_remaining); + return encoded_page_address(encoded_va) + (size - aligned_remaining); } EXPORT_SYMBOL(__page_frag_alloc_va_align);