Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/rmap: improve mlock tracking for large folios

The kernel currently does not mlock large folios when adding them to rmap,
stating that it is difficult to confirm that the folio is fully mapped and
safe to mlock it.

This leads to a significant undercount of Mlocked in /proc/meminfo,
causing problems in production where the stat was used to estimate system
utilization and determine if load shedding is required.

However, nowadays the caller passes a number of pages of the folio that
are getting mapped, making it easy to check if the entire folio is mapped
to the VMA.

mlock the folio on rmap if it is fully mapped to the VMA.

Mlocked in /proc/meminfo can still undercount, but the value is closer the
truth and is useful for userspace.

Link: https://lkml.kernel.org/r/20250923110711.690639-7-kirill@shutemov.name
Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
Acked-by: David Hildenbrand <david@redhat.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kiryl Shutsemau and committed by
Andrew Morton
ab521b41 357b9276

+12 -7
+12 -7
mm/rmap.c
··· 1463 1463 } 1464 1464 1465 1465 /* 1466 - * For large folio, only mlock it if it's fully mapped to VMA. It's 1467 - * not easy to check whether the large folio is fully mapped to VMA 1468 - * here. Only mlock normal 4K folio and leave page reclaim to handle 1469 - * large folio. 1466 + * Only mlock it if the folio is fully mapped to the VMA. 1467 + * 1468 + * Partially mapped folios can be split on reclaim and part outside 1469 + * of mlocked VMA can be evicted or freed. 1470 1470 */ 1471 - if (!folio_test_large(folio)) 1471 + if (folio_nr_pages(folio) == nr_pages) 1472 1472 mlock_vma_folio(folio, vma); 1473 1473 } 1474 1474 ··· 1601 1601 1602 1602 __folio_add_rmap(folio, page, nr_pages, vma, level); 1603 1603 1604 - /* See comments in folio_add_anon_rmap_*() */ 1605 - if (!folio_test_large(folio)) 1604 + /* 1605 + * Only mlock it if the folio is fully mapped to the VMA. 1606 + * 1607 + * Partially mapped folios can be split on reclaim and part outside 1608 + * of mlocked VMA can be evicted or freed. 1609 + */ 1610 + if (folio_nr_pages(folio) == nr_pages) 1606 1611 mlock_vma_folio(folio, vma); 1607 1612 } 1608 1613