From patchwork Wed Aug 21 17:39:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13771933 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 258E1C52D6F for ; Wed, 21 Aug 2024 17:39:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A88FE6B0166; Wed, 21 Aug 2024 13:39:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A38DE6B0167; Wed, 21 Aug 2024 13:39:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B5C26B0168; Wed, 21 Aug 2024 13:39:27 -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 6D1006B0166 for ; Wed, 21 Aug 2024 13:39:27 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 228CD12111A for ; Wed, 21 Aug 2024 17:39:27 +0000 (UTC) X-FDA: 82476964374.03.D55C04E Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf19.hostedemail.com (Postfix) with ESMTP id 7DC3C1A0004 for ; Wed, 21 Aug 2024 17:39:25 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=bJhcCAbp; dmarc=none; spf=none (imf19.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724261949; a=rsa-sha256; cv=none; b=WB1JtZdAaowGBe2A41HpzFiGEaoQmpez6fwEU+VDey50qeZ5bEu5CO10PxursDOTgBL2B2 yn1EAyzYYoaTV+wlANZbJUtcT6ZkEyoZ7YOWNFTbTe8RBDybtxmb1nQgQ6p4M22V8fencW o5WdfGjtN0iKPtso+PEKHuEUSSG8sFY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=bJhcCAbp; dmarc=none; spf=none (imf19.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724261949; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/gNJOwVwFwXavrN1cV3i41cegpVQxEH3cjGrajbBEY4=; b=1TlyrlHEF+jpbDPF1IsrHTwuvm2pple36sN8UMoVs2k/AK2UETggo8klzq8uYKA+RvZHxl XdkiEO6q1jB9ab4YjcYCUmNRcffX3zq3aRhXI+3RmjCNyBKq4a3pgcdUBOkQtZUn/hB2g9 Vlt81iOVOvPyq7597g216lLVtB0B5Ik= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=/gNJOwVwFwXavrN1cV3i41cegpVQxEH3cjGrajbBEY4=; b=bJhcCAbpNksm0dj6Eo3dRcdb3d hqFOdGGbwfMtufFwIQsVFOoYhnT9EArG6Yq6+Fa+ThFAy4KBeBLCbY0YtGs9Ac7kxN+y0rsPUYrWu sw2xcIoGudYL2YdzTWnurDRV/uU7+kn/5apsJbNfqxDKBqraXAiIPdODl9mOlB+Uj7ebgNigv0jzq qEkIyq4ETOJPbTYr+aRFxsGSCW2VZpvrIX0tDH7NkUIYqQ2SQFQ4z90vcvAob5b/ZW924BWuanMJo xwSNQgPT/UnA/3iRDO7nFyP1T0sEXUsDTGcH6LYRMy4nnS/b2ExPl968f3/+U/G6m8vewgVS6bp9O WtIqxv4g==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgpIi-00000009WdO-1Uci; Wed, 21 Aug 2024 17:39:16 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , David Hildenbrand , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, Kent Overstreet Subject: [PATCH 3/4] mm: Support only one page_type per page Date: Wed, 21 Aug 2024 18:39:11 +0100 Message-ID: <20240821173914.2270383-4-willy@infradead.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240821173914.2270383-1-willy@infradead.org> References: <20240821173914.2270383-1-willy@infradead.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 7DC3C1A0004 X-Rspamd-Server: rspam01 X-Stat-Signature: nc31ahd5namc365ajp1ankaohocf91yt X-HE-Tag: 1724261965-175199 X-HE-Meta: U2FsdGVkX19vzt+4bK6/k5Fy/5u8svJNmewlvwfy7Y70kTRQkkL3zSBq15+bnTUrvsP+DpzcY1+HohPG7EAG6DZpYRa4PoOwddeM8u23QYokNDp+w8eqF50wtN2BBhnV5ge1a5xpO8B9azwtBaTganrO7l38AkgRclJ0jsaVjT1IsSfHKr3eg35192WebtGS5FbgkDCpmWvyGNSk0Ol5RybzAc01XgLfO351Z0x5mI8AYCVUXVchGjwS2xuYj2CX4jdhqX87G/6CfuwYJkfB1HUpf4xI+pfflb9Ce7AsyDi2suPmwU/GaVxEtIICakPqKH7qaFu2ToWZAhMPKg3csf4vRBap5qRFzkX93wuNRNSFjfF6lc/q2YM8KA7VSuC31FL7O2IzrWNSd5ZV5cszZT4Dri0gIs1iX2VMoUPQ7UQ990pRgn/7XfjsY3sGtH4/4wia0EmToSIerBZy4dgQSuvAlMNh8CFkifutyy63+8pd3UOsYCjmvZ2pBpO+rhwdEX1gXD8wAGPpnpHRM2TnWtf57CgVKODYlyPGdwzl8tXacld+sPgVII5xZPAfkRX6T4ZyipWH3N/U6LmQJKdf2YupehD3mM6w7VneFvIm8yJz8YUxJcnvnOv+boCE/Mkmt890bZvpEuXlhaoaXce+tXrQov5Egdgt6NRX+LRKXj5XG6s4Dx2YbtEdPPRsXItOaND7LzAjAmolTkX9TM+EH0oN2kgarsTzcrjJ2AK9+/7ZIlmFtZsO5gDEmZP/Nx5eGoIpHxxqexdqO/7aNt63osgl060hPGJZzwS2ixqT6MKeg3B+QzwzjE8W2fjma8yUrz6YDn/cd7ic3k/UgQb8VEFuRK5i0HLPSZcTq5zgTanX+RUnHeAOdm6ku9zoQwVMKb4VPRTttnFjZ4MPwwT9gRaJQftBmB/4xcHiRrB0iy4DMPJegTyDg4VRUDE9Plcf4rhQU/0MIcPl24Qa6Sb Y/lEe9Tl bi/42aTb46qCKcsmWQRDUmZpXcW4FGziELKFJL/VQFhdl8MnaUa6ZoOxPDMBIWnFOSHf0UrBDN3cdwgB5mJjSrCjY7iXC1pH3U5R+kLY4IMgR/2lZVoZJqI6p6ChcYo+L0aJzP/kefriyr8Qb6Xkxm/9BYBPiXZVBZUFoWv6wBy2yA2EZMmpcpSCd+xBiBMl3xZHsVnYrh9WQgNDPk6zhqpPxUsKAWKNGPcZ5jMB3XycJES7UNfkpINiFHEjlEET58GLb/dQ+CAoQIMfUGNPsc4Ud09uo0TEzxdsPRR2r8QCbZ55lGrMMEiqPnWtjxqRA9xPiLtlD5yfiYQc= 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: By using a few values in the top byte, users of page_type can store up to 24 bits of additional data in page_type. It also reduces the code size as (with replacement of READ_ONCE() with data_race()), the kernel can check just a single byte. eg: ffffffff811e3a79: 8b 47 30 mov 0x30(%rdi),%eax ffffffff811e3a7c: 55 push %rbp ffffffff811e3a7d: 48 89 e5 mov %rsp,%rbp ffffffff811e3a80: 25 00 00 00 82 and $0x82000000,%eax ffffffff811e3a85: 3d 00 00 00 80 cmp $0x80000000,%eax ffffffff811e3a8a: 74 4d je ffffffff811e3ad9 becomes: ffffffff811e3a69: 80 7f 33 f5 cmpb $0xf5,0x33(%rdi) ffffffff811e3a6d: 55 push %rbp ffffffff811e3a6e: 48 89 e5 mov %rsp,%rbp ffffffff811e3a71: 74 4d je ffffffff811e3ac0 replacing three instructions with one. Signed-off-by: Matthew Wilcox (Oracle) Acked-by: David Hildenbrand --- include/linux/page-flags.h | 68 ++++++++++++++++---------------------- kernel/vmcore_info.c | 8 ++--- mm/debug.c | 31 +++++++++++++---- 3 files changed, 56 insertions(+), 51 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 998a99441e4f..0c738bda5d98 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -923,42 +923,29 @@ PAGEFLAG_FALSE(HasHWPoisoned, has_hwpoisoned) #endif /* - * For pages that are never mapped to userspace, - * page_type may be used. Because it is initialised to -1, we invert the - * sense of the bit, so __SetPageFoo *clears* the bit used for PageFoo, and - * __ClearPageFoo *sets* the bit used for PageFoo. We reserve a few high and - * low bits so that an underflow or overflow of _mapcount won't be - * mistaken for a page type value. + * For pages that do not use mapcount, page_type may be used. + * The low 24 bits of pagetype may be used for your own purposes, as long + * as you are careful to not affect the top 8 bits. The low bits of + * pagetype will be overwritten when you clear the page_type from the page. */ - enum pagetype { - PG_buddy = 0x40000000, - PG_offline = 0x20000000, - PG_table = 0x10000000, - PG_guard = 0x08000000, - PG_hugetlb = 0x04000000, - PG_slab = 0x02000000, - PG_zsmalloc = 0x01000000, - PG_unaccepted = 0x00800000, - - PAGE_TYPE_BASE = 0x80000000, - - /* - * Reserve 0xffff0000 - 0xfffffffe to catch _mapcount underflows and - * allow owners that set a type to reuse the lower 16 bit for their own - * purposes. - */ - PAGE_MAPCOUNT_RESERVE = ~0x0000ffff, + /* 0x00-0x7f are positive numbers, ie mapcount */ + /* Reserve 0x80-0xef for mapcount overflow. */ + PGTY_buddy = 0xf0, + PGTY_offline = 0xf1, + PGTY_table = 0xf2, + PGTY_guard = 0xf3, + PGTY_hugetlb = 0xf4, + PGTY_slab = 0xf5, + PGTY_zsmalloc = 0xf6, + PGTY_unaccepted = 0xf7, + + PGTY_mapcount_underflow = 0xff }; -#define PageType(page, flag) \ - ((READ_ONCE(page->page_type) & (PAGE_TYPE_BASE | flag)) == PAGE_TYPE_BASE) -#define folio_test_type(folio, flag) \ - ((READ_ONCE(folio->page.page_type) & (PAGE_TYPE_BASE | flag)) == PAGE_TYPE_BASE) - static inline bool page_type_has_type(int page_type) { - return page_type < PAGE_MAPCOUNT_RESERVE; + return page_type < (PGTY_mapcount_underflow << 24); } /* This takes a mapcount which is one more than page->_mapcount */ @@ -969,40 +956,41 @@ static inline bool page_mapcount_is_type(unsigned int mapcount) static inline bool page_has_type(const struct page *page) { - return page_type_has_type(READ_ONCE(page->page_type)); + return page_mapcount_is_type(data_race(page->page_type)); } #define FOLIO_TYPE_OPS(lname, fname) \ -static __always_inline bool folio_test_##fname(const struct folio *folio)\ +static __always_inline bool folio_test_##fname(const struct folio *folio) \ { \ - return folio_test_type(folio, PG_##lname); \ + return data_race(folio->page.page_type >> 24) == PGTY_##lname; \ } \ static __always_inline void __folio_set_##fname(struct folio *folio) \ { \ - VM_BUG_ON_FOLIO(!folio_test_type(folio, 0), folio); \ - folio->page.page_type &= ~PG_##lname; \ + VM_BUG_ON_FOLIO(data_race(folio->page.page_type) != UINT_MAX, \ + folio); \ + folio->page.page_type = PGTY_##lname << 24; \ } \ static __always_inline void __folio_clear_##fname(struct folio *folio) \ { \ VM_BUG_ON_FOLIO(!folio_test_##fname(folio), folio); \ - folio->page.page_type |= PG_##lname; \ + folio->page.page_type = UINT_MAX; \ } #define PAGE_TYPE_OPS(uname, lname, fname) \ FOLIO_TYPE_OPS(lname, fname) \ static __always_inline int Page##uname(const struct page *page) \ { \ - return PageType(page, PG_##lname); \ + return data_race(page->page_type >> 24) == PGTY_##lname; \ } \ static __always_inline void __SetPage##uname(struct page *page) \ { \ - VM_BUG_ON_PAGE(!PageType(page, 0), page); \ - page->page_type &= ~PG_##lname; \ + VM_BUG_ON_PAGE(data_race(page->page_type) != UINT_MAX, page); \ + page->page_type = PGTY_##lname << 24; \ } \ static __always_inline void __ClearPage##uname(struct page *page) \ { \ VM_BUG_ON_PAGE(!Page##uname(page), page); \ - page->page_type |= PG_##lname; \ + page->page_type = UINT_MAX; \ } /* diff --git a/kernel/vmcore_info.c b/kernel/vmcore_info.c index 8b4f8cc2e0ec..1fec61603ef3 100644 --- a/kernel/vmcore_info.c +++ b/kernel/vmcore_info.c @@ -198,17 +198,17 @@ static int __init crash_save_vmcoreinfo_init(void) VMCOREINFO_NUMBER(PG_private); VMCOREINFO_NUMBER(PG_swapcache); VMCOREINFO_NUMBER(PG_swapbacked); -#define PAGE_SLAB_MAPCOUNT_VALUE (~PG_slab) +#define PAGE_SLAB_MAPCOUNT_VALUE (PGTY_slab << 24) VMCOREINFO_NUMBER(PAGE_SLAB_MAPCOUNT_VALUE); #ifdef CONFIG_MEMORY_FAILURE VMCOREINFO_NUMBER(PG_hwpoison); #endif VMCOREINFO_NUMBER(PG_head_mask); -#define PAGE_BUDDY_MAPCOUNT_VALUE (~PG_buddy) +#define PAGE_BUDDY_MAPCOUNT_VALUE (PGTY_buddy << 24) VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE); -#define PAGE_HUGETLB_MAPCOUNT_VALUE (~PG_hugetlb) +#define PAGE_HUGETLB_MAPCOUNT_VALUE (PGTY_hugetlb << 24) VMCOREINFO_NUMBER(PAGE_HUGETLB_MAPCOUNT_VALUE); -#define PAGE_OFFLINE_MAPCOUNT_VALUE (~PG_offline) +#define PAGE_OFFLINE_MAPCOUNT_VALUE (PGTY_offline << 24) VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE); #ifdef CONFIG_KALLSYMS diff --git a/mm/debug.c b/mm/debug.c index 9f8e34537957..aa57d3ffd4ed 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -36,11 +36,6 @@ const struct trace_print_flags pageflag_names[] = { {0, NULL} }; -const struct trace_print_flags pagetype_names[] = { - __def_pagetype_names, - {0, NULL} -}; - const struct trace_print_flags gfpflag_names[] = { __def_gfpflag_names, {0, NULL} @@ -51,6 +46,27 @@ const struct trace_print_flags vmaflag_names[] = { {0, NULL} }; +#define DEF_PAGETYPE_NAME(_name) [PGTY_##_name - 0xf0] = __stringify(_name) + +static const char *page_type_names[] = { + DEF_PAGETYPE_NAME(slab), + DEF_PAGETYPE_NAME(hugetlb), + DEF_PAGETYPE_NAME(offline), + DEF_PAGETYPE_NAME(guard), + DEF_PAGETYPE_NAME(table), + DEF_PAGETYPE_NAME(buddy), + DEF_PAGETYPE_NAME(unaccepted), +}; + +static const char *page_type_name(unsigned int page_type) +{ + unsigned i = (page_type >> 24) - 0xf0; + + if (i >= ARRAY_SIZE(page_type_names)) + return "unknown"; + return page_type_names[i]; +} + static void __dump_folio(struct folio *folio, struct page *page, unsigned long pfn, unsigned long idx) { @@ -58,7 +74,7 @@ static void __dump_folio(struct folio *folio, struct page *page, int mapcount = atomic_read(&page->_mapcount); char *type = ""; - mapcount = page_type_has_type(mapcount) ? 0 : mapcount + 1; + mapcount = page_mapcount_is_type(mapcount) ? 0 : mapcount + 1; pr_warn("page: refcount:%d mapcount:%d mapping:%p index:%#lx pfn:%#lx\n", folio_ref_count(folio), mapcount, mapping, folio->index + idx, pfn); @@ -92,7 +108,8 @@ static void __dump_folio(struct folio *folio, struct page *page, pr_warn("%sflags: %pGp%s\n", type, &folio->flags, is_migrate_cma_folio(folio, pfn) ? " CMA" : ""); if (page_has_type(&folio->page)) - pr_warn("page_type: %x\n", folio->page.page_type); + pr_warn("page_type: %x(%s)\n", folio->page.page_type >> 24, + page_type_name(folio->page.page_type)); print_hex_dump(KERN_WARNING, "raw: ", DUMP_PREFIX_NONE, 32, sizeof(unsigned long), page,