Skip to contentSkip to conversation

mm: Handle compound pages better in __dump_page()

PATCH In review Kees Cook 2 participants 4 messages Nov 17, 2024 – Nov 18, 2024

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 mm: Handle compound pages better in __dump_page()

GCC 15's -Warray-bounds reports:

In function 'page_fixed_fake_head',

    inlined from '_compound_head' at ../include/linux/page-flags.h:251:24,
    inlined from '__dump_page' at ../mm/debug.c:123:11:

../include/asm-generic/rwonce.h:44:26: warning: array subscript 9 is outside array bounds of 'struct page[1]' [-Warray-bounds=]

(Not noted in this warning is that the code passes through page_folio() _Generic macro.)

It may not be that "precise" is always 1 page, so accessing "page[1]" in either page_folio() or folio_test_large() may cause problems. Instead, explicitly make precise 2 pages. Just open-coding page_folio() isn't sufficient to avoid the warning[1].

---
Cc: Matthew Wilcox <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: [email protected]

---
 mm/debug.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
mm/debug.c +7 -7
index aa57d3ffd4ed..7ea396e8c143 100644
@@ -123,15 +123,15 @@ 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;
+	struct page precise[2] = { };
 	unsigned long pfn = page_to_pfn(page);
 	unsigned long idx, nr_pages = 1;
 	int loops = 5;
 
 again:
-	memcpy(&precise, page, sizeof(*page));
-	foliop = page_folio(&precise);
-	if (foliop == (struct folio *)&precise) {
+	memcpy(&precise[0], page, sizeof(*page));
+	foliop = page_folio(&precise[0]);
+	if (foliop == (struct folio *)&precise[0]) {
 		idx = 0;
 		if (!folio_test_large(foliop))
 			goto dump;
@@ -150,13 +150,13 @@ static void __dump_page(const struct page *page)
 		if (loops-- > 0)
 			goto again;
 		pr_warn("page does not match folio\n");
-		precise.compound_head &= ~1UL;
-		foliop = (struct folio *)&precise;
+		precise[0].compound_head &= ~1UL;
+		foliop = (struct folio *)&precise[0];
 		idx = 0;
 	}
 
 dump:
-	__dump_folio(foliop, &precise, pfn, idx);
+	__dump_folio(foliop, &precise[0], pfn, idx);
 }
 
 void dump_page(const struct page *page, const char *reason)
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · Kees Cook

GCC 15's -Warray-bounds reports:

In function 'page_fixed_fake_head',

    inlined from '_compound_head' at ../include/linux/page-flags.h:251:24,
    inlined from '__dump_page' at ../mm/debug.c:123:11:

../include/asm-generic/rwonce.h:44:26: warning: array subscript 9 is outside array bounds of 'struct page[1]' [-Warray-bounds=]

Thanks for bringing this back up. I have a somewhat orphaned patch in my tree that has a terrible commit message which was no help.

That said, this patch is definitely wrong because it's unsafe to call page_fixed_fake_head().

Quoted 5 lines

(Not noted in this warning is that the code passes through page_folio() _Generic macro.)

It may not be that "precise" is always 1 page, so accessing "page[1]" in either page_folio() or folio_test_large() may cause problems.

folio_test_large() does not touch page[1]. Look:

static inline bool folio_test_large(const struct folio *folio)

{
        return folio_test_head(folio);

static __always_inline bool folio_test_head(const struct folio *folio)

{
        return test_bit(PG_head, const_folio_flags(folio, FOLIO_PF_ANY));

#define FOLIO_PF_ANY 0

static const unsigned long *const_folio_flags(const struct folio *folio,
unsigned n)

{
        const struct page *page = &folio->page;

        VM_BUG_ON_PGFLAGS(PageTail(page), page);
        VM_BUG_ON_PGFLAGS(n > 0 && !test_bit(PG_head, &page->flags), page);
        return &page[n].flags;

so we only look at page[0].

Quoted 2 lines

Instead, explicitly make precise 2 pages. Just open-coding page_folio() isn't sufficient to avoid the warning[1].

Why not? What goes wrong? I'm trying to get gcc-15 installed here now
...

author
#2 lore raw
Quoted 1 line · Matthew Wilcox

folio_test_large() does not touch page[1]. Look:

It does, though. :( It's via the PageTail(), which calls page_is_fake_head():

In function 'page_fixed_fake_head',

    inlined from 'page_is_fake_head' at ../include/linux/page-flags.h:237:9,
    inlined from 'PageTail' at ../include/linux/page-flags.h:281:47,
    inlined from 'const_folio_flags' at ../include/linux/page-flags.h:309:2,
    inlined from 'folio_test_head' at ../include/linux/page-flags.h:824:9,
    inlined from 'folio_test_large' at ../include/linux/page-flags.h:845:9,
    inlined from '__dump_page' at ../mm/debug.c:138:8:   
../include/asm-generic/rwonce.h:44:26: error: array subscript 9 is outside array bounds of 'struct p
age[1]' [-Werror=array-bounds=]               
   44 | #define __READ_ONCE(x)  (*(const volatile __unqual_scalar_typeof(x) *)&(x))
      |                         ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../include/asm-generic/rwonce.h:50:9: note: in expansion of macro '__READ_ONCE'
   50 |         __READ_ONCE(x);                                                 \
      |         ^~~~~~~~~~~

../include/linux/page-flags.h:221:38: note: in expansion of macro 'READ_ONCE'
221 | unsigned long head = READ_ONCE(page[1].compound_head);
| ^~~~~~~~~
../mm/debug.c: In function '__dump_page':
../mm/debug.c:126:21: note: at offset 72 into object 'precise' of size 64

  126 |         struct page precise;
      |                     ^~~~~~~
Quoted 4 lines
Quoted 2 lines

Instead, explicitly make precise 2 pages. Just open-coding page_folio() isn't sufficient to avoid the warning[1].

Why not? What goes wrong? I'm trying to get gcc-15 installed here now

With your original patch applied, I get the above warning.

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 4 lines · Kees Cook
Quoted 1 line · Matthew Wilcox

folio_test_large() does not touch page[1]. Look:

It does, though. :( It's via the PageTail(), which calls page_is_fake_head():

Oh. It shouldn't; that's unnecessary.

+++ b/include/linux/page-flags.h
@@ -306,7 +306,7 @@ static const unsigned long *const_folio_flags(const struct folio *folio,

 {
        const struct page *page = &folio->page;

- VM_BUG_ON_PGFLAGS(PageTail(page), page);

+       VM_BUG_ON_PGFLAGS(page->compound_head & 1, page);
        VM_BUG_ON_PGFLAGS(n > 0 && !test_bit(PG_head, &page->flags), page);
        return &page[n].flags;
 }
@@ -315,7 +315,7 @@ static unsigned long *folio_flags(struct folio *folio, unsigned n)
 {
        struct page *page = &folio->page;

- VM_BUG_ON_PGFLAGS(PageTail(page), page);

+       VM_BUG_ON_PGFLAGS(page->compound_head & 1, page);
        VM_BUG_ON_PGFLAGS(n > 0 && !test_bit(PG_head, &page->flags), page);
        return &page[n].flags;

}

should fix that.