]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mm: let _folio_nr_pages overlay memcg_data in first tail page
authorDavid Hildenbrand <david@redhat.com>
Mon, 3 Mar 2025 16:29:56 +0000 (17:29 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 18 Mar 2025 05:06:43 +0000 (22:06 -0700)
Let's free up some more of the "unconditionally available on 64BIT" space
in order-1 folios by letting _folio_nr_pages overlay memcg_data in the
first tail page (second folio page).  Consequently, we have the
optimization now whenever we have CONFIG_MEMCG, independent of 64BIT.

We have to make sure that page->memcg on tail pages does not return
"surprises".  page_memcg_check() already properly refuses PageTail().
Let's do that earlier in print_page_owner_memcg() to avoid printing wrong
"Slab cache page" information.  No other code should touch that field on
tail pages of compound pages.

Reset the "_nr_pages" to 0 when splitting folios, or when freeing them
back to the buddy (to avoid false page->memcg_data "bad page" reports).

Note that in __split_huge_page(), folio_nr_pages() would stop working
already as soon as we start messing with the subpages.

Most kernel configs should have at least CONFIG_MEMCG enabled, even if
disabled at runtime.  64byte "struct memmap" is what we usually have on
64BIT.

While at it, rename "_folio_nr_pages" to "_nr_pages".

Hopefully memdescs / dynamically allocating "strut folio" in the future
will further clean this up, e.g., making _nr_pages available in all
configs and maybe even in small folios.  Doing that should be fairly easy
on top of this change.

[david@redhat.com: make "make htmldoc" happy]
Link: https://lkml.kernel.org/r/a97f8a91-ec41-4796-81e3-7c9e0e491ba4@redhat.com
Link: https://lkml.kernel.org/r/20250303163014.1128035-4-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Andy Lutomirks^H^Hski <luto@kernel.org>
Cc: Borislav Betkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jann Horn <jannh@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Lance Yang <ioworker0@gmail.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Matthew Wilcow (Oracle) <willy@infradead.org>
Cc: Michal Koutn <mkoutny@suse.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: tejun heo <tj@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Zefan Li <lizefan.x@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/mm.h
include/linux/mm_types.h
mm/huge_memory.c
mm/internal.h
mm/page_alloc.c
mm/page_owner.c

index cf892c08a88cd6d364872eafeb8fe1dd45b6e052..c9c2ca34535022ad462932e50cc51654f4cfb374 100644 (file)
@@ -1199,10 +1199,10 @@ static inline unsigned int folio_large_order(const struct folio *folio)
        return folio->_flags_1 & 0xff;
 }
 
-#ifdef CONFIG_64BIT
+#ifdef NR_PAGES_IN_LARGE_FOLIO
 static inline long folio_large_nr_pages(const struct folio *folio)
 {
-       return folio->_folio_nr_pages;
+       return folio->_nr_pages;
 }
 #else
 static inline long folio_large_nr_pages(const struct folio *folio)
index b1827d78ff89142de3e6e78a493337099fa2c325..f3b519fbeca14ea23c54ae8dfb4b8f2ebd507b52 100644 (file)
@@ -287,6 +287,11 @@ typedef struct {
        unsigned long val;
 } swp_entry_t;
 
+#if defined(CONFIG_MEMCG) || defined(CONFIG_SLAB_OBJ_EXT)
+/* We have some extra room after the refcount in tail pages. */
+#define NR_PAGES_IN_LARGE_FOLIO
+#endif
+
 /**
  * struct folio - Represents a contiguous set of bytes.
  * @flags: Identical to the page flags.
@@ -312,7 +317,7 @@ typedef struct {
  * @_large_mapcount: Do not use directly, call folio_mapcount().
  * @_nr_pages_mapped: Do not use outside of rmap and debug code.
  * @_pincount: Do not use directly, call folio_maybe_dma_pinned().
- * @_folio_nr_pages: Do not use directly, call folio_nr_pages().
+ * @_nr_pages: Do not use directly, call folio_nr_pages().
  * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
  * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
  * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
@@ -376,14 +381,23 @@ struct folio {
                struct {
                        unsigned long _flags_1;
                        unsigned long _head_1;
+                       union {
+                               struct {
        /* public: */
