[RFC PATCH 2/3] mm: Implement for_each_valid_pfn() for CONFIG_FLATMEM

From: David Woodhouse
Date: Wed Apr 02 2025 - 16:19:04 EST


From: David Woodhouse <dwmw@xxxxxxxxxxxx>

In the FLATMEM case, the default pfn_valid() just checks that the PFN is
within the range [ ARCH_PFN_OFFSET .. ARCH_PFN_OFFSET + max_mapnr ).

The for_each_valid_pfn() function can therefore be a simple for() loop
using those as min/max respectively.

Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
include/asm-generic/memory_model.h | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memory_model.h
index a3b5029aebbd..4fe7dd3bc09c 100644
--- a/include/asm-generic/memory_model.h
+++ b/include/asm-generic/memory_model.h
@@ -30,7 +30,31 @@ static inline int pfn_valid(unsigned long pfn)
return pfn >= pfn_offset && (pfn - pfn_offset) < max_mapnr;
}
#define pfn_valid pfn_valid
-#endif
+
+static inline bool first_valid_pfn(unsigned long *pfn)
+{
+ /* avoid <linux/mm.h> include hell */
+ extern unsigned long max_mapnr;
+ unsigned long pfn_offset = ARCH_PFN_OFFSET;
+
+ if (*pfn < pfn_offset) {
+ *pfn = pfn_offset;
+ return true;
+ }
+
+ if ((*pfn - pfn_offset) < max_mapnr)
+ return true;
+
+ return false;
+}
+
+#ifndef for_each_valid_pfn
+#define for_each_valid_pfn(pfn, start_pfn, end_pfn) \
+ for (pfn = max_t(unsigned long start_pfn, ARCH_PFN_OFFSET); \
+ pfn < min_t(unsigned long, end_pfn, ARCH_PFN_OFFSET + max_mapnr); \
+ pfn++)
+#endif /* for_each_valid_pfn */
+#endif /* valid_pfn */

#elif defined(CONFIG_SPARSEMEM_VMEMMAP)

--
2.49.0