From mboxrd@z Thu Jan 1 00:00:00 1970 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 27491C54739 for ; Wed, 28 Aug 2024 03:35:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C3AB6B0096; Tue, 27 Aug 2024 23:35:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 673FB6B0098; Tue, 27 Aug 2024 23:35:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53AEF6B0099; Tue, 27 Aug 2024 23:35:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 35DBD6B0096 for ; Tue, 27 Aug 2024 23:35:38 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EE076161964 for ; Wed, 28 Aug 2024 03:35:37 +0000 (UTC) X-FDA: 82500239514.27.4E46163 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf09.hostedemail.com (Postfix) with ESMTP id DA9D9140017 for ; Wed, 28 Aug 2024 03:35:34 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724816071; a=rsa-sha256; cv=none; b=x8Sscndjp1x8tngac+ikjTrS49ipxjTnEhpN1+7tAgIHR3RVabA3CUdQtsNDI9MUp3So48 yigduzQNjvMPnZGwR8gJmtJ5+I+NyET2g6HOkr7x8U1tI/DDOv/vsSf/uOSq+xbUjfc+KY GiKq50tFuPT3BjH9fGkC2h4onudETrg= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724816071; 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=9jlrR8NXjnesbj2Km1v+6hvo5g2IH4tP8xQMuJ83oIo=; b=MDpnstNzzaoV1tKUoCsAniY0iip6hfLn9hO0ZtgFNWYTsHfntJmig94sL4hh14F9/uZWl/ Z8w4Bo1dd5mEkbaw3s8K3KqjuCZql3ixQjfXYfK87lw6dmdvHvcA4FxfL3plQDiSe3h3J6 YPsrd1+MZY1YNIwpoEbCMTkUVJWLQFo= Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Wtqj61K2qz1HHJy; Wed, 28 Aug 2024 11:32:10 +0800 (CST) Received: from dggpemf100008.china.huawei.com (unknown [7.185.36.138]) by mail.maildlp.com (Postfix) with ESMTPS id 9AECF1402CF; Wed, 28 Aug 2024 11:35:29 +0800 (CST) Received: from [10.174.177.243] (10.174.177.243) by dggpemf100008.china.huawei.com (7.185.36.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 28 Aug 2024 11:35:29 +0800 Message-ID: <2d19c48a-c550-4345-bf36-d05cd303c5de@huawei.com> Date: Wed, 28 Aug 2024 11:35:28 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 3/4] mm: Support only one page_type per page Content-Language: en-US To: "Matthew Wilcox (Oracle)" , Andrew Morton CC: David Hildenbrand , Hyeonggon Yoo <42.hyeyoo@gmail.com>, , Kent Overstreet References: <20240821173914.2270383-1-willy@infradead.org> <20240821173914.2270383-4-willy@infradead.org> From: Kefeng Wang In-Reply-To: <20240821173914.2270383-4-willy@infradead.org> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.243] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemf100008.china.huawei.com (7.185.36.138) X-Rspamd-Queue-Id: DA9D9140017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 9nqom8op6mg54rh4yuubqp4nb6ximmh6 X-HE-Tag: 1724816134-533827 X-HE-Meta: U2FsdGVkX19d1c0SoI9bo6zfwFCYwkmRLmqnQtKVaSWdZW5wh7bACvTzIyKU3aSIhWU8ol5OyzuhPw3IWDnOSzgAD+jS0Gex29LxHOR1EG5kL45rxenkAc+0fJ1ZZ84Cjd9eh2VkYKJHOX3pjbrF1mOD7SbIKjyveWJiqt4FZTKnLkotra6g+TjnKAgD/iqGaitUyvkiDMkjxfcLfnW5FLdGcb8rFSzyXhzWmVW3lNn2Zt1qz2mjJDQGkxQsHlXyL2enQvgNcQ35LL/A0ICzcOCPtL2/hvBZ4Bnr1Je1sQYOw7NmMcIM56cfwwPzkR73aqqfqd9Ji9KhLAIAObvzN7QcqrZMTuNiyqjMmYZkVOlOXUub/958ekCwR/u25NY6oV9ov8kXvby81HQQDYPEpn14CDuR/lOw723KsThHZfjBp06MxgN9Wkt5B6CtfZp6zaHWIyk1DJ5lhGmIzKWjGDysAiMCRmjdJM0U4Suo+3z9DMDWw7K5wdLO817Zs9Drs7mVubvJLV/s8FdYtpCunQQ2NrUT73/LRGQ4kTN2dwlkANhiv1toArZoz6mNJRXBopDPZZmiyLztYqSvP9knQ7bBBdxhcuVrsxRDQMPdN0FinyYtF1yQfKEvSWghY9uwRFmCZO4cODoN/k9BvemdwPaxdMAqRS7D9lzYXZDBVnfoOg8icEX7q+pfmqMCnxVWSPjdhEpesnRjUlvNY1JjFOsyA2r3UkeLu59FnCGxuovcf+n/U18Ur7mZIuUXhkzPgVS7qE+OOyP1yBgCZcOgDTYb8nlEQDxwaTyaQUD9pOkwKhHqmPI0LdOrTMiwM14r5D2AYr9s1OhZTgQou1GwyzaWZMpW2rZZhzKJOv+kwZJ7UVDH+990le03hi1i/huOHiVdKPGzWK27TpDY9JfpokryLpKyHcfxVfMImvS41S3lLY4LdwsBh532ZezWywVn4wRnwmYyaRZT4ZfVAA2 CppKlJSg +GNt1JEdFzrZtGBWfbuDA4pJ4Oi/vEPDjBJc3tJVo/9G1x4P4d2yrnuFCgTltfcMP64T/GJjPFeIiFrDhIi1npZi9LKpFm8EfIUfxAtnyrICwcWcBZSIBtNmAVmSrmveiNRz+jTxmwPiaAuntZdAM8gW5Gw4f5K26cLsnlkkzOJHByKVpd9UJtuSXPt9LgueVHjyW8X6h4kEYXj5oj8s5nxjhJh4Zk3pBhFqyGfpZxREE3K02i+WmODqMhR18GePs+1mwnCVx72btrSlg4XeA1XQDZJHOkqVohEH/nuTOUDh3hL4L3CLjZ/mqMTr8YAk8vTxOOy6UYo/O3ZZxDHwluDEwMg== 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: Hi Matthew, On 2024/8/22 1:39, Matthew Wilcox (Oracle) wrote: > 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) > --- > 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; \ > } > There are some UBSAN warning about __folio_set_##fname/__SetPage##uname, UBSAN: shift-out-of-bounds in ../include/linux/page-flags.h:998:1 left shift of 240 by 24 places cannot be represented in type 'int' ... Changing significant bit to unsigned to fix it. diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 28d2337525d1..2175ebceb41c 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -966,7 +966,7 @@ static __always_inline void __folio_set_##fname(struct folio *folio) \ { \ VM_BUG_ON_FOLIO(data_race(folio->page.page_type) != UINT_MAX, \ folio); \ - folio->page.page_type = PGTY_##lname << 24; \ + folio->page.page_type = (unsigned int)PGTY_##lname << 24; \ } \ static __always_inline void __folio_clear_##fname(struct folio *folio) \ { \ @@ -983,7 +983,7 @@ static __always_inline int Page##uname(const struct page *page) \ static __always_inline void __SetPage##uname(struct page *page) \ { \ VM_BUG_ON_PAGE(data_race(page->page_type) != UINT_MAX, page); \ - page->page_type = PGTY_##lname << 24; \ + page->page_type = (unsigned int)PGTY_##lname << 24; \ } \