unevictable-lru.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. ==============================
  2. Unevictable LRU Infrastructure
  3. ==============================
  4. .. contents:: :local:
  5. Introduction
  6. ============
  7. This document describes the Linux memory manager's "Unevictable LRU"
  8. infrastructure and the use of this to manage several types of "unevictable"
  9. folios.
  10. The document attempts to provide the overall rationale behind this mechanism
  11. and the rationale for some of the design decisions that drove the
  12. implementation. The latter design rationale is discussed in the context of an
  13. implementation description. Admittedly, one can obtain the implementation
  14. details - the "what does it do?" - by reading the code. One hopes that the
  15. descriptions below add value by provide the answer to "why does it do that?".
  16. The Unevictable LRU
  17. ===================
  18. The Unevictable LRU facility adds an additional LRU list to track unevictable
  19. folios and to hide these folios from vmscan. This mechanism is based on a patch
  20. by Larry Woodman of Red Hat to address several scalability problems with folio
  21. reclaim in Linux. The problems have been observed at customer sites on large
  22. memory x86_64 systems.
  23. To illustrate this with an example, a non-NUMA x86_64 platform with 128GB of
  24. main memory will have over 32 million 4k pages in a single node. When a large
  25. fraction of these pages are not evictable for any reason [see below], vmscan
  26. will spend a lot of time scanning the LRU lists looking for the small fraction
  27. of pages that are evictable. This can result in a situation where all CPUs are
  28. spending 100% of their time in vmscan for hours or days on end, with the system
  29. completely unresponsive.
  30. The unevictable list addresses the following classes of unevictable pages:
  31. * Those owned by ramfs.
  32. * Those owned by tmpfs with the noswap mount option.
  33. * Those mapped into SHM_LOCK'd shared memory regions.
  34. * Those mapped into VM_LOCKED [mlock()ed] VMAs.
  35. The infrastructure may also be able to handle other conditions that make pages
  36. unevictable, either by definition or by circumstance, in the future.
  37. The Unevictable LRU Folio List
  38. ------------------------------
  39. The Unevictable LRU folio list is a lie. It was never an LRU-ordered
  40. list, but a companion to the LRU-ordered anonymous and file, active and
  41. inactive folio lists; and now it is not even a folio list. But following
  42. familiar convention, here in this document and in the source, we often
  43. imagine it as a fifth LRU folio list.
  44. The Unevictable LRU infrastructure consists of an additional, per-node, LRU list
  45. called the "unevictable" list and an associated folio flag, PG_unevictable, to
  46. indicate that the folio is being managed on the unevictable list.
  47. The PG_unevictable flag is analogous to, and mutually exclusive with, the
  48. PG_active flag in that it indicates on which LRU list a folio resides when
  49. PG_lru is set.
  50. The Unevictable LRU infrastructure maintains unevictable folios as if they were
  51. on an additional LRU list for a few reasons:
  52. (1) We get to "treat unevictable folios just like we treat other folios in the
  53. system - which means we get to use the same code to manipulate them, the
  54. same code to isolate them (for migrate, etc.), the same code to keep track
  55. of the statistics, etc..." [Rik van Riel]
  56. (2) We want to be able to migrate unevictable folios between nodes for memory
  57. defragmentation, workload management and memory hotplug. The Linux kernel
  58. can only migrate folios that it can successfully isolate from the LRU
  59. lists (or "Movable" folios: outside of consideration here). If we were to
  60. maintain folios elsewhere than on an LRU-like list, where they can be
  61. detected by folio_isolate_lru(), we would prevent their migration.
  62. The unevictable list does not differentiate between file-backed and
  63. anonymous, swap-backed folios. This differentiation is only important
  64. while the folios are, in fact, evictable.
  65. The unevictable list benefits from the "arrayification" of the per-node LRU
  66. lists and statistics originally proposed and posted by Christoph Lameter.
  67. Memory Control Group Interaction
  68. --------------------------------
  69. The unevictable LRU facility interacts with the memory control group [aka
  70. memory controller; see Documentation/admin-guide/cgroup-v1/memory.rst] by
  71. extending the lru_list enum.
  72. The memory controller data structure automatically gets a per-node unevictable
  73. list as a result of the "arrayification" of the per-node LRU lists (one per
  74. lru_list enum element). The memory controller tracks the movement of pages to
  75. and from the unevictable list.
  76. When a memory control group comes under memory pressure, the controller will
  77. not attempt to reclaim pages on the unevictable list. This has a couple of
  78. effects:
  79. (1) Because the pages are "hidden" from reclaim on the unevictable list, the
  80. reclaim process can be more efficient, dealing only with pages that have a
  81. chance of being reclaimed.
  82. (2) On the other hand, if too many of the pages charged to the control group
  83. are unevictable, the evictable portion of the working set of the tasks in
  84. the control group may not fit into the available memory. This can cause
  85. the control group to thrash or to OOM-kill tasks.
  86. .. _mark_addr_space_unevict:
  87. Marking Address Spaces Unevictable
  88. ----------------------------------
  89. For facilities such as ramfs none of the pages attached to the address space
  90. may be evicted. To prevent eviction of any such pages, the AS_UNEVICTABLE
  91. address space flag is provided, and this can be manipulated by a filesystem
  92. using a number of wrapper functions:
  93. * ``void mapping_set_unevictable(struct address_space *mapping);``
  94. Mark the address space as being completely unevictable.
  95. * ``void mapping_clear_unevictable(struct address_space *mapping);``
  96. Mark the address space as being evictable.
  97. * ``int mapping_unevictable(struct address_space *mapping);``
  98. Query the address space, and return true if it is completely
  99. unevictable.
  100. These are currently used in three places in the kernel:
  101. (1) By ramfs to mark the address spaces of its inodes when they are created,
  102. and this mark remains for the life of the inode.
  103. (2) By SYSV SHM to mark SHM_LOCK'd address spaces until SHM_UNLOCK is called.
  104. Note that SHM_LOCK is not required to page in the locked pages if they're
  105. swapped out; the application must touch the pages manually if it wants to
  106. ensure they're in memory.
  107. (3) By the i915 driver to mark pinned address space until it's unpinned. The
  108. amount of unevictable memory marked by i915 driver is roughly the bounded
  109. object size in debugfs/dri/0/i915_gem_objects.
  110. Detecting Unevictable Pages
  111. ---------------------------
  112. The function folio_evictable() in mm/internal.h determines whether a folio is
  113. evictable or not using the query function outlined above [see section
  114. :ref:`Marking address spaces unevictable <mark_addr_space_unevict>`]
  115. to check the AS_UNEVICTABLE flag.
  116. For address spaces that are so marked after being populated (as SHM regions
  117. might be), the lock action (e.g. SHM_LOCK) can be lazy, and need not populate
  118. the page tables for the region as does, for example, mlock(), nor need it make
  119. any special effort to push any pages in the SHM_LOCK'd area to the unevictable
  120. list. Instead, vmscan will do this if and when it encounters the folios during
  121. a reclamation scan.
  122. On an unlock action (such as SHM_UNLOCK), the unlocker (e.g. shmctl()) must scan
  123. the pages in the region and "rescue" them from the unevictable list if no other
  124. condition is keeping them unevictable. If an unevictable region is destroyed,
  125. the pages are also "rescued" from the unevictable list in the process of
  126. freeing them.
  127. folio_evictable() also checks for mlocked folios by calling
  128. folio_test_mlocked(), which is set when a folio is faulted into a
  129. VM_LOCKED VMA, or found in a VMA being VM_LOCKED.
  130. Vmscan's Handling of Unevictable Folios
  131. ---------------------------------------
  132. If unevictable folios are culled in the fault path, or moved to the unevictable
  133. list at mlock() or mmap() time, vmscan will not encounter the folios until they
  134. have become evictable again (via munlock() for example) and have been "rescued"
  135. from the unevictable list. However, there may be situations where we decide,
  136. for the sake of expediency, to leave an unevictable folio on one of the regular
  137. active/inactive LRU lists for vmscan to deal with. vmscan checks for such
  138. folios in all of the shrink_{active|inactive|folio}_list() functions and will
  139. "cull" such folios that it encounters: that is, it diverts those folios to the
  140. unevictable list for the memory cgroup and node being scanned.
  141. There may be situations where a folio is mapped into a VM_LOCKED VMA,
  142. but the folio does not have the mlocked flag set. Such folios will make
  143. it all the way to shrink_active_list() or shrink_folio_list() where they
  144. will be detected when vmscan walks the reverse map in folio_referenced()
  145. or try_to_unmap(). The folio is culled to the unevictable list when it
  146. is released by the shrinker.
  147. To "cull" an unevictable folio, vmscan simply puts the folio back on
  148. the LRU list using folio_putback_lru() - the inverse operation to
  149. folio_isolate_lru() - after dropping the folio lock. Because the
  150. condition which makes the folio unevictable may change once the folio
  151. is unlocked, __pagevec_lru_add_fn() will recheck the unevictable state
  152. of a folio before placing it on the unevictable list.
  153. MLOCKED Pages
  154. =============
  155. The unevictable folio list is also useful for mlock(), in addition to ramfs and
  156. SYSV SHM. Note that mlock() is only available in CONFIG_MMU=y situations; in
  157. NOMMU situations, all mappings are effectively mlocked.
  158. History
  159. -------
  160. The "Unevictable mlocked Pages" infrastructure is based on work originally
  161. posted by Nick Piggin in an RFC patch entitled "mm: mlocked pages off LRU".
  162. Nick posted his patch as an alternative to a patch posted by Christoph Lameter
  163. to achieve the same objective: hiding mlocked pages from vmscan.
  164. In Nick's patch, he used one of the struct page LRU list link fields as a count
  165. of VM_LOCKED VMAs that map the page (Rik van Riel had the same idea three years
  166. earlier). But this use of the link field for a count prevented the management
  167. of the pages on an LRU list, and thus mlocked pages were not migratable as
  168. folio_isolate_lru() could not detect them, and the LRU list link field was not
  169. available to the migration subsystem.
  170. Nick resolved this by putting mlocked pages back on the LRU list before
  171. attempting to isolate them, thus abandoning the count of VM_LOCKED VMAs. When
  172. Nick's patch was integrated with the Unevictable LRU work, the count was
  173. replaced by walking the reverse map when munlocking, to determine whether any
  174. other VM_LOCKED VMAs still mapped the page.
  175. However, walking the reverse map for each page when munlocking was ugly and
  176. inefficient, and could lead to catastrophic contention on a file's rmap lock,
  177. when many processes which had it mlocked were trying to exit. In 5.18, the
  178. idea of keeping mlock_count in Unevictable LRU list link field was revived and
  179. put to work, without preventing the migration of mlocked pages. This is why
  180. the "Unevictable LRU list" cannot be a linked list of pages now; but there was
  181. no use for that linked list anyway - though its size is maintained for meminfo.
  182. Basic Management
  183. ----------------
  184. mlocked pages - pages mapped into a VM_LOCKED VMA - are a class of unevictable
  185. pages. When such a page has been "noticed" by the memory management subsystem,
  186. the folio is marked with the PG_mlocked flag. This can be manipulated using
  187. folio_set_mlocked() and folio_clear_mlocked() functions.
  188. A PG_mlocked page will be placed on the unevictable list when it is added to
  189. the LRU. Such pages can be "noticed" by memory management in several places:
  190. (1) in the mlock()/mlock2()/mlockall() system call handlers;
  191. (2) in the mmap() system call handler when mmapping a region with the
  192. MAP_LOCKED flag;
  193. (3) mmapping a region in a task that has called mlockall() with the MCL_FUTURE
  194. flag;
  195. (4) in the fault path and when a VM_LOCKED stack segment is expanded; or
  196. (5) as mentioned above, in vmscan:shrink_folio_list() when attempting to
  197. reclaim a page in a VM_LOCKED VMA by folio_referenced() or try_to_unmap().
  198. mlocked pages become unlocked and rescued from the unevictable list when:
  199. (1) mapped in a range unlocked via the munlock()/munlockall() system calls;
  200. (2) munmap()'d out of the last VM_LOCKED VMA that maps the page, including
  201. unmapping at task exit;
  202. (3) when the page is truncated from the last VM_LOCKED VMA of an mmapped file;
  203. or
  204. (4) before a page is COW'd in a VM_LOCKED VMA.
  205. mlock()/mlock2()/mlockall() System Call Handling
  206. ------------------------------------------------
  207. mlock(), mlock2() and mlockall() system call handlers proceed to mlock_fixup()
  208. for each VMA in the range specified by the call. In the case of mlockall(),
  209. this is the entire active address space of the task. Note that mlock_fixup()
  210. is used for both mlocking and munlocking a range of memory. A call to mlock()
  211. an already VM_LOCKED VMA, or to munlock() a VMA that is not VM_LOCKED, is
  212. treated as a no-op and mlock_fixup() simply returns.
  213. If the VMA passes some filtering as described in "Filtering Special VMAs"
  214. below, mlock_fixup() will attempt to merge the VMA with its neighbors or split
  215. off a subset of the VMA if the range does not cover the entire VMA. Any pages
  216. already present in the VMA are then marked as mlocked by mlock_folio() via
  217. mlock_pte_range() via walk_page_range() via mlock_vma_pages_range().
  218. Before returning from the system call, do_mlock() or mlockall() will call
  219. __mm_populate() to fault in the remaining pages via get_user_pages() and to
  220. mark those pages as mlocked as they are faulted.
  221. Note that the VMA being mlocked might be mapped with PROT_NONE. In this case,
  222. get_user_pages() will be unable to fault in the pages. That's okay. If pages
  223. do end up getting faulted into this VM_LOCKED VMA, they will be handled in the
  224. fault path - which is also how mlock2()'s MLOCK_ONFAULT areas are handled.
  225. For each PTE (or PMD) being faulted into a VMA, the page add rmap function
  226. calls mlock_vma_folio(), which calls mlock_folio() when the VMA is VM_LOCKED
  227. (unless it is a PTE mapping of a part of a transparent huge page). Or when
  228. it is a newly allocated anonymous page, folio_add_lru_vma() calls
  229. mlock_new_folio() instead: similar to mlock_folio(), but can make better
  230. judgments, since this page is held exclusively and known not to be on LRU yet.
  231. mlock_folio() sets PG_mlocked immediately, then places the page on the CPU's
  232. mlock folio batch, to batch up the rest of the work to be done under lru_lock by
  233. __mlock_folio(). __mlock_folio() sets PG_unevictable, initializes mlock_count
  234. and moves the page to unevictable state ("the unevictable LRU", but with
  235. mlock_count in place of LRU threading). Or if the page was already PG_lru
  236. and PG_unevictable and PG_mlocked, it simply increments the mlock_count.
  237. But in practice that may not work ideally: the page may not yet be on an LRU, or
  238. it may have been temporarily isolated from LRU. In such cases the mlock_count
  239. field cannot be touched, but will be set to 0 later when __munlock_folio()
  240. returns the page to "LRU". Races prohibit mlock_count from being set to 1 then:
  241. rather than risk stranding a page indefinitely as unevictable, always err with
  242. mlock_count on the low side, so that when munlocked the page will be rescued to
  243. an evictable LRU, then perhaps be mlocked again later if vmscan finds it in a
  244. VM_LOCKED VMA.
  245. Filtering Special VMAs
  246. ----------------------
  247. mlock_fixup() filters several classes of "special" VMAs:
  248. 1) VMAs with VM_IO or VM_PFNMAP set are skipped entirely. The pages behind
  249. these mappings are inherently pinned, so we don't need to mark them as
  250. mlocked. In any case, most of the pages have no struct page in which to so
  251. mark the page. Because of this, get_user_pages() will fail for these VMAs,
  252. so there is no sense in attempting to visit them.
  253. 2) VMAs mapping hugetlbfs page are already effectively pinned into memory. We
  254. neither need nor want to mlock() these pages. But __mm_populate() includes
  255. hugetlbfs ranges, allocating the huge pages and populating the PTEs.
  256. 3) VMAs with VM_DONTEXPAND are generally userspace mappings of kernel pages,
  257. such as the VDSO page, relay channel pages, etc. These pages are inherently
  258. unevictable and are not managed on the LRU lists. __mm_populate() includes
  259. these ranges, populating the PTEs if not already populated.
  260. 4) VMAs with VM_MIXEDMAP set are not marked VM_LOCKED, but __mm_populate()
  261. includes these ranges, populating the PTEs if not already populated.
  262. Note that for all of these special VMAs, mlock_fixup() does not set the
  263. VM_LOCKED flag. Therefore, we won't have to deal with them later during
  264. munlock(), munmap() or task exit. Neither does mlock_fixup() account these
  265. VMAs against the task's "locked_vm".
  266. munlock()/munlockall() System Call Handling
  267. -------------------------------------------
  268. The munlock() and munlockall() system calls are handled by the same
  269. mlock_fixup() function as mlock(), mlock2() and mlockall() system calls are.
  270. If called to munlock an already munlocked VMA, mlock_fixup() simply returns.
  271. Because of the VMA filtering discussed above, VM_LOCKED will not be set in
  272. any "special" VMAs. So, those VMAs will be ignored for munlock.
  273. If the VMA is VM_LOCKED, mlock_fixup() again attempts to merge or split off the
  274. specified range. All pages in the VMA are then munlocked by munlock_folio() via
  275. mlock_pte_range() via walk_page_range() via mlock_vma_pages_range() - the same
  276. function used when mlocking a VMA range, with new flags for the VMA indicating
  277. that it is munlock() being performed.
  278. munlock_folio() uses the mlock pagevec to batch up work to be done
  279. under lru_lock by __munlock_folio(). __munlock_folio() decrements the
  280. folio's mlock_count, and when that reaches 0 it clears the mlocked flag
  281. and clears the unevictable flag, moving the folio from unevictable state
  282. to the inactive LRU.
  283. But in practice that may not work ideally: the folio may not yet have reached
  284. "the unevictable LRU", or it may have been temporarily isolated from it. In
  285. those cases its mlock_count field is unusable and must be assumed to be 0: so
  286. that the folio will be rescued to an evictable LRU, then perhaps be mlocked
  287. again later if vmscan finds it in a VM_LOCKED VMA.
  288. Migrating MLOCKED Pages
  289. -----------------------
  290. A page that is being migrated has been isolated from the LRU lists and is held
  291. locked across unmapping of the page, updating the page's address space entry
  292. and copying the contents and state, until the page table entry has been
  293. replaced with an entry that refers to the new page. Linux supports migration
  294. of mlocked pages and other unevictable pages. PG_mlocked is cleared from the
  295. the old page when it is unmapped from the last VM_LOCKED VMA, and set when the
  296. new page is mapped in place of migration entry in a VM_LOCKED VMA. If the page
  297. was unevictable because mlocked, PG_unevictable follows PG_mlocked; but if the
  298. page was unevictable for other reasons, PG_unevictable is copied explicitly.
  299. Note that page migration can race with mlocking or munlocking of the same page.
  300. There is mostly no problem since page migration requires unmapping all PTEs of
  301. the old page (including munlock where VM_LOCKED), then mapping in the new page
  302. (including mlock where VM_LOCKED). The page table locks provide sufficient
  303. synchronization.
  304. However, since mlock_vma_pages_range() starts by setting VM_LOCKED on a VMA,
  305. before mlocking any pages already present, if one of those pages were migrated
  306. before mlock_pte_range() reached it, it would get counted twice in mlock_count.
  307. To prevent that, mlock_vma_pages_range() temporarily marks the VMA as VM_IO,
  308. so that mlock_vma_folio() will skip it.
  309. To complete page migration, we place the old and new pages back onto the LRU
  310. afterwards. The "unneeded" page - old page on success, new page on failure -
  311. is freed when the reference count held by the migration process is released.
  312. Compacting MLOCKED Pages
  313. ------------------------
  314. The memory map can be scanned for compactable regions and the default behavior
  315. is to let unevictable pages be moved. /proc/sys/vm/compact_unevictable_allowed
  316. controls this behavior (see Documentation/admin-guide/sysctl/vm.rst). The work
  317. of compaction is mostly handled by the page migration code and the same work
  318. flow as described in Migrating MLOCKED Pages will apply.
  319. MLOCKING Transparent Huge Pages
  320. -------------------------------
  321. A transparent huge page is represented by a single entry on an LRU list.
  322. Therefore, we can only make unevictable an entire compound page, not
  323. individual subpages.
  324. If a user tries to mlock() part of a huge page, and no user mlock()s the
  325. whole of the huge page, we want the rest of the page to be reclaimable.
  326. We cannot just split the page on partial mlock() as split_huge_page() can
  327. fail and a new intermittent failure mode for the syscall is undesirable.
  328. We handle this by keeping PTE-mlocked huge pages on evictable LRU lists:
  329. the PMD on the border of a VM_LOCKED VMA will be split into a PTE table.
  330. This way the huge page is accessible for vmscan. Under memory pressure the
  331. page will be split, subpages which belong to VM_LOCKED VMAs will be moved
  332. to the unevictable LRU and the rest can be reclaimed.
  333. /proc/meminfo's Unevictable and Mlocked amounts do not include those parts
  334. of a transparent huge page which are mapped only by PTEs in VM_LOCKED VMAs.
  335. mmap(MAP_LOCKED) System Call Handling
  336. -------------------------------------
  337. In addition to the mlock(), mlock2() and mlockall() system calls, an application
  338. can request that a region of memory be mlocked by supplying the MAP_LOCKED flag
  339. to the mmap() call. There is one important and subtle difference here, though.
  340. mmap() + mlock() will fail if the range cannot be faulted in (e.g. because
  341. mm_populate fails) and returns with ENOMEM while mmap(MAP_LOCKED) will not fail.
  342. The mmapped area will still have properties of the locked area - pages will not
  343. get swapped out - but major page faults to fault memory in might still happen.
  344. Furthermore, any mmap() call or brk() call that expands the heap by a task
  345. that has previously called mlockall() with the MCL_FUTURE flag will result
  346. in the newly mapped memory being mlocked. Before the unevictable/mlock
  347. changes, the kernel simply called make_pages_present() to allocate pages
  348. and populate the page table.
  349. To mlock a range of memory under the unevictable/mlock infrastructure,
  350. the mmap() handler and task address space expansion functions call
  351. populate_vma_page_range() specifying the vma and the address range to mlock.
  352. munmap()/exit()/exec() System Call Handling
  353. -------------------------------------------
  354. When unmapping an mlocked region of memory, whether by an explicit call to
  355. munmap() or via an internal unmap from exit() or exec() processing, we must
  356. munlock the pages if we're removing the last VM_LOCKED VMA that maps the pages.
  357. Before the unevictable/mlock changes, mlocking did not mark the pages in any
  358. way, so unmapping them required no processing.
  359. For each PTE (or PMD) being unmapped from a VMA, folio_remove_rmap_*() calls
  360. munlock_vma_folio(), which calls munlock_folio() when the VMA is VM_LOCKED
  361. (unless it was a PTE mapping of a part of a transparent huge page).
  362. munlock_folio() uses the mlock pagevec to batch up work to be done
  363. under lru_lock by __munlock_folio(). __munlock_folio() decrements the
  364. folio's mlock_count, and when that reaches 0 it clears the mlocked flag
  365. and clears the unevictable flag, moving the folio from unevictable state
  366. to the inactive LRU.
  367. But in practice that may not work ideally: the folio may not yet have reached
  368. "the unevictable LRU", or it may have been temporarily isolated from it. In
  369. those cases its mlock_count field is unusable and must be assumed to be 0: so
  370. that the folio will be rescued to an evictable LRU, then perhaps be mlocked
  371. again later if vmscan finds it in a VM_LOCKED VMA.
  372. Truncating MLOCKED Pages
  373. ------------------------
  374. File truncation or hole punching forcibly unmaps the deleted pages from
  375. userspace; truncation even unmaps and deletes any private anonymous pages
  376. which had been Copied-On-Write from the file pages now being truncated.
  377. Mlocked pages can be munlocked and deleted in this way: like with munmap(),
  378. for each PTE (or PMD) being unmapped from a VMA, folio_remove_rmap_*() calls
  379. munlock_vma_folio(), which calls munlock_folio() when the VMA is VM_LOCKED
  380. (unless it was a PTE mapping of a part of a transparent huge page).
  381. However, if there is a racing munlock(), since mlock_vma_pages_range() starts
  382. munlocking by clearing VM_LOCKED from a VMA, before munlocking all the pages
  383. present, if one of those pages were unmapped by truncation or hole punch before
  384. mlock_pte_range() reached it, it would not be recognized as mlocked by this VMA,
  385. and would not be counted out of mlock_count. In this rare case, a page may
  386. still appear as PG_mlocked after it has been fully unmapped: and it is left to
  387. release_pages() (or __page_cache_release()) to clear it and update statistics
  388. before freeing (this event is counted in /proc/vmstat unevictable_pgs_cleared,
  389. which is usually 0).
  390. Page Reclaim in shrink_*_list()
  391. -------------------------------
  392. vmscan's shrink_active_list() culls any obviously unevictable pages -
  393. i.e. !page_evictable(page) pages - diverting those to the unevictable list.
  394. However, shrink_active_list() only sees unevictable pages that made it onto the
  395. active/inactive LRU lists. Note that these pages do not have PG_unevictable
  396. set - otherwise they would be on the unevictable list and shrink_active_list()
  397. would never see them.
  398. Some examples of these unevictable pages on the LRU lists are:
  399. (1) ramfs pages that have been placed on the LRU lists when first allocated.
  400. (2) SHM_LOCK'd shared memory pages. shmctl(SHM_LOCK) does not attempt to
  401. allocate or fault in the pages in the shared memory region. This happens
  402. when an application accesses the page the first time after SHM_LOCK'ing
  403. the segment.
  404. (3) pages still mapped into VM_LOCKED VMAs, which should be marked mlocked,
  405. but events left mlock_count too low, so they were munlocked too early.
  406. vmscan's shrink_inactive_list() and shrink_folio_list() also divert obviously
  407. unevictable pages found on the inactive lists to the appropriate memory cgroup
  408. and node unevictable list.
  409. rmap's folio_referenced_one(), called via vmscan's shrink_active_list() or
  410. shrink_folio_list(), and rmap's try_to_unmap_one() called via shrink_folio_list(),
  411. check for (3) pages still mapped into VM_LOCKED VMAs, and call mlock_vma_folio()
  412. to correct them. Such pages are culled to the unevictable list when released
  413. by the shrinker.