DAMOS action handling functions for the physical address space, such as damon_pa_pageout(), walks folios of the given region. The walk uses pfn of a given physical address to find the folio. PHYS_PFN() is used for the address to pfn conversion. In some systems, pfns and physical addresses could use different types. On 32-bit systems having Physical Address Extensions, a pfn is 32 bits (unsigned long) while a physical address is 64 bits (phys_addr_t). If users set a region of addresses that are extremely high, the address to pfn conversion could wrap around the ULONG_MAX. The wrapped pfn could again be used for next address calculation. Because the loop end address is calculated in phys_addr_t without such wrap, the folio walk loop could infinitely continue. The issue will not happen in real use cases. Real sane users ain't have such a setup. But the consequence is bad: infinite loops. Fix the issue by limiting the physical address to the highest address that is available for the given system. Calculate the upper limit using the max_pfn. Apply the limit to damon_pa_phys_addr(), which is the single function for all core address to physical address conversion. With the fix, a slight behavioral difference under certain rare setup is introduced. Let's suppose the user sets monitoring target address ranges beyond max_pfn. Then, DAMON will effectively map every beyond-max_pfn memory to the last (max_pfn - 1) page. The last page could be monitor-sampled multiple times in a single sampling interval. DAMOS actions could also be applied multiple times to the page in a single DAMOS apply interval. The impact is that DAMON could be suboptimum on the specific and rare setup. But it is much better than the infinite loop. Sane users ain't make such a setup after all. One possible workaround is using an invalid but safe address as the limit, e.g., PHYS_PFN(max_pfn). But it makes damon_pa_phys_addr() return an invalid address sometimes. It could require callers to take care of it. For code simplicity, allowing the suboptimum behavior under certain unrealistic setup is a reasonable tradeoff. Signed-off-by: SJ Park --- damon_pa_phys_addr() was introduced by commit d8096848e73e ("mm/damon/paddr: support addr_unit for access monitoring"). But the infinite loop issue has started from the adoption of it, starting from that for DAMOS_PAGEOUT. Hence setting the DAMOS_PAGEOUT adoption as the Fixes:. Closes: https://sashiko.dev/#/bug/linux-05c97aa2-c84c-453e-936d-ddab87da4f5b Fixes: 85246435b249 ("mm/damon/paddr: support addr_unit for DAMOS_PAGEOUT") Cc: # 6.18.x Signed-off-by: SJ Park mm/damon/paddr: infinite folio walk fixup mm/damon/paddr.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c index 2cfdc356b415..41bbd47fb415 100644 --- a/mm/damon/paddr.c +++ b/mm/damon/paddr.c @@ -5,6 +5,7 @@ #define pr_fmt(fmt) "damon-pa: " fmt +#include #include #include #include @@ -19,7 +20,7 @@ static phys_addr_t damon_pa_phys_addr( unsigned long addr, unsigned long addr_unit) { - return (phys_addr_t)addr * addr_unit; + return min((phys_addr_t)addr * addr_unit, PFN_PHYS(max_pfn) - 1); } static unsigned long damon_pa_core_addr( base-commit: 1bf8555f485409fa2e71c110155a355c07664f94 -- 2.47.3