linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: Matthew Wilcox <willy@infradead.org>
Cc: Kees Cook <kees@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: [PATCH] mm: Handle compound pages better in __dump_page()
Date: Sat, 16 Nov 2024 21:52:44 -0800	[thread overview]
Message-ID: <20241117055243.work.907-kees@kernel.org> (raw)

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(-)

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	other threads:[~2024-11-17  5:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-17  5:52 Kees Cook [this message]
2024-11-18  4:10 ` Matthew Wilcox
2024-11-18  4:46   ` Kees Cook
2024-11-18  5:17     ` Matthew Wilcox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241117055243.work.907-kees@kernel.org \
    --to=kees@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox