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].

Link: https://lore.kernel.org/r/[email protected] [1]
Signed-off-by: Kees Cook <[email protected]>
---
Cc: Matthew Wilcox <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: [email protected]
---
 mm/debug.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/mm/debug.c b/mm/debug.c
index aa57d3ffd4ed..7ea396e8c143 100644
--- a/mm/debug.c
+++ b/mm/debug.c
@@ -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)
-- 
2.34.1


Reply via email to