[PATCH RFC v3] mm: Proper document tail pages fields for folio

From: Peter Xu
Date: Tue Aug 15 2023 - 17:27:37 EST


Tail page struct reuse is over-comlicated. Not only because we have
implicit uses of tail page fields (mapcounts, or private for thp swap
support, etc., that we may still use in the page structs, but not obvious
the relationship between that and the folio definitions), but also because
we have 32/64 bits layouts for struct page so it's unclear what we can use
and what we cannot when trying to find a new spot in folio struct.

It's also unclear on how many fields we can reuse for a tail page. The
real answer is (after help from Matthew): we have 7 WORDs guaranteed on 64
bits and 8 WORDs on 32 bits. Nothing more than that is guaranteed to even
exist. That means nothing over page->_refcount field can be reused.

Let's document it clearly on what we can use and what we can't when
extending folio on reusing tail page fields, with explanations on each of
them. Hopefully after the doc update it will make it easier when:

(1) Any reader to know exactly what folio field is where and for what,
the relationships between folio tail pages and struct page definitions,

(2) Any potential new fields to be added to a large folio, so we're clear
which field one can still reuse.

This is assuming WORD is defined as sizeof(void *) on any archs, just like
the other comment in struct page we already have.

The _mapcount/_refcount fields are also added for each tail page to clamp
the fields tight, with FOLIO_MATCH() making sure nothing messed up the
ordering.

Signed-off-by: Peter Xu <peterx@xxxxxxxxxx>
---

rfcv1: https://lore.kernel.org/all/20230810204944.53471-1-peterx@xxxxxxxxxx
rfcv2: https://lore.kernel.org/r/20230814184411.330496-1-peterx@xxxxxxxxxx

No change log since it changed quite a bit; I sent patch 1 separately as
non-rfc, while I merged the rest two patches because I just noticed I can
avoid reorder the fields, so no functional change should be intended, hence
no reason to split either.

Matthew, I wanted to remove the whole chunk of comments above the tail
pages from last version (which might fall into "over-documented" category),
but at last I still kept it; not only because I found that helpful to give
me a whole picture (maybe only me?), but also it's a good place to document
a few important things (e.g., on the fact that refcnt==0 is a must for all
tails). I'm open to removing the chunk or part of it, if you think the
rest is still ok.

This of course also conflict so far with the other series to drop
folio_order/... but I can always rebase if this is not NACKed.

Comments welcomed, thanks.
---
include/linux/mm_types.h | 69 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 66 insertions(+), 3 deletions(-)

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 81456fa5fda5..66f1b0814334 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -324,6 +324,35 @@ struct folio {
};
struct page page;
};
+ /*
+ * Some of the tail page fields may not be reused by the folio
+ * object because they have already been used by the page struct.
+ * On 32bits there are at least 8 WORDs while on 64 bits there're
+ * at least 7 WORDs, all ending at _refcount field.
+ *
+ * |--------+-------------+-------------------|
+ * | index | 32 bits | 64 bits |
+ * |--------+-------------+-------------------|
+ * | 0 | flags | flags |
+ * | 1 | head | head |
+ * | 2 | FREE | FREE |
+ * | 3 | FREE [1] | FREE [1] |
+ * | 4 | FREE | FREE |
+ * | 5 | FREE | private [2] |
+ * | 6 | mapcnt | mapcnt+refcnt [3] |
+ * | 7 | refcnt [3] | |
+ * |--------+-------------+-------------------|
+ *
+ * [1] "mapping" field. It is free to use but needs to be with
+ * some caution due to poisoning, see TAIL_MAPPING_REUSED_MAX.
+ *
+ * [2] "private" field, used when THP_SWAP is on (but disabled on
+ * 32 bits, so this index is FREE on 32bit or hugetlb folios).
+ * May need to be fixed finally.
+ *
+ * [3] "refcount" field must be zero for all tail pages. See e.g.
+ * has_unmovable_pages() on page_ref_count() check and comment.
+ */
union {
struct {
unsigned long _flags_1;
@@ -331,18 +360,29 @@ struct folio {
/* public: */
unsigned char _folio_dtor;
unsigned char _folio_order;
+ /* private: 2 bytes can be reused later */
+ unsigned char _free_1_0[2];
+ /* public: */
atomic_t _entire_mapcount;
atomic_t _nr_pages_mapped;
atomic_t _pincount;
#ifdef CONFIG_64BIT
unsigned int _folio_nr_pages;
+ /* private: 4 bytes can be reused later (64 bits only) */
+ unsigned char _free_1_1[4];
+ /* Currently used by THP_SWAP, to be fixed */
+ void *_private_1;
+ /* public: */
#endif
+ /* private: */
+ atomic_t _mapcount_1;
+ atomic_t _refcount_1;
/* private: the union with struct page is transitional */
};
struct page __page_1;
};
union {
- struct {
+ struct { /* hugetlb folios */
unsigned long _flags_2;
unsigned long _head_2;
/* public: */
@@ -351,13 +391,22 @@ struct folio {
void *_hugetlb_cgroup_rsvd;
void *_hugetlb_hwpoison;
/* private: the union with struct page is transitional */
+ atomic_t _mapcount_2;
+ atomic_t _refcount_2;
};
- struct {
+ struct { /* non-hugetlb folios */
unsigned long _flags_2a;
unsigned long _head_2a;
/* public: */
struct list_head _deferred_list;
- /* private: the union with struct page is transitional */
+ /* private: 8 more free bytes for either 32/64 bits */
+ unsigned char _free_2_2[8];
+#ifdef CONFIG_64BIT
+ /* currently used by THP_SWAP, to be fixed */
+ void *_private_2a;
+#endif
+ atomic_t _mapcount_2a;
+ atomic_t _refcount_2a;
};
struct page __page_2;
};
@@ -382,12 +431,26 @@ FOLIO_MATCH(memcg_data, memcg_data);
offsetof(struct page, pg) + sizeof(struct page))
FOLIO_MATCH(flags, _flags_1);
FOLIO_MATCH(compound_head, _head_1);
+#ifdef CONFIG_64BIT
+FOLIO_MATCH(private, _private_1);
+#endif
+FOLIO_MATCH(_mapcount, _mapcount_1);
+FOLIO_MATCH(_refcount, _refcount_1);
#undef FOLIO_MATCH
#define FOLIO_MATCH(pg, fl) \
static_assert(offsetof(struct folio, fl) == \
offsetof(struct page, pg) + 2 * sizeof(struct page))
FOLIO_MATCH(flags, _flags_2);
FOLIO_MATCH(compound_head, _head_2);
+FOLIO_MATCH(_mapcount, _mapcount_2);
+FOLIO_MATCH(_refcount, _refcount_2);
+FOLIO_MATCH(flags, _flags_2a);
+FOLIO_MATCH(compound_head, _head_2a);
+FOLIO_MATCH(_mapcount, _mapcount_2a);
+FOLIO_MATCH(_refcount, _refcount_2a);
+#ifdef CONFIG_64BIT
+FOLIO_MATCH(private, _private_2a);
+#endif
#undef FOLIO_MATCH

/*
--
2.41.0