mm: move PG_slab flag to page_type
Subsystems (5)
PATCH 1/1 mm: move PG_slab flag to page_type
For now, only SLAB uses _mapcount field as a number of active objects in a slab, and other slab allocators do not use it. As 16 bits are enough for that, use remaining 16 bits of _mapcount as page_type even when SLAB is used. And then move PG_slab flag to page_type!
Note that page_type is always placed in upper 16 bits of _mapcount to avoid confusing normal _mapcount as page_type. As underflow (actually I mean, yeah, overflow) is not a concern anymore, use more lower bits except bit zero.
Add more folio helpers for PAGE_TYPE_OPS() not to break existing slab implementations.
Remove PG_slab check from PAGE_FLAGS_CHECK_AT_FREE. buddy will still check if _mapcount is properly set at free.
Exclude PG_slab from hwpoison and show_page_flags() for now.
Note that with this patch, page_mapped() and folio_mapped() always return false for slab page.
---
I think this gives us two benefits:
- it frees a bit in flags field
- it makes it simpler to distinguish user-mapped pages and not-user-mapped pages.
Plus I'm writing a bit more of code including:
0) a few cleanup for code that checks !PageSlab() && page_mapped() or that does similar thing
1) provide human-readale string of page_type in dump_page
2) add show_page_types() for tracepoint
3) fix hwpoison ...etc.
Anyway This is an early RFC, I will very appreciate feedbacks!
include/linux/mm_types.h | 22 +++++++-- include/linux/page-flags.h | 83 ++++++++++++++++++++++++++-------- include/trace/events/mmflags.h | 1 - mm/memory-failure.c | 8 ---- mm/slab.h | 11 ++++- 5 files changed, 92 insertions(+), 33 deletions(-)
include/linux/mm_types.h +17 -5
@@ -193,12 +193,24 @@ struct page {atomic_t _mapcount;/* -* If the page is neither PageSlab nor mappable to userspace, -* the value stored here may help determine what this page -* is used for. See page-flags.h for a list of page types -* which are currently stored here. +* If the page is not mappable to userspace, the value +* stored here may help determine what this page is used for. +* See page-flags.h for a list of page types which are currently +* stored here. */ -unsigned int page_type; +struct { +/* +* Always place page_type in +* upper 16 bits of _mapcount +*/ +#ifdef CPU_BIG_ENDIAN +__u16 page_type; +__u16 active; +#else +__u16 active; +__u16 page_type; +#endif + }; };/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
include/linux/page-flags.h +65 -18
137 lines not on this page · raw message
include/trace/events/mmflags.h +0 -1
@@ -113,7 +113,6 @@ {1UL << PG_lru, "lru" }, \ {1UL << PG_active, "active" }, \ {1UL << PG_workingset, "workingset" }, \ - {1UL << PG_slab, "slab" }, \ {1UL << PG_owner_priv_1, "owner_priv_1" }, \ {1UL << PG_arch_1, "arch_1" }, \ {1UL << PG_reserved, "reserved" }, \
mm/memory-failure.c +0 -8
@@ -1123,7 +1123,6 @@ static int me_huge_page(struct page_state *ps, struct page *p)#define mlock (1UL << PG_mlocked)#define lru (1UL << PG_lru)#define head (1UL << PG_head) -#define slab (1UL << PG_slab)#define reserved (1UL << PG_reserved)static struct page_state error_states[] = {
@@ -1133,13 +1132,6 @@ static struct page_state error_states[] = {* PG_buddy pages only make a small fraction of all free pages. */ -/* -* Could in theory check if slab page is free or if we can drop -* currently unused objects without touching them. But just -* treat it as standard kernel for now. -*/ - { slab, slab, MF_MSG_SLAB, me_kernel }, - { head, head, MF_MSG_HUGE, me_huge_page }, { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty },
mm/slab.h +10 -1
@@ -20,7 +20,16 @@ struct slab { };struct rcu_head rcu_head; }; -unsigned int active; +struct { +/* always place page_type in upper 16 bits of _mapcount */ +#ifdef CPU_BIG_ENDIAN +__u16 page_type; +__u16 active; +#else +__u16 active; +__u16 page_type; +#endif + };#elif defined(CONFIG_SLUB)
Signature · 1 line
2.32.0
Quoted 48 lines · Hyeonggon Yoo
For now, only SLAB uses
_mapcountfield as a number of active objects in a slab, and other slab allocators do not use it. As 16 bits are enough for that, use remaining 16 bits of_mapcountaspage_typeeven when SLAB is used. And then movePG_slabflag topage_type!Note that
page_typeis always placed in upper 16 bits of_mapcountto avoid confusing normal_mapcountaspage_type. As underflow (actually I mean, yeah, overflow) is not a concern anymore, use more lower bits except bit zero.Add more folio helpers for
PAGE_TYPE_OPS()not to break existing slab implementations.Remove
PG_slabcheck fromPAGE_FLAGS_CHECK_AT_FREE. buddy will still check if_mapcountis properly set at free.Exclude
PG_slabfrom hwpoison andshow_page_flags()for now.Note that with this patch,
page_mapped()andfolio_mapped()always return false for slab page.Cc Andrew Morton <[email protected]>Cc Christoph Lameter <[email protected]>Cc Pekka Enberg <[email protected]>Cc David Rientjes <[email protected]>Cc Joonsoo Kim <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Roman Gushchin <[email protected]>Cc Naoya Horiguchi <[email protected]>Cc Miaohe Lin <[email protected]>Cc "Matthew Wilcox (Oracle)" <[email protected]>Cc Minchan Kim <[email protected]>Cc Mel Gorman <[email protected]>Cc Andrea Arcangeli <[email protected]>Cc Dan Williams <[email protected]>Cc Hugh Dickins <[email protected]>Cc Muchun Song <[email protected]>Cc David Hildenbrand <[email protected]>Cc Andrey Konovalov <[email protected]>Cc Marco Elver <[email protected]>Signed-off-by Hyeonggon Yoo <[email protected]>---
I think this gives us two benefits:
- it frees a bit in flags field
- it makes it simpler to distinguish user-mapped pages and not-user-mapped pages.
Quoted 8 lines
Plus I'm writing a bit more of code including:
0) a few cleanup for code that checks !PageSlab() &&page_mapped()or that does similar thing
1) provide human-readale string ofpage_typeindump_page
2) addshow_page_types()for tracepoint
3) fix hwpoison ...etc.Anyway This is an early RFC, I will very appreciate feedbacks!
Looks like I forgot to add RFC in subject :)
Quoted 13 lines · Hyeonggon Yoo
For now, only SLAB uses
_mapcountfield as a number of active objects in a slab, and other slab allocators do not use it. As 16 bits are enough for that, use remaining 16 bits of_mapcountaspage_typeeven when SLAB is used. And then movePG_slabflag topage_type!Note that
page_typeis always placed in upper 16 bits of_mapcountto avoid confusing normal_mapcountaspage_type. As underflow (actually I mean, yeah, overflow) is not a concern anymore, use more lower bits except bit zero.Add more folio helpers for
PAGE_TYPE_OPS()not to break existing slab implementations.
Quoted 8 lines
Remove
PG_slabcheck fromPAGE_FLAGS_CHECK_AT_FREE. buddy will still check if_mapcountis properly set at free.Exclude
PG_slabfrom hwpoison andshow_page_flags()for now.Note that with this patch,
page_mapped()andfolio_mapped()always return false for slab page.
[...]
Hi. a silly mistake:
Quoted 21 lines
include/linux/mm_types.h | 22 +++++++-- include/linux/page-flags.h | 83 ++++++++++++++++++++++++++-------- include/trace/events/mmflags.h | 1 - mm/memory-failure.c | 8 ---- mm/slab.h | 11 ++++- 5 files changed, 92 insertions(+), 33 deletions(-)include/linux/mm_types.h +2
-4@@ -193,12 +193,24 @@ struct page {atomic_t _mapcount;/* -* If the page is neither PageSlab nor mappable to userspace, -* the value stored here may help determine what this page -* is used for. See page-flags.h for a list of page types -* which are currently stored here. +* If the page is not mappable to userspace, the value +* stored here may help determine what this page is used for.
include/linux/mm_types.h struct page { · line 206
Quoted 10 lines
include/linux/mm_types.h +8
-1+* See page-flags.h for a list of page types which are currently +* stored here. */ -unsigned int page_type; +struct { +/* +* Always place page_type in +* upper 16 bits of _mapcount +*/ +#ifdef CPU_BIG_ENDIAN
s/CPU_BIG_ENDIAN/CONFIG_CPU_BIG_ENDIAN/g
Quoted 10 lines
include/linux/mm_types.h +7
-0+__u16 page_type; +__u16 active; +#else +__u16 active; +__u16 page_type; +#endif + }; };/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
[...]
mm/slab.h struct slab { · line 25
Quoted 12 lines
mm/slab.h +3
-1@@ -20,7 +20,16 @@ struct slab { };struct rcu_head rcu_head; }; -unsigned int active; +struct { +/* always place page_type in upper 16 bits of _mapcount */ +#ifdef CPU_BIG_ENDIAN
same here.
Quoted 13 lines
mm/slab.h +7
-0+__u16 page_type; +__u16 active; +#else +__u16 active; +__u16 page_type; +#endif + };#elif defined(CONFIG_SLUB)Signature · 1 line
2.32.0
Signature · 2 lines
Thanks, Hyeonggon
Quoted 13 lines · Hyeonggon Yoo
For now, only SLAB uses
_mapcountfield as a number of active objects in a slab, and other slab allocators do not use it. As 16 bits are enough for that, use remaining 16 bits of_mapcountaspage_typeeven when SLAB is used. And then movePG_slabflag topage_type!Note that
page_typeis always placed in upper 16 bits of_mapcountto avoid confusing normal_mapcountaspage_type. As underflow (actually I mean, yeah, overflow) is not a concern anymore, use more lower bits except bit zero.Add more folio helpers for
PAGE_TYPE_OPS()not to break existing slab implementations.
Quoted 7 lines
Remove
PG_slabcheck fromPAGE_FLAGS_CHECK_AT_FREE. buddy will still check if_mapcountis properly set at free.Exclude
PG_slabfrom hwpoison andshow_page_flags()for now.Note that with this patch,
page_mapped()andfolio_mapped()always return false for slab page.
This is an interesting approach. It raises some questions.
First, you say that folio_mapped() returns false for slab pages. That's only true for order-0 slab pages. For larger pages,
if (!folio_test_large(folio))
return atomic_read(&folio->_mapcount) >= 0;
if (atomic_read(folio_mapcount_ptr(folio)) >= 0)
return true;so that's going to depend what folio_mapcount_ptr() aliases with.
Second, this patch changes the behaviour of PageSlab() when applied to tail pages. Which raises the further question of what PageBuddy(), PageTable(), PageGuard() and PageIsolated() should do for multi-page folios, if that is even possible.
Third, can we do this without that awkward __u16 thing? Perhaps
-#define PG_buddy 0x00000080 -#define PG_offline 0x00000100 -#define PG_table 0x00000200 -#define PG_guard 0x00000400 +#define PG_buddy 0x00010000 +#define PG_offline 0x00020000 +#define PG_table 0x00040000 +#define PG_guard 0x00080000 +#define PG_slab 0x00100000
... and then use wrappers in slab.c to access the bottom 16 bits?
Quoted 32 lines · Matthew Wilcox
Quoted 13 lines · Hyeonggon Yoo
For now, only SLAB uses
_mapcountfield as a number of active objects in a slab, and other slab allocators do not use it. As 16 bits are enough for that, use remaining 16 bits of_mapcountaspage_typeeven when SLAB is used. And then movePG_slabflag topage_type!Note that
page_typeis always placed in upper 16 bits of_mapcountto avoid confusing normal_mapcountaspage_type. As underflow (actually I mean, yeah, overflow) is not a concern anymore, use more lower bits except bit zero.Add more folio helpers for
PAGE_TYPE_OPS()not to break existing slab implementations.Quoted 7 lines
Remove
PG_slabcheck fromPAGE_FLAGS_CHECK_AT_FREE. buddy will still check if_mapcountis properly set at free.Exclude
PG_slabfrom hwpoison andshow_page_flags()for now.Note that with this patch,
page_mapped()andfolio_mapped()always return false for slab page.This is an interesting approach. It raises some questions.
First, you say that
folio_mapped()returns false for slab pages. That's only true for order-0 slab pages. For larger pages,if (!folio_test_large(folio)) return atomic_read(&folio->_mapcount) >= 0; if (atomic_read(folio_mapcount_ptr(folio)) >= 0) return true;
Quoted 6 lines
so that's going to depend what
folio_mapcount_ptr()aliases with.Second, this patch changes the behaviour of
PageSlab()when applied to tail pages. Which raises the further question of whatPageBuddy(),PageTable(),PageGuard()andPageIsolated()should do for multi-page folios, if that is even possible.
IIRC, these flags never apply on real compound pages so far. For example, PageBuddy() is only set on the first page of a (budy-aligned) free chunk of memory, and all "remaining" (tail) pages have a refcount of zero and don't have the flag set.
There are page tables on some systems (e.g., s390x) that span multiple pages (pmd_alloc_one()). I think the behavior is similar -- no compound page, and only the first page has the flag set.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 17 lines · Matthew Wilcox
Quoted 16 lines · Hyeonggon Yoo
For now, only SLAB uses
_mapcountfield as a number of active objects in a slab, and other slab allocators do not use it. As 16 bits are enough for that, use remaining 16 bits of_mapcountaspage_typeeven when SLAB is used. And then movePG_slabflag topage_type!Note that
page_typeis always placed in upper 16 bits of_mapcountto avoid confusing normal_mapcountaspage_type. As underflow (actually I mean, yeah, overflow) is not a concern anymore, use more lower bits except bit zero.Add more folio helpers for
PAGE_TYPE_OPS()not to break existing slab implementations.Remove
PG_slabcheck fromPAGE_FLAGS_CHECK_AT_FREE. buddy will still check if_mapcountis properly set at free.
Quoted 6 lines
Quoted 4 lines
Exclude
PG_slabfrom hwpoison andshow_page_flags()for now.Note that with this patch,
page_mapped()andfolio_mapped()always return false for slab page.This is an interesting approach. It raises some questions.
Hello Matthew, sorry for late reply and I didn't mean to ignore your feedback. I realized compound pages and folio stuffs are my weak side and needed some time to learn :)
Quoted 9 lines
First, you say that
folio_mapped()returns false for slab pages. That's only true for order-0 slab pages. For larger pages,if (!folio_test_large(folio)) return atomic_read(&folio->_mapcount) >= 0; if (atomic_read(folio_mapcount_ptr(folio)) >= 0) return true;so that's going to depend what
folio_mapcount_ptr()aliases with.
IIUC it's true for order > 0 slab too.
As slab pages are not mapped to userspace at all, entire compound page nor base pages are not mapped to userspace.
AFAIK followings are true for order > 0 slab:
- (first tail page)->compound_mapcount is -1
- _mapcount of base pages are -1
So:
folio_mapped() and page_mapped() (if applied to head page) returns false for larger pages with this patch.
I wrote simple testcase and did check that folio_mapped() and page_mapped() returns false for both order-0 page and larger pages. (and SLAB returned true for them before)
Quoted 2 lines
Second, this patch changes the behaviour of
PageSlab()when applied to tail pages.
Altough it changes the way it checks the flag,
it does not change behavior when applied to tail pages - PageSlab() on tail page returns false with or without this patch.
If PageSlab() need to return true for tail pages too, we may make it check page_type at head page.
But I'm not sure when it the behavior is needed.
Can you please share your insight on this?
Quoted 3 lines
Which raises the further question of what
PageBuddy(),PageTable(),PageGuard()andPageIsolated()should do for multi-page folios, if that is even possible.
For users that uses real compound page like slab, we can make it check page_type of head page. (if needed)
But for cases David described, there isn't much thing we can do except making them to use real compound pages.
Quoted 12 lines
Third, can we do this without that awkward
__u16thing? Perhaps(quoted diff) +5
-4-#define PG_buddy 0x00000080 -#define PG_offline 0x00000100 -#define PG_table 0x00000200 -#define PG_guard 0x00000400 +#define PG_buddy 0x00010000 +#define PG_offline 0x00020000 +#define PG_table 0x00040000 +#define PG_guard 0x00080000 +#define PG_slab 0x00100000
Quoted 1 line
... and then use wrappers in
slab.cto access the bottom 16 bits?
Definitely! I prefer that way and will adjust in RFC v2.
Thank you for precious feedback.
Signature · 1 line
Hyeonggon
Quoted 10 lines · Hyeonggon Yoo
Quoted 9 lines
First, you say that
folio_mapped()returns false for slab pages. That's only true for order-0 slab pages. For larger pages,if (!folio_test_large(folio)) return atomic_read(&folio->_mapcount) >= 0; if (atomic_read(folio_mapcount_ptr(folio)) >= 0) return true;so that's going to depend what
folio_mapcount_ptr()aliases with.
Quoted 7 lines
IIUC it's true for order > 0 slab too.
As slab pages are not mapped to userspace at all, entire compound page nor base pages are not mapped to userspace.
AFAIK followings are true for order > 0 slab:
- (first tail page)->compound_mapcount is -1
That's the part I wasn't sure of. I think we do, in prep_compound_head().
Quoted 10 lines
-
_mapcountof base pages are -1So:
folio_mapped()andpage_mapped()(if applied to head page) returns false for larger pages with this patch.I wrote simple testcase and did check that
folio_mapped()andpage_mapped()returns false for both order-0 page and larger pages. (and SLAB returned true for them before)
Quoted 7 lines
Quoted 2 lines
Second, this patch changes the behaviour of
PageSlab()when applied to tail pages.Altough it changes the way it checks the flag,
it does not change behavior when applied to tail pages -
PageSlab()on tail page returns false with or without this patch.
Really? It seems to me that it returns true at the moment. Look:
__PAGEFLAG(Slab, slab, PF_NO_TAIL)
#define PF_NO_TAIL(page, enforce) ({ \
VM_BUG_ON_PGFLAGS(enforce && PageTail(page), page); \
PF_POISONED_CHECK(compound_head(page)); })so AFAICS, PageSlab checks the Slab bit on the head page, not the tail page.
Quoted 5 lines
If
PageSlab()need to return true for tail pages too, we may make it checkpage_typeat head page.But I'm not sure when it the behavior is needed.
Can you please share your insight on this?
There are tools like tools/vm/page-types.c which expect PageSlab to return true for tail pages.
Quoted 22 lines
Quoted 3 lines
Which raises the further question of what
PageBuddy(),PageTable(),PageGuard()andPageIsolated()should do for multi-page folios, if that is even possible.For users that uses real compound page like slab, we can make it check
page_typeof head page. (if needed)But for cases David described, there isn't much thing we can do except making them to use real compound pages.
Quoted 12 lines
Third, can we do this without that awkward
__u16thing? Perhaps(quoted diff) +5
-4-#define PG_buddy 0x00000080 -#define PG_offline 0x00000100 -#define PG_table 0x00000200 -#define PG_guard 0x00000400 +#define PG_buddy 0x00010000 +#define PG_offline 0x00020000 +#define PG_table 0x00040000 +#define PG_guard 0x00080000 +#define PG_slab 0x00100000
Quoted 5 lines
Quoted 1 line
... and then use wrappers in
slab.cto access the bottom 16 bits?Definitely! I prefer that way and will adjust in RFC v2.
Thank you for precious feedback.
No problem. I suggested (in an off-list email) that you consider counting 'active' by subtraction rather than addition because I have a feeling that
int active(struct slab *slab)
{
return ~(slab->page_type | PG_slab);
}would be better than
int active(struct slab *slab)
{
return slab->page_type & 0xffff;
}at least in part because you don't have to clear the bottom 16 bits of page_type when you clear PG_slab, and you don't have to re-set them when you set PG_slab.
Quoted 13 lines · Matthew Wilcox
Quoted 12 lines · Hyeonggon Yoo
Quoted 9 lines
First, you say that
folio_mapped()returns false for slab pages. That's only true for order-0 slab pages. For larger pages,if (!folio_test_large(folio)) return atomic_read(&folio->_mapcount) >= 0; if (atomic_read(folio_mapcount_ptr(folio)) >= 0) return true;so that's going to depend what
folio_mapcount_ptr()aliases with.IIUC it's true for order > 0 slab too.
Quoted 8 lines
Quoted 5 lines
As slab pages are not mapped to userspace at all, entire compound page nor base pages are not mapped to userspace.
AFAIK followings are true for order > 0 slab:
- (first tail page)->compound_mapcount is -1That's the part I wasn't sure of. I think we do, in
prep_compound_head().
Right, exactly!
Quoted 10 lines
Quoted 10 lines
-
_mapcountof base pages are -1So:
folio_mapped()andpage_mapped()(if applied to head page) returns false for larger pages with this patch.I wrote simple testcase and did check that
folio_mapped()andpage_mapped()returns false for both order-0 page and larger pages. (and SLAB returned true for them before)
FYI, This is still true even after fixing my mistaken test case (see below)
Quoted 11 lines
Quoted 8 lines
Quoted 3 lines
Second, this patch changes the behaviour of
PageSlab()when applied to tail pages.Altough it changes the way it checks the flag,
it does not change behavior when applied to tail pages -
PageSlab()on tail page returns false with or without this patch.Really? It seems to me that it returns true at the moment. Look:
Quoted 8 lines
__PAGEFLAG(Slab, slab, PF_NO_TAIL)#define PF_NO_TAIL(page, enforce) ({ \ VM_BUG_ON_PGFLAGS(enforce && PageTail(page), page); \ PF_POISONED_CHECK(compound_head(page)); })so AFAICS, PageSlab checks the Slab bit on the head page, not the tail page.
You are right. I misunderstood it due to my mistakenly written test case (without passing __GFP_COMP... how silly of me :D)
Hmm okay, then I will implement PF_NO_TAIL policy that works on page_type.
Quoted 48 lines
Quoted 6 lines
If
PageSlab()need to return true for tail pages too, we may make it checkpage_typeat head page.But I'm not sure when it the behavior is needed.
Can you please share your insight on this?There are tools like
tools/vm/page-types.cwhich expect PageSlab to return true for tail pages.Quoted 22 lines
Quoted 3 lines
Which raises the further question of what
PageBuddy(),PageTable(),PageGuard()andPageIsolated()should do for multi-page folios, if that is even possible.For users that uses real compound page like slab, we can make it check
page_typeof head page. (if needed)But for cases David described, there isn't much thing we can do except making them to use real compound pages.
Quoted 12 lines
Third, can we do this without that awkward
__u16thing? Perhaps(quoted diff) +5
-4-#define PG_buddy 0x00000080 -#define PG_offline 0x00000100 -#define PG_table 0x00000200 -#define PG_guard 0x00000400 +#define PG_buddy 0x00010000 +#define PG_offline 0x00020000 +#define PG_table 0x00040000 +#define PG_guard 0x00080000 +#define PG_slab 0x00100000Quoted 5 lines
Quoted 1 line
... and then use wrappers in
slab.cto access the bottom 16 bits?Definitely! I prefer that way and will adjust in RFC v2.
Thank you for precious feedback.
No problem. I suggested (in an off-list email) that you consider counting 'active' by subtraction rather than addition because I have a feeling that
int
active(struct slab *slab){ return ~(slab->page_type | PG_slab); }would be better than
Quoted 8 lines
int
active(struct slab *slab){ return slab->page_type & 0xffff; }at least in part because you don't have to clear the bottom 16 bits of
page_typewhen you clearPG_slab, and you don't have to re-set them when you setPG_slab.
Yeah, I was wondering what is the benefit of the that approach. After implementing both approach, your suggestion seems better to me too.
Many thanks, Matthew!
Signature · 1 line
Hyeonggon