Message ID | 20241117055243.work.907-kees@kernel.org (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | mm: Handle compound pages better in __dump_page() | expand |
On Sat, Nov 16, 2024 at 09:52:44PM -0800, Kees Cook wrote: > 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(). > (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]. > 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 ...
On Mon, Nov 18, 2024 at 04:10:52AM +0000, Matthew Wilcox wrote: > 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; | ^~~~~~~ > > 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.
On Sun, Nov 17, 2024 at 08:46:45PM -0800, Kees Cook wrote: > On Mon, Nov 18, 2024 at 04:10:52AM +0000, Matthew Wilcox wrote: > > 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.
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)
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/ZkN0aSE9zAB5aXvM@casper.infradead.org [1] Signed-off-by: Kees Cook <kees@kernel.org> --- Cc: Matthew Wilcox <willy@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: linux-mm@kvack.org --- mm/debug.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)