linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] mm/memory: Fix boundary check for next PFN in folio_pte_batch()
@ 2024-02-27  7:04 Lance Yang
  2024-02-27  7:30 ` David Hildenbrand
  0 siblings, 1 reply; 6+ messages in thread
From: Lance Yang @ 2024-02-27  7:04 UTC (permalink / raw)
  To: akpm; +Cc: ryan.roberts, 21cnbao, david, linux-mm, linux-kernel, Lance Yang

Previously, in folio_pte_batch(), only the upper boundary of the
folio was checked using '>=' for comparison. This led to
incorrect behavior when the next PFN exceeded the lower boundary
of the folio, especially in corner cases where the next PFN might
fall into a different folio.

Signed-off-by: Lance Yang <ioworker0@gmail.com>
---
 mm/memory.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 642b4f2be523..e5291d1e8c37 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -986,12 +986,15 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr,
 		pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags,
 		bool *any_writable)
 {
-	unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio);
+	unsigned long folio_start_pfn, folio_end_pfn;
 	const pte_t *end_ptep = start_ptep + max_nr;
 	pte_t expected_pte, *ptep;
 	bool writable;
 	int nr;
 
+	folio_start_pfn = folio_pfn(folio);
+	folio_end_pfn = folio_start_pfn + folio_nr_pages(folio);
+
 	if (any_writable)
 		*any_writable = false;
 
@@ -1015,7 +1018,7 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr,
 		 * corner cases the next PFN might fall into a different
 		 * folio.
 		 */
-		if (pte_pfn(pte) >= folio_end_pfn)
+		if (pte_pfn(pte) >= folio_end_pfn || pte_pfn(pte) < folio_start_pfn)
 			break;
 
 		if (any_writable)
-- 
2.33.1



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-02-27  8:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-27  7:04 [PATCH 1/1] mm/memory: Fix boundary check for next PFN in folio_pte_batch() Lance Yang
2024-02-27  7:30 ` David Hildenbrand
2024-02-27  8:23   ` Lance Yang
2024-02-27  8:33     ` David Hildenbrand
2024-02-27  8:45       ` Lance Yang
2024-02-27  8:46         ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox