[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <d2d7e1bf-baf4-4b53-bfa6-83887bec8a60@redhat.com>
Date: Fri, 11 Jul 2025 08:59:38 -0400
From: Luiz Capitulino <luizcap@...hat.com>
To: David Hildenbrand <david@...hat.com>, willy@...radead.org,
akpm@...ux-foundation.org
Cc: linux-kernel@...r.kernel.org, linux-mm@...ck.org, shivankg@....com,
sj@...nel.org
Subject: Re: [PATCH v2 2/4] mm/util: introduce snapshot_page()
On 2025-07-11 07:56, David Hildenbrand wrote:
> On 07.07.25 20:50, Luiz Capitulino wrote:
>> This commit refactors __dump_page() into snapshot_page().
>>
>> snapshot_page() tries to take a faithful snapshot of a page and its
>> folio representation. The snapshot is returned in the struct
>> page_snapshot parameter along with additional flags that are best
>> retrieved at snapshot creation time to reduce race windows.
>>
>> This function is intended to be used by callers that need a stable
>> representation of a struct page and struct folio so that pointers
>> or page information doesn't change while working on a page.
>>
>> The idea and original implementation of snapshot_page() comes from
>> Matthew Wilcox with suggestions for improvements from David Hildenbrand.
>> All bugs and misconceptions are mine.
>>
>> Signed-off-by: Luiz Capitulino <luizcap@...hat.com>
>> ---
>> include/linux/mm.h | 19 ++++++++++++
>> mm/debug.c | 42 +++----------------------
>> mm/util.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++
>> 3 files changed, 100 insertions(+), 38 deletions(-)
>>
>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>> index 0ef2ba0c667a..090968c6eebb 100644
>> --- a/include/linux/mm.h
>> +++ b/include/linux/mm.h
>> @@ -4184,4 +4184,23 @@ static inline bool page_pool_page_is_pp(struct page *page)
>> }
>> #endif
>> +#define PAGE_SNAPSHOT_FAITHFUL (1 << 0)
>> +#define PAGE_SNAPSHOT_PG_FREE (1 << 1)
>
> Can we call this "BUDDY" instead of FREE
>
> There are other types of free pages -- in particular free hugetlb folios -- which won't be covered by this check. We really only care about "buddy" pages, which correspond to "free" pages.
>
> That's what we check and alter expose :)
OK, I'll change.
>> +#define PAGE_SNAPSHOT_PG_IDLE (1 << 2)
> > +> +struct page_snapshot {
>> + struct folio folio_snapshot;
>> + struct page page_snapshot;
>> + unsigned long pfn;
>> + unsigned long idx;
>> + unsigned long flags;
>> +};
>> +
>> +static inline bool snapshot_page_is_faithful(const struct page_snapshot *ps)
>> +{
>> + return ps->flags & PAGE_SNAPSHOT_FAITHFUL;
>> +}
>> +
>> +void snapshot_page(struct page_snapshot *ps, const struct page *page);
>> +
>> #endif /* _LINUX_MM_H */
>> diff --git a/mm/debug.c b/mm/debug.c
>> index 907382257062..7349330ea506 100644
>> --- a/mm/debug.c
>> +++ b/mm/debug.c
>> @@ -129,47 +129,13 @@ static void __dump_folio(struct folio *folio, struct page *page,
>> static void __dump_page(const struct page *page)
>> {
>> - struct folio *foliop, folio;
>> - struct page precise;
>> - unsigned long head;
>> - unsigned long pfn = page_to_pfn(page);
>> - unsigned long idx, nr_pages = 1;
>> - int loops = 5;
>> -
>> -again:
>> - memcpy(&precise, page, sizeof(*page));
>> - head = precise.compound_head;
>> - if ((head & 1) == 0) {
>> - foliop = (struct folio *)&precise;
>> - idx = 0;
>> - if (!folio_test_large(foliop))
>> - goto dump;
>> - foliop = (struct folio *)page;
>> - } else {
>> - foliop = (struct folio *)(head - 1);
>> - idx = folio_page_idx(foliop, page);
>> - }
>> + struct page_snapshot ps;
>> - if (idx < MAX_FOLIO_NR_PAGES) {
>> - memcpy(&folio, foliop, 2 * sizeof(struct page));
>> - nr_pages = folio_nr_pages(&folio);
>> - if (nr_pages > 1)
>> - memcpy(&folio.__page_2, &foliop->__page_2,
>> - sizeof(struct page));
>> - foliop = &folio;
>> - }
>> -
>> - if (idx > nr_pages) {
>> - if (loops-- > 0)
>> - goto again;
>> + snapshot_page(&ps, page);
>> + if (!snapshot_page_is_faithful(&ps))
>> pr_warn("page does not match folio\n");
>> - precise.compound_head &= ~1UL;
>> - foliop = (struct folio *)&precise;
>> - idx = 0;
>> - }
>> -dump:
>> - __dump_folio(foliop, &precise, pfn, idx);
>> + __dump_folio(&ps.folio_snapshot, &ps.page_snapshot, ps.pfn, ps.idx);
>> }
>> void dump_page(const struct page *page, const char *reason)
>> diff --git a/mm/util.c b/mm/util.c
>> index 0b270c43d7d1..c38d213be83f 100644
>> --- a/mm/util.c
>> +++ b/mm/util.c
>> @@ -25,6 +25,7 @@
>> #include <linux/sizes.h>
>> #include <linux/compat.h>
>> #include <linux/fsnotify.h>
>> +#include <linux/page_idle.h>
>> #include <linux/uaccess.h>
>> @@ -1171,3 +1172,79 @@ int compat_vma_mmap_prepare(struct file *file, struct vm_area_struct *vma)
>> return 0;
>> }
>> EXPORT_SYMBOL(compat_vma_mmap_prepare);
>> +
>> +static void set_flags(struct page_snapshot *ps, const struct folio *folio,
>> + const struct page *page)
>> +{
>> + /*
>> + * Caveats on high order pages: PG_buddy and PG_slab will only be set
>> + * on the head page.
>> + */
>
> Talking about slab here is a bit misleading, IIRC they are proper compound pages today. For PG_buddy (which was renamed to PGTY_buddy), we
> are dealing with non-compound higher allocations.
>
> So talking about "head" is also a bit misleading.
>
> /*
> * Only the first page of a high-order buddy page has PageBuddy() set.
> * So we have to check manually whether this page is part of a high-
> * order buddy page.
> */
I'll use this comment.
>> + if (PageBuddy(page))
>> + ps->flags |= PAGE_SNAPSHOT_PG_FREE;
>> + else if (page_count(page) == 0 && is_free_buddy_page(page))
>> + ps->flags |= PAGE_SNAPSHOT_PG_FREE;
>> +
>> + if (folio_test_idle(folio))
>> + ps->flags |= PAGE_SNAPSHOT_PG_IDLE;
>> +}
>> +
>> +/*
>
> I would suggest writing proper kernel doc (you mostly have that already)
>
> Something like
>
> /**
> * snapshot_page() - Create a snapshot of a "struct page"
> * @ps: struct page_snapshot to store the page snapshot
> * @page: the page we want to snapshot
> *
> * Create a snapshot of a page and store its struct page and struct
> * folio ...
I'll fix this too.
>
>> + * Create a snapshot of a page and store its struct page and struct folio
>> + * representations in a struct page_snapshot.
>> + *
>> + * @ps: struct page_snapshot to store the page snapshot
>> + * @page: the page we want to snapshot
>> + *
>> + * Note that creating a faithful snapshot of a page may fail if the page
>> + * compound keeps changing (eg. due to folio split). In this case we set
>> + * ps->faithful to false and the snapshot will assume that @page refers
>> + * to a single page.
>> + */
>
Powered by blists - more mailing lists