History

Merge tag 'mm-stable-2026-02-11-19-22' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Pull MM updates from Andrew Morton: - "powerpc/64s: do not re-activate batched TLB flush" makes arch_{enter|leave}_lazy_mmu_mode() nest properly (Alexander Gordeev) It adds a generic enter/leave layer and switches architectures to use it. Various hacks were removed in the process. - "zram: introduce compressed data writeback" implements data compression for zram writeback (Richard Chang and Sergey Senozhatsky) - "mm: folio_zero_user: clear page ranges" adds clearing of contiguous page ranges for hugepages. Large improvements during demand faulting are demonstrated (David Hildenbrand) - "memcg cleanups" tidies up some memcg code (Chen Ridong) - "mm/damon: introduce {,max_}nr_snapshots and tracepoint for damos stats" improves DAMOS stat's provided information, deterministic control, and readability (SeongJae Park) - "selftests/mm: hugetlb cgroup charging: robustness fixes" fixes a few issues in the hugetlb cgroup charging selftests (Li Wang) - "Fix va_high_addr_switch.sh test failure - again" addresses several issues in the va_high_addr_switch test (Chunyu Hu) - "mm/damon/tests/core-kunit: extend existing test scenarios" improves the KUnit test coverage for DAMON (Shu Anzai) - "mm/khugepaged: fix dirty page handling for MADV_COLLAPSE" fixes a glitch in khugepaged which was causing madvise(MADV_COLLAPSE) to transiently return -EAGAIN (Shivank Garg) - "arch, mm: consolidate hugetlb early reservation" reworks and consolidates a pile of straggly code related to reservation of hugetlb memory from bootmem and creation of CMA areas for hugetlb (Mike Rapoport) - "mm: clean up anon_vma implementation" cleans up the anon_vma implementation in various ways (Lorenzo Stoakes) - "tweaks for __alloc_pages_slowpath()" does a little streamlining of the page allocator's slowpath code (Vlastimil Babka) - "memcg: separate private and public ID namespaces" cleans up the memcg ID code and prevents the internal-only private IDs from being exposed to userspace (Shakeel Butt) - "mm: hugetlb: allocate frozen gigantic folio" cleans up the allocation of frozen folios and avoids some atomic refcount operations (Kefeng Wang) - "mm/damon: advance DAMOS-based LRU sorting" improves DAMOS's movement of memory betewwn the active and inactive LRUs and adds auto-tuning of the ratio-based quotas and of monitoring intervals (SeongJae Park) - "Support page table check on PowerPC" makes CONFIG_PAGE_TABLE_CHECK_ENFORCED work on powerpc (Andrew Donnellan) - "nodemask: align nodes_and{,not} with underlying bitmap ops" makes nodes_and() and nodes_andnot() propagate the return values from the underlying bit operations, enabling some cleanup in calling code (Yury Norov) - "mm/damon: hide kdamond and kdamond_lock from API callers" cleans up some DAMON internal interfaces (SeongJae Park) - "mm/khugepaged: cleanups and scan limit fix" does some cleanup work in khupaged and fixes a scan limit accounting issue (Shivank Garg) - "mm: balloon infrastructure cleanups" goes to town on the balloon infrastructure and its page migration function. Mainly cleanups, also some locking simplification (David Hildenbrand) - "mm/vmscan: add tracepoint and reason for kswapd_failures reset" adds additional tracepoints to the page reclaim code (Jiayuan Chen) - "Replace wq users and add WQ_PERCPU to alloc_workqueue() users" is part of Marco's kernel-wide migration from the legacy workqueue APIs over to the preferred unbound workqueues (Marco Crivellari) - "Various mm kselftests improvements/fixes" provides various unrelated improvements/fixes for the mm kselftests (Kevin Brodsky) - "mm: accelerate gigantic folio allocation" greatly speeds up gigantic folio allocation, mainly by avoiding unnecessary work in pfn_range_valid_contig() (Kefeng Wang) - "selftests/damon: improve leak detection and wss estimation reliability" improves the reliability of two of the DAMON selftests (SeongJae Park) - "mm/damon: cleanup kdamond, damon_call(), damos filter and DAMON_MIN_REGION" does some cleanup work in the core DAMON code (SeongJae Park) - "Docs/mm/damon: update intro, modules, maintainer profile, and misc" performs maintenance work on the DAMON documentation (SeongJae Park) - "mm: add and use vma_assert_stabilised() helper" refactors and cleans up the core VMA code. The main aim here is to be able to use the mmap write lock's lockdep state to perform various assertions regarding the locking which the VMA code requires (Lorenzo Stoakes) - "mm, swap: swap table phase II: unify swapin use" removes some old swap code (swap cache bypassing and swap synchronization) which wasn't working very well. Various other cleanups and simplifications were made. The end result is a 20% speedup in one benchmark (Kairui Song) - "enable PT_RECLAIM on more 64-bit architectures" makes PT_RECLAIM available on 64-bit alpha, loongarch, mips, parisc, and um. Various cleanups were performed along the way (Qi Zheng) * tag 'mm-stable-2026-02-11-19-22' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (325 commits) mm/memory: handle non-split locks correctly in zap_empty_pte_table() mm: move pte table reclaim code to memory.c mm: make PT_RECLAIM depends on MMU_GATHER_RCU_TABLE_FREE mm: convert __HAVE_ARCH_TLB_REMOVE_TABLE to CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE config um: mm: enable MMU_GATHER_RCU_TABLE_FREE parisc: mm: enable MMU_GATHER_RCU_TABLE_FREE mips: mm: enable MMU_GATHER_RCU_TABLE_FREE LoongArch: mm: enable MMU_GATHER_RCU_TABLE_FREE alpha: mm: enable MMU_GATHER_RCU_TABLE_FREE mm: change mm/pt_reclaim.c to use asm/tlb.h instead of asm-generic/tlb.h mm/damon/stat: remove __read_mostly from memory_idle_ms_percentiles zsmalloc: make common caches global mm: add SPDX id lines to some mm source files mm/zswap: use %pe to print error pointers mm/vmscan: use %pe to print error pointers mm/readahead: fix typo in comment mm: khugepaged: fix NR_FILE_PAGES and NR_SHMEM in collapse_file() mm: refactor vma_map_pages to use vm_insert_pages mm/damon: unify address range representation with damon_addr_range mm/cma: replace snprintf with strscpy in cma_new_area ...

Linus Torvalds committed 9 months ago

Commit 4cff5c02 parents 541c433 fb4ddf2332 files · +6241 −5595Browse files

Unified · Split

332 files
  1. Documentation/ABI/testing/sysfs-block-zram +14 −0
  2. Documentation/ABI/testing/sysfs-kernel-mm-damon +13 −0
  3. Documentation/admin-guide/blockdev/zram.rst +23 −1
  4. Documentation/admin-guide/cgroup-v1/memory.rst +2 −3
  5. Documentation/admin-guide/laptops/index.rst +0 −1
  6. Documentation/admin-guide/laptops/laptop-mode.rst +0 −770
  7. Documentation/admin-guide/mm/damon/lru_sort.rst +37 −0
  8. Documentation/admin-guide/mm/damon/usage.rst +14 −5
  9. Documentation/admin-guide/mm/memory-hotplug.rst +16 −6
  10. Documentation/admin-guide/sysctl/vm.rst +28 −8
  11. Documentation/core-api/mm-api.rst +1 −1
  12. Documentation/driver-api/cxl/linux/early-boot.rst +1 −1
  13. Documentation/mm/damon/design.rst +29 −3
  14. Documentation/mm/damon/index.rst +9 −22
  15. Documentation/mm/damon/maintainer-profile.rst +3 −4
  16. Documentation/mm/memory-model.rst +0 −3
  17. Documentation/translations/zh_CN/mm/memory-model.rst +0 −2
  18. MAINTAINERS +11 −3
  19. arch/alpha/Kconfig +1 −0
  20. arch/alpha/include/asm/page.h +0 −1
  21. arch/alpha/include/asm/tlb.h +3 −3
  22. arch/alpha/kernel/setup.c +0 −1
  23. arch/alpha/mm/init.c +7 −9
  24. arch/arc/include/asm/page.h +2 −0
  25. arch/arc/mm/init.c +19 −18
  26. arch/arm/include/asm/page-nommu.h +0 −1
  27. arch/arm/include/asm/pgtable.h +2 −2
  28. arch/arm/mm/init.c +4 −21
  29. arch/arm/mm/mmu.c +1 −9
  30. arch/arm/mm/nommu.c +1 −9
  31. arch/arm64/Kconfig +1 −0
  32. arch/arm64/include/asm/hugetlb.h +0 −2
  33. arch/arm64/include/asm/page.h +0 −1
  34. arch/arm64/include/asm/pgtable.h +26 −61
  35. arch/arm64/include/asm/thread_info.h +1 −2
  36. arch/arm64/mm/hugetlbpage.c +12 −15
  37. arch/arm64/mm/init.c +16 −23
  38. arch/arm64/mm/mmu.c +4 −4
  39. arch/arm64/mm/pageattr.c +2 −2
  40. arch/csky/abiv1/inc/abi/page.h +1 −0
  41. arch/csky/abiv2/inc/abi/page.h +0 −7
  42. arch/csky/kernel/setup.c +8 −8
  43. arch/hexagon/include/asm/page.h +0 −1
  44. arch/hexagon/mm/init.c +5 −14
  45. arch/loongarch/Kconfig +1 −0
  46. arch/loongarch/include/asm/page.h +0 −1
  47. arch/loongarch/include/asm/pgalloc.h +3 −4
  48. arch/loongarch/include/asm/pgtable.h +0 −2
  49. arch/loongarch/kernel/setup.c +0 −10
  50. arch/loongarch/mm/init.c +1 −5
  51. arch/m68k/include/asm/page_no.h +0 −1
  52. arch/m68k/mm/init.c +5 −3
  53. arch/m68k/mm/mcfmmu.c +0 −3
  54. arch/m68k/mm/motorola.c +1 −5
  55. arch/m68k/mm/sun3mmu.c +0 −9
  56. arch/microblaze/include/asm/page.h +0 −1
  57. arch/microblaze/mm/init.c +10 −12
  58. arch/mips/Kconfig +1 −0
  59. arch/mips/include/asm/page.h +1 −0
  60. arch/mips/include/asm/pgalloc.h +3 −6
  61. arch/mips/include/asm/pgtable.h +1 −1
  62. arch/mips/kernel/setup.c +2 −13
  63. arch/mips/loongson64/numa.c +3 −7
  64. arch/mips/mm/init.c +1 −7
  65. arch/mips/sgi-ip27/ip27-memory.c +2 −6
  66. arch/nios2/include/asm/page.h +1 −0
  67. arch/nios2/mm/init.c +5 −7
  68. arch/openrisc/include/asm/page.h +0 −1
  69. arch/openrisc/mm/init.c +2 −8
  70. arch/parisc/Kconfig +1 −0
  71. arch/parisc/include/asm/page.h +0 −1
  72. arch/parisc/include/asm/tlb.h +2 −2
  73. arch/parisc/mm/init.c +2 −9
  74. arch/powerpc/Kconfig +2 −0
  75. arch/powerpc/include/asm/book3s/32/pgtable.h +11 −1
  76. arch/powerpc/include/asm/book3s/64/pgtable.h +52 −10
  77. arch/powerpc/include/asm/book3s/64/tlbflush-hash.h +9 −11
  78. arch/powerpc/include/asm/hugetlb.h +0 −5
  79. arch/powerpc/include/asm/nohash/pgtable.h +12 −1
  80. arch/powerpc/include/asm/page.h +1 −0
  81. arch/powerpc/include/asm/pgtable.h +10 −0
  82. arch/powerpc/include/asm/setup.h +4 −0
  83. arch/powerpc/include/asm/thread_info.h +0 −2
  84. arch/powerpc/include/asm/tlb.h +0 −1
  85. arch/powerpc/kernel/process.c +0 −25
  86. arch/powerpc/kernel/setup-common.c +0 −1
  87. arch/powerpc/mm/book3s64/hash_pgtable.c +4 −0
  88. arch/powerpc/mm/book3s64/hash_tlb.c +8 −6
  89. arch/powerpc/mm/book3s64/pgtable.c +16 −9
  90. arch/powerpc/mm/book3s64/radix_pgtable.c +6 −3
  91. arch/powerpc/mm/book3s64/subpage_prot.c +2 −2
  92. arch/powerpc/mm/hugetlbpage.c +4 −7
  93. arch/powerpc/mm/mem.c +10 −17
  94. arch/powerpc/mm/numa.c +0 −2
  95. arch/powerpc/mm/pgtable.c +12 −0
  96. arch/powerpc/platforms/Kconfig.cputype +1 −0
  97. arch/powerpc/platforms/pseries/Kconfig +1 −1
  98. arch/powerpc/platforms/pseries/cmm.c +10 −43
  99. arch/riscv/include/asm/page.h +0 −1
  100. arch/riscv/include/asm/pgtable.h +11 −11
  101. arch/riscv/mm/hugetlbpage.c +8 −0
  102. arch/riscv/mm/init.c +1 −9
  103. arch/s390/include/asm/page.h +0 −1
  104. arch/s390/include/asm/tlb.h +2 −4
  105. arch/s390/kernel/setup.c +0 −2
  106. arch/s390/mm/gmap_helpers.c +1 −1
  107. arch/s390/mm/hugetlbpage.c +8 −0
  108. arch/s390/mm/init.c +6 −7
  109. arch/s390/mm/pgtable.c +1 −1
  110. arch/sh/mm/init.c +5 −7
  111. arch/sparc/Kconfig +2 −0
  112. arch/sparc/include/asm/page_64.h +1 −0
  113. arch/sparc/include/asm/tlb_64.h +0 −1
  114. arch/sparc/include/asm/tlbflush_64.h +1 −4
  115. arch/sparc/mm/init_64.c +5 −12
  116. arch/sparc/mm/srmmu.c +7 −10
  117. arch/sparc/mm/tlb.c +13 −7
  118. arch/um/Kconfig +1 −0
  119. arch/um/include/asm/page.h +0 −1
  120. arch/um/kernel/mem.c +5 −5
  121. arch/x86/Kconfig +1 −1
  122. arch/x86/boot/compressed/misc.h +1 −0
  123. arch/x86/boot/startup/sme.c +1 −0
  124. arch/x86/include/asm/page.h +0 −6
  125. arch/x86/include/asm/page_32.h +6 −0
  126. arch/x86/include/asm/page_64.h +61 −17
  127. arch/x86/include/asm/paravirt.h +0 −1
  128. arch/x86/include/asm/pgtable.h +12 −11
  129. arch/x86/include/asm/thread_info.h +1 −3
  130. arch/x86/kernel/setup.c +0 −5
  131. arch/x86/lib/clear_page_64.S +8 −31
  132. arch/x86/mm/hugetlbpage.c +8 −0
  133. arch/x86/mm/init.c +1 −7
  134. arch/x86/mm/init_32.c +0 −2
  135. arch/x86/mm/init_64.c +0 −4
  136. arch/x86/mm/mm_internal.h +0 −1
  137. arch/x86/xen/enlighten_pv.c +1 −2
  138. arch/x86/xen/mmu_pv.c +2 −4
  139. arch/xtensa/include/asm/page.h +0 −1
  140. arch/xtensa/mm/init.c +7 −7
  141. block/blk-mq.c +0 −3
  142. drivers/block/zram/zram_drv.c +565 −434
  143. drivers/block/zram/zram_drv.h +10 −5
  144. drivers/misc/Kconfig +1 −1
  145. drivers/misc/vmw_balloon.c +29 −76
  146. drivers/virtio/Kconfig +1 −1
  147. drivers/virtio/virtio_balloon.c +21 −43
  148. fs/ext4/inode.c +1 −2
  149. fs/proc/task_mmu.c +2 −2
  150. fs/sync.c +0 −2
  151. fs/xfs/xfs_super.c +0 −9
  152. include/asm-generic/tlb.h +3 −4
  153. include/linux/backing-dev-defs.h +0 −3
  154. include/linux/balloon.h +77 −0
  155. include/linux/balloon_compaction.h +0 −160
  156. include/linux/cma.h +6 −21
  157. include/linux/damon.h +36 −29
  158. include/linux/gfp.h +28 −32
  159. include/linux/gfp_types.h +5 −2
  160. include/linux/highmem.h +97 −1
  161. include/linux/hugetlb.h +6 −9
  162. include/linux/khugepaged.h +4 −5
  163. include/linux/maple_tree.h +0 −9
  164. include/linux/memblock.h +2 −2
  165. include/linux/memcontrol.h +20 −28
  166. include/linux/mm.h +66 −6
  167. include/linux/mm_types.h +52 −5
  168. include/linux/mm_types_task.h +5 −0
  169. include/linux/mmap_lock.h +240 −39
  170. include/linux/mmdebug.h +10 −0
  171. include/linux/mmzone.h +15 −4
  172. include/linux/nodemask.h +4 −4
  173. include/linux/page-isolation.h +2 −0
  174. include/linux/page_table_check.h +40 −29
  175. include/linux/pgtable.h +154 −14
  176. include/linux/rmap.h +3 −73
  177. include/linux/sched.h +45 −0
  178. include/linux/swap.h +25 −46
  179. include/linux/vm_event_item.h +4 −4
  180. include/linux/vmstat.h +0 −6
  181. include/linux/writeback.h +0 −4
  182. include/linux/zsmalloc.h +6 −2
  183. include/trace/events/damon.h +41 −0
  184. include/trace/events/huge_memory.h +2 −1
  185. include/trace/events/vmscan.h +51 −0
  186. include/trace/events/writeback.h +0 −1
  187. include/uapi/linux/mempolicy.h +3 −0
  188. include/uapi/linux/sysctl.h +1 −1
  189. init/main.c +1 −0
  190. kernel/cgroup/cpuset.c +3 −4
  191. kernel/power/swap.c +6 −4
  192. lib/alloc_tag.c +17 −10
  193. lib/test_vmalloc.c +7 −4
  194. mm/Kconfig +31 −17
  195. mm/Makefile +2 −2
  196. mm/backing-dev.c +3 −6
  197. mm/balloon.c +140 −59
  198. mm/cma.c +71 −54
  199. mm/damon/core.c +142 −100
  200. mm/damon/lru_sort.c +112 −8
  201. mm/damon/paddr.c +12 −15
  202. mm/damon/reclaim.c +6 −4
  203. mm/damon/stat.c +3 −11
  204. mm/damon/sysfs-schemes.c +54 −4
  205. mm/damon/sysfs.c +9 −9
  206. mm/damon/tests/core-kunit.h +39 −12
  207. mm/damon/tests/vaddr-kunit.h +1 −1
  208. mm/damon/vaddr.c +12 −13
  209. mm/debug_vm_pgtable.c +17 −21
  210. mm/dmapool_test.c +1 −0
  211. mm/early_ioremap.c +12 −4
  212. mm/folio-compat.c +1 −0
  213. mm/gup.c +0 −11
  214. mm/gup_test.c +1 −0
  215. mm/highmem.c +2 −1
  216. mm/huge_memory.c +4 −4
  217. mm/hugetlb.c +93 −87
  218. mm/hugetlb_cgroup.c +2 −9
  219. mm/hugetlb_cma.c +37 −34
  220. mm/hugetlb_cma.h +5 −10
  221. mm/hugetlb_sysctl.c +11 −0
  222. mm/hugetlb_vmemmap.c +0 −11
  223. mm/internal.h +87 −19
  224. mm/kasan/kasan_test_c.c +35 −15
  225. mm/kasan/report.c +4 −4
  226. mm/kasan/shadow.c +4 −4
  227. mm/kfence/core.c +3 −3
  228. mm/khugepaged.c +102 −83
  229. mm/kmsan/kmsan_test.c +63 −1
  230. mm/list_lru.c +3 −3
  231. mm/madvise.c +12 −14
  232. mm/memblock.c +2 −2
  233. mm/memcontrol-v1.c +25 −3
  234. mm/memcontrol-v1.h +2 −4
  235. mm/memcontrol.c +34 −65
  236. mm/memfd.c +1 −2
  237. mm/memory-failure.c +1 −1
  238. mm/memory-tiers.c +3 −4
  239. mm/memory.c +260 −191
  240. mm/memory_hotplug.c +4 −4
  241. mm/mempolicy.c +2 −3
  242. mm/migrate.c +7 −7
  243. mm/migrate_device.c +6 −6
  244. mm/mm_init.c +18 −17
  245. mm/mmap_lock.c +114 −63
  246. mm/mmu_gather.c +3 −3
  247. mm/mprotect.c +2 −2
  248. mm/mremap.c +5 −5
  249. mm/mseal.c +2 −2
  250. mm/numa_memblks.c +1 −1
  251. mm/oom_kill.c +13 −13
  252. mm/page-writeback.c +15 −59
  253. mm/page_alloc.c +275 −150
  254. mm/page_io.c +2 −2
  255. mm/page_isolation.c +100 −89
  256. mm/page_reporting.c +1 −1
  257. mm/page_table_check.c +22 −19
  258. mm/pagewalk.c +2 −1
  259. mm/percpu.c +14 −1
  260. mm/pt_reclaim.c +0 −72
  261. mm/readahead.c +1 −1
  262. mm/rmap.c +205 −119
  263. mm/shmem.c +36 −54
  264. mm/show_mem.c +1 −2
  265. mm/shrinker_debug.c +7 −6
  266. mm/slub.c +2 −1
  267. mm/swap.c +1 −1
  268. mm/swap.h +59 −13
  269. mm/swap_state.c +223 −119
  270. mm/swapfile.c +384 −478
  271. mm/tests/lazy_mmu_mode_kunit.c +73 −0
  272. mm/userfaultfd.c +7 −11
  273. mm/vma.c +8 −8
  274. mm/vma.h +4 −10
  275. mm/vmalloc.c +20 −6
  276. mm/vmscan.c +113 −108
  277. mm/vmstat.c +8 −22
  278. mm/workingset.c +4 −4
  279. mm/zsmalloc.c +125 −67
  280. mm/zswap.c +23 −35
  281. tools/mm/slabinfo.c +1 −1
  282. tools/mm/thp_swap_allocator_test.c +1 −1
  283. tools/testing/selftests/damon/access_memory.c +21 −8
  284. tools/testing/selftests/damon/sysfs_memcg_path_leak.sh +14 −12
  285. tools/testing/selftests/damon/sysfs_update_schemes_tried_regions_wss_estimation.py +32 −9
  286. tools/testing/selftests/mm/.gitignore +0 −1
  287. tools/testing/selftests/mm/Makefile +34 −10
  288. tools/testing/selftests/mm/charge_reserved_hugetlb.sh +32 −23
  289. tools/testing/selftests/mm/check_config.sh +1 −2
  290. tools/testing/selftests/mm/cow.c +33 −10
  291. tools/testing/selftests/mm/hugetlb-madvise.c +1 −8
  292. tools/testing/selftests/mm/ksft_compaction.sh +4 −0
  293. tools/testing/selftests/mm/ksft_cow.sh +4 −0
  294. tools/testing/selftests/mm/ksft_gup_test.sh +4 −0
  295. tools/testing/selftests/mm/ksft_hmm.sh +4 −0
  296. tools/testing/selftests/mm/ksft_hugetlb.sh +4 −0
  297. tools/testing/selftests/mm/ksft_hugevm.sh +4 −0
  298. tools/testing/selftests/mm/ksft_ksm.sh +4 −0
  299. tools/testing/selftests/mm/ksft_ksm_numa.sh +4 −0
  300. tools/testing/selftests/mm/ksft_madv_guard.sh +4 −0
  301. tools/testing/selftests/mm/ksft_madv_populate.sh +4 −0
  302. tools/testing/selftests/mm/ksft_mdwe.sh +4 −0
  303. tools/testing/selftests/mm/ksft_memfd_secret.sh +4 −0
  304. tools/testing/selftests/mm/ksft_migration.sh +4 −0
  305. tools/testing/selftests/mm/ksft_mkdirty.sh +4 −0
  306. tools/testing/selftests/mm/ksft_mlock.sh +4 −0
  307. tools/testing/selftests/mm/ksft_mmap.sh +4 −0
  308. tools/testing/selftests/mm/ksft_mremap.sh +4 −0
  309. tools/testing/selftests/mm/ksft_page_frag.sh +4 −0
  310. tools/testing/selftests/mm/ksft_pagemap.sh +4 −0
  311. tools/testing/selftests/mm/ksft_pfnmap.sh +4 −0
  312. tools/testing/selftests/mm/ksft_pkey.sh +4 −0
  313. tools/testing/selftests/mm/ksft_process_madv.sh +4 −0
  314. tools/testing/selftests/mm/ksft_process_mrelease.sh +4 −0
  315. tools/testing/selftests/mm/ksft_rmap.sh +4 −0
  316. tools/testing/selftests/mm/ksft_soft_dirty.sh +4 −0
  317. tools/testing/selftests/mm/ksft_thp.sh +4 −0
  318. tools/testing/selftests/mm/ksft_userfaultfd.sh +4 −0
  319. tools/testing/selftests/mm/ksft_vma_merge.sh +4 −0
  320. tools/testing/selftests/mm/ksft_vmalloc.sh +4 −0
  321. tools/testing/selftests/mm/page_frag/Makefile +1 −1
  322. tools/testing/selftests/mm/pagemap_ioctl.c +7 −8
  323. tools/testing/selftests/mm/pfnmap.c +56 −37
  324. tools/testing/selftests/mm/run_vmtests.sh +4 −20
  325. tools/testing/selftests/mm/split_huge_page_test.c +1 −5
  326. tools/testing/selftests/mm/test_vmalloc.sh +28 −3
  327. tools/testing/selftests/mm/va_high_addr_switch.c +7 −3
  328. tools/testing/selftests/mm/va_high_addr_switch.sh +7 −5
  329. tools/testing/selftests/mm/virtual_address_range.c +0 −260
  330. tools/testing/selftests/mm/vm_util.h +7 −0
  331. tools/testing/selftests/mm/write_to_hugetlbfs.c +6 −3
  332. tools/testing/vma/vma_internal.h +10 −6
Documentation/ABI/testing/sysfs-block-zram+14−0
@@ -150,3 +150,17 @@ Contact: Sergey Senozhatsky <senozhatsky@chromium.org> · 149 unmodified lines
150150 Description:
151151 The algorithm_params file is write-only and is used to setup
152152 compression algorithm parameters.
153+
154+What: /sys/block/zram<id>/writeback_compressed
155+Date: Decemeber 2025
156+Contact: Richard Chang <richardycc@google.com>
157+Description:
158+ The writeback_compressed device atrribute toggles compressed
159+ writeback feature.
160+
161+What: /sys/block/zram<id>/writeback_batch_size
162+Date: November 2025
163+Contact: Sergey Senozhatsky <senozhatsky@chromium.org>
164+Description:
165+ The writeback_batch_size device atrribute sets the maximum
166+ number of in-flight writeback operations.
Documentation/ABI/testing/sysfs-kernel-mm-damon+13−0
@@ -516,6 +516,19 @@ Contact: SeongJae Park <sj@kernel.org> · 515 unmodified lines
516516 Description: Reading this file returns the number of the exceed events of
517517 the scheme's quotas.
518518
519+What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/schemes/<S>/stats/nr_snapshots
520+Date: Dec 2025
521+Contact: SeongJae Park <sj@kernel.org>
522+Description: Reading this file returns the total number of DAMON snapshots
523+ that the scheme has tried to be applied.
524+
525+What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/schemes/<S>/stats/max_nr_snapshots
526+Date: Dec 2025
527+Contact: SeongJae Park <sj@kernel.org>
528+Description: Writing a number to this file sets the upper limit of
529+ nr_snapshots that deactivates the scheme when the limit is
530+ reached or exceeded.
531+
519532 What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/schemes/<S>/tried_regions/total_bytes
520533 Date: Jul 2023
521534 Contact: SeongJae Park <sj@kernel.org>
Documentation/admin-guide/blockdev/zram.rst+23−1
@@ -214,6 +214,9 @@ mem_limit WO specifies the maximum amount of memory ZRAM can · 213 unmodified lines
214214 writeback_limit WO specifies the maximum amount of write IO zram
215215 can write out to backing device as 4KB unit
216216 writeback_limit_enable RW show and set writeback_limit feature
217+writeback_batch_size RW show and set maximum number of in-flight
218+ writeback operations
219+writeback_compressed RW show and set compressed writeback feature
217220 comp_algorithm RW show and change the compression algorithm
218221 algorithm_params WO setup compression algorithm parameters
219222 compact WO trigger memory compaction
@@ -222,7 +225,6 @@ backing_dev RW set up backend storage for zram to write out · 2 unmodified lines
222225 idle WO mark allocated slot as idle
223226 ====================== ====== ===============================================
224227
225−
226228 User space is advised to use the following files to read the device statistics.
227229
228230 File /sys/block/zram<id>/stat
@@ -434,6 +436,26 @@ system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of · 205 unmodified lines
434436 writeback happened until you reset the zram to allocate extra writeback
435437 budget in next setting is user's job.
436438
439+By default zram stores written back pages in decompressed (raw) form, which
440+means that writeback operation involves decompression of the page before
441+writing it to the backing device. This behavior can be changed by enabling
442+`writeback_compressed` feature, which causes zram to write compressed pages
443+to the backing device, thus avoiding decompression overhead. To enable
444+this feature, execute::
445+
446+ $ echo yes > /sys/block/zramX/writeback_compressed
447+
448+Note that this feature should be configured before the `zramX` device is
449+initialized.
450+
451+Depending on backing device storage type, writeback operation may benefit
452+from a higher number of in-flight write requests (batched writes). The
453+number of maximum in-flight writeback operations can be configured via
454+`writeback_batch_size` attribute. To change the default value (which is 32),
455+execute::
456+
457+ $ echo 64 > /sys/block/zramX/writeback_batch_size
458+
437459 If admin wants to measure writeback count in a certain period, they could
438460 know it via /sys/block/zram0/bd_stat's 3rd column.
439461
Documentation/admin-guide/cgroup-v1/memory.rst+2−3
@@ -311,9 +311,8 @@ Lock order is as follows:: · 310 unmodified lines
311311
312312 folio_lock
313313 mm->page_table_lock or split pte_lock
314− folio_memcg_lock (memcg->move_lock)
315− mapping->i_pages lock
316− lruvec->lru_lock.
314+ mapping->i_pages lock
315+ lruvec->lru_lock.
317316
318317 Per-node-per-memcgroup LRU (cgroup's private LRU) is guarded by
319318 lruvec->lru_lock; the folio LRU flag is cleared before
Documentation/admin-guide/laptops/index.rst+0−1
@@ -10,7 +10,6 @@ Laptop Drivers · 9 unmodified lines
1010 alienware-wmi
1111 asus-laptop
1212 disk-shock-protection
13− laptop-mode
1413 lg-laptop
1514 samsung-galaxybook
1615 sony-laptop
Documentation/admin-guide/laptops/laptop-mode.rst+0−770
@@ -1,770 +0,0 @@
1−===============================================
2−How to conserve battery power using laptop-mode
3−===============================================
4−
5−Document Author: Bart Samwel (bart@samwel.tk)
6−
7−Date created: January 2, 2004
8−
9−Last modified: December 06, 2004
10−
11−Introduction
12−------------
13−
14−Laptop mode is used to minimize the time that the hard disk needs to be spun up,
15−to conserve battery power on laptops. It has been reported to cause significant
16−power savings.
17−
18−.. Contents
19−
20− * Introduction
21− * Installation
22− * Caveats
23− * The Details
24− * Tips & Tricks
25− * Control script
26− * ACPI integration
27− * Monitoring tool
28−
29−
30−Installation
31−------------
32−
33−To use laptop mode, you don't need to set any kernel configuration options
34−or anything. Simply install all the files included in this document, and
35−laptop mode will automatically be started when you're on battery. For
36−your convenience, a tarball containing an installer can be downloaded at:
37−
38− http://www.samwel.tk/laptop_mode/laptop_mode/
39−
40−To configure laptop mode, you need to edit the configuration file, which is
41−located in /etc/default/laptop-mode on Debian-based systems, or in
42−/etc/sysconfig/laptop-mode on other systems.
43−
44−Unfortunately, automatic enabling of laptop mode does not work for
45−laptops that don't have ACPI. On those laptops, you need to start laptop
46−mode manually. To start laptop mode, run "laptop_mode start", and to
47−stop it, run "laptop_mode stop". (Note: The laptop mode tools package now
48−has experimental support for APM, you might want to try that first.)
49−
50−
51−Caveats
52−-------
53−
54−* The downside of laptop mode is that you have a chance of losing up to 10
55− minutes of work. If you cannot afford this, don't use it! The supplied ACPI
56− scripts automatically turn off laptop mode when the battery almost runs out,
57− so that you won't lose any data at the end of your battery life.
58−
59−* Most desktop hard drives have a very limited lifetime measured in spindown
60− cycles, typically about 50.000 times (it's usually listed on the spec sheet).
61− Check your drive's rating, and don't wear down your drive's lifetime if you
62− don't need to.
63−
64−* If you mount some of your ext3 filesystems with the -n option, then
65− the control script will not be able to remount them correctly. You must set
66− DO_REMOUNTS=0 in the control script, otherwise it will remount them with the
67− wrong options -- or it will fail because it cannot write to /etc/mtab.
68−
69−* If you have your filesystems listed as type "auto" in fstab, like I did, then
70− the control script will not recognize them as filesystems that need remounting.
71− You must list the filesystems with their true type instead.
72−
73−* It has been reported that some versions of the mutt mail client use file access
74− times to determine whether a folder contains new mail. If you use mutt and
75− experience this, you must disable the noatime remounting by setting the option
76− DO_REMOUNT_NOATIME to 0 in the configuration file.
77−
78−
79−The Details
80−-----------
81−
82−Laptop mode is controlled by the knob /proc/sys/vm/laptop_mode. This knob is
83−present for all kernels that have the laptop mode patch, regardless of any
84−configuration options. When the knob is set, any physical disk I/O (that might
85−have caused the hard disk to spin up) causes Linux to flush all dirty blocks. The
86−result of this is that after a disk has spun down, it will not be spun up
87−anymore to write dirty blocks, because those blocks had already been written
88−immediately after the most recent read operation. The value of the laptop_mode
89−knob determines the time between the occurrence of disk I/O and when the flush
90−is triggered. A sensible value for the knob is 5 seconds. Setting the knob to
91−0 disables laptop mode.
92−
93−To increase the effectiveness of the laptop_mode strategy, the laptop_mode
94−control script increases dirty_expire_centisecs and dirty_writeback_centisecs in
95−/proc/sys/vm to about 10 minutes (by default), which means that pages that are
96−dirtied are not forced to be written to disk as often. The control script also
97−changes the dirty background ratio, so that background writeback of dirty pages
98−is not done anymore. Combined with a higher commit value (also 10 minutes) for
99−ext3 filesystem (also done automatically by the control script),
100−this results in concentration of disk activity in a small time interval which
101−occurs only once every 10 minutes, or whenever the disk is forced to spin up by
102−a cache miss. The disk can then be spun down in the periods of inactivity.
103−
104−
105−Configuration
106−-------------
107−
108−The laptop mode configuration file is located in /etc/default/laptop-mode on
109−Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. It
110−contains the following options:
111−
112−MAX_AGE:
113−
114−Maximum time, in seconds, of hard drive spindown time that you are
115−comfortable with. Worst case, it's possible that you could lose this
116−amount of work if your battery fails while you're in laptop mode.
117−
118−MINIMUM_BATTERY_MINUTES:
119−
120−Automatically disable laptop mode if the remaining number of minutes of
121−battery power is less than this value. Default is 10 minutes.
122−
123−AC_HD/BATT_HD:
124−
125−The idle timeout that should be set on your hard drive when laptop mode
126−is active (BATT_HD) and when it is not active (AC_HD). The defaults are
127−20 seconds (value 4) for BATT_HD and 2 hours (value 244) for AC_HD. The
128−possible values are those listed in the manual page for "hdparm" for the
129−"-S" option.
130−
131−HD:
132−
133−The devices for which the spindown timeout should be adjusted by laptop mode.
134−Default is /dev/hda. If you specify multiple devices, separate them by a space.
135−
136−READAHEAD:
137−
138−Disk readahead, in 512-byte sectors, while laptop mode is active. A large
139−readahead can prevent disk accesses for things like executable pages (which are
140−loaded on demand while the application executes) and sequentially accessed data
141−(MP3s).
142−
143−DO_REMOUNTS:
144−
145−The control script automatically remounts any mounted journaled filesystems
146−with appropriate commit interval options. When this option is set to 0, this
147−feature is disabled.
148−
149−DO_REMOUNT_NOATIME:
150−
151−When remounting, should the filesystems be remounted with the noatime option?
152−Normally, this is set to "1" (enabled), but there may be programs that require
153−access time recording.
154−
155−DIRTY_RATIO:
156−
157−The percentage of memory that is allowed to contain "dirty" or unsaved data
158−before a writeback is forced, while laptop mode is active. Corresponds to
159−the /proc/sys/vm/dirty_ratio sysctl.
160−
161−DIRTY_BACKGROUND_RATIO:
162−
163−The percentage of memory that is allowed to contain "dirty" or unsaved data
164−after a forced writeback is done due to an exceeding of DIRTY_RATIO. Set
165−this nice and low. This corresponds to the /proc/sys/vm/dirty_background_ratio
166−sysctl.
167−
168−Note that the behaviour of dirty_background_ratio is quite different
169−when laptop mode is active and when it isn't. When laptop mode is inactive,
170−dirty_background_ratio is the threshold percentage at which background writeouts
171−start taking place. When laptop mode is active, however, background writeouts
172−are disabled, and the dirty_background_ratio only determines how much writeback
173−is done when dirty_ratio is reached.
174−
175−DO_CPU:
176−
177−Enable CPU frequency scaling when in laptop mode. (Requires CPUFreq to be setup.
178−See Documentation/admin-guide/pm/cpufreq.rst for more info. Disabled by default.)
179−
180−CPU_MAXFREQ:
181−
182−When on battery, what is the maximum CPU speed that the system should use? Legal
183−values are "slowest" for the slowest speed that your CPU is able to operate at,
184−or a value listed in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies.
185−
186−
187−Tips & Tricks
188−-------------
189−
190−* Bartek Kania reports getting up to 50 minutes of extra battery life (on top
191− of his regular 3 to 3.5 hours) using a spindown time of 5 seconds (BATT_HD=1).
192−
193−* You can spin down the disk while playing MP3, by setting disk readahead
194− to 8MB (READAHEAD=16384). Effectively, the disk will read a complete MP3 at
195− once, and will then spin down while the MP3 is playing. (Thanks to Bartek
196− Kania.)
197−
198−* Drew Scott Daniels observed: "I don't know why, but when I decrease the number
199− of colours that my display uses it consumes less battery power. I've seen
200− this on powerbooks too. I hope that this is a piece of information that
201− might be useful to the Laptop Mode patch or its users."
202−
203−* In syslog.conf, you can prefix entries with a dash `-` to omit syncing the
204− file after every logging. When you're using laptop-mode and your disk doesn't
205− spin down, this is a likely culprit.
206−
207−* Richard Atterer observed that laptop mode does not work well with noflushd
208− (http://noflushd.sourceforge.net/), it seems that noflushd prevents laptop-mode
209− from doing its thing.
210−
211−* If you're worried about your data, you might want to consider using a USB
212− memory stick or something like that as a "working area". (Be aware though
213− that flash memory can only handle a limited number of writes, and overuse
214− may wear out your memory stick pretty quickly. Do _not_ use journalling
215− filesystems on flash memory sticks.)
216−
217−
218−Configuration file for control and ACPI battery scripts
219−-------------------------------------------------------
220−
221−This allows the tunables to be changed for the scripts via an external
222−configuration file
223−
224−It should be installed as /etc/default/laptop-mode on Debian, and as
225−/etc/sysconfig/laptop-mode on Red Hat, SUSE, Mandrake, and other work-alikes.
226−
227−Config file::
228−
229− # Maximum time, in seconds, of hard drive spindown time that you are
230− # comfortable with. Worst case, it's possible that you could lose this
231− # amount of work if your battery fails you while in laptop mode.
232− #MAX_AGE=600
233−
234− # Automatically disable laptop mode when the number of minutes of battery
235− # that you have left goes below this threshold.
236− MINIMUM_BATTERY_MINUTES=10
237−
238− # Read-ahead, in 512-byte sectors. You can spin down the disk while playing MP3/OGG
239− # by setting the disk readahead to 8MB (READAHEAD=16384). Effectively, the disk
240− # will read a complete MP3 at once, and will then spin down while the MP3/OGG is
241− # playing.
242− #READAHEAD=4096
243−
244− # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
245− #DO_REMOUNTS=1
246−
247− # And shall we add the "noatime" option to that as well? (1=yes)
248− #DO_REMOUNT_NOATIME=1
249−
250− # Dirty synchronous ratio. At this percentage of dirty pages the process
251− # which
252− # calls write() does its own writeback
253− #DIRTY_RATIO=40
254−
255− #
256− # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been
257− # exceeded, the kernel will wake flusher threads which will then reduce the
258− # amount of dirty memory to dirty_background_ratio. Set this nice and low,
259− # so once some writeout has commenced, we do a lot of it.
260− #
261− #DIRTY_BACKGROUND_RATIO=5
262−
263− # kernel default dirty buffer age
264− #DEF_AGE=30
265− #DEF_UPDATE=5
266− #DEF_DIRTY_BACKGROUND_RATIO=10
267− #DEF_DIRTY_RATIO=40
268− #DEF_XFS_AGE_BUFFER=15
269− #DEF_XFS_SYNC_INTERVAL=30
270− #DEF_XFS_BUFD_INTERVAL=1
271−
272− # This must be adjusted manually to the value of HZ in the running kernel
273− # on 2.4, until the XFS people change their 2.4 external interfaces to work in
274− # centisecs. This can be automated, but it's a work in progress that still
275− # needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for
276− # external interfaces, and that is currently always set to 100. So you don't
277− # need to change this on 2.6.
278− #XFS_HZ=100
279−
280− # Should the maximum CPU frequency be adjusted down while on battery?
281− # Requires CPUFreq to be setup.
282− # See Documentation/admin-guide/pm/cpufreq.rst for more info
283− #DO_CPU=0
284−
285− # When on battery what is the maximum CPU speed that the system should
286− # use? Legal values are "slowest" for the slowest speed that your
287− # CPU is able to operate at, or a value listed in:
288− # /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
289− # Only applicable if DO_CPU=1.
290− #CPU_MAXFREQ=slowest
291−
292− # Idle timeout for your hard drive (man hdparm for valid values, -S option)
293− # Default is 2 hours on AC (AC_HD=244) and 20 seconds for battery (BATT_HD=4).
294− #AC_HD=244
295− #BATT_HD=4
296−
297− # The drives for which to adjust the idle timeout. Separate them by a space,
298− # e.g. HD="/dev/hda /dev/hdb".
299− #HD="/dev/hda"
300−
301− # Set the spindown timeout on a hard drive?
302− #DO_HD=1
303−
304−
305−Control script
306−--------------
307−
308−Please note that this control script works for the Linux 2.4 and 2.6 series (thanks
309−to Kiko Piris).
310−
311−Control script::
312−
313− #!/bin/bash
314−
315− # start or stop laptop_mode, best run by a power management daemon when
316− # ac gets connected/disconnected from a laptop
317− #
318− # install as /sbin/laptop_mode
319− #
320− # Contributors to this script: Kiko Piris
321− # Bart Samwel
322− # Micha Feigin
323− # Andrew Morton
324− # Herve Eychenne
325− # Dax Kelson
326− #
327− # Original Linux 2.4 version by: Jens Axboe
328−
329− #############################################################################
330−
331− # Source config
332− if [ -f /etc/default/laptop-mode ] ; then
333− # Debian
334− . /etc/default/laptop-mode
335− elif [ -f /etc/sysconfig/laptop-mode ] ; then
336− # Others
337− . /etc/sysconfig/laptop-mode
338− fi
339−
340− # Don't raise an error if the config file is incomplete
341− # set defaults instead:
342−
343− # Maximum time, in seconds, of hard drive spindown time that you are
344− # comfortable with. Worst case, it's possible that you could lose this
345− # amount of work if your battery fails you while in laptop mode.
346− MAX_AGE=${MAX_AGE:-'600'}
347−
348− # Read-ahead, in kilobytes
349− READAHEAD=${READAHEAD:-'4096'}
350−
351− # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
352− DO_REMOUNTS=${DO_REMOUNTS:-'1'}
353−
354− # And shall we add the "noatime" option to that as well? (1=yes)
355− DO_REMOUNT_NOATIME=${DO_REMOUNT_NOATIME:-'1'}
356−
357− # Shall we adjust the idle timeout on a hard drive?
358− DO_HD=${DO_HD:-'1'}
359−
360− # Adjust idle timeout on which hard drive?
361− HD="${HD:-'/dev/hda'}"
362−
363− # spindown time for HD (hdparm -S values)
364− AC_HD=${AC_HD:-'244'}
365− BATT_HD=${BATT_HD:-'4'}
366−
367− # Dirty synchronous ratio. At this percentage of dirty pages the process which
368− # calls write() does its own writeback
369− DIRTY_RATIO=${DIRTY_RATIO:-'40'}
370−
371− # cpu frequency scaling
372− # See Documentation/admin-guide/pm/cpufreq.rst for more info
373− DO_CPU=${CPU_MANAGE:-'0'}
374− CPU_MAXFREQ=${CPU_MAXFREQ:-'slowest'}
375−
376− #
377− # Allowed dirty background ratio, in percent. Once DIRTY_RATIO has been
378− # exceeded, the kernel will wake flusher threads which will then reduce the
379− # amount of dirty memory to dirty_background_ratio. Set this nice and low,
380− # so once some writeout has commenced, we do a lot of it.
381− #
382− DIRTY_BACKGROUND_RATIO=${DIRTY_BACKGROUND_RATIO:-'5'}
383−
384− # kernel default dirty buffer age
385− DEF_AGE=${DEF_AGE:-'30'}
386− DEF_UPDATE=${DEF_UPDATE:-'5'}
387− DEF_DIRTY_BACKGROUND_RATIO=${DEF_DIRTY_BACKGROUND_RATIO:-'10'}
388− DEF_DIRTY_RATIO=${DEF_DIRTY_RATIO:-'40'}
389− DEF_XFS_AGE_BUFFER=${DEF_XFS_AGE_BUFFER:-'15'}
390− DEF_XFS_SYNC_INTERVAL=${DEF_XFS_SYNC_INTERVAL:-'30'}
391− DEF_XFS_BUFD_INTERVAL=${DEF_XFS_BUFD_INTERVAL:-'1'}
392−
393− # This must be adjusted manually to the value of HZ in the running kernel
394− # on 2.4, until the XFS people change their 2.4 external interfaces to work in
395− # centisecs. This can be automated, but it's a work in progress that still needs
396− # some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external
397− # interfaces, and that is currently always set to 100. So you don't need to
398− # change this on 2.6.
399− XFS_HZ=${XFS_HZ:-'100'}
400−
401− #############################################################################
402−
403− KLEVEL="$(uname -r |
404− {
405− IFS='.' read a b c
406− echo $a.$b
407− }
408− )"
409− case "$KLEVEL" in
410− "2.4"|"2.6")
411− ;;
412− *)
413− echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2
414− exit 1
415− ;;
416− esac
417−
418− if [ ! -e /proc/sys/vm/laptop_mode ] ; then
419− echo "Kernel is not patched with laptop_mode patch." >&2
420− exit 1
421− fi
422−
423− if [ ! -w /proc/sys/vm/laptop_mode ] ; then
424− echo "You do not have enough privileges to enable laptop_mode." >&2
425− exit 1
426− fi
427−
428− # Remove an option (the first parameter) of the form option=<number> from
429− # a mount options string (the rest of the parameters).
430− parse_mount_opts () {
431− OPT="$1"
432− shift
433− echo ",$*," | sed \
434− -e 's/,'"$OPT"'=[0-9]*,/,/g' \
435− -e 's/,,*/,/g' \
436− -e 's/^,//' \
437− -e 's/,$//'
438− }
439−
440− # Remove an option (the first parameter) without any arguments from
441− # a mount option string (the rest of the parameters).
442− parse_nonumber_mount_opts () {
443− OPT="$1"
444− shift
445− echo ",$*," | sed \
446− -e 's/,'"$OPT"',/,/g' \
447− -e 's/,,*/,/g' \
448− -e 's/^,//' \
449− -e 's/,$//'
450− }
451−
452− # Find out the state of a yes/no option (e.g. "atime"/"noatime") in
453− # fstab for a given filesystem, and use this state to replace the
454− # value of the option in another mount options string. The device
455− # is the first argument, the option name the second, and the default
456− # value the third. The remainder is the mount options string.
457− #
458− # Example:
459− # parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime
460− #
461− # If fstab contains, say, "rw" for this filesystem, then the result
462− # will be "defaults,atime".
463− parse_yesno_opts_wfstab () {
464− L_DEV="$1"
465− OPT="$2"
466− DEF_OPT="$3"
467− shift 3
468− L_OPTS="$*"
469− PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)"
470− PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)"
471− # Watch for a default atime in fstab
472− FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
473− if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then
474− # option specified in fstab: extract the value and use it
475− if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then
476− echo "$PARSEDOPTS1,no$OPT"
477− else
478− # no$OPT not found -- so we must have $OPT.
479− echo "$PARSEDOPTS1,$OPT"
480− fi
481− else
482− # option not specified in fstab -- choose the default.
483− echo "$PARSEDOPTS1,$DEF_OPT"
484− fi
485− }
486−
487− # Find out the state of a numbered option (e.g. "commit=NNN") in
488− # fstab for a given filesystem, and use this state to replace the
489− # value of the option in another mount options string. The device
490− # is the first argument, and the option name the second. The
491− # remainder is the mount options string in which the replacement
492− # must be done.
493− #
494− # Example:
495− # parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7
496− #
497− # If fstab contains, say, "commit=3,rw" for this filesystem, then the
498− # result will be "rw,commit=3".
499− parse_mount_opts_wfstab () {
500− L_DEV="$1"
501− OPT="$2"
502− shift 2
503− L_OPTS="$*"
504− PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)"
505− # Watch for a default commit in fstab
506− FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
507− if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then
508− # option specified in fstab: extract the value, and use it
509− echo -n "$PARSEDOPTS1,$OPT="
510− echo ",$FSTAB_OPTS," | sed \
511− -e 's/.*,'"$OPT"'=//' \
512− -e 's/,.*//'
513− else
514− # option not specified in fstab: set it to 0
515− echo "$PARSEDOPTS1,$OPT=0"
516− fi
517− }
518−
519− deduce_fstype () {
520− MP="$1"
521− # My root filesystem unfortunately has
522− # type "unknown" in /etc/mtab. If we encounter
523− # "unknown", we try to get the type from fstab.
524− cat /etc/fstab |
525− grep -v '^#' |
526− while read FSTAB_DEV FSTAB_MP FSTAB_FST FSTAB_OPTS FSTAB_DUMP FSTAB_DUMP ; do
527− if [ "$FSTAB_MP" = "$MP" ]; then
528− echo $FSTAB_FST
529− exit 0
530− fi
531− done
532− }
533−
534− if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then
535− NOATIME_OPT=",noatime"
536− fi
537−
538− case "$1" in
539− start)
540− AGE=$((100*$MAX_AGE))
541− XFS_AGE=$(($XFS_HZ*$MAX_AGE))
542− echo -n "Starting laptop_mode"
543−
544− if [ -d /proc/sys/vm/pagebuf ] ; then
545− # (For 2.4 and early 2.6.)
546− # This only needs to be set, not reset -- it is only used when
547− # laptop mode is enabled.
548− echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age
549− echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
550− elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
551− # (A couple of early 2.6 laptop mode patches had these.)
552− # The same goes for these.
553− echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer
554− echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
555− elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then
556− # (2.6.6)
557− # But not for these -- they are also used in normal
558− # operation.
559− echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer
560− echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval
561− elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
562− # (2.6.7 upwards)
563− # And not for these either. These are in centisecs,
564− # not USER_HZ, so we have to use $AGE, not $XFS_AGE.
565− echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs
566− echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs
567− echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs
568− fi
569−
570− case "$KLEVEL" in
571− "2.4")
572− echo 1 > /proc/sys/vm/laptop_mode
573− echo "30 500 0 0 $AGE $AGE 60 20 0" > /proc/sys/vm/bdflush
574− ;;
575− "2.6")
576− echo 5 > /proc/sys/vm/laptop_mode
577− echo "$AGE" > /proc/sys/vm/dirty_writeback_centisecs
578− echo "$AGE" > /proc/sys/vm/dirty_expire_centisecs
579− echo "$DIRTY_RATIO" > /proc/sys/vm/dirty_ratio
580− echo "$DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio
581− ;;
582− esac
583− if [ $DO_REMOUNTS -eq 1 ]; then
584− cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
585− PARSEDOPTS="$(parse_mount_opts "$OPTS")"
586− if [ "$FST" = 'unknown' ]; then
587− FST=$(deduce_fstype $MP)
588− fi
589− case "$FST" in
590− "ext3")
591− PARSEDOPTS="$(parse_mount_opts commit "$OPTS")"
592− mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT
593− ;;
594− "xfs")
595− mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT
596− ;;
597− esac
598− if [ -b $DEV ] ; then
599− blockdev --setra $(($READAHEAD * 2)) $DEV
600− fi
601− done
602− fi
603− if [ $DO_HD -eq 1 ] ; then
604− for THISHD in $HD ; do
605− /sbin/hdparm -S $BATT_HD $THISHD > /dev/null 2>&1
606− /sbin/hdparm -B 1 $THISHD > /dev/null 2>&1
607− done
608− fi
609− if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
610− if [ $CPU_MAXFREQ = 'slowest' ]; then
611− CPU_MAXFREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
612− fi
613− echo $CPU_MAXFREQ > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
614− fi
615− echo "."
616− ;;
617− stop)
618− U_AGE=$((100*$DEF_UPDATE))
619− B_AGE=$((100*$DEF_AGE))
620− echo -n "Stopping laptop_mode"
621− echo 0 > /proc/sys/vm/laptop_mode
622− if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
623− # These need to be restored, if there are no lm_*.
624− echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer
625− echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/sync_interval
626− elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
627− # These need to be restored as well.
628− echo $((100*$DEF_XFS_AGE_BUFFER)) > /proc/sys/fs/xfs/age_buffer_centisecs
629− echo $((100*$DEF_XFS_SYNC_INTERVAL)) > /proc/sys/fs/xfs/xfssyncd_centisecs
630− echo $((100*$DEF_XFS_BUFD_INTERVAL)) > /proc/sys/fs/xfs/xfsbufd_centisecs
631− fi
632− case "$KLEVEL" in
633− "2.4")
634− echo "30 500 0 0 $U_AGE $B_AGE 60 20 0" > /proc/sys/vm/bdflush
635− ;;
636− "2.6")
637− echo "$U_AGE" > /proc/sys/vm/dirty_writeback_centisecs
638− echo "$B_AGE" > /proc/sys/vm/dirty_expire_centisecs
639− echo "$DEF_DIRTY_RATIO" > /proc/sys/vm/dirty_ratio
640− echo "$DEF_DIRTY_BACKGROUND_RATIO" > /proc/sys/vm/dirty_background_ratio
641− ;;
642− esac
643− if [ $DO_REMOUNTS -eq 1 ] ; then
644− cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
645− # Reset commit and atime options to defaults.
646− if [ "$FST" = 'unknown' ]; then
647− FST=$(deduce_fstype $MP)
648− fi
649− case "$FST" in
650− "ext3")
651− PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)"
652− PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)"
653− mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
654− ;;
655− "xfs")
656− PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)"
657− mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
658− ;;
659− esac
660− if [ -b $DEV ] ; then
661− blockdev --setra 256 $DEV
662− fi
663− done
664− fi
665− if [ $DO_HD -eq 1 ] ; then
666− for THISHD in $HD ; do
667− /sbin/hdparm -S $AC_HD $THISHD > /dev/null 2>&1
668− /sbin/hdparm -B 255 $THISHD > /dev/null 2>&1
669− done
670− fi
671− if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
672− echo `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq` > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
673− fi
674− echo "."
675− ;;
676− *)
677− echo "Usage: $0 {start|stop}" 2>&1
678− exit 1
679− ;;
680−
681− esac
682−
683− exit 0
684−
685−
686−ACPI integration
687−----------------
688−
689−Dax Kelson submitted this so that the ACPI acpid daemon will
690−kick off the laptop_mode script and run hdparm. The part that
691−automatically disables laptop mode when the battery is low was
692−written by Jan Topinski.
693−
694−/etc/acpi/events/ac_adapter::
695−
696− event=ac_adapter
697− action=/etc/acpi/actions/ac.sh %e
698−
699−/etc/acpi/events/battery::
700−
701− event=battery.*
702− action=/etc/acpi/actions/battery.sh %e
703−
704−/etc/acpi/actions/ac.sh::
705−
706− #!/bin/bash
707−
708− # ac on/offline event handler
709−
710− status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/$2/state`
711−
712− case $status in
713− "on-line")
714− /sbin/laptop_mode stop
715− exit 0
716− ;;
717− "off-line")
718− /sbin/laptop_mode start
719− exit 0
720− ;;
721− esac
722−
723−
724−/etc/acpi/actions/battery.sh::
725−
726− #! /bin/bash
727−
728− # Automatically disable laptop mode when the battery almost runs out.
729−
730− BATT_INFO=/proc/acpi/battery/$2/state
731−
732− if [[ -f /proc/sys/vm/laptop_mode ]]
733− then
734− LM=`cat /proc/sys/vm/laptop_mode`
735− if [[ $LM -gt 0 ]]
736− then
737− if [[ -f $BATT_INFO ]]
738− then
739− # Source the config file only now that we know we need
740− if [ -f /etc/default/laptop-mode ] ; then
741− # Debian
742− . /etc/default/laptop-mode
743− elif [ -f /etc/sysconfig/laptop-mode ] ; then
744− # Others
745− . /etc/sysconfig/laptop-mode
746− fi
747− MINIMUM_BATTERY_MINUTES=${MINIMUM_BATTERY_MINUTES:-'10'}
748−
749− ACTION="`cat $BATT_INFO | grep charging | cut -c 26-`"
750− if [[ ACTION -eq "discharging" ]]
751− then
752− PRESENT_RATE=`cat $BATT_INFO | grep "present rate:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `
753− REMAINING=`cat $BATT_INFO | grep "remaining capacity:" | sed "s/.* \([0-9][0-9]* \).*/\1/" `
754− fi
755− if (($REMAINING * 60 / $PRESENT_RATE < $MINIMUM_BATTERY_MINUTES))
756− then
757− /sbin/laptop_mode stop
758− fi
759− else
760− logger -p daemon.warning "You are using laptop mode and your battery interface $BATT_INFO is missing. This may lead to loss of data when the battery runs out. Check kernel ACPI support and /proc/acpi/battery folder, and edit /etc/acpi/battery.sh to set BATT_INFO to the correct path."
761− fi
762− fi
763− fi
764−
765−
766−Monitoring tool
767−---------------
768−
769−Bartek Kania submitted this, it can be used to measure how much time your disk
770−spends spun up/down. See tools/laptop/dslm/dslm.c
Documentation/admin-guide/mm/damon/lru_sort.rst+37−0
@@ -79,6 +79,43 @@ of parametrs except ``enabled`` again. Once the re-reading is done, this · 78 unmodified lines
7979 parameter is set as ``N``. If invalid parameters are found while the
8080 re-reading, DAMON_LRU_SORT will be disabled.
8181
82+active_mem_bp
83+-------------
84+
85+Desired active to [in]active memory ratio in bp (1/10,000).
86+
87+While keeping the caps that set by other quotas, DAMON_LRU_SORT automatically
88+increases and decreases the effective level of the quota aiming the LRU
89+[de]prioritizations of the hot and cold memory resulting in this active to
90+[in]active memory ratio. Value zero means disabling this auto-tuning feature.
91+
92+Disabled by default.
93+
94+Auto-tune monitoring intervals
95+------------------------------
96+
97+If this parameter is set as ``Y``, DAMON_LRU_SORT automatically tunes DAMON's
98+sampling and aggregation intervals. The auto-tuning aims to capture meaningful
99+amount of access events in each DAMON-snapshot, while keeping the sampling
100+interval 5 milliseconds in minimum, and 10 seconds in maximum. Setting this as
101+``N`` disables the auto-tuning.
102+
103+Disabled by default.
104+
105+filter_young_pages
106+------------------
107+
108+Filter [non-]young pages accordingly for LRU [de]prioritizations.
109+
110+If this is set, check page level access (youngness) once again before each
111+LRU [de]prioritization operation. LRU prioritization operation is skipped
112+if the page has not accessed since the last check (not young). LRU
113+deprioritization operation is skipped if the page has accessed since the
114+last check (young). The feature is enabled or disabled if this parameter is
115+set as ``Y`` or ``N``, respectively.
116+
117+Disabled by default.
118+
82119 hot_thres_access_freq
83120 ---------------------
84121
Documentation/admin-guide/mm/damon/usage.rst+14−5
@@ -6,6 +6,11 @@ Detailed Usages · 5 unmodified lines
66
77 DAMON provides below interfaces for different users.
88
9+- *Special-purpose DAMON modules.*
10+ :ref:`This <damon_modules_special_purpose>` is for people who are building,
11+ distributing, and/or administrating the kernel with special-purpose DAMON
12+ usages. Using this, users can use DAMON's major features for the given
13+ purposes in build, boot, or runtime in simple ways.
914 - *DAMON user space tool.*
1015 `This <https://github.com/damonitor/damo>`_ is for privileged people such as
1116 system administrators who want a just-working human-friendly interface.
@@ -87,7 +92,7 @@ comma (","). · 75 unmodified lines
8792 │ │ │ │ │ │ │ │ 0/type,matching,allow,memcg_path,addr_start,addr_end,target_idx,min,max
8893 │ │ │ │ │ │ │ :ref:`dests <damon_sysfs_dests>`/nr_dests
8994 │ │ │ │ │ │ │ │ 0/id,weight
90− │ │ │ │ │ │ │ :ref:`stats <sysfs_schemes_stats>`/nr_tried,sz_tried,nr_applied,sz_applied,sz_ops_filter_passed,qt_exceeds
95+ │ │ │ │ │ │ │ :ref:`stats <sysfs_schemes_stats>`/nr_tried,sz_tried,nr_applied,sz_applied,sz_ops_filter_passed,qt_exceeds,nr_snapshots,max_nr_snapshots
9196 │ │ │ │ │ │ │ :ref:`tried_regions <sysfs_schemes_tried_regions>`/total_bytes
9297 │ │ │ │ │ │ │ │ 0/start,end,nr_accesses,age,sz_filter_passed
9398 │ │ │ │ │ │ │ │ ...
@@ -543,10 +548,14 @@ online analysis or tuning of the schemes. Refer to :ref:`design doc · 449 unmodified lines
543548
544549 The statistics can be retrieved by reading the files under ``stats`` directory
545550 (``nr_tried``, ``sz_tried``, ``nr_applied``, ``sz_applied``,
546−``sz_ops_filter_passed``, and ``qt_exceeds``), respectively. The files are not
547−updated in real time, so you should ask DAMON sysfs interface to update the
548−content of the files for the stats by writing a special keyword,
549−``update_schemes_stats`` to the relevant ``kdamonds/<N>/state`` file.
551+``sz_ops_filter_passed``, ``qt_exceeds``, ``nr_snapshots`` and
552+``max_nr_snapshots``), respectively.
553+
554+The files are not updated in real time by default. Users should ask DAMON
555+sysfs interface to periodically update those using ``refresh_ms``, or do a one
556+time update by writing a special keyword, ``update_schemes_stats`` to the
557+relevant ``kdamonds/<N>/state`` file. Refer to :ref:`kdamond directory
558+<sysfs_kdamond>` for more details.
550559
551560 .. _sysfs_schemes_tried_regions:
552561
Documentation/admin-guide/mm/memory-hotplug.rst+16−6
@@ -603,17 +603,18 @@ ZONE_MOVABLE, especially when fine-tuning zone ratios: · 602 unmodified lines
603603 memory for metadata and page tables in the direct map; having a lot of offline
604604 memory blocks is not a typical case, though.
605605
606−- Memory ballooning without balloon compaction is incompatible with
607− ZONE_MOVABLE. Only some implementations, such as virtio-balloon and
608− pseries CMM, fully support balloon compaction.
606+- Memory ballooning without support for balloon memory migration is incompatible
607+ with ZONE_MOVABLE. Only some implementations, such as virtio-balloon and
608+ pseries CMM, fully support balloon memory migration.
609609
610− Further, the CONFIG_BALLOON_COMPACTION kernel configuration option might be
610+ Further, the CONFIG_BALLOON_MIGRATION kernel configuration option might be
611611 disabled. In that case, balloon inflation will only perform unmovable
612612 allocations and silently create a zone imbalance, usually triggered by
613613 inflation requests from the hypervisor.
614614
615−- Gigantic pages are unmovable, resulting in user space consuming a
616− lot of unmovable memory.
615+- Gigantic pages are unmovable when an architecture does not support
616+ huge page migration and/or the ``movable_gigantic_pages`` sysctl is false.
617+ See Documentation/admin-guide/sysctl/vm.rst for more info on this sysctl.
617618
618619 - Huge pages are unmovable when an architectures does not support huge
619620 page migration, resulting in a similar issue as with gigantic pages.
@@ -672,6 +673,15 @@ block might fail: · 52 unmodified lines
672673 - Concurrent activity that operates on the same physical memory area, such as
673674 allocating gigantic pages, can result in temporary offlining failures.
674675
676+- When an admin sets the ``movable_gigantic_pages`` sysctl to true, gigantic
677+ pages are allowed in ZONE_MOVABLE. This only allows migratable gigantic
678+ pages to be allocated; however, if there are no eligible destination gigantic
679+ pages at offline, the offlining operation will fail.
680+
681+ Users leveraging ``movable_gigantic_pages`` should weigh the value of
682+ ZONE_MOVABLE for increasing the reliability of gigantic page allocation
683+ against the potential loss of hot-unplug reliability.
684+
675685 - Out of memory when dissolving huge pages, especially when HugeTLB Vmemmap
676686 Optimization (HVO) is enabled.
677687
Documentation/admin-guide/sysctl/vm.rst+28−8
@@ -41,7 +41,6 @@ Currently, these files are in /proc/sys/vm: · 40 unmodified lines
4141 - extfrag_threshold
4242 - highmem_is_dirtyable
4343 - hugetlb_shm_group
44−- laptop_mode
4544 - legacy_va_layout
4645 - lowmem_reserve_ratio
4746 - max_map_count
@@ -54,6 +53,7 @@ Currently, these files are in /proc/sys/vm: · 6 unmodified lines
5453 - mmap_min_addr
5554 - mmap_rnd_bits
5655 - mmap_rnd_compat_bits
56+- movable_gigantic_pages
5757 - nr_hugepages
5858 - nr_hugepages_mempolicy
5959 - nr_overcommit_hugepages
@@ -365,13 +365,6 @@ hugetlb_shm_group contains group id that is allowed to create SysV · 305 unmodified lines
365365 shared memory segment using hugetlb page.
366366
367367
368−laptop_mode
369−===========
370−
371−laptop_mode is a knob that controls "laptop mode". All the things that are
372−controlled by this knob are discussed in Documentation/admin-guide/laptops/laptop-mode.rst.
373−
374−
375368 legacy_va_layout
376369 ================
377370
@@ -630,6 +623,33 @@ This value can be changed after boot using the · 252 unmodified lines
630623 /proc/sys/vm/mmap_rnd_compat_bits tunable
631624
632625
626+movable_gigantic_pages
627+======================
628+
629+This parameter controls whether gigantic pages may be allocated from
630+ZONE_MOVABLE. If set to non-zero, gigantic pages can be allocated
631+from ZONE_MOVABLE. ZONE_MOVABLE memory may be created via the kernel
632+boot parameter `kernelcore` or via memory hotplug as discussed in
633+Documentation/admin-guide/mm/memory-hotplug.rst.
634+
635+Support may depend on specific architecture.
636+
637+Note that using ZONE_MOVABLE gigantic pages make memory hotremove unreliable.
638+
639+Memory hot-remove operations will block indefinitely until the admin reserves
640+sufficient gigantic pages to service migration requests associated with the
641+memory offlining process. As HugeTLB gigantic page reservation is a manual
642+process (via `nodeN/hugepages/.../nr_hugepages` interfaces) this may not be
643+obvious when just attempting to offline a block of memory.
644+
645+Additionally, as multiple gigantic pages may be reserved on a single block,
646+it may appear that gigantic pages are available for migration when in reality
647+they are in the process of being removed. For example if `memoryN` contains
648+two gigantic pages, one reserved and one allocated, and an admin attempts to
649+offline that block, this operations may hang indefinitely unless another
650+reserved gigantic page is available on another block `memoryM`.
651+
652+
633653 nr_hugepages
634654 ============
635655
Documentation/core-api/mm-api.rst+1−1
@@ -130,5 +130,5 @@ More Memory Management Functions · 129 unmodified lines
130130 .. kernel-doc:: mm/vmscan.c
131131 .. kernel-doc:: mm/memory_hotplug.c
132132 .. kernel-doc:: mm/mmu_notifier.c
133−.. kernel-doc:: mm/balloon_compaction.c
133+.. kernel-doc:: mm/balloon.c
134134 .. kernel-doc:: mm/huge_memory.c
Documentation/driver-api/cxl/linux/early-boot.rst+1−1
@@ -125,7 +125,7 @@ The contiguous memory allocator (CMA) enables reservation of contiguous memory · 124 unmodified lines
125125 regions on NUMA nodes during early boot. However, CMA cannot reserve memory
126126 on NUMA nodes that are not online during early boot. ::
127127
128− void __init hugetlb_cma_reserve(int order) {
128+ void __init hugetlb_cma_reserve(void) {
129129 if (!node_online(nid))
130130 /* do not allow reservations */
131131 }
Documentation/mm/damon/design.rst+29−3
@@ -585,6 +585,10 @@ mechanism tries to make ``current_value`` of ``target_metric`` be same to · 584 unmodified lines
585585 specific NUMA node, in bp (1/10,000).
586586 - ``node_memcg_free_bp``: Specific cgroup's node unused memory ratio for a
587587 specific NUMA node, in bp (1/10,000).
588+- ``active_mem_bp``: Active to active + inactive (LRU) memory size ratio in bp
589+ (1/10,000).
590+- ``inactive_mem_bp``: Inactive to active + inactive (LRU) memory size ratio in
591+ bp (1/10,000).
588592
589593 ``nid`` is optionally required for only ``node_mem_used_bp``,
590594 ``node_mem_free_bp``, ``node_memcg_used_bp`` and ``node_memcg_free_bp`` to
@@ -718,6 +722,9 @@ scheme's execution. · 127 unmodified lines
718722 - ``nr_applied``: Total number of regions that the scheme is applied.
719723 - ``sz_applied``: Total size of regions that the scheme is applied.
720724 - ``qt_exceeds``: Total number of times the quota of the scheme has exceeded.
725+- ``nr_snapshots``: Total number of DAMON snapshots that the scheme is tried to
726+ be applied.
727+- ``max_nr_snapshots``: Upper limit of ``nr_snapshots``.
721728
722729 "A scheme is tried to be applied to a region" means DAMOS core logic determined
723730 the region is eligible to apply the scheme's :ref:`action
@@ -739,6 +746,10 @@ to exclude anonymous pages and the region has only anonymous pages, or if the · 15 unmodified lines
739746 action is ``pageout`` while all pages of the region are unreclaimable, applying
740747 the action to the region will fail.
741748
749+Unlike normal stats, ``max_nr_snapshots`` is set by users. If it is set as
750+non-zero and ``nr_snapshots`` be same to or greater than ``nr_snapshots``, the
751+scheme is deactivated.
752+
742753 To know how user-space can read the stats via :ref:`DAMON sysfs interface
743754 <sysfs_interface>`, refer to :ref:s`stats <sysfs_stats>` part of the
744755 documentation.
@@ -798,14 +809,16 @@ The ABIs are designed to be used for user space applications development, · 53 unmodified lines
798809 rather than human beings' fingers. Human users are recommended to use such
799810 user space tools. One such Python-written user space tool is available at
800811 Github (https://github.com/damonitor/damo), Pypi
801−(https://pypistats.org/packages/damo), and Fedora
802−(https://packages.fedoraproject.org/pkgs/python-damo/damo/).
812+(https://pypistats.org/packages/damo), and multiple distros
813+(https://repology.org/project/damo/versions).
803814
804815 Currently, one module for this type, namely 'DAMON sysfs interface' is
805816 available. Please refer to the ABI :ref:`doc <sysfs_interface>` for details of
806817 the interfaces.
807818
808819
820+.. _damon_modules_special_purpose:
821+
809822 Special-Purpose Access-aware Kernel Modules
810823 -------------------------------------------
811824
@@ -823,5 +836,18 @@ To support such cases, yet more DAMON API user kernel modules that provide more · 11 unmodified lines
823836 simple and optimized user space interfaces are available. Currently, two
824837 modules for proactive reclamation and LRU lists manipulation are provided. For
825838 more detail, please read the usage documents for those
826−(:doc:`/admin-guide/mm/damon/reclaim` and
839+(:doc:`/admin-guide/mm/damon/stat`, :doc:`/admin-guide/mm/damon/reclaim` and
827840 :doc:`/admin-guide/mm/damon/lru_sort`).
841+
842+
843+Sample DAMON Modules
844+--------------------
845+
846+DAMON modules that provides example DAMON kernel API usages.
847+
848+kernel programmers can build their own special or general purpose DAMON modules
849+using DAMON kernel API. To help them easily understand how DAMON kernel API
850+can be used, a few sample modules are provided under ``samples/damon/`` of the
851+linux source tree. Please note that these modules are not developed for being
852+used on real products, but only for showing how DAMON kernel API can be used in
853+simple ways.
Documentation/mm/damon/index.rst+9−22
@@ -4,28 +4,15 @@ · 3 unmodified lines
44 DAMON: Data Access MONitoring and Access-aware System Operations
55 ================================================================
66
7−DAMON is a Linux kernel subsystem that provides a framework for data access
8−monitoring and the monitoring results based system operations. The core
9−monitoring :ref:`mechanisms <damon_design_monitoring>` of DAMON make it
10−
11− - *accurate* (the monitoring output is useful enough for DRAM level memory
12− management; It might not appropriate for CPU Cache levels, though),
13− - *light-weight* (the monitoring overhead is low enough to be applied online),
14− and
15− - *scalable* (the upper-bound of the overhead is in constant range regardless
16− of the size of target workloads).
17−
18−Using this framework, therefore, the kernel can operate system in an
19−access-aware fashion. Because the features are also exposed to the :doc:`user
20−space </admin-guide/mm/damon/index>`, users who have special information about
21−their workloads can write personalized applications for better understanding
22−and optimizations of their workloads and systems.
23−
24−For easier development of such systems, DAMON provides a feature called
25−:ref:`DAMOS <damon_design_damos>` (DAMon-based Operation Schemes) in addition
26−to the monitoring. Using the feature, DAMON users in both kernel and :doc:`user
27−spaces </admin-guide/mm/damon/index>` can do access-aware system operations
28−with no code but simple configurations.
7+DAMON is a Linux kernel subsystem for efficient :ref:`data access monitoring
8+<damon_design_monitoring>` and :ref:`access-aware system operations
9+<damon_design_damos>`. It is designed for being
10+
11+ - *accurate* (for DRAM level memory management),
12+ - *light-weight* (for production online usages),
13+ - *scalable* (in terms of memory size),
14+ - *tunable* (for flexible usages), and
15+ - *autoamted* (for production operation without manual tunings).
2916
3017 .. toctree::
3118 :maxdepth: 2
Documentation/mm/damon/maintainer-profile.rst+3−4
@@ -3,8 +3,8 @@ · 2 unmodified lines
33 DAMON Maintainer Entry Profile
44 ==============================
55
6−The DAMON subsystem covers the files that are listed in 'DATA ACCESS MONITOR'
7−section of 'MAINTAINERS' file.
6+The DAMON subsystem covers the files that are listed in 'DAMON' section of
7+'MAINTAINERS' file.
88
99 The mailing lists for the subsystem are damon@lists.linux.dev and
1010 linux-mm@kvack.org. Patches should be made against the `mm-new tree
@@ -48,8 +48,7 @@ Further doing below and putting the results will be helpful. · 37 unmodified lines
4848 - Run `damon-tests/corr
4949 <https://github.com/damonitor/damon-tests/tree/master/corr>`_ for normal
5050 changes.
51−- Run `damon-tests/perf
52− <https://github.com/damonitor/damon-tests/tree/master/perf>`_ for performance
51+- Measure impacts on benchmarks or real world workloads for performance
5352 changes.
5453
5554 Key cycle dates
Documentation/mm/memory-model.rst+0−3
@@ -97,9 +97,6 @@ sections: · 96 unmodified lines
9797 `mem_section` objects and the number of rows is calculated to fit
9898 all the memory sections.
9999
100−The architecture setup code should call sparse_init() to
101−initialize the memory sections and the memory maps.
102−
103100 With SPARSEMEM there are two possible ways to convert a PFN to the
104101 corresponding `struct page` - a "classic sparse" and "sparse
105102 vmemmap". The selection is made at build time and it is determined by
Documentation/translations/zh_CN/mm/memory-model.rst+0−2
@@ -83,8 +83,6 @@ SPARSEMEM模型将物理内存显示为一个部分的集合。一个区段用me · 82 unmodified lines
8383 每一行包含价值 `PAGE_SIZE` 的 `mem_section` 对象,行数的计算是为了适应所有的
8484 内存区。
8585
86−架构设置代码应该调用sparse_init()来初始化内存区和内存映射。
87−
8886 通过SPARSEMEM,有两种可能的方式将PFN转换为相应的 `struct page` --"classic sparse"和
8987 "sparse vmemmap"。选择是在构建时进行的,它由 `CONFIG_SPARSEMEM_VMEMMAP` 的
9088 值决定。
MAINTAINERS+11−3
@@ -16583,6 +16583,17 @@ T: quilt git://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new · 16582 unmodified lines
1658316583 F: mm/
1658416584 F: tools/mm/
1658516585
16586+MEMORY MANAGEMENT - BALLOON
16587+M: Andrew Morton <akpm@linux-foundation.org>
16588+M: David Hildenbrand <david@kernel.org>
16589+L: linux-mm@kvack.org
16590+L: virtualization@lists.linux.dev
16591+S: Maintained
16592+W: http://www.linux-mm.org
16593+T: git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
16594+F: include/linux/balloon.h
16595+F: mm/balloon.c
16596+
1658616597 MEMORY MANAGEMENT - CORE
1658716598 M: Andrew Morton <akpm@linux-foundation.org>
1658816599 M: David Hildenbrand <david@kernel.org>
@@ -16810,7 +16821,6 @@ R: Shakeel Butt <shakeel.butt@linux.dev> · 221 unmodified lines
1681016821 R: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
1681116822 L: linux-mm@kvack.org
1681216823 S: Maintained
16813−F: mm/pt_reclaim.c
1681416824 F: mm/vmscan.c
1681516825 F: mm/workingset.c
1681616826
@@ -27776,9 +27786,7 @@ M: David Hildenbrand <david@kernel.org> · 10959 unmodified lines
2777627786 L: virtualization@lists.linux.dev
2777727787 S: Maintained
2777827788 F: drivers/virtio/virtio_balloon.c
27779−F: include/linux/balloon_compaction.h
2778027789 F: include/uapi/linux/virtio_balloon.h
27781−F: mm/balloon_compaction.c
2778227790
2778327791 VIRTIO BLOCK AND SCSI DRIVERS
2778427792 M: "Michael S. Tsirkin" <mst@redhat.com>
arch/alpha/Kconfig+1−0
@@ -38,6 +38,7 @@ config ALPHA · 37 unmodified lines
3838 select OLD_SIGSUSPEND
3939 select CPU_NO_EFFICIENT_FFS if !ALPHA_EV67
4040 select MMU_GATHER_NO_RANGE
41+ select MMU_GATHER_RCU_TABLE_FREE
4142 select SPARSEMEM_EXTREME if SPARSEMEM
4243 select ZONE_DMA
4344 help
arch/alpha/include/asm/page.h+0−1
@@ -11,7 +11,6 @@ · 10 unmodified lines
1111 #define STRICT_MM_TYPECHECKS
1212
1313 extern void clear_page(void *page);
14−#define clear_user_page(page, vaddr, pg) clear_page(page)
1514
1615 #define vma_alloc_zeroed_movable_folio(vma, vaddr) \
1716 vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma, vaddr)
arch/alpha/include/asm/tlb.h+3−3
@@ -4,7 +4,7 @@ · 3 unmodified lines
44
55 #include <asm-generic/tlb.h>
66
7−#define __pte_free_tlb(tlb, pte, address) pte_free((tlb)->mm, pte)
8−#define __pmd_free_tlb(tlb, pmd, address) pmd_free((tlb)->mm, pmd)
9−
7+#define __pte_free_tlb(tlb, pte, address) tlb_remove_ptdesc((tlb), page_ptdesc(pte))
8+#define __pmd_free_tlb(tlb, pmd, address) tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd))
9+
1010 #endif
arch/alpha/kernel/setup.c+0−1
@@ -607,7 +607,6 @@ setup_arch(char **cmdline_p) · 606 unmodified lines
607607 /* Find our memory. */
608608 setup_memory(kernel_end);
609609 memblock_set_bottom_up(true);
610− sparse_init();
611610
612611 /* First guess at cpu cache sizes. Do this before init_arch. */
613612 determine_cpu_caches(cpu->type);
arch/alpha/mm/init.c+7−9
@@ -208,12 +208,8 @@ callback_init(void * kernel_end) · 207 unmodified lines
208208 return kernel_end;
209209 }
210210
211−/*
212− * paging_init() sets up the memory map.
213− */
214−void __init paging_init(void)
211+void __init arch_zone_limits_init(unsigned long *max_zone_pfn)
215212 {
216− unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, };
217213 unsigned long dma_pfn;
218214
219215 dma_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
@@ -221,11 +217,13 @@ void __init paging_init(void) · 1 unmodified line
221217
222218 max_zone_pfn[ZONE_DMA] = dma_pfn;
223219 max_zone_pfn[ZONE_NORMAL] = max_pfn;
220+}
224221
225− /* Initialize mem_map[]. */
226− free_area_init(max_zone_pfn);
227−
228− /* Initialize the kernel's ZERO_PGE. */
222+/*
223+ * paging_init() initializes the kernel's ZERO_PGE.
224+ */
225+void __init paging_init(void)
226+{
229227 memset(absolute_pointer(ZERO_PGE), 0, PAGE_SIZE);
230228 }
231229
arch/arc/include/asm/page.h+2−0
@@ -32,6 +32,8 @@ struct page; · 31 unmodified lines
3232
3333 void copy_user_highpage(struct page *to, struct page *from,
3434 unsigned long u_vaddr, struct vm_area_struct *vma);
35+
36+#define clear_user_page clear_user_page
3537 void clear_user_page(void *to, unsigned long u_vaddr, struct page *page);
3638
3739 typedef struct {
arch/arc/mm/init.c+19−18
@@ -75,6 +75,25 @@ void __init early_init_dt_add_memory_arch(u64 base, u64 size) · 74 unmodified lines
7575 base, TO_MB(size), !in_use ? "Not used":"");
7676 }
7777
78+void __init arch_zone_limits_init(unsigned long *max_zone_pfn)
79+{
80+ /*----------------- node/zones setup --------------------------*/
81+ max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
82+
83+#ifdef CONFIG_HIGHMEM
84+ /*
85+ * max_high_pfn should be ok here for both HIGHMEM and HIGHMEM+PAE.
86+ * For HIGHMEM without PAE max_high_pfn should be less than
87+ * min_low_pfn to guarantee that these two regions don't overlap.
88+ * For PAE case highmem is greater than lowmem, so it is natural
89+ * to use max_high_pfn.
90+ *
91+ * In both cases, holes should be handled by pfn_valid().
92+ */
93+ max_zone_pfn[ZONE_HIGHMEM] = max_high_pfn;
94+#endif
95+}
96+
7897 /*
7998 * First memory setup routine called from setup_arch()
8099 * 1. setup swapper's mm @init_mm
@@ -83,8 +102,6 @@ void __init early_init_dt_add_memory_arch(u64 base, u64 size) · 2 unmodified lines
83102 */
84103 void __init setup_arch_memory(void)
85104 {
86− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
87−
88105 setup_initial_init_mm(_text, _etext, _edata, _end);
89106
90107 /* first page of system - kernel .vector starts here */
@@ -122,9 +139,6 @@ void __init setup_arch_memory(void) · 31 unmodified lines
122139
123140 memblock_dump_all();
124141
125− /*----------------- node/zones setup --------------------------*/
126− max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
127−
128142 #ifdef CONFIG_HIGHMEM
129143 /*
130144 * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
@@ -139,22 +153,9 @@ void __init setup_arch_memory(void) · 8 unmodified lines
139153 min_high_pfn = PFN_DOWN(high_mem_start);
140154 max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
141155
142− /*
143− * max_high_pfn should be ok here for both HIGHMEM and HIGHMEM+PAE.
144− * For HIGHMEM without PAE max_high_pfn should be less than
145− * min_low_pfn to guarantee that these two regions don't overlap.
146− * For PAE case highmem is greater than lowmem, so it is natural
147− * to use max_high_pfn.
148− *
149− * In both cases, holes should be handled by pfn_valid().
150− */
151− max_zone_pfn[ZONE_HIGHMEM] = max_high_pfn;
152−
153156 arch_pfn_offset = min(min_low_pfn, min_high_pfn);
154157 kmap_init();
155158 #endif /* CONFIG_HIGHMEM */
156−
157− free_area_init(max_zone_pfn);
158159 }
159160
160161 void __init arch_mm_preinit(void)
arch/arm/include/asm/page-nommu.h+0−1
@@ -11,7 +11,6 @@ · 10 unmodified lines
1111 #define clear_page(page) memset((page), 0, PAGE_SIZE)
1212 #define copy_page(to,from) memcpy((to), (from), PAGE_SIZE)
1313
14−#define clear_user_page(page, vaddr, pg) clear_page(page)
1514 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
1615
1716 /*
arch/arm/include/asm/pgtable.h+2−2
@@ -15,8 +15,8 @@ · 14 unmodified lines
1515 * ZERO_PAGE is a global shared page that is always zero: used
1616 * for zero-mapped memory areas etc..
1717 */
18−extern struct page *empty_zero_page;
19−#define ZERO_PAGE(vaddr) (empty_zero_page)
18+extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
19+#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
2020 #endif
2121
2222 #include <asm-generic/pgtable-nopud.h>
arch/arm/mm/init.c+4−21
@@ -107,19 +107,15 @@ void __init setup_dma_zone(const struct machine_desc *mdesc) · 106 unmodified lines
107107 #endif
108108 }
109109
110−static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
111− unsigned long max_high)
110+void __init arch_zone_limits_init(unsigned long *max_zone_pfn)
112111 {
113− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
114−
115112 #ifdef CONFIG_ZONE_DMA
116− max_zone_pfn[ZONE_DMA] = min(arm_dma_pfn_limit, max_low);
113+ max_zone_pfn[ZONE_DMA] = min(arm_dma_pfn_limit, max_low_pfn);
117114 #endif
118− max_zone_pfn[ZONE_NORMAL] = max_low;
115+ max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
119116 #ifdef CONFIG_HIGHMEM
120− max_zone_pfn[ZONE_HIGHMEM] = max_high;
117+ max_zone_pfn[ZONE_HIGHMEM] = max_pfn;
121118 #endif
122− free_area_init(max_zone_pfn);
123119 }
124120
125121 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
@@ -211,19 +207,6 @@ void __init bootmem_init(void) · 85 unmodified lines
211207
212208 early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
213209 (phys_addr_t)max_low_pfn << PAGE_SHIFT);
214−
215− /*
216− * sparse_init() tries to allocate memory from memblock, so must be
217− * done after the fixed reservations
218− */
219− sparse_init();
220−
221− /*
222− * Now free the memory - free_area_init needs
223− * the sparse mem_map arrays initialized by sparse_init()
224− * for memmap_init_zone(), otherwise all PFNs are invalid.
225− */
226− zone_sizes_init(min_low_pfn, max_low_pfn, max_pfn);
227210 }
228211
229212 /*
arch/arm/mm/mmu.c+1−9
@@ -45,7 +45,7 @@ extern unsigned long __atags_pointer; · 44 unmodified lines
4545 * empty_zero_page is a special page that is used for
4646 * zero-initialized data and COW.
4747 */
48−struct page *empty_zero_page;
48+unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
4949 EXPORT_SYMBOL(empty_zero_page);
5050
5151 /*
@@ -1754,8 +1754,6 @@ static void __init early_fixmap_shutdown(void) · 1702 unmodified lines
17541754 */
17551755 void __init paging_init(const struct machine_desc *mdesc)
17561756 {
1757− void *zero_page;
1758−
17591757 #ifdef CONFIG_XIP_KERNEL
17601758 /* Store the kernel RW RAM region start/end in these variables */
17611759 kernel_sec_start = CONFIG_PHYS_OFFSET & SECTION_MASK;
@@ -1781,13 +1779,7 @@ void __init paging_init(const struct machine_desc *mdesc) · 19 unmodified lines
17811779
17821780 top_pmd = pmd_off_k(0xffff0000);
17831781
1784− /* allocate the zero page. */
1785− zero_page = early_alloc(PAGE_SIZE);
1786−
17871782 bootmem_init();
1788−
1789− empty_zero_page = virt_to_page(zero_page);
1790− __flush_dcache_folio(NULL, page_folio(empty_zero_page));
17911783 }
17921784
17931785 void __init early_mm_init(const struct machine_desc *mdesc)
arch/arm/mm/nommu.c+1−9
@@ -31,7 +31,7 @@ unsigned long vectors_base; · 30 unmodified lines
3131 * empty_zero_page is a special page that is used for
3232 * zero-initialized data and COW.
3333 */
34−struct page *empty_zero_page;
34+unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
3535 EXPORT_SYMBOL(empty_zero_page);
3636
3737 #ifdef CONFIG_ARM_MPU
@@ -156,18 +156,10 @@ void __init adjust_lowmem_bounds(void) · 118 unmodified lines
156156 */
157157 void __init paging_init(const struct machine_desc *mdesc)
158158 {
159− void *zero_page;
160−
161159 early_trap_init((void *)vectors_base);
162160 mpu_setup();
163161
164− /* allocate the zero page. */
165− zero_page = (void *)memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
166−
167162 bootmem_init();
168−
169− empty_zero_page = virt_to_page(zero_page);
170− flush_dcache_page(empty_zero_page);
171163 }
172164
173165 /*
arch/arm64/Kconfig+1−0
@@ -35,6 +35,7 @@ config ARM64 · 34 unmodified lines
3535 select ARCH_HAS_KCOV
3636 select ARCH_HAS_KERNEL_FPU_SUPPORT if KERNEL_MODE_NEON
3737 select ARCH_HAS_KEEPINITRD
38+ select ARCH_HAS_LAZY_MMU_MODE
3839 select ARCH_HAS_MEMBARRIER_SYNC_CORE
3940 select ARCH_HAS_MEM_ENCRYPT
4041 select ARCH_SUPPORTS_MSEAL_SYSTEM_MAPPINGS
arch/arm64/include/asm/hugetlb.h+0−2
@@ -56,8 +56,6 @@ extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr, · 55 unmodified lines
5656 #define __HAVE_ARCH_HUGE_PTEP_GET
5757 extern pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
5858
59−void __init arm64_hugetlb_cma_reserve(void);
60−
6159 #define huge_ptep_modify_prot_start huge_ptep_modify_prot_start
6260 extern pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
6361 unsigned long addr, pte_t *ptep);
arch/arm64/include/asm/page.h+0−1
@@ -36,7 +36,6 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, · 35 unmodified lines
3636 bool tag_clear_highpages(struct page *to, int numpages);
3737 #define __HAVE_ARCH_TAG_CLEAR_HIGHPAGES
3838
39−#define clear_user_page(page, vaddr, pg) clear_page(page)
4039 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
4140
4241 typedef struct page *pgtable_t;
arch/arm64/include/asm/pgtable.h+26−61
@@ -62,61 +62,26 @@ static inline void emit_pte_barriers(void) · 61 unmodified lines
6262
6363 static inline void queue_pte_barriers(void)
6464 {
65− unsigned long flags;
66−
67− if (in_interrupt()) {
68− emit_pte_barriers();
69− return;
70− }
71−
72− flags = read_thread_flags();
73−
74− if (flags & BIT(TIF_LAZY_MMU)) {
65+ if (is_lazy_mmu_mode_active()) {
7566 /* Avoid the atomic op if already set. */
76− if (!(flags & BIT(TIF_LAZY_MMU_PENDING)))
67+ if (!test_thread_flag(TIF_LAZY_MMU_PENDING))
7768 set_thread_flag(TIF_LAZY_MMU_PENDING);
7869 } else {
7970 emit_pte_barriers();
8071 }
8172 }
8273
83−#define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
84−static inline void arch_enter_lazy_mmu_mode(void)
85−{
86− /*
87− * lazy_mmu_mode is not supposed to permit nesting. But in practice this
88− * does happen with CONFIG_DEBUG_PAGEALLOC, where a page allocation
89− * inside a lazy_mmu_mode section (such as zap_pte_range()) will change
90− * permissions on the linear map with apply_to_page_range(), which
91− * re-enters lazy_mmu_mode. So we tolerate nesting in our
92− * implementation. The first call to arch_leave_lazy_mmu_mode() will
93− * flush and clear the flag such that the remainder of the work in the
94− * outer nest behaves as if outside of lazy mmu mode. This is safe and
95− * keeps tracking simple.
96− */
97−
98− if (in_interrupt())
99− return;
100−
101− set_thread_flag(TIF_LAZY_MMU);
102−}
74+static inline void arch_enter_lazy_mmu_mode(void) {}
10375
10476 static inline void arch_flush_lazy_mmu_mode(void)
10577 {
106− if (in_interrupt())
107− return;
108−
10978 if (test_and_clear_thread_flag(TIF_LAZY_MMU_PENDING))
11079 emit_pte_barriers();
11180 }
11281
11382 static inline void arch_leave_lazy_mmu_mode(void)
11483 {
115− if (in_interrupt())
116− return;
117−
11884 arch_flush_lazy_mmu_mode();
119− clear_thread_flag(TIF_LAZY_MMU);
12085 }
12186
12287 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -708,22 +673,24 @@ static inline pgprot_t pud_pgprot(pud_t pud) · 585 unmodified lines
708673 return __pgprot(pud_val(pfn_pud(pfn, __pgprot(0))) ^ pud_val(pud));
709674 }
710675
711−static inline void __set_ptes_anysz(struct mm_struct *mm, pte_t *ptep,
712− pte_t pte, unsigned int nr,
676+static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,
677+ pte_t *ptep, pte_t pte, unsigned int nr,
713678 unsigned long pgsize)
714679 {
715680 unsigned long stride = pgsize >> PAGE_SHIFT;
716681
717682 switch (pgsize) {
718683 case PAGE_SIZE:
719− page_table_check_ptes_set(mm, ptep, pte, nr);
684+ page_table_check_ptes_set(mm, addr, ptep, pte, nr);
720685 break;
721686 case PMD_SIZE:
722− page_table_check_pmds_set(mm, (pmd_t *)ptep, pte_pmd(pte), nr);
687+ page_table_check_pmds_set(mm, addr, (pmd_t *)ptep,
688+ pte_pmd(pte), nr);
723689 break;
724690 #ifndef __PAGETABLE_PMD_FOLDED
725691 case PUD_SIZE:
726− page_table_check_puds_set(mm, (pud_t *)ptep, pte_pud(pte), nr);
692+ page_table_check_puds_set(mm, addr, (pud_t *)ptep,
693+ pte_pud(pte), nr);
727694 break;
728695 #endif
729696 default:
@@ -744,26 +711,23 @@ static inline void __set_ptes_anysz(struct mm_struct *mm, pte_t *ptep, · 14 unmodified lines
744711 __set_pte_complete(pte);
745712 }
746713
747−static inline void __set_ptes(struct mm_struct *mm,
748− unsigned long __always_unused addr,
714+static inline void __set_ptes(struct mm_struct *mm, unsigned long addr,
749715 pte_t *ptep, pte_t pte, unsigned int nr)
750716 {
751− __set_ptes_anysz(mm, ptep, pte, nr, PAGE_SIZE);
717+ __set_ptes_anysz(mm, addr, ptep, pte, nr, PAGE_SIZE);
752718 }
753719
754−static inline void __set_pmds(struct mm_struct *mm,
755− unsigned long __always_unused addr,
720+static inline void __set_pmds(struct mm_struct *mm, unsigned long addr,
756721 pmd_t *pmdp, pmd_t pmd, unsigned int nr)
757722 {
758− __set_ptes_anysz(mm, (pte_t *)pmdp, pmd_pte(pmd), nr, PMD_SIZE);
723+ __set_ptes_anysz(mm, addr, (pte_t *)pmdp, pmd_pte(pmd), nr, PMD_SIZE);
759724 }
760725 #define set_pmd_at(mm, addr, pmdp, pmd) __set_pmds(mm, addr, pmdp, pmd, 1)
761726
762−static inline void __set_puds(struct mm_struct *mm,
763− unsigned long __always_unused addr,
727+static inline void __set_puds(struct mm_struct *mm, unsigned long addr,
764728 pud_t *pudp, pud_t pud, unsigned int nr)
765729 {
766− __set_ptes_anysz(mm, (pte_t *)pudp, pud_pte(pud), nr, PUD_SIZE);
730+ __set_ptes_anysz(mm, addr, (pte_t *)pudp, pud_pte(pud), nr, PUD_SIZE);
767731 }
768732 #define set_pud_at(mm, addr, pudp, pud) __set_puds(mm, addr, pudp, pud, 1)
769733
@@ -1301,17 +1265,17 @@ static inline int pmdp_set_access_flags(struct vm_area_struct *vma, · 531 unmodified lines
13011265 #endif
13021266
13031267 #ifdef CONFIG_PAGE_TABLE_CHECK
1304−static inline bool pte_user_accessible_page(pte_t pte)
1268+static inline bool pte_user_accessible_page(pte_t pte, unsigned long addr)
13051269 {
13061270 return pte_valid(pte) && (pte_user(pte) || pte_user_exec(pte));
13071271 }
13081272
1309−static inline bool pmd_user_accessible_page(pmd_t pmd)
1273+static inline bool pmd_user_accessible_page(pmd_t pmd, unsigned long addr)
13101274 {
13111275 return pmd_valid(pmd) && !pmd_table(pmd) && (pmd_user(pmd) || pmd_user_exec(pmd));
13121276 }
13131277
1314−static inline bool pud_user_accessible_page(pud_t pud)
1278+static inline bool pud_user_accessible_page(pud_t pud, unsigned long addr)
13151279 {
13161280 return pud_valid(pud) && !pud_table(pud) && (pud_user(pud) || pud_user_exec(pud));
13171281 }
@@ -1370,6 +1334,7 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma, · 52 unmodified lines
13701334 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
13711335
13721336 static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,
1337+ unsigned long address,
13731338 pte_t *ptep,
13741339 unsigned long pgsize)
13751340 {
@@ -1377,14 +1342,14 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm, · 1 unmodified line
13771342
13781343 switch (pgsize) {
13791344 case PAGE_SIZE:
1380− page_table_check_pte_clear(mm, pte);
1345+ page_table_check_pte_clear(mm, address, pte);
13811346 break;
13821347 case PMD_SIZE:
1383− page_table_check_pmd_clear(mm, pte_pmd(pte));
1348+ page_table_check_pmd_clear(mm, address, pte_pmd(pte));
13841349 break;
13851350 #ifndef __PAGETABLE_PMD_FOLDED
13861351 case PUD_SIZE:
1387− page_table_check_pud_clear(mm, pte_pud(pte));
1352+ page_table_check_pud_clear(mm, address, pte_pud(pte));
13881353 break;
13891354 #endif
13901355 default:
@@ -1397,7 +1362,7 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm, · 6 unmodified lines
13971362 static inline pte_t __ptep_get_and_clear(struct mm_struct *mm,
13981363 unsigned long address, pte_t *ptep)
13991364 {
1400− return __ptep_get_and_clear_anysz(mm, ptep, PAGE_SIZE);
1365+ return __ptep_get_and_clear_anysz(mm, address, ptep, PAGE_SIZE);
14011366 }
14021367
14031368 static inline void __clear_full_ptes(struct mm_struct *mm, unsigned long addr,
@@ -1436,7 +1401,7 @@ static inline pte_t __get_and_clear_full_ptes(struct mm_struct *mm, · 32 unmodified lines
14361401 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
14371402 unsigned long address, pmd_t *pmdp)
14381403 {
1439− return pte_pmd(__ptep_get_and_clear_anysz(mm, (pte_t *)pmdp, PMD_SIZE));
1404+ return pte_pmd(__ptep_get_and_clear_anysz(mm, address, (pte_t *)pmdp, PMD_SIZE));
14401405 }
14411406 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
14421407
@@ -1525,7 +1490,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm, · 82 unmodified lines
15251490 static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
15261491 unsigned long address, pmd_t *pmdp, pmd_t pmd)
15271492 {
1528− page_table_check_pmd_set(vma->vm_mm, pmdp, pmd);
1493+ page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd);
15291494 return __pmd(xchg_relaxed(&pmd_val(*pmdp), pmd_val(pmd)));
15301495 }
15311496 #endif
arch/arm64/include/asm/thread_info.h+1−2
@@ -84,8 +84,7 @@ void arch_setup_new_exec(void); · 83 unmodified lines
8484 #define TIF_SME_VL_INHERIT 28 /* Inherit SME vl_onexec across exec */
8585 #define TIF_KERNEL_FPSTATE 29 /* Task is in a kernel mode FPSIMD section */
8686 #define TIF_TSC_SIGSEGV 30 /* SIGSEGV on counter-timer access */
87−#define TIF_LAZY_MMU 31 /* Task in lazy mmu mode */
88−#define TIF_LAZY_MMU_PENDING 32 /* Ops pending for lazy mmu mode exit */
87+#define TIF_LAZY_MMU_PENDING 31 /* Ops pending for lazy mmu mode exit */
8988
9089 #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
9190 #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
arch/arm64/mm/hugetlbpage.c+12−15
@@ -36,16 +36,12 @@ · 35 unmodified lines
3636 * huge pages could still be served from those areas.
3737 */
3838 #ifdef CONFIG_CMA
39−void __init arm64_hugetlb_cma_reserve(void)
39+unsigned int arch_hugetlb_cma_order(void)
4040 {
41− int order;
42−
4341 if (pud_sect_supported())
44− order = PUD_SHIFT - PAGE_SHIFT;
45− else
46− order = CONT_PMD_SHIFT - PAGE_SHIFT;
42+ return PUD_SHIFT - PAGE_SHIFT;
4743
48− hugetlb_cma_reserve(order);
44+ return CONT_PMD_SHIFT - PAGE_SHIFT;
4945 }
5046 #endif /* CONFIG_CMA */
5147
@@ -159,11 +155,12 @@ static pte_t get_clear_contig(struct mm_struct *mm, · 107 unmodified lines
159155 pte_t pte, tmp_pte;
160156 bool present;
161157
162− pte = __ptep_get_and_clear_anysz(mm, ptep, pgsize);
158+ pte = __ptep_get_and_clear_anysz(mm, addr, ptep, pgsize);
163159 present = pte_present(pte);
164160 while (--ncontig) {
165161 ptep++;
166− tmp_pte = __ptep_get_and_clear_anysz(mm, ptep, pgsize);
162+ addr += pgsize;
163+ tmp_pte = __ptep_get_and_clear_anysz(mm, addr, ptep, pgsize);
167164 if (present) {
168165 if (pte_dirty(tmp_pte))
169166 pte = pte_mkdirty(pte);
@@ -207,7 +204,7 @@ static void clear_flush(struct mm_struct *mm, · 37 unmodified lines
207204 unsigned long i, saddr = addr;
208205
209206 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
210− __ptep_get_and_clear_anysz(mm, ptep, pgsize);
207+ __ptep_get_and_clear_anysz(mm, addr, ptep, pgsize);
211208
212209 if (mm == &init_mm)
213210 flush_tlb_kernel_range(saddr, addr);
@@ -225,8 +222,8 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, · 11 unmodified lines
225222 ncontig = num_contig_ptes(sz, &pgsize);
226223
227224 if (!pte_present(pte)) {
228− for (i = 0; i < ncontig; i++, ptep++)
229− __set_ptes_anysz(mm, ptep, pte, 1, pgsize);
225+ for (i = 0; i < ncontig; i++, ptep++, addr += pgsize)
226+ __set_ptes_anysz(mm, addr, ptep, pte, 1, pgsize);
230227 return;
231228 }
232229
@@ -234,7 +231,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, · 1 unmodified line
234231 if (pte_cont(pte) && pte_valid(__ptep_get(ptep)))
235232 clear_flush(mm, addr, ptep, pgsize, ncontig);
236233
237− __set_ptes_anysz(mm, ptep, pte, ncontig, pgsize);
234+ __set_ptes_anysz(mm, addr, ptep, pte, ncontig, pgsize);
238235 }
239236
240237 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
@@ -449,7 +446,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, · 208 unmodified lines
449446 if (pte_young(orig_pte))
450447 pte = pte_mkyoung(pte);
451448
452− __set_ptes_anysz(mm, ptep, pte, ncontig, pgsize);
449+ __set_ptes_anysz(mm, addr, ptep, pte, ncontig, pgsize);
453450 return 1;
454451 }
455452
@@ -473,7 +470,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, · 17 unmodified lines
473470 pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
474471 pte = pte_wrprotect(pte);
475472
476− __set_ptes_anysz(mm, ptep, pte, ncontig, pgsize);
473+ __set_ptes_anysz(mm, addr, ptep, pte, ncontig, pgsize);
477474 }
478475
479476 pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/arm64/mm/init.c+16−23
@@ -118,9 +118,22 @@ static phys_addr_t __init max_zone_phys(phys_addr_t zone_limit) · 117 unmodified lines
118118 return min(zone_limit, memblock_end_of_DRAM() - 1) + 1;
119119 }
120120
121−static void __init zone_sizes_init(void)
121+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
122+{
123+ phys_addr_t __maybe_unused dma32_phys_limit =
124+ max_zone_phys(DMA_BIT_MASK(32));
125+
126+#ifdef CONFIG_ZONE_DMA
127+ max_zone_pfns[ZONE_DMA] = PFN_DOWN(max_zone_phys(zone_dma_limit));
128+#endif
129+#ifdef CONFIG_ZONE_DMA32
130+ max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
131+#endif
132+ max_zone_pfns[ZONE_NORMAL] = max_pfn;
133+}
134+
135+static void __init dma_limits_init(void)
122136 {
123− unsigned long max_zone_pfns[MAX_NR_ZONES] = {0};
124137 phys_addr_t __maybe_unused acpi_zone_dma_limit;
125138 phys_addr_t __maybe_unused dt_zone_dma_limit;
126139 phys_addr_t __maybe_unused dma32_phys_limit =
@@ -139,18 +152,13 @@ static void __init zone_sizes_init(void) · 12 unmodified lines
139152 if (memblock_start_of_DRAM() < U32_MAX)
140153 zone_dma_limit = min(zone_dma_limit, U32_MAX);
141154 arm64_dma_phys_limit = max_zone_phys(zone_dma_limit);
142− max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit);
143155 #endif
144156 #ifdef CONFIG_ZONE_DMA32
145− max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
146157 if (!arm64_dma_phys_limit)
147158 arm64_dma_phys_limit = dma32_phys_limit;
148159 #endif
149160 if (!arm64_dma_phys_limit)
150161 arm64_dma_phys_limit = PHYS_MASK + 1;
151− max_zone_pfns[ZONE_NORMAL] = max_pfn;
152−
153− free_area_init(max_zone_pfns);
154162 }
155163
156164 int pfn_is_map_memory(unsigned long pfn)
@@ -303,23 +311,8 @@ void __init bootmem_init(void) · 146 unmodified lines
303311
304312 arch_numa_init();
305313
306− /*
307− * must be done after arch_numa_init() which calls numa_init() to
308− * initialize node_online_map that gets used in hugetlb_cma_reserve()
309− * while allocating required CMA size across online nodes.
310− */
311−#if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
312− arm64_hugetlb_cma_reserve();
313−#endif
314−
315314 kvm_hyp_reserve();
316−
317− /*
318− * sparse_init() tries to allocate memory from memblock, so must be
319− * done after the fixed reservations
320− */
321− sparse_init();
322− zone_sizes_init();
315+ dma_limits_init();
323316
324317 /*
325318 * Reserve the CMA area after arm64_dma_phys_limit was initialised.
arch/arm64/mm/mmu.c+4−4
@@ -800,7 +800,7 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned long end) · 799 unmodified lines
800800 return -EINVAL;
801801
802802 mutex_lock(&pgtable_split_lock);
803− arch_enter_lazy_mmu_mode();
803+ lazy_mmu_mode_enable();
804804
805805 /*
806806 * The split_kernel_leaf_mapping_locked() may sleep, it is not a
@@ -822,7 +822,7 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned long end) · 15 unmodified lines
822822 ret = split_kernel_leaf_mapping_locked(end);
823823 }
824824
825− arch_leave_lazy_mmu_mode();
825+ lazy_mmu_mode_disable();
826826 mutex_unlock(&pgtable_split_lock);
827827 return ret;
828828 }
@@ -883,10 +883,10 @@ static int range_split_to_ptes(unsigned long start, unsigned long end, gfp_t gfp · 54 unmodified lines
883883 {
884884 int ret;
885885
886− arch_enter_lazy_mmu_mode();
886+ lazy_mmu_mode_enable();
887887 ret = walk_kernel_page_table_range_lockless(start, end,
888888 &split_to_ptes_ops, NULL, &gfp);
889− arch_leave_lazy_mmu_mode();
889+ lazy_mmu_mode_disable();
890890
891891 return ret;
892892 }
arch/arm64/mm/pageattr.c+2−2
@@ -110,7 +110,7 @@ static int update_range_prot(unsigned long start, unsigned long size, · 109 unmodified lines
110110 if (WARN_ON_ONCE(ret))
111111 return ret;
112112
113− arch_enter_lazy_mmu_mode();
113+ lazy_mmu_mode_enable();
114114
115115 /*
116116 * The caller must ensure that the range we are operating on does not
@@ -119,7 +119,7 @@ static int update_range_prot(unsigned long start, unsigned long size, · 2 unmodified lines
119119 */
120120 ret = walk_kernel_page_table_range_lockless(start, start + size,
121121 &pageattr_ops, NULL, &data);
122− arch_leave_lazy_mmu_mode();
122+ lazy_mmu_mode_disable();
123123
124124 return ret;
125125 }
arch/csky/abiv1/inc/abi/page.h+1−0
@@ -10,6 +10,7 @@ static inline unsigned long pages_do_alias(unsigned long addr1, · 9 unmodified lines
1010 return (addr1 ^ addr2) & (SHMLBA-1);
1111 }
1212
13+#define clear_user_page clear_user_page
1314 static inline void clear_user_page(void *addr, unsigned long vaddr,
1415 struct page *page)
1516 {
arch/csky/abiv2/inc/abi/page.h+0−7
@@ -1,11 +1,4 @@
11 /* SPDX-License-Identifier: GPL-2.0 */
2−
3−static inline void clear_user_page(void *addr, unsigned long vaddr,
4− struct page *page)
5−{
6− clear_page(addr);
7−}
8−
92 static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
103 struct page *page)
114 {
arch/csky/kernel/setup.c+8−8
@@ -51,11 +51,18 @@ disable: · 50 unmodified lines
5151 }
5252 #endif
5353
54+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
55+{
56+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
57+#ifdef CONFIG_HIGHMEM
58+ max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
59+#endif
60+}
61+
5462 static void __init csky_memblock_init(void)
5563 {
5664 unsigned long lowmem_size = PFN_DOWN(LOWMEM_LIMIT - PHYS_OFFSET_OFFSET);
5765 unsigned long sseg_size = PFN_DOWN(SSEG_SIZE - PHYS_OFFSET_OFFSET);
58− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
5966 signed long size;
6067
6168 memblock_reserve(__pa(_start), _end - _start);
@@ -83,12 +90,9 @@ static void __init csky_memblock_init(void) · 21 unmodified lines
8390 setup_initrd();
8491 #endif
8592
86− max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
87−
8893 mmu_init(min_low_pfn, max_low_pfn);
8994
9095 #ifdef CONFIG_HIGHMEM
91− max_zone_pfn[ZONE_HIGHMEM] = max_pfn;
9296
9397 highstart_pfn = max_low_pfn;
9498 highend_pfn = max_pfn;
@@ -96,8 +100,6 @@ static void __init csky_memblock_init(void) · 1 unmodified line
96100 memblock_set_current_limit(PFN_PHYS(max_low_pfn));
97101
98102 dma_contiguous_reserve(0);
99−
100− free_area_init(max_zone_pfn);
101103 }
102104
103105 void __init setup_arch(char **cmdline_p)
@@ -121,8 +123,6 @@ void __init setup_arch(char **cmdline_p) · 17 unmodified lines
121123 setup_smp();
122124 #endif
123125
124− sparse_init();
125−
126126 fixaddr_init();
127127
128128 #ifdef CONFIG_HIGHMEM
arch/hexagon/include/asm/page.h+0−1
@@ -113,7 +113,6 @@ static inline void clear_page(void *page) · 112 unmodified lines
113113 /*
114114 * Under assumption that kernel always "sees" user map...
115115 */
116−#define clear_user_page(page, vaddr, pg) clear_page(page)
117116 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
118117
119118 static inline unsigned long virt_to_pfn(const void *kaddr)
arch/hexagon/mm/init.c+5−14
@@ -54,17 +54,8 @@ void sync_icache_dcache(pte_t pte) · 53 unmodified lines
5454 __vmcache_idsync(addr, PAGE_SIZE);
5555 }
5656
57−/*
58− * In order to set up page allocator "nodes",
59− * somebody has to call free_area_init() for UMA.
60− *
61− * In this mode, we only have one pg_data_t
62− * structure: contig_mem_data.
63− */
64−static void __init paging_init(void)
57+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
6558 {
66− unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, };
67−
6859 /*
6960 * This is not particularly well documented anywhere, but
7061 * give ZONE_NORMAL all the memory, including the big holes
@@ -72,11 +63,11 @@ static void __init paging_init(void) · 1 unmodified line
7263 * in the bootmem_map; free_area_init should see those bits and
7364 * adjust accordingly.
7465 */
66+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
67+}
7568
76− max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
77−
78− free_area_init(max_zone_pfn); /* sets up the zonelists and mem_map */
79−
69+static void __init paging_init(void)
70+{
8071 /*
8172 * Set the init_mm descriptors "context" value to point to the
8273 * initial kernel segment table's physical address.
arch/loongarch/Kconfig+1−0
@@ -187,6 +187,7 @@ config LOONGARCH · 186 unmodified lines
187187 select IRQ_LOONGARCH_CPU
188188 select LOCK_MM_AND_FIND_VMA
189189 select MMU_GATHER_MERGE_VMAS if MMU
190+ select MMU_GATHER_RCU_TABLE_FREE
190191 select MODULES_USE_ELF_RELA if MODULES
191192 select NEED_PER_CPU_EMBED_FIRST_CHUNK
192193 select NEED_PER_CPU_PAGE_FIRST_CHUNK
arch/loongarch/include/asm/page.h+0−1
@@ -30,7 +30,6 @@ · 29 unmodified lines
3030 extern void clear_page(void *page);
3131 extern void copy_page(void *to, void *from);
3232
33−#define clear_user_page(page, vaddr, pg) clear_page(page)
3433 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
3534
3635 extern unsigned long shm_align_mask;
arch/loongarch/include/asm/pgalloc.h+3−4
@@ -55,8 +55,7 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm) · 54 unmodified lines
5555 return pte;
5656 }
5757
58−#define __pte_free_tlb(tlb, pte, address) \
59− tlb_remove_ptdesc((tlb), page_ptdesc(pte))
58+#define __pte_free_tlb(tlb, pte, address) tlb_remove_ptdesc((tlb), page_ptdesc(pte))
6059
6160 #ifndef __PAGETABLE_PMD_FOLDED
6261
@@ -79,7 +78,7 @@ static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address) · 16 unmodified lines
7978 return pmd;
8079 }
8180
82−#define __pmd_free_tlb(tlb, x, addr) pmd_free((tlb)->mm, x)
81+#define __pmd_free_tlb(tlb, x, addr) tlb_remove_ptdesc((tlb), virt_to_ptdesc(x))
8382
8483 #endif
8584
@@ -99,7 +98,7 @@ static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long address) · 13 unmodified lines
9998 return pud;
10099 }
101100
102−#define __pud_free_tlb(tlb, x, addr) pud_free((tlb)->mm, x)
101+#define __pud_free_tlb(tlb, x, addr) tlb_remove_ptdesc((tlb), virt_to_ptdesc(x))
103102
104103 #endif /* __PAGETABLE_PUD_FOLDED */
105104
arch/loongarch/include/asm/pgtable.h+0−2
@@ -353,8 +353,6 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte) · 352 unmodified lines
353353 return pte;
354354 }
355355
356−extern void paging_init(void);
357−
358356 #define pte_none(pte) (!(pte_val(pte) & ~_PAGE_GLOBAL))
359357 #define pte_present(pte) (pte_val(pte) & (_PAGE_PRESENT | _PAGE_PROTNONE))
360358 #define pte_no_exec(pte) (pte_val(pte) & _PAGE_NO_EXEC)
arch/loongarch/kernel/setup.c+0−10
@@ -402,14 +402,6 @@ static void __init arch_mem_init(char **cmdline_p) · 401 unmodified lines
402402
403403 check_kernel_sections_mem();
404404
405− /*
406− * In order to reduce the possibility of kernel panic when failed to
407− * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
408− * low memory as small as possible before swiotlb_init(), so make
409− * sparse_init() using top-down allocation.
410− */
411− memblock_set_bottom_up(false);
412− sparse_init();
413405 memblock_set_bottom_up(true);
414406
415407 swiotlb_init(true, SWIOTLB_VERBOSE);
@@ -621,8 +613,6 @@ void __init setup_arch(char **cmdline_p) · 205 unmodified lines
621613 prefill_possible_map();
622614 #endif
623615
624− paging_init();
625−
626616 #ifdef CONFIG_KASAN
627617 kasan_init();
628618 #endif
arch/loongarch/mm/init.c+1−5
@@ -60,16 +60,12 @@ int __ref page_is_ram(unsigned long pfn) · 59 unmodified lines
6060 return memblock_is_memory(addr) && !memblock_is_reserved(addr);
6161 }
6262
63−void __init paging_init(void)
63+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
6464 {
65− unsigned long max_zone_pfns[MAX_NR_ZONES];
66−
6765 #ifdef CONFIG_ZONE_DMA32
6866 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
6967 #endif
7068 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
71−
72− free_area_init(max_zone_pfns);
7369 }
7470
7571 void __ref free_initmem(void)
arch/m68k/include/asm/page_no.h+0−1
@@ -10,7 +10,6 @@ extern unsigned long memory_end; · 9 unmodified lines
1010 #define clear_page(page) memset((page), 0, PAGE_SIZE)
1111 #define copy_page(to,from) memcpy((to), (from), PAGE_SIZE)
1212
13−#define clear_user_page(page, vaddr, pg) clear_page(page)
1413 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
1514
1615 #define vma_alloc_zeroed_movable_folio(vma, vaddr) \
arch/m68k/mm/init.c+5−3
@@ -40,6 +40,11 @@ · 39 unmodified lines
4040 void *empty_zero_page;
4141 EXPORT_SYMBOL(empty_zero_page);
4242
43+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
44+{
45+ max_zone_pfns[ZONE_DMA] = PFN_DOWN(memblock_end_of_DRAM());
46+}
47+
4348 #ifdef CONFIG_MMU
4449
4550 int m68k_virt_to_node_shift;
@@ -64,13 +69,10 @@ void __init paging_init(void) · 18 unmodified lines
6469 * page_alloc get different views of the world.
6570 */
6671 unsigned long end_mem = memory_end & PAGE_MASK;
67− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
6872
6973 high_memory = (void *) end_mem;
7074
7175 empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
72− max_zone_pfn[ZONE_DMA] = end_mem >> PAGE_SHIFT;
73− free_area_init(max_zone_pfn);
7476 }
7577
7678 #endif /* CONFIG_MMU */
arch/m68k/mm/mcfmmu.c+0−3
@@ -39,7 +39,6 @@ void __init paging_init(void) · 38 unmodified lines
3939 pte_t *pg_table;
4040 unsigned long address, size;
4141 unsigned long next_pgtable;
42− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
4342 int i;
4443
4544 empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
@@ -73,8 +72,6 @@ void __init paging_init(void) · 27 unmodified lines
7372 }
7473
7574 current->mm = NULL;
76− max_zone_pfn[ZONE_DMA] = PFN_DOWN(_ramend);
77− free_area_init(max_zone_pfn);
7875 }
7976
8077 int cf_tlb_miss(struct pt_regs *regs, int write, int dtlb, int extension_word)
arch/m68k/mm/motorola.c+1−5
@@ -429,7 +429,6 @@ DECLARE_VM_GET_PAGE_PROT · 428 unmodified lines
429429 */
430430 void __init paging_init(void)
431431 {
432− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
433432 unsigned long min_addr, max_addr;
434433 unsigned long addr;
435434 int i;
@@ -511,12 +510,9 @@ void __init paging_init(void) · 75 unmodified lines
511510 set_fc(USER_DATA);
512511
513512 #ifdef DEBUG
514− printk ("before free_area_init\n");
513+ printk ("before node_set_state\n");
515514 #endif
516515 for (i = 0; i < m68k_num_memory; i++)
517516 if (node_present_pages(i))
518517 node_set_state(i, N_NORMAL_MEMORY);
519−
520− max_zone_pfn[ZONE_DMA] = memblock_end_of_DRAM();
521− free_area_init(max_zone_pfn);
522518 }
arch/m68k/mm/sun3mmu.c+0−9
@@ -41,7 +41,6 @@ void __init paging_init(void) · 40 unmodified lines
4141 unsigned long address;
4242 unsigned long next_pgtable;
4343 unsigned long bootmem_end;
44− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
4544 unsigned long size;
4645
4746 empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
@@ -80,14 +79,6 @@ void __init paging_init(void) · 32 unmodified lines
8079 mmu_emu_init(bootmem_end);
8180
8281 current->mm = NULL;
83−
84− /* memory sizing is a hack stolen from motorola.c.. hope it works for us */
85− max_zone_pfn[ZONE_DMA] = ((unsigned long)high_memory) >> PAGE_SHIFT;
86−
87− /* I really wish I knew why the following change made things better... -- Sam */
88− free_area_init(max_zone_pfn);
89−
90−
9182 }
9283
9384 static const pgprot_t protection_map[16] = {
arch/microblaze/include/asm/page.h+0−1
@@ -45,7 +45,6 @@ typedef unsigned long pte_basic_t; · 44 unmodified lines
4545 # define copy_page(to, from) memcpy((to), (from), PAGE_SIZE)
4646 # define clear_page(pgaddr) memset((pgaddr), 0, PAGE_SIZE)
4747
48−# define clear_user_page(pgaddr, vaddr, page) memset((pgaddr), 0, PAGE_SIZE)
4948 # define copy_user_page(vto, vfrom, vaddr, topg) \
5049 memcpy((vto), (vfrom), PAGE_SIZE)
5150
arch/microblaze/mm/init.c+10−12
@@ -54,32 +54,30 @@ static void __init highmem_init(void) · 53 unmodified lines
5454 }
5555 #endif /* CONFIG_HIGHMEM */
5656
57+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
58+{
59+#ifdef CONFIG_HIGHMEM
60+ max_zone_pfns[ZONE_DMA] = max_low_pfn;
61+ max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
62+#else
63+ max_zone_pfns[ZONE_DMA] = max_pfn;
64+#endif
65+}
66+
5767 /*
5868 * paging_init() sets up the page tables - in fact we've already done this.
5969 */
6070 static void __init paging_init(void)
6171 {
62− unsigned long zones_size[MAX_NR_ZONES];
6372 int idx;
6473
6574 /* Setup fixmaps */
6675 for (idx = 0; idx < __end_of_fixed_addresses; idx++)
6776 clear_fixmap(idx);
6877
69− /* Clean every zones */
70− memset(zones_size, 0, sizeof(zones_size));
71−
7278 #ifdef CONFIG_HIGHMEM
7379 highmem_init();
74−
75− zones_size[ZONE_DMA] = max_low_pfn;
76− zones_size[ZONE_HIGHMEM] = max_pfn;
77−#else
78− zones_size[ZONE_DMA] = max_pfn;
7980 #endif
80−
81− /* We don't have holes in memory map */
82− free_area_init(zones_size);
8381 }
8482
8583 void __init setup_memory(void)
arch/mips/Kconfig+1−0
@@ -99,6 +99,7 @@ config MIPS · 98 unmodified lines
9999 select IRQ_FORCED_THREADING
100100 select ISA if EISA
101101 select LOCK_MM_AND_FIND_VMA
102+ select MMU_GATHER_RCU_TABLE_FREE
102103 select MODULES_USE_ELF_REL if MODULES
103104 select MODULES_USE_ELF_RELA if MODULES && 64BIT
104105 select PERF_USE_VMALLOC
arch/mips/include/asm/page.h+1−0
@@ -90,6 +90,7 @@ static inline void clear_user_page(void *addr, unsigned long vaddr, · 89 unmodified lines
9090 if (pages_do_alias((unsigned long) addr, vaddr & PAGE_MASK))
9191 flush_data_cache_page((unsigned long)addr);
9292 }
93+#define clear_user_page clear_user_page
9394
9495 struct vm_area_struct;
9596 extern void copy_user_highpage(struct page *to, struct page *from,
arch/mips/include/asm/pgalloc.h+3−6
@@ -48,8 +48,7 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) · 47 unmodified lines
4848 extern void pgd_init(void *addr);
4949 extern pgd_t *pgd_alloc(struct mm_struct *mm);
5050
51−#define __pte_free_tlb(tlb, pte, address) \
52− tlb_remove_ptdesc((tlb), page_ptdesc(pte))
51+#define __pte_free_tlb(tlb, pte, address) tlb_remove_ptdesc((tlb), page_ptdesc(pte))
5352
5453 #ifndef __PAGETABLE_PMD_FOLDED
5554
@@ -72,7 +71,7 @@ static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address) · 16 unmodified lines
7271 return pmd;
7372 }
7473
75−#define __pmd_free_tlb(tlb, x, addr) pmd_free((tlb)->mm, x)
74+#define __pmd_free_tlb(tlb, x, addr) tlb_remove_ptdesc((tlb), virt_to_ptdesc(x))
7675
7776 #endif
7877
@@ -97,10 +96,8 @@ static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud) · 18 unmodified lines
9796 set_p4d(p4d, __p4d((unsigned long)pud));
9897 }
9998
100−#define __pud_free_tlb(tlb, x, addr) pud_free((tlb)->mm, x)
99+#define __pud_free_tlb(tlb, x, addr) tlb_remove_ptdesc((tlb), virt_to_ptdesc(x))
101100
102101 #endif /* __PAGETABLE_PUD_FOLDED */
103102
104−extern void pagetable_init(void);
105−
106103 #endif /* _ASM_PGALLOC_H */
arch/mips/include/asm/pgtable.h+1−1
@@ -56,7 +56,7 @@ extern unsigned long zero_page_mask; · 55 unmodified lines
5656 (virt_to_page((void *)(empty_zero_page + (((unsigned long)(vaddr)) & zero_page_mask))))
5757 #define __HAVE_COLOR_ZERO_PAGE
5858
59−extern void paging_init(void);
59+extern void pagetable_init(void);
6060
6161 /*
6262 * Conversion functions: convert a page and protection to a page entry,
arch/mips/kernel/setup.c+2−13
@@ -614,8 +614,7 @@ static void __init bootcmdline_init(void) · 613 unmodified lines
614614 * kernel but generic memory management system is still entirely uninitialized.
615615 *
616616 * o bootmem_init()
617− * o sparse_init()
618− * o paging_init()
617+ * o pagetable_init()
619618 * o dma_contiguous_reserve()
620619 *
621620 * At this stage the bootmem allocator is ready to use.
@@ -665,16 +664,6 @@ static void __init arch_mem_init(char **cmdline_p) · 43 unmodified lines
665664 mips_parse_crashkernel();
666665 device_tree_init();
667666
668− /*
669− * In order to reduce the possibility of kernel panic when failed to
670− * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
671− * low memory as small as possible before plat_swiotlb_setup(), so
672− * make sparse_init() using top-down allocation.
673− */
674− memblock_set_bottom_up(false);
675− sparse_init();
676− memblock_set_bottom_up(true);
677−
678667 plat_swiotlb_setup();
679668
680669 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
@@ -789,7 +778,7 @@ void __init setup_arch(char **cmdline_p) · 108 unmodified lines
789778 prefill_possible_map();
790779
791780 cpu_cache_init();
792− paging_init();
781+ pagetable_init();
793782
794783 memblock_dump_all();
795784
arch/mips/loongson64/numa.c+3−7
@@ -154,14 +154,10 @@ static __init void prom_meminit(void) · 153 unmodified lines
154154 }
155155 }
156156
157−void __init paging_init(void)
157+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
158158 {
159− unsigned long zones_size[MAX_NR_ZONES] = {0, };
160−
161− pagetable_init();
162− zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
163− zones_size[ZONE_NORMAL] = max_low_pfn;
164− free_area_init(zones_size);
159+ max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
160+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
165161 }
166162
167163 /* All PCI device belongs to logical Node-0 */
arch/mips/mm/init.c+1−7
@@ -394,12 +394,8 @@ void maar_init(void) · 393 unmodified lines
394394 }
395395
396396 #ifndef CONFIG_NUMA
397−void __init paging_init(void)
397+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
398398 {
399− unsigned long max_zone_pfns[MAX_NR_ZONES];
400−
401− pagetable_init();
402−
403399 #ifdef CONFIG_ZONE_DMA
404400 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
405401 #endif
@@ -417,8 +413,6 @@ void __init paging_init(void) · 11 unmodified lines
417413 max_zone_pfns[ZONE_HIGHMEM] = max_low_pfn;
418414 }
419415 #endif
420−
421− free_area_init(max_zone_pfns);
422416 }
423417
424418 #ifdef CONFIG_64BIT
arch/mips/sgi-ip27/ip27-memory.c+2−6
@@ -406,11 +406,7 @@ void __init prom_meminit(void) · 405 unmodified lines
406406 }
407407 }
408408
409−void __init paging_init(void)
409+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
410410 {
411− unsigned long zones_size[MAX_NR_ZONES] = {0, };
412−
413− pagetable_init();
414− zones_size[ZONE_NORMAL] = max_low_pfn;
415− free_area_init(zones_size);
411+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
416412 }
arch/nios2/include/asm/page.h+1−0
@@ -45,6 +45,7 @@ · 44 unmodified lines
4545
4646 struct page;
4747
48+#define clear_user_page clear_user_page
4849 extern void clear_user_page(void *addr, unsigned long vaddr, struct page *page);
4950 extern void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
5051 struct page *to);
arch/nios2/mm/init.c+5−7
@@ -38,6 +38,11 @@ · 37 unmodified lines
3838
3939 pgd_t *pgd_current;
4040
41+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
42+{
43+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
44+}
45+
4146 /*
4247 * paging_init() continues the virtual memory environment setup which
4348 * was begun by the code in arch/head.S.
@@ -46,16 +51,9 @@ pgd_t *pgd_current; · 2 unmodified lines
4651 */
4752 void __init paging_init(void)
4853 {
49− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
50−
5154 pagetable_init();
5255 pgd_current = swapper_pg_dir;
5356
54− max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
55−
56− /* pass the memory from the bootmem allocator to the main allocator */
57− free_area_init(max_zone_pfn);
58−
5957 flush_dcache_range((unsigned long)empty_zero_page,
6058 (unsigned long)empty_zero_page + PAGE_SIZE);
6159 }
arch/openrisc/include/asm/page.h+0−1
@@ -30,7 +30,6 @@ · 29 unmodified lines
3030 #define clear_page(page) memset((page), 0, PAGE_SIZE)
3131 #define copy_page(to, from) memcpy((to), (from), PAGE_SIZE)
3232
33−#define clear_user_page(page, vaddr, pg) clear_page(page)
3433 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
3534
3635 /*
arch/openrisc/mm/init.c+2−8
@@ -39,16 +39,12 @@ · 38 unmodified lines
3939
4040 int mem_init_done;
4141
42−static void __init zone_sizes_init(void)
42+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
4343 {
44− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
45−
4644 /*
4745 * We use only ZONE_NORMAL
4846 */
49− max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
50−
51− free_area_init(max_zone_pfn);
47+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
5248 }
5349
5450 extern const char _s_kernel_ro[], _e_kernel_ro[];
@@ -141,8 +137,6 @@ void __init paging_init(void) · 86 unmodified lines
141137
142138 map_ram();
143139
144− zone_sizes_init();
145−
146140 /* self modifying code ;) */
147141 /* Since the old TLB miss handler has been running up until now,
148142 * the kernel pages are still all RW, so we can still modify the
arch/parisc/Kconfig+1−0
@@ -79,6 +79,7 @@ config PARISC · 78 unmodified lines
7979 select GENERIC_CLOCKEVENTS
8080 select CPU_NO_EFFICIENT_FFS
8181 select THREAD_INFO_IN_TASK
82+ select MMU_GATHER_RCU_TABLE_FREE
8283 select NEED_DMA_MAP_STATE
8384 select NEED_SG_DMA_LENGTH
8485 select HAVE_ARCH_KGDB
arch/parisc/include/asm/page.h+0−1
@@ -21,7 +21,6 @@ struct vm_area_struct; · 20 unmodified lines
2121
2222 void clear_page_asm(void *page);
2323 void copy_page_asm(void *to, void *from);
24−#define clear_user_page(vto, vaddr, page) clear_page_asm(vto)
2524 void copy_user_highpage(struct page *to, struct page *from, unsigned long vaddr,
2625 struct vm_area_struct *vma);
2726 #define __HAVE_ARCH_COPY_USER_HIGHPAGE
arch/parisc/include/asm/tlb.h+2−2
@@ -5,8 +5,8 @@ · 4 unmodified lines
55 #include <asm-generic/tlb.h>
66
77 #if CONFIG_PGTABLE_LEVELS == 3
8−#define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tlb)->mm, pmd)
8+#define __pmd_free_tlb(tlb, pmd, addr) tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd))
99 #endif
10−#define __pte_free_tlb(tlb, pte, addr) pte_free((tlb)->mm, pte)
10+#define __pte_free_tlb(tlb, pte, addr) tlb_remove_ptdesc((tlb), page_ptdesc(pte))
1111
1212 #endif
arch/parisc/mm/init.c+2−9
@@ -693,13 +693,9 @@ static void __init fixmap_init(void) · 692 unmodified lines
693693 } while (addr < end);
694694 }
695695
696−static void __init parisc_bootmem_free(void)
696+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
697697 {
698− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
699−
700− max_zone_pfn[0] = memblock_end_of_DRAM();
701−
702− free_area_init(max_zone_pfn);
698+ max_zone_pfns[ZONE_NORMAL] = PFN_DOWN(memblock_end_of_DRAM());
703699 }
704700
705701 void __init paging_init(void)
@@ -710,9 +706,6 @@ void __init paging_init(void) · 4 unmodified lines
710706 fixmap_init();
711707 flush_cache_all_local(); /* start with known state */
712708 flush_tlb_all_local(NULL);
713−
714− sparse_init();
715− parisc_bootmem_free();
716709 }
717710
718711 static void alloc_btlb(unsigned long start, unsigned long end, int *slot,
arch/powerpc/Kconfig+2−0
@@ -172,6 +172,7 @@ config PPC · 171 unmodified lines
172172 select ARCH_STACKWALK
173173 select ARCH_SUPPORTS_ATOMIC_RMW
174174 select ARCH_SUPPORTS_DEBUG_PAGEALLOC if PPC_BOOK3S || PPC_8xx
175+ select ARCH_SUPPORTS_PAGE_TABLE_CHECK if !HUGETLB_PAGE
175176 select ARCH_SUPPORTS_SCHED_MC if SMP
176177 select ARCH_SUPPORTS_SCHED_SMT if PPC64 && SMP
177178 select SCHED_MC if ARCH_SUPPORTS_SCHED_MC
@@ -304,6 +305,7 @@ config PPC · 126 unmodified lines
304305 select LOCK_MM_AND_FIND_VMA
305306 select MMU_GATHER_PAGE_SIZE
306307 select MMU_GATHER_RCU_TABLE_FREE
308+ select HAVE_ARCH_TLB_REMOVE_TABLE
307309 select MMU_GATHER_MERGE_VMAS
308310 select MMU_LAZY_TLB_SHOOTDOWN if PPC_BOOK3S_64
309311 select MODULES_USE_ELF_RELA
arch/powerpc/include/asm/book3s/32/pgtable.h+11−1
@@ -198,6 +198,7 @@ void unmap_kernel_page(unsigned long va); · 197 unmodified lines
198198 #ifndef __ASSEMBLER__
199199 #include <linux/sched.h>
200200 #include <linux/threads.h>
201+#include <linux/page_table_check.h>
201202
202203 /* Bits to mask out from a PGD to get to the PUD page */
203204 #define PGD_MASKED_BITS 0
@@ -311,7 +312,11 @@ static inline int __ptep_test_and_clear_young(struct mm_struct *mm, · 107 unmodified lines
311312 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
312313 pte_t *ptep)
313314 {
314− return __pte(pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0));
315+ pte_t old_pte = __pte(pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0));
316+
317+ page_table_check_pte_clear(mm, addr, old_pte);
318+
319+ return old_pte;
315320 }
316321
317322 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
@@ -433,6 +438,11 @@ static inline bool pte_access_permitted(pte_t pte, bool write) · 115 unmodified lines
433438 return true;
434439 }
435440
441+static inline bool pte_user_accessible_page(pte_t pte, unsigned long addr)
442+{
443+ return pte_present(pte) && !is_kernel_addr(addr);
444+}
445+
436446 /* Conversion functions: convert a page and protection to a page entry,
437447 * and a page entry and page directory to the page they refer to.
438448 *
arch/powerpc/include/asm/book3s/64/pgtable.h+52−10
@@ -144,6 +144,8 @@ · 143 unmodified lines
144144 #define PAGE_KERNEL_ROX __pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
145145
146146 #ifndef __ASSEMBLER__
147+#include <linux/page_table_check.h>
148+
147149 /*
148150 * page table defines
149151 */
@@ -416,8 +418,11 @@ static inline void huge_ptep_set_wrprotect(struct mm_struct *mm, · 266 unmodified lines
416418 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
417419 unsigned long addr, pte_t *ptep)
418420 {
419− unsigned long old = pte_update(mm, addr, ptep, ~0UL, 0, 0);
420− return __pte(old);
421+ pte_t old_pte = __pte(pte_update(mm, addr, ptep, ~0UL, 0, 0));
422+
423+ page_table_check_pte_clear(mm, addr, old_pte);
424+
425+ return old_pte;
421426 }
422427
423428 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
@@ -426,11 +431,16 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, · 2 unmodified lines
426431 pte_t *ptep, int full)
427432 {
428433 if (full && radix_enabled()) {
434+ pte_t old_pte;
435+
429436 /*
430437 * We know that this is a full mm pte clear and
431438 * hence can be sure there is no parallel set_pte.
432439 */
433− return radix__ptep_get_and_clear_full(mm, addr, ptep, full);
440+ old_pte = radix__ptep_get_and_clear_full(mm, addr, ptep, full);
441+ page_table_check_pte_clear(mm, addr, old_pte);
442+
443+ return old_pte;
434444 }
435445 return ptep_get_and_clear(mm, addr, ptep);
436446 }
@@ -539,6 +549,11 @@ static inline bool pte_access_permitted(pte_t pte, bool write) · 102 unmodified lines
539549 return arch_pte_access_permitted(pte_val(pte), write, 0);
540550 }
541551
552+static inline bool pte_user_accessible_page(pte_t pte, unsigned long addr)
553+{
554+ return pte_present(pte) && pte_user(pte);
555+}
556+
542557 /*
543558 * Conversion functions: convert a page and protection to a page entry,
544559 * and a page entry and page directory to the page they refer to.
@@ -909,6 +924,12 @@ static inline bool pud_access_permitted(pud_t pud, bool write) · 364 unmodified lines
909924 return pte_access_permitted(pud_pte(pud), write);
910925 }
911926
927+#define pud_user_accessible_page pud_user_accessible_page
928+static inline bool pud_user_accessible_page(pud_t pud, unsigned long addr)
929+{
930+ return pud_leaf(pud) && pte_user_accessible_page(pud_pte(pud), addr);
931+}
932+
912933 #define __p4d_raw(x) ((p4d_t) { __pgd_raw(x) })
913934 static inline __be64 p4d_raw(p4d_t x)
914935 {
@@ -1074,6 +1095,12 @@ static inline bool pmd_access_permitted(pmd_t pmd, bool write) · 159 unmodified lines
10741095 return pte_access_permitted(pmd_pte(pmd), write);
10751096 }
10761097
1098+#define pmd_user_accessible_page pmd_user_accessible_page
1099+static inline bool pmd_user_accessible_page(pmd_t pmd, unsigned long addr)
1100+{
1101+ return pmd_leaf(pmd) && pte_user_accessible_page(pmd_pte(pmd), addr);
1102+}
1103+
10771104 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
10781105 extern pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot);
10791106 extern pud_t pfn_pud(unsigned long pfn, pgprot_t pgprot);
@@ -1284,19 +1311,34 @@ extern int pudp_test_and_clear_young(struct vm_area_struct *vma, · 204 unmodified lines
12841311 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
12851312 unsigned long addr, pmd_t *pmdp)
12861313 {
1287− if (radix_enabled())
1288− return radix__pmdp_huge_get_and_clear(mm, addr, pmdp);
1289− return hash__pmdp_huge_get_and_clear(mm, addr, pmdp);
1314+ pmd_t old_pmd;
1315+
1316+ if (radix_enabled()) {
1317+ old_pmd = radix__pmdp_huge_get_and_clear(mm, addr, pmdp);
1318+ } else {
1319+ old_pmd = hash__pmdp_huge_get_and_clear(mm, addr, pmdp);
1320+ }
1321+
1322+ page_table_check_pmd_clear(mm, addr, old_pmd);
1323+
1324+ return old_pmd;
12901325 }
12911326
12921327 #define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
12931328 static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
12941329 unsigned long addr, pud_t *pudp)
12951330 {
1296− if (radix_enabled())
1297− return radix__pudp_huge_get_and_clear(mm, addr, pudp);
1298− BUG();
1299− return *pudp;
1331+ pud_t old_pud;
1332+
1333+ if (radix_enabled()) {
1334+ old_pud = radix__pudp_huge_get_and_clear(mm, addr, pudp);
1335+ } else {
1336+ BUG();
1337+ }
1338+
1339+ page_table_check_pud_clear(mm, addr, old_pud);
1340+
1341+ return old_pud;
13001342 }
13011343
13021344 static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
arch/powerpc/include/asm/book3s/64/tlbflush-hash.h+9−11
@@ -12,7 +12,6 @@ · 11 unmodified lines
1212 #define PPC64_TLB_BATCH_NR 192
1313
1414 struct ppc64_tlb_batch {
15− int active;
1615 unsigned long index;
1716 struct mm_struct *mm;
1817 real_pte_t pte[PPC64_TLB_BATCH_NR];
@@ -24,12 +23,8 @@ DECLARE_PER_CPU(struct ppc64_tlb_batch, ppc64_tlb_batch); · 5 unmodified lines
2423
2524 extern void __flush_tlb_pending(struct ppc64_tlb_batch *batch);
2625
27−#define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
28−
2926 static inline void arch_enter_lazy_mmu_mode(void)
3027 {
31− struct ppc64_tlb_batch *batch;
32−
3328 if (radix_enabled())
3429 return;
3530 /*
@@ -37,11 +32,9 @@ static inline void arch_enter_lazy_mmu_mode(void) · 1 unmodified line
3732 * operating on kernel page tables.
3833 */
3934 preempt_disable();
40− batch = this_cpu_ptr(&ppc64_tlb_batch);
41− batch->active = 1;
4235 }
4336
44−static inline void arch_leave_lazy_mmu_mode(void)
37+static inline void arch_flush_lazy_mmu_mode(void)
4538 {
4639 struct ppc64_tlb_batch *batch;
4740
@@ -51,11 +44,16 @@ static inline void arch_leave_lazy_mmu_mode(void) · 3 unmodified lines
5144
5245 if (batch->index)
5346 __flush_tlb_pending(batch);
54− batch->active = 0;
55− preempt_enable();
5647 }
5748
58−#define arch_flush_lazy_mmu_mode() do {} while (0)
49+static inline void arch_leave_lazy_mmu_mode(void)
50+{
51+ if (radix_enabled())
52+ return;
53+
54+ arch_flush_lazy_mmu_mode();
55+ preempt_enable();
56+}
5957
6058 extern void hash__tlbiel_all(unsigned int action);
6159
arch/powerpc/include/asm/hugetlb.h+0−5
@@ -68,7 +68,6 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, · 67 unmodified lines
6868 unsigned long addr, pte_t *ptep,
6969 pte_t pte, int dirty);
7070
71−void gigantic_hugetlb_cma_reserve(void) __init;
7271 #include <asm-generic/hugetlb.h>
7372
7473 #else /* ! CONFIG_HUGETLB_PAGE */
@@ -77,10 +76,6 @@ static inline void flush_hugetlb_page(struct vm_area_struct *vma, · 2 unmodified lines
7776 {
7877 }
7978
80−static inline void __init gigantic_hugetlb_cma_reserve(void)
81−{
82−}
83−
8479 static inline void __init hugetlbpage_init_defaultsize(void)
8580 {
8681 }
arch/powerpc/include/asm/nohash/pgtable.h+12−1
@@ -29,6 +29,8 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · 28 unmodified lines
2929
3030 #ifndef __ASSEMBLER__
3131
32+#include <linux/page_table_check.h>
33+
3234 extern int icache_44x_need_flush;
3335
3436 #ifndef pte_huge_size
@@ -122,7 +124,11 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, · 87 unmodified lines
122124 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
123125 pte_t *ptep)
124126 {
125− return __pte(pte_update(mm, addr, ptep, ~0UL, 0, 0));
127+ pte_t old_pte = __pte(pte_update(mm, addr, ptep, ~0UL, 0, 0));
128+
129+ page_table_check_pte_clear(mm, addr, old_pte);
130+
131+ return old_pte;
126132 }
127133 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
128134
@@ -243,6 +249,11 @@ static inline bool pte_access_permitted(pte_t pte, bool write) · 114 unmodified lines
243249 return true;
244250 }
245251
252+static inline bool pte_user_accessible_page(pte_t pte, unsigned long addr)
253+{
254+ return pte_present(pte) && !is_kernel_addr(addr);
255+}
256+
246257 /* Conversion functions: convert a page and protection to a page entry,
247258 * and a page entry and page directory to the page they refer to.
248259 *
arch/powerpc/include/asm/page.h+1−0
@@ -271,6 +271,7 @@ static inline const void *pfn_to_kaddr(unsigned long pfn) · 270 unmodified lines
271271
272272 struct page;
273273 extern void clear_user_page(void *page, unsigned long vaddr, struct page *pg);
274+#define clear_user_page clear_user_page
274275 extern void copy_user_page(void *to, void *from, unsigned long vaddr,
275276 struct page *p);
276277 extern int devmem_is_allowed(unsigned long pfn);
arch/powerpc/include/asm/pgtable.h+10−0
@@ -34,6 +34,8 @@ struct mm_struct; · 33 unmodified lines
3434 void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
3535 pte_t pte, unsigned int nr);
3636 #define set_ptes set_ptes
37+void set_pte_at_unchecked(struct mm_struct *mm, unsigned long addr,
38+ pte_t *ptep, pte_t pte);
3739 #define update_mmu_cache(vma, addr, ptep) \
3840 update_mmu_cache_range(NULL, vma, addr, ptep, 1)
3941
@@ -202,6 +204,14 @@ static inline bool arch_supports_memmap_on_memory(unsigned long vmemmap_size) · 162 unmodified lines
202204
203205 #endif /* CONFIG_PPC64 */
204206
207+#ifndef pmd_user_accessible_page
208+#define pmd_user_accessible_page(pmd, addr) false
209+#endif
210+
211+#ifndef pud_user_accessible_page
212+#define pud_user_accessible_page(pud, addr) false
213+#endif
214+
205215 #endif /* __ASSEMBLER__ */
206216
207217 #endif /* _ASM_POWERPC_PGTABLE_H */
arch/powerpc/include/asm/setup.h+4−0
@@ -20,7 +20,11 @@ extern void reloc_got2(unsigned long); · 19 unmodified lines
2020
2121 void check_for_initrd(void);
2222 void mem_topology_setup(void);
23+#ifdef CONFIG_NUMA
2324 void initmem_init(void);
25+#else
26+static inline void initmem_init(void) {}
27+#endif
2428 void setup_panic(void);
2529 #define ARCH_PANIC_TIMEOUT 180
2630
arch/powerpc/include/asm/thread_info.h+0−2
@@ -154,12 +154,10 @@ void arch_setup_new_exec(void); · 153 unmodified lines
154154 /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
155155 #define TLF_NAPPING 0 /* idle thread enabled NAP mode */
156156 #define TLF_SLEEPING 1 /* suspend code enabled SLEEP mode */
157−#define TLF_LAZY_MMU 3 /* tlb_batch is active */
158157 #define TLF_RUNLATCH 4 /* Is the runlatch enabled? */
159158
160159 #define _TLF_NAPPING (1 << TLF_NAPPING)
161160 #define _TLF_SLEEPING (1 << TLF_SLEEPING)
162−#define _TLF_LAZY_MMU (1 << TLF_LAZY_MMU)
163161 #define _TLF_RUNLATCH (1 << TLF_RUNLATCH)
164162
165163 #ifndef __ASSEMBLER__
arch/powerpc/include/asm/tlb.h+0−1
@@ -37,7 +37,6 @@ extern void tlb_flush(struct mmu_gather *tlb); · 36 unmodified lines
3737 */
3838 #define tlb_needs_table_invalidate() radix_enabled()
3939
40−#define __HAVE_ARCH_TLB_REMOVE_TABLE
4140 /* Get the generic bits... */
4241 #include <asm-generic/tlb.h>
4342
arch/powerpc/kernel/process.c+0−25
@@ -1281,9 +1281,6 @@ struct task_struct *__switch_to(struct task_struct *prev, · 1280 unmodified lines
12811281 {
12821282 struct thread_struct *new_thread, *old_thread;
12831283 struct task_struct *last;
1284−#ifdef CONFIG_PPC_64S_HASH_MMU
1285− struct ppc64_tlb_batch *batch;
1286−#endif
12871284
12881285 new_thread = &new->thread;
12891286 old_thread = &current->thread;
@@ -1291,14 +1288,6 @@ struct task_struct *__switch_to(struct task_struct *prev, · 1 unmodified line
12911288 WARN_ON(!irqs_disabled());
12921289
12931290 #ifdef CONFIG_PPC_64S_HASH_MMU
1294− batch = this_cpu_ptr(&ppc64_tlb_batch);
1295− if (batch->active) {
1296− current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1297− if (batch->index)
1298− __flush_tlb_pending(batch);
1299− batch->active = 0;
1300− }
1301−
13021291 /*
13031292 * On POWER9 the copy-paste buffer can only paste into
13041293 * foreign real addresses, so unprivileged processes can not
@@ -1369,20 +1358,6 @@ struct task_struct *__switch_to(struct task_struct *prev, · 64 unmodified lines
13691358 */
13701359
13711360 #ifdef CONFIG_PPC_BOOK3S_64
1372−#ifdef CONFIG_PPC_64S_HASH_MMU
1373− /*
1374− * This applies to a process that was context switched while inside
1375− * arch_enter_lazy_mmu_mode(), to re-activate the batch that was
1376− * deactivated above, before _switch(). This will never be the case
1377− * for new tasks.
1378− */
1379− if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1380− current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
1381− batch = this_cpu_ptr(&ppc64_tlb_batch);
1382− batch->active = 1;
1383− }
1384−#endif
1385−
13861361 /*
13871362 * Math facilities are masked out of the child MSR in copy_thread.
13881363 * A new task does not need to restore_math because it will
arch/powerpc/kernel/setup-common.c+0−1
@@ -1003,7 +1003,6 @@ void __init setup_arch(char **cmdline_p) · 1002 unmodified lines
10031003 fadump_cma_init();
10041004 kdump_cma_reserve();
10051005 kvm_cma_reserve();
1006− gigantic_hugetlb_cma_reserve();
10071006
10081007 early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
10091008
arch/powerpc/mm/book3s64/hash_pgtable.c+4−0
@@ -8,6 +8,7 @@ · 7 unmodified lines
88 #include <linux/sched.h>
99 #include <linux/mm_types.h>
1010 #include <linux/mm.h>
11+#include <linux/page_table_check.h>
1112 #include <linux/stop_machine.h>
1213
1314 #include <asm/sections.h>
@@ -230,6 +231,9 @@ pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long addres · 216 unmodified lines
230231
231232 pmd = *pmdp;
232233 pmd_clear(pmdp);
234+
235+ page_table_check_pmd_clear(vma->vm_mm, address, pmd);
236+
233237 /*
234238 * Wait for all pending hash_page to finish. This is needed
235239 * in case of subpage collapse. When we collapse normal pages
arch/powerpc/mm/book3s64/hash_tlb.c+8−6
@@ -25,11 +25,12 @@ · 24 unmodified lines
2525 #include <asm/tlb.h>
2626 #include <asm/bug.h>
2727 #include <asm/pte-walk.h>
28−
28+#include <kunit/visibility.h>
2929
3030 #include <trace/events/thp.h>
3131
3232 DEFINE_PER_CPU(struct ppc64_tlb_batch, ppc64_tlb_batch);
33+EXPORT_SYMBOL_IF_KUNIT(ppc64_tlb_batch);
3334
3435 /*
3536 * A linux PTE was changed and the corresponding hash table entry
@@ -100,7 +101,7 @@ void hpte_need_flush(struct mm_struct *mm, unsigned long addr, · 64 unmodified lines
100101 * Check if we have an active batch on this CPU. If not, just
101102 * flush now and return.
102103 */
103− if (!batch->active) {
104+ if (!is_lazy_mmu_mode_active()) {
104105 flush_hash_page(vpn, rpte, psize, ssize, mm_is_thread_local(mm));
105106 put_cpu_var(ppc64_tlb_batch);
106107 return;
@@ -154,6 +155,7 @@ void __flush_tlb_pending(struct ppc64_tlb_batch *batch) · 47 unmodified lines
154155 flush_hash_range(i, local);
155156 batch->index = 0;
156157 }
158+EXPORT_SYMBOL_IF_KUNIT(__flush_tlb_pending);
157159
158160 void hash__tlb_flush(struct mmu_gather *tlb)
159161 {
@@ -205,7 +207,7 @@ void __flush_hash_table_range(unsigned long start, unsigned long end) · 45 unmodified lines
205207 * way to do things but is fine for our needs here.
206208 */
207209 local_irq_save(flags);
208− arch_enter_lazy_mmu_mode();
210+ lazy_mmu_mode_enable();
209211 for (; start < end; start += PAGE_SIZE) {
210212 pte_t *ptep = find_init_mm_pte(start, &hugepage_shift);
211213 unsigned long pte;
@@ -217,7 +219,7 @@ void __flush_hash_table_range(unsigned long start, unsigned long end) · 5 unmodified lines
217219 continue;
218220 hpte_need_flush(&init_mm, start, ptep, pte, hugepage_shift);
219221 }
220− arch_leave_lazy_mmu_mode();
222+ lazy_mmu_mode_disable();
221223 local_irq_restore(flags);
222224 }
223225
@@ -237,7 +239,7 @@ void flush_hash_table_pmd_range(struct mm_struct *mm, pmd_t *pmd, unsigned long · 13 unmodified lines
237239 * way to do things but is fine for our needs here.
238240 */
239241 local_irq_save(flags);
240− arch_enter_lazy_mmu_mode();
242+ lazy_mmu_mode_enable();
241243 start_pte = pte_offset_map(pmd, addr);
242244 if (!start_pte)
243245 goto out;
@@ -249,6 +251,6 @@ void flush_hash_table_pmd_range(struct mm_struct *mm, pmd_t *pmd, unsigned long · 5 unmodified lines
249251 }
250252 pte_unmap(start_pte);
251253 out:
252− arch_leave_lazy_mmu_mode();
254+ lazy_mmu_mode_disable();
253255 local_irq_restore(flags);
254256 }
arch/powerpc/mm/book3s64/pgtable.c+16−9
@@ -10,6 +10,7 @@ · 9 unmodified lines
1010 #include <linux/pkeys.h>
1111 #include <linux/debugfs.h>
1212 #include <linux/proc_fs.h>
13+#include <linux/page_table_check.h>
1314
1415 #include <asm/pgalloc.h>
1516 #include <asm/tlb.h>
@@ -127,7 +128,8 @@ void set_pmd_at(struct mm_struct *mm, unsigned long addr, · 111 unmodified lines
127128 WARN_ON(!(pmd_leaf(pmd)));
128129 #endif
129130 trace_hugepage_set_pmd(addr, pmd_val(pmd));
130− return set_pte_at(mm, addr, pmdp_ptep(pmdp), pmd_pte(pmd));
131+ page_table_check_pmd_set(mm, addr, pmdp, pmd);
132+ return set_pte_at_unchecked(mm, addr, pmdp_ptep(pmdp), pmd_pte(pmd));
131133 }
132134
133135 void set_pud_at(struct mm_struct *mm, unsigned long addr,
@@ -144,7 +146,8 @@ void set_pud_at(struct mm_struct *mm, unsigned long addr, · 10 unmodified lines
144146 WARN_ON(!(pud_leaf(pud)));
145147 #endif
146148 trace_hugepage_set_pud(addr, pud_val(pud));
147− return set_pte_at(mm, addr, pudp_ptep(pudp), pud_pte(pud));
149+ page_table_check_pud_set(mm, addr, pudp, pud);
150+ return set_pte_at_unchecked(mm, addr, pudp_ptep(pudp), pud_pte(pud));
148151 }
149152
150153 static void do_serialize(void *arg)
@@ -179,23 +182,27 @@ void serialize_against_pte_lookup(struct mm_struct *mm) · 28 unmodified lines
179182 pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
180183 pmd_t *pmdp)
181184 {
182− unsigned long old_pmd;
185+ pmd_t old_pmd;
183186
184187 VM_WARN_ON_ONCE(!pmd_present(*pmdp));
185− old_pmd = pmd_hugepage_update(vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID);
188+ old_pmd = __pmd(pmd_hugepage_update(vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID));
186189 flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
187− return __pmd(old_pmd);
190+ page_table_check_pmd_clear(vma->vm_mm, address, old_pmd);
191+
192+ return old_pmd;
188193 }
189194
190195 pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,
191196 pud_t *pudp)
192197 {
193− unsigned long old_pud;
198+ pud_t old_pud;
194199
195200 VM_WARN_ON_ONCE(!pud_present(*pudp));
196− old_pud = pud_hugepage_update(vma->vm_mm, address, pudp, _PAGE_PRESENT, _PAGE_INVALID);
201+ old_pud = __pud(pud_hugepage_update(vma->vm_mm, address, pudp, _PAGE_PRESENT, _PAGE_INVALID));
197202 flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE);
198− return __pud(old_pud);
203+ page_table_check_pud_clear(vma->vm_mm, address, old_pud);
204+
205+ return old_pud;
199206 }
200207
201208 pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma,
@@ -550,7 +557,7 @@ void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, · 348 unmodified lines
550557 if (radix_enabled())
551558 return radix__ptep_modify_prot_commit(vma, addr,
552559 ptep, old_pte, pte);
553− set_pte_at(vma->vm_mm, addr, ptep, pte);
560+ set_pte_at_unchecked(vma->vm_mm, addr, ptep, pte);
554561 }
555562
556563 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
arch/powerpc/mm/book3s64/radix_pgtable.c+6−3
@@ -14,6 +14,7 @@ · 13 unmodified lines
1414 #include <linux/of.h>
1515 #include <linux/of_fdt.h>
1616 #include <linux/mm.h>
17+#include <linux/page_table_check.h>
1718 #include <linux/hugetlb.h>
1819 #include <linux/string_helpers.h>
1920 #include <linux/memory.h>
@@ -1474,6 +1475,8 @@ pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long addre · 1454 unmodified lines
14741475 pmd = *pmdp;
14751476 pmd_clear(pmdp);
14761477
1478+ page_table_check_pmd_clear(vma->vm_mm, address, pmd);
1479+
14771480 radix__flush_tlb_collapsed_pmd(vma->vm_mm, address);
14781481
14791482 return pmd;
@@ -1606,7 +1609,7 @@ void radix__ptep_modify_prot_commit(struct vm_area_struct *vma, · 126 unmodified lines
16061609 (atomic_read(&mm->context.copros) > 0))
16071610 radix__flush_tlb_page(vma, addr);
16081611
1609− set_pte_at(mm, addr, ptep, pte);
1612+ set_pte_at_unchecked(mm, addr, ptep, pte);
16101613 }
16111614
16121615 int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
@@ -1617,7 +1620,7 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot) · 4 unmodified lines
16171620 if (!radix_enabled())
16181621 return 0;
16191622
1620− set_pte_at(&init_mm, 0 /* radix unused */, ptep, new_pud);
1623+ set_pte_at_unchecked(&init_mm, 0 /* radix unused */, ptep, new_pud);
16211624
16221625 return 1;
16231626 }
@@ -1664,7 +1667,7 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot) · 40 unmodified lines
16641667 if (!radix_enabled())
16651668 return 0;
16661669
1667− set_pte_at(&init_mm, 0 /* radix unused */, ptep, new_pmd);
1670+ set_pte_at_unchecked(&init_mm, 0 /* radix unused */, ptep, new_pmd);
16681671
16691672 return 1;
16701673 }
arch/powerpc/mm/book3s64/subpage_prot.c+2−2
@@ -73,13 +73,13 @@ static void hpte_flush_range(struct mm_struct *mm, unsigned long addr, · 72 unmodified lines
7373 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
7474 if (!pte)
7575 return;
76− arch_enter_lazy_mmu_mode();
76+ lazy_mmu_mode_enable();
7777 for (; npages > 0; --npages) {
7878 pte_update(mm, addr, pte, 0, 0, 0);
7979 addr += PAGE_SIZE;
8080 ++pte;
8181 }
82− arch_leave_lazy_mmu_mode();
82+ lazy_mmu_mode_disable();
8383 pte_unmap_unlock(pte - 1, ptl);
8484 }
8585
arch/powerpc/mm/hugetlbpage.c+4−7
@@ -200,18 +200,15 @@ static int __init hugetlbpage_init(void) · 199 unmodified lines
200200
201201 arch_initcall(hugetlbpage_init);
202202
203−void __init gigantic_hugetlb_cma_reserve(void)
203+unsigned int __init arch_hugetlb_cma_order(void)
204204 {
205− unsigned long order = 0;
206−
207205 if (radix_enabled())
208− order = PUD_SHIFT - PAGE_SHIFT;
206+ return PUD_SHIFT - PAGE_SHIFT;
209207 else if (!firmware_has_feature(FW_FEATURE_LPAR) && mmu_psize_defs[MMU_PAGE_16G].shift)
210208 /*
211209 * For pseries we do use ibm,expected#pages for reserving 16G pages.
212210 */
213− order = mmu_psize_to_shift(MMU_PAGE_16G) - PAGE_SHIFT;
211+ return mmu_psize_to_shift(MMU_PAGE_16G) - PAGE_SHIFT;
214212
215− if (order)
216− hugetlb_cma_reserve(order);
213+ return 0;
217214 }
arch/powerpc/mm/mem.c+10−17
@@ -182,11 +182,6 @@ void __init mem_topology_setup(void) · 181 unmodified lines
182182 memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
183183 }
184184
185−void __init initmem_init(void)
186−{
187− sparse_init();
188−}
189−
190185 /* mark pages that don't exist as nosave */
191186 static int __init mark_nonram_nosave(void)
192187 {
@@ -221,7 +216,16 @@ static int __init mark_nonram_nosave(void) · 28 unmodified lines
221216 * anyway) will take a first dip into ZONE_NORMAL and get otherwise served by
222217 * ZONE_DMA.
223218 */
224−static unsigned long max_zone_pfns[MAX_NR_ZONES];
219+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
220+{
221+#ifdef CONFIG_ZONE_DMA
222+ max_zone_pfns[ZONE_DMA] = min((zone_dma_limit >> PAGE_SHIFT) + 1, max_low_pfn);
223+#endif
224+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
225+#ifdef CONFIG_HIGHMEM
226+ max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
227+#endif
228+}
225229
226230 /*
227231 * paging_init() sets up the page tables - in fact we've already done this.
@@ -259,17 +263,6 @@ void __init paging_init(void) · 31 unmodified lines
259263
260264 zone_dma_limit = DMA_BIT_MASK(zone_dma_bits);
261265
262−#ifdef CONFIG_ZONE_DMA
263− max_zone_pfns[ZONE_DMA] = min(max_low_pfn,
264− 1UL << (zone_dma_bits - PAGE_SHIFT));
265−#endif
266− max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
267−#ifdef CONFIG_HIGHMEM
268− max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
269−#endif
270−
271− free_area_init(max_zone_pfns);
272−
273266 mark_nonram_nosave();
274267 }
275268
arch/powerpc/mm/numa.c+0−2
@@ -1213,8 +1213,6 @@ void __init initmem_init(void) · 1212 unmodified lines
12131213 setup_node_data(nid, start_pfn, end_pfn);
12141214 }
12151215
1216− sparse_init();
1217−
12181216 /*
12191217 * We need the numa_cpu_lookup_table to be accurate for all CPUs,
12201218 * even before we online them, so that we can use cpu_to_{node,mem}
arch/powerpc/mm/pgtable.c+12−0
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #include <linux/mm.h>
2323 #include <linux/percpu.h>
2424 #include <linux/hardirq.h>
25+#include <linux/page_table_check.h>
2526 #include <linux/hugetlb.h>
2627 #include <asm/tlbflush.h>
2728 #include <asm/tlb.h>
@@ -206,6 +207,9 @@ void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, · 178 unmodified lines
206207 * and not hw_valid ptes. Hence there is no translation cache flush
207208 * involved that need to be batched.
208209 */
210+
211+ page_table_check_ptes_set(mm, addr, ptep, pte, nr);
212+
209213 for (;;) {
210214
211215 /*
@@ -224,6 +228,14 @@ void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, · 12 unmodified lines
224228 }
225229 }
226230
231+void set_pte_at_unchecked(struct mm_struct *mm, unsigned long addr,
232+ pte_t *ptep, pte_t pte)
233+{
234+ VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
235+ pte = set_pte_filter(pte, addr);
236+ __set_pte_at(mm, addr, ptep, pte, 0);
237+}
238+
227239 void unmap_kernel_page(unsigned long va)
228240 {
229241 pmd_t *pmdp = pmd_off_k(va);
arch/powerpc/platforms/Kconfig.cputype+1−0
@@ -93,6 +93,7 @@ config PPC_BOOK3S_64 · 92 unmodified lines
9393 select IRQ_WORK
9494 select PPC_64S_HASH_MMU if !PPC_RADIX_MMU
9595 select KASAN_VMALLOC if KASAN
96+ select ARCH_HAS_LAZY_MMU_MODE
9697
9798 config PPC_BOOK3E_64
9899 bool "Embedded processors"
arch/powerpc/platforms/pseries/Kconfig+1−1
@@ -120,7 +120,7 @@ config PPC_SMLPAR · 119 unmodified lines
120120 config CMM
121121 tristate "Collaborative memory management"
122122 depends on PPC_SMLPAR
123− select MEMORY_BALLOON
123+ select BALLOON
124124 default y
125125 help
126126 Select this option, if you want to enable the kernel interface
arch/powerpc/platforms/pseries/cmm.c+10−43
@@ -19,7 +19,7 @@ · 18 unmodified lines
1919 #include <linux/stringify.h>
2020 #include <linux/swap.h>
2121 #include <linux/device.h>
22−#include <linux/balloon_compaction.h>
22+#include <linux/balloon.h>
2323 #include <asm/firmware.h>
2424 #include <asm/hvcall.h>
2525 #include <asm/mmu.h>
@@ -165,7 +165,6 @@ static long cmm_alloc_pages(long nr) · 139 unmodified lines
165165
166166 balloon_page_enqueue(&b_dev_info, page);
167167 atomic_long_inc(&loaned_pages);
168− adjust_managed_page_count(page, -1);
169168 nr--;
170169 }
171170
@@ -190,7 +189,6 @@ static long cmm_free_pages(long nr) · 18 unmodified lines
190189 if (!page)
191190 break;
192191 plpar_page_set_active(page);
193− adjust_managed_page_count(page, 1);
194192 __free_page(page);
195193 atomic_long_dec(&loaned_pages);
196194 nr--;
@@ -496,13 +494,11 @@ static struct notifier_block cmm_mem_nb = { · 299 unmodified lines
496494 .priority = CMM_MEM_HOTPLUG_PRI
497495 };
498496
499−#ifdef CONFIG_BALLOON_COMPACTION
497+#ifdef CONFIG_BALLOON_MIGRATION
500498 static int cmm_migratepage(struct balloon_dev_info *b_dev_info,
501499 struct page *newpage, struct page *page,
502500 enum migrate_mode mode)
503501 {
504− unsigned long flags;
505−
506502 /*
507503 * loan/"inflate" the newpage first.
508504 *
@@ -517,47 +513,17 @@ static int cmm_migratepage(struct balloon_dev_info *b_dev_info, · 8 unmodified lines
517513 return -EBUSY;
518514 }
519515
520− /* balloon page list reference */
521− get_page(newpage);
522−
523− /*
524− * When we migrate a page to a different zone, we have to fixup the
525− * count of both involved zones as we adjusted the managed page count
526− * when inflating.
527− */
528− if (page_zone(page) != page_zone(newpage)) {
529− adjust_managed_page_count(page, 1);
530− adjust_managed_page_count(newpage, -1);
531− }
532−
533− spin_lock_irqsave(&b_dev_info->pages_lock, flags);
534− balloon_page_insert(b_dev_info, newpage);
535− __count_vm_event(BALLOON_MIGRATE);
536− b_dev_info->isolated_pages--;
537− spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
538−
539516 /*
540517 * activate/"deflate" the old page. We ignore any errors just like the
541518 * other callers.
542519 */
543520 plpar_page_set_active(page);
544−
545− balloon_page_finalize(page);
546− /* balloon page list reference */
547− put_page(page);
548−
549521 return 0;
550522 }
551−
552−static void cmm_balloon_compaction_init(void)
553−{
554− b_dev_info.migratepage = cmm_migratepage;
555−}
556−#else /* CONFIG_BALLOON_COMPACTION */
557−static void cmm_balloon_compaction_init(void)
558−{
559−}
560−#endif /* CONFIG_BALLOON_COMPACTION */
523+#else /* CONFIG_BALLOON_MIGRATION */
524+int cmm_migratepage(struct balloon_dev_info *b_dev_info, struct page *newpage,
525+ struct page *page, enum migrate_mode mode);
526+#endif /* CONFIG_BALLOON_MIGRATION */
561527
562528 /**
563529 * cmm_init - Module initialization
@@ -573,11 +539,13 @@ static int cmm_init(void) · 9 unmodified lines
573539 return -EOPNOTSUPP;
574540
575541 balloon_devinfo_init(&b_dev_info);
576− cmm_balloon_compaction_init();
542+ b_dev_info.adjust_managed_page_count = true;
543+ if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))
544+ b_dev_info.migratepage = cmm_migratepage;
577545
578546 rc = register_oom_notifier(&cmm_oom_nb);
579547 if (rc < 0)
580− goto out_balloon_compaction;
548+ return rc;
581549
582550 if ((rc = register_reboot_notifier(&cmm_reboot_nb)))
583551 goto out_oom_notifier;
@@ -606,7 +574,6 @@ out_reboot_notifier: · 22 unmodified lines
606574 unregister_reboot_notifier(&cmm_reboot_nb);
607575 out_oom_notifier:
608576 unregister_oom_notifier(&cmm_oom_nb);
609−out_balloon_compaction:
610577 return rc;
611578 }
612579
arch/riscv/include/asm/page.h+0−1
@@ -50,7 +50,6 @@ void clear_page(void *page); · 49 unmodified lines
5050 #endif
5151 #define copy_page(to, from) memcpy((to), (from), PAGE_SIZE)
5252
53−#define clear_user_page(pgaddr, vaddr, page) clear_page(pgaddr)
5453 #define copy_user_page(vto, vfrom, vaddr, topg) \
5554 memcpy((vto), (vfrom), PAGE_SIZE)
5655
arch/riscv/include/asm/pgtable.h+11−11
@@ -627,7 +627,7 @@ static inline void __set_pte_at(struct mm_struct *mm, pte_t *ptep, pte_t pteval) · 626 unmodified lines
627627 static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
628628 pte_t *ptep, pte_t pteval, unsigned int nr)
629629 {
630− page_table_check_ptes_set(mm, ptep, pteval, nr);
630+ page_table_check_ptes_set(mm, addr, ptep, pteval, nr);
631631
632632 for (;;) {
633633 __set_pte_at(mm, ptep, pteval);
@@ -664,7 +664,7 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, · 30 unmodified lines
664664 set_pte(ptep, __pte(0));
665665 #endif
666666
667− page_table_check_pte_clear(mm, pte);
667+ page_table_check_pte_clear(mm, address, pte);
668668
669669 return pte;
670670 }
@@ -946,29 +946,29 @@ static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd) · 275 unmodified lines
946946 static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
947947 pmd_t *pmdp, pmd_t pmd)
948948 {
949− page_table_check_pmd_set(mm, pmdp, pmd);
949+ page_table_check_pmd_set(mm, addr, pmdp, pmd);
950950 return __set_pte_at(mm, (pte_t *)pmdp, pmd_pte(pmd));
951951 }
952952
953953 static inline void set_pud_at(struct mm_struct *mm, unsigned long addr,
954954 pud_t *pudp, pud_t pud)
955955 {
956− page_table_check_pud_set(mm, pudp, pud);
956+ page_table_check_pud_set(mm, addr, pudp, pud);
957957 return __set_pte_at(mm, (pte_t *)pudp, pud_pte(pud));
958958 }
959959
960960 #ifdef CONFIG_PAGE_TABLE_CHECK
961−static inline bool pte_user_accessible_page(pte_t pte)
961+static inline bool pte_user_accessible_page(pte_t pte, unsigned long addr)
962962 {
963963 return pte_present(pte) && pte_user(pte);
964964 }
965965
966−static inline bool pmd_user_accessible_page(pmd_t pmd)
966+static inline bool pmd_user_accessible_page(pmd_t pmd, unsigned long addr)
967967 {
968968 return pmd_leaf(pmd) && pmd_user(pmd);
969969 }
970970
971−static inline bool pud_user_accessible_page(pud_t pud)
971+static inline bool pud_user_accessible_page(pud_t pud, unsigned long addr)
972972 {
973973 return pud_leaf(pud) && pud_user(pud);
974974 }
@@ -1007,7 +1007,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, · 32 unmodified lines
10071007 pmd_clear(pmdp);
10081008 #endif
10091009
1010− page_table_check_pmd_clear(mm, pmd);
1010+ page_table_check_pmd_clear(mm, address, pmd);
10111011
10121012 return pmd;
10131013 }
@@ -1023,7 +1023,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm, · 9 unmodified lines
10231023 static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
10241024 unsigned long address, pmd_t *pmdp, pmd_t pmd)
10251025 {
1026− page_table_check_pmd_set(vma->vm_mm, pmdp, pmd);
1026+ page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd);
10271027 return __pmd(atomic_long_xchg((atomic_long_t *)pmdp, pmd_val(pmd)));
10281028 }
10291029
@@ -1101,7 +1101,7 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, · 71 unmodified lines
11011101 pud_clear(pudp);
11021102 #endif
11031103
1104− page_table_check_pud_clear(mm, pud);
1104+ page_table_check_pud_clear(mm, address, pud);
11051105
11061106 return pud;
11071107 }
@@ -1122,7 +1122,7 @@ static inline void update_mmu_cache_pud(struct vm_area_struct *vma, · 14 unmodified lines
11221122 static inline pud_t pudp_establish(struct vm_area_struct *vma,
11231123 unsigned long address, pud_t *pudp, pud_t pud)
11241124 {
1125− page_table_check_pud_set(vma->vm_mm, pudp, pud);
1125+ page_table_check_pud_set(vma->vm_mm, address, pudp, pud);
11261126 return __pud(atomic_long_xchg((atomic_long_t *)pudp, pud_val(pud)));
11271127 }
11281128
arch/riscv/mm/hugetlbpage.c+8−0
@@ -447,3 +447,11 @@ static __init int gigantic_pages_init(void) · 446 unmodified lines
447447 }
448448 arch_initcall(gigantic_pages_init);
449449 #endif
450+
451+unsigned int __init arch_hugetlb_cma_order(void)
452+{
453+ if (IS_ENABLED(CONFIG_64BIT))
454+ return PUD_SHIFT - PAGE_SHIFT;
455+
456+ return 0;
457+}
arch/riscv/mm/init.c+1−9
@@ -79,16 +79,12 @@ uintptr_t _dtb_early_pa __initdata; · 78 unmodified lines
7979
8080 phys_addr_t dma32_phys_limit __initdata;
8181
82−static void __init zone_sizes_init(void)
82+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
8383 {
84− unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
85−
8684 #ifdef CONFIG_ZONE_DMA32
8785 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
8886 #endif
8987 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
90−
91− free_area_init(max_zone_pfns);
9288 }
9389
9490 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
@@ -315,8 +311,6 @@ static void __init setup_bootmem(void) · 220 unmodified lines
315311 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
316312
317313 dma_contiguous_reserve(dma32_phys_limit);
318− if (IS_ENABLED(CONFIG_64BIT))
319− hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
320314 }
321315
322316 #ifdef CONFIG_RELOCATABLE
@@ -1434,12 +1428,10 @@ void __init misc_mem_init(void) · 1111 unmodified lines
14341428 {
14351429 early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
14361430 arch_numa_init();
1437− sparse_init();
14381431 #ifdef CONFIG_SPARSEMEM_VMEMMAP
14391432 /* The entire VMEMMAP region has been populated. Flush TLB for this region */
14401433 local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
14411434 #endif
1442− zone_sizes_init();
14431435 arch_reserve_crashkernel();
14441436 memblock_dump_all();
14451437 }
arch/s390/include/asm/page.h+0−1
@@ -65,7 +65,6 @@ static inline void copy_page(void *to, void *from) · 64 unmodified lines
6565 : : "memory", "cc");
6666 }
6767
68−#define clear_user_page(page, vaddr, pg) clear_page(page)
6968 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
7069
7170 #define vma_alloc_zeroed_movable_folio(vma, vaddr) \
arch/s390/include/asm/tlb.h+2−4
@@ -24,7 +24,7 @@ · 23 unmodified lines
2424
2525 static inline void tlb_flush(struct mmu_gather *tlb);
2626 static inline bool __tlb_remove_page_size(struct mmu_gather *tlb,
27− struct page *page, bool delay_rmap, int page_size);
27+ struct page *page, int page_size);
2828 static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb,
2929 struct page *page, unsigned int nr_pages, bool delay_rmap);
3030
@@ -46,10 +46,8 @@ static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb, · 15 unmodified lines
4646 * s390 doesn't delay rmap removal.
4747 */
4848 static inline bool __tlb_remove_page_size(struct mmu_gather *tlb,
49− struct page *page, bool delay_rmap, int page_size)
49+ struct page *page, int page_size)
5050 {
51− VM_WARN_ON_ONCE(delay_rmap);
52−
5351 free_folio_and_swap_cache(page_folio(page));
5452 return false;
5553 }
arch/s390/kernel/setup.c+0−2
@@ -963,8 +963,6 @@ void __init setup_arch(char **cmdline_p) · 962 unmodified lines
963963 setup_uv();
964964 dma_contiguous_reserve(ident_map_size);
965965 vmcp_cma_reserve();
966− if (cpu_has_edat2())
967− hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
968966
969967 reserve_crashkernel();
970968 #ifdef CONFIG_CRASH_DUMP
arch/s390/mm/gmap_helpers.c+1−1
@@ -32,7 +32,7 @@ static void ptep_zap_softleaf_entry(struct mm_struct *mm, softleaf_t entry) · 31 unmodified lines
3232 dec_mm_counter(mm, MM_SWAPENTS);
3333 else if (softleaf_is_migration(entry))
3434 dec_mm_counter(mm, mm_counter(softleaf_to_folio(entry)));
35− free_swap_and_cache(entry);
35+ swap_put_entries_direct(entry, 1);
3636 }
3737
3838 /**
arch/s390/mm/hugetlbpage.c+8−0
@@ -255,3 +255,11 @@ bool __init arch_hugetlb_valid_size(unsigned long size) · 254 unmodified lines
255255 else
256256 return false;
257257 }
258+
259+unsigned int __init arch_hugetlb_cma_order(void)
260+{
261+ if (cpu_has_edat2())
262+ return PUD_SHIFT - PAGE_SHIFT;
263+
264+ return 0;
265+}
arch/s390/mm/init.c+6−7
@@ -86,20 +86,19 @@ static void __init setup_zero_pages(void) · 85 unmodified lines
8686 zero_page_mask = ((PAGE_SIZE << order) - 1) & PAGE_MASK;
8787 }
8888
89+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
90+{
91+ max_zone_pfns[ZONE_DMA] = virt_to_pfn(MAX_DMA_ADDRESS);
92+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
93+}
94+
8995 /*
9096 * paging_init() sets up the page tables
9197 */
9298 void __init paging_init(void)
9399 {
94− unsigned long max_zone_pfns[MAX_NR_ZONES];
95−
96100 vmem_map_init();
97− sparse_init();
98101 zone_dma_limit = DMA_BIT_MASK(31);
99− memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
100− max_zone_pfns[ZONE_DMA] = virt_to_pfn(MAX_DMA_ADDRESS);
101− max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
102− free_area_init(max_zone_pfns);
103102 }
104103
105104 void mark_rodata_ro(void)
arch/s390/mm/pgtable.c+1−1
@@ -682,7 +682,7 @@ static void ptep_zap_softleaf_entry(struct mm_struct *mm, softleaf_t entry) · 681 unmodified lines
682682
683683 dec_mm_counter(mm, mm_counter(folio));
684684 }
685− free_swap_and_cache(entry);
685+ swap_put_entries_direct(entry, 1);
686686 }
687687
688688 void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
arch/sh/mm/init.c+5−7
@@ -227,8 +227,6 @@ static void __init do_init_bootmem(void) · 226 unmodified lines
227227 node_set_online(0);
228228
229229 plat_mem_setup();
230−
231− sparse_init();
232230 }
233231
234232 static void __init early_reserve_mem(void)
@@ -264,9 +262,13 @@ static void __init early_reserve_mem(void) · 29 unmodified lines
264262 reserve_crashkernel();
265263 }
266264
265+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
266+{
267+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
268+}
269+
267270 void __init paging_init(void)
268271 {
269− unsigned long max_zone_pfns[MAX_NR_ZONES];
270272 unsigned long vaddr, end;
271273
272274 sh_mv.mv_mem_init();
@@ -320,10 +322,6 @@ void __init paging_init(void) · 47 unmodified lines
320322 page_table_range_init(vaddr, end, swapper_pg_dir);
321323
322324 kmap_coherent_init();
323−
324− memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
325− max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
326− free_area_init(max_zone_pfns);
327325 }
328326
329327 unsigned int mem_init_done = 0;
arch/sparc/Kconfig+2−0
@@ -75,6 +75,7 @@ config SPARC64 · 74 unmodified lines
7575 select HAVE_KRETPROBES
7676 select HAVE_KPROBES
7777 select MMU_GATHER_RCU_TABLE_FREE if SMP
78+ select HAVE_ARCH_TLB_REMOVE_TABLE if SMP
7879 select MMU_GATHER_MERGE_VMAS
7980 select MMU_GATHER_NO_FLUSH_CACHE
8081 select HAVE_ARCH_TRANSPARENT_HUGEPAGE
@@ -113,6 +114,7 @@ config SPARC64 · 32 unmodified lines
113114 select NEED_PER_CPU_PAGE_FIRST_CHUNK
114115 select ARCH_SUPPORTS_SCHED_SMT if SMP
115116 select ARCH_SUPPORTS_SCHED_MC if SMP
117+ select ARCH_HAS_LAZY_MMU_MODE
116118
117119 config ARCH_PROC_KCORE_TEXT
118120 def_bool y
arch/sparc/include/asm/page_64.h+1−0
@@ -43,6 +43,7 @@ void _clear_page(void *page); · 42 unmodified lines
4343 #define clear_page(X) _clear_page((void *)(X))
4444 struct page;
4545 void clear_user_page(void *addr, unsigned long vaddr, struct page *page);
46+#define clear_user_page clear_user_page
4647 #define copy_page(X,Y) memcpy((void *)(X), (void *)(Y), PAGE_SIZE)
4748 void copy_user_page(void *to, void *from, unsigned long vaddr, struct page *topage);
4849 #define __HAVE_ARCH_COPY_USER_HIGHPAGE
arch/sparc/include/asm/tlb_64.h+0−1
@@ -33,7 +33,6 @@ void flush_tlb_pending(void); · 32 unmodified lines
3333 #define tlb_needs_table_invalidate() (false)
3434 #endif
3535
36−#define __HAVE_ARCH_TLB_REMOVE_TABLE
3736 #include <asm-generic/tlb.h>
3837
3938 #endif /* _SPARC64_TLB_H */
arch/sparc/include/asm/tlbflush_64.h+1−4
@@ -12,7 +12,6 @@ struct tlb_batch { · 11 unmodified lines
1212 unsigned int hugepage_shift;
1313 struct mm_struct *mm;
1414 unsigned long tlb_nr;
15− unsigned long active;
1615 unsigned long vaddrs[TLB_BATCH_NR];
1716 };
1817
@@ -39,12 +38,10 @@ static inline void flush_tlb_range(struct vm_area_struct *vma, · 20 unmodified lines
3938
4039 void flush_tlb_kernel_range(unsigned long start, unsigned long end);
4140
42−#define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
43−
4441 void flush_tlb_pending(void);
4542 void arch_enter_lazy_mmu_mode(void);
43+void arch_flush_lazy_mmu_mode(void);
4644 void arch_leave_lazy_mmu_mode(void);
47−#define arch_flush_lazy_mmu_mode() do {} while (0)
4845
4946 /* Local cpu only. */
5047 void __flush_tlb_all(void);
arch/sparc/mm/init_64.c+5−12
@@ -1609,8 +1609,6 @@ static unsigned long __init bootmem_init(unsigned long phys_base) · 1608 unmodified lines
16091609
16101610 /* XXX cpu notifier XXX */
16111611
1612− sparse_init();
1613−
16141612 return end_pfn;
16151613 }
16161614
@@ -2273,6 +2271,11 @@ static void __init reduce_memory(phys_addr_t limit_ram) · 656 unmodified lines
22732271 memblock_enforce_memory_limit(limit_ram);
22742272 }
22752273
2274+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
2275+{
2276+ max_zone_pfns[ZONE_NORMAL] = last_valid_pfn;
2277+}
2278+
22762279 void __init paging_init(void)
22772280 {
22782281 unsigned long end_pfn, shift, phys_base;
@@ -2448,16 +2451,6 @@ void __init paging_init(void) · 169 unmodified lines
24482451
24492452 kernel_physical_mapping_init();
24502453
2451− {
2452− unsigned long max_zone_pfns[MAX_NR_ZONES];
2453−
2454− memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
2455−
2456− max_zone_pfns[ZONE_NORMAL] = end_pfn;
2457−
2458− free_area_init(max_zone_pfns);
2459− }
2460−
24612454 printk("Booting Linux...\n");
24622455 }
24632456
arch/sparc/mm/srmmu.c+7−10
@@ -884,6 +884,13 @@ static void __init map_kernel(void) · 883 unmodified lines
884884
885885 void (*poke_srmmu)(void) = NULL;
886886
887+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
888+{
889+ max_zone_pfns[ZONE_DMA] = max_low_pfn;
890+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
891+ max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
892+}
893+
887894 void __init srmmu_paging_init(void)
888895 {
889896 int i;
@@ -963,16 +970,6 @@ void __init srmmu_paging_init(void) · 73 unmodified lines
963970 flush_tlb_all();
964971
965972 sparc_context_init(num_contexts);
966−
967− {
968− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
969−
970− max_zone_pfn[ZONE_DMA] = max_low_pfn;
971− max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
972− max_zone_pfn[ZONE_HIGHMEM] = highend_pfn;
973−
974− free_area_init(max_zone_pfn);
975− }
976973 }
977974
978975 void mmu_info(struct seq_file *m)
arch/sparc/mm/tlb.c+13−7
@@ -11,6 +11,8 @@ · 10 unmodified lines
1111 #include <linux/preempt.h>
1212 #include <linux/pagemap.h>
1313
14+#include <kunit/visibility.h>
15+
1416 #include <asm/tlbflush.h>
1517 #include <asm/cacheflush.h>
1618 #include <asm/mmu_context.h>
@@ -52,22 +54,26 @@ out: · 35 unmodified lines
5254
5355 void arch_enter_lazy_mmu_mode(void)
5456 {
55− struct tlb_batch *tb;
56−
5757 preempt_disable();
58− tb = this_cpu_ptr(&tlb_batch);
59− tb->active = 1;
6058 }
59+/* For lazy_mmu_mode KUnit tests */
60+EXPORT_SYMBOL_IF_KUNIT(arch_enter_lazy_mmu_mode);
6161
62−void arch_leave_lazy_mmu_mode(void)
62+void arch_flush_lazy_mmu_mode(void)
6363 {
6464 struct tlb_batch *tb = this_cpu_ptr(&tlb_batch);
6565
6666 if (tb->tlb_nr)
6767 flush_tlb_pending();
68− tb->active = 0;
68+}
69+EXPORT_SYMBOL_IF_KUNIT(arch_flush_lazy_mmu_mode);
70+
71+void arch_leave_lazy_mmu_mode(void)
72+{
73+ arch_flush_lazy_mmu_mode();
6974 preempt_enable();
7075 }
76+EXPORT_SYMBOL_IF_KUNIT(arch_leave_lazy_mmu_mode);
7177
7278 static void tlb_batch_add_one(struct mm_struct *mm, unsigned long vaddr,
7379 bool exec, unsigned int hugepage_shift)
@@ -86,7 +92,7 @@ static void tlb_batch_add_one(struct mm_struct *mm, unsigned long vaddr, · 12 unmodified lines
8692 nr = 0;
8793 }
8894
89− if (!tb->active) {
95+ if (!is_lazy_mmu_mode_active()) {
9096 flush_tsb_user_page(mm, vaddr, hugepage_shift);
9197 global_flush_tlb_page(mm, vaddr);
9298 goto out;
arch/um/Kconfig+1−0
@@ -42,6 +42,7 @@ config UML · 41 unmodified lines
4242 select HAVE_SYSCALL_TRACEPOINTS
4343 select THREAD_INFO_IN_TASK
4444 select SPARSE_IRQ
45+ select MMU_GATHER_RCU_TABLE_FREE
4546
4647 config MMU
4748 bool
arch/um/include/asm/page.h+0−1
@@ -26,7 +26,6 @@ struct page; · 25 unmodified lines
2626 #define clear_page(page) memset((void *)(page), 0, PAGE_SIZE)
2727 #define copy_page(to,from) memcpy((void *)(to), (void *)(from), PAGE_SIZE)
2828
29−#define clear_user_page(page, vaddr, pg) clear_page(page)
3029 #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
3130
3231 typedef struct { unsigned long pte; } pte_t;
arch/um/kernel/mem.c+5−5
@@ -84,18 +84,18 @@ void __init mem_init(void) · 83 unmodified lines
8484 kmalloc_ok = 1;
8585 }
8686
87−void __init paging_init(void)
87+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
8888 {
89− unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
89+ max_zone_pfns[ZONE_NORMAL] = high_physmem >> PAGE_SHIFT;
90+}
9091
92+void __init paging_init(void)
93+{
9194 empty_zero_page = (unsigned long *) memblock_alloc_low(PAGE_SIZE,
9295 PAGE_SIZE);
9396 if (!empty_zero_page)
9497 panic("%s: Failed to allocate %lu bytes align=%lx\n",
9598 __func__, PAGE_SIZE, PAGE_SIZE);
96−
97− max_zone_pfn[ZONE_NORMAL] = high_physmem >> PAGE_SHIFT;
98− free_area_init(max_zone_pfn);
9999 }
100100
101101 /*
arch/x86/Kconfig+1−1
@@ -331,7 +331,6 @@ config X86 · 330 unmodified lines
331331 select FUNCTION_ALIGNMENT_4B
332332 imply IMA_SECURE_AND_OR_TRUSTED_BOOT if EFI
333333 select HAVE_DYNAMIC_FTRACE_NO_PATCHABLE
334− select ARCH_SUPPORTS_PT_RECLAIM if X86_64
335334 select ARCH_SUPPORTS_SCHED_SMT if SMP
336335 select SCHED_SMT if SMP
337336 select ARCH_SUPPORTS_SCHED_CLUSTER if SMP
@@ -823,6 +822,7 @@ config PARAVIRT · 485 unmodified lines
823822 config PARAVIRT_XXL
824823 bool
825824 depends on X86_64
825+ select ARCH_HAS_LAZY_MMU_MODE
826826
827827 config PARAVIRT_SPINLOCKS
828828 bool "Paravirtualization layer for spinlocks"
arch/x86/boot/compressed/misc.h+1−0
@@ -11,6 +11,7 @@ · 10 unmodified lines
1111 #undef CONFIG_PARAVIRT
1212 #undef CONFIG_PARAVIRT_XXL
1313 #undef CONFIG_PARAVIRT_SPINLOCKS
14+#undef CONFIG_ARCH_HAS_LAZY_MMU_MODE
1415 #undef CONFIG_KASAN
1516 #undef CONFIG_KASAN_GENERIC
1617
arch/x86/boot/startup/sme.c+1−0
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424 #undef CONFIG_PARAVIRT
2525 #undef CONFIG_PARAVIRT_XXL
2626 #undef CONFIG_PARAVIRT_SPINLOCKS
27+#undef CONFIG_ARCH_HAS_LAZY_MMU_MODE
2728
2829 /*
2930 * This code runs before CPU feature bits are set. By default, the
arch/x86/include/asm/page.h+0−6
@@ -22,12 +22,6 @@ struct page; · 21 unmodified lines
2222 extern struct range pfn_mapped[];
2323 extern int nr_pfn_mapped;
2424
25−static inline void clear_user_page(void *page, unsigned long vaddr,
26− struct page *pg)
27−{
28− clear_page(page);
29−}
30−
3125 static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
3226 struct page *topage)
3327 {
arch/x86/include/asm/page_32.h+6−0
@@ -17,6 +17,12 @@ extern unsigned long __phys_addr(unsigned long); · 16 unmodified lines
1717
1818 #include <linux/string.h>
1919
20+/**
21+ * clear_page() - clear a page using a kernel virtual address.
22+ * @page: address of kernel page
23+ *
24+ * Does absolutely no exception handling.
25+ */
2026 static inline void clear_page(void *page)
2127 {
2228 memset(page, 0, PAGE_SIZE);
arch/x86/include/asm/page_64.h+61−17
@@ -48,26 +48,70 @@ static inline unsigned long __phys_addr_symbol(unsigned long x) · 47 unmodified lines
4848
4949 #define __phys_reloc_hide(x) (x)
5050
51−void clear_page_orig(void *page);
52−void clear_page_rep(void *page);
53−void clear_page_erms(void *page);
54−KCFI_REFERENCE(clear_page_orig);
55−KCFI_REFERENCE(clear_page_rep);
56−KCFI_REFERENCE(clear_page_erms);
57−
58−static inline void clear_page(void *page)
51+void __clear_pages_unrolled(void *page);
52+KCFI_REFERENCE(__clear_pages_unrolled);
53+
54+/**
55+ * clear_pages() - clear a page range using a kernel virtual address.
56+ * @addr: start address of kernel page range
57+ * @npages: number of pages
58+ *
59+ * Switch between three implementations of page clearing based on CPU
60+ * capabilities:
61+ *
62+ * - __clear_pages_unrolled(): the oldest, slowest and universally
63+ * supported method. Zeroes via 8-byte MOV instructions unrolled 8x
64+ * to write a 64-byte cacheline in each loop iteration.
65+ *
66+ * - "REP; STOSQ": really old CPUs had crummy REP implementations.
67+ * Vendor CPU setup code sets 'REP_GOOD' on CPUs where REP can be
68+ * trusted. The instruction writes 8-byte per REP iteration but
69+ * CPUs can internally batch these together and do larger writes.
70+ *
71+ * - "REP; STOSB": used on CPUs with "enhanced REP MOVSB/STOSB",
72+ * which enumerate 'ERMS' and provide an implementation which
73+ * unlike "REP; STOSQ" above wasn't overly picky about alignment.
74+ * The instruction writes 1-byte per REP iteration with CPUs
75+ * internally batching these together into larger writes and is
76+ * generally fastest of the three.
77+ *
78+ * Note that when running as a guest, features exposed by the CPU
79+ * might be mediated by the hypervisor. So, the STOSQ variant might
80+ * be in active use on some systems even when the hardware enumerates
81+ * ERMS.
82+ *
83+ * Does absolutely no exception handling.
84+ */
85+static inline void clear_pages(void *addr, unsigned int npages)
5986 {
87+ u64 len = npages * PAGE_SIZE;
88+ /*
89+ * Clean up KMSAN metadata for the pages being cleared. The assembly call
90+ * below clobbers @addr, so perform unpoisoning before it.
91+ */
92+ kmsan_unpoison_memory(addr, len);
93+
6094 /*
61− * Clean up KMSAN metadata for the page being cleared. The assembly call
62− * below clobbers @page, so we perform unpoisoning before it.
95+ * The inline asm embeds a CALL instruction and usually that is a no-no
96+ * due to the compiler not knowing that and thus being unable to track
97+ * callee-clobbered registers.
98+ *
99+ * In this case that is fine because the registers clobbered by
100+ * __clear_pages_unrolled() are part of the inline asm register
101+ * specification.
63102 */
64− kmsan_unpoison_memory(page, PAGE_SIZE);
65− alternative_call_2(clear_page_orig,
66− clear_page_rep, X86_FEATURE_REP_GOOD,
67− clear_page_erms, X86_FEATURE_ERMS,
68− "=D" (page),
69− "D" (page),
70− "cc", "memory", "rax", "rcx");
103+ asm volatile(ALTERNATIVE_2("call __clear_pages_unrolled",
104+ "shrq $3, %%rcx; rep stosq", X86_FEATURE_REP_GOOD,
105+ "rep stosb", X86_FEATURE_ERMS)
106+ : "+c" (len), "+D" (addr), ASM_CALL_CONSTRAINT
107+ : "a" (0)
108+ : "cc", "memory");
109+}
110+#define clear_pages clear_pages
111+
112+static inline void clear_page(void *addr)
113+{
114+ clear_pages(addr, 1);
71115 }
72116
73117 void copy_page(void *to, void *from);
arch/x86/include/asm/paravirt.h+0−1
@@ -492,7 +492,6 @@ static inline void arch_end_context_switch(struct task_struct *next) · 491 unmodified lines
492492 PVOP_VCALL1(pv_ops, cpu.end_context_switch, next);
493493 }
494494
495−#define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
496495 static inline void arch_enter_lazy_mmu_mode(void)
497496 {
498497 PVOP_VCALL0(pv_ops, mmu.lazy_mode.enter);
arch/x86/include/asm/pgtable.h+12−11
@@ -118,6 +118,7 @@ extern pmdval_t early_pmd_flags; · 117 unmodified lines
118118 #define __pte(x) native_make_pte(x)
119119
120120 #define arch_end_context_switch(prev) do {} while(0)
121+static inline void arch_flush_lazy_mmu_mode(void) {}
121122 #endif /* CONFIG_PARAVIRT_XXL */
122123
123124 static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set)
@@ -1213,14 +1214,14 @@ static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp) · 1089 unmodified lines
12131214 static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
12141215 pmd_t *pmdp, pmd_t pmd)
12151216 {
1216− page_table_check_pmd_set(mm, pmdp, pmd);
1217+ page_table_check_pmd_set(mm, addr, pmdp, pmd);
12171218 set_pmd(pmdp, pmd);
12181219 }
12191220
12201221 static inline void set_pud_at(struct mm_struct *mm, unsigned long addr,
12211222 pud_t *pudp, pud_t pud)
12221223 {
1223− page_table_check_pud_set(mm, pudp, pud);
1224+ page_table_check_pud_set(mm, addr, pudp, pud);
12241225 native_set_pud(pudp, pud);
12251226 }
12261227
@@ -1251,7 +1252,7 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, · 24 unmodified lines
12511252 pte_t *ptep)
12521253 {
12531254 pte_t pte = native_ptep_get_and_clear(ptep);
1254− page_table_check_pte_clear(mm, pte);
1255+ page_table_check_pte_clear(mm, addr, pte);
12551256 return pte;
12561257 }
12571258
@@ -1267,7 +1268,7 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, · 9 unmodified lines
12671268 * care about updates and native needs no locking
12681269 */
12691270 pte = native_local_ptep_get_and_clear(ptep);
1270− page_table_check_pte_clear(mm, pte);
1271+ page_table_check_pte_clear(mm, addr, pte);
12711272 } else {
12721273 pte = ptep_get_and_clear(mm, addr, ptep);
12731274 }
@@ -1318,7 +1319,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long · 44 unmodified lines
13181319 {
13191320 pmd_t pmd = native_pmdp_get_and_clear(pmdp);
13201321
1321− page_table_check_pmd_clear(mm, pmd);
1322+ page_table_check_pmd_clear(mm, addr, pmd);
13221323
13231324 return pmd;
13241325 }
@@ -1329,7 +1330,7 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, · 4 unmodified lines
13291330 {
13301331 pud_t pud = native_pudp_get_and_clear(pudp);
13311332
1332− page_table_check_pud_clear(mm, pud);
1333+ page_table_check_pud_clear(mm, addr, pud);
13331334
13341335 return pud;
13351336 }
@@ -1356,7 +1357,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm, · 20 unmodified lines
13561357 static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
13571358 unsigned long address, pmd_t *pmdp, pmd_t pmd)
13581359 {
1359− page_table_check_pmd_set(vma->vm_mm, pmdp, pmd);
1360+ page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd);
13601361 if (IS_ENABLED(CONFIG_SMP)) {
13611362 return xchg(pmdp, pmd);
13621363 } else {
@@ -1371,7 +1372,7 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, · 8 unmodified lines
13711372 static inline pud_t pudp_establish(struct vm_area_struct *vma,
13721373 unsigned long address, pud_t *pudp, pud_t pud)
13731374 {
1374− page_table_check_pud_set(vma->vm_mm, pudp, pud);
1375+ page_table_check_pud_set(vma->vm_mm, address, pudp, pud);
13751376 if (IS_ENABLED(CONFIG_SMP)) {
13761377 return xchg(pudp, pud);
13771378 } else {
@@ -1679,17 +1680,17 @@ static inline bool arch_has_hw_nonleaf_pmd_young(void) · 301 unmodified lines
16791680 #endif
16801681
16811682 #ifdef CONFIG_PAGE_TABLE_CHECK
1682−static inline bool pte_user_accessible_page(pte_t pte)
1683+static inline bool pte_user_accessible_page(pte_t pte, unsigned long addr)
16831684 {
16841685 return (pte_val(pte) & _PAGE_PRESENT) && (pte_val(pte) & _PAGE_USER);
16851686 }
16861687
1687−static inline bool pmd_user_accessible_page(pmd_t pmd)
1688+static inline bool pmd_user_accessible_page(pmd_t pmd, unsigned long addr)
16881689 {
16891690 return pmd_leaf(pmd) && (pmd_val(pmd) & _PAGE_PRESENT) && (pmd_val(pmd) & _PAGE_USER);
16901691 }
16911692
1692−static inline bool pud_user_accessible_page(pud_t pud)
1693+static inline bool pud_user_accessible_page(pud_t pud, unsigned long addr)
16931694 {
16941695 return pud_leaf(pud) && (pud_val(pud) & _PAGE_PRESENT) && (pud_val(pud) & _PAGE_USER);
16951696 }
arch/x86/include/asm/thread_info.h+1−3
@@ -100,8 +100,7 @@ struct thread_info { · 99 unmodified lines
100100 #define TIF_FORCED_TF 24 /* true if TF in eflags artificially */
101101 #define TIF_SINGLESTEP 25 /* reenable singlestep on user return*/
102102 #define TIF_BLOCKSTEP 26 /* set when we want DEBUGCTLMSR_BTF */
103−#define TIF_LAZY_MMU_UPDATES 27 /* task is updating the mmu lazily */
104−#define TIF_ADDR32 28 /* 32-bit address space on 64 bits */
103+#define TIF_ADDR32 27 /* 32-bit address space on 64 bits */
105104
106105 #define _TIF_SSBD BIT(TIF_SSBD)
107106 #define _TIF_SPEC_IB BIT(TIF_SPEC_IB)
@@ -114,7 +113,6 @@ struct thread_info { · 6 unmodified lines
114113 #define _TIF_FORCED_TF BIT(TIF_FORCED_TF)
115114 #define _TIF_BLOCKSTEP BIT(TIF_BLOCKSTEP)
116115 #define _TIF_SINGLESTEP BIT(TIF_SINGLESTEP)
117−#define _TIF_LAZY_MMU_UPDATES BIT(TIF_LAZY_MMU_UPDATES)
118116 #define _TIF_ADDR32 BIT(TIF_ADDR32)
119117
120118 /* flags to check in __switch_to() */
arch/x86/kernel/setup.c+0−5
@@ -1185,11 +1185,6 @@ void __init setup_arch(char **cmdline_p) · 1184 unmodified lines
11851185 initmem_init();
11861186 dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
11871187
1188− if (boot_cpu_has(X86_FEATURE_GBPAGES)) {
1189− hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
1190− hugetlb_bootmem_alloc();
1191− }
1192−
11931188 /*
11941189 * Reserve memory for crash kernel after SRAT is parsed so that it
11951190 * won't consume hotpluggable memory.
arch/x86/lib/clear_page_64.S+8−31
@@ -6,30 +6,15 @@ · 5 unmodified lines
66 #include <asm/asm.h>
77
88 /*
9− * Most CPUs support enhanced REP MOVSB/STOSB instructions. It is
10− * recommended to use this when possible and we do use them by default.
11− * If enhanced REP MOVSB/STOSB is not available, try to use fast string.
12− * Otherwise, use original.
9+ * Zero page aligned region.
10+ * %rdi - dest
11+ * %rcx - length
1312 */
14−
15−/*
16− * Zero a page.
17− * %rdi - page
18− */
19−SYM_TYPED_FUNC_START(clear_page_rep)
20− movl $4096/8,%ecx
21− xorl %eax,%eax
22− rep stosq
23− RET
24−SYM_FUNC_END(clear_page_rep)
25−EXPORT_SYMBOL_GPL(clear_page_rep)
26−
27−SYM_TYPED_FUNC_START(clear_page_orig)
28− xorl %eax,%eax
29− movl $4096/64,%ecx
13+SYM_TYPED_FUNC_START(__clear_pages_unrolled)
14+ shrq $6, %rcx
3015 .p2align 4
3116 .Lloop:
32− decl %ecx
17+ decq %rcx
3318 #define PUT(x) movq %rax,x*8(%rdi)
3419 movq %rax,(%rdi)
3520 PUT(1)
@@ -43,16 +28,8 @@ SYM_TYPED_FUNC_START(clear_page_orig) · 7 unmodified lines
4328 jnz .Lloop
4429 nop
4530 RET
46−SYM_FUNC_END(clear_page_orig)
47−EXPORT_SYMBOL_GPL(clear_page_orig)
48−
49−SYM_TYPED_FUNC_START(clear_page_erms)
50− movl $4096,%ecx
51− xorl %eax,%eax
52− rep stosb
53− RET
54−SYM_FUNC_END(clear_page_erms)
55−EXPORT_SYMBOL_GPL(clear_page_erms)
31+SYM_FUNC_END(__clear_pages_unrolled)
32+EXPORT_SYMBOL_GPL(__clear_pages_unrolled)
5633
5734 /*
5835 * Default clear user-space.
arch/x86/mm/hugetlbpage.c+8−0
@@ -42,3 +42,11 @@ static __init int gigantic_pages_init(void) · 41 unmodified lines
4242 arch_initcall(gigantic_pages_init);
4343 #endif
4444 #endif
45+
46+unsigned int __init arch_hugetlb_cma_order(void)
47+{
48+ if (boot_cpu_has(X86_FEATURE_GBPAGES))
49+ return PUD_SHIFT - PAGE_SHIFT;
50+
51+ return 0;
52+}
arch/x86/mm/init.c+1−7
@@ -996,12 +996,8 @@ void __init free_initrd_mem(unsigned long start, unsigned long end) · 995 unmodified lines
996996 }
997997 #endif
998998
999−void __init zone_sizes_init(void)
999+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
10001000 {
1001− unsigned long max_zone_pfns[MAX_NR_ZONES];
1002−
1003− memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
1004−
10051001 #ifdef CONFIG_ZONE_DMA
10061002 max_zone_pfns[ZONE_DMA] = min(MAX_DMA_PFN, max_low_pfn);
10071003 #endif
@@ -1012,8 +1008,6 @@ void __init zone_sizes_init(void) · 4 unmodified lines
10121008 #ifdef CONFIG_HIGHMEM
10131009 max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
10141010 #endif
1015−
1016− free_area_init(max_zone_pfns);
10171011 }
10181012
10191013 __visible DEFINE_PER_CPU_ALIGNED(struct tlb_state, cpu_tlbstate) = {
arch/x86/mm/init_32.c+0−2
@@ -654,8 +654,6 @@ void __init paging_init(void) · 653 unmodified lines
654654 * NOTE: at this point the bootmem allocator is fully available.
655655 */
656656 olpc_dt_build_devicetree();
657− sparse_init();
658− zone_sizes_init();
659657 }
660658
661659 /*
arch/x86/mm/init_64.c+0−4
@@ -833,8 +833,6 @@ void __init initmem_init(void) · 832 unmodified lines
833833
834834 void __init paging_init(void)
835835 {
836− sparse_init();
837−
838836 /*
839837 * clear the default setting with node 0
840838 * note: don't use nodes_clear here, that is really clearing when
@@ -843,8 +841,6 @@ void __init paging_init(void) · 2 unmodified lines
843841 */
844842 node_clear_state(0, N_MEMORY);
845843 node_clear_state(0, N_NORMAL_MEMORY);
846−
847− zone_sizes_init();
848844 }
849845
850846 #define PAGE_UNUSED 0xFD
arch/x86/mm/mm_internal.h+0−1
@@ -17,7 +17,6 @@ unsigned long kernel_physical_mapping_init(unsigned long start, · 16 unmodified lines
1717 unsigned long kernel_physical_mapping_change(unsigned long start,
1818 unsigned long end,
1919 unsigned long page_size_mask);
20−void zone_sizes_init(void);
2120
2221 extern int after_bootmem;
2322
arch/x86/xen/enlighten_pv.c+1−2
@@ -426,7 +426,6 @@ static void xen_start_context_switch(struct task_struct *prev) · 425 unmodified lines
426426
427427 if (this_cpu_read(xen_lazy_mode) == XEN_LAZY_MMU) {
428428 arch_leave_lazy_mmu_mode();
429− set_ti_thread_flag(task_thread_info(prev), TIF_LAZY_MMU_UPDATES);
430429 }
431430 enter_lazy(XEN_LAZY_CPU);
432431 }
@@ -437,7 +436,7 @@ static void xen_end_context_switch(struct task_struct *next) · 4 unmodified lines
437436
438437 xen_mc_flush();
439438 leave_lazy(XEN_LAZY_CPU);
440− if (test_and_clear_ti_thread_flag(task_thread_info(next), TIF_LAZY_MMU_UPDATES))
439+ if (__task_lazy_mmu_mode_active(next))
441440 arch_enter_lazy_mmu_mode();
442441 }
443442
arch/x86/xen/mmu_pv.c+2−4
@@ -2139,10 +2139,8 @@ static void xen_flush_lazy_mmu(void) · 2138 unmodified lines
21392139 {
21402140 preempt_disable();
21412141
2142− if (xen_get_lazy_mode() == XEN_LAZY_MMU) {
2143− arch_leave_lazy_mmu_mode();
2144− arch_enter_lazy_mmu_mode();
2145− }
2142+ if (xen_get_lazy_mode() == XEN_LAZY_MMU)
2143+ xen_mc_flush();
21462144
21472145 preempt_enable();
21482146 }
arch/xtensa/include/asm/page.h+0−1
@@ -126,7 +126,6 @@ void clear_user_highpage(struct page *page, unsigned long vaddr); · 125 unmodified lines
126126 void copy_user_highpage(struct page *to, struct page *from,
127127 unsigned long vaddr, struct vm_area_struct *vma);
128128 #else
129−# define clear_user_page(page, vaddr, pg) clear_page(page)
130129 # define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
131130 #endif
132131
arch/xtensa/mm/init.c+7−7
@@ -116,16 +116,16 @@ static void __init print_vm_layout(void) · 115 unmodified lines
116116 (unsigned long)(__bss_stop - __bss_start) >> 10);
117117 }
118118
119−void __init zones_init(void)
119+void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
120120 {
121− /* All pages are DMA-able, so we put them all in the DMA zone. */
122− unsigned long max_zone_pfn[MAX_NR_ZONES] = {
123− [ZONE_NORMAL] = max_low_pfn,
121+ max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
124122 #ifdef CONFIG_HIGHMEM
125− [ZONE_HIGHMEM] = max_pfn,
123+ max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
126124 #endif
127− };
128− free_area_init(max_zone_pfn);
125+}
126+
127+void __init zones_init(void)
128+{
129129 print_vm_layout();
130130 }
131131
block/blk-mq.c+0−3
@@ -823,9 +823,6 @@ void blk_mq_free_request(struct request *rq) · 822 unmodified lines
823823
824824 blk_mq_finish_request(rq);
825825
826− if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
827− laptop_io_completion(q->disk->bdi);
828−
829826 rq_qos_done(q, rq);
830827
831828 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
drivers/block/zram/zram_drv.c+565−434
@@ -12,8 +12,7 @@ · 11 unmodified lines
1212 *
1313 */
1414
15−#define KMSG_COMPONENT "zram"
16−#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15+#define pr_fmt(fmt) "zram: " fmt
1716
1817 #include <linux/module.h>
1918 #include <linux/kernel.h>
@@ -56,13 +55,10 @@ static size_t huge_class_size; · 36 unmodified lines
5655
5756 static const struct block_device_operations zram_devops;
5857
59−static void zram_free_page(struct zram *zram, size_t index);
60−static int zram_read_from_zspool(struct zram *zram, struct page *page,
61− u32 index);
62−
58+static void slot_free(struct zram *zram, u32 index);
6359 #define slot_dep_map(zram, index) (&(zram)->table[(index)].dep_map)
6460
65−static void zram_slot_lock_init(struct zram *zram, u32 index)
61+static void slot_lock_init(struct zram *zram, u32 index)
6662 {
6763 static struct lock_class_key __key;
6864
@@ -82,9 +78,9 @@ static void zram_slot_lock_init(struct zram *zram, u32 index) · 13 unmodified lines
8278 * 4) Use TRY lock variant when in atomic context
8379 * - must check return value and handle locking failers
8480 */
85−static __must_check bool zram_slot_trylock(struct zram *zram, u32 index)
81+static __must_check bool slot_trylock(struct zram *zram, u32 index)
8682 {
87− unsigned long *lock = &zram->table[index].flags;
83+ unsigned long *lock = &zram->table[index].__lock;
8884
8985 if (!test_and_set_bit_lock(ZRAM_ENTRY_LOCK, lock)) {
9086 mutex_acquire(slot_dep_map(zram, index), 0, 1, _RET_IP_);
@@ -95,18 +91,18 @@ static __must_check bool zram_slot_trylock(struct zram *zram, u32 index) · 4 unmodified lines
9591 return false;
9692 }
9793
98−static void zram_slot_lock(struct zram *zram, u32 index)
94+static void slot_lock(struct zram *zram, u32 index)
9995 {
100− unsigned long *lock = &zram->table[index].flags;
96+ unsigned long *lock = &zram->table[index].__lock;
10197
10298 mutex_acquire(slot_dep_map(zram, index), 0, 0, _RET_IP_);
10399 wait_on_bit_lock(lock, ZRAM_ENTRY_LOCK, TASK_UNINTERRUPTIBLE);
104100 lock_acquired(slot_dep_map(zram, index), _RET_IP_);
105101 }
106102
107−static void zram_slot_unlock(struct zram *zram, u32 index)
103+static void slot_unlock(struct zram *zram, u32 index)
108104 {
109− unsigned long *lock = &zram->table[index].flags;
105+ unsigned long *lock = &zram->table[index].__lock;
110106
111107 mutex_release(slot_dep_map(zram, index), _RET_IP_);
112108 clear_and_wake_up_bit(ZRAM_ENTRY_LOCK, lock);
@@ -122,52 +118,80 @@ static inline struct zram *dev_to_zram(struct device *dev) · 9 unmodified lines
122118 return (struct zram *)dev_to_disk(dev)->private_data;
123119 }
124120
125−static unsigned long zram_get_handle(struct zram *zram, u32 index)
121+static unsigned long get_slot_handle(struct zram *zram, u32 index)
126122 {
127123 return zram->table[index].handle;
128124 }
129125
130−static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
126+static void set_slot_handle(struct zram *zram, u32 index, unsigned long handle)
131127 {
132128 zram->table[index].handle = handle;
133129 }
134130
135−static bool zram_test_flag(struct zram *zram, u32 index,
136− enum zram_pageflags flag)
131+static bool test_slot_flag(struct zram *zram, u32 index,
132+ enum zram_pageflags flag)
137133 {
138− return zram->table[index].flags & BIT(flag);
134+ return zram->table[index].attr.flags & BIT(flag);
139135 }
140136
141−static void zram_set_flag(struct zram *zram, u32 index,
142− enum zram_pageflags flag)
137+static void set_slot_flag(struct zram *zram, u32 index,
138+ enum zram_pageflags flag)
143139 {
144− zram->table[index].flags |= BIT(flag);
140+ zram->table[index].attr.flags |= BIT(flag);
145141 }
146142
147−static void zram_clear_flag(struct zram *zram, u32 index,
148− enum zram_pageflags flag)
143+static void clear_slot_flag(struct zram *zram, u32 index,
144+ enum zram_pageflags flag)
149145 {
150− zram->table[index].flags &= ~BIT(flag);
146+ zram->table[index].attr.flags &= ~BIT(flag);
151147 }
152148
153−static size_t zram_get_obj_size(struct zram *zram, u32 index)
149+static size_t get_slot_size(struct zram *zram, u32 index)
154150 {
155− return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
151+ return zram->table[index].attr.flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
156152 }
157153
158−static void zram_set_obj_size(struct zram *zram,
159− u32 index, size_t size)
154+static void set_slot_size(struct zram *zram, u32 index, size_t size)
160155 {
161− unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
156+ unsigned long flags = zram->table[index].attr.flags >> ZRAM_FLAG_SHIFT;
157+
158+ zram->table[index].attr.flags = (flags << ZRAM_FLAG_SHIFT) | size;
159+}
162160
163− zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
161+static inline bool slot_allocated(struct zram *zram, u32 index)
162+{
163+ return get_slot_size(zram, index) ||
164+ test_slot_flag(zram, index, ZRAM_SAME) ||
165+ test_slot_flag(zram, index, ZRAM_WB);
166+}
167+
168+static inline void set_slot_comp_priority(struct zram *zram, u32 index,
169+ u32 prio)
170+{
171+ prio &= ZRAM_COMP_PRIORITY_MASK;
172+ /*
173+ * Clear previous priority value first, in case if we recompress
174+ * further an already recompressed page
175+ */
176+ zram->table[index].attr.flags &= ~(ZRAM_COMP_PRIORITY_MASK <<
177+ ZRAM_COMP_PRIORITY_BIT1);
178+ zram->table[index].attr.flags |= (prio << ZRAM_COMP_PRIORITY_BIT1);
164179 }
165180
166−static inline bool zram_allocated(struct zram *zram, u32 index)
181+static inline u32 get_slot_comp_priority(struct zram *zram, u32 index)
167182 {
168− return zram_get_obj_size(zram, index) ||
169− zram_test_flag(zram, index, ZRAM_SAME) ||
170− zram_test_flag(zram, index, ZRAM_WB);
183+ u32 prio = zram->table[index].attr.flags >> ZRAM_COMP_PRIORITY_BIT1;
184+
185+ return prio & ZRAM_COMP_PRIORITY_MASK;
186+}
187+
188+static void mark_slot_accessed(struct zram *zram, u32 index)
189+{
190+ clear_slot_flag(zram, index, ZRAM_IDLE);
191+ clear_slot_flag(zram, index, ZRAM_PP_SLOT);
192+#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
193+ zram->table[index].attr.ac_time = (u32)ktime_get_boottime_seconds();
194+#endif
171195 }
172196
173197 static inline void update_used_max(struct zram *zram, const unsigned long pages)
@@ -204,34 +228,6 @@ static inline bool is_partial_io(struct bio_vec *bvec) · 30 unmodified lines
204228 }
205229 #endif
206230
207−static inline void zram_set_priority(struct zram *zram, u32 index, u32 prio)
208−{
209− prio &= ZRAM_COMP_PRIORITY_MASK;
210− /*
211− * Clear previous priority value first, in case if we recompress
212− * further an already recompressed page
213− */
214− zram->table[index].flags &= ~(ZRAM_COMP_PRIORITY_MASK <<
215− ZRAM_COMP_PRIORITY_BIT1);
216− zram->table[index].flags |= (prio << ZRAM_COMP_PRIORITY_BIT1);
217−}
218−
219−static inline u32 zram_get_priority(struct zram *zram, u32 index)
220−{
221− u32 prio = zram->table[index].flags >> ZRAM_COMP_PRIORITY_BIT1;
222−
223− return prio & ZRAM_COMP_PRIORITY_MASK;
224−}
225−
226−static void zram_accessed(struct zram *zram, u32 index)
227−{
228− zram_clear_flag(zram, index, ZRAM_IDLE);
229− zram_clear_flag(zram, index, ZRAM_PP_SLOT);
230−#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
231− zram->table[index].ac_time = ktime_get_boottime();
232−#endif
233−}
234−
235231 #if defined CONFIG_ZRAM_WRITEBACK || defined CONFIG_ZRAM_MULTI_COMP
236232 struct zram_pp_slot {
237233 unsigned long index;
@@ -267,9 +263,9 @@ static void release_pp_slot(struct zram *zram, struct zram_pp_slot *pps) · 29 unmodified lines
267263 {
268264 list_del_init(&pps->entry);
269265
270− zram_slot_lock(zram, pps->index);
271− zram_clear_flag(zram, pps->index, ZRAM_PP_SLOT);
272− zram_slot_unlock(zram, pps->index);
266+ slot_lock(zram, pps->index);
267+ clear_slot_flag(zram, pps->index, ZRAM_PP_SLOT);
268+ slot_unlock(zram, pps->index);
273269
274270 kfree(pps);
275271 }
@@ -308,10 +304,10 @@ static bool place_pp_slot(struct zram *zram, struct zram_pp_ctl *ctl, · 32 unmodified lines
308304 INIT_LIST_HEAD(&pps->entry);
309305 pps->index = index;
310306
311− bid = zram_get_obj_size(zram, pps->index) / PP_BUCKET_SIZE_RANGE;
307+ bid = get_slot_size(zram, pps->index) / PP_BUCKET_SIZE_RANGE;
312308 list_add(&pps->entry, &ctl->pp_buckets[bid]);
313309
314− zram_set_flag(zram, pps->index, ZRAM_PP_SLOT);
310+ set_slot_flag(zram, pps->index, ZRAM_PP_SLOT);
315311 return true;
316312 }
317313
@@ -363,15 +359,14 @@ static bool page_same_filled(void *ptr, unsigned long *element) · 45 unmodified lines
363359 return true;
364360 }
365361
366−static ssize_t initstate_show(struct device *dev,
367− struct device_attribute *attr, char *buf)
362+static ssize_t initstate_show(struct device *dev, struct device_attribute *attr,
363+ char *buf)
368364 {
369365 u32 val;
370366 struct zram *zram = dev_to_zram(dev);
371367
372− down_read(&zram->init_lock);
368+ guard(rwsem_read)(&zram->dev_lock);
373369 val = init_done(zram);
374− up_read(&zram->init_lock);
375370
376371 return sysfs_emit(buf, "%u\n", val);
377372 }
@@ -385,7 +380,8 @@ static ssize_t disksize_show(struct device *dev, · 7 unmodified lines
385380 }
386381
387382 static ssize_t mem_limit_store(struct device *dev,
388− struct device_attribute *attr, const char *buf, size_t len)
383+ struct device_attribute *attr, const char *buf,
384+ size_t len)
389385 {
390386 u64 limit;
391387 char *tmp;
@@ -395,15 +391,15 @@ static ssize_t mem_limit_store(struct device *dev, · 3 unmodified lines
395391 if (buf == tmp) /* no chars parsed, invalid input */
396392 return -EINVAL;
397393
398− down_write(&zram->init_lock);
394+ guard(rwsem_write)(&zram->dev_lock);
399395 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
400− up_write(&zram->init_lock);
401396
402397 return len;
403398 }
404399
405400 static ssize_t mem_used_max_store(struct device *dev,
406− struct device_attribute *attr, const char *buf, size_t len)
401+ struct device_attribute *attr,
402+ const char *buf, size_t len)
407403 {
408404 int err;
409405 unsigned long val;
@@ -413,12 +409,11 @@ static ssize_t mem_used_max_store(struct device *dev, · 3 unmodified lines
413409 if (err || val != 0)
414410 return -EINVAL;
415411
416− down_read(&zram->init_lock);
412+ guard(rwsem_read)(&zram->dev_lock);
417413 if (init_done(zram)) {
418414 atomic_long_set(&zram->stats.max_used_pages,
419415 zs_get_total_pages(zram->mem_pool));
420416 }
421− up_read(&zram->init_lock);
422417
423418 return len;
424419 }
@@ -441,67 +436,66 @@ static void mark_idle(struct zram *zram, ktime_t cutoff) · 16 unmodified lines
441436 *
442437 * And ZRAM_WB slots simply cannot be ZRAM_IDLE.
443438 */
444− zram_slot_lock(zram, index);
445− if (!zram_allocated(zram, index) ||
446− zram_test_flag(zram, index, ZRAM_WB) ||
447− zram_test_flag(zram, index, ZRAM_SAME)) {
448− zram_slot_unlock(zram, index);
439+ slot_lock(zram, index);
440+ if (!slot_allocated(zram, index) ||
441+ test_slot_flag(zram, index, ZRAM_WB) ||
442+ test_slot_flag(zram, index, ZRAM_SAME)) {
443+ slot_unlock(zram, index);
449444 continue;
450445 }
451446
452447 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
453448 is_idle = !cutoff ||
454− ktime_after(cutoff, zram->table[index].ac_time);
449+ ktime_after(cutoff, zram->table[index].attr.ac_time);
455450 #endif
456451 if (is_idle)
457− zram_set_flag(zram, index, ZRAM_IDLE);
452+ set_slot_flag(zram, index, ZRAM_IDLE);
458453 else
459− zram_clear_flag(zram, index, ZRAM_IDLE);
460− zram_slot_unlock(zram, index);
454+ clear_slot_flag(zram, index, ZRAM_IDLE);
455+ slot_unlock(zram, index);
461456 }
462457 }
463458
464−static ssize_t idle_store(struct device *dev,
465− struct device_attribute *attr, const char *buf, size_t len)
459+static ssize_t idle_store(struct device *dev, struct device_attribute *attr,
460+ const char *buf, size_t len)
466461 {
467462 struct zram *zram = dev_to_zram(dev);
468− ktime_t cutoff_time = 0;
469− ssize_t rv = -EINVAL;
463+ ktime_t cutoff = 0;
470464
471465 if (!sysfs_streq(buf, "all")) {
472466 /*
473467 * If it did not parse as 'all' try to treat it as an integer
474468 * when we have memory tracking enabled.
475469 */
476− u64 age_sec;
470+ u32 age_sec;
477471
478− if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) && !kstrtoull(buf, 0, &age_sec))
479− cutoff_time = ktime_sub(ktime_get_boottime(),
480− ns_to_ktime(age_sec * NSEC_PER_SEC));
472+ if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) &&
473+ !kstrtouint(buf, 0, &age_sec))
474+ cutoff = ktime_sub((u32)ktime_get_boottime_seconds(),
475+ age_sec);
481476 else
482− goto out;
477+ return -EINVAL;
483478 }
484479
485− down_read(&zram->init_lock);
480+ guard(rwsem_read)(&zram->dev_lock);
486481 if (!init_done(zram))
487− goto out_unlock;
482+ return -EINVAL;
488483
489484 /*
490− * A cutoff_time of 0 marks everything as idle, this is the
485+ * A cutoff of 0 marks everything as idle, this is the
491486 * "all" behavior.
492487 */
493− mark_idle(zram, cutoff_time);
494− rv = len;
495−
496−out_unlock:
497− up_read(&zram->init_lock);
498−out:
499− return rv;
488+ mark_idle(zram, cutoff);
489+ return len;
500490 }
501491
502492 #ifdef CONFIG_ZRAM_WRITEBACK
503493 #define INVALID_BDEV_BLOCK (~0UL)
504494
495+static int read_from_zspool_raw(struct zram *zram, struct page *page,
496+ u32 index);
497+static int read_from_zspool(struct zram *zram, struct page *page, u32 index);
498+
505499 struct zram_wb_ctl {
506500 /* idle list is accessed only by the writeback task, no concurency */
507501 struct list_head idle_reqs;
@@ -522,23 +516,86 @@ struct zram_wb_req { · 14 unmodified lines
522516 struct list_head entry;
523517 };
524518
519+struct zram_rb_req {
520+ struct work_struct work;
521+ struct zram *zram;
522+ struct page *page;
523+ /* The read bio for backing device */
524+ struct bio *bio;
525+ unsigned long blk_idx;
526+ union {
527+ /* The original bio to complete (async read) */
528+ struct bio *parent;
529+ /* error status (sync read) */
530+ int error;
531+ };
532+ u32 index;
533+};
534+
535+#define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
536+static ssize_t bd_stat_show(struct device *dev, struct device_attribute *attr,
537+ char *buf)
538+{
539+ struct zram *zram = dev_to_zram(dev);
540+ ssize_t ret;
541+
542+ guard(rwsem_read)(&zram->dev_lock);
543+ ret = sysfs_emit(buf,
544+ "%8llu %8llu %8llu\n",
545+ FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
546+ FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
547+ FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
548+
549+ return ret;
550+}
551+
552+static ssize_t writeback_compressed_store(struct device *dev,
553+ struct device_attribute *attr,
554+ const char *buf, size_t len)
555+{
556+ struct zram *zram = dev_to_zram(dev);
557+ bool val;
558+
559+ if (kstrtobool(buf, &val))
560+ return -EINVAL;
561+
562+ guard(rwsem_write)(&zram->dev_lock);
563+ if (init_done(zram)) {
564+ return -EBUSY;
565+ }
566+
567+ zram->wb_compressed = val;
568+
569+ return len;
570+}
571+
572+static ssize_t writeback_compressed_show(struct device *dev,
573+ struct device_attribute *attr,
574+ char *buf)
575+{
576+ bool val;
577+ struct zram *zram = dev_to_zram(dev);
578+
579+ guard(rwsem_read)(&zram->dev_lock);
580+ val = zram->wb_compressed;
581+
582+ return sysfs_emit(buf, "%d\n", val);
583+}
584+
525585 static ssize_t writeback_limit_enable_store(struct device *dev,
526586 struct device_attribute *attr,
527587 const char *buf, size_t len)
528588 {
529589 struct zram *zram = dev_to_zram(dev);
530590 u64 val;
531− ssize_t ret = -EINVAL;
532591
533592 if (kstrtoull(buf, 10, &val))
534− return ret;
593+ return -EINVAL;
535594
536− down_write(&zram->init_lock);
595+ guard(rwsem_write)(&zram->dev_lock);
537596 zram->wb_limit_enable = val;
538− up_write(&zram->init_lock);
539− ret = len;
540597
541− return ret;
598+ return len;
542599 }
543600
544601 static ssize_t writeback_limit_enable_show(struct device *dev,
@@ -548,9 +605,8 @@ static ssize_t writeback_limit_enable_show(struct device *dev, · 3 unmodified lines
548605 bool val;
549606 struct zram *zram = dev_to_zram(dev);
550607
551− down_read(&zram->init_lock);
608+ guard(rwsem_read)(&zram->dev_lock);
552609 val = zram->wb_limit_enable;
553− up_read(&zram->init_lock);
554610
555611 return sysfs_emit(buf, "%d\n", val);
556612 }
@@ -561,10 +617,9 @@ static ssize_t writeback_limit_store(struct device *dev, · 4 unmodified lines
561617 {
562618 struct zram *zram = dev_to_zram(dev);
563619 u64 val;
564− ssize_t ret = -EINVAL;
565620
566621 if (kstrtoull(buf, 10, &val))
567− return ret;
622+ return -EINVAL;
568623
569624 /*
570625 * When the page size is greater than 4KB, if bd_wb_limit is set to
@@ -576,12 +631,10 @@ static ssize_t writeback_limit_store(struct device *dev, · 5 unmodified lines
576631 */
577632 val = rounddown(val, PAGE_SIZE / 4096);
578633
579− down_write(&zram->init_lock);
634+ guard(rwsem_write)(&zram->dev_lock);
580635 zram->bd_wb_limit = val;
581− up_write(&zram->init_lock);
582− ret = len;
583636
584− return ret;
637+ return len;
585638 }
586639
587640 static ssize_t writeback_limit_show(struct device *dev,
@@ -590,9 +643,8 @@ static ssize_t writeback_limit_show(struct device *dev, · 2 unmodified lines
590643 u64 val;
591644 struct zram *zram = dev_to_zram(dev);
592645
593− down_read(&zram->init_lock);
646+ guard(rwsem_read)(&zram->dev_lock);
594647 val = zram->bd_wb_limit;
595− up_read(&zram->init_lock);
596648
597649 return sysfs_emit(buf, "%llu\n", val);
598650 }
@@ -610,9 +662,8 @@ static ssize_t writeback_batch_size_store(struct device *dev, · 11 unmodified lines
610662 if (!val)
611663 return -EINVAL;
612664
613− down_write(&zram->init_lock);
665+ guard(rwsem_write)(&zram->dev_lock);
614666 zram->wb_batch_size = val;
615− up_write(&zram->init_lock);
616667
617668 return len;
618669 }
@@ -624,9 +675,8 @@ static ssize_t writeback_batch_size_show(struct device *dev, · 5 unmodified lines
624675 u32 val;
625676 struct zram *zram = dev_to_zram(dev);
626677
627− down_read(&zram->init_lock);
678+ guard(rwsem_read)(&zram->dev_lock);
628679 val = zram->wb_batch_size;
629− up_read(&zram->init_lock);
630680
631681 return sysfs_emit(buf, "%u\n", val);
632682 }
@@ -646,37 +696,33 @@ static void reset_bdev(struct zram *zram) · 13 unmodified lines
646696 }
647697
648698 static ssize_t backing_dev_show(struct device *dev,
649− struct device_attribute *attr, char *buf)
699+ struct device_attribute *attr, char *buf)
650700 {
651701 struct file *file;
652702 struct zram *zram = dev_to_zram(dev);
653703 char *p;
654704 ssize_t ret;
655705
656− down_read(&zram->init_lock);
706+ guard(rwsem_read)(&zram->dev_lock);
657707 file = zram->backing_dev;
658708 if (!file) {
659709 memcpy(buf, "none\n", 5);
660− up_read(&zram->init_lock);
661710 return 5;
662711 }
663712
664713 p = file_path(file, buf, PAGE_SIZE - 1);
665− if (IS_ERR(p)) {
666− ret = PTR_ERR(p);
667− goto out;
668− }
714+ if (IS_ERR(p))
715+ return PTR_ERR(p);
669716
670717 ret = strlen(p);
671718 memmove(buf, p, ret);
672719 buf[ret++] = '\n';
673−out:
674− up_read(&zram->init_lock);
675720 return ret;
676721 }
677722
678723 static ssize_t backing_dev_store(struct device *dev,
679− struct device_attribute *attr, const char *buf, size_t len)
724+ struct device_attribute *attr, const char *buf,
725+ size_t len)
680726 {
681727 char *file_name;
682728 size_t sz;
@@ -691,7 +737,7 @@ static ssize_t backing_dev_store(struct device *dev, · 8 unmodified lines
691737 if (!file_name)
692738 return -ENOMEM;
693739
694− down_write(&zram->init_lock);
740+ guard(rwsem_write)(&zram->dev_lock);
695741 if (init_done(zram)) {
696742 pr_info("Can't setup backing device for initialized device\n");
697743 err = -EBUSY;
@@ -739,7 +785,6 @@ static ssize_t backing_dev_store(struct device *dev, · 41 unmodified lines
739785 zram->backing_dev = backing_dev;
740786 zram->bitmap = bitmap;
741787 zram->nr_pages = nr_pages;
742− up_write(&zram->init_lock);
743788
744789 pr_info("setup backing device %s\n", file_name);
745790 kfree(file_name);
@@ -751,8 +796,6 @@ out: · 5 unmodified lines
751796 if (backing_dev)
752797 filp_close(backing_dev, NULL);
753798
754− up_write(&zram->init_lock);
755−
756799 kfree(file_name);
757800
758801 return err;
@@ -780,18 +823,6 @@ static void zram_release_bdev_block(struct zram *zram, unsigned long blk_idx) · 21 unmodified lines
780823 atomic64_dec(&zram->stats.bd_count);
781824 }
782825
783−static void read_from_bdev_async(struct zram *zram, struct page *page,
784− unsigned long entry, struct bio *parent)
785−{
786− struct bio *bio;
787−
788− bio = bio_alloc(zram->bdev, 1, parent->bi_opf, GFP_NOIO);
789− bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
790− __bio_add_page(bio, page, PAGE_SIZE, 0);
791− bio_chain(bio, parent);
792− submit_bio(bio);
793−}
794−
795826 static void release_wb_req(struct zram_wb_req *req)
796827 {
797828 __free_page(req->page);
@@ -870,7 +901,7 @@ release_wb_ctl: · 72 unmodified lines
870901
871902 static void zram_account_writeback_rollback(struct zram *zram)
872903 {
873− lockdep_assert_held_read(&zram->init_lock);
904+ lockdep_assert_held_write(&zram->dev_lock);
874905
875906 if (zram->wb_limit_enable)
876907 zram->bd_wb_limit += 1UL << (PAGE_SHIFT - 12);
@@ -878,7 +909,7 @@ static void zram_account_writeback_rollback(struct zram *zram) · 1 unmodified line
878909
879910 static void zram_account_writeback_submit(struct zram *zram)
880911 {
881− lockdep_assert_held_read(&zram->init_lock);
912+ lockdep_assert_held_write(&zram->dev_lock);
882913
883914 if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
884915 zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12);
@@ -886,8 +917,9 @@ static void zram_account_writeback_submit(struct zram *zram) · 1 unmodified line
886917
887918 static int zram_writeback_complete(struct zram *zram, struct zram_wb_req *req)
888919 {
889− u32 index = req->pps->index;
890− int err;
920+ u32 size, index = req->pps->index;
921+ int err, prio;
922+ bool huge;
891923
892924 err = blk_status_to_errno(req->bio.bi_status);
893925 if (err) {
@@ -901,7 +933,7 @@ static int zram_writeback_complete(struct zram *zram, struct zram_wb_req *req) · 7 unmodified lines
901933 }
902934
903935 atomic64_inc(&zram->stats.bd_writes);
904− zram_slot_lock(zram, index);
936+ slot_lock(zram, index);
905937 /*
906938 * We release slot lock during writeback so slot can change under us:
907939 * slot_free() or slot_free() and zram_write_page(). In both cases
@@ -909,18 +941,36 @@ static int zram_writeback_complete(struct zram *zram, struct zram_wb_req *req) · 1 unmodified line
909941 * set ZRAM_PP_SLOT on such slots until current post-processing
910942 * finishes.
911943 */
912− if (!zram_test_flag(zram, index, ZRAM_PP_SLOT)) {
944+ if (!test_slot_flag(zram, index, ZRAM_PP_SLOT)) {
913945 zram_release_bdev_block(zram, req->blk_idx);
914946 goto out;
915947 }
916948
917− zram_free_page(zram, index);
918− zram_set_flag(zram, index, ZRAM_WB);
919− zram_set_handle(zram, index, req->blk_idx);
949+ if (zram->wb_compressed) {
950+ /*
951+ * ZRAM_WB slots get freed, we need to preserve data required
952+ * for read decompression.
953+ */
954+ size = get_slot_size(zram, index);
955+ prio = get_slot_comp_priority(zram, index);
956+ huge = test_slot_flag(zram, index, ZRAM_HUGE);
957+ }
958+
959+ slot_free(zram, index);
960+ set_slot_flag(zram, index, ZRAM_WB);
961+ set_slot_handle(zram, index, req->blk_idx);
962+
963+ if (zram->wb_compressed) {
964+ if (huge)
965+ set_slot_flag(zram, index, ZRAM_HUGE);
966+ set_slot_size(zram, index, size);
967+ set_slot_comp_priority(zram, index, prio);
968+ }
969+
920970 atomic64_inc(&zram->stats.pages_stored);
921971
922972 out:
923− zram_slot_unlock(zram, index);
973+ slot_unlock(zram, index);
924974 return 0;
925975 }
926976
@@ -1041,18 +1091,22 @@ static int zram_writeback_slots(struct zram *zram, · 114 unmodified lines
10411091 }
10421092
10431093 index = pps->index;
1044− zram_slot_lock(zram, index);
1094+ slot_lock(zram, index);
10451095 /*
10461096 * scan_slots() sets ZRAM_PP_SLOT and releases slot lock, so
10471097 * slots can change in the meantime. If slots are accessed or
10481098 * freed they lose ZRAM_PP_SLOT flag and hence we don't
10491099 * post-process them.
10501100 */
1051− if (!zram_test_flag(zram, index, ZRAM_PP_SLOT))
1101+ if (!test_slot_flag(zram, index, ZRAM_PP_SLOT))
10521102 goto next;
1053− if (zram_read_from_zspool(zram, req->page, index))
1103+ if (zram->wb_compressed)
1104+ err = read_from_zspool_raw(zram, req->page, index);
1105+ else
1106+ err = read_from_zspool(zram, req->page, index);
1107+ if (err)
10541108 goto next;
1055− zram_slot_unlock(zram, index);
1109+ slot_unlock(zram, index);
10561110
10571111 /*
10581112 * From now on pp-slot is owned by the req, remove it from
@@ -1074,7 +1128,7 @@ static int zram_writeback_slots(struct zram *zram, · 15 unmodified lines
10741128 continue;
10751129
10761130 next:
1077− zram_slot_unlock(zram, index);
1131+ slot_unlock(zram, index);
10781132 release_pp_slot(zram, pps);
10791133 }
10801134
@@ -1167,27 +1221,27 @@ static int scan_slots_for_writeback(struct zram *zram, u32 mode, · 86 unmodified lines
11671221 while (index < hi) {
11681222 bool ok = true;
11691223
1170− zram_slot_lock(zram, index);
1171− if (!zram_allocated(zram, index))
1224+ slot_lock(zram, index);
1225+ if (!slot_allocated(zram, index))
11721226 goto next;
11731227
1174− if (zram_test_flag(zram, index, ZRAM_WB) ||
1175− zram_test_flag(zram, index, ZRAM_SAME))
1228+ if (test_slot_flag(zram, index, ZRAM_WB) ||
1229+ test_slot_flag(zram, index, ZRAM_SAME))
11761230 goto next;
11771231
11781232 if (mode & IDLE_WRITEBACK &&
1179− !zram_test_flag(zram, index, ZRAM_IDLE))
1233+ !test_slot_flag(zram, index, ZRAM_IDLE))
11801234 goto next;
11811235 if (mode & HUGE_WRITEBACK &&
1182− !zram_test_flag(zram, index, ZRAM_HUGE))
1236+ !test_slot_flag(zram, index, ZRAM_HUGE))
11831237 goto next;
11841238 if (mode & INCOMPRESSIBLE_WRITEBACK &&
1185− !zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
1239+ !test_slot_flag(zram, index, ZRAM_INCOMPRESSIBLE))
11861240 goto next;
11871241
11881242 ok = place_pp_slot(zram, ctl, index);
11891243 next:
1190− zram_slot_unlock(zram, index);
1244+ slot_unlock(zram, index);
11911245 if (!ok)
11921246 break;
11931247 index++;
@@ -1209,33 +1263,21 @@ static ssize_t writeback_store(struct device *dev, · 15 unmodified lines
12091263 ssize_t ret = len;
12101264 int err, mode = 0;
12111265
1212− down_read(&zram->init_lock);
1213− if (!init_done(zram)) {
1214− up_read(&zram->init_lock);
1266+ guard(rwsem_write)(&zram->dev_lock);
1267+ if (!init_done(zram))
12151268 return -EINVAL;
1216− }
1217−
1218− /* Do not permit concurrent post-processing actions. */
1219− if (atomic_xchg(&zram->pp_in_progress, 1)) {
1220− up_read(&zram->init_lock);
1221− return -EAGAIN;
1222− }
12231269
1224− if (!zram->backing_dev) {
1225− ret = -ENODEV;
1226− goto release_init_lock;
1227− }
1270+ if (!zram->backing_dev)
1271+ return -ENODEV;
12281272
12291273 pp_ctl = init_pp_ctl();
1230− if (!pp_ctl) {
1231− ret = -ENOMEM;
1232− goto release_init_lock;
1233− }
1274+ if (!pp_ctl)
1275+ return -ENOMEM;
12341276
12351277 wb_ctl = init_wb_ctl(zram);
12361278 if (!wb_ctl) {
12371279 ret = -ENOMEM;
1238− goto release_init_lock;
1280+ goto out;
12391281 }
12401282
12411283 args = skip_spaces(buf);
@@ -1259,7 +1301,7 @@ static ssize_t writeback_store(struct device *dev, · 17 unmodified lines
12591301 err = parse_mode(param, &mode);
12601302 if (err) {
12611303 ret = err;
1262− goto release_init_lock;
1304+ goto out;
12631305 }
12641306
12651307 scan_slots_for_writeback(zram, mode, lo, hi, pp_ctl);
@@ -1270,7 +1312,7 @@ static ssize_t writeback_store(struct device *dev, · 4 unmodified lines
12701312 err = parse_mode(val, &mode);
12711313 if (err) {
12721314 ret = err;
1273− goto release_init_lock;
1315+ goto out;
12741316 }
12751317
12761318 scan_slots_for_writeback(zram, mode, lo, hi, pp_ctl);
@@ -1281,7 +1323,7 @@ static ssize_t writeback_store(struct device *dev, · 4 unmodified lines
12811323 err = parse_page_index(val, nr_pages, &lo, &hi);
12821324 if (err) {
12831325 ret = err;
1284− goto release_init_lock;
1326+ goto out;
12851327 }
12861328
12871329 scan_slots_for_writeback(zram, mode, lo, hi, pp_ctl);
@@ -1292,7 +1334,7 @@ static ssize_t writeback_store(struct device *dev, · 4 unmodified lines
12921334 err = parse_page_indexes(val, nr_pages, &lo, &hi);
12931335 if (err) {
12941336 ret = err;
1295− goto release_init_lock;
1337+ goto out;
12961338 }
12971339
12981340 scan_slots_for_writeback(zram, mode, lo, hi, pp_ctl);
@@ -1304,33 +1346,147 @@ static ssize_t writeback_store(struct device *dev, · 5 unmodified lines
13041346 if (err)
13051347 ret = err;
13061348
1307−release_init_lock:
1349+out:
13081350 release_pp_ctl(zram, pp_ctl);
13091351 release_wb_ctl(wb_ctl);
1310− atomic_set(&zram->pp_in_progress, 0);
1311− up_read(&zram->init_lock);
13121352
13131353 return ret;
13141354 }
13151355
1316−struct zram_work {
1317− struct work_struct work;
1318− struct zram *zram;
1319− unsigned long entry;
1320− struct page *page;
1321− int error;
1322−};
1356+static int decompress_bdev_page(struct zram *zram, struct page *page, u32 index)
1357+{
1358+ struct zcomp_strm *zstrm;
1359+ unsigned int size;
1360+ int ret, prio;
1361+ void *src;
1362+
1363+ slot_lock(zram, index);
1364+ /* Since slot was unlocked we need to make sure it's still ZRAM_WB */
1365+ if (!test_slot_flag(zram, index, ZRAM_WB)) {
1366+ slot_unlock(zram, index);
1367+ /* We read some stale data, zero it out */
1368+ memset_page(page, 0, 0, PAGE_SIZE);
1369+ return -EIO;
1370+ }
1371+
1372+ if (test_slot_flag(zram, index, ZRAM_HUGE)) {
1373+ slot_unlock(zram, index);
1374+ return 0;
1375+ }
1376+
1377+ size = get_slot_size(zram, index);
1378+ prio = get_slot_comp_priority(zram, index);
1379+
1380+ zstrm = zcomp_stream_get(zram->comps[prio]);
1381+ src = kmap_local_page(page);
1382+ ret = zcomp_decompress(zram->comps[prio], zstrm, src, size,
1383+ zstrm->local_copy);
1384+ if (!ret)
1385+ copy_page(src, zstrm->local_copy);
1386+ kunmap_local(src);
1387+ zcomp_stream_put(zstrm);
1388+ slot_unlock(zram, index);
1389+
1390+ return ret;
1391+}
1392+
1393+static void zram_deferred_decompress(struct work_struct *w)
1394+{
1395+ struct zram_rb_req *req = container_of(w, struct zram_rb_req, work);
1396+ struct page *page = bio_first_page_all(req->bio);
1397+ struct zram *zram = req->zram;
1398+ u32 index = req->index;
1399+ int ret;
1400+
1401+ ret = decompress_bdev_page(zram, page, index);
1402+ if (ret)
1403+ req->parent->bi_status = BLK_STS_IOERR;
1404+
1405+ /* Decrement parent's ->remaining */
1406+ bio_endio(req->parent);
1407+ bio_put(req->bio);
1408+ kfree(req);
1409+}
13231410
1324−static void zram_sync_read(struct work_struct *work)
1411+static void zram_async_read_endio(struct bio *bio)
13251412 {
1326− struct zram_work *zw = container_of(work, struct zram_work, work);
1413+ struct zram_rb_req *req = bio->bi_private;
1414+ struct zram *zram = req->zram;
1415+
1416+ if (bio->bi_status) {
1417+ req->parent->bi_status = bio->bi_status;
1418+ bio_endio(req->parent);
1419+ bio_put(bio);
1420+ kfree(req);
1421+ return;
1422+ }
1423+
1424+ /*
1425+ * NOTE: zram_async_read_endio() is not exactly right place for this.
1426+ * Ideally, we need to do it after ZRAM_WB check, but this requires
1427+ * us to use wq path even on systems that don't enable compressed
1428+ * writeback, because we cannot take slot-lock in the current context.
1429+ *
1430+ * Keep the existing behavior for now.
1431+ */
1432+ if (zram->wb_compressed == false) {
1433+ /* No decompression needed, complete the parent IO */
1434+ bio_endio(req->parent);
1435+ bio_put(bio);
1436+ kfree(req);
1437+ return;
1438+ }
1439+
1440+ /*
1441+ * zram decompression is sleepable, so we need to deffer it to
1442+ * a preemptible context.
1443+ */
1444+ INIT_WORK(&req->work, zram_deferred_decompress);
1445+ queue_work(system_highpri_wq, &req->work);
1446+}
1447+
1448+static void read_from_bdev_async(struct zram *zram, struct page *page,
1449+ u32 index, unsigned long blk_idx,
1450+ struct bio *parent)
1451+{
1452+ struct zram_rb_req *req;
1453+ struct bio *bio;
1454+
1455+ req = kmalloc(sizeof(*req), GFP_NOIO);
1456+ if (!req)
1457+ return;
1458+
1459+ bio = bio_alloc(zram->bdev, 1, parent->bi_opf, GFP_NOIO);
1460+ if (!bio) {
1461+ kfree(req);
1462+ return;
1463+ }
1464+
1465+ req->zram = zram;
1466+ req->index = index;
1467+ req->blk_idx = blk_idx;
1468+ req->bio = bio;
1469+ req->parent = parent;
1470+
1471+ bio->bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
1472+ bio->bi_private = req;
1473+ bio->bi_end_io = zram_async_read_endio;
1474+
1475+ __bio_add_page(bio, page, PAGE_SIZE, 0);
1476+ bio_inc_remaining(parent);
1477+ submit_bio(bio);
1478+}
1479+
1480+static void zram_sync_read(struct work_struct *w)
1481+{
1482+ struct zram_rb_req *req = container_of(w, struct zram_rb_req, work);
13271483 struct bio_vec bv;
13281484 struct bio bio;
13291485
1330− bio_init(&bio, zw->zram->bdev, &bv, 1, REQ_OP_READ);
1331− bio.bi_iter.bi_sector = zw->entry * (PAGE_SIZE >> 9);
1332− __bio_add_page(&bio, zw->page, PAGE_SIZE, 0);
1333− zw->error = submit_bio_wait(&bio);
1486+ bio_init(&bio, req->zram->bdev, &bv, 1, REQ_OP_READ);
1487+ bio.bi_iter.bi_sector = req->blk_idx * (PAGE_SIZE >> 9);
1488+ __bio_add_page(&bio, req->page, PAGE_SIZE, 0);
1489+ req->error = submit_bio_wait(&bio);
13341490 }
13351491
13361492 /*
@@ -1338,39 +1494,42 @@ static void zram_sync_read(struct work_struct *work) · 1 unmodified line
13381494 * chained IO with parent IO in same context, it's a deadlock. To avoid that,
13391495 * use a worker thread context.
13401496 */
1341−static int read_from_bdev_sync(struct zram *zram, struct page *page,
1342− unsigned long entry)
1497+static int read_from_bdev_sync(struct zram *zram, struct page *page, u32 index,
1498+ unsigned long blk_idx)
13431499 {
1344− struct zram_work work;
1500+ struct zram_rb_req req;
1501+
1502+ req.page = page;
1503+ req.zram = zram;
1504+ req.blk_idx = blk_idx;
13451505
1346− work.page = page;
1347− work.zram = zram;
1348− work.entry = entry;
1506+ INIT_WORK_ONSTACK(&req.work, zram_sync_read);
1507+ queue_work(system_dfl_wq, &req.work);
1508+ flush_work(&req.work);
1509+ destroy_work_on_stack(&req.work);
13491510
1350− INIT_WORK_ONSTACK(&work.work, zram_sync_read);
1351− queue_work(system_dfl_wq, &work.work);
1352− flush_work(&work.work);
1353− destroy_work_on_stack(&work.work);
1511+ if (req.error || zram->wb_compressed == false)
1512+ return req.error;
13541513
1355− return work.error;
1514+ return decompress_bdev_page(zram, page, index);
13561515 }
13571516
1358−static int read_from_bdev(struct zram *zram, struct page *page,
1359− unsigned long entry, struct bio *parent)
1517+static int read_from_bdev(struct zram *zram, struct page *page, u32 index,
1518+ unsigned long blk_idx, struct bio *parent)
13601519 {
13611520 atomic64_inc(&zram->stats.bd_reads);
13621521 if (!parent) {
13631522 if (WARN_ON_ONCE(!IS_ENABLED(ZRAM_PARTIAL_IO)))
13641523 return -EIO;
1365− return read_from_bdev_sync(zram, page, entry);
1524+ return read_from_bdev_sync(zram, page, index, blk_idx);
13661525 }
1367− read_from_bdev_async(zram, page, entry, parent);
1526+ read_from_bdev_async(zram, page, index, blk_idx, parent);
13681527 return 0;
13691528 }
13701529 #else
13711530 static inline void reset_bdev(struct zram *zram) {};
1372−static int read_from_bdev(struct zram *zram, struct page *page,
1373− unsigned long entry, struct bio *parent)
1531+static int read_from_bdev(struct zram *zram, struct page *page, u32 index,
1532+ unsigned long blk_idx, struct bio *parent)
13741533 {
13751534 return -EIO;
13761535 }
@@ -1401,15 +1560,13 @@ static ssize_t read_block_state(struct file *file, char __user *buf, · 24 unmodified lines
14011560 ssize_t index, written = 0;
14021561 struct zram *zram = file->private_data;
14031562 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
1404− struct timespec64 ts;
14051563
14061564 kbuf = kvmalloc(count, GFP_KERNEL);
14071565 if (!kbuf)
14081566 return -ENOMEM;
14091567
1410− down_read(&zram->init_lock);
1568+ guard(rwsem_read)(&zram->dev_lock);
14111569 if (!init_done(zram)) {
1412− up_read(&zram->init_lock);
14131570 kvfree(kbuf);
14141571 return -EINVAL;
14151572 }
@@ -1417,35 +1574,32 @@ static ssize_t read_block_state(struct file *file, char __user *buf, · 1 unmodified line
14171574 for (index = *ppos; index < nr_pages; index++) {
14181575 int copied;
14191576
1420− zram_slot_lock(zram, index);
1421− if (!zram_allocated(zram, index))
1577+ slot_lock(zram, index);
1578+ if (!slot_allocated(zram, index))
14221579 goto next;
14231580
1424− ts = ktime_to_timespec64(zram->table[index].ac_time);
14251581 copied = snprintf(kbuf + written, count,
1426− "%12zd %12lld.%06lu %c%c%c%c%c%c\n",
1427− index, (s64)ts.tv_sec,
1428− ts.tv_nsec / NSEC_PER_USEC,
1429− zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
1430− zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
1431− zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.',
1432− zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.',
1433− zram_get_priority(zram, index) ? 'r' : '.',
1434− zram_test_flag(zram, index,
1582+ "%12zd %12u.%06d %c%c%c%c%c%c\n",
1583+ index, zram->table[index].attr.ac_time, 0,
1584+ test_slot_flag(zram, index, ZRAM_SAME) ? 's' : '.',
1585+ test_slot_flag(zram, index, ZRAM_WB) ? 'w' : '.',
1586+ test_slot_flag(zram, index, ZRAM_HUGE) ? 'h' : '.',
1587+ test_slot_flag(zram, index, ZRAM_IDLE) ? 'i' : '.',
1588+ get_slot_comp_priority(zram, index) ? 'r' : '.',
1589+ test_slot_flag(zram, index,
14351590 ZRAM_INCOMPRESSIBLE) ? 'n' : '.');
14361591
14371592 if (count <= copied) {
1438− zram_slot_unlock(zram, index);
1593+ slot_unlock(zram, index);
14391594 break;
14401595 }
14411596 written += copied;
14421597 count -= copied;
14431598 next:
1444− zram_slot_unlock(zram, index);
1599+ slot_unlock(zram, index);
14451600 *ppos += 1;
14461601 }
14471602
1448− up_read(&zram->init_lock);
14491603 if (copy_to_user(buf, kbuf, written))
14501604 written = -EFAULT;
14511605 kvfree(kbuf);
@@ -1512,16 +1666,14 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) · 60 unmodified lines
15121666 return -EINVAL;
15131667 }
15141668
1515− down_write(&zram->init_lock);
1669+ guard(rwsem_write)(&zram->dev_lock);
15161670 if (init_done(zram)) {
1517− up_write(&zram->init_lock);
15181671 kfree(compressor);
15191672 pr_info("Can't change algorithm for initialized device\n");
15201673 return -EBUSY;
15211674 }
15221675
15231676 comp_algorithm_set(zram, prio, compressor);
1524− up_write(&zram->init_lock);
15251677 return 0;
15261678 }
15271679
@@ -1642,9 +1794,8 @@ static ssize_t comp_algorithm_show(struct device *dev, · 114 unmodified lines
16421794 struct zram *zram = dev_to_zram(dev);
16431795 ssize_t sz;
16441796
1645− down_read(&zram->init_lock);
1797+ guard(rwsem_read)(&zram->dev_lock);
16461798 sz = zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_COMP], buf, 0);
1647− up_read(&zram->init_lock);
16481799 return sz;
16491800 }
16501801
@@ -1669,7 +1820,7 @@ static ssize_t recomp_algorithm_show(struct device *dev, · 18 unmodified lines
16691820 ssize_t sz = 0;
16701821 u32 prio;
16711822
1672− down_read(&zram->init_lock);
1823+ guard(rwsem_read)(&zram->dev_lock);
16731824 for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
16741825 if (!zram->comp_algs[prio])
16751826 continue;
@@ -1677,7 +1828,6 @@ static ssize_t recomp_algorithm_show(struct device *dev, · 1 unmodified line
16771828 sz += sysfs_emit_at(buf, sz, "#%d: ", prio);
16781829 sz += zcomp_available_show(zram->comp_algs[prio], buf, sz);
16791830 }
1680− up_read(&zram->init_lock);
16811831 return sz;
16821832 }
16831833
@@ -1723,42 +1873,38 @@ static ssize_t recomp_algorithm_store(struct device *dev, · 39 unmodified lines
17231873 }
17241874 #endif
17251875
1726−static ssize_t compact_store(struct device *dev,
1727− struct device_attribute *attr, const char *buf, size_t len)
1876+static ssize_t compact_store(struct device *dev, struct device_attribute *attr,
1877+ const char *buf, size_t len)
17281878 {
17291879 struct zram *zram = dev_to_zram(dev);
17301880
1731− down_read(&zram->init_lock);
1732− if (!init_done(zram)) {
1733− up_read(&zram->init_lock);
1881+ guard(rwsem_read)(&zram->dev_lock);
1882+ if (!init_done(zram))
17341883 return -EINVAL;
1735− }
17361884
17371885 zs_compact(zram->mem_pool);
1738− up_read(&zram->init_lock);
17391886
17401887 return len;
17411888 }
17421889
1743−static ssize_t io_stat_show(struct device *dev,
1744− struct device_attribute *attr, char *buf)
1890+static ssize_t io_stat_show(struct device *dev, struct device_attribute *attr,
1891+ char *buf)
17451892 {
17461893 struct zram *zram = dev_to_zram(dev);
17471894 ssize_t ret;
17481895
1749− down_read(&zram->init_lock);
1896+ guard(rwsem_read)(&zram->dev_lock);
17501897 ret = sysfs_emit(buf,
17511898 "%8llu %8llu 0 %8llu\n",
17521899 (u64)atomic64_read(&zram->stats.failed_reads),
17531900 (u64)atomic64_read(&zram->stats.failed_writes),
17541901 (u64)atomic64_read(&zram->stats.notify_free));
1755− up_read(&zram->init_lock);
17561902
17571903 return ret;
17581904 }
17591905
1760−static ssize_t mm_stat_show(struct device *dev,
1761− struct device_attribute *attr, char *buf)
1906+static ssize_t mm_stat_show(struct device *dev, struct device_attribute *attr,
1907+ char *buf)
17621908 {
17631909 struct zram *zram = dev_to_zram(dev);
17641910 struct zs_pool_stats pool_stats;
@@ -1768,7 +1914,7 @@ static ssize_t mm_stat_show(struct device *dev, · 3 unmodified lines
17681914
17691915 memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
17701916
1771− down_read(&zram->init_lock);
1917+ guard(rwsem_read)(&zram->dev_lock);
17721918 if (init_done(zram)) {
17731919 mem_used = zs_get_total_pages(zram->mem_pool);
17741920 zs_pool_stats(zram->mem_pool, &pool_stats);
@@ -1788,55 +1934,26 @@ static ssize_t mm_stat_show(struct device *dev, · 13 unmodified lines
17881934 atomic_long_read(&pool_stats.pages_compacted),
17891935 (u64)atomic64_read(&zram->stats.huge_pages),
17901936 (u64)atomic64_read(&zram->stats.huge_pages_since));
1791− up_read(&zram->init_lock);
17921937
17931938 return ret;
17941939 }
17951940
1796−#ifdef CONFIG_ZRAM_WRITEBACK
1797−#define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
1798−static ssize_t bd_stat_show(struct device *dev,
1799− struct device_attribute *attr, char *buf)
1800−{
1801− struct zram *zram = dev_to_zram(dev);
1802− ssize_t ret;
1803−
1804− down_read(&zram->init_lock);
1805− ret = sysfs_emit(buf,
1806− "%8llu %8llu %8llu\n",
1807− FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
1808− FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
1809− FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
1810− up_read(&zram->init_lock);
1811−
1812− return ret;
1813−}
1814−#endif
1815−
18161941 static ssize_t debug_stat_show(struct device *dev,
1817− struct device_attribute *attr, char *buf)
1942+ struct device_attribute *attr, char *buf)
18181943 {
18191944 int version = 1;
18201945 struct zram *zram = dev_to_zram(dev);
18211946 ssize_t ret;
18221947
1823− down_read(&zram->init_lock);
1948+ guard(rwsem_read)(&zram->dev_lock);
18241949 ret = sysfs_emit(buf,
18251950 "version: %d\n0 %8llu\n",
18261951 version,
18271952 (u64)atomic64_read(&zram->stats.miss_free));
1828− up_read(&zram->init_lock);
18291953
18301954 return ret;
18311955 }
18321956
1833−static DEVICE_ATTR_RO(io_stat);
1834−static DEVICE_ATTR_RO(mm_stat);
1835−#ifdef CONFIG_ZRAM_WRITEBACK
1836−static DEVICE_ATTR_RO(bd_stat);
1837−#endif
1838−static DEVICE_ATTR_RO(debug_stat);
1839−
18401957 static void zram_meta_free(struct zram *zram, u64 disksize)
18411958 {
18421959 size_t num_pages = disksize >> PAGE_SHIFT;
@@ -1847,7 +1964,7 @@ static void zram_meta_free(struct zram *zram, u64 disksize) · 4 unmodified lines
18471964
18481965 /* Free all pages that are still in this zram device */
18491966 for (index = 0; index < num_pages; index++)
1850− zram_free_page(zram, index);
1967+ slot_free(zram, index);
18511968
18521969 zs_destroy_pool(zram->mem_pool);
18531970 vfree(zram->table);
@@ -1874,32 +1991,32 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize) · 20 unmodified lines
18741991 huge_class_size = zs_huge_class_size(zram->mem_pool);
18751992
18761993 for (index = 0; index < num_pages; index++)
1877− zram_slot_lock_init(zram, index);
1994+ slot_lock_init(zram, index);
18781995
18791996 return true;
18801997 }
18811998
1882−static void zram_free_page(struct zram *zram, size_t index)
1999+static void slot_free(struct zram *zram, u32 index)
18832000 {
18842001 unsigned long handle;
18852002
18862003 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
1887− zram->table[index].ac_time = 0;
2004+ zram->table[index].attr.ac_time = 0;
18882005 #endif
18892006
1890− zram_clear_flag(zram, index, ZRAM_IDLE);
1891− zram_clear_flag(zram, index, ZRAM_INCOMPRESSIBLE);
1892− zram_clear_flag(zram, index, ZRAM_PP_SLOT);
1893− zram_set_priority(zram, index, 0);
2007+ clear_slot_flag(zram, index, ZRAM_IDLE);
2008+ clear_slot_flag(zram, index, ZRAM_INCOMPRESSIBLE);
2009+ clear_slot_flag(zram, index, ZRAM_PP_SLOT);
2010+ set_slot_comp_priority(zram, index, 0);
18942011
1895− if (zram_test_flag(zram, index, ZRAM_HUGE)) {
1896− zram_clear_flag(zram, index, ZRAM_HUGE);
2012+ if (test_slot_flag(zram, index, ZRAM_HUGE)) {
2013+ clear_slot_flag(zram, index, ZRAM_HUGE);
18972014 atomic64_dec(&zram->stats.huge_pages);
18982015 }
18992016
1900− if (zram_test_flag(zram, index, ZRAM_WB)) {
1901− zram_clear_flag(zram, index, ZRAM_WB);
1902− zram_release_bdev_block(zram, zram_get_handle(zram, index));
2017+ if (test_slot_flag(zram, index, ZRAM_WB)) {
2018+ clear_slot_flag(zram, index, ZRAM_WB);
2019+ zram_release_bdev_block(zram, get_slot_handle(zram, index));
19032020 goto out;
19042021 }
19052022
@@ -1907,24 +2024,24 @@ static void zram_free_page(struct zram *zram, size_t index) · 1 unmodified line
19072024 * No memory is allocated for same element filled pages.
19082025 * Simply clear same page flag.
19092026 */
1910− if (zram_test_flag(zram, index, ZRAM_SAME)) {
1911− zram_clear_flag(zram, index, ZRAM_SAME);
2027+ if (test_slot_flag(zram, index, ZRAM_SAME)) {
2028+ clear_slot_flag(zram, index, ZRAM_SAME);
19122029 atomic64_dec(&zram->stats.same_pages);
19132030 goto out;
19142031 }
19152032
1916− handle = zram_get_handle(zram, index);
2033+ handle = get_slot_handle(zram, index);
19172034 if (!handle)
19182035 return;
19192036
19202037 zs_free(zram->mem_pool, handle);
19212038
1922− atomic64_sub(zram_get_obj_size(zram, index),
2039+ atomic64_sub(get_slot_size(zram, index),
19232040 &zram->stats.compr_data_size);
19242041 out:
19252042 atomic64_dec(&zram->stats.pages_stored);
1926− zram_set_handle(zram, index, 0);
1927− zram_set_obj_size(zram, index, 0);
2043+ set_slot_handle(zram, index, 0);
2044+ set_slot_size(zram, index, 0);
19282045 }
19292046
19302047 static int read_same_filled_page(struct zram *zram, struct page *page,
@@ -1933,7 +2050,7 @@ static int read_same_filled_page(struct zram *zram, struct page *page, · 2 unmodified lines
19332050 void *mem;
19342051
19352052 mem = kmap_local_page(page);
1936− zram_fill_page(mem, PAGE_SIZE, zram_get_handle(zram, index));
2053+ zram_fill_page(mem, PAGE_SIZE, get_slot_handle(zram, index));
19372054 kunmap_local(mem);
19382055 return 0;
19392056 }
@@ -1944,12 +2061,12 @@ static int read_incompressible_page(struct zram *zram, struct page *page, · 4 unmodified lines
19442061 unsigned long handle;
19452062 void *src, *dst;
19462063
1947− handle = zram_get_handle(zram, index);
1948− src = zs_obj_read_begin(zram->mem_pool, handle, NULL);
2064+ handle = get_slot_handle(zram, index);
2065+ src = zs_obj_read_begin(zram->mem_pool, handle, PAGE_SIZE, NULL);
19492066 dst = kmap_local_page(page);
19502067 copy_page(dst, src);
19512068 kunmap_local(dst);
1952− zs_obj_read_end(zram->mem_pool, handle, src);
2069+ zs_obj_read_end(zram->mem_pool, handle, PAGE_SIZE, src);
19532070
19542071 return 0;
19552072 }
@@ -1962,33 +2079,60 @@ static int read_compressed_page(struct zram *zram, struct page *page, u32 index) · 6 unmodified lines
19622079 void *src, *dst;
19632080 int ret, prio;
19642081
1965− handle = zram_get_handle(zram, index);
1966− size = zram_get_obj_size(zram, index);
1967− prio = zram_get_priority(zram, index);
2082+ handle = get_slot_handle(zram, index);
2083+ size = get_slot_size(zram, index);
2084+ prio = get_slot_comp_priority(zram, index);
19682085
19692086 zstrm = zcomp_stream_get(zram->comps[prio]);
1970− src = zs_obj_read_begin(zram->mem_pool, handle, zstrm->local_copy);
2087+ src = zs_obj_read_begin(zram->mem_pool, handle, size,
2088+ zstrm->local_copy);
19712089 dst = kmap_local_page(page);
19722090 ret = zcomp_decompress(zram->comps[prio], zstrm, src, size, dst);
19732091 kunmap_local(dst);
1974− zs_obj_read_end(zram->mem_pool, handle, src);
2092+ zs_obj_read_end(zram->mem_pool, handle, size, src);
19752093 zcomp_stream_put(zstrm);
19762094
19772095 return ret;
19782096 }
19792097
2098+#if defined CONFIG_ZRAM_WRITEBACK
2099+static int read_from_zspool_raw(struct zram *zram, struct page *page, u32 index)
2100+{
2101+ struct zcomp_strm *zstrm;
2102+ unsigned long handle;
2103+ unsigned int size;
2104+ void *src;
2105+
2106+ handle = get_slot_handle(zram, index);
2107+ size = get_slot_size(zram, index);
2108+
2109+ /*
2110+ * We need to get stream just for ->local_copy buffer, in
2111+ * case if object spans two physical pages. No decompression
2112+ * takes place here, as we read raw compressed data.
2113+ */
2114+ zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
2115+ src = zs_obj_read_begin(zram->mem_pool, handle, size,
2116+ zstrm->local_copy);
2117+ memcpy_to_page(page, 0, src, size);
2118+ zs_obj_read_end(zram->mem_pool, handle, size, src);
2119+ zcomp_stream_put(zstrm);
2120+
2121+ return 0;
2122+}
2123+#endif
2124+
19802125 /*
19812126 * Reads (decompresses if needed) a page from zspool (zsmalloc).
19822127 * Corresponding ZRAM slot should be locked.
19832128 */
1984−static int zram_read_from_zspool(struct zram *zram, struct page *page,
1985− u32 index)
2129+static int read_from_zspool(struct zram *zram, struct page *page, u32 index)
19862130 {
1987− if (zram_test_flag(zram, index, ZRAM_SAME) ||
1988− !zram_get_handle(zram, index))
2131+ if (test_slot_flag(zram, index, ZRAM_SAME) ||
2132+ !get_slot_handle(zram, index))
19892133 return read_same_filled_page(zram, page, index);
19902134
1991− if (!zram_test_flag(zram, index, ZRAM_HUGE))
2135+ if (!test_slot_flag(zram, index, ZRAM_HUGE))
19922136 return read_compressed_page(zram, page, index);
19932137 else
19942138 return read_incompressible_page(zram, page, index);
@@ -1999,20 +2143,20 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index, · 4 unmodified lines
19992143 {
20002144 int ret;
20012145
2002− zram_slot_lock(zram, index);
2003− if (!zram_test_flag(zram, index, ZRAM_WB)) {
2146+ slot_lock(zram, index);
2147+ if (!test_slot_flag(zram, index, ZRAM_WB)) {
20042148 /* Slot should be locked through out the function call */
2005− ret = zram_read_from_zspool(zram, page, index);
2006− zram_slot_unlock(zram, index);
2149+ ret = read_from_zspool(zram, page, index);
2150+ slot_unlock(zram, index);
20072151 } else {
2008− unsigned long blk_idx = zram_get_handle(zram, index);
2152+ unsigned long blk_idx = get_slot_handle(zram, index);
20092153
20102154 /*
20112155 * The slot should be unlocked before reading from the backing
20122156 * device.
20132157 */
2014− zram_slot_unlock(zram, index);
2015− ret = read_from_bdev(zram, page, blk_idx, parent);
2158+ slot_unlock(zram, index);
2159+ ret = read_from_bdev(zram, page, index, blk_idx, parent);
20162160 }
20172161
20182162 /* Should NEVER happen. Return bio error if it does. */
@@ -2052,11 +2196,11 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, · 33 unmodified lines
20522196 static int write_same_filled_page(struct zram *zram, unsigned long fill,
20532197 u32 index)
20542198 {
2055− zram_slot_lock(zram, index);
2056− zram_free_page(zram, index);
2057− zram_set_flag(zram, index, ZRAM_SAME);
2058− zram_set_handle(zram, index, fill);
2059− zram_slot_unlock(zram, index);
2199+ slot_lock(zram, index);
2200+ slot_free(zram, index);
2201+ set_slot_flag(zram, index, ZRAM_SAME);
2202+ set_slot_handle(zram, index, fill);
2203+ slot_unlock(zram, index);
20602204
20612205 atomic64_inc(&zram->stats.same_pages);
20622206 atomic64_inc(&zram->stats.pages_stored);
@@ -2090,12 +2234,12 @@ static int write_incompressible_page(struct zram *zram, struct page *page, · 27 unmodified lines
20902234 zs_obj_write(zram->mem_pool, handle, src, PAGE_SIZE);
20912235 kunmap_local(src);
20922236
2093− zram_slot_lock(zram, index);
2094− zram_free_page(zram, index);
2095− zram_set_flag(zram, index, ZRAM_HUGE);
2096− zram_set_handle(zram, index, handle);
2097− zram_set_obj_size(zram, index, PAGE_SIZE);
2098− zram_slot_unlock(zram, index);
2237+ slot_lock(zram, index);
2238+ slot_free(zram, index);
2239+ set_slot_flag(zram, index, ZRAM_HUGE);
2240+ set_slot_handle(zram, index, handle);
2241+ set_slot_size(zram, index, PAGE_SIZE);
2242+ slot_unlock(zram, index);
20992243
21002244 atomic64_add(PAGE_SIZE, &zram->stats.compr_data_size);
21012245 atomic64_inc(&zram->stats.huge_pages);
@@ -2155,11 +2299,11 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) · 53 unmodified lines
21552299 zs_obj_write(zram->mem_pool, handle, zstrm->buffer, comp_len);
21562300 zcomp_stream_put(zstrm);
21572301
2158− zram_slot_lock(zram, index);
2159− zram_free_page(zram, index);
2160− zram_set_handle(zram, index, handle);
2161− zram_set_obj_size(zram, index, comp_len);
2162− zram_slot_unlock(zram, index);
2302+ slot_lock(zram, index);
2303+ slot_free(zram, index);
2304+ set_slot_handle(zram, index, handle);
2305+ set_slot_size(zram, index, comp_len);
2306+ slot_unlock(zram, index);
21632307
21642308 /* Update stats */
21652309 atomic64_inc(&zram->stats.pages_stored);
@@ -2210,30 +2354,30 @@ static int scan_slots_for_recompress(struct zram *zram, u32 mode, u32 prio_max, · 44 unmodified lines
22102354 for (index = 0; index < nr_pages; index++) {
22112355 bool ok = true;
22122356
2213− zram_slot_lock(zram, index);
2214− if (!zram_allocated(zram, index))
2357+ slot_lock(zram, index);
2358+ if (!slot_allocated(zram, index))
22152359 goto next;
22162360
22172361 if (mode & RECOMPRESS_IDLE &&
2218− !zram_test_flag(zram, index, ZRAM_IDLE))
2362+ !test_slot_flag(zram, index, ZRAM_IDLE))
22192363 goto next;
22202364
22212365 if (mode & RECOMPRESS_HUGE &&
2222− !zram_test_flag(zram, index, ZRAM_HUGE))
2366+ !test_slot_flag(zram, index, ZRAM_HUGE))
22232367 goto next;
22242368
2225− if (zram_test_flag(zram, index, ZRAM_WB) ||
2226− zram_test_flag(zram, index, ZRAM_SAME) ||
2227− zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
2369+ if (test_slot_flag(zram, index, ZRAM_WB) ||
2370+ test_slot_flag(zram, index, ZRAM_SAME) ||
2371+ test_slot_flag(zram, index, ZRAM_INCOMPRESSIBLE))
22282372 goto next;
22292373
22302374 /* Already compressed with same of higher priority */
2231− if (zram_get_priority(zram, index) + 1 >= prio_max)
2375+ if (get_slot_comp_priority(zram, index) + 1 >= prio_max)
22322376 goto next;
22332377
22342378 ok = place_pp_slot(zram, ctl, index);
22352379 next:
2236− zram_slot_unlock(zram, index);
2380+ slot_unlock(zram, index);
22372381 if (!ok)
22382382 break;
22392383 }
@@ -2262,18 +2406,18 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, · 22 unmodified lines
22622406 void *src;
22632407 int ret = 0;
22642408
2265− handle_old = zram_get_handle(zram, index);
2409+ handle_old = get_slot_handle(zram, index);
22662410 if (!handle_old)
22672411 return -EINVAL;
22682412
2269− comp_len_old = zram_get_obj_size(zram, index);
2413+ comp_len_old = get_slot_size(zram, index);
22702414 /*
22712415 * Do not recompress objects that are already "small enough".
22722416 */
22732417 if (comp_len_old < threshold)
22742418 return 0;
22752419
2276− ret = zram_read_from_zspool(zram, page, index);
2420+ ret = read_from_zspool(zram, page, index);
22772421 if (ret)
22782422 return ret;
22792423
@@ -2282,11 +2426,11 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, · 2 unmodified lines
22822426 * we don't preserve IDLE flag and don't incorrectly pick this entry
22832427 * for different post-processing type (e.g. writeback).
22842428 */
2285− zram_clear_flag(zram, index, ZRAM_IDLE);
2429+ clear_slot_flag(zram, index, ZRAM_IDLE);
22862430
22872431 class_index_old = zs_lookup_class_index(zram->mem_pool, comp_len_old);
22882432
2289− prio = max(prio, zram_get_priority(zram, index) + 1);
2433+ prio = max(prio, get_slot_comp_priority(zram, index) + 1);
22902434 /*
22912435 * Recompression slots scan should not select slots that are
22922436 * already compressed with a higher priority algorithm, but
@@ -2353,7 +2497,7 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, · 60 unmodified lines
23532497 */
23542498 if (prio < zram->num_active_comps)
23552499 return 0;
2356− zram_set_flag(zram, index, ZRAM_INCOMPRESSIBLE);
2500+ set_slot_flag(zram, index, ZRAM_INCOMPRESSIBLE);
23572501 return 0;
23582502 }
23592503
@@ -2362,14 +2506,15 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, · 2 unmodified lines
23622506 * avoid direct reclaim. Allocation error is not fatal since
23632507 * we still have the old object in the mem_pool.
23642508 *
2365− * XXX: technically, the node we really want here is the node that holds
2366− * the original compressed data. But that would require us to modify
2367− * zsmalloc API to return this information. For now, we will make do with
2368− * the node of the page allocated for recompression.
2509+ * XXX: technically, the node we really want here is the node that
2510+ * holds the original compressed data. But that would require us to
2511+ * modify zsmalloc API to return this information. For now, we will
2512+ * make do with the node of the page allocated for recompression.
23692513 */
23702514 handle_new = zs_malloc(zram->mem_pool, comp_len_new,
23712515 GFP_NOIO | __GFP_NOWARN |
2372− __GFP_HIGHMEM | __GFP_MOVABLE, page_to_nid(page));
2516+ __GFP_HIGHMEM | __GFP_MOVABLE,
2517+ page_to_nid(page));
23732518 if (IS_ERR_VALUE(handle_new)) {
23742519 zcomp_stream_put(zstrm);
23752520 return PTR_ERR((void *)handle_new);
@@ -2378,10 +2523,10 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, · 2 unmodified lines
23782523 zs_obj_write(zram->mem_pool, handle_new, zstrm->buffer, comp_len_new);
23792524 zcomp_stream_put(zstrm);
23802525
2381− zram_free_page(zram, index);
2382− zram_set_handle(zram, index, handle_new);
2383− zram_set_obj_size(zram, index, comp_len_new);
2384− zram_set_priority(zram, index, prio);
2526+ slot_free(zram, index);
2527+ set_slot_handle(zram, index, handle_new);
2528+ set_slot_size(zram, index, comp_len_new);
2529+ set_slot_comp_priority(zram, index, prio);
23852530
23862531 atomic64_add(comp_len_new, &zram->stats.compr_data_size);
23872532 atomic64_inc(&zram->stats.pages_stored);
@@ -2466,17 +2611,9 @@ static ssize_t recompress_store(struct device *dev, · 78 unmodified lines
24662611 if (threshold >= huge_class_size)
24672612 return -EINVAL;
24682613
2469− down_read(&zram->init_lock);
2470− if (!init_done(zram)) {
2471− ret = -EINVAL;
2472− goto release_init_lock;
2473− }
2474−
2475− /* Do not permit concurrent post-processing actions. */
2476− if (atomic_xchg(&zram->pp_in_progress, 1)) {
2477− up_read(&zram->init_lock);
2478− return -EAGAIN;
2479− }
2614+ guard(rwsem_write)(&zram->dev_lock);
2615+ if (!init_done(zram))
2616+ return -EINVAL;
24802617
24812618 if (algo) {
24822619 bool found = false;
@@ -2494,26 +2631,26 @@ static ssize_t recompress_store(struct device *dev, · 11 unmodified lines
24942631
24952632 if (!found) {
24962633 ret = -EINVAL;
2497− goto release_init_lock;
2634+ goto out;
24982635 }
24992636 }
25002637
25012638 prio_max = min(prio_max, (u32)zram->num_active_comps);
25022639 if (prio >= prio_max) {
25032640 ret = -EINVAL;
2504− goto release_init_lock;
2641+ goto out;
25052642 }
25062643
25072644 page = alloc_page(GFP_KERNEL);
25082645 if (!page) {
25092646 ret = -ENOMEM;
2510− goto release_init_lock;
2647+ goto out;
25112648 }
25122649
25132650 ctl = init_pp_ctl();
25142651 if (!ctl) {
25152652 ret = -ENOMEM;
2516− goto release_init_lock;
2653+ goto out;
25172654 }
25182655
25192656 scan_slots_for_recompress(zram, mode, prio_max, ctl);
@@ -2525,15 +2662,15 @@ static ssize_t recompress_store(struct device *dev, · 5 unmodified lines
25252662 if (!num_recomp_pages)
25262663 break;
25272664
2528− zram_slot_lock(zram, pps->index);
2529− if (!zram_test_flag(zram, pps->index, ZRAM_PP_SLOT))
2665+ slot_lock(zram, pps->index);
2666+ if (!test_slot_flag(zram, pps->index, ZRAM_PP_SLOT))
25302667 goto next;
25312668
25322669 err = recompress_slot(zram, pps->index, page,
25332670 &num_recomp_pages, threshold,
25342671 prio, prio_max);
25352672 next:
2536− zram_slot_unlock(zram, pps->index);
2673+ slot_unlock(zram, pps->index);
25372674 release_pp_slot(zram, pps);
25382675
25392676 if (err) {
@@ -2544,12 +2681,10 @@ next: · 4 unmodified lines
25442681 cond_resched();
25452682 }
25462683
2547−release_init_lock:
2684+out:
25482685 if (page)
25492686 __free_page(page);
25502687 release_pp_ctl(zram, ctl);
2551− atomic_set(&zram->pp_in_progress, 0);
2552− up_read(&zram->init_lock);
25532688 return ret;
25542689 }
25552690 #endif
@@ -2580,9 +2715,9 @@ static void zram_bio_discard(struct zram *zram, struct bio *bio) · 24 unmodified lines
25802715 }
25812716
25822717 while (n >= PAGE_SIZE) {
2583− zram_slot_lock(zram, index);
2584− zram_free_page(zram, index);
2585− zram_slot_unlock(zram, index);
2718+ slot_lock(zram, index);
2719+ slot_free(zram, index);
2720+ slot_unlock(zram, index);
25862721 atomic64_inc(&zram->stats.notify_free);
25872722 index++;
25882723 n -= PAGE_SIZE;
@@ -2611,9 +2746,9 @@ static void zram_bio_read(struct zram *zram, struct bio *bio) · 22 unmodified lines
26112746 }
26122747 flush_dcache_page(bv.bv_page);
26132748
2614− zram_slot_lock(zram, index);
2615− zram_accessed(zram, index);
2616− zram_slot_unlock(zram, index);
2749+ slot_lock(zram, index);
2750+ mark_slot_accessed(zram, index);
2751+ slot_unlock(zram, index);
26172752
26182753 bio_advance_iter_single(bio, &iter, bv.bv_len);
26192754 } while (iter.bi_size);
@@ -2641,9 +2776,9 @@ static void zram_bio_write(struct zram *zram, struct bio *bio) · 21 unmodified lines
26412776 break;
26422777 }
26432778
2644− zram_slot_lock(zram, index);
2645− zram_accessed(zram, index);
2646− zram_slot_unlock(zram, index);
2779+ slot_lock(zram, index);
2780+ mark_slot_accessed(zram, index);
2781+ slot_unlock(zram, index);
26472782
26482783 bio_advance_iter_single(bio, &iter, bv.bv_len);
26492784 } while (iter.bi_size);
@@ -2684,13 +2819,13 @@ static void zram_slot_free_notify(struct block_device *bdev, · 34 unmodified lines
26842819 zram = bdev->bd_disk->private_data;
26852820
26862821 atomic64_inc(&zram->stats.notify_free);
2687− if (!zram_slot_trylock(zram, index)) {
2822+ if (!slot_trylock(zram, index)) {
26882823 atomic64_inc(&zram->stats.miss_free);
26892824 return;
26902825 }
26912826
2692− zram_free_page(zram, index);
2693− zram_slot_unlock(zram, index);
2827+ slot_free(zram, index);
2828+ slot_unlock(zram, index);
26942829 }
26952830
26962831 static void zram_comp_params_reset(struct zram *zram)
@@ -2728,7 +2863,7 @@ static void zram_destroy_comps(struct zram *zram) · 31 unmodified lines
27282863
27292864 static void zram_reset_device(struct zram *zram)
27302865 {
2731− down_write(&zram->init_lock);
2866+ guard(rwsem_write)(&zram->dev_lock);
27322867
27332868 zram->limit_pages = 0;
27342869
@@ -2740,15 +2875,13 @@ static void zram_reset_device(struct zram *zram) · 5 unmodified lines
27402875 zram->disksize = 0;
27412876 zram_destroy_comps(zram);
27422877 memset(&zram->stats, 0, sizeof(zram->stats));
2743− atomic_set(&zram->pp_in_progress, 0);
27442878 reset_bdev(zram);
27452879
27462880 comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
2747− up_write(&zram->init_lock);
27482881 }
27492882
2750−static ssize_t disksize_store(struct device *dev,
2751− struct device_attribute *attr, const char *buf, size_t len)
2883+static ssize_t disksize_store(struct device *dev, struct device_attribute *attr,
2884+ const char *buf, size_t len)
27522885 {
27532886 u64 disksize;
27542887 struct zcomp *comp;
@@ -2760,18 +2893,15 @@ static ssize_t disksize_store(struct device *dev, · 5 unmodified lines
27602893 if (!disksize)
27612894 return -EINVAL;
27622895
2763− down_write(&zram->init_lock);
2896+ guard(rwsem_write)(&zram->dev_lock);
27642897 if (init_done(zram)) {
27652898 pr_info("Cannot change disksize for initialized device\n");
2766− err = -EBUSY;
2767− goto out_unlock;
2899+ return -EBUSY;
27682900 }
27692901
27702902 disksize = PAGE_ALIGN(disksize);
2771− if (!zram_meta_alloc(zram, disksize)) {
2772− err = -ENOMEM;
2773− goto out_unlock;
2774− }
2903+ if (!zram_meta_alloc(zram, disksize))
2904+ return -ENOMEM;
27752905
27762906 for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
27772907 if (!zram->comp_algs[prio])
@@ -2791,15 +2921,12 @@ static ssize_t disksize_store(struct device *dev, · 13 unmodified lines
27912921 }
27922922 zram->disksize = disksize;
27932923 set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT);
2794− up_write(&zram->init_lock);
27952924
27962925 return len;
27972926
27982927 out_free_comps:
27992928 zram_destroy_comps(zram);
28002929 zram_meta_free(zram, disksize);
2801−out_unlock:
2802− up_write(&zram->init_lock);
28032930 return err;
28042931 }
28052932
@@ -2862,6 +2989,9 @@ static const struct block_device_operations zram_devops = { · 56 unmodified lines
28622989 .owner = THIS_MODULE
28632990 };
28642991
2992+static DEVICE_ATTR_RO(io_stat);
2993+static DEVICE_ATTR_RO(mm_stat);
2994+static DEVICE_ATTR_RO(debug_stat);
28652995 static DEVICE_ATTR_WO(compact);
28662996 static DEVICE_ATTR_RW(disksize);
28672997 static DEVICE_ATTR_RO(initstate);
@@ -2871,11 +3001,13 @@ static DEVICE_ATTR_WO(mem_used_max); · 3 unmodified lines
28713001 static DEVICE_ATTR_WO(idle);
28723002 static DEVICE_ATTR_RW(comp_algorithm);
28733003 #ifdef CONFIG_ZRAM_WRITEBACK
3004+static DEVICE_ATTR_RO(bd_stat);
28743005 static DEVICE_ATTR_RW(backing_dev);
28753006 static DEVICE_ATTR_WO(writeback);
28763007 static DEVICE_ATTR_RW(writeback_limit);
28773008 static DEVICE_ATTR_RW(writeback_limit_enable);
28783009 static DEVICE_ATTR_RW(writeback_batch_size);
3010+static DEVICE_ATTR_RW(writeback_compressed);
28793011 #endif
28803012 #ifdef CONFIG_ZRAM_MULTI_COMP
28813013 static DEVICE_ATTR_RW(recomp_algorithm);
@@ -2893,17 +3025,16 @@ static struct attribute *zram_disk_attrs[] = { · 11 unmodified lines
28933025 &dev_attr_idle.attr,
28943026 &dev_attr_comp_algorithm.attr,
28953027 #ifdef CONFIG_ZRAM_WRITEBACK
3028+ &dev_attr_bd_stat.attr,
28963029 &dev_attr_backing_dev.attr,
28973030 &dev_attr_writeback.attr,
28983031 &dev_attr_writeback_limit.attr,
28993032 &dev_attr_writeback_limit_enable.attr,
29003033 &dev_attr_writeback_batch_size.attr,
3034+ &dev_attr_writeback_compressed.attr,
29013035 #endif
29023036 &dev_attr_io_stat.attr,
29033037 &dev_attr_mm_stat.attr,
2904−#ifdef CONFIG_ZRAM_WRITEBACK
2905− &dev_attr_bd_stat.attr,
2906−#endif
29073038 &dev_attr_debug_stat.attr,
29083039 #ifdef CONFIG_ZRAM_MULTI_COMP
29093040 &dev_attr_recomp_algorithm.attr,
@@ -2957,9 +3088,10 @@ static int zram_add(void) · 47 unmodified lines
29573088 goto out_free_dev;
29583089 device_id = ret;
29593090
2960− init_rwsem(&zram->init_lock);
3091+ init_rwsem(&zram->dev_lock);
29613092 #ifdef CONFIG_ZRAM_WRITEBACK
29623093 zram->wb_batch_size = 32;
3094+ zram->wb_compressed = false;
29633095 #endif
29643096
29653097 /* gendisk structure */
@@ -2978,7 +3110,6 @@ static int zram_add(void) · 12 unmodified lines
29783110 zram->disk->fops = &zram_devops;
29793111 zram->disk->private_data = zram;
29803112 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
2981− atomic_set(&zram->pp_in_progress, 0);
29823113 zram_comp_params_reset(zram);
29833114 comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
29843115
@@ -3139,7 +3270,7 @@ static int __init zram_init(void) · 154 unmodified lines
31393270 struct zram_table_entry zram_te;
31403271 int ret;
31413272
3142− BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.flags) * 8);
3273+ BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.attr.flags) * 8);
31433274
31443275 ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
31453276 zcomp_cpu_up_prepare, zcomp_cpu_dead);
drivers/block/zram/zram_drv.h+10−5
@@ -65,10 +65,15 @@ enum zram_pageflags { · 64 unmodified lines
6565 */
6666 struct zram_table_entry {
6767 unsigned long handle;
68− unsigned long flags;
68+ union {
69+ unsigned long __lock;
70+ struct attr {
71+ u32 flags;
6972 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
70− ktime_t ac_time;
73+ u32 ac_time;
7174 #endif
75+ } attr;
76+ };
7277 struct lockdep_map dep_map;
7378 };
7479
@@ -106,8 +111,8 @@ struct zram { · 31 unmodified lines
106111 struct zcomp *comps[ZRAM_MAX_COMPS];
107112 struct zcomp_params params[ZRAM_MAX_COMPS];
108113 struct gendisk *disk;
109− /* Prevent concurrent execution of device init */
110− struct rw_semaphore init_lock;
114+ /* Locks the device either in exclusive or in shared mode */
115+ struct rw_semaphore dev_lock;
111116 /*
112117 * the number of pages zram can consume for storing compressed data
113118 */
@@ -128,6 +133,7 @@ struct zram { · 14 unmodified lines
128133 #ifdef CONFIG_ZRAM_WRITEBACK
129134 struct file *backing_dev;
130135 bool wb_limit_enable;
136+ bool wb_compressed;
131137 u32 wb_batch_size;
132138 u64 bd_wb_limit;
133139 struct block_device *bdev;
@@ -137,6 +143,5 @@ struct zram { · 3 unmodified lines
137143 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
138144 struct dentry *debugfs_dir;
139145 #endif
140− atomic_t pp_in_progress;
141146 };
142147 #endif
drivers/misc/Kconfig+1−1
@@ -410,7 +410,7 @@ config DS1682 · 409 unmodified lines
410410 config VMWARE_BALLOON
411411 tristate "VMware Balloon Driver"
412412 depends on VMWARE_VMCI && X86 && HYPERVISOR_GUEST
413− select MEMORY_BALLOON
413+ select BALLOON
414414 help
415415 This is VMware physical memory management driver which acts
416416 like a "balloon" that can be inflated to reclaim physical pages
drivers/misc/vmw_balloon.c+29−76
@@ -29,7 +29,7 @@ · 28 unmodified lines
2929 #include <linux/rwsem.h>
3030 #include <linux/slab.h>
3131 #include <linux/spinlock.h>
32−#include <linux/balloon_compaction.h>
32+#include <linux/balloon.h>
3333 #include <linux/vmw_vmci_defs.h>
3434 #include <linux/vmw_vmci_api.h>
3535 #include <asm/hypervisor.h>
@@ -354,10 +354,15 @@ struct vmballoon { · 318 unmodified lines
354354 /**
355355 * @huge_pages - list of the inflated 2MB pages.
356356 *
357− * Protected by @b_dev_info.pages_lock .
357+ * Protected by @huge_pages_lock.
358358 */
359359 struct list_head huge_pages;
360360
361+ /**
362+ * @huge_pages_lock: lock for the list of inflated 2MB pages.
363+ */
364+ spinlock_t huge_pages_lock;
365+
361366 /**
362367 * @vmci_doorbell.
363368 *
@@ -987,7 +992,6 @@ static void vmballoon_enqueue_page_list(struct vmballoon *b, · 623 unmodified lines
987992 unsigned int *n_pages,
988993 enum vmballoon_page_size_type page_size)
989994 {
990− unsigned long flags;
991995 struct page *page;
992996
993997 if (page_size == VMW_BALLOON_4K_PAGE) {
@@ -995,9 +999,9 @@ static void vmballoon_enqueue_page_list(struct vmballoon *b, · 1 unmodified line
995999 } else {
9961000 /*
9971001 * Keep the huge pages in a local list which is not available
998− * for the balloon compaction mechanism.
1002+ * for the balloon page migration.
9991003 */
1000− spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1004+ spin_lock(&b->huge_pages_lock);
10011005
10021006 list_for_each_entry(page, pages, lru) {
10031007 vmballoon_mark_page_offline(page, VMW_BALLOON_2M_PAGE);
@@ -1006,7 +1010,7 @@ static void vmballoon_enqueue_page_list(struct vmballoon *b, · 2 unmodified lines
10061010 list_splice_init(pages, &b->huge_pages);
10071011 __count_vm_events(BALLOON_INFLATE, *n_pages *
10081012 vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1009− spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1013+ spin_unlock(&b->huge_pages_lock);
10101014 }
10111015
10121016 *n_pages = 0;
@@ -1033,7 +1037,6 @@ static void vmballoon_dequeue_page_list(struct vmballoon *b, · 20 unmodified lines
10331037 {
10341038 struct page *page, *tmp;
10351039 unsigned int i = 0;
1036− unsigned long flags;
10371040
10381041 /* In the case of 4k pages, use the compaction infrastructure */
10391042 if (page_size == VMW_BALLOON_4K_PAGE) {
@@ -1043,7 +1046,7 @@ static void vmballoon_dequeue_page_list(struct vmballoon *b, · 3 unmodified lines
10431046 }
10441047
10451048 /* 2MB pages */
1046− spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1049+ spin_lock(&b->huge_pages_lock);
10471050 list_for_each_entry_safe(page, tmp, &b->huge_pages, lru) {
10481051 vmballoon_mark_page_online(page, VMW_BALLOON_2M_PAGE);
10491052
@@ -1054,7 +1057,7 @@ static void vmballoon_dequeue_page_list(struct vmballoon *b, · 4 unmodified lines
10541057
10551058 __count_vm_events(BALLOON_DEFLATE,
10561059 i * vmballoon_page_in_frames(VMW_BALLOON_2M_PAGE));
1057− spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1060+ spin_unlock(&b->huge_pages_lock);
10581061 *n_pages = i;
10591062 }
10601063
@@ -1716,7 +1719,7 @@ static inline void vmballoon_debugfs_exit(struct vmballoon *b) · 655 unmodified lines
17161719 #endif /* CONFIG_DEBUG_FS */
17171720
17181721
1719−#ifdef CONFIG_BALLOON_COMPACTION
1722+#ifdef CONFIG_BALLOON_MIGRATION
17201723 /**
17211724 * vmballoon_migratepage() - migrates a balloon page.
17221725 * @b_dev_info: balloon device information descriptor.
@@ -1724,18 +1727,17 @@ static inline void vmballoon_debugfs_exit(struct vmballoon *b) · 1 unmodified line
17241727 * @page: a ballooned page that should be migrated.
17251728 * @mode: migration mode, ignored.
17261729 *
1727− * This function is really open-coded, but that is according to the interface
1728− * that balloon_compaction provides.
1729− *
17301730 * Return: zero on success, -EAGAIN when migration cannot be performed
1731− * momentarily, and -EBUSY if migration failed and should be retried
1732− * with that specific page.
1731+ * momentarily, -EBUSY if migration failed and should be retried
1732+ * with that specific page, and -ENOENT when deflating @page
1733+ * succeeded but inflating @newpage failed, effectively deflating
1734+ * the balloon.
17331735 */
17341736 static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
17351737 struct page *newpage, struct page *page,
17361738 enum migrate_mode mode)
17371739 {
1738− unsigned long status, flags;
1740+ unsigned long status;
17391741 struct vmballoon *b;
17401742 int ret = 0;
17411743
@@ -1773,14 +1775,6 @@ static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info, · 31 unmodified lines
17731775 goto out_unlock;
17741776 }
17751777
1776− /*
1777− * The page is isolated, so it is safe to delete it without holding
1778− * @pages_lock . We keep holding @comm_lock since we will need it in a
1779− * second.
1780− */
1781− balloon_page_finalize(page);
1782− put_page(page);
1783−
17841778 /* Inflate */
17851779 vmballoon_add_page(b, 0, newpage);
17861780 status = vmballoon_lock_op(b, 1, VMW_BALLOON_4K_PAGE,
@@ -1799,60 +1793,21 @@ static int vmballoon_migratepage(struct balloon_dev_info *b_dev_info, · 12 unmodified lines
17991793 * change.
18001794 */
18011795 atomic64_dec(&b->size);
1802− } else {
18031796 /*
1804− * Success. Take a reference for the page, and we will add it to
1805− * the list after acquiring the lock.
1797+ * Tell the core that we're deflating the old page and don't
1798+ * need the new page.
18061799 */
1807− get_page(newpage);
1808− }
1809−
1810− /* Update the balloon list under the @pages_lock */
1811− spin_lock_irqsave(&b->b_dev_info.pages_lock, flags);
1812−
1813− /*
1814− * On inflation success, we already took a reference for the @newpage.
1815− * If we succeed just insert it to the list and update the statistics
1816− * under the lock.
1817− */
1818− if (status == VMW_BALLOON_SUCCESS) {
1819− balloon_page_insert(&b->b_dev_info, newpage);
1820− __count_vm_event(BALLOON_MIGRATE);
1800+ ret = -ENOENT;
18211801 }
1822−
1823− /*
1824− * We deflated successfully, so regardless to the inflation success, we
1825− * need to reduce the number of isolated_pages.
1826− */
1827− b->b_dev_info.isolated_pages--;
1828− spin_unlock_irqrestore(&b->b_dev_info.pages_lock, flags);
1829−
18301802 out_unlock:
18311803 up_read(&b->conf_sem);
18321804 return ret;
18331805 }
1834−
1835−/**
1836− * vmballoon_compaction_init() - initialized compaction for the balloon.
1837− *
1838− * @b: pointer to the balloon.
1839− *
1840− * If during the initialization a failure occurred, this function does not
1841− * perform cleanup. The caller must call vmballoon_compaction_deinit() in this
1842− * case.
1843− *
1844− * Return: zero on success or error code on failure.
1845− */
1846−static __init void vmballoon_compaction_init(struct vmballoon *b)
1847−{
1848− b->b_dev_info.migratepage = vmballoon_migratepage;
1849−}
1850−
1851−#else /* CONFIG_BALLOON_COMPACTION */
1852−static inline void vmballoon_compaction_init(struct vmballoon *b)
1853−{
1854−}
1855−#endif /* CONFIG_BALLOON_COMPACTION */
1806+#else /* CONFIG_BALLOON_MIGRATION */
1807+int vmballoon_migratepage(struct balloon_dev_info *b_dev_info,
1808+ struct page *newpage, struct page *page,
1809+ enum migrate_mode mode);
1810+#endif /* CONFIG_BALLOON_MIGRATION */
18561811
18571812 static int __init vmballoon_init(void)
18581813 {
@@ -1871,14 +1826,12 @@ static int __init vmballoon_init(void) · 12 unmodified lines
18711826 if (error)
18721827 return error;
18731828
1874− /*
1875− * Initialization of compaction must be done after the call to
1876− * balloon_devinfo_init() .
1877− */
18781829 balloon_devinfo_init(&balloon.b_dev_info);
1879− vmballoon_compaction_init(&balloon);
1830+ if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))
1831+ balloon.b_dev_info.migratepage = vmballoon_migratepage;
18801832
18811833 INIT_LIST_HEAD(&balloon.huge_pages);
1834+ spin_lock_init(&balloon.huge_pages_lock);
18821835 spin_lock_init(&balloon.comm_lock);
18831836 init_rwsem(&balloon.conf_sem);
18841837 balloon.vmci_doorbell = VMCI_INVALID_HANDLE;
drivers/virtio/Kconfig+1−1
@@ -112,7 +112,7 @@ config VIRTIO_PMEM · 111 unmodified lines
112112 config VIRTIO_BALLOON
113113 tristate "Virtio balloon driver"
114114 depends on VIRTIO
115− select MEMORY_BALLOON
115+ select BALLOON
116116 select PAGE_REPORTING
117117 help
118118 This driver supports increasing and decreasing the amount
drivers/virtio/virtio_balloon.c+21−43
@@ -13,7 +13,7 @@ · 12 unmodified lines
1313 #include <linux/delay.h>
1414 #include <linux/slab.h>
1515 #include <linux/module.h>
16−#include <linux/balloon_compaction.h>
16+#include <linux/balloon.h>
1717 #include <linux/oom.h>
1818 #include <linux/wait.h>
1919 #include <linux/mm.h>
@@ -242,8 +242,8 @@ static void set_page_pfns(struct virtio_balloon *vb, · 222 unmodified lines
242242 static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num)
243243 {
244244 unsigned int num_allocated_pages;
245+ struct page *page, *next;
245246 unsigned int num_pfns;
246− struct page *page;
247247 LIST_HEAD(pages);
248248
249249 /* We can only do one array worth at a time. */
@@ -262,21 +262,19 @@ static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) · 12 unmodified lines
262262 break;
263263 }
264264
265− balloon_page_push(&pages, page);
265+ list_add(&page->lru, &pages);
266266 }
267267
268268 mutex_lock(&vb->balloon_lock);
269269
270270 vb->num_pfns = 0;
271271
272− while ((page = balloon_page_pop(&pages))) {
272+ list_for_each_entry_safe(page, next, &pages, lru) {
273+ list_del(&page->lru);
273274 balloon_page_enqueue(&vb->vb_dev_info, page);
274275
275276 set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
276277 vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
277− if (!virtio_has_feature(vb->vdev,
278− VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
279− adjust_managed_page_count(page, -1);
280278 vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
281279 }
282280
@@ -295,9 +293,6 @@ static void release_pages_balloon(struct virtio_balloon *vb, · 12 unmodified lines
295293 struct page *page, *next;
296294
297295 list_for_each_entry_safe(page, next, pages, lru) {
298− if (!virtio_has_feature(vb->vdev,
299− VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
300− adjust_managed_page_count(page, 1);
301296 list_del(&page->lru);
302297 put_page(page); /* balloon reference */
303298 }
@@ -480,15 +475,19 @@ static inline s64 towards_target(struct virtio_balloon *vb) · 176 unmodified lines
480475 static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
481476 unsigned long num_to_return)
482477 {
483− struct page *page;
484− unsigned long num_returned;
478+ unsigned long num_returned = 0;
479+ struct page *page, *next;
480+
481+ if (unlikely(!num_to_return))
482+ return 0;
485483
486484 spin_lock_irq(&vb->free_page_list_lock);
487− for (num_returned = 0; num_returned < num_to_return; num_returned++) {
488− page = balloon_page_pop(&vb->free_page_list);
489− if (!page)
490− break;
485+
486+ list_for_each_entry_safe(page, next, &vb->free_page_list, lru) {
487+ list_del(&page->lru);
491488 __free_pages(page, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
489+ if (++num_returned == num_to_return)
490+ break;
492491 }
493492 vb->num_free_page_blocks -= num_returned;
494493 spin_unlock_irq(&vb->free_page_list_lock);
@@ -723,7 +722,7 @@ static int get_free_page_and_send(struct virtio_balloon *vb) · 228 unmodified lines
723722 }
724723 virtqueue_kick(vq);
725724 spin_lock_irq(&vb->free_page_list_lock);
726− balloon_page_push(&vb->free_page_list, page);
725+ list_add(&page->lru, &vb->free_page_list);
727726 vb->num_free_page_blocks++;
728727 spin_unlock_irq(&vb->free_page_list_lock);
729728 } else {
@@ -803,7 +802,7 @@ static void report_free_page_func(struct work_struct *work) · 73 unmodified lines
803802 }
804803 }
805804
806−#ifdef CONFIG_BALLOON_COMPACTION
805+#ifdef CONFIG_BALLOON_MIGRATION
807806 /*
808807 * virtballoon_migratepage - perform the balloon page migration on behalf of
809808 * a compaction thread. (called under page lock)
@@ -827,7 +826,6 @@ static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info, · 17 unmodified lines
827826 {
828827 struct virtio_balloon *vb = container_of(vb_dev_info,
829828 struct virtio_balloon, vb_dev_info);
830− unsigned long flags;
831829
832830 /*
833831 * In order to avoid lock contention while migrating pages concurrently
@@ -840,25 +838,7 @@ static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info, · 6 unmodified lines
840838 if (!mutex_trylock(&vb->balloon_lock))
841839 return -EAGAIN;
842840
843− get_page(newpage); /* balloon reference */
844−
845− /*
846− * When we migrate a page to a different zone and adjusted the
847− * managed page count when inflating, we have to fixup the count of
848− * both involved zones.
849− */
850− if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM) &&
851− page_zone(page) != page_zone(newpage)) {
852− adjust_managed_page_count(page, 1);
853− adjust_managed_page_count(newpage, -1);
854− }
855−
856841 /* balloon's page migration 1st step -- inflate "newpage" */
857− spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
858− balloon_page_insert(vb_dev_info, newpage);
859− vb_dev_info->isolated_pages--;
860− __count_vm_event(BALLOON_MIGRATE);
861− spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
862842 vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
863843 set_page_pfns(vb, vb->pfns, newpage);
864844 tell_host(vb, vb->inflate_vq);
@@ -869,13 +849,9 @@ static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info, · 4 unmodified lines
869849 tell_host(vb, vb->deflate_vq);
870850
871851 mutex_unlock(&vb->balloon_lock);
872−
873− balloon_page_finalize(page);
874− put_page(page); /* balloon reference */
875−
876852 return 0;
877853 }
878−#endif /* CONFIG_BALLOON_COMPACTION */
854+#endif /* CONFIG_BALLOON_MIGRATION */
879855
880856 static unsigned long shrink_free_pages(struct virtio_balloon *vb,
881857 unsigned long pages_to_free)
@@ -970,7 +946,9 @@ static int virtballoon_probe(struct virtio_device *vdev) · 88 unmodified lines
970946 if (err)
971947 goto out_free_vb;
972948
973−#ifdef CONFIG_BALLOON_COMPACTION
949+ if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
950+ vb->vb_dev_info.adjust_managed_page_count = true;
951+#ifdef CONFIG_BALLOON_MIGRATION
974952 vb->vb_dev_info.migratepage = virtballoon_migratepage;
975953 #endif
976954 if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
fs/ext4/inode.c+1−2
@@ -3298,8 +3298,7 @@ int ext4_alloc_da_blocks(struct inode *inode) · 3297 unmodified lines
32983298 /*
32993299 * We do something simple for now. The filemap_flush() will
33003300 * also start triggering a write of the data blocks, which is
3301− * not strictly speaking necessary (and for users of
3302− * laptop_mode, not even desirable). However, to do otherwise
3301+ * not strictly speaking necessary. However, to do otherwise
33033302 * would require replicating code paths in:
33043303 *
33053304 * ext4_writepages() ->
fs/proc/task_mmu.c+2−2
@@ -2751,7 +2751,7 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long start, · 2750 unmodified lines
27512751 return 0;
27522752 }
27532753
2754− arch_enter_lazy_mmu_mode();
2754+ lazy_mmu_mode_enable();
27552755
27562756 if ((p->arg.flags & PM_SCAN_WP_MATCHING) && !p->vec_out) {
27572757 /* Fast path for performing exclusive WP */
@@ -2821,7 +2821,7 @@ flush_and_return: · 63 unmodified lines
28212821 if (flush_end)
28222822 flush_tlb_range(vma, start, addr);
28232823
2824− arch_leave_lazy_mmu_mode();
2824+ lazy_mmu_mode_disable();
28252825 pte_unmap_unlock(start_pte, ptl);
28262826
28272827 cond_resched();
fs/sync.c+0−2
@@ -104,8 +104,6 @@ void ksys_sync(void) · 103 unmodified lines
104104 iterate_supers(sync_fs_one_sb, &wait);
105105 sync_bdevs(false);
106106 sync_bdevs(true);
107− if (unlikely(laptop_mode))
108− laptop_sync_completion();
109107 }
110108
111109 SYSCALL_DEFINE0(sync)
fs/xfs/xfs_super.c+0−9
@@ -825,15 +825,6 @@ xfs_fs_sync_fs( · 824 unmodified lines
825825 if (error)
826826 return error;
827827
828− if (laptop_mode) {
829− /*
830− * The disk must be active because we're syncing.
831− * We schedule log work now (now that the disk is
832− * active) instead of later (when it might not be).
833− */
834− flush_delayed_work(&mp->m_log->l_work);
835− }
836−
837828 /*
838829 * If we are called with page faults frozen out, it means we are about
839830 * to freeze the transaction subsystem. Take the opportunity to shut
include/asm-generic/tlb.h+3−4
@@ -213,7 +213,7 @@ struct mmu_table_batch { · 212 unmodified lines
213213 #define MAX_TABLE_BATCH \
214214 ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
215215
216−#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE
216+#ifndef CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE
217217 static inline void __tlb_remove_table(void *table)
218218 {
219219 struct ptdesc *ptdesc = (struct ptdesc *)table;
@@ -287,8 +287,7 @@ struct mmu_gather_batch { · 67 unmodified lines
287287 */
288288 #define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH)
289289
290−extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
291− bool delay_rmap, int page_size);
290+extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size);
292291 bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page,
293292 unsigned int nr_pages, bool delay_rmap);
294293
@@ -510,7 +509,7 @@ static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) · 215 unmodified lines
510509 static inline void tlb_remove_page_size(struct mmu_gather *tlb,
511510 struct page *page, int page_size)
512511 {
513− if (__tlb_remove_page_size(tlb, page, false, page_size))
512+ if (__tlb_remove_page_size(tlb, page, page_size))
514513 tlb_flush_mmu(tlb);
515514 }
516515
include/linux/backing-dev-defs.h+0−3
@@ -46,7 +46,6 @@ enum wb_reason { · 45 unmodified lines
4646 WB_REASON_VMSCAN,
4747 WB_REASON_SYNC,
4848 WB_REASON_PERIODIC,
49− WB_REASON_LAPTOP_TIMER,
5049 WB_REASON_FS_FREE_SPACE,
5150 /*
5251 * There is no bdi forker thread any more and works are done
@@ -204,8 +203,6 @@ struct backing_dev_info { · 151 unmodified lines
204203 char dev_name[64];
205204 struct device *owner;
206205
207− struct timer_list laptop_mode_wb_timer;
208−
209206 #ifdef CONFIG_DEBUG_FS
210207 struct dentry *debug_dir;
211208 #endif
include/linux/balloon.h+77−0
@@ -0,0 +1,77 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Common interface for implementing a memory balloon, including support
4+ * for migration of pages inflated in a memory balloon.
5+ *
6+ * Balloon page migration makes use of the general "movable_ops page migration"
7+ * feature.
8+ *
9+ * page->private is used to reference the responsible balloon device.
10+ * That these pages have movable_ops, and which movable_ops apply,
11+ * is derived from the page type (PageOffline()) combined with the
12+ * PG_movable_ops flag (PageMovableOps()).
13+ *
14+ * Once the page type and the PG_movable_ops are set, migration code
15+ * can initiate page isolation by invoking the
16+ * movable_operations()->isolate_page() callback
17+ *
18+ * As long as page->private is set, the page is either on the balloon list
19+ * or isolated for migration. If page->private is not set, the page is
20+ * either still getting inflated, or was deflated to be freed by the balloon
21+ * driver soon. Isolation is impossible in both cases.
22+ *
23+ * As the page isolation scanning step a compaction thread does is a lockless
24+ * procedure (from a page standpoint), it might bring some racy situations while
25+ * performing balloon page migration. In order to sort out these racy scenarios
26+ * and safely perform balloon's page migration we must, always, ensure following
27+ * these simple rules:
28+ *
29+ * i. Inflation/deflation must set/clear page->private under the
30+ * balloon_pages_lock
31+ *
32+ * ii. isolation or dequeueing procedure must remove the page from balloon
33+ * device page list under balloon_pages_lock
34+ *
35+ * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
36+ */
37+#ifndef _LINUX_BALLOON_H
38+#define _LINUX_BALLOON_H
39+#include <linux/pagemap.h>
40+#include <linux/page-flags.h>
41+#include <linux/migrate.h>
42+#include <linux/gfp.h>
43+#include <linux/err.h>
44+#include <linux/list.h>
45+
46+/*
47+ * Balloon device information descriptor.
48+ * This struct is used to allow the common balloon page migration interface
49+ * procedures to find the proper balloon device holding memory pages they'll
50+ * have to cope for page migration, as well as it serves the balloon driver as
51+ * a page book-keeper for its registered balloon devices.
52+ */
53+struct balloon_dev_info {
54+ unsigned long isolated_pages; /* # of isolated pages for migration */
55+ struct list_head pages; /* Pages enqueued & handled to Host */
56+ int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
57+ struct page *page, enum migrate_mode mode);
58+ bool adjust_managed_page_count;
59+};
60+
61+struct page *balloon_page_alloc(void);
62+void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
63+ struct page *page);
64+struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
65+size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
66+ struct list_head *pages);
67+size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
68+ struct list_head *pages, size_t n_req_pages);
69+
70+static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
71+{
72+ balloon->isolated_pages = 0;
73+ INIT_LIST_HEAD(&balloon->pages);
74+ balloon->migratepage = NULL;
75+ balloon->adjust_managed_page_count = false;
76+}
77+#endif /* _LINUX_BALLOON_H */
include/linux/balloon_compaction.h+0−160
@@ -1,160 +0,0 @@
1−/* SPDX-License-Identifier: GPL-2.0 */
2−/*
3− * include/linux/balloon_compaction.h
4− *
5− * Common interface definitions for making balloon pages movable by compaction.
6− *
7− * Balloon page migration makes use of the general "movable_ops page migration"
8− * feature.
9− *
10− * page->private is used to reference the responsible balloon device.
11− * That these pages have movable_ops, and which movable_ops apply,
12− * is derived from the page type (PageOffline()) combined with the
13− * PG_movable_ops flag (PageMovableOps()).
14− *
15− * As the page isolation scanning step a compaction thread does is a lockless
16− * procedure (from a page standpoint), it might bring some racy situations while
17− * performing balloon page compaction. In order to sort out these racy scenarios
18− * and safely perform balloon's page compaction and migration we must, always,
19− * ensure following these simple rules:
20− *
21− * i. Setting the PG_movable_ops flag and page->private with the following
22− * lock order
23− * +-page_lock(page);
24− * +--spin_lock_irq(&b_dev_info->pages_lock);
25− *
26− * ii. isolation or dequeueing procedure must remove the page from balloon
27− * device page list under b_dev_info->pages_lock.
28− *
29− * The functions provided by this interface are placed to help on coping with
30− * the aforementioned balloon page corner case, as well as to ensure the simple
31− * set of exposed rules are satisfied while we are dealing with balloon pages
32− * compaction / migration.
33− *
34− * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
35− */
36−#ifndef _LINUX_BALLOON_COMPACTION_H
37−#define _LINUX_BALLOON_COMPACTION_H
38−#include <linux/pagemap.h>
39−#include <linux/page-flags.h>
40−#include <linux/migrate.h>
41−#include <linux/gfp.h>
42−#include <linux/err.h>
43−#include <linux/fs.h>
44−#include <linux/list.h>
45−
46−/*
47− * Balloon device information descriptor.
48− * This struct is used to allow the common balloon compaction interface
49− * procedures to find the proper balloon device holding memory pages they'll
50− * have to cope for page compaction / migration, as well as it serves the
51− * balloon driver as a page book-keeper for its registered balloon devices.
52− */
53−struct balloon_dev_info {
54− unsigned long isolated_pages; /* # of isolated pages for migration */
55− spinlock_t pages_lock; /* Protection to pages list */
56− struct list_head pages; /* Pages enqueued & handled to Host */
57− int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
58− struct page *page, enum migrate_mode mode);
59−};
60−
61−extern struct page *balloon_page_alloc(void);
62−extern void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
63− struct page *page);
64−extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
65−extern size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
66− struct list_head *pages);
67−extern size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
68− struct list_head *pages, size_t n_req_pages);
69−
70−static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
71−{
72− balloon->isolated_pages = 0;
73− spin_lock_init(&balloon->pages_lock);
74− INIT_LIST_HEAD(&balloon->pages);
75− balloon->migratepage = NULL;
76−}
77−
78−#ifdef CONFIG_BALLOON_COMPACTION
79−extern const struct movable_operations balloon_mops;
80−/*
81− * balloon_page_device - get the b_dev_info descriptor for the balloon device
82− * that enqueues the given page.
83− */
84−static inline struct balloon_dev_info *balloon_page_device(struct page *page)
85−{
86− return (struct balloon_dev_info *)page_private(page);
87−}
88−#endif /* CONFIG_BALLOON_COMPACTION */
89−
90−/*
91− * balloon_page_insert - insert a page into the balloon's page list and make
92− * the page->private assignment accordingly.
93− * @balloon : pointer to balloon device
94− * @page : page to be assigned as a 'balloon page'
95− *
96− * Caller must ensure the page is locked and the spin_lock protecting balloon
97− * pages list is held before inserting a page into the balloon device.
98− */
99−static inline void balloon_page_insert(struct balloon_dev_info *balloon,
100− struct page *page)
101−{
102− __SetPageOffline(page);
103− if (IS_ENABLED(CONFIG_BALLOON_COMPACTION)) {
104− SetPageMovableOps(page);
105− set_page_private(page, (unsigned long)balloon);
106− }
107− list_add(&page->lru, &balloon->pages);
108−}
109−
110−static inline gfp_t balloon_mapping_gfp_mask(void)
111−{
112− if (IS_ENABLED(CONFIG_BALLOON_COMPACTION))
113− return GFP_HIGHUSER_MOVABLE;
114− return GFP_HIGHUSER;
115−}
116−
117−/*
118− * balloon_page_finalize - prepare a balloon page that was removed from the
119− * balloon list for release to the page allocator
120− * @page: page to be released to the page allocator
121− *
122− * Caller must ensure that the page is locked.
123− */
124−static inline void balloon_page_finalize(struct page *page)
125−{
126− if (IS_ENABLED(CONFIG_BALLOON_COMPACTION))
127− set_page_private(page, 0);
128− /* PageOffline is sticky until the page is freed to the buddy. */
129−}
130−
131−/*
132− * balloon_page_push - insert a page into a page list.
133− * @head : pointer to list
134− * @page : page to be added
135− *
136− * Caller must ensure the page is private and protect the list.
137− */
138−static inline void balloon_page_push(struct list_head *pages, struct page *page)
139−{
140− list_add(&page->lru, pages);
141−}
142−
143−/*
144− * balloon_page_pop - remove a page from a page list.
145− * @head : pointer to list
146− * @page : page to be added
147− *
148− * Caller must ensure the page is private and protect the list.
149− */
150−static inline struct page *balloon_page_pop(struct list_head *pages)
151−{
152− struct page *page = list_first_entry_or_null(pages, struct page, lru);
153−
154− if (!page)
155− return NULL;
156−
157− list_del(&page->lru);
158− return page;
159−}
160−#endif /* _LINUX_BALLOON_COMPACTION_H */
include/linux/cma.h+6−21
@@ -49,9 +49,14 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, · 48 unmodified lines
4949 struct cma **res_cma);
5050 extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,
5151 bool no_warn);
52−extern bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count);
5352 extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long count);
5453
54+struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
55+ unsigned int align, bool no_warn);
56+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);
57+bool cma_release_frozen(struct cma *cma, const struct page *pages,
58+ unsigned long count);
59+
5560 extern int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data);
5661 extern bool cma_intersects(struct cma *cma, unsigned long start, unsigned long end);
5762
@@ -66,24 +71,4 @@ static inline bool cma_skip_dt_default_reserved_mem(void) · 8 unmodified lines
6671 }
6772 #endif
6873
69−#ifdef CONFIG_CMA
70−struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp);
71−bool cma_free_folio(struct cma *cma, const struct folio *folio);
72−bool cma_validate_zones(struct cma *cma);
73−#else
74−static inline struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp)
75−{
76− return NULL;
77−}
78−
79−static inline bool cma_free_folio(struct cma *cma, const struct folio *folio)
80−{
81− return false;
82−}
83−static inline bool cma_validate_zones(struct cma *cma)
84−{
85− return false;
86−}
87−#endif
88−
8974 #endif
include/linux/damon.h+36−29
@@ -15,7 +15,7 @@ · 14 unmodified lines
1515 #include <linux/random.h>
1616
1717 /* Minimal region size. Every damon_region is aligned by this. */
18−#define DAMON_MIN_REGION PAGE_SIZE
18+#define DAMON_MIN_REGION_SZ PAGE_SIZE
1919 /* Max priority score for DAMON-based operation schemes */
2020 #define DAMOS_MAX_SCORE (99)
2121
@@ -155,6 +155,8 @@ enum damos_action { · 133 unmodified lines
155155 * @DAMOS_QUOTA_NODE_MEM_FREE_BP: MemFree ratio of a node.
156156 * @DAMOS_QUOTA_NODE_MEMCG_USED_BP: MemUsed ratio of a node for a cgroup.
157157 * @DAMOS_QUOTA_NODE_MEMCG_FREE_BP: MemFree ratio of a node for a cgroup.
158+ * @DAMOS_QUOTA_ACTIVE_MEM_BP: Active to total LRU memory ratio.
159+ * @DAMOS_QUOTA_INACTIVE_MEM_BP: Inactive to total LRU memory ratio.
158160 * @NR_DAMOS_QUOTA_GOAL_METRICS: Number of DAMOS quota goal metrics.
159161 *
160162 * Metrics equal to larger than @NR_DAMOS_QUOTA_GOAL_METRICS are unsupported.
@@ -166,6 +168,8 @@ enum damos_quota_goal_metric { · 5 unmodified lines
166168 DAMOS_QUOTA_NODE_MEM_FREE_BP,
167169 DAMOS_QUOTA_NODE_MEMCG_USED_BP,
168170 DAMOS_QUOTA_NODE_MEMCG_FREE_BP,
171+ DAMOS_QUOTA_ACTIVE_MEM_BP,
172+ DAMOS_QUOTA_INACTIVE_MEM_BP,
169173 NR_DAMOS_QUOTA_GOAL_METRICS,
170174 };
171175
@@ -203,7 +207,7 @@ struct damos_quota_goal { · 31 unmodified lines
203207 u64 last_psi_total;
204208 struct {
205209 int nid;
206− unsigned short memcg_id;
210+ u64 memcg_id;
207211 };
208212 };
209213 struct list_head list;
@@ -330,6 +334,8 @@ struct damos_watermarks { · 120 unmodified lines
330334 * @sz_ops_filter_passed:
331335 * Total bytes that passed ops layer-handled DAMOS filters.
332336 * @qt_exceeds: Total number of times the quota of the scheme has exceeded.
337+ * @nr_snapshots:
338+ * Total number of DAMON snapshots that the scheme has tried.
333339 *
334340 * "Tried an action to a region" in this context means the DAMOS core logic
335341 * determined the region as eligible to apply the action. The access pattern
@@ -355,6 +361,7 @@ struct damos_stat { · 19 unmodified lines
355361 unsigned long sz_applied;
356362 unsigned long sz_ops_filter_passed;
357363 unsigned long qt_exceeds;
364+ unsigned long nr_snapshots;
358365 };
359366
360367 /**
@@ -416,7 +423,7 @@ struct damos_filter { · 55 unmodified lines
416423 bool matching;
417424 bool allow;
418425 union {
419− unsigned short memcg_id;
426+ u64 memcg_id;
420427 struct damon_addr_range addr_range;
421428 int target_idx;
422429 struct damon_size_range sz_range;
@@ -496,6 +503,7 @@ struct damos_migrate_dests { · 73 unmodified lines
496503 * @ops_filters: ops layer handling &struct damos_filter objects list.
497504 * @last_applied: Last @action applied ops-managing entity.
498505 * @stat: Statistics of this scheme.
506+ * @max_nr_snapshots: Upper limit of nr_snapshots stat.
499507 * @list: List head for siblings.
500508 *
501509 * For each @apply_interval_us, DAMON finds regions which fit in the
@@ -529,9 +537,10 @@ struct damos_migrate_dests { · 27 unmodified lines
529537 * unsets @last_applied when each regions walking for applying the scheme is
530538 * finished.
531539 *
532− * After applying the &action to each region, &stat_count and &stat_sz is
533− * updated to reflect the number of regions and total size of regions that the
534− * &action is applied.
540+ * After applying the &action to each region, &stat is updated.
541+ *
542+ * If &max_nr_snapshots is set as non-zero and &stat.nr_snapshots be same to or
543+ * greater than it, the scheme is deactivated.
535544 */
536545 struct damos {
537546 struct damos_access_pattern pattern;
@@ -566,6 +575,7 @@ struct damos { · 28 unmodified lines
566575 struct list_head ops_filters;
567576 void *last_applied;
568577 struct damos_stat stat;
578+ unsigned long max_nr_snapshots;
569579 struct list_head list;
570580 };
571581
@@ -597,7 +607,6 @@ enum damon_ops_id { · 25 unmodified lines
597607 * @apply_scheme: Apply a DAMON-based operation scheme.
598608 * @target_valid: Determine if the target is valid.
599609 * @cleanup_target: Clean up each target before deallocation.
600− * @cleanup: Clean up the context.
601610 *
602611 * DAMON can be extended for various address spaces and usages. For this,
603612 * users should register the low level operations for their target address
@@ -630,7 +639,6 @@ enum damon_ops_id { · 26 unmodified lines
630639 * @target_valid should check whether the target is still valid for the
631640 * monitoring.
632641 * @cleanup_target is called before the target will be deallocated.
633− * @cleanup is called from @kdamond just before its termination.
634642 */
635643 struct damon_operations {
636644 enum damon_ops_id id;
@@ -646,7 +654,6 @@ struct damon_operations { · 9 unmodified lines
646654 struct damos *scheme, unsigned long *sz_filter_passed);
647655 bool (*target_valid)(struct damon_target *t);
648656 void (*cleanup_target)(struct damon_target *t);
649− void (*cleanup)(struct damon_ctx *context);
650657 };
651658
652659 /*
@@ -656,7 +663,7 @@ struct damon_operations { · 3 unmodified lines
656663 * @data: Data that will be passed to @fn.
657664 * @repeat: Repeat invocations.
658665 * @return_code: Return code from @fn invocation.
659− * @dealloc_on_cancel: De-allocate when canceled.
666+ * @dealloc_on_cancel: If @repeat is true, de-allocate when canceled.
660667 *
661668 * Control damon_call(), which requests specific kdamond to invoke a given
662669 * function. Refer to damon_call() for more details.
@@ -749,27 +756,24 @@ struct damon_attrs { · 86 unmodified lines
749756 * of the monitoring.
750757 *
751758 * @attrs: Monitoring attributes for accuracy/overhead control.
752− * @kdamond: Kernel thread who does the monitoring.
753− * @kdamond_lock: Mutex for the synchronizations with @kdamond.
754759 *
755− * For each monitoring context, one kernel thread for the monitoring is
756− * created. The pointer to the thread is stored in @kdamond.
760+ * For each monitoring context, one kernel thread for the monitoring, namely
761+ * kdamond, is created. The pid of kdamond can be retrieved using
762+ * damon_kdamond_pid().
757763 *
758− * Once started, the monitoring thread runs until explicitly required to be
759− * terminated or every monitoring target is invalid. The validity of the
760− * targets is checked via the &damon_operations.target_valid of @ops. The
761− * termination can also be explicitly requested by calling damon_stop().
762− * The thread sets @kdamond to NULL when it terminates. Therefore, users can
763− * know whether the monitoring is ongoing or terminated by reading @kdamond.
764− * Reads and writes to @kdamond from outside of the monitoring thread must
765− * be protected by @kdamond_lock.
764+ * Once started, kdamond runs until explicitly required to be terminated or
765+ * every monitoring target is invalid. The validity of the targets is checked
766+ * via the &damon_operations.target_valid of @ops. The termination can also be
767+ * explicitly requested by calling damon_stop(). To know if a kdamond is
768+ * running, damon_is_running() can be used.
766769 *
767− * Note that the monitoring thread protects only @kdamond via @kdamond_lock.
768− * Accesses to other fields must be protected by themselves.
770+ * While the kdamond is running, all accesses to &struct damon_ctx from a
771+ * thread other than the kdamond should be made using safe DAMON APIs,
772+ * including damon_call() and damos_walk().
769773 *
770774 * @ops: Set of monitoring operations for given use cases.
771775 * @addr_unit: Scale factor for core to ops address conversion.
772− * @min_sz_region: Minimum region size.
776+ * @min_region_sz: Minimum region size.
773777 * @adaptive_targets: Head of monitoring targets (&damon_target) list.
774778 * @schemes: Head of schemes (&damos) list.
775779 */
@@ -806,13 +810,15 @@ struct damon_ctx { · 30 unmodified lines
806810 struct damos_walk_control *walk_control;
807811 struct mutex walk_control_lock;
808812
809−/* public: */
813+ /* Working thread of the given DAMON context */
810814 struct task_struct *kdamond;
815+ /* Protects @kdamond field access */
811816 struct mutex kdamond_lock;
812817
818+/* public: */
813819 struct damon_operations ops;
814820 unsigned long addr_unit;
815− unsigned long min_sz_region;
821+ unsigned long min_region_sz;
816822
817823 struct list_head adaptive_targets;
818824 struct list_head schemes;
@@ -901,7 +907,7 @@ static inline void damon_insert_region(struct damon_region *r, · 82 unmodified lines
901907 void damon_add_region(struct damon_region *r, struct damon_target *t);
902908 void damon_destroy_region(struct damon_region *r, struct damon_target *t);
903909 int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
904− unsigned int nr_ranges, unsigned long min_sz_region);
910+ unsigned int nr_ranges, unsigned long min_region_sz);
905911 void damon_update_region_access_rate(struct damon_region *r, bool accessed,
906912 struct damon_attrs *attrs);
907913
@@ -962,13 +968,14 @@ bool damon_initialized(void); · 54 unmodified lines
962968 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
963969 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
964970 bool damon_is_running(struct damon_ctx *ctx);
971+int damon_kdamond_pid(struct damon_ctx *ctx);
965972
966973 int damon_call(struct damon_ctx *ctx, struct damon_call_control *control);
967974 int damos_walk(struct damon_ctx *ctx, struct damos_walk_control *control);
968975
969976 int damon_set_region_biggest_system_ram_default(struct damon_target *t,
970977 unsigned long *start, unsigned long *end,
971− unsigned long min_sz_region);
978+ unsigned long min_region_sz);
972979
973980 #endif /* CONFIG_DAMON */
974981
include/linux/gfp.h+28−32
@@ -407,9 +407,15 @@ extern gfp_t gfp_allowed_mask; · 406 unmodified lines
407407 /* Returns true if the gfp_mask allows use of ALLOC_NO_WATERMARK */
408408 bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
409409
410+/* A helper for checking if gfp includes all the specified flags */
411+static inline bool gfp_has_flags(gfp_t gfp, gfp_t flags)
412+{
413+ return (gfp & flags) == flags;
414+}
415+
410416 static inline bool gfp_has_io_fs(gfp_t gfp)
411417 {
412− return (gfp & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS);
418+ return gfp_has_flags(gfp, __GFP_IO | __GFP_FS);
413419 }
414420
415421 /*
@@ -430,39 +436,29 @@ typedef unsigned int __bitwise acr_flags_t; · 14 unmodified lines
430436 #define ACR_FLAGS_CMA ((__force acr_flags_t)BIT(0)) // allocate for CMA
431437
432438 /* The below functions must be run on a range from a single zone. */
433−extern int alloc_contig_range_noprof(unsigned long start, unsigned long end,
434− acr_flags_t alloc_flags, gfp_t gfp_mask);
435−#define alloc_contig_range(...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__))
436−
437−extern struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
438− int nid, nodemask_t *nodemask);
439−#define alloc_contig_pages(...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__))
440−
441−#endif
439+int alloc_contig_frozen_range_noprof(unsigned long start, unsigned long end,
440+ acr_flags_t alloc_flags, gfp_t gfp_mask);
441+#define alloc_contig_frozen_range(...) \
442+ alloc_hooks(alloc_contig_frozen_range_noprof(__VA_ARGS__))
443+
444+int alloc_contig_range_noprof(unsigned long start, unsigned long end,
445+ acr_flags_t alloc_flags, gfp_t gfp_mask);
446+#define alloc_contig_range(...) \
447+ alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__))
448+
449+struct page *alloc_contig_frozen_pages_noprof(unsigned long nr_pages,
450+ gfp_t gfp_mask, int nid, nodemask_t *nodemask);
451+#define alloc_contig_frozen_pages(...) \
452+ alloc_hooks(alloc_contig_frozen_pages_noprof(__VA_ARGS__))
453+
454+struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
455+ int nid, nodemask_t *nodemask);
456+#define alloc_contig_pages(...) \
457+ alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__))
458+
459+void free_contig_frozen_range(unsigned long pfn, unsigned long nr_pages);
442460 void free_contig_range(unsigned long pfn, unsigned long nr_pages);
443−
444−#ifdef CONFIG_CONTIG_ALLOC
445−static inline struct folio *folio_alloc_gigantic_noprof(int order, gfp_t gfp,
446− int nid, nodemask_t *node)
447−{
448− struct page *page;
449−
450− if (WARN_ON(!order || !(gfp & __GFP_COMP)))
451− return NULL;
452−
453− page = alloc_contig_pages_noprof(1 << order, gfp, nid, node);
454−
455− return page ? page_folio(page) : NULL;
456−}
457−#else
458−static inline struct folio *folio_alloc_gigantic_noprof(int order, gfp_t gfp,
459− int nid, nodemask_t *node)
460−{
461− return NULL;
462−}
463461 #endif
464−/* This should be paired with folio_put() rather than free_contig_range(). */
465−#define folio_alloc_gigantic(...) alloc_hooks(folio_alloc_gigantic_noprof(__VA_ARGS__))
466462
467463 DEFINE_FREE(free_page, void *, free_page((unsigned long)_T))
468464
include/linux/gfp_types.h+5−2
@@ -309,8 +309,10 @@ enum { · 308 unmodified lines
309309 *
310310 * %GFP_ATOMIC users can not sleep and need the allocation to succeed. A lower
311311 * watermark is applied to allow access to "atomic reserves".
312− * The current implementation doesn't support NMI and few other strict
313− * non-preemptive contexts (e.g. raw_spin_lock). The same applies to %GFP_NOWAIT.
312+ * The current implementation doesn't support NMI, nor contexts that disable
313+ * preemption under PREEMPT_RT. This includes raw_spin_lock() and plain
314+ * preempt_disable() - see "Memory allocation" in
315+ * Documentation/core-api/real-time/differences.rst for more info.
314316 *
315317 * %GFP_KERNEL is typical for kernel-internal allocations. The caller requires
316318 * %ZONE_NORMAL or a lower zone for direct access but can direct reclaim.
@@ -321,6 +323,7 @@ enum { · 4 unmodified lines
321323 * %GFP_NOWAIT is for kernel allocations that should not stall for direct
322324 * reclaim, start physical IO or use any filesystem callback. It is very
323325 * likely to fail to allocate memory, even for very small allocations.
326+ * The same restrictions on calling contexts apply as for %GFP_ATOMIC.
324327 *
325328 * %GFP_NOIO will use direct reclaim to discard clean pages or slab pages
326329 * that do not require the starting of any physical IO.
include/linux/highmem.h+97−1
@@ -197,15 +197,111 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size) · 196 unmodified lines
197197 }
198198 #endif
199199
200−/* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
201200 #ifndef clear_user_highpage
201+#ifndef clear_user_page
202+/**
203+ * clear_user_page() - clear a page to be mapped to user space
204+ * @addr: the address of the page
205+ * @vaddr: the address of the user mapping
206+ * @page: the page
207+ *
208+ * We condition the definition of clear_user_page() on the architecture
209+ * not having a custom clear_user_highpage(). That's because if there
210+ * is some special flushing needed for clear_user_highpage() then it
211+ * is likely that clear_user_page() also needs some magic. And, since
212+ * our only caller is the generic clear_user_highpage(), not defining
213+ * is not much of a loss.
214+ */
215+static inline void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
216+{
217+ clear_page(addr);
218+}
219+#endif
220+
221+/**
222+ * clear_user_pages() - clear a page range to be mapped to user space
223+ * @addr: start address
224+ * @vaddr: start address of the user mapping
225+ * @page: start page
226+ * @npages: number of pages
227+ *
228+ * Assumes that the region (@addr, +@npages) has been validated
229+ * already so this does no exception handling.
230+ *
231+ * If the architecture provides a clear_user_page(), use that;
232+ * otherwise, we can safely use clear_pages().
233+ */
234+static inline void clear_user_pages(void *addr, unsigned long vaddr,
235+ struct page *page, unsigned int npages)
236+{
237+
238+#ifdef clear_user_page
239+ do {
240+ clear_user_page(addr, vaddr, page);
241+ addr += PAGE_SIZE;
242+ vaddr += PAGE_SIZE;
243+ page++;
244+ } while (--npages);
245+#else
246+ /*
247+ * Prefer clear_pages() to allow for architectural optimizations
248+ * when operating on contiguous page ranges.
249+ */
250+ clear_pages(addr, npages);
251+#endif
252+}
253+
254+/**
255+ * clear_user_highpage() - clear a page to be mapped to user space
256+ * @page: start page
257+ * @vaddr: start address of the user mapping
258+ *
259+ * With !CONFIG_HIGHMEM this (and the copy_user_highpage() below) will
260+ * be plain clear_user_page() (and copy_user_page()).
261+ */
202262 static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
203263 {
204264 void *addr = kmap_local_page(page);
205265 clear_user_page(addr, vaddr, page);
206266 kunmap_local(addr);
207267 }
268+#endif /* clear_user_highpage */
269+
270+/**
271+ * clear_user_highpages() - clear a page range to be mapped to user space
272+ * @page: start page
273+ * @vaddr: start address of the user mapping
274+ * @npages: number of pages
275+ *
276+ * Assumes that all the pages in the region (@page, +@npages) are valid
277+ * so this does no exception handling.
278+ */
279+static inline void clear_user_highpages(struct page *page, unsigned long vaddr,
280+ unsigned int npages)
281+{
282+
283+#if defined(clear_user_highpage) || defined(CONFIG_HIGHMEM)
284+ /*
285+ * An architecture defined clear_user_highpage() implies special
286+ * handling is needed.
287+ *
288+ * So we use that or, the generic variant if CONFIG_HIGHMEM is
289+ * enabled.
290+ */
291+ do {
292+ clear_user_highpage(page, vaddr);
293+ vaddr += PAGE_SIZE;
294+ page++;
295+ } while (--npages);
296+#else
297+
298+ /*
299+ * Prefer clear_user_pages() to allow for architectural optimizations
300+ * when operating on contiguous page ranges.
301+ */
302+ clear_user_pages(page_address(page), vaddr, page, npages);
208303 #endif
304+}
209305
210306 #ifndef vma_alloc_zeroed_movable_folio
211307 /**
include/linux/hugetlb.h+6−9
@@ -171,11 +171,11 @@ bool hugetlbfs_pagecache_present(struct hstate *h, · 170 unmodified lines
171171
172172 struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio);
173173
174+extern int movable_gigantic_pages __read_mostly;
174175 extern int sysctl_hugetlb_shm_group __read_mostly;
175176 extern struct list_head huge_boot_pages[MAX_NUMNODES];
176177
177178 void hugetlb_bootmem_alloc(void);
178−bool hugetlb_bootmem_allocated(void);
179179 extern nodemask_t hugetlb_bootmem_nodes;
180180 void hugetlb_bootmem_set_nodes(void);
181181
@@ -280,6 +280,8 @@ void fixup_hugetlb_reservations(struct vm_area_struct *vma); · 98 unmodified lines
280280 void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
281281 int hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
282282
283+unsigned int arch_hugetlb_cma_order(void);
284+
283285 #else /* !CONFIG_HUGETLB_PAGE */
284286
285287 static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma)
@@ -929,7 +931,7 @@ static inline bool hugepage_movable_supported(struct hstate *h) · 643 unmodified lines
929931 if (!hugepage_migration_supported(h))
930932 return false;
931933
932− if (hstate_is_gigantic(h))
934+ if (hstate_is_gigantic(h) && !movable_gigantic_pages)
933935 return false;
934936 return true;
935937 }
@@ -1303,11 +1305,6 @@ static inline bool hugetlbfs_pagecache_present( · 367 unmodified lines
13031305 static inline void hugetlb_bootmem_alloc(void)
13041306 {
13051307 }
1306−
1307−static inline bool hugetlb_bootmem_allocated(void)
1308−{
1309− return false;
1310−}
13111308 #endif /* CONFIG_HUGETLB_PAGE */
13121309
13131310 static inline spinlock_t *huge_pte_lock(struct hstate *h,
@@ -1321,9 +1318,9 @@ static inline spinlock_t *huge_pte_lock(struct hstate *h, · 7 unmodified lines
13211318 }
13221319
13231320 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
1324−extern void __init hugetlb_cma_reserve(int order);
1321+extern void __init hugetlb_cma_reserve(void);
13251322 #else
1326−static inline __init void hugetlb_cma_reserve(int order)
1323+static inline __init void hugetlb_cma_reserve(void)
13271324 {
13281325 }
13291326 #endif
include/linux/khugepaged.h+4−5
@@ -17,8 +17,8 @@ extern void khugepaged_enter_vma(struct vm_area_struct *vma, · 16 unmodified lines
1717 vm_flags_t vm_flags);
1818 extern void khugepaged_min_free_kbytes_update(void);
1919 extern bool current_is_khugepaged(void);
20−extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
21− bool install_pmd);
20+void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
21+ bool install_pmd);
2222
2323 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
2424 {
@@ -42,10 +42,9 @@ static inline void khugepaged_enter_vma(struct vm_area_struct *vma, · 17 unmodified lines
4242 vm_flags_t vm_flags)
4343 {
4444 }
45−static inline int collapse_pte_mapped_thp(struct mm_struct *mm,
46− unsigned long addr, bool install_pmd)
45+static inline void collapse_pte_mapped_thp(struct mm_struct *mm,
46+ unsigned long addr, bool install_pmd)
4747 {
48− return 0;
4948 }
5049
5150 static inline void khugepaged_min_free_kbytes_update(void)
include/linux/maple_tree.h+0−9
@@ -129,13 +129,6 @@ struct maple_arange_64 { · 128 unmodified lines
129129 struct maple_metadata meta;
130130 };
131131
132−struct maple_alloc {
133− unsigned long total;
134− unsigned char node_count;
135− unsigned int request_count;
136− struct maple_alloc *slot[MAPLE_ALLOC_SLOTS];
137−};
138−
139132 struct maple_topiary {
140133 struct maple_pnode *parent;
141134 struct maple_enode *next; /* Overlaps the pivot */
@@ -306,7 +299,6 @@ struct maple_node { · 164 unmodified lines
306299 };
307300 struct maple_range_64 mr64;
308301 struct maple_arange_64 ma64;
309− struct maple_alloc alloc;
310302 };
311303 };
312304
@@ -536,7 +528,6 @@ bool mas_nomem(struct ma_state *mas, gfp_t gfp); · 223 unmodified lines
536528 void mas_pause(struct ma_state *mas);
537529 void maple_tree_init(void);
538530 void mas_destroy(struct ma_state *mas);
539−int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries);
540531
541532 void *mas_prev(struct ma_state *mas, unsigned long min);
542533 void *mas_prev_range(struct ma_state *mas, unsigned long max);
include/linux/memblock.h+2−2
@@ -598,9 +598,9 @@ extern void *alloc_large_system_hash(const char *tablename, · 597 unmodified lines
598598 */
599599 #ifdef CONFIG_NUMA
600600 #define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
601−extern int hashdist; /* Distribute hashes across NUMA nodes? */
601+extern bool hashdist; /* Distribute hashes across NUMA nodes? */
602602 #else
603−#define hashdist (0)
603+#define hashdist (false)
604604 #endif
605605
606606 #ifdef CONFIG_MEMTEST
include/linux/memcontrol.h+20−28
@@ -65,7 +65,7 @@ struct mem_cgroup_reclaim_cookie { · 64 unmodified lines
6565
6666 #define MEM_CGROUP_ID_SHIFT 16
6767
68−struct mem_cgroup_id {
68+struct mem_cgroup_private_id {
6969 int id;
7070 refcount_t ref;
7171 };
@@ -191,7 +191,7 @@ struct mem_cgroup { · 119 unmodified lines
191191 struct cgroup_subsys_state css;
192192
193193 /* Private memcg ID. Used to ID objects that outlive the cgroup */
194− struct mem_cgroup_id id;
194+ struct mem_cgroup_private_id id;
195195
196196 /* Accounted resources */
197197 struct page_counter memory; /* Both v1 & v2 */
@@ -557,13 +557,15 @@ static inline bool mem_cgroup_disabled(void) · 359 unmodified lines
557557 static inline void mem_cgroup_protection(struct mem_cgroup *root,
558558 struct mem_cgroup *memcg,
559559 unsigned long *min,
560− unsigned long *low)
560+ unsigned long *low,
561+ unsigned long *usage)
561562 {
562− *min = *low = 0;
563+ *min = *low = *usage = 0;
563564
564565 if (mem_cgroup_disabled())
565566 return;
566567
568+ *usage = page_counter_read(&memcg->memory);
567569 /*
568570 * There is no reclaim protection applied to a targeted reclaim.
569571 * We are special casing this specific case here because
@@ -819,23 +821,21 @@ void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); · 249 unmodified lines
819821 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
820822 int (*)(struct task_struct *, void *), void *arg);
821823
822−static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
824+static inline unsigned short mem_cgroup_private_id(struct mem_cgroup *memcg)
823825 {
824826 if (mem_cgroup_disabled())
825827 return 0;
826828
827829 return memcg->id.id;
828830 }
829−struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
831+struct mem_cgroup *mem_cgroup_from_private_id(unsigned short id);
830832
831−#ifdef CONFIG_SHRINKER_DEBUG
832−static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
833+static inline u64 mem_cgroup_id(struct mem_cgroup *memcg)
833834 {
834− return memcg ? cgroup_ino(memcg->css.cgroup) : 0;
835+ return memcg ? cgroup_id(memcg->css.cgroup) : 0;
835836 }
836837
837−struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino);
838−#endif
838+struct mem_cgroup *mem_cgroup_get_from_id(u64 id);
839839
840840 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
841841 {
@@ -919,8 +919,6 @@ static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask) · 77 unmodified lines
919919
920920 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
921921
922−unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
923−
924922 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
925923 struct task_struct *p);
926924
@@ -1108,9 +1106,10 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm, · 181 unmodified lines
11081106 static inline void mem_cgroup_protection(struct mem_cgroup *root,
11091107 struct mem_cgroup *memcg,
11101108 unsigned long *min,
1111− unsigned long *low)
1109+ unsigned long *low,
1110+ unsigned long *usage)
11121111 {
1113− *min = *low = 0;
1112+ *min = *low = *usage = 0;
11141113 }
11151114
11161115 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
@@ -1283,29 +1282,27 @@ static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, · 166 unmodified lines
12831282 {
12841283 }
12851284
1286−static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1285+static inline unsigned short mem_cgroup_private_id(struct mem_cgroup *memcg)
12871286 {
12881287 return 0;
12891288 }
12901289
1291−static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1290+static inline struct mem_cgroup *mem_cgroup_from_private_id(unsigned short id)
12921291 {
12931292 WARN_ON_ONCE(id);
12941293 /* XXX: This should always return root_mem_cgroup */
12951294 return NULL;
12961295 }
12971296
1298−#ifdef CONFIG_SHRINKER_DEBUG
1299−static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
1297+static inline u64 mem_cgroup_id(struct mem_cgroup *memcg)
13001298 {
13011299 return 0;
13021300 }
13031301
1304−static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
1302+static inline struct mem_cgroup *mem_cgroup_get_from_id(u64 id)
13051303 {
13061304 return NULL;
13071305 }
1308−#endif
13091306
13101307 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
13111308 {
@@ -1334,11 +1331,6 @@ static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) · 22 unmodified lines
13341331 return 0;
13351332 }
13361333
1337−static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1338−{
1339− return 0;
1340−}
1341−
13421334 static inline void
13431335 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
13441336 {
@@ -1750,7 +1742,7 @@ static inline int memcg_kmem_id(struct mem_cgroup *memcg) · 405 unmodified lines
17501742 return memcg ? memcg->kmemcg_id : -1;
17511743 }
17521744
1753−struct mem_cgroup *mem_cgroup_from_slab_obj(void *p);
1745+struct mem_cgroup *mem_cgroup_from_virt(void *p);
17541746
17551747 static inline void count_objcg_events(struct obj_cgroup *objcg,
17561748 enum vm_event_item idx,
@@ -1822,7 +1814,7 @@ static inline int memcg_kmem_id(struct mem_cgroup *memcg) · 65 unmodified lines
18221814 return -1;
18231815 }
18241816
1825−static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
1817+static inline struct mem_cgroup *mem_cgroup_from_virt(void *p)
18261818 {
18271819 return NULL;
18281820 }
include/linux/mm.h+66−6
@@ -46,6 +46,7 @@ struct pt_regs; · 45 unmodified lines
4646 struct folio_batch;
4747
4848 void arch_mm_preinit(void);
49+void mm_core_init_early(void);
4950 void mm_core_init(void);
5051 void init_mm_internals(void);
5152
@@ -1008,10 +1009,7 @@ static inline void vma_flag_set_atomic(struct vm_area_struct *vma, · 956 unmodified lines
10081009 {
10091010 unsigned long *bitmap = ACCESS_PRIVATE(&vma->flags, __vma_flags);
10101011
1011− /* mmap read lock/VMA read lock must be held. */
1012− if (!rwsem_is_locked(&vma->vm_mm->mmap_lock))
1013− vma_assert_locked(vma);
1014−
1012+ vma_assert_stabilised(vma);
10151013 if (__vma_flag_atomic_valid(vma, bit))
10161014 set_bit((__force int)bit, bitmap);
10171015 }
@@ -2906,6 +2904,13 @@ static inline unsigned long get_mm_rss(struct mm_struct *mm) · 1888 unmodified lines
29062904 get_mm_counter(mm, MM_SHMEMPAGES);
29072905 }
29082906
2907+static inline unsigned long get_mm_rss_sum(struct mm_struct *mm)
2908+{
2909+ return get_mm_counter_sum(mm, MM_FILEPAGES) +
2910+ get_mm_counter_sum(mm, MM_ANONPAGES) +
2911+ get_mm_counter_sum(mm, MM_SHMEMPAGES);
2912+}
2913+
29092914 static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
29102915 {
29112916 return max(mm->hiwater_rss, get_mm_rss(mm));
@@ -3518,7 +3523,7 @@ static inline unsigned long get_num_physpages(void) · 606 unmodified lines
35183523 }
35193524
35203525 /*
3521− * Using memblock node mappings, an architecture may initialise its
3526+ * FIXME: Using memblock node mappings, an architecture may initialise its
35223527 * zones, allocate the backing mem_map and account for memory holes in an
35233528 * architecture independent manner.
35243529 *
@@ -3533,7 +3538,7 @@ static inline unsigned long get_num_physpages(void) · 8 unmodified lines
35333538 * memblock_add_node(base, size, nid, MEMBLOCK_NONE)
35343539 * free_area_init(max_zone_pfns);
35353540 */
3536−void free_area_init(unsigned long *max_zone_pfn);
3541+void arch_zone_limits_init(unsigned long *max_zone_pfn);
35373542 unsigned long node_map_pfn_alignment(void);
35383543 extern unsigned long absent_pages_in_range(unsigned long start_pfn,
35393544 unsigned long end_pfn);
@@ -4180,6 +4185,61 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, · 640 unmodified lines
41804185 unsigned int order) {}
41814186 #endif /* CONFIG_DEBUG_PAGEALLOC */
41824187
4188+#ifndef clear_pages
4189+/**
4190+ * clear_pages() - clear a page range for kernel-internal use.
4191+ * @addr: start address
4192+ * @npages: number of pages
4193+ *
4194+ * Use clear_user_pages() instead when clearing a page range to be
4195+ * mapped to user space.
4196+ *
4197+ * Does absolutely no exception handling.
4198+ *
4199+ * Note that even though the clearing operation is preemptible, clear_pages()
4200+ * does not (and on architectures where it reduces to a few long-running
4201+ * instructions, might not be able to) call cond_resched() to check if
4202+ * rescheduling is required.
4203+ *
4204+ * When running under preemptible models this is not a problem. Under
4205+ * cooperatively scheduled models, however, the caller is expected to
4206+ * limit @npages to no more than PROCESS_PAGES_NON_PREEMPT_BATCH.
4207+ */
4208+static inline void clear_pages(void *addr, unsigned int npages)
4209+{
4210+ do {
4211+ clear_page(addr);
4212+ addr += PAGE_SIZE;
4213+ } while (--npages);
4214+}
4215+#endif
4216+
4217+#ifndef PROCESS_PAGES_NON_PREEMPT_BATCH
4218+#ifdef clear_pages
4219+/*
4220+ * The architecture defines clear_pages(), and we assume that it is
4221+ * generally "fast". So choose a batch size large enough to allow the processor
4222+ * headroom for optimizing the operation and yet small enough that we see
4223+ * reasonable preemption latency for when this optimization is not possible
4224+ * (ex. slow microarchitectures, memory bandwidth saturation.)
4225+ *
4226+ * With a value of 32MB and assuming a memory bandwidth of ~10GBps, this should
4227+ * result in worst case preemption latency of around 3ms when clearing pages.
4228+ *
4229+ * (See comment above clear_pages() for why preemption latency is a concern
4230+ * here.)
4231+ */
4232+#define PROCESS_PAGES_NON_PREEMPT_BATCH (SZ_32M >> PAGE_SHIFT)
4233+#else /* !clear_pages */
4234+/*
4235+ * The architecture does not provide a clear_pages() implementation. Assume
4236+ * that clear_page() -- which clear_pages() will fallback to -- is relatively
4237+ * slow and choose a small value for PROCESS_PAGES_NON_PREEMPT_BATCH.
4238+ */
4239+#define PROCESS_PAGES_NON_PREEMPT_BATCH 1
4240+#endif
4241+#endif
4242+
41834243 #ifdef __HAVE_ARCH_GATE_AREA
41844244 extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
41854245 extern int in_gate_area_no_mm(unsigned long addr);
include/linux/mm_types.h+52−5
@@ -752,8 +752,18 @@ static inline struct anon_vma_name *anon_vma_name_alloc(const char *name) · 751 unmodified lines
752752 }
753753 #endif
754754
755−#define VMA_LOCK_OFFSET 0x40000000
756−#define VMA_REF_LIMIT (VMA_LOCK_OFFSET - 1)
755+/*
756+ * While __vma_enter_locked() is working to ensure are no read-locks held on a
757+ * VMA (either while acquiring a VMA write lock or marking a VMA detached) we
758+ * set the VM_REFCNT_EXCLUDE_READERS_FLAG in vma->vm_refcnt to indiciate to
759+ * vma_start_read() that the reference count should be left alone.
760+ *
761+ * See the comment describing vm_refcnt in vm_area_struct for details as to
762+ * which values the VMA reference count can be.
763+ */
764+#define VM_REFCNT_EXCLUDE_READERS_BIT (30)
765+#define VM_REFCNT_EXCLUDE_READERS_FLAG (1U << VM_REFCNT_EXCLUDE_READERS_BIT)
766+#define VM_REFCNT_LIMIT (VM_REFCNT_EXCLUDE_READERS_FLAG - 1)
757767
758768 struct vma_numab_state {
759769 /*
@@ -935,10 +945,10 @@ struct vm_area_struct { · 175 unmodified lines
935945 /*
936946 * Can only be written (using WRITE_ONCE()) while holding both:
937947 * - mmap_lock (in write mode)
938− * - vm_refcnt bit at VMA_LOCK_OFFSET is set
948+ * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_FLAG is set
939949 * Can be read reliably while holding one of:
940950 * - mmap_lock (in read or write mode)
941− * - vm_refcnt bit at VMA_LOCK_OFFSET is set or vm_refcnt > 1
951+ * - vm_refcnt bit at VM_REFCNT_EXCLUDE_READERS_BIT is set or vm_refcnt > 1
942952 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
943953 * while holding nothing (except RCU to keep the VMA struct allocated).
944954 *
@@ -980,7 +990,44 @@ struct vm_area_struct { · 35 unmodified lines
980990 struct vma_numab_state *numab_state; /* NUMA Balancing state */
981991 #endif
982992 #ifdef CONFIG_PER_VMA_LOCK
983− /* Unstable RCU readers are allowed to read this. */
993+ /*
994+ * Used to keep track of firstly, whether the VMA is attached, secondly,
995+ * if attached, how many read locks are taken, and thirdly, if the
996+ * VM_REFCNT_EXCLUDE_READERS_FLAG is set, whether any read locks held
997+ * are currently in the process of being excluded.
998+ *
999+ * This value can be equal to:
1000+ *
1001+ * 0 - Detached. IMPORTANT: when the refcnt is zero, readers cannot
1002+ * increment it.
1003+ *
1004+ * 1 - Attached and either unlocked or write-locked. Write locks are
1005+ * identified via __is_vma_write_locked() which checks for equality of
1006+ * vma->vm_lock_seq and mm->mm_lock_seq.
1007+ *
1008+ * >1, < VM_REFCNT_EXCLUDE_READERS_FLAG - Read-locked or (unlikely)
1009+ * write-locked with other threads having temporarily incremented the
1010+ * reference count prior to determining it is write-locked and
1011+ * decrementing it again.
1012+ *
1013+ * VM_REFCNT_EXCLUDE_READERS_FLAG - Detached, pending
1014+ * __vma_end_exclude_readers() completion which will decrement the
1015+ * reference count to zero. IMPORTANT - at this stage no further readers
1016+ * can increment the reference count. It can only be reduced.
1017+ *
1018+ * VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either write-locking
1019+ * an attached VMA and has yet to invoke __vma_end_exclude_readers(),
1020+ * OR a thread is detaching a VMA and is waiting on a single spurious
1021+ * reader in order to decrement the reference count. IMPORTANT - as
1022+ * above, no further readers can increment the reference count.
1023+ *
1024+ * > VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - A thread is either
1025+ * write-locking or detaching a VMA is waiting on readers to
1026+ * exit. IMPORTANT - as above, no further readers can increment the
1027+ * reference count.
1028+ *
1029+ * NOTE: Unstable RCU readers are allowed to read this.
1030+ */
9841031 refcount_t vm_refcnt ____cacheline_aligned_in_smp;
9851032 #ifdef CONFIG_DEBUG_LOCK_ALLOC
9861033 struct lockdep_map vmlock_dep_map;
include/linux/mm_types_task.h+5−0
@@ -88,4 +88,9 @@ struct tlbflush_unmap_batch { · 87 unmodified lines
8888 #endif
8989 };
9090
91+struct lazy_mmu_state {
92+ u8 enable_count;
93+ u8 pause_count;
94+};
95+
9196 #endif /* _LINUX_MM_TYPES_TASK_H */
include/linux/mmap_lock.h+240−39
@@ -78,6 +78,43 @@ static inline void mmap_assert_write_locked(const struct mm_struct *mm) · 77 unmodified lines
7878
7979 #ifdef CONFIG_PER_VMA_LOCK
8080
81+#ifdef CONFIG_LOCKDEP
82+#define __vma_lockdep_map(vma) (&vma->vmlock_dep_map)
83+#else
84+#define __vma_lockdep_map(vma) NULL
85+#endif
86+
87+/*
88+ * VMA locks do not behave like most ordinary locks found in the kernel, so we
89+ * cannot quite have full lockdep tracking in the way we would ideally prefer.
90+ *
91+ * Read locks act as shared locks which exclude an exclusive lock being
92+ * taken. We therefore mark these accordingly on read lock acquire/release.
93+ *
94+ * Write locks are acquired exclusively per-VMA, but released in a shared
95+ * fashion, that is upon vma_end_write_all(), we update the mmap's seqcount such
96+ * that write lock is released.
97+ *
98+ * We therefore cannot track write locks per-VMA, nor do we try. Mitigating this
99+ * is the fact that, of course, we do lockdep-track the mmap lock rwsem which
100+ * must be held when taking a VMA write lock.
101+ *
102+ * We do, however, want to indicate that during either acquisition of a VMA
103+ * write lock or detachment of a VMA that we require the lock held be exclusive,
104+ * so we utilise lockdep to do so.
105+ */
106+#define __vma_lockdep_acquire_read(vma) \
107+ lock_acquire_shared(__vma_lockdep_map(vma), 0, 1, NULL, _RET_IP_)
108+#define __vma_lockdep_release_read(vma) \
109+ lock_release(__vma_lockdep_map(vma), _RET_IP_)
110+#define __vma_lockdep_acquire_exclusive(vma) \
111+ lock_acquire_exclusive(__vma_lockdep_map(vma), 0, 0, NULL, _RET_IP_)
112+#define __vma_lockdep_release_exclusive(vma) \
113+ lock_release(__vma_lockdep_map(vma), _RET_IP_)
114+/* Only meaningful if CONFIG_LOCK_STAT is defined. */
115+#define __vma_lockdep_stat_mark_acquired(vma) \
116+ lock_acquired(__vma_lockdep_map(vma), _RET_IP_)
117+
81118 static inline void mm_lock_seqcount_init(struct mm_struct *mm)
82119 {
83120 seqcount_init(&mm->mm_lock_seq);
@@ -115,36 +152,81 @@ static inline void vma_lock_init(struct vm_area_struct *vma, bool reset_refcnt) · 31 unmodified lines
115152 #ifdef CONFIG_DEBUG_LOCK_ALLOC
116153 static struct lock_class_key lockdep_key;
117154
118− lockdep_init_map(&vma->vmlock_dep_map, "vm_lock", &lockdep_key, 0);
155+ lockdep_init_map(__vma_lockdep_map(vma), "vm_lock", &lockdep_key, 0);
119156 #endif
120157 if (reset_refcnt)
121158 refcount_set(&vma->vm_refcnt, 0);
122159 vma->vm_lock_seq = UINT_MAX;
123160 }
124161
125−static inline bool is_vma_writer_only(int refcnt)
162+/*
163+ * This function determines whether the input VMA reference count describes a
164+ * VMA which has excluded all VMA read locks.
165+ *
166+ * In the case of a detached VMA, we may incorrectly indicate that readers are
167+ * excluded when one remains, because in that scenario we target a refcount of
168+ * VM_REFCNT_EXCLUDE_READERS_FLAG, rather than the attached target of
169+ * VM_REFCNT_EXCLUDE_READERS_FLAG + 1.
170+ *
171+ * However, the race window for that is very small so it is unlikely.
172+ *
173+ * Returns: true if readers are excluded, false otherwise.
174+ */
175+static inline bool __vma_are_readers_excluded(int refcnt)
126176 {
127177 /*
128− * With a writer and no readers, refcnt is VMA_LOCK_OFFSET if the vma
129− * is detached and (VMA_LOCK_OFFSET + 1) if it is attached. Waiting on
130− * a detached vma happens only in vma_mark_detached() and is a rare
131− * case, therefore most of the time there will be no unnecessary wakeup.
178+ * See the comment describing the vm_area_struct->vm_refcnt field for
179+ * details of possible refcnt values.
132180 */
133− return (refcnt & VMA_LOCK_OFFSET) && refcnt <= VMA_LOCK_OFFSET + 1;
181+ return (refcnt & VM_REFCNT_EXCLUDE_READERS_FLAG) &&
182+ refcnt <= VM_REFCNT_EXCLUDE_READERS_FLAG + 1;
183+}
184+
185+/*
186+ * Actually decrement the VMA reference count.
187+ *
188+ * The function returns the reference count as it was immediately after the
189+ * decrement took place. If it returns zero, the VMA is now detached.
190+ */
191+static inline __must_check unsigned int
192+__vma_refcount_put_return(struct vm_area_struct *vma)
193+{
194+ int oldcnt;
195+
196+ if (__refcount_dec_and_test(&vma->vm_refcnt, &oldcnt))
197+ return 0;
198+
199+ return oldcnt - 1;
134200 }
135201
202+/**
203+ * vma_refcount_put() - Drop reference count in VMA vm_refcnt field due to a
204+ * read-lock being dropped.
205+ * @vma: The VMA whose reference count we wish to decrement.
206+ *
207+ * If we were the last reader, wake up threads waiting to obtain an exclusive
208+ * lock.
209+ */
136210 static inline void vma_refcount_put(struct vm_area_struct *vma)
137211 {
138− /* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt */
212+ /* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt. */
139213 struct mm_struct *mm = vma->vm_mm;
140− int oldcnt;
214+ int newcnt;
141215
142− rwsem_release(&vma->vmlock_dep_map, _RET_IP_);
143− if (!__refcount_dec_and_test(&vma->vm_refcnt, &oldcnt)) {
216+ __vma_lockdep_release_read(vma);
217+ newcnt = __vma_refcount_put_return(vma);
144218
145− if (is_vma_writer_only(oldcnt - 1))
146− rcuwait_wake_up(&mm->vma_writer_wait);
147− }
219+ /*
220+ * __vma_start_exclude_readers() may be sleeping waiting for readers to
221+ * drop their reference count, so wake it up if we were the last reader
222+ * blocking it from being acquired.
223+ *
224+ * We may be raced by other readers temporarily incrementing the
225+ * reference count, though the race window is very small, this might
226+ * cause spurious wakeups.
227+ */
228+ if (newcnt && __vma_are_readers_excluded(newcnt))
229+ rcuwait_wake_up(&mm->vma_writer_wait);
148230 }
149231
150232 /*
@@ -159,10 +241,10 @@ static inline bool vma_start_read_locked_nested(struct vm_area_struct *vma, int · 8 unmodified lines
159241
160242 mmap_assert_locked(vma->vm_mm);
161243 if (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt,
162− VMA_REF_LIMIT)))
244+ VM_REFCNT_LIMIT)))
163245 return false;
164246
165− rwsem_acquire_read(&vma->vmlock_dep_map, 0, 1, _RET_IP_);
247+ __vma_lockdep_acquire_read(vma);
166248 return true;
167249 }
168250
@@ -182,21 +264,31 @@ static inline void vma_end_read(struct vm_area_struct *vma) · 13 unmodified lines
182264 vma_refcount_put(vma);
183265 }
184266
185−/* WARNING! Can only be used if mmap_lock is expected to be write-locked */
186−static inline bool __is_vma_write_locked(struct vm_area_struct *vma, unsigned int *mm_lock_seq)
267+static inline unsigned int __vma_raw_mm_seqnum(struct vm_area_struct *vma)
187268 {
269+ const struct mm_struct *mm = vma->vm_mm;
270+
271+ /* We must hold an exclusive write lock for this access to be valid. */
188272 mmap_assert_write_locked(vma->vm_mm);
273+ return mm->mm_lock_seq.sequence;
274+}
189275
276+/*
277+ * Determine whether a VMA is write-locked. Must be invoked ONLY if the mmap
278+ * write lock is held.
279+ *
280+ * Returns true if write-locked, otherwise false.
281+ */
282+static inline bool __is_vma_write_locked(struct vm_area_struct *vma)
283+{
190284 /*
191285 * current task is holding mmap_write_lock, both vma->vm_lock_seq and
192286 * mm->mm_lock_seq can't be concurrently modified.
193287 */
194− *mm_lock_seq = vma->vm_mm->mm_lock_seq.sequence;
195− return (vma->vm_lock_seq == *mm_lock_seq);
288+ return vma->vm_lock_seq == __vma_raw_mm_seqnum(vma);
196289 }
197290
198−int __vma_start_write(struct vm_area_struct *vma, unsigned int mm_lock_seq,
199− int state);
291+int __vma_start_write(struct vm_area_struct *vma, int state);
200292
201293 /*
202294 * Begin writing to a VMA.
@@ -205,12 +297,10 @@ int __vma_start_write(struct vm_area_struct *vma, unsigned int mm_lock_seq, · 2 unmodified lines
205297 */
206298 static inline void vma_start_write(struct vm_area_struct *vma)
207299 {
208− unsigned int mm_lock_seq;
209−
210− if (__is_vma_write_locked(vma, &mm_lock_seq))
300+ if (__is_vma_write_locked(vma))
211301 return;
212302
213− __vma_start_write(vma, mm_lock_seq, TASK_UNINTERRUPTIBLE);
303+ __vma_start_write(vma, TASK_UNINTERRUPTIBLE);
214304 }
215305
216306 /**
@@ -229,26 +319,110 @@ static inline void vma_start_write(struct vm_area_struct *vma) · 12 unmodified lines
229319 static inline __must_check
230320 int vma_start_write_killable(struct vm_area_struct *vma)
231321 {
232− unsigned int mm_lock_seq;
233−
234− if (__is_vma_write_locked(vma, &mm_lock_seq))
322+ if (__is_vma_write_locked(vma))
235323 return 0;
236− return __vma_start_write(vma, mm_lock_seq, TASK_KILLABLE);
324+
325+ return __vma_start_write(vma, TASK_KILLABLE);
237326 }
238327
328+/**
329+ * vma_assert_write_locked() - assert that @vma holds a VMA write lock.
330+ * @vma: The VMA to assert.
331+ */
239332 static inline void vma_assert_write_locked(struct vm_area_struct *vma)
240333 {
241− unsigned int mm_lock_seq;
242−
243− VM_BUG_ON_VMA(!__is_vma_write_locked(vma, &mm_lock_seq), vma);
334+ VM_WARN_ON_ONCE_VMA(!__is_vma_write_locked(vma), vma);
244335 }
245336
337+/**
338+ * vma_assert_locked() - assert that @vma holds either a VMA read or a VMA write
339+ * lock and is not detached.
340+ * @vma: The VMA to assert.
341+ */
246342 static inline void vma_assert_locked(struct vm_area_struct *vma)
247343 {
248− unsigned int mm_lock_seq;
344+ unsigned int refcnt;
345+
346+ if (IS_ENABLED(CONFIG_LOCKDEP)) {
347+ if (!lock_is_held(__vma_lockdep_map(vma)))
348+ vma_assert_write_locked(vma);
349+ return;
350+ }
351+
352+ /*
353+ * See the comment describing the vm_area_struct->vm_refcnt field for
354+ * details of possible refcnt values.
355+ */
356+ refcnt = refcount_read(&vma->vm_refcnt);
357+
358+ /*
359+ * In this case we're either read-locked, write-locked with temporary
360+ * readers, or in the midst of excluding readers, all of which means
361+ * we're locked.
362+ */
363+ if (refcnt > 1)
364+ return;
365+
366+ /* It is a bug for the VMA to be detached here. */
367+ VM_WARN_ON_ONCE_VMA(!refcnt, vma);
368+
369+ /*
370+ * OK, the VMA has a reference count of 1 which means it is either
371+ * unlocked and attached or write-locked, so assert that it is
372+ * write-locked.
373+ */
374+ vma_assert_write_locked(vma);
375+}
376+
377+/**
378+ * vma_assert_stabilised() - assert that this VMA cannot be changed from
379+ * underneath us either by having a VMA or mmap lock held.
380+ * @vma: The VMA whose stability we wish to assess.
381+ *
382+ * If lockdep is enabled we can precisely ensure stability via either an mmap
383+ * lock owned by us or a specific VMA lock.
384+ *
385+ * With lockdep disabled we may sometimes race with other threads acquiring the
386+ * mmap read lock simultaneous with our VMA read lock.
387+ */
388+static inline void vma_assert_stabilised(struct vm_area_struct *vma)
389+{
390+ /*
391+ * If another thread owns an mmap lock, it may go away at any time, and
392+ * thus is no guarantee of stability.
393+ *
394+ * If lockdep is enabled we can accurately determine if an mmap lock is
395+ * held and owned by us. Otherwise we must approximate.
396+ *
397+ * It doesn't necessarily mean we are not stabilised however, as we may
398+ * hold a VMA read lock (not a write lock as this would require an owned
399+ * mmap lock).
400+ *
401+ * If (assuming lockdep is not enabled) we were to assert a VMA read
402+ * lock first we may also run into issues, as other threads can hold VMA
403+ * read locks simlutaneous to us.
404+ *
405+ * Therefore if lockdep is not enabled we risk a false negative (i.e. no
406+ * assert fired). If accurate checking is required, enable lockdep.
407+ */
408+ if (IS_ENABLED(CONFIG_LOCKDEP)) {
409+ if (lockdep_is_held(&vma->vm_mm->mmap_lock))
410+ return;
411+ } else {
412+ if (rwsem_is_locked(&vma->vm_mm->mmap_lock))
413+ return;
414+ }
249415
250− VM_BUG_ON_VMA(refcount_read(&vma->vm_refcnt) <= 1 &&
251− !__is_vma_write_locked(vma, &mm_lock_seq), vma);
416+ /*
417+ * We're not stabilised by the mmap lock, so assert that we're
418+ * stabilised by a VMA lock.
419+ */
420+ vma_assert_locked(vma);
421+}
422+
423+static inline bool vma_is_attached(struct vm_area_struct *vma)
424+{
425+ return refcount_read(&vma->vm_refcnt);
252426 }
253427
254428 /*
@@ -258,12 +432,12 @@ static inline void vma_assert_locked(struct vm_area_struct *vma) · 3 unmodified lines
258432 */
259433 static inline void vma_assert_attached(struct vm_area_struct *vma)
260434 {
261− WARN_ON_ONCE(!refcount_read(&vma->vm_refcnt));
435+ WARN_ON_ONCE(!vma_is_attached(vma));
262436 }
263437
264438 static inline void vma_assert_detached(struct vm_area_struct *vma)
265439 {
266− WARN_ON_ONCE(refcount_read(&vma->vm_refcnt));
440+ WARN_ON_ONCE(vma_is_attached(vma));
267441 }
268442
269443 static inline void vma_mark_attached(struct vm_area_struct *vma)
@@ -273,7 +447,28 @@ static inline void vma_mark_attached(struct vm_area_struct *vma) · 3 unmodified lines
273447 refcount_set_release(&vma->vm_refcnt, 1);
274448 }
275449
276−void vma_mark_detached(struct vm_area_struct *vma);
450+void __vma_exclude_readers_for_detach(struct vm_area_struct *vma);
451+
452+static inline void vma_mark_detached(struct vm_area_struct *vma)
453+{
454+ vma_assert_write_locked(vma);
455+ vma_assert_attached(vma);
456+
457+ /*
458+ * The VMA still being attached (refcnt > 0) - is unlikely, because the
459+ * vma has been already write-locked and readers can increment vm_refcnt
460+ * only temporarily before they check vm_lock_seq, realize the vma is
461+ * locked and drop back the vm_refcnt. That is a narrow window for
462+ * observing a raised vm_refcnt.
463+ *
464+ * See the comment describing the vm_area_struct->vm_refcnt field for
465+ * details of possible refcnt values.
466+ */
467+ if (likely(!__vma_refcount_put_return(vma)))
468+ return;
469+
470+ __vma_exclude_readers_for_detach(vma);
471+}
277472
278473 struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
279474 unsigned long address);
@@ -327,6 +522,12 @@ static inline void vma_assert_locked(struct vm_area_struct *vma) · 47 unmodified lines
327522 mmap_assert_locked(vma->vm_mm);
328523 }
329524
525+static inline void vma_assert_stabilised(struct vm_area_struct *vma)
526+{
527+ /* If no VMA locks, then either mmap lock suffices to stabilise. */
528+ mmap_assert_locked(vma->vm_mm);
529+}
530+
330531 #endif /* CONFIG_PER_VMA_LOCK */
331532
332533 static inline void mmap_write_lock(struct mm_struct *mm)
include/linux/mmdebug.h+10−0
@@ -47,6 +47,15 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi); · 46 unmodified lines
4747 BUG(); \
4848 } \
4949 } while (0)
50+#define VM_WARN_ON_PAGE(cond, page) ({ \
51+ int __ret_warn = !!(cond); \
52+ \
53+ if (unlikely(__ret_warn)) { \
54+ dump_page(page, "VM_WARN_ON_PAGE(" __stringify(cond)")");\
55+ WARN_ON(1); \
56+ } \
57+ unlikely(__ret_warn); \
58+})
5059 #define VM_WARN_ON_ONCE_PAGE(cond, page) ({ \
5160 static bool __section(".data..once") __warned; \
5261 int __ret_warn_once = !!(cond); \
@@ -122,6 +131,7 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi); · 69 unmodified lines
122131 #define VM_BUG_ON_MM(cond, mm) VM_BUG_ON(cond)
123132 #define VM_WARN_ON(cond) BUILD_BUG_ON_INVALID(cond)
124133 #define VM_WARN_ON_ONCE(cond) BUILD_BUG_ON_INVALID(cond)
134+#define VM_WARN_ON_PAGE(cond, page) BUILD_BUG_ON_INVALID(cond)
125135 #define VM_WARN_ON_ONCE_PAGE(cond, page) BUILD_BUG_ON_INVALID(cond)
126136 #define VM_WARN_ON_FOLIO(cond, folio) BUILD_BUG_ON_INVALID(cond)
127137 #define VM_WARN_ON_ONCE_FOLIO(cond, folio) BUILD_BUG_ON_INVALID(cond)
include/linux/mmzone.h+15−4
@@ -1534,14 +1534,27 @@ static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat) · 1533 unmodified lines
15341534 #include <linux/memory_hotplug.h>
15351535
15361536 void build_all_zonelists(pg_data_t *pgdat);
1537−void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
1538− enum zone_type highest_zoneidx);
15391537 bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
15401538 int highest_zoneidx, unsigned int alloc_flags,
15411539 long free_pages);
15421540 bool zone_watermark_ok(struct zone *z, unsigned int order,
15431541 unsigned long mark, int highest_zoneidx,
15441542 unsigned int alloc_flags);
1543+
1544+enum kswapd_clear_hopeless_reason {
1545+ KSWAPD_CLEAR_HOPELESS_OTHER = 0,
1546+ KSWAPD_CLEAR_HOPELESS_KSWAPD,
1547+ KSWAPD_CLEAR_HOPELESS_DIRECT,
1548+ KSWAPD_CLEAR_HOPELESS_PCP,
1549+};
1550+
1551+void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
1552+ enum zone_type highest_zoneidx);
1553+void kswapd_try_clear_hopeless(struct pglist_data *pgdat,
1554+ unsigned int order, int highest_zoneidx);
1555+void kswapd_clear_hopeless(pg_data_t *pgdat, enum kswapd_clear_hopeless_reason reason);
1556+bool kswapd_test_hopeless(pg_data_t *pgdat);
1557+
15451558 /*
15461559 * Memory initialization context, use to differentiate memory added by
15471560 * the platform statically or via memory hotplug interface.
@@ -2286,9 +2299,7 @@ static inline unsigned long next_present_section_nr(unsigned long section_nr) · 738 unmodified lines
22862299 #define pfn_to_nid(pfn) (0)
22872300 #endif
22882301
2289−void sparse_init(void);
22902302 #else
2291−#define sparse_init() do {} while (0)
22922303 #define sparse_index_init(_sec, _nid) do {} while (0)
22932304 #define sparse_vmemmap_init_nid_early(_nid) do {} while (0)
22942305 #define sparse_vmemmap_init_nid_late(_nid) do {} while (0)
include/linux/nodemask.h+4−4
@@ -157,10 +157,10 @@ static __always_inline bool __node_test_and_set(int node, nodemask_t *addr) · 156 unmodified lines
157157
158158 #define nodes_and(dst, src1, src2) \
159159 __nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
160−static __always_inline void __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
160+static __always_inline bool __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
161161 const nodemask_t *src2p, unsigned int nbits)
162162 {
163− bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
163+ return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
164164 }
165165
166166 #define nodes_or(dst, src1, src2) \
@@ -181,10 +181,10 @@ static __always_inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1 · 14 unmodified lines
181181
182182 #define nodes_andnot(dst, src1, src2) \
183183 __nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
184−static __always_inline void __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
184+static __always_inline bool __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
185185 const nodemask_t *src2p, unsigned int nbits)
186186 {
187− bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
187+ return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
188188 }
189189
190190 #define nodes_copy(dst, src) __nodes_copy(&(dst), &(src), MAX_NUMNODES)
include/linux/page-isolation.h+2−0
@@ -67,4 +67,6 @@ void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn); · 66 unmodified lines
6767
6868 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
6969 enum pb_isolate_mode mode);
70+bool page_is_unmovable(struct zone *zone, struct page *page,
71+ enum pb_isolate_mode mode, unsigned long *step);
7072 #endif
include/linux/page_table_check.h+40−29
@@ -14,15 +14,18 @@ extern struct static_key_true page_table_check_disabled; · 13 unmodified lines
1414 extern struct page_ext_operations page_table_check_ops;
1515
1616 void __page_table_check_zero(struct page *page, unsigned int order);
17−void __page_table_check_pte_clear(struct mm_struct *mm, pte_t pte);
18−void __page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd);
19−void __page_table_check_pud_clear(struct mm_struct *mm, pud_t pud);
20−void __page_table_check_ptes_set(struct mm_struct *mm, pte_t *ptep, pte_t pte,
21− unsigned int nr);
22−void __page_table_check_pmds_set(struct mm_struct *mm, pmd_t *pmdp, pmd_t pmd,
23− unsigned int nr);
24−void __page_table_check_puds_set(struct mm_struct *mm, pud_t *pudp, pud_t pud,
25− unsigned int nr);
17+void __page_table_check_pte_clear(struct mm_struct *mm, unsigned long addr,
18+ pte_t pte);
19+void __page_table_check_pmd_clear(struct mm_struct *mm, unsigned long addr,
20+ pmd_t pmd);
21+void __page_table_check_pud_clear(struct mm_struct *mm, unsigned long addr,
22+ pud_t pud);
23+void __page_table_check_ptes_set(struct mm_struct *mm, unsigned long addr,
24+ pte_t *ptep, pte_t pte, unsigned int nr);
25+void __page_table_check_pmds_set(struct mm_struct *mm, unsigned long addr,
26+ pmd_t *pmdp, pmd_t pmd, unsigned int nr);
27+void __page_table_check_puds_set(struct mm_struct *mm, unsigned long addr,
28+ pud_t *pudp, pud_t pud, unsigned int nr);
2629 void __page_table_check_pte_clear_range(struct mm_struct *mm,
2730 unsigned long addr,
2831 pmd_t pmd);
@@ -43,55 +46,59 @@ static inline void page_table_check_free(struct page *page, unsigned int order) · 14 unmodified lines
4346 __page_table_check_zero(page, order);
4447 }
4548
46−static inline void page_table_check_pte_clear(struct mm_struct *mm, pte_t pte)
49+static inline void page_table_check_pte_clear(struct mm_struct *mm,
50+ unsigned long addr, pte_t pte)
4751 {
4852 if (static_branch_likely(&page_table_check_disabled))
4953 return;
5054
51− __page_table_check_pte_clear(mm, pte);
55+ __page_table_check_pte_clear(mm, addr, pte);
5256 }
5357
54−static inline void page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd)
58+static inline void page_table_check_pmd_clear(struct mm_struct *mm,
59+ unsigned long addr, pmd_t pmd)
5560 {
5661 if (static_branch_likely(&page_table_check_disabled))
5762 return;
5863
59− __page_table_check_pmd_clear(mm, pmd);
64+ __page_table_check_pmd_clear(mm, addr, pmd);
6065 }
6166
62−static inline void page_table_check_pud_clear(struct mm_struct *mm, pud_t pud)
67+static inline void page_table_check_pud_clear(struct mm_struct *mm,
68+ unsigned long addr, pud_t pud)
6369 {
6470 if (static_branch_likely(&page_table_check_disabled))
6571 return;
6672
67− __page_table_check_pud_clear(mm, pud);
73+ __page_table_check_pud_clear(mm, addr, pud);
6874 }
6975
7076 static inline void page_table_check_ptes_set(struct mm_struct *mm,
71− pte_t *ptep, pte_t pte, unsigned int nr)
77+ unsigned long addr, pte_t *ptep,
78+ pte_t pte, unsigned int nr)
7279 {
7380 if (static_branch_likely(&page_table_check_disabled))
7481 return;
7582
76− __page_table_check_ptes_set(mm, ptep, pte, nr);
83+ __page_table_check_ptes_set(mm, addr, ptep, pte, nr);
7784 }
7885
7986 static inline void page_table_check_pmds_set(struct mm_struct *mm,
80− pmd_t *pmdp, pmd_t pmd, unsigned int nr)
87+ unsigned long addr, pmd_t *pmdp, pmd_t pmd, unsigned int nr)
8188 {
8289 if (static_branch_likely(&page_table_check_disabled))
8390 return;
8491
85− __page_table_check_pmds_set(mm, pmdp, pmd, nr);
92+ __page_table_check_pmds_set(mm, addr, pmdp, pmd, nr);
8693 }
8794
8895 static inline void page_table_check_puds_set(struct mm_struct *mm,
89− pud_t *pudp, pud_t pud, unsigned int nr)
96+ unsigned long addr, pud_t *pudp, pud_t pud, unsigned int nr)
9097 {
9198 if (static_branch_likely(&page_table_check_disabled))
9299 return;
93100
94− __page_table_check_puds_set(mm, pudp, pud, nr);
101+ __page_table_check_puds_set(mm, addr, pudp, pud, nr);
95102 }
96103
97104 static inline void page_table_check_pte_clear_range(struct mm_struct *mm,
@@ -114,30 +121,34 @@ static inline void page_table_check_free(struct page *page, unsigned int order) · 16 unmodified lines
114121 {
115122 }
116123
117−static inline void page_table_check_pte_clear(struct mm_struct *mm, pte_t pte)
124+static inline void page_table_check_pte_clear(struct mm_struct *mm,
125+ unsigned long addr, pte_t pte)
118126 {
119127 }
120128
121−static inline void page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd)
129+static inline void page_table_check_pmd_clear(struct mm_struct *mm,
130+ unsigned long addr, pmd_t pmd)
122131 {
123132 }
124133
125−static inline void page_table_check_pud_clear(struct mm_struct *mm, pud_t pud)
134+static inline void page_table_check_pud_clear(struct mm_struct *mm,
135+ unsigned long addr, pud_t pud)
126136 {
127137 }
128138
129139 static inline void page_table_check_ptes_set(struct mm_struct *mm,
130− pte_t *ptep, pte_t pte, unsigned int nr)
140+ unsigned long addr, pte_t *ptep,
141+ pte_t pte, unsigned int nr)
131142 {
132143 }
133144
134145 static inline void page_table_check_pmds_set(struct mm_struct *mm,
135− pmd_t *pmdp, pmd_t pmd, unsigned int nr)
146+ unsigned long addr, pmd_t *pmdp, pmd_t pmd, unsigned int nr)
136147 {
137148 }
138149
139150 static inline void page_table_check_puds_set(struct mm_struct *mm,
140− pud_t *pudp, pud_t pud, unsigned int nr)
151+ unsigned long addr, pud_t *pudp, pud_t pud, unsigned int nr)
141152 {
142153 }
143154
@@ -149,7 +160,7 @@ static inline void page_table_check_pte_clear_range(struct mm_struct *mm, · 5 unmodified lines
149160
150161 #endif /* CONFIG_PAGE_TABLE_CHECK */
151162
152−#define page_table_check_pmd_set(mm, pmdp, pmd) page_table_check_pmds_set(mm, pmdp, pmd, 1)
153−#define page_table_check_pud_set(mm, pudp, pud) page_table_check_puds_set(mm, pudp, pud, 1)
163+#define page_table_check_pmd_set(mm, addr, pmdp, pmd) page_table_check_pmds_set(mm, addr, pmdp, pmd, 1)
164+#define page_table_check_pud_set(mm, addr, pudp, pud) page_table_check_puds_set(mm, addr, pudp, pud, 1)
154165
155166 #endif /* __LINUX_PAGE_TABLE_CHECK_H */
include/linux/pgtable.h+154−14
@@ -225,16 +225,156 @@ static inline int pmd_dirty(pmd_t pmd) · 224 unmodified lines
225225 * up to date.
226226 *
227227 * In the general case, no lock is guaranteed to be held between entry and exit
228− * of the lazy mode. So the implementation must assume preemption may be enabled
229− * and cpu migration is possible; it must take steps to be robust against this.
230− * (In practice, for user PTE updates, the appropriate page table lock(s) are
231− * held, but for kernel PTE updates, no lock is held). Nesting is not permitted
232− * and the mode cannot be used in interrupt context.
228+ * of the lazy mode. (In practice, for user PTE updates, the appropriate page
229+ * table lock(s) are held, but for kernel PTE updates, no lock is held).
230+ * The implementation must therefore assume preemption may be enabled upon
231+ * entry to the mode and cpu migration is possible; it must take steps to be
232+ * robust against this. An implementation may handle this by disabling
233+ * preemption, as a consequence generic code may not sleep while the lazy MMU
234+ * mode is active.
235+ *
236+ * The mode is disabled in interrupt context and calls to the lazy_mmu API have
237+ * no effect.
238+ *
239+ * The lazy MMU mode is enabled for a given block of code using:
240+ *
241+ * lazy_mmu_mode_enable();
242+ * <code>
243+ * lazy_mmu_mode_disable();
244+ *
245+ * Nesting is permitted: <code> may itself use an enable()/disable() pair.
246+ * A nested call to enable() has no functional effect; however disable() causes
247+ * any batched architectural state to be flushed regardless of nesting. After a
248+ * call to disable(), the caller can therefore rely on all previous page table
249+ * modifications to have taken effect, but the lazy MMU mode may still be
250+ * enabled.
251+ *
252+ * In certain cases, it may be desirable to temporarily pause the lazy MMU mode.
253+ * This can be done using:
254+ *
255+ * lazy_mmu_mode_pause();
256+ * <code>
257+ * lazy_mmu_mode_resume();
258+ *
259+ * pause() ensures that the mode is exited regardless of the nesting level;
260+ * resume() re-enters the mode at the same nesting level. Any call to the
261+ * lazy_mmu_mode_* API between those two calls has no effect. In particular,
262+ * this means that pause()/resume() pairs may nest.
263+ *
264+ * is_lazy_mmu_mode_active() can be used to check whether the lazy MMU mode is
265+ * currently enabled.
233266 */
234−#ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
235−static inline void arch_enter_lazy_mmu_mode(void) {}
236−static inline void arch_leave_lazy_mmu_mode(void) {}
237−static inline void arch_flush_lazy_mmu_mode(void) {}
267+#ifdef CONFIG_ARCH_HAS_LAZY_MMU_MODE
268+/**
269+ * lazy_mmu_mode_enable() - Enable the lazy MMU mode.
270+ *
271+ * Enters a new lazy MMU mode section; if the mode was not already enabled,
272+ * enables it and calls arch_enter_lazy_mmu_mode().
273+ *
274+ * Must be paired with a call to lazy_mmu_mode_disable().
275+ *
276+ * Has no effect if called:
277+ * - While paused - see lazy_mmu_mode_pause()
278+ * - In interrupt context
279+ */
280+static inline void lazy_mmu_mode_enable(void)
281+{
282+ struct lazy_mmu_state *state = &current->lazy_mmu_state;
283+
284+ if (in_interrupt() || state->pause_count > 0)
285+ return;
286+
287+ VM_WARN_ON_ONCE(state->enable_count == U8_MAX);
288+
289+ if (state->enable_count++ == 0)
290+ arch_enter_lazy_mmu_mode();
291+}
292+
293+/**
294+ * lazy_mmu_mode_disable() - Disable the lazy MMU mode.
295+ *
296+ * Exits the current lazy MMU mode section. If it is the outermost section,
297+ * disables the mode and calls arch_leave_lazy_mmu_mode(). Otherwise (nested
298+ * section), calls arch_flush_lazy_mmu_mode().
299+ *
300+ * Must match a call to lazy_mmu_mode_enable().
301+ *
302+ * Has no effect if called:
303+ * - While paused - see lazy_mmu_mode_pause()
304+ * - In interrupt context
305+ */
306+static inline void lazy_mmu_mode_disable(void)
307+{
308+ struct lazy_mmu_state *state = &current->lazy_mmu_state;
309+
310+ if (in_interrupt() || state->pause_count > 0)
311+ return;
312+
313+ VM_WARN_ON_ONCE(state->enable_count == 0);
314+
315+ if (--state->enable_count == 0)
316+ arch_leave_lazy_mmu_mode();
317+ else /* Exiting a nested section */
318+ arch_flush_lazy_mmu_mode();
319+
320+}
321+
322+/**
323+ * lazy_mmu_mode_pause() - Pause the lazy MMU mode.
324+ *
325+ * Pauses the lazy MMU mode; if it is currently active, disables it and calls
326+ * arch_leave_lazy_mmu_mode().
327+ *
328+ * Must be paired with a call to lazy_mmu_mode_resume(). Calls to the
329+ * lazy_mmu_mode_* API have no effect until the matching resume() call.
330+ *
331+ * Has no effect if called:
332+ * - While paused (inside another pause()/resume() pair)
333+ * - In interrupt context
334+ */
335+static inline void lazy_mmu_mode_pause(void)
336+{
337+ struct lazy_mmu_state *state = &current->lazy_mmu_state;
338+
339+ if (in_interrupt())
340+ return;
341+
342+ VM_WARN_ON_ONCE(state->pause_count == U8_MAX);
343+
344+ if (state->pause_count++ == 0 && state->enable_count > 0)
345+ arch_leave_lazy_mmu_mode();
346+}
347+
348+/**
349+ * lazy_mmu_mode_resume() - Resume the lazy MMU mode.
350+ *
351+ * Resumes the lazy MMU mode; if it was active at the point where the matching
352+ * call to lazy_mmu_mode_pause() was made, re-enables it and calls
353+ * arch_enter_lazy_mmu_mode().
354+ *
355+ * Must match a call to lazy_mmu_mode_pause().
356+ *
357+ * Has no effect if called:
358+ * - While paused (inside another pause()/resume() pair)
359+ * - In interrupt context
360+ */
361+static inline void lazy_mmu_mode_resume(void)
362+{
363+ struct lazy_mmu_state *state = &current->lazy_mmu_state;
364+
365+ if (in_interrupt())
366+ return;
367+
368+ VM_WARN_ON_ONCE(state->pause_count == 0);
369+
370+ if (--state->pause_count == 0 && state->enable_count > 0)
371+ arch_enter_lazy_mmu_mode();
372+}
373+#else
374+static inline void lazy_mmu_mode_enable(void) {}
375+static inline void lazy_mmu_mode_disable(void) {}
376+static inline void lazy_mmu_mode_pause(void) {}
377+static inline void lazy_mmu_mode_resume(void) {}
238378 #endif
239379
240380 #ifndef pte_batch_hint
@@ -289,7 +429,7 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) · 48 unmodified lines
289429 static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
290430 pte_t *ptep, pte_t pte, unsigned int nr)
291431 {
292− page_table_check_ptes_set(mm, ptep, pte, nr);
432+ page_table_check_ptes_set(mm, addr, ptep, pte, nr);
293433
294434 for (;;) {
295435 set_pte(ptep, pte);
@@ -494,7 +634,7 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, · 198 unmodified lines
494634 {
495635 pte_t pte = ptep_get(ptep);
496636 pte_clear(mm, address, ptep);
497− page_table_check_pte_clear(mm, pte);
637+ page_table_check_pte_clear(mm, address, pte);
498638 return pte;
499639 }
500640 #endif
@@ -553,7 +693,7 @@ static inline void ptep_clear(struct mm_struct *mm, unsigned long addr, · 52 unmodified lines
553693 * No need for ptep_get_and_clear(): page table check doesn't care about
554694 * any bits that could have been set by HW concurrently.
555695 */
556− page_table_check_pte_clear(mm, pte);
696+ page_table_check_pte_clear(mm, addr, pte);
557697 }
558698
559699 #ifdef CONFIG_GUP_GET_PXX_LOW_HIGH
@@ -648,7 +788,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, · 88 unmodified lines
648788 pmd_t pmd = *pmdp;
649789
650790 pmd_clear(pmdp);
651− page_table_check_pmd_clear(mm, pmd);
791+ page_table_check_pmd_clear(mm, address, pmd);
652792
653793 return pmd;
654794 }
@@ -661,7 +801,7 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, · 6 unmodified lines
661801 pud_t pud = *pudp;
662802
663803 pud_clear(pudp);
664− page_table_check_pud_clear(mm, pud);
804+ page_table_check_pud_clear(mm, address, pud);
665805
666806 return pud;
667807 }
include/linux/rmap.h+3−73
@@ -92,6 +92,7 @@ struct anon_vma_chain { · 91 unmodified lines
9292 };
9393
9494 enum ttu_flags {
95+ TTU_USE_SHARED_ZEROPAGE = 0x2, /* for unused pages of large folios */
9596 TTU_SPLIT_HUGE_PMD = 0x4, /* split huge PMD if any */
9697 TTU_IGNORE_MLOCK = 0x8, /* ignore mlock */
9798 TTU_SYNC = 0x10, /* avoid racy checks with PVMW_SYNC */
@@ -104,75 +105,8 @@ enum ttu_flags { · 6 unmodified lines
104105 };
105106
106107 #ifdef CONFIG_MMU
107−static inline void get_anon_vma(struct anon_vma *anon_vma)
108−{
109− atomic_inc(&anon_vma->refcount);
110−}
111−
112−void __put_anon_vma(struct anon_vma *anon_vma);
113−
114−static inline void put_anon_vma(struct anon_vma *anon_vma)
115−{
116− if (atomic_dec_and_test(&anon_vma->refcount))
117− __put_anon_vma(anon_vma);
118−}
119−
120−static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
121−{
122− down_write(&anon_vma->root->rwsem);
123−}
124−
125−static inline int anon_vma_trylock_write(struct anon_vma *anon_vma)
126−{
127− return down_write_trylock(&anon_vma->root->rwsem);
128−}
129−
130−static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
131−{
132− up_write(&anon_vma->root->rwsem);
133−}
134−
135−static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
136−{
137− down_read(&anon_vma->root->rwsem);
138−}
139−
140−static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
141−{
142− return down_read_trylock(&anon_vma->root->rwsem);
143−}
144−
145−static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
146−{
147− up_read(&anon_vma->root->rwsem);
148−}
149108
150−
151−/*
152− * anon_vma helper functions.
153− */
154109 void anon_vma_init(void); /* create anon_vma_cachep */
155−int __anon_vma_prepare(struct vm_area_struct *);
156−void unlink_anon_vmas(struct vm_area_struct *);
157−int anon_vma_clone(struct vm_area_struct *, struct vm_area_struct *);
158−int anon_vma_fork(struct vm_area_struct *, struct vm_area_struct *);
159−
160−static inline int anon_vma_prepare(struct vm_area_struct *vma)
161−{
162− if (likely(vma->anon_vma))
163− return 0;
164−
165− return __anon_vma_prepare(vma);
166−}
167−
168−static inline void anon_vma_merge(struct vm_area_struct *vma,
169− struct vm_area_struct *next)
170−{
171− VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
172− unlink_anon_vmas(next);
173−}
174−
175−struct anon_vma *folio_get_anon_vma(const struct folio *folio);
176110
177111 #ifdef CONFIG_MM_ID
178112 static __always_inline void folio_lock_large_mapcount(struct folio *folio)
@@ -1000,12 +934,8 @@ int mapping_wrprotect_range(struct address_space *mapping, pgoff_t pgoff, · 821 unmodified lines
1000934 int pfn_mkclean_range(unsigned long pfn, unsigned long nr_pages, pgoff_t pgoff,
1001935 struct vm_area_struct *vma);
1002936
1003−enum rmp_flags {
1004− RMP_LOCKED = 1 << 0,
1005− RMP_USE_SHARED_ZEROPAGE = 1 << 1,
1006−};
1007−
1008−void remove_migration_ptes(struct folio *src, struct folio *dst, int flags);
937+void remove_migration_ptes(struct folio *src, struct folio *dst,
938+ enum ttu_flags flags);
1009939
1010940 /*
1011941 * rmap_walk_control: To control rmap traversing for specific needs
include/linux/sched.h+45−0
@@ -1410,6 +1410,10 @@ struct task_struct { · 1409 unmodified lines
14101410
14111411 struct page_frag task_frag;
14121412
1413+#ifdef CONFIG_ARCH_HAS_LAZY_MMU_MODE
1414+ struct lazy_mmu_state lazy_mmu_state;
1415+#endif
1416+
14131417 #ifdef CONFIG_TASK_DELAY_ACCT
14141418 struct task_delay_info *delays;
14151419 #endif
@@ -1693,6 +1697,47 @@ static inline char task_state_to_char(struct task_struct *tsk) · 277 unmodified lines
16931697 return task_index_to_char(task_state_index(tsk));
16941698 }
16951699
1700+#ifdef CONFIG_ARCH_HAS_LAZY_MMU_MODE
1701+/**
1702+ * __task_lazy_mmu_mode_active() - Test the lazy MMU mode state for a task.
1703+ * @tsk: The task to check.
1704+ *
1705+ * Test whether @tsk has its lazy MMU mode state set to active (i.e. enabled
1706+ * and not paused).
1707+ *
1708+ * This function only considers the state saved in task_struct; to test whether
1709+ * current actually is in lazy MMU mode, is_lazy_mmu_mode_active() should be
1710+ * used instead.
1711+ *
1712+ * This function is intended for architectures that implement the lazy MMU
1713+ * mode; it must not be called from generic code.
1714+ */
1715+static inline bool __task_lazy_mmu_mode_active(struct task_struct *tsk)
1716+{
1717+ struct lazy_mmu_state *state = &tsk->lazy_mmu_state;
1718+
1719+ return state->enable_count > 0 && state->pause_count == 0;
1720+}
1721+
1722+/**
1723+ * is_lazy_mmu_mode_active() - Test whether we are currently in lazy MMU mode.
1724+ *
1725+ * Test whether the current context is in lazy MMU mode. This is true if both:
1726+ * 1. We are not in interrupt context
1727+ * 2. Lazy MMU mode is active for the current task
1728+ *
1729+ * This function is intended for architectures that implement the lazy MMU
1730+ * mode; it must not be called from generic code.
1731+ */
1732+static inline bool is_lazy_mmu_mode_active(void)
1733+{
1734+ if (in_interrupt())
1735+ return false;
1736+
1737+ return __task_lazy_mmu_mode_active(current);
1738+}
1739+#endif
1740+
16961741 extern struct pid *cad_pid;
16971742
16981743 /*
include/linux/swap.h+25−46
@@ -224,13 +224,11 @@ enum { · 223 unmodified lines
224224 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
225225
226226 /* Bit flag in swap_map */
227−#define SWAP_HAS_CACHE 0x40 /* Flag page is cached, in first swap_map */
228227 #define COUNT_CONTINUED 0x80 /* Flag swap_map continuation for full count */
229228
230229 /* Special value in first swap_map */
231230 #define SWAP_MAP_MAX 0x3e /* Max count */
232231 #define SWAP_MAP_BAD 0x3f /* Note page is bad */
233−#define SWAP_MAP_SHMEM 0xbf /* Owned by shmem/tmpfs */
234232
235233 /* Special value in each swap_map continuation */
236234 #define SWAP_CONT_MAX 0x7f /* Max count */
@@ -453,16 +451,7 @@ static inline long get_nr_swap_pages(void) · 216 unmodified lines
453451 }
454452
455453 extern void si_swapinfo(struct sysinfo *);
456−int folio_alloc_swap(struct folio *folio);
457−bool folio_free_swap(struct folio *folio);
458−void put_swap_folio(struct folio *folio, swp_entry_t entry);
459−extern swp_entry_t get_swap_page_of_type(int);
460454 extern int add_swap_count_continuation(swp_entry_t, gfp_t);
461−extern void swap_shmem_alloc(swp_entry_t, int);
462−extern int swap_duplicate(swp_entry_t);
463−extern int swapcache_prepare(swp_entry_t entry, int nr);
464−extern void swap_free_nr(swp_entry_t entry, int nr_pages);
465−extern void free_swap_and_cache_nr(swp_entry_t entry, int nr);
466455 int swap_type_of(dev_t device, sector_t offset);
467456 int find_first_swap(dev_t *device);
468457 extern unsigned int count_swap_pages(int, int);
@@ -474,6 +463,29 @@ struct backing_dev_info; · 5 unmodified lines
474463 extern struct swap_info_struct *get_swap_device(swp_entry_t entry);
475464 sector_t swap_folio_sector(struct folio *folio);
476465
466+/*
467+ * If there is an existing swap slot reference (swap entry) and the caller
468+ * guarantees that there is no race modification of it (e.g., PTL
469+ * protecting the swap entry in page table; shmem's cmpxchg protects t
470+ * he swap entry in shmem mapping), these two helpers below can be used
471+ * to put/dup the entries directly.
472+ *
473+ * All entries must be allocated by folio_alloc_swap(). And they must have
474+ * a swap count > 1. See comments of folio_*_swap helpers for more info.
475+ */
476+int swap_dup_entry_direct(swp_entry_t entry);
477+void swap_put_entries_direct(swp_entry_t entry, int nr);
478+
479+/*
480+ * folio_free_swap tries to free the swap entries pinned by a swap cache
481+ * folio, it has to be here to be called by other components.
482+ */
483+bool folio_free_swap(struct folio *folio);
484+
485+/* Allocate / free (hibernation) exclusive entries */
486+swp_entry_t swap_alloc_hibernation_slot(int type);
487+void swap_free_hibernation_slot(swp_entry_t entry);
488+
477489 static inline void put_swap_device(struct swap_info_struct *si)
478490 {
479491 percpu_ref_put(&si->users);
@@ -501,10 +513,6 @@ static inline void put_swap_device(struct swap_info_struct *si) · 21 unmodified lines
501513 #define free_pages_and_swap_cache(pages, nr) \
502514 release_pages((pages), (nr));
503515
504−static inline void free_swap_and_cache_nr(swp_entry_t entry, int nr)
505−{
506−}
507−
508516 static inline void free_swap_cache(struct folio *folio)
509517 {
510518 }
@@ -514,25 +522,12 @@ static inline int add_swap_count_continuation(swp_entry_t swp, gfp_t gfp_mask) · 3 unmodified lines
514522 return 0;
515523 }
516524
517−static inline void swap_shmem_alloc(swp_entry_t swp, int nr)
518−{
519−}
520−
521−static inline int swap_duplicate(swp_entry_t swp)
522−{
523− return 0;
524−}
525−
526−static inline int swapcache_prepare(swp_entry_t swp, int nr)
525+static inline int swap_dup_entry_direct(swp_entry_t ent)
527526 {
528527 return 0;
529528 }
530529
531−static inline void swap_free_nr(swp_entry_t entry, int nr_pages)
532−{
533−}
534−
535−static inline void put_swap_folio(struct folio *folio, swp_entry_t swp)
530+static inline void swap_put_entries_direct(swp_entry_t ent, int nr)
536531 {
537532 }
538533
@@ -551,11 +546,6 @@ static inline int swp_swapcount(swp_entry_t entry) · 12 unmodified lines
551546 return 0;
552547 }
553548
554−static inline int folio_alloc_swap(struct folio *folio)
555−{
556− return -EINVAL;
557−}
558−
559549 static inline bool folio_free_swap(struct folio *folio)
560550 {
561551 return false;
@@ -568,17 +558,6 @@ static inline int add_swap_extent(struct swap_info_struct *sis, · 6 unmodified lines
568558 return -EINVAL;
569559 }
570560 #endif /* CONFIG_SWAP */
571−
572−static inline void free_swap_and_cache(swp_entry_t entry)
573−{
574− free_swap_and_cache_nr(entry, 1);
575−}
576−
577−static inline void swap_free(swp_entry_t entry)
578−{
579− swap_free_nr(entry, 1);
580−}
581−
582561 #ifdef CONFIG_MEMCG
583562 static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
584563 {
include/linux/vm_event_item.h+4−4
@@ -122,13 +122,13 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, · 121 unmodified lines
122122 THP_SWPOUT,
123123 THP_SWPOUT_FALLBACK,
124124 #endif
125−#ifdef CONFIG_MEMORY_BALLOON
125+#ifdef CONFIG_BALLOON
126126 BALLOON_INFLATE,
127127 BALLOON_DEFLATE,
128−#ifdef CONFIG_BALLOON_COMPACTION
128+#ifdef CONFIG_BALLOON_MIGRATION
129129 BALLOON_MIGRATE,
130−#endif
131−#endif
130+#endif /* CONFIG_BALLOON_MIGRATION */
131+#endif /* CONFIG_BALLOON */
132132 #ifdef CONFIG_DEBUG_TLBFLUSH
133133 NR_TLB_REMOTE_FLUSH, /* cpu tried to flush others' tlbs */
134134 NR_TLB_REMOTE_FLUSH_RECEIVED,/* cpu received ipi for flush */
include/linux/vmstat.h+0−6
@@ -286,10 +286,8 @@ void mod_node_page_state(struct pglist_data *, enum node_stat_item, long); · 285 unmodified lines
286286 void inc_node_page_state(struct page *, enum node_stat_item);
287287 void dec_node_page_state(struct page *, enum node_stat_item);
288288
289−extern void inc_node_state(struct pglist_data *, enum node_stat_item);
290289 extern void __inc_zone_state(struct zone *, enum zone_stat_item);
291290 extern void __inc_node_state(struct pglist_data *, enum node_stat_item);
292−extern void dec_zone_state(struct zone *, enum zone_stat_item);
293291 extern void __dec_zone_state(struct zone *, enum zone_stat_item);
294292 extern void __dec_node_state(struct pglist_data *, enum node_stat_item);
295293
@@ -395,10 +393,6 @@ static inline void __dec_node_page_state(struct page *page, · 99 unmodified lines
395393 #define dec_node_page_state __dec_node_page_state
396394 #define mod_node_page_state __mod_node_page_state
397395
398−#define inc_zone_state __inc_zone_state
399−#define inc_node_state __inc_node_state
400−#define dec_zone_state __dec_zone_state
401−
402396 #define set_pgdat_percpu_threshold(pgdat, callback) { }
403397
404398 static inline void refresh_zone_stat_thresholds(void) { }
include/linux/writeback.h+0−4
@@ -328,9 +328,6 @@ struct dirty_throttle_control { · 327 unmodified lines
328328 bool dirty_exceeded;
329329 };
330330
331−void laptop_io_completion(struct backing_dev_info *info);
332−void laptop_sync_completion(void);
333−void laptop_mode_timer_fn(struct timer_list *t);
334331 bool node_dirty_ok(struct pglist_data *pgdat);
335332 int wb_domain_init(struct wb_domain *dom, gfp_t gfp);
336333 #ifdef CONFIG_CGROUP_WRITEBACK
@@ -342,7 +339,6 @@ extern struct wb_domain global_wb_domain; · 5 unmodified lines
342339 /* These are exported to sysctl. */
343340 extern unsigned int dirty_writeback_interval;
344341 extern unsigned int dirty_expire_interval;
345−extern int laptop_mode;
346342
347343 void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty);
348344 unsigned long wb_calc_thresh(struct bdi_writeback *wb, unsigned long thresh);
include/linux/zsmalloc.h+6−2
@@ -22,6 +22,7 @@ struct zs_pool_stats { · 21 unmodified lines
2222 };
2323
2424 struct zs_pool;
25+struct scatterlist;
2526
2627 struct zs_pool *zs_create_pool(const char *name);
2728 void zs_destroy_pool(struct zs_pool *pool);
@@ -40,9 +41,12 @@ unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size); · 12 unmodified lines
4041 void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats);
4142
4243 void *zs_obj_read_begin(struct zs_pool *pool, unsigned long handle,
43− void *local_copy);
44+ size_t mem_len, void *local_copy);
4445 void zs_obj_read_end(struct zs_pool *pool, unsigned long handle,
45− void *handle_mem);
46+ size_t mem_len, void *handle_mem);
47+void zs_obj_read_sg_begin(struct zs_pool *pool, unsigned long handle,
48+ struct scatterlist *sg, size_t mem_len);
49+void zs_obj_read_sg_end(struct zs_pool *pool, unsigned long handle);
4650 void zs_obj_write(struct zs_pool *pool, unsigned long handle,
4751 void *handle_mem, size_t mem_len);
4852
include/trace/events/damon.h+41−0
@@ -9,6 +9,47 @@ · 8 unmodified lines
99 #include <linux/types.h>
1010 #include <linux/tracepoint.h>
1111
12+TRACE_EVENT(damos_stat_after_apply_interval,
13+
14+ TP_PROTO(unsigned int context_idx, unsigned int scheme_idx,
15+ struct damos_stat *stat),
16+
17+ TP_ARGS(context_idx, scheme_idx, stat),
18+
19+ TP_STRUCT__entry(
20+ __field(unsigned int, context_idx)
21+ __field(unsigned int, scheme_idx)
22+ __field(unsigned long, nr_tried)
23+ __field(unsigned long, sz_tried)
24+ __field(unsigned long, nr_applied)
25+ __field(unsigned long, sz_applied)
26+ __field(unsigned long, sz_ops_filter_passed)
27+ __field(unsigned long, qt_exceeds)
28+ __field(unsigned long, nr_snapshots)
29+ ),
30+
31+ TP_fast_assign(
32+ __entry->context_idx = context_idx;
33+ __entry->scheme_idx = scheme_idx;
34+ __entry->nr_tried = stat->nr_tried;
35+ __entry->sz_tried = stat->sz_tried;
36+ __entry->nr_applied = stat->nr_applied;
37+ __entry->sz_applied = stat->sz_applied;
38+ __entry->sz_ops_filter_passed = stat->sz_ops_filter_passed;
39+ __entry->qt_exceeds = stat->qt_exceeds;
40+ __entry->nr_snapshots = stat->nr_snapshots;
41+ ),
42+
43+ TP_printk("ctx_idx=%u scheme_idx=%u nr_tried=%lu sz_tried=%lu "
44+ "nr_applied=%lu sz_tried=%lu sz_ops_filter_passed=%lu "
45+ "qt_exceeds=%lu nr_snapshots=%lu",
46+ __entry->context_idx, __entry->scheme_idx,
47+ __entry->nr_tried, __entry->sz_tried,
48+ __entry->nr_applied, __entry->sz_applied,
49+ __entry->sz_ops_filter_passed, __entry->qt_exceeds,
50+ __entry->nr_snapshots)
51+);
52+
1253 TRACE_EVENT(damos_esz,
1354
1455 TP_PROTO(unsigned int context_idx, unsigned int scheme_idx,
include/trace/events/huge_memory.h+2−1
@@ -37,7 +37,8 @@ · 36 unmodified lines
3737 EM( SCAN_PAGE_HAS_PRIVATE, "page_has_private") \
3838 EM( SCAN_STORE_FAILED, "store_failed") \
3939 EM( SCAN_COPY_MC, "copy_poisoned_page") \
40− EMe(SCAN_PAGE_FILLED, "page_filled")
40+ EM( SCAN_PAGE_FILLED, "page_filled") \
41+ EMe(SCAN_PAGE_DIRTY_OR_WRITEBACK, "page_dirty_or_writeback")
4142
4243 #undef EM
4344 #undef EMe
include/trace/events/vmscan.h+51−0
@@ -40,6 +40,16 @@ · 39 unmodified lines
4040 {_VMSCAN_THROTTLE_CONGESTED, "VMSCAN_THROTTLE_CONGESTED"} \
4141 ) : "VMSCAN_THROTTLE_NONE"
4242
43+TRACE_DEFINE_ENUM(KSWAPD_CLEAR_HOPELESS_OTHER);
44+TRACE_DEFINE_ENUM(KSWAPD_CLEAR_HOPELESS_KSWAPD);
45+TRACE_DEFINE_ENUM(KSWAPD_CLEAR_HOPELESS_DIRECT);
46+TRACE_DEFINE_ENUM(KSWAPD_CLEAR_HOPELESS_PCP);
47+
48+#define kswapd_clear_hopeless_reason_ops \
49+ {KSWAPD_CLEAR_HOPELESS_KSWAPD, "KSWAPD"}, \
50+ {KSWAPD_CLEAR_HOPELESS_DIRECT, "DIRECT"}, \
51+ {KSWAPD_CLEAR_HOPELESS_PCP, "PCP"}, \
52+ {KSWAPD_CLEAR_HOPELESS_OTHER, "OTHER"}
4353
4454 #define trace_reclaim_flags(file) ( \
4555 (file ? RECLAIM_WB_FILE : RECLAIM_WB_ANON) | \
@@ -535,6 +545,47 @@ TRACE_EVENT(mm_vmscan_throttled, · 489 unmodified lines
535545 __entry->usec_delayed,
536546 show_throttle_flags(__entry->reason))
537547 );
548+
549+TRACE_EVENT(mm_vmscan_kswapd_reclaim_fail,
550+
551+ TP_PROTO(int nid, int failures),
552+
553+ TP_ARGS(nid, failures),
554+
555+ TP_STRUCT__entry(
556+ __field(int, nid)
557+ __field(int, failures)
558+ ),
559+
560+ TP_fast_assign(
561+ __entry->nid = nid;
562+ __entry->failures = failures;
563+ ),
564+
565+ TP_printk("nid=%d failures=%d",
566+ __entry->nid, __entry->failures)
567+);
568+
569+TRACE_EVENT(mm_vmscan_kswapd_clear_hopeless,
570+
571+ TP_PROTO(int nid, int reason),
572+
573+ TP_ARGS(nid, reason),
574+
575+ TP_STRUCT__entry(
576+ __field(int, nid)
577+ __field(int, reason)
578+ ),
579+
580+ TP_fast_assign(
581+ __entry->nid = nid;
582+ __entry->reason = reason;
583+ ),
584+
585+ TP_printk("nid=%d reason=%s",
586+ __entry->nid,
587+ __print_symbolic(__entry->reason, kswapd_clear_hopeless_reason_ops))
588+);
538589 #endif /* _TRACE_VMSCAN_H */
539590
540591 /* This part must be outside protection */
include/trace/events/writeback.h+0−1
@@ -42,7 +42,6 @@ · 41 unmodified lines
4242 EM( WB_REASON_VMSCAN, "vmscan") \
4343 EM( WB_REASON_SYNC, "sync") \
4444 EM( WB_REASON_PERIODIC, "periodic") \
45− EM( WB_REASON_LAPTOP_TIMER, "laptop_timer") \
4645 EM( WB_REASON_FS_FREE_SPACE, "fs_free_space") \
4746 EM( WB_REASON_FORKER_THREAD, "forker_thread") \
4847 EMe(WB_REASON_FOREIGN_FLUSH, "foreign_flush")
include/uapi/linux/mempolicy.h+3−0
@@ -39,6 +39,9 @@ enum { · 38 unmodified lines
3939 #define MPOL_MODE_FLAGS \
4040 (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES | MPOL_F_NUMA_BALANCING)
4141
42+/* Whether the nodemask is specified by users */
43+#define MPOL_USER_NODEMASK_FLAGS (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES)
44+
4245 /* Flags for get_mempolicy */
4346 #define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
4447 #define MPOL_F_ADDR (1<<1) /* look up vma using address */
include/uapi/linux/sysctl.h+1−1
@@ -182,7 +182,7 @@ enum · 181 unmodified lines
182182 VM_LOWMEM_RESERVE_RATIO=20,/* reservation ratio for lower memory zones */
183183 VM_MIN_FREE_KBYTES=21, /* Minimum free kilobytes to maintain */
184184 VM_MAX_MAP_COUNT=22, /* int: Maximum number of mmaps/address-space */
185− VM_LAPTOP_MODE=23, /* vm laptop mode */
185+
186186 VM_BLOCK_DUMP=24, /* block dump mode */
187187 VM_HUGETLB_GROUP=25, /* permitted hugetlb group */
188188 VM_VFS_CACHE_PRESSURE=26, /* dcache/icache reclaim pressure */
init/main.c+1−0
@@ -1025,6 +1025,7 @@ void start_kernel(void) · 1024 unmodified lines
10251025 page_address_init();
10261026 pr_notice("%s", linux_banner);
10271027 setup_arch(&command_line);
1028+ mm_core_init_early();
10281029 /* Static keys and static calls are needed by LSMs */
10291030 jump_label_init();
10301031 static_call_init();
kernel/cgroup/cpuset.c+3−4
@@ -458,9 +458,8 @@ static void guarantee_active_cpus(struct task_struct *tsk, · 457 unmodified lines
458458 */
459459 static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
460460 {
461− while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
461+ while (!nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]))
462462 cs = parent_cs(cs);
463− nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
464463 }
465464
466465 /**
@@ -2622,13 +2621,13 @@ static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) · 2155 unmodified lines
26222621 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
26232622 struct cpuset *parent = parent_cs(cp);
26242623
2625− nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
2624+ bool has_mems = nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
26262625
26272626 /*
26282627 * If it becomes empty, inherit the effective mask of the
26292628 * parent, which is guaranteed to have some MEMs.
26302629 */
2631− if (is_in_v2_mode() && nodes_empty(*new_mems))
2630+ if (is_in_v2_mode() && !has_mems)
26322631 *new_mems = parent->effective_mems;
26332632
26342633 /* Skip the whole subtree if the nodemask remains the same. */
kernel/power/swap.c+6−4
@@ -174,10 +174,10 @@ sector_t alloc_swapdev_block(int swap) · 173 unmodified lines
174174 * Allocate a swap page and register that it has been allocated, so that
175175 * it can be freed in case of an error.
176176 */
177− offset = swp_offset(get_swap_page_of_type(swap));
177+ offset = swp_offset(swap_alloc_hibernation_slot(swap));
178178 if (offset) {
179179 if (swsusp_extents_insert(offset))
180− swap_free(swp_entry(swap, offset));
180+ swap_free_hibernation_slot(swp_entry(swap, offset));
181181 else
182182 return swapdev_block(swap, offset);
183183 }
@@ -186,6 +186,7 @@ sector_t alloc_swapdev_block(int swap) · 2 unmodified lines
186186
187187 void free_all_swap_pages(int swap)
188188 {
189+ unsigned long offset;
189190 struct rb_node *node;
190191
191192 /*
@@ -197,8 +198,9 @@ void free_all_swap_pages(int swap) · 5 unmodified lines
197198
198199 ext = rb_entry(node, struct swsusp_extent, node);
199200 rb_erase(node, &swsusp_extents);
200− swap_free_nr(swp_entry(swap, ext->start),
201− ext->end - ext->start + 1);
201+
202+ for (offset = ext->start; offset <= ext->end; offset++)
203+ swap_free_hibernation_slot(swp_entry(swap, offset));
202204
203205 kfree(ext);
204206 }
lib/alloc_tag.c+17−10
@@ -776,31 +776,38 @@ EXPORT_SYMBOL(page_alloc_tagging_ops); · 775 unmodified lines
776776 static int proc_mem_profiling_handler(const struct ctl_table *table, int write,
777777 void *buffer, size_t *lenp, loff_t *ppos)
778778 {
779− if (!mem_profiling_support && write)
780− return -EINVAL;
779+ if (write) {
780+ /*
781+ * Call from do_sysctl_args() which is a no-op since the same
782+ * value was already set by setup_early_mem_profiling.
783+ * Return success to avoid warnings from do_sysctl_args().
784+ */
785+ if (!current->mm)
786+ return 0;
787+
788+#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
789+ /* User can't toggle profiling while debugging */
790+ return -EACCES;
791+#endif
792+ if (!mem_profiling_support)
793+ return -EINVAL;
794+ }
781795
782796 return proc_do_static_key(table, write, buffer, lenp, ppos);
783797 }
784798
785799
786−static struct ctl_table memory_allocation_profiling_sysctls[] = {
800+static const struct ctl_table memory_allocation_profiling_sysctls[] = {
787801 {
788802 .procname = "mem_profiling",
789803 .data = &mem_alloc_profiling_key,
790−#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
791− .mode = 0444,
792−#else
793804 .mode = 0644,
794−#endif
795805 .proc_handler = proc_mem_profiling_handler,
796806 },
797807 };
798808
799809 static void __init sysctl_init(void)
800810 {
801− if (!mem_profiling_support)
802− memory_allocation_profiling_sysctls[0].mode = 0444;
803−
804811 register_sysctl_init("vm", memory_allocation_profiling_sysctls);
805812 }
806813 #else /* CONFIG_SYSCTL */
lib/test_vmalloc.c+7−4
@@ -58,6 +58,9 @@ __param(int, run_test_mask, 7, · 57 unmodified lines
5858 /* Add a new test case description here. */
5959 );
6060
61+__param(int, nr_pcpu_objects, 35000,
62+ "Number of pcpu objects to allocate for pcpu_alloc_test");
63+
6164 /*
6265 * This is for synchronization of setup phase.
6366 */
@@ -317,24 +320,24 @@ pcpu_alloc_test(void) · 253 unmodified lines
317320 size_t size, align;
318321 int i;
319322
320− pcpu = vmalloc(sizeof(void __percpu *) * 35000);
323+ pcpu = vmalloc(sizeof(void __percpu *) * nr_pcpu_objects);
321324 if (!pcpu)
322325 return -1;
323326
324− for (i = 0; i < 35000; i++) {
327+ for (i = 0; i < nr_pcpu_objects; i++) {
325328 size = get_random_u32_inclusive(1, PAGE_SIZE / 4);
326329
327330 /*
328331 * Maximum PAGE_SIZE
329332 */
330− align = 1 << get_random_u32_inclusive(1, 11);
333+ align = 1 << get_random_u32_inclusive(1, PAGE_SHIFT - 1);
331334
332335 pcpu[i] = __alloc_percpu(size, align);
333336 if (!pcpu[i])
334337 rv = -1;
335338 }
336339
337− for (i = 0; i < 35000; i++)
340+ for (i = 0; i < nr_pcpu_objects; i++)
338341 free_percpu(pcpu[i]);
339342
340343 vfree(pcpu);
mm/Kconfig+31−17
@@ -582,23 +582,20 @@ config SPLIT_PMD_PTLOCKS · 581 unmodified lines
582582
583583 #
584584 # support for memory balloon
585−config MEMORY_BALLOON
585+config BALLOON
586586 bool
587587
588588 #
589−# support for memory balloon compaction
590−config BALLOON_COMPACTION
591− bool "Allow for balloon memory compaction/migration"
589+# support for memory balloon page migration
590+config BALLOON_MIGRATION
591+ bool "Allow for balloon memory migration"
592592 default y
593− depends on COMPACTION && MEMORY_BALLOON
593+ depends on MIGRATION && BALLOON
594594 help
595− Memory fragmentation introduced by ballooning might reduce
596− significantly the number of 2MB contiguous memory blocks that can be
597− used within a guest, thus imposing performance penalties associated
598− with the reduced number of transparent huge pages that could be used
599− by the guest workload. Allowing the compaction & migration for memory
600− pages enlisted as being part of memory balloon devices avoids the
601− scenario aforementioned and helps improving memory defragmentation.
595+ Allow for migration of pages inflated in a memory balloon such that
596+ they can be allocated from memory areas only available for movable
597+ allocations (e.g., ZONE_MOVABLE, CMA) and such that they can be
598+ migrated for memory defragmentation purposes by memory compaction.
602599
603600 #
604601 # support for memory compaction
@@ -1440,14 +1437,12 @@ config ARCH_HAS_USER_SHADOW_STACK · 835 unmodified lines
14401437 The architecture has hardware support for userspace shadow call
14411438 stacks (eg, x86 CET, arm64 GCS or RISC-V Zicfiss).
14421439
1443−config ARCH_SUPPORTS_PT_RECLAIM
1440+config HAVE_ARCH_TLB_REMOVE_TABLE
14441441 def_bool n
14451442
14461443 config PT_RECLAIM
1447− bool "reclaim empty user page table pages"
1448− default y
1449− depends on ARCH_SUPPORTS_PT_RECLAIM && MMU && SMP
1450− select MMU_GATHER_RCU_TABLE_FREE
1444+ def_bool y
1445+ depends on MMU_GATHER_RCU_TABLE_FREE && !HAVE_ARCH_TLB_REMOVE_TABLE
14511446 help
14521447 Try to reclaim empty user page table pages in paths other than munmap
14531448 and exit_mmap path.
@@ -1457,6 +1452,25 @@ config PT_RECLAIM · 3 unmodified lines
14571452 config FIND_NORMAL_PAGE
14581453 def_bool n
14591454
1455+config ARCH_HAS_LAZY_MMU_MODE
1456+ bool
1457+ help
1458+ The architecture uses the lazy MMU mode. This allows changes to
1459+ MMU-related architectural state to be deferred until the mode is
1460+ exited. See <linux/pgtable.h> for details.
1461+
1462+config LAZY_MMU_MODE_KUNIT_TEST
1463+ tristate "KUnit tests for the lazy MMU mode" if !KUNIT_ALL_TESTS
1464+ depends on ARCH_HAS_LAZY_MMU_MODE
1465+ depends on KUNIT
1466+ default KUNIT_ALL_TESTS
1467+ help
1468+ Enable this option to check that the lazy MMU mode interface behaves
1469+ as expected. Only tests for the generic interface are included (not
1470+ architecture-specific behaviours).
1471+
1472+ If unsure, say N.
1473+
14601474 source "mm/damon/Kconfig"
14611475
14621476 endmenu
mm/Makefile+2−2
@@ -125,7 +125,7 @@ obj-$(CONFIG_CMA) += cma.o · 124 unmodified lines
125125 obj-$(CONFIG_NUMA) += numa.o
126126 obj-$(CONFIG_NUMA_MEMBLKS) += numa_memblks.o
127127 obj-$(CONFIG_NUMA_EMU) += numa_emulation.o
128−obj-$(CONFIG_MEMORY_BALLOON) += balloon_compaction.o
128+obj-$(CONFIG_BALLOON) += balloon.o
129129 obj-$(CONFIG_PAGE_EXTENSION) += page_ext.o
130130 obj-$(CONFIG_PAGE_TABLE_CHECK) += page_table_check.o
131131 obj-$(CONFIG_CMA_DEBUGFS) += cma_debug.o
@@ -149,4 +149,4 @@ obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o · 17 unmodified lines
149149 obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
150150 obj-$(CONFIG_EXECMEM) += execmem.o
151151 obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o
152−obj-$(CONFIG_PT_RECLAIM) += pt_reclaim.o
152+obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o
mm/backing-dev.c+3−6
@@ -939,7 +939,7 @@ void wb_memcg_offline(struct mem_cgroup *memcg) · 938 unmodified lines
939939 memcg_cgwb_list->next = NULL; /* prevent new wb's */
940940 spin_unlock_irq(&cgwb_lock);
941941
942− queue_work(system_unbound_wq, &cleanup_offline_cgwbs_work);
942+ queue_work(system_dfl_wq, &cleanup_offline_cgwbs_work);
943943 }
944944
945945 /**
@@ -971,10 +971,10 @@ static int __init cgwb_init(void) · 25 unmodified lines
971971 {
972972 /*
973973 * There can be many concurrent release work items overwhelming
974− * system_wq. Put them in a separate wq and limit concurrency.
974+ * system_percpu_wq. Put them in a separate wq and limit concurrency.
975975 * There's no point in executing many of these in parallel.
976976 */
977− cgwb_release_wq = alloc_workqueue("cgwb_release", 0, 1);
977+ cgwb_release_wq = alloc_workqueue("cgwb_release", WQ_PERCPU, 1);
978978 if (!cgwb_release_wq)
979979 return -ENOMEM;
980980
@@ -1034,7 +1034,6 @@ struct backing_dev_info *bdi_alloc(int node_id) · 53 unmodified lines
10341034 bdi->capabilities = BDI_CAP_WRITEBACK;
10351035 bdi->ra_pages = VM_READAHEAD_PAGES;
10361036 bdi->io_pages = VM_READAHEAD_PAGES;
1037− timer_setup(&bdi->laptop_mode_wb_timer, laptop_mode_timer_fn, 0);
10381037 return bdi;
10391038 }
10401039 EXPORT_SYMBOL(bdi_alloc);
@@ -1156,8 +1155,6 @@ static void bdi_remove_from_list(struct backing_dev_info *bdi) · 115 unmodified lines
11561155
11571156 void bdi_unregister(struct backing_dev_info *bdi)
11581157 {
1159− timer_delete_sync(&bdi->laptop_mode_wb_timer);
1160−
11611158 /* make sure nobody finds us on the bdi_list anymore */
11621159 bdi_remove_from_list(bdi);
11631160 wb_shutdown(&bdi->wb);
mm/balloon.crenamed from mm/balloon_compaction.c+140−59
@@ -1,28 +1,62 @@
11 // SPDX-License-Identifier: GPL-2.0-only
22 /*
3− * mm/balloon_compaction.c
4− *
5− * Common interface for making balloon pages movable by compaction.
3+ * Common interface for implementing a memory balloon, including support
4+ * for migration of pages inflated in a memory balloon.
65 *
76 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
87 */
98 #include <linux/mm.h>
109 #include <linux/slab.h>
1110 #include <linux/export.h>
12−#include <linux/balloon_compaction.h>
11+#include <linux/balloon.h>
12+
13+/*
14+ * Lock protecting the balloon_dev_info of all devices. We don't really
15+ * expect more than one device.
16+ */
17+static DEFINE_SPINLOCK(balloon_pages_lock);
18+
19+/**
20+ * balloon_page_insert - insert a page into the balloon's page list and make
21+ * the page->private assignment accordingly.
22+ * @balloon : pointer to balloon device
23+ * @page : page to be assigned as a 'balloon page'
24+ *
25+ * Caller must ensure the balloon_pages_lock is held.
26+ */
27+static void balloon_page_insert(struct balloon_dev_info *balloon,
28+ struct page *page)
29+{
30+ lockdep_assert_held(&balloon_pages_lock);
31+ __SetPageOffline(page);
32+ if (IS_ENABLED(CONFIG_BALLOON_MIGRATION)) {
33+ SetPageMovableOps(page);
34+ set_page_private(page, (unsigned long)balloon);
35+ }
36+ list_add(&page->lru, &balloon->pages);
37+}
38+
39+/**
40+ * balloon_page_finalize - prepare a balloon page that was removed from the
41+ * balloon list for release to the page allocator
42+ * @page: page to be released to the page allocator
43+ *
44+ * Caller must ensure the balloon_pages_lock is held.
45+ */
46+static void balloon_page_finalize(struct page *page)
47+{
48+ lockdep_assert_held(&balloon_pages_lock);
49+ if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))
50+ set_page_private(page, 0);
51+ /* PageOffline is sticky until the page is freed to the buddy. */
52+}
1353
1454 static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,
1555 struct page *page)
1656 {
17− /*
18− * Block others from accessing the 'page' when we get around to
19− * establishing additional references. We should be the only one
20− * holding a reference to the 'page' at this point. If we are not, then
21− * memory corruption is possible and we should stop execution.
22− */
23− BUG_ON(!trylock_page(page));
2457 balloon_page_insert(b_dev_info, page);
25− unlock_page(page);
58+ if (b_dev_info->adjust_managed_page_count)
59+ adjust_managed_page_count(page, -1);
2660 __count_vm_event(BALLOON_INFLATE);
2761 inc_node_page_state(page, NR_BALLOON_PAGES);
2862 }
@@ -45,13 +79,13 @@ size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info, · 16 unmodified lines
4579 unsigned long flags;
4680 size_t n_pages = 0;
4781
48− spin_lock_irqsave(&b_dev_info->pages_lock, flags);
82+ spin_lock_irqsave(&balloon_pages_lock, flags);
4983 list_for_each_entry_safe(page, tmp, pages, lru) {
5084 list_del(&page->lru);
5185 balloon_page_enqueue_one(b_dev_info, page);
5286 n_pages++;
5387 }
54− spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
88+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
5589 return n_pages;
5690 }
5791 EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);
@@ -81,34 +115,26 @@ size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info, · 23 unmodified lines
81115 unsigned long flags;
82116 size_t n_pages = 0;
83117
84− spin_lock_irqsave(&b_dev_info->pages_lock, flags);
118+ spin_lock_irqsave(&balloon_pages_lock, flags);
85119 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
86120 if (n_pages == n_req_pages)
87121 break;
88−
89− /*
90− * Block others from accessing the 'page' while we get around to
91− * establishing additional references and preparing the 'page'
92− * to be released by the balloon driver.
93− */
94− if (!trylock_page(page))
95− continue;
96−
97122 list_del(&page->lru);
123+ if (b_dev_info->adjust_managed_page_count)
124+ adjust_managed_page_count(page, 1);
98125 balloon_page_finalize(page);
99126 __count_vm_event(BALLOON_DEFLATE);
100127 list_add(&page->lru, pages);
101− unlock_page(page);
102128 dec_node_page_state(page, NR_BALLOON_PAGES);
103129 n_pages++;
104130 }
105− spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
131+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
106132
107133 return n_pages;
108134 }
109135 EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);
110136
111−/*
137+/**
112138 * balloon_page_alloc - allocates a new page for insertion into the balloon
113139 * page list.
114140 *
@@ -120,14 +146,18 @@ EXPORT_SYMBOL_GPL(balloon_page_list_dequeue); · 5 unmodified lines
120146 */
121147 struct page *balloon_page_alloc(void)
122148 {
123− struct page *page = alloc_page(balloon_mapping_gfp_mask() |
124− __GFP_NOMEMALLOC | __GFP_NORETRY |
125− __GFP_NOWARN);
126− return page;
149+ gfp_t gfp_flags = __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
150+
151+ if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))
152+ gfp_flags |= GFP_HIGHUSER_MOVABLE;
153+ else
154+ gfp_flags |= GFP_HIGHUSER;
155+
156+ return alloc_page(gfp_flags);
127157 }
128158 EXPORT_SYMBOL_GPL(balloon_page_alloc);
129159
130−/*
160+/**
131161 * balloon_page_enqueue - inserts a new page into the balloon page list.
132162 *
133163 * @b_dev_info: balloon device descriptor where we will insert a new page
@@ -136,22 +166,21 @@ EXPORT_SYMBOL_GPL(balloon_page_alloc); · 2 unmodified lines
136166 * Drivers must call this function to properly enqueue a new allocated balloon
137167 * page before definitively removing the page from the guest system.
138168 *
139− * Drivers must not call balloon_page_enqueue on pages that have been pushed to
140− * a list with balloon_page_push before removing them with balloon_page_pop. To
141− * enqueue a list of pages, use balloon_page_list_enqueue instead.
169+ * Drivers must not enqueue pages while page->lru is still in
170+ * use, and must not use page->lru until a page was unqueued again.
142171 */
143172 void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
144173 struct page *page)
145174 {
146175 unsigned long flags;
147176
148− spin_lock_irqsave(&b_dev_info->pages_lock, flags);
177+ spin_lock_irqsave(&balloon_pages_lock, flags);
149178 balloon_page_enqueue_one(b_dev_info, page);
150− spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
179+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
151180 }
152181 EXPORT_SYMBOL_GPL(balloon_page_enqueue);
153182
154−/*
183+/**
155184 * balloon_page_dequeue - removes a page from balloon's page list and returns
156185 * its address to allow the driver to release the page.
157186 * @b_dev_info: balloon device descriptor where we will grab a page from.
@@ -187,32 +216,42 @@ struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info) · 29 unmodified lines
187216 * BUG() here, otherwise the balloon driver may get stuck in
188217 * an infinite loop while attempting to release all its pages.
189218 */
190− spin_lock_irqsave(&b_dev_info->pages_lock, flags);
219+ spin_lock_irqsave(&balloon_pages_lock, flags);
191220 if (unlikely(list_empty(&b_dev_info->pages) &&
192221 !b_dev_info->isolated_pages))
193222 BUG();
194− spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
223+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
195224 return NULL;
196225 }
197226 return list_first_entry(&pages, struct page, lru);
198227 }
199228 EXPORT_SYMBOL_GPL(balloon_page_dequeue);
200229
201−#ifdef CONFIG_BALLOON_COMPACTION
230+#ifdef CONFIG_BALLOON_MIGRATION
231+static struct balloon_dev_info *balloon_page_device(struct page *page)
232+{
233+ return (struct balloon_dev_info *)page_private(page);
234+}
202235
203236 static bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
204237
205238 {
206− struct balloon_dev_info *b_dev_info = balloon_page_device(page);
239+ struct balloon_dev_info *b_dev_info;
207240 unsigned long flags;
208241
209− if (!b_dev_info)
242+ spin_lock_irqsave(&balloon_pages_lock, flags);
243+ b_dev_info = balloon_page_device(page);
244+ if (!b_dev_info) {
245+ /*
246+ * The page already got deflated and removed from the
247+ * balloon list.
248+ */
249+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
210250 return false;
211−
212− spin_lock_irqsave(&b_dev_info->pages_lock, flags);
251+ }
213252 list_del(&page->lru);
214253 b_dev_info->isolated_pages++;
215− spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
254+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
216255
217256 return true;
218257 }
@@ -222,33 +261,75 @@ static void balloon_page_putback(struct page *page) · 3 unmodified lines
222261 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
223262 unsigned long flags;
224263
225− /* Isolated balloon pages cannot get deflated. */
264+ /*
265+ * When we isolated the page, the page was still inflated in a balloon
266+ * device. As isolated balloon pages cannot get deflated, we still have
267+ * a balloon device here.
268+ */
226269 if (WARN_ON_ONCE(!b_dev_info))
227270 return;
228271
229− spin_lock_irqsave(&b_dev_info->pages_lock, flags);
272+ spin_lock_irqsave(&balloon_pages_lock, flags);
230273 list_add(&page->lru, &b_dev_info->pages);
231274 b_dev_info->isolated_pages--;
232− spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
275+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
233276 }
234277
235−/* move_to_new_page() counterpart for a ballooned page */
236278 static int balloon_page_migrate(struct page *newpage, struct page *page,
237279 enum migrate_mode mode)
238280 {
239− struct balloon_dev_info *balloon = balloon_page_device(page);
240−
241− VM_BUG_ON_PAGE(!PageLocked(page), page);
242− VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
281+ struct balloon_dev_info *b_dev_info = balloon_page_device(page);
282+ unsigned long flags;
283+ int rc;
243284
244− /* Isolated balloon pages cannot get deflated. */
245− if (WARN_ON_ONCE(!balloon))
285+ /*
286+ * When we isolated the page, the page was still inflated in a balloon
287+ * device. As isolated balloon pages cannot get deflated, we still have
288+ * a balloon device here.
289+ */
290+ if (WARN_ON_ONCE(!b_dev_info))
246291 return -EAGAIN;
247292
248− return balloon->migratepage(balloon, newpage, page, mode);
293+ rc = b_dev_info->migratepage(b_dev_info, newpage, page, mode);
294+ if (rc < 0 && rc != -ENOENT)
295+ return rc;
296+
297+ spin_lock_irqsave(&balloon_pages_lock, flags);
298+ if (!rc) {
299+ /* Insert the new page into the balloon list. */
300+ get_page(newpage);
301+ balloon_page_insert(b_dev_info, newpage);
302+ __count_vm_event(BALLOON_MIGRATE);
303+
304+ if (b_dev_info->adjust_managed_page_count &&
305+ page_zone(page) != page_zone(newpage)) {
306+ /*
307+ * When we migrate a page to a different zone we
308+ * have to fixup the count of both involved zones.
309+ */
310+ adjust_managed_page_count(page, 1);
311+ adjust_managed_page_count(newpage, -1);
312+ }
313+ } else {
314+ /* Old page was deflated but new page not inflated. */
315+ __count_vm_event(BALLOON_DEFLATE);
316+
317+ if (b_dev_info->adjust_managed_page_count)
318+ adjust_managed_page_count(page, 1);
319+ }
320+
321+ b_dev_info->isolated_pages--;
322+
323+ /* Free the now-deflated page we isolated in balloon_page_isolate(). */
324+ balloon_page_finalize(page);
325+ spin_unlock_irqrestore(&balloon_pages_lock, flags);
326+
327+ put_page(page);
328+
329+ return 0;
249330 }
250331
251−const struct movable_operations balloon_mops = {
332+static const struct movable_operations balloon_mops = {
252333 .migrate_page = balloon_page_migrate,
253334 .isolate_page = balloon_page_isolate,
254335 .putback_page = balloon_page_putback,
@@ -260,4 +341,4 @@ static int __init balloon_init(void) · 5 unmodified lines
260341 }
261342 core_initcall(balloon_init);
262343
263−#endif /* CONFIG_BALLOON_COMPACTION */
344+#endif /* CONFIG_BALLOON_MIGRATION */
mm/cma.c+71−54
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #include <linux/mm.h>
2323 #include <linux/sizes.h>
2424 #include <linux/slab.h>
25+#include <linux/string.h>
2526 #include <linux/string_choices.h>
2627 #include <linux/log2.h>
2728 #include <linux/cma.h>
@@ -233,7 +234,7 @@ static int __init cma_new_area(const char *name, phys_addr_t size, · 205 unmodified lines
233234 cma_area_count++;
234235
235236 if (name)
236− snprintf(cma->name, CMA_MAX_NAME, "%s", name);
237+ strscpy(cma->name, name);
237238 else
238239 snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count);
239240
@@ -836,7 +837,7 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr, · 596 unmodified lines
836837 spin_unlock_irq(&cma->lock);
837838
838839 mutex_lock(&cma->alloc_mutex);
839− ret = alloc_contig_range(pfn, pfn + count, ACR_FLAGS_CMA, gfp);
840+ ret = alloc_contig_frozen_range(pfn, pfn + count, ACR_FLAGS_CMA, gfp);
840841 mutex_unlock(&cma->alloc_mutex);
841842 if (!ret)
842843 break;
@@ -856,8 +857,8 @@ out: · 13 unmodified lines
856857 return ret;
857858 }
858859
859−static struct page *__cma_alloc(struct cma *cma, unsigned long count,
860− unsigned int align, gfp_t gfp)
860+static struct page *__cma_alloc_frozen(struct cma *cma,
861+ unsigned long count, unsigned int align, gfp_t gfp)
861862 {
862863 struct page *page = NULL;
863864 int ret = -ENOMEM, r;
@@ -914,6 +915,21 @@ static struct page *__cma_alloc(struct cma *cma, unsigned long count, · 50 unmodified lines
914915 return page;
915916 }
916917
918+struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
919+ unsigned int align, bool no_warn)
920+{
921+ gfp_t gfp = GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0);
922+
923+ return __cma_alloc_frozen(cma, count, align, gfp);
924+}
925+
926+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order)
927+{
928+ gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;
929+
930+ return __cma_alloc_frozen(cma, 1 << order, order, gfp);
931+}
932+
917933 /**
918934 * cma_alloc() - allocate pages from contiguous area
919935 * @cma: Contiguous memory region for which the allocation is performed.
@@ -926,50 +942,61 @@ static struct page *__cma_alloc(struct cma *cma, unsigned long count, · 6 unmodified lines
926942 */
927943 struct page *cma_alloc(struct cma *cma, unsigned long count,
928944 unsigned int align, bool no_warn)
929−{
930− return __cma_alloc(cma, count, align, GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0));
931−}
932−
933−struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp)
934945 {
935946 struct page *page;
936947
937− if (WARN_ON(!order || !(gfp & __GFP_COMP)))
938− return NULL;
939−
940− page = __cma_alloc(cma, 1 << order, order, gfp);
948+ page = cma_alloc_frozen(cma, count, align, no_warn);
949+ if (page)
950+ set_pages_refcounted(page, count);
941951
942− return page ? page_folio(page) : NULL;
952+ return page;
943953 }
944954
945−bool cma_pages_valid(struct cma *cma, const struct page *pages,
946− unsigned long count)
955+static struct cma_memrange *find_cma_memrange(struct cma *cma,
956+ const struct page *pages, unsigned long count)
947957 {
948− unsigned long pfn, end;
958+ struct cma_memrange *cmr = NULL;
959+ unsigned long pfn, end_pfn;
949960 int r;
950− struct cma_memrange *cmr;
951− bool ret;
961+
962+ pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count);
952963
953964 if (!cma || !pages || count > cma->count)
954− return false;
965+ return NULL;
955966
956967 pfn = page_to_pfn(pages);
957− ret = false;
958968
959969 for (r = 0; r < cma->nranges; r++) {
960970 cmr = &cma->ranges[r];
961− end = cmr->base_pfn + cmr->count;
962− if (pfn >= cmr->base_pfn && pfn < end) {
963− ret = pfn + count <= end;
964− break;
971+ end_pfn = cmr->base_pfn + cmr->count;
972+ if (pfn >= cmr->base_pfn && pfn < end_pfn) {
973+ if (pfn + count <= end_pfn)
974+ break;
975+
976+ VM_WARN_ON_ONCE(1);
965977 }
966978 }
967979
968− if (!ret)
969− pr_debug("%s(page %p, count %lu)\n",
970− __func__, (void *)pages, count);
980+ if (r == cma->nranges) {
981+ pr_debug("%s(page %p, count %lu, no cma range matches the page range)\n",
982+ __func__, (void *)pages, count);
983+ return NULL;
984+ }
971985
972− return ret;
986+ return cmr;
987+}
988+
989+static void __cma_release_frozen(struct cma *cma, struct cma_memrange *cmr,
990+ const struct page *pages, unsigned long count)
991+{
992+ unsigned long pfn = page_to_pfn(pages);
993+
994+ pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count);
995+
996+ free_contig_frozen_range(pfn, count);
997+ cma_clear_bitmap(cma, cmr, pfn, count);
998+ cma_sysfs_account_release_pages(cma, count);
999+ trace_cma_release(cma->name, pfn, pages, count);
9731000 }
9741001
9751002 /**
@@ -986,43 +1013,33 @@ bool cma_release(struct cma *cma, const struct page *pages, · 10 unmodified lines
9861013 unsigned long count)
9871014 {
9881015 struct cma_memrange *cmr;
989− unsigned long pfn, end_pfn;
990− int r;
1016+ unsigned long i, pfn;
9911017
992− pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count);
993−
994− if (!cma_pages_valid(cma, pages, count))
1018+ cmr = find_cma_memrange(cma, pages, count);
1019+ if (!cmr)
9951020 return false;
9961021
9971022 pfn = page_to_pfn(pages);
998− end_pfn = pfn + count;
1023+ for (i = 0; i < count; i++, pfn++)
1024+ VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
9991025
1000− for (r = 0; r < cma->nranges; r++) {
1001− cmr = &cma->ranges[r];
1002− if (pfn >= cmr->base_pfn &&
1003− pfn < (cmr->base_pfn + cmr->count)) {
1004− VM_BUG_ON(end_pfn > cmr->base_pfn + cmr->count);
1005− break;
1006− }
1007− }
1008−
1009− if (r == cma->nranges)
1010− return false;
1011−
1012− free_contig_range(pfn, count);
1013− cma_clear_bitmap(cma, cmr, pfn, count);
1014− cma_sysfs_account_release_pages(cma, count);
1015− trace_cma_release(cma->name, pfn, pages, count);
1026+ __cma_release_frozen(cma, cmr, pages, count);
10161027
10171028 return true;
10181029 }
10191030
1020−bool cma_free_folio(struct cma *cma, const struct folio *folio)
1031+bool cma_release_frozen(struct cma *cma, const struct page *pages,
1032+ unsigned long count)
10211033 {
1022− if (WARN_ON(!folio_test_large(folio)))
1034+ struct cma_memrange *cmr;
1035+
1036+ cmr = find_cma_memrange(cma, pages, count);
1037+ if (!cmr)
10231038 return false;
10241039
1025− return cma_release(cma, &folio->page, folio_nr_pages(folio));
1040+ __cma_release_frozen(cma, cmr, pages, count);
1041+
1042+ return true;
10261043 }
10271044
10281045 int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data)
mm/damon/core.c+142−100
@@ -157,6 +157,12 @@ void damon_destroy_region(struct damon_region *r, struct damon_target *t) · 156 unmodified lines
157157 damon_free_region(r);
158158 }
159159
160+static bool damon_is_last_region(struct damon_region *r,
161+ struct damon_target *t)
162+{
163+ return list_is_last(&r->list, &t->regions_list);
164+}
165+
160166 /*
161167 * Check whether a region is intersecting an address range
162168 *
@@ -197,7 +203,7 @@ static int damon_fill_regions_holes(struct damon_region *first, · 34 unmodified lines
197203 * @t: the given target.
198204 * @ranges: array of new monitoring target ranges.
199205 * @nr_ranges: length of @ranges.
200− * @min_sz_region: minimum region size.
206+ * @min_region_sz: minimum region size.
201207 *
202208 * This function adds new regions to, or modify existing regions of a
203209 * monitoring target to fit in specific ranges.
@@ -205,7 +211,7 @@ static int damon_fill_regions_holes(struct damon_region *first, · 1 unmodified line
205211 * Return: 0 if success, or negative error code otherwise.
206212 */
207213 int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
208− unsigned int nr_ranges, unsigned long min_sz_region)
214+ unsigned int nr_ranges, unsigned long min_region_sz)
209215 {
210216 struct damon_region *r, *next;
211217 unsigned int i;
@@ -242,16 +248,16 @@ int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges, · 30 unmodified lines
242248 /* no region intersects with this range */
243249 newr = damon_new_region(
244250 ALIGN_DOWN(range->start,
245− min_sz_region),
246− ALIGN(range->end, min_sz_region));
251+ min_region_sz),
252+ ALIGN(range->end, min_region_sz));
247253 if (!newr)
248254 return -ENOMEM;
249255 damon_insert_region(newr, damon_prev_region(r), r, t);
250256 } else {
251257 /* resize intersecting regions to fit in this range */
252258 first->ar.start = ALIGN_DOWN(range->start,
253− min_sz_region);
254− last->ar.end = ALIGN(range->end, min_sz_region);
259+ min_region_sz);
260+ last->ar.end = ALIGN(range->end, min_region_sz);
255261
256262 /* fill possible holes in the range */
257263 err = damon_fill_regions_holes(first, last, t);
@@ -278,7 +284,7 @@ struct damos_filter *damos_new_filter(enum damos_filter_type type, · 20 unmodified lines
278284 }
279285
280286 /**
281− * damos_filter_for_ops() - Return if the filter is ops-hndled one.
287+ * damos_filter_for_ops() - Return if the filter is ops-handled one.
282288 * @type: type of the filter.
283289 *
284290 * Return: true if the filter of @type needs to be handled by ops layer, false
@@ -395,6 +401,7 @@ struct damos *damon_new_scheme(struct damos_access_pattern *pattern, · 110 unmodified lines
395401 INIT_LIST_HEAD(&scheme->core_filters);
396402 INIT_LIST_HEAD(&scheme->ops_filters);
397403 scheme->stat = (struct damos_stat){};
404+ scheme->max_nr_snapshots = 0;
398405 INIT_LIST_HEAD(&scheme->list);
399406
400407 scheme->quota = *(damos_quota_init(quota));
@@ -546,7 +553,7 @@ struct damon_ctx *damon_new_ctx(void) · 145 unmodified lines
546553 ctx->attrs.max_nr_regions = 1000;
547554
548555 ctx->addr_unit = 1;
549− ctx->min_sz_region = DAMON_MIN_REGION;
556+ ctx->min_region_sz = DAMON_MIN_REGION_SZ;
550557
551558 INIT_LIST_HEAD(&ctx->adaptive_targets);
552559 INIT_LIST_HEAD(&ctx->schemes);
@@ -1072,7 +1079,11 @@ static int damos_commit(struct damos *dst, struct damos *src) · 519 unmodified lines
10721079 return err;
10731080
10741081 err = damos_commit_filters(dst, src);
1075− return err;
1082+ if (err)
1083+ return err;
1084+
1085+ dst->max_nr_snapshots = src->max_nr_snapshots;
1086+ return 0;
10761087 }
10771088
10781089 static int damon_commit_schemes(struct damon_ctx *dst, struct damon_ctx *src)
@@ -1131,7 +1142,7 @@ static struct damon_target *damon_nth_target(int n, struct damon_ctx *ctx) · 52 unmodified lines
11311142 * If @src has no region, @dst keeps current regions.
11321143 */
11331144 static int damon_commit_target_regions(struct damon_target *dst,
1134− struct damon_target *src, unsigned long src_min_sz_region)
1145+ struct damon_target *src, unsigned long src_min_region_sz)
11351146 {
11361147 struct damon_region *src_region;
11371148 struct damon_addr_range *ranges;
@@ -1148,7 +1159,7 @@ static int damon_commit_target_regions(struct damon_target *dst, · 10 unmodified lines
11481159 i = 0;
11491160 damon_for_each_region(src_region, src)
11501161 ranges[i++] = src_region->ar;
1151− err = damon_set_regions(dst, ranges, i, src_min_sz_region);
1162+ err = damon_set_regions(dst, ranges, i, src_min_region_sz);
11521163 kfree(ranges);
11531164 return err;
11541165 }
@@ -1156,11 +1167,11 @@ static int damon_commit_target_regions(struct damon_target *dst, · 1 unmodified line
11561167 static int damon_commit_target(
11571168 struct damon_target *dst, bool dst_has_pid,
11581169 struct damon_target *src, bool src_has_pid,
1159− unsigned long src_min_sz_region)
1170+ unsigned long src_min_region_sz)
11601171 {
11611172 int err;
11621173
1163− err = damon_commit_target_regions(dst, src, src_min_sz_region);
1174+ err = damon_commit_target_regions(dst, src, src_min_region_sz);
11641175 if (err)
11651176 return err;
11661177 if (dst_has_pid)
@@ -1187,7 +1198,7 @@ static int damon_commit_targets( · 20 unmodified lines
11871198 err = damon_commit_target(
11881199 dst_target, damon_target_has_pid(dst),
11891200 src_target, damon_target_has_pid(src),
1190− src->min_sz_region);
1201+ src->min_region_sz);
11911202 if (err)
11921203 return err;
11931204 } else {
@@ -1214,7 +1225,7 @@ static int damon_commit_targets( · 20 unmodified lines
12141225 return -ENOMEM;
12151226 err = damon_commit_target(new_target, false,
12161227 src_target, damon_target_has_pid(src),
1217− src->min_sz_region);
1228+ src->min_region_sz);
12181229 if (err) {
12191230 damon_destroy_target(new_target, NULL);
12201231 return err;
@@ -1261,7 +1272,7 @@ int damon_commit_ctx(struct damon_ctx *dst, struct damon_ctx *src) · 40 unmodified lines
12611272 }
12621273 dst->ops = src->ops;
12631274 dst->addr_unit = src->addr_unit;
1264− dst->min_sz_region = src->min_sz_region;
1275+ dst->min_region_sz = src->min_region_sz;
12651276
12661277 return 0;
12671278 }
@@ -1294,8 +1305,8 @@ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) · 26 unmodified lines
12941305
12951306 if (ctx->attrs.min_nr_regions)
12961307 sz /= ctx->attrs.min_nr_regions;
1297− if (sz < ctx->min_sz_region)
1298− sz = ctx->min_sz_region;
1308+ if (sz < ctx->min_region_sz)
1309+ sz = ctx->min_region_sz;
12991310
13001311 return sz;
13011312 }
@@ -1431,6 +1442,23 @@ bool damon_is_running(struct damon_ctx *ctx) · 129 unmodified lines
14311442 return running;
14321443 }
14331444
1445+/**
1446+ * damon_kdamond_pid() - Return pid of a given DAMON context's worker thread.
1447+ * @ctx: The DAMON context of the question.
1448+ *
1449+ * Return: pid if @ctx is running, negative error code otherwise.
1450+ */
1451+int damon_kdamond_pid(struct damon_ctx *ctx)
1452+{
1453+ int pid = -EINVAL;
1454+
1455+ mutex_lock(&ctx->kdamond_lock);
1456+ if (ctx->kdamond)
1457+ pid = ctx->kdamond->pid;
1458+ mutex_unlock(&ctx->kdamond_lock);
1459+ return pid;
1460+}
1461+
14341462 /*
14351463 * damon_call_handle_inactive_ctx() - handle DAMON call request that added to
14361464 * an inactive context.
@@ -1604,7 +1632,7 @@ static unsigned long damon_get_intervals_adaptation_bp(struct damon_ctx *c) · 167 unmodified lines
16041632 adaptation_bp = damon_feed_loop_next_input(100000000, score_bp) /
16051633 10000;
16061634 /*
1607− * adaptaion_bp ranges from 1 to 20,000. Avoid too rapid reduction of
1635+ * adaptation_bp ranges from 1 to 20,000. Avoid too rapid reduction of
16081636 * the intervals by rescaling [1,10,000] to [5000, 10,000].
16091637 */
16101638 if (adaptation_bp <= 10000)
@@ -1668,7 +1696,7 @@ static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t, · 57 unmodified lines
16681696 * @t: The target of the region.
16691697 * @rp: The pointer to the region.
16701698 * @s: The scheme to be applied.
1671− * @min_sz_region: minimum region size.
1699+ * @min_region_sz: minimum region size.
16721700 *
16731701 * If a quota of a scheme has exceeded in a quota charge window, the scheme's
16741702 * action would applied to only a part of the target access pattern fulfilling
@@ -1686,7 +1714,8 @@ static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t, · 11 unmodified lines
16861714 * Return: true if the region should be entirely skipped, false otherwise.
16871715 */
16881716 static bool damos_skip_charged_region(struct damon_target *t,
1689− struct damon_region **rp, struct damos *s, unsigned long min_sz_region)
1717+ struct damon_region **rp, struct damos *s,
1718+ unsigned long min_region_sz)
16901719 {
16911720 struct damon_region *r = *rp;
16921721 struct damos_quota *quota = &s->quota;
@@ -1708,11 +1737,11 @@ static bool damos_skip_charged_region(struct damon_target *t, · 15 unmodified lines
17081737 if (quota->charge_addr_from && r->ar.start <
17091738 quota->charge_addr_from) {
17101739 sz_to_skip = ALIGN_DOWN(quota->charge_addr_from -
1711− r->ar.start, min_sz_region);
1740+ r->ar.start, min_region_sz);
17121741 if (!sz_to_skip) {
1713− if (damon_sz_region(r) <= min_sz_region)
1742+ if (damon_sz_region(r) <= min_region_sz)
17141743 return true;
1715− sz_to_skip = min_sz_region;
1744+ sz_to_skip = min_region_sz;
17161745 }
17171746 damon_split_region_at(t, r, sz_to_skip);
17181747 r = damon_next_region(r);
@@ -1738,7 +1767,7 @@ static void damos_update_stat(struct damos *s, · 19 unmodified lines
17381767
17391768 static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t,
17401769 struct damon_region *r, struct damos_filter *filter,
1741− unsigned long min_sz_region)
1770+ unsigned long min_region_sz)
17421771 {
17431772 bool matched = false;
17441773 struct damon_target *ti;
@@ -1755,8 +1784,8 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t, · 10 unmodified lines
17551784 matched = target_idx == filter->target_idx;
17561785 break;
17571786 case DAMOS_FILTER_TYPE_ADDR:
1758− start = ALIGN_DOWN(filter->addr_range.start, min_sz_region);
1759− end = ALIGN_DOWN(filter->addr_range.end, min_sz_region);
1787+ start = ALIGN_DOWN(filter->addr_range.start, min_region_sz);
1788+ end = ALIGN_DOWN(filter->addr_range.end, min_region_sz);
17601789
17611790 /* inside the range */
17621791 if (start <= r->ar.start && r->ar.end <= end) {
@@ -1785,14 +1814,14 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t, · 22 unmodified lines
17851814 return matched == filter->matching;
17861815 }
17871816
1788−static bool damos_filter_out(struct damon_ctx *ctx, struct damon_target *t,
1817+static bool damos_core_filter_out(struct damon_ctx *ctx, struct damon_target *t,
17891818 struct damon_region *r, struct damos *s)
17901819 {
17911820 struct damos_filter *filter;
17921821
17931822 s->core_filters_allowed = false;
17941823 damos_for_each_core_filter(filter, s) {
1795− if (damos_filter_match(ctx, t, r, filter, ctx->min_sz_region)) {
1824+ if (damos_filter_match(ctx, t, r, filter, ctx->min_region_sz)) {
17961825 if (filter->allow)
17971826 s->core_filters_allowed = true;
17981827 return !filter->allow;
@@ -1927,12 +1956,12 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t, · 128 unmodified lines
19271956 if (c->ops.apply_scheme) {
19281957 if (quota->esz && quota->charged_sz + sz > quota->esz) {
19291958 sz = ALIGN_DOWN(quota->esz - quota->charged_sz,
1930− c->min_sz_region);
1959+ c->min_region_sz);
19311960 if (!sz)
19321961 goto update_stat;
19331962 damon_split_region_at(t, r, sz);
19341963 }
1935− if (damos_filter_out(c, t, r, s))
1964+ if (damos_core_filter_out(c, t, r, s))
19361965 return;
19371966 ktime_get_coarse_ts64(&begin);
19381967 trace_damos_before_apply(cidx, sidx, tidx, r,
@@ -1975,13 +2004,18 @@ static void damon_do_apply_schemes(struct damon_ctx *c, · 36 unmodified lines
19752004 if (quota->esz && quota->charged_sz >= quota->esz)
19762005 continue;
19772006
1978− if (damos_skip_charged_region(t, &r, s, c->min_sz_region))
2007+ if (damos_skip_charged_region(t, &r, s, c->min_region_sz))
19792008 continue;
19802009
1981− if (!damos_valid_target(c, t, r, s))
2010+ if (s->max_nr_snapshots &&
2011+ s->max_nr_snapshots <= s->stat.nr_snapshots)
19822012 continue;
19832013
1984− damos_apply_scheme(c, t, r, s);
2014+ if (damos_valid_target(c, t, r, s))
2015+ damos_apply_scheme(c, t, r, s);
2016+
2017+ if (damon_is_last_region(r, t))
2018+ s->stat.nr_snapshots++;
19852019 }
19862020 }
19872021
@@ -2078,16 +2112,13 @@ static unsigned long damos_get_node_memcg_used_bp( · 90 unmodified lines
20782112 unsigned long used_pages, numerator;
20792113 struct sysinfo i;
20802114
2081− rcu_read_lock();
2082− memcg = mem_cgroup_from_id(goal->memcg_id);
2083− if (!memcg || !mem_cgroup_tryget(memcg)) {
2084− rcu_read_unlock();
2115+ memcg = mem_cgroup_get_from_id(goal->memcg_id);
2116+ if (!memcg) {
20852117 if (goal->metric == DAMOS_QUOTA_NODE_MEMCG_USED_BP)
20862118 return 0;
20872119 else /* DAMOS_QUOTA_NODE_MEMCG_FREE_BP */
20882120 return 10000;
20892121 }
2090− rcu_read_unlock();
20912122
20922123 mem_cgroup_flush_stats(memcg);
20932124 lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(goal->nid));
@@ -2119,6 +2150,23 @@ static unsigned long damos_get_node_memcg_used_bp( · 25 unmodified lines
21192150 }
21202151 #endif
21212152
2153+/*
2154+ * Returns LRU-active or inactive memory to total LRU memory size ratio.
2155+ */
2156+static unsigned int damos_get_in_active_mem_bp(bool active_ratio)
2157+{
2158+ unsigned long active, inactive, total;
2159+
2160+ /* This should align with /proc/meminfo output */
2161+ active = global_node_page_state(NR_LRU_BASE + LRU_ACTIVE_ANON) +
2162+ global_node_page_state(NR_LRU_BASE + LRU_ACTIVE_FILE);
2163+ inactive = global_node_page_state(NR_LRU_BASE + LRU_INACTIVE_ANON) +
2164+ global_node_page_state(NR_LRU_BASE + LRU_INACTIVE_FILE);
2165+ total = active + inactive;
2166+ if (active_ratio)
2167+ return active * 10000 / total;
2168+ return inactive * 10000 / total;
2169+}
21222170
21232171 static void damos_set_quota_goal_current_value(struct damos_quota_goal *goal)
21242172 {
@@ -2141,6 +2189,11 @@ static void damos_set_quota_goal_current_value(struct damos_quota_goal *goal) · 16 unmodified lines
21412189 case DAMOS_QUOTA_NODE_MEMCG_FREE_BP:
21422190 goal->current_value = damos_get_node_memcg_used_bp(goal);
21432191 break;
2192+ case DAMOS_QUOTA_ACTIVE_MEM_BP:
2193+ case DAMOS_QUOTA_INACTIVE_MEM_BP:
2194+ goal->current_value = damos_get_in_active_mem_bp(
2195+ goal->metric == DAMOS_QUOTA_ACTIVE_MEM_BP);
2196+ break;
21442197 default:
21452198 break;
21462199 }
@@ -2273,6 +2326,22 @@ static void damos_adjust_quota(struct damon_ctx *c, struct damos *s) · 126 unmodified lines
22732326 quota->min_score = score;
22742327 }
22752328
2329+static void damos_trace_stat(struct damon_ctx *c, struct damos *s)
2330+{
2331+ unsigned int cidx = 0, sidx = 0;
2332+ struct damos *siter;
2333+
2334+ if (!trace_damos_stat_after_apply_interval_enabled())
2335+ return;
2336+
2337+ damon_for_each_scheme(siter, c) {
2338+ if (siter == s)
2339+ break;
2340+ sidx++;
2341+ }
2342+ trace_damos_stat_after_apply_interval(cidx, sidx, &s->stat);
2343+}
2344+
22762345 static void kdamond_apply_schemes(struct damon_ctx *c)
22772346 {
22782347 struct damon_target *t;
@@ -2299,6 +2368,9 @@ static void kdamond_apply_schemes(struct damon_ctx *c) · 20 unmodified lines
22992368
23002369 mutex_lock(&c->walk_control_lock);
23012370 damon_for_each_target(t, c) {
2371+ if (c->ops.target_valid && c->ops.target_valid(t) == false)
2372+ continue;
2373+
23022374 damon_for_each_region_safe(r, next_r, t)
23032375 damon_do_apply_schemes(c, t, r);
23042376 }
@@ -2311,6 +2383,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) · 6 unmodified lines
23112383 (s->apply_interval_us ? s->apply_interval_us :
23122384 c->attrs.aggr_interval) / sample_interval;
23132385 s->last_applied = NULL;
2386+ damos_trace_stat(c, s);
23142387 }
23152388 mutex_unlock(&c->walk_control_lock);
23162389 }
@@ -2424,7 +2497,7 @@ static void damon_split_region_at(struct damon_target *t, · 107 unmodified lines
24242497
24252498 /* Split every region in the given target into 'nr_subs' regions */
24262499 static void damon_split_regions_of(struct damon_target *t, int nr_subs,
2427− unsigned long min_sz_region)
2500+ unsigned long min_region_sz)
24282501 {
24292502 struct damon_region *r, *next;
24302503 unsigned long sz_region, sz_sub = 0;
@@ -2434,13 +2507,13 @@ static void damon_split_regions_of(struct damon_target *t, int nr_subs, · 3 unmodified lines
24342507 sz_region = damon_sz_region(r);
24352508
24362509 for (i = 0; i < nr_subs - 1 &&
2437− sz_region > 2 * min_sz_region; i++) {
2510+ sz_region > 2 * min_region_sz; i++) {
24382511 /*
24392512 * Randomly select size of left sub-region to be at
24402513 * least 10 percent and at most 90% of original region
24412514 */
24422515 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
2443− sz_region / 10, min_sz_region);
2516+ sz_region / 10, min_region_sz);
24442517 /* Do not allow blank region */
24452518 if (sz_sub == 0 || sz_sub >= sz_region)
24462519 continue;
@@ -2480,7 +2553,7 @@ static void kdamond_split_regions(struct damon_ctx *ctx) · 33 unmodified lines
24802553 nr_subregions = 3;
24812554
24822555 damon_for_each_target(t, ctx)
2483− damon_split_regions_of(t, nr_subregions, ctx->min_sz_region);
2556+ damon_split_regions_of(t, nr_subregions, ctx->min_region_sz);
24842557
24852558 last_nr_regions = nr_regions;
24862559 }
@@ -2577,41 +2650,30 @@ static void kdamond_usleep(unsigned long usecs) · 90 unmodified lines
25772650 */
25782651 static void kdamond_call(struct damon_ctx *ctx, bool cancel)
25792652 {
2580− struct damon_call_control *control;
2581− LIST_HEAD(repeat_controls);
2582− int ret = 0;
2653+ struct damon_call_control *control, *next;
2654+ LIST_HEAD(controls);
25832655
2584− while (true) {
2585− mutex_lock(&ctx->call_controls_lock);
2586− control = list_first_entry_or_null(&ctx->call_controls,
2587− struct damon_call_control, list);
2588− mutex_unlock(&ctx->call_controls_lock);
2589− if (!control)
2590− break;
2591− if (cancel) {
2656+ mutex_lock(&ctx->call_controls_lock);
2657+ list_splice_tail_init(&ctx->call_controls, &controls);
2658+ mutex_unlock(&ctx->call_controls_lock);
2659+
2660+ list_for_each_entry_safe(control, next, &controls, list) {
2661+ if (!control->repeat || cancel)
2662+ list_del(&control->list);
2663+
2664+ if (cancel)
25922665 control->canceled = true;
2593− } else {
2594− ret = control->fn(control->data);
2595− control->return_code = ret;
2596− }
2597− mutex_lock(&ctx->call_controls_lock);
2598− list_del(&control->list);
2599− mutex_unlock(&ctx->call_controls_lock);
2600− if (!control->repeat) {
2666+ else
2667+ control->return_code = control->fn(control->data);
2668+
2669+ if (!control->repeat)
26012670 complete(&control->completion);
2602− } else if (control->canceled && control->dealloc_on_cancel) {
2671+ else if (control->canceled && control->dealloc_on_cancel)
26032672 kfree(control);
2604− continue;
2605− } else {
2606− list_add(&control->list, &repeat_controls);
2607− }
26082673 }
2609− control = list_first_entry_or_null(&repeat_controls,
2610− struct damon_call_control, list);
2611− if (!control || cancel)
2612− return;
2674+
26132675 mutex_lock(&ctx->call_controls_lock);
2614− list_add_tail(&control->list, &ctx->call_controls);
2676+ list_splice_tail(&controls, &ctx->call_controls);
26152677 mutex_unlock(&ctx->call_controls_lock);
26162678 }
26172679
@@ -2670,8 +2732,6 @@ static void kdamond_init_ctx(struct damon_ctx *ctx) · 52 unmodified lines
26702732 static int kdamond_fn(void *data)
26712733 {
26722734 struct damon_ctx *ctx = data;
2673− struct damon_target *t;
2674− struct damon_region *r, *next;
26752735 unsigned int max_nr_accesses = 0;
26762736 unsigned long sz_limit = 0;
26772737
@@ -2747,7 +2807,7 @@ static int kdamond_fn(void *data) · 69 unmodified lines
27472807 *
27482808 * Reset ->next_aggregation_sis to avoid that.
27492809 * It will anyway correctly updated after this
2750− * if caluse.
2810+ * if clause.
27512811 */
27522812 ctx->next_aggregation_sis =
27532813 next_aggregation_sis;
@@ -2776,47 +2836,29 @@ static int kdamond_fn(void *data) · 22 unmodified lines
27762836 }
27772837 }
27782838 done:
2779− damon_for_each_target(t, ctx) {
2780− damon_for_each_region_safe(r, next, t)
2781− damon_destroy_region(r, t);
2782− }
2839+ damon_destroy_targets(ctx);
27832840
2784− if (ctx->ops.cleanup)
2785− ctx->ops.cleanup(ctx);
27862841 kfree(ctx->regions_score_histogram);
27872842 kdamond_call(ctx, true);
2843+ damos_walk_cancel(ctx);
27882844
27892845 pr_debug("kdamond (%d) finishes\n", current->pid);
27902846 mutex_lock(&ctx->kdamond_lock);
27912847 ctx->kdamond = NULL;
27922848 mutex_unlock(&ctx->kdamond_lock);
27932849
2794− damos_walk_cancel(ctx);
2795−
27962850 mutex_lock(&damon_lock);
27972851 nr_running_ctxs--;
27982852 if (!nr_running_ctxs && running_exclusive_ctxs)
27992853 running_exclusive_ctxs = false;
28002854 mutex_unlock(&damon_lock);
28012855
2802− damon_destroy_targets(ctx);
28032856 return 0;
28042857 }
28052858
2806−/*
2807− * struct damon_system_ram_region - System RAM resource address region of
2808− * [@start, @end).
2809− * @start: Start address of the region (inclusive).
2810− * @end: End address of the region (exclusive).
2811− */
2812−struct damon_system_ram_region {
2813− unsigned long start;
2814− unsigned long end;
2815−};
2816−
28172859 static int walk_system_ram(struct resource *res, void *arg)
28182860 {
2819− struct damon_system_ram_region *a = arg;
2861+ struct damon_addr_range *a = arg;
28202862
28212863 if (a->end - a->start < resource_size(res)) {
28222864 a->start = res->start;
@@ -2833,7 +2875,7 @@ static bool damon_find_biggest_system_ram(unsigned long *start, · 10 unmodified lines
28332875 unsigned long *end)
28342876
28352877 {
2836− struct damon_system_ram_region arg = {};
2878+ struct damon_addr_range arg = {};
28372879
28382880 walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
28392881 if (arg.end <= arg.start)
@@ -2850,7 +2892,7 @@ static bool damon_find_biggest_system_ram(unsigned long *start, · 10 unmodified lines
28502892 * @t: The monitoring target to set the region.
28512893 * @start: The pointer to the start address of the region.
28522894 * @end: The pointer to the end address of the region.
2853− * @min_sz_region: Minimum region size.
2895+ * @min_region_sz: Minimum region size.
28542896 *
28552897 * This function sets the region of @t as requested by @start and @end. If the
28562898 * values of @start and @end are zero, however, this function finds the biggest
@@ -2862,7 +2904,7 @@ static bool damon_find_biggest_system_ram(unsigned long *start, · 5 unmodified lines
28622904 */
28632905 int damon_set_region_biggest_system_ram_default(struct damon_target *t,
28642906 unsigned long *start, unsigned long *end,
2865− unsigned long min_sz_region)
2907+ unsigned long min_region_sz)
28662908 {
28672909 struct damon_addr_range addr_range;
28682910
@@ -2875,7 +2917,7 @@ int damon_set_region_biggest_system_ram_default(struct damon_target *t, · 6 unmodified lines
28752917
28762918 addr_range.start = *start;
28772919 addr_range.end = *end;
2878− return damon_set_regions(t, &addr_range, 1, min_sz_region);
2920+ return damon_set_regions(t, &addr_range, 1, min_region_sz);
28792921 }
28802922
28812923 /*
mm/damon/lru_sort.c+112−8
@@ -34,13 +34,56 @@ static bool enabled __read_mostly; · 33 unmodified lines
3434 *
3535 * Input parameters that updated while DAMON_LRU_SORT is running are not
3636 * applied by default. Once this parameter is set as ``Y``, DAMON_LRU_SORT
37− * reads values of parametrs except ``enabled`` again. Once the re-reading is
37+ * reads values of parameters except ``enabled`` again. Once the re-reading is
3838 * done, this parameter is set as ``N``. If invalid parameters are found while
3939 * the re-reading, DAMON_LRU_SORT will be disabled.
4040 */
4141 static bool commit_inputs __read_mostly;
4242 module_param(commit_inputs, bool, 0600);
4343
44+/*
45+ * Desired active to [in]active memory ratio in bp (1/10,000).
46+ *
47+ * While keeping the caps that set by other quotas, DAMON_LRU_SORT
48+ * automatically increases and decreases the effective level of the quota
49+ * aiming the LRU [de]prioritizations of the hot and cold memory resulting in
50+ * this active to [in]active memory ratio. Value zero means disabling this
51+ * auto-tuning feature.
52+ *
53+ * Disabled by default.
54+ */
55+static unsigned long active_mem_bp __read_mostly;
56+module_param(active_mem_bp, ulong, 0600);
57+
58+/*
59+ * Auto-tune monitoring intervals.
60+ *
61+ * If this parameter is set as ``Y``, DAMON_LRU_SORT automatically tunes
62+ * DAMON's sampling and aggregation intervals. The auto-tuning aims to capture
63+ * meaningful amount of access events in each DAMON-snapshot, while keeping the
64+ * sampling interval 5 milliseconds in minimum, and 10 seconds in maximum.
65+ * Setting this as ``N`` disables the auto-tuning.
66+ *
67+ * Disabled by default.
68+ */
69+static bool autotune_monitoring_intervals __read_mostly;
70+module_param(autotune_monitoring_intervals, bool, 0600);
71+
72+/*
73+ * Filter [non-]young pages accordingly for LRU [de]prioritizations.
74+ *
75+ * If this is set, check page level access (youngness) once again before each
76+ * LRU [de]prioritization operation. LRU prioritization operation is skipped
77+ * if the page has not accessed since the last check (not young). LRU
78+ * deprioritization operation is skipped if the page has accessed since the
79+ * last check (young). The feature is enabled or disabled if this parameter is
80+ * set as ``Y`` or ``N``, respectively.
81+ *
82+ * Disabled by default.
83+ */
84+static bool filter_young_pages __read_mostly;
85+module_param(filter_young_pages, bool, 0600);
86+
4487 /*
4588 * Access frequency threshold for hot memory regions identification in permil.
4689 *
@@ -71,7 +114,7 @@ static struct damos_quota damon_lru_sort_quota = { · 24 unmodified lines
71114 /* Within the quota, mark hotter regions accessed first. */
72115 .weight_sz = 0,
73116 .weight_nr_accesses = 1,
74− .weight_age = 0,
117+ .weight_age = 1,
75118 };
76119 DEFINE_DAMON_MODULES_DAMOS_TIME_QUOTA(damon_lru_sort_quota);
77120
@@ -193,10 +236,53 @@ static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres) · 115 unmodified lines
193236 return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_DEPRIO);
194237 }
195238
239+static int damon_lru_sort_add_quota_goals(struct damos *hot_scheme,
240+ struct damos *cold_scheme)
241+{
242+ struct damos_quota_goal *goal;
243+
244+ if (!active_mem_bp)
245+ return 0;
246+ goal = damos_new_quota_goal(DAMOS_QUOTA_ACTIVE_MEM_BP, active_mem_bp);
247+ if (!goal)
248+ return -ENOMEM;
249+ damos_add_quota_goal(&hot_scheme->quota, goal);
250+ /* aim 0.2 % goal conflict, to keep little ping pong */
251+ goal = damos_new_quota_goal(DAMOS_QUOTA_INACTIVE_MEM_BP,
252+ 10000 - active_mem_bp + 2);
253+ if (!goal)
254+ return -ENOMEM;
255+ damos_add_quota_goal(&cold_scheme->quota, goal);
256+ return 0;
257+}
258+
259+static int damon_lru_sort_add_filters(struct damos *hot_scheme,
260+ struct damos *cold_scheme)
261+{
262+ struct damos_filter *filter;
263+
264+ if (!filter_young_pages)
265+ return 0;
266+
267+ /* disallow prioritizing not-young pages */
268+ filter = damos_new_filter(DAMOS_FILTER_TYPE_YOUNG, false, false);
269+ if (!filter)
270+ return -ENOMEM;
271+ damos_add_filter(hot_scheme, filter);
272+
273+ /* disabllow de-prioritizing young pages */
274+ filter = damos_new_filter(DAMOS_FILTER_TYPE_YOUNG, true, false);
275+ if (!filter)
276+ return -ENOMEM;
277+ damos_add_filter(cold_scheme, filter);
278+ return 0;
279+}
280+
196281 static int damon_lru_sort_apply_parameters(void)
197282 {
198283 struct damon_ctx *param_ctx;
199284 struct damon_target *param_target;
285+ struct damon_attrs attrs;
200286 struct damos *hot_scheme, *cold_scheme;
201287 unsigned int hot_thres, cold_thres;
202288 int err;
@@ -212,25 +298,34 @@ static int damon_lru_sort_apply_parameters(void) · 9 unmodified lines
212298 if (!monitor_region_start && !monitor_region_end)
213299 addr_unit = 1;
214300 param_ctx->addr_unit = addr_unit;
215− param_ctx->min_sz_region = max(DAMON_MIN_REGION / addr_unit, 1);
301+ param_ctx->min_region_sz = max(DAMON_MIN_REGION_SZ / addr_unit, 1);
216302
217303 if (!damon_lru_sort_mon_attrs.sample_interval) {
218304 err = -EINVAL;
219305 goto out;
220306 }
221307
222− err = damon_set_attrs(param_ctx, &damon_lru_sort_mon_attrs);
308+ attrs = damon_lru_sort_mon_attrs;
309+ if (autotune_monitoring_intervals) {
310+ attrs.sample_interval = 5000;
311+ attrs.aggr_interval = 100000;
312+ attrs.intervals_goal.access_bp = 40;
313+ attrs.intervals_goal.aggrs = 3;
314+ attrs.intervals_goal.min_sample_us = 5000;
315+ attrs.intervals_goal.max_sample_us = 10 * 1000 * 1000;
316+ }
317+ err = damon_set_attrs(param_ctx, &attrs);
223318 if (err)
224319 goto out;
225320
226321 err = -ENOMEM;
227− hot_thres = damon_max_nr_accesses(&damon_lru_sort_mon_attrs) *
322+ hot_thres = damon_max_nr_accesses(&attrs) *
228323 hot_thres_access_freq / 1000;
229324 hot_scheme = damon_lru_sort_new_hot_scheme(hot_thres);
230325 if (!hot_scheme)
231326 goto out;
232327
233− cold_thres = cold_min_age / damon_lru_sort_mon_attrs.aggr_interval;
328+ cold_thres = cold_min_age / attrs.aggr_interval;
234329 cold_scheme = damon_lru_sort_new_cold_scheme(cold_thres);
235330 if (!cold_scheme) {
236331 damon_destroy_scheme(hot_scheme);
@@ -240,10 +335,17 @@ static int damon_lru_sort_apply_parameters(void) · 3 unmodified lines
240335 damon_set_schemes(param_ctx, &hot_scheme, 1);
241336 damon_add_scheme(param_ctx, cold_scheme);
242337
338+ err = damon_lru_sort_add_quota_goals(hot_scheme, cold_scheme);
339+ if (err)
340+ goto out;
341+ err = damon_lru_sort_add_filters(hot_scheme, cold_scheme);
342+ if (err)
343+ goto out;
344+
243345 err = damon_set_region_biggest_system_ram_default(param_target,
244346 &monitor_region_start,
245347 &monitor_region_end,
246− param_ctx->min_sz_region);
348+ param_ctx->min_region_sz);
247349 if (err)
248350 goto out;
249351 err = damon_commit_ctx(ctx, param_ctx);
@@ -303,7 +405,9 @@ static int damon_lru_sort_turn(bool on) · 53 unmodified lines
303405 err = damon_start(&ctx, 1, true);
304406 if (err)
305407 return err;
306− kdamond_pid = ctx->kdamond->pid;
408+ kdamond_pid = damon_kdamond_pid(ctx);
409+ if (kdamond_pid < 0)
410+ return kdamond_pid;
307411 return damon_call(ctx, &call_control);
308412 }
309413
mm/damon/paddr.c+12−15
@@ -156,7 +156,7 @@ static unsigned long damon_pa_pageout(struct damon_region *r, · 155 unmodified lines
156156 LIST_HEAD(folio_list);
157157 bool install_young_filter = true;
158158 struct damos_filter *filter;
159− struct folio *folio;
159+ struct folio *folio = NULL;
160160
161161 /* check access in page level again by default */
162162 damos_for_each_ops_filter(filter, s) {
@@ -206,13 +206,13 @@ put_folio: · 43 unmodified lines
206206 return damon_pa_core_addr(applied * PAGE_SIZE, addr_unit);
207207 }
208208
209−static inline unsigned long damon_pa_mark_accessed_or_deactivate(
209+static inline unsigned long damon_pa_de_activate(
210210 struct damon_region *r, unsigned long addr_unit,
211− struct damos *s, bool mark_accessed,
211+ struct damos *s, bool activate,
212212 unsigned long *sz_filter_passed)
213213 {
214214 phys_addr_t addr, applied = 0;
215− struct folio *folio;
215+ struct folio *folio = NULL;
216216
217217 addr = damon_pa_phys_addr(r->ar.start, addr_unit);
218218 while (addr < damon_pa_phys_addr(r->ar.end, addr_unit)) {
@@ -227,8 +227,8 @@ static inline unsigned long damon_pa_mark_accessed_or_deactivate( · 8 unmodified lines
227227 else
228228 *sz_filter_passed += folio_size(folio) / addr_unit;
229229
230− if (mark_accessed)
231− folio_mark_accessed(folio);
230+ if (activate)
231+ folio_activate(folio);
232232 else
233233 folio_deactivate(folio);
234234 applied += folio_nr_pages(folio);
@@ -240,20 +240,18 @@ put_folio: · 5 unmodified lines
240240 return damon_pa_core_addr(applied * PAGE_SIZE, addr_unit);
241241 }
242242
243−static unsigned long damon_pa_mark_accessed(struct damon_region *r,
243+static unsigned long damon_pa_activate_pages(struct damon_region *r,
244244 unsigned long addr_unit, struct damos *s,
245245 unsigned long *sz_filter_passed)
246246 {
247− return damon_pa_mark_accessed_or_deactivate(r, addr_unit, s, true,
248− sz_filter_passed);
247+ return damon_pa_de_activate(r, addr_unit, s, true, sz_filter_passed);
249248 }
250249
251250 static unsigned long damon_pa_deactivate_pages(struct damon_region *r,
252251 unsigned long addr_unit, struct damos *s,
253252 unsigned long *sz_filter_passed)
254253 {
255− return damon_pa_mark_accessed_or_deactivate(r, addr_unit, s, false,
256− sz_filter_passed);
254+ return damon_pa_de_activate(r, addr_unit, s, false, sz_filter_passed);
257255 }
258256
259257 static unsigned long damon_pa_migrate(struct damon_region *r,
@@ -262,7 +260,7 @@ static unsigned long damon_pa_migrate(struct damon_region *r, · 2 unmodified lines
262260 {
263261 phys_addr_t addr, applied;
264262 LIST_HEAD(folio_list);
265− struct folio *folio;
263+ struct folio *folio = NULL;
266264
267265 addr = damon_pa_phys_addr(r->ar.start, addr_unit);
268266 while (addr < damon_pa_phys_addr(r->ar.end, addr_unit)) {
@@ -295,7 +293,7 @@ static unsigned long damon_pa_stat(struct damon_region *r, · 26 unmodified lines
295293 unsigned long *sz_filter_passed)
296294 {
297295 phys_addr_t addr;
298− struct folio *folio;
296+ struct folio *folio = NULL;
299297
300298 if (!damos_ops_has_filter(s))
301299 return 0;
@@ -327,7 +325,7 @@ static unsigned long damon_pa_apply_scheme(struct damon_ctx *ctx, · 25 unmodified lines
327325 case DAMOS_PAGEOUT:
328326 return damon_pa_pageout(r, aunit, scheme, sz_filter_passed);
329327 case DAMOS_LRU_PRIO:
330− return damon_pa_mark_accessed(r, aunit, scheme,
328+ return damon_pa_activate_pages(r, aunit, scheme,
331329 sz_filter_passed);
332330 case DAMOS_LRU_DEPRIO:
333331 return damon_pa_deactivate_pages(r, aunit, scheme,
@@ -375,7 +373,6 @@ static int __init damon_pa_initcall(void) · 41 unmodified lines
375373 .prepare_access_checks = damon_pa_prepare_access_checks,
376374 .check_accesses = damon_pa_check_accesses,
377375 .target_valid = NULL,
378− .cleanup = NULL,
379376 .apply_scheme = damon_pa_apply_scheme,
380377 .get_scheme_score = damon_pa_scheme_score,
381378 };
mm/damon/reclaim.c+6−4
@@ -34,7 +34,7 @@ static bool enabled __read_mostly; · 33 unmodified lines
3434 *
3535 * Input parameters that updated while DAMON_RECLAIM is running are not applied
3636 * by default. Once this parameter is set as ``Y``, DAMON_RECLAIM reads values
37− * of parametrs except ``enabled`` again. Once the re-reading is done, this
37+ * of parameters except ``enabled`` again. Once the re-reading is done, this
3838 * parameter is set as ``N``. If invalid parameters are found while the
3939 * re-reading, DAMON_RECLAIM will be disabled.
4040 */
@@ -208,7 +208,7 @@ static int damon_reclaim_apply_parameters(void) · 167 unmodified lines
208208 if (!monitor_region_start && !monitor_region_end)
209209 addr_unit = 1;
210210 param_ctx->addr_unit = addr_unit;
211− param_ctx->min_sz_region = max(DAMON_MIN_REGION / addr_unit, 1);
211+ param_ctx->min_region_sz = max(DAMON_MIN_REGION_SZ / addr_unit, 1);
212212
213213 if (!damon_reclaim_mon_attrs.aggr_interval) {
214214 err = -EINVAL;
@@ -251,7 +251,7 @@ static int damon_reclaim_apply_parameters(void) · 36 unmodified lines
251251 err = damon_set_region_biggest_system_ram_default(param_target,
252252 &monitor_region_start,
253253 &monitor_region_end,
254− param_ctx->min_sz_region);
254+ param_ctx->min_region_sz);
255255 if (err)
256256 goto out;
257257 err = damon_commit_ctx(ctx, param_ctx);
@@ -307,7 +307,9 @@ static int damon_reclaim_turn(bool on) · 49 unmodified lines
307307 err = damon_start(&ctx, 1, true);
308308 if (err)
309309 return err;
310− kdamond_pid = ctx->kdamond->pid;
310+ kdamond_pid = damon_kdamond_pid(ctx);
311+ if (kdamond_pid < 0)
312+ return kdamond_pid;
311313 return damon_call(ctx, &call_control);
312314 }
313315
mm/damon/stat.c+3−11
@@ -1,6 +1,6 @@
11 // SPDX-License-Identifier: GPL-2.0
22 /*
3− * Shows data access monitoring resutls in simple metrics.
3+ * Shows data access monitoring results in simple metrics.
44 */
55
66 #define pr_fmt(fmt) "damon-stat: " fmt
@@ -34,7 +34,7 @@ module_param(estimated_memory_bandwidth, ulong, 0400); · 27 unmodified lines
3434 MODULE_PARM_DESC(estimated_memory_bandwidth,
3535 "Estimated memory bandwidth usage in bytes per second");
3636
37−static long memory_idle_ms_percentiles[101] __read_mostly = {0,};
37+static long memory_idle_ms_percentiles[101] = {0,};
3838 module_param_array(memory_idle_ms_percentiles, long, NULL, 0400);
3939 MODULE_PARM_DESC(memory_idle_ms_percentiles,
4040 "Memory idle time percentiles in milliseconds");
@@ -173,14 +173,6 @@ static struct damon_ctx *damon_stat_build_ctx(void) · 132 unmodified lines
173173 if (damon_set_attrs(ctx, &attrs))
174174 goto free_out;
175175
176− /*
177− * auto-tune sampling and aggregation interval aiming 4% DAMON-observed
178− * accesses ratio, keeping sampling interval in [5ms, 10s] range.
179− */
180− ctx->attrs.intervals_goal = (struct damon_intervals_goal) {
181− .access_bp = 400, .aggrs = 3,
182− .min_sample_us = 5000, .max_sample_us = 10000000,
183− };
184176 if (damon_select_ops(ctx, DAMON_OPS_PADDR))
185177 goto free_out;
186178
@@ -189,7 +181,7 @@ static struct damon_ctx *damon_stat_build_ctx(void) · 2 unmodified lines
189181 goto free_out;
190182 damon_add_target(ctx, target);
191183 if (damon_set_region_biggest_system_ram_default(target, &start, &end,
192− ctx->min_sz_region))
184+ ctx->min_region_sz))
193185 goto free_out;
194186 return ctx;
195187 free_out:
mm/damon/sysfs-schemes.c+54−4
@@ -204,6 +204,8 @@ struct damon_sysfs_stats { · 203 unmodified lines
204204 unsigned long sz_applied;
205205 unsigned long sz_ops_filter_passed;
206206 unsigned long qt_exceeds;
207+ unsigned long nr_snapshots;
208+ unsigned long max_nr_snapshots;
207209 };
208210
209211 static struct damon_sysfs_stats *damon_sysfs_stats_alloc(void)
@@ -265,6 +267,37 @@ static ssize_t qt_exceeds_show(struct kobject *kobj, · 55 unmodified lines
265267 return sysfs_emit(buf, "%lu\n", stats->qt_exceeds);
266268 }
267269
270+static ssize_t nr_snapshots_show(struct kobject *kobj,
271+ struct kobj_attribute *attr, char *buf)
272+{
273+ struct damon_sysfs_stats *stats = container_of(kobj,
274+ struct damon_sysfs_stats, kobj);
275+
276+ return sysfs_emit(buf, "%lu\n", stats->nr_snapshots);
277+}
278+
279+static ssize_t max_nr_snapshots_show(struct kobject *kobj,
280+ struct kobj_attribute *attr, char *buf)
281+{
282+ struct damon_sysfs_stats *stats = container_of(kobj,
283+ struct damon_sysfs_stats, kobj);
284+
285+ return sysfs_emit(buf, "%lu\n", stats->max_nr_snapshots);
286+}
287+
288+static ssize_t max_nr_snapshots_store(struct kobject *kobj,
289+ struct kobj_attribute *attr, const char *buf, size_t count)
290+{
291+ struct damon_sysfs_stats *stats = container_of(kobj,
292+ struct damon_sysfs_stats, kobj);
293+ unsigned long max_nr_snapshots, err = kstrtoul(buf, 0, &max_nr_snapshots);
294+
295+ if (err)
296+ return err;
297+ stats->max_nr_snapshots = max_nr_snapshots;
298+ return count;
299+}
300+
268301 static void damon_sysfs_stats_release(struct kobject *kobj)
269302 {
270303 kfree(container_of(kobj, struct damon_sysfs_stats, kobj));
@@ -288,6 +321,12 @@ static struct kobj_attribute damon_sysfs_stats_sz_ops_filter_passed_attr = · 17 unmodified lines
288321 static struct kobj_attribute damon_sysfs_stats_qt_exceeds_attr =
289322 __ATTR_RO_MODE(qt_exceeds, 0400);
290323
324+static struct kobj_attribute damon_sysfs_stats_nr_snapshots_attr =
325+ __ATTR_RO_MODE(nr_snapshots, 0400);
326+
327+static struct kobj_attribute damon_sysfs_stats_max_nr_snapshots_attr =
328+ __ATTR_RW_MODE(max_nr_snapshots, 0600);
329+
291330 static struct attribute *damon_sysfs_stats_attrs[] = {
292331 &damon_sysfs_stats_nr_tried_attr.attr,
293332 &damon_sysfs_stats_sz_tried_attr.attr,
@@ -295,6 +334,8 @@ static struct attribute *damon_sysfs_stats_attrs[] = { · 1 unmodified line
295334 &damon_sysfs_stats_sz_applied_attr.attr,
296335 &damon_sysfs_stats_sz_ops_filter_passed_attr.attr,
297336 &damon_sysfs_stats_qt_exceeds_attr.attr,
337+ &damon_sysfs_stats_nr_snapshots_attr.attr,
338+ &damon_sysfs_stats_max_nr_snapshots_attr.attr,
298339 NULL,
299340 };
300341 ATTRIBUTE_GROUPS(damon_sysfs_stats);
@@ -1038,6 +1079,14 @@ struct damos_sysfs_qgoal_metric_name damos_sysfs_qgoal_metric_names[] = { · 737 unmodified lines
10381079 .metric = DAMOS_QUOTA_NODE_MEMCG_FREE_BP,
10391080 .name = "node_memcg_free_bp",
10401081 },
1082+ {
1083+ .metric = DAMOS_QUOTA_ACTIVE_MEM_BP,
1084+ .name = "active_mem_bp",
1085+ },
1086+ {
1087+ .metric = DAMOS_QUOTA_INACTIVE_MEM_BP,
1088+ .name = "inactive_mem_bp",
1089+ },
10411090 };
10421091
10431092 static ssize_t target_metric_show(struct kobject *kobj,
@@ -2288,7 +2337,6 @@ static ssize_t target_nid_store(struct kobject *kobj, · 1244 unmodified lines
22882337 struct damon_sysfs_scheme, kobj);
22892338 int err = 0;
22902339
2291− /* TODO: error handling for target_nid range. */
22922340 err = kstrtoint(buf, 0, &scheme->target_nid);
22932341
22942342 return err ? err : count;
@@ -2454,7 +2502,7 @@ static bool damon_sysfs_memcg_path_eq(struct mem_cgroup *memcg, · 159 unmodified lines
24542502 return false;
24552503 }
24562504
2457−static int damon_sysfs_memcg_path_to_id(char *memcg_path, unsigned short *id)
2505+static int damon_sysfs_memcg_path_to_id(char *memcg_path, u64 *id)
24582506 {
24592507 struct mem_cgroup *memcg;
24602508 char *path;
@@ -2469,8 +2517,8 @@ static int damon_sysfs_memcg_path_to_id(char *memcg_path, unsigned short *id) · 8 unmodified lines
24692517
24702518 for (memcg = mem_cgroup_iter(NULL, NULL, NULL); memcg;
24712519 memcg = mem_cgroup_iter(NULL, memcg, NULL)) {
2472− /* skip removed memcg */
2473− if (!mem_cgroup_id(memcg))
2520+ /* skip offlined memcg */
2521+ if (!mem_cgroup_online(memcg))
24742522 continue;
24752523 if (damon_sysfs_memcg_path_eq(memcg, path, memcg_path)) {
24762524 *id = mem_cgroup_id(memcg);
@@ -2719,6 +2767,7 @@ static struct damos *damon_sysfs_mk_scheme( · 242 unmodified lines
27192767 damon_destroy_scheme(scheme);
27202768 return NULL;
27212769 }
2770+ scheme->max_nr_snapshots = sysfs_scheme->stats->max_nr_snapshots;
27222771 return scheme;
27232772 }
27242773
@@ -2763,6 +2812,7 @@ void damon_sysfs_schemes_update_stats( · 38 unmodified lines
27632812 sysfs_stats->sz_ops_filter_passed =
27642813 scheme->stat.sz_ops_filter_passed;
27652814 sysfs_stats->qt_exceeds = scheme->stat.qt_exceeds;
2815+ sysfs_stats->nr_snapshots = scheme->stat.nr_snapshots;
27662816 }
27672817 }
27682818
mm/damon/sysfs.c+9−9
@@ -1365,7 +1365,7 @@ static int damon_sysfs_set_attrs(struct damon_ctx *ctx, · 1364 unmodified lines
13651365
13661366 static int damon_sysfs_set_regions(struct damon_target *t,
13671367 struct damon_sysfs_regions *sysfs_regions,
1368− unsigned long min_sz_region)
1368+ unsigned long min_region_sz)
13691369 {
13701370 struct damon_addr_range *ranges = kmalloc_array(sysfs_regions->nr,
13711371 sizeof(*ranges), GFP_KERNEL | __GFP_NOWARN);
@@ -1387,7 +1387,7 @@ static int damon_sysfs_set_regions(struct damon_target *t, · 15 unmodified lines
13871387 if (ranges[i - 1].end > ranges[i].start)
13881388 goto out;
13891389 }
1390− err = damon_set_regions(t, ranges, sysfs_regions->nr, min_sz_region);
1390+ err = damon_set_regions(t, ranges, sysfs_regions->nr, min_region_sz);
13911391 out:
13921392 kfree(ranges);
13931393 return err;
@@ -1409,7 +1409,8 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target, · 15 unmodified lines
14091409 return -EINVAL;
14101410 }
14111411 t->obsolete = sys_target->obsolete;
1412− return damon_sysfs_set_regions(t, sys_target->regions, ctx->min_sz_region);
1412+ return damon_sysfs_set_regions(t, sys_target->regions,
1413+ ctx->min_region_sz);
14131414 }
14141415
14151416 static int damon_sysfs_add_targets(struct damon_ctx *ctx,
@@ -1469,8 +1470,8 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx, · 53 unmodified lines
14691470 ctx->addr_unit = sys_ctx->addr_unit;
14701471 /* addr_unit is respected by only DAMON_OPS_PADDR */
14711472 if (sys_ctx->ops_id == DAMON_OPS_PADDR)
1472− ctx->min_sz_region = max(
1473− DAMON_MIN_REGION / sys_ctx->addr_unit, 1);
1473+ ctx->min_region_sz = max(
1474+ DAMON_MIN_REGION_SZ / sys_ctx->addr_unit, 1);
14741475 err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
14751476 if (err)
14761477 return err;
@@ -1819,10 +1820,9 @@ static ssize_t pid_show(struct kobject *kobj, · 342 unmodified lines
18191820 if (!ctx)
18201821 goto out;
18211822
1822− mutex_lock(&ctx->kdamond_lock);
1823− if (ctx->kdamond)
1824− pid = ctx->kdamond->pid;
1825− mutex_unlock(&ctx->kdamond_lock);
1823+ pid = damon_kdamond_pid(ctx);
1824+ if (pid < 0)
1825+ pid = -1;
18261826 out:
18271827 mutex_unlock(&damon_sysfs_lock);
18281828 return sysfs_emit(buf, "%d\n", pid);
mm/damon/tests/core-kunit.h+39−12
@@ -158,6 +158,7 @@ static void damon_test_split_at(struct kunit *test) · 157 unmodified lines
158158 r->nr_accesses_bp = 420000;
159159 r->nr_accesses = 42;
160160 r->last_nr_accesses = 15;
161+ r->age = 10;
161162 damon_add_region(r, t);
162163 damon_split_region_at(t, r, 25);
163164 KUNIT_EXPECT_EQ(test, r->ar.start, 0ul);
@@ -170,6 +171,7 @@ static void damon_test_split_at(struct kunit *test) · 6 unmodified lines
170171 KUNIT_EXPECT_EQ(test, r->nr_accesses_bp, r_new->nr_accesses_bp);
171172 KUNIT_EXPECT_EQ(test, r->nr_accesses, r_new->nr_accesses);
172173 KUNIT_EXPECT_EQ(test, r->last_nr_accesses, r_new->last_nr_accesses);
174+ KUNIT_EXPECT_EQ(test, r->age, r_new->age);
173175
174176 damon_free_target(t);
175177 }
@@ -190,6 +192,7 @@ static void damon_test_merge_two(struct kunit *test) · 14 unmodified lines
190192 }
191193 r->nr_accesses = 10;
192194 r->nr_accesses_bp = 100000;
195+ r->age = 9;
193196 damon_add_region(r, t);
194197 r2 = damon_new_region(100, 300);
195198 if (!r2) {
@@ -198,12 +201,15 @@ static void damon_test_merge_two(struct kunit *test) · 2 unmodified lines
198201 }
199202 r2->nr_accesses = 20;
200203 r2->nr_accesses_bp = 200000;
204+ r2->age = 21;
201205 damon_add_region(r2, t);
202206
203207 damon_merge_two_regions(t, r, r2);
204208 KUNIT_EXPECT_EQ(test, r->ar.start, 0ul);
205209 KUNIT_EXPECT_EQ(test, r->ar.end, 300ul);
206210 KUNIT_EXPECT_EQ(test, r->nr_accesses, 16u);
211+ KUNIT_EXPECT_EQ(test, r->nr_accesses_bp, 160000u);
212+ KUNIT_EXPECT_EQ(test, r->age, 17u);
207213
208214 i = 0;
209215 damon_for_each_region(r3, t) {
@@ -232,12 +238,12 @@ static void damon_test_merge_regions_of(struct kunit *test) · 22 unmodified lines
232238 {
233239 struct damon_target *t;
234240 struct damon_region *r;
235− unsigned long sa[] = {0, 100, 114, 122, 130, 156, 170, 184};
236− unsigned long ea[] = {100, 112, 122, 130, 156, 170, 184, 230};
237− unsigned int nrs[] = {0, 0, 10, 10, 20, 30, 1, 2};
241+ unsigned long sa[] = {0, 100, 114, 122, 130, 156, 170, 184, 230};
242+ unsigned long ea[] = {100, 112, 122, 130, 156, 170, 184, 230, 10170};
243+ unsigned int nrs[] = {0, 0, 10, 10, 20, 30, 1, 2, 5};
238244
239− unsigned long saddrs[] = {0, 114, 130, 156, 170};
240− unsigned long eaddrs[] = {112, 130, 156, 170, 230};
245+ unsigned long saddrs[] = {0, 114, 130, 156, 170, 230};
246+ unsigned long eaddrs[] = {112, 130, 156, 170, 230, 10170};
241247 int i;
242248
243249 t = damon_new_target();
@@ -255,9 +261,9 @@ static void damon_test_merge_regions_of(struct kunit *test) · 11 unmodified lines
255261 }
256262
257263 damon_merge_regions_of(t, 9, 9999);
258− /* 0-112, 114-130, 130-156, 156-170 */
259− KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 5u);
260− for (i = 0; i < 5; i++) {
264+ /* 0-112, 114-130, 130-156, 156-170, 170-230, 230-10170 */
265+ KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 6u);
266+ for (i = 0; i < 6; i++) {
261267 r = __nth_region_of(t, i);
262268 KUNIT_EXPECT_EQ(test, r->ar.start, saddrs[i]);
263269 KUNIT_EXPECT_EQ(test, r->ar.end, eaddrs[i]);
@@ -269,6 +275,9 @@ static void damon_test_split_regions_of(struct kunit *test) · 5 unmodified lines
269275 {
270276 struct damon_target *t;
271277 struct damon_region *r;
278+ unsigned long sa[] = {0, 300, 500};
279+ unsigned long ea[] = {220, 400, 700};
280+ int i;
272281
273282 t = damon_new_target();
274283 if (!t)
@@ -295,6 +304,23 @@ static void damon_test_split_regions_of(struct kunit *test) · 20 unmodified lines
295304 damon_split_regions_of(t, 4, 1);
296305 KUNIT_EXPECT_LE(test, damon_nr_regions(t), 4u);
297306 damon_free_target(t);
307+
308+ t = damon_new_target();
309+ if (!t)
310+ kunit_skip(test, "third target alloc fail");
311+ for (i = 0; i < ARRAY_SIZE(sa); i++) {
312+ r = damon_new_region(sa[i], ea[i]);
313+ if (!r) {
314+ damon_free_target(t);
315+ kunit_skip(test, "region alloc fail");
316+ }
317+ damon_add_region(r, t);
318+ }
319+ damon_split_regions_of(t, 4, 5);
320+ KUNIT_EXPECT_LE(test, damon_nr_regions(t), 12u);
321+ damon_for_each_region(r, t)
322+ KUNIT_EXPECT_GE(test, damon_sz_region(r) % 5ul, 0ul);
323+ damon_free_target(t);
298324 }
299325
300326 static void damon_test_ops_registration(struct kunit *test)
@@ -574,9 +600,10 @@ static void damos_test_commit_quota_goal(struct kunit *test) · 273 unmodified lines
574600 });
575601 damos_test_commit_quota_goal_for(test, &dst,
576602 &(struct damos_quota_goal) {
577− .metric = DAMOS_QUOTA_USER_INPUT,
578− .target_value = 789,
579− .current_value = 12,
603+ .metric = DAMOS_QUOTA_SOME_MEM_PSI_US,
604+ .target_value = 234,
605+ .current_value = 345,
606+ .last_psi_total = 567,
580607 });
581608 }
582609
@@ -1159,7 +1186,7 @@ static void damon_test_set_filters_default_reject(struct kunit *test) · 576 unmodified lines
11591186 damos_set_filters_default_reject(&scheme);
11601187 /*
11611188 * A core-handled allow-filter is installed.
1162− * Rejct by default on core layer filtering stage due to the last
1189+ * Reject by default on core layer filtering stage due to the last
11631190 * core-layer-filter's behavior.
11641191 * Allow by default on ops layer filtering stage due to the absence of
11651192 * ops layer filters.
mm/damon/tests/vaddr-kunit.h+1−1
@@ -147,7 +147,7 @@ static void damon_do_test_apply_three_regions(struct kunit *test, · 146 unmodified lines
147147 damon_add_region(r, t);
148148 }
149149
150− damon_set_regions(t, three_regions, 3, DAMON_MIN_REGION);
150+ damon_set_regions(t, three_regions, 3, DAMON_MIN_REGION_SZ);
151151
152152 for (i = 0; i < nr_expected / 2; i++) {
153153 r = __nth_region_of(t, i);
mm/damon/vaddr.c+12−13
@@ -19,8 +19,8 @@ · 18 unmodified lines
1919 #include "ops-common.h"
2020
2121 #ifdef CONFIG_DAMON_VADDR_KUNIT_TEST
22−#undef DAMON_MIN_REGION
23−#define DAMON_MIN_REGION 1
22+#undef DAMON_MIN_REGION_SZ
23+#define DAMON_MIN_REGION_SZ 1
2424 #endif
2525
2626 /*
@@ -78,7 +78,7 @@ static int damon_va_evenly_split_region(struct damon_target *t, · 51 unmodified lines
7878
7979 orig_end = r->ar.end;
8080 sz_orig = damon_sz_region(r);
81− sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION);
81+ sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION_SZ);
8282
8383 if (!sz_piece)
8484 return -EINVAL;
@@ -161,12 +161,12 @@ next: · 76 unmodified lines
161161 swap(first_gap, second_gap);
162162
163163 /* Store the result */
164− regions[0].start = ALIGN(start, DAMON_MIN_REGION);
165− regions[0].end = ALIGN(first_gap.start, DAMON_MIN_REGION);
166− regions[1].start = ALIGN(first_gap.end, DAMON_MIN_REGION);
167− regions[1].end = ALIGN(second_gap.start, DAMON_MIN_REGION);
168− regions[2].start = ALIGN(second_gap.end, DAMON_MIN_REGION);
169− regions[2].end = ALIGN(prev->vm_end, DAMON_MIN_REGION);
164+ regions[0].start = ALIGN(start, DAMON_MIN_REGION_SZ);
165+ regions[0].end = ALIGN(first_gap.start, DAMON_MIN_REGION_SZ);
166+ regions[1].start = ALIGN(first_gap.end, DAMON_MIN_REGION_SZ);
167+ regions[1].end = ALIGN(second_gap.start, DAMON_MIN_REGION_SZ);
168+ regions[2].start = ALIGN(second_gap.end, DAMON_MIN_REGION_SZ);
169+ regions[2].end = ALIGN(prev->vm_end, DAMON_MIN_REGION_SZ);
170170
171171 return 0;
172172 }
@@ -259,8 +259,8 @@ static void __damon_va_init_regions(struct damon_ctx *ctx, · 86 unmodified lines
259259 sz += regions[i].end - regions[i].start;
260260 if (ctx->attrs.min_nr_regions)
261261 sz /= ctx->attrs.min_nr_regions;
262− if (sz < DAMON_MIN_REGION)
263− sz = DAMON_MIN_REGION;
262+ if (sz < DAMON_MIN_REGION_SZ)
263+ sz = DAMON_MIN_REGION_SZ;
264264
265265 /* Set the initial three regions of the target */
266266 for (i = 0; i < 3; i++) {
@@ -299,7 +299,7 @@ static void damon_va_update(struct damon_ctx *ctx) · 32 unmodified lines
299299 damon_for_each_target(t, ctx) {
300300 if (damon_va_three_regions(t, three_regions))
301301 continue;
302− damon_set_regions(t, three_regions, 3, DAMON_MIN_REGION);
302+ damon_set_regions(t, three_regions, 3, DAMON_MIN_REGION_SZ);
303303 }
304304 }
305305
@@ -1014,7 +1014,6 @@ static int __init damon_va_initcall(void) · 708 unmodified lines
10141014 .check_accesses = damon_va_check_accesses,
10151015 .target_valid = damon_va_target_valid,
10161016 .cleanup_target = damon_va_cleanup_target,
1017− .cleanup = NULL,
10181017 .apply_scheme = damon_va_apply_scheme,
10191018 .get_scheme_score = damon_va_scheme_score,
10201019 };
mm/debug_vm_pgtable.c+17−21
@@ -971,22 +971,26 @@ static unsigned long __init get_random_vaddr(void) · 970 unmodified lines
971971 return random_vaddr;
972972 }
973973
974−static void __init destroy_args(struct pgtable_debug_args *args)
974+static void __init
975+debug_vm_pgtable_free_huge_page(struct pgtable_debug_args *args,
976+ unsigned long pfn, int order)
975977 {
976− struct page *page = NULL;
978+#ifdef CONFIG_CONTIG_ALLOC
979+ if (args->is_contiguous_page) {
980+ free_contig_range(pfn, 1 << order);
981+ return;
982+ }
983+#endif
984+ __free_pages(pfn_to_page(pfn), order);
985+}
977986
987+static void __init destroy_args(struct pgtable_debug_args *args)
988+{
978989 /* Free (huge) page */
979990 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
980991 has_transparent_pud_hugepage() &&
981992 args->pud_pfn != ULONG_MAX) {
982− if (args->is_contiguous_page) {
983− free_contig_range(args->pud_pfn,
984− (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
985− } else {
986− page = pfn_to_page(args->pud_pfn);
987− __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
988− }
989−
993+ debug_vm_pgtable_free_huge_page(args, args->pud_pfn, HPAGE_PUD_ORDER);
990994 args->pud_pfn = ULONG_MAX;
991995 args->pmd_pfn = ULONG_MAX;
992996 args->pte_pfn = ULONG_MAX;
@@ -995,20 +999,13 @@ static void __init destroy_args(struct pgtable_debug_args *args) · 2 unmodified lines
995999 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
9961000 has_transparent_hugepage() &&
9971001 args->pmd_pfn != ULONG_MAX) {
998− if (args->is_contiguous_page) {
999− free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
1000− } else {
1001− page = pfn_to_page(args->pmd_pfn);
1002− __free_pages(page, HPAGE_PMD_ORDER);
1003− }
1004−
1002+ debug_vm_pgtable_free_huge_page(args, args->pmd_pfn, HPAGE_PMD_ORDER);
10051003 args->pmd_pfn = ULONG_MAX;
10061004 args->pte_pfn = ULONG_MAX;
10071005 }
10081006
10091007 if (args->pte_pfn != ULONG_MAX) {
1010− page = pfn_to_page(args->pte_pfn);
1011− __free_page(page);
1008+ __free_page(pfn_to_page(args->pte_pfn));
10121009
10131010 args->pte_pfn = ULONG_MAX;
10141011 }
@@ -1242,8 +1239,7 @@ static int __init init_args(struct pgtable_debug_args *args) · 227 unmodified lines
12421239 */
12431240 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
12441241 has_transparent_pud_hugepage()) {
1245− page = debug_vm_pgtable_alloc_huge_page(args,
1246− HPAGE_PUD_SHIFT - PAGE_SHIFT);
1242+ page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PUD_ORDER);
12471243 if (page) {
12481244 args->pud_pfn = page_to_pfn(page);
12491245 args->pmd_pfn = args->pud_pfn;
mm/dmapool_test.c+1−0
@@ -1,3 +1,4 @@
1+// SPDX-License-Identifier: GPL-2.0
12 #include <linux/device.h>
23 #include <linux/dma-map-ops.h>
34 #include <linux/dma-mapping.h>
mm/early_ioremap.c+12−4
@@ -30,6 +30,14 @@ static int __init early_ioremap_debug_setup(char *str) · 29 unmodified lines
3030 }
3131 early_param("early_ioremap_debug", early_ioremap_debug_setup);
3232
33+#define early_ioremap_dbg(fmt, args...) \
34+ do { \
35+ if (unlikely(early_ioremap_debug)) { \
36+ pr_warn(fmt, ##args); \
37+ dump_stack(); \
38+ } \
39+ } while (0)
40+
3341 static int after_paging_init __initdata;
3442
3543 pgprot_t __init __weak early_memremap_pgprot_adjust(resource_size_t phys_addr,
@@ -139,6 +147,9 @@ __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot) · 103 unmodified lines
139147 if (WARN_ON(nrpages > NR_FIX_BTMAPS))
140148 return NULL;
141149
150+ early_ioremap_dbg("%s(%pa, %08lx) [%d] => %08lx + %08lx\n",
151+ __func__, &phys_addr, size, slot, slot_virt[slot], offset);
152+
142153 /*
143154 * Ok, go for it..
144155 */
@@ -152,8 +163,6 @@ __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot) · 7 unmodified lines
152163 --idx;
153164 --nrpages;
154165 }
155− WARN(early_ioremap_debug, "%s(%pa, %08lx) [%d] => %08lx + %08lx\n",
156− __func__, &phys_addr, size, slot, offset, slot_virt[slot]);
157166
158167 prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
159168 return prev_map[slot];
@@ -184,8 +193,7 @@ void __init early_iounmap(void __iomem *addr, unsigned long size) · 24 unmodified lines
184193 __func__, addr, size, slot, prev_size[slot]))
185194 return;
186195
187− WARN(early_ioremap_debug, "%s(%p, %08lx) [%d]\n",
188− __func__, addr, size, slot);
196+ early_ioremap_dbg("%s(%p, %08lx) [%d]\n", __func__, addr, size, slot);
189197
190198 virt_addr = (unsigned long)addr;
191199 if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)))
mm/folio-compat.c+1−0
@@ -1,3 +1,4 @@
1+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * Compatibility functions which bloat the callers too much to make inline.
34 * All of the callers of these functions should be converted to use folios
mm/gup.c+0−11
@@ -2806,17 +2806,6 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags) · 2805 unmodified lines
28062806 return !reject_file_backed || shmem_mapping(mapping);
28072807 }
28082808
2809−static void __maybe_unused gup_fast_undo_dev_pagemap(int *nr, int nr_start,
2810− unsigned int flags, struct page **pages)
2811−{
2812− while ((*nr) - nr_start) {
2813− struct folio *folio = page_folio(pages[--(*nr)]);
2814−
2815− folio_clear_referenced(folio);
2816− gup_put_folio(folio, 1, flags);
2817− }
2818−}
2819−
28202809 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
28212810 /*
28222811 * GUP-fast relies on pte change detection to avoid concurrent pgtable
mm/gup_test.c+1−0
@@ -1,3 +1,4 @@
1+// SPDX-License-Identifier: GPL-2.0
12 #include <linux/kernel.h>
23 #include <linux/mm.h>
34 #include <linux/slab.h>
mm/highmem.c+2−1
@@ -180,12 +180,13 @@ struct page *__kmap_to_page(void *vaddr) · 179 unmodified lines
180180 for (i = 0; i < kctrl->idx; i++) {
181181 unsigned long base_addr;
182182 int idx;
183+ pte_t pteval = kctrl->pteval[i];
183184
184185 idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
185186 base_addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
186187
187188 if (base_addr == base)
188− return pte_page(kctrl->pteval[i]);
189+ return pte_page(pteval);
189190 }
190191 }
191192
mm/huge_memory.c+4−4
@@ -3431,7 +3431,7 @@ static void remap_page(struct folio *folio, unsigned long nr, int flags) · 3430 unmodified lines
34313431 if (!folio_test_anon(folio))
34323432 return;
34333433 for (;;) {
3434− remove_migration_ptes(folio, folio, RMP_LOCKED | flags);
3434+ remove_migration_ptes(folio, folio, TTU_RMAP_LOCKED | flags);
34353435 i += folio_nr_pages(folio);
34363436 if (i >= nr)
34373437 break;
@@ -3944,7 +3944,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order, · 506 unmodified lines
39443944 int old_order = folio_order(folio);
39453945 struct folio *new_folio, *next;
39463946 int nr_shmem_dropped = 0;
3947− int remap_flags = 0;
3947+ enum ttu_flags ttu_flags = 0;
39483948 int ret;
39493949 pgoff_t end = 0;
39503950
@@ -4064,9 +4064,9 @@ fail: · 113 unmodified lines
40644064 shmem_uncharge(mapping->host, nr_shmem_dropped);
40654065
40664066 if (!ret && is_anon && !folio_is_device_private(folio))
4067− remap_flags = RMP_USE_SHARED_ZEROPAGE;
4067+ ttu_flags = TTU_USE_SHARED_ZEROPAGE;
40684068
4069− remap_page(folio, 1 << old_order, remap_flags);
4069+ remap_page(folio, 1 << old_order, ttu_flags);
40704070
40714071 /*
40724072 * Unlock all after-split folios except the one containing
mm/hugetlb.c+93−87
@@ -121,16 +121,6 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma, · 120 unmodified lines
121121 unsigned long start, unsigned long end, bool take_locks);
122122 static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
123123
124−static void hugetlb_free_folio(struct folio *folio)
125−{
126− if (folio_test_hugetlb_cma(folio)) {
127− hugetlb_cma_free_folio(folio);
128− return;
129− }
130−
131− folio_put(folio);
132−}
133−
134124 static inline bool subpool_is_free(struct hugepage_subpool *spool)
135125 {
136126 if (spool->count)
@@ -588,8 +578,9 @@ hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from, · 451 unmodified lines
588578 record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg);
589579 list_add(&nrg->link, rg);
590580 coalesce_file_region(map, nrg);
591− } else
581+ } else {
592582 *regions_needed += 1;
583+ }
593584
594585 return to - from;
595586 }
@@ -1257,8 +1248,9 @@ void hugetlb_dup_vma_private(struct vm_area_struct *vma) · 661 unmodified lines
12571248
12581249 if (vma_lock && vma_lock->vma != vma)
12591250 vma->vm_private_data = NULL;
1260− } else
1251+ } else {
12611252 vma->vm_private_data = NULL;
1253+ }
12621254 }
12631255
12641256 /*
@@ -1417,47 +1409,25 @@ err: · 152 unmodified lines
14171409 return NULL;
14181410 }
14191411
1420−#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
1421−#ifdef CONFIG_CONTIG_ALLOC
1422−static struct folio *alloc_gigantic_folio(int order, gfp_t gfp_mask,
1412+#if defined(CONFIG_ARCH_HAS_GIGANTIC_PAGE) && defined(CONFIG_CONTIG_ALLOC)
1413+static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,
14231414 int nid, nodemask_t *nodemask)
14241415 {
14251416 struct folio *folio;
1426− bool retried = false;
1427−
1428−retry:
1429− folio = hugetlb_cma_alloc_folio(order, gfp_mask, nid, nodemask);
1430− if (!folio) {
1431− if (hugetlb_cma_exclusive_alloc())
1432− return NULL;
1433−
1434− folio = folio_alloc_gigantic(order, gfp_mask, nid, nodemask);
1435− if (!folio)
1436− return NULL;
1437− }
14381417
1439− if (folio_ref_freeze(folio, 1))
1418+ folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nodemask);
1419+ if (folio)
14401420 return folio;
14411421
1442− pr_warn("HugeTLB: unexpected refcount on PFN %lu\n", folio_pfn(folio));
1443− hugetlb_free_folio(folio);
1444− if (!retried) {
1445− retried = true;
1446− goto retry;
1447− }
1448− return NULL;
1449−}
1422+ if (hugetlb_cma_exclusive_alloc())
1423+ return NULL;
14501424
1451−#else /* !CONFIG_CONTIG_ALLOC */
1452−static struct folio *alloc_gigantic_folio(int order, gfp_t gfp_mask, int nid,
1453− nodemask_t *nodemask)
1454−{
1455− return NULL;
1425+ folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
1426+ nid, nodemask);
1427+ return folio;
14561428 }
1457−#endif /* CONFIG_CONTIG_ALLOC */
1458−
1459−#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
1460−static struct folio *alloc_gigantic_folio(int order, gfp_t gfp_mask, int nid,
1429+#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */
1430+static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask, int nid,
14611431 nodemask_t *nodemask)
14621432 {
14631433 return NULL;
@@ -1587,9 +1557,11 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, · 123 unmodified lines
15871557 if (unlikely(folio_test_hwpoison(folio)))
15881558 folio_clear_hugetlb_hwpoison(folio);
15891559
1590− folio_ref_unfreeze(folio, 1);
1591−
1592− hugetlb_free_folio(folio);
1560+ VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
1561+ if (folio_test_hugetlb_cma(folio))
1562+ hugetlb_cma_free_frozen_folio(folio);
1563+ else
1564+ free_frozen_pages(&folio->page, folio_order(folio));
15931565 }
15941566
15951567 /*
@@ -1869,7 +1841,7 @@ struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio) · 273 unmodified lines
18691841 return NULL;
18701842 }
18711843
1872−static struct folio *alloc_buddy_hugetlb_folio(int order, gfp_t gfp_mask,
1844+static struct folio *alloc_buddy_frozen_folio(int order, gfp_t gfp_mask,
18731845 int nid, nodemask_t *nmask, nodemask_t *node_alloc_noretry)
18741846 {
18751847 struct folio *folio;
@@ -1925,10 +1897,10 @@ static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, · 49 unmodified lines
19251897 nid = numa_mem_id();
19261898
19271899 if (order_is_gigantic(order))
1928− folio = alloc_gigantic_folio(order, gfp_mask, nid, nmask);
1900+ folio = alloc_gigantic_frozen_folio(order, gfp_mask, nid, nmask);
19291901 else
1930− folio = alloc_buddy_hugetlb_folio(order, gfp_mask, nid, nmask,
1931− node_alloc_noretry);
1902+ folio = alloc_buddy_frozen_folio(order, gfp_mask, nid, nmask,
1903+ node_alloc_noretry);
19321904 if (folio)
19331905 init_new_hugetlb_folio(folio);
19341906 return folio;
@@ -2106,8 +2078,9 @@ retry: · 171 unmodified lines
21062078 h->max_huge_pages++;
21072079 goto out;
21082080 }
2109− } else
2081+ } else {
21102082 rc = 0;
2083+ }
21112084
21122085 update_and_free_hugetlb_folio(h, folio, false);
21132086 return rc;
@@ -2702,11 +2675,12 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, · 588 unmodified lines
27022675 * be consumed on a subsequent allocation.
27032676 */
27042677 folio_set_hugetlb_restore_reserve(folio);
2705− } else
2678+ } else {
27062679 /*
27072680 * No reservation present, do nothing
27082681 */
2709− vma_end_reservation(h, vma, address);
2682+ vma_end_reservation(h, vma, address);
2683+ }
27102684 }
27112685 }
27122686
@@ -2836,23 +2810,62 @@ int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) · 123 unmodified lines
28362810 */
28372811 int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn)
28382812 {
2839− struct folio *folio;
2813+ unsigned long nr = 0;
2814+ struct page *page;
2815+ struct hstate *h;
2816+ LIST_HEAD(list);
28402817 int ret = 0;
28412818
2842− LIST_HEAD(isolate_list);
2819+ /* Avoid pfn iterations if no free non-gigantic huge pages */
2820+ for_each_hstate(h) {
2821+ if (hstate_is_gigantic(h))
2822+ continue;
2823+
2824+ nr += h->free_huge_pages;
2825+ if (nr)
2826+ break;
2827+ }
2828+
2829+ if (!nr)
2830+ return 0;
28432831
28442832 while (start_pfn < end_pfn) {
2845− folio = pfn_folio(start_pfn);
2833+ page = pfn_to_page(start_pfn);
2834+ nr = 1;
28462835
2847− /* Not to disrupt normal path by vainly holding hugetlb_lock */
2848− if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) {
2849− ret = alloc_and_dissolve_hugetlb_folio(folio, &isolate_list);
2850− if (ret)
2851− break;
2836+ if (PageHuge(page) || PageCompound(page)) {
2837+ struct folio *folio = page_folio(page);
2838+
2839+ nr = folio_nr_pages(folio) - folio_page_idx(folio, page);
2840+
2841+ /*
2842+ * Don't disrupt normal path by vainly holding
2843+ * hugetlb_lock
2844+ */
2845+ if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) {
2846+ if (order_is_gigantic(folio_order(folio))) {
2847+ ret = -ENOMEM;
2848+ break;
2849+ }
2850+
2851+ ret = alloc_and_dissolve_hugetlb_folio(folio, &list);
2852+ if (ret)
2853+ break;
2854+
2855+ putback_movable_pages(&list);
2856+ }
2857+ } else if (PageBuddy(page)) {
2858+ /*
2859+ * Buddy order check without zone lock is unsafe and
2860+ * the order is maybe invalid, but race should be
2861+ * small, and the worst thing is skipping free hugetlb.
2862+ */
2863+ const unsigned int order = buddy_order_unsafe(page);
28522864
2853− putback_movable_pages(&isolate_list);
2865+ if (order <= MAX_PAGE_ORDER)
2866+ nr = 1UL << order;
28542867 }
2855− start_pfn++;
2868+ start_pfn += nr;
28562869 }
28572870
28582871 return ret;
@@ -3019,13 +3032,10 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, · 160 unmodified lines
30193032
30203033 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
30213034 hugetlb_acct_memory(h, -rsv_adjust);
3022− if (map_chg) {
3023− spin_lock_irq(&hugetlb_lock);
3024− hugetlb_cgroup_uncharge_folio_rsvd(
3025− hstate_index(h), pages_per_huge_page(h),
3026− folio);
3027− spin_unlock_irq(&hugetlb_lock);
3028− }
3035+ spin_lock_irq(&hugetlb_lock);
3036+ hugetlb_cgroup_uncharge_folio_rsvd(
3037+ hstate_index(h), pages_per_huge_page(h), folio);
3038+ spin_unlock_irq(&hugetlb_lock);
30293039 }
30303040 }
30313041
@@ -3425,6 +3435,13 @@ static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) · 393 unmodified lines
34253435
34263436 folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
34273437 &node_states[N_MEMORY], NULL);
3438+ if (!folio && !list_empty(&folio_list) &&
3439+ hugetlb_vmemmap_optimizable_size(h)) {
3440+ prep_and_add_allocated_folios(h, &folio_list);
3441+ INIT_LIST_HEAD(&folio_list);
3442+ folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
3443+ &node_states[N_MEMORY], NULL);
3444+ }
34283445 if (!folio)
34293446 break;
34303447 list_add(&folio->lru, &folio_list);
@@ -4159,7 +4176,6 @@ static int __init hugetlb_init(void) · 728 unmodified lines
41594176 }
41604177 }
41614178
4162− hugetlb_cma_check();
41634179 hugetlb_init_hstates();
41644180 gather_bootmem_prealloc();
41654181 report_hugepages();
@@ -4487,21 +4503,11 @@ void __init hugetlb_bootmem_set_nodes(void) · 321 unmodified lines
44874503 }
44884504 }
44894505
4490−static bool __hugetlb_bootmem_allocated __initdata;
4491−
4492−bool __init hugetlb_bootmem_allocated(void)
4493−{
4494− return __hugetlb_bootmem_allocated;
4495−}
4496−
44974506 void __init hugetlb_bootmem_alloc(void)
44984507 {
44994508 struct hstate *h;
45004509 int i;
45014510
4502− if (__hugetlb_bootmem_allocated)
4503− return;
4504−
45054511 hugetlb_bootmem_set_nodes();
45064512
45074513 for (i = 0; i < MAX_NUMNODES; i++)
@@ -4515,8 +4521,6 @@ void __init hugetlb_bootmem_alloc(void) · 7 unmodified lines
45154521 if (hstate_is_gigantic(h))
45164522 hugetlb_hstate_alloc_pages(h);
45174523 }
4518−
4519− __hugetlb_bootmem_allocated = true;
45204524 }
45214525
45224526 /*
@@ -4718,10 +4722,12 @@ static void hugetlb_vm_op_open(struct vm_area_struct *vma) · 195 unmodified lines
47184722 if (vma_lock->vma != vma) {
47194723 vma->vm_private_data = NULL;
47204724 hugetlb_vma_lock_alloc(vma);
4721− } else
4725+ } else {
47224726 pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__);
4723− } else
4727+ }
4728+ } else {
47244729 hugetlb_vma_lock_alloc(vma);
4730+ }
47254731 }
47264732 }
47274733
mm/hugetlb_cgroup.c+2−9
@@ -1,3 +1,4 @@
1+// SPDX-License-Identifier: LGPL-2.1
12 /*
23 *
34 * Copyright IBM Corporation, 2012
@@ -7,14 +8,6 @@ · 3 unmodified lines
78 * Copyright (C) 2019 Red Hat, Inc.
89 * Author: Giuseppe Scrivano <gscrivan@redhat.com>
910 *
10− * This program is free software; you can redistribute it and/or modify it
11− * under the terms of version 2.1 of the GNU Lesser General Public License
12− * as published by the Free Software Foundation.
13− *
14− * This program is distributed in the hope that it would be useful, but
15− * WITHOUT ANY WARRANTY; without even the implied warranty of
16− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17− *
1811 */
1912
2013 #include <linux/cgroup.h>
@@ -822,7 +815,7 @@ hugetlb_cgroup_cfttypes_init(struct hstate *h, struct cftype *cft, · 801 unmodified lines
822815 for (i = 0; i < tmpl_size; cft++, tmpl++, i++) {
823816 *cft = *tmpl;
824817 /* rebuild the name */
825− snprintf(cft->name, MAX_CFTYPE_NAME, "%s.%s", buf, tmpl->name);
818+ scnprintf(cft->name, MAX_CFTYPE_NAME, "%s.%s", buf, tmpl->name);
826819 /* rebuild the private */
827820 cft->private = MEMFILE_PRIVATE(idx, tmpl->private);
828821 /* rebuild the file_offset */
mm/hugetlb_cma.c+37−34
@@ -13,42 +13,46 @@ · 12 unmodified lines
1313 #include "hugetlb_cma.h"
1414
1515
16−static struct cma *hugetlb_cma[MAX_NUMNODES];
16+static struct cma *hugetlb_cma[MAX_NUMNODES] __ro_after_init;
1717 static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata;
18−static bool hugetlb_cma_only;
19−static unsigned long hugetlb_cma_size __initdata;
18+static bool hugetlb_cma_only __ro_after_init;
19+static unsigned long hugetlb_cma_size __ro_after_init;
2020
21−void hugetlb_cma_free_folio(struct folio *folio)
21+void hugetlb_cma_free_frozen_folio(struct folio *folio)
2222 {
23− int nid = folio_nid(folio);
24−
25− WARN_ON_ONCE(!cma_free_folio(hugetlb_cma[nid], folio));
23+ WARN_ON_ONCE(!cma_release_frozen(hugetlb_cma[folio_nid(folio)],
24+ &folio->page, folio_nr_pages(folio)));
2625 }
2726
28−
29−struct folio *hugetlb_cma_alloc_folio(int order, gfp_t gfp_mask,
30− int nid, nodemask_t *nodemask)
27+struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
28+ int nid, nodemask_t *nodemask)
3129 {
3230 int node;
33− struct folio *folio = NULL;
31+ struct folio *folio;
32+ struct page *page = NULL;
33+
34+ if (!hugetlb_cma_size)
35+ return NULL;
3436
3537 if (hugetlb_cma[nid])
36− folio = cma_alloc_folio(hugetlb_cma[nid], order, gfp_mask);
38+ page = cma_alloc_frozen_compound(hugetlb_cma[nid], order);
3739
38− if (!folio && !(gfp_mask & __GFP_THISNODE)) {
40+ if (!page && !(gfp_mask & __GFP_THISNODE)) {
3941 for_each_node_mask(node, *nodemask) {
4042 if (node == nid || !hugetlb_cma[node])
4143 continue;
4244
43− folio = cma_alloc_folio(hugetlb_cma[node], order, gfp_mask);
44− if (folio)
45+ page = cma_alloc_frozen_compound(hugetlb_cma[node], order);
46+ if (page)
4547 break;
4648 }
4749 }
4850
49− if (folio)
50− folio_set_hugetlb_cma(folio);
51+ if (!page)
52+ return NULL;
5153
54+ folio = page_folio(page);
55+ folio_set_hugetlb_cma(folio);
5256 return folio;
5357 }
5458
@@ -85,9 +89,6 @@ hugetlb_cma_alloc_bootmem(struct hstate *h, int *nid, bool node_exact) · 30 unmodified lines
8589 return m;
8690 }
8791
88−
89−static bool cma_reserve_called __initdata;
90−
9192 static int __init cmdline_parse_hugetlb_cma(char *p)
9293 {
9394 int nid, count = 0;
@@ -134,12 +135,26 @@ static int __init cmdline_parse_hugetlb_cma_only(char *p) · 40 unmodified lines
134135
135136 early_param("hugetlb_cma_only", cmdline_parse_hugetlb_cma_only);
136137
137−void __init hugetlb_cma_reserve(int order)
138+unsigned int __weak arch_hugetlb_cma_order(void)
138139 {
139− unsigned long size, reserved, per_node;
140+ return 0;
141+}
142+
143+void __init hugetlb_cma_reserve(void)
144+{
145+ unsigned long size, reserved, per_node, order;
140146 bool node_specific_cma_alloc = false;
141147 int nid;
142148
149+ if (!hugetlb_cma_size)
150+ return;
151+
152+ order = arch_hugetlb_cma_order();
153+ if (!order) {
154+ pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
155+ return;
156+ }
157+
143158 /*
144159 * HugeTLB CMA reservation is required for gigantic
145160 * huge pages which could not be allocated via the
@@ -147,10 +162,6 @@ void __init hugetlb_cma_reserve(int order) · 1 unmodified line
147162 * breaking this assumption.
148163 */
149164 VM_WARN_ON(order <= MAX_PAGE_ORDER);
150− cma_reserve_called = true;
151−
152− if (!hugetlb_cma_size)
153− return;
154165
155166 hugetlb_bootmem_set_nodes();
156167
@@ -244,14 +255,6 @@ void __init hugetlb_cma_reserve(int order) · 87 unmodified lines
244255 hugetlb_cma_size = 0;
245256 }
246257
247−void __init hugetlb_cma_check(void)
248−{
249− if (!hugetlb_cma_size || cma_reserve_called)
250− return;
251−
252− pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
253−}
254−
255258 bool hugetlb_cma_exclusive_alloc(void)
256259 {
257260 return hugetlb_cma_only;
mm/hugetlb_cma.h+5−10
@@ -3,23 +3,22 @@ · 2 unmodified lines
33 #define _LINUX_HUGETLB_CMA_H
44
55 #ifdef CONFIG_CMA
6−void hugetlb_cma_free_folio(struct folio *folio);
7−struct folio *hugetlb_cma_alloc_folio(int order, gfp_t gfp_mask,
6+void hugetlb_cma_free_frozen_folio(struct folio *folio);
7+struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
88 int nid, nodemask_t *nodemask);
99 struct huge_bootmem_page *hugetlb_cma_alloc_bootmem(struct hstate *h, int *nid,
1010 bool node_exact);
11−void hugetlb_cma_check(void);
1211 bool hugetlb_cma_exclusive_alloc(void);
1312 unsigned long hugetlb_cma_total_size(void);
1413 void hugetlb_cma_validate_params(void);
1514 bool hugetlb_early_cma(struct hstate *h);
1615 #else
17−static inline void hugetlb_cma_free_folio(struct folio *folio)
16+static inline void hugetlb_cma_free_frozen_folio(struct folio *folio)
1817 {
1918 }
2019
21−static inline struct folio *hugetlb_cma_alloc_folio(int order, gfp_t gfp_mask,
22− int nid, nodemask_t *nodemask)
20+static inline struct folio *hugetlb_cma_alloc_frozen_folio(int order,
21+ gfp_t gfp_mask, int nid, nodemask_t *nodemask)
2322 {
2423 return NULL;
2524 }
@@ -31,10 +30,6 @@ struct huge_bootmem_page *hugetlb_cma_alloc_bootmem(struct hstate *h, int *nid, · 5 unmodified lines
3130 return NULL;
3231 }
3332
34−static inline void hugetlb_cma_check(void)
35−{
36−}
37−
3833 static inline bool hugetlb_cma_exclusive_alloc(void)
3934 {
4035 return false;
mm/hugetlb_sysctl.c+11−0
@@ -8,6 +8,8 @@ · 7 unmodified lines
88
99 #include "hugetlb_internal.h"
1010
11+int movable_gigantic_pages;
12+
1113 #ifdef CONFIG_SYSCTL
1214 static int proc_hugetlb_doulongvec_minmax(const struct ctl_table *table, int write,
1315 void *buffer, size_t *length,
@@ -125,6 +127,15 @@ static const struct ctl_table hugetlb_table[] = { · 111 unmodified lines
125127 .mode = 0644,
126128 .proc_handler = hugetlb_overcommit_handler,
127129 },
130+#ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
131+ {
132+ .procname = "movable_gigantic_pages",
133+ .data = &movable_gigantic_pages,
134+ .maxlen = sizeof(int),
135+ .mode = 0644,
136+ .proc_handler = proc_dointvec,
137+ },
138+#endif
128139 };
129140
130141 void __init hugetlb_sysctl_init(void)
mm/hugetlb_vmemmap.c+0−11
@@ -794,14 +794,6 @@ void __init hugetlb_vmemmap_init_early(int nid) · 793 unmodified lines
794794 struct huge_bootmem_page *m = NULL;
795795 void *map;
796796
797− /*
798− * Noting to do if bootmem pages were not allocated
799− * early in boot, or if HVO wasn't enabled in the
800− * first place.
801− */
802− if (!hugetlb_bootmem_allocated())
803− return;
804−
805797 if (!READ_ONCE(vmemmap_optimize_enabled))
806798 return;
807799
@@ -847,9 +839,6 @@ void __init hugetlb_vmemmap_init_late(int nid) · 39 unmodified lines
847839 struct hstate *h;
848840 void *map;
849841
850− if (!hugetlb_bootmem_allocated())
851− return;
852−
853842 if (!READ_ONCE(vmemmap_optimize_enabled))
854843 return;
855844
mm/internal.h+87−19
@@ -171,7 +171,7 @@ static inline int mmap_file(struct file *file, struct vm_area_struct *vma) · 170 unmodified lines
171171
172172 /*
173173 * OK, we tried to call the file hook for mmap(), but an error
174− * arose. The mapping is in an inconsistent state and we most not invoke
174+ * arose. The mapping is in an inconsistent state and we must not invoke
175175 * any further hooks on it.
176176 */
177177 vma->vm_ops = &vma_dummy_vm_ops;
@@ -199,6 +199,73 @@ static inline void vma_close(struct vm_area_struct *vma) · 21 unmodified lines
199199
200200 #ifdef CONFIG_MMU
201201
202+static inline void get_anon_vma(struct anon_vma *anon_vma)
203+{
204+ atomic_inc(&anon_vma->refcount);
205+}
206+
207+void __put_anon_vma(struct anon_vma *anon_vma);
208+
209+static inline void put_anon_vma(struct anon_vma *anon_vma)
210+{
211+ if (atomic_dec_and_test(&anon_vma->refcount))
212+ __put_anon_vma(anon_vma);
213+}
214+
215+static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
216+{
217+ down_write(&anon_vma->root->rwsem);
218+}
219+
220+static inline int anon_vma_trylock_write(struct anon_vma *anon_vma)
221+{
222+ return down_write_trylock(&anon_vma->root->rwsem);
223+}
224+
225+static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
226+{
227+ up_write(&anon_vma->root->rwsem);
228+}
229+
230+static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
231+{
232+ down_read(&anon_vma->root->rwsem);
233+}
234+
235+static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
236+{
237+ return down_read_trylock(&anon_vma->root->rwsem);
238+}
239+
240+static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
241+{
242+ up_read(&anon_vma->root->rwsem);
243+}
244+
245+struct anon_vma *folio_get_anon_vma(const struct folio *folio);
246+
247+/* Operations which modify VMAs. */
248+enum vma_operation {
249+ VMA_OP_SPLIT,
250+ VMA_OP_MERGE_UNFAULTED,
251+ VMA_OP_REMAP,
252+ VMA_OP_FORK,
253+};
254+
255+int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src,
256+ enum vma_operation operation);
257+int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma);
258+int __anon_vma_prepare(struct vm_area_struct *vma);
259+void unlink_anon_vmas(struct vm_area_struct *vma);
260+
261+static inline int anon_vma_prepare(struct vm_area_struct *vma)
262+{
263+ if (likely(vma->anon_vma))
264+ return 0;
265+
266+ return __anon_vma_prepare(vma);
267+}
268+
202269 /* Flags for folio_pte_batch(). */
203270 typedef int __bitwise fpb_t;
204271
@@ -513,6 +580,14 @@ static inline void set_page_refcounted(struct page *page) · 308 unmodified lines
513580 set_page_count(page, 1);
514581 }
515582
583+static inline void set_pages_refcounted(struct page *page, unsigned long nr_pages)
584+{
585+ unsigned long pfn = page_to_pfn(page);
586+
587+ for (; nr_pages--; pfn++)
588+ set_page_refcounted(pfn_to_page(pfn));
589+}
590+
516591 /*
517592 * Return true if a folio needs ->release_folio() calling upon it.
518593 */
@@ -853,6 +928,12 @@ void memmap_init_range(unsigned long, int, unsigned long, unsigned long, · 334 unmodified lines
853928 unsigned long, enum meminit_context, struct vmem_altmap *, int,
854929 bool);
855930
931+#ifdef CONFIG_SPARSEMEM
932+void sparse_init(void);
933+#else
934+static inline void sparse_init(void) {}
935+#endif /* CONFIG_SPARSEMEM */
936+
856937 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
857938
858939 /*
@@ -929,9 +1010,14 @@ void init_cma_reserved_pageblock(struct page *page); · 70 unmodified lines
9291010 struct cma;
9301011
9311012 #ifdef CONFIG_CMA
1013+bool cma_validate_zones(struct cma *cma);
9321014 void *cma_reserve_early(struct cma *cma, unsigned long size);
9331015 void init_cma_pageblock(struct page *page);
9341016 #else
1017+static inline bool cma_validate_zones(struct cma *cma)
1018+{
1019+ return false;
1020+}
9351021 static inline void *cma_reserve_early(struct cma *cma, unsigned long size)
9361022 {
9371023 return NULL;
@@ -1658,24 +1744,6 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start, · 720 unmodified lines
16581744 unsigned long end, const struct mm_walk_ops *ops,
16591745 pgd_t *pgd, void *private);
16601746
1661−/* pt_reclaim.c */
1662−bool try_get_and_clear_pmd(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdval);
1663−void free_pte(struct mm_struct *mm, unsigned long addr, struct mmu_gather *tlb,
1664− pmd_t pmdval);
1665−void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
1666− struct mmu_gather *tlb);
1667−
1668−#ifdef CONFIG_PT_RECLAIM
1669−bool reclaim_pt_is_enabled(unsigned long start, unsigned long end,
1670− struct zap_details *details);
1671−#else
1672−static inline bool reclaim_pt_is_enabled(unsigned long start, unsigned long end,
1673− struct zap_details *details)
1674−{
1675− return false;
1676−}
1677−#endif /* CONFIG_PT_RECLAIM */
1678−
16791747 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm);
16801748 int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm);
16811749
mm/kasan/kasan_test_c.c+35−15
@@ -1840,6 +1840,29 @@ static void vmalloc_helpers_tags(struct kunit *test) · 1839 unmodified lines
18401840 vfree(ptr);
18411841 }
18421842
1843+static void vmalloc_oob_helper(struct kunit *test, char *v_ptr, size_t size)
1844+{
1845+ /*
1846+ * We have to be careful not to hit the guard page in vmalloc tests.
1847+ * The MMU will catch that and crash us.
1848+ */
1849+
1850+ /* Make sure in-bounds accesses are valid. */
1851+ v_ptr[0] = 0;
1852+ v_ptr[size - 1] = 0;
1853+
1854+ /*
1855+ * An unaligned access past the requested vmalloc size.
1856+ * Only generic KASAN can precisely detect these.
1857+ */
1858+ if (IS_ENABLED(CONFIG_KASAN_GENERIC))
1859+ KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)v_ptr)[size]);
1860+
1861+ /* An aligned access into the first out-of-bounds granule. */
1862+ size = round_up(size, KASAN_GRANULE_SIZE);
1863+ KUNIT_EXPECT_KASAN_FAIL_READ(test, ((volatile char *)v_ptr)[size]);
1864+}
1865+
18431866 static void vmalloc_oob(struct kunit *test)
18441867 {
18451868 char *v_ptr, *p_ptr;
@@ -1856,24 +1879,21 @@ static void vmalloc_oob(struct kunit *test) · 10 unmodified lines
18561879
18571880 OPTIMIZER_HIDE_VAR(v_ptr);
18581881
1859− /*
1860− * We have to be careful not to hit the guard page in vmalloc tests.
1861− * The MMU will catch that and crash us.
1862− */
1882+ vmalloc_oob_helper(test, v_ptr, size);
18631883
1864− /* Make sure in-bounds accesses are valid. */
1865− v_ptr[0] = 0;
1866− v_ptr[size - 1] = 0;
1884+ size -= KASAN_GRANULE_SIZE + 1;
1885+ v_ptr = vrealloc(v_ptr, size, GFP_KERNEL);
1886+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
18671887
1868− /*
1869− * An unaligned access past the requested vmalloc size.
1870− * Only generic KASAN can precisely detect these.
1871− */
1872− if (IS_ENABLED(CONFIG_KASAN_GENERIC))
1873− KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)v_ptr)[size]);
1888+ OPTIMIZER_HIDE_VAR(v_ptr);
18741889
1875− /* An aligned access into the first out-of-bounds granule. */
1876− KUNIT_EXPECT_KASAN_FAIL_READ(test, ((volatile char *)v_ptr)[size + 5]);
1890+ vmalloc_oob_helper(test, v_ptr, size);
1891+
1892+ size += 2 * KASAN_GRANULE_SIZE + 2;
1893+ v_ptr = vrealloc(v_ptr, size, GFP_KERNEL);
1894+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, v_ptr);
1895+
1896+ vmalloc_oob_helper(test, v_ptr, size);
18771897
18781898 /* Check that in-bounds accesses to the physical page are valid. */
18791899 page = vmalloc_to_page(v_ptr);
mm/kasan/report.c+4−4
@@ -203,7 +203,7 @@ static inline void fail_non_kasan_kunit_test(void) { } · 202 unmodified lines
203203
204204 static DEFINE_RAW_SPINLOCK(report_lock);
205205
206−static void start_report(unsigned long *flags, bool sync)
206+static void start_report(unsigned long *flags)
207207 {
208208 fail_non_kasan_kunit_test();
209209 /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
@@ -543,7 +543,7 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_ty · 333 unmodified lines
543543 if (unlikely(!report_enabled()))
544544 return;
545545
546− start_report(&flags, true);
546+ start_report(&flags);
547547
548548 __memset(&info, 0, sizeof(info));
549549 info.type = type;
@@ -581,7 +581,7 @@ bool kasan_report(const void *addr, size_t size, bool is_write, · 31 unmodified lines
581581 goto out;
582582 }
583583
584− start_report(&irq_flags, true);
584+ start_report(&irq_flags);
585585
586586 __memset(&info, 0, sizeof(info));
587587 info.type = KASAN_REPORT_ACCESS;
@@ -615,7 +615,7 @@ void kasan_report_async(void) · 27 unmodified lines
615615 if (unlikely(!report_enabled()))
616616 return;
617617
618− start_report(&flags, false);
618+ start_report(&flags);
619619 pr_err("BUG: KASAN: invalid-access\n");
620620 pr_err("Asynchronous fault: no details available\n");
621621 pr_err("\n");
mm/kasan/shadow.c+4−4
@@ -305,7 +305,7 @@ static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr, · 304 unmodified lines
305305 pte_t pte;
306306 int index;
307307
308− arch_leave_lazy_mmu_mode();
308+ lazy_mmu_mode_pause();
309309
310310 index = PFN_DOWN(addr - data->start);
311311 page = data->pages[index];
@@ -319,7 +319,7 @@ static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr, · 7 unmodified lines
319319 }
320320 spin_unlock(&init_mm.page_table_lock);
321321
322− arch_enter_lazy_mmu_mode();
322+ lazy_mmu_mode_resume();
323323
324324 return 0;
325325 }
@@ -471,7 +471,7 @@ static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr, · 145 unmodified lines
471471 pte_t pte;
472472 int none;
473473
474− arch_leave_lazy_mmu_mode();
474+ lazy_mmu_mode_pause();
475475
476476 spin_lock(&init_mm.page_table_lock);
477477 pte = ptep_get(ptep);
@@ -483,7 +483,7 @@ static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr, · 5 unmodified lines
483483 if (likely(!none))
484484 __free_page(pfn_to_page(pte_pfn(pte)));
485485
486− arch_enter_lazy_mmu_mode();
486+ lazy_mmu_mode_resume();
487487
488488 return 0;
489489 }
mm/kfence/core.c+3−3
@@ -905,7 +905,7 @@ static void toggle_allocation_gate(struct work_struct *work) · 904 unmodified lines
905905 /* Disable static key and reset timer. */
906906 static_branch_disable(&kfence_allocation_key);
907907 #endif
908− queue_delayed_work(system_unbound_wq, &kfence_timer,
908+ queue_delayed_work(system_dfl_wq, &kfence_timer,
909909 msecs_to_jiffies(kfence_sample_interval));
910910 }
911911
@@ -955,7 +955,7 @@ static void kfence_init_enable(void) · 43 unmodified lines
955955 #endif
956956
957957 WRITE_ONCE(kfence_enabled, true);
958− queue_delayed_work(system_unbound_wq, &kfence_timer, 0);
958+ queue_delayed_work(system_dfl_wq, &kfence_timer, 0);
959959
960960 pr_info("initialized - using %lu bytes for %d objects at 0x%p-0x%p\n", KFENCE_POOL_SIZE,
961961 CONFIG_KFENCE_NUM_OBJECTS, (void *)__kfence_pool,
@@ -1051,7 +1051,7 @@ static int kfence_enable_late(void) · 89 unmodified lines
10511051 return kfence_init_late();
10521052
10531053 WRITE_ONCE(kfence_enabled, true);
1054− queue_delayed_work(system_unbound_wq, &kfence_timer, 0);
1054+ queue_delayed_work(system_dfl_wq, &kfence_timer, 0);
10551055 pr_info("re-enabled\n");
10561056 return 0;
10571057 }
mm/khugepaged.c+102−83
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #include <linux/dax.h>
2323 #include <linux/ksm.h>
2424 #include <linux/pgalloc.h>
25+#include <linux/backing-dev.h>
2526
2627 #include <asm/tlb.h>
2728 #include "internal.h"
@@ -58,6 +59,7 @@ enum scan_result { · 30 unmodified lines
5859 SCAN_STORE_FAILED,
5960 SCAN_COPY_MC,
6061 SCAN_PAGE_FILLED,
62+ SCAN_PAGE_DIRTY_OR_WRITEBACK,
6163 };
6264
6365 #define CREATE_TRACE_POINTS
@@ -535,17 +537,16 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte, · 471 unmodified lines
535537 }
536538 }
537539
538−static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
539− unsigned long start_addr,
540− pte_t *pte,
541− struct collapse_control *cc,
542− struct list_head *compound_pagelist)
540+static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
541+ unsigned long start_addr, pte_t *pte, struct collapse_control *cc,
542+ struct list_head *compound_pagelist)
543543 {
544544 struct page *page = NULL;
545545 struct folio *folio = NULL;
546546 unsigned long addr = start_addr;
547547 pte_t *_pte;
548− int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
548+ int none_or_zero = 0, shared = 0, referenced = 0;
549+ enum scan_result result = SCAN_FAIL;
549550
550551 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
551552 _pte++, addr += PAGE_SIZE) {
@@ -778,13 +779,13 @@ static void __collapse_huge_page_copy_failed(pte_t *pte, · 226 unmodified lines
778779 * @ptl: lock on raw pages' PTEs
779780 * @compound_pagelist: list that stores compound pages
780781 */
781−static int __collapse_huge_page_copy(pte_t *pte, struct folio *folio,
782+static enum scan_result __collapse_huge_page_copy(pte_t *pte, struct folio *folio,
782783 pmd_t *pmd, pmd_t orig_pmd, struct vm_area_struct *vma,
783784 unsigned long address, spinlock_t *ptl,
784785 struct list_head *compound_pagelist)
785786 {
786787 unsigned int i;
787− int result = SCAN_SUCCEED;
788+ enum scan_result result = SCAN_SUCCEED;
788789
789790 /*
790791 * Copying pages' contents is subject to memory poison at any iteration.
@@ -826,7 +827,7 @@ static void khugepaged_alloc_sleep(void) · 35 unmodified lines
826827 remove_wait_queue(&khugepaged_wait, &wait);
827828 }
828829
829−struct collapse_control khugepaged_collapse_control = {
830+static struct collapse_control khugepaged_collapse_control = {
830831 .is_khugepaged = true,
831832 };
832833
@@ -896,10 +897,8 @@ static int hpage_collapse_find_target_node(struct collapse_control *cc) · 63 unmodified lines
896897 * Returns enum scan_result value.
897898 */
898899
899−static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
900− bool expect_anon,
901− struct vm_area_struct **vmap,
902− struct collapse_control *cc)
900+static enum scan_result hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
901+ bool expect_anon, struct vm_area_struct **vmap, struct collapse_control *cc)
903902 {
904903 struct vm_area_struct *vma;
905904 enum tva_type type = cc->is_khugepaged ? TVA_KHUGEPAGED :
@@ -928,7 +927,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address, · 22 unmodified lines
928927 return SCAN_SUCCEED;
929928 }
930929
931−static inline int check_pmd_state(pmd_t *pmd)
930+static inline enum scan_result check_pmd_state(pmd_t *pmd)
932931 {
933932 pmd_t pmde = pmdp_get_lockless(pmd);
934933
@@ -951,9 +950,8 @@ static inline int check_pmd_state(pmd_t *pmd) · 16 unmodified lines
951950 return SCAN_SUCCEED;
952951 }
953952
954−static int find_pmd_or_thp_or_none(struct mm_struct *mm,
955− unsigned long address,
956− pmd_t **pmd)
953+static enum scan_result find_pmd_or_thp_or_none(struct mm_struct *mm,
954+ unsigned long address, pmd_t **pmd)
957955 {
958956 *pmd = mm_find_pmd(mm, address);
959957 if (!*pmd)
@@ -962,12 +960,11 @@ static int find_pmd_or_thp_or_none(struct mm_struct *mm, · 2 unmodified lines
962960 return check_pmd_state(*pmd);
963961 }
964962
965−static int check_pmd_still_valid(struct mm_struct *mm,
966− unsigned long address,
967− pmd_t *pmd)
963+static enum scan_result check_pmd_still_valid(struct mm_struct *mm,
964+ unsigned long address, pmd_t *pmd)
968965 {
969966 pmd_t *new_pmd;
970− int result = find_pmd_or_thp_or_none(mm, address, &new_pmd);
967+ enum scan_result result = find_pmd_or_thp_or_none(mm, address, &new_pmd);
971968
972969 if (result != SCAN_SUCCEED)
973970 return result;
@@ -983,15 +980,14 @@ static int check_pmd_still_valid(struct mm_struct *mm, · 9 unmodified lines
983980 * Called and returns without pte mapped or spinlocks held.
984981 * Returns result: if not SCAN_SUCCEED, mmap_lock has been released.
985982 */
986−static int __collapse_huge_page_swapin(struct mm_struct *mm,
987− struct vm_area_struct *vma,
988− unsigned long start_addr, pmd_t *pmd,
989− int referenced)
983+static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm,
984+ struct vm_area_struct *vma, unsigned long start_addr, pmd_t *pmd,
985+ int referenced)
990986 {
991987 int swapped_in = 0;
992988 vm_fault_t ret = 0;
993989 unsigned long addr, end = start_addr + (HPAGE_PMD_NR * PAGE_SIZE);
994− int result;
990+ enum scan_result result;
995991 pte_t *pte = NULL;
996992 spinlock_t *ptl;
997993
@@ -1060,8 +1056,8 @@ out: · 62 unmodified lines
10601056 return result;
10611057 }
10621058
1063−static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
1064− struct collapse_control *cc)
1059+static enum scan_result alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
1060+ struct collapse_control *cc)
10651061 {
10661062 gfp_t gfp = (cc->is_khugepaged ? alloc_hugepage_khugepaged_gfpmask() :
10671063 GFP_TRANSHUGE);
@@ -1088,9 +1084,8 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, · 20 unmodified lines
10881084 return SCAN_SUCCEED;
10891085 }
10901086
1091−static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
1092− int referenced, int unmapped,
1093− struct collapse_control *cc)
1087+static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long address,
1088+ int referenced, int unmapped, struct collapse_control *cc)
10941089 {
10951090 LIST_HEAD(compound_pagelist);
10961091 pmd_t *pmd, _pmd;
@@ -1098,7 +1093,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, · 1 unmodified line
10981093 pgtable_t pgtable;
10991094 struct folio *folio;
11001095 spinlock_t *pmd_ptl, *pte_ptl;
1101− int result = SCAN_FAIL;
1096+ enum scan_result result = SCAN_FAIL;
11021097 struct vm_area_struct *vma;
11031098 struct mmu_notifier_range range;
11041099
@@ -1244,15 +1239,14 @@ out_nolock: · 139 unmodified lines
12441239 return result;
12451240 }
12461241
1247−static int hpage_collapse_scan_pmd(struct mm_struct *mm,
1248− struct vm_area_struct *vma,
1249− unsigned long start_addr, bool *mmap_locked,
1250− struct collapse_control *cc)
1242+static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
1243+ struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
1244+ struct collapse_control *cc)
12511245 {
12521246 pmd_t *pmd;
12531247 pte_t *pte, *_pte;
1254− int result = SCAN_FAIL, referenced = 0;
1255− int none_or_zero = 0, shared = 0;
1248+ int none_or_zero = 0, shared = 0, referenced = 0;
1249+ enum scan_result result = SCAN_FAIL;
12561250 struct page *page = NULL;
12571251 struct folio *folio = NULL;
12581252 unsigned long addr;
@@ -1439,8 +1433,8 @@ static void collect_mm_slot(struct mm_slot *slot) · 180 unmodified lines
14391433 }
14401434
14411435 /* folio must be locked, and mmap_lock must be held */
1442−static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
1443− pmd_t *pmdp, struct folio *folio, struct page *page)
1436+static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
1437+ pmd_t *pmdp, struct folio *folio, struct page *page)
14441438 {
14451439 struct mm_struct *mm = vma->vm_mm;
14461440 struct vm_fault vmf = {
@@ -1475,22 +1469,11 @@ static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr, · 28 unmodified lines
14751469 return SCAN_SUCCEED;
14761470 }
14771471
1478−/**
1479− * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
1480− * address haddr.
1481− *
1482− * @mm: process address space where collapse happens
1483− * @addr: THP collapse address
1484− * @install_pmd: If a huge PMD should be installed
1485− *
1486− * This function checks whether all the PTEs in the PMD are pointing to the
1487− * right THP. If so, retract the page table so the THP can refault in with
1488− * as pmd-mapped. Possibly install a huge PMD mapping the THP.
1489− */
1490−int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
1491− bool install_pmd)
1472+static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
1473+ bool install_pmd)
14921474 {
1493− int nr_mapped_ptes = 0, result = SCAN_FAIL;
1475+ enum scan_result result = SCAN_FAIL;
1476+ int nr_mapped_ptes = 0;
14941477 unsigned int nr_batch_ptes;
14951478 struct mmu_notifier_range range;
14961479 bool notified = false;
@@ -1709,6 +1692,24 @@ drop_folio: · 212 unmodified lines
17091692 return result;
17101693 }
17111694
1695+/**
1696+ * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
1697+ * address haddr.
1698+ *
1699+ * @mm: process address space where collapse happens
1700+ * @addr: THP collapse address
1701+ * @install_pmd: If a huge PMD should be installed
1702+ *
1703+ * This function checks whether all the PTEs in the PMD are pointing to the
1704+ * right THP. If so, retract the page table so the THP can refault in with
1705+ * as pmd-mapped. Possibly install a huge PMD mapping the THP.
1706+ */
1707+void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
1708+ bool install_pmd)
1709+{
1710+ try_collapse_pte_mapped_thp(mm, addr, install_pmd);
1711+}
1712+
17121713 /* Can we retract page tables for this file-backed VMA? */
17131714 static bool file_backed_vma_is_retractable(struct vm_area_struct *vma)
17141715 {
@@ -1854,9 +1855,8 @@ drop_pml: · 139 unmodified lines
18541855 * + unlock old pages
18551856 * + unlock and free huge page;
18561857 */
1857−static int collapse_file(struct mm_struct *mm, unsigned long addr,
1858− struct file *file, pgoff_t start,
1859− struct collapse_control *cc)
1858+static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
1859+ struct file *file, pgoff_t start, struct collapse_control *cc)
18601860 {
18611861 struct address_space *mapping = file->f_mapping;
18621862 struct page *dst;
@@ -1864,7 +1864,8 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, · 1 unmodified line
18641864 pgoff_t index = 0, end = start + HPAGE_PMD_NR;
18651865 LIST_HEAD(pagelist);
18661866 XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
1867− int nr_none = 0, result = SCAN_SUCCEED;
1867+ enum scan_result result = SCAN_SUCCEED;
1868+ int nr_none = 0;
18681869 bool is_shmem = shmem_file(file);
18691870
18701871 VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
@@ -1967,11 +1968,11 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, · 96 unmodified lines
19671968 */
19681969 xas_unlock_irq(&xas);
19691970 filemap_flush(mapping);
1970− result = SCAN_FAIL;
1971+ result = SCAN_PAGE_DIRTY_OR_WRITEBACK;
19711972 goto xa_unlocked;
19721973 } else if (folio_test_writeback(folio)) {
19731974 xas_unlock_irq(&xas);
1974− result = SCAN_FAIL;
1975+ result = SCAN_PAGE_DIRTY_OR_WRITEBACK;
19751976 goto xa_unlocked;
19761977 } else if (folio_trylock(folio)) {
19771978 folio_get(folio);
@@ -2018,7 +2019,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, · 40 unmodified lines
20182019 * folio is dirty because it hasn't been flushed
20192020 * since first write.
20202021 */
2021− result = SCAN_FAIL;
2022+ result = SCAN_PAGE_DIRTY_OR_WRITEBACK;
20222023 goto out_unlock;
20232024 }
20242025
@@ -2194,16 +2195,13 @@ immap_locked: · 169 unmodified lines
21942195 xas_lock_irq(&xas);
21952196 }
21962197
2197− if (is_shmem)
2198+ if (is_shmem) {
2199+ lruvec_stat_mod_folio(new_folio, NR_SHMEM, HPAGE_PMD_NR);
21982200 lruvec_stat_mod_folio(new_folio, NR_SHMEM_THPS, HPAGE_PMD_NR);
2199− else
2201+ } else {
22002202 lruvec_stat_mod_folio(new_folio, NR_FILE_THPS, HPAGE_PMD_NR);
2201−
2202− if (nr_none) {
2203− lruvec_stat_mod_folio(new_folio, NR_FILE_PAGES, nr_none);
2204− /* nr_none is always 0 for non-shmem. */
2205− lruvec_stat_mod_folio(new_folio, NR_SHMEM, nr_none);
22062203 }
2204+ lruvec_stat_mod_folio(new_folio, NR_FILE_PAGES, HPAGE_PMD_NR);
22072205
22082206 /*
22092207 * Mark new_folio as uptodate before inserting it into the
@@ -2225,7 +2223,7 @@ immap_locked: · 15 unmodified lines
22252223
22262224 /*
22272225 * Remove pte page tables, so we can re-fault the page as huge.
2228− * If MADV_COLLAPSE, adjust result to call collapse_pte_mapped_thp().
2226+ * If MADV_COLLAPSE, adjust result to call try_collapse_pte_mapped_thp().
22292227 */
22302228 retract_page_tables(mapping, start);
22312229 if (cc && !cc->is_khugepaged)
@@ -2237,6 +2235,11 @@ immap_locked: · 5 unmodified lines
22372235 */
22382236 list_for_each_entry_safe(folio, tmp, &pagelist, lru) {
22392237 list_del(&folio->lru);
2238+ lruvec_stat_mod_folio(folio, NR_FILE_PAGES,
2239+ -folio_nr_pages(folio));
2240+ if (is_shmem)
2241+ lruvec_stat_mod_folio(folio, NR_SHMEM,
2242+ -folio_nr_pages(folio));
22402243 folio->mapping = NULL;
22412244 folio_clear_active(folio);
22422245 folio_clear_unevictable(folio);
@@ -2285,16 +2288,15 @@ out: · 42 unmodified lines
22852288 return result;
22862289 }
22872290
2288−static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
2289− struct file *file, pgoff_t start,
2290− struct collapse_control *cc)
2291+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
2292+ struct file *file, pgoff_t start, struct collapse_control *cc)
22912293 {
22922294 struct folio *folio = NULL;
22932295 struct address_space *mapping = file->f_mapping;
22942296 XA_STATE(xas, &mapping->i_pages, start);
22952297 int present, swap;
22962298 int node = NUMA_NO_NODE;
2297− int result = SCAN_SUCCEED;
2299+ enum scan_result result = SCAN_SUCCEED;
22982300
22992301 present = 0;
23002302 swap = 0;
@@ -2392,7 +2394,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr, · 91 unmodified lines
23922394 return result;
23932395 }
23942396
2395−static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
2397+static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result *result,
23962398 struct collapse_control *cc)
23972399 __releases(&khugepaged_mm_lock)
23982400 __acquires(&khugepaged_mm_lock)
@@ -2440,14 +2442,15 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · 41 unmodified lines
24402442 break;
24412443 }
24422444 if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
2443−skip:
24442445 progress++;
24452446 continue;
24462447 }
24472448 hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
24482449 hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
2449− if (khugepaged_scan.address > hend)
2450− goto skip;
2450+ if (khugepaged_scan.address > hend) {
2451+ progress++;
2452+ continue;
2453+ }
24512454 if (khugepaged_scan.address < hstart)
24522455 khugepaged_scan.address = hstart;
24532456 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
@@ -2476,7 +2479,7 @@ skip: · 22 unmodified lines
24762479 mmap_read_lock(mm);
24772480 if (hpage_collapse_test_exit_or_disable(mm))
24782481 goto breakouterloop;
2479− *result = collapse_pte_mapped_thp(mm,
2482+ *result = try_collapse_pte_mapped_thp(mm,
24802483 khugepaged_scan.address, false);
24812484 if (*result == SCAN_PMD_MAPPED)
24822485 *result = SCAN_SUCCEED;
@@ -2552,7 +2555,7 @@ static void khugepaged_do_scan(struct collapse_control *cc) · 69 unmodified lines
25522555 unsigned int progress = 0, pass_through_head = 0;
25532556 unsigned int pages = READ_ONCE(khugepaged_pages_to_scan);
25542557 bool wait = true;
2555− int result = SCAN_SUCCEED;
2558+ enum scan_result result = SCAN_SUCCEED;
25562559
25572560 lru_add_drain_all();
25582561
@@ -2747,6 +2750,7 @@ static int madvise_collapse_errno(enum scan_result r) · 188 unmodified lines
27472750 case SCAN_PAGE_LRU:
27482751 case SCAN_DEL_PAGE_LRU:
27492752 case SCAN_PAGE_FILLED:
2753+ case SCAN_PAGE_DIRTY_OR_WRITEBACK:
27502754 return -EAGAIN;
27512755 /*
27522756 * Other: Trying again likely not to succeed / error intrinsic to
@@ -2764,7 +2768,8 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · 11 unmodified lines
27642768 struct collapse_control *cc;
27652769 struct mm_struct *mm = vma->vm_mm;
27662770 unsigned long hstart, hend, addr;
2767− int thps = 0, last_fail = SCAN_FAIL;
2771+ enum scan_result last_fail = SCAN_FAIL;
2772+ int thps = 0;
27682773 bool mmap_locked = true;
27692774
27702775 BUG_ON(vma->vm_start > start);
@@ -2785,8 +2790,10 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · 14 unmodified lines
27852790 hend = end & HPAGE_PMD_MASK;
27862791
27872792 for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
2788− int result = SCAN_FAIL;
2793+ enum scan_result result = SCAN_FAIL;
2794+ bool triggered_wb = false;
27892795
2796+retry:
27902797 if (!mmap_locked) {
27912798 cond_resched();
27922799 mmap_read_lock(mm);
@@ -2807,8 +2814,20 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · 14 unmodified lines
28072814
28082815 mmap_read_unlock(mm);
28092816 mmap_locked = false;
2817+ *lock_dropped = true;
28102818 result = hpage_collapse_scan_file(mm, addr, file, pgoff,
28112819 cc);
2820+
2821+ if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
2822+ mapping_can_writeback(file->f_mapping)) {
2823+ loff_t lstart = (loff_t)pgoff << PAGE_SHIFT;
2824+ loff_t lend = lstart + HPAGE_PMD_SIZE - 1;
2825+
2826+ filemap_write_and_wait_range(file->f_mapping, lstart, lend);
2827+ triggered_wb = true;
2828+ fput(file);
2829+ goto retry;
2830+ }
28122831 fput(file);
28132832 } else {
28142833 result = hpage_collapse_scan_pmd(mm, vma, addr,
@@ -2826,7 +2845,7 @@ handle_result: · 11 unmodified lines
28262845 case SCAN_PTE_MAPPED_HUGEPAGE:
28272846 BUG_ON(mmap_locked);
28282847 mmap_read_lock(mm);
2829− result = collapse_pte_mapped_thp(mm, addr, true);
2848+ result = try_collapse_pte_mapped_thp(mm, addr, true);
28302849 mmap_read_unlock(mm);
28312850 goto handle_result;
28322851 /* Whitelisted set of results where continuing OK */
mm/kmsan/kmsan_test.c+63−1
@@ -361,7 +361,7 @@ static void test_init_vmalloc(struct kunit *test) · 360 unmodified lines
361361 KUNIT_EXPECT_TRUE(test, report_matches(&expect));
362362 }
363363
364−/* Test case: ensure that use-after-free reporting works. */
364+/* Test case: ensure that use-after-free reporting works for kmalloc. */
365365 static void test_uaf(struct kunit *test)
366366 {
367367 EXPECTATION_USE_AFTER_FREE(expect);
@@ -378,6 +378,65 @@ static void test_uaf(struct kunit *test) · 10 unmodified lines
378378 KUNIT_EXPECT_TRUE(test, report_matches(&expect));
379379 }
380380
381+static void test_uninit_page(struct kunit *test)
382+{
383+ EXPECTATION_UNINIT_VALUE(expect);
384+ struct page *page;
385+ int *ptr;
386+
387+ kunit_info(test, "uninitialized page allocation (UMR report)\n");
388+ page = alloc_pages(GFP_KERNEL, 0);
389+ ptr = page_address(page);
390+ USE(*ptr);
391+ __free_pages(page, 0);
392+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
393+}
394+
395+static volatile char *test_uaf_pages_helper(int order, int offset)
396+{
397+ struct page *page;
398+ volatile char *var;
399+
400+ /* Memory is initialized up until __free_pages() thanks to __GFP_ZERO. */
401+ page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
402+ var = page_address(page) + offset;
403+ __free_pages(page, order);
404+
405+ return var;
406+}
407+
408+/* Test case: ensure that use-after-free reporting works for a freed page. */
409+static void test_uaf_pages(struct kunit *test)
410+{
411+ EXPECTATION_USE_AFTER_FREE(expect);
412+ volatile char value;
413+
414+ kunit_info(test, "use-after-free on a freed page (UMR report)\n");
415+ /* Allocate a single page, free it, then try to access it. */
416+ value = *test_uaf_pages_helper(0, 3);
417+ USE(value);
418+
419+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
420+}
421+
422+/* Test case: ensure that UAF reporting works for high order pages. */
423+static void test_uaf_high_order_pages(struct kunit *test)
424+{
425+ EXPECTATION_USE_AFTER_FREE(expect);
426+ volatile char value;
427+
428+ kunit_info(test,
429+ "use-after-free on a freed high-order page (UMR report)\n");
430+ /*
431+ * Create a high-order non-compound page, free it, then try to access
432+ * its tail page.
433+ */
434+ value = *test_uaf_pages_helper(1, PAGE_SIZE + 3);
435+ USE(value);
436+
437+ KUNIT_EXPECT_TRUE(test, report_matches(&expect));
438+}
439+
381440 /*
382441 * Test case: ensure that uninitialized values are propagated through per-CPU
383442 * memory.
@@ -682,7 +741,10 @@ static struct kunit_case kmsan_test_cases[] = { · 298 unmodified lines
682741 KUNIT_CASE(test_uninit_kmsan_check_memory),
683742 KUNIT_CASE(test_init_kmsan_vmap_vunmap),
684743 KUNIT_CASE(test_init_vmalloc),
744+ KUNIT_CASE(test_uninit_page),
685745 KUNIT_CASE(test_uaf),
746+ KUNIT_CASE(test_uaf_pages),
747+ KUNIT_CASE(test_uaf_high_order_pages),
686748 KUNIT_CASE(test_percpu_propagate),
687749 KUNIT_CASE(test_printk),
688750 KUNIT_CASE(test_init_memcpy),
mm/list_lru.c+3−3
@@ -187,7 +187,7 @@ bool list_lru_add_obj(struct list_lru *lru, struct list_head *item) · 186 unmodified lines
187187
188188 if (list_lru_memcg_aware(lru)) {
189189 rcu_read_lock();
190− ret = list_lru_add(lru, item, nid, mem_cgroup_from_slab_obj(item));
190+ ret = list_lru_add(lru, item, nid, mem_cgroup_from_virt(item));
191191 rcu_read_unlock();
192192 } else {
193193 ret = list_lru_add(lru, item, nid, NULL);
@@ -224,7 +224,7 @@ bool list_lru_del_obj(struct list_lru *lru, struct list_head *item) · 30 unmodified lines
224224
225225 if (list_lru_memcg_aware(lru)) {
226226 rcu_read_lock();
227− ret = list_lru_del(lru, item, nid, mem_cgroup_from_slab_obj(item));
227+ ret = list_lru_del(lru, item, nid, mem_cgroup_from_virt(item));
228228 rcu_read_unlock();
229229 } else {
230230 ret = list_lru_del(lru, item, nid, NULL);
@@ -369,7 +369,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid, · 138 unmodified lines
369369
370370 xa_for_each(&lru->xa, index, mlru) {
371371 rcu_read_lock();
372− memcg = mem_cgroup_from_id(index);
372+ memcg = mem_cgroup_from_private_id(index);
373373 if (!mem_cgroup_tryget(memcg)) {
374374 rcu_read_unlock();
375375 continue;
mm/madvise.c+12−14
@@ -109,9 +109,7 @@ void anon_vma_name_free(struct kref *kref) · 108 unmodified lines
109109
110110 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
111111 {
112− if (!rwsem_is_locked(&vma->vm_mm->mmap_lock))
113− vma_assert_locked(vma);
114−
112+ vma_assert_stabilised(vma);
115113 return vma->anon_name;
116114 }
117115
@@ -453,7 +451,7 @@ restart: · 335 unmodified lines
453451 if (!start_pte)
454452 return 0;
455453 flush_tlb_batched_pending(mm);
456− arch_enter_lazy_mmu_mode();
454+ lazy_mmu_mode_enable();
457455 for (; addr < end; pte += nr, addr += nr * PAGE_SIZE) {
458456 nr = 1;
459457 ptent = ptep_get(pte);
@@ -461,7 +459,7 @@ restart: · 1 unmodified line
461459 if (++batch_count == SWAP_CLUSTER_MAX) {
462460 batch_count = 0;
463461 if (need_resched()) {
464− arch_leave_lazy_mmu_mode();
462+ lazy_mmu_mode_disable();
465463 pte_unmap_unlock(start_pte, ptl);
466464 cond_resched();
467465 goto restart;
@@ -497,7 +495,7 @@ restart: · 29 unmodified lines
497495 if (!folio_trylock(folio))
498496 continue;
499497 folio_get(folio);
500− arch_leave_lazy_mmu_mode();
498+ lazy_mmu_mode_disable();
501499 pte_unmap_unlock(start_pte, ptl);
502500 start_pte = NULL;
503501 err = split_folio(folio);
@@ -508,7 +506,7 @@ restart: · 4 unmodified lines
508506 if (!start_pte)
509507 break;
510508 flush_tlb_batched_pending(mm);
511− arch_enter_lazy_mmu_mode();
509+ lazy_mmu_mode_enable();
512510 if (!err)
513511 nr = 0;
514512 continue;
@@ -556,7 +554,7 @@ restart: · 41 unmodified lines
556554 }
557555
558556 if (start_pte) {
559− arch_leave_lazy_mmu_mode();
557+ lazy_mmu_mode_disable();
560558 pte_unmap_unlock(start_pte, ptl);
561559 }
562560 if (pageout)
@@ -675,7 +673,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · 112 unmodified lines
675673 if (!start_pte)
676674 return 0;
677675 flush_tlb_batched_pending(mm);
678− arch_enter_lazy_mmu_mode();
676+ lazy_mmu_mode_enable();
679677 for (; addr != end; pte += nr, addr += PAGE_SIZE * nr) {
680678 nr = 1;
681679 ptent = ptep_get(pte);
@@ -694,7 +692,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · 12 unmodified lines
694692 max_nr = (end - addr) / PAGE_SIZE;
695693 nr = swap_pte_batch(pte, max_nr, ptent);
696694 nr_swap -= nr;
697− free_swap_and_cache_nr(entry, nr);
695+ swap_put_entries_direct(entry, nr);
698696 clear_not_present_full_ptes(mm, addr, pte, nr, tlb->fullmm);
699697 } else if (softleaf_is_hwpoison(entry) ||
700698 softleaf_is_poison_marker(entry)) {
@@ -724,7 +722,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · 23 unmodified lines
724722 if (!folio_trylock(folio))
725723 continue;
726724 folio_get(folio);
727− arch_leave_lazy_mmu_mode();
725+ lazy_mmu_mode_disable();
728726 pte_unmap_unlock(start_pte, ptl);
729727 start_pte = NULL;
730728 err = split_folio(folio);
@@ -735,7 +733,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · 4 unmodified lines
735733 if (!start_pte)
736734 break;
737735 flush_tlb_batched_pending(mm);
738− arch_enter_lazy_mmu_mode();
736+ lazy_mmu_mode_enable();
739737 if (!err)
740738 nr = 0;
741739 continue;
@@ -775,7 +773,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · 33 unmodified lines
775773 if (nr_swap)
776774 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
777775 if (start_pte) {
778− arch_leave_lazy_mmu_mode();
776+ lazy_mmu_mode_disable();
779777 pte_unmap_unlock(start_pte, ptl);
780778 }
781779 cond_resched();
@@ -1867,7 +1865,7 @@ static bool is_valid_madvise(unsigned long start, size_t len_in, int behavior) · 1085 unmodified lines
18671865 * madvise_should_skip() - Return if the request is invalid or nothing.
18681866 * @start: Start address of madvise-requested address range.
18691867 * @len_in: Length of madvise-requested address range.
1870− * @behavior: Requested madvise behavor.
1868+ * @behavior: Requested madvise behavior.
18711869 * @err: Pointer to store an error code from the check.
18721870 *
18731871 * If the specified behaviour is invalid or nothing would occur, we skip the
mm/memblock.c+2−2
@@ -773,7 +773,7 @@ bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_byt · 772 unmodified lines
773773 unsigned long start_pfn, end_pfn, mem_size_mb;
774774 int nid, i;
775775
776− /* calculate lose page */
776+ /* calculate lost page */
777777 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
778778 if (!numa_valid_node(nid))
779779 nr_pages += end_pfn - start_pfn;
@@ -2414,7 +2414,7 @@ EXPORT_SYMBOL_GPL(reserve_mem_find_by_name); · 1634 unmodified lines
24142414
24152415 /**
24162416 * reserve_mem_release_by_name - Release reserved memory region with a given name
2417− * @name: The name that is attatched to a reserved memory region
2417+ * @name: The name that is attached to a reserved memory region
24182418 *
24192419 * Forcibly release the pages in the reserved memory region so that those memory
24202420 * can be used as free memory. After released the reserved region size becomes 0.
mm/memcontrol-v1.c+25−3
@@ -427,6 +427,28 @@ static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, · 426 unmodified lines
427427 }
428428 #endif
429429
430+static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
431+{
432+ unsigned long val;
433+
434+ if (mem_cgroup_is_root(memcg)) {
435+ /*
436+ * Approximate root's usage from global state. This isn't
437+ * perfect, but the root usage was always an approximation.
438+ */
439+ val = global_node_page_state(NR_FILE_PAGES) +
440+ global_node_page_state(NR_ANON_MAPPED);
441+ if (swap)
442+ val += total_swap_pages - get_nr_swap_pages();
443+ } else {
444+ if (!swap)
445+ val = page_counter_read(&memcg->memory);
446+ else
447+ val = page_counter_read(&memcg->memsw);
448+ }
449+ return val;
450+}
451+
430452 static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
431453 {
432454 struct mem_cgroup_threshold_ary *t;
@@ -613,14 +635,14 @@ void memcg1_swapout(struct folio *folio, swp_entry_t entry) · 180 unmodified lines
613635 * have an ID allocated to it anymore, charge the closest online
614636 * ancestor for the swap instead and transfer the memory+swap charge.
615637 */
616− swap_memcg = mem_cgroup_id_get_online(memcg);
638+ swap_memcg = mem_cgroup_private_id_get_online(memcg);
617639 nr_entries = folio_nr_pages(folio);
618640 /* Get references for the tail pages, too */
619641 if (nr_entries > 1)
620− mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
642+ mem_cgroup_private_id_get_many(swap_memcg, nr_entries - 1);
621643 mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
622644
623− swap_cgroup_record(folio, mem_cgroup_id(swap_memcg), entry);
645+ swap_cgroup_record(folio, mem_cgroup_private_id(swap_memcg), entry);
624646
625647 folio_unqueue_deferred_split(folio);
626648 folio->memcg_data = 0;
mm/memcontrol-v1.h+2−4
@@ -22,15 +22,13 @@ · 21 unmodified lines
2222 iter != NULL; \
2323 iter = mem_cgroup_iter(NULL, iter, NULL))
2424
25−unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap);
26−
2725 void drain_all_stock(struct mem_cgroup *root_memcg);
2826
2927 unsigned long memcg_events(struct mem_cgroup *memcg, int event);
3028 int memory_stat_show(struct seq_file *m, void *v);
3129
32−void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n);
33−struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg);
30+void mem_cgroup_private_id_get_many(struct mem_cgroup *memcg, unsigned int n);
31+struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *memcg);
3432
3533 /* Cgroup v1-specific declarations */
3634 #ifdef CONFIG_MEMCG_V1
mm/memcontrol.c+34−65
@@ -646,7 +646,7 @@ static void flush_memcg_stats_dwork(struct work_struct *w) · 645 unmodified lines
646646 * in latency-sensitive paths is as cheap as possible.
647647 */
648648 __mem_cgroup_flush_stats(root_mem_cgroup, true);
649− queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME);
649+ queue_delayed_work(system_dfl_wq, &stats_flush_dwork, FLUSH_TIME);
650650 }
651651
652652 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
@@ -816,7 +816,7 @@ void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val) · 163 unmodified lines
816816 struct lruvec *lruvec;
817817
818818 rcu_read_lock();
819− memcg = mem_cgroup_from_slab_obj(p);
819+ memcg = mem_cgroup_from_virt(p);
820820
821821 /*
822822 * Untracked pages have no memcg, no lruvec. Update only the
@@ -1639,11 +1639,6 @@ unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) · 816 unmodified lines
16391639 return max;
16401640 }
16411641
1642−unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1643−{
1644− return page_counter_read(&memcg->memory);
1645−}
1646−
16471642 void __memcg_memory_event(struct mem_cgroup *memcg,
16481643 enum memcg_memory_event event, bool allow_spinning)
16491644 {
@@ -2658,7 +2653,7 @@ struct mem_cgroup *mem_cgroup_from_obj_slab(struct slab *slab, void *p) · 1008 unmodified lines
26582653 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
26592654 * cgroup_mutex, etc.
26602655 */
2661−struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
2656+struct mem_cgroup *mem_cgroup_from_virt(void *p)
26622657 {
26632658 struct slab *slab;
26642659
@@ -3320,28 +3315,6 @@ void folio_split_memcg_refs(struct folio *folio, unsigned old_order, · 655 unmodified lines
33203315 css_get_many(&__folio_memcg(folio)->css, new_refs);
33213316 }
33223317
3323−unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3324−{
3325− unsigned long val;
3326−
3327− if (mem_cgroup_is_root(memcg)) {
3328− /*
3329− * Approximate root's usage from global state. This isn't
3330− * perfect, but the root usage was always an approximation.
3331− */
3332− val = global_node_page_state(NR_FILE_PAGES) +
3333− global_node_page_state(NR_ANON_MAPPED);
3334− if (swap)
3335− val += total_swap_pages - get_nr_swap_pages();
3336− } else {
3337− if (!swap)
3338− val = page_counter_read(&memcg->memory);
3339− else
3340− val = page_counter_read(&memcg->memsw);
3341− }
3342− return val;
3343−}
3344−
33453318 static int memcg_online_kmem(struct mem_cgroup *memcg)
33463319 {
33473320 struct obj_cgroup *objcg;
@@ -3629,38 +3602,38 @@ static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) · 281 unmodified lines
36293602 */
36303603
36313604 #define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1)
3632−static DEFINE_XARRAY_ALLOC1(mem_cgroup_ids);
3605+static DEFINE_XARRAY_ALLOC1(mem_cgroup_private_ids);
36333606
3634−static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
3607+static void mem_cgroup_private_id_remove(struct mem_cgroup *memcg)
36353608 {
36363609 if (memcg->id.id > 0) {
3637− xa_erase(&mem_cgroup_ids, memcg->id.id);
3610+ xa_erase(&mem_cgroup_private_ids, memcg->id.id);
36383611 memcg->id.id = 0;
36393612 }
36403613 }
36413614
3642−void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg,
3615+void __maybe_unused mem_cgroup_private_id_get_many(struct mem_cgroup *memcg,
36433616 unsigned int n)
36443617 {
36453618 refcount_add(n, &memcg->id.ref);
36463619 }
36473620
3648−static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
3621+static void mem_cgroup_private_id_put_many(struct mem_cgroup *memcg, unsigned int n)
36493622 {
36503623 if (refcount_sub_and_test(n, &memcg->id.ref)) {
3651− mem_cgroup_id_remove(memcg);
3624+ mem_cgroup_private_id_remove(memcg);
36523625
36533626 /* Memcg ID pins CSS */
36543627 css_put(&memcg->css);
36553628 }
36563629 }
36573630
3658−static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
3631+static inline void mem_cgroup_private_id_put(struct mem_cgroup *memcg)
36593632 {
3660− mem_cgroup_id_put_many(memcg, 1);
3633+ mem_cgroup_private_id_put_many(memcg, 1);
36613634 }
36623635
3663−struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
3636+struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *memcg)
36643637 {
36653638 while (!refcount_inc_not_zero(&memcg->id.ref)) {
36663639 /*
@@ -3679,39 +3652,35 @@ struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg) · 12 unmodified lines
36793652 }
36803653
36813654 /**
3682− * mem_cgroup_from_id - look up a memcg from a memcg id
3655+ * mem_cgroup_from_private_id - look up a memcg from a memcg id
36833656 * @id: the memcg id to look up
36843657 *
36853658 * Caller must hold rcu_read_lock().
36863659 */
3687−struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
3660+struct mem_cgroup *mem_cgroup_from_private_id(unsigned short id)
36883661 {
36893662 WARN_ON_ONCE(!rcu_read_lock_held());
3690− return xa_load(&mem_cgroup_ids, id);
3663+ return xa_load(&mem_cgroup_private_ids, id);
36913664 }
36923665
3693−#ifdef CONFIG_SHRINKER_DEBUG
3694−struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
3666+struct mem_cgroup *mem_cgroup_get_from_id(u64 id)
36953667 {
36963668 struct cgroup *cgrp;
36973669 struct cgroup_subsys_state *css;
3698− struct mem_cgroup *memcg;
3670+ struct mem_cgroup *memcg = NULL;
36993671
3700− cgrp = cgroup_get_from_id(ino);
3672+ cgrp = cgroup_get_from_id(id);
37013673 if (IS_ERR(cgrp))
3702− return ERR_CAST(cgrp);
3674+ return NULL;
37033675
37043676 css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys);
37053677 if (css)
37063678 memcg = container_of(css, struct mem_cgroup, css);
3707− else
3708− memcg = ERR_PTR(-ENOENT);
37093679
37103680 cgroup_put(cgrp);
37113681
37123682 return memcg;
37133683 }
3714−#endif
37153684
37163685 static void free_mem_cgroup_per_node_info(struct mem_cgroup_per_node *pn)
37173686 {
@@ -3786,7 +3755,7 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) · 68 unmodified lines
37863755 if (!memcg)
37873756 return ERR_PTR(-ENOMEM);
37883757
3789− error = xa_alloc(&mem_cgroup_ids, &memcg->id.id, NULL,
3758+ error = xa_alloc(&mem_cgroup_private_ids, &memcg->id.id, NULL,
37903759 XA_LIMIT(1, MEM_CGROUP_ID_MAX), GFP_KERNEL);
37913760 if (error)
37923761 goto fail;
@@ -3846,7 +3815,7 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) · 53 unmodified lines
38463815 lru_gen_init_memcg(memcg);
38473816 return memcg;
38483817 fail:
3849− mem_cgroup_id_remove(memcg);
3818+ mem_cgroup_private_id_remove(memcg);
38503819 __mem_cgroup_free(memcg);
38513820 return ERR_PTR(error);
38523821 }
@@ -3920,7 +3889,7 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) · 67 unmodified lines
39203889 goto offline_kmem;
39213890
39223891 if (unlikely(mem_cgroup_is_root(memcg)) && !mem_cgroup_disabled())
3923− queue_delayed_work(system_unbound_wq, &stats_flush_dwork,
3892+ queue_delayed_work(system_dfl_wq, &stats_flush_dwork,
39243893 FLUSH_TIME);
39253894 lru_gen_online_memcg(memcg);
39263895
@@ -3929,7 +3898,7 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) · 2 unmodified lines
39293898 css_get(css);
39303899
39313900 /*
3932− * Ensure mem_cgroup_from_id() works once we're fully online.
3901+ * Ensure mem_cgroup_from_private_id() works once we're fully online.
39333902 *
39343903 * We could do this earlier and require callers to filter with
39353904 * css_tryget_online(). But right now there are no users that
@@ -3938,13 +3907,13 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) · 2 unmodified lines
39383907 * publish it here at the end of onlining. This matches the
39393908 * regular ID destruction during offlining.
39403909 */
3941− xa_store(&mem_cgroup_ids, memcg->id.id, memcg, GFP_KERNEL);
3910+ xa_store(&mem_cgroup_private_ids, memcg->id.id, memcg, GFP_KERNEL);
39423911
39433912 return 0;
39443913 offline_kmem:
39453914 memcg_offline_kmem(memcg);
39463915 remove_id:
3947− mem_cgroup_id_remove(memcg);
3916+ mem_cgroup_private_id_remove(memcg);
39483917 return -ENOMEM;
39493918 }
39503919
@@ -3967,7 +3936,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) · 16 unmodified lines
39673936
39683937 drain_all_stock(memcg);
39693938
3970− mem_cgroup_id_put(memcg);
3939+ mem_cgroup_private_id_put(memcg);
39713940 }
39723941
39733942 static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
@@ -4854,7 +4823,7 @@ int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, · 880 unmodified lines
48544823
48554824 id = lookup_swap_cgroup_id(entry);
48564825 rcu_read_lock();
4857− memcg = mem_cgroup_from_id(id);
4826+ memcg = mem_cgroup_from_private_id(id);
48584827 if (!memcg || !css_tryget_online(&memcg->css))
48594828 memcg = get_mem_cgroup_from_mm(mm);
48604829 rcu_read_unlock();
@@ -5051,7 +5020,7 @@ void mem_cgroup_migrate(struct folio *old, struct folio *new) · 190 unmodified lines
50515020 memcg = folio_memcg(old);
50525021 /*
50535022 * Note that it is normal to see !memcg for a hugetlb folio.
5054− * For e.g, itt could have been allocated when memory_hugetlb_accounting
5023+ * For e.g, it could have been allocated when memory_hugetlb_accounting
50555024 * was not selected.
50565025 */
50575026 VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(old) && !memcg, old);
@@ -5257,22 +5226,22 @@ int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry) · 199 unmodified lines
52575226 return 0;
52585227 }
52595228
5260− memcg = mem_cgroup_id_get_online(memcg);
5229+ memcg = mem_cgroup_private_id_get_online(memcg);
52615230
52625231 if (!mem_cgroup_is_root(memcg) &&
52635232 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
52645233 memcg_memory_event(memcg, MEMCG_SWAP_MAX);
52655234 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
5266− mem_cgroup_id_put(memcg);
5235+ mem_cgroup_private_id_put(memcg);
52675236 return -ENOMEM;
52685237 }
52695238
52705239 /* Get references for the tail pages, too */
52715240 if (nr_pages > 1)
5272− mem_cgroup_id_get_many(memcg, nr_pages - 1);
5241+ mem_cgroup_private_id_get_many(memcg, nr_pages - 1);
52735242 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
52745243
5275− swap_cgroup_record(folio, mem_cgroup_id(memcg), entry);
5244+ swap_cgroup_record(folio, mem_cgroup_private_id(memcg), entry);
52765245
52775246 return 0;
52785247 }
@@ -5289,7 +5258,7 @@ void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages) · 10 unmodified lines
52895258
52905259 id = swap_cgroup_clear(entry, nr_pages);
52915260 rcu_read_lock();
5292− memcg = mem_cgroup_from_id(id);
5261+ memcg = mem_cgroup_from_private_id(id);
52935262 if (memcg) {
52945263 if (!mem_cgroup_is_root(memcg)) {
52955264 if (do_memsw_account())
@@ -5298,7 +5267,7 @@ void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages) · 2 unmodified lines
52985267 page_counter_uncharge(&memcg->swap, nr_pages);
52995268 }
53005269 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
5301− mem_cgroup_id_put_many(memcg, nr_pages);
5270+ mem_cgroup_private_id_put_many(memcg, nr_pages);
53025271 }
53035272 rcu_read_unlock();
53045273 }
mm/memfd.c+1−2
@@ -1,10 +1,9 @@
1+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * memfd_create system call and file sealing support
34 *
45 * Code was originally included in shmem.c, and broken out to facilitate
56 * use by hugetlbfs as well as tmpfs.
6− *
7− * This file is released under the GPL.
87 */
98
109 #include <linux/fs.h>
mm/memory-failure.c+1−1
@@ -868,7 +868,7 @@ static int kill_accessing_process(struct task_struct *p, unsigned long pfn, · 867 unmodified lines
868868 *
869869 * MF_RECOVERED - The m-f() handler marks the page as PG_hwpoisoned'ed.
870870 * The page has been completely isolated, that is, unmapped, taken out of
871− * the buddy system, or hole-punnched out of the file mapping.
871+ * the buddy system, or hole-punched out of the file mapping.
872872 */
873873 static const char *action_name[] = {
874874 [MF_IGNORED] = "Ignored",
mm/memory-tiers.c+3−4
@@ -475,8 +475,7 @@ static void establish_demotion_targets(void) · 474 unmodified lines
475475 */
476476 list_for_each_entry_reverse(memtier, &memory_tiers, list) {
477477 tier_nodes = get_memtier_nodemask(memtier);
478− nodes_and(tier_nodes, node_states[N_CPU], tier_nodes);
479− if (!nodes_empty(tier_nodes)) {
478+ if (nodes_and(tier_nodes, node_states[N_CPU], tier_nodes)) {
480479 /*
481480 * abstract distance below the max value of this memtier
482481 * is considered toptier.
@@ -648,7 +647,7 @@ void clear_node_memory_type(int node, struct memory_dev_type *memtype) · 165 unmodified lines
648647 if (node_memory_types[node].memtype == memtype || !memtype)
649648 node_memory_types[node].map_count--;
650649 /*
651− * If we umapped all the attached devices to this node,
650+ * If we unmapped all the attached devices to this node,
652651 * clear the node memory type.
653652 */
654653 if (!node_memory_types[node].map_count) {
@@ -956,7 +955,7 @@ static ssize_t demotion_enabled_store(struct kobject *kobj, · 301 unmodified lines
956955 struct pglist_data *pgdat;
957956
958957 for_each_online_pgdat(pgdat)
959− atomic_set(&pgdat->kswapd_failures, 0);
958+ kswapd_clear_hopeless(pgdat, KSWAPD_CLEAR_HOPELESS_OTHER);
960959 }
961960
962961 return count;
mm/memory.c+260−191
@@ -934,7 +934,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, · 933 unmodified lines
934934 struct page *page;
935935
936936 if (likely(softleaf_is_swap(entry))) {
937− if (swap_duplicate(entry) < 0)
937+ if (swap_dup_entry_direct(entry) < 0)
938938 return -EIO;
939939
940940 /* make sure dst_mm is on swapoff's mmlist. */
@@ -1256,7 +1256,7 @@ again: · 315 unmodified lines
12561256 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
12571257 orig_src_pte = src_pte;
12581258 orig_dst_pte = dst_pte;
1259− arch_enter_lazy_mmu_mode();
1259+ lazy_mmu_mode_enable();
12601260
12611261 do {
12621262 nr = 1;
@@ -1325,7 +1325,7 @@ again: · 62 unmodified lines
13251325 } while (dst_pte += nr, src_pte += nr, addr += PAGE_SIZE * nr,
13261326 addr != end);
13271327
1328− arch_leave_lazy_mmu_mode();
1328+ lazy_mmu_mode_disable();
13291329 pte_unmap_unlock(orig_src_pte, src_ptl);
13301330 add_mm_rss_vec(dst_mm, rss);
13311331 pte_unmap_unlock(orig_dst_pte, dst_ptl);
@@ -1748,7 +1748,7 @@ static inline int zap_nonpresent_ptes(struct mmu_gather *tlb, · 416 unmodified lines
17481748
17491749 nr = swap_pte_batch(pte, max_nr, ptent);
17501750 rss[MM_SWAPENTS] -= nr;
1751− free_swap_and_cache_nr(entry, nr);
1751+ swap_put_entries_direct(entry, nr);
17521752 } else if (softleaf_is_migration(entry)) {
17531753 struct folio *folio = softleaf_to_folio(entry);
17541754
@@ -1821,11 +1821,70 @@ static inline int do_zap_pte_range(struct mmu_gather *tlb, · 66 unmodified lines
18211821 return nr;
18221822 }
18231823
1824+static bool pte_table_reclaim_possible(unsigned long start, unsigned long end,
1825+ struct zap_details *details)
1826+{
1827+ if (!IS_ENABLED(CONFIG_PT_RECLAIM))
1828+ return false;
1829+ /* Only zap if we are allowed to and cover the full page table. */
1830+ return details && details->reclaim_pt && (end - start >= PMD_SIZE);
1831+}
1832+
1833+static bool zap_empty_pte_table(struct mm_struct *mm, pmd_t *pmd,
1834+ spinlock_t *ptl, pmd_t *pmdval)
1835+{
1836+ spinlock_t *pml = pmd_lockptr(mm, pmd);
1837+
1838+ if (ptl != pml && !spin_trylock(pml))
1839+ return false;
1840+
1841+ *pmdval = pmdp_get(pmd);
1842+ pmd_clear(pmd);
1843+ if (ptl != pml)
1844+ spin_unlock(pml);
1845+ return true;
1846+}
1847+
1848+static bool zap_pte_table_if_empty(struct mm_struct *mm, pmd_t *pmd,
1849+ unsigned long addr, pmd_t *pmdval)
1850+{
1851+ spinlock_t *pml, *ptl = NULL;
1852+ pte_t *start_pte, *pte;
1853+ int i;
1854+
1855+ pml = pmd_lock(mm, pmd);
1856+ start_pte = pte_offset_map_rw_nolock(mm, pmd, addr, pmdval, &ptl);
1857+ if (!start_pte)
1858+ goto out_ptl;
1859+ if (ptl != pml)
1860+ spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
1861+
1862+ for (i = 0, pte = start_pte; i < PTRS_PER_PTE; i++, pte++) {
1863+ if (!pte_none(ptep_get(pte)))
1864+ goto out_ptl;
1865+ }
1866+ pte_unmap(start_pte);
1867+
1868+ pmd_clear(pmd);
1869+
1870+ if (ptl != pml)
1871+ spin_unlock(ptl);
1872+ spin_unlock(pml);
1873+ return true;
1874+out_ptl:
1875+ if (start_pte)
1876+ pte_unmap_unlock(start_pte, ptl);
1877+ if (ptl != pml)
1878+ spin_unlock(pml);
1879+ return false;
1880+}
1881+
18241882 static unsigned long zap_pte_range(struct mmu_gather *tlb,
18251883 struct vm_area_struct *vma, pmd_t *pmd,
18261884 unsigned long addr, unsigned long end,
18271885 struct zap_details *details)
18281886 {
1887+ bool can_reclaim_pt = pte_table_reclaim_possible(addr, end, details);
18291888 bool force_flush = false, force_break = false;
18301889 struct mm_struct *mm = tlb->mm;
18311890 int rss[NR_MM_COUNTERS];
@@ -1834,7 +1893,6 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, · 2 unmodified lines
18341893 pte_t *pte;
18351894 pmd_t pmdval;
18361895 unsigned long start = addr;
1837− bool can_reclaim_pt = reclaim_pt_is_enabled(start, end, details);
18381896 bool direct_reclaim = true;
18391897 int nr;
18401898
@@ -1846,7 +1904,7 @@ retry: · 5 unmodified lines
18461904 return addr;
18471905
18481906 flush_tlb_batched_pending(mm);
1849− arch_enter_lazy_mmu_mode();
1907+ lazy_mmu_mode_enable();
18501908 do {
18511909 bool any_skipped = false;
18521910
@@ -1875,10 +1933,10 @@ retry: · 22 unmodified lines
18751933 * from being repopulated by another thread.
18761934 */
18771935 if (can_reclaim_pt && direct_reclaim && addr == end)
1878− direct_reclaim = try_get_and_clear_pmd(mm, pmd, &pmdval);
1936+ direct_reclaim = zap_empty_pte_table(mm, pmd, ptl, &pmdval);
18791937
18801938 add_mm_rss_vec(mm, rss);
1881− arch_leave_lazy_mmu_mode();
1939+ lazy_mmu_mode_disable();
18821940
18831941 /* Do the actual TLB flush before dropping ptl */
18841942 if (force_flush) {
@@ -1904,10 +1962,10 @@ retry: · 19 unmodified lines
19041962 }
19051963
19061964 if (can_reclaim_pt) {
1907− if (direct_reclaim)
1908− free_pte(mm, start, tlb, pmdval);
1909− else
1910− try_to_free_pte(mm, pmd, start, tlb);
1965+ if (direct_reclaim || zap_pte_table_if_empty(mm, pmd, start, &pmdval)) {
1966+ pte_free_tlb(tlb, pmd_pgtable(pmdval), addr);
1967+ mm_dec_nr_ptes(mm);
1968+ }
19111969 }
19121970
19131971 return addr;
@@ -2499,7 +2557,6 @@ static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, · 585 unmodified lines
24992557 {
25002558 unsigned long count = vma_pages(vma);
25012559 unsigned long uaddr = vma->vm_start;
2502− int ret, i;
25032560
25042561 /* Fail if the user requested offset is beyond the end of the object */
25052562 if (offset >= num)
@@ -2509,14 +2566,7 @@ static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, · 3 unmodified lines
25092566 if (count > num - offset)
25102567 return -ENXIO;
25112568
2512− for (i = 0; i < count; i++) {
2513− ret = vm_insert_page(vma, uaddr, pages[offset + i]);
2514− if (ret < 0)
2515− return ret;
2516− uaddr += PAGE_SIZE;
2517− }
2518−
2519− return 0;
2569+ return vm_insert_pages(vma, uaddr, pages + offset, &count);
25202570 }
25212571
25222572 /**
@@ -2816,7 +2866,7 @@ static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, · 293 unmodified lines
28162866 mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
28172867 if (!pte)
28182868 return -ENOMEM;
2819− arch_enter_lazy_mmu_mode();
2869+ lazy_mmu_mode_enable();
28202870 do {
28212871 BUG_ON(!pte_none(ptep_get(pte)));
28222872 if (!pfn_modify_allowed(pfn, prot)) {
@@ -2826,7 +2876,7 @@ static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, · 3 unmodified lines
28262876 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
28272877 pfn++;
28282878 } while (pte++, addr += PAGE_SIZE, addr != end);
2829− arch_leave_lazy_mmu_mode();
2879+ lazy_mmu_mode_disable();
28302880 pte_unmap_unlock(mapped_pte, ptl);
28312881 return err;
28322882 }
@@ -3177,7 +3227,7 @@ static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, · 344 unmodified lines
31773227 return -EINVAL;
31783228 }
31793229
3180− arch_enter_lazy_mmu_mode();
3230+ lazy_mmu_mode_enable();
31813231
31823232 if (fn) {
31833233 do {
@@ -3190,7 +3240,7 @@ static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, · 6 unmodified lines
31903240 }
31913241 *mask |= PGTBL_PTE_MODIFIED;
31923242
3193− arch_leave_lazy_mmu_mode();
3243+ lazy_mmu_mode_disable();
31943244
31953245 if (mm != &init_mm)
31963246 pte_unmap_unlock(mapped_pte, ptl);
@@ -4357,12 +4407,27 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) · 1160 unmodified lines
43574407 return 0;
43584408 }
43594409
4360−static inline bool should_try_to_free_swap(struct folio *folio,
4410+/*
4411+ * Check if we should call folio_free_swap to free the swap cache.
4412+ * folio_free_swap only frees the swap cache to release the slot if swap
4413+ * count is zero, so we don't need to check the swap count here.
4414+ */
4415+static inline bool should_try_to_free_swap(struct swap_info_struct *si,
4416+ struct folio *folio,
43614417 struct vm_area_struct *vma,
4418+ unsigned int extra_refs,
43624419 unsigned int fault_flags)
43634420 {
43644421 if (!folio_test_swapcache(folio))
43654422 return false;
4423+ /*
4424+ * Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap
4425+ * cache can help save some IO or memory overhead, but these devices
4426+ * are fast, and meanwhile, swap cache pinning the slot deferring the
4427+ * release of metadata or fragmentation is a more critical issue.
4428+ */
4429+ if (data_race(si->flags & SWP_SYNCHRONOUS_IO))
4430+ return true;
43664431 if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) ||
43674432 folio_test_mlocked(folio))
43684433 return true;
@@ -4373,7 +4438,7 @@ static inline bool should_try_to_free_swap(struct folio *folio, · 4 unmodified lines
43734438 * reference only in case it's likely that we'll be the exclusive user.
43744439 */
43754440 return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) &&
4376− folio_ref_count(folio) == (1 + folio_nr_pages(folio));
4441+ folio_ref_count(folio) == (extra_refs + folio_nr_pages(folio));
43774442 }
43784443
43794444 static vm_fault_t pte_marker_clear(struct vm_fault *vmf)
@@ -4611,7 +4676,16 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf) · 231 unmodified lines
46114676 }
46124677 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
46134678
4614−static DECLARE_WAIT_QUEUE_HEAD(swapcache_wq);
4679+/* Sanity check that a folio is fully exclusive */
4680+static void check_swap_exclusive(struct folio *folio, swp_entry_t entry,
4681+ unsigned int nr_pages)
4682+{
4683+ /* Called under PT locked and folio locked, the swap count is stable */
4684+ do {
4685+ VM_WARN_ON_ONCE_FOLIO(__swap_count(entry) != 1, folio);
4686+ entry.val++;
4687+ } while (--nr_pages);
4688+}
46154689
46164690 /*
46174691 * We enter with non-exclusive mmap_lock (to exclude vma changes,
@@ -4624,17 +4698,14 @@ static DECLARE_WAIT_QUEUE_HEAD(swapcache_wq); · 6 unmodified lines
46244698 vm_fault_t do_swap_page(struct vm_fault *vmf)
46254699 {
46264700 struct vm_area_struct *vma = vmf->vma;
4627− struct folio *swapcache, *folio = NULL;
4628− DECLARE_WAITQUEUE(wait, current);
4701+ struct folio *swapcache = NULL, *folio;
46294702 struct page *page;
46304703 struct swap_info_struct *si = NULL;
46314704 rmap_t rmap_flags = RMAP_NONE;
4632− bool need_clear_cache = false;
46334705 bool exclusive = false;
46344706 softleaf_t entry;
46354707 pte_t pte;
46364708 vm_fault_t ret = 0;
4637− void *shadow = NULL;
46384709 int nr_pages;
46394710 unsigned long page_idx;
46404711 unsigned long address;
@@ -4705,57 +4776,21 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) · 64 unmodified lines
47054776 folio = swap_cache_get_folio(entry);
47064777 if (folio)
47074778 swap_update_readahead(folio, vma, vmf->address);
4708− swapcache = folio;
4709−
47104779 if (!folio) {
4711− if (data_race(si->flags & SWP_SYNCHRONOUS_IO) &&
4712− __swap_count(entry) == 1) {
4713− /* skip swapcache */
4780+ if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
47144781 folio = alloc_swap_folio(vmf);
47154782 if (folio) {
4716− __folio_set_locked(folio);
4717− __folio_set_swapbacked(folio);
4718−
4719− nr_pages = folio_nr_pages(folio);
4720− if (folio_test_large(folio))
4721− entry.val = ALIGN_DOWN(entry.val, nr_pages);
47224783 /*
4723− * Prevent parallel swapin from proceeding with
4724− * the cache flag. Otherwise, another thread
4725− * may finish swapin first, free the entry, and
4726− * swapout reusing the same entry. It's
4727− * undetectable as pte_same() returns true due
4728− * to entry reuse.
4784+ * folio is charged, so swapin can only fail due
4785+ * to raced swapin and return NULL.
47294786 */
4730− if (swapcache_prepare(entry, nr_pages)) {
4731− /*
4732− * Relax a bit to prevent rapid
4733− * repeated page faults.
4734− */
4735− add_wait_queue(&swapcache_wq, &wait);
4736− schedule_timeout_uninterruptible(1);
4737− remove_wait_queue(&swapcache_wq, &wait);
4738− goto out_page;
4739− }
4740− need_clear_cache = true;
4741−
4742− memcg1_swapin(entry, nr_pages);
4743−
4744− shadow = swap_cache_get_shadow(entry);
4745− if (shadow)
4746− workingset_refault(folio, shadow);
4747−
4748− folio_add_lru(folio);
4749−
4750− /* To provide entry to swap_read_folio() */
4751− folio->swap = entry;
4752− swap_read_folio(folio, NULL);
4753− folio->private = NULL;
4787+ swapcache = swapin_folio(entry, folio);
4788+ if (swapcache != folio)
4789+ folio_put(folio);
4790+ folio = swapcache;
47544791 }
47554792 } else {
4756− folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
4757− vmf);
4758− swapcache = folio;
4793+ folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf);
47594794 }
47604795
47614796 if (!folio) {
@@ -4777,60 +4812,58 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) · 15 unmodified lines
47774812 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
47784813 }
47794814
4815+ swapcache = folio;
47804816 ret |= folio_lock_or_retry(folio, vmf);
47814817 if (ret & VM_FAULT_RETRY)
47824818 goto out_release;
47834819
47844820 page = folio_file_page(folio, swp_offset(entry));
4785− if (swapcache) {
4786− /*
4787− * Make sure folio_free_swap() or swapoff did not release the
4788− * swapcache from under us. The page pin, and pte_same test
4789− * below, are not enough to exclude that. Even if it is still
4790− * swapcache, we need to check that the page's swap has not
4791− * changed.
4792− */
4793− if (unlikely(!folio_matches_swap_entry(folio, entry)))
4794− goto out_page;
4795−
4796− if (unlikely(PageHWPoison(page))) {
4797− /*
4798− * hwpoisoned dirty swapcache pages are kept for killing
4799− * owner processes (which may be unknown at hwpoison time)
4800− */
4801− ret = VM_FAULT_HWPOISON;
4802− goto out_page;
4803− }
4804−
4805− /*
4806− * KSM sometimes has to copy on read faults, for example, if
4807− * folio->index of non-ksm folios would be nonlinear inside the
4808− * anon VMA -- the ksm flag is lost on actual swapout.
4809− */
4810− folio = ksm_might_need_to_copy(folio, vma, vmf->address);
4811− if (unlikely(!folio)) {
4812− ret = VM_FAULT_OOM;
4813− folio = swapcache;
4814− goto out_page;
4815− } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) {
4816− ret = VM_FAULT_HWPOISON;
4817− folio = swapcache;
4818− goto out_page;
4819− }
4820− if (folio != swapcache)
4821− page = folio_page(folio, 0);
4821+ /*
4822+ * Make sure folio_free_swap() or swapoff did not release the
4823+ * swapcache from under us. The page pin, and pte_same test
4824+ * below, are not enough to exclude that. Even if it is still
4825+ * swapcache, we need to check that the page's swap has not
4826+ * changed.
4827+ */
4828+ if (unlikely(!folio_matches_swap_entry(folio, entry)))
4829+ goto out_page;
48224830
4831+ if (unlikely(PageHWPoison(page))) {
48234832 /*
4824− * If we want to map a page that's in the swapcache writable, we
4825− * have to detect via the refcount if we're really the exclusive
4826− * owner. Try removing the extra reference from the local LRU
4827− * caches if required.
4833+ * hwpoisoned dirty swapcache pages are kept for killing
4834+ * owner processes (which may be unknown at hwpoison time)
48284835 */
4829− if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache &&
4830− !folio_test_ksm(folio) && !folio_test_lru(folio))
4831− lru_add_drain();
4836+ ret = VM_FAULT_HWPOISON;
4837+ goto out_page;
48324838 }
48334839
4840+ /*
4841+ * KSM sometimes has to copy on read faults, for example, if
4842+ * folio->index of non-ksm folios would be nonlinear inside the
4843+ * anon VMA -- the ksm flag is lost on actual swapout.
4844+ */
4845+ folio = ksm_might_need_to_copy(folio, vma, vmf->address);
4846+ if (unlikely(!folio)) {
4847+ ret = VM_FAULT_OOM;
4848+ folio = swapcache;
4849+ goto out_page;
4850+ } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) {
4851+ ret = VM_FAULT_HWPOISON;
4852+ folio = swapcache;
4853+ goto out_page;
4854+ } else if (folio != swapcache)
4855+ page = folio_page(folio, 0);
4856+
4857+ /*
4858+ * If we want to map a page that's in the swapcache writable, we
4859+ * have to detect via the refcount if we're really the exclusive
4860+ * owner. Try removing the extra reference from the local LRU
4861+ * caches if required.
4862+ */
4863+ if ((vmf->flags & FAULT_FLAG_WRITE) &&
4864+ !folio_test_ksm(folio) && !folio_test_lru(folio))
4865+ lru_add_drain();
4866+
48344867 folio_throttle_swaprate(folio, GFP_KERNEL);
48354868
48364869 /*
@@ -4846,24 +4879,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) · 9 unmodified lines
48464879 goto out_nomap;
48474880 }
48484881
4849− /* allocated large folios for SWP_SYNCHRONOUS_IO */
4850− if (folio_test_large(folio) && !folio_test_swapcache(folio)) {
4851− unsigned long nr = folio_nr_pages(folio);
4852− unsigned long folio_start = ALIGN_DOWN(vmf->address, nr * PAGE_SIZE);
4853− unsigned long idx = (vmf->address - folio_start) / PAGE_SIZE;
4854− pte_t *folio_ptep = vmf->pte - idx;
4855− pte_t folio_pte = ptep_get(folio_ptep);
4856−
4857− if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) ||
4858− swap_pte_batch(folio_ptep, nr, folio_pte) != nr)
4859− goto out_nomap;
4860−
4861− page_idx = idx;
4862− address = folio_start;
4863− ptep = folio_ptep;
4864− goto check_folio;
4865− }
4866−
48674882 nr_pages = 1;
48684883 page_idx = 0;
48694884 address = vmf->address;
@@ -4907,12 +4922,37 @@ check_folio: · 37 unmodified lines
49074922 BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio));
49084923 BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page));
49094924
4925+ /*
4926+ * If a large folio already belongs to anon mapping, then we
4927+ * can just go on and map it partially.
4928+ * If not, with the large swapin check above failing, the page table
4929+ * have changed, so sub pages might got charged to the wrong cgroup,
4930+ * or even should be shmem. So we have to free it and fallback.
4931+ * Nothing should have touched it, both anon and shmem checks if a
4932+ * large folio is fully appliable before use.
4933+ *
4934+ * This will be removed once we unify folio allocation in the swap cache
4935+ * layer, where allocation of a folio stabilizes the swap entries.
4936+ */
4937+ if (!folio_test_anon(folio) && folio_test_large(folio) &&
4938+ nr_pages != folio_nr_pages(folio)) {
4939+ if (!WARN_ON_ONCE(folio_test_dirty(folio)))
4940+ swap_cache_del_folio(folio);
4941+ goto out_nomap;
4942+ }
4943+
49104944 /*
49114945 * Check under PT lock (to protect against concurrent fork() sharing
49124946 * the swap entry concurrently) for certainly exclusive pages.
49134947 */
49144948 if (!folio_test_ksm(folio)) {
4949+ /*
4950+ * The can_swapin_thp check above ensures all PTE have
4951+ * same exclusiveness. Checking just one PTE is fine.
4952+ */
49154953 exclusive = pte_swp_exclusive(vmf->orig_pte);
4954+ if (exclusive)
4955+ check_swap_exclusive(folio, entry, nr_pages);
49164956 if (folio != swapcache) {
49174957 /*
49184958 * We have a fresh page that is not exposed to the
@@ -4946,19 +4986,10 @@ check_folio: · 27 unmodified lines
49464986 /*
49474987 * Some architectures may have to restore extra metadata to the page
49484988 * when reading from swap. This metadata may be indexed by swap entry
4949− * so this must be called before swap_free().
4989+ * so this must be called before folio_put_swap().
49504990 */
49514991 arch_swap_restore(folio_swap(entry, folio), folio);
49524992
4953− /*
4954− * Remove the swap entry and conditionally try to free up the swapcache.
4955− * We're already holding a reference on the page but haven't mapped it
4956− * yet.
4957− */
4958− swap_free_nr(entry, nr_pages);
4959− if (should_try_to_free_swap(folio, vma, vmf->flags))
4960− folio_free_swap(folio);
4961−
49624993 add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages);
49634994 add_mm_counter(vma->vm_mm, MM_SWAPENTS, -nr_pages);
49644995 pte = mk_pte(page, vma->vm_page_prot);
@@ -4990,22 +5021,24 @@ check_folio: · 25 unmodified lines
49905021 vmf->orig_pte = pte_advance_pfn(pte, page_idx);
49915022
49925023 /* ksm created a completely new copy */
4993− if (unlikely(folio != swapcache && swapcache)) {
5024+ if (unlikely(folio != swapcache)) {
49945025 folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE);
49955026 folio_add_lru_vma(folio, vma);
5027+ folio_put_swap(swapcache, NULL);
49965028 } else if (!folio_test_anon(folio)) {
49975029 /*
4998− * We currently only expect small !anon folios which are either
4999− * fully exclusive or fully shared, or new allocated large
5000− * folios which are fully exclusive. If we ever get large
5001− * folios within swapcache here, we have to be careful.
5030+ * We currently only expect !anon folios that are fully
5031+ * mappable. See the comment after can_swapin_thp above.
50025032 */
5003− VM_WARN_ON_ONCE(folio_test_large(folio) && folio_test_swapcache(folio));
5004− VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
5033+ VM_WARN_ON_ONCE_FOLIO(folio_nr_pages(folio) != nr_pages, folio);
5034+ VM_WARN_ON_ONCE_FOLIO(folio_mapped(folio), folio);
50055035 folio_add_new_anon_rmap(folio, vma, address, rmap_flags);
5036+ folio_put_swap(folio, NULL);
50065037 } else {
5038+ VM_WARN_ON_ONCE(nr_pages != 1 && nr_pages != folio_nr_pages(folio));
50075039 folio_add_anon_rmap_ptes(folio, page, nr_pages, vma, address,
5008− rmap_flags);
5040+ rmap_flags);
5041+ folio_put_swap(folio, nr_pages == 1 ? page : NULL);
50095042 }
50105043
50115044 VM_BUG_ON(!folio_test_anon(folio) ||
@@ -5014,13 +5047,21 @@ check_folio: · 2 unmodified lines
50145047 arch_do_swap_page_nr(vma->vm_mm, vma, address,
50155048 pte, pte, nr_pages);
50165049
5050+ /*
5051+ * Remove the swap entry and conditionally try to free up the swapcache.
5052+ * Do it after mapping, so raced page faults will likely see the folio
5053+ * in swap cache and wait on the folio lock.
5054+ */
5055+ if (should_try_to_free_swap(si, folio, vma, nr_pages, vmf->flags))
5056+ folio_free_swap(folio);
5057+
50175058 folio_unlock(folio);
5018− if (folio != swapcache && swapcache) {
5059+ if (unlikely(folio != swapcache)) {
50195060 /*
50205061 * Hold the lock to avoid the swap entry to be reused
50215062 * until we take the PT lock for the pte_same() check
50225063 * (to avoid false positives from pte_same). For
5023− * further safety release the lock after the swap_free
5064+ * further safety release the lock after the folio_put_swap
50245065 * so that the swap count won't change under a
50255066 * parallel locked swapcache.
50265067 */
@@ -5041,12 +5082,6 @@ unlock: · 14 unmodified lines
50415082 if (vmf->pte)
50425083 pte_unmap_unlock(vmf->pte, vmf->ptl);
50435084 out:
5044− /* Clear the swap cache pin for direct swapin after PTL unlock */
5045− if (need_clear_cache) {
5046− swapcache_clear(si, entry, nr_pages);
5047− if (waitqueue_active(&swapcache_wq))
5048− wake_up(&swapcache_wq);
5049− }
50505085 if (si)
50515086 put_swap_device(si);
50525087 return ret;
@@ -5054,18 +5089,15 @@ out_nomap: · 1 unmodified line
50545089 if (vmf->pte)
50555090 pte_unmap_unlock(vmf->pte, vmf->ptl);
50565091 out_page:
5092+ if (folio_test_swapcache(folio))
5093+ folio_free_swap(folio);
50575094 folio_unlock(folio);
50585095 out_release:
50595096 folio_put(folio);
5060− if (folio != swapcache && swapcache) {
5097+ if (folio != swapcache) {
50615098 folio_unlock(swapcache);
50625099 folio_put(swapcache);
50635100 }
5064− if (need_clear_cache) {
5065− swapcache_clear(si, entry, nr_pages);
5066− if (waitqueue_active(&swapcache_wq))
5067− wake_up(&swapcache_wq);
5068− }
50695101 if (si)
50705102 put_swap_device(si);
50715103 return ret;
@@ -5935,7 +5967,7 @@ int numa_migrate_check(struct folio *folio, struct vm_fault *vmf, · 863 unmodified lines
59355967 else
59365968 *last_cpupid = folio_last_cpupid(folio);
59375969
5938− /* Record the current PID acceesing VMA */
5970+ /* Record the current PID accessing VMA */
59395971 vma_set_access_pid_bit(vma);
59405972
59415973 count_vm_numa_event(NUMA_HINT_FAULTS);
@@ -6254,7 +6286,7 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · 312 unmodified lines
62546286 * Use the maywrite version to indicate that vmf->pte may be
62556287 * modified, but since we will use pte_same() to detect the
62566288 * change of the !pte_none() entry, there is no need to recheck
6257− * the pmdval. Here we chooes to pass a dummy variable instead
6289+ * the pmdval. Here we choose to pass a dummy variable instead
62586290 * of NULL, which helps new user think about why this place is
62596291 * special.
62606292 */
@@ -7240,40 +7272,77 @@ static inline int process_huge_page( · 979 unmodified lines
72407272 return 0;
72417273 }
72427274
7243−static void clear_gigantic_page(struct folio *folio, unsigned long addr_hint,
7244− unsigned int nr_pages)
7275+static void clear_contig_highpages(struct page *page, unsigned long addr,
7276+ unsigned int nr_pages)
72457277 {
7246− unsigned long addr = ALIGN_DOWN(addr_hint, folio_size(folio));
7247− int i;
7278+ unsigned int i, count;
7279+ /*
7280+ * When clearing we want to operate on the largest extent possible to
7281+ * allow for architecture specific extent based optimizations.
7282+ *
7283+ * However, since clear_user_highpages() (and primitives clear_user_pages(),
7284+ * clear_pages()), do not call cond_resched(), limit the unit size when
7285+ * running under non-preemptible scheduling models.
7286+ */
7287+ const unsigned int unit = preempt_model_preemptible() ?
7288+ nr_pages : PROCESS_PAGES_NON_PREEMPT_BATCH;
72487289
72497290 might_sleep();
7250− for (i = 0; i < nr_pages; i++) {
7291+
7292+ for (i = 0; i < nr_pages; i += count) {
72517293 cond_resched();
7252− clear_user_highpage(folio_page(folio, i), addr + i * PAGE_SIZE);
7294+
7295+ count = min(unit, nr_pages - i);
7296+ clear_user_highpages(page + i, addr + i * PAGE_SIZE, count);
72537297 }
72547298 }
72557299
7256−static int clear_subpage(unsigned long addr, int idx, void *arg)
7257−{
7258− struct folio *folio = arg;
7259−
7260− clear_user_highpage(folio_page(folio, idx), addr);
7261− return 0;
7262−}
7300+/*
7301+ * When zeroing a folio, we want to differentiate between pages in the
7302+ * vicinity of the faulting address where we have spatial and temporal
7303+ * locality, and those far away where we don't.
7304+ *
7305+ * Use a radius of 2 for determining the local neighbourhood.
7306+ */
7307+#define FOLIO_ZERO_LOCALITY_RADIUS 2
72637308
72647309 /**
72657310 * folio_zero_user - Zero a folio which will be mapped to userspace.
72667311 * @folio: The folio to zero.
7267− * @addr_hint: The address will be accessed or the base address if uncelar.
7312+ * @addr_hint: The address accessed by the user or the base address.
72687313 */
72697314 void folio_zero_user(struct folio *folio, unsigned long addr_hint)
72707315 {
7271− unsigned int nr_pages = folio_nr_pages(folio);
7316+ const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(folio));
7317+ const long fault_idx = (addr_hint - base_addr) / PAGE_SIZE;
7318+ const struct range pg = DEFINE_RANGE(0, folio_nr_pages(folio) - 1);
7319+ const int radius = FOLIO_ZERO_LOCALITY_RADIUS;
7320+ struct range r[3];
7321+ int i;
72727322
7273− if (unlikely(nr_pages > MAX_ORDER_NR_PAGES))
7274− clear_gigantic_page(folio, addr_hint, nr_pages);
7275− else
7276− process_huge_page(addr_hint, nr_pages, clear_subpage, folio);
7323+ /*
7324+ * Faulting page and its immediate neighbourhood. Will be cleared at the
7325+ * end to keep its cachelines hot.
7326+ */
7327+ r[2] = DEFINE_RANGE(clamp_t(s64, fault_idx - radius, pg.start, pg.end),
7328+ clamp_t(s64, fault_idx + radius, pg.start, pg.end));
7329+
7330+ /* Region to the left of the fault */
7331+ r[1] = DEFINE_RANGE(pg.start,
7332+ clamp_t(s64, r[2].start - 1, pg.start - 1, r[2].start));
7333+
7334+ /* Region to the right of the fault: always valid for the common fault_idx=0 case. */
7335+ r[0] = DEFINE_RANGE(clamp_t(s64, r[2].end + 1, r[2].end, pg.end + 1),
7336+ pg.end);
7337+
7338+ for (i = 0; i < ARRAY_SIZE(r); i++) {
7339+ const unsigned long addr = base_addr + r[i].start * PAGE_SIZE;
7340+ const unsigned int nr_pages = range_len(&r[i]);
7341+ struct page *page = folio_page(folio, r[i].start);
7342+
7343+ if (nr_pages > 0)
7344+ clear_contig_highpages(page, addr, nr_pages);
7345+ }
72777346 }
72787347
72797348 static int copy_user_gigantic_page(struct folio *dst, struct folio *src,
mm/memory_hotplug.c+4−4
@@ -926,7 +926,7 @@ static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn · 925 unmodified lines
926926 *
927927 * MOVABLE : KERNEL_EARLY
928928 *
929− * Whereby KERNEL_EARLY is memory in one of the kernel zones, available sinze
929+ * Whereby KERNEL_EARLY is memory in one of the kernel zones, available since
930930 * boot. We base our calculation on KERNEL_EARLY internally, because:
931931 *
932932 * a) Hotplugged memory in one of the kernel zones can sometimes still get
@@ -946,8 +946,8 @@ static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn · 13 unmodified lines
946946 * We rely on "present pages" instead of "managed pages", as the latter is
947947 * highly unreliable and dynamic in virtualized environments, and does not
948948 * consider boot time allocations. For example, memory ballooning adjusts the
949− * managed pages when inflating/deflating the balloon, and balloon compaction
950− * can even migrate inflated pages between zones.
949+ * managed pages when inflating/deflating the balloon, and balloon page
950+ * migration can even migrate inflated pages between zones.
951951 *
952952 * Using "present pages" is better but some things to keep in mind are:
953953 *
@@ -1258,7 +1258,7 @@ static pg_data_t *hotadd_init_pgdat(int nid) · 304 unmodified lines
12581258 * NODE_DATA is preallocated (free_area_init) but its internal
12591259 * state is not allocated completely. Add missing pieces.
12601260 * Completely offline nodes stay around and they just need
1261− * reintialization.
1261+ * reinitialization.
12621262 */
12631263 pgdat = NODE_DATA(nid);
12641264
mm/mempolicy.c+2−3
@@ -365,7 +365,7 @@ static const struct mempolicy_operations { · 364 unmodified lines
365365
366366 static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
367367 {
368− return pol->flags & MPOL_MODE_FLAGS;
368+ return pol->flags & MPOL_USER_NODEMASK_FLAGS;
369369 }
370370
371371 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
@@ -1909,8 +1909,7 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, · 1537 unmodified lines
19091909 }
19101910
19111911 task_nodes = cpuset_mems_allowed(current);
1912− nodes_and(*new, *new, task_nodes);
1913− if (nodes_empty(*new))
1912+ if (!nodes_and(*new, *new, task_nodes))
19141913 goto out_put;
19151914
19161915 err = security_task_movememory(task);
mm/migrate.c+7−7
@@ -88,7 +88,7 @@ static const struct movable_operations *page_movable_ops(struct page *page) · 87 unmodified lines
8888 * back to the buddy.
8989 */
9090 if (PageOffline(page))
91− /* Only balloon compaction sets PageOffline pages movable. */
91+ /* Only balloon page migration sets PageOffline pages movable. */
9292 return offline_movable_ops;
9393 if (PageZsmalloc(page))
9494 return zsmalloc_movable_ops;
@@ -452,11 +452,12 @@ static bool remove_migration_pte(struct folio *folio, · 357 unmodified lines
452452 * Get rid of all migration entries and replace them by
453453 * references to the indicated page.
454454 */
455−void remove_migration_ptes(struct folio *src, struct folio *dst, int flags)
455+void remove_migration_ptes(struct folio *src, struct folio *dst,
456+ enum ttu_flags flags)
456457 {
457458 struct rmap_walk_arg rmap_walk_arg = {
458459 .folio = src,
459− .map_unused_to_zeropage = flags & RMP_USE_SHARED_ZEROPAGE,
460+ .map_unused_to_zeropage = flags & TTU_USE_SHARED_ZEROPAGE,
460461 };
461462
462463 struct rmap_walk_control rwc = {
@@ -464,9 +465,9 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, int flags) · 1 unmodified line
464465 .arg = &rmap_walk_arg,
465466 };
466467
467− VM_BUG_ON_FOLIO((flags & RMP_USE_SHARED_ZEROPAGE) && (src != dst), src);
468+ VM_BUG_ON_FOLIO((flags & TTU_USE_SHARED_ZEROPAGE) && (src != dst), src);
468469
469− if (flags & RMP_LOCKED)
470+ if (flags & TTU_RMAP_LOCKED)
470471 rmap_walk_locked(dst, &rwc);
471472 else
472473 rmap_walk(dst, &rwc);
@@ -1521,8 +1522,7 @@ static int unmap_and_move_huge_page(new_folio_t get_new_folio, · 1048 unmodified lines
15211522 rc = move_to_new_folio(dst, src, mode);
15221523
15231524 if (page_was_mapped)
1524− remove_migration_ptes(src, !rc ? dst : src,
1525− ttu ? RMP_LOCKED : 0);
1525+ remove_migration_ptes(src, !rc ? dst : src, ttu);
15261526
15271527 if (ttu & TTU_RMAP_LOCKED)
15281528 i_mmap_unlock_write(mapping);
mm/migrate_device.c+6−6
@@ -271,7 +271,7 @@ again: · 270 unmodified lines
271271 ptep = pte_offset_map_lock(mm, pmdp, start, &ptl);
272272 if (!ptep)
273273 goto again;
274− arch_enter_lazy_mmu_mode();
274+ lazy_mmu_mode_enable();
275275 ptep += (addr - start) / PAGE_SIZE;
276276
277277 for (; addr < end; addr += PAGE_SIZE, ptep++) {
@@ -313,7 +313,7 @@ again: · 35 unmodified lines
313313 if (folio_test_large(folio)) {
314314 int ret;
315315
316− arch_leave_lazy_mmu_mode();
316+ lazy_mmu_mode_disable();
317317 pte_unmap_unlock(ptep, ptl);
318318 ret = migrate_vma_split_folio(folio,
319319 migrate->fault_page);
@@ -356,7 +356,7 @@ again: · 36 unmodified lines
356356 if (folio && folio_test_large(folio)) {
357357 int ret;
358358
359− arch_leave_lazy_mmu_mode();
359+ lazy_mmu_mode_disable();
360360 pte_unmap_unlock(ptep, ptl);
361361 ret = migrate_vma_split_folio(folio,
362362 migrate->fault_page);
@@ -485,7 +485,7 @@ next: · 122 unmodified lines
485485 if (unmapped)
486486 flush_tlb_range(walk->vma, start, end);
487487
488− arch_leave_lazy_mmu_mode();
488+ lazy_mmu_mode_disable();
489489 pte_unmap_unlock(ptep - 1, ptl);
490490
491491 return 0;
@@ -1419,10 +1419,10 @@ EXPORT_SYMBOL(migrate_device_range); · 927 unmodified lines
14191419
14201420 /**
14211421 * migrate_device_pfns() - migrate device private pfns to normal memory.
1422− * @src_pfns: pre-popluated array of source device private pfns to migrate.
1422+ * @src_pfns: pre-populated array of source device private pfns to migrate.
14231423 * @npages: number of pages to migrate.
14241424 *
1425− * Similar to migrate_device_range() but supports non-contiguous pre-popluated
1425+ * Similar to migrate_device_range() but supports non-contiguous pre-populated
14261426 * array of device pages to migrate.
14271427 */
14281428 int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)
mm/mm_init.c+18−17
@@ -187,7 +187,7 @@ void mm_compute_batch(int overcommit_policy) · 186 unmodified lines
187187 /*
188188 * For policy OVERCOMMIT_NEVER, set batch size to 0.4% of
189189 * (total memory/#cpus), and lift it to 25% for other policies
190− * to easy the possible lock contention for percpu_counter
190+ * to ease the possible lock contention for percpu_counter
191191 * vm_committed_as, while the max limit is INT_MAX
192192 */
193193 if (overcommit_policy == OVERCOMMIT_NEVER)
@@ -646,21 +646,18 @@ int __meminit early_pfn_to_nid(unsigned long pfn) · 452 unmodified lines
646646 return nid;
647647 }
648648
649−int hashdist = HASHDIST_DEFAULT;
649+bool hashdist = HASHDIST_DEFAULT;
650650
651651 static int __init set_hashdist(char *str)
652652 {
653− if (!str)
654− return 0;
655− hashdist = simple_strtoul(str, &str, 0);
656− return 1;
653+ return kstrtobool(str, &hashdist) == 0;
657654 }
658655 __setup("hashdist=", set_hashdist);
659656
660657 static inline void fixup_hashdist(void)
661658 {
662659 if (num_node_state(N_MEMORY) == 1)
663− hashdist = 0;
660+ hashdist = false;
664661 }
665662 #else
666663 static inline void fixup_hashdist(void) {}
@@ -1748,7 +1745,7 @@ static void __init free_area_init_node(int nid) · 1081 unmodified lines
17481745 lru_gen_init_pgdat(pgdat);
17491746 }
17501747
1751−/* Any regular or high memory on that node ? */
1748+/* Any regular or high memory on that node? */
17521749 static void __init check_for_memory(pg_data_t *pgdat)
17531750 {
17541751 enum zone_type zone_type;
@@ -1810,7 +1807,6 @@ static void __init set_high_memory(void) · 55 unmodified lines
18101807
18111808 /**
18121809 * free_area_init - Initialise all pg_data_t and zone data
1813− * @max_zone_pfn: an array of max PFNs for each zone
18141810 *
18151811 * This will call free_area_init_node() for each active node in the system.
18161812 * Using the page ranges provided by memblock_set_node(), the size of each
@@ -1821,17 +1817,15 @@ static void __init set_high_memory(void) · 4 unmodified lines
18211817 * starts where the previous one ended. For example, ZONE_DMA32 starts
18221818 * at arch_max_dma_pfn.
18231819 */
1824−void __init free_area_init(unsigned long *max_zone_pfn)
1820+static void __init free_area_init(void)
18251821 {
1822+ unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
18261823 unsigned long start_pfn, end_pfn;
18271824 int i, nid, zone;
18281825 bool descending;
18291826
1830− /* Record where the zone boundaries are */
1831− memset(arch_zone_lowest_possible_pfn, 0,
1832− sizeof(arch_zone_lowest_possible_pfn));
1833− memset(arch_zone_highest_possible_pfn, 0,
1834− sizeof(arch_zone_highest_possible_pfn));
1827+ arch_zone_limits_init(max_zone_pfn);
1828+ sparse_init();
18351829
18361830 start_pfn = PHYS_PFN(memblock_start_of_DRAM());
18371831 descending = arch_has_descending_max_zone_pfns();
@@ -2048,7 +2042,7 @@ static unsigned long __init deferred_init_pages(struct zone *zone, · 210 unmodified lines
20482042 * Initialize and free pages.
20492043 *
20502044 * At this point reserved pages and struct pages that correspond to holes in
2051− * memblock.memory are already intialized so every free range has a valid
2045+ * memblock.memory are already initialized so every free range has a valid
20522046 * memory map around it.
20532047 * This ensures that access of pages that are ahead of the range being
20542048 * initialized (computing buddy page in __free_one_page()) always reads a valid
@@ -2681,13 +2675,20 @@ void __init __weak mem_init(void) · 626 unmodified lines
26812675 {
26822676 }
26832677
2678+void __init mm_core_init_early(void)
2679+{
2680+ hugetlb_cma_reserve();
2681+ hugetlb_bootmem_alloc();
2682+
2683+ free_area_init();
2684+}
2685+
26842686 /*
26852687 * Set up kernel memory allocators
26862688 */
26872689 void __init mm_core_init(void)
26882690 {
26892691 arch_mm_preinit();
2690− hugetlb_bootmem_alloc();
26912692
26922693 /* Initializations relying on SMP setup */
26932694 BUILD_BUG_ON(MAX_ZONELISTS > 2);
mm/mmap_lock.c+114−63
@@ -45,65 +45,111 @@ EXPORT_SYMBOL(__mmap_lock_do_trace_released); · 44 unmodified lines
4545
4646 #ifdef CONFIG_MMU
4747 #ifdef CONFIG_PER_VMA_LOCK
48+
49+/* State shared across __vma_[start, end]_exclude_readers. */
50+struct vma_exclude_readers_state {
51+ /* Input parameters. */
52+ struct vm_area_struct *vma;
53+ int state; /* TASK_KILLABLE or TASK_UNINTERRUPTIBLE. */
54+ bool detaching;
55+
56+ /* Output parameters. */
57+ bool detached;
58+ bool exclusive; /* Are we exclusively locked? */
59+};
60+
4861 /*
49− * __vma_enter_locked() returns 0 immediately if the vma is not
50− * attached, otherwise it waits for any current readers to finish and
51− * returns 1. Returns -EINTR if a signal is received while waiting.
62+ * Now that all readers have been evicted, mark the VMA as being out of the
63+ * 'exclude readers' state.
5264 */
53−static inline int __vma_enter_locked(struct vm_area_struct *vma,
54− bool detaching, int state)
65+static void __vma_end_exclude_readers(struct vma_exclude_readers_state *ves)
5566 {
56− int err;
57− unsigned int tgt_refcnt = VMA_LOCK_OFFSET;
67+ struct vm_area_struct *vma = ves->vma;
5868
59− mmap_assert_write_locked(vma->vm_mm);
69+ VM_WARN_ON_ONCE(ves->detached);
70+
71+ ves->detached = refcount_sub_and_test(VM_REFCNT_EXCLUDE_READERS_FLAG,
72+ &vma->vm_refcnt);
73+ __vma_lockdep_release_exclusive(vma);
74+}
75+
76+static unsigned int get_target_refcnt(struct vma_exclude_readers_state *ves)
77+{
78+ const unsigned int tgt = ves->detaching ? 0 : 1;
79+
80+ return tgt | VM_REFCNT_EXCLUDE_READERS_FLAG;
81+}
82+
83+/*
84+ * Mark the VMA as being in a state of excluding readers, check to see if any
85+ * VMA read locks are indeed held, and if so wait for them to be released.
86+ *
87+ * Note that this function pairs with vma_refcount_put() which will wake up this
88+ * thread when it detects that the last reader has released its lock.
89+ *
90+ * The ves->state parameter ought to be set to TASK_UNINTERRUPTIBLE in cases
91+ * where we wish the thread to sleep uninterruptibly or TASK_KILLABLE if a fatal
92+ * signal is permitted to kill it.
93+ *
94+ * The function sets the ves->exclusive parameter to true if readers were
95+ * excluded, or false if the VMA was detached or an error arose on wait.
96+ *
97+ * If the function indicates an exclusive lock was acquired via ves->exclusive
98+ * the caller is required to invoke __vma_end_exclude_readers() once the
99+ * exclusive state is no longer required.
100+ *
101+ * If ves->state is set to something other than TASK_UNINTERRUPTIBLE, the
102+ * function may also return -EINTR to indicate a fatal signal was received while
103+ * waiting. Otherwise, the function returns 0.
104+ */
105+static int __vma_start_exclude_readers(struct vma_exclude_readers_state *ves)
106+{
107+ struct vm_area_struct *vma = ves->vma;
108+ unsigned int tgt_refcnt = get_target_refcnt(ves);
109+ int err = 0;
60110
61− /* Additional refcnt if the vma is attached. */
62− if (!detaching)
63− tgt_refcnt++;
111+ mmap_assert_write_locked(vma->vm_mm);
64112
65113 /*
66114 * If vma is detached then only vma_mark_attached() can raise the
67115 * vm_refcnt. mmap_write_lock prevents racing with vma_mark_attached().
116+ *
117+ * See the comment describing the vm_area_struct->vm_refcnt field for
118+ * details of possible refcnt values.
68119 */
69− if (!refcount_add_not_zero(VMA_LOCK_OFFSET, &vma->vm_refcnt))
120+ if (!refcount_add_not_zero(VM_REFCNT_EXCLUDE_READERS_FLAG, &vma->vm_refcnt)) {
121+ ves->detached = true;
70122 return 0;
123+ }
71124
72− rwsem_acquire(&vma->vmlock_dep_map, 0, 0, _RET_IP_);
125+ __vma_lockdep_acquire_exclusive(vma);
73126 err = rcuwait_wait_event(&vma->vm_mm->vma_writer_wait,
74127 refcount_read(&vma->vm_refcnt) == tgt_refcnt,
75− state);
128+ ves->state);
76129 if (err) {
77− if (refcount_sub_and_test(VMA_LOCK_OFFSET, &vma->vm_refcnt)) {
78− /*
79− * The wait failed, but the last reader went away
80− * as well. Tell the caller the VMA is detached.
81− */
82− WARN_ON_ONCE(!detaching);
83− err = 0;
84− }
85− rwsem_release(&vma->vmlock_dep_map, _RET_IP_);
130+ __vma_end_exclude_readers(ves);
86131 return err;
87132 }
88− lock_acquired(&vma->vmlock_dep_map, _RET_IP_);
89133
90− return 1;
91−}
92−
93−static inline void __vma_exit_locked(struct vm_area_struct *vma, bool *detached)
94−{
95− *detached = refcount_sub_and_test(VMA_LOCK_OFFSET, &vma->vm_refcnt);
96− rwsem_release(&vma->vmlock_dep_map, _RET_IP_);
134+ __vma_lockdep_stat_mark_acquired(vma);
135+ ves->exclusive = true;
136+ return 0;
97137 }
98138
99−int __vma_start_write(struct vm_area_struct *vma, unsigned int mm_lock_seq,
100− int state)
139+int __vma_start_write(struct vm_area_struct *vma, int state)
101140 {
102− int locked;
141+ const unsigned int mm_lock_seq = __vma_raw_mm_seqnum(vma);
142+ struct vma_exclude_readers_state ves = {
143+ .vma = vma,
144+ .state = state,
145+ };
146+ int err;
103147
104− locked = __vma_enter_locked(vma, false, state);
105− if (locked < 0)
106− return locked;
148+ err = __vma_start_exclude_readers(&ves);
149+ if (err) {
150+ WARN_ON_ONCE(ves.detached);
151+ return err;
152+ }
107153
108154 /*
109155 * We should use WRITE_ONCE() here because we can have concurrent reads
@@ -113,39 +159,42 @@ int __vma_start_write(struct vm_area_struct *vma, unsigned int mm_lock_seq, · 3 unmodified lines
113159 */
114160 WRITE_ONCE(vma->vm_lock_seq, mm_lock_seq);
115161
116− if (locked) {
117− bool detached;
118−
119− __vma_exit_locked(vma, &detached);
120− WARN_ON_ONCE(detached); /* vma should remain attached */
162+ if (ves.exclusive) {
163+ __vma_end_exclude_readers(&ves);
164+ /* VMA should remain attached. */
165+ WARN_ON_ONCE(ves.detached);
121166 }
122167
123168 return 0;
124169 }
125170 EXPORT_SYMBOL_GPL(__vma_start_write);
126171
127−void vma_mark_detached(struct vm_area_struct *vma)
172+void __vma_exclude_readers_for_detach(struct vm_area_struct *vma)
128173 {
129− vma_assert_write_locked(vma);
130− vma_assert_attached(vma);
174+ struct vma_exclude_readers_state ves = {
175+ .vma = vma,
176+ .state = TASK_UNINTERRUPTIBLE,
177+ .detaching = true,
178+ };
179+ int err;
131180
132181 /*
133− * We are the only writer, so no need to use vma_refcount_put().
134− * The condition below is unlikely because the vma has been already
135− * write-locked and readers can increment vm_refcnt only temporarily
136− * before they check vm_lock_seq, realize the vma is locked and drop
137− * back the vm_refcnt. That is a narrow window for observing a raised
138− * vm_refcnt.
182+ * Wait until the VMA is detached with no readers. Since we hold the VMA
183+ * write lock, the only read locks that might be present are those from
184+ * threads trying to acquire the read lock and incrementing the
185+ * reference count before realising the write lock is held and
186+ * decrementing it.
139187 */
140− if (unlikely(!refcount_dec_and_test(&vma->vm_refcnt))) {
141− /* Wait until vma is detached with no readers. */
142− if (__vma_enter_locked(vma, true, TASK_UNINTERRUPTIBLE)) {
143− bool detached;
144−
145− __vma_exit_locked(vma, &detached);
146− WARN_ON_ONCE(!detached);
147− }
188+ err = __vma_start_exclude_readers(&ves);
189+ if (!err && ves.exclusive) {
190+ /*
191+ * Once this is complete, no readers can increment the
192+ * reference count, and the VMA is marked detached.
193+ */
194+ __vma_end_exclude_readers(&ves);
148195 }
196+ /* If an error arose but we were detached anyway, we don't care. */
197+ WARN_ON_ONCE(!ves.detached);
149198 }
150199
151200 /*
@@ -180,19 +229,21 @@ static inline struct vm_area_struct *vma_start_read(struct mm_struct *mm, · 28 unmodified lines
180229 }
181230
182231 /*
183− * If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited_acquire()
184− * will fail because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET.
232+ * If VM_REFCNT_EXCLUDE_READERS_FLAG is set,
233+ * __refcount_inc_not_zero_limited_acquire() will fail because
234+ * VM_REFCNT_LIMIT is less than VM_REFCNT_EXCLUDE_READERS_FLAG.
235+ *
185236 * Acquire fence is required here to avoid reordering against later
186237 * vm_lock_seq check and checks inside lock_vma_under_rcu().
187238 */
188239 if (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt,
189− VMA_REF_LIMIT))) {
240+ VM_REFCNT_LIMIT))) {
190241 /* return EAGAIN if vma got detached from under us */
191242 vma = oldcnt ? NULL : ERR_PTR(-EAGAIN);
192243 goto err;
193244 }
194245
195− rwsem_acquire_read(&vma->vmlock_dep_map, 0, 1, _RET_IP_);
246+ __vma_lockdep_acquire_read(vma);
196247
197248 if (unlikely(vma->vm_mm != mm))
198249 goto err_unstable;
mm/mmu_gather.c+3−3
@@ -1,3 +1,4 @@
1+// SPDX-License-Identifier: GPL-2.0
12 #include <linux/gfp.h>
23 #include <linux/highmem.h>
34 #include <linux/kernel.h>
@@ -210,10 +211,9 @@ bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page, · 206 unmodified lines
210211 PAGE_SIZE);
211212 }
212213
213−bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
214− bool delay_rmap, int page_size)
214+bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
215215 {
216− return __tlb_remove_folio_pages_size(tlb, page, 1, delay_rmap, page_size);
216+ return __tlb_remove_folio_pages_size(tlb, page, 1, false, page_size);
217217 }
218218
219219 #endif /* MMU_GATHER_NO_GATHER */
mm/mprotect.c+2−2
@@ -233,7 +233,7 @@ static long change_pte_range(struct mmu_gather *tlb, · 232 unmodified lines
233233 is_private_single_threaded = vma_is_single_threaded_private(vma);
234234
235235 flush_tlb_batched_pending(vma->vm_mm);
236− arch_enter_lazy_mmu_mode();
236+ lazy_mmu_mode_enable();
237237 do {
238238 nr_ptes = 1;
239239 oldpte = ptep_get(pte);
@@ -379,7 +379,7 @@ static long change_pte_range(struct mmu_gather *tlb, · 139 unmodified lines
379379 }
380380 }
381381 } while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
382− arch_leave_lazy_mmu_mode();
382+ lazy_mmu_mode_disable();
383383 pte_unmap_unlock(pte - 1, ptl);
384384
385385 return pages;
mm/mremap.c+5−5
@@ -260,7 +260,7 @@ static int move_ptes(struct pagetable_move_control *pmc, · 259 unmodified lines
260260 if (new_ptl != old_ptl)
261261 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
262262 flush_tlb_batched_pending(vma->vm_mm);
263− arch_enter_lazy_mmu_mode();
263+ lazy_mmu_mode_enable();
264264
265265 for (; old_addr < old_end; old_ptep += nr_ptes, old_addr += nr_ptes * PAGE_SIZE,
266266 new_ptep += nr_ptes, new_addr += nr_ptes * PAGE_SIZE) {
@@ -305,7 +305,7 @@ static int move_ptes(struct pagetable_move_control *pmc, · 38 unmodified lines
305305 }
306306 }
307307
308− arch_leave_lazy_mmu_mode();
308+ lazy_mmu_mode_disable();
309309 if (force_flush)
310310 flush_tlb_range(vma, old_end - len, old_end);
311311 if (new_ptl != old_ptl)
@@ -678,7 +678,7 @@ static bool can_realign_addr(struct pagetable_move_control *pmc, · 366 unmodified lines
678678 /*
679679 * We don't want to have to go hunting for VMAs from the end of the old
680680 * VMA to the next page table boundary, also we want to make sure the
681− * operation is wortwhile.
681+ * operation is worthwhile.
682682 *
683683 * So ensure that we only perform this realignment if the end of the
684684 * range being copied reaches or crosses the page table boundary.
@@ -926,7 +926,7 @@ static bool vrm_overlaps(struct vma_remap_struct *vrm) · 241 unmodified lines
926926 /*
927927 * Will a new address definitely be assigned? This either if the user specifies
928928 * it via MREMAP_FIXED, or if MREMAP_DONTUNMAP is used, indicating we will
929− * always detemrine a target address.
929+ * always determine a target address.
930930 */
931931 static bool vrm_implies_new_addr(struct vma_remap_struct *vrm)
932932 {
@@ -1806,7 +1806,7 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm) · 873 unmodified lines
18061806 /*
18071807 * move_vma() need us to stay 4 maps below the threshold, otherwise
18081808 * it will bail out at the very beginning.
1809− * That is a problem if we have already unmaped the regions here
1809+ * That is a problem if we have already unmapped the regions here
18101810 * (new_addr, and old_addr), because userspace will not know the
18111811 * state of the vma's after it gets -ENOMEM.
18121812 * So, to avoid such scenario we can pre-compute if the whole
mm/mseal.c+2−2
@@ -21,7 +21,7 @@ · 20 unmodified lines
2121 * It disallows unmapped regions from start to end whether they exist at the
2222 * start, in the middle, or at the end of the range, or any combination thereof.
2323 *
24− * This is because after sealng a range, there's nothing to stop memory mapping
24+ * This is because after sealing a range, there's nothing to stop memory mapping
2525 * of ranges in the remaining gaps later, meaning that the user might then
2626 * wrongly consider the entirety of the mseal()'d range to be sealed when it
2727 * in fact isn't.
@@ -124,7 +124,7 @@ static int mseal_apply(struct mm_struct *mm, · 96 unmodified lines
124124 * -EINVAL:
125125 * invalid input flags.
126126 * start address is not page aligned.
127− * Address arange (start + len) overflow.
127+ * Address range (start + len) overflow.
128128 * -ENOMEM:
129129 * addr is not a valid address (not allocated).
130130 * end (start + len) is not a valid address.
mm/numa_memblks.c+1−1
@@ -467,7 +467,7 @@ int __init numa_memblks_init(int (*init_func)(void), · 466 unmodified lines
467467 * We reset memblock back to the top-down direction
468468 * here because if we configured ACPI_NUMA, we have
469469 * parsed SRAT in init_func(). It is ok to have the
470− * reset here even if we did't configure ACPI_NUMA
470+ * reset here even if we didn't configure ACPI_NUMA
471471 * or acpi numa init fails and fallbacks to dummy
472472 * numa init.
473473 */
mm/oom_kill.c+13−13
@@ -228,7 +228,7 @@ long oom_badness(struct task_struct *p, unsigned long totalpages) · 227 unmodified lines
228228 * The baseline for the badness score is the proportion of RAM that each
229229 * task's rss, pagetable and swap space use.
230230 */
231− points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
231+ points = get_mm_rss_sum(p->mm) + get_mm_counter_sum(p->mm, MM_SWAPENTS) +
232232 mm_pgtables_bytes(p->mm) / PAGE_SIZE;
233233 task_unlock(p);
234234
@@ -402,10 +402,10 @@ static int dump_task(struct task_struct *p, void *arg) · 167 unmodified lines
402402
403403 pr_info("[%7d] %5d %5d %8lu %8lu %8lu %8lu %9lu %8ld %8lu %5hd %s\n",
404404 task->pid, from_kuid(&init_user_ns, task_uid(task)),
405− task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
406− get_mm_counter(task->mm, MM_ANONPAGES), get_mm_counter(task->mm, MM_FILEPAGES),
407− get_mm_counter(task->mm, MM_SHMEMPAGES), mm_pgtables_bytes(task->mm),
408− get_mm_counter(task->mm, MM_SWAPENTS),
405+ task->tgid, task->mm->total_vm, get_mm_rss_sum(task->mm),
406+ get_mm_counter_sum(task->mm, MM_ANONPAGES), get_mm_counter_sum(task->mm, MM_FILEPAGES),
407+ get_mm_counter_sum(task->mm, MM_SHMEMPAGES), mm_pgtables_bytes(task->mm),
408+ get_mm_counter_sum(task->mm, MM_SWAPENTS),
409409 task->signal->oom_score_adj, task->comm);
410410 task_unlock(task);
411411
@@ -458,7 +458,7 @@ static void dump_oom_victim(struct oom_control *oc, struct task_struct *victim) · 46 unmodified lines
458458
459459 static void dump_header(struct oom_control *oc)
460460 {
461− pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
461+ pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%d\n",
462462 current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
463463 current->signal->oom_score_adj);
464464 if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
@@ -604,9 +604,9 @@ static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm) · 139 unmodified lines
604604
605605 pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
606606 task_pid_nr(tsk), tsk->comm,
607− K(get_mm_counter(mm, MM_ANONPAGES)),
608− K(get_mm_counter(mm, MM_FILEPAGES)),
609− K(get_mm_counter(mm, MM_SHMEMPAGES)));
607+ K(get_mm_counter_sum(mm, MM_ANONPAGES)),
608+ K(get_mm_counter_sum(mm, MM_FILEPAGES)),
609+ K(get_mm_counter_sum(mm, MM_SHMEMPAGES)));
610610 out_finish:
611611 trace_finish_task_reaping(tsk->pid);
612612 out_unlock:
@@ -958,11 +958,11 @@ static void __oom_kill_process(struct task_struct *victim, const char *message) · 345 unmodified lines
958958 */
959959 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
960960 mark_oom_victim(victim);
961− pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n",
961+ pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%d\n",
962962 message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
963− K(get_mm_counter(mm, MM_ANONPAGES)),
964− K(get_mm_counter(mm, MM_FILEPAGES)),
965− K(get_mm_counter(mm, MM_SHMEMPAGES)),
963+ K(get_mm_counter_sum(mm, MM_ANONPAGES)),
964+ K(get_mm_counter_sum(mm, MM_FILEPAGES)),
965+ K(get_mm_counter_sum(mm, MM_SHMEMPAGES)),
966966 from_kuid(&init_user_ns, task_uid(victim)),
967967 mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
968968 task_unlock(victim);
mm/page-writeback.c+15−59
@@ -109,14 +109,6 @@ EXPORT_SYMBOL_GPL(dirty_writeback_interval); · 108 unmodified lines
109109 */
110110 unsigned int dirty_expire_interval = 30 * 100; /* centiseconds */
111111
112−/*
113− * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
114− * a full sync is triggered after this time elapses without any disk activity.
115− */
116−int laptop_mode;
117−
118−EXPORT_SYMBOL(laptop_mode);
119−
120112 /* End of sysctl-exported parameters */
121113
122114 struct wb_domain global_wb_domain;
@@ -1843,17 +1835,7 @@ static int balance_dirty_pages(struct bdi_writeback *wb, · 1720 unmodified lines
18431835 balance_domain_limits(mdtc, strictlimit);
18441836 }
18451837
1846− /*
1847− * In laptop mode, we wait until hitting the higher threshold
1848− * before starting background writeout, and then write out all
1849− * the way down to the lower threshold. So slow writers cause
1850− * minimal disk activity.
1851− *
1852− * In normal mode, we start background writeout at the lower
1853− * background_thresh, to keep the amount of dirty memory low.
1854− */
1855− if (!laptop_mode && nr_dirty > gdtc->bg_thresh &&
1856− !writeback_in_progress(wb))
1838+ if (nr_dirty > gdtc->bg_thresh && !writeback_in_progress(wb))
18571839 wb_start_background_writeback(wb);
18581840
18591841 /*
@@ -1876,10 +1858,6 @@ free_running: · 16 unmodified lines
18761858 break;
18771859 }
18781860
1879− /* Start writeback even when in laptop mode */
1880− if (unlikely(!writeback_in_progress(wb)))
1881− wb_start_background_writeback(wb);
1882−
18831861 mem_cgroup_flush_foreign(wb);
18841862
18851863 /*
@@ -2198,41 +2176,6 @@ static int dirty_writeback_centisecs_handler(const struct ctl_table *table, int · 312 unmodified lines
21982176 }
21992177 #endif
22002178
2201−void laptop_mode_timer_fn(struct timer_list *t)
2202−{
2203− struct backing_dev_info *backing_dev_info =
2204− timer_container_of(backing_dev_info, t, laptop_mode_wb_timer);
2205−
2206− wakeup_flusher_threads_bdi(backing_dev_info, WB_REASON_LAPTOP_TIMER);
2207−}
2208−
2209−/*
2210− * We've spun up the disk and we're in laptop mode: schedule writeback
2211− * of all dirty data a few seconds from now. If the flush is already scheduled
2212− * then push it back - the user is still using the disk.
2213− */
2214−void laptop_io_completion(struct backing_dev_info *info)
2215−{
2216− mod_timer(&info->laptop_mode_wb_timer, jiffies + laptop_mode);
2217−}
2218−
2219−/*
2220− * We're in laptop mode and we've just synced. The sync's writes will have
2221− * caused another writeback to be scheduled by laptop_io_completion.
2222− * Nothing needs to be written back anymore, so we unschedule the writeback.
2223− */
2224−void laptop_sync_completion(void)
2225−{
2226− struct backing_dev_info *bdi;
2227−
2228− rcu_read_lock();
2229−
2230− list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
2231− timer_delete(&bdi->laptop_mode_wb_timer);
2232−
2233− rcu_read_unlock();
2234−}
2235−
22362179 /*
22372180 * If ratelimit_pages is too high then we can get into dirty-data overload
22382181 * if a large number of processes all perform writes at the same time.
@@ -2263,6 +2206,19 @@ static int page_writeback_cpu_online(unsigned int cpu) · 24 unmodified lines
22632206
22642207 #ifdef CONFIG_SYSCTL
22652208
2209+static int laptop_mode;
2210+static int laptop_mode_handler(const struct ctl_table *table, int write,
2211+ void *buffer, size_t *lenp, loff_t *ppos)
2212+{
2213+ int ret = proc_dointvec_jiffies(table, write, buffer, lenp, ppos);
2214+
2215+ if (!ret && write)
2216+ pr_warn("%s: vm.laptop_mode is deprecated. Ignoring setting.\n",
2217+ current->comm);
2218+
2219+ return ret;
2220+}
2221+
22662222 /* this is needed for the proc_doulongvec_minmax of vm_dirty_bytes */
22672223 static const unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
22682224
@@ -2332,7 +2288,7 @@ static const struct ctl_table vm_page_writeback_sysctls[] = { · 63 unmodified lines
23322288 .data = &laptop_mode,
23332289 .maxlen = sizeof(laptop_mode),
23342290 .mode = 0644,
2335− .proc_handler = proc_dointvec_jiffies,
2291+ .proc_handler = laptop_mode_handler,
23362292 },
23372293 };
23382294 #endif
mm/page_alloc.c+275−150
@@ -1,6 +1,5 @@
11 // SPDX-License-Identifier: GPL-2.0-only
22 /*
3− * linux/mm/page_alloc.c
43 *
54 * Manages the free list, the system allocates free pages here.
65 * Note that kmalloc() lives in slab.c
@@ -1853,7 +1852,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order, · 1846 unmodified lines
18531852
18541853 /*
18551854 * As memory initialization might be integrated into KASAN,
1856− * KASAN unpoisoning and memory initializion code must be
1855+ * KASAN unpoisoning and memory initialization code must be
18571856 * kept together to avoid discrepancies in behavior.
18581857 */
18591858
@@ -2946,9 +2945,9 @@ static bool free_frozen_page_commit(struct zone *zone, · 1086 unmodified lines
29462945 * 'hopeless node' to stay in that state for a while. Let
29472946 * kswapd work again by resetting kswapd_failures.
29482947 */
2949− if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES &&
2948+ if (kswapd_test_hopeless(pgdat) &&
29502949 next_memory_node(pgdat->node_id) < MAX_NUMNODES)
2951− atomic_set(&pgdat->kswapd_failures, 0);
2950+ kswapd_clear_hopeless(pgdat, KSWAPD_CLEAR_HOPELESS_PCP);
29522951 }
29532952 return ret;
29542953 }
@@ -3112,6 +3111,15 @@ void free_unref_folios(struct folio_batch *folios) · 157 unmodified lines
31123111 folio_batch_reinit(folios);
31133112 }
31143113
3114+static void __split_page(struct page *page, unsigned int order)
3115+{
3116+ VM_WARN_ON_PAGE(PageCompound(page), page);
3117+
3118+ split_page_owner(page, order, 0);
3119+ pgalloc_tag_split(page_folio(page), order, 0);
3120+ split_page_memcg(page, order);
3121+}
3122+
31153123 /*
31163124 * split_page takes a non-compound higher-order page, and splits it into
31173125 * n (1<<order) sub-pages: page[0..n]
@@ -3124,14 +3132,12 @@ void split_page(struct page *page, unsigned int order) · 6 unmodified lines
31243132 {
31253133 int i;
31263134
3127− VM_BUG_ON_PAGE(PageCompound(page), page);
3128− VM_BUG_ON_PAGE(!page_count(page), page);
3135+ VM_WARN_ON_PAGE(!page_count(page), page);
31293136
31303137 for (i = 1; i < (1 << order); i++)
31313138 set_page_refcounted(page + i);
3132− split_page_owner(page, order, 0);
3133− pgalloc_tag_split(page_folio(page), order, 0);
3134− split_page_memcg(page, order);
3139+
3140+ __split_page(page, order);
31353141 }
31363142 EXPORT_SYMBOL_GPL(split_page);
31373143
@@ -4699,7 +4705,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, · 1561 unmodified lines
46994705 struct alloc_context *ac)
47004706 {
47014707 bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
4702− bool can_compact = gfp_compaction_allowed(gfp_mask);
4708+ bool can_compact = can_direct_reclaim && gfp_compaction_allowed(gfp_mask);
47034709 bool nofail = gfp_mask & __GFP_NOFAIL;
47044710 const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER;
47054711 struct page *page = NULL;
@@ -4712,6 +4718,8 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, · 6 unmodified lines
47124718 unsigned int cpuset_mems_cookie;
47134719 unsigned int zonelist_iter_cookie;
47144720 int reserve_flags;
4721+ bool compact_first = false;
4722+ bool can_retry_reserves = true;
47154723
47164724 if (unlikely(nofail)) {
47174725 /*
@@ -4735,6 +4743,19 @@ restart: · 17 unmodified lines
47354743 cpuset_mems_cookie = read_mems_allowed_begin();
47364744 zonelist_iter_cookie = zonelist_iter_begin();
47374745
4746+ /*
4747+ * For costly allocations, try direct compaction first, as it's likely
4748+ * that we have enough base pages and don't need to reclaim. For non-
4749+ * movable high-order allocations, do that as well, as compaction will
4750+ * try prevent permanent fragmentation by migrating from blocks of the
4751+ * same migratetype.
4752+ */
4753+ if (can_compact && (costly_order || (order > 0 &&
4754+ ac->migratetype != MIGRATE_MOVABLE))) {
4755+ compact_first = true;
4756+ compact_priority = INIT_COMPACT_PRIORITY;
4757+ }
4758+
47384759 /*
47394760 * The fast path uses conservative alloc_flags to succeed only until
47404761 * kswapd needs to be woken up, and to avoid the cost of setting up
@@ -4766,6 +4787,8 @@ restart: · 25 unmodified lines
47664787 goto nopage;
47674788 }
47684789
4790+retry:
4791+ /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */
47694792 if (alloc_flags & ALLOC_KSWAPD)
47704793 wake_all_kswapds(order, gfp_mask, ac);
47714794
@@ -4777,74 +4800,6 @@ restart: · 5 unmodified lines
47774800 if (page)
47784801 goto got_pg;
47794802
4780− /*
4781− * For costly allocations, try direct compaction first, as it's likely
4782− * that we have enough base pages and don't need to reclaim. For non-
4783− * movable high-order allocations, do that as well, as compaction will
4784− * try prevent permanent fragmentation by migrating from blocks of the
4785− * same migratetype.
4786− * Don't try this for allocations that are allowed to ignore
4787− * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen.
4788− */
4789− if (can_direct_reclaim && can_compact &&
4790− (costly_order ||
4791− (order > 0 && ac->migratetype != MIGRATE_MOVABLE))
4792− && !gfp_pfmemalloc_allowed(gfp_mask)) {
4793− page = __alloc_pages_direct_compact(gfp_mask, order,
4794− alloc_flags, ac,
4795− INIT_COMPACT_PRIORITY,
4796− &compact_result);
4797− if (page)
4798− goto got_pg;
4799−
4800− /*
4801− * Checks for costly allocations with __GFP_NORETRY, which
4802− * includes some THP page fault allocations
4803− */
4804− if (costly_order && (gfp_mask & __GFP_NORETRY)) {
4805− /*
4806− * If allocating entire pageblock(s) and compaction
4807− * failed because all zones are below low watermarks
4808− * or is prohibited because it recently failed at this
4809− * order, fail immediately unless the allocator has
4810− * requested compaction and reclaim retry.
4811− *
4812− * Reclaim is
4813− * - potentially very expensive because zones are far
4814− * below their low watermarks or this is part of very
4815− * bursty high order allocations,
4816− * - not guaranteed to help because isolate_freepages()
4817− * may not iterate over freed pages as part of its
4818− * linear scan, and
4819− * - unlikely to make entire pageblocks free on its
4820− * own.
4821− */
4822− if (compact_result == COMPACT_SKIPPED ||
4823− compact_result == COMPACT_DEFERRED)
4824− goto nopage;
4825−
4826− /*
4827− * Looks like reclaim/compaction is worth trying, but
4828− * sync compaction could be very expensive, so keep
4829− * using async compaction.
4830− */
4831− compact_priority = INIT_COMPACT_PRIORITY;
4832− }
4833− }
4834−
4835−retry:
4836− /*
4837− * Deal with possible cpuset update races or zonelist updates to avoid
4838− * infinite retries.
4839− */
4840− if (check_retry_cpuset(cpuset_mems_cookie, ac) ||
4841− check_retry_zonelist(zonelist_iter_cookie))
4842− goto restart;
4843−
4844− /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */
4845− if (alloc_flags & ALLOC_KSWAPD)
4846− wake_all_kswapds(order, gfp_mask, ac);
4847−
48484803 reserve_flags = __gfp_pfmemalloc_flags(gfp_mask);
48494804 if (reserve_flags)
48504805 alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, reserve_flags) |
@@ -4859,12 +4814,18 @@ retry: · 8 unmodified lines
48594814 ac->nodemask = NULL;
48604815 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
48614816 ac->highest_zoneidx, ac->nodemask);
4862− }
48634817
4864− /* Attempt with potentially adjusted zonelist and alloc_flags */
4865− page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
4866− if (page)
4867− goto got_pg;
4818+ /*
4819+ * The first time we adjust anything due to being allowed to
4820+ * ignore memory policies or watermarks, retry immediately. This
4821+ * allows us to keep the first allocation attempt optimistic so
4822+ * it can succeed in a zone that is still above watermarks.
4823+ */
4824+ if (can_retry_reserves) {
4825+ can_retry_reserves = false;
4826+ goto retry;
4827+ }
4828+ }
48684829
48694830 /* Caller is not willing to reclaim, we can't balance anything */
48704831 if (!can_direct_reclaim)
@@ -4875,10 +4836,12 @@ retry: · 4 unmodified lines
48754836 goto nopage;
48764837
48774838 /* Try direct reclaim and then allocating */
4878− page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac,
4879− &did_some_progress);
4880− if (page)
4881− goto got_pg;
4839+ if (!compact_first) {
4840+ page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags,
4841+ ac, &did_some_progress);
4842+ if (page)
4843+ goto got_pg;
4844+ }
48824845
48834846 /* Try direct compaction and then allocating */
48844847 page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac,
@@ -4886,6 +4849,33 @@ retry: · 1 unmodified line
48864849 if (page)
48874850 goto got_pg;
48884851
4852+ if (compact_first) {
4853+ /*
4854+ * THP page faults may attempt local node only first, but are
4855+ * then allowed to only compact, not reclaim, see
4856+ * alloc_pages_mpol().
4857+ *
4858+ * Compaction has failed above and we don't want such THP
4859+ * allocations to put reclaim pressure on a single node in a
4860+ * situation where other nodes might have plenty of available
4861+ * memory.
4862+ */
4863+ if (gfp_has_flags(gfp_mask, __GFP_NORETRY | __GFP_THISNODE))
4864+ goto nopage;
4865+
4866+ /*
4867+ * For the initial compaction attempt we have lowered its
4868+ * priority. Restore it for further retries, if those are
4869+ * allowed. With __GFP_NORETRY there will be a single round of
4870+ * reclaim and compaction with the lowered priority.
4871+ */
4872+ if (!(gfp_mask & __GFP_NORETRY))
4873+ compact_priority = DEF_COMPACT_PRIORITY;
4874+
4875+ compact_first = false;
4876+ goto retry;
4877+ }
4878+
48894879 /* Do not loop if specifically requested */
48904880 if (gfp_mask & __GFP_NORETRY)
48914881 goto nopage;
@@ -4898,6 +4888,15 @@ retry: · 6 unmodified lines
48984888 !(gfp_mask & __GFP_RETRY_MAYFAIL)))
48994889 goto nopage;
49004890
4891+ /*
4892+ * Deal with possible cpuset update races or zonelist updates to avoid
4893+ * infinite retries. No "goto retry;" can be placed above this check
4894+ * unless it can execute just once.
4895+ */
4896+ if (check_retry_cpuset(cpuset_mems_cookie, ac) ||
4897+ check_retry_zonelist(zonelist_iter_cookie))
4898+ goto restart;
4899+
49014900 if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
49024901 did_some_progress > 0, &no_progress_loops))
49034902 goto retry;
@@ -5401,9 +5400,7 @@ static void *make_alloc_exact(unsigned long addr, unsigned int order, · 497 unmodified lines
54015400 struct page *page = virt_to_page((void *)addr);
54025401 struct page *last = page + nr;
54035402
5404− split_page_owner(page, order, 0);
5405− pgalloc_tag_split(page_folio(page), order, 0);
5406− split_page_memcg(page, order);
5403+ __split_page(page, order);
54075404 while (page < --last)
54085405 set_page_refcounted(last);
54095406
@@ -6896,7 +6893,7 @@ static int __alloc_contig_migrate_range(struct compact_control *cc, · 1486 unmodified lines
68966893 return (ret < 0) ? ret : 0;
68976894 }
68986895
6899−static void split_free_pages(struct list_head *list, gfp_t gfp_mask)
6896+static void split_free_frozen_pages(struct list_head *list, gfp_t gfp_mask)
69006897 {
69016898 int order;
69026899
@@ -6908,11 +6905,10 @@ static void split_free_pages(struct list_head *list, gfp_t gfp_mask) · 5 unmodified lines
69086905 int i;
69096906
69106907 post_alloc_hook(page, order, gfp_mask);
6911− set_page_refcounted(page);
69126908 if (!order)
69136909 continue;
69146910
6915− split_page(page, order);
6911+ __split_page(page, order);
69166912
69176913 /* Add all subpages to the order-0 head, in sequence. */
69186914 list_del(&page->lru);
@@ -6956,8 +6952,14 @@ static int __alloc_contig_verify_gfp_mask(gfp_t gfp_mask, gfp_t *gfp_cc_mask) · 37 unmodified lines
69566952 return 0;
69576953 }
69586954
6955+static void __free_contig_frozen_range(unsigned long pfn, unsigned long nr_pages)
6956+{
6957+ for (; nr_pages--; pfn++)
6958+ free_frozen_pages(pfn_to_page(pfn), 0);
6959+}
6960+
69596961 /**
6960− * alloc_contig_range() -- tries to allocate given range of pages
6962+ * alloc_contig_frozen_range() -- tries to allocate given range of frozen pages
69616963 * @start: start PFN to allocate
69626964 * @end: one-past-the-last PFN to allocate
69636965 * @alloc_flags: allocation information
@@ -6972,12 +6974,15 @@ static int __alloc_contig_verify_gfp_mask(gfp_t gfp_mask, gfp_t *gfp_cc_mask) · 8 unmodified lines
69726974 * pageblocks in the range. Once isolated, the pageblocks should not
69736975 * be modified by others.
69746976 *
6975− * Return: zero on success or negative error code. On success all
6976− * pages which PFN is in [start, end) are allocated for the caller and
6977− * need to be freed with free_contig_range().
6977+ * All frozen pages which PFN is in [start, end) are allocated for the
6978+ * caller, and they could be freed with free_contig_frozen_range(),
6979+ * free_frozen_pages() also could be used to free compound frozen pages
6980+ * directly.
6981+ *
6982+ * Return: zero on success or negative error code.
69786983 */
6979−int alloc_contig_range_noprof(unsigned long start, unsigned long end,
6980− acr_flags_t alloc_flags, gfp_t gfp_mask)
6984+int alloc_contig_frozen_range_noprof(unsigned long start, unsigned long end,
6985+ acr_flags_t alloc_flags, gfp_t gfp_mask)
69816986 {
69826987 const unsigned int order = ilog2(end - start);
69836988 unsigned long outer_start, outer_end;
@@ -7093,19 +7098,18 @@ int alloc_contig_range_noprof(unsigned long start, unsigned long end, · 109 unmodified lines
70937098 }
70947099
70957100 if (!(gfp_mask & __GFP_COMP)) {
7096− split_free_pages(cc.freepages, gfp_mask);
7101+ split_free_frozen_pages(cc.freepages, gfp_mask);
70977102
70987103 /* Free head and tail (if any) */
70997104 if (start != outer_start)
7100− free_contig_range(outer_start, start - outer_start);
7105+ __free_contig_frozen_range(outer_start, start - outer_start);
71017106 if (end != outer_end)
7102− free_contig_range(end, outer_end - end);
7107+ __free_contig_frozen_range(end, outer_end - end);
71037108 } else if (start == outer_start && end == outer_end && is_power_of_2(end - start)) {
71047109 struct page *head = pfn_to_page(start);
71057110
71067111 check_new_pages(head, order);
71077112 prep_new_page(head, order, gfp_mask, 0);
7108− set_page_refcounted(head);
71097113 } else {
71107114 ret = -EINVAL;
71117115 WARN(true, "PFN range: requested [%lu, %lu), allocated [%lu, %lu)\n",
@@ -7115,36 +7119,86 @@ done: · 3 unmodified lines
71157119 undo_isolate_page_range(start, end);
71167120 return ret;
71177121 }
7118−EXPORT_SYMBOL(alloc_contig_range_noprof);
7122+EXPORT_SYMBOL(alloc_contig_frozen_range_noprof);
71197123
7120−static int __alloc_contig_pages(unsigned long start_pfn,
7121− unsigned long nr_pages, gfp_t gfp_mask)
7124+/**
7125+ * alloc_contig_range() -- tries to allocate given range of pages
7126+ * @start: start PFN to allocate
7127+ * @end: one-past-the-last PFN to allocate
7128+ * @alloc_flags: allocation information
7129+ * @gfp_mask: GFP mask.
7130+ *
7131+ * This routine is a wrapper around alloc_contig_frozen_range(), it can't
7132+ * be used to allocate compound pages, the refcount of each allocated page
7133+ * will be set to one.
7134+ *
7135+ * All pages which PFN is in [start, end) are allocated for the caller,
7136+ * and should be freed with free_contig_range() or by manually calling
7137+ * __free_page() on each allocated page.
7138+ *
7139+ * Return: zero on success or negative error code.
7140+ */
7141+int alloc_contig_range_noprof(unsigned long start, unsigned long end,
7142+ acr_flags_t alloc_flags, gfp_t gfp_mask)
71227143 {
7123− unsigned long end_pfn = start_pfn + nr_pages;
7144+ int ret;
71247145
7125− return alloc_contig_range_noprof(start_pfn, end_pfn, ACR_FLAGS_NONE,
7126− gfp_mask);
7146+ if (WARN_ON(gfp_mask & __GFP_COMP))
7147+ return -EINVAL;
7148+
7149+ ret = alloc_contig_frozen_range_noprof(start, end, alloc_flags, gfp_mask);
7150+ if (!ret)
7151+ set_pages_refcounted(pfn_to_page(start), end - start);
7152+
7153+ return ret;
71277154 }
7155+EXPORT_SYMBOL(alloc_contig_range_noprof);
71287156
71297157 static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
7130− unsigned long nr_pages)
7158+ unsigned long nr_pages, bool skip_hugetlb,
7159+ bool *skipped_hugetlb)
71317160 {
7132− unsigned long i, end_pfn = start_pfn + nr_pages;
7161+ unsigned long end_pfn = start_pfn + nr_pages;
71337162 struct page *page;
71347163
7135− for (i = start_pfn; i < end_pfn; i++) {
7136− page = pfn_to_online_page(i);
7164+ while (start_pfn < end_pfn) {
7165+ unsigned long step = 1;
7166+
7167+ page = pfn_to_online_page(start_pfn);
71377168 if (!page)
71387169 return false;
71397170
71407171 if (page_zone(page) != z)
71417172 return false;
71427173
7143− if (PageReserved(page))
7174+ if (page_is_unmovable(z, page, PB_ISOLATE_MODE_OTHER, &step))
71447175 return false;
71457176
7146− if (PageHuge(page))
7147− return false;
7177+ /*
7178+ * Only consider ranges containing hugepages if those pages are
7179+ * smaller than the requested contiguous region. e.g.:
7180+ * Move 2MB pages to free up a 1GB range.
7181+ * Don't move 1GB pages to free up a 2MB range.
7182+ *
7183+ * This makes contiguous allocation more reliable if multiple
7184+ * hugepage sizes are used without causing needless movement.
7185+ */
7186+ if (PageHuge(page)) {
7187+ unsigned int order;
7188+
7189+ if (skip_hugetlb) {
7190+ *skipped_hugetlb = true;
7191+ return false;
7192+ }
7193+
7194+ page = compound_head(page);
7195+ order = compound_order(page);
7196+ if ((order >= MAX_FOLIO_ORDER) ||
7197+ (nr_pages <= (1 << order)))
7198+ return false;
7199+ }
7200+
7201+ start_pfn += step;
71487202 }
71497203 return true;
71507204 }
@@ -7158,7 +7212,7 @@ static bool zone_spans_last_pfn(const struct zone *zone, · 7 unmodified lines
71587212 }
71597213
71607214 /**
7161− * alloc_contig_pages() -- tries to find and allocate contiguous range of pages
7215+ * alloc_contig_frozen_pages() -- tries to find and allocate contiguous range of frozen pages
71627216 * @nr_pages: Number of contiguous pages to allocate
71637217 * @gfp_mask: GFP mask. Node/zone/placement hints limit the search; only some
71647218 * action and reclaim modifiers are supported. Reclaim modifiers
@@ -7166,28 +7220,34 @@ static bool zone_spans_last_pfn(const struct zone *zone, · 1 unmodified line
71667220 * @nid: Target node
71677221 * @nodemask: Mask for other possible nodes
71687222 *
7169− * This routine is a wrapper around alloc_contig_range(). It scans over zones
7170− * on an applicable zonelist to find a contiguous pfn range which can then be
7171− * tried for allocation with alloc_contig_range(). This routine is intended
7172− * for allocation requests which can not be fulfilled with the buddy allocator.
7223+ * This routine is a wrapper around alloc_contig_frozen_range(). It scans over
7224+ * zones on an applicable zonelist to find a contiguous pfn range which can then
7225+ * be tried for allocation with alloc_contig_frozen_range(). This routine is
7226+ * intended for allocation requests which can not be fulfilled with the buddy
7227+ * allocator.
71737228 *
71747229 * The allocated memory is always aligned to a page boundary. If nr_pages is a
71757230 * power of two, then allocated range is also guaranteed to be aligned to same
71767231 * nr_pages (e.g. 1GB request would be aligned to 1GB).
71777232 *
7178− * Allocated pages can be freed with free_contig_range() or by manually calling
7179− * __free_page() on each allocated page.
7233+ * Allocated frozen pages need be freed with free_contig_frozen_range(),
7234+ * or by manually calling free_frozen_pages() on each allocated frozen
7235+ * non-compound page, for compound frozen pages could be freed with
7236+ * free_frozen_pages() directly.
71807237 *
7181− * Return: pointer to contiguous pages on success, or NULL if not successful.
7238+ * Return: pointer to contiguous frozen pages on success, or NULL if not successful.
71827239 */
7183−struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
7184− int nid, nodemask_t *nodemask)
7240+struct page *alloc_contig_frozen_pages_noprof(unsigned long nr_pages,
7241+ gfp_t gfp_mask, int nid, nodemask_t *nodemask)
71857242 {
71867243 unsigned long ret, pfn, flags;
71877244 struct zonelist *zonelist;
71887245 struct zone *zone;
71897246 struct zoneref *z;
7247+ bool skip_hugetlb = true;
7248+ bool skipped_hugetlb = false;
71907249
7250+retry:
71917251 zonelist = node_zonelist(nid, gfp_mask);
71927252 for_each_zone_zonelist_nodemask(zone, z, zonelist,
71937253 gfp_zone(gfp_mask), nodemask) {
@@ -7195,16 +7255,20 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, · 1 unmodified line
71957255
71967256 pfn = ALIGN(zone->zone_start_pfn, nr_pages);
71977257 while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
7198− if (pfn_range_valid_contig(zone, pfn, nr_pages)) {
7258+ if (pfn_range_valid_contig(zone, pfn, nr_pages,
7259+ skip_hugetlb,
7260+ &skipped_hugetlb)) {
71997261 /*
72007262 * We release the zone lock here because
7201− * alloc_contig_range() will also lock the zone
7202− * at some point. If there's an allocation
7203− * spinning on this lock, it may win the race
7204− * and cause alloc_contig_range() to fail...
7263+ * alloc_contig_frozen_range() will also lock
7264+ * the zone at some point. If there's an
7265+ * allocation spinning on this lock, it may
7266+ * win the race and cause allocation to fail.
72057267 */
72067268 spin_unlock_irqrestore(&zone->lock, flags);
7207− ret = __alloc_contig_pages(pfn, nr_pages,
7269+ ret = alloc_contig_frozen_range_noprof(pfn,
7270+ pfn + nr_pages,
7271+ ACR_FLAGS_NONE,
72087272 gfp_mask);
72097273 if (!ret)
72107274 return pfn_to_page(pfn);
@@ -7214,35 +7278,96 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, · 3 unmodified lines
72147278 }
72157279 spin_unlock_irqrestore(&zone->lock, flags);
72167280 }
7281+ /*
7282+ * If we failed, retry the search, but treat regions with HugeTLB pages
7283+ * as valid targets. This retains fast-allocations on first pass
7284+ * without trying to migrate HugeTLB pages (which may fail). On the
7285+ * second pass, we will try moving HugeTLB pages when those pages are
7286+ * smaller than the requested contiguous region size.
7287+ */
7288+ if (skip_hugetlb && skipped_hugetlb) {
7289+ skip_hugetlb = false;
7290+ goto retry;
7291+ }
72177292 return NULL;
72187293 }
7219−#endif /* CONFIG_CONTIG_ALLOC */
7294+EXPORT_SYMBOL(alloc_contig_frozen_pages_noprof);
72207295
7221−void free_contig_range(unsigned long pfn, unsigned long nr_pages)
7296+/**
7297+ * alloc_contig_pages() -- tries to find and allocate contiguous range of pages
7298+ * @nr_pages: Number of contiguous pages to allocate
7299+ * @gfp_mask: GFP mask.
7300+ * @nid: Target node
7301+ * @nodemask: Mask for other possible nodes
7302+ *
7303+ * This routine is a wrapper around alloc_contig_frozen_pages(), it can't
7304+ * be used to allocate compound pages, the refcount of each allocated page
7305+ * will be set to one.
7306+ *
7307+ * Allocated pages can be freed with free_contig_range() or by manually
7308+ * calling __free_page() on each allocated page.
7309+ *
7310+ * Return: pointer to contiguous pages on success, or NULL if not successful.
7311+ */
7312+struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
7313+ int nid, nodemask_t *nodemask)
72227314 {
7223− unsigned long count = 0;
7224− struct folio *folio = pfn_folio(pfn);
7315+ struct page *page;
72257316
7226− if (folio_test_large(folio)) {
7227− int expected = folio_nr_pages(folio);
7317+ if (WARN_ON(gfp_mask & __GFP_COMP))
7318+ return NULL;
72287319
7229− if (nr_pages == expected)
7230− folio_put(folio);
7231− else
7232− WARN(true, "PFN %lu: nr_pages %lu != expected %d\n",
7233− pfn, nr_pages, expected);
7320+ page = alloc_contig_frozen_pages_noprof(nr_pages, gfp_mask, nid,
7321+ nodemask);
7322+ if (page)
7323+ set_pages_refcounted(page, nr_pages);
7324+
7325+ return page;
7326+}
7327+EXPORT_SYMBOL(alloc_contig_pages_noprof);
7328+
7329+/**
7330+ * free_contig_frozen_range() -- free the contiguous range of frozen pages
7331+ * @pfn: start PFN to free
7332+ * @nr_pages: Number of contiguous frozen pages to free
7333+ *
7334+ * This can be used to free the allocated compound/non-compound frozen pages.
7335+ */
7336+void free_contig_frozen_range(unsigned long pfn, unsigned long nr_pages)
7337+{
7338+ struct page *first_page = pfn_to_page(pfn);
7339+ const unsigned int order = ilog2(nr_pages);
7340+
7341+ if (WARN_ON_ONCE(first_page != compound_head(first_page)))
7342+ return;
7343+
7344+ if (PageHead(first_page)) {
7345+ WARN_ON_ONCE(order != compound_order(first_page));
7346+ free_frozen_pages(first_page, order);
72347347 return;
72357348 }
72367349
7237− for (; nr_pages--; pfn++) {
7238− struct page *page = pfn_to_page(pfn);
7350+ __free_contig_frozen_range(pfn, nr_pages);
7351+}
7352+EXPORT_SYMBOL(free_contig_frozen_range);
72397353
7240− count += page_count(page) != 1;
7241− __free_page(page);
7242− }
7243− WARN(count != 0, "%lu pages are still in use!\n", count);
7354+/**
7355+ * free_contig_range() -- free the contiguous range of pages
7356+ * @pfn: start PFN to free
7357+ * @nr_pages: Number of contiguous pages to free
7358+ *
7359+ * This can be only used to free the allocated non-compound pages.
7360+ */
7361+void free_contig_range(unsigned long pfn, unsigned long nr_pages)
7362+{
7363+ if (WARN_ON_ONCE(PageHead(pfn_to_page(pfn))))
7364+ return;
7365+
7366+ for (; nr_pages--; pfn++)
7367+ __free_page(pfn_to_page(pfn));
72447368 }
72457369 EXPORT_SYMBOL(free_contig_range);
7370+#endif /* CONFIG_CONTIG_ALLOC */
72467371
72477372 /*
72487373 * Effectively disable pcplists for the zone by setting the high limit to 0
@@ -7658,7 +7783,7 @@ struct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned · 409 unmodified lines
76587783 * unsafe in NMI. If spin_trylock() is called from hard IRQ the current
76597784 * task may be waiting for one rt_spin_lock, but rt_spin_trylock() will
76607785 * mark the task as the owner of another rt_spin_lock which will
7661− * confuse PI logic, so return immediately if called form hard IRQ or
7786+ * confuse PI logic, so return immediately if called from hard IRQ or
76627787 * NMI.
76637788 *
76647789 * Note, irqs_disabled() case is ok. This function can be called
mm/page_io.c+2−2
@@ -450,14 +450,14 @@ void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug) · 449 unmodified lines
450450
451451 VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
452452 /*
453− * ->flags can be updated non-atomicially (scan_swap_map_slots),
453+ * ->flags can be updated non-atomically (scan_swap_map_slots),
454454 * but that will never affect SWP_FS_OPS, so the data_race
455455 * is safe.
456456 */
457457 if (data_race(sis->flags & SWP_FS_OPS))
458458 swap_writepage_fs(folio, swap_plug);
459459 /*
460− * ->flags can be updated non-atomicially (scan_swap_map_slots),
460+ * ->flags can be updated non-atomically (scan_swap_map_slots),
461461 * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
462462 * is safe.
463463 */
mm/page_isolation.c+100−89
@@ -15,6 +15,100 @@ · 14 unmodified lines
1515 #define CREATE_TRACE_POINTS
1616 #include <trace/events/page_isolation.h>
1717
18+bool page_is_unmovable(struct zone *zone, struct page *page,
19+ enum pb_isolate_mode mode, unsigned long *step)
20+{
21+ /*
22+ * Both, bootmem allocations and memory holes are marked
23+ * PG_reserved and are unmovable. We can even have unmovable
24+ * allocations inside ZONE_MOVABLE, for example when
25+ * specifying "movablecore".
26+ */
27+ if (PageReserved(page))
28+ return true;
29+
30+ /*
31+ * If the zone is movable and we have ruled out all reserved
32+ * pages then it should be reasonably safe to assume the rest
33+ * is movable.
34+ */
35+ if (zone_idx(zone) == ZONE_MOVABLE)
36+ return false;
37+
38+ /*
39+ * Hugepages are not in LRU lists, but they're movable.
40+ * THPs are on the LRU, but need to be counted as #small pages.
41+ * We need not scan over tail pages because we don't
42+ * handle each tail page individually in migration.
43+ */
44+ if (PageHuge(page) || PageCompound(page)) {
45+ struct folio *folio = page_folio(page);
46+
47+ if (folio_test_hugetlb(folio)) {
48+ struct hstate *h;
49+
50+ if (!IS_ENABLED(CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION))
51+ return true;
52+
53+ /*
54+ * The huge page may be freed so can not
55+ * use folio_hstate() directly.
56+ */
57+ h = size_to_hstate(folio_size(folio));
58+ if (h && !hugepage_migration_supported(h))
59+ return true;
60+
61+ } else if (!folio_test_lru(folio)) {
62+ return true;
63+ }
64+
65+ *step = folio_nr_pages(folio) - folio_page_idx(folio, page);
66+ return false;
67+ }
68+
69+ /*
70+ * We can't use page_count without pin a page
71+ * because another CPU can free compound page.
72+ * This check already skips compound tails of THP
73+ * because their page->_refcount is zero at all time.
74+ */
75+ if (!page_ref_count(page)) {
76+ if (PageBuddy(page))
77+ *step = (1 << buddy_order(page));
78+ return false;
79+ }
80+
81+ /*
82+ * The HWPoisoned page may be not in buddy system, and
83+ * page_count() is not 0.
84+ */
85+ if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && PageHWPoison(page))
86+ return false;
87+
88+ /*
89+ * We treat all PageOffline() pages as movable when offlining
90+ * to give drivers a chance to decrement their reference count
91+ * in MEM_GOING_OFFLINE in order to indicate that these pages
92+ * can be offlined as there are no direct references anymore.
93+ * For actually unmovable PageOffline() where the driver does
94+ * not support this, we will fail later when trying to actually
95+ * move these pages that still have a reference count > 0.
96+ * (false negatives in this function only)
97+ */
98+ if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && PageOffline(page))
99+ return false;
100+
101+ if (PageLRU(page) || page_has_movable_ops(page))
102+ return false;
103+
104+ /*
105+ * If there are RECLAIMABLE pages, we need to check
106+ * it. But now, memory offline itself doesn't call
107+ * shrink_node_slabs() and it still to be fixed.
108+ */
109+ return true;
110+}
111+
18112 /*
19113 * This function checks whether the range [start_pfn, end_pfn) includes
20114 * unmovable pages or not. The range must fall into a single pageblock and
@@ -35,7 +129,6 @@ static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long e · 14 unmodified lines
35129 {
36130 struct page *page = pfn_to_page(start_pfn);
37131 struct zone *zone = page_zone(page);
38− unsigned long pfn;
39132
40133 VM_BUG_ON(pageblock_start_pfn(start_pfn) !=
41134 pageblock_start_pfn(end_pfn - 1));
@@ -52,96 +145,14 @@ static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long e · 10 unmodified lines
52145 return page;
53146 }
54147
55− for (pfn = start_pfn; pfn < end_pfn; pfn++) {
56− page = pfn_to_page(pfn);
148+ while (start_pfn < end_pfn) {
149+ unsigned long step = 1;
57150
58− /*
59− * Both, bootmem allocations and memory holes are marked
60− * PG_reserved and are unmovable. We can even have unmovable
61− * allocations inside ZONE_MOVABLE, for example when
62− * specifying "movablecore".
63− */
64− if (PageReserved(page))
151+ page = pfn_to_page(start_pfn);
152+ if (page_is_unmovable(zone, page, mode, &step))
65153 return page;
66154
67− /*
68− * If the zone is movable and we have ruled out all reserved
69− * pages then it should be reasonably safe to assume the rest
70− * is movable.
71− */
72− if (zone_idx(zone) == ZONE_MOVABLE)
73− continue;
74−
75− /*
76− * Hugepages are not in LRU lists, but they're movable.
77− * THPs are on the LRU, but need to be counted as #small pages.
78− * We need not scan over tail pages because we don't
79− * handle each tail page individually in migration.
80− */
81− if (PageHuge(page) || PageTransCompound(page)) {
82− struct folio *folio = page_folio(page);
83− unsigned int skip_pages;
84−
85− if (PageHuge(page)) {
86− struct hstate *h;
87−
88− /*
89− * The huge page may be freed so can not
90− * use folio_hstate() directly.
91− */
92− h = size_to_hstate(folio_size(folio));
93− if (h && !hugepage_migration_supported(h))
94− return page;
95− } else if (!folio_test_lru(folio)) {
96− return page;
97− }
98−
99− skip_pages = folio_nr_pages(folio) - folio_page_idx(folio, page);
100− pfn += skip_pages - 1;
101− continue;
102− }
103−
104− /*
105− * We can't use page_count without pin a page
106− * because another CPU can free compound page.
107− * This check already skips compound tails of THP
108− * because their page->_refcount is zero at all time.
109− */
110− if (!page_ref_count(page)) {
111− if (PageBuddy(page))
112− pfn += (1 << buddy_order(page)) - 1;
113− continue;
114− }
115−
116− /*
117− * The HWPoisoned page may be not in buddy system, and
118− * page_count() is not 0.
119− */
120− if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && PageHWPoison(page))
121− continue;
122−
123− /*
124− * We treat all PageOffline() pages as movable when offlining
125− * to give drivers a chance to decrement their reference count
126− * in MEM_GOING_OFFLINE in order to indicate that these pages
127− * can be offlined as there are no direct references anymore.
128− * For actually unmovable PageOffline() where the driver does
129− * not support this, we will fail later when trying to actually
130− * move these pages that still have a reference count > 0.
131− * (false negatives in this function only)
132− */
133− if ((mode == PB_ISOLATE_MODE_MEM_OFFLINE) && PageOffline(page))
134− continue;
135−
136− if (PageLRU(page) || page_has_movable_ops(page))
137− continue;
138−
139− /*
140− * If there are RECLAIMABLE pages, we need to check
141− * it. But now, memory offline itself doesn't call
142− * shrink_node_slabs() and it still to be fixed.
143− */
144− return page;
155+ start_pfn += step;
145156 }
146157 return NULL;
147158 }
@@ -301,7 +312,7 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages) · 153 unmodified lines
301312 * pageblock. When not all pageblocks within a page are isolated at the same
302313 * time, free page accounting can go wrong. For example, in the case of
303314 * MAX_PAGE_ORDER = pageblock_order + 1, a MAX_PAGE_ORDER page has two
304− * pagelbocks.
315+ * pageblocks.
305316 * [ MAX_PAGE_ORDER ]
306317 * [ pageblock0 | pageblock1 ]
307318 * When either pageblock is isolated, if it is a free page, the page is not
mm/page_reporting.c+1−1
@@ -123,7 +123,7 @@ page_reporting_drain(struct page_reporting_dev_info *prdev, · 122 unmodified lines
123123 continue;
124124
125125 /*
126− * If page was not comingled with another page we can
126+ * If page was not commingled with another page we can
127127 * consider the result to be "reported" since the page
128128 * hasn't been modified, otherwise we will need to
129129 * report on the new larger page when we make our way
mm/page_table_check.c+22−19
@@ -145,34 +145,37 @@ void __page_table_check_zero(struct page *page, unsigned int order) · 144 unmodified lines
145145 rcu_read_unlock();
146146 }
147147
148−void __page_table_check_pte_clear(struct mm_struct *mm, pte_t pte)
148+void __page_table_check_pte_clear(struct mm_struct *mm, unsigned long addr,
149+ pte_t pte)
149150 {
150151 if (&init_mm == mm)
151152 return;
152153
153− if (pte_user_accessible_page(pte)) {
154+ if (pte_user_accessible_page(pte, addr)) {
154155 page_table_check_clear(pte_pfn(pte), PAGE_SIZE >> PAGE_SHIFT);
155156 }
156157 }
157158 EXPORT_SYMBOL(__page_table_check_pte_clear);
158159
159−void __page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd)
160+void __page_table_check_pmd_clear(struct mm_struct *mm, unsigned long addr,
161+ pmd_t pmd)
160162 {
161163 if (&init_mm == mm)
162164 return;
163165
164− if (pmd_user_accessible_page(pmd)) {
166+ if (pmd_user_accessible_page(pmd, addr)) {
165167 page_table_check_clear(pmd_pfn(pmd), PMD_SIZE >> PAGE_SHIFT);
166168 }
167169 }
168170 EXPORT_SYMBOL(__page_table_check_pmd_clear);
169171
170−void __page_table_check_pud_clear(struct mm_struct *mm, pud_t pud)
172+void __page_table_check_pud_clear(struct mm_struct *mm, unsigned long addr,
173+ pud_t pud)
171174 {
172175 if (&init_mm == mm)
173176 return;
174177
175− if (pud_user_accessible_page(pud)) {
178+ if (pud_user_accessible_page(pud, addr)) {
176179 page_table_check_clear(pud_pfn(pud), PUD_SIZE >> PAGE_SHIFT);
177180 }
178181 }
@@ -196,8 +199,8 @@ static void page_table_check_pte_flags(pte_t pte) · 17 unmodified lines
196199 }
197200 }
198201
199−void __page_table_check_ptes_set(struct mm_struct *mm, pte_t *ptep, pte_t pte,
200− unsigned int nr)
202+void __page_table_check_ptes_set(struct mm_struct *mm, unsigned long addr,
203+ pte_t *ptep, pte_t pte, unsigned int nr)
201204 {
202205 unsigned int i;
203206
@@ -207,8 +210,8 @@ void __page_table_check_ptes_set(struct mm_struct *mm, pte_t *ptep, pte_t pte, · 3 unmodified lines
207210 page_table_check_pte_flags(pte);
208211
209212 for (i = 0; i < nr; i++)
210− __page_table_check_pte_clear(mm, ptep_get(ptep + i));
211− if (pte_user_accessible_page(pte))
213+ __page_table_check_pte_clear(mm, addr + PAGE_SIZE * i, ptep_get(ptep + i));
214+ if (pte_user_accessible_page(pte, addr))
212215 page_table_check_set(pte_pfn(pte), nr, pte_write(pte));
213216 }
214217 EXPORT_SYMBOL(__page_table_check_ptes_set);
@@ -225,8 +228,8 @@ static inline void page_table_check_pmd_flags(pmd_t pmd) · 10 unmodified lines
225228 }
226229 }
227230
228−void __page_table_check_pmds_set(struct mm_struct *mm, pmd_t *pmdp, pmd_t pmd,
229− unsigned int nr)
231+void __page_table_check_pmds_set(struct mm_struct *mm, unsigned long addr,
232+ pmd_t *pmdp, pmd_t pmd, unsigned int nr)
230233 {
231234 unsigned long stride = PMD_SIZE >> PAGE_SHIFT;
232235 unsigned int i;
@@ -237,14 +240,14 @@ void __page_table_check_pmds_set(struct mm_struct *mm, pmd_t *pmdp, pmd_t pmd, · 4 unmodified lines
237240 page_table_check_pmd_flags(pmd);
238241
239242 for (i = 0; i < nr; i++)
240− __page_table_check_pmd_clear(mm, *(pmdp + i));
241− if (pmd_user_accessible_page(pmd))
243+ __page_table_check_pmd_clear(mm, addr + PMD_SIZE * i, *(pmdp + i));
244+ if (pmd_user_accessible_page(pmd, addr))
242245 page_table_check_set(pmd_pfn(pmd), stride * nr, pmd_write(pmd));
243246 }
244247 EXPORT_SYMBOL(__page_table_check_pmds_set);
245248
246−void __page_table_check_puds_set(struct mm_struct *mm, pud_t *pudp, pud_t pud,
247− unsigned int nr)
249+void __page_table_check_puds_set(struct mm_struct *mm, unsigned long addr,
250+ pud_t *pudp, pud_t pud, unsigned int nr)
248251 {
249252 unsigned long stride = PUD_SIZE >> PAGE_SHIFT;
250253 unsigned int i;
@@ -253,8 +256,8 @@ void __page_table_check_puds_set(struct mm_struct *mm, pud_t *pudp, pud_t pud, · 2 unmodified lines
253256 return;
254257
255258 for (i = 0; i < nr; i++)
256− __page_table_check_pud_clear(mm, *(pudp + i));
257− if (pud_user_accessible_page(pud))
259+ __page_table_check_pud_clear(mm, addr + PUD_SIZE * i, *(pudp + i));
260+ if (pud_user_accessible_page(pud, addr))
258261 page_table_check_set(pud_pfn(pud), stride * nr, pud_write(pud));
259262 }
260263 EXPORT_SYMBOL(__page_table_check_puds_set);
@@ -273,7 +276,7 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm, · 12 unmodified lines
273276 if (WARN_ON(!ptep))
274277 return;
275278 for (i = 0; i < PTRS_PER_PTE; i++) {
276− __page_table_check_pte_clear(mm, ptep_get(ptep));
279+ __page_table_check_pte_clear(mm, addr, ptep_get(ptep));
277280 addr += PAGE_SIZE;
278281 ptep++;
279282 }
mm/pagewalk.c+2−1
@@ -313,7 +313,8 @@ static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr, · 312 unmodified lines
313313 unsigned long end)
314314 {
315315 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
316− return boundary < end ? boundary : end;
316+
317+ return min(boundary, end);
317318 }
318319
319320 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
mm/percpu.c+14−1
@@ -1279,12 +1279,16 @@ static int pcpu_free_area(struct pcpu_chunk *chunk, int off) · 1278 unmodified lines
12791279 int bit_off, bits, end, oslot, freed;
12801280
12811281 lockdep_assert_held(&pcpu_lock);
1282− pcpu_stats_area_dealloc(chunk);
12831282
12841283 oslot = pcpu_chunk_slot(chunk);
12851284
12861285 bit_off = off / PCPU_MIN_ALLOC_SIZE;
12871286
1287+ /* check invalid free */
1288+ if (!test_bit(bit_off, chunk->alloc_map) ||
1289+ !test_bit(bit_off, chunk->bound_map))
1290+ return 0;
1291+
12881292 /* find end index */
12891293 end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk),
12901294 bit_off + 1);
@@ -1303,6 +1307,8 @@ static int pcpu_free_area(struct pcpu_chunk *chunk, int off) · 12 unmodified lines
13031307
13041308 pcpu_chunk_relocate(chunk, oslot);
13051309
1310+ pcpu_stats_area_dealloc(chunk);
1311+
13061312 return freed;
13071313 }
13081314
@@ -2242,6 +2248,13 @@ void free_percpu(void __percpu *ptr) · 933 unmodified lines
22422248
22432249 spin_lock_irqsave(&pcpu_lock, flags);
22442250 size = pcpu_free_area(chunk, off);
2251+ if (size == 0) {
2252+ spin_unlock_irqrestore(&pcpu_lock, flags);
2253+
2254+ /* invalid percpu free */
2255+ WARN_ON_ONCE(1);
2256+ return;
2257+ }
22452258
22462259 pcpu_alloc_tag_free_hook(chunk, off, size);
22472260
mm/pt_reclaim.c+0−72
@@ -1,72 +0,0 @@
1−// SPDX-License-Identifier: GPL-2.0
2−#include <linux/hugetlb.h>
3−#include <linux/pgalloc.h>
4−
5−#include <asm-generic/tlb.h>
6−
7−#include "internal.h"
8−
9−bool reclaim_pt_is_enabled(unsigned long start, unsigned long end,
10− struct zap_details *details)
11−{
12− return details && details->reclaim_pt && (end - start >= PMD_SIZE);
13−}
14−
15−bool try_get_and_clear_pmd(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdval)
16−{
17− spinlock_t *pml = pmd_lockptr(mm, pmd);
18−
19− if (!spin_trylock(pml))
20− return false;
21−
22− *pmdval = pmdp_get_lockless(pmd);
23− pmd_clear(pmd);
24− spin_unlock(pml);
25−
26− return true;
27−}
28−
29−void free_pte(struct mm_struct *mm, unsigned long addr, struct mmu_gather *tlb,
30− pmd_t pmdval)
31−{
32− pte_free_tlb(tlb, pmd_pgtable(pmdval), addr);
33− mm_dec_nr_ptes(mm);
34−}
35−
36−void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
37− struct mmu_gather *tlb)
38−{
39− pmd_t pmdval;
40− spinlock_t *pml, *ptl = NULL;
41− pte_t *start_pte, *pte;
42− int i;
43−
44− pml = pmd_lock(mm, pmd);
45− start_pte = pte_offset_map_rw_nolock(mm, pmd, addr, &pmdval, &ptl);
46− if (!start_pte)
47− goto out_ptl;
48− if (ptl != pml)
49− spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
50−
51− /* Check if it is empty PTE page */
52− for (i = 0, pte = start_pte; i < PTRS_PER_PTE; i++, pte++) {
53− if (!pte_none(ptep_get(pte)))
54− goto out_ptl;
55− }
56− pte_unmap(start_pte);
57−
58− pmd_clear(pmd);
59−
60− if (ptl != pml)
61− spin_unlock(ptl);
62− spin_unlock(pml);
63−
64− free_pte(mm, addr, tlb, pmdval);
65−
66− return;
67−out_ptl:
68− if (start_pte)
69− pte_unmap_unlock(start_pte, ptl);
70− if (ptl != pml)
71− spin_unlock(pml);
72−}
mm/readahead.c+1−1
@@ -439,7 +439,7 @@ static unsigned long get_next_ra_size(struct file_ra_state *ra, · 438 unmodified lines
439439 * based on I/O request size and the max_readahead.
440440 *
441441 * The code ramps up the readahead size aggressively at first, but slow down as
442− * it approaches max_readhead.
442+ * it approaches max_readahead.
443443 */
444444
445445 static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index,
mm/rmap.c+205−119
@@ -1,8 +1,8 @@
1+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * mm/rmap.c - physical to virtual reverse mappings
34 *
45 * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
5− * Released under the General Public License (GPL).
66 *
77 * Simple, low overhead reverse mapping scheme.
88 * Please try to keep this thing as modular as possible.
@@ -82,6 +82,7 @@ · 73 unmodified lines
8282 #include <trace/events/migrate.h>
8383
8484 #include "internal.h"
85+#include "swap.h"
8586
8687 static struct kmem_cache *anon_vma_cachep;
8788 static struct kmem_cache *anon_vma_chain_cachep;
@@ -146,14 +147,13 @@ static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain) · 58 unmodified lines
146147 kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain);
147148 }
148149
149−static void anon_vma_chain_link(struct vm_area_struct *vma,
150− struct anon_vma_chain *avc,
151− struct anon_vma *anon_vma)
150+static void anon_vma_chain_assign(struct vm_area_struct *vma,
151+ struct anon_vma_chain *avc,
152+ struct anon_vma *anon_vma)
152153 {
153154 avc->vma = vma;
154155 avc->anon_vma = anon_vma;
155156 list_add(&avc->same_vma, &vma->anon_vma_chain);
156− anon_vma_interval_tree_insert(avc, &anon_vma->rb_root);
157157 }
158158
159159 /**
@@ -210,7 +210,8 @@ int __anon_vma_prepare(struct vm_area_struct *vma) · 50 unmodified lines
210210 spin_lock(&mm->page_table_lock);
211211 if (likely(!vma->anon_vma)) {
212212 vma->anon_vma = anon_vma;
213− anon_vma_chain_link(vma, avc, anon_vma);
213+ anon_vma_chain_assign(vma, avc, anon_vma);
214+ anon_vma_interval_tree_insert(avc, &anon_vma->rb_root);
214215 anon_vma->num_active_vmas++;
215216 allocated = NULL;
216217 avc = NULL;
@@ -231,97 +232,141 @@ int __anon_vma_prepare(struct vm_area_struct *vma) · 14 unmodified lines
231232 return -ENOMEM;
232233 }
233234
234−/*
235− * This is a useful helper function for locking the anon_vma root as
236− * we traverse the vma->anon_vma_chain, looping over anon_vma's that
237− * have the same vma.
238− *
239− * Such anon_vma's should have the same root, so you'd expect to see
240− * just a single mutex_lock for the whole traversal.
241− */
242−static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma)
243−{
244− struct anon_vma *new_root = anon_vma->root;
245− if (new_root != root) {
246− if (WARN_ON_ONCE(root))
247− up_write(&root->rwsem);
248− root = new_root;
249− down_write(&root->rwsem);
250− }
251− return root;
235+static void check_anon_vma_clone(struct vm_area_struct *dst,
236+ struct vm_area_struct *src,
237+ enum vma_operation operation)
238+{
239+ /* The write lock must be held. */
240+ mmap_assert_write_locked(src->vm_mm);
241+ /* If not a fork then must be on same mm. */
242+ VM_WARN_ON_ONCE(operation != VMA_OP_FORK && dst->vm_mm != src->vm_mm);
243+
244+ /* If we have anything to do src->anon_vma must be provided. */
245+ VM_WARN_ON_ONCE(!src->anon_vma && !list_empty(&src->anon_vma_chain));
246+ VM_WARN_ON_ONCE(!src->anon_vma && dst->anon_vma);
247+ /* We are establishing a new anon_vma_chain. */
248+ VM_WARN_ON_ONCE(!list_empty(&dst->anon_vma_chain));
249+ /*
250+ * On fork, dst->anon_vma is set NULL (temporarily). Otherwise, anon_vma
251+ * must be the same across dst and src.
252+ */
253+ VM_WARN_ON_ONCE(dst->anon_vma && dst->anon_vma != src->anon_vma);
254+ /*
255+ * Essentially equivalent to above - if not a no-op, we should expect
256+ * dst->anon_vma to be set for everything except a fork.
257+ */
258+ VM_WARN_ON_ONCE(operation != VMA_OP_FORK && src->anon_vma &&
259+ !dst->anon_vma);
260+ /* For the anon_vma to be compatible, it can only be singular. */
261+ VM_WARN_ON_ONCE(operation == VMA_OP_MERGE_UNFAULTED &&
262+ !list_is_singular(&src->anon_vma_chain));
263+#ifdef CONFIG_PER_VMA_LOCK
264+ /* Only merging an unfaulted VMA leaves the destination attached. */
265+ VM_WARN_ON_ONCE(operation != VMA_OP_MERGE_UNFAULTED &&
266+ vma_is_attached(dst));
267+#endif
252268 }
253269
254−static inline void unlock_anon_vma_root(struct anon_vma *root)
270+static void maybe_reuse_anon_vma(struct vm_area_struct *dst,
271+ struct anon_vma *anon_vma)
255272 {
256− if (root)
257− up_write(&root->rwsem);
273+ /* If already populated, nothing to do.*/
274+ if (dst->anon_vma)
275+ return;
276+
277+ /*
278+ * We reuse an anon_vma if any linking VMAs were unmapped and it has
279+ * only a single child at most.
280+ */
281+ if (anon_vma->num_active_vmas > 0)
282+ return;
283+ if (anon_vma->num_children > 1)
284+ return;
285+
286+ dst->anon_vma = anon_vma;
287+ anon_vma->num_active_vmas++;
258288 }
259289
260−/*
261− * Attach the anon_vmas from src to dst.
262− * Returns 0 on success, -ENOMEM on failure.
263− *
264− * anon_vma_clone() is called by vma_expand(), vma_merge(), __split_vma(),
265− * copy_vma() and anon_vma_fork(). The first four want an exact copy of src,
266− * while the last one, anon_vma_fork(), may try to reuse an existing anon_vma to
267− * prevent endless growth of anon_vma. Since dst->anon_vma is set to NULL before
268− * call, we can identify this case by checking (!dst->anon_vma &&
269− * src->anon_vma).
270− *
271− * If (!dst->anon_vma && src->anon_vma) is true, this function tries to find
272− * and reuse existing anon_vma which has no vmas and only one child anon_vma.
273− * This prevents degradation of anon_vma hierarchy to endless linear chain in
274− * case of constantly forking task. On the other hand, an anon_vma with more
275− * than one child isn't reused even if there was no alive vma, thus rmap
276− * walker has a good chance of avoiding scanning the whole hierarchy when it
277− * searches where page is mapped.
290+static void cleanup_partial_anon_vmas(struct vm_area_struct *vma);
291+
292+/**
293+ * anon_vma_clone - Establishes new anon_vma_chain objects in @dst linking to
294+ * all of the anon_vma objects contained within @src anon_vma_chain's.
295+ * @dst: The destination VMA with an empty anon_vma_chain.
296+ * @src: The source VMA we wish to duplicate.
297+ * @operation: The type of operation which resulted in the clone.
298+ *
299+ * This is the heart of the VMA side of the anon_vma implementation - we invoke
300+ * this function whenever we need to set up a new VMA's anon_vma state.
301+ *
302+ * This is invoked for:
303+ *
304+ * - VMA Merge, but only when @dst is unfaulted and @src is faulted - meaning we
305+ * clone @src into @dst.
306+ * - VMA split.
307+ * - VMA (m)remap.
308+ * - Fork of faulted VMA.
309+ *
310+ * In all cases other than fork this is simply a duplication. Fork additionally
311+ * adds a new active anon_vma.
312+ *
313+ * ONLY in the case of fork do we try to 'reuse' existing anon_vma's in an
314+ * anon_vma hierarchy, reusing anon_vma's which have no VMA associated with them
315+ * but do have a single child. This is to avoid waste of memory when repeatedly
316+ * forking.
317+ *
318+ * Returns: 0 on success, -ENOMEM on failure.
278319 */
279−int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
320+int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src,
321+ enum vma_operation operation)
280322 {
281323 struct anon_vma_chain *avc, *pavc;
282− struct anon_vma *root = NULL;
283−
284− list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) {
285− struct anon_vma *anon_vma;
286−
287− avc = anon_vma_chain_alloc(GFP_NOWAIT);
288− if (unlikely(!avc)) {
289− unlock_anon_vma_root(root);
290− root = NULL;
291− avc = anon_vma_chain_alloc(GFP_KERNEL);
292− if (!avc)
293− goto enomem_failure;
294− }
295− anon_vma = pavc->anon_vma;
296− root = lock_anon_vma_root(root, anon_vma);
297− anon_vma_chain_link(dst, avc, anon_vma);
324+ struct anon_vma *active_anon_vma = src->anon_vma;
298325
299− /*
300− * Reuse existing anon_vma if it has no vma and only one
301− * anon_vma child.
302− *
303− * Root anon_vma is never reused:
304− * it has self-parent reference and at least one child.
305− */
306− if (!dst->anon_vma && src->anon_vma &&
307− anon_vma->num_children < 2 &&
308− anon_vma->num_active_vmas == 0)
309− dst->anon_vma = anon_vma;
326+ check_anon_vma_clone(dst, src, operation);
327+
328+ if (!active_anon_vma)
329+ return 0;
330+
331+ /*
332+ * Allocate AVCs. We don't need an anon_vma lock for this as we
333+ * are not updating the anon_vma rbtree nor are we changing
334+ * anon_vma statistics.
335+ *
336+ * Either src, dst have the same mm for which we hold an exclusive mmap
337+ * write lock, or we are forking and we hold it on src->vm_mm and dst is
338+ * not yet accessible to other threads so there's no possibliity of the
339+ * unlinked AVC's being observed yet.
340+ */
341+ list_for_each_entry(pavc, &src->anon_vma_chain, same_vma) {
342+ avc = anon_vma_chain_alloc(GFP_KERNEL);
343+ if (!avc)
344+ goto enomem_failure;
345+
346+ anon_vma_chain_assign(dst, avc, pavc->anon_vma);
310347 }
311− if (dst->anon_vma)
348+
349+ /*
350+ * Now link the anon_vma's back to the newly inserted AVCs.
351+ * Note that all anon_vma's share the same root.
352+ */
353+ anon_vma_lock_write(src->anon_vma);
354+ list_for_each_entry_reverse(avc, &dst->anon_vma_chain, same_vma) {
355+ struct anon_vma *anon_vma = avc->anon_vma;
356+
357+ anon_vma_interval_tree_insert(avc, &anon_vma->rb_root);
358+ if (operation == VMA_OP_FORK)
359+ maybe_reuse_anon_vma(dst, anon_vma);
360+ }
361+
362+ if (operation != VMA_OP_FORK)
312363 dst->anon_vma->num_active_vmas++;
313− unlock_anon_vma_root(root);
364+
365+ anon_vma_unlock_write(active_anon_vma);
314366 return 0;
315367
316368 enomem_failure:
317− /*
318− * dst->anon_vma is dropped here otherwise its num_active_vmas can
319− * be incorrectly decremented in unlink_anon_vmas().
320− * We can safely do this because callers of anon_vma_clone() don't care
321− * about dst->anon_vma if anon_vma_clone() failed.
322− */
323− dst->anon_vma = NULL;
324− unlink_anon_vmas(dst);
369+ cleanup_partial_anon_vmas(dst);
325370 return -ENOMEM;
326371 }
327372
@@ -334,7 +379,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) · 6 unmodified lines
334379 {
335380 struct anon_vma_chain *avc;
336381 struct anon_vma *anon_vma;
337− int error;
382+ int rc;
338383
339384 /* Don't bother if the parent process has no anon_vma here. */
340385 if (!pvma->anon_vma)
@@ -343,27 +388,35 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) · 2 unmodified lines
343388 /* Drop inherited anon_vma, we'll reuse existing or allocate new. */
344389 vma->anon_vma = NULL;
345390
391+ anon_vma = anon_vma_alloc();
392+ if (!anon_vma)
393+ return -ENOMEM;
394+ avc = anon_vma_chain_alloc(GFP_KERNEL);
395+ if (!avc) {
396+ put_anon_vma(anon_vma);
397+ return -ENOMEM;
398+ }
399+
346400 /*
347401 * First, attach the new VMA to the parent VMA's anon_vmas,
348402 * so rmap can find non-COWed pages in child processes.
349403 */
350− error = anon_vma_clone(vma, pvma);
351− if (error)
352− return error;
353−
354− /* An existing anon_vma has been reused, all done then. */
355− if (vma->anon_vma)
356− return 0;
404+ rc = anon_vma_clone(vma, pvma, VMA_OP_FORK);
405+ /* An error arose or an existing anon_vma was reused, all done then. */
406+ if (rc || vma->anon_vma) {
407+ put_anon_vma(anon_vma);
408+ anon_vma_chain_free(avc);
409+ return rc;
410+ }
357411
358− /* Then add our own anon_vma. */
359− anon_vma = anon_vma_alloc();
360− if (!anon_vma)
361− goto out_error;
362− anon_vma->num_active_vmas++;
363− avc = anon_vma_chain_alloc(GFP_KERNEL);
364− if (!avc)
365− goto out_error_free_anon_vma;
412+ /*
413+ * OK no reuse, so add our own anon_vma.
414+ *
415+ * Since it is not linked anywhere we can safely manipulate anon_vma
416+ * fields without a lock.
417+ */
366418
419+ anon_vma->num_active_vmas = 1;
367420 /*
368421 * The root anon_vma's rwsem is the lock actually used when we
369422 * lock any of the anon_vmas in this anon_vma tree.
@@ -378,24 +431,59 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) · 8 unmodified lines
378431 get_anon_vma(anon_vma->root);
379432 /* Mark this anon_vma as the one where our new (COWed) pages go. */
380433 vma->anon_vma = anon_vma;
434+ anon_vma_chain_assign(vma, avc, anon_vma);
435+ /* Now let rmap see it. */
381436 anon_vma_lock_write(anon_vma);
382− anon_vma_chain_link(vma, avc, anon_vma);
437+ anon_vma_interval_tree_insert(avc, &anon_vma->rb_root);
383438 anon_vma->parent->num_children++;
384439 anon_vma_unlock_write(anon_vma);
385440
386441 return 0;
442+}
387443
388− out_error_free_anon_vma:
389− put_anon_vma(anon_vma);
390− out_error:
391− unlink_anon_vmas(vma);
392− return -ENOMEM;
444+/*
445+ * In the unfortunate case of anon_vma_clone() failing to allocate memory we
446+ * have to clean things up.
447+ *
448+ * Since we allocate anon_vma_chain's before we insert them into the interval
449+ * trees, we simply have to free up the AVC's and remove the entries from the
450+ * VMA's anon_vma_chain.
451+ */
452+static void cleanup_partial_anon_vmas(struct vm_area_struct *vma)
453+{
454+ struct anon_vma_chain *avc, *next;
455+
456+ list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
457+ list_del(&avc->same_vma);
458+ anon_vma_chain_free(avc);
459+ }
393460 }
394461
462+/**
463+ * unlink_anon_vmas() - remove all links between a VMA and anon_vma's, freeing
464+ * anon_vma_chain objects.
465+ * @vma: The VMA whose links to anon_vma objects is to be severed.
466+ *
467+ * As part of the process anon_vma_chain's are freed,
468+ * anon_vma->num_children,num_active_vmas is updated as required and, if the
469+ * relevant anon_vma references no further VMAs, its reference count is
470+ * decremented.
471+ */
395472 void unlink_anon_vmas(struct vm_area_struct *vma)
396473 {
397474 struct anon_vma_chain *avc, *next;
398− struct anon_vma *root = NULL;
475+ struct anon_vma *active_anon_vma = vma->anon_vma;
476+
477+ /* Always hold mmap lock, read-lock on unmap possibly. */
478+ mmap_assert_locked(vma->vm_mm);
479+
480+ /* Unfaulted is a no-op. */
481+ if (!active_anon_vma) {
482+ VM_WARN_ON_ONCE(!list_empty(&vma->anon_vma_chain));
483+ return;
484+ }
485+
486+ anon_vma_lock_write(active_anon_vma);
399487
400488 /*
401489 * Unlink each anon_vma chained to the VMA. This list is ordered
@@ -404,7 +492,6 @@ void unlink_anon_vmas(struct vm_area_struct *vma) · 2 unmodified lines
404492 list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
405493 struct anon_vma *anon_vma = avc->anon_vma;
406494
407− root = lock_anon_vma_root(root, anon_vma);
408495 anon_vma_interval_tree_remove(avc, &anon_vma->rb_root);
409496
410497 /*
@@ -419,16 +506,15 @@ void unlink_anon_vmas(struct vm_area_struct *vma) · 8 unmodified lines
419506 list_del(&avc->same_vma);
420507 anon_vma_chain_free(avc);
421508 }
422− if (vma->anon_vma) {
423− vma->anon_vma->num_active_vmas--;
424509
425− /*
426− * vma would still be needed after unlink, and anon_vma will be prepared
427− * when handle fault.
428− */
429− vma->anon_vma = NULL;
430− }
431− unlock_anon_vma_root(root);
510+ active_anon_vma->num_active_vmas--;
511+ /*
512+ * vma would still be needed after unlink, and anon_vma will be prepared
513+ * when handle fault.
514+ */
515+ vma->anon_vma = NULL;
516+ anon_vma_unlock_write(active_anon_vma);
517+
432518
433519 /*
434520 * Iterate the list once more, it now only contains empty and unlinked
@@ -2147,7 +2233,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, · 1712 unmodified lines
21472233 goto discard;
21482234 }
21492235
2150− if (swap_duplicate(entry) < 0) {
2236+ if (folio_dup_swap(folio, subpage) < 0) {
21512237 set_pte_at(mm, address, pvmw.pte, pteval);
21522238 goto walk_abort;
21532239 }
@@ -2158,7 +2244,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, · 4 unmodified lines
21582244 * so we'll not check/care.
21592245 */
21602246 if (arch_unmap_one(mm, vma, address, pteval) < 0) {
2161− swap_free(entry);
2247+ folio_put_swap(folio, subpage);
21622248 set_pte_at(mm, address, pvmw.pte, pteval);
21632249 goto walk_abort;
21642250 }
@@ -2166,7 +2252,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, · 1 unmodified line
21662252 /* See folio_try_share_anon_rmap(): clear PTE first. */
21672253 if (anon_exclusive &&
21682254 folio_try_share_anon_rmap_pte(folio, subpage)) {
2169− swap_free(entry);
2255+ folio_put_swap(folio, subpage);
21702256 set_pte_at(mm, address, pvmw.pte, pteval);
21712257 goto walk_abort;
21722258 }
mm/shmem.c+36−54
@@ -1,3 +1,4 @@
1+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * Resizable virtual memory filesystem for Linux.
34 *
@@ -17,8 +18,6 @@ · 13 unmodified lines
1718 *
1819 * tiny-shmem:
1920 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20− *
21− * This file is released under the GPL.
2221 */
2322
2423 #include <linux/fs.h>
@@ -983,7 +982,7 @@ static long shmem_free_swap(struct address_space *mapping, · 958 unmodified lines
983982 xas_unlock_irq(&xas);
984983
985984 if (nr_pages)
986− free_swap_and_cache_nr(radix_to_swp_entry(radswap), nr_pages);
985+ swap_put_entries_direct(radix_to_swp_entry(radswap), nr_pages);
987986
988987 return nr_pages;
989988 }
@@ -1622,11 +1621,23 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug, · 632 unmodified lines
16221621 }
16231622
16241623 if (split) {
1624+ int order;
1625+
16251626 try_split:
1627+ order = folio_order(folio);
16261628 /* Ensure the subpages are still dirty */
16271629 folio_test_set_dirty(folio);
16281630 if (split_folio_to_list(folio, folio_list))
16291631 goto redirty;
1632+
1633+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1634+ if (order >= HPAGE_PMD_ORDER) {
1635+ count_memcg_folio_events(folio, THP_SWPOUT_FALLBACK, 1);
1636+ count_vm_event(THP_SWPOUT_FALLBACK);
1637+ }
1638+#endif
1639+ count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK);
1640+
16301641 folio_clear_dirty(folio);
16311642 }
16321643
@@ -1684,7 +1695,7 @@ try_split: · 51 unmodified lines
16841695 spin_unlock(&shmem_swaplist_lock);
16851696 }
16861697
1687− swap_shmem_alloc(folio->swap, nr_pages);
1698+ folio_dup_swap(folio, NULL);
16881699 shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap));
16891700
16901701 BUG_ON(folio_mapped(folio));
@@ -1705,7 +1716,7 @@ try_split: · 14 unmodified lines
17051716 /* Swap entry might be erased by racing shmem_free_swap() */
17061717 if (!error) {
17071718 shmem_recalc_inode(inode, 0, -nr_pages);
1708− swap_free_nr(folio->swap, nr_pages);
1719+ folio_put_swap(folio, NULL);
17091720 }
17101721
17111722 /*
@@ -2031,10 +2042,9 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode, · 319 unmodified lines
20312042 swp_entry_t entry, int order, gfp_t gfp)
20322043 {
20332044 struct shmem_inode_info *info = SHMEM_I(inode);
2045+ struct folio *new, *swapcache;
20342046 int nr_pages = 1 << order;
2035− struct folio *new;
20362047 gfp_t alloc_gfp;
2037− void *shadow;
20382048
20392049 /*
20402050 * We have arrived here because our zones are constrained, so don't
@@ -2074,34 +2084,19 @@ retry: · 33 unmodified lines
20742084 goto fallback;
20752085 }
20762086
2077− /*
2078− * Prevent parallel swapin from proceeding with the swap cache flag.
2079− *
2080− * Of course there is another possible concurrent scenario as well,
2081− * that is to say, the swap cache flag of a large folio has already
2082− * been set by swapcache_prepare(), while another thread may have
2083− * already split the large swap entry stored in the shmem mapping.
2084− * In this case, shmem_add_to_page_cache() will help identify the
2085− * concurrent swapin and return -EEXIST.
2086− */
2087− if (swapcache_prepare(entry, nr_pages)) {
2087+ swapcache = swapin_folio(entry, new);
2088+ if (swapcache != new) {
20882089 folio_put(new);
2089− new = ERR_PTR(-EEXIST);
2090− /* Try smaller folio to avoid cache conflict */
2091− goto fallback;
2090+ if (!swapcache) {
2091+ /*
2092+ * The new folio is charged already, swapin can
2093+ * only fail due to another raced swapin.
2094+ */
2095+ new = ERR_PTR(-EEXIST);
2096+ goto fallback;
2097+ }
20922098 }
2093−
2094− __folio_set_locked(new);
2095− __folio_set_swapbacked(new);
2096− new->swap = entry;
2097−
2098− memcg1_swapin(entry, nr_pages);
2099− shadow = swap_cache_get_shadow(entry);
2100− if (shadow)
2101− workingset_refault(new, shadow);
2102− folio_add_lru(new);
2103− swap_read_folio(new, NULL);
2104− return new;
2099+ return swapcache;
21052100 fallback:
21062101 /* Order 0 swapin failed, nothing to fallback to, abort */
21072102 if (!order)
@@ -2191,8 +2186,7 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, · 83 unmodified lines
21912186 }
21922187
21932188 static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
2194− struct folio *folio, swp_entry_t swap,
2195− bool skip_swapcache)
2189+ struct folio *folio, swp_entry_t swap)
21962190 {
21972191 struct address_space *mapping = inode->i_mapping;
21982192 swp_entry_t swapin_error;
@@ -2208,15 +2202,14 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, · 9 unmodified lines
22082202
22092203 nr_pages = folio_nr_pages(folio);
22102204 folio_wait_writeback(folio);
2211− if (!skip_swapcache)
2212− swap_cache_del_folio(folio);
2205+ folio_put_swap(folio, NULL);
2206+ swap_cache_del_folio(folio);
22132207 /*
22142208 * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks
22152209 * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks)
22162210 * in shmem_evict_inode().
22172211 */
22182212 shmem_recalc_inode(inode, -nr_pages, -nr_pages);
2219− swap_free_nr(swap, nr_pages);
22202213 }
22212214
22222215 static int shmem_split_large_entry(struct inode *inode, pgoff_t index,
@@ -2309,7 +2302,6 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, · 86 unmodified lines
23092302 softleaf_t index_entry;
23102303 struct swap_info_struct *si;
23112304 struct folio *folio = NULL;
2312− bool skip_swapcache = false;
23132305 int error, nr_pages, order;
23142306 pgoff_t offset;
23152307
@@ -2352,7 +2344,6 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, · 36 unmodified lines
23522344 folio = NULL;
23532345 goto failed;
23542346 }
2355− skip_swapcache = true;
23562347 } else {
23572348 /* Cached swapin only supports order 0 folio */
23582349 folio = shmem_swapin_cluster(swap, gfp, info, index);
@@ -2408,9 +2399,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, · 49 unmodified lines
24082399 * and swap cache folios are never partially freed.
24092400 */
24102401 folio_lock(folio);
2411− if ((!skip_swapcache && !folio_test_swapcache(folio)) ||
2412− shmem_confirm_swap(mapping, index, swap) < 0 ||
2413− folio->swap.val != swap.val) {
2402+ if (!folio_matches_swap_entry(folio, swap) ||
2403+ shmem_confirm_swap(mapping, index, swap) < 0) {
24142404 error = -EEXIST;
24152405 goto unlock;
24162406 }
@@ -2442,14 +2432,9 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, · 25 unmodified lines
24422432 if (sgp == SGP_WRITE)
24432433 folio_mark_accessed(folio);
24442434
2445− if (skip_swapcache) {
2446− folio->swap.val = 0;
2447− swapcache_clear(si, swap, nr_pages);
2448− } else {
2449− swap_cache_del_folio(folio);
2450− }
2435+ folio_put_swap(folio, NULL);
2436+ swap_cache_del_folio(folio);
24512437 folio_mark_dirty(folio);
2452− swap_free_nr(swap, nr_pages);
24532438 put_swap_device(si);
24542439
24552440 *foliop = folio;
@@ -2458,14 +2443,11 @@ failed: · 2 unmodified lines
24582443 if (shmem_confirm_swap(mapping, index, swap) < 0)
24592444 error = -EEXIST;
24602445 if (error == -EIO)
2461− shmem_set_folio_swapin_error(inode, index, folio, swap,
2462− skip_swapcache);
2446+ shmem_set_folio_swapin_error(inode, index, folio, swap);
24632447 unlock:
24642448 if (folio)
24652449 folio_unlock(folio);
24662450 failed_nolock:
2467− if (skip_swapcache)
2468− swapcache_clear(si, folio->swap, folio_nr_pages(folio));
24692451 if (folio)
24702452 folio_put(folio);
24712453 put_swap_device(si);
mm/show_mem.c+1−2
@@ -278,8 +278,7 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z · 277 unmodified lines
278278 #endif
279279 K(node_page_state(pgdat, NR_PAGETABLE)),
280280 K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
281− str_yes_no(atomic_read(&pgdat->kswapd_failures) >=
282− MAX_RECLAIM_RETRIES),
281+ str_yes_no(kswapd_test_hopeless(pgdat)),
283282 K(node_page_state(pgdat, NR_BALLOON_PAGES)));
284283 }
285284
mm/shrinker_debug.c+7−6
@@ -70,7 +70,7 @@ static int shrinker_debugfs_count_show(struct seq_file *m, void *v) · 69 unmodified lines
7070 memcg_aware ? memcg : NULL,
7171 count_per_node);
7272 if (total) {
73− seq_printf(m, "%lu", mem_cgroup_ino(memcg));
73+ seq_printf(m, "%llu", mem_cgroup_id(memcg));
7474 for_each_node(nid)
7575 seq_printf(m, " %lu", count_per_node[nid]);
7676 seq_putc(m, '\n');
@@ -106,7 +106,8 @@ static ssize_t shrinker_debugfs_scan_write(struct file *file, · 29 unmodified lines
106106 size_t size, loff_t *pos)
107107 {
108108 struct shrinker *shrinker = file->private_data;
109− unsigned long nr_to_scan = 0, ino, read_len;
109+ unsigned long nr_to_scan = 0, read_len;
110+ u64 id;
110111 struct shrink_control sc = {
111112 .gfp_mask = GFP_KERNEL,
112113 };
@@ -119,7 +120,7 @@ static ssize_t shrinker_debugfs_scan_write(struct file *file, · 6 unmodified lines
119120 return -EFAULT;
120121 kbuf[read_len] = '\0';
121122
122− if (sscanf(kbuf, "%lu %d %lu", &ino, &nid, &nr_to_scan) != 3)
123+ if (sscanf(kbuf, "%llu %d %lu", &id, &nid, &nr_to_scan) != 3)
123124 return -EINVAL;
124125
125126 if (nid < 0 || nid >= nr_node_ids)
@@ -129,15 +130,15 @@ static ssize_t shrinker_debugfs_scan_write(struct file *file, · 3 unmodified lines
129130 return size;
130131
131132 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
132− memcg = mem_cgroup_get_from_ino(ino);
133− if (!memcg || IS_ERR(memcg))
133+ memcg = mem_cgroup_get_from_id(id);
134+ if (!memcg)
134135 return -ENOENT;
135136
136137 if (!mem_cgroup_online(memcg)) {
137138 mem_cgroup_put(memcg);
138139 return -ENOENT;
139140 }
140− } else if (ino != 0) {
141+ } else if (id != 0) {
141142 return -EINVAL;
142143 }
143144
mm/slub.c+2−1
@@ -8334,7 +8334,8 @@ void __init kmem_cache_init(void) · 8333 unmodified lines
83348334
83358335 void __init kmem_cache_init_late(void)
83368336 {
8337− flushwq = alloc_workqueue("slub_flushwq", WQ_MEM_RECLAIM, 0);
8337+ flushwq = alloc_workqueue("slub_flushwq", WQ_MEM_RECLAIM | WQ_PERCPU,
8338+ 0);
83388339 WARN_ON(!flushwq);
83398340 }
83408341
mm/swap.c+1−1
@@ -513,7 +513,7 @@ void folio_add_lru(struct folio *folio) · 512 unmodified lines
513513 EXPORT_SYMBOL(folio_add_lru);
514514
515515 /**
516− * folio_add_lru_vma() - Add a folio to the appropate LRU list for this VMA.
516+ * folio_add_lru_vma() - Add a folio to the appropriate LRU list for this VMA.
517517 * @folio: The folio to be added to the LRU.
518518 * @vma: VMA in which the folio is mapped.
519519 *
mm/swap.h+59−13
@@ -183,6 +183,33 @@ static inline void swap_cluster_unlock_irq(struct swap_cluster_info *ci) · 182 unmodified lines
183183 spin_unlock_irq(&ci->lock);
184184 }
185185
186+/*
187+ * Below are the core routines for doing swap for a folio.
188+ * All helpers requires the folio to be locked, and a locked folio
189+ * in the swap cache pins the swap entries / slots allocated to the
190+ * folio, swap relies heavily on the swap cache and folio lock for
191+ * synchronization.
192+ *
193+ * folio_alloc_swap(): the entry point for a folio to be swapped
194+ * out. It allocates swap slots and pins the slots with swap cache.
195+ * The slots start with a swap count of zero.
196+ *
197+ * folio_dup_swap(): increases the swap count of a folio, usually
198+ * during it gets unmapped and a swap entry is installed to replace
199+ * it (e.g., swap entry in page table). A swap slot with swap
200+ * count == 0 should only be increasd by this helper.
201+ *
202+ * folio_put_swap(): does the opposite thing of folio_dup_swap().
203+ */
204+int folio_alloc_swap(struct folio *folio);
205+int folio_dup_swap(struct folio *folio, struct page *subpage);
206+void folio_put_swap(struct folio *folio, struct page *subpage);
207+
208+/* For internal use */
209+extern void swap_entries_free(struct swap_info_struct *si,
210+ struct swap_cluster_info *ci,
211+ unsigned long offset, unsigned int nr_pages);
212+
186213 /* linux/mm/page_io.c */
187214 int sio_pool_init(void);
188215 struct swap_iocb;
@@ -236,7 +263,7 @@ static inline bool folio_matches_swap_entry(const struct folio *folio, · 47 unmodified lines
236263
237264 /*
238265 * All swap cache helpers below require the caller to ensure the swap entries
239− * used are valid and stablize the device by any of the following ways:
266+ * used are valid and stabilize the device by any of the following ways:
240267 * - Hold a reference by get_swap_device(): this ensures a single entry is
241268 * valid and increases the swap device's refcount.
242269 * - Locking a folio in the swap cache: this ensures the folio's swap entries
@@ -245,11 +272,16 @@ static inline bool folio_matches_swap_entry(const struct folio *folio, · 2 unmodified lines
245272 * swap entries in the page table, similar to locking swap cache folio.
246273 * - See the comment of get_swap_device() for more complex usage.
247274 */
275+bool swap_cache_has_folio(swp_entry_t entry);
248276 struct folio *swap_cache_get_folio(swp_entry_t entry);
249277 void *swap_cache_get_shadow(swp_entry_t entry);
250−void swap_cache_add_folio(struct folio *folio, swp_entry_t entry, void **shadow);
251278 void swap_cache_del_folio(struct folio *folio);
279+struct folio *swap_cache_alloc_folio(swp_entry_t entry, gfp_t gfp_flags,
280+ struct mempolicy *mpol, pgoff_t ilx,
281+ bool *alloced);
252282 /* Below helpers require the caller to lock and pass in the swap cluster. */
283+void __swap_cache_add_folio(struct swap_cluster_info *ci,
284+ struct folio *folio, swp_entry_t entry);
253285 void __swap_cache_del_folio(struct swap_cluster_info *ci,
254286 struct folio *folio, swp_entry_t entry, void *shadow);
255287 void __swap_cache_replace_folio(struct swap_cluster_info *ci,
@@ -261,13 +293,11 @@ void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr); · 5 unmodified lines
261293 struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
262294 struct vm_area_struct *vma, unsigned long addr,
263295 struct swap_iocb **plug);
264−struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_flags,
265− struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated,
266− bool skip_if_exists);
267296 struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t flag,
268297 struct mempolicy *mpol, pgoff_t ilx);
269298 struct folio *swapin_readahead(swp_entry_t entry, gfp_t flag,
270299 struct vm_fault *vmf);
300+struct folio *swapin_folio(swp_entry_t entry, struct folio *folio);
271301 void swap_update_readahead(struct folio *folio, struct vm_area_struct *vma,
272302 unsigned long addr);
273303
@@ -303,8 +333,6 @@ static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr, · 29 unmodified lines
303333
304334 static inline int non_swapcache_batch(swp_entry_t entry, int max_nr)
305335 {
306− struct swap_info_struct *si = __swap_entry_to_info(entry);
307− pgoff_t offset = swp_offset(entry);
308336 int i;
309337
310338 /*
@@ -313,8 +341,9 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) · 2 unmodified lines
313341 * be in conflict with the folio in swap cache.
314342 */
315343 for (i = 0; i < max_nr; i++) {
316− if ((si->swap_map[offset + i] & SWAP_HAS_CACHE))
344+ if (swap_cache_has_folio(entry))
317345 return i;
346+ entry.val++;
318347 }
319348
320349 return i;
@@ -353,9 +382,24 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry) · 32 unmodified lines
353382 return NULL;
354383 }
355384
385+static inline int folio_alloc_swap(struct folio *folio)
386+{
387+ return -EINVAL;
388+}
389+
390+static inline int folio_dup_swap(struct folio *folio, struct page *page)
391+{
392+ return -EINVAL;
393+}
394+
395+static inline void folio_put_swap(struct folio *folio, struct page *page)
396+{
397+}
398+
356399 static inline void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
357400 {
358401 }
402+
359403 static inline void swap_write_unplug(struct swap_iocb *sio)
360404 {
361405 }
@@ -386,6 +430,11 @@ static inline struct folio *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask, · 24 unmodified lines
386430 return NULL;
387431 }
388432
433+static inline struct folio *swapin_folio(swp_entry_t entry, struct folio *folio)
434+{
435+ return NULL;
436+}
437+
389438 static inline void swap_update_readahead(struct folio *folio,
390439 struct vm_area_struct *vma, unsigned long addr)
391440 {
@@ -397,8 +446,9 @@ static inline int swap_writeout(struct folio *folio, · 5 unmodified lines
397446 return 0;
398447 }
399448
400−static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr)
449+static inline bool swap_cache_has_folio(swp_entry_t entry)
401450 {
451+ return false;
402452 }
403453
404454 static inline struct folio *swap_cache_get_folio(swp_entry_t entry)
@@ -411,10 +461,6 @@ static inline void *swap_cache_get_shadow(swp_entry_t entry) · 6 unmodified lines
411461 return NULL;
412462 }
413463
414−static inline void swap_cache_add_folio(struct folio *folio, swp_entry_t entry, void **shadow)
415−{
416−}
417−
418464 static inline void swap_cache_del_folio(struct folio *folio)
419465 {
420466 }
mm/swap_state.c+223−119
@@ -81,7 +81,7 @@ void show_swap_cache_info(void) · 80 unmodified lines
8181 * Context: Caller must ensure @entry is valid and protect the swap device
8282 * with reference count or locks.
8383 * Return: Returns the found folio on success, NULL otherwise. The caller
84− * must lock nd check if the folio still matches the swap entry before
84+ * must lock and check if the folio still matches the swap entry before
8585 * use (e.g., folio_matches_swap_entry).
8686 */
8787 struct folio *swap_cache_get_folio(swp_entry_t entry)
@@ -102,6 +102,22 @@ struct folio *swap_cache_get_folio(swp_entry_t entry) · 14 unmodified lines
102102 return NULL;
103103 }
104104
105+/**
106+ * swap_cache_has_folio - Check if a swap slot has cache.
107+ * @entry: swap entry indicating the slot.
108+ *
109+ * Context: Caller must ensure @entry is valid and protect the swap
110+ * device with reference count or locks.
111+ */
112+bool swap_cache_has_folio(swp_entry_t entry)
113+{
114+ unsigned long swp_tb;
115+
116+ swp_tb = swap_table_get(__swap_entry_to_cluster(entry),
117+ swp_cluster_offset(entry));
118+ return swp_tb_is_folio(swp_tb);
119+}
120+
105121 /**
106122 * swap_cache_get_shadow - Looks up a shadow in the swap cache.
107123 * @entry: swap entry used for the lookup.
@@ -121,6 +137,34 @@ void *swap_cache_get_shadow(swp_entry_t entry) · 13 unmodified lines
121137 return NULL;
122138 }
123139
140+void __swap_cache_add_folio(struct swap_cluster_info *ci,
141+ struct folio *folio, swp_entry_t entry)
142+{
143+ unsigned long new_tb;
144+ unsigned int ci_start, ci_off, ci_end;
145+ unsigned long nr_pages = folio_nr_pages(folio);
146+
147+ VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
148+ VM_WARN_ON_ONCE_FOLIO(folio_test_swapcache(folio), folio);
149+ VM_WARN_ON_ONCE_FOLIO(!folio_test_swapbacked(folio), folio);
150+
151+ new_tb = folio_to_swp_tb(folio);
152+ ci_start = swp_cluster_offset(entry);
153+ ci_off = ci_start;
154+ ci_end = ci_start + nr_pages;
155+ do {
156+ VM_WARN_ON_ONCE(swp_tb_is_folio(__swap_table_get(ci, ci_off)));
157+ __swap_table_set(ci, ci_off, new_tb);
158+ } while (++ci_off < ci_end);
159+
160+ folio_ref_add(folio, nr_pages);
161+ folio_set_swapcache(folio);
162+ folio->swap = entry;
163+
164+ node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages);
165+ lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages);
166+}
167+
124168 /**
125169 * swap_cache_add_folio - Add a folio into the swap cache.
126170 * @folio: The folio to be added.
@@ -130,43 +174,51 @@ void *swap_cache_get_shadow(swp_entry_t entry) · 3 unmodified lines
130174 *
131175 * Context: Caller must ensure @entry is valid and protect the swap device
132176 * with reference count or locks.
133− * The caller also needs to update the corresponding swap_map slots with
134− * SWAP_HAS_CACHE bit to avoid race or conflict.
135177 */
136−void swap_cache_add_folio(struct folio *folio, swp_entry_t entry, void **shadowp)
178+static int swap_cache_add_folio(struct folio *folio, swp_entry_t entry,
179+ void **shadowp)
137180 {
181+ int err;
138182 void *shadow = NULL;
139− unsigned long old_tb, new_tb;
183+ unsigned long old_tb;
184+ struct swap_info_struct *si;
140185 struct swap_cluster_info *ci;
141− unsigned int ci_start, ci_off, ci_end;
186+ unsigned int ci_start, ci_off, ci_end, offset;
142187 unsigned long nr_pages = folio_nr_pages(folio);
143188
144− VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
145− VM_WARN_ON_ONCE_FOLIO(folio_test_swapcache(folio), folio);
146− VM_WARN_ON_ONCE_FOLIO(!folio_test_swapbacked(folio), folio);
147−
148− new_tb = folio_to_swp_tb(folio);
189+ si = __swap_entry_to_info(entry);
149190 ci_start = swp_cluster_offset(entry);
150191 ci_end = ci_start + nr_pages;
151192 ci_off = ci_start;
152− ci = swap_cluster_lock(__swap_entry_to_info(entry), swp_offset(entry));
193+ offset = swp_offset(entry);
194+ ci = swap_cluster_lock(si, swp_offset(entry));
195+ if (unlikely(!ci->table)) {
196+ err = -ENOENT;
197+ goto failed;
198+ }
153199 do {
154− old_tb = __swap_table_xchg(ci, ci_off, new_tb);
155− WARN_ON_ONCE(swp_tb_is_folio(old_tb));
200+ old_tb = __swap_table_get(ci, ci_off);
201+ if (unlikely(swp_tb_is_folio(old_tb))) {
202+ err = -EEXIST;
203+ goto failed;
204+ }
205+ if (unlikely(!__swap_count(swp_entry(swp_type(entry), offset)))) {
206+ err = -ENOENT;
207+ goto failed;
208+ }
156209 if (swp_tb_is_shadow(old_tb))
157210 shadow = swp_tb_to_shadow(old_tb);
211+ offset++;
158212 } while (++ci_off < ci_end);
159−
160− folio_ref_add(folio, nr_pages);
161− folio_set_swapcache(folio);
162− folio->swap = entry;
213+ __swap_cache_add_folio(ci, folio, entry);
163214 swap_cluster_unlock(ci);
164−
165− node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages);
166− lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages);
167−
168215 if (shadowp)
169216 *shadowp = shadow;
217+ return 0;
218+
219+failed:
220+ swap_cluster_unlock(ci);
221+ return err;
170222 }
171223
172224 /**
@@ -185,8 +237,10 @@ void swap_cache_add_folio(struct folio *folio, swp_entry_t entry, void **shadowp · 12 unmodified lines
185237 void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio,
186238 swp_entry_t entry, void *shadow)
187239 {
240+ struct swap_info_struct *si;
188241 unsigned long old_tb, new_tb;
189242 unsigned int ci_start, ci_off, ci_end;
243+ bool folio_swapped = false, need_free = false;
190244 unsigned long nr_pages = folio_nr_pages(folio);
191245
192246 VM_WARN_ON_ONCE(__swap_entry_to_cluster(entry) != ci);
@@ -194,6 +248,7 @@ void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio, · 1 unmodified line
194248 VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
195249 VM_WARN_ON_ONCE_FOLIO(folio_test_writeback(folio), folio);
196250
251+ si = __swap_entry_to_info(entry);
197252 new_tb = shadow_swp_to_tb(shadow);
198253 ci_start = swp_cluster_offset(entry);
199254 ci_end = ci_start + nr_pages;
@@ -203,12 +258,27 @@ void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio, · 3 unmodified lines
203258 old_tb = __swap_table_xchg(ci, ci_off, new_tb);
204259 WARN_ON_ONCE(!swp_tb_is_folio(old_tb) ||
205260 swp_tb_to_folio(old_tb) != folio);
261+ if (__swap_count(swp_entry(si->type,
262+ swp_offset(entry) + ci_off - ci_start)))
263+ folio_swapped = true;
264+ else
265+ need_free = true;
206266 } while (++ci_off < ci_end);
207267
208268 folio->swap.val = 0;
209269 folio_clear_swapcache(folio);
210270 node_stat_mod_folio(folio, NR_FILE_PAGES, -nr_pages);
211271 lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr_pages);
272+
273+ if (!folio_swapped) {
274+ swap_entries_free(si, ci, swp_offset(entry), nr_pages);
275+ } else if (need_free) {
276+ do {
277+ if (!__swap_count(entry))
278+ swap_entries_free(si, ci, swp_offset(entry), 1);
279+ entry.val++;
280+ } while (--nr_pages);
281+ }
212282 }
213283
214284 /**
@@ -230,7 +300,6 @@ void swap_cache_del_folio(struct folio *folio) · 15 unmodified lines
230300 __swap_cache_del_folio(ci, folio, entry, NULL);
231301 swap_cluster_unlock(ci);
232302
233− put_swap_folio(folio, entry);
234303 folio_ref_sub(folio, folio_nr_pages(folio));
235304 }
236305
@@ -283,7 +352,7 @@ void __swap_cache_replace_folio(struct swap_cluster_info *ci, · 46 unmodified lines
283352 }
284353
285354 /**
286− * swap_cache_clear_shadow - Clears a set of shadows in the swap cache.
355+ * __swap_cache_clear_shadow - Clears a set of shadows in the swap cache.
287356 * @entry: The starting index entry.
288357 * @nr_ents: How many slots need to be cleared.
289358 *
@@ -401,108 +470,143 @@ void swap_update_readahead(struct folio *folio, struct vm_area_struct *vma, · 111 unmodified lines
401470 }
402471 }
403472
404−struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
405− struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated,
406− bool skip_if_exists)
473+/**
474+ * __swap_cache_prepare_and_add - Prepare the folio and add it to swap cache.
475+ * @entry: swap entry to be bound to the folio.
476+ * @folio: folio to be added.
477+ * @gfp: memory allocation flags for charge, can be 0 if @charged if true.
478+ * @charged: if the folio is already charged.
479+ *
480+ * Update the swap_map and add folio as swap cache, typically before swapin.
481+ * All swap slots covered by the folio must have a non-zero swap count.
482+ *
483+ * Context: Caller must protect the swap device with reference count or locks.
484+ * Return: Returns the folio being added on success. Returns the existing folio
485+ * if @entry is already cached. Returns NULL if raced with swapin or swapoff.
486+ */
487+static struct folio *__swap_cache_prepare_and_add(swp_entry_t entry,
488+ struct folio *folio,
489+ gfp_t gfp, bool charged)
407490 {
408− struct swap_info_struct *si = __swap_entry_to_info(entry);
409− struct folio *folio;
410− struct folio *new_folio = NULL;
411− struct folio *result = NULL;
412− void *shadow = NULL;
491+ struct folio *swapcache = NULL;
492+ void *shadow;
493+ int ret;
413494
414− *new_page_allocated = false;
495+ __folio_set_locked(folio);
496+ __folio_set_swapbacked(folio);
415497 for (;;) {
416− int err;
498+ ret = swap_cache_add_folio(folio, entry, &shadow);
499+ if (!ret)
500+ break;
417501
418502 /*
419− * Check the swap cache first, if a cached folio is found,
420− * return it unlocked. The caller will lock and check it.
503+ * Large order allocation needs special handling on
504+ * race: if a smaller folio exists in cache, swapin needs
505+ * to fallback to order 0, and doing a swap cache lookup
506+ * might return a folio that is irrelevant to the faulting
507+ * entry because @entry is aligned down. Just return NULL.
421508 */
422− folio = swap_cache_get_folio(entry);
423− if (folio)
424− goto got_folio;
509+ if (ret != -EEXIST || folio_test_large(folio))
510+ goto failed;
425511
426− /*
427− * Just skip read ahead for unused swap slot.
428− */
429− if (!swap_entry_swapped(si, entry))
430− goto put_and_return;
512+ swapcache = swap_cache_get_folio(entry);
513+ if (swapcache)
514+ goto failed;
515+ }
431516
432− /*
433− * Get a new folio to read into from swap. Allocate it now if
434− * new_folio not exist, before marking swap_map SWAP_HAS_CACHE,
435− * when -EEXIST will cause any racers to loop around until we
436− * add it to cache.
437− */
438− if (!new_folio) {
439− new_folio = folio_alloc_mpol(gfp_mask, 0, mpol, ilx, numa_node_id());
440− if (!new_folio)
441− goto put_and_return;
442− }
517+ if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) {
518+ swap_cache_del_folio(folio);
519+ goto failed;
520+ }
443521
444− /*
445− * Swap entry may have been freed since our caller observed it.
446− */
447− err = swapcache_prepare(entry, 1);
448− if (!err)
449− break;
450− else if (err != -EEXIST)
451− goto put_and_return;
522+ memcg1_swapin(entry, folio_nr_pages(folio));
523+ if (shadow)
524+ workingset_refault(folio, shadow);
452525
453− /*
454− * Protect against a recursive call to __read_swap_cache_async()
455− * on the same entry waiting forever here because SWAP_HAS_CACHE
456− * is set but the folio is not the swap cache yet. This can
457− * happen today if mem_cgroup_swapin_charge_folio() below
458− * triggers reclaim through zswap, which may call
459− * __read_swap_cache_async() in the writeback path.
460− */
461− if (skip_if_exists)
462− goto put_and_return;
526+ /* Caller will initiate read into locked folio */
527+ folio_add_lru(folio);
528+ return folio;
463529
464− /*
465− * We might race against __swap_cache_del_folio(), and
466− * stumble across a swap_map entry whose SWAP_HAS_CACHE
467− * has not yet been cleared. Or race against another
468− * __read_swap_cache_async(), which has set SWAP_HAS_CACHE
469− * in swap_map, but not yet added its folio to swap cache.
470− */
471− schedule_timeout_uninterruptible(1);
472− }
530+failed:
531+ folio_unlock(folio);
532+ return swapcache;
533+}
473534
474− /*
475− * The swap entry is ours to swap in. Prepare the new folio.
476− */
477− __folio_set_locked(new_folio);
478− __folio_set_swapbacked(new_folio);
535+/**
536+ * swap_cache_alloc_folio - Allocate folio for swapped out slot in swap cache.
537+ * @entry: the swapped out swap entry to be binded to the folio.
538+ * @gfp_mask: memory allocation flags
539+ * @mpol: NUMA memory allocation policy to be applied
540+ * @ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE
541+ * @new_page_allocated: sets true if allocation happened, false otherwise
542+ *
543+ * Allocate a folio in the swap cache for one swap slot, typically before
544+ * doing IO (e.g. swap in or zswap writeback). The swap slot indicated by
545+ * @entry must have a non-zero swap count (swapped out).
546+ * Currently only supports order 0.
547+ *
548+ * Context: Caller must protect the swap device with reference count or locks.
549+ * Return: Returns the existing folio if @entry is cached already. Returns
550+ * NULL if failed due to -ENOMEM or @entry have a swap count < 1.
551+ */
552+struct folio *swap_cache_alloc_folio(swp_entry_t entry, gfp_t gfp_mask,
553+ struct mempolicy *mpol, pgoff_t ilx,
554+ bool *new_page_allocated)
555+{
556+ struct swap_info_struct *si = __swap_entry_to_info(entry);
557+ struct folio *folio;
558+ struct folio *result = NULL;
479559
480− if (mem_cgroup_swapin_charge_folio(new_folio, NULL, gfp_mask, entry))
481− goto fail_unlock;
560+ *new_page_allocated = false;
561+ /* Check the swap cache again for readahead path. */
562+ folio = swap_cache_get_folio(entry);
563+ if (folio)
564+ return folio;
482565
483− swap_cache_add_folio(new_folio, entry, &shadow);
484− memcg1_swapin(entry, 1);
566+ /* Skip allocation for unused and bad swap slot for readahead. */
567+ if (!swap_entry_swapped(si, entry))
568+ return NULL;
485569
486− if (shadow)
487− workingset_refault(new_folio, shadow);
488−
489− /* Caller will initiate read into locked new_folio */
490− folio_add_lru(new_folio);
491− *new_page_allocated = true;
492− folio = new_folio;
493−got_folio:
494− result = folio;
495− goto put_and_return;
496−
497−fail_unlock:
498− put_swap_folio(new_folio, entry);
499− folio_unlock(new_folio);
500−put_and_return:
501− if (!(*new_page_allocated) && new_folio)
502− folio_put(new_folio);
570+ /* Allocate a new folio to be added into the swap cache. */
571+ folio = folio_alloc_mpol(gfp_mask, 0, mpol, ilx, numa_node_id());
572+ if (!folio)
573+ return NULL;
574+ /* Try add the new folio, returns existing folio or NULL on failure. */
575+ result = __swap_cache_prepare_and_add(entry, folio, gfp_mask, false);
576+ if (result == folio)
577+ *new_page_allocated = true;
578+ else
579+ folio_put(folio);
503580 return result;
504581 }
505582
583+/**
584+ * swapin_folio - swap-in one or multiple entries skipping readahead.
585+ * @entry: starting swap entry to swap in
586+ * @folio: a new allocated and charged folio
587+ *
588+ * Reads @entry into @folio, @folio will be added to the swap cache.
589+ * If @folio is a large folio, the @entry will be rounded down to align
590+ * with the folio size.
591+ *
592+ * Return: returns pointer to @folio on success. If folio is a large folio
593+ * and this raced with another swapin, NULL will be returned to allow fallback
594+ * to order 0. Else, if another folio was already added to the swap cache,
595+ * return that swap cache folio instead.
596+ */
597+struct folio *swapin_folio(swp_entry_t entry, struct folio *folio)
598+{
599+ struct folio *swapcache;
600+ pgoff_t offset = swp_offset(entry);
601+ unsigned long nr_pages = folio_nr_pages(folio);
602+
603+ entry = swp_entry(swp_type(entry), round_down(offset, nr_pages));
604+ swapcache = __swap_cache_prepare_and_add(entry, folio, 0, true);
605+ if (swapcache == folio)
606+ swap_read_folio(folio, NULL);
607+ return swapcache;
608+}
609+
506610 /*
507611 * Locate a page of swap in physical memory, reserving swap cache space
508612 * and reading the disk if it is not already cached.
@@ -524,8 +628,8 @@ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, · 15 unmodified lines
524628 return NULL;
525629
526630 mpol = get_vma_policy(vma, addr, 0, &ilx);
527− folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx,
528− &page_allocated, false);
631+ folio = swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx,
632+ &page_allocated);
529633 mpol_cond_put(mpol);
530634
531635 if (page_allocated)
@@ -642,9 +746,9 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, · 110 unmodified lines
642746 blk_start_plug(&plug);
643747 for (offset = start_offset; offset <= end_offset ; offset++) {
644748 /* Ok, do the async read-ahead now */
645− folio = __read_swap_cache_async(
646− swp_entry(swp_type(entry), offset),
647− gfp_mask, mpol, ilx, &page_allocated, false);
749+ folio = swap_cache_alloc_folio(
750+ swp_entry(swp_type(entry), offset), gfp_mask, mpol, ilx,
751+ &page_allocated);
648752 if (!folio)
649753 continue;
650754 if (page_allocated) {
@@ -661,8 +765,8 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, · 10 unmodified lines
661765 lru_add_drain(); /* Push any new pages onto the LRU now */
662766 skip:
663767 /* The page was likely read above, so no need for plugging here */
664− folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx,
665− &page_allocated, false);
768+ folio = swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx,
769+ &page_allocated);
666770 if (unlikely(page_allocated))
667771 swap_read_folio(folio, NULL);
668772 return folio;
@@ -766,8 +870,8 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, · 97 unmodified lines
766870 if (!si)
767871 continue;
768872 }
769− folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx,
770− &page_allocated, false);
873+ folio = swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx,
874+ &page_allocated);
771875 if (si)
772876 put_swap_device(si);
773877 if (!folio)
@@ -788,8 +892,8 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, · 14 unmodified lines
788892 lru_add_drain();
789893 skip:
790894 /* The folio was likely read above, so no need for plugging here */
791− folio = __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx,
792− &page_allocated, false);
895+ folio = swap_cache_alloc_folio(targ_entry, gfp_mask, mpol, targ_ilx,
896+ &page_allocated);
793897 if (unlikely(page_allocated))
794898 swap_read_folio(folio, NULL);
795899 return folio;
mm/swapfile.c+384−478
@@ -48,16 +48,18 @@ · 47 unmodified lines
4848 #include <linux/swap_cgroup.h>
4949 #include "swap_table.h"
5050 #include "internal.h"
51+#include "swap_table.h"
5152 #include "swap.h"
5253
5354 static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
5455 unsigned char);
5556 static void free_swap_count_continuations(struct swap_info_struct *);
56−static void swap_entries_free(struct swap_info_struct *si,
57− struct swap_cluster_info *ci,
58− swp_entry_t entry, unsigned int nr_pages);
5957 static void swap_range_alloc(struct swap_info_struct *si,
6058 unsigned int nr_entries);
59+static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr);
60+static void swap_put_entry_locked(struct swap_info_struct *si,
61+ struct swap_cluster_info *ci,
62+ unsigned long offset);
6163 static bool folio_swapcache_freeable(struct folio *folio);
6264 static void move_cluster(struct swap_info_struct *si,
6365 struct swap_cluster_info *ci, struct list_head *list,
@@ -81,9 +83,7 @@ bool swap_migration_ad_supported; · 17 unmodified lines
8183 #endif /* CONFIG_MIGRATION */
8284
8385 static const char Bad_file[] = "Bad swap file entry ";
84−static const char Unused_file[] = "Unused swap file entry ";
8586 static const char Bad_offset[] = "Bad swap offset entry ";
86−static const char Unused_offset[] = "Unused swap offset entry ";
8787
8888 /*
8989 * all active swap_info_structs
@@ -144,11 +144,6 @@ static struct swap_info_struct *swap_entry_to_info(swp_entry_t entry) · 54 unmodified lines
144144 return swap_type_to_info(swp_type(entry));
145145 }
146146
147−static inline unsigned char swap_count(unsigned char ent)
148−{
149− return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */
150−}
151−
152147 /*
153148 * Use the second highest bit of inuse_pages counter as the indicator
154149 * if one swap device is on the available plist, so the atomic can
@@ -180,39 +175,25 @@ static long swap_usage_in_pages(struct swap_info_struct *si) · 25 unmodified lines
180175 #define TTRS_FULL 0x4
181176
182177 static bool swap_only_has_cache(struct swap_info_struct *si,
183− unsigned long offset, int nr_pages)
178+ struct swap_cluster_info *ci,
179+ unsigned long offset, int nr_pages)
184180 {
181+ unsigned int ci_off = offset % SWAPFILE_CLUSTER;
185182 unsigned char *map = si->swap_map + offset;
186183 unsigned char *map_end = map + nr_pages;
184+ unsigned long swp_tb;
187185
188186 do {
189− VM_BUG_ON(!(*map & SWAP_HAS_CACHE));
190− if (*map != SWAP_HAS_CACHE)
187+ swp_tb = __swap_table_get(ci, ci_off);
188+ VM_WARN_ON_ONCE(!swp_tb_is_folio(swp_tb));
189+ if (*map)
191190 return false;
191+ ++ci_off;
192192 } while (++map < map_end);
193193
194194 return true;
195195 }
196196
197−static bool swap_is_last_map(struct swap_info_struct *si,
198− unsigned long offset, int nr_pages, bool *has_cache)
199−{
200− unsigned char *map = si->swap_map + offset;
201− unsigned char *map_end = map + nr_pages;
202− unsigned char count = *map;
203−
204− if (swap_count(count) != 1 && swap_count(count) != SWAP_MAP_SHMEM)
205− return false;
206−
207− while (++map < map_end) {
208− if (*map != count)
209− return false;
210− }
211−
212− *has_cache = !!(count & SWAP_HAS_CACHE);
213− return true;
214−}
215−
216197 /*
217198 * returns number of pages in the folio that backs the swap entry. If positive,
218199 * the folio was reclaimed. If negative, the folio was not reclaimed. If 0, no
@@ -262,12 +243,12 @@ again: · 43 unmodified lines
262243 goto out_unlock;
263244
264245 /*
265− * It's safe to delete the folio from swap cache only if the folio's
266− * swap_map is HAS_CACHE only, which means the slots have no page table
246+ * It's safe to delete the folio from swap cache only if the folio
247+ * is in swap cache with swap count == 0. The slots have no page table
267248 * reference or pending writeback, and can't be allocated to others.
268249 */
269250 ci = swap_cluster_lock(si, offset);
270− need_reclaim = swap_only_has_cache(si, offset, nr_pages);
251+ need_reclaim = swap_only_has_cache(si, ci, offset, nr_pages);
271252 swap_cluster_unlock(ci);
272253 if (!need_reclaim)
273254 goto out_unlock;
@@ -777,68 +758,84 @@ static int swap_cluster_setup_bad_slot(struct swap_cluster_info *cluster_info, · 503 unmodified lines
777758 return 0;
778759 }
779760
761+/*
762+ * Reclaim drops the ci lock, so the cluster may become unusable (freed or
763+ * stolen by a lower order). @usable will be set to false if that happens.
764+ */
780765 static bool cluster_reclaim_range(struct swap_info_struct *si,
781766 struct swap_cluster_info *ci,
782− unsigned long start, unsigned long end)
767+ unsigned long start, unsigned int order,
768+ bool *usable)
783769 {
770+ unsigned int nr_pages = 1 << order;
771+ unsigned long offset = start, end = start + nr_pages;
784772 unsigned char *map = si->swap_map;
785− unsigned long offset = start;
786− int nr_reclaim;
773+ unsigned long swp_tb;
787774
788775 spin_unlock(&ci->lock);
789776 do {
790− switch (READ_ONCE(map[offset])) {
791− case 0:
792− offset++;
793− break;
794− case SWAP_HAS_CACHE:
795− nr_reclaim = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY);
796− if (nr_reclaim > 0)
797− offset += nr_reclaim;
798− else
799− goto out;
777+ if (READ_ONCE(map[offset]))
800778 break;
801− default:
802− goto out;
779+ swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
780+ if (swp_tb_is_folio(swp_tb)) {
781+ if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY) < 0)
782+ break;
803783 }
804− } while (offset < end);
805−out:
784+ } while (++offset < end);
806785 spin_lock(&ci->lock);
786+
787+ /*
788+ * We just dropped ci->lock so cluster could be used by another
789+ * order or got freed, check if it's still usable or empty.
790+ */
791+ if (!cluster_is_usable(ci, order)) {
792+ *usable = false;
793+ return false;
794+ }
795+ *usable = true;
796+
797+ /* Fast path, no need to scan if the whole cluster is empty */
798+ if (cluster_is_empty(ci))
799+ return true;
800+
807801 /*
808802 * Recheck the range no matter reclaim succeeded or not, the slot
809803 * could have been be freed while we are not holding the lock.
810804 */
811− for (offset = start; offset < end; offset++)
812− if (READ_ONCE(map[offset]))
805+ for (offset = start; offset < end; offset++) {
806+ swp_tb = __swap_table_get(ci, offset % SWAPFILE_CLUSTER);
807+ if (map[offset] || !swp_tb_is_null(swp_tb))
813808 return false;
809+ }
814810
815811 return true;
816812 }
817813
818814 static bool cluster_scan_range(struct swap_info_struct *si,
819815 struct swap_cluster_info *ci,
820− unsigned long start, unsigned int nr_pages,
816+ unsigned long offset, unsigned int nr_pages,
821817 bool *need_reclaim)
822818 {
823− unsigned long offset, end = start + nr_pages;
819+ unsigned long end = offset + nr_pages;
824820 unsigned char *map = si->swap_map;
821+ unsigned long swp_tb;
825822
826823 if (cluster_is_empty(ci))
827824 return true;
828825
829− for (offset = start; offset < end; offset++) {
830− switch (READ_ONCE(map[offset])) {
831− case 0:
832− continue;
833− case SWAP_HAS_CACHE:
826+ do {
827+ if (map[offset])
828+ return false;
829+ swp_tb = __swap_table_get(ci, offset % SWAPFILE_CLUSTER);
830+ if (swp_tb_is_folio(swp_tb)) {
834831 if (!vm_swap_full())
835832 return false;
836833 *need_reclaim = true;
837− continue;
838− default:
839− return false;
834+ } else {
835+ /* A entry with no count and no cache must be null */
836+ VM_WARN_ON_ONCE(!swp_tb_is_null(swp_tb));
840837 }
841− }
838+ } while (++offset < end);
842839
843840 return true;
844841 }
@@ -863,28 +860,52 @@ static void swap_cluster_assert_table_empty(struct swap_cluster_info *ci, · 18 unmodified lines
863860 }
864861 }
865862
866−static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster_info *ci,
867− unsigned int start, unsigned char usage,
868− unsigned int order)
863+static bool cluster_alloc_range(struct swap_info_struct *si,
864+ struct swap_cluster_info *ci,
865+ struct folio *folio,
866+ unsigned int offset)
869867 {
870− unsigned int nr_pages = 1 << order;
868+ unsigned long nr_pages;
869+ unsigned int order;
871870
872871 lockdep_assert_held(&ci->lock);
873872
874873 if (!(si->flags & SWP_WRITEOK))
875874 return false;
876875
876+ /*
877+ * All mm swap allocation starts with a folio (folio_alloc_swap),
878+ * it's also the only allocation path for large orders allocation.
879+ * Such swap slots starts with count == 0 and will be increased
880+ * upon folio unmap.
881+ *
882+ * Else, it's a exclusive order 0 allocation for hibernation.
883+ * The slot starts with count == 1 and never increases.
884+ */
885+ if (likely(folio)) {
886+ order = folio_order(folio);
887+ nr_pages = 1 << order;
888+ __swap_cache_add_folio(ci, folio, swp_entry(si->type, offset));
889+ } else if (IS_ENABLED(CONFIG_HIBERNATION)) {
890+ order = 0;
891+ nr_pages = 1;
892+ WARN_ON_ONCE(si->swap_map[offset]);
893+ si->swap_map[offset] = 1;
894+ swap_cluster_assert_table_empty(ci, offset, 1);
895+ } else {
896+ /* Allocation without folio is only possible with hibernation */
897+ WARN_ON_ONCE(1);
898+ return false;
899+ }
900+
877901 /*
878902 * The first allocation in a cluster makes the
879903 * cluster exclusive to this order
880904 */
881905 if (cluster_is_empty(ci))
882906 ci->order = order;
883−
884− memset(si->swap_map + start, usage, nr_pages);
885− swap_cluster_assert_table_empty(ci, start, nr_pages);
886− swap_range_alloc(si, nr_pages);
887907 ci->count += nr_pages;
908+ swap_range_alloc(si, nr_pages);
888909
889910 return true;
890911 }
@@ -892,17 +913,17 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster · 1 unmodified line
892913 /* Try use a new cluster for current CPU and allocate from it. */
893914 static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
894915 struct swap_cluster_info *ci,
895− unsigned long offset,
896− unsigned int order,
897− unsigned char usage)
916+ struct folio *folio, unsigned long offset)
898917 {
899918 unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
900919 unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
901920 unsigned long end = min(start + SWAPFILE_CLUSTER, si->max);
921+ unsigned int order = likely(folio) ? folio_order(folio) : 0;
902922 unsigned int nr_pages = 1 << order;
903− bool need_reclaim, ret;
923+ bool need_reclaim, ret, usable;
904924
905925 lockdep_assert_held(&ci->lock);
926+ VM_WARN_ON(!cluster_is_usable(ci, order));
906927
907928 if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER)
908929 goto out;
@@ -912,14 +933,8 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, · 3 unmodified lines
912933 if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim))
913934 continue;
914935 if (need_reclaim) {
915− ret = cluster_reclaim_range(si, ci, offset, offset + nr_pages);
916− /*
917− * Reclaim drops ci->lock and cluster could be used
918− * by another order. Not checking flag as off-list
919− * cluster has no flag set, and change of list
920− * won't cause fragmentation.
921− */
922− if (!cluster_is_usable(ci, order))
936+ ret = cluster_reclaim_range(si, ci, offset, order, &usable);
937+ if (!usable)
923938 goto out;
924939 if (cluster_is_empty(ci))
925940 offset = start;
@@ -927,7 +942,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, · 1 unmodified line
927942 if (!ret)
928943 continue;
929944 }
930− if (!cluster_alloc_range(si, ci, offset, usage, order))
945+ if (!cluster_alloc_range(si, ci, folio, offset))
931946 break;
932947 found = offset;
933948 offset += nr_pages;
@@ -949,8 +964,7 @@ out: · 15 unmodified lines
949964
950965 static unsigned int alloc_swap_scan_list(struct swap_info_struct *si,
951966 struct list_head *list,
952− unsigned int order,
953− unsigned char usage,
967+ struct folio *folio,
954968 bool scan_all)
955969 {
956970 unsigned int found = SWAP_ENTRY_INVALID;
@@ -962,7 +976,7 @@ static unsigned int alloc_swap_scan_list(struct swap_info_struct *si, · 5 unmodified lines
962976 if (!ci)
963977 break;
964978 offset = cluster_offset(si, ci);
965− found = alloc_swap_scan_cluster(si, ci, offset, order, usage);
979+ found = alloc_swap_scan_cluster(si, ci, folio, offset);
966980 if (found)
967981 break;
968982 } while (scan_all);
@@ -987,7 +1001,8 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) · 18 unmodified lines
9871001 to_scan--;
9881002
9891003 while (offset < end) {
990− if (READ_ONCE(map[offset]) == SWAP_HAS_CACHE) {
1004+ if (!READ_ONCE(map[offset]) &&
1005+ swp_tb_is_folio(swap_table_get(ci, offset % SWAPFILE_CLUSTER))) {
9911006 spin_unlock(&ci->lock);
9921007 nr_reclaim = __try_to_reclaim_swap(si, offset,
9931008 TTRS_ANYWAY);
@@ -1023,10 +1038,11 @@ static void swap_reclaim_work(struct work_struct *work) · 29 unmodified lines
10231038 * Try to allocate swap entries with specified order and try set a new
10241039 * cluster for current CPU too.
10251040 */
1026−static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int order,
1027− unsigned char usage)
1041+static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
1042+ struct folio *folio)
10281043 {
10291044 struct swap_cluster_info *ci;
1045+ unsigned int order = likely(folio) ? folio_order(folio) : 0;
10301046 unsigned int offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
10311047
10321048 /*
@@ -1048,8 +1064,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o · 15 unmodified lines
10481064 if (cluster_is_usable(ci, order)) {
10491065 if (cluster_is_empty(ci))
10501066 offset = cluster_offset(si, ci);
1051− found = alloc_swap_scan_cluster(si, ci, offset,
1052− order, usage);
1067+ found = alloc_swap_scan_cluster(si, ci, folio, offset);
10531068 } else {
10541069 swap_cluster_unlock(ci);
10551070 }
@@ -1063,22 +1078,19 @@ new_cluster: · 7 unmodified lines
10631078 * to spread out the writes.
10641079 */
10651080 if (si->flags & SWP_PAGE_DISCARD) {
1066− found = alloc_swap_scan_list(si, &si->free_clusters, order, usage,
1067− false);
1081+ found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
10681082 if (found)
10691083 goto done;
10701084 }
10711085
10721086 if (order < PMD_ORDER) {
1073− found = alloc_swap_scan_list(si, &si->nonfull_clusters[order],
1074− order, usage, true);
1087+ found = alloc_swap_scan_list(si, &si->nonfull_clusters[order], folio, true);
10751088 if (found)
10761089 goto done;
10771090 }
10781091
10791092 if (!(si->flags & SWP_PAGE_DISCARD)) {
1080− found = alloc_swap_scan_list(si, &si->free_clusters, order, usage,
1081− false);
1093+ found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
10821094 if (found)
10831095 goto done;
10841096 }
@@ -1092,10 +1104,9 @@ new_cluster: · 7 unmodified lines
10921104 * Scan only one fragment cluster is good enough. Order 0
10931105 * allocation will surely success, and large allocation
10941106 * failure is not critical. Scanning one cluster still
1095− * keeps the list rotated and reclaimed (for HAS_CACHE).
1107+ * keeps the list rotated and reclaimed (for clean swap cache).
10961108 */
1097− found = alloc_swap_scan_list(si, &si->frag_clusters[order], order,
1098− usage, false);
1109+ found = alloc_swap_scan_list(si, &si->frag_clusters[order], folio, false);
10991110 if (found)
11001111 goto done;
11011112 }
@@ -1109,13 +1120,11 @@ new_cluster: · 7 unmodified lines
11091120 * Clusters here have at least one usable slots and can't fail order 0
11101121 * allocation, but reclaim may drop si->lock and race with another user.
11111122 */
1112− found = alloc_swap_scan_list(si, &si->frag_clusters[o],
1113− 0, usage, true);
1123+ found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true);
11141124 if (found)
11151125 goto done;
11161126
1117− found = alloc_swap_scan_list(si, &si->nonfull_clusters[o],
1118− 0, usage, true);
1127+ found = alloc_swap_scan_list(si, &si->nonfull_clusters[o], folio, true);
11191128 if (found)
11201129 goto done;
11211130 }
@@ -1306,12 +1315,12 @@ static bool get_swap_device_info(struct swap_info_struct *si) · 184 unmodified lines
13061315 * Fast path try to get swap entries with specified order from current
13071316 * CPU's swap entry pool (a cluster).
13081317 */
1309−static bool swap_alloc_fast(swp_entry_t *entry,
1310− int order)
1318+static bool swap_alloc_fast(struct folio *folio)
13111319 {
1320+ unsigned int order = folio_order(folio);
13121321 struct swap_cluster_info *ci;
13131322 struct swap_info_struct *si;
1314− unsigned int offset, found = SWAP_ENTRY_INVALID;
1323+ unsigned int offset;
13151324
13161325 /*
13171326 * Once allocated, swap_info_struct will never be completely freed,
@@ -1326,22 +1335,18 @@ static bool swap_alloc_fast(swp_entry_t *entry, · 8 unmodified lines
13261335 if (cluster_is_usable(ci, order)) {
13271336 if (cluster_is_empty(ci))
13281337 offset = cluster_offset(si, ci);
1329− found = alloc_swap_scan_cluster(si, ci, offset, order, SWAP_HAS_CACHE);
1330− if (found)
1331− *entry = swp_entry(si->type, found);
1338+ alloc_swap_scan_cluster(si, ci, folio, offset);
13321339 } else {
13331340 swap_cluster_unlock(ci);
13341341 }
13351342
13361343 put_swap_device(si);
1337− return !!found;
1344+ return folio_test_swapcache(folio);
13381345 }
13391346
13401347 /* Rotate the device and switch to a new cluster */
1341−static void swap_alloc_slow(swp_entry_t *entry,
1342− int order)
1348+static void swap_alloc_slow(struct folio *folio)
13431349 {
1344− unsigned long offset;
13451350 struct swap_info_struct *si, *next;
13461351
13471352 spin_lock(&swap_avail_lock);
@@ -1351,13 +1356,11 @@ start_over: · 3 unmodified lines
13511356 plist_requeue(&si->avail_list, &swap_avail_head);
13521357 spin_unlock(&swap_avail_lock);
13531358 if (get_swap_device_info(si)) {
1354− offset = cluster_alloc_swap_entry(si, order, SWAP_HAS_CACHE);
1359+ cluster_alloc_swap_entry(si, folio);
13551360 put_swap_device(si);
1356− if (offset) {
1357− *entry = swp_entry(si->type, offset);
1361+ if (folio_test_swapcache(folio))
13581362 return;
1359− }
1360− if (order)
1363+ if (folio_test_large(folio))
13611364 return;
13621365 }
13631366
@@ -1408,6 +1411,75 @@ start_over: · 44 unmodified lines
14081411 return false;
14091412 }
14101413
1414+/**
1415+ * swap_put_entries_cluster - Decrease the swap count of a set of slots.
1416+ * @si: The swap device.
1417+ * @start: start offset of slots.
1418+ * @nr: number of slots.
1419+ * @reclaim_cache: if true, also reclaim the swap cache.
1420+ *
1421+ * This helper decreases the swap count of a set of slots and tries to
1422+ * batch free them. Also reclaims the swap cache if @reclaim_cache is true.
1423+ * Context: The caller must ensure that all slots belong to the same
1424+ * cluster and their swap count doesn't go underflow.
1425+ */
1426+static void swap_put_entries_cluster(struct swap_info_struct *si,
1427+ unsigned long start, int nr,
1428+ bool reclaim_cache)
1429+{
1430+ unsigned long offset = start, end = start + nr;
1431+ unsigned long batch_start = SWAP_ENTRY_INVALID;
1432+ struct swap_cluster_info *ci;
1433+ bool need_reclaim = false;
1434+ unsigned int nr_reclaimed;
1435+ unsigned long swp_tb;
1436+ unsigned int count;
1437+
1438+ ci = swap_cluster_lock(si, offset);
1439+ do {
1440+ swp_tb = __swap_table_get(ci, offset % SWAPFILE_CLUSTER);
1441+ count = si->swap_map[offset];
1442+ VM_WARN_ON(count < 1 || count == SWAP_MAP_BAD);
1443+ if (count == 1) {
1444+ /* count == 1 and non-cached slots will be batch freed. */
1445+ if (!swp_tb_is_folio(swp_tb)) {
1446+ if (!batch_start)
1447+ batch_start = offset;
1448+ continue;
1449+ }
1450+ /* count will be 0 after put, slot can be reclaimed */
1451+ need_reclaim = true;
1452+ }
1453+ /*
1454+ * A count != 1 or cached slot can't be freed. Put its swap
1455+ * count and then free the interrupted pending batch. Cached
1456+ * slots will be freed when folio is removed from swap cache
1457+ * (__swap_cache_del_folio).
1458+ */
1459+ swap_put_entry_locked(si, ci, offset);
1460+ if (batch_start) {
1461+ swap_entries_free(si, ci, batch_start, offset - batch_start);
1462+ batch_start = SWAP_ENTRY_INVALID;
1463+ }
1464+ } while (++offset < end);
1465+
1466+ if (batch_start)
1467+ swap_entries_free(si, ci, batch_start, offset - batch_start);
1468+ swap_cluster_unlock(ci);
1469+
1470+ if (!need_reclaim || !reclaim_cache)
1471+ return;
1472+
1473+ offset = start;
1474+ do {
1475+ nr_reclaimed = __try_to_reclaim_swap(si, offset,
1476+ TTRS_UNMAPPED | TTRS_FULL);
1477+ offset++;
1478+ if (nr_reclaimed)
1479+ offset = round_up(offset, abs(nr_reclaimed));
1480+ } while (offset < end);
1481+}
1482+
14111483 /**
14121484 * folio_alloc_swap - allocate swap space for a folio
14131485 * @folio: folio we want to move to swap
@@ -1422,7 +1494,6 @@ int folio_alloc_swap(struct folio *folio) · 8 unmodified lines
14221494 {
14231495 unsigned int order = folio_order(folio);
14241496 unsigned int size = 1 << order;
1425− swp_entry_t entry = {};
14261497
14271498 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
14281499 VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
@@ -1447,89 +1518,94 @@ int folio_alloc_swap(struct folio *folio) · 18 unmodified lines
14471518
14481519 again:
14491520 local_lock(&percpu_swap_cluster.lock);
1450− if (!swap_alloc_fast(&entry, order))
1451− swap_alloc_slow(&entry, order);
1521+ if (!swap_alloc_fast(folio))
1522+ swap_alloc_slow(folio);
14521523 local_unlock(&percpu_swap_cluster.lock);
14531524
1454− if (unlikely(!order && !entry.val)) {
1525+ if (!order && unlikely(!folio_test_swapcache(folio))) {
14551526 if (swap_sync_discard())
14561527 goto again;
14571528 }
14581529
14591530 /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */
1460− if (mem_cgroup_try_charge_swap(folio, entry))
1461− goto out_free;
1531+ if (unlikely(mem_cgroup_try_charge_swap(folio, folio->swap)))
1532+ swap_cache_del_folio(folio);
14621533
1463− if (!entry.val)
1534+ if (unlikely(!folio_test_swapcache(folio)))
14641535 return -ENOMEM;
14651536
1466− swap_cache_add_folio(folio, entry, NULL);
1467−
14681537 return 0;
1538+}
1539+
1540+/**
1541+ * folio_dup_swap() - Increase swap count of swap entries of a folio.
1542+ * @folio: folio with swap entries bounded.
1543+ * @subpage: if not NULL, only increase the swap count of this subpage.
1544+ *
1545+ * Typically called when the folio is unmapped and have its swap entry to
1546+ * take its palce.
1547+ *
1548+ * Context: Caller must ensure the folio is locked and in the swap cache.
1549+ * NOTE: The caller also has to ensure there is no raced call to
1550+ * swap_put_entries_direct on its swap entry before this helper returns, or
1551+ * the swap map may underflow. Currently, we only accept @subpage == NULL
1552+ * for shmem due to the limitation of swap continuation: shmem always
1553+ * duplicates the swap entry only once, so there is no such issue for it.
1554+ */
1555+int folio_dup_swap(struct folio *folio, struct page *subpage)
1556+{
1557+ int err = 0;
1558+ swp_entry_t entry = folio->swap;
1559+ unsigned long nr_pages = folio_nr_pages(folio);
1560+
1561+ VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
1562+ VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio);
1563+
1564+ if (subpage) {
1565+ entry.val += folio_page_idx(folio, subpage);
1566+ nr_pages = 1;
1567+ }
1568+
1569+ while (!err && __swap_duplicate(entry, 1, nr_pages) == -ENOMEM)
1570+ err = add_swap_count_continuation(entry, GFP_ATOMIC);
14691571
1470−out_free:
1471− put_swap_folio(folio, entry);
1472− return -ENOMEM;
1572+ return err;
14731573 }
14741574
1475−static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
1575+/**
1576+ * folio_put_swap() - Decrease swap count of swap entries of a folio.
1577+ * @folio: folio with swap entries bounded, must be in swap cache and locked.
1578+ * @subpage: if not NULL, only decrease the swap count of this subpage.
1579+ *
1580+ * This won't free the swap slots even if swap count drops to zero, they are
1581+ * still pinned by the swap cache. User may call folio_free_swap to free them.
1582+ * Context: Caller must ensure the folio is locked and in the swap cache.
1583+ */
1584+void folio_put_swap(struct folio *folio, struct page *subpage)
14761585 {
1477− struct swap_info_struct *si;
1478− unsigned long offset;
1586+ swp_entry_t entry = folio->swap;
1587+ unsigned long nr_pages = folio_nr_pages(folio);
1588+ struct swap_info_struct *si = __swap_entry_to_info(entry);
14791589
1480− if (!entry.val)
1481− goto out;
1482− si = swap_entry_to_info(entry);
1483− if (!si)
1484− goto bad_nofile;
1485− if (data_race(!(si->flags & SWP_USED)))
1486− goto bad_device;
1487− offset = swp_offset(entry);
1488− if (offset >= si->max)
1489− goto bad_offset;
1490− if (data_race(!si->swap_map[swp_offset(entry)]))
1491− goto bad_free;
1492− return si;
1590+ VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
1591+ VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio);
14931592
1494−bad_free:
1495− pr_err("%s: %s%08lx\n", __func__, Unused_offset, entry.val);
1496− goto out;
1497−bad_offset:
1498− pr_err("%s: %s%08lx\n", __func__, Bad_offset, entry.val);
1499− goto out;
1500−bad_device:
1501− pr_err("%s: %s%08lx\n", __func__, Unused_file, entry.val);
1502− goto out;
1503−bad_nofile:
1504− pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
1505−out:
1506− return NULL;
1593+ if (subpage) {
1594+ entry.val += folio_page_idx(folio, subpage);
1595+ nr_pages = 1;
1596+ }
1597+
1598+ swap_put_entries_cluster(si, swp_offset(entry), nr_pages, false);
15071599 }
15081600
1509−static unsigned char swap_entry_put_locked(struct swap_info_struct *si,
1510− struct swap_cluster_info *ci,
1511− swp_entry_t entry,
1512− unsigned char usage)
1601+static void swap_put_entry_locked(struct swap_info_struct *si,
1602+ struct swap_cluster_info *ci,
1603+ unsigned long offset)
15131604 {
1514− unsigned long offset = swp_offset(entry);
15151605 unsigned char count;
1516− unsigned char has_cache;
15171606
15181607 count = si->swap_map[offset];
1519−
1520− has_cache = count & SWAP_HAS_CACHE;
1521− count &= ~SWAP_HAS_CACHE;
1522−
1523− if (usage == SWAP_HAS_CACHE) {
1524− VM_BUG_ON(!has_cache);
1525− has_cache = 0;
1526− } else if (count == SWAP_MAP_SHMEM) {
1527− /*
1528− * Or we could insist on shmem.c using a special
1529− * swap_shmem_free() and free_shmem_swap_and_cache()...
1530− */
1531− count = 0;
1532− } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
1608+ if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
15331609 if (count == COUNT_CONTINUED) {
15341610 if (swap_count_continued(si, offset, count))
15351611 count = SWAP_MAP_MAX | COUNT_CONTINUED;
@@ -1539,13 +1615,9 @@ static unsigned char swap_entry_put_locked(struct swap_info_struct *si, · 3 unmodified lines
15391615 count--;
15401616 }
15411617
1542− usage = count | has_cache;
1543− if (usage)
1544− WRITE_ONCE(si->swap_map[offset], usage);
1545− else
1546− swap_entries_free(si, ci, entry, 1);
1547−
1548− return usage;
1618+ WRITE_ONCE(si->swap_map[offset], count);
1619+ if (!count && !swp_tb_is_folio(__swap_table_get(ci, offset % SWAPFILE_CLUSTER)))
1620+ swap_entries_free(si, ci, offset, 1);
15491621 }
15501622
15511623 /*
@@ -1574,10 +1646,9 @@ static unsigned char swap_entry_put_locked(struct swap_info_struct *si, · 22 unmodified lines
15741646 * CPU1 CPU2
15751647 * do_swap_page()
15761648 * ... swapoff+swapon
1577− * __read_swap_cache_async()
1578− * swapcache_prepare()
1579− * __swap_duplicate()
1580− * // check swap_map
1649+ * swap_cache_alloc_folio()
1650+ * swap_cache_add_folio()
1651+ * // check swap_map
15811652 * // verify PTE not changed
15821653 *
15831654 * In __swap_duplicate(), the swap_map need to be checked before
@@ -1614,105 +1685,15 @@ put_out: · 30 unmodified lines
16141685 return NULL;
16151686 }
16161687
1617−static void swap_entries_put_cache(struct swap_info_struct *si,
1618− swp_entry_t entry, int nr)
1619−{
1620− unsigned long offset = swp_offset(entry);
1621− struct swap_cluster_info *ci;
1622−
1623− ci = swap_cluster_lock(si, offset);
1624− if (swap_only_has_cache(si, offset, nr)) {
1625− swap_entries_free(si, ci, entry, nr);
1626− } else {
1627− for (int i = 0; i < nr; i++, entry.val++)
1628− swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE);
1629− }
1630− swap_cluster_unlock(ci);
1631−}
1632−
1633−static bool swap_entries_put_map(struct swap_info_struct *si,
1634− swp_entry_t entry, int nr)
1635−{
1636− unsigned long offset = swp_offset(entry);
1637− struct swap_cluster_info *ci;
1638− bool has_cache = false;
1639− unsigned char count;
1640− int i;
1641−
1642− if (nr <= 1)
1643− goto fallback;
1644− count = swap_count(data_race(si->swap_map[offset]));
1645− if (count != 1 && count != SWAP_MAP_SHMEM)
1646− goto fallback;
1647−
1648− ci = swap_cluster_lock(si, offset);
1649− if (!swap_is_last_map(si, offset, nr, &has_cache)) {
1650− goto locked_fallback;
1651− }
1652− if (!has_cache)
1653− swap_entries_free(si, ci, entry, nr);
1654− else
1655− for (i = 0; i < nr; i++)
1656− WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE);
1657− swap_cluster_unlock(ci);
1658−
1659− return has_cache;
1660−
1661−fallback:
1662− ci = swap_cluster_lock(si, offset);
1663−locked_fallback:
1664− for (i = 0; i < nr; i++, entry.val++) {
1665− count = swap_entry_put_locked(si, ci, entry, 1);
1666− if (count == SWAP_HAS_CACHE)
1667− has_cache = true;
1668− }
1669− swap_cluster_unlock(ci);
1670− return has_cache;
1671−}
1672−
1673−/*
1674− * Only functions with "_nr" suffix are able to free entries spanning
1675− * cross multi clusters, so ensure the range is within a single cluster
1676− * when freeing entries with functions without "_nr" suffix.
1677− */
1678−static bool swap_entries_put_map_nr(struct swap_info_struct *si,
1679− swp_entry_t entry, int nr)
1680−{
1681− int cluster_nr, cluster_rest;
1682− unsigned long offset = swp_offset(entry);
1683− bool has_cache = false;
1684−
1685− cluster_rest = SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER;
1686− while (nr) {
1687− cluster_nr = min(nr, cluster_rest);
1688− has_cache |= swap_entries_put_map(si, entry, cluster_nr);
1689− cluster_rest = SWAPFILE_CLUSTER;
1690− nr -= cluster_nr;
1691− entry.val += cluster_nr;
1692− }
1693−
1694− return has_cache;
1695−}
1696−
1697−/*
1698− * Check if it's the last ref of swap entry in the freeing path.
1699− * Qualified value includes 1, SWAP_HAS_CACHE or SWAP_MAP_SHMEM.
1700− */
1701−static inline bool __maybe_unused swap_is_last_ref(unsigned char count)
1702−{
1703− return (count == SWAP_HAS_CACHE) || (count == 1) ||
1704− (count == SWAP_MAP_SHMEM);
1705−}
1706−
17071688 /*
17081689 * Drop the last ref of swap entries, caller have to ensure all entries
17091690 * belong to the same cgroup and cluster.
17101691 */
1711−static void swap_entries_free(struct swap_info_struct *si,
1712− struct swap_cluster_info *ci,
1713− swp_entry_t entry, unsigned int nr_pages)
1692+void swap_entries_free(struct swap_info_struct *si,
1693+ struct swap_cluster_info *ci,
1694+ unsigned long offset, unsigned int nr_pages)
17141695 {
1715− unsigned long offset = swp_offset(entry);
1696+ swp_entry_t entry = swp_entry(si->type, offset);
17161697 unsigned char *map = si->swap_map + offset;
17171698 unsigned char *map_end = map + nr_pages;
17181699
@@ -1723,7 +1704,7 @@ static void swap_entries_free(struct swap_info_struct *si, · 4 unmodified lines
17231704
17241705 ci->count -= nr_pages;
17251706 do {
1726− VM_BUG_ON(!swap_is_last_ref(*map));
1707+ VM_WARN_ON(*map > 1);
17271708 *map = 0;
17281709 } while (++map < map_end);
17291710
@@ -1737,55 +1718,18 @@ static void swap_entries_free(struct swap_info_struct *si, · 7 unmodified lines
17371718 partial_free_cluster(si, ci);
17381719 }
17391720
1740−/*
1741− * Caller has made sure that the swap device corresponding to entry
1742− * is still around or has not been recycled.
1743− */
1744−void swap_free_nr(swp_entry_t entry, int nr_pages)
1745−{
1746− int nr;
1747− struct swap_info_struct *sis;
1748− unsigned long offset = swp_offset(entry);
1749−
1750− sis = _swap_info_get(entry);
1751− if (!sis)
1752− return;
1753−
1754− while (nr_pages) {
1755− nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);
1756− swap_entries_put_map(sis, swp_entry(sis->type, offset), nr);
1757− offset += nr;
1758− nr_pages -= nr;
1759− }
1760−}
1761−
1762−/*
1763− * Called after dropping swapcache to decrease refcnt to swap entries.
1764− */
1765−void put_swap_folio(struct folio *folio, swp_entry_t entry)
1766−{
1767− struct swap_info_struct *si;
1768− int size = 1 << swap_entry_order(folio_order(folio));
1769−
1770− si = _swap_info_get(entry);
1771− if (!si)
1772− return;
1773−
1774− swap_entries_put_cache(si, entry, size);
1775−}
1776−
17771721 int __swap_count(swp_entry_t entry)
17781722 {
17791723 struct swap_info_struct *si = __swap_entry_to_info(entry);
17801724 pgoff_t offset = swp_offset(entry);
17811725
1782− return swap_count(si->swap_map[offset]);
1726+ return si->swap_map[offset];
17831727 }
17841728
1785−/*
1786− * How many references to @entry are currently swapped out?
1787− * This does not give an exact answer when swap count is continued,
1788− * but does include the high COUNT_CONTINUED flag to allow for that.
1729+/**
1730+ * swap_entry_swapped - Check if the swap entry is swapped.
1731+ * @si: the swap device.
1732+ * @entry: the swap entry.
17891733 */
17901734 bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry)
17911735 {
@@ -1794,9 +1738,10 @@ bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry) · 2 unmodified lines
17941738 int count;
17951739
17961740 ci = swap_cluster_lock(si, offset);
1797− count = swap_count(si->swap_map[offset]);
1741+ count = si->swap_map[offset];
17981742 swap_cluster_unlock(ci);
1799− return !!count;
1743+
1744+ return count && count != SWAP_MAP_BAD;
18001745 }
18011746
18021747 /*
@@ -1812,7 +1757,7 @@ int swp_swapcount(swp_entry_t entry) · 9 unmodified lines
18121757 pgoff_t offset;
18131758 unsigned char *map;
18141759
1815− si = _swap_info_get(entry);
1760+ si = get_swap_device(entry);
18161761 if (!si)
18171762 return 0;
18181763
@@ -1820,7 +1765,7 @@ int swp_swapcount(swp_entry_t entry) · 1 unmodified line
18201765
18211766 ci = swap_cluster_lock(si, offset);
18221767
1823− count = swap_count(si->swap_map[offset]);
1768+ count = si->swap_map[offset];
18241769 if (!(count & COUNT_CONTINUED))
18251770 goto out;
18261771
@@ -1842,6 +1787,7 @@ int swp_swapcount(swp_entry_t entry) · 15 unmodified lines
18421787 } while (tmp_count & COUNT_CONTINUED);
18431788 out:
18441789 swap_cluster_unlock(ci);
1790+ put_swap_device(si);
18451791 return count;
18461792 }
18471793
@@ -1858,12 +1804,12 @@ static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, · 10 unmodified lines
18581804
18591805 ci = swap_cluster_lock(si, offset);
18601806 if (nr_pages == 1) {
1861− if (swap_count(map[roffset]))
1807+ if (map[roffset])
18621808 ret = true;
18631809 goto unlock_out;
18641810 }
18651811 for (i = 0; i < nr_pages; i++) {
1866− if (swap_count(map[offset + i])) {
1812+ if (map[offset + i]) {
18671813 ret = true;
18681814 break;
18691815 }
@@ -1876,11 +1822,12 @@ unlock_out: · 6 unmodified lines
18761822 static bool folio_swapped(struct folio *folio)
18771823 {
18781824 swp_entry_t entry = folio->swap;
1879− struct swap_info_struct *si = _swap_info_get(entry);
1825+ struct swap_info_struct *si;
18801826
1881− if (!si)
1882− return false;
1827+ VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
1828+ VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
18831829
1830+ si = __swap_entry_to_info(entry);
18841831 if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!folio_test_large(folio)))
18851832 return swap_entry_swapped(si, entry);
18861833
@@ -1939,73 +1886,45 @@ bool folio_free_swap(struct folio *folio) · 52 unmodified lines
19391886 }
19401887
19411888 /**
1942− * free_swap_and_cache_nr() - Release reference on range of swap entries and
1943− * reclaim their cache if no more references remain.
1889+ * swap_put_entries_direct() - Release reference on range of swap entries and
1890+ * reclaim their cache if no more references remain.
19441891 * @entry: First entry of range.
19451892 * @nr: Number of entries in range.
19461893 *
19471894 * For each swap entry in the contiguous range, release a reference. If any swap
19481895 * entries become free, try to reclaim their underlying folios, if present. The
19491896 * offset range is defined by [entry.offset, entry.offset + nr).
1897+ *
1898+ * Context: Caller must ensure there is no race condition on the reference
1899+ * owner. e.g., locking the PTL of a PTE containing the entry being released.
19501900 */
1951−void free_swap_and_cache_nr(swp_entry_t entry, int nr)
1901+void swap_put_entries_direct(swp_entry_t entry, int nr)
19521902 {
19531903 const unsigned long start_offset = swp_offset(entry);
19541904 const unsigned long end_offset = start_offset + nr;
1905+ unsigned long offset, cluster_end;
19551906 struct swap_info_struct *si;
1956− bool any_only_cache = false;
1957− unsigned long offset;
19581907
19591908 si = get_swap_device(entry);
1960− if (!si)
1909+ if (WARN_ON_ONCE(!si))
19611910 return;
1962−
1963− if (WARN_ON(end_offset > si->max))
1964− goto out;
1965−
1966− /*
1967− * First free all entries in the range.
1968− */
1969− any_only_cache = swap_entries_put_map_nr(si, entry, nr);
1970−
1971− /*
1972− * Short-circuit the below loop if none of the entries had their
1973− * reference drop to zero.
1974− */
1975− if (!any_only_cache)
1911+ if (WARN_ON_ONCE(end_offset > si->max))
19761912 goto out;
19771913
1978− /*
1979− * Now go back over the range trying to reclaim the swap cache.
1980− */
1981− for (offset = start_offset; offset < end_offset; offset += nr) {
1982− nr = 1;
1983− if (READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) {
1984− /*
1985− * Folios are always naturally aligned in swap so
1986− * advance forward to the next boundary. Zero means no
1987− * folio was found for the swap entry, so advance by 1
1988− * in this case. Negative value means folio was found
1989− * but could not be reclaimed. Here we can still advance
1990− * to the next boundary.
1991− */
1992− nr = __try_to_reclaim_swap(si, offset,
1993− TTRS_UNMAPPED | TTRS_FULL);
1994− if (nr == 0)
1995− nr = 1;
1996− else if (nr < 0)
1997− nr = -nr;
1998− nr = ALIGN(offset + 1, nr) - offset;
1999− }
2000− }
2001−
1914+ /* Put entries and reclaim cache in each cluster */
1915+ offset = start_offset;
1916+ do {
1917+ cluster_end = min(round_up(offset + 1, SWAPFILE_CLUSTER), end_offset);
1918+ swap_put_entries_cluster(si, offset, cluster_end - offset, true);
1919+ offset = cluster_end;
1920+ } while (offset < end_offset);
20021921 out:
20031922 put_swap_device(si);
20041923 }
20051924
20061925 #ifdef CONFIG_HIBERNATION
2007−
2008−swp_entry_t get_swap_page_of_type(int type)
1926+/* Allocate a slot for hibernation */
1927+swp_entry_t swap_alloc_hibernation_slot(int type)
20091928 {
20101929 struct swap_info_struct *si = swap_type_to_info(type);
20111930 unsigned long offset;
@@ -2018,11 +1937,11 @@ swp_entry_t get_swap_page_of_type(int type) · 6 unmodified lines
20181937 if (get_swap_device_info(si)) {
20191938 if (si->flags & SWP_WRITEOK) {
20201939 /*
2021− * Grab the local lock to be complaint
1940+ * Grab the local lock to be compliant
20221941 * with swap table allocation.
20231942 */
20241943 local_lock(&percpu_swap_cluster.lock);
2025− offset = cluster_alloc_swap_entry(si, 0, 1);
1944+ offset = cluster_alloc_swap_entry(si, NULL);
20261945 local_unlock(&percpu_swap_cluster.lock);
20271946 if (offset)
20281947 entry = swp_entry(si->type, offset);
@@ -2033,6 +1952,26 @@ fail: · 4 unmodified lines
20331952 return entry;
20341953 }
20351954
1955+/* Free a slot allocated by swap_alloc_hibernation_slot */
1956+void swap_free_hibernation_slot(swp_entry_t entry)
1957+{
1958+ struct swap_info_struct *si;
1959+ struct swap_cluster_info *ci;
1960+ pgoff_t offset = swp_offset(entry);
1961+
1962+ si = get_swap_device(entry);
1963+ if (WARN_ON(!si))
1964+ return;
1965+
1966+ ci = swap_cluster_lock(si, offset);
1967+ swap_put_entry_locked(si, ci, offset);
1968+ swap_cluster_unlock(ci);
1969+
1970+ /* In theory readahead might add it to the swap cache by accident */
1971+ __try_to_reclaim_swap(si, offset, TTRS_ANYWAY);
1972+ put_swap_device(si);
1973+}
1974+
20361975 /*
20371976 * Find the swap type that corresponds to given device (if any).
20381977 *
@@ -2194,7 +2133,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · 155 unmodified lines
21942133 /*
21952134 * Some architectures may have to restore extra metadata to the page
21962135 * when reading from swap. This metadata may be indexed by swap entry
2197− * so this must be called before swap_free().
2136+ * so this must be called before folio_put_swap().
21982137 */
21992138 arch_swap_restore(folio_swap(entry, folio), folio);
22002139
@@ -2235,7 +2174,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · 34 unmodified lines
22352174 new_pte = pte_mkuffd_wp(new_pte);
22362175 setpte:
22372176 set_pte_at(vma->vm_mm, addr, pte, new_pte);
2238− swap_free(entry);
2177+ folio_put_swap(swapcache, folio_file_page(swapcache, swp_offset(entry)));
22392178 out:
22402179 if (pte)
22412180 pte_unmap_unlock(pte, ptl);
@@ -2430,6 +2369,7 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si, · 188 unmodified lines
24302369 unsigned int prev)
24312370 {
24322371 unsigned int i;
2372+ unsigned long swp_tb;
24332373 unsigned char count;
24342374
24352375 /*
@@ -2440,7 +2380,11 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si, · 4 unmodified lines
24402380 */
24412381 for (i = prev + 1; i < si->max; i++) {
24422382 count = READ_ONCE(si->swap_map[i]);
2443− if (count && swap_count(count) != SWAP_MAP_BAD)
2383+ swp_tb = swap_table_get(__swap_offset_to_cluster(si, i),
2384+ i % SWAPFILE_CLUSTER);
2385+ if (count == SWAP_MAP_BAD)
2386+ continue;
2387+ if (count || swp_tb_is_folio(swp_tb))
24442388 break;
24452389 if ((i % LATENCY_LIMIT) == 0)
24462390 cond_resched();
@@ -3650,67 +3594,39 @@ void si_swapinfo(struct sysinfo *val) · 1203 unmodified lines
36503594 * Returns error code in following case.
36513595 * - success -> 0
36523596 * - swp_entry is invalid -> EINVAL
3653− * - swap-cache reference is requested but there is already one. -> EEXIST
3654− * - swap-cache reference is requested but the entry is not used. -> ENOENT
3597+ * - swap-mapped reference is requested but the entry is not used. -> ENOENT
36553598 * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
36563599 */
3657−static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr)
3600+static int swap_dup_entries(struct swap_info_struct *si,
3601+ struct swap_cluster_info *ci,
3602+ unsigned long offset,
3603+ unsigned char usage, int nr)
36583604 {
3659− struct swap_info_struct *si;
3660− struct swap_cluster_info *ci;
3661− unsigned long offset;
3605+ int i;
36623606 unsigned char count;
3663− unsigned char has_cache;
3664− int err, i;
3665−
3666− si = swap_entry_to_info(entry);
3667− if (WARN_ON_ONCE(!si)) {
3668− pr_err("%s%08lx\n", Bad_file, entry.val);
3669− return -EINVAL;
3670− }
3671−
3672− offset = swp_offset(entry);
3673− VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);
3674− VM_WARN_ON(usage == 1 && nr > 1);
3675− ci = swap_cluster_lock(si, offset);
36763607
3677− err = 0;
36783608 for (i = 0; i < nr; i++) {
36793609 count = si->swap_map[offset + i];
3680−
36813610 /*
3682− * swapin_readahead() doesn't check if a swap entry is valid, so the
3683− * swap entry could be SWAP_MAP_BAD. Check here with lock held.
3611+ * For swapin out, allocator never allocates bad slots. for
3612+ * swapin, readahead is guarded by swap_entry_swapped.
36843613 */
3685− if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
3686− err = -ENOENT;
3687− goto unlock_out;
3688− }
3689−
3690− has_cache = count & SWAP_HAS_CACHE;
3691− count &= ~SWAP_HAS_CACHE;
3692−
3693− if (!count && !has_cache) {
3694− err = -ENOENT;
3695− } else if (usage == SWAP_HAS_CACHE) {
3696− if (has_cache)
3697− err = -EEXIST;
3698− } else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) {
3699− err = -EINVAL;
3700− }
3701−
3702− if (err)
3703− goto unlock_out;
3614+ if (WARN_ON(count == SWAP_MAP_BAD))
3615+ return -ENOENT;
3616+ /*
3617+ * Swap count duplication must be guarded by either swap cache folio (from
3618+ * folio_dup_swap) or external lock of existing entry (from swap_dup_entry_direct).
3619+ */
3620+ if (WARN_ON(!count &&
3621+ !swp_tb_is_folio(__swap_table_get(ci, offset % SWAPFILE_CLUSTER))))
3622+ return -ENOENT;
3623+ if (WARN_ON((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX))
3624+ return -EINVAL;
37043625 }
37053626
37063627 for (i = 0; i < nr; i++) {
37073628 count = si->swap_map[offset + i];
3708− has_cache = count & SWAP_HAS_CACHE;
3709− count &= ~SWAP_HAS_CACHE;
3710−
3711− if (usage == SWAP_HAS_CACHE)
3712− has_cache = SWAP_HAS_CACHE;
3713− else if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
3629+ if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
37143630 count += usage;
37153631 else if (swap_count_continued(si, offset + i, count))
37163632 count = COUNT_CONTINUED;
@@ -3719,65 +3635,55 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) · 2 unmodified lines
37193635 * Don't need to rollback changes, because if
37203636 * usage == 1, there must be nr == 1.
37213637 */
3722− err = -ENOMEM;
3723− goto unlock_out;
3638+ return -ENOMEM;
37243639 }
37253640
3726− WRITE_ONCE(si->swap_map[offset + i], count | has_cache);
3641+ WRITE_ONCE(si->swap_map[offset + i], count);
37273642 }
37283643
3729−unlock_out:
3730− swap_cluster_unlock(ci);
3731− return err;
3644+ return 0;
37323645 }
37333646
3734−/*
3735− * Help swapoff by noting that swap entry belongs to shmem/tmpfs
3736− * (in which case its reference count is never incremented).
3737− */
3738−void swap_shmem_alloc(swp_entry_t entry, int nr)
3647+static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr)
37393648 {
3740− __swap_duplicate(entry, SWAP_MAP_SHMEM, nr);
3649+ int err;
3650+ struct swap_info_struct *si;
3651+ struct swap_cluster_info *ci;
3652+ unsigned long offset = swp_offset(entry);
3653+
3654+ si = swap_entry_to_info(entry);
3655+ if (WARN_ON_ONCE(!si)) {
3656+ pr_err("%s%08lx\n", Bad_file, entry.val);
3657+ return -EINVAL;
3658+ }
3659+
3660+ VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);
3661+ ci = swap_cluster_lock(si, offset);
3662+ err = swap_dup_entries(si, ci, offset, usage, nr);
3663+ swap_cluster_unlock(ci);
3664+ return err;
37413665 }
37423666
37433667 /*
3744− * Increase reference count of swap entry by 1.
3668+ * swap_dup_entry_direct() - Increase reference count of a swap entry by one.
3669+ * @entry: first swap entry from which we want to increase the refcount.
3670+ *
37453671 * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
37463672 * but could not be atomically allocated. Returns 0, just as if it succeeded,
37473673 * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
37483674 * might occur if a page table entry has got corrupted.
3675+ *
3676+ * Context: Caller must ensure there is no race condition on the reference
3677+ * owner. e.g., locking the PTL of a PTE containing the entry being increased.
37493678 */
3750−int swap_duplicate(swp_entry_t entry)
3679+int swap_dup_entry_direct(swp_entry_t entry)
37513680 {
37523681 int err = 0;
3753−
37543682 while (!err && __swap_duplicate(entry, 1, 1) == -ENOMEM)
37553683 err = add_swap_count_continuation(entry, GFP_ATOMIC);
37563684 return err;
37573685 }
37583686
3759−/*
3760− * @entry: first swap entry from which we allocate nr swap cache.
3761− *
3762− * Called when allocating swap cache for existing swap entries,
3763− * This can return error codes. Returns 0 at success.
3764− * -EEXIST means there is a swap cache.
3765− * Note: return code is different from swap_duplicate().
3766− */
3767−int swapcache_prepare(swp_entry_t entry, int nr)
3768−{
3769− return __swap_duplicate(entry, SWAP_HAS_CACHE, nr);
3770−}
3771−
3772−/*
3773− * Caller should ensure entries belong to the same folio so
3774− * the entries won't span cross cluster boundary.
3775− */
3776−void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr)
3777−{
3778− swap_entries_put_cache(si, entry, nr);
3779−}
3780−
37813687 /*
37823688 * add_swap_count_continuation - called when a swap count is duplicated
37833689 * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
@@ -3823,7 +3729,7 @@ int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) · 39 unmodified lines
38233729
38243730 ci = swap_cluster_lock(si, offset);
38253731
3826− count = swap_count(si->swap_map[offset]);
3732+ count = si->swap_map[offset];
38273733
38283734 if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
38293735 /*
@@ -3895,7 +3801,7 @@ outer: · 65 unmodified lines
38953801 * into, carry if so, or else fail until a new continuation page is allocated;
38963802 * when the original swap_map count is decremented from 0 with continuation,
38973803 * borrow from the continuation and report whether it still holds more.
3898− * Called while __swap_duplicate() or caller of swap_entry_put_locked()
3804+ * Called while __swap_duplicate() or caller of swap_put_entry_locked()
38993805 * holds cluster lock.
39003806 */
39013807 static bool swap_count_continued(struct swap_info_struct *si,
mm/tests/lazy_mmu_mode_kunit.c+73−0
@@ -0,0 +1,73 @@
1+// SPDX-License-Identifier: GPL-2.0-only
2+#include <kunit/test.h>
3+#include <linux/pgtable.h>
4+
5+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
6+
7+static void expect_not_active(struct kunit *test)
8+{
9+ KUNIT_EXPECT_FALSE(test, is_lazy_mmu_mode_active());
10+}
11+
12+static void expect_active(struct kunit *test)
13+{
14+ KUNIT_EXPECT_TRUE(test, is_lazy_mmu_mode_active());
15+}
16+
17+static void lazy_mmu_mode_active(struct kunit *test)
18+{
19+ expect_not_active(test);
20+
21+ lazy_mmu_mode_enable();
22+ expect_active(test);
23+
24+ {
25+ /* Nested section */
26+ lazy_mmu_mode_enable();
27+ expect_active(test);
28+
29+ lazy_mmu_mode_disable();
30+ expect_active(test);
31+ }
32+
33+ {
34+ /* Paused section */
35+ lazy_mmu_mode_pause();
36+ expect_not_active(test);
37+
38+ {
39+ /* No effect (paused) */
40+ lazy_mmu_mode_enable();
41+ expect_not_active(test);
42+
43+ lazy_mmu_mode_disable();
44+ expect_not_active(test);
45+
46+ lazy_mmu_mode_pause();
47+ expect_not_active(test);
48+
49+ lazy_mmu_mode_resume();
50+ expect_not_active(test);
51+ }
52+
53+ lazy_mmu_mode_resume();
54+ expect_active(test);
55+ }
56+
57+ lazy_mmu_mode_disable();
58+ expect_not_active(test);
59+}
60+
61+static struct kunit_case lazy_mmu_mode_test_cases[] = {
62+ KUNIT_CASE(lazy_mmu_mode_active),
63+ {}
64+};
65+
66+static struct kunit_suite lazy_mmu_mode_test_suite = {
67+ .name = "lazy_mmu_mode",
68+ .test_cases = lazy_mmu_mode_test_cases,
69+};
70+kunit_test_suite(lazy_mmu_mode_test_suite);
71+
72+MODULE_DESCRIPTION("Tests for the lazy MMU mode");
73+MODULE_LICENSE("GPL");
mm/userfaultfd.c+7−11
@@ -1103,7 +1103,7 @@ static long move_present_ptes(struct mm_struct *mm, · 1102 unmodified lines
11031103 /* It's safe to drop the reference now as the page-table is holding one. */
11041104 folio_put(*first_src_folio);
11051105 *first_src_folio = NULL;
1106− arch_enter_lazy_mmu_mode();
1106+ lazy_mmu_mode_enable();
11071107
11081108 while (true) {
11091109 orig_src_pte = ptep_get_and_clear(mm, src_addr, src_pte);
@@ -1140,7 +1140,7 @@ static long move_present_ptes(struct mm_struct *mm, · 30 unmodified lines
11401140 break;
11411141 }
11421142
1143− arch_leave_lazy_mmu_mode();
1143+ lazy_mmu_mode_disable();
11441144 if (src_addr > src_start)
11451145 flush_tlb_range(src_vma, src_start, src_addr);
11461146
@@ -1190,17 +1190,13 @@ static int move_swap_pte(struct mm_struct *mm, struct vm_area_struct *dst_vma, · 43 unmodified lines
11901190 * Check if the swap entry is cached after acquiring the src_pte
11911191 * lock. Otherwise, we might miss a newly loaded swap cache folio.
11921192 *
1193− * Check swap_map directly to minimize overhead, READ_ONCE is sufficient.
11941193 * We are trying to catch newly added swap cache, the only possible case is
11951194 * when a folio is swapped in and out again staying in swap cache, using the
11961195 * same entry before the PTE check above. The PTL is acquired and released
1197− * twice, each time after updating the swap_map's flag. So holding
1198− * the PTL here ensures we see the updated value. False positive is possible,
1199− * e.g. SWP_SYNCHRONOUS_IO swapin may set the flag without touching the
1200− * cache, or during the tiny synchronization window between swap cache and
1201− * swap_map, but it will be gone very quickly, worst result is retry jitters.
1196+ * twice, each time after updating the swap table. So holding
1197+ * the PTL here ensures we see the updated value.
12021198 */
1203− if (READ_ONCE(si->swap_map[swp_offset(entry)]) & SWAP_HAS_CACHE) {
1199+ if (swap_cache_has_folio(entry)) {
12041200 double_pt_unlock(dst_ptl, src_ptl);
12051201 return -EAGAIN;
12061202 }
@@ -1274,7 +1270,7 @@ retry: · 67 unmodified lines
12741270 * Use the maywrite version to indicate that dst_pte will be modified,
12751271 * since dst_pte needs to be none, the subsequent pte_same() check
12761272 * cannot prevent the dst_pte page from being freed concurrently, so we
1277− * also need to abtain dst_pmdval and recheck pmd_same() later.
1273+ * also need to obtain dst_pmdval and recheck pmd_same() later.
12781274 */
12791275 dst_pte = pte_offset_map_rw_nolock(mm, dst_pmd, dst_addr, &dst_pmdval,
12801276 &dst_ptl);
@@ -1330,7 +1326,7 @@ retry: · 49 unmodified lines
13301326 goto out;
13311327 }
13321328
1333− /* If PTE changed after we locked the folio them start over */
1329+ /* If PTE changed after we locked the folio then start over */
13341330 if (src_folio && unlikely(!pte_same(src_folio_pte, orig_src_pte))) {
13351331 ret = -EAGAIN;
13361332 goto out;
mm/vma.c+8−8
@@ -381,7 +381,7 @@ again: · 380 unmodified lines
381381 fput(vp->file);
382382 }
383383 if (vp->remove->anon_vma)
384− anon_vma_merge(vp->vma, vp->remove);
384+ unlink_anon_vmas(vp->remove);
385385 mm->map_count--;
386386 mpol_put(vma_policy(vp->remove));
387387 if (!vp->remove2)
@@ -530,7 +530,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · 142 unmodified lines
530530 if (err)
531531 goto out_free_vmi;
532532
533− err = anon_vma_clone(new, vma);
533+ err = anon_vma_clone(new, vma, VMA_OP_SPLIT);
534534 if (err)
535535 goto out_free_mpol;
536536
@@ -628,7 +628,7 @@ static int dup_anon_vma(struct vm_area_struct *dst, · 91 unmodified lines
628628
629629 vma_assert_write_locked(dst);
630630 dst->anon_vma = src->anon_vma;
631− ret = anon_vma_clone(dst, src);
631+ ret = anon_vma_clone(dst, src, VMA_OP_MERGE_UNFAULTED);
632632 if (ret)
633633 return ret;
634634
@@ -1901,7 +1901,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, · 1266 unmodified lines
19011901 vma_set_range(new_vma, addr, addr + len, pgoff);
19021902 if (vma_dup_policy(vma, new_vma))
19031903 goto out_free_vma;
1904− if (anon_vma_clone(new_vma, vma))
1904+ if (anon_vma_clone(new_vma, vma, VMA_OP_REMAP))
19051905 goto out_free_mempol;
19061906 if (new_vma->vm_file)
19071907 get_file(new_vma->vm_file);
@@ -2951,10 +2951,10 @@ retry: · 1043 unmodified lines
29512951 return -ENOMEM;
29522952
29532953 /*
2954− * Adjust for the gap first so it doesn't interfere with the
2955− * later alignment. The first step is the minimum needed to
2956− * fulill the start gap, the next steps is the minimum to align
2957− * that. It is the minimum needed to fulill both.
2954+ * Adjust for the gap first so it doesn't interfere with the later
2955+ * alignment. The first step is the minimum needed to fulfill the start
2956+ * gap, the next step is the minimum to align that. It is the minimum
2957+ * needed to fulfill both.
29582958 */
29592959 gap = vma_iter_addr(&vmi) + info->start_gap;
29602960 gap += (info->align_offset - gap) & info->align_mask;
mm/vma.h+4−10
@@ -267,7 +267,7 @@ void unmap_region(struct ma_state *mas, struct vm_area_struct *vma, · 266 unmodified lines
267267 struct vm_area_struct *prev, struct vm_area_struct *next);
268268
269269 /**
270− * vma_modify_flags() - Peform any necessary split/merge in preparation for
270+ * vma_modify_flags() - Perform any necessary split/merge in preparation for
271271 * setting VMA flags to *@vm_flags in the range @start to @end contained within
272272 * @vma.
273273 * @vmi: Valid VMA iterator positioned at @vma.
@@ -295,7 +295,7 @@ __must_check struct vm_area_struct *vma_modify_flags(struct vma_iterator *vmi, · 21 unmodified lines
295295 vm_flags_t *vm_flags_ptr);
296296
297297 /**
298− * vma_modify_name() - Peform any necessary split/merge in preparation for
298+ * vma_modify_name() - Perform any necessary split/merge in preparation for
299299 * setting anonymous VMA name to @new_name in the range @start to @end contained
300300 * within @vma.
301301 * @vmi: Valid VMA iterator positioned at @vma.
@@ -319,7 +319,7 @@ __must_check struct vm_area_struct *vma_modify_name(struct vma_iterator *vmi, · 17 unmodified lines
319319 struct anon_vma_name *new_name);
320320
321321 /**
322− * vma_modify_policy() - Peform any necessary split/merge in preparation for
322+ * vma_modify_policy() - Perform any necessary split/merge in preparation for
323323 * setting NUMA policy to @new_pol in the range @start to @end contained
324324 * within @vma.
325325 * @vmi: Valid VMA iterator positioned at @vma.
@@ -343,7 +343,7 @@ __must_check struct vm_area_struct *vma_modify_policy(struct vma_iterator *vmi, · 17 unmodified lines
343343 struct mempolicy *new_pol);
344344
345345 /**
346− * vma_modify_flags_uffd() - Peform any necessary split/merge in preparation for
346+ * vma_modify_flags_uffd() - Perform any necessary split/merge in preparation for
347347 * setting VMA flags to @vm_flags and UFFD context to @new_ctx in the range
348348 * @start to @end contained within @vma.
349349 * @vmi: Valid VMA iterator positioned at @vma.
@@ -561,12 +561,6 @@ static inline unsigned long vma_iter_end(struct vma_iterator *vmi) · 211 unmodified lines
561561 return vmi->mas.last + 1;
562562 }
563563
564−static inline int vma_iter_bulk_alloc(struct vma_iterator *vmi,
565− unsigned long count)
566−{
567− return mas_expected_entries(&vmi->mas, count);
568−}
569−
570564 static inline
571565 struct vm_area_struct *vma_iter_prev_range(struct vma_iterator *vmi)
572566 {
mm/vmalloc.c+20−6
@@ -108,7 +108,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · 107 unmodified lines
108108 if (!pte)
109109 return -ENOMEM;
110110
111− arch_enter_lazy_mmu_mode();
111+ lazy_mmu_mode_enable();
112112
113113 do {
114114 if (unlikely(!pte_none(ptep_get(pte)))) {
@@ -134,7 +134,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · 19 unmodified lines
134134 pfn++;
135135 } while (pte += PFN_DOWN(size), addr += size, addr != end);
136136
137− arch_leave_lazy_mmu_mode();
137+ lazy_mmu_mode_disable();
138138 *mask |= PGTBL_PTE_MODIFIED;
139139 return 0;
140140 }
@@ -305,6 +305,11 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end, · 164 unmodified lines
305305 int err;
306306 pgtbl_mod_mask mask = 0;
307307
308+ /*
309+ * Might allocate pagetables (for most archs a more precise annotation
310+ * would be might_alloc(GFP_PGTABLE_KERNEL)). Also might shootdown TLB
311+ * (requires IRQs enabled on x86).
312+ */
308313 might_sleep();
309314 BUG_ON(addr >= end);
310315
@@ -366,7 +371,7 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · 55 unmodified lines
366371 unsigned long size = PAGE_SIZE;
367372
368373 pte = pte_offset_kernel(pmd, addr);
369− arch_enter_lazy_mmu_mode();
374+ lazy_mmu_mode_enable();
370375
371376 do {
372377 #ifdef CONFIG_HUGETLB_PAGE
@@ -385,7 +390,7 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · 12 unmodified lines
385390 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
386391 } while (pte += (size >> PAGE_SHIFT), addr += size, addr != end);
387392
388− arch_leave_lazy_mmu_mode();
393+ lazy_mmu_mode_disable();
389394 *mask |= PGTBL_PTE_MODIFIED;
390395 }
391396
@@ -533,7 +538,7 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr, · 141 unmodified lines
533538 if (!pte)
534539 return -ENOMEM;
535540
536− arch_enter_lazy_mmu_mode();
541+ lazy_mmu_mode_enable();
537542
538543 do {
539544 struct page *page = pages[*nr];
@@ -555,7 +560,7 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr, · 15 unmodified lines
555560 (*nr)++;
556561 } while (pte++, addr += PAGE_SIZE, addr != end);
557562
558− arch_leave_lazy_mmu_mode();
563+ lazy_mmu_mode_disable();
559564 *mask |= PGTBL_PTE_MODIFIED;
560565
561566 return err;
@@ -2268,11 +2273,14 @@ decay_va_pool_node(struct vmap_node *vn, bool full_decay) · 1706 unmodified lines
22682273 reclaim_list_global(&decay_list);
22692274 }
22702275
2276+#define KASAN_RELEASE_BATCH_SIZE 32
2277+
22712278 static void
22722279 kasan_release_vmalloc_node(struct vmap_node *vn)
22732280 {
22742281 struct vmap_area *va;
22752282 unsigned long start, end;
2283+ unsigned int batch_count = 0;
22762284
22772285 start = list_first_entry(&vn->purge_list, struct vmap_area, list)->va_start;
22782286 end = list_last_entry(&vn->purge_list, struct vmap_area, list)->va_end;
@@ -2282,6 +2290,11 @@ kasan_release_vmalloc_node(struct vmap_node *vn) · 3 unmodified lines
22822290 kasan_release_vmalloc(va->va_start, va->va_end,
22832291 va->va_start, va->va_end,
22842292 KASAN_VMALLOC_PAGE_RANGE);
2293+
2294+ if (need_resched() || (++batch_count >= KASAN_RELEASE_BATCH_SIZE)) {
2295+ cond_resched();
2296+ batch_count = 0;
2297+ }
22852298 }
22862299
22872300 kasan_release_vmalloc(start, end, start, end, KASAN_VMALLOC_TLB_FLUSH);
@@ -4354,6 +4367,7 @@ need_realloc: · 2066 unmodified lines
43544367
43554368 return n;
43564369 }
4370+EXPORT_SYMBOL(vrealloc_node_align_noprof);
43574371
43584372 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
43594373 #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
mm/vmscan.c+113−108
@@ -63,7 +63,6 @@ · 62 unmodified lines
6363 #include <asm/div64.h>
6464
6565 #include <linux/swapops.h>
66−#include <linux/balloon_compaction.h>
6766 #include <linux/sched/sysctl.h>
6867
6968 #include "internal.h"
@@ -104,13 +103,13 @@ struct scan_control { · 34 unmodified lines
104103 unsigned int force_deactivate:1;
105104 unsigned int skipped_deactivate:1;
106105
107− /* Writepage batching in laptop mode; RECLAIM_WRITE */
106+ /* zone_reclaim_mode, boost reclaim */
108107 unsigned int may_writepage:1;
109108
110− /* Can mapped folios be reclaimed? */
109+ /* zone_reclaim_mode */
111110 unsigned int may_unmap:1;
112111
113− /* Can folios be swapped as part of reclaim? */
112+ /* zome_reclaim_mode, boost reclaim, cgroup restrictions */
114113 unsigned int may_swap:1;
115114
116115 /* Not allow cache_trim_mode to be turned on as part of reclaim? */
@@ -507,7 +506,7 @@ static bool skip_throttle_noprogress(pg_data_t *pgdat) · 390 unmodified lines
507506 * If kswapd is disabled, reschedule if necessary but do not
508507 * throttle as the system is likely near OOM.
509508 */
510− if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES)
509+ if (kswapd_test_hopeless(pgdat))
511510 return true;
512511
513512 /*
@@ -758,10 +757,9 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio, · 244 unmodified lines
758757
759758 if (reclaimed && !mapping_exiting(mapping))
760759 shadow = workingset_eviction(folio, target_memcg);
761− __swap_cache_del_folio(ci, folio, swap, shadow);
762760 memcg1_swapout(folio, swap);
761+ __swap_cache_del_folio(ci, folio, swap, shadow);
763762 swap_cluster_unlock_irq(ci);
764− put_swap_folio(folio, swap);
765763 } else {
766764 void (*free_folio)(struct folio *);
767765
@@ -1063,7 +1061,7 @@ static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask) · 295 unmodified lines
10631061 /*
10641062 * We can "enter_fs" for swap-cache with only __GFP_IO
10651063 * providing this isn't SWP_FS_OPS.
1066− * ->flags can be updated non-atomicially (scan_swap_map_slots),
1064+ * ->flags can be updated non-atomically (scan_swap_map_slots),
10671065 * but that will never affect SWP_FS_OPS, so the data_race
10681066 * is safe.
10691067 */
@@ -1276,58 +1274,58 @@ retry: · 206 unmodified lines
12761274 * Try to allocate it some swap space here.
12771275 * Lazyfree folio could be freed directly
12781276 */
1279− if (folio_test_anon(folio) && folio_test_swapbacked(folio)) {
1280− if (!folio_test_swapcache(folio)) {
1281− if (!(sc->gfp_mask & __GFP_IO))
1282− goto keep_locked;
1283− if (folio_maybe_dma_pinned(folio))
1284− goto keep_locked;
1285− if (folio_test_large(folio)) {
1286− /* cannot split folio, skip it */
1287− if (folio_expected_ref_count(folio) !=
1288− folio_ref_count(folio) - 1)
1289− goto activate_locked;
1290− /*
1291− * Split partially mapped folios right away.
1292− * We can free the unmapped pages without IO.
1293− */
1294− if (data_race(!list_empty(&folio->_deferred_list) &&
1295− folio_test_partially_mapped(folio)) &&
1296− split_folio_to_list(folio, folio_list))
1297− goto activate_locked;
1298− }
1299− if (folio_alloc_swap(folio)) {
1300− int __maybe_unused order = folio_order(folio);
1301−
1302− if (!folio_test_large(folio))
1303− goto activate_locked_split;
1304− /* Fallback to swap normal pages */
1305− if (split_folio_to_list(folio, folio_list))
1306− goto activate_locked;
1307−#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1308− if (nr_pages >= HPAGE_PMD_NR) {
1309− count_memcg_folio_events(folio,
1310− THP_SWPOUT_FALLBACK, 1);
1311− count_vm_event(THP_SWPOUT_FALLBACK);
1312− }
1313−#endif
1314− count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK);
1315− if (folio_alloc_swap(folio))
1316− goto activate_locked_split;
1317− }
1277+ if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
1278+ !folio_test_swapcache(folio)) {
1279+ if (!(sc->gfp_mask & __GFP_IO))
1280+ goto keep_locked;
1281+ if (folio_maybe_dma_pinned(folio))
1282+ goto keep_locked;
1283+ if (folio_test_large(folio)) {
1284+ /* cannot split folio, skip it */
1285+ if (folio_expected_ref_count(folio) !=
1286+ folio_ref_count(folio) - 1)
1287+ goto activate_locked;
13181288 /*
1319− * Normally the folio will be dirtied in unmap because its
1320− * pte should be dirty. A special case is MADV_FREE page. The
1321− * page's pte could have dirty bit cleared but the folio's
1322− * SwapBacked flag is still set because clearing the dirty bit
1323− * and SwapBacked flag has no lock protected. For such folio,
1324− * unmap will not set dirty bit for it, so folio reclaim will
1325− * not write the folio out. This can cause data corruption when
1326− * the folio is swapped in later. Always setting the dirty flag
1327− * for the folio solves the problem.
1289+ * Split partially mapped folios right away.
1290+ * We can free the unmapped pages without IO.
13281291 */
1329− folio_mark_dirty(folio);
1292+ if (data_race(!list_empty(&folio->_deferred_list) &&
1293+ folio_test_partially_mapped(folio)) &&
1294+ split_folio_to_list(folio, folio_list))
1295+ goto activate_locked;
13301296 }
1297+ if (folio_alloc_swap(folio)) {
1298+ int __maybe_unused order = folio_order(folio);
1299+
1300+ if (!folio_test_large(folio))
1301+ goto activate_locked_split;
1302+ /* Fallback to swap normal pages */
1303+ if (split_folio_to_list(folio, folio_list))
1304+ goto activate_locked;
1305+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1306+ if (nr_pages >= HPAGE_PMD_NR) {
1307+ count_memcg_folio_events(folio,
1308+ THP_SWPOUT_FALLBACK, 1);
1309+ count_vm_event(THP_SWPOUT_FALLBACK);
1310+ }
1311+#endif
1312+ count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK);
1313+ if (folio_alloc_swap(folio))
1314+ goto activate_locked_split;
1315+ }
1316+ /*
1317+ * Normally the folio will be dirtied in unmap because
1318+ * its pte should be dirty. A special case is MADV_FREE
1319+ * page. The page's pte could have dirty bit cleared but
1320+ * the folio's SwapBacked flag is still set because
1321+ * clearing the dirty bit and SwapBacked flag has no
1322+ * lock protected. For such folio, unmap will not set
1323+ * dirty bit for it, so folio reclaim will not write the
1324+ * folio out. This can cause data corruption when the
1325+ * folio is swapped in later. Always setting the dirty
1326+ * flag for the folio solves the problem.
1327+ */
1328+ folio_mark_dirty(folio);
13311329 }
13321330
13331331 /*
@@ -2451,9 +2449,9 @@ static inline void calculate_pressure_balance(struct scan_control *sc, · 1117 unmodified lines
24512449 static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
24522450 struct scan_control *sc, unsigned long scan)
24532451 {
2454− unsigned long min, low;
2452+ unsigned long min, low, usage;
24552453
2456− mem_cgroup_protection(sc->target_mem_cgroup, memcg, &min, &low);
2454+ mem_cgroup_protection(sc->target_mem_cgroup, memcg, &min, &low, &usage);
24572455
24582456 if (min || low) {
24592457 /*
@@ -2485,7 +2483,6 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg, · 25 unmodified lines
24852483 * again by how much of the total memory used is under
24862484 * hard protection.
24872485 */
2488− unsigned long cgroup_size = mem_cgroup_size(memcg);
24892486 unsigned long protection;
24902487
24912488 /* memory.low scaling, make sure we retry before OOM */
@@ -2497,9 +2494,9 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg, · 5 unmodified lines
24972494 }
24982495
24992496 /* Avoid TOCTOU with earlier protection check */
2500− cgroup_size = max(cgroup_size, protection);
2497+ usage = max(usage, protection);
25012498
2502− scan -= scan * protection / (cgroup_size + 1);
2499+ scan -= scan * protection / (usage + 1);
25032500
25042501 /*
25052502 * Minimally target SWAP_CLUSTER_MAX pages to keep
@@ -3516,7 +3513,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, · 1010 unmodified lines
35163513 return false;
35173514 }
35183515
3519− arch_enter_lazy_mmu_mode();
3516+ lazy_mmu_mode_enable();
35203517 restart:
35213518 for (i = pte_index(start), addr = start; addr != end; i++, addr += PAGE_SIZE) {
35223519 unsigned long pfn;
@@ -3557,7 +3554,7 @@ restart: · 34 unmodified lines
35573554 if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end))
35583555 goto restart;
35593556
3560− arch_leave_lazy_mmu_mode();
3557+ lazy_mmu_mode_disable();
35613558 pte_unmap_unlock(pte, ptl);
35623559
35633560 return suitable_to_scan(total, young);
@@ -3598,7 +3595,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area · 34 unmodified lines
35983595 if (!spin_trylock(ptl))
35993596 goto done;
36003597
3601− arch_enter_lazy_mmu_mode();
3598+ lazy_mmu_mode_enable();
36023599
36033600 do {
36043601 unsigned long pfn;
@@ -3645,7 +3642,7 @@ next: · 40 unmodified lines
36453642
36463643 walk_update_folio(walk, last, gen, dirty);
36473644
3648− arch_leave_lazy_mmu_mode();
3645+ lazy_mmu_mode_disable();
36493646 spin_unlock(ptl);
36503647 done:
36513648 *first = -1;
@@ -4244,7 +4241,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) · 592 unmodified lines
42444241 }
42454242 }
42464243
4247− arch_enter_lazy_mmu_mode();
4244+ lazy_mmu_mode_enable();
42484245
42494246 pte -= (addr - start) / PAGE_SIZE;
42504247
@@ -4278,7 +4275,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) · 27 unmodified lines
42784275
42794276 walk_update_folio(walk, last, gen, dirty);
42804277
4281− arch_leave_lazy_mmu_mode();
4278+ lazy_mmu_mode_disable();
42824279
42834280 /* feedback from rmap walkers to page table walkers */
42844281 if (mm_state && suitable_to_scan(i, young))
@@ -5067,7 +5064,7 @@ static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control * · 782 unmodified lines
50675064 blk_finish_plug(&plug);
50685065 done:
50695066 if (sc->nr_reclaimed > reclaimed)
5070− atomic_set(&pgdat->kswapd_failures, 0);
5067+ kswapd_try_clear_hopeless(pgdat, sc->order, sc->reclaim_idx);
50715068 }
50725069
50735070 /******************************************************************************
@@ -5417,7 +5414,7 @@ static int lru_gen_seq_show(struct seq_file *m, void *v) · 343 unmodified lines
54175414 if (memcg)
54185415 cgroup_path(memcg->css.cgroup, m->private, PATH_MAX);
54195416 #endif
5420− seq_printf(m, "memcg %5hu %s\n", mem_cgroup_id(memcg), path);
5417+ seq_printf(m, "memcg %llu %s\n", mem_cgroup_id(memcg), path);
54215418 }
54225419
54235420 seq_printf(m, " node %5d\n", nid);
@@ -5502,7 +5499,7 @@ static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_co · 78 unmodified lines
55025499 return -EINTR;
55035500 }
55045501
5505−static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq,
5502+static int run_cmd(char cmd, u64 memcg_id, int nid, unsigned long seq,
55065503 struct scan_control *sc, int swappiness, unsigned long opt)
55075504 {
55085505 struct lruvec *lruvec;
@@ -5513,14 +5510,7 @@ static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq, · 4 unmodified lines
55135510 return -EINVAL;
55145511
55155512 if (!mem_cgroup_disabled()) {
5516− rcu_read_lock();
5517−
5518− memcg = mem_cgroup_from_id(memcg_id);
5519− if (!mem_cgroup_tryget(memcg))
5520− memcg = NULL;
5521−
5522− rcu_read_unlock();
5523−
5513+ memcg = mem_cgroup_get_from_id(memcg_id);
55245514 if (!memcg)
55255515 return -EINVAL;
55265516 }
@@ -5592,7 +5582,7 @@ static ssize_t lru_gen_seq_write(struct file *file, const char __user *src, · 65 unmodified lines
55925582 int n;
55935583 int end;
55945584 char cmd, swap_string[5];
5595− unsigned int memcg_id;
5585+ u64 memcg_id;
55965586 unsigned int nid;
55975587 unsigned long seq;
55985588 unsigned int swappiness;
@@ -5602,7 +5592,7 @@ static ssize_t lru_gen_seq_write(struct file *file, const char __user *src, · 3 unmodified lines
56025592 if (!*cur)
56035593 continue;
56045594
5605− n = sscanf(cur, "%c %u %u %lu %n %4s %n %lu %n", &cmd, &memcg_id, &nid,
5595+ n = sscanf(cur, "%c %llu %u %lu %n %4s %n %lu %n", &cmd, &memcg_id, &nid,
56065596 &seq, &end, swap_string, &end, &opt, &end);
56075597 if (n < 4 || cur[end]) {
56085598 err = -EINVAL;
@@ -6141,7 +6131,7 @@ again: · 532 unmodified lines
61416131 * successful direct reclaim run will revive a dormant kswapd.
61426132 */
61436133 if (reclaimable)
6144− atomic_set(&pgdat->kswapd_failures, 0);
6134+ kswapd_try_clear_hopeless(pgdat, sc->order, sc->reclaim_idx);
61456135 else if (sc->cache_trim_mode)
61466136 sc->cache_trim_mode_failed = 1;
61476137 }
@@ -6366,13 +6356,6 @@ retry: · 218 unmodified lines
63666356
63676357 if (sc->compaction_ready)
63686358 break;
6369−
6370− /*
6371− * If we're getting trouble reclaiming, start doing
6372− * writepage even in laptop mode.
6373− */
6374− if (sc->priority < DEF_PRIORITY - 2)
6375− sc->may_writepage = 1;
63766359 } while (--sc->priority >= 0);
63776360
63786361 last_pgdat = NULL;
@@ -6453,7 +6436,7 @@ static bool allow_direct_reclaim(pg_data_t *pgdat) · 74 unmodified lines
64536436 int i;
64546437 bool wmark_ok;
64556438
6456− if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES)
6439+ if (kswapd_test_hopeless(pgdat))
64576440 return true;
64586441
64596442 for_each_managed_zone_pgdat(zone, pgdat, i, ZONE_NORMAL) {
@@ -6581,7 +6564,7 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order, · 121 unmodified lines
65816564 .order = order,
65826565 .nodemask = nodemask,
65836566 .priority = DEF_PRIORITY,
6584− .may_writepage = !laptop_mode,
6567+ .may_writepage = 1,
65856568 .may_unmap = 1,
65866569 .may_swap = 1,
65876570 };
@@ -6625,7 +6608,7 @@ unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, · 37 unmodified lines
66256608 struct scan_control sc = {
66266609 .nr_to_reclaim = SWAP_CLUSTER_MAX,
66276610 .target_mem_cgroup = memcg,
6628− .may_writepage = !laptop_mode,
6611+ .may_writepage = 1,
66296612 .may_unmap = 1,
66306613 .reclaim_idx = MAX_NR_ZONES - 1,
66316614 .may_swap = !noswap,
@@ -6671,7 +6654,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, · 39 unmodified lines
66716654 .reclaim_idx = MAX_NR_ZONES - 1,
66726655 .target_mem_cgroup = memcg,
66736656 .priority = DEF_PRIORITY,
6674− .may_writepage = !laptop_mode,
6657+ .may_writepage = 1,
66756658 .may_unmap = 1,
66766659 .may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP),
66776660 .proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE),
@@ -6862,7 +6845,7 @@ static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, · 184 unmodified lines
68626845 wake_up_all(&pgdat->pfmemalloc_wait);
68636846
68646847 /* Hopeless node, leave it to direct reclaim */
6865− if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES)
6848+ if (kswapd_test_hopeless(pgdat))

102 more lines aren’t shown here.

mm/vmstat.c+8−22

This diff is large: its 75 lines aren’t shown here.

mm/workingset.c+4−4

This diff is large: its 36 lines aren’t shown here.

mm/zsmalloc.c+125−67

This diff is large: its 357 lines aren’t shown here.

mm/zswap.c+23−35

This diff is large: its 117 lines aren’t shown here.

tools/mm/slabinfo.c+1−1

This diff is large: its 9 lines aren’t shown here.

tools/mm/thp_swap_allocator_test.c+1−1

This diff is large: its 9 lines aren’t shown here.

tools/testing/selftests/damon/access_memory.c+21−8

This diff is large: its 55 lines aren’t shown here.

tools/testing/selftests/damon/sysfs_memcg_path_leak.sh+14−12

This diff is large: its 44 lines aren’t shown here.

tools/testing/selftests/damon/sysfs_update_schemes_tried_regions_wss_estimation.py+32−9

This diff is large: its 72 lines aren’t shown here.

tools/testing/selftests/mm/.gitignore+0−1

This diff is large: its 8 lines aren’t shown here.

tools/testing/selftests/mm/Makefile+34−10

This diff is large: its 85 lines aren’t shown here.

tools/testing/selftests/mm/charge_reserved_hugetlb.sh+32−23

This diff is large: its 98 lines aren’t shown here.

tools/testing/selftests/mm/check_config.sh+1−2

This diff is large: its 10 lines aren’t shown here.

tools/testing/selftests/mm/cow.c+33−10

This diff is large: its 85 lines aren’t shown here.

tools/testing/selftests/mm/hugetlb-madvise.c+1−8

This diff is large: its 16 lines aren’t shown here.

tools/testing/selftests/mm/ksft_compaction.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_cow.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_gup_test.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_hmm.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_hugetlb.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_hugevm.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_ksm.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_ksm_numa.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_madv_guard.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_madv_populate.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_mdwe.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_memfd_secret.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_migration.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_mkdirty.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_mlock.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_mmap.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_mremap.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_page_frag.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_pagemap.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_pfnmap.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_pkey.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_process_madv.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_process_mrelease.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_rmap.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_soft_dirty.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_thp.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_userfaultfd.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_vma_merge.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/ksft_vmalloc.sh+4−0

This diff is large: its 5 lines aren’t shown here.

tools/testing/selftests/mm/page_frag/Makefile+1−1

This diff is large: its 7 lines aren’t shown here.

tools/testing/selftests/mm/pagemap_ioctl.c+7−8

This diff is large: its 56 lines aren’t shown here.

tools/testing/selftests/mm/pfnmap.c+56−37

This diff is large: its 162 lines aren’t shown here.

tools/testing/selftests/mm/run_vmtests.sh+4−20

This diff is large: its 40 lines aren’t shown here.

tools/testing/selftests/mm/split_huge_page_test.c+1−5

This diff is large: its 13 lines aren’t shown here.

tools/testing/selftests/mm/test_vmalloc.sh+28−3

This diff is large: its 71 lines aren’t shown here.

tools/testing/selftests/mm/va_high_addr_switch.c+7−3

This diff is large: its 22 lines aren’t shown here.

tools/testing/selftests/mm/va_high_addr_switch.sh+7−5

This diff is large: its 25 lines aren’t shown here.

tools/testing/selftests/mm/virtual_address_range.c+0−260

This diff is large: its 261 lines aren’t shown here.

tools/testing/selftests/mm/vm_util.h+7−0

This diff is large: its 14 lines aren’t shown here.

tools/testing/selftests/mm/write_to_hugetlbfs.c+6−3

This diff is large: its 30 lines aren’t shown here.

tools/testing/vma/vma_internal.h+10−6

This diff is large: its 37 lines aren’t shown here.