-                       atomic_t _large_mapcount;
-                       atomic_t _entire_mapcount;
-                       atomic_t _nr_pages_mapped;
-                       atomic_t _pincount;
-#ifdef CONFIG_64BIT
-                       unsigned int _folio_nr_pages;
-#endif
+                                       atomic_t _large_mapcount;
+                                       atomic_t _entire_mapcount;
+                                       atomic_t _nr_pages_mapped;
+                                       atomic_t _pincount;
+       /* private: the union with struct page is transitional */
+                               };
+                               unsigned long _usable_1[4];
+                       };
+                       atomic_t _mapcount_1;
+                       atomic_t _refcount_1;
+       /* public: */
+#ifdef NR_PAGES_IN_LARGE_FOLIO
+                       unsigned int _nr_pages;
+#endif /* NR_PAGES_IN_LARGE_FOLIO */
        /* private: the union with struct page is transitional */
                };
                struct page __page_1;
@@ -435,6 +449,8 @@ FOLIO_MATCH(_last_cpupid, _last_cpupid);
                        offsetof(struct page, pg) + sizeof(struct page))
 FOLIO_MATCH(flags, _flags_1);
 FOLIO_MATCH(compound_head, _head_1);
+FOLIO_MATCH(_mapcount, _mapcount_1);
+FOLIO_MATCH(_refcount, _refcount_1);
 #undef FOLIO_MATCH
 #define FOLIO_MATCH(pg, fl)                                            \
        static_assert(offsetof(struct folio, fl) ==                     \
index 9a15fd3453ff6b419c0eb2273bbfd42d7be5a94d..2364990974ba4f16cf8b98bb9ba1b905b9fb7d75 100644 (file)
@@ -3399,6 +3399,14 @@ static void __split_huge_page(struct page *page, struct list_head *list,
        int order = folio_order(folio);
        unsigned int nr = 1 << order;
 
+       /*
+        * Reset any memcg data overlay in the tail pages. folio_nr_pages()
+        * is unreliable after this point.
+        */
+#ifdef NR_PAGES_IN_LARGE_FOLIO
+       folio->_nr_pages = 0;
+#endif
+
        /* complete memcg works before add pages to LRU */
        split_page_memcg(head, order, new_order);
 
index 1ac433d2092bb779bbb9d8ca3ffcc1847a5b7b39..1cd977413859dca051ff44bef362e7216b245851 100644 (file)
@@ -725,8 +725,8 @@ static inline void folio_set_order(struct folio *folio, unsigned int order)
                return;
 
        folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order;
-#ifdef CONFIG_64BIT
-       folio->_folio_nr_pages = 1U << order;
+#ifdef NR_PAGES_IN_LARGE_FOLIO
+       folio->_nr_pages = 1U << order;
 #endif
 }
 
index 57f959af79c56e9ce119c24f958daed60a14f630..e1135dff9a863cf3b4f371ea8a3e2c86e062b5d6 100644 (file)
@@ -1174,8 +1174,12 @@ __always_inline bool free_pages_prepare(struct page *page,
        if (unlikely(order)) {
                int i;
 
-               if (compound)
+               if (compound) {
                        page[1].flags &= ~PAGE_FLAGS_SECOND;
+#ifdef NR_PAGES_IN_LARGE_FOLIO
+                       folio->_nr_pages = 0;
+#endif
+               }
                for (i = 1; i < (1 << order); i++) {
                        if (compound)
                                bad += free_tail_page_prepare(page, page + i);
index 2d6360eaccbb6ee34bd3654823fa1e687a407a05..a409e2561a8fd91c6c5928cf632ced56a1b3dfa7 100644 (file)
@@ -507,7 +507,7 @@ static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret,
 
        rcu_read_lock();
        memcg_data = READ_ONCE(page->memcg_data);
-       if (!memcg_data)
+       if (!memcg_data || PageTail(page))
                goto out_unlock;
 
        if (memcg_data & MEMCG_DATA_OBJEXTS)