damon_pa_stat() advances its cursor by PAGE_SIZE or the current folio size. If the physical end of a region is near PHYS_ADDR_MAX, either addition can wrap the cursor to zero and make the stat action walk almost the entire physical address space. Saturate each advance at the converted region end. Calculate the end in the loop for now so this stable fix does not depend on the separate change that hoists the conversion out of the loop. Fixes: 01e7ee33a0ca ("mm/damon/paddr: support addr_unit for DAMOS_STAT") Cc: stable@vger.kernel.org Suggested-by: SJ Park Signed-off-by: Kyuwon Cho --- mm/damon/paddr.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c index 9b57f5e9cf63..23a8315532b7 100644 --- a/mm/damon/paddr.c +++ b/mm/damon/paddr.c @@ -480,7 +480,8 @@ static unsigned long damon_pa_stat(struct damon_region *r, unsigned long addr_unit, struct damos *s, unsigned long *sz_filter_passed) { - phys_addr_t addr; + phys_addr_t addr, end; + unsigned long size; struct folio *folio = NULL; if (!damos_ops_has_filter(s)) @@ -488,15 +489,19 @@ static unsigned long damon_pa_stat(struct damon_region *r, addr = damon_pa_phys_addr(r->ar.start, addr_unit); while (addr < damon_pa_phys_addr(r->ar.end, addr_unit)) { + end = damon_pa_phys_addr(r->ar.end, addr_unit); folio = damon_get_folio(PHYS_PFN(addr)); if (damon_pa_invalid_damos_folio(folio, s)) { - addr += PAGE_SIZE; + size = PAGE_SIZE; + addr = size < end - addr ? addr + size : end; continue; } if (!damos_pa_filter_out(s, folio)) *sz_filter_passed += folio_size(folio) / addr_unit; - addr = PFN_PHYS(folio_pfn(folio)) + folio_size(folio); + addr = PFN_PHYS(folio_pfn(folio)); + size = folio_size(folio); + addr = size < end - addr ? addr + size : end; folio_put(folio); } s->last_applied = folio; -- 2.43.0