Skip to contentSkip to conversation

mm/page_alloc: clear page tag refs when onlining hotplugged memory

PATCH New Zenghui Yu 2 participants 2 messages Oct 20 – Oct 21

Subsystems (2)
lore
Share & tools
lore

Zenghui Yu

author
#0 lore raw

PATCH 1/1 mm/page_alloc: clear page tag refs when onlining hotplugged memory

From: "Zenghui Yu (Huawei)" <[email protected]>

Onlining hotplugged memory (e.g., a dev_dax device bound to kmem) with CONFIG_MEM_ALLOC_PROFILING_DEBUG triggers:

alloc_tag was not set

  WARNING: ./include/linux/alloc_tag.h:166 at __pgalloc_tag_sub+0x1fc/0x254
  Call trace:
   __pgalloc_tag_sub+0x1fc/0x254 (P)
   __free_pages_ok+0x14c/0x6ac
   __free_pages_core+0xa8/0x188
   generic_online_page+0x14/0x20
   online_pages+0x220/0x3b0
   memory_subsys_online+0xfc/0x198
   device_online+0x58/0xe0
   state_store+0xf0/0xf8

Onlined pages enter the buddy allocator without ever being allocated, so their allocation tag references are NULL. The free path expects a valid tag or a CODETAG_EMPTY mark and warns otherwise. Other paths feeding never-allocated pages into the buddy (memblock_free_pages(), init_cma_reserved_pageblock(), free_reserved_pages()) mark them with clear_page_tag_ref() first; the hotplug onlining path was missed.

Mark the pages as not tagged in the MEMINIT_HOTPLUG branch of __free_pages_core(), where PageOffline and the refcount are already reset.

---
 mm/page_alloc.c | 1 +
 1 file changed, 1 insertion(+)
mm/page_alloc.c +1 -0
index e71e514ea18d..9561f2cd873e 100644
@@ -1604,6 +1604,7 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,
 			VM_WARN_ON_ONCE(PageReserved(p));
 			__ClearPageOffline(p);
 			set_page_count(p, 0);
+			clear_page_tag_ref(p);
 		}
 
 		adjust_managed_page_count(page, nr_pages);
Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mm/page_alloc: clear_page_tag_ref() called on tail pages in __free_pages_core() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm/page_alloc: clear_page_tag_ref() called on tail pages in __free_pages_core() --

commit e2abce3265909945612b858336381b3dbe9dca76
Author: Zenghui Yu (Huawei) <[email protected]>

mm/page_alloc: clear page tag refs when onlining hotplugged memory

This commit calls clear_page_tag_ref() on hotplugged pages in __free_pages_core() so that freeing never-allocated pages into the buddy allocator does not trigger a warning under CONFIG_MEM_ALLOC_PROFILING_DEBUG.

mm/page_alloc.c void __meminit __free_pages_core(struct page *page, unsigned int order, · line 1609

Quoted 12 lines
mm/page_alloc.c +1 -0
index 2daf358269ae7..9cef0e9294abb 100644
@@ -1603,6 +1603,7 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,
 			VM_WARN_ON_ONCE(PageReserved(p));
 			__ClearPageOffline(p);
 			set_page_count(p, 0);
+			clear_page_tag_ref(p);
 		}
 
 		adjust_managed_page_count(page, nr_pages);

[Severity: Medium]
Should clear_page_tag_ref() be called once on page outside the per-page loop (and for both MEMINIT_HOTPLUG and MEMINIT_EARLY contexts) rather than on every p inside the MEMINIT_HOTPLUG loop?

Under CONFIG_MEM_ALLOC_PROFILING_DEBUG, clear_page_tag_ref(p) sets the page's tag reference to CODETAG_EMPTY (or CODETAG_ID_EMPTY when compressed profiling is enabled) rather than NULL. When __free_pages_core() calls __free_pages_ok() -> __free_pages_prepare(), only the first page's marker is consumed and reset back to NULL:

mm/page_alloc.c:__free_pages_prepare()

    pgalloc_tag_sub(page, 1 << order);
        alloc_tag_sub(ref, bytes)
            if (is_codetag_empty(ref)) {
                ref->ct = NULL;
                return;
            }

Calling clear_page_tag_ref(p) on all nr_pages leaves CODETAG_EMPTY set on the remaining (1 << order) - 1 tail pages in the buddy allocator. If one of those pages is later allocated without a tag (for example via an _noprof allocation where current->alloc_tag is NULL) and subsequently freed, the leftover CODETAG_EMPTY marker prevents alloc_tag_sub_check() from warning. Other callers such as init_cma_reserved_pageblock() and memblock_free_pages() only call clear_page_tag_ref(page) on the head page:

mm/mm_init.c:init_cma_reserved_pageblock() {
    do {
        __ClearPageReserved(p);
        set_page_count(p, 0);
    } while (++p, --i);
    ...
    /* pages were reserved and not allocated */
    clear_page_tag_ref(page);
    __free_pages(page, pageblock_order);
}

In addition, placing clear_page_tag_ref() only in the MEMINIT_HOTPLUG branch misses MEMINIT_EARLY callers of __free_pages_core() that run after allocation tag references are initialized and do not call clear_page_tag_ref() themselves, such as deferred_free_pages() in mm/mm_init.c (when compressed profiling is enabled) and do_collection() in mm/kmsan/init.c (where KMSAN intercepts pages in memblock_free_pages() before clear_page_tag_ref(page) and releases them later via __free_pages_core(..., MEMINIT_EARLY)).

เครดิต

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1