History

Merge branch 'akpm' (patches from Andrew)

Merge updates from Andrew Morton: - a few misc bits - ocfs2 - most(?) of MM * emailed patches from Andrew Morton <akpm@linux-foundation.org>: (125 commits) thp: fix comments of __pmd_trans_huge_lock() cgroup: remove unnecessary 0 check from css_from_id() cgroup: fix idr leak for the first cgroup root mm: memcontrol: fix documentation for compound parameter mm: memcontrol: remove BUG_ON in uncharge_list mm: fix build warnings in <linux/compaction.h> mm, thp: convert from optimistic swapin collapsing to conservative mm, thp: fix comment inconsistency for swapin readahead functions thp: update Documentation/{vm/transhuge,filesystems/proc}.txt shmem: split huge pages beyond i_size under memory pressure thp: introduce CONFIG_TRANSPARENT_HUGE_PAGECACHE khugepaged: add support of collapse for tmpfs/shmem pages shmem: make shmem_inode_info::lock irq-safe khugepaged: move up_read(mmap_sem) out of khugepaged_alloc_page() thp: extract khugepaged from mm/huge_memory.c shmem, thp: respect MADV_{NO,}HUGEPAGE for file mappings shmem: add huge pages support shmem: get_unmapped_area align huge page shmem: prepare huge= mount option and sysfs knob mm, rmap: account shmem thp pages ...

Linus Torvalds committed 10 years ago

Commit 0e06f5c2 parents f7816ad 8f19b0c186 files · +7414 −4185Browse files

Unified · Split

186 files
  1. Documentation/blockdev/zram.txt +46 −36
  2. Documentation/filesystems/Locking +9 −5
  3. Documentation/filesystems/dax.txt +4 −2
  4. Documentation/filesystems/proc.txt +9 −0
  5. Documentation/filesystems/vfs.txt +11 −0
  6. Documentation/vm/page_migration +107 −1
  7. Documentation/vm/transhuge.txt +92 −36
  8. Documentation/vm/unevictable-lru.txt +21 −0
  9. Makefile +42 −27
  10. arch/alpha/mm/fault.c +1 −1
  11. arch/arc/mm/fault.c +1 −1
  12. arch/arm/include/asm/pgalloc.h +1 −1
  13. arch/arm/include/asm/tlb.h +25 −4
  14. arch/arm/mm/fault.c +1 −1
  15. arch/arm/mm/pgd.c +1 −1
  16. arch/arm64/mm/fault.c +1 −1
  17. arch/avr32/mm/fault.c +1 −1
  18. arch/cris/mm/fault.c +1 −1
  19. arch/frv/mm/fault.c +1 −1
  20. arch/hexagon/mm/vm_fault.c +1 −1
  21. arch/ia64/include/asm/tlb.h +26 −5
  22. arch/ia64/mm/fault.c +1 −1
  23. arch/m32r/kernel/m32r_ksyms.c +3 −0
  24. arch/m32r/lib/Makefile +2 −2
  25. arch/m32r/lib/libgcc.h +23 −0
  26. arch/m32r/lib/ucmpdi2.c +17 −0
  27. arch/m32r/mm/fault.c +1 −1
  28. arch/m68k/mm/fault.c +1 −1
  29. arch/metag/mm/fault.c +1 −1
  30. arch/microblaze/mm/fault.c +1 −1
  31. arch/mips/mm/fault.c +1 −1
  32. arch/mn10300/mm/fault.c +1 −1
  33. arch/nios2/mm/fault.c +1 −1
  34. arch/openrisc/mm/fault.c +1 −1
  35. arch/parisc/mm/fault.c +1 −1
  36. arch/powerpc/include/asm/pgtable.h +2 −4
  37. arch/powerpc/mm/copro_fault.c +1 −1
  38. arch/powerpc/mm/fault.c +1 −1
  39. arch/s390/include/asm/tlb.h +20 −2
  40. arch/s390/mm/fault.c +1 −1
  41. arch/score/mm/fault.c +1 −1
  42. arch/sh/include/asm/tlb.h +19 −1
  43. arch/sh/mm/fault.c +1 −1
  44. arch/sparc/mm/fault_32.c +2 −2
  45. arch/sparc/mm/fault_64.c +1 −1
  46. arch/tile/mm/fault.c +1 −1
  47. arch/um/include/asm/tlb.h +19 −1
  48. arch/um/kernel/trap.c +1 −1
  49. arch/unicore32/mm/fault.c +1 −1
  50. arch/x86/Makefile +0 −8
  51. arch/x86/include/asm/pgalloc.h +10 −2
  52. arch/x86/mm/fault.c +1 −1
  53. arch/x86/mm/pgtable.c +7 −3
  54. arch/xtensa/mm/fault.c +1 −1
  55. drivers/base/memory.c +17 −11
  56. drivers/base/node.c +9 −4
  57. drivers/block/zram/Kconfig +2 −13
  58. drivers/block/zram/Makefile +1 −3
  59. drivers/block/zram/zcomp.c +95 −55
  60. drivers/block/zram/zcomp.h +9 −27
  61. drivers/block/zram/zcomp_lz4.c +0 −56
  62. drivers/block/zram/zcomp_lz4.h +0 −17
  63. drivers/block/zram/zcomp_lzo.c +0 −56
  64. drivers/block/zram/zcomp_lzo.h +0 −17
  65. drivers/block/zram/zram_drv.c +28 −20
  66. drivers/block/zram/zram_drv.h +3 −2
  67. drivers/char/mem.c +24 −0
  68. drivers/iommu/amd_iommu_v2.c +1 −2
  69. drivers/iommu/intel-svm.c +1 −1
  70. drivers/tty/sysrq.c +1 −0
  71. drivers/video/fbdev/core/fbmon.c +0 −1
  72. drivers/virtio/virtio_balloon.c +47 −5
  73. drivers/xen/xen-selfballoon.c +2 −2
  74. fs/btrfs/extent_io.c +2 −1
  75. fs/cifs/file.c +1 −1
  76. fs/dax.c +14 −59
  77. fs/ext2/file.c +2 −2
  78. fs/ext4/file.c +2 −2
  79. fs/ext4/readpage.c +1 −1
  80. fs/f2fs/data.c +2 −1
  81. fs/fs-writeback.c +86 −25
  82. fs/inode.c +2 −0
  83. fs/mpage.c +3 −1
  84. fs/ocfs2/cluster/tcp.c +2 −6
  85. fs/ocfs2/dlm/dlmdebug.c +2 −24
  86. fs/ocfs2/dlm/dlmdebug.h +0 −1
  87. fs/ocfs2/dlmglue.c +1 −12
  88. fs/ocfs2/inode.h +0 −7
  89. fs/ocfs2/journal.c +21 −20
  90. fs/ocfs2/stackglue.c +0 −2
  91. fs/ocfs2/super.c +0 −1
  92. fs/orangefs/inode.c +1 −1
  93. fs/pipe.c +26 −6
  94. fs/proc/meminfo.c +5 −2
  95. fs/proc/task_mmu.c +9 −1
  96. fs/super.c +2 −0
  97. fs/userfaultfd.c +11 −11
  98. fs/xfs/xfs_file.c +3 −3
  99. include/asm-generic/tlb.h +48 −11
  100. include/linux/balloon_compaction.h +15 −36
  101. include/linux/compaction.h +1 −0
  102. include/linux/dax.h +0 −5
  103. include/linux/debugobjects.h +1 −1
  104. include/linux/frontswap.h +20 −14
  105. include/linux/fs.h +6 −0
  106. include/linux/gfp.h +1 −9
  107. include/linux/huge_mm.h +21 −15
  108. include/linux/khugepaged.h +5 −0
  109. include/linux/ksm.h +1 −2
  110. include/linux/memblock.h +9 −11
  111. include/linux/memcontrol.h +7 −96
  112. include/linux/memory_hotplug.h +2 −0
  113. include/linux/migrate.h +17 −0
  114. include/linux/mm.h +39 −14
  115. include/linux/mm_types.h +37 −36
  116. include/linux/mmdebug.h +2 −0
  117. include/linux/mmzone.h +6 −2
  118. include/linux/oom.h +6 −4
  119. include/linux/page-flags.h +75 −53
  120. include/linux/page_ext.h +2 −2
  121. include/linux/page_owner.h +5 −7
  122. include/linux/pagemap.h +3 −3
  123. include/linux/radix-tree.h +1 −0
  124. include/linux/rmap.h +1 −1
  125. include/linux/shmem_fs.h +43 −2
  126. include/linux/slab.h +2 −0
  127. include/linux/slab_def.h +1 −1
  128. include/linux/slub_def.h +5 −0
  129. include/linux/userfaultfd_k.h +2 −6
  130. include/linux/vm_event_item.h +7 −0
  131. include/linux/writeback.h +3 −0
  132. include/trace/events/huge_memory.h +41 −9
  133. include/trace/events/writeback.h +18 −4
  134. include/uapi/linux/magic.h +2 −0
  135. init/Kconfig +2 −2
  136. ipc/shm.c +6 −4
  137. kernel/fork.c +3 −3
  138. lib/Kconfig.debug +1 −0
  139. lib/dma-debug.c +2 −0
  140. lib/radix-tree.c +79 −5
  141. mm/Kconfig +8 −0
  142. mm/Makefile +1 −1
  143. mm/balloon_compaction.c +17 −77
  144. mm/compaction.c +91 −32
  145. mm/filemap.c +146 −71
  146. mm/frontswap.c +15 −20
  147. mm/gup.c +6 −3
  148. mm/huge_memory.c +366 −1645
  149. mm/hugetlb.c +21 −33
  150. mm/internal.h +4 −0
  151. mm/khugepaged.c +1922 −0
  152. mm/ksm.c +4 −5
  153. mm/memblock.c +3 −0
  154. mm/memcontrol.c +89 −45
  155. mm/memory.c +505 −380
  156. mm/memory_hotplug.c +58 −12
  157. mm/mempolicy.c +3 −1
  158. mm/migrate.c +209 −53
  159. mm/mmap.c +22 −4
  160. mm/mprotect.c +1 −1
  161. mm/mremap.c +1 −2
  162. mm/nommu.c +2 −1
  163. mm/oom_kill.c +37 −28
  164. mm/page-writeback.c +19 −0
  165. mm/page_alloc.c +62 −102
  166. mm/page_isolation.c +7 −6
  167. mm/page_owner.c +131 −26
  168. mm/readahead.c +6 −7
  169. mm/rmap.c +57 −21
  170. mm/shmem.c +824 −94
  171. mm/slab.c +26 −64
  172. mm/slab.h +26 −4
  173. mm/slab_common.c +48 −1
  174. mm/slub.c +133 −12
  175. mm/swap.c +2 −0
  176. mm/swapfile.c +1 −1
  177. mm/truncate.c +23 −5
  178. mm/util.c +10 −2
  179. mm/vmalloc.c +3 −3
  180. mm/vmscan.c +7 −1
  181. mm/vmstat.c +7 −1
  182. mm/zsmalloc.c +945 −405
  183. net/unix/af_unix.c +1 −0
  184. scripts/bloat-o-meter +2 −2
  185. scripts/tags.sh +3 −0
  186. tools/vm/page_owner_sort.c +7 −2
Documentation/blockdev/zram.txt+46−36
@@ -59,23 +59,23 @@ num_devices parameter is optional and tells zram how many devices should be · 58 unmodified lines
5959 pre-created. Default: 1.
6060
6161 2) Set max number of compression streams
62− Regardless the value passed to this attribute, ZRAM will always
63− allocate multiple compression streams - one per online CPUs - thus
64− allowing several concurrent compression operations. The number of
65− allocated compression streams goes down when some of the CPUs
66− become offline. There is no single-compression-stream mode anymore,
67− unless you are running a UP system or has only 1 CPU online.
68−
69− To find out how many streams are currently available:
62+Regardless the value passed to this attribute, ZRAM will always
63+allocate multiple compression streams - one per online CPUs - thus
64+allowing several concurrent compression operations. The number of
65+allocated compression streams goes down when some of the CPUs
66+become offline. There is no single-compression-stream mode anymore,
67+unless you are running a UP system or has only 1 CPU online.
68+
69+To find out how many streams are currently available:
7070 cat /sys/block/zram0/max_comp_streams
7171
7272 3) Select compression algorithm
73− Using comp_algorithm device attribute one can see available and
74− currently selected (shown in square brackets) compression algorithms,
75− change selected compression algorithm (once the device is initialised
76− there is no way to change compression algorithm).
73+Using comp_algorithm device attribute one can see available and
74+currently selected (shown in square brackets) compression algorithms,
75+change selected compression algorithm (once the device is initialised
76+there is no way to change compression algorithm).
7777
78− Examples:
78+Examples:
7979 #show supported compression algorithms
8080 cat /sys/block/zram0/comp_algorithm
8181 lzo [lz4]
@@ -83,17 +83,27 @@ pre-created. Default: 1. · 1 unmodified line
8383 #select lzo compression algorithm
8484 echo lzo > /sys/block/zram0/comp_algorithm
8585
86+For the time being, the `comp_algorithm' content does not necessarily
87+show every compression algorithm supported by the kernel. We keep this
88+list primarily to simplify device configuration and one can configure
89+a new device with a compression algorithm that is not listed in
90+`comp_algorithm'. The thing is that, internally, ZRAM uses Crypto API
91+and, if some of the algorithms were built as modules, it's impossible
92+to list all of them using, for instance, /proc/crypto or any other
93+method. This, however, has an advantage of permitting the usage of
94+custom crypto compression modules (implementing S/W or H/W compression).
95+
8696 4) Set Disksize
87− Set disk size by writing the value to sysfs node 'disksize'.
88− The value can be either in bytes or you can use mem suffixes.
89− Examples:
90− # Initialize /dev/zram0 with 50MB disksize
91− echo $((50*1024*1024)) > /sys/block/zram0/disksize
97+Set disk size by writing the value to sysfs node 'disksize'.
98+The value can be either in bytes or you can use mem suffixes.
99+Examples:
100+ # Initialize /dev/zram0 with 50MB disksize
101+ echo $((50*1024*1024)) > /sys/block/zram0/disksize
92102
93− # Using mem suffixes
94− echo 256K > /sys/block/zram0/disksize
95− echo 512M > /sys/block/zram0/disksize
96− echo 1G > /sys/block/zram0/disksize
103+ # Using mem suffixes
104+ echo 256K > /sys/block/zram0/disksize
105+ echo 512M > /sys/block/zram0/disksize
106+ echo 1G > /sys/block/zram0/disksize
97107
98108 Note:
99109 There is little point creating a zram of greater than twice the size of memory
@@ -101,20 +111,20 @@ since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the · 1 unmodified line
101111 size of the disk when not in use so a huge zram is wasteful.
102112
103113 5) Set memory limit: Optional
104− Set memory limit by writing the value to sysfs node 'mem_limit'.
105− The value can be either in bytes or you can use mem suffixes.
106− In addition, you could change the value in runtime.
107− Examples:
108− # limit /dev/zram0 with 50MB memory
109− echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
110−
111− # Using mem suffixes
112− echo 256K > /sys/block/zram0/mem_limit
113− echo 512M > /sys/block/zram0/mem_limit
114− echo 1G > /sys/block/zram0/mem_limit
115−
116− # To disable memory limit
117− echo 0 > /sys/block/zram0/mem_limit
114+Set memory limit by writing the value to sysfs node 'mem_limit'.
115+The value can be either in bytes or you can use mem suffixes.
116+In addition, you could change the value in runtime.
117+Examples:
118+ # limit /dev/zram0 with 50MB memory
119+ echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
120+
121+ # Using mem suffixes
122+ echo 256K > /sys/block/zram0/mem_limit
123+ echo 512M > /sys/block/zram0/mem_limit
124+ echo 1G > /sys/block/zram0/mem_limit
125+
126+ # To disable memory limit
127+ echo 0 > /sys/block/zram0/mem_limit
118128
119129 6) Activate:
120130 mkswap /dev/zram0
Documentation/filesystems/Locking+9−5
@@ -195,7 +195,9 @@ prototypes: · 194 unmodified lines
195195 int (*releasepage) (struct page *, int);
196196 void (*freepage)(struct page *);
197197 int (*direct_IO)(struct kiocb *, struct iov_iter *iter);
198+ bool (*isolate_page) (struct page *, isolate_mode_t);
198199 int (*migratepage)(struct address_space *, struct page *, struct page *);
200+ void (*putback_page) (struct page *);
199201 int (*launder_page)(struct page *);
200202 int (*is_partially_uptodate)(struct page *, unsigned long, unsigned long);
201203 int (*error_remove_page)(struct address_space *, struct page *);
@@ -219,7 +221,9 @@ invalidatepage: yes · 17 unmodified lines
219221 releasepage: yes
220222 freepage: yes
221223 direct_IO:
224+isolate_page: yes
222225 migratepage: yes (both)
226+putback_page: yes
223227 launder_page: yes
224228 is_partially_uptodate: yes
225229 error_remove_page: yes
@@ -544,13 +548,13 @@ subsequent truncate), and then return with VM_FAULT_LOCKED, and the page · 318 unmodified lines
544548 locked. The VM will unlock the page.
545549
546550 ->map_pages() is called when VM asks to map easy accessible pages.
547−Filesystem should find and map pages associated with offsets from "pgoff"
548−till "max_pgoff". ->map_pages() is called with page table locked and must
551+Filesystem should find and map pages associated with offsets from "start_pgoff"
552+till "end_pgoff". ->map_pages() is called with page table locked and must
549553 not block. If it's not possible to reach a page without blocking,
550554 filesystem should skip it. Filesystem should use do_set_pte() to setup
551−page table entry. Pointer to entry associated with offset "pgoff" is
552−passed in "pte" field in vm_fault structure. Pointers to entries for other
553−offsets should be calculated relative to "pte".
555+page table entry. Pointer to entry associated with the page is passed in
556+"pte" field in fault_env structure. Pointers to entries for other offsets
557+should be calculated relative to "pte".
554558
555559 ->page_mkwrite() is called when a previously read-only pte is
556560 about to become writeable. The filesystem again must ensure that there are
Documentation/filesystems/dax.txt+4−2
@@ -49,6 +49,7 @@ These block devices may be used for inspiration: · 48 unmodified lines
4949 - axonram: Axon DDR2 device driver
5050 - brd: RAM backed block device driver
5151 - dcssblk: s390 dcss block device driver
52+- pmem: NVDIMM persistent memory driver
5253
5354
5455 Implementation Tips for Filesystem Writers
@@ -75,8 +76,9 @@ calls to get_block() (for example by a page-fault racing with a read() · 20 unmodified lines
7576 or a write()) work correctly.
7677
7778 These filesystems may be used for inspiration:
78−- ext2: the second extended filesystem, see Documentation/filesystems/ext2.txt
79−- ext4: the fourth extended filesystem, see Documentation/filesystems/ext4.txt
79+- ext2: see Documentation/filesystems/ext2.txt
80+- ext4: see Documentation/filesystems/ext4.txt
81+- xfs: see Documentation/filesystems/xfs.txt
8082
8183
8284 Handling Media Errors
Documentation/filesystems/proc.txt+9−0
@@ -436,6 +436,7 @@ Private_Dirty: 0 kB · 435 unmodified lines
436436 Referenced: 892 kB
437437 Anonymous: 0 kB
438438 AnonHugePages: 0 kB
439+ShmemPmdMapped: 0 kB
439440 Shared_Hugetlb: 0 kB
440441 Private_Hugetlb: 0 kB
441442 Swap: 0 kB
@@ -464,6 +465,8 @@ accessed. · 22 unmodified lines
464465 a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE
465466 and a page is modified, the file page is replaced by a private anonymous copy.
466467 "AnonHugePages" shows the ammount of memory backed by transparent hugepage.
468+"ShmemPmdMapped" shows the ammount of shared (shmem/tmpfs) memory backed by
469+huge pages.
467470 "Shared_Hugetlb" and "Private_Hugetlb" show the ammounts of memory backed by
468471 hugetlbfs page which is *not* counted in "RSS" or "PSS" field for historical
469472 reasons. And these are not included in {Shared,Private}_{Clean,Dirty} field.
@@ -868,6 +871,9 @@ VmallocTotal: 112216 kB · 398 unmodified lines
868871 VmallocUsed: 428 kB
869872 VmallocChunk: 111088 kB
870873 AnonHugePages: 49152 kB
874+ShmemHugePages: 0 kB
875+ShmemPmdMapped: 0 kB
876+
871877
872878 MemTotal: Total usable ram (i.e. physical ram minus a few reserved
873879 bits and the kernel binary code)
@@ -912,6 +918,9 @@ MemAvailable: An estimate of how much memory is available for starting new · 38 unmodified lines
912918 AnonHugePages: Non-file backed huge pages mapped into userspace page tables
913919 Mapped: files which have been mmaped, such as libraries
914920 Shmem: Total memory used by shared memory (shmem) and tmpfs
921+ShmemHugePages: Memory used by shared memory (shmem) and tmpfs allocated
922+ with huge pages
923+ShmemPmdMapped: Shared memory mapped into userspace with huge pages
915924 Slab: in-kernel data structures cache
916925 SReclaimable: Part of Slab, that might be reclaimed, such as caches
917926 SUnreclaim: Part of Slab, that cannot be reclaimed on memory pressure
Documentation/filesystems/vfs.txt+11−0
@@ -592,9 +592,14 @@ struct address_space_operations { · 591 unmodified lines
592592 int (*releasepage) (struct page *, int);
593593 void (*freepage)(struct page *);
594594 ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
595+ /* isolate a page for migration */
596+ bool (*isolate_page) (struct page *, isolate_mode_t);
595597 /* migrate the contents of a page to the specified target */
596598 int (*migratepage) (struct page *, struct page *);
599+ /* put migration-failed page back to right list */
600+ void (*putback_page) (struct page *);
597601 int (*launder_page) (struct page *);
602+
598603 int (*is_partially_uptodate) (struct page *, unsigned long,
599604 unsigned long);
600605 void (*is_dirty_writeback) (struct page *, bool *, bool *);
@@ -747,6 +752,10 @@ struct address_space_operations { · 146 unmodified lines
747752 and transfer data directly between the storage and the
748753 application's address space.
749754
755+ isolate_page: Called by the VM when isolating a movable non-lru page.
756+ If page is successfully isolated, VM marks the page as PG_isolated
757+ via __SetPageIsolated.
758+
750759 migrate_page: This is used to compact the physical memory usage.
751760 If the VM wants to relocate a page (maybe off a memory card
752761 that is signalling imminent failure) it will pass a new page
@@ -754,6 +763,8 @@ struct address_space_operations { · 1 unmodified line
754763 transfer any private data across and update any references
755764 that it has to the page.
756765
766+ putback_page: Called by the VM when isolated page's migration fails.
767+
757768 launder_page: Called before freeing a page - it writes back the dirty page. To
758769 prevent redirtying the page, it is kept locked during the whole
759770 operation.
Documentation/vm/page_migration+107−1
@@ -142,5 +142,111 @@ Steps: · 141 unmodified lines
142142 20. The new page is moved to the LRU and can be scanned by the swapper
143143 etc again.
144144
145−Christoph Lameter, May 8, 2006.
145+C. Non-LRU page migration
146+-------------------------
147+
148+Although original migration aimed for reducing the latency of memory access
149+for NUMA, compaction who want to create high-order page is also main customer.
150+
151+Current problem of the implementation is that it is designed to migrate only
152+*LRU* pages. However, there are potential non-lru pages which can be migrated
153+in drivers, for example, zsmalloc, virtio-balloon pages.
154+
155+For virtio-balloon pages, some parts of migration code path have been hooked
156+up and added virtio-balloon specific functions to intercept migration logics.
157+It's too specific to a driver so other drivers who want to make their pages
158+movable would have to add own specific hooks in migration path.
159+
160+To overclome the problem, VM supports non-LRU page migration which provides
161+generic functions for non-LRU movable pages without driver specific hooks
162+migration path.
163+
164+If a driver want to make own pages movable, it should define three functions
165+which are function pointers of struct address_space_operations.
166+
167+1. bool (*isolate_page) (struct page *page, isolate_mode_t mode);
168+
169+What VM expects on isolate_page function of driver is to return *true*
170+if driver isolates page successfully. On returing true, VM marks the page
171+as PG_isolated so concurrent isolation in several CPUs skip the page
172+for isolation. If a driver cannot isolate the page, it should return *false*.
173+
174+Once page is successfully isolated, VM uses page.lru fields so driver
175+shouldn't expect to preserve values in that fields.
176+
177+2. int (*migratepage) (struct address_space *mapping,
178+ struct page *newpage, struct page *oldpage, enum migrate_mode);
179+
180+After isolation, VM calls migratepage of driver with isolated page.
181+The function of migratepage is to move content of the old page to new page
182+and set up fields of struct page newpage. Keep in mind that you should
183+indicate to the VM the oldpage is no longer movable via __ClearPageMovable()
184+under page_lock if you migrated the oldpage successfully and returns
185+MIGRATEPAGE_SUCCESS. If driver cannot migrate the page at the moment, driver
186+can return -EAGAIN. On -EAGAIN, VM will retry page migration in a short time
187+because VM interprets -EAGAIN as "temporal migration failure". On returning
188+any error except -EAGAIN, VM will give up the page migration without retrying
189+in this time.
190+
191+Driver shouldn't touch page.lru field VM using in the functions.
192+
193+3. void (*putback_page)(struct page *);
194+
195+If migration fails on isolated page, VM should return the isolated page
196+to the driver so VM calls driver's putback_page with migration failed page.
197+In this function, driver should put the isolated page back to the own data
198+structure.
146199
200+4. non-lru movable page flags
201+
202+There are two page flags for supporting non-lru movable page.
203+
204+* PG_movable
205+
206+Driver should use the below function to make page movable under page_lock.
207+
208+ void __SetPageMovable(struct page *page, struct address_space *mapping)
209+
210+It needs argument of address_space for registering migration family functions
211+which will be called by VM. Exactly speaking, PG_movable is not a real flag of
212+struct page. Rather than, VM reuses page->mapping's lower bits to represent it.
213+
214+ #define PAGE_MAPPING_MOVABLE 0x2
215+ page->mapping = page->mapping | PAGE_MAPPING_MOVABLE;
216+
217+so driver shouldn't access page->mapping directly. Instead, driver should
218+use page_mapping which mask off the low two bits of page->mapping under
219+page lock so it can get right struct address_space.
220+
221+For testing of non-lru movable page, VM supports __PageMovable function.
222+However, it doesn't guarantee to identify non-lru movable page because
223+page->mapping field is unified with other variables in struct page.
224+As well, if driver releases the page after isolation by VM, page->mapping
225+doesn't have stable value although it has PAGE_MAPPING_MOVABLE
226+(Look at __ClearPageMovable). But __PageMovable is cheap to catch whether
227+page is LRU or non-lru movable once the page has been isolated. Because
228+LRU pages never can have PAGE_MAPPING_MOVABLE in page->mapping. It is also
229+good for just peeking to test non-lru movable pages before more expensive
230+checking with lock_page in pfn scanning to select victim.
231+
232+For guaranteeing non-lru movable page, VM provides PageMovable function.
233+Unlike __PageMovable, PageMovable functions validates page->mapping and
234+mapping->a_ops->isolate_page under lock_page. The lock_page prevents sudden
235+destroying of page->mapping.
236+
237+Driver using __SetPageMovable should clear the flag via __ClearMovablePage
238+under page_lock before the releasing the page.
239+
240+* PG_isolated
241+
242+To prevent concurrent isolation among several CPUs, VM marks isolated page
243+as PG_isolated under lock_page. So if a CPU encounters PG_isolated non-lru
244+movable page, it can skip it. Driver doesn't need to manipulate the flag
245+because VM will set/clear it automatically. Keep in mind that if driver
246+sees PG_isolated page, it means the page have been isolated by VM so it
247+shouldn't touch page.lru field.
248+PG_isolated is alias with PG_reclaim flag so driver shouldn't use the flag
249+for own purpose.
250+
251+Christoph Lameter, May 8, 2006.
252+Minchan Kim, Mar 28, 2016.
Documentation/vm/transhuge.txt+92−36
@@ -9,8 +9,8 @@ using huge pages for the backing of virtual memory with huge pages · 8 unmodified lines
99 that supports the automatic promotion and demotion of page sizes and
1010 without the shortcomings of hugetlbfs.
1111
12−Currently it only works for anonymous memory mappings but in the
13−future it can expand over the pagecache layer starting with tmpfs.
12+Currently it only works for anonymous memory mappings and tmpfs/shmem.
13+But in the future it can expand to other filesystems.
1414
1515 The reason applications are running faster is because of two
1616 factors. The first factor is almost completely irrelevant and it's not
@@ -57,10 +57,6 @@ miss is going to run faster. · 40 unmodified lines
5757 feature that applies to all dynamic high order allocations in the
5858 kernel)
5959
60−- this initial support only offers the feature in the anonymous memory
61− regions but it'd be ideal to move it to tmpfs and the pagecache
62− later
63−
6460 Transparent Hugepage Support maximizes the usefulness of free memory
6561 if compared to the reservation approach of hugetlbfs by allowing all
6662 unused memory to be used as cache or other movable (or even unmovable
@@ -94,21 +90,21 @@ madvise(MADV_HUGEPAGE) on their critical mmapped regions. · 27 unmodified lines
9490
9591 == sysfs ==
9692
97−Transparent Hugepage Support can be entirely disabled (mostly for
98−debugging purposes) or only enabled inside MADV_HUGEPAGE regions (to
99−avoid the risk of consuming more memory resources) or enabled system
100−wide. This can be achieved with one of:
93+Transparent Hugepage Support for anonymous memory can be entirely disabled
94+(mostly for debugging purposes) or only enabled inside MADV_HUGEPAGE
95+regions (to avoid the risk of consuming more memory resources) or enabled
96+system wide. This can be achieved with one of:
10197
10298 echo always >/sys/kernel/mm/transparent_hugepage/enabled
10399 echo madvise >/sys/kernel/mm/transparent_hugepage/enabled
104100 echo never >/sys/kernel/mm/transparent_hugepage/enabled
105101
106102 It's also possible to limit defrag efforts in the VM to generate
107−hugepages in case they're not immediately free to madvise regions or
108−to never try to defrag memory and simply fallback to regular pages
109−unless hugepages are immediately available. Clearly if we spend CPU
110−time to defrag memory, we would expect to gain even more by the fact
111−we use hugepages later instead of regular pages. This isn't always
103+anonymous hugepages in case they're not immediately free to madvise
104+regions or to never try to defrag memory and simply fallback to regular
105+pages unless hugepages are immediately available. Clearly if we spend CPU
106+time to defrag memory, we would expect to gain even more by the fact we
107+use hugepages later instead of regular pages. This isn't always
112108 guaranteed, but it may be more likely in case the allocation is for a
113109 MADV_HUGEPAGE region.
114110
@@ -133,9 +129,9 @@ that are have used madvise(MADV_HUGEPAGE). This is the default behaviour. · 18 unmodified lines
133129
134130 "never" should be self-explanatory.
135131
136−By default kernel tries to use huge zero page on read page fault.
137−It's possible to disable huge zero page by writing 0 or enable it
138−back by writing 1:
132+By default kernel tries to use huge zero page on read page fault to
133+anonymous mapping. It's possible to disable huge zero page by writing 0
134+or enable it back by writing 1:
139135
140136 echo 0 >/sys/kernel/mm/transparent_hugepage/use_zero_page
141137 echo 1 >/sys/kernel/mm/transparent_hugepage/use_zero_page
@@ -204,21 +200,67 @@ Support by passing the parameter "transparent_hugepage=always" or · 62 unmodified lines
204200 "transparent_hugepage=madvise" or "transparent_hugepage=never"
205201 (without "") to the kernel command line.
206202
203+== Hugepages in tmpfs/shmem ==
204+
205+You can control hugepage allocation policy in tmpfs with mount option
206+"huge=". It can have following values:
207+
208+ - "always":
209+ Attempt to allocate huge pages every time we need a new page;
210+
211+ - "never":
212+ Do not allocate huge pages;
213+
214+ - "within_size":
215+ Only allocate huge page if it will be fully within i_size.
216+ Also respect fadvise()/madvise() hints;
217+
218+ - "advise:
219+ Only allocate huge pages if requested with fadvise()/madvise();
220+
221+The default policy is "never".
222+
223+"mount -o remount,huge= /mountpoint" works fine after mount: remounting
224+huge=never will not attempt to break up huge pages at all, just stop more
225+from being allocated.
226+
227+There's also sysfs knob to control hugepage allocation policy for internal
228+shmem mount: /sys/kernel/mm/transparent_hugepage/shmem_enabled. The mount
229+is used for SysV SHM, memfds, shared anonymous mmaps (of /dev/zero or
230+MAP_ANONYMOUS), GPU drivers' DRM objects, Ashmem.
231+
232+In addition to policies listed above, shmem_enabled allows two further
233+values:
234+
235+ - "deny":
236+ For use in emergencies, to force the huge option off from
237+ all mounts;
238+ - "force":
239+ Force the huge option on for all - very useful for testing;
240+
207241 == Need of application restart ==
208242
209−The transparent_hugepage/enabled values only affect future
210−behavior. So to make them effective you need to restart any
211−application that could have been using hugepages. This also applies to
212−the regions registered in khugepaged.
243+The transparent_hugepage/enabled values and tmpfs mount option only affect
244+future behavior. So to make them effective you need to restart any
245+application that could have been using hugepages. This also applies to the
246+regions registered in khugepaged.
213247
214248 == Monitoring usage ==
215249
216−The number of transparent huge pages currently used by the system is
217−available by reading the AnonHugePages field in /proc/meminfo. To
218−identify what applications are using transparent huge pages, it is
219−necessary to read /proc/PID/smaps and count the AnonHugePages fields
220−for each mapping. Note that reading the smaps file is expensive and
221−reading it frequently will incur overhead.
250+The number of anonymous transparent huge pages currently used by the
251+system is available by reading the AnonHugePages field in /proc/meminfo.
252+To identify what applications are using anonymous transparent huge pages,
253+it is necessary to read /proc/PID/smaps and count the AnonHugePages fields
254+for each mapping.
255+
256+The number of file transparent huge pages mapped to userspace is available
257+by reading ShmemPmdMapped and ShmemHugePages fields in /proc/meminfo.
258+To identify what applications are mapping file transparent huge pages, it
259+is necessary to read /proc/PID/smaps and count the FileHugeMapped fields
260+for each mapping.
261+
262+Note that reading the smaps file is expensive and reading it
263+frequently will incur overhead.
222264
223265 There are a number of counters in /proc/vmstat that may be used to
224266 monitor how successfully the system is providing huge pages for use.
@@ -238,6 +280,12 @@ thp_collapse_alloc_failed is incremented if khugepaged found a range · 13 unmodified lines
238280 of pages that should be collapsed into one huge page but failed
239281 the allocation.
240282
283+thp_file_alloc is incremented every time a file huge page is successfully
284+i allocated.
285+
286+thp_file_mapped is incremented every time a file huge page is mapped into
287+ user address space.
288+
241289 thp_split_page is incremented every time a huge page is split into base
242290 pages. This can happen for a variety of reasons but a common
243291 reason is that a huge page is old and is being reclaimed.
@@ -403,19 +451,27 @@ pages: · 159 unmodified lines
403451 on relevant sub-page of the compound page.
404452
405453 - map/unmap of the whole compound page accounted in compound_mapcount
406− (stored in first tail page).
454+ (stored in first tail page). For file huge pages, we also increment
455+ ->_mapcount of all sub-pages in order to have race-free detection of
456+ last unmap of subpages.
407457
408−PageDoubleMap() indicates that ->_mapcount in all subpages is offset up by one.
409−This additional reference is required to get race-free detection of unmap of
410−subpages when we have them mapped with both PMDs and PTEs.
458+PageDoubleMap() indicates that the page is *possibly* mapped with PTEs.
459+
460+For anonymous pages PageDoubleMap() also indicates ->_mapcount in all
461+subpages is offset up by one. This additional reference is required to
462+get race-free detection of unmap of subpages when we have them mapped with
463+both PMDs and PTEs.
411464
412465 This is optimization required to lower overhead of per-subpage mapcount
413466 tracking. The alternative is alter ->_mapcount in all subpages on each
414467 map/unmap of the whole compound page.
415468
416−We set PG_double_map when a PMD of the page got split for the first time,
417−but still have PMD mapping. The additional references go away with last
418−compound_mapcount.
469+For anonymous pages, we set PG_double_map when a PMD of the page got split
470+for the first time, but still have PMD mapping. The additional references
471+go away with last compound_mapcount.
472+
473+File pages get PG_double_map set on first map of the page with PTE and
474+goes away when the page gets evicted from page cache.
419475
420476 split_huge_page internally has to distribute the refcounts in the head
421477 page to the tail pages before clearing all PG_head/tail bits from the page
@@ -427,7 +483,7 @@ sum of mapcount of all sub-pages plus one (split_huge_page caller must · 5 unmodified lines
427483 have reference for head page).
428484
429485 split_huge_page uses migration entries to stabilize page->_refcount and
430−page->_mapcount.
486+page->_mapcount of anonymous pages. File pages just got unmapped.
431487
432488 We safe against physical memory scanners too: the only legitimate way
433489 scanner can get reference to a page is get_page_unless_zero().
Documentation/vm/unevictable-lru.txt+21−0
@@ -461,6 +461,27 @@ unevictable LRU is enabled, the work of compaction is mostly handled by · 460 unmodified lines
461461 the page migration code and the same work flow as described in MIGRATING
462462 MLOCKED PAGES will apply.
463463
464+MLOCKING TRANSPARENT HUGE PAGES
465+-------------------------------
466+
467+A transparent huge page is represented by a single entry on an LRU list.
468+Therefore, we can only make unevictable an entire compound page, not
469+individual subpages.
470+
471+If a user tries to mlock() part of a huge page, we want the rest of the
472+page to be reclaimable.
473+
474+We cannot just split the page on partial mlock() as split_huge_page() can
475+fail and new intermittent failure mode for the syscall is undesirable.
476+
477+We handle this by keeping PTE-mapped huge pages on normal LRU lists: the
478+PMD on border of VM_LOCKED VMA will be split into PTE table.
479+
480+This way the huge page is accessible for vmscan. Under memory pressure the
481+page will be split, subpages which belong to VM_LOCKED VMAs will be moved
482+to unevictable LRU and the rest can be reclaimed.
483+
484+See also comment in follow_trans_huge_pmd().
464485
465486 mmap(MAP_LOCKED) SYSTEM CALL HANDLING
466487 -------------------------------------
Makefile+42−27
@@ -647,41 +647,28 @@ ifneq ($(CONFIG_FRAME_WARN),0) · 646 unmodified lines
647647 KBUILD_CFLAGS += $(call cc-option,-Wframe-larger-than=${CONFIG_FRAME_WARN})
648648 endif
649649
650−# Handle stack protector mode.
651−#
652−# Since kbuild can potentially perform two passes (first with the old
653−# .config values and then with updated .config values), we cannot error out
654−# if a desired compiler option is unsupported. If we were to error, kbuild
655−# could never get to the second pass and actually notice that we changed
656−# the option to something that was supported.
657−#
658−# Additionally, we don't want to fallback and/or silently change which compiler
659−# flags will be used, since that leads to producing kernels with different
660−# security feature characteristics depending on the compiler used. ("But I
661−# selected CC_STACKPROTECTOR_STRONG! Why did it build with _REGULAR?!")
662−#
663−# The middle ground is to warn here so that the failed option is obvious, but
664−# to let the build fail with bad compiler flags so that we can't produce a
665−# kernel when there is a CONFIG and compiler mismatch.
666−#
650+# This selects the stack protector compiler flag. Testing it is delayed
651+# until after .config has been reprocessed, in the prepare-compiler-check
652+# target.
667653 ifdef CONFIG_CC_STACKPROTECTOR_REGULAR
668654 stackp-flag := -fstack-protector
669− ifeq ($(call cc-option, $(stackp-flag)),)
670− $(warning Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: \
671− -fstack-protector not supported by compiler)
672− endif
655+ stackp-name := REGULAR
673656 else
674657 ifdef CONFIG_CC_STACKPROTECTOR_STRONG
675658 stackp-flag := -fstack-protector-strong
676− ifeq ($(call cc-option, $(stackp-flag)),)
677− $(warning Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: \
678− -fstack-protector-strong not supported by compiler)
679− endif
659+ stackp-name := STRONG
680660 else
681661 # Force off for distro compilers that enable stack protector by default.
682662 stackp-flag := $(call cc-option, -fno-stack-protector)
683663 endif
684664 endif
665+# Find arch-specific stack protector compiler sanity-checking script.
666+ifdef CONFIG_CC_STACKPROTECTOR
667+ stackp-path := $(srctree)/scripts/gcc-$(ARCH)_$(BITS)-has-stack-protector.sh
668+ ifneq ($(wildcard $(stackp-path)),)
669+ stackp-check := $(stackp-path)
670+ endif
671+endif
685672 KBUILD_CFLAGS += $(stackp-flag)
686673
687674 ifdef CONFIG_KCOV
@@ -1017,8 +1004,10 @@ ifneq ($(KBUILD_SRC),) · 329 unmodified lines
10171004 fi;
10181005 endif
10191006
1020−# prepare2 creates a makefile if using a separate output directory
1021−prepare2: prepare3 outputmakefile asm-generic
1007+# prepare2 creates a makefile if using a separate output directory.
1008+# From this point forward, .config has been reprocessed, so any rules
1009+# that need to depend on updated CONFIG_* values can be checked here.
1010+prepare2: prepare3 prepare-compiler-check outputmakefile asm-generic
10221011
10231012 prepare1: prepare2 $(version_h) include/generated/utsrelease.h \
10241013 include/config/auto.conf
@@ -1049,6 +1038,32 @@ endif · 24 unmodified lines
10491038 PHONY += prepare-objtool
10501039 prepare-objtool: $(objtool_target)
10511040
1041+# Check for CONFIG flags that require compiler support. Abort the build
1042+# after .config has been processed, but before the kernel build starts.
1043+#
1044+# For security-sensitive CONFIG options, we don't want to fallback and/or
1045+# silently change which compiler flags will be used, since that leads to
1046+# producing kernels with different security feature characteristics
1047+# depending on the compiler used. (For example, "But I selected
1048+# CC_STACKPROTECTOR_STRONG! Why did it build with _REGULAR?!")
1049+PHONY += prepare-compiler-check
1050+prepare-compiler-check: FORCE
1051+# Make sure compiler supports requested stack protector flag.
1052+ifdef stackp-name
1053+ ifeq ($(call cc-option, $(stackp-flag)),)
1054+ @echo Cannot use CONFIG_CC_STACKPROTECTOR_$(stackp-name): \
1055+ $(stackp-flag) not supported by compiler >&2 && exit 1
1056+ endif
1057+endif
1058+# Make sure compiler does not have buggy stack-protector support.
1059+ifdef stackp-check
1060+ ifneq ($(shell $(CONFIG_SHELL) $(stackp-check) $(CC) $(KBUILD_CPPFLAGS) $(biarch)),y)
1061+ @echo Cannot use CONFIG_CC_STACKPROTECTOR_$(stackp-name): \
1062+ $(stackp-flag) available but compiler is broken >&2 && exit 1
1063+ endif
1064+endif
1065+ @:
1066+
10521067 # Generate some files
10531068 # ---------------------------------------------------------------------------
10541069
arch/alpha/mm/fault.c+1−1
@@ -147,7 +147,7 @@ retry: · 146 unmodified lines
147147 /* If for any reason at all we couldn't handle the fault,
148148 make sure we exit gracefully rather than endlessly redo
149149 the fault. */
150− fault = handle_mm_fault(mm, vma, address, flags);
150+ fault = handle_mm_fault(vma, address, flags);
151151
152152 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
153153 return;
arch/arc/mm/fault.c+1−1
@@ -137,7 +137,7 @@ good_area: · 136 unmodified lines
137137 * make sure we exit gracefully rather than endlessly redo
138138 * the fault.
139139 */
140− fault = handle_mm_fault(mm, vma, address, flags);
140+ fault = handle_mm_fault(vma, address, flags);
141141
142142 /* If Pagefault was interrupted by SIGKILL, exit page fault "early" */
143143 if (unlikely(fatal_signal_pending(current))) {
arch/arm/include/asm/pgalloc.h+1−1
@@ -57,7 +57,7 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) · 56 unmodified lines
5757 extern pgd_t *pgd_alloc(struct mm_struct *mm);
5858 extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);
5959
60−#define PGALLOC_GFP (GFP_KERNEL | __GFP_NOTRACK | __GFP_REPEAT | __GFP_ZERO)
60+#define PGALLOC_GFP (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO)
6161
6262 static inline void clean_pte_table(pte_t *pte)
6363 {
arch/arm/include/asm/tlb.h+25−4
@@ -209,17 +209,38 @@ tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) · 208 unmodified lines
209209 tlb_flush(tlb);
210210 }
211211
212−static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
212+static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
213213 {
214+ if (tlb->nr == tlb->max)
215+ return true;
214216 tlb->pages[tlb->nr++] = page;
215− VM_BUG_ON(tlb->nr > tlb->max);
216− return tlb->max - tlb->nr;
217+ return false;
217218 }
218219
219220 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
220221 {
221− if (!__tlb_remove_page(tlb, page))
222+ if (__tlb_remove_page(tlb, page)) {
222223 tlb_flush_mmu(tlb);
224+ __tlb_remove_page(tlb, page);
225+ }
226+}
227+
228+static inline bool __tlb_remove_page_size(struct mmu_gather *tlb,
229+ struct page *page, int page_size)
230+{
231+ return __tlb_remove_page(tlb, page);
232+}
233+
234+static inline bool __tlb_remove_pte_page(struct mmu_gather *tlb,
235+ struct page *page)
236+{
237+ return __tlb_remove_page(tlb, page);
238+}
239+
240+static inline void tlb_remove_page_size(struct mmu_gather *tlb,
241+ struct page *page, int page_size)
242+{
243+ return tlb_remove_page(tlb, page);
223244 }
224245
225246 static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
arch/arm/mm/fault.c+1−1
@@ -243,7 +243,7 @@ good_area: · 242 unmodified lines
243243 goto out;
244244 }
245245
246− return handle_mm_fault(mm, vma, addr & PAGE_MASK, flags);
246+ return handle_mm_fault(vma, addr & PAGE_MASK, flags);
247247
248248 check_stack:
249249 /* Don't allow expansion below FIRST_USER_ADDRESS */
arch/arm/mm/pgd.c+1−1
@@ -23,7 +23,7 @@ · 22 unmodified lines
2323 #define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL)
2424 #define __pgd_free(pgd) kfree(pgd)
2525 #else
26−#define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_REPEAT, 2)
26+#define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
2727 #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
2828 #endif
2929
arch/arm64/mm/fault.c+1−1
@@ -233,7 +233,7 @@ good_area: · 232 unmodified lines
233233 goto out;
234234 }
235235
236− return handle_mm_fault(mm, vma, addr & PAGE_MASK, mm_flags);
236+ return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags);
237237
238238 check_stack:
239239 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
arch/avr32/mm/fault.c+1−1
@@ -134,7 +134,7 @@ good_area: · 133 unmodified lines
134134 * sure we exit gracefully rather than endlessly redo the
135135 * fault.
136136 */
137− fault = handle_mm_fault(mm, vma, address, flags);
137+ fault = handle_mm_fault(vma, address, flags);
138138
139139 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
140140 return;
arch/cris/mm/fault.c+1−1
@@ -168,7 +168,7 @@ retry: · 167 unmodified lines
168168 * the fault.
169169 */
170170
171− fault = handle_mm_fault(mm, vma, address, flags);
171+ fault = handle_mm_fault(vma, address, flags);
172172
173173 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
174174 return;
arch/frv/mm/fault.c+1−1
@@ -164,7 +164,7 @@ asmlinkage void do_page_fault(int datammu, unsigned long esr0, unsigned long ear · 163 unmodified lines
164164 * make sure we exit gracefully rather than endlessly redo
165165 * the fault.
166166 */
167− fault = handle_mm_fault(mm, vma, ear0, flags);
167+ fault = handle_mm_fault(vma, ear0, flags);
168168 if (unlikely(fault & VM_FAULT_ERROR)) {
169169 if (fault & VM_FAULT_OOM)
170170 goto out_of_memory;
arch/hexagon/mm/vm_fault.c+1−1
@@ -101,7 +101,7 @@ good_area: · 100 unmodified lines
101101 break;
102102 }
103103
104− fault = handle_mm_fault(mm, vma, address, flags);
104+ fault = handle_mm_fault(vma, address, flags);
105105
106106 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
107107 return;
arch/ia64/include/asm/tlb.h+26−5
@@ -205,17 +205,18 @@ tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) · 204 unmodified lines
205205 * must be delayed until after the TLB has been flushed (see comments at the beginning of
206206 * this file).
207207 */
208−static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
208+static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
209209 {
210+ if (tlb->nr == tlb->max)
211+ return true;
212+
210213 tlb->need_flush = 1;
211214
212215 if (!tlb->nr && tlb->pages == tlb->local)
213216 __tlb_alloc_page(tlb);
214217
215218 tlb->pages[tlb->nr++] = page;
216− VM_BUG_ON(tlb->nr > tlb->max);
217−
218− return tlb->max - tlb->nr;
219+ return false;
219220 }
220221
221222 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
@@ -235,8 +236,28 @@ static inline void tlb_flush_mmu(struct mmu_gather *tlb) · 13 unmodified lines
235236
236237 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
237238 {
238− if (!__tlb_remove_page(tlb, page))
239+ if (__tlb_remove_page(tlb, page)) {
239240 tlb_flush_mmu(tlb);
241+ __tlb_remove_page(tlb, page);
242+ }
243+}
244+
245+static inline bool __tlb_remove_page_size(struct mmu_gather *tlb,
246+ struct page *page, int page_size)
247+{
248+ return __tlb_remove_page(tlb, page);
249+}
250+
251+static inline bool __tlb_remove_pte_page(struct mmu_gather *tlb,
252+ struct page *page)
253+{
254+ return __tlb_remove_page(tlb, page);
255+}
256+
257+static inline void tlb_remove_page_size(struct mmu_gather *tlb,
258+ struct page *page, int page_size)
259+{
260+ return tlb_remove_page(tlb, page);
240261 }
241262
242263 /*
arch/ia64/mm/fault.c+1−1
@@ -159,7 +159,7 @@ retry: · 158 unmodified lines
159159 * sure we exit gracefully rather than endlessly redo the
160160 * fault.
161161 */
162− fault = handle_mm_fault(mm, vma, address, flags);
162+ fault = handle_mm_fault(vma, address, flags);
163163
164164 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
165165 return;
arch/m32r/kernel/m32r_ksyms.c+3−0
@@ -41,6 +41,9 @@ EXPORT_SYMBOL(cpu_data); · 40 unmodified lines
4141 EXPORT_SYMBOL(smp_flush_tlb_page);
4242 #endif
4343
44+extern int __ucmpdi2(unsigned long long a, unsigned long long b);
45+EXPORT_SYMBOL(__ucmpdi2);
46+
4447 /* compiler generated symbol */
4548 extern void __ashldi3(void);
4649 extern void __ashrdi3(void);
arch/m32r/lib/Makefile+2−2
@@ -3,5 +3,5 @@ · 2 unmodified lines
33 #
44
55 lib-y := checksum.o ashxdi3.o memset.o memcpy.o \
6− delay.o strlen.o usercopy.o csum_partial_copy.o
7−
6+ delay.o strlen.o usercopy.o csum_partial_copy.o \
7+ ucmpdi2.o
arch/m32r/lib/libgcc.h+23−0
@@ -0,0 +1,23 @@
1+#ifndef __ASM_LIBGCC_H
2+#define __ASM_LIBGCC_H
3+
4+#include <asm/byteorder.h>
5+
6+#ifdef __BIG_ENDIAN
7+struct DWstruct {
8+ int high, low;
9+};
10+#elif defined(__LITTLE_ENDIAN)
11+struct DWstruct {
12+ int low, high;
13+};
14+#else
15+#error I feel sick.
16+#endif
17+
18+typedef union {
19+ struct DWstruct s;
20+ long long ll;
21+} DWunion;
22+
23+#endif /* __ASM_LIBGCC_H */
arch/m32r/lib/ucmpdi2.c+17−0
@@ -0,0 +1,17 @@
1+#include "libgcc.h"
2+
3+int __ucmpdi2(unsigned long long a, unsigned long long b)
4+{
5+ const DWunion au = {.ll = a};
6+ const DWunion bu = {.ll = b};
7+
8+ if ((unsigned int)au.s.high < (unsigned int)bu.s.high)
9+ return 0;
10+ else if ((unsigned int)au.s.high > (unsigned int)bu.s.high)
11+ return 2;
12+ if ((unsigned int)au.s.low < (unsigned int)bu.s.low)
13+ return 0;
14+ else if ((unsigned int)au.s.low > (unsigned int)bu.s.low)
15+ return 2;
16+ return 1;
17+}
arch/m32r/mm/fault.c+1−1
@@ -196,7 +196,7 @@ good_area: · 195 unmodified lines
196196 */
197197 addr = (address & PAGE_MASK);
198198 set_thread_fault_code(error_code);
199− fault = handle_mm_fault(mm, vma, addr, flags);
199+ fault = handle_mm_fault(vma, addr, flags);
200200 if (unlikely(fault & VM_FAULT_ERROR)) {
201201 if (fault & VM_FAULT_OOM)
202202 goto out_of_memory;
arch/m68k/mm/fault.c+1−1
@@ -136,7 +136,7 @@ good_area: · 135 unmodified lines
136136 * the fault.
137137 */
138138
139− fault = handle_mm_fault(mm, vma, address, flags);
139+ fault = handle_mm_fault(vma, address, flags);
140140 pr_debug("handle_mm_fault returns %d\n", fault);
141141
142142 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
arch/metag/mm/fault.c+1−1
@@ -133,7 +133,7 @@ good_area: · 132 unmodified lines
133133 * make sure we exit gracefully rather than endlessly redo
134134 * the fault.
135135 */
136− fault = handle_mm_fault(mm, vma, address, flags);
136+ fault = handle_mm_fault(vma, address, flags);
137137
138138 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
139139 return 0;
arch/microblaze/mm/fault.c+1−1
@@ -216,7 +216,7 @@ good_area: · 215 unmodified lines
216216 * make sure we exit gracefully rather than endlessly redo
217217 * the fault.
218218 */
219− fault = handle_mm_fault(mm, vma, address, flags);
219+ fault = handle_mm_fault(vma, address, flags);
220220
221221 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
222222 return;
arch/mips/mm/fault.c+1−1
@@ -153,7 +153,7 @@ good_area: · 152 unmodified lines
153153 * make sure we exit gracefully rather than endlessly redo
154154 * the fault.
155155 */
156− fault = handle_mm_fault(mm, vma, address, flags);
156+ fault = handle_mm_fault(vma, address, flags);
157157
158158 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
159159 return;
arch/mn10300/mm/fault.c+1−1
@@ -254,7 +254,7 @@ good_area: · 253 unmodified lines
254254 * make sure we exit gracefully rather than endlessly redo
255255 * the fault.
256256 */
257− fault = handle_mm_fault(mm, vma, address, flags);
257+ fault = handle_mm_fault(vma, address, flags);
258258
259259 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
260260 return;
arch/nios2/mm/fault.c+1−1
@@ -131,7 +131,7 @@ good_area: · 130 unmodified lines
131131 * make sure we exit gracefully rather than endlessly redo
132132 * the fault.
133133 */
134− fault = handle_mm_fault(mm, vma, address, flags);
134+ fault = handle_mm_fault(vma, address, flags);
135135
136136 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
137137 return;
arch/openrisc/mm/fault.c+1−1
@@ -163,7 +163,7 @@ good_area: · 162 unmodified lines
163163 * the fault.
164164 */
165165
166− fault = handle_mm_fault(mm, vma, address, flags);
166+ fault = handle_mm_fault(vma, address, flags);
167167
168168 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
169169 return;
arch/parisc/mm/fault.c+1−1
@@ -239,7 +239,7 @@ good_area: · 238 unmodified lines
239239 * fault.
240240 */
241241
242− fault = handle_mm_fault(mm, vma, address, flags);
242+ fault = handle_mm_fault(vma, address, flags);
243243
244244 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
245245 return;
arch/powerpc/include/asm/pgtable.h+2−4
@@ -71,10 +71,8 @@ pte_t *__find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea, · 70 unmodified lines
7171 static inline pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
7272 bool *is_thp, unsigned *shift)
7373 {
74− if (!arch_irqs_disabled()) {
75− pr_info("%s called with irq enabled\n", __func__);
76− dump_stack();
77− }
74+ VM_WARN(!arch_irqs_disabled(),
75+ "%s called with irq enabled\n", __func__);
7876 return __find_linux_pte_or_hugepte(pgdir, ea, is_thp, shift);
7977 }
8078
arch/powerpc/mm/copro_fault.c+1−1
@@ -75,7 +75,7 @@ int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea, · 74 unmodified lines
7575 }
7676
7777 ret = 0;
78− *flt = handle_mm_fault(mm, vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
78+ *flt = handle_mm_fault(vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
7979 if (unlikely(*flt & VM_FAULT_ERROR)) {
8080 if (*flt & VM_FAULT_OOM) {
8181 ret = -ENOMEM;
arch/powerpc/mm/fault.c+1−1
@@ -429,7 +429,7 @@ good_area: · 428 unmodified lines
429429 * make sure we exit gracefully rather than endlessly redo
430430 * the fault.
431431 */
432− fault = handle_mm_fault(mm, vma, address, flags);
432+ fault = handle_mm_fault(vma, address, flags);
433433 if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
434434 if (fault & VM_FAULT_SIGSEGV)
435435 goto bad_area;
arch/s390/include/asm/tlb.h+20−2
@@ -87,10 +87,10 @@ static inline void tlb_finish_mmu(struct mmu_gather *tlb, · 86 unmodified lines
8787 * tlb_ptep_clear_flush. In both flush modes the tlb for a page cache page
8888 * has already been freed, so just do free_page_and_swap_cache.
8989 */
90−static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
90+static inline bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
9191 {
9292 free_page_and_swap_cache(page);
93− return 1; /* avoid calling tlb_flush_mmu */
93+ return false; /* avoid calling tlb_flush_mmu */
9494 }
9595
9696 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
@@ -98,6 +98,24 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page) · 1 unmodified line
9898 free_page_and_swap_cache(page);
9999 }
100100
101+static inline bool __tlb_remove_page_size(struct mmu_gather *tlb,
102+ struct page *page, int page_size)
103+{
104+ return __tlb_remove_page(tlb, page);
105+}
106+
107+static inline bool __tlb_remove_pte_page(struct mmu_gather *tlb,
108+ struct page *page)
109+{
110+ return __tlb_remove_page(tlb, page);
111+}
112+
113+static inline void tlb_remove_page_size(struct mmu_gather *tlb,
114+ struct page *page, int page_size)
115+{
116+ return tlb_remove_page(tlb, page);
117+}
118+
101119 /*
102120 * pte_free_tlb frees a pte table and clears the CRSTE for the
103121 * page table from the tlb.
arch/s390/mm/fault.c+1−1
@@ -456,7 +456,7 @@ retry: · 455 unmodified lines
456456 * make sure we exit gracefully rather than endlessly redo
457457 * the fault.
458458 */
459− fault = handle_mm_fault(mm, vma, address, flags);
459+ fault = handle_mm_fault(vma, address, flags);
460460 /* No reason to continue if interrupted by SIGKILL. */
461461 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
462462 fault = VM_FAULT_SIGNAL;
arch/score/mm/fault.c+1−1
@@ -111,7 +111,7 @@ good_area: · 110 unmodified lines
111111 * make sure we exit gracefully rather than endlessly redo
112112 * the fault.
113113 */
114− fault = handle_mm_fault(mm, vma, address, flags);
114+ fault = handle_mm_fault(vma, address, flags);
115115 if (unlikely(fault & VM_FAULT_ERROR)) {
116116 if (fault & VM_FAULT_OOM)
117117 goto out_of_memory;
arch/sh/include/asm/tlb.h+19−1
@@ -101,7 +101,7 @@ static inline void tlb_flush_mmu(struct mmu_gather *tlb) · 100 unmodified lines
101101 static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
102102 {
103103 free_page_and_swap_cache(page);
104− return 1; /* avoid calling tlb_flush_mmu */
104+ return false; /* avoid calling tlb_flush_mmu */
105105 }
106106
107107 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
@@ -109,6 +109,24 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page) · 1 unmodified line
109109 __tlb_remove_page(tlb, page);
110110 }
111111
112+static inline bool __tlb_remove_page_size(struct mmu_gather *tlb,
113+ struct page *page, int page_size)
114+{
115+ return __tlb_remove_page(tlb, page);
116+}
117+
118+static inline bool __tlb_remove_pte_page(struct mmu_gather *tlb,
119+ struct page *page)
120+{
121+ return __tlb_remove_page(tlb, page);
122+}
123+
124+static inline void tlb_remove_page_size(struct mmu_gather *tlb,
125+ struct page *page, int page_size)
126+{
127+ return tlb_remove_page(tlb, page);
128+}
129+
112130 #define pte_free_tlb(tlb, ptep, addr) pte_free((tlb)->mm, ptep)
113131 #define pmd_free_tlb(tlb, pmdp, addr) pmd_free((tlb)->mm, pmdp)
114132 #define pud_free_tlb(tlb, pudp, addr) pud_free((tlb)->mm, pudp)
arch/sh/mm/fault.c+1−1
@@ -487,7 +487,7 @@ good_area: · 486 unmodified lines
487487 * make sure we exit gracefully rather than endlessly redo
488488 * the fault.
489489 */
490− fault = handle_mm_fault(mm, vma, address, flags);
490+ fault = handle_mm_fault(vma, address, flags);
491491
492492 if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR)))
493493 if (mm_fault_error(regs, error_code, address, fault))
arch/sparc/mm/fault_32.c+2−2
@@ -241,7 +241,7 @@ good_area: · 240 unmodified lines
241241 * make sure we exit gracefully rather than endlessly redo
242242 * the fault.
243243 */
244− fault = handle_mm_fault(mm, vma, address, flags);
244+ fault = handle_mm_fault(vma, address, flags);
245245
246246 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
247247 return;
@@ -411,7 +411,7 @@ good_area: · 163 unmodified lines
411411 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
412412 goto bad_area;
413413 }
414− switch (handle_mm_fault(mm, vma, address, flags)) {
414+ switch (handle_mm_fault(vma, address, flags)) {
415415 case VM_FAULT_SIGBUS:
416416 case VM_FAULT_OOM:
417417 goto do_sigbus;
arch/sparc/mm/fault_64.c+1−1
@@ -436,7 +436,7 @@ good_area: · 435 unmodified lines
436436 goto bad_area;
437437 }
438438
439− fault = handle_mm_fault(mm, vma, address, flags);
439+ fault = handle_mm_fault(vma, address, flags);
440440
441441 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
442442 goto exit_exception;
arch/tile/mm/fault.c+1−1
@@ -434,7 +434,7 @@ good_area: · 433 unmodified lines
434434 * make sure we exit gracefully rather than endlessly redo
435435 * the fault.
436436 */
437− fault = handle_mm_fault(mm, vma, address, flags);
437+ fault = handle_mm_fault(vma, address, flags);
438438
439439 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
440440 return 0;
arch/um/include/asm/tlb.h+19−1
@@ -102,7 +102,7 @@ static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page) · 101 unmodified lines
102102 {
103103 tlb->need_flush = 1;
104104 free_page_and_swap_cache(page);
105− return 1; /* avoid calling tlb_flush_mmu */
105+ return false; /* avoid calling tlb_flush_mmu */
106106 }
107107
108108 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
@@ -110,6 +110,24 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page) · 1 unmodified line
110110 __tlb_remove_page(tlb, page);
111111 }
112112
113+static inline bool __tlb_remove_page_size(struct mmu_gather *tlb,
114+ struct page *page, int page_size)
115+{
116+ return __tlb_remove_page(tlb, page);
117+}
118+
119+static inline bool __tlb_remove_pte_page(struct mmu_gather *tlb,
120+ struct page *page)
121+{
122+ return __tlb_remove_page(tlb, page);
123+}
124+
125+static inline void tlb_remove_page_size(struct mmu_gather *tlb,
126+ struct page *page, int page_size)
127+{
128+ return tlb_remove_page(tlb, page);
129+}
130+
113131 /**
114132 * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
115133 *
arch/um/kernel/trap.c+1−1
@@ -73,7 +73,7 @@ good_area: · 72 unmodified lines
7373 do {
7474 int fault;
7575
76− fault = handle_mm_fault(mm, vma, address, flags);
76+ fault = handle_mm_fault(vma, address, flags);
7777
7878 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
7979 goto out_nosemaphore;
arch/unicore32/mm/fault.c+1−1
@@ -194,7 +194,7 @@ good_area: · 193 unmodified lines
194194 * If for any reason at all we couldn't handle the fault, make
195195 * sure we exit gracefully rather than endlessly redo the fault.
196196 */
197− fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, flags);
197+ fault = handle_mm_fault(vma, addr & PAGE_MASK, flags);
198198 return fault;
199199
200200 check_stack:
arch/x86/Makefile+0−8
@@ -126,14 +126,6 @@ else · 125 unmodified lines
126126 KBUILD_CFLAGS += $(call cc-option,-maccumulate-outgoing-args)
127127 endif
128128
129−# Make sure compiler does not have buggy stack-protector support.
130−ifdef CONFIG_CC_STACKPROTECTOR
131− cc_has_sp := $(srctree)/scripts/gcc-x86_$(BITS)-has-stack-protector.sh
132− ifneq ($(shell $(CONFIG_SHELL) $(cc_has_sp) $(CC) $(KBUILD_CPPFLAGS) $(biarch)),y)
133− $(warning stack-protector enabled but compiler support broken)
134− endif
135−endif
136−
137129 ifdef CONFIG_X86_X32
138130 x32_ld_ok := $(call try-run,\
139131 /bin/echo -e '1: .quad 1b' | \
arch/x86/include/asm/pgalloc.h+10−2
@@ -81,7 +81,11 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, · 80 unmodified lines
8181 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
8282 {
8383 struct page *page;
84− page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
84+ gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO;
85+
86+ if (mm == &init_mm)
87+ gfp &= ~__GFP_ACCOUNT;
88+ page = alloc_pages(gfp, 0);
8589 if (!page)
8690 return NULL;
8791 if (!pgtable_pmd_page_ctor(page)) {
@@ -125,7 +129,11 @@ static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud) · 37 unmodified lines
125129
126130 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
127131 {
128− return (pud_t *)get_zeroed_page(GFP_KERNEL);
132+ gfp_t gfp = GFP_KERNEL_ACCOUNT;
133+
134+ if (mm == &init_mm)
135+ gfp &= ~__GFP_ACCOUNT;
136+ return (pud_t *)get_zeroed_page(gfp);
129137 }
130138
131139 static inline void pud_free(struct mm_struct *mm, pud_t *pud)
arch/x86/mm/fault.c+1−1
@@ -1353,7 +1353,7 @@ good_area: · 1352 unmodified lines
13531353 * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
13541354 * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
13551355 */
1356− fault = handle_mm_fault(mm, vma, address, flags);
1356+ fault = handle_mm_fault(vma, address, flags);
13571357 major |= fault & VM_FAULT_MAJOR;
13581358
13591359 /*
arch/x86/mm/pgtable.c+7−3
@@ -6,7 +6,7 @@ · 5 unmodified lines
66 #include <asm/fixmap.h>
77 #include <asm/mtrr.h>
88
9−#define PGALLOC_GFP GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO
9+#define PGALLOC_GFP (GFP_KERNEL_ACCOUNT | __GFP_NOTRACK | __GFP_ZERO)
1010
1111 #ifdef CONFIG_HIGHPTE
1212 #define PGALLOC_USER_GFP __GFP_HIGHMEM
@@ -18,7 +18,7 @@ gfp_t __userpte_alloc_gfp = PGALLOC_GFP | PGALLOC_USER_GFP; · 5 unmodified lines
1818
1919 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
2020 {
21− return (pte_t *)__get_free_page(PGALLOC_GFP);
21+ return (pte_t *)__get_free_page(PGALLOC_GFP & ~__GFP_ACCOUNT);
2222 }
2323
2424 pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
@@ -207,9 +207,13 @@ static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[]) · 182 unmodified lines
207207 {
208208 int i;
209209 bool failed = false;
210+ gfp_t gfp = PGALLOC_GFP;
211+
212+ if (mm == &init_mm)
213+ gfp &= ~__GFP_ACCOUNT;
210214
211215 for(i = 0; i < PREALLOCATED_PMDS; i++) {
212− pmd_t *pmd = (pmd_t *)__get_free_page(PGALLOC_GFP);
216+ pmd_t *pmd = (pmd_t *)__get_free_page(gfp);
213217 if (!pmd)
214218 failed = true;
215219 if (pmd && !pgtable_pmd_page_ctor(virt_to_page(pmd))) {
arch/xtensa/mm/fault.c+1−1
@@ -110,7 +110,7 @@ good_area: · 109 unmodified lines
110110 * make sure we exit gracefully rather than endlessly redo
111111 * the fault.
112112 */
113− fault = handle_mm_fault(mm, vma, address, flags);
113+ fault = handle_mm_fault(vma, address, flags);
114114
115115 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
116116 return;
drivers/base/memory.c+17−11
@@ -391,6 +391,7 @@ static ssize_t show_valid_zones(struct device *dev, · 390 unmodified lines
391391 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
392392 struct page *first_page;
393393 struct zone *zone;
394+ int zone_shift = 0;
394395
395396 start_pfn = section_nr_to_pfn(mem->start_section_nr);
396397 end_pfn = start_pfn + nr_pages;
@@ -402,21 +403,26 @@ static ssize_t show_valid_zones(struct device *dev, · 5 unmodified lines
402403
403404 zone = page_zone(first_page);
404405
405− if (zone_idx(zone) == ZONE_MOVABLE - 1) {
406− /*The mem block is the last memoryblock of this zone.*/
407− if (end_pfn == zone_end_pfn(zone))
408− return sprintf(buf, "%s %s\n",
409− zone->name, (zone + 1)->name);
406+ /* MMOP_ONLINE_KEEP */
407+ sprintf(buf, "%s", zone->name);
408+
409+ /* MMOP_ONLINE_KERNEL */
410+ zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL);
411+ if (zone_shift) {
412+ strcat(buf, " ");
413+ strcat(buf, (zone + zone_shift)->name);
410414 }
411415
412− if (zone_idx(zone) == ZONE_MOVABLE) {
413− /*The mem block is the first memoryblock of ZONE_MOVABLE.*/
414− if (start_pfn == zone->zone_start_pfn)
415− return sprintf(buf, "%s %s\n",
416− zone->name, (zone - 1)->name);
416+ /* MMOP_ONLINE_MOVABLE */
417+ zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE);
418+ if (zone_shift) {
419+ strcat(buf, " ");
420+ strcat(buf, (zone + zone_shift)->name);
417421 }
418422
419− return sprintf(buf, "%s\n", zone->name);
423+ strcat(buf, "\n");
424+
425+ return strlen(buf);
420426 }
421427 static DEVICE_ATTR(valid_zones, 0444, show_valid_zones, NULL);
422428 #endif
drivers/base/node.c+9−4
@@ -113,6 +113,8 @@ static ssize_t node_read_meminfo(struct device *dev, · 112 unmodified lines
113113 "Node %d SUnreclaim: %8lu kB\n"
114114 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
115115 "Node %d AnonHugePages: %8lu kB\n"
116+ "Node %d ShmemHugePages: %8lu kB\n"
117+ "Node %d ShmemPmdMapped: %8lu kB\n"
116118 #endif
117119 ,
118120 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
@@ -131,10 +133,13 @@ static ssize_t node_read_meminfo(struct device *dev, · 12 unmodified lines
131133 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
132134 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
133135 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
134− nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE))
135− , nid,
136− K(node_page_state(nid, NR_ANON_TRANSPARENT_HUGEPAGES) *
137− HPAGE_PMD_NR));
136+ nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
137+ nid, K(node_page_state(nid, NR_ANON_THPS) *
138+ HPAGE_PMD_NR),
139+ nid, K(node_page_state(nid, NR_SHMEM_THPS) *
140+ HPAGE_PMD_NR),
141+ nid, K(node_page_state(nid, NR_SHMEM_PMDMAPPED) *
142+ HPAGE_PMD_NR));
138143 #else
139144 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
140145 #endif
drivers/block/zram/Kconfig+2−13
@@ -1,8 +1,7 @@
11 config ZRAM
22 tristate "Compressed RAM block device support"
3− depends on BLOCK && SYSFS && ZSMALLOC
4− select LZO_COMPRESS
5− select LZO_DECOMPRESS
3+ depends on BLOCK && SYSFS && ZSMALLOC && CRYPTO
4+ select CRYPTO_LZO
65 default n
76 help
87 Creates virtual block devices called /dev/zramX (X = 0, 1, ...).
@@ -14,13 +13,3 @@ config ZRAM · 5 unmodified lines
1413 disks and maybe many more.
1514
1615 See zram.txt for more information.
17−
18−config ZRAM_LZ4_COMPRESS
19− bool "Enable LZ4 algorithm support"
20− depends on ZRAM
21− select LZ4_COMPRESS
22− select LZ4_DECOMPRESS
23− default n
24− help
25− This option enables LZ4 compression algorithm support. Compression
26− algorithm can be changed using `comp_algorithm' device attribute.
drivers/block/zram/Makefile+1−3
@@ -1,5 +1,3 @@
1−zram-y := zcomp_lzo.o zcomp.o zram_drv.o
2−
3−zram-$(CONFIG_ZRAM_LZ4_COMPRESS) += zcomp_lz4.o
1+zram-y := zcomp.o zram_drv.o
42
53 obj-$(CONFIG_ZRAM) += zram.o
drivers/block/zram/zcomp.c+95−55
@@ -14,108 +14,150 @@ · 13 unmodified lines
1414 #include <linux/wait.h>
1515 #include <linux/sched.h>
1616 #include <linux/cpu.h>
17+#include <linux/crypto.h>
1718
1819 #include "zcomp.h"
19−#include "zcomp_lzo.h"
20−#ifdef CONFIG_ZRAM_LZ4_COMPRESS
21−#include "zcomp_lz4.h"
22−#endif
2320
24−static struct zcomp_backend *backends[] = {
25− &zcomp_lzo,
26−#ifdef CONFIG_ZRAM_LZ4_COMPRESS
27− &zcomp_lz4,
21+static const char * const backends[] = {
22+ "lzo",
23+#if IS_ENABLED(CONFIG_CRYPTO_LZ4)
24+ "lz4",
25+#endif
26+#if IS_ENABLED(CONFIG_CRYPTO_DEFLATE)
27+ "deflate",
28+#endif
29+#if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
30+ "lz4hc",
31+#endif
32+#if IS_ENABLED(CONFIG_CRYPTO_842)
33+ "842",
2834 #endif
2935 NULL
3036 };
3137
32−static struct zcomp_backend *find_backend(const char *compress)
33−{
34− int i = 0;
35− while (backends[i]) {
36− if (sysfs_streq(compress, backends[i]->name))
37− break;
38− i++;
39− }
40− return backends[i];
41−}
42−
43−static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
38+static void zcomp_strm_free(struct zcomp_strm *zstrm)
4439 {
45− if (zstrm->private)
46− comp->backend->destroy(zstrm->private);
40+ if (!IS_ERR_OR_NULL(zstrm->tfm))
41+ crypto_free_comp(zstrm->tfm);
4742 free_pages((unsigned long)zstrm->buffer, 1);
4843 kfree(zstrm);
4944 }
5045
5146 /*
52− * allocate new zcomp_strm structure with ->private initialized by
47+ * allocate new zcomp_strm structure with ->tfm initialized by
5348 * backend, return NULL on error
5449 */
55−static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp, gfp_t flags)
50+static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
5651 {
57− struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), flags);
52+ struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
5853 if (!zstrm)
5954 return NULL;
6055
61− zstrm->private = comp->backend->create(flags);
56+ zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
6257 /*
6358 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
6459 * case when compressed size is larger than the original one
6560 */
66− zstrm->buffer = (void *)__get_free_pages(flags | __GFP_ZERO, 1);
67− if (!zstrm->private || !zstrm->buffer) {
68− zcomp_strm_free(comp, zstrm);
61+ zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
62+ if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
63+ zcomp_strm_free(zstrm);
6964 zstrm = NULL;
7065 }
7166 return zstrm;
7267 }
7368
69+bool zcomp_available_algorithm(const char *comp)
70+{
71+ int i = 0;
72+
73+ while (backends[i]) {
74+ if (sysfs_streq(comp, backends[i]))
75+ return true;
76+ i++;
77+ }
78+
79+ /*
80+ * Crypto does not ignore a trailing new line symbol,
81+ * so make sure you don't supply a string containing
82+ * one.
83+ * This also means that we permit zcomp initialisation
84+ * with any compressing algorithm known to crypto api.
85+ */
86+ return crypto_has_comp(comp, 0, 0) == 1;
87+}
88+
7489 /* show available compressors */
7590 ssize_t zcomp_available_show(const char *comp, char *buf)
7691 {
92+ bool known_algorithm = false;
7793 ssize_t sz = 0;
7894 int i = 0;
7995
80− while (backends[i]) {
81− if (!strcmp(comp, backends[i]->name))
96+ for (; backends[i]; i++) {
97+ if (!strcmp(comp, backends[i])) {
98+ known_algorithm = true;
8299 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
83− "[%s] ", backends[i]->name);
84− else
100+ "[%s] ", backends[i]);
101+ } else {
85102 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
86− "%s ", backends[i]->name);
87− i++;
103+ "%s ", backends[i]);
104+ }
88105 }
106+
107+ /*
108+ * Out-of-tree module known to crypto api or a missing
109+ * entry in `backends'.
110+ */
111+ if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
112+ sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
113+ "[%s] ", comp);
114+
89115 sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
90116 return sz;
91117 }
92118
93−bool zcomp_available_algorithm(const char *comp)
94−{
95− return find_backend(comp) != NULL;
96−}
97−
98−struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
119+struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
99120 {
100121 return *get_cpu_ptr(comp->stream);
101122 }
102123
103−void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm)
124+void zcomp_stream_put(struct zcomp *comp)
104125 {
105126 put_cpu_ptr(comp->stream);
106127 }
107128
108−int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
109− const unsigned char *src, size_t *dst_len)
129+int zcomp_compress(struct zcomp_strm *zstrm,
130+ const void *src, unsigned int *dst_len)
110131 {
111− return comp->backend->compress(src, zstrm->buffer, dst_len,
112− zstrm->private);
132+ /*
133+ * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized
134+ * because sometimes we can endup having a bigger compressed data
135+ * due to various reasons: for example compression algorithms tend
136+ * to add some padding to the compressed buffer. Speaking of padding,
137+ * comp algorithm `842' pads the compressed length to multiple of 8
138+ * and returns -ENOSP when the dst memory is not big enough, which
139+ * is not something that ZRAM wants to see. We can handle the
140+ * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we
141+ * receive -ERRNO from the compressing backend we can't help it
142+ * anymore. To make `842' happy we need to tell the exact size of
143+ * the dst buffer, zram_drv will take care of the fact that
144+ * compressed buffer is too big.
145+ */
146+ *dst_len = PAGE_SIZE * 2;
147+
148+ return crypto_comp_compress(zstrm->tfm,
149+ src, PAGE_SIZE,
150+ zstrm->buffer, dst_len);
113151 }
114152
115−int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
116− size_t src_len, unsigned char *dst)
153+int zcomp_decompress(struct zcomp_strm *zstrm,
154+ const void *src, unsigned int src_len, void *dst)
117155 {
118− return comp->backend->decompress(src, src_len, dst);
156+ unsigned int dst_len = PAGE_SIZE;
157+
158+ return crypto_comp_decompress(zstrm->tfm,
159+ src, src_len,
160+ dst, &dst_len);
119161 }
120162
121163 static int __zcomp_cpu_notifier(struct zcomp *comp,
@@ -127,7 +169,7 @@ static int __zcomp_cpu_notifier(struct zcomp *comp, · 5 unmodified lines
127169 case CPU_UP_PREPARE:
128170 if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
129171 break;
130− zstrm = zcomp_strm_alloc(comp, GFP_KERNEL);
172+ zstrm = zcomp_strm_alloc(comp);
131173 if (IS_ERR_OR_NULL(zstrm)) {
132174 pr_err("Can't allocate a compression stream\n");
133175 return NOTIFY_BAD;
@@ -138,7 +180,7 @@ static int __zcomp_cpu_notifier(struct zcomp *comp, · 4 unmodified lines
138180 case CPU_UP_CANCELED:
139181 zstrm = *per_cpu_ptr(comp->stream, cpu);
140182 if (!IS_ERR_OR_NULL(zstrm))
141− zcomp_strm_free(comp, zstrm);
183+ zcomp_strm_free(zstrm);
142184 *per_cpu_ptr(comp->stream, cpu) = NULL;
143185 break;
144186 default:
@@ -209,18 +251,16 @@ void zcomp_destroy(struct zcomp *comp) · 64 unmodified lines
209251 struct zcomp *zcomp_create(const char *compress)
210252 {
211253 struct zcomp *comp;
212− struct zcomp_backend *backend;
213254 int error;
214255
215− backend = find_backend(compress);
216− if (!backend)
256+ if (!zcomp_available_algorithm(compress))
217257 return ERR_PTR(-EINVAL);
218258
219259 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
220260 if (!comp)
221261 return ERR_PTR(-ENOMEM);
222262
223− comp->backend = backend;
263+ comp->name = compress;
224264 error = zcomp_init(comp);
225265 if (error) {
226266 kfree(comp);
drivers/block/zram/zcomp.h+9−27
@@ -13,33 +13,15 @@ · 12 unmodified lines
1313 struct zcomp_strm {
1414 /* compression/decompression buffer */
1515 void *buffer;
16− /*
17− * The private data of the compression stream, only compression
18− * stream backend can touch this (e.g. compression algorithm
19− * working memory)
20− */
21− void *private;
22−};
23−
24−/* static compression backend */
25−struct zcomp_backend {
26− int (*compress)(const unsigned char *src, unsigned char *dst,
27− size_t *dst_len, void *private);
28−
29− int (*decompress)(const unsigned char *src, size_t src_len,
30− unsigned char *dst);
31−
32− void *(*create)(gfp_t flags);
33− void (*destroy)(void *private);
34−
35− const char *name;
16+ struct crypto_comp *tfm;
3617 };
3718
3819 /* dynamic per-device compression frontend */
3920 struct zcomp {
4021 struct zcomp_strm * __percpu *stream;
41− struct zcomp_backend *backend;
4222 struct notifier_block notifier;
23+
24+ const char *name;
4325 };
4426
4527 ssize_t zcomp_available_show(const char *comp, char *buf);
@@ -48,14 +30,14 @@ bool zcomp_available_algorithm(const char *comp); · 2 unmodified lines
4830 struct zcomp *zcomp_create(const char *comp);
4931 void zcomp_destroy(struct zcomp *comp);
5032
51−struct zcomp_strm *zcomp_strm_find(struct zcomp *comp);
52−void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm);
33+struct zcomp_strm *zcomp_stream_get(struct zcomp *comp);
34+void zcomp_stream_put(struct zcomp *comp);
5335
54−int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
55− const unsigned char *src, size_t *dst_len);
36+int zcomp_compress(struct zcomp_strm *zstrm,
37+ const void *src, unsigned int *dst_len);
5638
57−int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
58− size_t src_len, unsigned char *dst);
39+int zcomp_decompress(struct zcomp_strm *zstrm,
40+ const void *src, unsigned int src_len, void *dst);
5941
6042 bool zcomp_set_max_streams(struct zcomp *comp, int num_strm);
6143 #endif /* _ZCOMP_H_ */
drivers/block/zram/zcomp_lz4.c+0−56
@@ -1,56 +0,0 @@
1−/*
2− * Copyright (C) 2014 Sergey Senozhatsky.
3− *
4− * This program is free software; you can redistribute it and/or
5− * modify it under the terms of the GNU General Public License
6− * as published by the Free Software Foundation; either version
7− * 2 of the License, or (at your option) any later version.
8− */
9−
10−#include <linux/kernel.h>
11−#include <linux/slab.h>
12−#include <linux/lz4.h>
13−#include <linux/vmalloc.h>
14−#include <linux/mm.h>
15−
16−#include "zcomp_lz4.h"
17−
18−static void *zcomp_lz4_create(gfp_t flags)
19−{
20− void *ret;
21−
22− ret = kmalloc(LZ4_MEM_COMPRESS, flags);
23− if (!ret)
24− ret = __vmalloc(LZ4_MEM_COMPRESS,
25− flags | __GFP_HIGHMEM,
26− PAGE_KERNEL);
27− return ret;
28−}
29−
30−static void zcomp_lz4_destroy(void *private)
31−{
32− kvfree(private);
33−}
34−
35−static int zcomp_lz4_compress(const unsigned char *src, unsigned char *dst,
36− size_t *dst_len, void *private)
37−{
38− /* return : Success if return 0 */
39− return lz4_compress(src, PAGE_SIZE, dst, dst_len, private);
40−}
41−
42−static int zcomp_lz4_decompress(const unsigned char *src, size_t src_len,
43− unsigned char *dst)
44−{
45− size_t dst_len = PAGE_SIZE;
46− /* return : Success if return 0 */
47− return lz4_decompress_unknownoutputsize(src, src_len, dst, &dst_len);
48−}
49−
50−struct zcomp_backend zcomp_lz4 = {
51− .compress = zcomp_lz4_compress,
52− .decompress = zcomp_lz4_decompress,
53− .create = zcomp_lz4_create,
54− .destroy = zcomp_lz4_destroy,
55− .name = "lz4",
56−};
drivers/block/zram/zcomp_lz4.h+0−17
@@ -1,17 +0,0 @@
1−/*
2− * Copyright (C) 2014 Sergey Senozhatsky.
3− *
4− * This program is free software; you can redistribute it and/or
5− * modify it under the terms of the GNU General Public License
6− * as published by the Free Software Foundation; either version
7− * 2 of the License, or (at your option) any later version.
8− */
9−
10−#ifndef _ZCOMP_LZ4_H_
11−#define _ZCOMP_LZ4_H_
12−
13−#include "zcomp.h"
14−
15−extern struct zcomp_backend zcomp_lz4;
16−
17−#endif /* _ZCOMP_LZ4_H_ */
drivers/block/zram/zcomp_lzo.c+0−56
@@ -1,56 +0,0 @@
1−/*
2− * Copyright (C) 2014 Sergey Senozhatsky.
3− *
4− * This program is free software; you can redistribute it and/or
5− * modify it under the terms of the GNU General Public License
6− * as published by the Free Software Foundation; either version
7− * 2 of the License, or (at your option) any later version.
8− */
9−
10−#include <linux/kernel.h>
11−#include <linux/slab.h>
12−#include <linux/lzo.h>
13−#include <linux/vmalloc.h>
14−#include <linux/mm.h>
15−
16−#include "zcomp_lzo.h"
17−
18−static void *lzo_create(gfp_t flags)
19−{
20− void *ret;
21−
22− ret = kmalloc(LZO1X_MEM_COMPRESS, flags);
23− if (!ret)
24− ret = __vmalloc(LZO1X_MEM_COMPRESS,
25− flags | __GFP_HIGHMEM,
26− PAGE_KERNEL);
27− return ret;
28−}
29−
30−static void lzo_destroy(void *private)
31−{
32− kvfree(private);
33−}
34−
35−static int lzo_compress(const unsigned char *src, unsigned char *dst,
36− size_t *dst_len, void *private)
37−{
38− int ret = lzo1x_1_compress(src, PAGE_SIZE, dst, dst_len, private);
39− return ret == LZO_E_OK ? 0 : ret;
40−}
41−
42−static int lzo_decompress(const unsigned char *src, size_t src_len,
43− unsigned char *dst)
44−{
45− size_t dst_len = PAGE_SIZE;
46− int ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len);
47− return ret == LZO_E_OK ? 0 : ret;
48−}
49−
50−struct zcomp_backend zcomp_lzo = {
51− .compress = lzo_compress,
52− .decompress = lzo_decompress,
53− .create = lzo_create,
54− .destroy = lzo_destroy,
55− .name = "lzo",
56−};
drivers/block/zram/zcomp_lzo.h+0−17
@@ -1,17 +0,0 @@
1−/*
2− * Copyright (C) 2014 Sergey Senozhatsky.
3− *
4− * This program is free software; you can redistribute it and/or
5− * modify it under the terms of the GNU General Public License
6− * as published by the Free Software Foundation; either version
7− * 2 of the License, or (at your option) any later version.
8− */
9−
10−#ifndef _ZCOMP_LZO_H_
11−#define _ZCOMP_LZO_H_
12−
13−#include "zcomp.h"
14−
15−extern struct zcomp_backend zcomp_lzo;
16−
17−#endif /* _ZCOMP_LZO_H_ */
drivers/block/zram/zram_drv.c+28−20
@@ -342,9 +342,16 @@ static ssize_t comp_algorithm_store(struct device *dev, · 341 unmodified lines
342342 struct device_attribute *attr, const char *buf, size_t len)
343343 {
344344 struct zram *zram = dev_to_zram(dev);
345+ char compressor[CRYPTO_MAX_ALG_NAME];
345346 size_t sz;
346347
347− if (!zcomp_available_algorithm(buf))
348+ strlcpy(compressor, buf, sizeof(compressor));
349+ /* ignore trailing newline */
350+ sz = strlen(compressor);
351+ if (sz > 0 && compressor[sz - 1] == '\n')
352+ compressor[sz - 1] = 0x00;
353+
354+ if (!zcomp_available_algorithm(compressor))
348355 return -EINVAL;
349356
350357 down_write(&zram->init_lock);
@@ -353,13 +360,8 @@ static ssize_t comp_algorithm_store(struct device *dev, · 2 unmodified lines
353360 pr_info("Can't change algorithm for initialized device\n");
354361 return -EBUSY;
355362 }
356− strlcpy(zram->compressor, buf, sizeof(zram->compressor));
357−
358− /* ignore trailing newline */
359− sz = strlen(zram->compressor);
360− if (sz > 0 && zram->compressor[sz - 1] == '\n')
361− zram->compressor[sz - 1] = 0x00;
362363
364+ strlcpy(zram->compressor, compressor, sizeof(compressor));
363365 up_write(&zram->init_lock);
364366 return len;
365367 }
@@ -563,7 +565,7 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index) · 197 unmodified lines
563565 unsigned char *cmem;
564566 struct zram_meta *meta = zram->meta;
565567 unsigned long handle;
566− size_t size;
568+ unsigned int size;
567569
568570 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
569571 handle = meta->table[index].handle;
@@ -576,10 +578,14 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index) · 6 unmodified lines
576578 }
577579
578580 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
579− if (size == PAGE_SIZE)
581+ if (size == PAGE_SIZE) {
580582 copy_page(mem, cmem);
581− else
582− ret = zcomp_decompress(zram->comp, cmem, size, mem);
583+ } else {
584+ struct zcomp_strm *zstrm = zcomp_stream_get(zram->comp);
585+
586+ ret = zcomp_decompress(zstrm, cmem, size, mem);
587+ zcomp_stream_put(zram->comp);
588+ }
583589 zs_unmap_object(meta->mem_pool, handle);
584590 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
585591
@@ -646,7 +652,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, · 60 unmodified lines
646652 int offset)
647653 {
648654 int ret = 0;
649− size_t clen;
655+ unsigned int clen;
650656 unsigned long handle = 0;
651657 struct page *page;
652658 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
@@ -695,8 +701,8 @@ compress_again: · 42 unmodified lines
695701 goto out;
696702 }
697703
698− zstrm = zcomp_strm_find(zram->comp);
699− ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
704+ zstrm = zcomp_stream_get(zram->comp);
705+ ret = zcomp_compress(zstrm, uncmem, &clen);
700706 if (!is_partial_io(bvec)) {
701707 kunmap_atomic(user_mem);
702708 user_mem = NULL;
@@ -732,19 +738,21 @@ compress_again: · 29 unmodified lines
732738 handle = zs_malloc(meta->mem_pool, clen,
733739 __GFP_KSWAPD_RECLAIM |
734740 __GFP_NOWARN |
735− __GFP_HIGHMEM);
741+ __GFP_HIGHMEM |
742+ __GFP_MOVABLE);
736743 if (!handle) {
737− zcomp_strm_release(zram->comp, zstrm);
744+ zcomp_stream_put(zram->comp);
738745 zstrm = NULL;
739746
740747 atomic64_inc(&zram->stats.writestall);
741748
742749 handle = zs_malloc(meta->mem_pool, clen,
743− GFP_NOIO | __GFP_HIGHMEM);
750+ GFP_NOIO | __GFP_HIGHMEM |
751+ __GFP_MOVABLE);
744752 if (handle)
745753 goto compress_again;
746754
747− pr_err("Error allocating memory for compressed page: %u, size=%zu\n",
755+ pr_err("Error allocating memory for compressed page: %u, size=%u\n",
748756 index, clen);
749757 ret = -ENOMEM;
750758 goto out;
@@ -769,7 +777,7 @@ compress_again: · 18 unmodified lines
769777 memcpy(cmem, src, clen);
770778 }
771779
772− zcomp_strm_release(zram->comp, zstrm);
780+ zcomp_stream_put(zram->comp);
773781 zstrm = NULL;
774782 zs_unmap_object(meta->mem_pool, handle);
775783
@@ -789,7 +797,7 @@ compress_again: · 13 unmodified lines
789797 atomic64_inc(&zram->stats.pages_stored);
790798 out:
791799 if (zstrm)
792− zcomp_strm_release(zram->comp, zstrm);
800+ zcomp_stream_put(zram->comp);
793801 if (is_partial_io(bvec))
794802 kfree(uncmem);
795803 return ret;
drivers/block/zram/zram_drv.h+3−2
@@ -15,8 +15,9 @@ · 14 unmodified lines
1515 #ifndef _ZRAM_DRV_H_
1616 #define _ZRAM_DRV_H_
1717
18−#include <linux/spinlock.h>
18+#include <linux/rwsem.h>
1919 #include <linux/zsmalloc.h>
20+#include <linux/crypto.h>
2021
2122 #include "zcomp.h"
2223
@@ -113,7 +114,7 @@ struct zram { · 90 unmodified lines
113114 * we can store in a disk.
114115 */
115116 u64 disksize; /* bytes */
116− char compressor[10];
117+ char compressor[CRYPTO_MAX_ALG_NAME];
117118 /*
118119 * zram is claimed so open request will be failed
119120 */
drivers/char/mem.c+24−0
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #include <linux/device.h>
2323 #include <linux/highmem.h>
2424 #include <linux/backing-dev.h>
25+#include <linux/shmem_fs.h>
2526 #include <linux/splice.h>
2627 #include <linux/pfn.h>
2728 #include <linux/export.h>
@@ -657,6 +658,28 @@ static int mmap_zero(struct file *file, struct vm_area_struct *vma) · 629 unmodified lines
657658 return 0;
658659 }
659660
661+static unsigned long get_unmapped_area_zero(struct file *file,
662+ unsigned long addr, unsigned long len,
663+ unsigned long pgoff, unsigned long flags)
664+{
665+#ifdef CONFIG_MMU
666+ if (flags & MAP_SHARED) {
667+ /*
668+ * mmap_zero() will call shmem_zero_setup() to create a file,
669+ * so use shmem's get_unmapped_area in case it can be huge;
670+ * and pass NULL for file as in mmap.c's get_unmapped_area(),
671+ * so as not to confuse shmem with our handle on "/dev/zero".
672+ */
673+ return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
674+ }
675+
676+ /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
677+ return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
678+#else
679+ return -ENOSYS;
680+#endif
681+}
682+
660683 static ssize_t write_full(struct file *file, const char __user *buf,
661684 size_t count, loff_t *ppos)
662685 {
@@ -764,6 +787,7 @@ static const struct file_operations zero_fops = { · 101 unmodified lines
764787 .read_iter = read_iter_zero,
765788 .write_iter = write_iter_zero,
766789 .mmap = mmap_zero,
790+ .get_unmapped_area = get_unmapped_area_zero,
767791 #ifndef CONFIG_MMU
768792 .mmap_capabilities = zero_mmap_capabilities,
769793 #endif
drivers/iommu/amd_iommu_v2.c+1−2
@@ -538,8 +538,7 @@ static void do_fault(struct work_struct *work) · 537 unmodified lines
538538 if (access_error(vma, fault))
539539 goto out;
540540
541− ret = handle_mm_fault(mm, vma, address, flags);
542−
541+ ret = handle_mm_fault(vma, address, flags);
543542 out:
544543 up_read(&mm->mmap_sem);
545544
drivers/iommu/intel-svm.c+1−1
@@ -583,7 +583,7 @@ static irqreturn_t prq_event_thread(int irq, void *d) · 582 unmodified lines
583583 if (access_error(vma, req))
584584 goto invalid;
585585
586− ret = handle_mm_fault(svm->mm, vma, address,
586+ ret = handle_mm_fault(vma, address,
587587 req->wr_req ? FAULT_FLAG_WRITE : 0);
588588 if (ret & VM_FAULT_ERROR)
589589 goto invalid;
drivers/tty/sysrq.c+1−0
@@ -363,6 +363,7 @@ static void moom_callback(struct work_struct *ignored) · 362 unmodified lines
363363 struct oom_control oc = {
364364 .zonelist = node_zonelist(first_memory_node, gfp_mask),
365365 .nodemask = NULL,
366+ .memcg = NULL,
366367 .gfp_mask = gfp_mask,
367368 .order = -1,
368369 };
drivers/video/fbdev/core/fbmon.c+0−1
@@ -1496,7 +1496,6 @@ int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var) · 1495 unmodified lines
14961496 }
14971497 void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs)
14981498 {
1499− specs = NULL;
15001499 }
15011500 void fb_edid_add_monspecs(unsigned char *edid, struct fb_monspecs *specs)
15021501 {
drivers/virtio/virtio_balloon.c+47−5
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030 #include <linux/oom.h>
3131 #include <linux/wait.h>
3232 #include <linux/mm.h>
33+#include <linux/mount.h>
3334
3435 /*
3536 * Balloon device works in 4K page units. So each page is pointed to by
@@ -45,6 +46,10 @@ static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES; · 9 unmodified lines
4546 module_param(oom_pages, int, S_IRUSR | S_IWUSR);
4647 MODULE_PARM_DESC(oom_pages, "pages to free on OOM");
4748
49+#ifdef CONFIG_BALLOON_COMPACTION
50+static struct vfsmount *balloon_mnt;
51+#endif
52+
4853 struct virtio_balloon {
4954 struct virtio_device *vdev;
5055 struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
@@ -490,6 +495,24 @@ static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info, · 439 unmodified lines
490495
491496 return MIGRATEPAGE_SUCCESS;
492497 }
498+
499+static struct dentry *balloon_mount(struct file_system_type *fs_type,
500+ int flags, const char *dev_name, void *data)
501+{
502+ static const struct dentry_operations ops = {
503+ .d_dname = simple_dname,
504+ };
505+
506+ return mount_pseudo(fs_type, "balloon-kvm:", NULL, &ops,
507+ BALLOON_KVM_MAGIC);
508+}
509+
510+static struct file_system_type balloon_fs = {
511+ .name = "balloon-kvm",
512+ .mount = balloon_mount,
513+ .kill_sb = kill_anon_super,
514+};
515+
493516 #endif /* CONFIG_BALLOON_COMPACTION */
494517
495518 static int virtballoon_probe(struct virtio_device *vdev)
@@ -519,9 +542,6 @@ static int virtballoon_probe(struct virtio_device *vdev) · 23 unmodified lines
519542 vb->vdev = vdev;
520543
521544 balloon_devinfo_init(&vb->vb_dev_info);
522−#ifdef CONFIG_BALLOON_COMPACTION
523− vb->vb_dev_info.migratepage = virtballoon_migratepage;
524−#endif
525545
526546 err = init_vqs(vb);
527547 if (err)
@@ -531,13 +551,33 @@ static int virtballoon_probe(struct virtio_device *vdev) · 3 unmodified lines
531551 vb->nb.priority = VIRTBALLOON_OOM_NOTIFY_PRIORITY;
532552 err = register_oom_notifier(&vb->nb);
533553 if (err < 0)
534− goto out_oom_notify;
554+ goto out_del_vqs;
555+
556+#ifdef CONFIG_BALLOON_COMPACTION
557+ balloon_mnt = kern_mount(&balloon_fs);
558+ if (IS_ERR(balloon_mnt)) {
559+ err = PTR_ERR(balloon_mnt);
560+ unregister_oom_notifier(&vb->nb);
561+ goto out_del_vqs;
562+ }
563+
564+ vb->vb_dev_info.migratepage = virtballoon_migratepage;
565+ vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
566+ if (IS_ERR(vb->vb_dev_info.inode)) {
567+ err = PTR_ERR(vb->vb_dev_info.inode);
568+ kern_unmount(balloon_mnt);
569+ unregister_oom_notifier(&vb->nb);
570+ vb->vb_dev_info.inode = NULL;
571+ goto out_del_vqs;
572+ }
573+ vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
574+#endif
535575
536576 virtio_device_ready(vdev);
537577
538578 return 0;
539579
540−out_oom_notify:
580+out_del_vqs:
541581 vdev->config->del_vqs(vdev);
542582 out_free_vb:
543583 kfree(vb);
@@ -571,6 +611,8 @@ static void virtballoon_remove(struct virtio_device *vdev) · 27 unmodified lines
571611 cancel_work_sync(&vb->update_balloon_stats_work);
572612
573613 remove_common(vb);
614+ if (vb->vb_dev_info.inode)
615+ iput(vb->vb_dev_info.inode);
574616 kfree(vb);
575617 }
576618
drivers/xen/xen-selfballoon.c+2−2
@@ -195,7 +195,7 @@ static void selfballoon_process(struct work_struct *work) · 194 unmodified lines
195195 MB2PAGES(selfballoon_reserved_mb);
196196 #ifdef CONFIG_FRONTSWAP
197197 /* allow space for frontswap pages to be repatriated */
198− if (frontswap_selfshrinking && frontswap_enabled)
198+ if (frontswap_selfshrinking)
199199 goal_pages += frontswap_curr_pages();
200200 #endif
201201 if (cur_pages > goal_pages)
@@ -230,7 +230,7 @@ static void selfballoon_process(struct work_struct *work) · 28 unmodified lines
230230 reset_timer = true;
231231 }
232232 #ifdef CONFIG_FRONTSWAP
233− if (frontswap_selfshrinking && frontswap_enabled) {
233+ if (frontswap_selfshrinking) {
234234 frontswap_selfshrink();
235235 reset_timer = true;
236236 }
fs/btrfs/extent_io.c+2−1
@@ -4178,7 +4178,8 @@ int extent_readpages(struct extent_io_tree *tree, · 4177 unmodified lines
41784178 prefetchw(&page->flags);
41794179 list_del(&page->lru);
41804180 if (add_to_page_cache_lru(page, mapping,
4181− page->index, GFP_NOFS)) {
4181+ page->index,
4182+ readahead_gfp_mask(mapping))) {
41824183 put_page(page);
41834184 continue;
41844185 }
fs/cifs/file.c+1−1
@@ -3366,7 +3366,7 @@ readpages_get_pages(struct address_space *mapping, struct list_head *page_list, · 3365 unmodified lines
33663366 struct page *page, *tpage;
33673367 unsigned int expected_index;
33683368 int rc;
3369− gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
3369+ gfp_t gfp = readahead_gfp_mask(mapping);
33703370
33713371 INIT_LIST_HEAD(tmplist);
33723372
fs/dax.c+14−59
@@ -819,16 +819,16 @@ static int dax_insert_mapping(struct address_space *mapping, · 818 unmodified lines
819819 }
820820
821821 /**
822− * __dax_fault - handle a page fault on a DAX file
822+ * dax_fault - handle a page fault on a DAX file
823823 * @vma: The virtual memory area where the fault occurred
824824 * @vmf: The description of the fault
825825 * @get_block: The filesystem method used to translate file offsets to blocks
826826 *
827827 * When a page fault occurs, filesystems may call this helper in their
828− * fault handler for DAX files. __dax_fault() assumes the caller has done all
828+ * fault handler for DAX files. dax_fault() assumes the caller has done all
829829 * the necessary locking for the page fault to proceed successfully.
830830 */
831−int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
831+int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
832832 get_block_t get_block)
833833 {
834834 struct file *file = vma->vm_file;
@@ -913,33 +913,6 @@ int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, · 78 unmodified lines
913913 return VM_FAULT_SIGBUS | major;
914914 return VM_FAULT_NOPAGE | major;
915915 }
916−EXPORT_SYMBOL(__dax_fault);
917−
918−/**
919− * dax_fault - handle a page fault on a DAX file
920− * @vma: The virtual memory area where the fault occurred
921− * @vmf: The description of the fault
922− * @get_block: The filesystem method used to translate file offsets to blocks
923− *
924− * When a page fault occurs, filesystems may call this helper in their
925− * fault handler for DAX files.
926− */
927−int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
928− get_block_t get_block)
929−{
930− int result;
931− struct super_block *sb = file_inode(vma->vm_file)->i_sb;
932−
933− if (vmf->flags & FAULT_FLAG_WRITE) {
934− sb_start_pagefault(sb);
935− file_update_time(vma->vm_file);
936− }
937− result = __dax_fault(vma, vmf, get_block);
938− if (vmf->flags & FAULT_FLAG_WRITE)
939− sb_end_pagefault(sb);
940−
941− return result;
942−}
943916 EXPORT_SYMBOL_GPL(dax_fault);
944917
945918 #if defined(CONFIG_TRANSPARENT_HUGEPAGE)
@@ -967,7 +940,16 @@ static void __dax_dbg(struct buffer_head *bh, unsigned long address, · 21 unmodified lines
967940
968941 #define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
969942
970−int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
943+/**
944+ * dax_pmd_fault - handle a PMD fault on a DAX file
945+ * @vma: The virtual memory area where the fault occurred
946+ * @vmf: The description of the fault
947+ * @get_block: The filesystem method used to translate file offsets to blocks
948+ *
949+ * When a page fault occurs, filesystems may call this helper in their
950+ * pmd_fault handler for DAX files.
951+ */
952+int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
971953 pmd_t *pmd, unsigned int flags, get_block_t get_block)
972954 {
973955 struct file *file = vma->vm_file;
@@ -1119,7 +1101,7 @@ int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, · 145 unmodified lines
11191101 *
11201102 * The PMD path doesn't have an equivalent to
11211103 * dax_pfn_mkwrite(), though, so for a read followed by a
1122− * write we traverse all the way through __dax_pmd_fault()
1104+ * write we traverse all the way through dax_pmd_fault()
11231105 * twice. This means we can just skip inserting a radix tree
11241106 * entry completely on the initial read and just wait until
11251107 * the write to insert a dirty entry.
@@ -1148,33 +1130,6 @@ int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, · 22 unmodified lines
11481130 result = VM_FAULT_FALLBACK;
11491131 goto out;
11501132 }
1151−EXPORT_SYMBOL_GPL(__dax_pmd_fault);
1152−
1153−/**
1154− * dax_pmd_fault - handle a PMD fault on a DAX file
1155− * @vma: The virtual memory area where the fault occurred
1156− * @vmf: The description of the fault
1157− * @get_block: The filesystem method used to translate file offsets to blocks
1158− *
1159− * When a page fault occurs, filesystems may call this helper in their
1160− * pmd_fault handler for DAX files.
1161− */
1162−int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
1163− pmd_t *pmd, unsigned int flags, get_block_t get_block)
1164−{
1165− int result;
1166− struct super_block *sb = file_inode(vma->vm_file)->i_sb;
1167−
1168− if (flags & FAULT_FLAG_WRITE) {
1169− sb_start_pagefault(sb);
1170− file_update_time(vma->vm_file);
1171− }
1172− result = __dax_pmd_fault(vma, address, pmd, flags, get_block);
1173− if (flags & FAULT_FLAG_WRITE)
1174− sb_end_pagefault(sb);
1175−
1176− return result;
1177−}
11781133 EXPORT_SYMBOL_GPL(dax_pmd_fault);
11791134 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
11801135
fs/ext2/file.c+2−2
@@ -51,7 +51,7 @@ static int ext2_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · 50 unmodified lines
5151 }
5252 down_read(&ei->dax_sem);
5353
54− ret = __dax_fault(vma, vmf, ext2_get_block);
54+ ret = dax_fault(vma, vmf, ext2_get_block);
5555
5656 up_read(&ei->dax_sem);
5757 if (vmf->flags & FAULT_FLAG_WRITE)
@@ -72,7 +72,7 @@ static int ext2_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr, · 14 unmodified lines
7272 }
7373 down_read(&ei->dax_sem);
7474
75− ret = __dax_pmd_fault(vma, addr, pmd, flags, ext2_get_block);
75+ ret = dax_pmd_fault(vma, addr, pmd, flags, ext2_get_block);
7676
7777 up_read(&ei->dax_sem);
7878 if (flags & FAULT_FLAG_WRITE)
fs/ext4/file.c+2−2
@@ -202,7 +202,7 @@ static int ext4_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · 201 unmodified lines
202202 if (IS_ERR(handle))
203203 result = VM_FAULT_SIGBUS;
204204 else
205− result = __dax_fault(vma, vmf, ext4_dax_get_block);
205+ result = dax_fault(vma, vmf, ext4_dax_get_block);
206206
207207 if (write) {
208208 if (!IS_ERR(handle))
@@ -237,7 +237,7 @@ static int ext4_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr, · 28 unmodified lines
237237 if (IS_ERR(handle))
238238 result = VM_FAULT_SIGBUS;
239239 else
240− result = __dax_pmd_fault(vma, addr, pmd, flags,
240+ result = dax_pmd_fault(vma, addr, pmd, flags,
241241 ext4_dax_get_block);
242242
243243 if (write) {
fs/ext4/readpage.c+1−1
@@ -130,7 +130,7 @@ int ext4_mpage_readpages(struct address_space *mapping, · 129 unmodified lines
130130 page = list_entry(pages->prev, struct page, lru);
131131 list_del(&page->lru);
132132 if (add_to_page_cache_lru(page, mapping, page->index,
133− mapping_gfp_constraint(mapping, GFP_KERNEL)))
133+ readahead_gfp_mask(mapping)))
134134 goto next_page;
135135 }
136136
fs/f2fs/data.c+2−1
@@ -1002,7 +1002,8 @@ static int f2fs_mpage_readpages(struct address_space *mapping, · 1001 unmodified lines
10021002 page = list_entry(pages->prev, struct page, lru);
10031003 list_del(&page->lru);
10041004 if (add_to_page_cache_lru(page, mapping,
1005− page->index, GFP_KERNEL))
1005+ page->index,
1006+ readahead_gfp_mask(mapping)))
10061007 goto next_page;
10071008 }
10081009
fs/fs-writeback.c+86−25
@@ -980,6 +980,42 @@ void inode_io_list_del(struct inode *inode) · 979 unmodified lines
980980 spin_unlock(&wb->list_lock);
981981 }
982982
983+/*
984+ * mark an inode as under writeback on the sb
985+ */
986+void sb_mark_inode_writeback(struct inode *inode)
987+{
988+ struct super_block *sb = inode->i_sb;
989+ unsigned long flags;
990+
991+ if (list_empty(&inode->i_wb_list)) {
992+ spin_lock_irqsave(&sb->s_inode_wblist_lock, flags);
993+ if (list_empty(&inode->i_wb_list)) {
994+ list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb);
995+ trace_sb_mark_inode_writeback(inode);
996+ }
997+ spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags);
998+ }
999+}
1000+
1001+/*
1002+ * clear an inode as under writeback on the sb
1003+ */
1004+void sb_clear_inode_writeback(struct inode *inode)
1005+{
1006+ struct super_block *sb = inode->i_sb;
1007+ unsigned long flags;
1008+
1009+ if (!list_empty(&inode->i_wb_list)) {
1010+ spin_lock_irqsave(&sb->s_inode_wblist_lock, flags);
1011+ if (!list_empty(&inode->i_wb_list)) {
1012+ list_del_init(&inode->i_wb_list);
1013+ trace_sb_clear_inode_writeback(inode);
1014+ }
1015+ spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags);
1016+ }
1017+}
1018+
9831019 /*
9841020 * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
9851021 * furthest end of its superblock's dirty-inode list.
@@ -2154,7 +2190,7 @@ EXPORT_SYMBOL(__mark_inode_dirty); · 1168 unmodified lines
21542190 */
21552191 static void wait_sb_inodes(struct super_block *sb)
21562192 {
2157− struct inode *inode, *old_inode = NULL;
2193+ LIST_HEAD(sync_list);
21582194
21592195 /*
21602196 * We need to be protected against the filesystem going from
@@ -2163,38 +2199,60 @@ static void wait_sb_inodes(struct super_block *sb) · 2 unmodified lines
21632199 WARN_ON(!rwsem_is_locked(&sb->s_umount));
21642200
21652201 mutex_lock(&sb->s_sync_lock);
2166− spin_lock(&sb->s_inode_list_lock);
21672202
21682203 /*
2169− * Data integrity sync. Must wait for all pages under writeback,
2170− * because there may have been pages dirtied before our sync
2171− * call, but which had writeout started before we write it out.
2172− * In which case, the inode may not be on the dirty list, but
2173− * we still have to wait for that writeout.
2204+ * Splice the writeback list onto a temporary list to avoid waiting on
2205+ * inodes that have started writeback after this point.
2206+ *
2207+ * Use rcu_read_lock() to keep the inodes around until we have a
2208+ * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as
2209+ * the local list because inodes can be dropped from either by writeback
2210+ * completion.
2211+ */
2212+ rcu_read_lock();
2213+ spin_lock_irq(&sb->s_inode_wblist_lock);
2214+ list_splice_init(&sb->s_inodes_wb, &sync_list);
2215+
2216+ /*
2217+ * Data integrity sync. Must wait for all pages under writeback, because
2218+ * there may have been pages dirtied before our sync call, but which had
2219+ * writeout started before we write it out. In which case, the inode
2220+ * may not be on the dirty list, but we still have to wait for that
2221+ * writeout.
21742222 */
2175− list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
2223+ while (!list_empty(&sync_list)) {
2224+ struct inode *inode = list_first_entry(&sync_list, struct inode,
2225+ i_wb_list);
21762226 struct address_space *mapping = inode->i_mapping;
21772227
2228+ /*
2229+ * Move each inode back to the wb list before we drop the lock
2230+ * to preserve consistency between i_wb_list and the mapping
2231+ * writeback tag. Writeback completion is responsible to remove
2232+ * the inode from either list once the writeback tag is cleared.
2233+ */
2234+ list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb);
2235+
2236+ /*
2237+ * The mapping can appear untagged while still on-list since we
2238+ * do not have the mapping lock. Skip it here, wb completion
2239+ * will remove it.
2240+ */
2241+ if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK))
2242+ continue;
2243+
2244+ spin_unlock_irq(&sb->s_inode_wblist_lock);
2245+
21782246 spin_lock(&inode->i_lock);
2179− if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
2180− (mapping->nrpages == 0)) {
2247+ if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
21812248 spin_unlock(&inode->i_lock);
2249+
2250+ spin_lock_irq(&sb->s_inode_wblist_lock);
21822251 continue;
21832252 }
21842253 __iget(inode);
21852254 spin_unlock(&inode->i_lock);
2186− spin_unlock(&sb->s_inode_list_lock);
2187−
2188− /*
2189− * We hold a reference to 'inode' so it couldn't have been
2190− * removed from s_inodes list while we dropped the
2191− * s_inode_list_lock. We cannot iput the inode now as we can
2192− * be holding the last reference and we cannot iput it under
2193− * s_inode_list_lock. So we keep the reference and iput it
2194− * later.
2195− */
2196− iput(old_inode);
2197− old_inode = inode;
2255+ rcu_read_unlock();
21982256
21992257 /*
22002258 * We keep the error status of individual mapping so that
@@ -2205,10 +2263,13 @@ static void wait_sb_inodes(struct super_block *sb) · 4 unmodified lines
22052263
22062264 cond_resched();
22072265
2208− spin_lock(&sb->s_inode_list_lock);
2266+ iput(inode);
2267+
2268+ rcu_read_lock();
2269+ spin_lock_irq(&sb->s_inode_wblist_lock);
22092270 }
2210− spin_unlock(&sb->s_inode_list_lock);
2211− iput(old_inode);
2271+ spin_unlock_irq(&sb->s_inode_wblist_lock);
2272+ rcu_read_unlock();
22122273 mutex_unlock(&sb->s_sync_lock);
22132274 }
22142275
fs/inode.c+2−0
@@ -365,6 +365,7 @@ void inode_init_once(struct inode *inode) · 364 unmodified lines
365365 INIT_HLIST_NODE(&inode->i_hash);
366366 INIT_LIST_HEAD(&inode->i_devices);
367367 INIT_LIST_HEAD(&inode->i_io_list);
368+ INIT_LIST_HEAD(&inode->i_wb_list);
368369 INIT_LIST_HEAD(&inode->i_lru);
369370 address_space_init_once(&inode->i_data);
370371 i_size_ordered_init(inode);
@@ -507,6 +508,7 @@ void clear_inode(struct inode *inode) · 136 unmodified lines
507508 BUG_ON(!list_empty(&inode->i_data.private_list));
508509 BUG_ON(!(inode->i_state & I_FREEING));
509510 BUG_ON(inode->i_state & I_CLEAR);
511+ BUG_ON(!list_empty(&inode->i_wb_list));
510512 /* don't need i_lock here, no concurrent mods to i_state */
511513 inode->i_state = I_FREEING | I_CLEAR;
512514 }
fs/mpage.c+3−1
@@ -72,6 +72,8 @@ mpage_alloc(struct block_device *bdev, · 71 unmodified lines
7272 {
7373 struct bio *bio;
7474
75+ /* Restrict the given (page cache) mask for slab allocations */
76+ gfp_flags &= GFP_KERNEL;
7577 bio = bio_alloc(gfp_flags, nr_vecs);
7678
7779 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
@@ -363,7 +365,7 @@ mpage_readpages(struct address_space *mapping, struct list_head *pages, · 285 unmodified lines
363365 sector_t last_block_in_bio = 0;
364366 struct buffer_head map_bh;
365367 unsigned long first_logical_block = 0;
366− gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
368+ gfp_t gfp = readahead_gfp_mask(mapping);
367369
368370 map_bh.b_state = 0;
369371 map_bh.b_size = 0;
fs/ocfs2/cluster/tcp.c+2−6
@@ -1618,16 +1618,12 @@ static void o2net_start_connect(struct work_struct *work) · 1617 unmodified lines
16181618
16191619 /* watch for racing with tearing a node down */
16201620 node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
1621− if (node == NULL) {
1622− ret = 0;
1621+ if (node == NULL)
16231622 goto out;
1624− }
16251623
16261624 mynode = o2nm_get_node_by_num(o2nm_this_node());
1627− if (mynode == NULL) {
1628− ret = 0;
1625+ if (mynode == NULL)
16291626 goto out;
1630− }
16311627
16321628 spin_lock(&nn->nn_lock);
16331629 /*
fs/ocfs2/dlm/dlmdebug.c+2−24
@@ -347,26 +347,6 @@ static struct dentry *dlm_debugfs_root; · 346 unmodified lines
347347 #define DLM_DEBUGFS_PURGE_LIST "purge_list"
348348
349349 /* begin - utils funcs */
350−static void dlm_debug_free(struct kref *kref)
351−{
352− struct dlm_debug_ctxt *dc;
353−
354− dc = container_of(kref, struct dlm_debug_ctxt, debug_refcnt);
355−
356− kfree(dc);
357−}
358−
359−static void dlm_debug_put(struct dlm_debug_ctxt *dc)
360−{
361− if (dc)
362− kref_put(&dc->debug_refcnt, dlm_debug_free);
363−}
364−
365−static void dlm_debug_get(struct dlm_debug_ctxt *dc)
366−{
367− kref_get(&dc->debug_refcnt);
368−}
369−
370350 static int debug_release(struct inode *inode, struct file *file)
371351 {
372352 free_page((unsigned long)file->private_data);
@@ -932,11 +912,9 @@ int dlm_debug_init(struct dlm_ctxt *dlm) · 559 unmodified lines
932912 goto bail;
933913 }
934914
935− dlm_debug_get(dc);
936915 return 0;
937916
938917 bail:
939− dlm_debug_shutdown(dlm);
940918 return -ENOMEM;
941919 }
942920
@@ -949,7 +927,8 @@ void dlm_debug_shutdown(struct dlm_ctxt *dlm) · 6 unmodified lines
949927 debugfs_remove(dc->debug_mle_dentry);
950928 debugfs_remove(dc->debug_lockres_dentry);
951929 debugfs_remove(dc->debug_state_dentry);
952− dlm_debug_put(dc);
930+ kfree(dc);
931+ dc = NULL;
953932 }
954933 }
955934
@@ -969,7 +948,6 @@ int dlm_create_debugfs_subroot(struct dlm_ctxt *dlm) · 13 unmodified lines
969948 mlog_errno(-ENOMEM);
970949 goto bail;
971950 }
972− kref_init(&dlm->dlm_debug_ctxt->debug_refcnt);
973951
974952 return 0;
975953 bail:
fs/ocfs2/dlm/dlmdebug.h+0−1
@@ -30,7 +30,6 @@ void dlm_print_one_mle(struct dlm_master_list_entry *mle); · 29 unmodified lines
3030 #ifdef CONFIG_DEBUG_FS
3131
3232 struct dlm_debug_ctxt {
33− struct kref debug_refcnt;
3433 struct dentry *debug_state_dentry;
3534 struct dentry *debug_lockres_dentry;
3635 struct dentry *debug_mle_dentry;
fs/ocfs2/dlmglue.c+1−12
@@ -1635,7 +1635,6 @@ int ocfs2_create_new_inode_locks(struct inode *inode) · 1634 unmodified lines
16351635 int ret;
16361636 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
16371637
1638− BUG_ON(!inode);
16391638 BUG_ON(!ocfs2_inode_is_new(inode));
16401639
16411640 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
@@ -1665,10 +1664,8 @@ int ocfs2_create_new_inode_locks(struct inode *inode) · 23 unmodified lines
16651664 }
16661665
16671666 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1668− if (ret) {
1667+ if (ret)
16691668 mlog_errno(ret);
1670− goto bail;
1671− }
16721669
16731670 bail:
16741671 return ret;
@@ -1680,8 +1677,6 @@ int ocfs2_rw_lock(struct inode *inode, int write) · 5 unmodified lines
16801677 struct ocfs2_lock_res *lockres;
16811678 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
16821679
1683− BUG_ON(!inode);
1684−
16851680 mlog(0, "inode %llu take %s RW lock\n",
16861681 (unsigned long long)OCFS2_I(inode)->ip_blkno,
16871682 write ? "EXMODE" : "PRMODE");
@@ -1724,8 +1719,6 @@ int ocfs2_open_lock(struct inode *inode) · 36 unmodified lines
17241719 struct ocfs2_lock_res *lockres;
17251720 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
17261721
1727− BUG_ON(!inode);
1728−
17291722 mlog(0, "inode %llu take PRMODE open lock\n",
17301723 (unsigned long long)OCFS2_I(inode)->ip_blkno);
17311724
@@ -1749,8 +1742,6 @@ int ocfs2_try_open_lock(struct inode *inode, int write) · 17 unmodified lines
17491742 struct ocfs2_lock_res *lockres;
17501743 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
17511744
1752− BUG_ON(!inode);
1753−
17541745 mlog(0, "inode %llu try to take %s open lock\n",
17551746 (unsigned long long)OCFS2_I(inode)->ip_blkno,
17561747 write ? "EXMODE" : "PRMODE");
@@ -2328,8 +2319,6 @@ int ocfs2_inode_lock_full_nested(struct inode *inode, · 571 unmodified lines
23282319 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
23292320 struct buffer_head *local_bh = NULL;
23302321
2331− BUG_ON(!inode);
2332−
23332322 mlog(0, "inode %llu, take %s META lock\n",
23342323 (unsigned long long)OCFS2_I(inode)->ip_blkno,
23352324 ex ? "EXMODE" : "PRMODE");
fs/ocfs2/inode.h+0−7
@@ -145,22 +145,15 @@ int ocfs2_drop_inode(struct inode *inode); · 144 unmodified lines
145145 struct inode *ocfs2_ilookup(struct super_block *sb, u64 feoff);
146146 struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 feoff, unsigned flags,
147147 int sysfile_type);
148−int ocfs2_inode_init_private(struct inode *inode);
149148 int ocfs2_inode_revalidate(struct dentry *dentry);
150149 void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
151150 int create_ino);
152−void ocfs2_read_inode(struct inode *inode);
153−void ocfs2_read_inode2(struct inode *inode, void *opaque);
154−ssize_t ocfs2_rw_direct(int rw, struct file *filp, char *buf,
155− size_t size, loff_t *offp);
156151 void ocfs2_sync_blockdev(struct super_block *sb);
157152 void ocfs2_refresh_inode(struct inode *inode,
158153 struct ocfs2_dinode *fe);
159154 int ocfs2_mark_inode_dirty(handle_t *handle,
160155 struct inode *inode,
161156 struct buffer_head *bh);
162−struct buffer_head *ocfs2_bread(struct inode *inode,
163− int block, int *err, int reada);
164157
165158 void ocfs2_set_inode_flags(struct inode *inode);
166159 void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi);
fs/ocfs2/journal.c+21−20
@@ -1159,10 +1159,8 @@ static int ocfs2_force_read_journal(struct inode *inode) · 1158 unmodified lines
11591159 int status = 0;
11601160 int i;
11611161 u64 v_blkno, p_blkno, p_blocks, num_blocks;
1162−#define CONCURRENT_JOURNAL_FILL 32ULL
1163− struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
1164−
1165− memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
1162+ struct buffer_head *bh = NULL;
1163+ struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
11661164
11671165 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
11681166 v_blkno = 0;
@@ -1174,29 +1172,32 @@ static int ocfs2_force_read_journal(struct inode *inode) · 5 unmodified lines
11741172 goto bail;
11751173 }
11761174
1177− if (p_blocks > CONCURRENT_JOURNAL_FILL)
1178− p_blocks = CONCURRENT_JOURNAL_FILL;
1179−
1180− /* We are reading journal data which should not
1181− * be put in the uptodate cache */
1182− status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
1183− p_blkno, p_blocks, bhs);
1184− if (status < 0) {
1185− mlog_errno(status);
1186− goto bail;
1187− }
1175+ for (i = 0; i < p_blocks; i++, p_blkno++) {
1176+ bh = __find_get_block(osb->sb->s_bdev, p_blkno,
1177+ osb->sb->s_blocksize);
1178+ /* block not cached. */
1179+ if (!bh)
1180+ continue;
1181+
1182+ brelse(bh);
1183+ bh = NULL;
1184+ /* We are reading journal data which should not
1185+ * be put in the uptodate cache.
1186+ */
1187+ status = ocfs2_read_blocks_sync(osb, p_blkno, 1, &bh);
1188+ if (status < 0) {
1189+ mlog_errno(status);
1190+ goto bail;
1191+ }
11881192
1189− for(i = 0; i < p_blocks; i++) {
1190− brelse(bhs[i]);
1191− bhs[i] = NULL;
1193+ brelse(bh);
1194+ bh = NULL;
11921195 }
11931196
11941197 v_blkno += p_blocks;
11951198 }
11961199
11971200 bail:
1198− for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
1199− brelse(bhs[i]);
12001201 return status;
12011202 }
12021203
fs/ocfs2/stackglue.c+0−2
@@ -735,8 +735,6 @@ static void __exit ocfs2_stack_glue_exit(void) · 734 unmodified lines
735735 {
736736 memset(&locking_max_version, 0,
737737 sizeof(struct ocfs2_protocol_version));
738− locking_max_version.pv_major = 0;
739− locking_max_version.pv_minor = 0;
740738 ocfs2_sysfs_exit();
741739 if (ocfs2_table_header)
742740 unregister_sysctl_table(ocfs2_table_header);
fs/ocfs2/super.c+0−1
@@ -2072,7 +2072,6 @@ static int ocfs2_initialize_super(struct super_block *sb, · 2071 unmodified lines
20722072 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
20732073
20742074 osb->sb = sb;
2075− /* Save off for ocfs2_rw_direct */
20762075 osb->s_sectsize_bits = blksize_bits(sector_size);
20772076 BUG_ON(!osb->s_sectsize_bits);
20782077
fs/orangefs/inode.c+1−1
@@ -80,7 +80,7 @@ static int orangefs_readpages(struct file *file, · 79 unmodified lines
8080 if (!add_to_page_cache(page,
8181 mapping,
8282 page->index,
83− GFP_KERNEL)) {
83+ readahead_gfp_mask(mapping))) {
8484 ret = read_one_page(page);
8585 gossip_debug(GOSSIP_INODE_DEBUG,
8686 "failure adding page to cache, read_one_page returned: %d\n",
fs/pipe.c+26−6
@@ -21,6 +21,7 @@ · 20 unmodified lines
2121 #include <linux/audit.h>
2222 #include <linux/syscalls.h>
2323 #include <linux/fcntl.h>
24+#include <linux/memcontrol.h>
2425
2526 #include <asm/uaccess.h>
2627 #include <asm/ioctls.h>
@@ -137,6 +138,22 @@ static void anon_pipe_buf_release(struct pipe_inode_info *pipe, · 110 unmodified lines
137138 put_page(page);
138139 }
139140
141+static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
142+ struct pipe_buffer *buf)
143+{
144+ struct page *page = buf->page;
145+
146+ if (page_count(page) == 1) {
147+ if (memcg_kmem_enabled()) {
148+ memcg_kmem_uncharge(page, 0);
149+ __ClearPageKmemcg(page);
150+ }
151+ __SetPageLocked(page);
152+ return 0;
153+ }
154+ return 1;
155+}
156+
140157 /**
141158 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
142159 * @pipe: the pipe that the buffer belongs to
@@ -219,7 +236,7 @@ static const struct pipe_buf_operations anon_pipe_buf_ops = { · 76 unmodified lines
219236 .can_merge = 1,
220237 .confirm = generic_pipe_buf_confirm,
221238 .release = anon_pipe_buf_release,
222− .steal = generic_pipe_buf_steal,
239+ .steal = anon_pipe_buf_steal,
223240 .get = generic_pipe_buf_get,
224241 };
225242
@@ -227,7 +244,7 @@ static const struct pipe_buf_operations packet_pipe_buf_ops = { · 1 unmodified line
227244 .can_merge = 0,
228245 .confirm = generic_pipe_buf_confirm,
229246 .release = anon_pipe_buf_release,
230− .steal = generic_pipe_buf_steal,
247+ .steal = anon_pipe_buf_steal,
231248 .get = generic_pipe_buf_get,
232249 };
233250
@@ -405,7 +422,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) · 171 unmodified lines
405422 int copied;
406423
407424 if (!page) {
408− page = alloc_page(GFP_HIGHUSER);
425+ page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
409426 if (unlikely(!page)) {
410427 ret = ret ? : -ENOMEM;
411428 break;
@@ -611,7 +628,7 @@ struct pipe_inode_info *alloc_pipe_info(void) · 199 unmodified lines
611628 {
612629 struct pipe_inode_info *pipe;
613630
614− pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
631+ pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
615632 if (pipe) {
616633 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
617634 struct user_struct *user = get_current_user();
@@ -619,7 +636,9 @@ struct pipe_inode_info *alloc_pipe_info(void) · 1 unmodified line
619636 if (!too_many_pipe_buffers_hard(user)) {
620637 if (too_many_pipe_buffers_soft(user))
621638 pipe_bufs = 1;
622− pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
639+ pipe->bufs = kcalloc(pipe_bufs,
640+ sizeof(struct pipe_buffer),
641+ GFP_KERNEL_ACCOUNT);
623642 }
624643
625644 if (pipe->bufs) {
@@ -1010,7 +1029,8 @@ static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages) · 384 unmodified lines
10101029 if (nr_pages < pipe->nrbufs)
10111030 return -EBUSY;
10121031
1013− bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
1032+ bufs = kcalloc(nr_pages, sizeof(*bufs),
1033+ GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
10141034 if (unlikely(!bufs))
10151035 return -ENOMEM;
10161036
fs/proc/meminfo.c+5−2
@@ -105,6 +105,8 @@ static int meminfo_proc_show(struct seq_file *m, void *v) · 104 unmodified lines
105105 #endif
106106 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
107107 "AnonHugePages: %8lu kB\n"
108+ "ShmemHugePages: %8lu kB\n"
109+ "ShmemPmdMapped: %8lu kB\n"
108110 #endif
109111 #ifdef CONFIG_CMA
110112 "CmaTotal: %8lu kB\n"
@@ -162,8 +164,9 @@ static int meminfo_proc_show(struct seq_file *m, void *v) · 51 unmodified lines
162164 , atomic_long_read(&num_poisoned_pages) << (PAGE_SHIFT - 10)
163165 #endif
164166 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
165− , K(global_page_state(NR_ANON_TRANSPARENT_HUGEPAGES) *
166− HPAGE_PMD_NR)
167+ , K(global_page_state(NR_ANON_THPS) * HPAGE_PMD_NR)
168+ , K(global_page_state(NR_SHMEM_THPS) * HPAGE_PMD_NR)
169+ , K(global_page_state(NR_SHMEM_PMDMAPPED) * HPAGE_PMD_NR)
167170 #endif
168171 #ifdef CONFIG_CMA
169172 , K(totalcma_pages)
fs/proc/task_mmu.c+9−1
@@ -448,6 +448,7 @@ struct mem_size_stats { · 447 unmodified lines
448448 unsigned long referenced;
449449 unsigned long anonymous;
450450 unsigned long anonymous_thp;
451+ unsigned long shmem_thp;
451452 unsigned long swap;
452453 unsigned long shared_hugetlb;
453454 unsigned long private_hugetlb;
@@ -576,7 +577,12 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, · 122 unmodified lines
576577 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
577578 if (IS_ERR_OR_NULL(page))
578579 return;
579− mss->anonymous_thp += HPAGE_PMD_SIZE;
580+ if (PageAnon(page))
581+ mss->anonymous_thp += HPAGE_PMD_SIZE;
582+ else if (PageSwapBacked(page))
583+ mss->shmem_thp += HPAGE_PMD_SIZE;
584+ else
585+ VM_BUG_ON_PAGE(1, page);
580586 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
581587 }
582588 #else
@@ -770,6 +776,7 @@ static int show_smap(struct seq_file *m, void *v, int is_pid) · 187 unmodified lines
770776 "Referenced: %8lu kB\n"
771777 "Anonymous: %8lu kB\n"
772778 "AnonHugePages: %8lu kB\n"
779+ "ShmemPmdMapped: %8lu kB\n"
773780 "Shared_Hugetlb: %8lu kB\n"
774781 "Private_Hugetlb: %7lu kB\n"
775782 "Swap: %8lu kB\n"
@@ -787,6 +794,7 @@ static int show_smap(struct seq_file *m, void *v, int is_pid) · 11 unmodified lines
787794 mss.referenced >> 10,
788795 mss.anonymous >> 10,
789796 mss.anonymous_thp >> 10,
797+ mss.shmem_thp >> 10,
790798 mss.shared_hugetlb >> 10,
791799 mss.private_hugetlb >> 10,
792800 mss.swap >> 10,
fs/super.c+2−0
@@ -206,6 +206,8 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags) · 205 unmodified lines
206206 mutex_init(&s->s_sync_lock);
207207 INIT_LIST_HEAD(&s->s_inodes);
208208 spin_lock_init(&s->s_inode_list_lock);
209+ INIT_LIST_HEAD(&s->s_inodes_wb);
210+ spin_lock_init(&s->s_inode_wblist_lock);
209211
210212 if (list_lru_init_memcg(&s->s_dentry_lru))
211213 goto fail;
fs/userfaultfd.c+11−11
@@ -257,10 +257,9 @@ out: · 256 unmodified lines
257257 * fatal_signal_pending()s, and the mmap_sem must be released before
258258 * returning it.
259259 */
260−int handle_userfault(struct vm_area_struct *vma, unsigned long address,
261− unsigned int flags, unsigned long reason)
260+int handle_userfault(struct fault_env *fe, unsigned long reason)
262261 {
263− struct mm_struct *mm = vma->vm_mm;
262+ struct mm_struct *mm = fe->vma->vm_mm;
264263 struct userfaultfd_ctx *ctx;
265264 struct userfaultfd_wait_queue uwq;
266265 int ret;
@@ -269,7 +268,7 @@ int handle_userfault(struct vm_area_struct *vma, unsigned long address, · 2 unmodified lines
269268 BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
270269
271270 ret = VM_FAULT_SIGBUS;
272− ctx = vma->vm_userfaultfd_ctx.ctx;
271+ ctx = fe->vma->vm_userfaultfd_ctx.ctx;
273272 if (!ctx)
274273 goto out;
275274
@@ -302,17 +301,17 @@ int handle_userfault(struct vm_area_struct *vma, unsigned long address, · 26 unmodified lines
302301 * without first stopping userland access to the memory. For
303302 * VM_UFFD_MISSING userfaults this is enough for now.
304303 */
305− if (unlikely(!(flags & FAULT_FLAG_ALLOW_RETRY))) {
304+ if (unlikely(!(fe->flags & FAULT_FLAG_ALLOW_RETRY))) {
306305 /*
307306 * Validate the invariant that nowait must allow retry
308307 * to be sure not to return SIGBUS erroneously on
309308 * nowait invocations.
310309 */
311− BUG_ON(flags & FAULT_FLAG_RETRY_NOWAIT);
310+ BUG_ON(fe->flags & FAULT_FLAG_RETRY_NOWAIT);
312311 #ifdef CONFIG_DEBUG_VM
313312 if (printk_ratelimit()) {
314313 printk(KERN_WARNING
315− "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags);
314+ "FAULT_FLAG_ALLOW_RETRY missing %x\n", fe->flags);
316315 dump_stack();
317316 }
318317 #endif
@@ -324,7 +323,7 @@ int handle_userfault(struct vm_area_struct *vma, unsigned long address, · 5 unmodified lines
324323 * and wait.
325324 */
326325 ret = VM_FAULT_RETRY;
327− if (flags & FAULT_FLAG_RETRY_NOWAIT)
326+ if (fe->flags & FAULT_FLAG_RETRY_NOWAIT)
328327 goto out;
329328
330329 /* take the reference before dropping the mmap_sem */
@@ -332,10 +331,11 @@ int handle_userfault(struct vm_area_struct *vma, unsigned long address, · 1 unmodified line
332331
333332 init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
334333 uwq.wq.private = current;
335− uwq.msg = userfault_msg(address, flags, reason);
334+ uwq.msg = userfault_msg(fe->address, fe->flags, reason);
336335 uwq.ctx = ctx;
337336
338− return_to_userland = (flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
337+ return_to_userland =
338+ (fe->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
339339 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
340340
341341 spin_lock(&ctx->fault_pending_wqh.lock);
@@ -353,7 +353,7 @@ int handle_userfault(struct vm_area_struct *vma, unsigned long address, · 11 unmodified lines
353353 TASK_KILLABLE);
354354 spin_unlock(&ctx->fault_pending_wqh.lock);
355355
356− must_wait = userfaultfd_must_wait(ctx, address, flags, reason);
356+ must_wait = userfaultfd_must_wait(ctx, fe->address, fe->flags, reason);
357357 up_read(&mm->mmap_sem);
358358
359359 if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
fs/xfs/xfs_file.c+3−3
@@ -1551,7 +1551,7 @@ xfs_filemap_page_mkwrite( · 1550 unmodified lines
15511551 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
15521552
15531553 if (IS_DAX(inode)) {
1554− ret = __dax_mkwrite(vma, vmf, xfs_get_blocks_dax_fault);
1554+ ret = dax_mkwrite(vma, vmf, xfs_get_blocks_dax_fault);
15551555 } else {
15561556 ret = block_page_mkwrite(vma, vmf, xfs_get_blocks);
15571557 ret = block_page_mkwrite_return(ret);
@@ -1585,7 +1585,7 @@ xfs_filemap_fault( · 27 unmodified lines
15851585 * changes to xfs_get_blocks_direct() to map unwritten extent
15861586 * ioend for conversion on read-only mappings.
15871587 */
1588− ret = __dax_fault(vma, vmf, xfs_get_blocks_dax_fault);
1588+ ret = dax_fault(vma, vmf, xfs_get_blocks_dax_fault);
15891589 } else
15901590 ret = filemap_fault(vma, vmf);
15911591 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
@@ -1622,7 +1622,7 @@ xfs_filemap_pmd_fault( · 30 unmodified lines
16221622 }
16231623
16241624 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1625− ret = __dax_pmd_fault(vma, addr, pmd, flags, xfs_get_blocks_dax_fault);
1625+ ret = dax_pmd_fault(vma, addr, pmd, flags, xfs_get_blocks_dax_fault);
16261626 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
16271627
16281628 if (flags & FAULT_FLAG_WRITE)
include/asm-generic/tlb.h+48−11
@@ -107,6 +107,12 @@ struct mmu_gather { · 106 unmodified lines
107107 struct mmu_gather_batch local;
108108 struct page *__pages[MMU_GATHER_BUNDLE];
109109 unsigned int batch_count;
110+ /*
111+ * __tlb_adjust_range will track the new addr here,
112+ * that that we can adjust the range after the flush
113+ */
114+ unsigned long addr;
115+ int page_size;
110116 };
111117
112118 #define HAVE_GENERIC_MMU_GATHER
@@ -115,23 +121,20 @@ void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long · 2 unmodified lines
115121 void tlb_flush_mmu(struct mmu_gather *tlb);
116122 void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start,
117123 unsigned long end);
118−int __tlb_remove_page(struct mmu_gather *tlb, struct page *page);
119−
120−/* tlb_remove_page
121− * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
122− * required.
123− */
124−static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
125−{
126− if (!__tlb_remove_page(tlb, page))
127− tlb_flush_mmu(tlb);
128−}
124+extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
125+ int page_size);
129126
130127 static inline void __tlb_adjust_range(struct mmu_gather *tlb,
131128 unsigned long address)
132129 {
133130 tlb->start = min(tlb->start, address);
134131 tlb->end = max(tlb->end, address + PAGE_SIZE);
132+ /*
133+ * Track the last address with which we adjusted the range. This
134+ * will be used later to adjust again after a mmu_flush due to
135+ * failed __tlb_remove_page
136+ */
137+ tlb->addr = address;
135138 }
136139
137140 static inline void __tlb_reset_range(struct mmu_gather *tlb)
@@ -144,6 +147,40 @@ static inline void __tlb_reset_range(struct mmu_gather *tlb) · 6 unmodified lines
144147 }
145148 }
146149
150+static inline void tlb_remove_page_size(struct mmu_gather *tlb,
151+ struct page *page, int page_size)
152+{
153+ if (__tlb_remove_page_size(tlb, page, page_size)) {
154+ tlb_flush_mmu(tlb);
155+ tlb->page_size = page_size;
156+ __tlb_adjust_range(tlb, tlb->addr);
157+ __tlb_remove_page_size(tlb, page, page_size);
158+ }
159+}
160+
161+static bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
162+{
163+ return __tlb_remove_page_size(tlb, page, PAGE_SIZE);
164+}
165+
166+/* tlb_remove_page
167+ * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
168+ * required.
169+ */
170+static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
171+{
172+ return tlb_remove_page_size(tlb, page, PAGE_SIZE);
173+}
174+
175+static inline bool __tlb_remove_pte_page(struct mmu_gather *tlb, struct page *page)
176+{
177+ /* active->nr should be zero when we call this */
178+ VM_BUG_ON_PAGE(tlb->active->nr, page);
179+ tlb->page_size = PAGE_SIZE;
180+ __tlb_adjust_range(tlb, tlb->addr);
181+ return __tlb_remove_page(tlb, page);
182+}
183+
147184 /*
148185 * In the case of tlb vma handling, we can optimise these away in the
149186 * case where we're doing a full MM flush. When we're doing a munmap,
include/linux/balloon_compaction.h+15−36
@@ -48,6 +48,7 @@ · 47 unmodified lines
4848 #include <linux/migrate.h>
4949 #include <linux/gfp.h>
5050 #include <linux/err.h>
51+#include <linux/fs.h>
5152
5253 /*
5354 * Balloon device information descriptor.
@@ -62,6 +63,7 @@ struct balloon_dev_info { · 8 unmodified lines
6263 struct list_head pages; /* Pages enqueued & handled to Host */
6364 int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
6465 struct page *page, enum migrate_mode mode);
66+ struct inode *inode;
6567 };
6668
6769 extern struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info);
@@ -73,44 +75,18 @@ static inline void balloon_devinfo_init(struct balloon_dev_info *balloon) · 5 unmodified lines
7375 spin_lock_init(&balloon->pages_lock);
7476 INIT_LIST_HEAD(&balloon->pages);
7577 balloon->migratepage = NULL;
78+ balloon->inode = NULL;
7679 }
7780
7881 #ifdef CONFIG_BALLOON_COMPACTION
79−extern bool balloon_page_isolate(struct page *page);
82+extern const struct address_space_operations balloon_aops;
83+extern bool balloon_page_isolate(struct page *page,
84+ isolate_mode_t mode);
8085 extern void balloon_page_putback(struct page *page);
81−extern int balloon_page_migrate(struct page *newpage,
86+extern int balloon_page_migrate(struct address_space *mapping,
87+ struct page *newpage,
8288 struct page *page, enum migrate_mode mode);
8389
84−/*
85− * __is_movable_balloon_page - helper to perform @page PageBalloon tests
86− */
87−static inline bool __is_movable_balloon_page(struct page *page)
88−{
89− return PageBalloon(page);
90−}
91−
92−/*
93− * balloon_page_movable - test PageBalloon to identify balloon pages
94− * and PagePrivate to check that the page is not
95− * isolated and can be moved by compaction/migration.
96− *
97− * As we might return false positives in the case of a balloon page being just
98− * released under us, this need to be re-tested later, under the page lock.
99− */
100−static inline bool balloon_page_movable(struct page *page)
101−{
102− return PageBalloon(page) && PagePrivate(page);
103−}
104−
105−/*
106− * isolated_balloon_page - identify an isolated balloon page on private
107− * compaction/migration page lists.
108− */
109−static inline bool isolated_balloon_page(struct page *page)
110−{
111− return PageBalloon(page);
112−}
113−
11490 /*
11591 * balloon_page_insert - insert a page into the balloon's page list and make
11692 * the page->private assignment accordingly.
@@ -124,7 +100,7 @@ static inline void balloon_page_insert(struct balloon_dev_info *balloon, · 7 unmodified lines
124100 struct page *page)
125101 {
126102 __SetPageBalloon(page);
127− SetPagePrivate(page);
103+ __SetPageMovable(page, balloon->inode->i_mapping);
128104 set_page_private(page, (unsigned long)balloon);
129105 list_add(&page->lru, &balloon->pages);
130106 }
@@ -140,11 +116,14 @@ static inline void balloon_page_insert(struct balloon_dev_info *balloon, · 9 unmodified lines
140116 static inline void balloon_page_delete(struct page *page)
141117 {
142118 __ClearPageBalloon(page);
119+ __ClearPageMovable(page);
143120 set_page_private(page, 0);
144− if (PagePrivate(page)) {
145− ClearPagePrivate(page);
121+ /*
122+ * No touch page.lru field once @page has been isolated
123+ * because VM is using the field.
124+ */
125+ if (!PageIsolated(page))
146126 list_del(&page->lru);
147− }
148127 }
149128
150129 /*
include/linux/compaction.h+1−0
@@ -212,6 +212,7 @@ static inline void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_i · 211 unmodified lines
212212 #endif /* CONFIG_COMPACTION */
213213
214214 #if defined(CONFIG_COMPACTION) && defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
215+struct node;
215216 extern int compaction_register_node(struct node *node);
216217 extern void compaction_unregister_node(struct node *node);
217218
include/linux/dax.h+0−5
@@ -14,7 +14,6 @@ ssize_t dax_do_io(struct kiocb *, struct inode *, struct iov_iter *, · 13 unmodified lines
1414 int dax_zero_page_range(struct inode *, loff_t from, unsigned len, get_block_t);
1515 int dax_truncate_page(struct inode *, loff_t from, get_block_t);
1616 int dax_fault(struct vm_area_struct *, struct vm_fault *, get_block_t);
17−int __dax_fault(struct vm_area_struct *, struct vm_fault *, get_block_t);
1817 int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index);
1918 void dax_wake_mapping_entry_waiter(struct address_space *mapping,
2019 pgoff_t index, bool wake_all);
@@ -46,19 +45,15 @@ static inline int __dax_zero_page_range(struct block_device *bdev, · 25 unmodified lines
4645 #if defined(CONFIG_TRANSPARENT_HUGEPAGE)
4746 int dax_pmd_fault(struct vm_area_struct *, unsigned long addr, pmd_t *,
4847 unsigned int flags, get_block_t);
49−int __dax_pmd_fault(struct vm_area_struct *, unsigned long addr, pmd_t *,
50− unsigned int flags, get_block_t);
5148 #else
5249 static inline int dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
5350 pmd_t *pmd, unsigned int flags, get_block_t gb)
5451 {
5552 return VM_FAULT_FALLBACK;
5653 }
57−#define __dax_pmd_fault dax_pmd_fault
5854 #endif
5955 int dax_pfn_mkwrite(struct vm_area_struct *, struct vm_fault *);
6056 #define dax_mkwrite(vma, vmf, gb) dax_fault(vma, vmf, gb)
61−#define __dax_mkwrite(vma, vmf, gb) __dax_fault(vma, vmf, gb)
6257
6358 static inline bool vma_is_dax(struct vm_area_struct *vma)
6459 {
include/linux/debugobjects.h+1−1
@@ -38,7 +38,7 @@ struct debug_obj { · 37 unmodified lines
3838 * @name: name of the object typee
3939 * @debug_hint: function returning address, which have associated
4040 * kernel symbol, to allow identify the object
41− * @is_static_object return true if the obj is static, otherwise return false
41+ * @is_static_object: return true if the obj is static, otherwise return false
4242 * @fixup_init: fixup function, which is called when the init check
4343 * fails. All fixup functions must return true if fixup
4444 * was successful, otherwise return false
include/linux/frontswap.h+20−14
@@ -4,6 +4,7 @@ · 3 unmodified lines
44 #include <linux/swap.h>
55 #include <linux/mm.h>
66 #include <linux/bitops.h>
7+#include <linux/jump_label.h>
78
89 struct frontswap_ops {
910 void (*init)(unsigned); /* this swap type was just swapon'ed */
@@ -14,7 +15,6 @@ struct frontswap_ops { · 4 unmodified lines
1415 struct frontswap_ops *next; /* private pointer to next ops */
1516 };
1617
17−extern bool frontswap_enabled;
1818 extern void frontswap_register_ops(struct frontswap_ops *ops);
1919 extern void frontswap_shrink(unsigned long);
2020 extern unsigned long frontswap_curr_pages(void);
@@ -30,7 +30,12 @@ extern void __frontswap_invalidate_page(unsigned, pgoff_t); · 9 unmodified lines
3030 extern void __frontswap_invalidate_area(unsigned);
3131
3232 #ifdef CONFIG_FRONTSWAP
33−#define frontswap_enabled (1)
33+extern struct static_key_false frontswap_enabled_key;
34+
35+static inline bool frontswap_enabled(void)
36+{
37+ return static_branch_unlikely(&frontswap_enabled_key);
38+}
3439
3540 static inline bool frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
3641 {
@@ -50,7 +55,10 @@ static inline unsigned long *frontswap_map_get(struct swap_info_struct *p) · 13 unmodified lines
5055 #else
5156 /* all inline routines become no-ops and all externs are ignored */
5257
53−#define frontswap_enabled (0)
58+static inline bool frontswap_enabled(void)
59+{
60+ return false;
61+}
5462
5563 static inline bool frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
5664 {
@@ -70,37 +78,35 @@ static inline unsigned long *frontswap_map_get(struct swap_info_struct *p) · 13 unmodified lines
7078
7179 static inline int frontswap_store(struct page *page)
7280 {
73− int ret = -1;
81+ if (frontswap_enabled())
82+ return __frontswap_store(page);
7483
75− if (frontswap_enabled)
76− ret = __frontswap_store(page);
77− return ret;
84+ return -1;
7885 }
7986
8087 static inline int frontswap_load(struct page *page)
8188 {
82− int ret = -1;
89+ if (frontswap_enabled())
90+ return __frontswap_load(page);
8391
84− if (frontswap_enabled)
85− ret = __frontswap_load(page);
86− return ret;
92+ return -1;
8793 }
8894
8995 static inline void frontswap_invalidate_page(unsigned type, pgoff_t offset)
9096 {
91− if (frontswap_enabled)
97+ if (frontswap_enabled())
9298 __frontswap_invalidate_page(type, offset);
9399 }
94100
95101 static inline void frontswap_invalidate_area(unsigned type)
96102 {
97− if (frontswap_enabled)
103+ if (frontswap_enabled())
98104 __frontswap_invalidate_area(type);
99105 }
100106
101107 static inline void frontswap_init(unsigned type, unsigned long *map)
102108 {
103− if (frontswap_enabled)
109+ if (frontswap_enabled())
104110 __frontswap_init(type, map);
105111 }
106112
include/linux/fs.h+6−0
@@ -398,6 +398,8 @@ struct address_space_operations { · 397 unmodified lines
398398 */
399399 int (*migratepage) (struct address_space *,
400400 struct page *, struct page *, enum migrate_mode);
401+ bool (*isolate_page)(struct page *, isolate_mode_t);
402+ void (*putback_page)(struct page *);
401403 int (*launder_page) (struct page *);
402404 int (*is_partially_uptodate) (struct page *, unsigned long,
403405 unsigned long);
@@ -661,6 +663,7 @@ struct inode { · 257 unmodified lines
661663 #endif
662664 struct list_head i_lru; /* inode LRU list */
663665 struct list_head i_sb_list;
666+ struct list_head i_wb_list; /* backing dev writeback list */
664667 union {
665668 struct hlist_head i_dentry;
666669 struct rcu_head i_rcu;
@@ -1444,6 +1447,9 @@ struct super_block { · 777 unmodified lines
14441447 /* s_inode_list_lock protects s_inodes */
14451448 spinlock_t s_inode_list_lock ____cacheline_aligned_in_smp;
14461449 struct list_head s_inodes; /* all inodes */
1450+
1451+ spinlock_t s_inode_wblist_lock;
1452+ struct list_head s_inodes_wb; /* writeback inodes */
14471453 };
14481454
14491455 extern struct timespec current_fs_time(struct super_block *sb);
include/linux/gfp.h+1−9
@@ -78,8 +78,7 @@ struct vm_area_struct; · 77 unmodified lines
7878 * __GFP_THISNODE forces the allocation to be satisified from the requested
7979 * node with no fallbacks or placement policy enforcements.
8080 *
81− * __GFP_ACCOUNT causes the allocation to be accounted to kmemcg (only relevant
82− * to kmem allocations).
81+ * __GFP_ACCOUNT causes the allocation to be accounted to kmemcg.
8382 */
8483 #define __GFP_RECLAIMABLE ((__force gfp_t)___GFP_RECLAIMABLE)
8584 #define __GFP_WRITE ((__force gfp_t)___GFP_WRITE)
@@ -486,10 +485,6 @@ extern struct page *alloc_pages_vma(gfp_t gfp_mask, int order, · 400 unmodified lines
486485 #define alloc_page_vma_node(gfp_mask, vma, addr, node) \
487486 alloc_pages_vma(gfp_mask, 0, vma, addr, node, false)
488487
489−extern struct page *alloc_kmem_pages(gfp_t gfp_mask, unsigned int order);
490−extern struct page *alloc_kmem_pages_node(int nid, gfp_t gfp_mask,
491− unsigned int order);
492−
493488 extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
494489 extern unsigned long get_zeroed_page(gfp_t gfp_mask);
495490
@@ -513,9 +508,6 @@ extern void *__alloc_page_frag(struct page_frag_cache *nc, · 17 unmodified lines
513508 unsigned int fragsz, gfp_t gfp_mask);
514509 extern void __free_page_frag(void *addr);
515510
516−extern void __free_kmem_pages(struct page *page, unsigned int order);
517−extern void free_kmem_pages(unsigned long addr, unsigned int order);
518−
519511 #define __free_page(page) __free_pages((page), 0)
520512 #define free_page(addr) free_pages((addr), 0)
521513
include/linux/huge_mm.h+21−15
@@ -1,20 +1,12 @@
11 #ifndef _LINUX_HUGE_MM_H
22 #define _LINUX_HUGE_MM_H
33
4−extern int do_huge_pmd_anonymous_page(struct mm_struct *mm,
5− struct vm_area_struct *vma,
6− unsigned long address, pmd_t *pmd,
7− unsigned int flags);
4+extern int do_huge_pmd_anonymous_page(struct fault_env *fe);
85 extern int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
96 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
107 struct vm_area_struct *vma);
11−extern void huge_pmd_set_accessed(struct mm_struct *mm,
12− struct vm_area_struct *vma,
13− unsigned long address, pmd_t *pmd,
14− pmd_t orig_pmd, int dirty);
15−extern int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
16− unsigned long address, pmd_t *pmd,
17− pmd_t orig_pmd);
8+extern void huge_pmd_set_accessed(struct fault_env *fe, pmd_t orig_pmd);
9+extern int do_huge_pmd_wp_page(struct fault_env *fe, pmd_t orig_pmd);
1810 extern struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1911 unsigned long addr,
2012 pmd_t *pmd,
@@ -49,6 +41,18 @@ enum transparent_hugepage_flag { · 28 unmodified lines
4941 #endif
5042 };
5143
44+struct kobject;
45+struct kobj_attribute;
46+
47+extern ssize_t single_hugepage_flag_store(struct kobject *kobj,
48+ struct kobj_attribute *attr,
49+ const char *buf, size_t count,
50+ enum transparent_hugepage_flag flag);
51+extern ssize_t single_hugepage_flag_show(struct kobject *kobj,
52+ struct kobj_attribute *attr, char *buf,
53+ enum transparent_hugepage_flag flag);
54+extern struct kobj_attribute shmem_enabled_attr;
55+
5256 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
5357 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
5458
@@ -134,8 +138,7 @@ static inline int hpage_nr_pages(struct page *page) · 79 unmodified lines
134138 return 1;
135139 }
136140
137−extern int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
138− unsigned long addr, pmd_t pmd, pmd_t *pmdp);
141+extern int do_huge_pmd_numa_page(struct fault_env *fe, pmd_t orig_pmd);
139142
140143 extern struct page *huge_zero_page;
141144
@@ -152,6 +155,8 @@ static inline bool is_huge_zero_pmd(pmd_t pmd) · 10 unmodified lines
152155 struct page *get_huge_zero_page(void);
153156 void put_huge_zero_page(void);
154157
158+#define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
159+
155160 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
156161 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
157162 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
@@ -161,6 +166,8 @@ void put_huge_zero_page(void); · 3 unmodified lines
161166
162167 #define transparent_hugepage_enabled(__vma) 0
163168
169+static inline void prep_transhuge_page(struct page *page) {}
170+
164171 #define transparent_hugepage_flags 0UL
165172 static inline int
166173 split_huge_page_to_list(struct page *page, struct list_head *list)
@@ -196,8 +203,7 @@ static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd, · 29 unmodified lines
196203 return NULL;
197204 }
198205
199−static inline int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
200− unsigned long addr, pmd_t pmd, pmd_t *pmdp)
206+static inline int do_huge_pmd_numa_page(struct fault_env *fe, pmd_t orig_pmd)
201207 {
202208 return 0;
203209 }
include/linux/khugepaged.h+5−0
@@ -4,6 +4,11 @@ · 3 unmodified lines
44 #include <linux/sched.h> /* MMF_VM_HUGEPAGE */
55
66 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
7+extern struct attribute_group khugepaged_attr_group;
8+
9+extern int khugepaged_init(void);
10+extern void khugepaged_destroy(void);
11+extern int start_stop_khugepaged(void);
712 extern int __khugepaged_enter(struct mm_struct *mm);
813 extern void __khugepaged_exit(struct mm_struct *mm);
914 extern int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
include/linux/ksm.h+1−2
@@ -43,8 +43,7 @@ static inline struct stable_node *page_stable_node(struct page *page) · 42 unmodified lines
4343 static inline void set_page_stable_node(struct page *page,
4444 struct stable_node *stable_node)
4545 {
46− page->mapping = (void *)stable_node +
47− (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
46+ page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM);
4847 }
4948
5049 /*
include/linux/memblock.h+9−11
@@ -73,8 +73,8 @@ extern bool movable_node_enabled; · 72 unmodified lines
7373 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
7474
7575 phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
76− phys_addr_t start, phys_addr_t end,
77− int nid, ulong flags);
76+ phys_addr_t start, phys_addr_t end,
77+ int nid, ulong flags);
7878 phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
7979 phys_addr_t size, phys_addr_t align);
8080 phys_addr_t get_allocated_memblock_reserved_regions_info(phys_addr_t *addr);
@@ -110,7 +110,7 @@ void __next_mem_range_rev(u64 *idx, int nid, ulong flags, · 29 unmodified lines
110110 phys_addr_t *out_end, int *out_nid);
111111
112112 void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
113− phys_addr_t *out_end);
113+ phys_addr_t *out_end);
114114
115115 /**
116116 * for_each_mem_range - iterate through memblock areas from type_a and not
@@ -148,7 +148,7 @@ void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start, · 31 unmodified lines
148148 p_start, p_end, p_nid) \
149149 for (i = (u64)ULLONG_MAX, \
150150 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
151− p_start, p_end, p_nid); \
151+ p_start, p_end, p_nid); \
152152 i != (u64)ULLONG_MAX; \
153153 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
154154 p_start, p_end, p_nid))
@@ -163,8 +163,7 @@ void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start, · 8 unmodified lines
163163 * is initialized.
164164 */
165165 #define for_each_reserved_mem_region(i, p_start, p_end) \
166− for (i = 0UL, \
167− __next_reserved_mem_region(&i, p_start, p_end); \
166+ for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
168167 i != (u64)ULLONG_MAX; \
169168 __next_reserved_mem_region(&i, p_start, p_end))
170169
@@ -403,15 +402,14 @@ static inline unsigned long memblock_region_reserved_end_pfn(const struct memblo · 232 unmodified lines
403402 }
404403
405404 #define for_each_memblock(memblock_type, region) \
406− for (region = memblock.memblock_type.regions; \
405+ for (region = memblock.memblock_type.regions; \
407406 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
408407 region++)
409408
410409 #define for_each_memblock_type(memblock_type, rgn) \
411− idx = 0; \
412− rgn = &memblock_type->regions[idx]; \
413− for (idx = 0; idx < memblock_type->cnt; \
414− idx++,rgn = &memblock_type->regions[idx])
410+ for (idx = 0, rgn = &memblock_type->regions[0]; \
411+ idx < memblock_type->cnt; \
412+ idx++, rgn = &memblock_type->regions[idx])
415413
416414 #ifdef CONFIG_MEMTEST
417415 extern void early_memtest(phys_addr_t start, phys_addr_t end);
include/linux/memcontrol.h+7−96
@@ -749,6 +749,13 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) · 748 unmodified lines
749749 }
750750 #endif
751751
752+struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
753+void memcg_kmem_put_cache(struct kmem_cache *cachep);
754+int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
755+ struct mem_cgroup *memcg);
756+int memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
757+void memcg_kmem_uncharge(struct page *page, int order);
758+
752759 #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
753760 extern struct static_key_false memcg_kmem_enabled_key;
754761
@@ -769,22 +776,6 @@ static inline bool memcg_kmem_enabled(void) · 14 unmodified lines
769776 return static_branch_unlikely(&memcg_kmem_enabled_key);
770777 }
771778
772−/*
773− * In general, we'll do everything in our power to not incur in any overhead
774− * for non-memcg users for the kmem functions. Not even a function call, if we
775− * can avoid it.
776− *
777− * Therefore, we'll inline all those functions so that in the best case, we'll
778− * see that kmemcg is off for everybody and proceed quickly. If it is on,
779− * we'll still do most of the flag checking inline. We check a lot of
780− * conditions, but because they are pretty simple, they are expected to be
781− * fast.
782− */
783−int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
784− struct mem_cgroup *memcg);
785−int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
786−void __memcg_kmem_uncharge(struct page *page, int order);
787−
788779 /*
789780 * helper for accessing a memcg's index. It will be used as an index in the
790781 * child cache array in kmem_cache, and also to derive its name. This function
@@ -795,67 +786,6 @@ static inline int memcg_cache_id(struct mem_cgroup *memcg) · 4 unmodified lines
795786 return memcg ? memcg->kmemcg_id : -1;
796787 }
797788
798−struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
799−void __memcg_kmem_put_cache(struct kmem_cache *cachep);
800−
801−static inline bool __memcg_kmem_bypass(void)
802−{
803− if (!memcg_kmem_enabled())
804− return true;
805− if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
806− return true;
807− return false;
808−}
809−
810−/**
811− * memcg_kmem_charge: charge a kmem page
812− * @page: page to charge
813− * @gfp: reclaim mode
814− * @order: allocation order
815− *
816− * Returns 0 on success, an error code on failure.
817− */
818−static __always_inline int memcg_kmem_charge(struct page *page,
819− gfp_t gfp, int order)
820−{
821− if (__memcg_kmem_bypass())
822− return 0;
823− if (!(gfp & __GFP_ACCOUNT))
824− return 0;
825− return __memcg_kmem_charge(page, gfp, order);
826−}
827−
828−/**
829− * memcg_kmem_uncharge: uncharge a kmem page
830− * @page: page to uncharge
831− * @order: allocation order
832− */
833−static __always_inline void memcg_kmem_uncharge(struct page *page, int order)
834−{
835− if (memcg_kmem_enabled())
836− __memcg_kmem_uncharge(page, order);
837−}
838−
839−/**
840− * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
841− * @cachep: the original global kmem cache
842− *
843− * All memory allocated from a per-memcg cache is charged to the owner memcg.
844− */
845−static __always_inline struct kmem_cache *
846−memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
847−{
848− if (__memcg_kmem_bypass())
849− return cachep;
850− return __memcg_kmem_get_cache(cachep, gfp);
851−}
852−
853−static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
854−{
855− if (memcg_kmem_enabled())
856− __memcg_kmem_put_cache(cachep);
857−}
858−
859789 /**
860790 * memcg_kmem_update_page_stat - update kmem page state statistics
861791 * @page: the page
@@ -878,15 +808,6 @@ static inline bool memcg_kmem_enabled(void) · 16 unmodified lines
878808 return false;
879809 }
880810
881−static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
882−{
883− return 0;
884−}
885−
886−static inline void memcg_kmem_uncharge(struct page *page, int order)
887−{
888−}
889−
890811 static inline int memcg_cache_id(struct mem_cgroup *memcg)
891812 {
892813 return -1;
@@ -900,16 +821,6 @@ static inline void memcg_put_cache_ids(void) · 7 unmodified lines
900821 {
901822 }
902823
903−static inline struct kmem_cache *
904−memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
905−{
906− return cachep;
907−}
908−
909−static inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
910−{
911−}
912−
913824 static inline void memcg_kmem_update_page_stat(struct page *page,
914825 enum mem_cgroup_stat_index idx, int val)
915826 {
include/linux/memory_hotplug.h+2−0
@@ -284,5 +284,7 @@ extern void sparse_remove_one_section(struct zone *zone, struct mem_section *ms, · 283 unmodified lines
284284 unsigned long map_offset);
285285 extern struct page *sparse_decode_mem_map(unsigned long coded_mem_map,
286286 unsigned long pnum);
287+extern int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
288+ enum zone_type target);
287289
288290 #endif /* __LINUX_MEMORY_HOTPLUG_H */
include/linux/migrate.h+17−0
@@ -37,6 +37,8 @@ extern int migrate_page(struct address_space *, · 36 unmodified lines
3737 struct page *, struct page *, enum migrate_mode);
3838 extern int migrate_pages(struct list_head *l, new_page_t new, free_page_t free,
3939 unsigned long private, enum migrate_mode mode, int reason);
40+extern bool isolate_movable_page(struct page *page, isolate_mode_t mode);
41+extern void putback_movable_page(struct page *page);
4042
4143 extern int migrate_prep(void);
4244 extern int migrate_prep_local(void);
@@ -69,6 +71,21 @@ static inline int migrate_huge_page_move_mapping(struct address_space *mapping, · 26 unmodified lines
6971
7072 #endif /* CONFIG_MIGRATION */
7173
74+#ifdef CONFIG_COMPACTION
75+extern int PageMovable(struct page *page);
76+extern void __SetPageMovable(struct page *page, struct address_space *mapping);
77+extern void __ClearPageMovable(struct page *page);
78+#else
79+static inline int PageMovable(struct page *page) { return 0; };
80+static inline void __SetPageMovable(struct page *page,
81+ struct address_space *mapping)
82+{
83+}
84+static inline void __ClearPageMovable(struct page *page)
85+{
86+}
87+#endif
88+
7289 #ifdef CONFIG_NUMA_BALANCING
7390 extern bool pmd_trans_migrating(pmd_t pmd);
7491 extern int migrate_misplaced_page(struct page *page,
include/linux/mm.h+39−14
@@ -309,10 +309,34 @@ struct vm_fault { · 308 unmodified lines
309309 * VM_FAULT_DAX_LOCKED and fill in
310310 * entry here.
311311 */
312− /* for ->map_pages() only */
313− pgoff_t max_pgoff; /* map pages for offset from pgoff till
314− * max_pgoff inclusive */
315− pte_t *pte; /* pte entry associated with ->pgoff */
312+};
313+
314+/*
315+ * Page fault context: passes though page fault handler instead of endless list
316+ * of function arguments.
317+ */
318+struct fault_env {
319+ struct vm_area_struct *vma; /* Target VMA */
320+ unsigned long address; /* Faulting virtual address */
321+ unsigned int flags; /* FAULT_FLAG_xxx flags */
322+ pmd_t *pmd; /* Pointer to pmd entry matching
323+ * the 'address'
324+ */
325+ pte_t *pte; /* Pointer to pte entry matching
326+ * the 'address'. NULL if the page
327+ * table hasn't been allocated.
328+ */
329+ spinlock_t *ptl; /* Page table lock.
330+ * Protects pte page table if 'pte'
331+ * is not NULL, otherwise pmd.
332+ */
333+ pgtable_t prealloc_pte; /* Pre-allocated pte page table.
334+ * vm_ops->map_pages() calls
335+ * alloc_set_pte() from atomic context.
336+ * do_fault_around() pre-allocates
337+ * page table to avoid allocation from
338+ * atomic context.
339+ */
316340 };
317341
318342 /*
@@ -327,7 +351,8 @@ struct vm_operations_struct { · 8 unmodified lines
327351 int (*fault)(struct vm_area_struct *vma, struct vm_fault *vmf);
328352 int (*pmd_fault)(struct vm_area_struct *, unsigned long address,
329353 pmd_t *, unsigned int flags);
330− void (*map_pages)(struct vm_area_struct *vma, struct vm_fault *vmf);
354+ void (*map_pages)(struct fault_env *fe,
355+ pgoff_t start_pgoff, pgoff_t end_pgoff);
331356
332357 /* notification that a previously read-only page is about to become
333358 * writable, if an error is returned it will cause a SIGBUS */
@@ -537,7 +562,6 @@ void __put_page(struct page *page); · 203 unmodified lines
537562 void put_pages_list(struct list_head *pages);
538563
539564 void split_page(struct page *page, unsigned int order);
540−int split_free_page(struct page *page);
541565
542566 /*
543567 * Compound pages have a destructor function. Provide a
@@ -601,8 +625,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) · 57 unmodified lines
601625 return pte;
602626 }
603627
604−void do_set_pte(struct vm_area_struct *vma, unsigned long address,
605− struct page *page, pte_t *pte, bool write, bool anon);
628+int alloc_set_pte(struct fault_env *fe, struct mem_cgroup *memcg,
629+ struct page *page);
606630 #endif
607631
608632 /*
@@ -1035,6 +1059,7 @@ static inline pgoff_t page_file_index(struct page *page) · 426 unmodified lines
10351059 }
10361060
10371061 bool page_mapped(struct page *page);
1062+struct address_space *page_mapping(struct page *page);
10381063
10391064 /*
10401065 * Return true only if the page has been allocated with
@@ -1215,15 +1240,14 @@ int generic_error_remove_page(struct address_space *mapping, struct page *page); · 174 unmodified lines
12151240 int invalidate_inode_page(struct page *page);
12161241
12171242 #ifdef CONFIG_MMU
1218−extern int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1219− unsigned long address, unsigned int flags);
1243+extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
1244+ unsigned int flags);
12201245 extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
12211246 unsigned long address, unsigned int fault_flags,
12221247 bool *unlocked);
12231248 #else
1224−static inline int handle_mm_fault(struct mm_struct *mm,
1225− struct vm_area_struct *vma, unsigned long address,
1226− unsigned int flags)
1249+static inline int handle_mm_fault(struct vm_area_struct *vma,
1250+ unsigned long address, unsigned int flags)
12271251 {
12281252 /* should never happen if there's no MMU */
12291253 BUG();
@@ -2063,7 +2087,8 @@ extern void truncate_inode_pages_final(struct address_space *); · 833 unmodified lines
20632087
20642088 /* generic vm_area_ops exported for stackable file systems */
20652089 extern int filemap_fault(struct vm_area_struct *, struct vm_fault *);
2066−extern void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf);
2090+extern void filemap_map_pages(struct fault_env *fe,
2091+ pgoff_t start_pgoff, pgoff_t end_pgoff);
20672092 extern int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf);
20682093
20692094 /* mm/page-writeback.c */
include/linux/mm_types.h+37−36
@@ -60,51 +60,52 @@ struct page { · 59 unmodified lines
6060 };
6161
6262 /* Second double word */
63− struct {
64− union {
65− pgoff_t index; /* Our offset within mapping. */
66− void *freelist; /* sl[aou]b first free object */
67− /* page_deferred_list().prev -- second tail page */
68− };
63+ union {
64+ pgoff_t index; /* Our offset within mapping. */
65+ void *freelist; /* sl[aou]b first free object */
66+ /* page_deferred_list().prev -- second tail page */
67+ };
6968
70− union {
69+ union {
7170 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
7271 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
73− /* Used for cmpxchg_double in slub */
74− unsigned long counters;
72+ /* Used for cmpxchg_double in slub */
73+ unsigned long counters;
7574 #else
76− /*
77− * Keep _refcount separate from slub cmpxchg_double
78− * data. As the rest of the double word is protected by
79− * slab_lock but _refcount is not.
80− */
81− unsigned counters;
75+ /*
76+ * Keep _refcount separate from slub cmpxchg_double data.
77+ * As the rest of the double word is protected by slab_lock
78+ * but _refcount is not.
79+ */
80+ unsigned counters;
8281 #endif
82+ struct {
8383
84− struct {
85−
86− union {
87− /*
88− * Count of ptes mapped in mms, to show
89− * when page is mapped & limit reverse
90− * map searches.
91− */
92− atomic_t _mapcount;
93−
94− struct { /* SLUB */
95− unsigned inuse:16;
96− unsigned objects:15;
97− unsigned frozen:1;
98− };
99− int units; /* SLOB */
100− };
84+ union {
10185 /*
102− * Usage count, *USE WRAPPER FUNCTION*
103− * when manual accounting. See page_ref.h
86+ * Count of ptes mapped in mms, to show when
87+ * page is mapped & limit reverse map searches.
88+ *
89+ * Extra information about page type may be
90+ * stored here for pages that are never mapped,
91+ * in which case the value MUST BE <= -2.
92+ * See page-flags.h for more details.
10493 */
105− atomic_t _refcount;
94+ atomic_t _mapcount;
95+
96+ unsigned int active; /* SLAB */
97+ struct { /* SLUB */
98+ unsigned inuse:16;
99+ unsigned objects:15;
100+ unsigned frozen:1;
101+ };
102+ int units; /* SLOB */
106103 };
107− unsigned int active; /* SLAB */
104+ /*
105+ * Usage count, *USE WRAPPER FUNCTION* when manual
106+ * accounting. See page_ref.h
107+ */
108+ atomic_t _refcount;
108109 };
109110 };
110111
include/linux/mmdebug.h+2−0
@@ -39,6 +39,7 @@ void dump_mm(const struct mm_struct *mm); · 38 unmodified lines
3939 #define VM_WARN_ON(cond) WARN_ON(cond)
4040 #define VM_WARN_ON_ONCE(cond) WARN_ON_ONCE(cond)
4141 #define VM_WARN_ONCE(cond, format...) WARN_ONCE(cond, format)
42+#define VM_WARN(cond, format...) WARN(cond, format)
4243 #else
4344 #define VM_BUG_ON(cond) BUILD_BUG_ON_INVALID(cond)
4445 #define VM_BUG_ON_PAGE(cond, page) VM_BUG_ON(cond)
@@ -47,6 +48,7 @@ void dump_mm(const struct mm_struct *mm); · 2 unmodified lines
4748 #define VM_WARN_ON(cond) BUILD_BUG_ON_INVALID(cond)
4849 #define VM_WARN_ON_ONCE(cond) BUILD_BUG_ON_INVALID(cond)
4950 #define VM_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)
51+#define VM_WARN(cond, format...) BUILD_BUG_ON_INVALID(cond)
5052 #endif
5153
5254 #ifdef CONFIG_DEBUG_VIRTUAL
include/linux/mmzone.h+6−2
@@ -140,6 +140,9 @@ enum zone_stat_item { · 139 unmodified lines
140140 NR_DIRTIED, /* page dirtyings since bootup */
141141 NR_WRITTEN, /* page writings since bootup */
142142 NR_PAGES_SCANNED, /* pages scanned since last reclaim */
143+#if IS_ENABLED(CONFIG_ZSMALLOC)
144+ NR_ZSPAGES, /* allocated in zsmalloc */
145+#endif
143146 #ifdef CONFIG_NUMA
144147 NUMA_HIT, /* allocated in intended node */
145148 NUMA_MISS, /* allocated in non intended node */
@@ -151,7 +154,9 @@ enum zone_stat_item { · 5 unmodified lines
151154 WORKINGSET_REFAULT,
152155 WORKINGSET_ACTIVATE,
153156 WORKINGSET_NODERECLAIM,
154− NR_ANON_TRANSPARENT_HUGEPAGES,
157+ NR_ANON_THPS,
158+ NR_SHMEM_THPS,
159+ NR_SHMEM_PMDMAPPED,
155160 NR_FREE_CMA_PAGES,
156161 NR_VM_ZONE_STAT_ITEMS };
157162
@@ -524,7 +529,6 @@ struct zone { · 366 unmodified lines
524529
525530 enum zone_flags {
526531 ZONE_RECLAIM_LOCKED, /* prevents concurrent reclaim */
527− ZONE_OOM_LOCKED, /* zone is in OOM killer zonelist */
528532 ZONE_CONGESTED, /* zone has many dirty pages backed by
529533 * a congested BDI
530534 */
include/linux/oom.h+6−4
@@ -23,6 +23,9 @@ struct oom_control { · 22 unmodified lines
2323 /* Used to determine mempolicy */
2424 nodemask_t *nodemask;
2525
26+ /* Memory cgroup in which oom is invoked, or NULL for global oom */
27+ struct mem_cgroup *memcg;
28+
2629 /* Used to determine cpuset and node locality requirement */
2730 const gfp_t gfp_mask;
2831
@@ -83,14 +86,13 @@ extern unsigned long oom_badness(struct task_struct *p, · 54 unmodified lines
8386
8487 extern void oom_kill_process(struct oom_control *oc, struct task_struct *p,
8588 unsigned int points, unsigned long totalpages,
86− struct mem_cgroup *memcg, const char *message);
89+ const char *message);
8790
8891 extern void check_panic_on_oom(struct oom_control *oc,
89− enum oom_constraint constraint,
90− struct mem_cgroup *memcg);
92+ enum oom_constraint constraint);
9193
9294 extern enum oom_scan_t oom_scan_process_thread(struct oom_control *oc,
93− struct task_struct *task, unsigned long totalpages);
95+ struct task_struct *task);
9496
9597 extern bool out_of_memory(struct oom_control *oc);
9698
include/linux/page-flags.h+75−53
@@ -129,6 +129,9 @@ enum pageflags { · 128 unmodified lines
129129
130130 /* Compound pages. Stored in first tail page's flags */
131131 PG_double_map = PG_private_2,
132+
133+ /* non-lru isolated movable page */
134+ PG_isolated = PG_reclaim,
132135 };
133136
134137 #ifndef __GENERATING_BOUNDS_H
@@ -292,11 +295,11 @@ PAGEFLAG(OwnerPriv1, owner_priv_1, PF_ANY) · 157 unmodified lines
292295 */
293296 TESTPAGEFLAG(Writeback, writeback, PF_NO_COMPOUND)
294297 TESTSCFLAG(Writeback, writeback, PF_NO_COMPOUND)
295−PAGEFLAG(MappedToDisk, mappedtodisk, PF_NO_COMPOUND)
298+PAGEFLAG(MappedToDisk, mappedtodisk, PF_NO_TAIL)
296299
297300 /* PG_readahead is only used for reads; PG_reclaim is only for writes */
298−PAGEFLAG(Reclaim, reclaim, PF_NO_COMPOUND)
299− TESTCLEARFLAG(Reclaim, reclaim, PF_NO_COMPOUND)
301+PAGEFLAG(Reclaim, reclaim, PF_NO_TAIL)
302+ TESTCLEARFLAG(Reclaim, reclaim, PF_NO_TAIL)
300303 PAGEFLAG(Readahead, reclaim, PF_NO_COMPOUND)
301304 TESTCLEARFLAG(Readahead, reclaim, PF_NO_COMPOUND)
302305
@@ -357,29 +360,37 @@ PAGEFLAG(Idle, idle, PF_ANY) · 54 unmodified lines
357360 * with the PAGE_MAPPING_ANON bit set to distinguish it. See rmap.h.
358361 *
359362 * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
360− * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
361− * and then page->mapping points, not to an anon_vma, but to a private
363+ * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
364+ * bit; and then page->mapping points, not to an anon_vma, but to a private
362365 * structure which KSM associates with that merged page. See ksm.h.
363366 *
364− * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
367+ * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
368+ * page and then page->mapping points a struct address_space.
365369 *
366370 * Please note that, confusingly, "page_mapping" refers to the inode
367371 * address_space which maps the page from disk; whereas "page_mapped"
368372 * refers to user virtual address space into which the page is mapped.
369373 */
370−#define PAGE_MAPPING_ANON 1
371−#define PAGE_MAPPING_KSM 2
372−#define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
374+#define PAGE_MAPPING_ANON 0x1
375+#define PAGE_MAPPING_MOVABLE 0x2
376+#define PAGE_MAPPING_KSM (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
377+#define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
373378
374−static __always_inline int PageAnonHead(struct page *page)
379+static __always_inline int PageMappingFlags(struct page *page)
375380 {
376− return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
381+ return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) != 0;
377382 }
378383
379384 static __always_inline int PageAnon(struct page *page)
380385 {
381386 page = compound_head(page);
382− return PageAnonHead(page);
387+ return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
388+}
389+
390+static __always_inline int __PageMovable(struct page *page)
391+{
392+ return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
393+ PAGE_MAPPING_MOVABLE;
383394 }
384395
385396 #ifdef CONFIG_KSM
@@ -393,7 +404,7 @@ static __always_inline int PageKsm(struct page *page) · 7 unmodified lines
393404 {
394405 page = compound_head(page);
395406 return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
396− (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
407+ PAGE_MAPPING_KSM;
397408 }
398409 #else
399410 TESTPAGEFLAG_FALSE(Ksm)
@@ -570,6 +581,17 @@ static inline int PageDoubleMap(struct page *page) · 170 unmodified lines
570581 return PageHead(page) && test_bit(PG_double_map, &page[1].flags);
571582 }
572583
584+static inline void SetPageDoubleMap(struct page *page)
585+{
586+ VM_BUG_ON_PAGE(!PageHead(page), page);
587+ set_bit(PG_double_map, &page[1].flags);
588+}
589+
590+static inline void ClearPageDoubleMap(struct page *page)
591+{
592+ VM_BUG_ON_PAGE(!PageHead(page), page);
593+ clear_bit(PG_double_map, &page[1].flags);
594+}
573595 static inline int TestSetPageDoubleMap(struct page *page)
574596 {
575597 VM_BUG_ON_PAGE(!PageHead(page), page);
@@ -587,59 +609,59 @@ TESTPAGEFLAG_FALSE(TransHuge) · 11 unmodified lines
587609 TESTPAGEFLAG_FALSE(TransCompound)
588610 TESTPAGEFLAG_FALSE(TransCompoundMap)
589611 TESTPAGEFLAG_FALSE(TransTail)
590−TESTPAGEFLAG_FALSE(DoubleMap)
612+PAGEFLAG_FALSE(DoubleMap)
591613 TESTSETFLAG_FALSE(DoubleMap)
592614 TESTCLEARFLAG_FALSE(DoubleMap)
593615 #endif
594616
617+/*
618+ * For pages that are never mapped to userspace, page->mapcount may be
619+ * used for storing extra information about page type. Any value used
620+ * for this purpose must be <= -2, but it's better start not too close
621+ * to -2 so that an underflow of the page_mapcount() won't be mistaken
622+ * for a special page.
623+ */
624+#define PAGE_MAPCOUNT_OPS(uname, lname) \
625+static __always_inline int Page##uname(struct page *page) \
626+{ \
627+ return atomic_read(&page->_mapcount) == \
628+ PAGE_##lname##_MAPCOUNT_VALUE; \
629+} \
630+static __always_inline void __SetPage##uname(struct page *page) \
631+{ \
632+ VM_BUG_ON_PAGE(atomic_read(&page->_mapcount) != -1, page); \
633+ atomic_set(&page->_mapcount, PAGE_##lname##_MAPCOUNT_VALUE); \
634+} \
635+static __always_inline void __ClearPage##uname(struct page *page) \
636+{ \
637+ VM_BUG_ON_PAGE(!Page##uname(page), page); \
638+ atomic_set(&page->_mapcount, -1); \
639+}
640+
595641 /*
596642 * PageBuddy() indicate that the page is free and in the buddy system
597643 * (see mm/page_alloc.c).
598− *
599− * PAGE_BUDDY_MAPCOUNT_VALUE must be <= -2 but better not too close to
600− * -2 so that an underflow of the page_mapcount() won't be mistaken
601− * for a genuine PAGE_BUDDY_MAPCOUNT_VALUE. -128 can be created very
602− * efficiently by most CPU architectures.
603644 */
604−#define PAGE_BUDDY_MAPCOUNT_VALUE (-128)
605−
606−static inline int PageBuddy(struct page *page)
607−{
608− return atomic_read(&page->_mapcount) == PAGE_BUDDY_MAPCOUNT_VALUE;
609−}
645+#define PAGE_BUDDY_MAPCOUNT_VALUE (-128)
646+PAGE_MAPCOUNT_OPS(Buddy, BUDDY)
610647
611−static inline void __SetPageBuddy(struct page *page)
612−{
613− VM_BUG_ON_PAGE(atomic_read(&page->_mapcount) != -1, page);
614− atomic_set(&page->_mapcount, PAGE_BUDDY_MAPCOUNT_VALUE);
615−}
648+/*
649+ * PageBalloon() is set on pages that are on the balloon page list
650+ * (see mm/balloon_compaction.c).
651+ */
652+#define PAGE_BALLOON_MAPCOUNT_VALUE (-256)
653+PAGE_MAPCOUNT_OPS(Balloon, BALLOON)
616654
617−static inline void __ClearPageBuddy(struct page *page)
618−{
619− VM_BUG_ON_PAGE(!PageBuddy(page), page);
620− atomic_set(&page->_mapcount, -1);
621−}
655+/*
656+ * If kmemcg is enabled, the buddy allocator will set PageKmemcg() on
657+ * pages allocated with __GFP_ACCOUNT. It gets cleared on page free.
658+ */
659+#define PAGE_KMEMCG_MAPCOUNT_VALUE (-512)
660+PAGE_MAPCOUNT_OPS(Kmemcg, KMEMCG)
622661
623662 extern bool is_free_buddy_page(struct page *page);
624663
625−#define PAGE_BALLOON_MAPCOUNT_VALUE (-256)
626−
627−static inline int PageBalloon(struct page *page)
628−{
629− return atomic_read(&page->_mapcount) == PAGE_BALLOON_MAPCOUNT_VALUE;
630−}
631−
632−static inline void __SetPageBalloon(struct page *page)
633−{
634− VM_BUG_ON_PAGE(atomic_read(&page->_mapcount) != -1, page);
635− atomic_set(&page->_mapcount, PAGE_BALLOON_MAPCOUNT_VALUE);
636−}
637−
638−static inline void __ClearPageBalloon(struct page *page)
639−{
640− VM_BUG_ON_PAGE(!PageBalloon(page), page);
641− atomic_set(&page->_mapcount, -1);
642−}
664+__PAGEFLAG(Isolated, isolated, PF_ANY);
643665
644666 /*
645667 * If network-based swap is enabled, sl*b must keep track of whether pages
include/linux/page_ext.h+2−2
@@ -3,6 +3,7 @@ · 2 unmodified lines
33
44 #include <linux/types.h>
55 #include <linux/stacktrace.h>
6+#include <linux/stackdepot.h>
67
78 struct pglist_data;
89 struct page_ext_operations {
@@ -44,9 +45,8 @@ struct page_ext { · 35 unmodified lines
4445 #ifdef CONFIG_PAGE_OWNER
4546 unsigned int order;
4647 gfp_t gfp_mask;
47− unsigned int nr_entries;
4848 int last_migrate_reason;
49− unsigned long trace_entries[8];
49+ depot_stack_handle_t handle;
5050 #endif
5151 };
5252
include/linux/page_owner.h+5−7
@@ -10,7 +10,7 @@ extern struct page_ext_operations page_owner_ops; · 9 unmodified lines
1010 extern void __reset_page_owner(struct page *page, unsigned int order);
1111 extern void __set_page_owner(struct page *page,
1212 unsigned int order, gfp_t gfp_mask);
13−extern gfp_t __get_page_owner_gfp(struct page *page);
13+extern void __split_page_owner(struct page *page, unsigned int order);
1414 extern void __copy_page_owner(struct page *oldpage, struct page *newpage);
1515 extern void __set_page_owner_migrate_reason(struct page *page, int reason);
1616 extern void __dump_page_owner(struct page *page);
@@ -28,12 +28,10 @@ static inline void set_page_owner(struct page *page, · 11 unmodified lines
2828 __set_page_owner(page, order, gfp_mask);
2929 }
3030
31−static inline gfp_t get_page_owner_gfp(struct page *page)
31+static inline void split_page_owner(struct page *page, unsigned int order)
3232 {
3333 if (static_branch_unlikely(&page_owner_inited))
34− return __get_page_owner_gfp(page);
35− else
36− return 0;
34+ __split_page_owner(page, order);
3735 }
3836 static inline void copy_page_owner(struct page *oldpage, struct page *newpage)
3937 {
@@ -58,9 +56,9 @@ static inline void set_page_owner(struct page *page, · 18 unmodified lines
5856 unsigned int order, gfp_t gfp_mask)
5957 {
6058 }
61−static inline gfp_t get_page_owner_gfp(struct page *page)
59+static inline void split_page_owner(struct page *page,
60+ unsigned int order)
6261 {
63− return 0;
6462 }
6563 static inline void copy_page_owner(struct page *oldpage, struct page *newpage)
6664 {
include/linux/pagemap.h+3−3
@@ -209,10 +209,10 @@ static inline struct page *page_cache_alloc_cold(struct address_space *x) · 208 unmodified lines
209209 return __page_cache_alloc(mapping_gfp_mask(x)|__GFP_COLD);
210210 }
211211
212−static inline struct page *page_cache_alloc_readahead(struct address_space *x)
212+static inline gfp_t readahead_gfp_mask(struct address_space *x)
213213 {
214− return __page_cache_alloc(mapping_gfp_mask(x) |
215− __GFP_COLD | __GFP_NORETRY | __GFP_NOWARN);
214+ return mapping_gfp_mask(x) |
215+ __GFP_COLD | __GFP_NORETRY | __GFP_NOWARN;
216216 }
217217
218218 typedef int filler_t(void *, struct page *);
include/linux/radix-tree.h+1−0
@@ -291,6 +291,7 @@ unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root, · 290 unmodified lines
291291 unsigned long first_index, unsigned int max_items);
292292 int radix_tree_preload(gfp_t gfp_mask);
293293 int radix_tree_maybe_preload(gfp_t gfp_mask);
294+int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order);
294295 void radix_tree_init(void);
295296 void *radix_tree_tag_set(struct radix_tree_root *root,
296297 unsigned long index, unsigned int tag);
include/linux/rmap.h+1−1
@@ -165,7 +165,7 @@ void do_page_add_anon_rmap(struct page *, struct vm_area_struct *, · 164 unmodified lines
165165 unsigned long, int);
166166 void page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
167167 unsigned long, bool);
168−void page_add_file_rmap(struct page *);
168+void page_add_file_rmap(struct page *, bool);
169169 void page_remove_rmap(struct page *, bool);
170170
171171 void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *,
include/linux/shmem_fs.h+43−2
@@ -16,8 +16,9 @@ struct shmem_inode_info { · 15 unmodified lines
1616 unsigned long flags;
1717 unsigned long alloced; /* data pages alloced to file */
1818 unsigned long swapped; /* subtotal assigned to swap */
19− struct shared_policy policy; /* NUMA memory alloc policy */
19+ struct list_head shrinklist; /* shrinkable hpage inodes */
2020 struct list_head swaplist; /* chain of maybes on swap */
21+ struct shared_policy policy; /* NUMA memory alloc policy */
2122 struct simple_xattrs xattrs; /* list of xattrs */
2223 struct inode vfs_inode;
2324 };
@@ -28,10 +29,14 @@ struct shmem_sb_info { · 4 unmodified lines
2829 unsigned long max_inodes; /* How many inodes are allowed */
2930 unsigned long free_inodes; /* How many are left for allocation */
3031 spinlock_t stat_lock; /* Serialize shmem_sb_info changes */
32+ umode_t mode; /* Mount mode for root directory */
33+ unsigned char huge; /* Whether to try for hugepages */
3134 kuid_t uid; /* Mount uid for root directory */
3235 kgid_t gid; /* Mount gid for root directory */
33− umode_t mode; /* Mount mode for root directory */
3436 struct mempolicy *mpol; /* default memory policy for mappings */
37+ spinlock_t shrinklist_lock; /* Protects shrinklist */
38+ struct list_head shrinklist; /* List of shinkable inodes */
39+ unsigned long shrinklist_len; /* Length of shrinklist */
3540 };
3641
3742 static inline struct shmem_inode_info *SHMEM_I(struct inode *inode)
@@ -49,6 +54,8 @@ extern struct file *shmem_file_setup(const char *name, · 11 unmodified lines
4954 extern struct file *shmem_kernel_file_setup(const char *name, loff_t size,
5055 unsigned long flags);
5156 extern int shmem_zero_setup(struct vm_area_struct *);
57+extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
58+ unsigned long len, unsigned long pgoff, unsigned long flags);
5259 extern int shmem_lock(struct file *file, int lock, struct user_struct *user);
5360 extern bool shmem_mapping(struct address_space *mapping);
5461 extern void shmem_unlock_mapping(struct address_space *mapping);
@@ -61,6 +68,19 @@ extern unsigned long shmem_swap_usage(struct vm_area_struct *vma); · 6 unmodified lines
6168 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
6269 pgoff_t start, pgoff_t end);
6370
71+/* Flag allocation requirements to shmem_getpage */
72+enum sgp_type {
73+ SGP_READ, /* don't exceed i_size, don't allocate page */
74+ SGP_CACHE, /* don't exceed i_size, may allocate page */
75+ SGP_NOHUGE, /* like SGP_CACHE, but no huge pages */
76+ SGP_HUGE, /* like SGP_CACHE, huge pages preferred */
77+ SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
78+ SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
79+};
80+
81+extern int shmem_getpage(struct inode *inode, pgoff_t index,
82+ struct page **pagep, enum sgp_type sgp);
83+
6484 static inline struct page *shmem_read_mapping_page(
6585 struct address_space *mapping, pgoff_t index)
6686 {
@@ -68,6 +88,18 @@ static inline struct page *shmem_read_mapping_page( · 1 unmodified line
6888 mapping_gfp_mask(mapping));
6989 }
7090
91+static inline bool shmem_file(struct file *file)
92+{
93+ if (!IS_ENABLED(CONFIG_SHMEM))
94+ return false;
95+ if (!file || !file->f_mapping)
96+ return false;
97+ return shmem_mapping(file->f_mapping);
98+}
99+
100+extern bool shmem_charge(struct inode *inode, long pages);
101+extern void shmem_uncharge(struct inode *inode, long pages);
102+
71103 #ifdef CONFIG_TMPFS
72104
73105 extern int shmem_add_seals(struct file *file, unsigned int seals);
@@ -83,4 +115,13 @@ static inline long shmem_fcntl(struct file *f, unsigned int c, unsigned long a) · 9 unmodified lines
83115
84116 #endif
85117
118+#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
119+extern bool shmem_huge_enabled(struct vm_area_struct *vma);
120+#else
121+static inline bool shmem_huge_enabled(struct vm_area_struct *vma)
122+{
123+ return false;
124+}
125+#endif
126+
86127 #endif
include/linux/slab.h+2−0
@@ -565,6 +565,8 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) · 564 unmodified lines
565565 {
566566 if (size != 0 && n > SIZE_MAX / size)
567567 return NULL;
568+ if (__builtin_constant_p(n) && __builtin_constant_p(size))
569+ return kmalloc(n * size, flags);
568570 return __kmalloc(n * size, flags);
569571 }
570572
include/linux/slab_def.h+1−1
@@ -81,7 +81,7 @@ struct kmem_cache { · 80 unmodified lines
8181 #endif
8282
8383 #ifdef CONFIG_SLAB_FREELIST_RANDOM
84− void *random_seq;
84+ unsigned int *random_seq;
8585 #endif
8686
8787 struct kmem_cache_node *node[MAX_NUMNODES];
include/linux/slub_def.h+5−0
@@ -99,6 +99,11 @@ struct kmem_cache { · 98 unmodified lines
9999 */
100100 int remote_node_defrag_ratio;
101101 #endif
102+
103+#ifdef CONFIG_SLAB_FREELIST_RANDOM
104+ unsigned int *random_seq;
105+#endif
106+
102107 struct kmem_cache_node *node[MAX_NUMNODES];
103108 };
104109
include/linux/userfaultfd_k.h+2−6
@@ -27,8 +27,7 @@ · 26 unmodified lines
2727 #define UFFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
2828 #define UFFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS)
2929
30−extern int handle_userfault(struct vm_area_struct *vma, unsigned long address,
31− unsigned int flags, unsigned long reason);
30+extern int handle_userfault(struct fault_env *fe, unsigned long reason);
3231
3332 extern ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
3433 unsigned long src_start, unsigned long len);
@@ -56,10 +55,7 @@ static inline bool userfaultfd_armed(struct vm_area_struct *vma) · 21 unmodified lines
5655 #else /* CONFIG_USERFAULTFD */
5756
5857 /* mm helpers */
59−static inline int handle_userfault(struct vm_area_struct *vma,
60− unsigned long address,
61− unsigned int flags,
62− unsigned long reason)
58+static inline int handle_userfault(struct fault_env *fe, unsigned long reason)
6359 {
6460 return VM_FAULT_SIGBUS;
6561 }
include/linux/vm_event_item.h+7−0
@@ -70,6 +70,8 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, · 69 unmodified lines
7070 THP_FAULT_FALLBACK,
7171 THP_COLLAPSE_ALLOC,
7272 THP_COLLAPSE_ALLOC_FAILED,
73+ THP_FILE_ALLOC,
74+ THP_FILE_MAPPED,
7375 THP_SPLIT_PAGE,
7476 THP_SPLIT_PAGE_FAILED,
7577 THP_DEFERRED_SPLIT_PAGE,
@@ -100,4 +102,9 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, · 24 unmodified lines
100102 NR_VM_EVENT_ITEMS
101103 };
102104
105+#ifndef CONFIG_TRANSPARENT_HUGEPAGE
106+#define THP_FILE_ALLOC ({ BUILD_BUG(); 0; })
107+#define THP_FILE_MAPPED ({ BUILD_BUG(); 0; })
108+#endif
109+
103110 #endif /* VM_EVENT_ITEM_H_INCLUDED */
include/linux/writeback.h+3−0
@@ -384,4 +384,7 @@ void tag_pages_for_writeback(struct address_space *mapping, · 383 unmodified lines
384384
385385 void account_page_redirty(struct page *page);
386386
387+void sb_mark_inode_writeback(struct inode *inode);
388+void sb_clear_inode_writeback(struct inode *inode);
389+
387390 #endif /* WRITEBACK_H */
include/trace/events/huge_memory.h+41−9
@@ -13,7 +13,7 @@ · 12 unmodified lines
1313 EM( SCAN_EXCEED_NONE_PTE, "exceed_none_pte") \
1414 EM( SCAN_PTE_NON_PRESENT, "pte_non_present") \
1515 EM( SCAN_PAGE_RO, "no_writable_page") \
16− EM( SCAN_NO_REFERENCED_PAGE, "no_referenced_page") \
16+ EM( SCAN_LACK_REFERENCED_PAGE, "lack_referenced_page") \
1717 EM( SCAN_PAGE_NULL, "page_null") \
1818 EM( SCAN_SCAN_ABORT, "scan_aborted") \
1919 EM( SCAN_PAGE_COUNT, "not_suitable_page_count") \
@@ -28,7 +28,9 @@ · 8 unmodified lines
2828 EM( SCAN_SWAP_CACHE_PAGE, "page_swap_cache") \
2929 EM( SCAN_DEL_PAGE_LRU, "could_not_delete_page_from_lru")\
3030 EM( SCAN_ALLOC_HUGE_PAGE_FAIL, "alloc_huge_page_failed") \
31− EMe( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed")
31+ EM( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed") \
32+ EM( SCAN_EXCEED_SWAP_PTE, "exceed_swap_pte") \
33+ EMe(SCAN_TRUNCATED, "truncated") \
3234
3335 #undef EM
3436 #undef EMe
@@ -45,17 +47,18 @@ SCAN_STATUS · 10 unmodified lines
4547 TRACE_EVENT(mm_khugepaged_scan_pmd,
4648
4749 TP_PROTO(struct mm_struct *mm, struct page *page, bool writable,
48− bool referenced, int none_or_zero, int status),
50+ int referenced, int none_or_zero, int status, int unmapped),
4951
50− TP_ARGS(mm, page, writable, referenced, none_or_zero, status),
52+ TP_ARGS(mm, page, writable, referenced, none_or_zero, status, unmapped),
5153
5254 TP_STRUCT__entry(
5355 __field(struct mm_struct *, mm)
5456 __field(unsigned long, pfn)
5557 __field(bool, writable)
56− __field(bool, referenced)
58+ __field(int, referenced)
5759 __field(int, none_or_zero)
5860 __field(int, status)
61+ __field(int, unmapped)
5962 ),
6063
6164 TP_fast_assign(
@@ -65,15 +68,17 @@ TRACE_EVENT(mm_khugepaged_scan_pmd, · 3 unmodified lines
6568 __entry->referenced = referenced;
6669 __entry->none_or_zero = none_or_zero;
6770 __entry->status = status;
71+ __entry->unmapped = unmapped;
6872 ),
6973
70− TP_printk("mm=%p, scan_pfn=0x%lx, writable=%d, referenced=%d, none_or_zero=%d, status=%s",
74+ TP_printk("mm=%p, scan_pfn=0x%lx, writable=%d, referenced=%d, none_or_zero=%d, status=%s, unmapped=%d",
7175 __entry->mm,
7276 __entry->pfn,
7377 __entry->writable,
7478 __entry->referenced,
7579 __entry->none_or_zero,
76− __print_symbolic(__entry->status, SCAN_STATUS))
80+ __print_symbolic(__entry->status, SCAN_STATUS),
81+ __entry->unmapped)
7782 );
7883
7984 TRACE_EVENT(mm_collapse_huge_page,
@@ -103,14 +108,14 @@ TRACE_EVENT(mm_collapse_huge_page, · 23 unmodified lines
103108 TRACE_EVENT(mm_collapse_huge_page_isolate,
104109
105110 TP_PROTO(struct page *page, int none_or_zero,
106− bool referenced, bool writable, int status),
111+ int referenced, bool writable, int status),
107112
108113 TP_ARGS(page, none_or_zero, referenced, writable, status),
109114
110115 TP_STRUCT__entry(
111116 __field(unsigned long, pfn)
112117 __field(int, none_or_zero)
113− __field(bool, referenced)
118+ __field(int, referenced)
114119 __field(bool, writable)
115120 __field(int, status)
116121 ),
@@ -131,5 +136,32 @@ TRACE_EVENT(mm_collapse_huge_page_isolate, · 14 unmodified lines
131136 __print_symbolic(__entry->status, SCAN_STATUS))
132137 );
133138
139+TRACE_EVENT(mm_collapse_huge_page_swapin,
140+
141+ TP_PROTO(struct mm_struct *mm, int swapped_in, int referenced, int ret),
142+
143+ TP_ARGS(mm, swapped_in, referenced, ret),
144+
145+ TP_STRUCT__entry(
146+ __field(struct mm_struct *, mm)
147+ __field(int, swapped_in)
148+ __field(int, referenced)
149+ __field(int, ret)
150+ ),
151+
152+ TP_fast_assign(
153+ __entry->mm = mm;
154+ __entry->swapped_in = swapped_in;
155+ __entry->referenced = referenced;
156+ __entry->ret = ret;
157+ ),
158+
159+ TP_printk("mm=%p, swapped_in=%d, referenced=%d, ret=%d",
160+ __entry->mm,
161+ __entry->swapped_in,
162+ __entry->referenced,
163+ __entry->ret)
164+);
165+
134166 #endif /* __HUGE_MEMORY_H */
135167 #include <trace/define_trace.h>
include/trace/events/writeback.h+18−4
@@ -696,7 +696,7 @@ DEFINE_EVENT(writeback_single_inode_template, writeback_single_inode, · 695 unmodified lines
696696 TP_ARGS(inode, wbc, nr_to_write)
697697 );
698698
699−DECLARE_EVENT_CLASS(writeback_lazytime_template,
699+DECLARE_EVENT_CLASS(writeback_inode_template,
700700 TP_PROTO(struct inode *inode),
701701
702702 TP_ARGS(inode),
@@ -723,25 +723,39 @@ DECLARE_EVENT_CLASS(writeback_lazytime_template, · 20 unmodified lines
723723 show_inode_state(__entry->state), __entry->mode)
724724 );
725725
726−DEFINE_EVENT(writeback_lazytime_template, writeback_lazytime,
726+DEFINE_EVENT(writeback_inode_template, writeback_lazytime,
727727 TP_PROTO(struct inode *inode),
728728
729729 TP_ARGS(inode)
730730 );
731731
732−DEFINE_EVENT(writeback_lazytime_template, writeback_lazytime_iput,
732+DEFINE_EVENT(writeback_inode_template, writeback_lazytime_iput,
733733 TP_PROTO(struct inode *inode),
734734
735735 TP_ARGS(inode)
736736 );
737737
738−DEFINE_EVENT(writeback_lazytime_template, writeback_dirty_inode_enqueue,
738+DEFINE_EVENT(writeback_inode_template, writeback_dirty_inode_enqueue,
739739
740740 TP_PROTO(struct inode *inode),
741741
742742 TP_ARGS(inode)
743743 );
744744
745+/*
746+ * Inode writeback list tracking.
747+ */
748+
749+DEFINE_EVENT(writeback_inode_template, sb_mark_inode_writeback,
750+ TP_PROTO(struct inode *inode),
751+ TP_ARGS(inode)
752+);
753+
754+DEFINE_EVENT(writeback_inode_template, sb_clear_inode_writeback,
755+ TP_PROTO(struct inode *inode),
756+ TP_ARGS(inode)
757+);
758+
745759 #endif /* _TRACE_WRITEBACK_H */
746760
747761 /* This part must be outside protection */
include/uapi/linux/magic.h+2−0
@@ -80,5 +80,7 @@ · 79 unmodified lines
8080 #define BPF_FS_MAGIC 0xcafe4a11
8181 /* Since UDF 2.01 is ISO 13346 based... */
8282 #define UDF_SUPER_MAGIC 0x15013346
83+#define BALLOON_KVM_MAGIC 0x13661366
84+#define ZSMALLOC_MAGIC 0x58295829
8385
8486 #endif /* __LINUX_MAGIC_H__ */
init/Kconfig+2−2
@@ -1786,10 +1786,10 @@ endchoice · 1785 unmodified lines
17861786
17871787 config SLAB_FREELIST_RANDOM
17881788 default n
1789− depends on SLAB
1789+ depends on SLAB || SLUB
17901790 bool "SLAB freelist randomization"
17911791 help
1792− Randomizes the freelist order used on creating new SLABs. This
1792+ Randomizes the freelist order used on creating new pages. This
17931793 security feature reduces the predictability of the kernel slab
17941794 allocator against heap overflows.
17951795
ipc/shm.c+6−4
@@ -476,13 +476,15 @@ static const struct file_operations shm_file_operations = { · 475 unmodified lines
476476 .mmap = shm_mmap,
477477 .fsync = shm_fsync,
478478 .release = shm_release,
479−#ifndef CONFIG_MMU
480479 .get_unmapped_area = shm_get_unmapped_area,
481−#endif
482480 .llseek = noop_llseek,
483481 .fallocate = shm_fallocate,
484482 };
485483
484+/*
485+ * shm_file_operations_huge is now identical to shm_file_operations,
486+ * but we keep it distinct for the sake of is_file_shm_hugepages().
487+ */
486488 static const struct file_operations shm_file_operations_huge = {
487489 .mmap = shm_mmap,
488490 .fsync = shm_fsync,
@@ -764,10 +766,10 @@ static void shm_add_rss_swap(struct shmid_kernel *shp, · 275 unmodified lines
764766 } else {
765767 #ifdef CONFIG_SHMEM
766768 struct shmem_inode_info *info = SHMEM_I(inode);
767− spin_lock(&info->lock);
769+ spin_lock_irq(&info->lock);
768770 *rss_add += inode->i_mapping->nrpages;
769771 *swp_add += info->swapped;
770− spin_unlock(&info->lock);
772+ spin_unlock_irq(&info->lock);
771773 #else
772774 *rss_add += inode->i_mapping->nrpages;
773775 #endif
kernel/fork.c+3−3
@@ -162,8 +162,8 @@ void __weak arch_release_thread_stack(unsigned long *stack) · 161 unmodified lines
162162 static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
163163 int node)
164164 {
165− struct page *page = alloc_kmem_pages_node(node, THREADINFO_GFP,
166− THREAD_SIZE_ORDER);
165+ struct page *page = alloc_pages_node(node, THREADINFO_GFP,
166+ THREAD_SIZE_ORDER);
167167
168168 if (page)
169169 memcg_kmem_update_page_stat(page, MEMCG_KERNEL_STACK,
@@ -178,7 +178,7 @@ static inline void free_thread_stack(unsigned long *stack) · 8 unmodified lines
178178
179179 memcg_kmem_update_page_stat(page, MEMCG_KERNEL_STACK,
180180 -(1 << THREAD_SIZE_ORDER));
181− __free_kmem_pages(page, THREAD_SIZE_ORDER);
181+ __free_pages(page, THREAD_SIZE_ORDER);
182182 }
183183 # else
184184 static struct kmem_cache *thread_stack_cache;
lib/Kconfig.debug+1−0
@@ -244,6 +244,7 @@ config PAGE_OWNER · 243 unmodified lines
244244 depends on DEBUG_KERNEL && STACKTRACE_SUPPORT
245245 select DEBUG_FS
246246 select STACKTRACE
247+ select STACKDEPOT
247248 select PAGE_EXTENSION
248249 help
249250 This keeps track of what call chain is the owner of a page, may
lib/dma-debug.c+2−0
@@ -253,6 +253,7 @@ static int hash_fn(struct dma_debug_entry *entry) · 252 unmodified lines
253253 */
254254 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
255255 unsigned long *flags)
256+ __acquires(&dma_entry_hash[idx].lock)
256257 {
257258 int idx = hash_fn(entry);
258259 unsigned long __flags;
@@ -267,6 +268,7 @@ static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry, · 8 unmodified lines
267268 */
268269 static void put_hash_bucket(struct hash_bucket *bucket,
269270 unsigned long *flags)
271+ __releases(&bucket->lock)
270272 {
271273 unsigned long __flags = *flags;
272274
lib/radix-tree.c+79−5
@@ -38,6 +38,9 @@ · 37 unmodified lines
3838 #include <linux/preempt.h> /* in_interrupt() */
3939
4040
41+/* Number of nodes in fully populated tree of given height */
42+static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
43+
4144 /*
4245 * Radix tree node cache.
4346 */
@@ -342,7 +345,7 @@ radix_tree_node_free(struct radix_tree_node *node) · 298 unmodified lines
342345 * To make use of this facility, the radix tree must be initialised without
343346 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
344347 */
345−static int __radix_tree_preload(gfp_t gfp_mask)
348+static int __radix_tree_preload(gfp_t gfp_mask, int nr)
346349 {
347350 struct radix_tree_preload *rtp;
348351 struct radix_tree_node *node;
@@ -350,14 +353,14 @@ static int __radix_tree_preload(gfp_t gfp_mask) · 1 unmodified line
350353
351354 preempt_disable();
352355 rtp = this_cpu_ptr(&radix_tree_preloads);
353− while (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
356+ while (rtp->nr < nr) {
354357 preempt_enable();
355358 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
356359 if (node == NULL)
357360 goto out;
358361 preempt_disable();
359362 rtp = this_cpu_ptr(&radix_tree_preloads);
360− if (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
363+ if (rtp->nr < nr) {
361364 node->private_data = rtp->nodes;
362365 rtp->nodes = node;
363366 rtp->nr++;
@@ -383,7 +386,7 @@ int radix_tree_preload(gfp_t gfp_mask) · 19 unmodified lines
383386 {
384387 /* Warn on non-sensical use... */
385388 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
386− return __radix_tree_preload(gfp_mask);
389+ return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
387390 }
388391 EXPORT_SYMBOL(radix_tree_preload);
389392
@@ -395,13 +398,58 @@ EXPORT_SYMBOL(radix_tree_preload); · 5 unmodified lines
395398 int radix_tree_maybe_preload(gfp_t gfp_mask)
396399 {
397400 if (gfpflags_allow_blocking(gfp_mask))
398− return __radix_tree_preload(gfp_mask);
401+ return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
399402 /* Preloading doesn't help anything with this gfp mask, skip it */
400403 preempt_disable();
401404 return 0;
402405 }
403406 EXPORT_SYMBOL(radix_tree_maybe_preload);
404407
408+/*
409+ * The same as function above, but preload number of nodes required to insert
410+ * (1 << order) continuous naturally-aligned elements.
411+ */
412+int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
413+{
414+ unsigned long nr_subtrees;
415+ int nr_nodes, subtree_height;
416+
417+ /* Preloading doesn't help anything with this gfp mask, skip it */
418+ if (!gfpflags_allow_blocking(gfp_mask)) {
419+ preempt_disable();
420+ return 0;
421+ }
422+
423+ /*
424+ * Calculate number and height of fully populated subtrees it takes to
425+ * store (1 << order) elements.
426+ */
427+ nr_subtrees = 1 << order;
428+ for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
429+ subtree_height++)
430+ nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
431+
432+ /*
433+ * The worst case is zero height tree with a single item at index 0 and
434+ * then inserting items starting at ULONG_MAX - (1 << order).
435+ *
436+ * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
437+ * 0-index item.
438+ */
439+ nr_nodes = RADIX_TREE_MAX_PATH;
440+
441+ /* Plus branch to fully populated subtrees. */
442+ nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
443+
444+ /* Root node is shared. */
445+ nr_nodes--;
446+
447+ /* Plus nodes required to build subtrees. */
448+ nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
449+
450+ return __radix_tree_preload(gfp_mask, nr_nodes);
451+}
452+
405453 /*
406454 * The maximum index which can be stored in a radix tree
407455 */
@@ -1571,6 +1619,31 @@ radix_tree_node_ctor(void *arg) · 1163 unmodified lines
15711619 INIT_LIST_HEAD(&node->private_list);
15721620 }
15731621
1622+static __init unsigned long __maxindex(unsigned int height)
1623+{
1624+ unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1625+ int shift = RADIX_TREE_INDEX_BITS - width;
1626+
1627+ if (shift < 0)
1628+ return ~0UL;
1629+ if (shift >= BITS_PER_LONG)
1630+ return 0UL;
1631+ return ~0UL >> shift;
1632+}
1633+
1634+static __init void radix_tree_init_maxnodes(void)
1635+{
1636+ unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1637+ unsigned int i, j;
1638+
1639+ for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1640+ height_to_maxindex[i] = __maxindex(i);
1641+ for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1642+ for (j = i; j > 0; j--)
1643+ height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1644+ }
1645+}
1646+
15741647 static int radix_tree_callback(struct notifier_block *nfb,
15751648 unsigned long action, void *hcpu)
15761649 {
@@ -1597,5 +1670,6 @@ void __init radix_tree_init(void) · 20 unmodified lines
15971670 sizeof(struct radix_tree_node), 0,
15981671 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
15991672 radix_tree_node_ctor);
1673+ radix_tree_init_maxnodes();
16001674 hotcpu_notifier(radix_tree_callback, 0);
16011675 }
mm/Kconfig+8−0
@@ -439,6 +439,14 @@ choice · 438 unmodified lines
439439 benefit.
440440 endchoice
441441
442+#
443+# We don't deposit page tables on file THP mapping,
444+# but Power makes use of them to address MMU quirk.
445+#
446+config TRANSPARENT_HUGE_PAGECACHE
447+ def_bool y
448+ depends on TRANSPARENT_HUGEPAGE && !PPC
449+
442450 #
443451 # UP and nommu archs use km based percpu allocator
444452 #
mm/Makefile+1−1
@@ -74,7 +74,7 @@ obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o · 73 unmodified lines
7474 obj-$(CONFIG_MEMTEST) += memtest.o
7575 obj-$(CONFIG_MIGRATION) += migrate.o
7676 obj-$(CONFIG_QUICKLIST) += quicklist.o
77−obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o
77+obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
7878 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
7979 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
8080 obj-$(CONFIG_MEMCG_SWAP) += swap_cgroup.o
mm/balloon_compaction.c+17−77
@@ -70,7 +70,7 @@ struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info) · 69 unmodified lines
7070 */
7171 if (trylock_page(page)) {
7272 #ifdef CONFIG_BALLOON_COMPACTION
73− if (!PagePrivate(page)) {
73+ if (PageIsolated(page)) {
7474 /* raced with isolation */
7575 unlock_page(page);
7676 continue;
@@ -106,110 +106,50 @@ EXPORT_SYMBOL_GPL(balloon_page_dequeue); · 29 unmodified lines
106106
107107 #ifdef CONFIG_BALLOON_COMPACTION
108108
109−static inline void __isolate_balloon_page(struct page *page)
109+bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
110+
110111 {
111112 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
112113 unsigned long flags;
113114
114115 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
115− ClearPagePrivate(page);
116116 list_del(&page->lru);
117117 b_dev_info->isolated_pages++;
118118 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
119+
120+ return true;
119121 }
120122
121−static inline void __putback_balloon_page(struct page *page)
123+void balloon_page_putback(struct page *page)
122124 {
123125 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
124126 unsigned long flags;
125127
126128 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
127− SetPagePrivate(page);
128129 list_add(&page->lru, &b_dev_info->pages);
129130 b_dev_info->isolated_pages--;
130131 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
131132 }
132133
133−/* __isolate_lru_page() counterpart for a ballooned page */
134−bool balloon_page_isolate(struct page *page)
135−{
136− /*
137− * Avoid burning cycles with pages that are yet under __free_pages(),
138− * or just got freed under us.
139− *
140− * In case we 'win' a race for a balloon page being freed under us and
141− * raise its refcount preventing __free_pages() from doing its job
142− * the put_page() at the end of this block will take care of
143− * release this page, thus avoiding a nasty leakage.
144− */
145− if (likely(get_page_unless_zero(page))) {
146− /*
147− * As balloon pages are not isolated from LRU lists, concurrent
148− * compaction threads can race against page migration functions
149− * as well as race against the balloon driver releasing a page.
150− *
151− * In order to avoid having an already isolated balloon page
152− * being (wrongly) re-isolated while it is under migration,
153− * or to avoid attempting to isolate pages being released by
154− * the balloon driver, lets be sure we have the page lock
155− * before proceeding with the balloon page isolation steps.
156− */
157− if (likely(trylock_page(page))) {
158− /*
159− * A ballooned page, by default, has PagePrivate set.
160− * Prevent concurrent compaction threads from isolating
161− * an already isolated balloon page by clearing it.
162− */
163− if (balloon_page_movable(page)) {
164− __isolate_balloon_page(page);
165− unlock_page(page);
166− return true;
167− }
168− unlock_page(page);
169− }
170− put_page(page);
171− }
172− return false;
173−}
174−
175−/* putback_lru_page() counterpart for a ballooned page */
176−void balloon_page_putback(struct page *page)
177−{
178− /*
179− * 'lock_page()' stabilizes the page and prevents races against
180− * concurrent isolation threads attempting to re-isolate it.
181− */
182− lock_page(page);
183−
184− if (__is_movable_balloon_page(page)) {
185− __putback_balloon_page(page);
186− /* drop the extra ref count taken for page isolation */
187− put_page(page);
188− } else {
189− WARN_ON(1);
190− dump_page(page, "not movable balloon page");
191− }
192− unlock_page(page);
193−}
194134
195135 /* move_to_new_page() counterpart for a ballooned page */
196−int balloon_page_migrate(struct page *newpage,
197− struct page *page, enum migrate_mode mode)
136+int balloon_page_migrate(struct address_space *mapping,
137+ struct page *newpage, struct page *page,
138+ enum migrate_mode mode)
198139 {
199140 struct balloon_dev_info *balloon = balloon_page_device(page);
200− int rc = -EAGAIN;
201141
202142 VM_BUG_ON_PAGE(!PageLocked(page), page);
203143 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
204144
205− if (WARN_ON(!__is_movable_balloon_page(page))) {
206− dump_page(page, "not movable balloon page");
207− return rc;
208− }
145+ return balloon->migratepage(balloon, newpage, page, mode);
146+}
209147
210− if (balloon && balloon->migratepage)
211− rc = balloon->migratepage(balloon, newpage, page, mode);
148+const struct address_space_operations balloon_aops = {
149+ .migratepage = balloon_page_migrate,
150+ .isolate_page = balloon_page_isolate,
151+ .putback_page = balloon_page_putback,
152+};
153+EXPORT_SYMBOL_GPL(balloon_aops);
212154
213− return rc;
214−}
215155 #endif /* CONFIG_BALLOON_COMPACTION */
mm/compaction.c+91−32
@@ -15,11 +15,11 @@ · 14 unmodified lines
1515 #include <linux/backing-dev.h>
1616 #include <linux/sysctl.h>
1717 #include <linux/sysfs.h>
18−#include <linux/balloon_compaction.h>
1918 #include <linux/page-isolation.h>
2019 #include <linux/kasan.h>
2120 #include <linux/kthread.h>
2221 #include <linux/freezer.h>
22+#include <linux/page_owner.h>
2323 #include "internal.h"
2424
2525 #ifdef CONFIG_COMPACTION
@@ -65,13 +65,27 @@ static unsigned long release_freepages(struct list_head *freelist) · 39 unmodified lines
6565
6666 static void map_pages(struct list_head *list)
6767 {
68− struct page *page;
68+ unsigned int i, order, nr_pages;
69+ struct page *page, *next;
70+ LIST_HEAD(tmp_list);
71+
72+ list_for_each_entry_safe(page, next, list, lru) {
73+ list_del(&page->lru);
6974
70− list_for_each_entry(page, list, lru) {
71− arch_alloc_page(page, 0);
72− kernel_map_pages(page, 1, 1);
73− kasan_alloc_pages(page, 0);
75+ order = page_private(page);
76+ nr_pages = 1 << order;
77+
78+ post_alloc_hook(page, order, __GFP_MOVABLE);
79+ if (order)
80+ split_page(page, order);
81+
82+ for (i = 0; i < nr_pages; i++) {
83+ list_add(&page->lru, &tmp_list);
84+ page++;
85+ }
7486 }
87+
88+ list_splice(&tmp_list, list);
7589 }
7690
7791 static inline bool migrate_async_suitable(int migratetype)
@@ -81,6 +95,44 @@ static inline bool migrate_async_suitable(int migratetype) · 3 unmodified lines
8195
8296 #ifdef CONFIG_COMPACTION
8397
98+int PageMovable(struct page *page)
99+{
100+ struct address_space *mapping;
101+
102+ VM_BUG_ON_PAGE(!PageLocked(page), page);
103+ if (!__PageMovable(page))
104+ return 0;
105+
106+ mapping = page_mapping(page);
107+ if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
108+ return 1;
109+
110+ return 0;
111+}
112+EXPORT_SYMBOL(PageMovable);
113+
114+void __SetPageMovable(struct page *page, struct address_space *mapping)
115+{
116+ VM_BUG_ON_PAGE(!PageLocked(page), page);
117+ VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
118+ page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
119+}
120+EXPORT_SYMBOL(__SetPageMovable);
121+
122+void __ClearPageMovable(struct page *page)
123+{
124+ VM_BUG_ON_PAGE(!PageLocked(page), page);
125+ VM_BUG_ON_PAGE(!PageMovable(page), page);
126+ /*
127+ * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
128+ * flag so that VM can catch up released page by driver after isolation.
129+ * With it, VM migration doesn't try to put it back.
130+ */
131+ page->mapping = (void *)((unsigned long)page->mapping &
132+ PAGE_MAPPING_MOVABLE);
133+}
134+EXPORT_SYMBOL(__ClearPageMovable);
135+
84136 /* Do not skip compaction more than 64 times */
85137 #define COMPACT_MAX_DEFER_SHIFT 6
86138
@@ -368,12 +420,13 @@ static unsigned long isolate_freepages_block(struct compact_control *cc, · 281 unmodified lines
368420 unsigned long flags = 0;
369421 bool locked = false;
370422 unsigned long blockpfn = *start_pfn;
423+ unsigned int order;
371424
372425 cursor = pfn_to_page(blockpfn);
373426
374427 /* Isolate free pages. */
375428 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
376− int isolated, i;
429+ int isolated;
377430 struct page *page = cursor;
378431
379432 /*
@@ -439,17 +492,17 @@ static unsigned long isolate_freepages_block(struct compact_control *cc, · 59 unmodified lines
439492 goto isolate_fail;
440493 }
441494
442− /* Found a free page, break it into order-0 pages */
443− isolated = split_free_page(page);
495+ /* Found a free page, will break it into order-0 pages */
496+ order = page_order(page);
497+ isolated = __isolate_free_page(page, order);
444498 if (!isolated)
445499 break;
500+ set_page_private(page, order);
446501
447502 total_isolated += isolated;
448503 cc->nr_freepages += isolated;
449− for (i = 0; i < isolated; i++) {
450− list_add(&page->lru, freelist);
451− page++;
452− }
504+ list_add_tail(&page->lru, freelist);
505+
453506 if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
454507 blockpfn += isolated;
455508 break;
@@ -568,7 +621,7 @@ isolate_freepages_range(struct compact_control *cc, · 112 unmodified lines
568621 */
569622 }
570623
571− /* split_free_page does not map the pages */
624+ /* __isolate_free_page() does not map the pages */
572625 map_pages(&freelist);
573626
574627 if (pfn < end_pfn) {
@@ -670,7 +723,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · 95 unmodified lines
670723
671724 /* Time to isolate some pages for migration */
672725 for (; low_pfn < end_pfn; low_pfn++) {
673− bool is_lru;
674726
675727 if (skip_on_failure && low_pfn >= next_skip_pfn) {
676728 /*
@@ -732,21 +784,6 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · 55 unmodified lines
732784 continue;
733785 }
734786
735− /*
736− * Check may be lockless but that's ok as we recheck later.
737− * It's possible to migrate LRU pages and balloon pages
738− * Skip any other type of page
739− */
740− is_lru = PageLRU(page);
741− if (!is_lru) {
742− if (unlikely(balloon_page_movable(page))) {
743− if (balloon_page_isolate(page)) {
744− /* Successfully isolated */
745− goto isolate_success;
746− }
747− }
748− }
749−
750787 /*
751788 * Regardless of being on LRU, compound pages such as THP and
752789 * hugetlbfs are not to be compacted. We can potentially save
@@ -763,8 +800,30 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · 10 unmodified lines
763800 goto isolate_fail;
764801 }
765802
766− if (!is_lru)
803+ /*
804+ * Check may be lockless but that's ok as we recheck later.
805+ * It's possible to migrate LRU and non-lru movable pages.
806+ * Skip any other type of page
807+ */
808+ if (!PageLRU(page)) {
809+ /*
810+ * __PageMovable can return false positive so we need
811+ * to verify it under page_lock.
812+ */
813+ if (unlikely(__PageMovable(page)) &&
814+ !PageIsolated(page)) {
815+ if (locked) {
816+ spin_unlock_irqrestore(&zone->lru_lock,
817+ flags);
818+ locked = false;
819+ }
820+
821+ if (isolate_movable_page(page, isolate_mode))
822+ goto isolate_success;
823+ }
824+
767825 goto isolate_fail;
826+ }
768827
769828 /*
770829 * Migration will fail if an anonymous page is pinned in memory,
@@ -1059,7 +1118,7 @@ static void isolate_freepages(struct compact_control *cc) · 288 unmodified lines
10591118 }
10601119 }
10611120
1062− /* split_free_page does not map the pages */
1121+ /* __isolate_free_page() does not map the pages */
10631122 map_pages(freelist);
10641123
10651124 /*
mm/filemap.c+146−71
@@ -114,14 +114,14 @@ static void page_cache_tree_delete(struct address_space *mapping, · 113 unmodified lines
114114 struct page *page, void *shadow)
115115 {
116116 struct radix_tree_node *node;
117+ int i, nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
117118
118− VM_BUG_ON(!PageLocked(page));
119−
120− node = radix_tree_replace_clear_tags(&mapping->page_tree, page->index,
121− shadow);
119+ VM_BUG_ON_PAGE(!PageLocked(page), page);
120+ VM_BUG_ON_PAGE(PageTail(page), page);
121+ VM_BUG_ON_PAGE(nr != 1 && shadow, page);
122122
123123 if (shadow) {
124− mapping->nrexceptional++;
124+ mapping->nrexceptional += nr;
125125 /*
126126 * Make sure the nrexceptional update is committed before
127127 * the nrpages update so that final truncate racing
@@ -130,31 +130,38 @@ static void page_cache_tree_delete(struct address_space *mapping, · 2 unmodified lines
130130 */
131131 smp_wmb();
132132 }
133− mapping->nrpages--;
133+ mapping->nrpages -= nr;
134134
135− if (!node)
136− return;
137−
138− workingset_node_pages_dec(node);
139− if (shadow)
140− workingset_node_shadows_inc(node);
141− else
142− if (__radix_tree_delete_node(&mapping->page_tree, node))
135+ for (i = 0; i < nr; i++) {
136+ node = radix_tree_replace_clear_tags(&mapping->page_tree,
137+ page->index + i, shadow);
138+ if (!node) {
139+ VM_BUG_ON_PAGE(nr != 1, page);
143140 return;
141+ }
144142
145− /*
146− * Track node that only contains shadow entries. DAX mappings contain
147− * no shadow entries and may contain other exceptional entries so skip
148− * those.
149− *
150− * Avoid acquiring the list_lru lock if already tracked. The
151− * list_empty() test is safe as node->private_list is
152− * protected by mapping->tree_lock.
153− */
154− if (!dax_mapping(mapping) && !workingset_node_pages(node) &&
155− list_empty(&node->private_list)) {
156− node->private_data = mapping;
157− list_lru_add(&workingset_shadow_nodes, &node->private_list);
143+ workingset_node_pages_dec(node);
144+ if (shadow)
145+ workingset_node_shadows_inc(node);
146+ else
147+ if (__radix_tree_delete_node(&mapping->page_tree, node))
148+ continue;
149+
150+ /*
151+ * Track node that only contains shadow entries. DAX mappings
152+ * contain no shadow entries and may contain other exceptional
153+ * entries so skip those.
154+ *
155+ * Avoid acquiring the list_lru lock if already tracked.
156+ * The list_empty() test is safe as node->private_list is
157+ * protected by mapping->tree_lock.
158+ */
159+ if (!dax_mapping(mapping) && !workingset_node_pages(node) &&
160+ list_empty(&node->private_list)) {
161+ node->private_data = mapping;
162+ list_lru_add(&workingset_shadow_nodes,
163+ &node->private_list);
164+ }
158165 }
159166 }
160167
@@ -166,6 +173,7 @@ static void page_cache_tree_delete(struct address_space *mapping, · 5 unmodified lines
166173 void __delete_from_page_cache(struct page *page, void *shadow)
167174 {
168175 struct address_space *mapping = page->mapping;
176+ int nr = hpage_nr_pages(page);
169177
170178 trace_mm_filemap_delete_from_page_cache(page);
171179 /*
@@ -178,6 +186,7 @@ void __delete_from_page_cache(struct page *page, void *shadow) · 6 unmodified lines
178186 else
179187 cleancache_invalidate_page(mapping, page);
180188
189+ VM_BUG_ON_PAGE(PageTail(page), page);
181190 VM_BUG_ON_PAGE(page_mapped(page), page);
182191 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
183192 int mapcount;
@@ -209,9 +218,14 @@ void __delete_from_page_cache(struct page *page, void *shadow) · 25 unmodified lines
209218
210219 /* hugetlb pages do not participate in page cache accounting. */
211220 if (!PageHuge(page))
212− __dec_zone_page_state(page, NR_FILE_PAGES);
213− if (PageSwapBacked(page))
214− __dec_zone_page_state(page, NR_SHMEM);
221+ __mod_zone_page_state(page_zone(page), NR_FILE_PAGES, -nr);
222+ if (PageSwapBacked(page)) {
223+ __mod_zone_page_state(page_zone(page), NR_SHMEM, -nr);
224+ if (PageTransHuge(page))
225+ __dec_zone_page_state(page, NR_SHMEM_THPS);
226+ } else {
227+ VM_BUG_ON_PAGE(PageTransHuge(page) && !PageHuge(page), page);
228+ }
215229
216230 /*
217231 * At this point page must be either written or cleaned by truncate.
@@ -235,9 +249,8 @@ void __delete_from_page_cache(struct page *page, void *shadow) · 17 unmodified lines
235249 */
236250 void delete_from_page_cache(struct page *page)
237251 {
238− struct address_space *mapping = page->mapping;
252+ struct address_space *mapping = page_mapping(page);
239253 unsigned long flags;
240−
241254 void (*freepage)(struct page *);
242255
243256 BUG_ON(!PageLocked(page));
@@ -250,7 +263,13 @@ void delete_from_page_cache(struct page *page) · 6 unmodified lines
250263
251264 if (freepage)
252265 freepage(page);
253− put_page(page);
266+
267+ if (PageTransHuge(page) && !PageHuge(page)) {
268+ page_ref_sub(page, HPAGE_PMD_NR);
269+ VM_BUG_ON_PAGE(page_count(page) <= 0, page);
270+ } else {
271+ put_page(page);
272+ }
254273 }
255274 EXPORT_SYMBOL(delete_from_page_cache);
256275
@@ -1053,7 +1072,7 @@ EXPORT_SYMBOL(page_cache_prev_hole); · 796 unmodified lines
10531072 struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
10541073 {
10551074 void **pagep;
1056− struct page *page;
1075+ struct page *head, *page;
10571076
10581077 rcu_read_lock();
10591078 repeat:
@@ -1073,16 +1092,24 @@ repeat: · 13 unmodified lines
10731092 */
10741093 goto out;
10751094 }
1076− if (!page_cache_get_speculative(page))
1095+
1096+ head = compound_head(page);
1097+ if (!page_cache_get_speculative(head))
10771098 goto repeat;
10781099
1100+ /* The page was split under us? */
1101+ if (compound_head(page) != head) {
1102+ put_page(head);
1103+ goto repeat;
1104+ }
1105+
10791106 /*
10801107 * Has the page moved?
10811108 * This is part of the lockless pagecache protocol. See
10821109 * include/linux/pagemap.h for details.
10831110 */
10841111 if (unlikely(page != *pagep)) {
1085− put_page(page);
1112+ put_page(head);
10861113 goto repeat;
10871114 }
10881115 }
@@ -1118,12 +1145,12 @@ repeat: · 29 unmodified lines
11181145 if (page && !radix_tree_exception(page)) {
11191146 lock_page(page);
11201147 /* Has the page been truncated? */
1121− if (unlikely(page->mapping != mapping)) {
1148+ if (unlikely(page_mapping(page) != mapping)) {
11221149 unlock_page(page);
11231150 put_page(page);
11241151 goto repeat;
11251152 }
1126− VM_BUG_ON_PAGE(page->index != offset, page);
1153+ VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
11271154 }
11281155 return page;
11291156 }
@@ -1255,7 +1282,7 @@ unsigned find_get_entries(struct address_space *mapping, · 125 unmodified lines
12551282
12561283 rcu_read_lock();
12571284 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1258− struct page *page;
1285+ struct page *head, *page;
12591286 repeat:
12601287 page = radix_tree_deref_slot(slot);
12611288 if (unlikely(!page))
@@ -1272,12 +1299,20 @@ repeat: · 10 unmodified lines
12721299 */
12731300 goto export;
12741301 }
1275− if (!page_cache_get_speculative(page))
1302+
1303+ head = compound_head(page);
1304+ if (!page_cache_get_speculative(head))
12761305 goto repeat;
12771306
1307+ /* The page was split under us? */
1308+ if (compound_head(page) != head) {
1309+ put_page(head);
1310+ goto repeat;
1311+ }
1312+
12781313 /* Has the page moved? */
12791314 if (unlikely(page != *slot)) {
1280− put_page(page);
1315+ put_page(head);
12811316 goto repeat;
12821317 }
12831318 export:
@@ -1318,7 +1353,7 @@ unsigned find_get_pages(struct address_space *mapping, pgoff_t start, · 34 unmodified lines
13181353
13191354 rcu_read_lock();
13201355 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1321− struct page *page;
1356+ struct page *head, *page;
13221357 repeat:
13231358 page = radix_tree_deref_slot(slot);
13241359 if (unlikely(!page))
@@ -1337,12 +1372,19 @@ repeat: · 12 unmodified lines
13371372 continue;
13381373 }
13391374
1340− if (!page_cache_get_speculative(page))
1375+ head = compound_head(page);
1376+ if (!page_cache_get_speculative(head))
13411377 goto repeat;
13421378
1379+ /* The page was split under us? */
1380+ if (compound_head(page) != head) {
1381+ put_page(head);
1382+ goto repeat;
1383+ }
1384+
13431385 /* Has the page moved? */
13441386 if (unlikely(page != *slot)) {
1345− put_page(page);
1387+ put_page(head);
13461388 goto repeat;
13471389 }
13481390
@@ -1379,7 +1421,7 @@ unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, · 30 unmodified lines
13791421
13801422 rcu_read_lock();
13811423 radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
1382− struct page *page;
1424+ struct page *head, *page;
13831425 repeat:
13841426 page = radix_tree_deref_slot(slot);
13851427 /* The hole, there no reason to continue */
@@ -1399,12 +1441,19 @@ repeat: · 13 unmodified lines
13991441 break;
14001442 }
14011443
1402− if (!page_cache_get_speculative(page))
1444+ head = compound_head(page);
1445+ if (!page_cache_get_speculative(head))
14031446 goto repeat;
14041447
1448+ /* The page was split under us? */
1449+ if (compound_head(page) != head) {
1450+ put_page(head);
1451+ goto repeat;
1452+ }
1453+
14051454 /* Has the page moved? */
14061455 if (unlikely(page != *slot)) {
1407− put_page(page);
1456+ put_page(head);
14081457 goto repeat;
14091458 }
14101459
@@ -1413,7 +1462,7 @@ repeat: · 2 unmodified lines
14131462 * otherwise we can get both false positives and false
14141463 * negatives, which is just confusing to the caller.
14151464 */
1416− if (page->mapping == NULL || page->index != iter.index) {
1465+ if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
14171466 put_page(page);
14181467 break;
14191468 }
@@ -1451,7 +1500,7 @@ unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, · 31 unmodified lines
14511500 rcu_read_lock();
14521501 radix_tree_for_each_tagged(slot, &mapping->page_tree,
14531502 &iter, *index, tag) {
1454− struct page *page;
1503+ struct page *head, *page;
14551504 repeat:
14561505 page = radix_tree_deref_slot(slot);
14571506 if (unlikely(!page))
@@ -1476,12 +1525,19 @@ repeat: · 18 unmodified lines
14761525 continue;
14771526 }
14781527
1479− if (!page_cache_get_speculative(page))
1528+ head = compound_head(page);
1529+ if (!page_cache_get_speculative(head))
14801530 goto repeat;
14811531
1532+ /* The page was split under us? */
1533+ if (compound_head(page) != head) {
1534+ put_page(head);
1535+ goto repeat;
1536+ }
1537+
14821538 /* Has the page moved? */
14831539 if (unlikely(page != *slot)) {
1484− put_page(page);
1540+ put_page(head);
14851541 goto repeat;
14861542 }
14871543
@@ -1525,7 +1581,7 @@ unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start, · 37 unmodified lines
15251581 rcu_read_lock();
15261582 radix_tree_for_each_tagged(slot, &mapping->page_tree,
15271583 &iter, start, tag) {
1528− struct page *page;
1584+ struct page *head, *page;
15291585 repeat:
15301586 page = radix_tree_deref_slot(slot);
15311587 if (unlikely(!page))
@@ -1543,12 +1599,20 @@ repeat: · 11 unmodified lines
15431599 */
15441600 goto export;
15451601 }
1546− if (!page_cache_get_speculative(page))
1602+
1603+ head = compound_head(page);
1604+ if (!page_cache_get_speculative(head))
15471605 goto repeat;
15481606
1607+ /* The page was split under us? */
1608+ if (compound_head(page) != head) {
1609+ put_page(head);
1610+ goto repeat;
1611+ }
1612+
15491613 /* Has the page moved? */
15501614 if (unlikely(page != *slot)) {
1551− put_page(page);
1615+ put_page(head);
15521616 goto repeat;
15531617 }
15541618 export:
@@ -2128,21 +2192,21 @@ page_not_uptodate: · 573 unmodified lines
21282192 }
21292193 EXPORT_SYMBOL(filemap_fault);
21302194
2131−void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
2195+void filemap_map_pages(struct fault_env *fe,
2196+ pgoff_t start_pgoff, pgoff_t end_pgoff)
21322197 {
21332198 struct radix_tree_iter iter;
21342199 void **slot;
2135− struct file *file = vma->vm_file;
2200+ struct file *file = fe->vma->vm_file;
21362201 struct address_space *mapping = file->f_mapping;
2202+ pgoff_t last_pgoff = start_pgoff;
21372203 loff_t size;
2138− struct page *page;
2139− unsigned long address = (unsigned long) vmf->virtual_address;
2140− unsigned long addr;
2141− pte_t *pte;
2204+ struct page *head, *page;
21422205
21432206 rcu_read_lock();
2144− radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, vmf->pgoff) {
2145− if (iter.index > vmf->max_pgoff)
2207+ radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
2208+ start_pgoff) {
2209+ if (iter.index > end_pgoff)
21462210 break;
21472211 repeat:
21482212 page = radix_tree_deref_slot(slot);
@@ -2156,12 +2220,19 @@ repeat: · 7 unmodified lines
21562220 goto next;
21572221 }
21582222
2159− if (!page_cache_get_speculative(page))
2223+ head = compound_head(page);
2224+ if (!page_cache_get_speculative(head))
21602225 goto repeat;
21612226
2227+ /* The page was split under us? */
2228+ if (compound_head(page) != head) {
2229+ put_page(head);
2230+ goto repeat;
2231+ }
2232+
21622233 /* Has the page moved? */
21632234 if (unlikely(page != *slot)) {
2164− put_page(page);
2235+ put_page(head);
21652236 goto repeat;
21662237 }
21672238
@@ -2179,14 +2250,15 @@ repeat: · 11 unmodified lines
21792250 if (page->index >= size >> PAGE_SHIFT)
21802251 goto unlock;
21812252
2182− pte = vmf->pte + page->index - vmf->pgoff;
2183− if (!pte_none(*pte))
2184− goto unlock;
2185−
21862253 if (file->f_ra.mmap_miss > 0)
21872254 file->f_ra.mmap_miss--;
2188− addr = address + (page->index - vmf->pgoff) * PAGE_SIZE;
2189− do_set_pte(vma, addr, page, pte, false, false);
2255+
2256+ fe->address += (iter.index - last_pgoff) << PAGE_SHIFT;
2257+ if (fe->pte)
2258+ fe->pte += iter.index - last_pgoff;
2259+ last_pgoff = iter.index;
2260+ if (alloc_set_pte(fe, NULL, page))
2261+ goto unlock;
21902262 unlock_page(page);
21912263 goto next;
21922264 unlock:
@@ -2194,7 +2266,10 @@ unlock: · 1 unmodified line
21942266 skip:
21952267 put_page(page);
21962268 next:
2197− if (iter.index == vmf->max_pgoff)
2269+ /* Huge page is mapped? No need to proceed. */
2270+ if (pmd_trans_huge(*fe->pmd))
2271+ break;
2272+ if (iter.index == end_pgoff)
21982273 break;
21992274 }
22002275 rcu_read_unlock();
mm/frontswap.c+15−20
@@ -20,6 +20,8 @@ · 19 unmodified lines
2020 #include <linux/frontswap.h>
2121 #include <linux/swapfile.h>
2222
23+DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
24+
2325 /*
2426 * frontswap_ops are added by frontswap_register_ops, and provide the
2527 * frontswap "backend" implementation functions. Multiple implementations
@@ -139,6 +141,8 @@ void frontswap_register_ops(struct frontswap_ops *ops) · 113 unmodified lines
139141 ops->next = frontswap_ops;
140142 } while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
141143
144+ static_branch_inc(&frontswap_enabled_key);
145+
142146 spin_lock(&swap_lock);
143147 plist_for_each_entry(si, &swap_active_head, list) {
144148 if (si->frontswap_map)
@@ -189,7 +193,7 @@ void __frontswap_init(unsigned type, unsigned long *map) · 44 unmodified lines
189193 struct swap_info_struct *sis = swap_info[type];
190194 struct frontswap_ops *ops;
191195
192− BUG_ON(sis == NULL);
196+ VM_BUG_ON(sis == NULL);
193197
194198 /*
195199 * p->frontswap is a bitmap that we MUST have to figure out which page
@@ -248,15 +252,9 @@ int __frontswap_store(struct page *page) · 52 unmodified lines
248252 pgoff_t offset = swp_offset(entry);
249253 struct frontswap_ops *ops;
250254
251− /*
252− * Return if no backend registed.
253− * Don't need to inc frontswap_failed_stores here.
254− */
255− if (!frontswap_ops)
256− return -1;
257−
258− BUG_ON(!PageLocked(page));
259− BUG_ON(sis == NULL);
255+ VM_BUG_ON(!frontswap_ops);
256+ VM_BUG_ON(!PageLocked(page));
257+ VM_BUG_ON(sis == NULL);
260258
261259 /*
262260 * If a dup, we must remove the old page first; we can't leave the
@@ -303,11 +301,10 @@ int __frontswap_load(struct page *page) · 40 unmodified lines
303301 pgoff_t offset = swp_offset(entry);
304302 struct frontswap_ops *ops;
305303
306− if (!frontswap_ops)
307− return -1;
304+ VM_BUG_ON(!frontswap_ops);
305+ VM_BUG_ON(!PageLocked(page));
306+ VM_BUG_ON(sis == NULL);
308307
309− BUG_ON(!PageLocked(page));
310− BUG_ON(sis == NULL);
311308 if (!__frontswap_test(sis, offset))
312309 return -1;
313310
@@ -337,10 +334,9 @@ void __frontswap_invalidate_page(unsigned type, pgoff_t offset) · 23 unmodified lines
337334 struct swap_info_struct *sis = swap_info[type];
338335 struct frontswap_ops *ops;
339336
340− if (!frontswap_ops)
341− return;
337+ VM_BUG_ON(!frontswap_ops);
338+ VM_BUG_ON(sis == NULL);
342339
343− BUG_ON(sis == NULL);
344340 if (!__frontswap_test(sis, offset))
345341 return;
346342
@@ -360,10 +356,9 @@ void __frontswap_invalidate_area(unsigned type) · 13 unmodified lines
360356 struct swap_info_struct *sis = swap_info[type];
361357 struct frontswap_ops *ops;
362358
363− if (!frontswap_ops)
364− return;
359+ VM_BUG_ON(!frontswap_ops);
360+ VM_BUG_ON(sis == NULL);
365361
366− BUG_ON(sis == NULL);
367362 if (sis->frontswap_map == NULL)
368363 return;
369364
mm/gup.c+6−3
@@ -279,6 +279,8 @@ struct page *follow_page_mask(struct vm_area_struct *vma, · 278 unmodified lines
279279 spin_unlock(ptl);
280280 ret = 0;
281281 split_huge_pmd(vma, pmd, address);
282+ if (pmd_trans_unstable(pmd))
283+ ret = -EBUSY;
282284 } else {
283285 get_page(page);
284286 spin_unlock(ptl);
@@ -286,6 +288,8 @@ struct page *follow_page_mask(struct vm_area_struct *vma, · 1 unmodified line
286288 ret = split_huge_page(page);
287289 unlock_page(page);
288290 put_page(page);
291+ if (pmd_none(*pmd))
292+ return no_page_table(vma, flags);
289293 }
290294
291295 return ret ? ERR_PTR(ret) :
@@ -350,7 +354,6 @@ unmap: · 58 unmodified lines
350354 static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
351355 unsigned long address, unsigned int *flags, int *nonblocking)
352356 {
353− struct mm_struct *mm = vma->vm_mm;
354357 unsigned int fault_flags = 0;
355358 int ret;
356359
@@ -375,7 +378,7 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, · 18 unmodified lines
375378 fault_flags |= FAULT_FLAG_TRIED;
376379 }
377380
378− ret = handle_mm_fault(mm, vma, address, fault_flags);
381+ ret = handle_mm_fault(vma, address, fault_flags);
379382 if (ret & VM_FAULT_ERROR) {
380383 if (ret & VM_FAULT_OOM)
381384 return -ENOMEM;
@@ -690,7 +693,7 @@ retry: · 308 unmodified lines
690693 if (!vma_permits_fault(vma, fault_flags))
691694 return -EFAULT;
692695
693− ret = handle_mm_fault(mm, vma, address, fault_flags);
696+ ret = handle_mm_fault(vma, address, fault_flags);
694697 major |= ret & VM_FAULT_MAJOR;
695698 if (ret & VM_FAULT_ERROR) {
696699 if (ret & VM_FAULT_OOM)
mm/huge_memory.c+366−1645
@@ -18,7 +18,6 @@ · 17 unmodified lines
1818 #include <linux/mm_inline.h>
1919 #include <linux/swapops.h>
2020 #include <linux/dax.h>
21−#include <linux/kthread.h>
2221 #include <linux/khugepaged.h>
2322 #include <linux/freezer.h>
2423 #include <linux/pfn_t.h>
@@ -30,39 +29,12 @@ · 5 unmodified lines
3029 #include <linux/hashtable.h>
3130 #include <linux/userfaultfd_k.h>
3231 #include <linux/page_idle.h>
32+#include <linux/shmem_fs.h>
3333
3434 #include <asm/tlb.h>
3535 #include <asm/pgalloc.h>
3636 #include "internal.h"
3737
38−enum scan_result {
39− SCAN_FAIL,
40− SCAN_SUCCEED,
41− SCAN_PMD_NULL,
42− SCAN_EXCEED_NONE_PTE,
43− SCAN_PTE_NON_PRESENT,
44− SCAN_PAGE_RO,
45− SCAN_NO_REFERENCED_PAGE,
46− SCAN_PAGE_NULL,
47− SCAN_SCAN_ABORT,
48− SCAN_PAGE_COUNT,
49− SCAN_PAGE_LRU,
50− SCAN_PAGE_LOCK,
51− SCAN_PAGE_ANON,
52− SCAN_PAGE_COMPOUND,
53− SCAN_ANY_PROCESS,
54− SCAN_VMA_NULL,
55− SCAN_VMA_CHECK,
56− SCAN_ADDRESS_RANGE,
57− SCAN_SWAP_CACHE_PAGE,
58− SCAN_DEL_PAGE_LRU,
59− SCAN_ALLOC_HUGE_PAGE_FAIL,
60− SCAN_CGROUP_CHARGE_FAIL
61−};
62−
63−#define CREATE_TRACE_POINTS
64−#include <trace/events/huge_memory.h>
65−
6638 /*
6739 * By default transparent hugepage support is disabled in order that avoid
6840 * to risk increase the memory footprint of applications without a guaranteed
@@ -82,127 +54,8 @@ unsigned long transparent_hugepage_flags __read_mostly = · 13 unmodified lines
8254 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
8355 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
8456
85−/* default scan 8*512 pte (or vmas) every 30 second */
86−static unsigned int khugepaged_pages_to_scan __read_mostly;
87−static unsigned int khugepaged_pages_collapsed;
88−static unsigned int khugepaged_full_scans;
89−static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
90−/* during fragmentation poll the hugepage allocator once every minute */
91−static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
92−static unsigned long khugepaged_sleep_expire;
93−static struct task_struct *khugepaged_thread __read_mostly;
94−static DEFINE_MUTEX(khugepaged_mutex);
95−static DEFINE_SPINLOCK(khugepaged_mm_lock);
96−static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
97−/*
98− * default collapse hugepages if there is at least one pte mapped like
99− * it would have happened if the vma was large enough during page
100− * fault.
101− */
102−static unsigned int khugepaged_max_ptes_none __read_mostly;
103−
104−static int khugepaged(void *none);
105−static int khugepaged_slab_init(void);
106−static void khugepaged_slab_exit(void);
107−
108−#define MM_SLOTS_HASH_BITS 10
109−static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
110−
111−static struct kmem_cache *mm_slot_cache __read_mostly;
112−
113−/**
114− * struct mm_slot - hash lookup from mm to mm_slot
115− * @hash: hash collision list
116− * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
117− * @mm: the mm that this information is valid for
118− */
119−struct mm_slot {
120− struct hlist_node hash;
121− struct list_head mm_node;
122− struct mm_struct *mm;
123−};
124−
125−/**
126− * struct khugepaged_scan - cursor for scanning
127− * @mm_head: the head of the mm list to scan
128− * @mm_slot: the current mm_slot we are scanning
129− * @address: the next address inside that to be scanned
130− *
131− * There is only the one khugepaged_scan instance of this cursor structure.
132− */
133−struct khugepaged_scan {
134− struct list_head mm_head;
135− struct mm_slot *mm_slot;
136− unsigned long address;
137−};
138−static struct khugepaged_scan khugepaged_scan = {
139− .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
140−};
141−
14257 static struct shrinker deferred_split_shrinker;
14358
144−static void set_recommended_min_free_kbytes(void)
145−{
146− struct zone *zone;
147− int nr_zones = 0;
148− unsigned long recommended_min;
149−
150− for_each_populated_zone(zone)
151− nr_zones++;
152−
153− /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
154− recommended_min = pageblock_nr_pages * nr_zones * 2;
155−
156− /*
157− * Make sure that on average at least two pageblocks are almost free
158− * of another type, one for a migratetype to fall back to and a
159− * second to avoid subsequent fallbacks of other types There are 3
160− * MIGRATE_TYPES we care about.
161− */
162− recommended_min += pageblock_nr_pages * nr_zones *
163− MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
164−
165− /* don't ever allow to reserve more than 5% of the lowmem */
166− recommended_min = min(recommended_min,
167− (unsigned long) nr_free_buffer_pages() / 20);
168− recommended_min <<= (PAGE_SHIFT-10);
169−
170− if (recommended_min > min_free_kbytes) {
171− if (user_min_free_kbytes >= 0)
172− pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
173− min_free_kbytes, recommended_min);
174−
175− min_free_kbytes = recommended_min;
176− }
177− setup_per_zone_wmarks();
178−}
179−
180−static int start_stop_khugepaged(void)
181−{
182− int err = 0;
183− if (khugepaged_enabled()) {
184− if (!khugepaged_thread)
185− khugepaged_thread = kthread_run(khugepaged, NULL,
186− "khugepaged");
187− if (IS_ERR(khugepaged_thread)) {
188− pr_err("khugepaged: kthread_run(khugepaged) failed\n");
189− err = PTR_ERR(khugepaged_thread);
190− khugepaged_thread = NULL;
191− goto fail;
192− }
193−
194− if (!list_empty(&khugepaged_scan.mm_head))
195− wake_up_interruptible(&khugepaged_wait);
196−
197− set_recommended_min_free_kbytes();
198− } else if (khugepaged_thread) {
199− kthread_stop(khugepaged_thread);
200− khugepaged_thread = NULL;
201− }
202−fail:
203− return err;
204−}
205−
20659 static atomic_t huge_zero_refcount;
20760 struct page *huge_zero_page __read_mostly;
20861
@@ -328,12 +181,7 @@ static ssize_t enabled_store(struct kobject *kobj, · 119 unmodified lines
328181 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
329182
330183 if (ret > 0) {
331− int err;
332−
333− mutex_lock(&khugepaged_mutex);
334− err = start_stop_khugepaged();
335− mutex_unlock(&khugepaged_mutex);
336−
184+ int err = start_stop_khugepaged();
337185 if (err)
338186 ret = err;
339187 }
@@ -343,7 +191,7 @@ static ssize_t enabled_store(struct kobject *kobj, · 3 unmodified lines
343191 static struct kobj_attribute enabled_attr =
344192 __ATTR(enabled, 0644, enabled_show, enabled_store);
345193
346−static ssize_t single_flag_show(struct kobject *kobj,
194+ssize_t single_hugepage_flag_show(struct kobject *kobj,
347195 struct kobj_attribute *attr, char *buf,
348196 enum transparent_hugepage_flag flag)
349197 {
@@ -351,7 +199,7 @@ static ssize_t single_flag_show(struct kobject *kobj, · 1 unmodified line
351199 !!test_bit(flag, &transparent_hugepage_flags));
352200 }
353201
354−static ssize_t single_flag_store(struct kobject *kobj,
202+ssize_t single_hugepage_flag_store(struct kobject *kobj,
355203 struct kobj_attribute *attr,
356204 const char *buf, size_t count,
357205 enum transparent_hugepage_flag flag)
@@ -406,13 +254,13 @@ static struct kobj_attribute defrag_attr = · 48 unmodified lines
406254 static ssize_t use_zero_page_show(struct kobject *kobj,
407255 struct kobj_attribute *attr, char *buf)
408256 {
409− return single_flag_show(kobj, attr, buf,
257+ return single_hugepage_flag_show(kobj, attr, buf,
410258 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
411259 }
412260 static ssize_t use_zero_page_store(struct kobject *kobj,
413261 struct kobj_attribute *attr, const char *buf, size_t count)
414262 {
415− return single_flag_store(kobj, attr, buf, count,
263+ return single_hugepage_flag_store(kobj, attr, buf, count,
416264 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
417265 }
418266 static struct kobj_attribute use_zero_page_attr =
@@ -421,14 +269,14 @@ static struct kobj_attribute use_zero_page_attr = · 2 unmodified lines
421269 static ssize_t debug_cow_show(struct kobject *kobj,
422270 struct kobj_attribute *attr, char *buf)
423271 {
424− return single_flag_show(kobj, attr, buf,
272+ return single_hugepage_flag_show(kobj, attr, buf,
425273 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
426274 }
427275 static ssize_t debug_cow_store(struct kobject *kobj,
428276 struct kobj_attribute *attr,
429277 const char *buf, size_t count)
430278 {
431− return single_flag_store(kobj, attr, buf, count,
279+ return single_hugepage_flag_store(kobj, attr, buf, count,
432280 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
433281 }
434282 static struct kobj_attribute debug_cow_attr =
@@ -439,6 +287,9 @@ static struct attribute *hugepage_attr[] = { · 4 unmodified lines
439287 &enabled_attr.attr,
440288 &defrag_attr.attr,
441289 &use_zero_page_attr.attr,
290+#if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
291+ &shmem_enabled_attr.attr,
292+#endif
442293 #ifdef CONFIG_DEBUG_VM
443294 &debug_cow_attr.attr,
444295 #endif
@@ -449,171 +300,6 @@ static struct attribute_group hugepage_attr_group = { · 4 unmodified lines
449300 .attrs = hugepage_attr,
450301 };
451302
452−static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
453− struct kobj_attribute *attr,
454− char *buf)
455−{
456− return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
457−}
458−
459−static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
460− struct kobj_attribute *attr,
461− const char *buf, size_t count)
462−{
463− unsigned long msecs;
464− int err;
465−
466− err = kstrtoul(buf, 10, &msecs);
467− if (err || msecs > UINT_MAX)
468− return -EINVAL;
469−
470− khugepaged_scan_sleep_millisecs = msecs;
471− khugepaged_sleep_expire = 0;
472− wake_up_interruptible(&khugepaged_wait);
473−
474− return count;
475−}
476−static struct kobj_attribute scan_sleep_millisecs_attr =
477− __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
478− scan_sleep_millisecs_store);
479−
480−static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
481− struct kobj_attribute *attr,
482− char *buf)
483−{
484− return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
485−}
486−
487−static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
488− struct kobj_attribute *attr,
489− const char *buf, size_t count)
490−{
491− unsigned long msecs;
492− int err;
493−
494− err = kstrtoul(buf, 10, &msecs);
495− if (err || msecs > UINT_MAX)
496− return -EINVAL;
497−
498− khugepaged_alloc_sleep_millisecs = msecs;
499− khugepaged_sleep_expire = 0;
500− wake_up_interruptible(&khugepaged_wait);
501−
502− return count;
503−}
504−static struct kobj_attribute alloc_sleep_millisecs_attr =
505− __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
506− alloc_sleep_millisecs_store);
507−
508−static ssize_t pages_to_scan_show(struct kobject *kobj,
509− struct kobj_attribute *attr,
510− char *buf)
511−{
512− return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
513−}
514−static ssize_t pages_to_scan_store(struct kobject *kobj,
515− struct kobj_attribute *attr,
516− const char *buf, size_t count)
517−{
518− int err;
519− unsigned long pages;
520−
521− err = kstrtoul(buf, 10, &pages);
522− if (err || !pages || pages > UINT_MAX)
523− return -EINVAL;
524−
525− khugepaged_pages_to_scan = pages;
526−
527− return count;
528−}
529−static struct kobj_attribute pages_to_scan_attr =
530− __ATTR(pages_to_scan, 0644, pages_to_scan_show,
531− pages_to_scan_store);
532−
533−static ssize_t pages_collapsed_show(struct kobject *kobj,
534− struct kobj_attribute *attr,
535− char *buf)
536−{
537− return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
538−}
539−static struct kobj_attribute pages_collapsed_attr =
540− __ATTR_RO(pages_collapsed);
541−
542−static ssize_t full_scans_show(struct kobject *kobj,
543− struct kobj_attribute *attr,
544− char *buf)
545−{
546− return sprintf(buf, "%u\n", khugepaged_full_scans);
547−}
548−static struct kobj_attribute full_scans_attr =
549− __ATTR_RO(full_scans);
550−
551−static ssize_t khugepaged_defrag_show(struct kobject *kobj,
552− struct kobj_attribute *attr, char *buf)
553−{
554− return single_flag_show(kobj, attr, buf,
555− TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
556−}
557−static ssize_t khugepaged_defrag_store(struct kobject *kobj,
558− struct kobj_attribute *attr,
559− const char *buf, size_t count)
560−{
561− return single_flag_store(kobj, attr, buf, count,
562− TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
563−}
564−static struct kobj_attribute khugepaged_defrag_attr =
565− __ATTR(defrag, 0644, khugepaged_defrag_show,
566− khugepaged_defrag_store);
567−
568−/*
569− * max_ptes_none controls if khugepaged should collapse hugepages over
570− * any unmapped ptes in turn potentially increasing the memory
571− * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
572− * reduce the available free memory in the system as it
573− * runs. Increasing max_ptes_none will instead potentially reduce the
574− * free memory in the system during the khugepaged scan.
575− */
576−static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
577− struct kobj_attribute *attr,
578− char *buf)
579−{
580− return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
581−}
582−static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
583− struct kobj_attribute *attr,
584− const char *buf, size_t count)
585−{
586− int err;
587− unsigned long max_ptes_none;
588−
589− err = kstrtoul(buf, 10, &max_ptes_none);
590− if (err || max_ptes_none > HPAGE_PMD_NR-1)
591− return -EINVAL;
592−
593− khugepaged_max_ptes_none = max_ptes_none;
594−
595− return count;
596−}
597−static struct kobj_attribute khugepaged_max_ptes_none_attr =
598− __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
599− khugepaged_max_ptes_none_store);
600−
601−static struct attribute *khugepaged_attr[] = {
602− &khugepaged_defrag_attr.attr,
603− &khugepaged_max_ptes_none_attr.attr,
604− &pages_to_scan_attr.attr,
605− &pages_collapsed_attr.attr,
606− &full_scans_attr.attr,
607− &scan_sleep_millisecs_attr.attr,
608− &alloc_sleep_millisecs_attr.attr,
609− NULL,
610−};
611−
612−static struct attribute_group khugepaged_attr_group = {
613− .attrs = khugepaged_attr,
614− .name = "khugepaged",
615−};
616−
617303 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
618304 {
619305 int err;
@@ -672,8 +358,6 @@ static int __init hugepage_init(void) · 52 unmodified lines
672358 return -EINVAL;
673359 }
674360
675− khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
676− khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
677361 /*
678362 * hugepages can't be allocated by the buddy allocator
679363 */
@@ -688,7 +372,7 @@ static int __init hugepage_init(void) · 8 unmodified lines
688372 if (err)
689373 goto err_sysfs;
690374
691− err = khugepaged_slab_init();
375+ err = khugepaged_init();
692376 if (err)
693377 goto err_slab;
694378
@@ -719,7 +403,7 @@ err_khugepaged: · 24 unmodified lines
719403 err_split_shrinker:
720404 unregister_shrinker(&huge_zero_page_shrinker);
721405 err_hzp_shrinker:
722− khugepaged_slab_exit();
406+ khugepaged_destroy();
723407 err_slab:
724408 hugepage_exit_sysfs(hugepage_kobj);
725409 err_sysfs:
@@ -765,11 +449,6 @@ pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) · 39 unmodified lines
765449 return pmd;
766450 }
767451
768−static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
769−{
770− return pmd_mkhuge(mk_pmd(page, prot));
771−}
772−
773452 static inline struct list_head *page_deferred_list(struct page *page)
774453 {
775454 /*
@@ -790,26 +469,23 @@ void prep_transhuge_page(struct page *page) · 14 unmodified lines
790469 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
791470 }
792471
793−static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
794− struct vm_area_struct *vma,
795− unsigned long address, pmd_t *pmd,
796− struct page *page, gfp_t gfp,
797− unsigned int flags)
472+static int __do_huge_pmd_anonymous_page(struct fault_env *fe, struct page *page,
473+ gfp_t gfp)
798474 {
475+ struct vm_area_struct *vma = fe->vma;
799476 struct mem_cgroup *memcg;
800477 pgtable_t pgtable;
801− spinlock_t *ptl;
802− unsigned long haddr = address & HPAGE_PMD_MASK;
478+ unsigned long haddr = fe->address & HPAGE_PMD_MASK;
803479
804480 VM_BUG_ON_PAGE(!PageCompound(page), page);
805481
806− if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
482+ if (mem_cgroup_try_charge(page, vma->vm_mm, gfp, &memcg, true)) {
807483 put_page(page);
808484 count_vm_event(THP_FAULT_FALLBACK);
809485 return VM_FAULT_FALLBACK;
810486 }
811487
812− pgtable = pte_alloc_one(mm, haddr);
488+ pgtable = pte_alloc_one(vma->vm_mm, haddr);
813489 if (unlikely(!pgtable)) {
814490 mem_cgroup_cancel_charge(page, memcg, true);
815491 put_page(page);
@@ -824,12 +500,12 @@ static int __do_huge_pmd_anonymous_page(struct mm_struct *mm, · 8 unmodified lines
824500 */
825501 __SetPageUptodate(page);
826502
827− ptl = pmd_lock(mm, pmd);
828− if (unlikely(!pmd_none(*pmd))) {
829− spin_unlock(ptl);
503+ fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
504+ if (unlikely(!pmd_none(*fe->pmd))) {
505+ spin_unlock(fe->ptl);
830506 mem_cgroup_cancel_charge(page, memcg, true);
831507 put_page(page);
832− pte_free(mm, pgtable);
508+ pte_free(vma->vm_mm, pgtable);
833509 } else {
834510 pmd_t entry;
835511
@@ -837,12 +513,11 @@ static int __do_huge_pmd_anonymous_page(struct mm_struct *mm, · 1 unmodified line
837513 if (userfaultfd_missing(vma)) {
838514 int ret;
839515
840− spin_unlock(ptl);
516+ spin_unlock(fe->ptl);
841517 mem_cgroup_cancel_charge(page, memcg, true);
842518 put_page(page);
843− pte_free(mm, pgtable);
844− ret = handle_userfault(vma, address, flags,
845− VM_UFFD_MISSING);
519+ pte_free(vma->vm_mm, pgtable);
520+ ret = handle_userfault(fe, VM_UFFD_MISSING);
846521 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
847522 return ret;
848523 }
@@ -852,11 +527,11 @@ static int __do_huge_pmd_anonymous_page(struct mm_struct *mm, · 3 unmodified lines
852527 page_add_new_anon_rmap(page, vma, haddr, true);
853528 mem_cgroup_commit_charge(page, memcg, false, true);
854529 lru_cache_add_active_or_unevictable(page, vma);
855− pgtable_trans_huge_deposit(mm, pmd, pgtable);
856− set_pmd_at(mm, haddr, pmd, entry);
857− add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
858− atomic_long_inc(&mm->nr_ptes);
859− spin_unlock(ptl);
530+ pgtable_trans_huge_deposit(vma->vm_mm, fe->pmd, pgtable);
531+ set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
532+ add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
533+ atomic_long_inc(&vma->vm_mm->nr_ptes);
534+ spin_unlock(fe->ptl);
860535 count_vm_event(THP_FAULT_ALLOC);
861536 }
862537
@@ -883,12 +558,6 @@ static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · 20 unmodified lines
883558 return GFP_TRANSHUGE | reclaim_flags;
884559 }
885560
886−/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
887−static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
888−{
889− return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
890−}
891−
892561 /* Caller must hold page table lock. */
893562 static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
894563 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
@@ -906,13 +575,12 @@ static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm, · 11 unmodified lines
906575 return true;
907576 }
908577
909−int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
910− unsigned long address, pmd_t *pmd,
911− unsigned int flags)
578+int do_huge_pmd_anonymous_page(struct fault_env *fe)
912579 {
580+ struct vm_area_struct *vma = fe->vma;
913581 gfp_t gfp;
914582 struct page *page;
915− unsigned long haddr = address & HPAGE_PMD_MASK;
583+ unsigned long haddr = fe->address & HPAGE_PMD_MASK;
916584
917585 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
918586 return VM_FAULT_FALLBACK;
@@ -920,42 +588,40 @@ int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
920588 return VM_FAULT_OOM;
921589 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
922590 return VM_FAULT_OOM;
923− if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
591+ if (!(fe->flags & FAULT_FLAG_WRITE) &&
592+ !mm_forbids_zeropage(vma->vm_mm) &&
924593 transparent_hugepage_use_zero_page()) {
925− spinlock_t *ptl;
926594 pgtable_t pgtable;
927595 struct page *zero_page;
928596 bool set;
929597 int ret;
930− pgtable = pte_alloc_one(mm, haddr);
598+ pgtable = pte_alloc_one(vma->vm_mm, haddr);
931599 if (unlikely(!pgtable))
932600 return VM_FAULT_OOM;
933601 zero_page = get_huge_zero_page();
934602 if (unlikely(!zero_page)) {
935− pte_free(mm, pgtable);
603+ pte_free(vma->vm_mm, pgtable);
936604 count_vm_event(THP_FAULT_FALLBACK);
937605 return VM_FAULT_FALLBACK;
938606 }
939− ptl = pmd_lock(mm, pmd);
607+ fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
940608 ret = 0;
941609 set = false;
942− if (pmd_none(*pmd)) {
610+ if (pmd_none(*fe->pmd)) {
943611 if (userfaultfd_missing(vma)) {
944− spin_unlock(ptl);
945− ret = handle_userfault(vma, address, flags,
946− VM_UFFD_MISSING);
612+ spin_unlock(fe->ptl);
613+ ret = handle_userfault(fe, VM_UFFD_MISSING);
947614 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
948615 } else {
949− set_huge_zero_page(pgtable, mm, vma,
950− haddr, pmd,
951− zero_page);
952− spin_unlock(ptl);
616+ set_huge_zero_page(pgtable, vma->vm_mm, vma,
617+ haddr, fe->pmd, zero_page);
618+ spin_unlock(fe->ptl);
953619 set = true;
954620 }
955621 } else
956− spin_unlock(ptl);
622+ spin_unlock(fe->ptl);
957623 if (!set) {
958− pte_free(mm, pgtable);
624+ pte_free(vma->vm_mm, pgtable);
959625 put_huge_zero_page();
960626 }
961627 return ret;
@@ -967,8 +633,7 @@ int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, · 5 unmodified lines
967633 return VM_FAULT_FALLBACK;
968634 }
969635 prep_transhuge_page(page);
970− return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
971− flags);
636+ return __do_huge_pmd_anonymous_page(fe, page, gfp);
972637 }
973638
974639 static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
@@ -1080,14 +745,15 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, · 105 unmodified lines
1080745 struct page *src_page;
1081746 pmd_t pmd;
1082747 pgtable_t pgtable = NULL;
1083− int ret;
748+ int ret = -ENOMEM;
1084749
1085− if (!vma_is_dax(vma)) {
1086− ret = -ENOMEM;
1087− pgtable = pte_alloc_one(dst_mm, addr);
1088− if (unlikely(!pgtable))
1089− goto out;
1090− }
750+ /* Skip if can be re-fill on fault */
751+ if (!vma_is_anonymous(vma))
752+ return 0;
753+
754+ pgtable = pte_alloc_one(dst_mm, addr);
755+ if (unlikely(!pgtable))
756+ goto out;
1091757
1092758 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1093759 src_ptl = pmd_lockptr(src_mm, src_pmd);
@@ -1095,7 +761,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, · 1 unmodified line
1095761
1096762 ret = -EAGAIN;
1097763 pmd = *src_pmd;
1098− if (unlikely(!pmd_trans_huge(pmd) && !pmd_devmap(pmd))) {
764+ if (unlikely(!pmd_trans_huge(pmd))) {
1099765 pte_free(dst_mm, pgtable);
1100766 goto out_unlock;
1101767 }
@@ -1118,16 +784,13 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, · 16 unmodified lines
1118784 goto out_unlock;
1119785 }
1120786
1121− if (!vma_is_dax(vma)) {
1122− /* thp accounting separate from pmd_devmap accounting */
1123− src_page = pmd_page(pmd);
1124− VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1125− get_page(src_page);
1126− page_dup_rmap(src_page, true);
1127− add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1128− atomic_long_inc(&dst_mm->nr_ptes);
1129− pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1130− }
787+ src_page = pmd_page(pmd);
788+ VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
789+ get_page(src_page);
790+ page_dup_rmap(src_page, true);
791+ add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
792+ atomic_long_inc(&dst_mm->nr_ptes);
793+ pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1131794
1132795 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1133796 pmd = pmd_mkold(pmd_wrprotect(pmd));
@@ -1141,38 +804,31 @@ out: · 7 unmodified lines
1141804 return ret;
1142805 }
1143806
1144−void huge_pmd_set_accessed(struct mm_struct *mm,
1145− struct vm_area_struct *vma,
1146− unsigned long address,
1147− pmd_t *pmd, pmd_t orig_pmd,
1148− int dirty)
807+void huge_pmd_set_accessed(struct fault_env *fe, pmd_t orig_pmd)
1149808 {
1150− spinlock_t *ptl;
1151809 pmd_t entry;
1152810 unsigned long haddr;
1153811
1154− ptl = pmd_lock(mm, pmd);
1155− if (unlikely(!pmd_same(*pmd, orig_pmd)))
812+ fe->ptl = pmd_lock(fe->vma->vm_mm, fe->pmd);
813+ if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
1156814 goto unlock;
1157815
1158816 entry = pmd_mkyoung(orig_pmd);
1159− haddr = address & HPAGE_PMD_MASK;
1160− if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1161− update_mmu_cache_pmd(vma, address, pmd);
817+ haddr = fe->address & HPAGE_PMD_MASK;
818+ if (pmdp_set_access_flags(fe->vma, haddr, fe->pmd, entry,
819+ fe->flags & FAULT_FLAG_WRITE))
820+ update_mmu_cache_pmd(fe->vma, fe->address, fe->pmd);
1162821
1163822 unlock:
1164− spin_unlock(ptl);
823+ spin_unlock(fe->ptl);
1165824 }
1166825
1167−static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1168− struct vm_area_struct *vma,
1169− unsigned long address,
1170− pmd_t *pmd, pmd_t orig_pmd,
1171− struct page *page,
1172− unsigned long haddr)
826+static int do_huge_pmd_wp_page_fallback(struct fault_env *fe, pmd_t orig_pmd,
827+ struct page *page)
1173828 {
829+ struct vm_area_struct *vma = fe->vma;
830+ unsigned long haddr = fe->address & HPAGE_PMD_MASK;
1174831 struct mem_cgroup *memcg;
1175− spinlock_t *ptl;
1176832 pgtable_t pgtable;
1177833 pmd_t _pmd;
1178834 int ret = 0, i;
@@ -1189,11 +845,11 @@ static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm, · 10 unmodified lines
1189845
1190846 for (i = 0; i < HPAGE_PMD_NR; i++) {
1191847 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1192− __GFP_OTHER_NODE,
1193− vma, address, page_to_nid(page));
848+ __GFP_OTHER_NODE, vma,
849+ fe->address, page_to_nid(page));
1194850 if (unlikely(!pages[i] ||
1195− mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
1196− &memcg, false))) {
851+ mem_cgroup_try_charge(pages[i], vma->vm_mm,
852+ GFP_KERNEL, &memcg, false))) {
1197853 if (pages[i])
1198854 put_page(pages[i]);
1199855 while (--i >= 0) {
@@ -1219,41 +875,41 @@ static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm, · 19 unmodified lines
1219875
1220876 mmun_start = haddr;
1221877 mmun_end = haddr + HPAGE_PMD_SIZE;
1222− mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
878+ mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
1223879
1224− ptl = pmd_lock(mm, pmd);
1225− if (unlikely(!pmd_same(*pmd, orig_pmd)))
880+ fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
881+ if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
1226882 goto out_free_pages;
1227883 VM_BUG_ON_PAGE(!PageHead(page), page);
1228884
1229− pmdp_huge_clear_flush_notify(vma, haddr, pmd);
885+ pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
1230886 /* leave pmd empty until pte is filled */
1231887
1232− pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1233− pmd_populate(mm, &_pmd, pgtable);
888+ pgtable = pgtable_trans_huge_withdraw(vma->vm_mm, fe->pmd);
889+ pmd_populate(vma->vm_mm, &_pmd, pgtable);
1234890
1235891 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1236− pte_t *pte, entry;
892+ pte_t entry;
1237893 entry = mk_pte(pages[i], vma->vm_page_prot);
1238894 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1239895 memcg = (void *)page_private(pages[i]);
1240896 set_page_private(pages[i], 0);
1241− page_add_new_anon_rmap(pages[i], vma, haddr, false);
897+ page_add_new_anon_rmap(pages[i], fe->vma, haddr, false);
1242898 mem_cgroup_commit_charge(pages[i], memcg, false, false);
1243899 lru_cache_add_active_or_unevictable(pages[i], vma);
1244− pte = pte_offset_map(&_pmd, haddr);
1245− VM_BUG_ON(!pte_none(*pte));
1246− set_pte_at(mm, haddr, pte, entry);
1247− pte_unmap(pte);
900+ fe->pte = pte_offset_map(&_pmd, haddr);
901+ VM_BUG_ON(!pte_none(*fe->pte));
902+ set_pte_at(vma->vm_mm, haddr, fe->pte, entry);
903+ pte_unmap(fe->pte);
1248904 }
1249905 kfree(pages);
1250906
1251907 smp_wmb(); /* make pte visible before pmd */
1252− pmd_populate(mm, pmd, pgtable);
908+ pmd_populate(vma->vm_mm, fe->pmd, pgtable);
1253909 page_remove_rmap(page, true);
1254− spin_unlock(ptl);
910+ spin_unlock(fe->ptl);
1255911
1256− mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
912+ mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
1257913
1258914 ret |= VM_FAULT_WRITE;
1259915 put_page(page);
@@ -1262,8 +918,8 @@ out: · 2 unmodified lines
1262918 return ret;
1263919
1264920 out_free_pages:
1265− spin_unlock(ptl);
1266− mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
921+ spin_unlock(fe->ptl);
922+ mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
1267923 for (i = 0; i < HPAGE_PMD_NR; i++) {
1268924 memcg = (void *)page_private(pages[i]);
1269925 set_page_private(pages[i], 0);
@@ -1274,25 +930,23 @@ out_free_pages: · 4 unmodified lines
1274930 goto out;
1275931 }
1276932
1277−int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1278− unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
933+int do_huge_pmd_wp_page(struct fault_env *fe, pmd_t orig_pmd)
1279934 {
1280− spinlock_t *ptl;
1281− int ret = 0;
935+ struct vm_area_struct *vma = fe->vma;
1282936 struct page *page = NULL, *new_page;
1283937 struct mem_cgroup *memcg;
1284− unsigned long haddr;
938+ unsigned long haddr = fe->address & HPAGE_PMD_MASK;
1285939 unsigned long mmun_start; /* For mmu_notifiers */
1286940 unsigned long mmun_end; /* For mmu_notifiers */
1287941 gfp_t huge_gfp; /* for allocation and charge */
942+ int ret = 0;
1288943
1289− ptl = pmd_lockptr(mm, pmd);
944+ fe->ptl = pmd_lockptr(vma->vm_mm, fe->pmd);
1290945 VM_BUG_ON_VMA(!vma->anon_vma, vma);
1291− haddr = address & HPAGE_PMD_MASK;
1292946 if (is_huge_zero_pmd(orig_pmd))
1293947 goto alloc;
1294− spin_lock(ptl);
1295− if (unlikely(!pmd_same(*pmd, orig_pmd)))
948+ spin_lock(fe->ptl);
949+ if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
1296950 goto out_unlock;
1297951
1298952 page = pmd_page(orig_pmd);
@@ -1305,13 +959,13 @@ int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, · 6 unmodified lines
1305959 pmd_t entry;
1306960 entry = pmd_mkyoung(orig_pmd);
1307961 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1308− if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
1309− update_mmu_cache_pmd(vma, address, pmd);
962+ if (pmdp_set_access_flags(vma, haddr, fe->pmd, entry, 1))
963+ update_mmu_cache_pmd(vma, fe->address, fe->pmd);
1310964 ret |= VM_FAULT_WRITE;
1311965 goto out_unlock;
1312966 }
1313967 get_page(page);
1314− spin_unlock(ptl);
968+ spin_unlock(fe->ptl);
1315969 alloc:
1316970 if (transparent_hugepage_enabled(vma) &&
1317971 !transparent_hugepage_debug_cow()) {
@@ -1324,13 +978,12 @@ alloc: · 6 unmodified lines
1324978 prep_transhuge_page(new_page);
1325979 } else {
1326980 if (!page) {
1327− split_huge_pmd(vma, pmd, address);
981+ split_huge_pmd(vma, fe->pmd, fe->address);
1328982 ret |= VM_FAULT_FALLBACK;
1329983 } else {
1330− ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1331− pmd, orig_pmd, page, haddr);
984+ ret = do_huge_pmd_wp_page_fallback(fe, orig_pmd, page);
1332985 if (ret & VM_FAULT_OOM) {
1333− split_huge_pmd(vma, pmd, address);
986+ split_huge_pmd(vma, fe->pmd, fe->address);
1334987 ret |= VM_FAULT_FALLBACK;
1335988 }
1336989 put_page(page);
@@ -1339,14 +992,12 @@ alloc: · 2 unmodified lines
1339992 goto out;
1340993 }
1341994
1342− if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1343− true))) {
995+ if (unlikely(mem_cgroup_try_charge(new_page, vma->vm_mm,
996+ huge_gfp, &memcg, true))) {
1344997 put_page(new_page);
1345− if (page) {
1346− split_huge_pmd(vma, pmd, address);
998+ split_huge_pmd(vma, fe->pmd, fe->address);
999+ if (page)
13471000 put_page(page);
1348− } else
1349− split_huge_pmd(vma, pmd, address);
13501001 ret |= VM_FAULT_FALLBACK;
13511002 count_vm_event(THP_FAULT_FALLBACK);
13521003 goto out;
@@ -1362,13 +1013,13 @@ alloc: · 9 unmodified lines
13621013
13631014 mmun_start = haddr;
13641015 mmun_end = haddr + HPAGE_PMD_SIZE;
1365− mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1016+ mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
13661017
1367− spin_lock(ptl);
1018+ spin_lock(fe->ptl);
13681019 if (page)
13691020 put_page(page);
1370− if (unlikely(!pmd_same(*pmd, orig_pmd))) {
1371− spin_unlock(ptl);
1021+ if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) {
1022+ spin_unlock(fe->ptl);
13721023 mem_cgroup_cancel_charge(new_page, memcg, true);
13731024 put_page(new_page);
13741025 goto out_mn;
@@ -1376,14 +1027,14 @@ alloc: · 1 unmodified line
13761027 pmd_t entry;
13771028 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
13781029 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1379− pmdp_huge_clear_flush_notify(vma, haddr, pmd);
1030+ pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
13801031 page_add_new_anon_rmap(new_page, vma, haddr, true);
13811032 mem_cgroup_commit_charge(new_page, memcg, false, true);
13821033 lru_cache_add_active_or_unevictable(new_page, vma);
1383− set_pmd_at(mm, haddr, pmd, entry);
1384− update_mmu_cache_pmd(vma, address, pmd);
1034+ set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
1035+ update_mmu_cache_pmd(vma, fe->address, fe->pmd);
13851036 if (!page) {
1386− add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
1037+ add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
13871038 put_huge_zero_page();
13881039 } else {
13891040 VM_BUG_ON_PAGE(!PageHead(page), page);
@@ -1392,13 +1043,13 @@ alloc: · 2 unmodified lines
13921043 }
13931044 ret |= VM_FAULT_WRITE;
13941045 }
1395− spin_unlock(ptl);
1046+ spin_unlock(fe->ptl);
13961047 out_mn:
1397− mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1048+ mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
13981049 out:
13991050 return ret;
14001051 out_unlock:
1401− spin_unlock(ptl);
1052+ spin_unlock(fe->ptl);
14021053 return ret;
14031054 }
14041055
@@ -1432,6 +1083,8 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, · 27 unmodified lines
14321083 * We don't mlock() pte-mapped THPs. This way we can avoid
14331084 * leaking mlocked pages into non-VM_LOCKED VMAs.
14341085 *
1086+ * For anon THP:
1087+ *
14351088 * In most cases the pmd is the only mapping of the page as we
14361089 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
14371090 * writable private mappings in populate_vma_page_range().
@@ -1439,15 +1092,26 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, · 1 unmodified line
14391092 * The only scenario when we have the page shared here is if we
14401093 * mlocking read-only mapping shared over fork(). We skip
14411094 * mlocking such pages.
1095+ *
1096+ * For file THP:
1097+ *
1098+ * We can expect PageDoubleMap() to be stable under page lock:
1099+ * for file pages we set it in page_add_file_rmap(), which
1100+ * requires page to be locked.
14421101 */
1443− if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1444− page->mapping && trylock_page(page)) {
1445− lru_add_drain();
1446− if (page->mapping)
1447− mlock_vma_page(page);
1448− unlock_page(page);
1449− }
1102+
1103+ if (PageAnon(page) && compound_mapcount(page) != 1)
1104+ goto skip_mlock;
1105+ if (PageDoubleMap(page) || !page->mapping)
1106+ goto skip_mlock;
1107+ if (!trylock_page(page))
1108+ goto skip_mlock;
1109+ lru_add_drain();
1110+ if (page->mapping && !PageDoubleMap(page))
1111+ mlock_vma_page(page);
1112+ unlock_page(page);
14501113 }
1114+skip_mlock:
14511115 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
14521116 VM_BUG_ON_PAGE(!PageCompound(page), page);
14531117 if (flags & FOLL_GET)
@@ -1458,13 +1122,12 @@ out: · 4 unmodified lines
14581122 }
14591123
14601124 /* NUMA hinting page fault entry point for trans huge pmds */
1461−int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1462− unsigned long addr, pmd_t pmd, pmd_t *pmdp)
1125+int do_huge_pmd_numa_page(struct fault_env *fe, pmd_t pmd)
14631126 {
1464− spinlock_t *ptl;
1127+ struct vm_area_struct *vma = fe->vma;
14651128 struct anon_vma *anon_vma = NULL;
14661129 struct page *page;
1467− unsigned long haddr = addr & HPAGE_PMD_MASK;
1130+ unsigned long haddr = fe->address & HPAGE_PMD_MASK;
14681131 int page_nid = -1, this_nid = numa_node_id();
14691132 int target_nid, last_cpupid = -1;
14701133 bool page_locked;
@@ -1475,8 +1138,8 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 4 unmodified lines
14751138 /* A PROT_NONE fault should not end up here */
14761139 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
14771140
1478− ptl = pmd_lock(mm, pmdp);
1479− if (unlikely(!pmd_same(pmd, *pmdp)))
1141+ fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
1142+ if (unlikely(!pmd_same(pmd, *fe->pmd)))
14801143 goto out_unlock;
14811144
14821145 /*
@@ -1484,9 +1147,9 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
14841147 * without disrupting NUMA hinting information. Do not relock and
14851148 * check_same as the page may no longer be mapped.
14861149 */
1487− if (unlikely(pmd_trans_migrating(*pmdp))) {
1488− page = pmd_page(*pmdp);
1489− spin_unlock(ptl);
1150+ if (unlikely(pmd_trans_migrating(*fe->pmd))) {
1151+ page = pmd_page(*fe->pmd);
1152+ spin_unlock(fe->ptl);
14901153 wait_on_page_locked(page);
14911154 goto out;
14921155 }
@@ -1519,7 +1182,7 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 26 unmodified lines
15191182
15201183 /* Migration could have started since the pmd_trans_migrating check */
15211184 if (!page_locked) {
1522− spin_unlock(ptl);
1185+ spin_unlock(fe->ptl);
15231186 wait_on_page_locked(page);
15241187 page_nid = -1;
15251188 goto out;
@@ -1530,12 +1193,12 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 4 unmodified lines
15301193 * to serialises splits
15311194 */
15321195 get_page(page);
1533− spin_unlock(ptl);
1196+ spin_unlock(fe->ptl);
15341197 anon_vma = page_lock_anon_vma_read(page);
15351198
15361199 /* Confirm the PMD did not change while page_table_lock was released */
1537− spin_lock(ptl);
1538− if (unlikely(!pmd_same(pmd, *pmdp))) {
1200+ spin_lock(fe->ptl);
1201+ if (unlikely(!pmd_same(pmd, *fe->pmd))) {
15391202 unlock_page(page);
15401203 put_page(page);
15411204 page_nid = -1;
@@ -1553,9 +1216,9 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 11 unmodified lines
15531216 * Migrate the THP to the requested node, returns with page unlocked
15541217 * and access rights restored.
15551218 */
1556− spin_unlock(ptl);
1557− migrated = migrate_misplaced_transhuge_page(mm, vma,
1558− pmdp, pmd, addr, page, target_nid);
1219+ spin_unlock(fe->ptl);
1220+ migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma,
1221+ fe->pmd, pmd, fe->address, page, target_nid);
15591222 if (migrated) {
15601223 flags |= TNF_MIGRATED;
15611224 page_nid = target_nid;
@@ -1570,18 +1233,18 @@ clear_pmdnuma: · 8 unmodified lines
15701233 pmd = pmd_mkyoung(pmd);
15711234 if (was_writable)
15721235 pmd = pmd_mkwrite(pmd);
1573− set_pmd_at(mm, haddr, pmdp, pmd);
1574− update_mmu_cache_pmd(vma, addr, pmdp);
1236+ set_pmd_at(vma->vm_mm, haddr, fe->pmd, pmd);
1237+ update_mmu_cache_pmd(vma, fe->address, fe->pmd);
15751238 unlock_page(page);
15761239 out_unlock:
1577− spin_unlock(ptl);
1240+ spin_unlock(fe->ptl);
15781241
15791242 out:
15801243 if (anon_vma)
15811244 page_unlock_anon_vma_read(anon_vma);
15821245
15831246 if (page_nid != -1)
1584− task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
1247+ task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, fe->flags);
15851248
15861249 return 0;
15871250 }
@@ -1684,12 +1347,18 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, · 96 unmodified lines
16841347 struct page *page = pmd_page(orig_pmd);
16851348 page_remove_rmap(page, true);
16861349 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1687− add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
16881350 VM_BUG_ON_PAGE(!PageHead(page), page);
1689− pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1690− atomic_long_dec(&tlb->mm->nr_ptes);
1351+ if (PageAnon(page)) {
1352+ pgtable_t pgtable;
1353+ pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
1354+ pte_free(tlb->mm, pgtable);
1355+ atomic_long_dec(&tlb->mm->nr_ptes);
1356+ add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1357+ } else {
1358+ add_mm_counter(tlb->mm, MM_FILEPAGES, -HPAGE_PMD_NR);
1359+ }
16911360 spin_unlock(ptl);
1692− tlb_remove_page(tlb, page);
1361+ tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
16931362 }
16941363 return 1;
16951364 }
@@ -1779,7 +1448,8 @@ int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, · 83 unmodified lines
17791448 entry = pmd_mkwrite(entry);
17801449 ret = HPAGE_PMD_NR;
17811450 set_pmd_at(mm, addr, pmd, entry);
1782− BUG_ON(!preserve_write && pmd_write(entry));
1451+ BUG_ON(vma_is_anonymous(vma) && !preserve_write &&
1452+ pmd_write(entry));
17831453 }
17841454 spin_unlock(ptl);
17851455 }
@@ -1788,10 +1458,10 @@ int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, · 2 unmodified lines
17881458 }
17891459
17901460 /*
1791− * Returns true if a given pmd maps a thp, false otherwise.
1461+ * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
17921462 *
1793− * Note that if it returns true, this routine returns without unlocking page
1794− * table lock. So callers must unlock it.
1463+ * Note that if it returns page table lock pointer, this routine returns without
1464+ * unlocking page table lock. So callers must unlock it.
17951465 */
17961466 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
17971467 {
@@ -1803,1105 +1473,79 @@ spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma) · 5 unmodified lines
18031473 return NULL;
18041474 }
18051475
1806−#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
1807−
1808−int hugepage_madvise(struct vm_area_struct *vma,
1809− unsigned long *vm_flags, int advice)
1476+static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
1477+ unsigned long haddr, pmd_t *pmd)
18101478 {
1811− switch (advice) {
1812− case MADV_HUGEPAGE:
1813−#ifdef CONFIG_S390
1814− /*
1815− * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1816− * can't handle this properly after s390_enable_sie, so we simply
1817− * ignore the madvise to prevent qemu from causing a SIGSEGV.
1818− */
1819− if (mm_has_pgste(vma->vm_mm))
1820− return 0;
1821−#endif
1822− /*
1823− * Be somewhat over-protective like KSM for now!
1824− */
1825− if (*vm_flags & VM_NO_THP)
1826− return -EINVAL;
1827− *vm_flags &= ~VM_NOHUGEPAGE;
1828− *vm_flags |= VM_HUGEPAGE;
1829− /*
1830− * If the vma become good for khugepaged to scan,
1831− * register it here without waiting a page fault that
1832− * may not happen any time soon.
1833− */
1834− if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
1835− return -ENOMEM;
1836− break;
1837− case MADV_NOHUGEPAGE:
1838− /*
1839− * Be somewhat over-protective like KSM for now!
1840− */
1841− if (*vm_flags & VM_NO_THP)
1842− return -EINVAL;
1843− *vm_flags &= ~VM_HUGEPAGE;
1844− *vm_flags |= VM_NOHUGEPAGE;
1845− /*
1846− * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1847− * this vma even if we leave the mm registered in khugepaged if
1848− * it got registered before VM_NOHUGEPAGE was set.
1849− */
1850− break;
1851− }
1479+ struct mm_struct *mm = vma->vm_mm;
1480+ pgtable_t pgtable;
1481+ pmd_t _pmd;
1482+ int i;
18521483
1853− return 0;
1854−}
1484+ /* leave pmd empty until pte is filled */
1485+ pmdp_huge_clear_flush_notify(vma, haddr, pmd);
18551486
1856−static int __init khugepaged_slab_init(void)
1857−{
1858− mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1859− sizeof(struct mm_slot),
1860− __alignof__(struct mm_slot), 0, NULL);
1861− if (!mm_slot_cache)
1862− return -ENOMEM;
1487+ pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1488+ pmd_populate(mm, &_pmd, pgtable);
18631489
1864− return 0;
1490+ for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1491+ pte_t *pte, entry;
1492+ entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1493+ entry = pte_mkspecial(entry);
1494+ pte = pte_offset_map(&_pmd, haddr);
1495+ VM_BUG_ON(!pte_none(*pte));
1496+ set_pte_at(mm, haddr, pte, entry);
1497+ pte_unmap(pte);
1498+ }
1499+ smp_wmb(); /* make pte visible before pmd */
1500+ pmd_populate(mm, pmd, pgtable);
1501+ put_huge_zero_page();
18651502 }
18661503
1867−static void __init khugepaged_slab_exit(void)
1504+static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
1505+ unsigned long haddr, bool freeze)
18681506 {
1869− kmem_cache_destroy(mm_slot_cache);
1870−}
1507+ struct mm_struct *mm = vma->vm_mm;
1508+ struct page *page;
1509+ pgtable_t pgtable;
1510+ pmd_t _pmd;
1511+ bool young, write, dirty;
1512+ unsigned long addr;
1513+ int i;
18711514
1872−static inline struct mm_slot *alloc_mm_slot(void)
1873−{
1874− if (!mm_slot_cache) /* initialization failed */
1875− return NULL;
1876− return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1877−}
1515+ VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
1516+ VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
1517+ VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
1518+ VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
18781519
1879−static inline void free_mm_slot(struct mm_slot *mm_slot)
1880−{
1881− kmem_cache_free(mm_slot_cache, mm_slot);
1882−}
1520+ count_vm_event(THP_SPLIT_PMD);
18831521
1884−static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1885−{
1886− struct mm_slot *mm_slot;
1522+ if (!vma_is_anonymous(vma)) {
1523+ _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
1524+ if (is_huge_zero_pmd(_pmd))
1525+ put_huge_zero_page();
1526+ if (vma_is_dax(vma))
1527+ return;
1528+ page = pmd_page(_pmd);
1529+ if (!PageReferenced(page) && pmd_young(_pmd))
1530+ SetPageReferenced(page);
1531+ page_remove_rmap(page, true);
1532+ put_page(page);
1533+ add_mm_counter(mm, MM_FILEPAGES, -HPAGE_PMD_NR);
1534+ return;
1535+ } else if (is_huge_zero_pmd(*pmd)) {
1536+ return __split_huge_zero_page_pmd(vma, haddr, pmd);
1537+ }
18871538
1888− hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
1889− if (mm == mm_slot->mm)
1890− return mm_slot;
1539+ page = pmd_page(*pmd);
1540+ VM_BUG_ON_PAGE(!page_count(page), page);
1541+ page_ref_add(page, HPAGE_PMD_NR - 1);
1542+ write = pmd_write(*pmd);
1543+ young = pmd_young(*pmd);
1544+ dirty = pmd_dirty(*pmd);
18911545
1892− return NULL;
1893−}
1894−
1895−static void insert_to_mm_slots_hash(struct mm_struct *mm,
1896− struct mm_slot *mm_slot)
1897−{
1898− mm_slot->mm = mm;
1899− hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
1900−}
1901−
1902−static inline int khugepaged_test_exit(struct mm_struct *mm)
1903−{
1904− return atomic_read(&mm->mm_users) == 0;
1905−}
1906−
1907−int __khugepaged_enter(struct mm_struct *mm)
1908−{
1909− struct mm_slot *mm_slot;
1910− int wakeup;
1911−
1912− mm_slot = alloc_mm_slot();
1913− if (!mm_slot)
1914− return -ENOMEM;
1915−
1916− /* __khugepaged_exit() must not run from under us */
1917− VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
1918− if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1919− free_mm_slot(mm_slot);
1920− return 0;
1921− }
1922−
1923− spin_lock(&khugepaged_mm_lock);
1924− insert_to_mm_slots_hash(mm, mm_slot);
1925− /*
1926− * Insert just behind the scanning cursor, to let the area settle
1927− * down a little.
1928− */
1929− wakeup = list_empty(&khugepaged_scan.mm_head);
1930− list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1931− spin_unlock(&khugepaged_mm_lock);
1932−
1933− atomic_inc(&mm->mm_count);
1934− if (wakeup)
1935− wake_up_interruptible(&khugepaged_wait);
1936−
1937− return 0;
1938−}
1939−
1940−int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1941− unsigned long vm_flags)
1942−{
1943− unsigned long hstart, hend;
1944− if (!vma->anon_vma)
1945− /*
1946− * Not yet faulted in so we will register later in the
1947− * page fault if needed.
1948− */
1949− return 0;
1950− if (vma->vm_ops || (vm_flags & VM_NO_THP))
1951− /* khugepaged not yet working on file or special mappings */
1952− return 0;
1953− hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1954− hend = vma->vm_end & HPAGE_PMD_MASK;
1955− if (hstart < hend)
1956− return khugepaged_enter(vma, vm_flags);
1957− return 0;
1958−}
1959−
1960−void __khugepaged_exit(struct mm_struct *mm)
1961−{
1962− struct mm_slot *mm_slot;
1963− int free = 0;
1964−
1965− spin_lock(&khugepaged_mm_lock);
1966− mm_slot = get_mm_slot(mm);
1967− if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1968− hash_del(&mm_slot->hash);
1969− list_del(&mm_slot->mm_node);
1970− free = 1;
1971− }
1972− spin_unlock(&khugepaged_mm_lock);
1973−
1974− if (free) {
1975− clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1976− free_mm_slot(mm_slot);
1977− mmdrop(mm);
1978− } else if (mm_slot) {
1979− /*
1980− * This is required to serialize against
1981− * khugepaged_test_exit() (which is guaranteed to run
1982− * under mmap sem read mode). Stop here (after we
1983− * return all pagetables will be destroyed) until
1984− * khugepaged has finished working on the pagetables
1985− * under the mmap_sem.
1986− */
1987− down_write(&mm->mmap_sem);
1988− up_write(&mm->mmap_sem);
1989− }
1990−}
1991−
1992−static void release_pte_page(struct page *page)
1993−{
1994− /* 0 stands for page_is_file_cache(page) == false */
1995− dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1996− unlock_page(page);
1997− putback_lru_page(page);
1998−}
1999−
2000−static void release_pte_pages(pte_t *pte, pte_t *_pte)
2001−{
2002− while (--_pte >= pte) {
2003− pte_t pteval = *_pte;
2004− if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
2005− release_pte_page(pte_page(pteval));
2006− }
2007−}
2008−
2009−static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2010− unsigned long address,
2011− pte_t *pte)
2012−{
2013− struct page *page = NULL;
2014− pte_t *_pte;
2015− int none_or_zero = 0, result = 0;
2016− bool referenced = false, writable = false;
2017−
2018− for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2019− _pte++, address += PAGE_SIZE) {
2020− pte_t pteval = *_pte;
2021− if (pte_none(pteval) || (pte_present(pteval) &&
2022− is_zero_pfn(pte_pfn(pteval)))) {
2023− if (!userfaultfd_armed(vma) &&
2024− ++none_or_zero <= khugepaged_max_ptes_none) {
2025− continue;
2026− } else {
2027− result = SCAN_EXCEED_NONE_PTE;
2028− goto out;
2029− }
2030− }
2031− if (!pte_present(pteval)) {
2032− result = SCAN_PTE_NON_PRESENT;
2033− goto out;
2034− }
2035− page = vm_normal_page(vma, address, pteval);
2036− if (unlikely(!page)) {
2037− result = SCAN_PAGE_NULL;
2038− goto out;
2039− }
2040−
2041− VM_BUG_ON_PAGE(PageCompound(page), page);
2042− VM_BUG_ON_PAGE(!PageAnon(page), page);
2043− VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
2044−
2045− /*
2046− * We can do it before isolate_lru_page because the
2047− * page can't be freed from under us. NOTE: PG_lock
2048− * is needed to serialize against split_huge_page
2049− * when invoked from the VM.
2050− */
2051− if (!trylock_page(page)) {
2052− result = SCAN_PAGE_LOCK;
2053− goto out;
2054− }
2055−
2056− /*
2057− * cannot use mapcount: can't collapse if there's a gup pin.
2058− * The page must only be referenced by the scanned process
2059− * and page swap cache.
2060− */
2061− if (page_count(page) != 1 + !!PageSwapCache(page)) {
2062− unlock_page(page);
2063− result = SCAN_PAGE_COUNT;
2064− goto out;
2065− }
2066− if (pte_write(pteval)) {
2067− writable = true;
2068− } else {
2069− if (PageSwapCache(page) &&
2070− !reuse_swap_page(page, NULL)) {
2071− unlock_page(page);
2072− result = SCAN_SWAP_CACHE_PAGE;
2073− goto out;
2074− }
2075− /*
2076− * Page is not in the swap cache. It can be collapsed
2077− * into a THP.
2078− */
2079− }
2080−
2081− /*
2082− * Isolate the page to avoid collapsing an hugepage
2083− * currently in use by the VM.
2084− */
2085− if (isolate_lru_page(page)) {
2086− unlock_page(page);
2087− result = SCAN_DEL_PAGE_LRU;
2088− goto out;
2089− }
2090− /* 0 stands for page_is_file_cache(page) == false */
2091− inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2092− VM_BUG_ON_PAGE(!PageLocked(page), page);
2093− VM_BUG_ON_PAGE(PageLRU(page), page);
2094−
2095− /* If there is no mapped pte young don't collapse the page */
2096− if (pte_young(pteval) ||
2097− page_is_young(page) || PageReferenced(page) ||
2098− mmu_notifier_test_young(vma->vm_mm, address))
2099− referenced = true;
2100− }
2101− if (likely(writable)) {
2102− if (likely(referenced)) {
2103− result = SCAN_SUCCEED;
2104− trace_mm_collapse_huge_page_isolate(page, none_or_zero,
2105− referenced, writable, result);
2106− return 1;
2107− }
2108− } else {
2109− result = SCAN_PAGE_RO;
2110− }
2111−
2112−out:
2113− release_pte_pages(pte, _pte);
2114− trace_mm_collapse_huge_page_isolate(page, none_or_zero,
2115− referenced, writable, result);
2116− return 0;
2117−}
2118−
2119−static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2120− struct vm_area_struct *vma,
2121− unsigned long address,
2122− spinlock_t *ptl)
2123−{
2124− pte_t *_pte;
2125− for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2126− pte_t pteval = *_pte;
2127− struct page *src_page;
2128−
2129− if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
2130− clear_user_highpage(page, address);
2131− add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2132− if (is_zero_pfn(pte_pfn(pteval))) {
2133− /*
2134− * ptl mostly unnecessary.
2135− */
2136− spin_lock(ptl);
2137− /*
2138− * paravirt calls inside pte_clear here are
2139− * superfluous.
2140− */
2141− pte_clear(vma->vm_mm, address, _pte);
2142− spin_unlock(ptl);
2143− }
2144− } else {
2145− src_page = pte_page(pteval);
2146− copy_user_highpage(page, src_page, address, vma);
2147− VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
2148− release_pte_page(src_page);
2149− /*
2150− * ptl mostly unnecessary, but preempt has to
2151− * be disabled to update the per-cpu stats
2152− * inside page_remove_rmap().
2153− */
2154− spin_lock(ptl);
2155− /*
2156− * paravirt calls inside pte_clear here are
2157− * superfluous.
2158− */
2159− pte_clear(vma->vm_mm, address, _pte);
2160− page_remove_rmap(src_page, false);
2161− spin_unlock(ptl);
2162− free_page_and_swap_cache(src_page);
2163− }
2164−
2165− address += PAGE_SIZE;
2166− page++;
2167− }
2168−}
2169−
2170−static void khugepaged_alloc_sleep(void)
2171−{
2172− DEFINE_WAIT(wait);
2173−
2174− add_wait_queue(&khugepaged_wait, &wait);
2175− freezable_schedule_timeout_interruptible(
2176− msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2177− remove_wait_queue(&khugepaged_wait, &wait);
2178−}
2179−
2180−static int khugepaged_node_load[MAX_NUMNODES];
2181−
2182−static bool khugepaged_scan_abort(int nid)
2183−{
2184− int i;
2185−
2186− /*
2187− * If zone_reclaim_mode is disabled, then no extra effort is made to
2188− * allocate memory locally.
2189− */
2190− if (!zone_reclaim_mode)
2191− return false;
2192−
2193− /* If there is a count for this node already, it must be acceptable */
2194− if (khugepaged_node_load[nid])
2195− return false;
2196−
2197− for (i = 0; i < MAX_NUMNODES; i++) {
2198− if (!khugepaged_node_load[i])
2199− continue;
2200− if (node_distance(nid, i) > RECLAIM_DISTANCE)
2201− return true;
2202− }
2203− return false;
2204−}
2205−
2206−#ifdef CONFIG_NUMA
2207−static int khugepaged_find_target_node(void)
2208−{
2209− static int last_khugepaged_target_node = NUMA_NO_NODE;
2210− int nid, target_node = 0, max_value = 0;
2211−
2212− /* find first node with max normal pages hit */
2213− for (nid = 0; nid < MAX_NUMNODES; nid++)
2214− if (khugepaged_node_load[nid] > max_value) {
2215− max_value = khugepaged_node_load[nid];
2216− target_node = nid;
2217− }
2218−
2219− /* do some balance if several nodes have the same hit record */
2220− if (target_node <= last_khugepaged_target_node)
2221− for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2222− nid++)
2223− if (max_value == khugepaged_node_load[nid]) {
2224− target_node = nid;
2225− break;
2226− }
2227−
2228− last_khugepaged_target_node = target_node;
2229− return target_node;
2230−}
2231−
2232−static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2233−{
2234− if (IS_ERR(*hpage)) {
2235− if (!*wait)
2236− return false;
2237−
2238− *wait = false;
2239− *hpage = NULL;
2240− khugepaged_alloc_sleep();
2241− } else if (*hpage) {
2242− put_page(*hpage);
2243− *hpage = NULL;
2244− }
2245−
2246− return true;
2247−}
2248−
2249−static struct page *
2250−khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
2251− unsigned long address, int node)
2252−{
2253− VM_BUG_ON_PAGE(*hpage, *hpage);
2254−
2255− /*
2256− * Before allocating the hugepage, release the mmap_sem read lock.
2257− * The allocation can take potentially a long time if it involves
2258− * sync compaction, and we do not need to hold the mmap_sem during
2259− * that. We will recheck the vma after taking it again in write mode.
2260− */
2261− up_read(&mm->mmap_sem);
2262−
2263− *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
2264− if (unlikely(!*hpage)) {
2265− count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2266− *hpage = ERR_PTR(-ENOMEM);
2267− return NULL;
2268− }
2269−
2270− prep_transhuge_page(*hpage);
2271− count_vm_event(THP_COLLAPSE_ALLOC);
2272− return *hpage;
2273−}
2274−#else
2275−static int khugepaged_find_target_node(void)
2276−{
2277− return 0;
2278−}
2279−
2280−static inline struct page *alloc_khugepaged_hugepage(void)
2281−{
2282− struct page *page;
2283−
2284− page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2285− HPAGE_PMD_ORDER);
2286− if (page)
2287− prep_transhuge_page(page);
2288− return page;
2289−}
2290−
2291−static struct page *khugepaged_alloc_hugepage(bool *wait)
2292−{
2293− struct page *hpage;
2294−
2295− do {
2296− hpage = alloc_khugepaged_hugepage();
2297− if (!hpage) {
2298− count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2299− if (!*wait)
2300− return NULL;
2301−
2302− *wait = false;
2303− khugepaged_alloc_sleep();
2304− } else
2305− count_vm_event(THP_COLLAPSE_ALLOC);
2306− } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2307−
2308− return hpage;
2309−}
2310−
2311−static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2312−{
2313− if (!*hpage)
2314− *hpage = khugepaged_alloc_hugepage(wait);
2315−
2316− if (unlikely(!*hpage))
2317− return false;
2318−
2319− return true;
2320−}
2321−
2322−static struct page *
2323−khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
2324− unsigned long address, int node)
2325−{
2326− up_read(&mm->mmap_sem);
2327− VM_BUG_ON(!*hpage);
2328−
2329− return *hpage;
2330−}
2331−#endif
2332−
2333−static bool hugepage_vma_check(struct vm_area_struct *vma)
2334−{
2335− if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2336− (vma->vm_flags & VM_NOHUGEPAGE))
2337− return false;
2338− if (!vma->anon_vma || vma->vm_ops)
2339− return false;
2340− if (is_vma_temporary_stack(vma))
2341− return false;
2342− return !(vma->vm_flags & VM_NO_THP);
2343−}
2344−
2345−static void collapse_huge_page(struct mm_struct *mm,
2346− unsigned long address,
2347− struct page **hpage,
2348− struct vm_area_struct *vma,
2349− int node)
2350−{
2351− pmd_t *pmd, _pmd;
2352− pte_t *pte;
2353− pgtable_t pgtable;
2354− struct page *new_page;
2355− spinlock_t *pmd_ptl, *pte_ptl;
2356− int isolated = 0, result = 0;
2357− unsigned long hstart, hend;
2358− struct mem_cgroup *memcg;
2359− unsigned long mmun_start; /* For mmu_notifiers */
2360− unsigned long mmun_end; /* For mmu_notifiers */
2361− gfp_t gfp;
2362−
2363− VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2364−
2365− /* Only allocate from the target node */
2366− gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
2367−
2368− /* release the mmap_sem read lock. */
2369− new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
2370− if (!new_page) {
2371− result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2372− goto out_nolock;
2373− }
2374−
2375− if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
2376− result = SCAN_CGROUP_CHARGE_FAIL;
2377− goto out_nolock;
2378− }
2379−
2380− /*
2381− * Prevent all access to pagetables with the exception of
2382− * gup_fast later hanlded by the ptep_clear_flush and the VM
2383− * handled by the anon_vma lock + PG_lock.
2384− */
2385− down_write(&mm->mmap_sem);
2386− if (unlikely(khugepaged_test_exit(mm))) {
2387− result = SCAN_ANY_PROCESS;
2388− goto out;
2389− }
2390−
2391− vma = find_vma(mm, address);
2392− if (!vma) {
2393− result = SCAN_VMA_NULL;
2394− goto out;
2395− }
2396− hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2397− hend = vma->vm_end & HPAGE_PMD_MASK;
2398− if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2399− result = SCAN_ADDRESS_RANGE;
2400− goto out;
2401− }
2402− if (!hugepage_vma_check(vma)) {
2403− result = SCAN_VMA_CHECK;
2404− goto out;
2405− }
2406− pmd = mm_find_pmd(mm, address);
2407− if (!pmd) {
2408− result = SCAN_PMD_NULL;
2409− goto out;
2410− }
2411−
2412− anon_vma_lock_write(vma->anon_vma);
2413−
2414− pte = pte_offset_map(pmd, address);
2415− pte_ptl = pte_lockptr(mm, pmd);
2416−
2417− mmun_start = address;
2418− mmun_end = address + HPAGE_PMD_SIZE;
2419− mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2420− pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
2421− /*
2422− * After this gup_fast can't run anymore. This also removes
2423− * any huge TLB entry from the CPU so we won't allow
2424− * huge and small TLB entries for the same virtual address
2425− * to avoid the risk of CPU bugs in that area.
2426− */
2427− _pmd = pmdp_collapse_flush(vma, address, pmd);
2428− spin_unlock(pmd_ptl);
2429− mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2430−
2431− spin_lock(pte_ptl);
2432− isolated = __collapse_huge_page_isolate(vma, address, pte);
2433− spin_unlock(pte_ptl);
2434−
2435− if (unlikely(!isolated)) {
2436− pte_unmap(pte);
2437− spin_lock(pmd_ptl);
2438− BUG_ON(!pmd_none(*pmd));
2439− /*
2440− * We can only use set_pmd_at when establishing
2441− * hugepmds and never for establishing regular pmds that
2442− * points to regular pagetables. Use pmd_populate for that
2443− */
2444− pmd_populate(mm, pmd, pmd_pgtable(_pmd));
2445− spin_unlock(pmd_ptl);
2446− anon_vma_unlock_write(vma->anon_vma);
2447− result = SCAN_FAIL;
2448− goto out;
2449− }
2450−
2451− /*
2452− * All pages are isolated and locked so anon_vma rmap
2453− * can't run anymore.
2454− */
2455− anon_vma_unlock_write(vma->anon_vma);
2456−
2457− __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
2458− pte_unmap(pte);
2459− __SetPageUptodate(new_page);
2460− pgtable = pmd_pgtable(_pmd);
2461−
2462− _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2463− _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
2464−
2465− /*
2466− * spin_lock() below is not the equivalent of smp_wmb(), so
2467− * this is needed to avoid the copy_huge_page writes to become
2468− * visible after the set_pmd_at() write.
2469− */
2470− smp_wmb();
2471−
2472− spin_lock(pmd_ptl);
2473− BUG_ON(!pmd_none(*pmd));
2474− page_add_new_anon_rmap(new_page, vma, address, true);
2475− mem_cgroup_commit_charge(new_page, memcg, false, true);
2476− lru_cache_add_active_or_unevictable(new_page, vma);
2477− pgtable_trans_huge_deposit(mm, pmd, pgtable);
2478− set_pmd_at(mm, address, pmd, _pmd);
2479− update_mmu_cache_pmd(vma, address, pmd);
2480− spin_unlock(pmd_ptl);
2481−
2482− *hpage = NULL;
2483−
2484− khugepaged_pages_collapsed++;
2485− result = SCAN_SUCCEED;
2486−out_up_write:
2487− up_write(&mm->mmap_sem);
2488− trace_mm_collapse_huge_page(mm, isolated, result);
2489− return;
2490−
2491−out_nolock:
2492− trace_mm_collapse_huge_page(mm, isolated, result);
2493− return;
2494−out:
2495− mem_cgroup_cancel_charge(new_page, memcg, true);
2496− goto out_up_write;
2497−}
2498−
2499−static int khugepaged_scan_pmd(struct mm_struct *mm,
2500− struct vm_area_struct *vma,
2501− unsigned long address,
2502− struct page **hpage)
2503−{
2504− pmd_t *pmd;
2505− pte_t *pte, *_pte;
2506− int ret = 0, none_or_zero = 0, result = 0;
2507− struct page *page = NULL;
2508− unsigned long _address;
2509− spinlock_t *ptl;
2510− int node = NUMA_NO_NODE;
2511− bool writable = false, referenced = false;
2512−
2513− VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2514−
2515− pmd = mm_find_pmd(mm, address);
2516− if (!pmd) {
2517− result = SCAN_PMD_NULL;
2518− goto out;
2519− }
2520−
2521− memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
2522− pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2523− for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2524− _pte++, _address += PAGE_SIZE) {
2525− pte_t pteval = *_pte;
2526− if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
2527− if (!userfaultfd_armed(vma) &&
2528− ++none_or_zero <= khugepaged_max_ptes_none) {
2529− continue;
2530− } else {
2531− result = SCAN_EXCEED_NONE_PTE;
2532− goto out_unmap;
2533− }
2534− }
2535− if (!pte_present(pteval)) {
2536− result = SCAN_PTE_NON_PRESENT;
2537− goto out_unmap;
2538− }
2539− if (pte_write(pteval))
2540− writable = true;
2541−
2542− page = vm_normal_page(vma, _address, pteval);
2543− if (unlikely(!page)) {
2544− result = SCAN_PAGE_NULL;
2545− goto out_unmap;
2546− }
2547−
2548− /* TODO: teach khugepaged to collapse THP mapped with pte */
2549− if (PageCompound(page)) {
2550− result = SCAN_PAGE_COMPOUND;
2551− goto out_unmap;
2552− }
2553−
2554− /*
2555− * Record which node the original page is from and save this
2556− * information to khugepaged_node_load[].
2557− * Khupaged will allocate hugepage from the node has the max
2558− * hit record.
2559− */
2560− node = page_to_nid(page);
2561− if (khugepaged_scan_abort(node)) {
2562− result = SCAN_SCAN_ABORT;
2563− goto out_unmap;
2564− }
2565− khugepaged_node_load[node]++;
2566− if (!PageLRU(page)) {
2567− result = SCAN_PAGE_LRU;
2568− goto out_unmap;
2569− }
2570− if (PageLocked(page)) {
2571− result = SCAN_PAGE_LOCK;
2572− goto out_unmap;
2573− }
2574− if (!PageAnon(page)) {
2575− result = SCAN_PAGE_ANON;
2576− goto out_unmap;
2577− }
2578−
2579− /*
2580− * cannot use mapcount: can't collapse if there's a gup pin.
2581− * The page must only be referenced by the scanned process
2582− * and page swap cache.
2583− */
2584− if (page_count(page) != 1 + !!PageSwapCache(page)) {
2585− result = SCAN_PAGE_COUNT;
2586− goto out_unmap;
2587− }
2588− if (pte_young(pteval) ||
2589− page_is_young(page) || PageReferenced(page) ||
2590− mmu_notifier_test_young(vma->vm_mm, address))
2591− referenced = true;
2592− }
2593− if (writable) {
2594− if (referenced) {
2595− result = SCAN_SUCCEED;
2596− ret = 1;
2597− } else {
2598− result = SCAN_NO_REFERENCED_PAGE;
2599− }
2600− } else {
2601− result = SCAN_PAGE_RO;
2602− }
2603−out_unmap:
2604− pte_unmap_unlock(pte, ptl);
2605− if (ret) {
2606− node = khugepaged_find_target_node();
2607− /* collapse_huge_page will return with the mmap_sem released */
2608− collapse_huge_page(mm, address, hpage, vma, node);
2609− }
2610−out:
2611− trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
2612− none_or_zero, result);
2613− return ret;
2614−}
2615−
2616−static void collect_mm_slot(struct mm_slot *mm_slot)
2617−{
2618− struct mm_struct *mm = mm_slot->mm;
2619−
2620− VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2621−
2622− if (khugepaged_test_exit(mm)) {
2623− /* free mm_slot */
2624− hash_del(&mm_slot->hash);
2625− list_del(&mm_slot->mm_node);
2626−
2627− /*
2628− * Not strictly needed because the mm exited already.
2629− *
2630− * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2631− */
2632−
2633− /* khugepaged_mm_lock actually not necessary for the below */
2634− free_mm_slot(mm_slot);
2635− mmdrop(mm);
2636− }
2637−}
2638−
2639−static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2640− struct page **hpage)
2641− __releases(&khugepaged_mm_lock)
2642− __acquires(&khugepaged_mm_lock)
2643−{
2644− struct mm_slot *mm_slot;
2645− struct mm_struct *mm;
2646− struct vm_area_struct *vma;
2647− int progress = 0;
2648−
2649− VM_BUG_ON(!pages);
2650− VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2651−
2652− if (khugepaged_scan.mm_slot)
2653− mm_slot = khugepaged_scan.mm_slot;
2654− else {
2655− mm_slot = list_entry(khugepaged_scan.mm_head.next,
2656− struct mm_slot, mm_node);
2657− khugepaged_scan.address = 0;
2658− khugepaged_scan.mm_slot = mm_slot;
2659− }
2660− spin_unlock(&khugepaged_mm_lock);
2661−
2662− mm = mm_slot->mm;
2663− down_read(&mm->mmap_sem);
2664− if (unlikely(khugepaged_test_exit(mm)))
2665− vma = NULL;
2666− else
2667− vma = find_vma(mm, khugepaged_scan.address);
2668−
2669− progress++;
2670− for (; vma; vma = vma->vm_next) {
2671− unsigned long hstart, hend;
2672−
2673− cond_resched();
2674− if (unlikely(khugepaged_test_exit(mm))) {
2675− progress++;
2676− break;
2677− }
2678− if (!hugepage_vma_check(vma)) {
2679−skip:
2680− progress++;
2681− continue;
2682− }
2683− hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2684− hend = vma->vm_end & HPAGE_PMD_MASK;
2685− if (hstart >= hend)
2686− goto skip;
2687− if (khugepaged_scan.address > hend)
2688− goto skip;
2689− if (khugepaged_scan.address < hstart)
2690− khugepaged_scan.address = hstart;
2691− VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2692−
2693− while (khugepaged_scan.address < hend) {
2694− int ret;
2695− cond_resched();
2696− if (unlikely(khugepaged_test_exit(mm)))
2697− goto breakouterloop;
2698−
2699− VM_BUG_ON(khugepaged_scan.address < hstart ||
2700− khugepaged_scan.address + HPAGE_PMD_SIZE >
2701− hend);
2702− ret = khugepaged_scan_pmd(mm, vma,
2703− khugepaged_scan.address,
2704− hpage);
2705− /* move to next address */
2706− khugepaged_scan.address += HPAGE_PMD_SIZE;
2707− progress += HPAGE_PMD_NR;
2708− if (ret)
2709− /* we released mmap_sem so break loop */
2710− goto breakouterloop_mmap_sem;
2711− if (progress >= pages)
2712− goto breakouterloop;
2713− }
2714− }
2715−breakouterloop:
2716− up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2717−breakouterloop_mmap_sem:
2718−
2719− spin_lock(&khugepaged_mm_lock);
2720− VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2721− /*
2722− * Release the current mm_slot if this mm is about to die, or
2723− * if we scanned all vmas of this mm.
2724− */
2725− if (khugepaged_test_exit(mm) || !vma) {
2726− /*
2727− * Make sure that if mm_users is reaching zero while
2728− * khugepaged runs here, khugepaged_exit will find
2729− * mm_slot not pointing to the exiting mm.
2730− */
2731− if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2732− khugepaged_scan.mm_slot = list_entry(
2733− mm_slot->mm_node.next,
2734− struct mm_slot, mm_node);
2735− khugepaged_scan.address = 0;
2736− } else {
2737− khugepaged_scan.mm_slot = NULL;
2738− khugepaged_full_scans++;
2739− }
2740−
2741− collect_mm_slot(mm_slot);
2742− }
2743−
2744− return progress;
2745−}
2746−
2747−static int khugepaged_has_work(void)
2748−{
2749− return !list_empty(&khugepaged_scan.mm_head) &&
2750− khugepaged_enabled();
2751−}
2752−
2753−static int khugepaged_wait_event(void)
2754−{
2755− return !list_empty(&khugepaged_scan.mm_head) ||
2756− kthread_should_stop();
2757−}
2758−
2759−static void khugepaged_do_scan(void)
2760−{
2761− struct page *hpage = NULL;
2762− unsigned int progress = 0, pass_through_head = 0;
2763− unsigned int pages = khugepaged_pages_to_scan;
2764− bool wait = true;
2765−
2766− barrier(); /* write khugepaged_pages_to_scan to local stack */
2767−
2768− while (progress < pages) {
2769− if (!khugepaged_prealloc_page(&hpage, &wait))
2770− break;
2771−
2772− cond_resched();
2773−
2774− if (unlikely(kthread_should_stop() || try_to_freeze()))
2775− break;
2776−
2777− spin_lock(&khugepaged_mm_lock);
2778− if (!khugepaged_scan.mm_slot)
2779− pass_through_head++;
2780− if (khugepaged_has_work() &&
2781− pass_through_head < 2)
2782− progress += khugepaged_scan_mm_slot(pages - progress,
2783− &hpage);
2784− else
2785− progress = pages;
2786− spin_unlock(&khugepaged_mm_lock);
2787− }
2788−
2789− if (!IS_ERR_OR_NULL(hpage))
2790− put_page(hpage);
2791−}
2792−
2793−static bool khugepaged_should_wakeup(void)
2794−{
2795− return kthread_should_stop() ||
2796− time_after_eq(jiffies, khugepaged_sleep_expire);
2797−}
2798−
2799−static void khugepaged_wait_work(void)
2800−{
2801− if (khugepaged_has_work()) {
2802− const unsigned long scan_sleep_jiffies =
2803− msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2804−
2805− if (!scan_sleep_jiffies)
2806− return;
2807−
2808− khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
2809− wait_event_freezable_timeout(khugepaged_wait,
2810− khugepaged_should_wakeup(),
2811− scan_sleep_jiffies);
2812− return;
2813− }
2814−
2815− if (khugepaged_enabled())
2816− wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2817−}
2818−
2819−static int khugepaged(void *none)
2820−{
2821− struct mm_slot *mm_slot;
2822−
2823− set_freezable();
2824− set_user_nice(current, MAX_NICE);
2825−
2826− while (!kthread_should_stop()) {
2827− khugepaged_do_scan();
2828− khugepaged_wait_work();
2829− }
2830−
2831− spin_lock(&khugepaged_mm_lock);
2832− mm_slot = khugepaged_scan.mm_slot;
2833− khugepaged_scan.mm_slot = NULL;
2834− if (mm_slot)
2835− collect_mm_slot(mm_slot);
2836− spin_unlock(&khugepaged_mm_lock);
2837− return 0;
2838−}
2839−
2840−static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2841− unsigned long haddr, pmd_t *pmd)
2842−{
2843− struct mm_struct *mm = vma->vm_mm;
2844− pgtable_t pgtable;
2845− pmd_t _pmd;
2846− int i;
2847−
2848− /* leave pmd empty until pte is filled */
2849− pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2850−
2851− pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2852− pmd_populate(mm, &_pmd, pgtable);
2853−
2854− for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2855− pte_t *pte, entry;
2856− entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2857− entry = pte_mkspecial(entry);
2858− pte = pte_offset_map(&_pmd, haddr);
2859− VM_BUG_ON(!pte_none(*pte));
2860− set_pte_at(mm, haddr, pte, entry);
2861− pte_unmap(pte);
2862− }
2863− smp_wmb(); /* make pte visible before pmd */
2864− pmd_populate(mm, pmd, pgtable);
2865− put_huge_zero_page();
2866−}
2867−
2868−static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
2869− unsigned long haddr, bool freeze)
2870−{
2871− struct mm_struct *mm = vma->vm_mm;
2872− struct page *page;
2873− pgtable_t pgtable;
2874− pmd_t _pmd;
2875− bool young, write, dirty;
2876− unsigned long addr;
2877− int i;
2878−
2879− VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2880− VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2881− VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
2882− VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
2883−
2884− count_vm_event(THP_SPLIT_PMD);
2885−
2886− if (vma_is_dax(vma)) {
2887− pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2888− if (is_huge_zero_pmd(_pmd))
2889− put_huge_zero_page();
2890− return;
2891− } else if (is_huge_zero_pmd(*pmd)) {
2892− return __split_huge_zero_page_pmd(vma, haddr, pmd);
2893− }
2894−
2895− page = pmd_page(*pmd);
2896− VM_BUG_ON_PAGE(!page_count(page), page);
2897− page_ref_add(page, HPAGE_PMD_NR - 1);
2898− write = pmd_write(*pmd);
2899− young = pmd_young(*pmd);
2900− dirty = pmd_dirty(*pmd);
2901−
2902− pmdp_huge_split_prepare(vma, haddr, pmd);
2903− pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2904− pmd_populate(mm, &_pmd, pgtable);
1546+ pmdp_huge_split_prepare(vma, haddr, pmd);
1547+ pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1548+ pmd_populate(mm, &_pmd, pgtable);
29051549
29061550 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
29071551 pte_t entry, *pte;
@@ -2942,7 +1586,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · 34 unmodified lines
29421586
29431587 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
29441588 /* Last compound_mapcount is gone. */
2945− __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1589+ __dec_zone_page_state(page, NR_ANON_THPS);
29461590 if (TestClearPageDoubleMap(page)) {
29471591 /* No need in mapcount reference anymore */
29481592 for (i = 0; i < HPAGE_PMD_NR; i++)
@@ -3076,12 +1720,15 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma, · 127 unmodified lines
30761720
30771721 static void freeze_page(struct page *page)
30781722 {
3079− enum ttu_flags ttu_flags = TTU_MIGRATION | TTU_IGNORE_MLOCK |
3080− TTU_IGNORE_ACCESS | TTU_RMAP_LOCKED;
1723+ enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS |
1724+ TTU_RMAP_LOCKED;
30811725 int i, ret;
30821726
30831727 VM_BUG_ON_PAGE(!PageHead(page), page);
30841728
1729+ if (PageAnon(page))
1730+ ttu_flags |= TTU_MIGRATION;
1731+
30851732 /* We only need TTU_SPLIT_HUGE_PMD once */
30861733 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
30871734 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
@@ -3091,7 +1738,7 @@ static void freeze_page(struct page *page) · 3 unmodified lines
30911738
30921739 ret = try_to_unmap(page + i, ttu_flags);
30931740 }
3094− VM_BUG_ON(ret);
1741+ VM_BUG_ON_PAGE(ret, page + i - 1);
30951742 }
30961743
30971744 static void unfreeze_page(struct page *page)
@@ -3113,15 +1760,20 @@ static void __split_huge_page_tail(struct page *head, int tail, · 15 unmodified lines
31131760 /*
31141761 * tail_page->_refcount is zero and not changing from under us. But
31151762 * get_page_unless_zero() may be running from under us on the
3116− * tail_page. If we used atomic_set() below instead of atomic_inc(), we
3117− * would then run atomic_set() concurrently with
1763+ * tail_page. If we used atomic_set() below instead of atomic_inc() or
1764+ * atomic_add(), we would then run atomic_set() concurrently with
31181765 * get_page_unless_zero(), and atomic_set() is implemented in C not
31191766 * using locked ops. spin_unlock on x86 sometime uses locked ops
31201767 * because of PPro errata 66, 92, so unless somebody can guarantee
31211768 * atomic_set() here would be safe on all archs (and not only on x86),
3122− * it's safer to use atomic_inc().
1769+ * it's safer to use atomic_inc()/atomic_add().
31231770 */
3124− page_ref_inc(page_tail);
1771+ if (PageAnon(head)) {
1772+ page_ref_inc(page_tail);
1773+ } else {
1774+ /* Additional pin to radix tree */
1775+ page_ref_add(page_tail, 2);
1776+ }
31251777
31261778 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
31271779 page_tail->flags |= (head->flags &
@@ -3157,25 +1809,46 @@ static void __split_huge_page_tail(struct page *head, int tail, · 29 unmodified lines
31571809 lru_add_page_tail(head, page_tail, lruvec, list);
31581810 }
31591811
3160−static void __split_huge_page(struct page *page, struct list_head *list)
1812+static void __split_huge_page(struct page *page, struct list_head *list,
1813+ unsigned long flags)
31611814 {
31621815 struct page *head = compound_head(page);
31631816 struct zone *zone = page_zone(head);
31641817 struct lruvec *lruvec;
1818+ pgoff_t end = -1;
31651819 int i;
31661820
3167− /* prevent PageLRU to go away from under us, and freeze lru stats */
3168− spin_lock_irq(&zone->lru_lock);
31691821 lruvec = mem_cgroup_page_lruvec(head, zone);
31701822
31711823 /* complete memcg works before add pages to LRU */
31721824 mem_cgroup_split_huge_fixup(head);
31731825
3174− for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
1826+ if (!PageAnon(page))
1827+ end = DIV_ROUND_UP(i_size_read(head->mapping->host), PAGE_SIZE);
1828+
1829+ for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
31751830 __split_huge_page_tail(head, i, lruvec, list);
1831+ /* Some pages can be beyond i_size: drop them from page cache */
1832+ if (head[i].index >= end) {
1833+ __ClearPageDirty(head + i);
1834+ __delete_from_page_cache(head + i, NULL);
1835+ if (IS_ENABLED(CONFIG_SHMEM) && PageSwapBacked(head))
1836+ shmem_uncharge(head->mapping->host, 1);
1837+ put_page(head + i);
1838+ }
1839+ }
31761840
31771841 ClearPageCompound(head);
3178− spin_unlock_irq(&zone->lru_lock);
1842+ /* See comment in __split_huge_page_tail() */
1843+ if (PageAnon(head)) {
1844+ page_ref_inc(head);
1845+ } else {
1846+ /* Additional pin to radix tree */
1847+ page_ref_add(head, 2);
1848+ spin_unlock(&head->mapping->tree_lock);
1849+ }
1850+
1851+ spin_unlock_irqrestore(&page_zone(head)->lru_lock, flags);
31791852
31801853 unfreeze_page(head);
31811854
@@ -3198,18 +1871,22 @@ static void __split_huge_page(struct page *page, struct list_head *list) · 16 unmodified lines
31981871
31991872 int total_mapcount(struct page *page)
32001873 {
3201− int i, ret;
1874+ int i, compound, ret;
32021875
32031876 VM_BUG_ON_PAGE(PageTail(page), page);
32041877
32051878 if (likely(!PageCompound(page)))
32061879 return atomic_read(&page->_mapcount) + 1;
32071880
3208− ret = compound_mapcount(page);
1881+ compound = compound_mapcount(page);
32091882 if (PageHuge(page))
3210− return ret;
1883+ return compound;
1884+ ret = compound;
32111885 for (i = 0; i < HPAGE_PMD_NR; i++)
32121886 ret += atomic_read(&page[i]._mapcount) + 1;
1887+ /* File pages has compound_mapcount included in _mapcount */
1888+ if (!PageAnon(page))
1889+ return ret - compound * HPAGE_PMD_NR;
32131890 if (PageDoubleMap(page))
32141891 ret -= HPAGE_PMD_NR;
32151892 return ret;
@@ -3296,36 +1973,54 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) · 80 unmodified lines
32961973 {
32971974 struct page *head = compound_head(page);
32981975 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
3299− struct anon_vma *anon_vma;
3300− int count, mapcount, ret;
1976+ struct anon_vma *anon_vma = NULL;
1977+ struct address_space *mapping = NULL;
1978+ int count, mapcount, extra_pins, ret;
33011979 bool mlocked;
33021980 unsigned long flags;
33031981
33041982 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3305− VM_BUG_ON_PAGE(!PageAnon(page), page);
33061983 VM_BUG_ON_PAGE(!PageLocked(page), page);
33071984 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
33081985 VM_BUG_ON_PAGE(!PageCompound(page), page);
33091986
3310− /*
3311− * The caller does not necessarily hold an mmap_sem that would prevent
3312− * the anon_vma disappearing so we first we take a reference to it
3313− * and then lock the anon_vma for write. This is similar to
3314− * page_lock_anon_vma_read except the write lock is taken to serialise
3315− * against parallel split or collapse operations.
3316− */
3317− anon_vma = page_get_anon_vma(head);
3318− if (!anon_vma) {
3319− ret = -EBUSY;
3320− goto out;
1987+ if (PageAnon(head)) {
1988+ /*
1989+ * The caller does not necessarily hold an mmap_sem that would
1990+ * prevent the anon_vma disappearing so we first we take a
1991+ * reference to it and then lock the anon_vma for write. This
1992+ * is similar to page_lock_anon_vma_read except the write lock
1993+ * is taken to serialise against parallel split or collapse
1994+ * operations.
1995+ */
1996+ anon_vma = page_get_anon_vma(head);
1997+ if (!anon_vma) {
1998+ ret = -EBUSY;
1999+ goto out;
2000+ }
2001+ extra_pins = 0;
2002+ mapping = NULL;
2003+ anon_vma_lock_write(anon_vma);
2004+ } else {
2005+ mapping = head->mapping;
2006+
2007+ /* Truncated ? */
2008+ if (!mapping) {
2009+ ret = -EBUSY;
2010+ goto out;
2011+ }
2012+
2013+ /* Addidional pins from radix tree */
2014+ extra_pins = HPAGE_PMD_NR;
2015+ anon_vma = NULL;
2016+ i_mmap_lock_read(mapping);
33212017 }
3322− anon_vma_lock_write(anon_vma);
33232018
33242019 /*
33252020 * Racy check if we can split the page, before freeze_page() will
33262021 * split PMDs
33272022 */
3328− if (total_mapcount(head) != page_count(head) - 1) {
2023+ if (total_mapcount(head) != page_count(head) - extra_pins - 1) {
33292024 ret = -EBUSY;
33302025 goto out_unlock;
33312026 }
@@ -3338,35 +2033,62 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) · 6 unmodified lines
33382033 if (mlocked)
33392034 lru_add_drain();
33402035
2036+ /* prevent PageLRU to go away from under us, and freeze lru stats */
2037+ spin_lock_irqsave(&page_zone(head)->lru_lock, flags);
2038+
2039+ if (mapping) {
2040+ void **pslot;
2041+
2042+ spin_lock(&mapping->tree_lock);
2043+ pslot = radix_tree_lookup_slot(&mapping->page_tree,
2044+ page_index(head));
2045+ /*
2046+ * Check if the head page is present in radix tree.
2047+ * We assume all tail are present too, if head is there.
2048+ */
2049+ if (radix_tree_deref_slot_protected(pslot,
2050+ &mapping->tree_lock) != head)
2051+ goto fail;
2052+ }
2053+
33412054 /* Prevent deferred_split_scan() touching ->_refcount */
3342− spin_lock_irqsave(&pgdata->split_queue_lock, flags);
2055+ spin_lock(&pgdata->split_queue_lock);
33432056 count = page_count(head);
33442057 mapcount = total_mapcount(head);
3345− if (!mapcount && count == 1) {
2058+ if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) {
33462059 if (!list_empty(page_deferred_list(head))) {
33472060 pgdata->split_queue_len--;
33482061 list_del(page_deferred_list(head));
33492062 }
3350− spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
3351− __split_huge_page(page, list);
2063+ if (mapping)
2064+ __dec_zone_page_state(page, NR_SHMEM_THPS);
2065+ spin_unlock(&pgdata->split_queue_lock);
2066+ __split_huge_page(page, list, flags);
33522067 ret = 0;
3353− } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
3354− spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
3355− pr_alert("total_mapcount: %u, page_count(): %u\n",
3356− mapcount, count);
3357− if (PageTail(page))
3358− dump_page(head, NULL);
3359− dump_page(page, "total_mapcount(head) > 0");
3360− BUG();
33612068 } else {
3362− spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
2069+ if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
2070+ pr_alert("total_mapcount: %u, page_count(): %u\n",
2071+ mapcount, count);
2072+ if (PageTail(page))
2073+ dump_page(head, NULL);
2074+ dump_page(page, "total_mapcount(head) > 0");
2075+ BUG();
2076+ }
2077+ spin_unlock(&pgdata->split_queue_lock);
2078+fail: if (mapping)
2079+ spin_unlock(&mapping->tree_lock);
2080+ spin_unlock_irqrestore(&page_zone(head)->lru_lock, flags);
33632081 unfreeze_page(head);
33642082 ret = -EBUSY;
33652083 }
33662084
33672085 out_unlock:
3368− anon_vma_unlock_write(anon_vma);
3369− put_anon_vma(anon_vma);
2086+ if (anon_vma) {
2087+ anon_vma_unlock_write(anon_vma);
2088+ put_anon_vma(anon_vma);
2089+ }
2090+ if (mapping)
2091+ i_mmap_unlock_read(mapping);
33702092 out:
33712093 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
33722094 return ret;
@@ -3489,8 +2211,7 @@ static int split_huge_pages_set(void *data, u64 val) · 116 unmodified lines
34892211 if (zone != page_zone(page))
34902212 goto next;
34912213
3492− if (!PageHead(page) || !PageAnon(page) ||
3493− PageHuge(page))
2214+ if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
34942215 goto next;
34952216
34962217 total++;
mm/hugetlb.c+21−33
@@ -3179,7 +3179,6 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · 3178 unmodified lines
31793179 unsigned long start, unsigned long end,
31803180 struct page *ref_page)
31813181 {
3182− int force_flush = 0;
31833182 struct mm_struct *mm = vma->vm_mm;
31843183 unsigned long address;
31853184 pte_t *ptep;
@@ -3198,19 +3197,22 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · 12 unmodified lines
31983197 tlb_start_vma(tlb, vma);
31993198 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
32003199 address = start;
3201−again:
32023200 for (; address < end; address += sz) {
32033201 ptep = huge_pte_offset(mm, address);
32043202 if (!ptep)
32053203 continue;
32063204
32073205 ptl = huge_pte_lock(h, mm, ptep);
3208− if (huge_pmd_unshare(mm, &address, ptep))
3209− goto unlock;
3206+ if (huge_pmd_unshare(mm, &address, ptep)) {
3207+ spin_unlock(ptl);
3208+ continue;
3209+ }
32103210
32113211 pte = huge_ptep_get(ptep);
3212− if (huge_pte_none(pte))
3213− goto unlock;
3212+ if (huge_pte_none(pte)) {
3213+ spin_unlock(ptl);
3214+ continue;
3215+ }
32143216
32153217 /*
32163218 * Migrating hugepage or HWPoisoned hugepage is already
@@ -3218,7 +3220,8 @@ again: · 1 unmodified line
32183220 */
32193221 if (unlikely(!pte_present(pte))) {
32203222 huge_pte_clear(mm, address, ptep);
3221− goto unlock;
3223+ spin_unlock(ptl);
3224+ continue;
32223225 }
32233226
32243227 page = pte_page(pte);
@@ -3228,9 +3231,10 @@ again: · 3 unmodified lines
32283231 * are about to unmap is the actual page of interest.
32293232 */
32303233 if (ref_page) {
3231− if (page != ref_page)
3232− goto unlock;
3233−
3234+ if (page != ref_page) {
3235+ spin_unlock(ptl);
3236+ continue;
3237+ }
32343238 /*
32353239 * Mark the VMA as having unmapped its page so that
32363240 * future faults in this VMA will fail rather than
@@ -3246,30 +3250,14 @@ again: · 9 unmodified lines
32463250
32473251 hugetlb_count_sub(pages_per_huge_page(h), mm);
32483252 page_remove_rmap(page, true);
3249− force_flush = !__tlb_remove_page(tlb, page);
3250− if (force_flush) {
3251− address += sz;
3252− spin_unlock(ptl);
3253− break;
3254− }
3255− /* Bail out after unmapping reference page if supplied */
3256− if (ref_page) {
3257− spin_unlock(ptl);
3258− break;
3259− }
3260−unlock:
3253+
32613254 spin_unlock(ptl);
3262− }
3263− /*
3264− * mmu_gather ran out of room to batch pages, we break out of
3265− * the PTE lock to avoid doing the potential expensive TLB invalidate
3266− * and page-free while holding it.
3267− */
3268− if (force_flush) {
3269− force_flush = 0;
3270− tlb_flush_mmu(tlb);
3271− if (address < end && !ref_page)
3272− goto again;
3255+ tlb_remove_page_size(tlb, page, huge_page_size(h));
3256+ /*
3257+ * Bail out after unmapping reference page if supplied
3258+ */
3259+ if (ref_page)
3260+ break;
32733261 }
32743262 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
32753263 tlb_end_vma(tlb, vma);
mm/internal.h+4−0
@@ -36,6 +36,8 @@ · 35 unmodified lines
3636 /* Do not use these with a slab allocator */
3737 #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
3838
39+int do_swap_page(struct fault_env *fe, pte_t orig_pte);
40+
3941 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
4042 unsigned long floor, unsigned long ceiling);
4143
@@ -150,6 +152,8 @@ extern int __isolate_free_page(struct page *page, unsigned int order); · 108 unmodified lines
150152 extern void __free_pages_bootmem(struct page *page, unsigned long pfn,
151153 unsigned int order);
152154 extern void prep_compound_page(struct page *page, unsigned int order);
155+extern void post_alloc_hook(struct page *page, unsigned int order,
156+ gfp_t gfp_flags);
153157 extern int user_min_free_kbytes;
154158
155159 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
mm/khugepaged.c+1922−0
@@ -0,0 +1,1922 @@
1+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2+
3+#include <linux/mm.h>
4+#include <linux/sched.h>
5+#include <linux/mmu_notifier.h>
6+#include <linux/rmap.h>
7+#include <linux/swap.h>
8+#include <linux/mm_inline.h>
9+#include <linux/kthread.h>
10+#include <linux/khugepaged.h>
11+#include <linux/freezer.h>
12+#include <linux/mman.h>
13+#include <linux/hashtable.h>
14+#include <linux/userfaultfd_k.h>
15+#include <linux/page_idle.h>
16+#include <linux/swapops.h>
17+#include <linux/shmem_fs.h>
18+
19+#include <asm/tlb.h>
20+#include <asm/pgalloc.h>
21+#include "internal.h"
22+
23+enum scan_result {
24+ SCAN_FAIL,
25+ SCAN_SUCCEED,
26+ SCAN_PMD_NULL,
27+ SCAN_EXCEED_NONE_PTE,
28+ SCAN_PTE_NON_PRESENT,
29+ SCAN_PAGE_RO,
30+ SCAN_LACK_REFERENCED_PAGE,
31+ SCAN_PAGE_NULL,
32+ SCAN_SCAN_ABORT,
33+ SCAN_PAGE_COUNT,
34+ SCAN_PAGE_LRU,
35+ SCAN_PAGE_LOCK,
36+ SCAN_PAGE_ANON,
37+ SCAN_PAGE_COMPOUND,
38+ SCAN_ANY_PROCESS,
39+ SCAN_VMA_NULL,
40+ SCAN_VMA_CHECK,
41+ SCAN_ADDRESS_RANGE,
42+ SCAN_SWAP_CACHE_PAGE,
43+ SCAN_DEL_PAGE_LRU,
44+ SCAN_ALLOC_HUGE_PAGE_FAIL,
45+ SCAN_CGROUP_CHARGE_FAIL,
46+ SCAN_EXCEED_SWAP_PTE,
47+ SCAN_TRUNCATED,
48+};
49+
50+#define CREATE_TRACE_POINTS
51+#include <trace/events/huge_memory.h>
52+
53+/* default scan 8*512 pte (or vmas) every 30 second */
54+static unsigned int khugepaged_pages_to_scan __read_mostly;
55+static unsigned int khugepaged_pages_collapsed;
56+static unsigned int khugepaged_full_scans;
57+static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
58+/* during fragmentation poll the hugepage allocator once every minute */
59+static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
60+static unsigned long khugepaged_sleep_expire;
61+static DEFINE_SPINLOCK(khugepaged_mm_lock);
62+static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
63+/*
64+ * default collapse hugepages if there is at least one pte mapped like
65+ * it would have happened if the vma was large enough during page
66+ * fault.
67+ */
68+static unsigned int khugepaged_max_ptes_none __read_mostly;
69+static unsigned int khugepaged_max_ptes_swap __read_mostly;
70+
71+#define MM_SLOTS_HASH_BITS 10
72+static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
73+
74+static struct kmem_cache *mm_slot_cache __read_mostly;
75+
76+/**
77+ * struct mm_slot - hash lookup from mm to mm_slot
78+ * @hash: hash collision list
79+ * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
80+ * @mm: the mm that this information is valid for
81+ */
82+struct mm_slot {
83+ struct hlist_node hash;
84+ struct list_head mm_node;
85+ struct mm_struct *mm;
86+};
87+
88+/**
89+ * struct khugepaged_scan - cursor for scanning
90+ * @mm_head: the head of the mm list to scan
91+ * @mm_slot: the current mm_slot we are scanning
92+ * @address: the next address inside that to be scanned
93+ *
94+ * There is only the one khugepaged_scan instance of this cursor structure.
95+ */
96+struct khugepaged_scan {
97+ struct list_head mm_head;
98+ struct mm_slot *mm_slot;
99+ unsigned long address;
100+};
101+
102+static struct khugepaged_scan khugepaged_scan = {
103+ .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
104+};
105+
106+static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
107+ struct kobj_attribute *attr,
108+ char *buf)
109+{
110+ return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
111+}
112+
113+static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
114+ struct kobj_attribute *attr,
115+ const char *buf, size_t count)
116+{
117+ unsigned long msecs;
118+ int err;
119+
120+ err = kstrtoul(buf, 10, &msecs);
121+ if (err || msecs > UINT_MAX)
122+ return -EINVAL;
123+
124+ khugepaged_scan_sleep_millisecs = msecs;
125+ khugepaged_sleep_expire = 0;
126+ wake_up_interruptible(&khugepaged_wait);
127+
128+ return count;
129+}
130+static struct kobj_attribute scan_sleep_millisecs_attr =
131+ __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
132+ scan_sleep_millisecs_store);
133+
134+static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
135+ struct kobj_attribute *attr,
136+ char *buf)
137+{
138+ return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
139+}
140+
141+static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
142+ struct kobj_attribute *attr,
143+ const char *buf, size_t count)
144+{
145+ unsigned long msecs;
146+ int err;
147+
148+ err = kstrtoul(buf, 10, &msecs);
149+ if (err || msecs > UINT_MAX)
150+ return -EINVAL;
151+
152+ khugepaged_alloc_sleep_millisecs = msecs;
153+ khugepaged_sleep_expire = 0;
154+ wake_up_interruptible(&khugepaged_wait);
155+
156+ return count;
157+}
158+static struct kobj_attribute alloc_sleep_millisecs_attr =
159+ __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
160+ alloc_sleep_millisecs_store);
161+
162+static ssize_t pages_to_scan_show(struct kobject *kobj,
163+ struct kobj_attribute *attr,
164+ char *buf)
165+{
166+ return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
167+}
168+static ssize_t pages_to_scan_store(struct kobject *kobj,
169+ struct kobj_attribute *attr,
170+ const char *buf, size_t count)
171+{
172+ int err;
173+ unsigned long pages;
174+
175+ err = kstrtoul(buf, 10, &pages);
176+ if (err || !pages || pages > UINT_MAX)
177+ return -EINVAL;
178+
179+ khugepaged_pages_to_scan = pages;
180+
181+ return count;
182+}
183+static struct kobj_attribute pages_to_scan_attr =
184+ __ATTR(pages_to_scan, 0644, pages_to_scan_show,
185+ pages_to_scan_store);
186+
187+static ssize_t pages_collapsed_show(struct kobject *kobj,
188+ struct kobj_attribute *attr,
189+ char *buf)
190+{
191+ return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
192+}
193+static struct kobj_attribute pages_collapsed_attr =
194+ __ATTR_RO(pages_collapsed);
195+
196+static ssize_t full_scans_show(struct kobject *kobj,
197+ struct kobj_attribute *attr,
198+ char *buf)
199+{
200+ return sprintf(buf, "%u\n", khugepaged_full_scans);
201+}
202+static struct kobj_attribute full_scans_attr =
203+ __ATTR_RO(full_scans);
204+
205+static ssize_t khugepaged_defrag_show(struct kobject *kobj,
206+ struct kobj_attribute *attr, char *buf)
207+{
208+ return single_hugepage_flag_show(kobj, attr, buf,
209+ TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
210+}
211+static ssize_t khugepaged_defrag_store(struct kobject *kobj,
212+ struct kobj_attribute *attr,
213+ const char *buf, size_t count)
214+{
215+ return single_hugepage_flag_store(kobj, attr, buf, count,
216+ TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
217+}
218+static struct kobj_attribute khugepaged_defrag_attr =
219+ __ATTR(defrag, 0644, khugepaged_defrag_show,
220+ khugepaged_defrag_store);
221+
222+/*
223+ * max_ptes_none controls if khugepaged should collapse hugepages over
224+ * any unmapped ptes in turn potentially increasing the memory
225+ * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
226+ * reduce the available free memory in the system as it
227+ * runs. Increasing max_ptes_none will instead potentially reduce the
228+ * free memory in the system during the khugepaged scan.
229+ */
230+static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
231+ struct kobj_attribute *attr,
232+ char *buf)
233+{
234+ return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
235+}
236+static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
237+ struct kobj_attribute *attr,
238+ const char *buf, size_t count)
239+{
240+ int err;
241+ unsigned long max_ptes_none;
242+
243+ err = kstrtoul(buf, 10, &max_ptes_none);
244+ if (err || max_ptes_none > HPAGE_PMD_NR-1)
245+ return -EINVAL;
246+
247+ khugepaged_max_ptes_none = max_ptes_none;
248+
249+ return count;
250+}
251+static struct kobj_attribute khugepaged_max_ptes_none_attr =
252+ __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
253+ khugepaged_max_ptes_none_store);
254+
255+static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
256+ struct kobj_attribute *attr,
257+ char *buf)
258+{
259+ return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
260+}
261+
262+static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
263+ struct kobj_attribute *attr,
264+ const char *buf, size_t count)
265+{
266+ int err;
267+ unsigned long max_ptes_swap;
268+
269+ err = kstrtoul(buf, 10, &max_ptes_swap);
270+ if (err || max_ptes_swap > HPAGE_PMD_NR-1)
271+ return -EINVAL;
272+
273+ khugepaged_max_ptes_swap = max_ptes_swap;
274+
275+ return count;
276+}
277+
278+static struct kobj_attribute khugepaged_max_ptes_swap_attr =
279+ __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
280+ khugepaged_max_ptes_swap_store);
281+
282+static struct attribute *khugepaged_attr[] = {
283+ &khugepaged_defrag_attr.attr,
284+ &khugepaged_max_ptes_none_attr.attr,
285+ &pages_to_scan_attr.attr,
286+ &pages_collapsed_attr.attr,
287+ &full_scans_attr.attr,
288+ &scan_sleep_millisecs_attr.attr,
289+ &alloc_sleep_millisecs_attr.attr,
290+ &khugepaged_max_ptes_swap_attr.attr,
291+ NULL,
292+};
293+
294+struct attribute_group khugepaged_attr_group = {
295+ .attrs = khugepaged_attr,
296+ .name = "khugepaged",
297+};
298+
299+#define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB)
300+
301+int hugepage_madvise(struct vm_area_struct *vma,
302+ unsigned long *vm_flags, int advice)
303+{
304+ switch (advice) {
305+ case MADV_HUGEPAGE:
306+#ifdef CONFIG_S390
307+ /*
308+ * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
309+ * can't handle this properly after s390_enable_sie, so we simply
310+ * ignore the madvise to prevent qemu from causing a SIGSEGV.
311+ */
312+ if (mm_has_pgste(vma->vm_mm))
313+ return 0;
314+#endif
315+ *vm_flags &= ~VM_NOHUGEPAGE;
316+ *vm_flags |= VM_HUGEPAGE;
317+ /*
318+ * If the vma become good for khugepaged to scan,
319+ * register it here without waiting a page fault that
320+ * may not happen any time soon.
321+ */
322+ if (!(*vm_flags & VM_NO_KHUGEPAGED) &&
323+ khugepaged_enter_vma_merge(vma, *vm_flags))
324+ return -ENOMEM;
325+ break;
326+ case MADV_NOHUGEPAGE:
327+ *vm_flags &= ~VM_HUGEPAGE;
328+ *vm_flags |= VM_NOHUGEPAGE;
329+ /*
330+ * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
331+ * this vma even if we leave the mm registered in khugepaged if
332+ * it got registered before VM_NOHUGEPAGE was set.
333+ */
334+ break;
335+ }
336+
337+ return 0;
338+}
339+
340+int __init khugepaged_init(void)
341+{
342+ mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
343+ sizeof(struct mm_slot),
344+ __alignof__(struct mm_slot), 0, NULL);
345+ if (!mm_slot_cache)
346+ return -ENOMEM;
347+
348+ khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
349+ khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
350+ khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
351+
352+ return 0;
353+}
354+
355+void __init khugepaged_destroy(void)
356+{
357+ kmem_cache_destroy(mm_slot_cache);
358+}
359+
360+static inline struct mm_slot *alloc_mm_slot(void)
361+{
362+ if (!mm_slot_cache) /* initialization failed */
363+ return NULL;
364+ return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
365+}
366+
367+static inline void free_mm_slot(struct mm_slot *mm_slot)
368+{
369+ kmem_cache_free(mm_slot_cache, mm_slot);
370+}
371+
372+static struct mm_slot *get_mm_slot(struct mm_struct *mm)
373+{
374+ struct mm_slot *mm_slot;
375+
376+ hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
377+ if (mm == mm_slot->mm)
378+ return mm_slot;
379+
380+ return NULL;
381+}
382+
383+static void insert_to_mm_slots_hash(struct mm_struct *mm,
384+ struct mm_slot *mm_slot)
385+{
386+ mm_slot->mm = mm;
387+ hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
388+}
389+
390+static inline int khugepaged_test_exit(struct mm_struct *mm)
391+{
392+ return atomic_read(&mm->mm_users) == 0;
393+}
394+
395+int __khugepaged_enter(struct mm_struct *mm)
396+{
397+ struct mm_slot *mm_slot;
398+ int wakeup;
399+
400+ mm_slot = alloc_mm_slot();
401+ if (!mm_slot)
402+ return -ENOMEM;
403+
404+ /* __khugepaged_exit() must not run from under us */
405+ VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
406+ if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
407+ free_mm_slot(mm_slot);
408+ return 0;
409+ }
410+
411+ spin_lock(&khugepaged_mm_lock);
412+ insert_to_mm_slots_hash(mm, mm_slot);
413+ /*
414+ * Insert just behind the scanning cursor, to let the area settle
415+ * down a little.
416+ */
417+ wakeup = list_empty(&khugepaged_scan.mm_head);
418+ list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
419+ spin_unlock(&khugepaged_mm_lock);
420+
421+ atomic_inc(&mm->mm_count);
422+ if (wakeup)
423+ wake_up_interruptible(&khugepaged_wait);
424+
425+ return 0;
426+}
427+
428+int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
429+ unsigned long vm_flags)
430+{
431+ unsigned long hstart, hend;
432+ if (!vma->anon_vma)
433+ /*
434+ * Not yet faulted in so we will register later in the
435+ * page fault if needed.
436+ */
437+ return 0;
438+ if (vma->vm_ops || (vm_flags & VM_NO_KHUGEPAGED))
439+ /* khugepaged not yet working on file or special mappings */
440+ return 0;
441+ hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
442+ hend = vma->vm_end & HPAGE_PMD_MASK;
443+ if (hstart < hend)
444+ return khugepaged_enter(vma, vm_flags);
445+ return 0;
446+}
447+
448+void __khugepaged_exit(struct mm_struct *mm)
449+{
450+ struct mm_slot *mm_slot;
451+ int free = 0;
452+
453+ spin_lock(&khugepaged_mm_lock);
454+ mm_slot = get_mm_slot(mm);
455+ if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
456+ hash_del(&mm_slot->hash);
457+ list_del(&mm_slot->mm_node);
458+ free = 1;
459+ }
460+ spin_unlock(&khugepaged_mm_lock);
461+
462+ if (free) {
463+ clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
464+ free_mm_slot(mm_slot);
465+ mmdrop(mm);
466+ } else if (mm_slot) {
467+ /*
468+ * This is required to serialize against
469+ * khugepaged_test_exit() (which is guaranteed to run
470+ * under mmap sem read mode). Stop here (after we
471+ * return all pagetables will be destroyed) until
472+ * khugepaged has finished working on the pagetables
473+ * under the mmap_sem.
474+ */
475+ down_write(&mm->mmap_sem);
476+ up_write(&mm->mmap_sem);
477+ }
478+}
479+
480+static void release_pte_page(struct page *page)
481+{
482+ /* 0 stands for page_is_file_cache(page) == false */
483+ dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
484+ unlock_page(page);
485+ putback_lru_page(page);
486+}
487+
488+static void release_pte_pages(pte_t *pte, pte_t *_pte)
489+{
490+ while (--_pte >= pte) {
491+ pte_t pteval = *_pte;
492+ if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
493+ release_pte_page(pte_page(pteval));
494+ }
495+}
496+
497+static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
498+ unsigned long address,
499+ pte_t *pte)
500+{
501+ struct page *page = NULL;
502+ pte_t *_pte;
503+ int none_or_zero = 0, result = 0, referenced = 0;
504+ bool writable = false;
505+
506+ for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
507+ _pte++, address += PAGE_SIZE) {
508+ pte_t pteval = *_pte;
509+ if (pte_none(pteval) || (pte_present(pteval) &&
510+ is_zero_pfn(pte_pfn(pteval)))) {
511+ if (!userfaultfd_armed(vma) &&
512+ ++none_or_zero <= khugepaged_max_ptes_none) {
513+ continue;
514+ } else {
515+ result = SCAN_EXCEED_NONE_PTE;
516+ goto out;
517+ }
518+ }
519+ if (!pte_present(pteval)) {
520+ result = SCAN_PTE_NON_PRESENT;
521+ goto out;
522+ }
523+ page = vm_normal_page(vma, address, pteval);
524+ if (unlikely(!page)) {
525+ result = SCAN_PAGE_NULL;
526+ goto out;
527+ }
528+
529+ VM_BUG_ON_PAGE(PageCompound(page), page);
530+ VM_BUG_ON_PAGE(!PageAnon(page), page);
531+ VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
532+
533+ /*
534+ * We can do it before isolate_lru_page because the
535+ * page can't be freed from under us. NOTE: PG_lock
536+ * is needed to serialize against split_huge_page
537+ * when invoked from the VM.
538+ */
539+ if (!trylock_page(page)) {
540+ result = SCAN_PAGE_LOCK;
541+ goto out;
542+ }
543+
544+ /*
545+ * cannot use mapcount: can't collapse if there's a gup pin.
546+ * The page must only be referenced by the scanned process
547+ * and page swap cache.
548+ */
549+ if (page_count(page) != 1 + !!PageSwapCache(page)) {
550+ unlock_page(page);
551+ result = SCAN_PAGE_COUNT;
552+ goto out;
553+ }
554+ if (pte_write(pteval)) {
555+ writable = true;
556+ } else {
557+ if (PageSwapCache(page) &&
558+ !reuse_swap_page(page, NULL)) {
559+ unlock_page(page);
560+ result = SCAN_SWAP_CACHE_PAGE;
561+ goto out;
562+ }
563+ /*
564+ * Page is not in the swap cache. It can be collapsed
565+ * into a THP.
566+ */
567+ }
568+
569+ /*
570+ * Isolate the page to avoid collapsing an hugepage
571+ * currently in use by the VM.
572+ */
573+ if (isolate_lru_page(page)) {
574+ unlock_page(page);
575+ result = SCAN_DEL_PAGE_LRU;
576+ goto out;
577+ }
578+ /* 0 stands for page_is_file_cache(page) == false */
579+ inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
580+ VM_BUG_ON_PAGE(!PageLocked(page), page);
581+ VM_BUG_ON_PAGE(PageLRU(page), page);
582+
583+ /* There should be enough young pte to collapse the page */
584+ if (pte_young(pteval) ||
585+ page_is_young(page) || PageReferenced(page) ||
586+ mmu_notifier_test_young(vma->vm_mm, address))
587+ referenced++;
588+ }
589+ if (likely(writable)) {
590+ if (likely(referenced)) {
591+ result = SCAN_SUCCEED;
592+ trace_mm_collapse_huge_page_isolate(page, none_or_zero,
593+ referenced, writable, result);
594+ return 1;
595+ }
596+ } else {
597+ result = SCAN_PAGE_RO;
598+ }
599+
600+out:
601+ release_pte_pages(pte, _pte);
602+ trace_mm_collapse_huge_page_isolate(page, none_or_zero,
603+ referenced, writable, result);
604+ return 0;
605+}
606+
607+static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
608+ struct vm_area_struct *vma,
609+ unsigned long address,
610+ spinlock_t *ptl)
611+{
612+ pte_t *_pte;
613+ for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
614+ pte_t pteval = *_pte;
615+ struct page *src_page;
616+
617+ if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
618+ clear_user_highpage(page, address);
619+ add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
620+ if (is_zero_pfn(pte_pfn(pteval))) {
621+ /*
622+ * ptl mostly unnecessary.
623+ */
624+ spin_lock(ptl);
625+ /*
626+ * paravirt calls inside pte_clear here are
627+ * superfluous.
628+ */
629+ pte_clear(vma->vm_mm, address, _pte);
630+ spin_unlock(ptl);
631+ }
632+ } else {
633+ src_page = pte_page(pteval);
634+ copy_user_highpage(page, src_page, address, vma);
635+ VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
636+ release_pte_page(src_page);
637+ /*
638+ * ptl mostly unnecessary, but preempt has to
639+ * be disabled to update the per-cpu stats
640+ * inside page_remove_rmap().
641+ */
642+ spin_lock(ptl);
643+ /*
644+ * paravirt calls inside pte_clear here are
645+ * superfluous.
646+ */
647+ pte_clear(vma->vm_mm, address, _pte);
648+ page_remove_rmap(src_page, false);
649+ spin_unlock(ptl);
650+ free_page_and_swap_cache(src_page);
651+ }
652+
653+ address += PAGE_SIZE;
654+ page++;
655+ }
656+}
657+
658+static void khugepaged_alloc_sleep(void)
659+{
660+ DEFINE_WAIT(wait);
661+
662+ add_wait_queue(&khugepaged_wait, &wait);
663+ freezable_schedule_timeout_interruptible(
664+ msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
665+ remove_wait_queue(&khugepaged_wait, &wait);
666+}
667+
668+static int khugepaged_node_load[MAX_NUMNODES];
669+
670+static bool khugepaged_scan_abort(int nid)
671+{
672+ int i;
673+
674+ /*
675+ * If zone_reclaim_mode is disabled, then no extra effort is made to
676+ * allocate memory locally.
677+ */
678+ if (!zone_reclaim_mode)
679+ return false;
680+
681+ /* If there is a count for this node already, it must be acceptable */
682+ if (khugepaged_node_load[nid])
683+ return false;
684+
685+ for (i = 0; i < MAX_NUMNODES; i++) {
686+ if (!khugepaged_node_load[i])
687+ continue;
688+ if (node_distance(nid, i) > RECLAIM_DISTANCE)
689+ return true;
690+ }
691+ return false;
692+}
693+
694+/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
695+static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
696+{
697+ return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
698+}
699+
700+#ifdef CONFIG_NUMA
701+static int khugepaged_find_target_node(void)
702+{
703+ static int last_khugepaged_target_node = NUMA_NO_NODE;
704+ int nid, target_node = 0, max_value = 0;
705+
706+ /* find first node with max normal pages hit */
707+ for (nid = 0; nid < MAX_NUMNODES; nid++)
708+ if (khugepaged_node_load[nid] > max_value) {
709+ max_value = khugepaged_node_load[nid];
710+ target_node = nid;
711+ }
712+
713+ /* do some balance if several nodes have the same hit record */
714+ if (target_node <= last_khugepaged_target_node)
715+ for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
716+ nid++)
717+ if (max_value == khugepaged_node_load[nid]) {
718+ target_node = nid;
719+ break;
720+ }
721+
722+ last_khugepaged_target_node = target_node;
723+ return target_node;
724+}
725+
726+static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
727+{
728+ if (IS_ERR(*hpage)) {
729+ if (!*wait)
730+ return false;
731+
732+ *wait = false;
733+ *hpage = NULL;
734+ khugepaged_alloc_sleep();
735+ } else if (*hpage) {
736+ put_page(*hpage);
737+ *hpage = NULL;
738+ }
739+
740+ return true;
741+}
742+
743+static struct page *
744+khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
745+{
746+ VM_BUG_ON_PAGE(*hpage, *hpage);
747+
748+ *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
749+ if (unlikely(!*hpage)) {
750+ count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
751+ *hpage = ERR_PTR(-ENOMEM);
752+ return NULL;
753+ }
754+
755+ prep_transhuge_page(*hpage);
756+ count_vm_event(THP_COLLAPSE_ALLOC);
757+ return *hpage;
758+}
759+#else
760+static int khugepaged_find_target_node(void)
761+{
762+ return 0;
763+}
764+
765+static inline struct page *alloc_khugepaged_hugepage(void)
766+{
767+ struct page *page;
768+
769+ page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
770+ HPAGE_PMD_ORDER);
771+ if (page)
772+ prep_transhuge_page(page);
773+ return page;
774+}
775+
776+static struct page *khugepaged_alloc_hugepage(bool *wait)
777+{
778+ struct page *hpage;
779+
780+ do {
781+ hpage = alloc_khugepaged_hugepage();
782+ if (!hpage) {
783+ count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
784+ if (!*wait)
785+ return NULL;
786+
787+ *wait = false;
788+ khugepaged_alloc_sleep();
789+ } else
790+ count_vm_event(THP_COLLAPSE_ALLOC);
791+ } while (unlikely(!hpage) && likely(khugepaged_enabled()));
792+
793+ return hpage;
794+}
795+
796+static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
797+{
798+ if (!*hpage)
799+ *hpage = khugepaged_alloc_hugepage(wait);
800+
801+ if (unlikely(!*hpage))
802+ return false;
803+
804+ return true;
805+}
806+
807+static struct page *
808+khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
809+{
810+ VM_BUG_ON(!*hpage);
811+
812+ return *hpage;
813+}
814+#endif
815+
816+static bool hugepage_vma_check(struct vm_area_struct *vma)
817+{
818+ if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
819+ (vma->vm_flags & VM_NOHUGEPAGE))
820+ return false;
821+ if (shmem_file(vma->vm_file)) {
822+ if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
823+ return false;
824+ return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
825+ HPAGE_PMD_NR);
826+ }
827+ if (!vma->anon_vma || vma->vm_ops)
828+ return false;
829+ if (is_vma_temporary_stack(vma))
830+ return false;
831+ return !(vma->vm_flags & VM_NO_KHUGEPAGED);
832+}
833+
834+/*
835+ * If mmap_sem temporarily dropped, revalidate vma
836+ * before taking mmap_sem.
837+ * Return 0 if succeeds, otherwise return none-zero
838+ * value (scan code).
839+ */
840+
841+static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address)
842+{
843+ struct vm_area_struct *vma;
844+ unsigned long hstart, hend;
845+
846+ if (unlikely(khugepaged_test_exit(mm)))
847+ return SCAN_ANY_PROCESS;
848+
849+ vma = find_vma(mm, address);
850+ if (!vma)
851+ return SCAN_VMA_NULL;
852+
853+ hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
854+ hend = vma->vm_end & HPAGE_PMD_MASK;
855+ if (address < hstart || address + HPAGE_PMD_SIZE > hend)
856+ return SCAN_ADDRESS_RANGE;
857+ if (!hugepage_vma_check(vma))
858+ return SCAN_VMA_CHECK;
859+ return 0;
860+}
861+
862+/*
863+ * Bring missing pages in from swap, to complete THP collapse.
864+ * Only done if khugepaged_scan_pmd believes it is worthwhile.
865+ *
866+ * Called and returns without pte mapped or spinlocks held,
867+ * but with mmap_sem held to protect against vma changes.
868+ */
869+
870+static bool __collapse_huge_page_swapin(struct mm_struct *mm,
871+ struct vm_area_struct *vma,
872+ unsigned long address, pmd_t *pmd,
873+ int referenced)
874+{
875+ pte_t pteval;
876+ int swapped_in = 0, ret = 0;
877+ struct fault_env fe = {
878+ .vma = vma,
879+ .address = address,
880+ .flags = FAULT_FLAG_ALLOW_RETRY,
881+ .pmd = pmd,
882+ };
883+
884+ fe.pte = pte_offset_map(pmd, address);
885+ for (; fe.address < address + HPAGE_PMD_NR*PAGE_SIZE;
886+ fe.pte++, fe.address += PAGE_SIZE) {
887+ pteval = *fe.pte;
888+ if (!is_swap_pte(pteval))
889+ continue;
890+ swapped_in++;
891+ /* we only decide to swapin, if there is enough young ptes */
892+ if (referenced < HPAGE_PMD_NR/2) {
893+ trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
894+ return false;
895+ }
896+ ret = do_swap_page(&fe, pteval);
897+
898+ /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
899+ if (ret & VM_FAULT_RETRY) {
900+ down_read(&mm->mmap_sem);
901+ if (hugepage_vma_revalidate(mm, address)) {
902+ /* vma is no longer available, don't continue to swapin */
903+ trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
904+ return false;
905+ }
906+ /* check if the pmd is still valid */
907+ if (mm_find_pmd(mm, address) != pmd)
908+ return false;
909+ }
910+ if (ret & VM_FAULT_ERROR) {
911+ trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
912+ return false;
913+ }
914+ /* pte is unmapped now, we need to map it */
915+ fe.pte = pte_offset_map(pmd, fe.address);
916+ }
917+ fe.pte--;
918+ pte_unmap(fe.pte);
919+ trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
920+ return true;
921+}
922+
923+static void collapse_huge_page(struct mm_struct *mm,
924+ unsigned long address,
925+ struct page **hpage,
926+ struct vm_area_struct *vma,
927+ int node, int referenced)
928+{
929+ pmd_t *pmd, _pmd;
930+ pte_t *pte;
931+ pgtable_t pgtable;
932+ struct page *new_page;
933+ spinlock_t *pmd_ptl, *pte_ptl;
934+ int isolated = 0, result = 0;
935+ struct mem_cgroup *memcg;
936+ unsigned long mmun_start; /* For mmu_notifiers */
937+ unsigned long mmun_end; /* For mmu_notifiers */
938+ gfp_t gfp;
939+
940+ VM_BUG_ON(address & ~HPAGE_PMD_MASK);
941+
942+ /* Only allocate from the target node */
943+ gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
944+
945+ /*
946+ * Before allocating the hugepage, release the mmap_sem read lock.
947+ * The allocation can take potentially a long time if it involves
948+ * sync compaction, and we do not need to hold the mmap_sem during
949+ * that. We will recheck the vma after taking it again in write mode.
950+ */
951+ up_read(&mm->mmap_sem);
952+ new_page = khugepaged_alloc_page(hpage, gfp, node);
953+ if (!new_page) {
954+ result = SCAN_ALLOC_HUGE_PAGE_FAIL;
955+ goto out_nolock;
956+ }
957+
958+ if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
959+ result = SCAN_CGROUP_CHARGE_FAIL;
960+ goto out_nolock;
961+ }
962+
963+ down_read(&mm->mmap_sem);
964+ result = hugepage_vma_revalidate(mm, address);
965+ if (result) {
966+ mem_cgroup_cancel_charge(new_page, memcg, true);
967+ up_read(&mm->mmap_sem);
968+ goto out_nolock;
969+ }
970+
971+ pmd = mm_find_pmd(mm, address);
972+ if (!pmd) {
973+ result = SCAN_PMD_NULL;
974+ mem_cgroup_cancel_charge(new_page, memcg, true);
975+ up_read(&mm->mmap_sem);
976+ goto out_nolock;
977+ }
978+
979+ /*
980+ * __collapse_huge_page_swapin always returns with mmap_sem locked.
981+ * If it fails, we release mmap_sem and jump out_nolock.
982+ * Continuing to collapse causes inconsistency.
983+ */
984+ if (!__collapse_huge_page_swapin(mm, vma, address, pmd, referenced)) {
985+ mem_cgroup_cancel_charge(new_page, memcg, true);
986+ up_read(&mm->mmap_sem);
987+ goto out_nolock;
988+ }
989+
990+ up_read(&mm->mmap_sem);
991+ /*
992+ * Prevent all access to pagetables with the exception of
993+ * gup_fast later handled by the ptep_clear_flush and the VM
994+ * handled by the anon_vma lock + PG_lock.
995+ */
996+ down_write(&mm->mmap_sem);
997+ result = hugepage_vma_revalidate(mm, address);
998+ if (result)
999+ goto out;
1000+ /* check if the pmd is still valid */
1001+ if (mm_find_pmd(mm, address) != pmd)
1002+ goto out;
1003+
1004+ anon_vma_lock_write(vma->anon_vma);
1005+
1006+ pte = pte_offset_map(pmd, address);
1007+ pte_ptl = pte_lockptr(mm, pmd);
1008+
1009+ mmun_start = address;
1010+ mmun_end = address + HPAGE_PMD_SIZE;
1011+ mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1012+ pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
1013+ /*
1014+ * After this gup_fast can't run anymore. This also removes
1015+ * any huge TLB entry from the CPU so we won't allow
1016+ * huge and small TLB entries for the same virtual address
1017+ * to avoid the risk of CPU bugs in that area.
1018+ */
1019+ _pmd = pmdp_collapse_flush(vma, address, pmd);
1020+ spin_unlock(pmd_ptl);
1021+ mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1022+
1023+ spin_lock(pte_ptl);
1024+ isolated = __collapse_huge_page_isolate(vma, address, pte);
1025+ spin_unlock(pte_ptl);
1026+
1027+ if (unlikely(!isolated)) {
1028+ pte_unmap(pte);
1029+ spin_lock(pmd_ptl);
1030+ BUG_ON(!pmd_none(*pmd));
1031+ /*
1032+ * We can only use set_pmd_at when establishing
1033+ * hugepmds and never for establishing regular pmds that
1034+ * points to regular pagetables. Use pmd_populate for that
1035+ */
1036+ pmd_populate(mm, pmd, pmd_pgtable(_pmd));
1037+ spin_unlock(pmd_ptl);
1038+ anon_vma_unlock_write(vma->anon_vma);
1039+ result = SCAN_FAIL;
1040+ goto out;
1041+ }
1042+
1043+ /*
1044+ * All pages are isolated and locked so anon_vma rmap
1045+ * can't run anymore.
1046+ */
1047+ anon_vma_unlock_write(vma->anon_vma);
1048+
1049+ __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
1050+ pte_unmap(pte);
1051+ __SetPageUptodate(new_page);
1052+ pgtable = pmd_pgtable(_pmd);
1053+
1054+ _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
1055+ _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
1056+
1057+ /*
1058+ * spin_lock() below is not the equivalent of smp_wmb(), so
1059+ * this is needed to avoid the copy_huge_page writes to become
1060+ * visible after the set_pmd_at() write.
1061+ */
1062+ smp_wmb();
1063+
1064+ spin_lock(pmd_ptl);
1065+ BUG_ON(!pmd_none(*pmd));
1066+ page_add_new_anon_rmap(new_page, vma, address, true);
1067+ mem_cgroup_commit_charge(new_page, memcg, false, true);
1068+ lru_cache_add_active_or_unevictable(new_page, vma);
1069+ pgtable_trans_huge_deposit(mm, pmd, pgtable);
1070+ set_pmd_at(mm, address, pmd, _pmd);
1071+ update_mmu_cache_pmd(vma, address, pmd);
1072+ spin_unlock(pmd_ptl);
1073+
1074+ *hpage = NULL;
1075+
1076+ khugepaged_pages_collapsed++;
1077+ result = SCAN_SUCCEED;
1078+out_up_write:
1079+ up_write(&mm->mmap_sem);
1080+out_nolock:
1081+ trace_mm_collapse_huge_page(mm, isolated, result);
1082+ return;
1083+out:
1084+ mem_cgroup_cancel_charge(new_page, memcg, true);
1085+ goto out_up_write;
1086+}
1087+
1088+static int khugepaged_scan_pmd(struct mm_struct *mm,
1089+ struct vm_area_struct *vma,
1090+ unsigned long address,
1091+ struct page **hpage)
1092+{
1093+ pmd_t *pmd;
1094+ pte_t *pte, *_pte;
1095+ int ret = 0, none_or_zero = 0, result = 0, referenced = 0;
1096+ struct page *page = NULL;
1097+ unsigned long _address;
1098+ spinlock_t *ptl;
1099+ int node = NUMA_NO_NODE, unmapped = 0;
1100+ bool writable = false;
1101+
1102+ VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1103+
1104+ pmd = mm_find_pmd(mm, address);
1105+ if (!pmd) {
1106+ result = SCAN_PMD_NULL;
1107+ goto out;
1108+ }
1109+
1110+ memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
1111+ pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1112+ for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
1113+ _pte++, _address += PAGE_SIZE) {
1114+ pte_t pteval = *_pte;
1115+ if (is_swap_pte(pteval)) {
1116+ if (++unmapped <= khugepaged_max_ptes_swap) {
1117+ continue;
1118+ } else {
1119+ result = SCAN_EXCEED_SWAP_PTE;
1120+ goto out_unmap;
1121+ }
1122+ }
1123+ if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
1124+ if (!userfaultfd_armed(vma) &&
1125+ ++none_or_zero <= khugepaged_max_ptes_none) {
1126+ continue;
1127+ } else {
1128+ result = SCAN_EXCEED_NONE_PTE;
1129+ goto out_unmap;
1130+ }
1131+ }
1132+ if (!pte_present(pteval)) {
1133+ result = SCAN_PTE_NON_PRESENT;
1134+ goto out_unmap;
1135+ }
1136+ if (pte_write(pteval))
1137+ writable = true;
1138+
1139+ page = vm_normal_page(vma, _address, pteval);
1140+ if (unlikely(!page)) {
1141+ result = SCAN_PAGE_NULL;
1142+ goto out_unmap;
1143+ }
1144+
1145+ /* TODO: teach khugepaged to collapse THP mapped with pte */
1146+ if (PageCompound(page)) {
1147+ result = SCAN_PAGE_COMPOUND;
1148+ goto out_unmap;
1149+ }
1150+
1151+ /*
1152+ * Record which node the original page is from and save this
1153+ * information to khugepaged_node_load[].
1154+ * Khupaged will allocate hugepage from the node has the max
1155+ * hit record.
1156+ */
1157+ node = page_to_nid(page);
1158+ if (khugepaged_scan_abort(node)) {
1159+ result = SCAN_SCAN_ABORT;
1160+ goto out_unmap;
1161+ }
1162+ khugepaged_node_load[node]++;
1163+ if (!PageLRU(page)) {
1164+ result = SCAN_PAGE_LRU;
1165+ goto out_unmap;
1166+ }
1167+ if (PageLocked(page)) {
1168+ result = SCAN_PAGE_LOCK;
1169+ goto out_unmap;
1170+ }
1171+ if (!PageAnon(page)) {
1172+ result = SCAN_PAGE_ANON;
1173+ goto out_unmap;
1174+ }
1175+
1176+ /*
1177+ * cannot use mapcount: can't collapse if there's a gup pin.
1178+ * The page must only be referenced by the scanned process
1179+ * and page swap cache.
1180+ */
1181+ if (page_count(page) != 1 + !!PageSwapCache(page)) {
1182+ result = SCAN_PAGE_COUNT;
1183+ goto out_unmap;
1184+ }
1185+ if (pte_young(pteval) ||
1186+ page_is_young(page) || PageReferenced(page) ||
1187+ mmu_notifier_test_young(vma->vm_mm, address))
1188+ referenced++;
1189+ }
1190+ if (writable) {
1191+ if (referenced) {
1192+ result = SCAN_SUCCEED;
1193+ ret = 1;
1194+ } else {
1195+ result = SCAN_LACK_REFERENCED_PAGE;
1196+ }
1197+ } else {
1198+ result = SCAN_PAGE_RO;
1199+ }
1200+out_unmap:
1201+ pte_unmap_unlock(pte, ptl);
1202+ if (ret) {
1203+ node = khugepaged_find_target_node();
1204+ /* collapse_huge_page will return with the mmap_sem released */
1205+ collapse_huge_page(mm, address, hpage, vma, node, referenced);
1206+ }
1207+out:
1208+ trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
1209+ none_or_zero, result, unmapped);
1210+ return ret;
1211+}
1212+
1213+static void collect_mm_slot(struct mm_slot *mm_slot)
1214+{
1215+ struct mm_struct *mm = mm_slot->mm;
1216+
1217+ VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
1218+
1219+ if (khugepaged_test_exit(mm)) {
1220+ /* free mm_slot */
1221+ hash_del(&mm_slot->hash);
1222+ list_del(&mm_slot->mm_node);
1223+
1224+ /*
1225+ * Not strictly needed because the mm exited already.
1226+ *
1227+ * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1228+ */
1229+
1230+ /* khugepaged_mm_lock actually not necessary for the below */
1231+ free_mm_slot(mm_slot);
1232+ mmdrop(mm);
1233+ }
1234+}
1235+
1236+#if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
1237+static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
1238+{
1239+ struct vm_area_struct *vma;
1240+ unsigned long addr;
1241+ pmd_t *pmd, _pmd;
1242+
1243+ i_mmap_lock_write(mapping);
1244+ vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
1245+ /* probably overkill */
1246+ if (vma->anon_vma)
1247+ continue;
1248+ addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
1249+ if (addr & ~HPAGE_PMD_MASK)
1250+ continue;
1251+ if (vma->vm_end < addr + HPAGE_PMD_SIZE)
1252+ continue;
1253+ pmd = mm_find_pmd(vma->vm_mm, addr);
1254+ if (!pmd)
1255+ continue;
1256+ /*
1257+ * We need exclusive mmap_sem to retract page table.
1258+ * If trylock fails we would end up with pte-mapped THP after
1259+ * re-fault. Not ideal, but it's more important to not disturb
1260+ * the system too much.
1261+ */
1262+ if (down_write_trylock(&vma->vm_mm->mmap_sem)) {
1263+ spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd);
1264+ /* assume page table is clear */
1265+ _pmd = pmdp_collapse_flush(vma, addr, pmd);
1266+ spin_unlock(ptl);
1267+ up_write(&vma->vm_mm->mmap_sem);
1268+ atomic_long_dec(&vma->vm_mm->nr_ptes);
1269+ pte_free(vma->vm_mm, pmd_pgtable(_pmd));
1270+ }
1271+ }
1272+ i_mmap_unlock_write(mapping);
1273+}
1274+
1275+/**
1276+ * collapse_shmem - collapse small tmpfs/shmem pages into huge one.
1277+ *
1278+ * Basic scheme is simple, details are more complex:
1279+ * - allocate and freeze a new huge page;
1280+ * - scan over radix tree replacing old pages the new one
1281+ * + swap in pages if necessary;
1282+ * + fill in gaps;
1283+ * + keep old pages around in case if rollback is required;
1284+ * - if replacing succeed:
1285+ * + copy data over;
1286+ * + free old pages;
1287+ * + unfreeze huge page;
1288+ * - if replacing failed;
1289+ * + put all pages back and unfreeze them;
1290+ * + restore gaps in the radix-tree;
1291+ * + free huge page;
1292+ */
1293+static void collapse_shmem(struct mm_struct *mm,
1294+ struct address_space *mapping, pgoff_t start,
1295+ struct page **hpage, int node)
1296+{
1297+ gfp_t gfp;
1298+ struct page *page, *new_page, *tmp;
1299+ struct mem_cgroup *memcg;
1300+ pgoff_t index, end = start + HPAGE_PMD_NR;
1301+ LIST_HEAD(pagelist);
1302+ struct radix_tree_iter iter;
1303+ void **slot;
1304+ int nr_none = 0, result = SCAN_SUCCEED;
1305+
1306+ VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
1307+
1308+ /* Only allocate from the target node */
1309+ gfp = alloc_hugepage_khugepaged_gfpmask() |
1310+ __GFP_OTHER_NODE | __GFP_THISNODE;
1311+
1312+ new_page = khugepaged_alloc_page(hpage, gfp, node);
1313+ if (!new_page) {
1314+ result = SCAN_ALLOC_HUGE_PAGE_FAIL;
1315+ goto out;
1316+ }
1317+
1318+ if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
1319+ result = SCAN_CGROUP_CHARGE_FAIL;
1320+ goto out;
1321+ }
1322+
1323+ new_page->index = start;
1324+ new_page->mapping = mapping;
1325+ __SetPageSwapBacked(new_page);
1326+ __SetPageLocked(new_page);
1327+ BUG_ON(!page_ref_freeze(new_page, 1));
1328+
1329+
1330+ /*
1331+ * At this point the new_page is 'frozen' (page_count() is zero), locked
1332+ * and not up-to-date. It's safe to insert it into radix tree, because
1333+ * nobody would be able to map it or use it in other way until we
1334+ * unfreeze it.
1335+ */
1336+
1337+ index = start;
1338+ spin_lock_irq(&mapping->tree_lock);
1339+ radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1340+ int n = min(iter.index, end) - index;
1341+
1342+ /*
1343+ * Handle holes in the radix tree: charge it from shmem and
1344+ * insert relevant subpage of new_page into the radix-tree.
1345+ */
1346+ if (n && !shmem_charge(mapping->host, n)) {
1347+ result = SCAN_FAIL;
1348+ break;
1349+ }
1350+ nr_none += n;
1351+ for (; index < min(iter.index, end); index++) {
1352+ radix_tree_insert(&mapping->page_tree, index,
1353+ new_page + (index % HPAGE_PMD_NR));
1354+ }
1355+
1356+ /* We are done. */
1357+ if (index >= end)
1358+ break;
1359+
1360+ page = radix_tree_deref_slot_protected(slot,
1361+ &mapping->tree_lock);
1362+ if (radix_tree_exceptional_entry(page) || !PageUptodate(page)) {
1363+ spin_unlock_irq(&mapping->tree_lock);
1364+ /* swap in or instantiate fallocated page */
1365+ if (shmem_getpage(mapping->host, index, &page,
1366+ SGP_NOHUGE)) {
1367+ result = SCAN_FAIL;
1368+ goto tree_unlocked;
1369+ }
1370+ spin_lock_irq(&mapping->tree_lock);
1371+ } else if (trylock_page(page)) {
1372+ get_page(page);
1373+ } else {
1374+ result = SCAN_PAGE_LOCK;
1375+ break;
1376+ }
1377+
1378+ /*
1379+ * The page must be locked, so we can drop the tree_lock
1380+ * without racing with truncate.
1381+ */
1382+ VM_BUG_ON_PAGE(!PageLocked(page), page);
1383+ VM_BUG_ON_PAGE(!PageUptodate(page), page);
1384+ VM_BUG_ON_PAGE(PageTransCompound(page), page);
1385+
1386+ if (page_mapping(page) != mapping) {
1387+ result = SCAN_TRUNCATED;
1388+ goto out_unlock;
1389+ }
1390+ spin_unlock_irq(&mapping->tree_lock);
1391+
1392+ if (isolate_lru_page(page)) {
1393+ result = SCAN_DEL_PAGE_LRU;
1394+ goto out_isolate_failed;
1395+ }
1396+
1397+ if (page_mapped(page))
1398+ unmap_mapping_range(mapping, index << PAGE_SHIFT,
1399+ PAGE_SIZE, 0);
1400+
1401+ spin_lock_irq(&mapping->tree_lock);
1402+
1403+ VM_BUG_ON_PAGE(page_mapped(page), page);
1404+
1405+ /*
1406+ * The page is expected to have page_count() == 3:
1407+ * - we hold a pin on it;
1408+ * - one reference from radix tree;
1409+ * - one from isolate_lru_page;
1410+ */
1411+ if (!page_ref_freeze(page, 3)) {
1412+ result = SCAN_PAGE_COUNT;
1413+ goto out_lru;
1414+ }
1415+
1416+ /*
1417+ * Add the page to the list to be able to undo the collapse if
1418+ * something go wrong.
1419+ */
1420+ list_add_tail(&page->lru, &pagelist);
1421+
1422+ /* Finally, replace with the new page. */
1423+ radix_tree_replace_slot(slot,
1424+ new_page + (index % HPAGE_PMD_NR));
1425+
1426+ index++;
1427+ continue;
1428+out_lru:
1429+ spin_unlock_irq(&mapping->tree_lock);
1430+ putback_lru_page(page);
1431+out_isolate_failed:
1432+ unlock_page(page);
1433+ put_page(page);
1434+ goto tree_unlocked;
1435+out_unlock:
1436+ unlock_page(page);
1437+ put_page(page);
1438+ break;
1439+ }
1440+
1441+ /*
1442+ * Handle hole in radix tree at the end of the range.
1443+ * This code only triggers if there's nothing in radix tree
1444+ * beyond 'end'.
1445+ */
1446+ if (result == SCAN_SUCCEED && index < end) {
1447+ int n = end - index;
1448+
1449+ if (!shmem_charge(mapping->host, n)) {
1450+ result = SCAN_FAIL;
1451+ goto tree_locked;
1452+ }
1453+
1454+ for (; index < end; index++) {
1455+ radix_tree_insert(&mapping->page_tree, index,
1456+ new_page + (index % HPAGE_PMD_NR));
1457+ }
1458+ nr_none += n;
1459+ }
1460+
1461+tree_locked:
1462+ spin_unlock_irq(&mapping->tree_lock);
1463+tree_unlocked:
1464+
1465+ if (result == SCAN_SUCCEED) {
1466+ unsigned long flags;
1467+ struct zone *zone = page_zone(new_page);
1468+
1469+ /*
1470+ * Replacing old pages with new one has succeed, now we need to
1471+ * copy the content and free old pages.
1472+ */
1473+ list_for_each_entry_safe(page, tmp, &pagelist, lru) {
1474+ copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
1475+ page);
1476+ list_del(&page->lru);
1477+ unlock_page(page);
1478+ page_ref_unfreeze(page, 1);
1479+ page->mapping = NULL;
1480+ ClearPageActive(page);
1481+ ClearPageUnevictable(page);
1482+ put_page(page);
1483+ }
1484+
1485+ local_irq_save(flags);
1486+ __inc_zone_page_state(new_page, NR_SHMEM_THPS);
1487+ if (nr_none) {
1488+ __mod_zone_page_state(zone, NR_FILE_PAGES, nr_none);
1489+ __mod_zone_page_state(zone, NR_SHMEM, nr_none);
1490+ }
1491+ local_irq_restore(flags);
1492+
1493+ /*
1494+ * Remove pte page tables, so we can re-faulti
1495+ * the page as huge.
1496+ */
1497+ retract_page_tables(mapping, start);
1498+
1499+ /* Everything is ready, let's unfreeze the new_page */
1500+ set_page_dirty(new_page);
1501+ SetPageUptodate(new_page);
1502+ page_ref_unfreeze(new_page, HPAGE_PMD_NR);
1503+ mem_cgroup_commit_charge(new_page, memcg, false, true);
1504+ lru_cache_add_anon(new_page);
1505+ unlock_page(new_page);
1506+
1507+ *hpage = NULL;
1508+ } else {
1509+ /* Something went wrong: rollback changes to the radix-tree */
1510+ shmem_uncharge(mapping->host, nr_none);
1511+ spin_lock_irq(&mapping->tree_lock);
1512+ radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
1513+ start) {
1514+ if (iter.index >= end)
1515+ break;
1516+ page = list_first_entry_or_null(&pagelist,
1517+ struct page, lru);
1518+ if (!page || iter.index < page->index) {
1519+ if (!nr_none)
1520+ break;
1521+ /* Put holes back where they were */
1522+ radix_tree_replace_slot(slot, NULL);
1523+ nr_none--;
1524+ continue;
1525+ }
1526+
1527+ VM_BUG_ON_PAGE(page->index != iter.index, page);
1528+
1529+ /* Unfreeze the page. */
1530+ list_del(&page->lru);
1531+ page_ref_unfreeze(page, 2);
1532+ radix_tree_replace_slot(slot, page);
1533+ spin_unlock_irq(&mapping->tree_lock);
1534+ putback_lru_page(page);
1535+ unlock_page(page);
1536+ spin_lock_irq(&mapping->tree_lock);
1537+ }
1538+ VM_BUG_ON(nr_none);
1539+ spin_unlock_irq(&mapping->tree_lock);
1540+
1541+ /* Unfreeze new_page, caller would take care about freeing it */
1542+ page_ref_unfreeze(new_page, 1);
1543+ mem_cgroup_cancel_charge(new_page, memcg, true);
1544+ unlock_page(new_page);
1545+ new_page->mapping = NULL;
1546+ }
1547+out:
1548+ VM_BUG_ON(!list_empty(&pagelist));
1549+ /* TODO: tracepoints */
1550+}
1551+
1552+static void khugepaged_scan_shmem(struct mm_struct *mm,
1553+ struct address_space *mapping,
1554+ pgoff_t start, struct page **hpage)
1555+{
1556+ struct page *page = NULL;
1557+ struct radix_tree_iter iter;
1558+ void **slot;
1559+ int present, swap;
1560+ int node = NUMA_NO_NODE;
1561+ int result = SCAN_SUCCEED;
1562+
1563+ present = 0;
1564+ swap = 0;
1565+ memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
1566+ rcu_read_lock();
1567+ radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1568+ if (iter.index >= start + HPAGE_PMD_NR)
1569+ break;
1570+
1571+ page = radix_tree_deref_slot(slot);
1572+ if (radix_tree_deref_retry(page)) {
1573+ slot = radix_tree_iter_retry(&iter);
1574+ continue;
1575+ }
1576+
1577+ if (radix_tree_exception(page)) {
1578+ if (++swap > khugepaged_max_ptes_swap) {
1579+ result = SCAN_EXCEED_SWAP_PTE;
1580+ break;
1581+ }
1582+ continue;
1583+ }
1584+
1585+ if (PageTransCompound(page)) {
1586+ result = SCAN_PAGE_COMPOUND;
1587+ break;
1588+ }
1589+
1590+ node = page_to_nid(page);
1591+ if (khugepaged_scan_abort(node)) {
1592+ result = SCAN_SCAN_ABORT;
1593+ break;
1594+ }
1595+ khugepaged_node_load[node]++;
1596+
1597+ if (!PageLRU(page)) {
1598+ result = SCAN_PAGE_LRU;
1599+ break;
1600+ }
1601+
1602+ if (page_count(page) != 1 + page_mapcount(page)) {
1603+ result = SCAN_PAGE_COUNT;
1604+ break;
1605+ }
1606+
1607+ /*
1608+ * We probably should check if the page is referenced here, but
1609+ * nobody would transfer pte_young() to PageReferenced() for us.
1610+ * And rmap walk here is just too costly...
1611+ */
1612+
1613+ present++;
1614+
1615+ if (need_resched()) {
1616+ cond_resched_rcu();
1617+ slot = radix_tree_iter_next(&iter);
1618+ }
1619+ }
1620+ rcu_read_unlock();
1621+
1622+ if (result == SCAN_SUCCEED) {
1623+ if (present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
1624+ result = SCAN_EXCEED_NONE_PTE;
1625+ } else {
1626+ node = khugepaged_find_target_node();
1627+ collapse_shmem(mm, mapping, start, hpage, node);
1628+ }
1629+ }
1630+
1631+ /* TODO: tracepoints */
1632+}
1633+#else
1634+static void khugepaged_scan_shmem(struct mm_struct *mm,
1635+ struct address_space *mapping,
1636+ pgoff_t start, struct page **hpage)
1637+{
1638+ BUILD_BUG();
1639+}
1640+#endif
1641+
1642+static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
1643+ struct page **hpage)
1644+ __releases(&khugepaged_mm_lock)
1645+ __acquires(&khugepaged_mm_lock)
1646+{
1647+ struct mm_slot *mm_slot;
1648+ struct mm_struct *mm;
1649+ struct vm_area_struct *vma;
1650+ int progress = 0;
1651+
1652+ VM_BUG_ON(!pages);
1653+ VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
1654+
1655+ if (khugepaged_scan.mm_slot)
1656+ mm_slot = khugepaged_scan.mm_slot;
1657+ else {
1658+ mm_slot = list_entry(khugepaged_scan.mm_head.next,
1659+ struct mm_slot, mm_node);
1660+ khugepaged_scan.address = 0;
1661+ khugepaged_scan.mm_slot = mm_slot;
1662+ }
1663+ spin_unlock(&khugepaged_mm_lock);
1664+
1665+ mm = mm_slot->mm;
1666+ down_read(&mm->mmap_sem);
1667+ if (unlikely(khugepaged_test_exit(mm)))
1668+ vma = NULL;
1669+ else
1670+ vma = find_vma(mm, khugepaged_scan.address);
1671+
1672+ progress++;
1673+ for (; vma; vma = vma->vm_next) {
1674+ unsigned long hstart, hend;
1675+
1676+ cond_resched();
1677+ if (unlikely(khugepaged_test_exit(mm))) {
1678+ progress++;
1679+ break;
1680+ }
1681+ if (!hugepage_vma_check(vma)) {
1682+skip:
1683+ progress++;
1684+ continue;
1685+ }
1686+ hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1687+ hend = vma->vm_end & HPAGE_PMD_MASK;
1688+ if (hstart >= hend)
1689+ goto skip;
1690+ if (khugepaged_scan.address > hend)
1691+ goto skip;
1692+ if (khugepaged_scan.address < hstart)
1693+ khugepaged_scan.address = hstart;
1694+ VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
1695+
1696+ while (khugepaged_scan.address < hend) {
1697+ int ret;
1698+ cond_resched();
1699+ if (unlikely(khugepaged_test_exit(mm)))
1700+ goto breakouterloop;
1701+
1702+ VM_BUG_ON(khugepaged_scan.address < hstart ||
1703+ khugepaged_scan.address + HPAGE_PMD_SIZE >
1704+ hend);
1705+ if (shmem_file(vma->vm_file)) {
1706+ struct file *file;
1707+ pgoff_t pgoff = linear_page_index(vma,
1708+ khugepaged_scan.address);
1709+ if (!shmem_huge_enabled(vma))
1710+ goto skip;
1711+ file = get_file(vma->vm_file);
1712+ up_read(&mm->mmap_sem);
1713+ ret = 1;
1714+ khugepaged_scan_shmem(mm, file->f_mapping,
1715+ pgoff, hpage);
1716+ fput(file);
1717+ } else {
1718+ ret = khugepaged_scan_pmd(mm, vma,
1719+ khugepaged_scan.address,
1720+ hpage);
1721+ }
1722+ /* move to next address */
1723+ khugepaged_scan.address += HPAGE_PMD_SIZE;
1724+ progress += HPAGE_PMD_NR;
1725+ if (ret)
1726+ /* we released mmap_sem so break loop */
1727+ goto breakouterloop_mmap_sem;
1728+ if (progress >= pages)
1729+ goto breakouterloop;
1730+ }
1731+ }
1732+breakouterloop:
1733+ up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
1734+breakouterloop_mmap_sem:
1735+
1736+ spin_lock(&khugepaged_mm_lock);
1737+ VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
1738+ /*
1739+ * Release the current mm_slot if this mm is about to die, or
1740+ * if we scanned all vmas of this mm.
1741+ */
1742+ if (khugepaged_test_exit(mm) || !vma) {
1743+ /*
1744+ * Make sure that if mm_users is reaching zero while
1745+ * khugepaged runs here, khugepaged_exit will find
1746+ * mm_slot not pointing to the exiting mm.
1747+ */
1748+ if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
1749+ khugepaged_scan.mm_slot = list_entry(
1750+ mm_slot->mm_node.next,
1751+ struct mm_slot, mm_node);
1752+ khugepaged_scan.address = 0;
1753+ } else {
1754+ khugepaged_scan.mm_slot = NULL;
1755+ khugepaged_full_scans++;
1756+ }
1757+
1758+ collect_mm_slot(mm_slot);
1759+ }
1760+
1761+ return progress;
1762+}
1763+
1764+static int khugepaged_has_work(void)
1765+{
1766+ return !list_empty(&khugepaged_scan.mm_head) &&
1767+ khugepaged_enabled();
1768+}
1769+
1770+static int khugepaged_wait_event(void)
1771+{
1772+ return !list_empty(&khugepaged_scan.mm_head) ||
1773+ kthread_should_stop();
1774+}
1775+
1776+static void khugepaged_do_scan(void)
1777+{
1778+ struct page *hpage = NULL;
1779+ unsigned int progress = 0, pass_through_head = 0;
1780+ unsigned int pages = khugepaged_pages_to_scan;
1781+ bool wait = true;
1782+
1783+ barrier(); /* write khugepaged_pages_to_scan to local stack */
1784+
1785+ while (progress < pages) {
1786+ if (!khugepaged_prealloc_page(&hpage, &wait))
1787+ break;
1788+
1789+ cond_resched();
1790+
1791+ if (unlikely(kthread_should_stop() || try_to_freeze()))
1792+ break;
1793+
1794+ spin_lock(&khugepaged_mm_lock);
1795+ if (!khugepaged_scan.mm_slot)
1796+ pass_through_head++;
1797+ if (khugepaged_has_work() &&
1798+ pass_through_head < 2)
1799+ progress += khugepaged_scan_mm_slot(pages - progress,
1800+ &hpage);
1801+ else
1802+ progress = pages;
1803+ spin_unlock(&khugepaged_mm_lock);
1804+ }
1805+
1806+ if (!IS_ERR_OR_NULL(hpage))
1807+ put_page(hpage);
1808+}
1809+
1810+static bool khugepaged_should_wakeup(void)
1811+{
1812+ return kthread_should_stop() ||
1813+ time_after_eq(jiffies, khugepaged_sleep_expire);
1814+}
1815+
1816+static void khugepaged_wait_work(void)
1817+{
1818+ if (khugepaged_has_work()) {
1819+ const unsigned long scan_sleep_jiffies =
1820+ msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
1821+
1822+ if (!scan_sleep_jiffies)
1823+ return;
1824+
1825+ khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
1826+ wait_event_freezable_timeout(khugepaged_wait,
1827+ khugepaged_should_wakeup(),
1828+ scan_sleep_jiffies);
1829+ return;
1830+ }
1831+
1832+ if (khugepaged_enabled())
1833+ wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
1834+}
1835+
1836+static int khugepaged(void *none)
1837+{
1838+ struct mm_slot *mm_slot;
1839+
1840+ set_freezable();
1841+ set_user_nice(current, MAX_NICE);
1842+
1843+ while (!kthread_should_stop()) {
1844+ khugepaged_do_scan();
1845+ khugepaged_wait_work();
1846+ }
1847+
1848+ spin_lock(&khugepaged_mm_lock);
1849+ mm_slot = khugepaged_scan.mm_slot;
1850+ khugepaged_scan.mm_slot = NULL;
1851+ if (mm_slot)
1852+ collect_mm_slot(mm_slot);
1853+ spin_unlock(&khugepaged_mm_lock);
1854+ return 0;
1855+}
1856+
1857+static void set_recommended_min_free_kbytes(void)
1858+{
1859+ struct zone *zone;
1860+ int nr_zones = 0;
1861+ unsigned long recommended_min;
1862+
1863+ for_each_populated_zone(zone)
1864+ nr_zones++;
1865+
1866+ /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
1867+ recommended_min = pageblock_nr_pages * nr_zones * 2;
1868+
1869+ /*
1870+ * Make sure that on average at least two pageblocks are almost free
1871+ * of another type, one for a migratetype to fall back to and a
1872+ * second to avoid subsequent fallbacks of other types There are 3
1873+ * MIGRATE_TYPES we care about.
1874+ */
1875+ recommended_min += pageblock_nr_pages * nr_zones *
1876+ MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
1877+
1878+ /* don't ever allow to reserve more than 5% of the lowmem */
1879+ recommended_min = min(recommended_min,
1880+ (unsigned long) nr_free_buffer_pages() / 20);
1881+ recommended_min <<= (PAGE_SHIFT-10);
1882+
1883+ if (recommended_min > min_free_kbytes) {
1884+ if (user_min_free_kbytes >= 0)
1885+ pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
1886+ min_free_kbytes, recommended_min);
1887+
1888+ min_free_kbytes = recommended_min;
1889+ }
1890+ setup_per_zone_wmarks();
1891+}
1892+
1893+int start_stop_khugepaged(void)
1894+{
1895+ static struct task_struct *khugepaged_thread __read_mostly;
1896+ static DEFINE_MUTEX(khugepaged_mutex);
1897+ int err = 0;
1898+
1899+ mutex_lock(&khugepaged_mutex);
1900+ if (khugepaged_enabled()) {
1901+ if (!khugepaged_thread)
1902+ khugepaged_thread = kthread_run(khugepaged, NULL,
1903+ "khugepaged");
1904+ if (IS_ERR(khugepaged_thread)) {
1905+ pr_err("khugepaged: kthread_run(khugepaged) failed\n");
1906+ err = PTR_ERR(khugepaged_thread);
1907+ khugepaged_thread = NULL;
1908+ goto fail;
1909+ }
1910+
1911+ if (!list_empty(&khugepaged_scan.mm_head))
1912+ wake_up_interruptible(&khugepaged_wait);
1913+
1914+ set_recommended_min_free_kbytes();
1915+ } else if (khugepaged_thread) {
1916+ kthread_stop(khugepaged_thread);
1917+ khugepaged_thread = NULL;
1918+ }
1919+fail:
1920+ mutex_unlock(&khugepaged_mutex);
1921+ return err;
1922+}
mm/ksm.c+4−5
@@ -376,9 +376,8 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr) · 375 unmodified lines
376376 if (IS_ERR_OR_NULL(page))
377377 break;
378378 if (PageKsm(page))
379− ret = handle_mm_fault(vma->vm_mm, vma, addr,
380− FAULT_FLAG_WRITE |
381− FAULT_FLAG_REMOTE);
379+ ret = handle_mm_fault(vma, addr,
380+ FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE);
382381 else
383382 ret = VM_FAULT_WRITE;
384383 put_page(page);
@@ -532,8 +531,8 @@ static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it) · 147 unmodified lines
532531 void *expected_mapping;
533532 unsigned long kpfn;
534533
535− expected_mapping = (void *)stable_node +
536− (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
534+ expected_mapping = (void *)((unsigned long)stable_node |
535+ PAGE_MAPPING_KSM);
537536 again:
538537 kpfn = READ_ONCE(stable_node->kpfn);
539538 page = pfn_to_page(kpfn);
mm/memblock.c+3−0
@@ -584,6 +584,9 @@ repeat: · 583 unmodified lines
584584 nid, flags);
585585 }
586586
587+ if (!nr_new)
588+ return 0;
589+
587590 /*
588591 * If this was the first round, resize array and repeat for actual
589592 * insertions; otherwise, merge and return.
mm/memcontrol.c+89−45
@@ -1259,6 +1259,7 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · 1258 unmodified lines
12591259 struct oom_control oc = {
12601260 .zonelist = NULL,
12611261 .nodemask = NULL,
1262+ .memcg = memcg,
12621263 .gfp_mask = gfp_mask,
12631264 .order = order,
12641265 };
@@ -1281,7 +1282,7 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · 16 unmodified lines
12811282 goto unlock;
12821283 }
12831284
1284− check_panic_on_oom(&oc, CONSTRAINT_MEMCG, memcg);
1285+ check_panic_on_oom(&oc, CONSTRAINT_MEMCG);
12851286 totalpages = mem_cgroup_get_limit(memcg) ? : 1;
12861287 for_each_mem_cgroup_tree(iter, memcg) {
12871288 struct css_task_iter it;
@@ -1289,7 +1290,7 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · 1 unmodified line
12891290
12901291 css_task_iter_start(&iter->css, &it);
12911292 while ((task = css_task_iter_next(&it))) {
1292− switch (oom_scan_process_thread(&oc, task, totalpages)) {
1293+ switch (oom_scan_process_thread(&oc, task)) {
12931294 case OOM_SCAN_SELECT:
12941295 if (chosen)
12951296 put_task_struct(chosen);
@@ -1329,7 +1330,7 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · 33 unmodified lines
13291330
13301331 if (chosen) {
13311332 points = chosen_points * 1000 / totalpages;
1332− oom_kill_process(&oc, chosen, points, totalpages, memcg,
1333+ oom_kill_process(&oc, chosen, points, totalpages,
13331334 "Memory cgroup out of memory");
13341335 }
13351336 unlock:
@@ -2272,20 +2273,30 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · 936 unmodified lines
22722273 current->memcg_kmem_skip_account = 0;
22732274 }
22742275
2275−/*
2276+static inline bool memcg_kmem_bypass(void)
2277+{
2278+ if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
2279+ return true;
2280+ return false;
2281+}
2282+
2283+/**
2284+ * memcg_kmem_get_cache: select the correct per-memcg cache for allocation
2285+ * @cachep: the original global kmem cache
2286+ *
22762287 * Return the kmem_cache we're supposed to use for a slab allocation.
22772288 * We try to use the current memcg's version of the cache.
22782289 *
2279− * If the cache does not exist yet, if we are the first user of it,
2280− * we either create it immediately, if possible, or create it asynchronously
2281− * in a workqueue.
2282− * In the latter case, we will let the current allocation go through with
2283− * the original cache.
2290+ * If the cache does not exist yet, if we are the first user of it, we
2291+ * create it asynchronously in a workqueue and let the current allocation
2292+ * go through with the original cache.
22842293 *
2285− * Can't be called in interrupt context or from kernel threads.
2286− * This function needs to be called with rcu_read_lock() held.
2294+ * This function takes a reference to the cache it returns to assure it
2295+ * won't get destroyed while we are working with it. Once the caller is
2296+ * done with it, memcg_kmem_put_cache() must be called to release the
2297+ * reference.
22872298 */
2288−struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
2299+struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep)
22892300 {
22902301 struct mem_cgroup *memcg;
22912302 struct kmem_cache *memcg_cachep;
@@ -2293,10 +2304,7 @@ struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) · 1 unmodified line
22932304
22942305 VM_BUG_ON(!is_root_cache(cachep));
22952306
2296− if (cachep->flags & SLAB_ACCOUNT)
2297− gfp |= __GFP_ACCOUNT;
2298−
2299− if (!(gfp & __GFP_ACCOUNT))
2307+ if (memcg_kmem_bypass())
23002308 return cachep;
23012309
23022310 if (current->memcg_kmem_skip_account)
@@ -2329,14 +2337,27 @@ out: · 26 unmodified lines
23292337 return cachep;
23302338 }
23312339
2332−void __memcg_kmem_put_cache(struct kmem_cache *cachep)
2340+/**
2341+ * memcg_kmem_put_cache: drop reference taken by memcg_kmem_get_cache
2342+ * @cachep: the cache returned by memcg_kmem_get_cache
2343+ */
2344+void memcg_kmem_put_cache(struct kmem_cache *cachep)
23332345 {
23342346 if (!is_root_cache(cachep))
23352347 css_put(&cachep->memcg_params.memcg->css);
23362348 }
23372349
2338−int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
2339− struct mem_cgroup *memcg)
2350+/**
2351+ * memcg_kmem_charge: charge a kmem page
2352+ * @page: page to charge
2353+ * @gfp: reclaim mode
2354+ * @order: allocation order
2355+ * @memcg: memory cgroup to charge
2356+ *
2357+ * Returns 0 on success, an error code on failure.
2358+ */
2359+int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
2360+ struct mem_cgroup *memcg)
23402361 {
23412362 unsigned int nr_pages = 1 << order;
23422363 struct page_counter *counter;
@@ -2357,19 +2378,34 @@ int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order, · 14 unmodified lines
23572378 return 0;
23582379 }
23592380
2360−int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
2381+/**
2382+ * memcg_kmem_charge: charge a kmem page to the current memory cgroup
2383+ * @page: page to charge
2384+ * @gfp: reclaim mode
2385+ * @order: allocation order
2386+ *
2387+ * Returns 0 on success, an error code on failure.
2388+ */
2389+int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
23612390 {
23622391 struct mem_cgroup *memcg;
23632392 int ret = 0;
23642393
2394+ if (memcg_kmem_bypass())
2395+ return 0;
2396+
23652397 memcg = get_mem_cgroup_from_mm(current->mm);
23662398 if (!mem_cgroup_is_root(memcg))
2367− ret = __memcg_kmem_charge_memcg(page, gfp, order, memcg);
2399+ ret = memcg_kmem_charge_memcg(page, gfp, order, memcg);
23682400 css_put(&memcg->css);
23692401 return ret;
23702402 }
2371−
2372−void __memcg_kmem_uncharge(struct page *page, int order)
2403+/**
2404+ * memcg_kmem_uncharge: uncharge a kmem page
2405+ * @page: page to uncharge
2406+ * @order: allocation order
2407+ */
2408+void memcg_kmem_uncharge(struct page *page, int order)
23732409 {
23742410 struct mem_cgroup *memcg = page->mem_cgroup;
23752411 unsigned int nr_pages = 1 << order;
@@ -4409,7 +4445,7 @@ static struct page *mc_handle_present_pte(struct vm_area_struct *vma, · 2033 unmodified lines
44094445
44104446 #ifdef CONFIG_SWAP
44114447 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4412− unsigned long addr, pte_t ptent, swp_entry_t *entry)
4448+ pte_t ptent, swp_entry_t *entry)
44134449 {
44144450 struct page *page = NULL;
44154451 swp_entry_t ent = pte_to_swp_entry(ptent);
@@ -4428,7 +4464,7 @@ static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, · 12 unmodified lines
44284464 }
44294465 #else
44304466 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4431− unsigned long addr, pte_t ptent, swp_entry_t *entry)
4467+ pte_t ptent, swp_entry_t *entry)
44324468 {
44334469 return NULL;
44344470 }
@@ -4471,7 +4507,7 @@ static struct page *mc_handle_file_pte(struct vm_area_struct *vma, · 36 unmodified lines
44714507 /**
44724508 * mem_cgroup_move_account - move account of the page
44734509 * @page: the page
4474− * @nr_pages: number of regular pages (>1 for huge pages)
4510+ * @compound: charge the page as compound or small page
44754511 * @from: mem_cgroup which the page is moved from.
44764512 * @to: mem_cgroup which the page is moved to. @from != @to.
44774513 *
@@ -4593,7 +4629,7 @@ static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, · 115 unmodified lines
45934629 if (pte_present(ptent))
45944630 page = mc_handle_present_pte(vma, addr, ptent);
45954631 else if (is_swap_pte(ptent))
4596− page = mc_handle_swap_pte(vma, addr, ptent, &ent);
4632+ page = mc_handle_swap_pte(vma, ptent, &ent);
45974633 else if (pte_none(ptent))
45984634 page = mc_handle_file_pte(vma, addr, ptent, &ent);
45994635
@@ -5333,6 +5369,7 @@ bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg) · 733 unmodified lines
53335369 * @mm: mm context of the victim
53345370 * @gfp_mask: reclaim mode
53355371 * @memcgp: charged memcg return
5372+ * @compound: charge the page as compound or small page
53365373 *
53375374 * Try to charge @page to the memcg that @mm belongs to, reclaiming
53385375 * pages according to @gfp_mask if necessary.
@@ -5395,6 +5432,7 @@ out: · 56 unmodified lines
53955432 * @page: page to charge
53965433 * @memcg: memcg to charge the page to
53975434 * @lrucare: page might be on LRU already
5435+ * @compound: charge the page as compound or small page
53985436 *
53995437 * Finalize a charge transaction started by mem_cgroup_try_charge(),
54005438 * after page->mapping has been set up. This must happen atomically
@@ -5446,6 +5484,7 @@ void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg, · 45 unmodified lines
54465484 * mem_cgroup_cancel_charge - cancel a page charge
54475485 * @page: page to charge
54485486 * @memcg: memcg to charge the page to
5487+ * @compound: charge the page as compound or small page
54495488 *
54505489 * Cancel a charge transaction started by mem_cgroup_try_charge().
54515490 */
@@ -5469,15 +5508,18 @@ void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg, · 17 unmodified lines
54695508
54705509 static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
54715510 unsigned long nr_anon, unsigned long nr_file,
5472− unsigned long nr_huge, struct page *dummy_page)
5511+ unsigned long nr_huge, unsigned long nr_kmem,
5512+ struct page *dummy_page)
54735513 {
5474− unsigned long nr_pages = nr_anon + nr_file;
5514+ unsigned long nr_pages = nr_anon + nr_file + nr_kmem;
54755515 unsigned long flags;
54765516
54775517 if (!mem_cgroup_is_root(memcg)) {
54785518 page_counter_uncharge(&memcg->memory, nr_pages);
54795519 if (do_memsw_account())
54805520 page_counter_uncharge(&memcg->memsw, nr_pages);
5521+ if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && nr_kmem)
5522+ page_counter_uncharge(&memcg->kmem, nr_kmem);
54815523 memcg_oom_recover(memcg);
54825524 }
54835525
@@ -5500,6 +5542,7 @@ static void uncharge_list(struct list_head *page_list) · 16 unmodified lines
55005542 unsigned long nr_anon = 0;
55015543 unsigned long nr_file = 0;
55025544 unsigned long nr_huge = 0;
5545+ unsigned long nr_kmem = 0;
55035546 unsigned long pgpgout = 0;
55045547 struct list_head *next;
55055548 struct page *page;
@@ -5510,8 +5553,6 @@ static void uncharge_list(struct list_head *page_list) · 4 unmodified lines
55105553 */
55115554 next = page_list->next;
55125555 do {
5513− unsigned int nr_pages = 1;
5514−
55155556 page = list_entry(next, struct page, lru);
55165557 next = page->lru.next;
55175558
@@ -5530,31 +5571,34 @@ static void uncharge_list(struct list_head *page_list) · 12 unmodified lines
55305571 if (memcg != page->mem_cgroup) {
55315572 if (memcg) {
55325573 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5533− nr_huge, page);
5534− pgpgout = nr_anon = nr_file = nr_huge = 0;
5574+ nr_huge, nr_kmem, page);
5575+ pgpgout = nr_anon = nr_file =
5576+ nr_huge = nr_kmem = 0;
55355577 }
55365578 memcg = page->mem_cgroup;
55375579 }
55385580
5539− if (PageTransHuge(page)) {
5540− nr_pages <<= compound_order(page);
5541− VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5542− nr_huge += nr_pages;
5543− }
5581+ if (!PageKmemcg(page)) {
5582+ unsigned int nr_pages = 1;
55445583
5545− if (PageAnon(page))
5546− nr_anon += nr_pages;
5547− else
5548− nr_file += nr_pages;
5584+ if (PageTransHuge(page)) {
5585+ nr_pages <<= compound_order(page);
5586+ nr_huge += nr_pages;
5587+ }
5588+ if (PageAnon(page))
5589+ nr_anon += nr_pages;
5590+ else
5591+ nr_file += nr_pages;
5592+ pgpgout++;
5593+ } else
5594+ nr_kmem += 1 << compound_order(page);
55495595
55505596 page->mem_cgroup = NULL;
5551−
5552− pgpgout++;
55535597 } while (next != page_list);
55545598
55555599 if (memcg)
55565600 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5557− nr_huge, page);
5601+ nr_huge, nr_kmem, page);
55585602 }
55595603
55605604 /**
mm/memory.c+505−380
@@ -233,6 +233,7 @@ void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long · 232 unmodified lines
233233 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
234234 tlb->batch = NULL;
235235 #endif
236+ tlb->page_size = 0;
236237
237238 __tlb_reset_range(tlb);
238239 }
@@ -292,23 +293,31 @@ void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long e · 53 unmodified lines
292293 * handling the additional races in SMP caused by other CPUs caching valid
293294 * mappings in their TLBs. Returns the number of free page slots left.
294295 * When out of page slots we must call tlb_flush_mmu().
296+ *returns true if the caller should flush.
295297 */
296−int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
298+bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
297299 {
298300 struct mmu_gather_batch *batch;
299301
300302 VM_BUG_ON(!tlb->end);
301303
304+ if (!tlb->page_size)
305+ tlb->page_size = page_size;
306+ else {
307+ if (page_size != tlb->page_size)
308+ return true;
309+ }
310+
302311 batch = tlb->active;
303− batch->pages[batch->nr++] = page;
304312 if (batch->nr == batch->max) {
305313 if (!tlb_next_batch(tlb))
306− return 0;
314+ return true;
307315 batch = tlb->active;
308316 }
309317 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
310318
311− return batch->max - batch->nr;
319+ batch->pages[batch->nr++] = page;
320+ return false;
312321 }
313322
314323 #endif /* HAVE_GENERIC_MMU_GATHER */
@@ -1109,6 +1118,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, · 794 unmodified lines
11091118 pte_t *start_pte;
11101119 pte_t *pte;
11111120 swp_entry_t entry;
1121+ struct page *pending_page = NULL;
11121122
11131123 again:
11141124 init_rss_vec(rss);
@@ -1132,7 +1142,7 @@ again: · 17 unmodified lines
11321142 * unmap shared but keep private pages.
11331143 */
11341144 if (details->check_mapping &&
1135− details->check_mapping != page->mapping)
1145+ details->check_mapping != page_rmapping(page))
11361146 continue;
11371147 }
11381148 ptent = ptep_get_and_clear_full(mm, addr, pte,
@@ -1160,8 +1170,9 @@ again: · 21 unmodified lines
11601170 page_remove_rmap(page, false);
11611171 if (unlikely(page_mapcount(page) < 0))
11621172 print_bad_pte(vma, addr, ptent, page);
1163− if (unlikely(!__tlb_remove_page(tlb, page))) {
1173+ if (unlikely(__tlb_remove_page(tlb, page))) {
11641174 force_flush = 1;
1175+ pending_page = page;
11651176 addr += PAGE_SIZE;
11661177 break;
11671178 }
@@ -1202,7 +1213,11 @@ again: · 34 unmodified lines
12021213 if (force_flush) {
12031214 force_flush = 0;
12041215 tlb_flush_mmu_free(tlb);
1205−
1216+ if (pending_page) {
1217+ /* remove the page with new size */
1218+ __tlb_remove_pte_page(tlb, pending_page);
1219+ pending_page = NULL;
1220+ }
12061221 if (addr != end)
12071222 goto again;
12081223 }
@@ -1479,7 +1494,7 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr, · 270 unmodified lines
14791494 /* Ok, finally just insert the thing.. */
14801495 get_page(page);
14811496 inc_mm_counter_fast(mm, mm_counter_file(page));
1482− page_add_file_rmap(page);
1497+ page_add_file_rmap(page, false);
14831498 set_pte_at(mm, addr, pte, mk_pte(page, prot));
14841499
14851500 retval = 0;
@@ -2055,13 +2070,11 @@ static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page, · 569 unmodified lines
20552070 * case, all we need to do here is to mark the page as writable and update
20562071 * any related book-keeping.
20572072 */
2058−static inline int wp_page_reuse(struct mm_struct *mm,
2059− struct vm_area_struct *vma, unsigned long address,
2060− pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
2061− struct page *page, int page_mkwrite,
2062− int dirty_shared)
2063− __releases(ptl)
2073+static inline int wp_page_reuse(struct fault_env *fe, pte_t orig_pte,
2074+ struct page *page, int page_mkwrite, int dirty_shared)
2075+ __releases(fe->ptl)
20642076 {
2077+ struct vm_area_struct *vma = fe->vma;
20652078 pte_t entry;
20662079 /*
20672080 * Clear the pages cpupid information as the existing
@@ -2071,12 +2084,12 @@ static inline int wp_page_reuse(struct mm_struct *mm, · 3 unmodified lines
20712084 if (page)
20722085 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
20732086
2074− flush_cache_page(vma, address, pte_pfn(orig_pte));
2087+ flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
20752088 entry = pte_mkyoung(orig_pte);
20762089 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2077− if (ptep_set_access_flags(vma, address, page_table, entry, 1))
2078− update_mmu_cache(vma, address, page_table);
2079− pte_unmap_unlock(page_table, ptl);
2090+ if (ptep_set_access_flags(vma, fe->address, fe->pte, entry, 1))
2091+ update_mmu_cache(vma, fe->address, fe->pte);
2092+ pte_unmap_unlock(fe->pte, fe->ptl);
20802093
20812094 if (dirty_shared) {
20822095 struct address_space *mapping;
@@ -2122,30 +2135,31 @@ static inline int wp_page_reuse(struct mm_struct *mm, · 39 unmodified lines
21222135 * held to the old page, as well as updating the rmap.
21232136 * - In any case, unlock the PTL and drop the reference we took to the old page.
21242137 */
2125−static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma,
2126− unsigned long address, pte_t *page_table, pmd_t *pmd,
2127− pte_t orig_pte, struct page *old_page)
2138+static int wp_page_copy(struct fault_env *fe, pte_t orig_pte,
2139+ struct page *old_page)
21282140 {
2141+ struct vm_area_struct *vma = fe->vma;
2142+ struct mm_struct *mm = vma->vm_mm;
21292143 struct page *new_page = NULL;
2130− spinlock_t *ptl = NULL;
21312144 pte_t entry;
21322145 int page_copied = 0;
2133− const unsigned long mmun_start = address & PAGE_MASK; /* For mmu_notifiers */
2134− const unsigned long mmun_end = mmun_start + PAGE_SIZE; /* For mmu_notifiers */
2146+ const unsigned long mmun_start = fe->address & PAGE_MASK;
2147+ const unsigned long mmun_end = mmun_start + PAGE_SIZE;
21352148 struct mem_cgroup *memcg;
21362149
21372150 if (unlikely(anon_vma_prepare(vma)))
21382151 goto oom;
21392152
21402153 if (is_zero_pfn(pte_pfn(orig_pte))) {
2141− new_page = alloc_zeroed_user_highpage_movable(vma, address);
2154+ new_page = alloc_zeroed_user_highpage_movable(vma, fe->address);
21422155 if (!new_page)
21432156 goto oom;
21442157 } else {
2145− new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2158+ new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
2159+ fe->address);
21462160 if (!new_page)
21472161 goto oom;
2148− cow_user_page(new_page, old_page, address, vma);
2162+ cow_user_page(new_page, old_page, fe->address, vma);
21492163 }
21502164
21512165 if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
@@ -2158,8 +2172,8 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, · 6 unmodified lines
21582172 /*
21592173 * Re-check the pte - we dropped the lock
21602174 */
2161− page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2162− if (likely(pte_same(*page_table, orig_pte))) {
2175+ fe->pte = pte_offset_map_lock(mm, fe->pmd, fe->address, &fe->ptl);
2176+ if (likely(pte_same(*fe->pte, orig_pte))) {
21632177 if (old_page) {
21642178 if (!PageAnon(old_page)) {
21652179 dec_mm_counter_fast(mm,
@@ -2169,7 +2183,7 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, · 3 unmodified lines
21692183 } else {
21702184 inc_mm_counter_fast(mm, MM_ANONPAGES);
21712185 }
2172− flush_cache_page(vma, address, pte_pfn(orig_pte));
2186+ flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
21732187 entry = mk_pte(new_page, vma->vm_page_prot);
21742188 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
21752189 /*
@@ -2178,8 +2192,8 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, · 2 unmodified lines
21782192 * seen in the presence of one thread doing SMC and another
21792193 * thread doing COW.
21802194 */
2181− ptep_clear_flush_notify(vma, address, page_table);
2182− page_add_new_anon_rmap(new_page, vma, address, false);
2195+ ptep_clear_flush_notify(vma, fe->address, fe->pte);
2196+ page_add_new_anon_rmap(new_page, vma, fe->address, false);
21832197 mem_cgroup_commit_charge(new_page, memcg, false, false);
21842198 lru_cache_add_active_or_unevictable(new_page, vma);
21852199 /*
@@ -2187,8 +2201,8 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
21872201 * mmu page tables (such as kvm shadow page tables), we want the
21882202 * new page to be mapped directly into the secondary page table.
21892203 */
2190− set_pte_at_notify(mm, address, page_table, entry);
2191− update_mmu_cache(vma, address, page_table);
2204+ set_pte_at_notify(mm, fe->address, fe->pte, entry);
2205+ update_mmu_cache(vma, fe->address, fe->pte);
21922206 if (old_page) {
21932207 /*
21942208 * Only after switching the pte to the new page may
@@ -2225,7 +2239,7 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, · 30 unmodified lines
22252239 if (new_page)
22262240 put_page(new_page);
22272241
2228− pte_unmap_unlock(page_table, ptl);
2242+ pte_unmap_unlock(fe->pte, fe->ptl);
22292243 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
22302244 if (old_page) {
22312245 /*
@@ -2253,44 +2267,43 @@ oom: · 21 unmodified lines
22532267 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
22542268 * mapping
22552269 */
2256−static int wp_pfn_shared(struct mm_struct *mm,
2257− struct vm_area_struct *vma, unsigned long address,
2258− pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
2259− pmd_t *pmd)
2270+static int wp_pfn_shared(struct fault_env *fe, pte_t orig_pte)
22602271 {
2272+ struct vm_area_struct *vma = fe->vma;
2273+
22612274 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
22622275 struct vm_fault vmf = {
22632276 .page = NULL,
2264− .pgoff = linear_page_index(vma, address),
2265− .virtual_address = (void __user *)(address & PAGE_MASK),
2277+ .pgoff = linear_page_index(vma, fe->address),
2278+ .virtual_address =
2279+ (void __user *)(fe->address & PAGE_MASK),
22662280 .flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE,
22672281 };
22682282 int ret;
22692283
2270− pte_unmap_unlock(page_table, ptl);
2284+ pte_unmap_unlock(fe->pte, fe->ptl);
22712285 ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
22722286 if (ret & VM_FAULT_ERROR)
22732287 return ret;
2274− page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2288+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2289+ &fe->ptl);
22752290 /*
22762291 * We might have raced with another page fault while we
22772292 * released the pte_offset_map_lock.
22782293 */
2279− if (!pte_same(*page_table, orig_pte)) {
2280− pte_unmap_unlock(page_table, ptl);
2294+ if (!pte_same(*fe->pte, orig_pte)) {
2295+ pte_unmap_unlock(fe->pte, fe->ptl);
22812296 return 0;
22822297 }
22832298 }
2284− return wp_page_reuse(mm, vma, address, page_table, ptl, orig_pte,
2285− NULL, 0, 0);
2299+ return wp_page_reuse(fe, orig_pte, NULL, 0, 0);
22862300 }
22872301
2288−static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma,
2289− unsigned long address, pte_t *page_table,
2290− pmd_t *pmd, spinlock_t *ptl, pte_t orig_pte,
2291− struct page *old_page)
2292− __releases(ptl)
2302+static int wp_page_shared(struct fault_env *fe, pte_t orig_pte,
2303+ struct page *old_page)
2304+ __releases(fe->ptl)
22932305 {
2306+ struct vm_area_struct *vma = fe->vma;
22942307 int page_mkwrite = 0;
22952308
22962309 get_page(old_page);
@@ -2298,8 +2311,8 @@ static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
22982311 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
22992312 int tmp;
23002313
2301− pte_unmap_unlock(page_table, ptl);
2302− tmp = do_page_mkwrite(vma, old_page, address);
2314+ pte_unmap_unlock(fe->pte, fe->ptl);
2315+ tmp = do_page_mkwrite(vma, old_page, fe->address);
23032316 if (unlikely(!tmp || (tmp &
23042317 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
23052318 put_page(old_page);
@@ -2311,19 +2324,18 @@ static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma, · 5 unmodified lines
23112324 * they did, we just return, as we can count on the
23122325 * MMU to tell us if they didn't also make it writable.
23132326 */
2314− page_table = pte_offset_map_lock(mm, pmd, address,
2315− &ptl);
2316− if (!pte_same(*page_table, orig_pte)) {
2327+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2328+ &fe->ptl);
2329+ if (!pte_same(*fe->pte, orig_pte)) {
23172330 unlock_page(old_page);
2318− pte_unmap_unlock(page_table, ptl);
2331+ pte_unmap_unlock(fe->pte, fe->ptl);
23192332 put_page(old_page);
23202333 return 0;
23212334 }
23222335 page_mkwrite = 1;
23232336 }
23242337
2325− return wp_page_reuse(mm, vma, address, page_table, ptl,
2326− orig_pte, old_page, page_mkwrite, 1);
2338+ return wp_page_reuse(fe, orig_pte, old_page, page_mkwrite, 1);
23272339 }
23282340
23292341 /*
@@ -2344,14 +2356,13 @@ static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma, · 14 unmodified lines
23442356 * but allow concurrent faults), with pte both mapped and locked.
23452357 * We return with mmap_sem still held, but pte unmapped and unlocked.
23462358 */
2347−static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
2348− unsigned long address, pte_t *page_table, pmd_t *pmd,
2349− spinlock_t *ptl, pte_t orig_pte)
2350− __releases(ptl)
2359+static int do_wp_page(struct fault_env *fe, pte_t orig_pte)
2360+ __releases(fe->ptl)
23512361 {
2362+ struct vm_area_struct *vma = fe->vma;
23522363 struct page *old_page;
23532364
2354− old_page = vm_normal_page(vma, address, orig_pte);
2365+ old_page = vm_normal_page(vma, fe->address, orig_pte);
23552366 if (!old_page) {
23562367 /*
23572368 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
@@ -2362,12 +2373,10 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, · 4 unmodified lines
23622373 */
23632374 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
23642375 (VM_WRITE|VM_SHARED))
2365− return wp_pfn_shared(mm, vma, address, page_table, ptl,
2366− orig_pte, pmd);
2376+ return wp_pfn_shared(fe, orig_pte);
23672377
2368− pte_unmap_unlock(page_table, ptl);
2369− return wp_page_copy(mm, vma, address, page_table, pmd,
2370− orig_pte, old_page);
2378+ pte_unmap_unlock(fe->pte, fe->ptl);
2379+ return wp_page_copy(fe, orig_pte, old_page);
23712380 }
23722381
23732382 /*
@@ -2378,13 +2387,13 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, · 4 unmodified lines
23782387 int total_mapcount;
23792388 if (!trylock_page(old_page)) {
23802389 get_page(old_page);
2381− pte_unmap_unlock(page_table, ptl);
2390+ pte_unmap_unlock(fe->pte, fe->ptl);
23822391 lock_page(old_page);
2383− page_table = pte_offset_map_lock(mm, pmd, address,
2384− &ptl);
2385− if (!pte_same(*page_table, orig_pte)) {
2392+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
2393+ fe->address, &fe->ptl);
2394+ if (!pte_same(*fe->pte, orig_pte)) {
23862395 unlock_page(old_page);
2387− pte_unmap_unlock(page_table, ptl);
2396+ pte_unmap_unlock(fe->pte, fe->ptl);
23882397 put_page(old_page);
23892398 return 0;
23902399 }
@@ -2402,14 +2411,12 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, · 11 unmodified lines
24022411 page_move_anon_rmap(old_page, vma);
24032412 }
24042413 unlock_page(old_page);
2405− return wp_page_reuse(mm, vma, address, page_table, ptl,
2406− orig_pte, old_page, 0, 0);
2414+ return wp_page_reuse(fe, orig_pte, old_page, 0, 0);
24072415 }
24082416 unlock_page(old_page);
24092417 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
24102418 (VM_WRITE|VM_SHARED))) {
2411− return wp_page_shared(mm, vma, address, page_table, pmd,
2412− ptl, orig_pte, old_page);
2419+ return wp_page_shared(fe, orig_pte, old_page);
24132420 }
24142421
24152422 /*
@@ -2417,9 +2424,8 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
24172424 */
24182425 get_page(old_page);
24192426
2420− pte_unmap_unlock(page_table, ptl);
2421− return wp_page_copy(mm, vma, address, page_table, pmd,
2422− orig_pte, old_page);
2427+ pte_unmap_unlock(fe->pte, fe->ptl);
2428+ return wp_page_copy(fe, orig_pte, old_page);
24232429 }
24242430
24252431 static void unmap_mapping_range_vma(struct vm_area_struct *vma,
@@ -2507,11 +2513,9 @@ EXPORT_SYMBOL(unmap_mapping_range); · 81 unmodified lines
25072513 * We return with the mmap_sem locked or unlocked in the same cases
25082514 * as does filemap_fault().
25092515 */
2510−static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
2511− unsigned long address, pte_t *page_table, pmd_t *pmd,
2512− unsigned int flags, pte_t orig_pte)
2516+int do_swap_page(struct fault_env *fe, pte_t orig_pte)
25132517 {
2514− spinlock_t *ptl;
2518+ struct vm_area_struct *vma = fe->vma;
25152519 struct page *page, *swapcache;
25162520 struct mem_cgroup *memcg;
25172521 swp_entry_t entry;
@@ -2520,17 +2524,17 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 2 unmodified lines
25202524 int exclusive = 0;
25212525 int ret = 0;
25222526
2523− if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
2527+ if (!pte_unmap_same(vma->vm_mm, fe->pmd, fe->pte, orig_pte))
25242528 goto out;
25252529
25262530 entry = pte_to_swp_entry(orig_pte);
25272531 if (unlikely(non_swap_entry(entry))) {
25282532 if (is_migration_entry(entry)) {
2529− migration_entry_wait(mm, pmd, address);
2533+ migration_entry_wait(vma->vm_mm, fe->pmd, fe->address);
25302534 } else if (is_hwpoison_entry(entry)) {
25312535 ret = VM_FAULT_HWPOISON;
25322536 } else {
2533− print_bad_pte(vma, address, orig_pte, NULL);
2537+ print_bad_pte(vma, fe->address, orig_pte, NULL);
25342538 ret = VM_FAULT_SIGBUS;
25352539 }
25362540 goto out;
@@ -2539,14 +2543,15 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 2 unmodified lines
25392543 page = lookup_swap_cache(entry);
25402544 if (!page) {
25412545 page = swapin_readahead(entry,
2542− GFP_HIGHUSER_MOVABLE, vma, address);
2546+ GFP_HIGHUSER_MOVABLE, vma, fe->address);
25432547 if (!page) {
25442548 /*
25452549 * Back out if somebody else faulted in this pte
25462550 * while we released the pte lock.
25472551 */
2548− page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2549− if (likely(pte_same(*page_table, orig_pte)))
2552+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
2553+ fe->address, &fe->ptl);
2554+ if (likely(pte_same(*fe->pte, orig_pte)))
25502555 ret = VM_FAULT_OOM;
25512556 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
25522557 goto unlock;
@@ -2555,7 +2560,7 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 2 unmodified lines
25552560 /* Had to read the page from swap area: Major fault */
25562561 ret = VM_FAULT_MAJOR;
25572562 count_vm_event(PGMAJFAULT);
2558− mem_cgroup_count_vm_event(mm, PGMAJFAULT);
2563+ mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
25592564 } else if (PageHWPoison(page)) {
25602565 /*
25612566 * hwpoisoned dirty swapcache pages are kept for killing
@@ -2568,7 +2573,7 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 6 unmodified lines
25682573 }
25692574
25702575 swapcache = page;
2571− locked = lock_page_or_retry(page, mm, flags);
2576+ locked = lock_page_or_retry(page, vma->vm_mm, fe->flags);
25722577
25732578 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
25742579 if (!locked) {
@@ -2585,14 +2590,15 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 10 unmodified lines
25852590 if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
25862591 goto out_page;
25872592
2588− page = ksm_might_need_to_copy(page, vma, address);
2593+ page = ksm_might_need_to_copy(page, vma, fe->address);
25892594 if (unlikely(!page)) {
25902595 ret = VM_FAULT_OOM;
25912596 page = swapcache;
25922597 goto out_page;
25932598 }
25942599
2595− if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false)) {
2600+ if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
2601+ &memcg, false)) {
25962602 ret = VM_FAULT_OOM;
25972603 goto out_page;
25982604 }
@@ -2600,8 +2606,9 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
26002606 /*
26012607 * Back out if somebody else already faulted in this pte.
26022608 */
2603− page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2604− if (unlikely(!pte_same(*page_table, orig_pte)))
2609+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2610+ &fe->ptl);
2611+ if (unlikely(!pte_same(*fe->pte, orig_pte)))
26052612 goto out_nomap;
26062613
26072614 if (unlikely(!PageUptodate(page))) {
@@ -2619,24 +2626,24 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 11 unmodified lines
26192626 * must be called after the swap_free(), or it will never succeed.
26202627 */
26212628
2622− inc_mm_counter_fast(mm, MM_ANONPAGES);
2623− dec_mm_counter_fast(mm, MM_SWAPENTS);
2629+ inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
2630+ dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
26242631 pte = mk_pte(page, vma->vm_page_prot);
2625− if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
2632+ if ((fe->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
26262633 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
2627− flags &= ~FAULT_FLAG_WRITE;
2634+ fe->flags &= ~FAULT_FLAG_WRITE;
26282635 ret |= VM_FAULT_WRITE;
26292636 exclusive = RMAP_EXCLUSIVE;
26302637 }
26312638 flush_icache_page(vma, page);
26322639 if (pte_swp_soft_dirty(orig_pte))
26332640 pte = pte_mksoft_dirty(pte);
2634− set_pte_at(mm, address, page_table, pte);
2641+ set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
26352642 if (page == swapcache) {
2636− do_page_add_anon_rmap(page, vma, address, exclusive);
2643+ do_page_add_anon_rmap(page, vma, fe->address, exclusive);
26372644 mem_cgroup_commit_charge(page, memcg, true, false);
26382645 } else { /* ksm created a completely new copy */
2639− page_add_new_anon_rmap(page, vma, address, false);
2646+ page_add_new_anon_rmap(page, vma, fe->address, false);
26402647 mem_cgroup_commit_charge(page, memcg, false, false);
26412648 lru_cache_add_active_or_unevictable(page, vma);
26422649 }
@@ -2659,22 +2666,22 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · 16 unmodified lines
26592666 put_page(swapcache);
26602667 }
26612668
2662− if (flags & FAULT_FLAG_WRITE) {
2663− ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
2669+ if (fe->flags & FAULT_FLAG_WRITE) {
2670+ ret |= do_wp_page(fe, pte);
26642671 if (ret & VM_FAULT_ERROR)
26652672 ret &= VM_FAULT_ERROR;
26662673 goto out;
26672674 }
26682675
26692676 /* No need to invalidate - it was non-present before */
2670− update_mmu_cache(vma, address, page_table);
2677+ update_mmu_cache(vma, fe->address, fe->pte);
26712678 unlock:
2672− pte_unmap_unlock(page_table, ptl);
2679+ pte_unmap_unlock(fe->pte, fe->ptl);
26732680 out:
26742681 return ret;
26752682 out_nomap:
26762683 mem_cgroup_cancel_charge(page, memcg, false);
2677− pte_unmap_unlock(page_table, ptl);
2684+ pte_unmap_unlock(fe->pte, fe->ptl);
26782685 out_page:
26792686 unlock_page(page);
26802687 out_release:
@@ -2725,37 +2732,51 @@ static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned lo · 44 unmodified lines
27252732 * but allow concurrent faults), and pte mapped but not yet locked.
27262733 * We return with mmap_sem still held, but pte unmapped and unlocked.
27272734 */
2728−static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
2729− unsigned long address, pte_t *page_table, pmd_t *pmd,
2730− unsigned int flags)
2735+static int do_anonymous_page(struct fault_env *fe)
27312736 {
2737+ struct vm_area_struct *vma = fe->vma;
27322738 struct mem_cgroup *memcg;
27332739 struct page *page;
2734− spinlock_t *ptl;
27352740 pte_t entry;
27362741
2737− pte_unmap(page_table);
2738−
27392742 /* File mapping without ->vm_ops ? */
27402743 if (vma->vm_flags & VM_SHARED)
27412744 return VM_FAULT_SIGBUS;
27422745
27432746 /* Check if we need to add a guard page to the stack */
2744− if (check_stack_guard_page(vma, address) < 0)
2747+ if (check_stack_guard_page(vma, fe->address) < 0)
27452748 return VM_FAULT_SIGSEGV;
27462749
2750+ /*
2751+ * Use pte_alloc() instead of pte_alloc_map(). We can't run
2752+ * pte_offset_map() on pmds where a huge pmd might be created
2753+ * from a different thread.
2754+ *
2755+ * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
2756+ * parallel threads are excluded by other means.
2757+ *
2758+ * Here we only have down_read(mmap_sem).
2759+ */
2760+ if (pte_alloc(vma->vm_mm, fe->pmd, fe->address))
2761+ return VM_FAULT_OOM;
2762+
2763+ /* See the comment in pte_alloc_one_map() */
2764+ if (unlikely(pmd_trans_unstable(fe->pmd)))
2765+ return 0;
2766+
27472767 /* Use the zero-page for reads */
2748− if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) {
2749− entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
2768+ if (!(fe->flags & FAULT_FLAG_WRITE) &&
2769+ !mm_forbids_zeropage(vma->vm_mm)) {
2770+ entry = pte_mkspecial(pfn_pte(my_zero_pfn(fe->address),
27502771 vma->vm_page_prot));
2751− page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2752− if (!pte_none(*page_table))
2772+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2773+ &fe->ptl);
2774+ if (!pte_none(*fe->pte))
27532775 goto unlock;
27542776 /* Deliver the page fault to userland, check inside PT lock */
27552777 if (userfaultfd_missing(vma)) {
2756− pte_unmap_unlock(page_table, ptl);
2757− return handle_userfault(vma, address, flags,
2758− VM_UFFD_MISSING);
2778+ pte_unmap_unlock(fe->pte, fe->ptl);
2779+ return handle_userfault(fe, VM_UFFD_MISSING);
27592780 }
27602781 goto setpte;
27612782 }
@@ -2763,11 +2784,11 @@ static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
27632784 /* Allocate our own private page. */
27642785 if (unlikely(anon_vma_prepare(vma)))
27652786 goto oom;
2766− page = alloc_zeroed_user_highpage_movable(vma, address);
2787+ page = alloc_zeroed_user_highpage_movable(vma, fe->address);
27672788 if (!page)
27682789 goto oom;
27692790
2770− if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false))
2791+ if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
27712792 goto oom_free_page;
27722793
27732794 /*
@@ -2781,30 +2802,30 @@ static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, · 7 unmodified lines
27812802 if (vma->vm_flags & VM_WRITE)
27822803 entry = pte_mkwrite(pte_mkdirty(entry));
27832804
2784− page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2785− if (!pte_none(*page_table))
2805+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2806+ &fe->ptl);
2807+ if (!pte_none(*fe->pte))
27862808 goto release;
27872809
27882810 /* Deliver the page fault to userland, check inside PT lock */
27892811 if (userfaultfd_missing(vma)) {
2790− pte_unmap_unlock(page_table, ptl);
2812+ pte_unmap_unlock(fe->pte, fe->ptl);
27912813 mem_cgroup_cancel_charge(page, memcg, false);
27922814 put_page(page);
2793− return handle_userfault(vma, address, flags,
2794− VM_UFFD_MISSING);
2815+ return handle_userfault(fe, VM_UFFD_MISSING);
27952816 }
27962817
2797− inc_mm_counter_fast(mm, MM_ANONPAGES);
2798− page_add_new_anon_rmap(page, vma, address, false);
2818+ inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
2819+ page_add_new_anon_rmap(page, vma, fe->address, false);
27992820 mem_cgroup_commit_charge(page, memcg, false, false);
28002821 lru_cache_add_active_or_unevictable(page, vma);
28012822 setpte:
2802− set_pte_at(mm, address, page_table, entry);
2823+ set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
28032824
28042825 /* No need to invalidate - it was non-present before */
2805− update_mmu_cache(vma, address, page_table);
2826+ update_mmu_cache(vma, fe->address, fe->pte);
28062827 unlock:
2807− pte_unmap_unlock(page_table, ptl);
2828+ pte_unmap_unlock(fe->pte, fe->ptl);
28082829 return 0;
28092830 release:
28102831 mem_cgroup_cancel_charge(page, memcg, false);
@@ -2821,17 +2842,16 @@ oom: · 10 unmodified lines
28212842 * released depending on flags and vma->vm_ops->fault() return value.
28222843 * See filemap_fault() and __lock_page_retry().
28232844 */
2824−static int __do_fault(struct vm_area_struct *vma, unsigned long address,
2825− pgoff_t pgoff, unsigned int flags,
2826− struct page *cow_page, struct page **page,
2827− void **entry)
2845+static int __do_fault(struct fault_env *fe, pgoff_t pgoff,
2846+ struct page *cow_page, struct page **page, void **entry)
28282847 {
2848+ struct vm_area_struct *vma = fe->vma;
28292849 struct vm_fault vmf;
28302850 int ret;
28312851
2832− vmf.virtual_address = (void __user *)(address & PAGE_MASK);
2852+ vmf.virtual_address = (void __user *)(fe->address & PAGE_MASK);
28332853 vmf.pgoff = pgoff;
2834− vmf.flags = flags;
2854+ vmf.flags = fe->flags;
28352855 vmf.page = NULL;
28362856 vmf.gfp_mask = __get_fault_gfp_mask(vma);
28372857 vmf.cow_page = cow_page;
@@ -2860,41 +2880,168 @@ static int __do_fault(struct vm_area_struct *vma, unsigned long address, · 22 unmodified lines
28602880 return ret;
28612881 }
28622882
2883+static int pte_alloc_one_map(struct fault_env *fe)
2884+{
2885+ struct vm_area_struct *vma = fe->vma;
2886+
2887+ if (!pmd_none(*fe->pmd))
2888+ goto map_pte;
2889+ if (fe->prealloc_pte) {
2890+ fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
2891+ if (unlikely(!pmd_none(*fe->pmd))) {
2892+ spin_unlock(fe->ptl);
2893+ goto map_pte;
2894+ }
2895+
2896+ atomic_long_inc(&vma->vm_mm->nr_ptes);
2897+ pmd_populate(vma->vm_mm, fe->pmd, fe->prealloc_pte);
2898+ spin_unlock(fe->ptl);
2899+ fe->prealloc_pte = 0;
2900+ } else if (unlikely(pte_alloc(vma->vm_mm, fe->pmd, fe->address))) {
2901+ return VM_FAULT_OOM;
2902+ }
2903+map_pte:
2904+ /*
2905+ * If a huge pmd materialized under us just retry later. Use
2906+ * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd
2907+ * didn't become pmd_trans_huge under us and then back to pmd_none, as
2908+ * a result of MADV_DONTNEED running immediately after a huge pmd fault
2909+ * in a different thread of this mm, in turn leading to a misleading
2910+ * pmd_trans_huge() retval. All we have to ensure is that it is a
2911+ * regular pmd that we can walk with pte_offset_map() and we can do that
2912+ * through an atomic read in C, which is what pmd_trans_unstable()
2913+ * provides.
2914+ */
2915+ if (pmd_trans_unstable(fe->pmd) || pmd_devmap(*fe->pmd))
2916+ return VM_FAULT_NOPAGE;
2917+
2918+ fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2919+ &fe->ptl);
2920+ return 0;
2921+}
2922+
2923+#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
2924+
2925+#define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
2926+static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
2927+ unsigned long haddr)
2928+{
2929+ if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
2930+ (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
2931+ return false;
2932+ if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
2933+ return false;
2934+ return true;
2935+}
2936+
2937+static int do_set_pmd(struct fault_env *fe, struct page *page)
2938+{
2939+ struct vm_area_struct *vma = fe->vma;
2940+ bool write = fe->flags & FAULT_FLAG_WRITE;
2941+ unsigned long haddr = fe->address & HPAGE_PMD_MASK;
2942+ pmd_t entry;
2943+ int i, ret;
2944+
2945+ if (!transhuge_vma_suitable(vma, haddr))
2946+ return VM_FAULT_FALLBACK;
2947+
2948+ ret = VM_FAULT_FALLBACK;
2949+ page = compound_head(page);
2950+
2951+ fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
2952+ if (unlikely(!pmd_none(*fe->pmd)))
2953+ goto out;
2954+
2955+ for (i = 0; i < HPAGE_PMD_NR; i++)
2956+ flush_icache_page(vma, page + i);
2957+
2958+ entry = mk_huge_pmd(page, vma->vm_page_prot);
2959+ if (write)
2960+ entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
2961+
2962+ add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR);
2963+ page_add_file_rmap(page, true);
2964+
2965+ set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
2966+
2967+ update_mmu_cache_pmd(vma, haddr, fe->pmd);
2968+
2969+ /* fault is handled */
2970+ ret = 0;
2971+ count_vm_event(THP_FILE_MAPPED);
2972+out:
2973+ spin_unlock(fe->ptl);
2974+ return ret;
2975+}
2976+#else
2977+static int do_set_pmd(struct fault_env *fe, struct page *page)
2978+{
2979+ BUILD_BUG();
2980+ return 0;
2981+}
2982+#endif
2983+
28632984 /**
2864− * do_set_pte - setup new PTE entry for given page and add reverse page mapping.
2985+ * alloc_set_pte - setup new PTE entry for given page and add reverse page
2986+ * mapping. If needed, the fucntion allocates page table or use pre-allocated.
28652987 *
2866− * @vma: virtual memory area
2867− * @address: user virtual address
2988+ * @fe: fault environment
2989+ * @memcg: memcg to charge page (only for private mappings)
28682990 * @page: page to map
2869− * @pte: pointer to target page table entry
2870− * @write: true, if new entry is writable
2871− * @anon: true, if it's anonymous page
28722991 *
2873− * Caller must hold page table lock relevant for @pte.
2992+ * Caller must take care of unlocking fe->ptl, if fe->pte is non-NULL on return.
28742993 *
28752994 * Target users are page handler itself and implementations of
28762995 * vm_ops->map_pages.
28772996 */
2878−void do_set_pte(struct vm_area_struct *vma, unsigned long address,
2879− struct page *page, pte_t *pte, bool write, bool anon)
2997+int alloc_set_pte(struct fault_env *fe, struct mem_cgroup *memcg,
2998+ struct page *page)
28802999 {
3000+ struct vm_area_struct *vma = fe->vma;
3001+ bool write = fe->flags & FAULT_FLAG_WRITE;
28813002 pte_t entry;
3003+ int ret;
3004+
3005+ if (pmd_none(*fe->pmd) && PageTransCompound(page) &&
3006+ IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
3007+ /* THP on COW? */
3008+ VM_BUG_ON_PAGE(memcg, page);
3009+
3010+ ret = do_set_pmd(fe, page);
3011+ if (ret != VM_FAULT_FALLBACK)
3012+ return ret;
3013+ }
3014+
3015+ if (!fe->pte) {
3016+ ret = pte_alloc_one_map(fe);
3017+ if (ret)
3018+ return ret;
3019+ }
3020+
3021+ /* Re-check under ptl */
3022+ if (unlikely(!pte_none(*fe->pte)))
3023+ return VM_FAULT_NOPAGE;
28823024
28833025 flush_icache_page(vma, page);
28843026 entry = mk_pte(page, vma->vm_page_prot);
28853027 if (write)
28863028 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2887− if (anon) {
3029+ /* copy-on-write page */
3030+ if (write && !(vma->vm_flags & VM_SHARED)) {
28883031 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
2889− page_add_new_anon_rmap(page, vma, address, false);
3032+ page_add_new_anon_rmap(page, vma, fe->address, false);
3033+ mem_cgroup_commit_charge(page, memcg, false, false);
3034+ lru_cache_add_active_or_unevictable(page, vma);
28903035 } else {
28913036 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
2892− page_add_file_rmap(page);
3037+ page_add_file_rmap(page, false);
28933038 }
2894− set_pte_at(vma->vm_mm, address, pte, entry);
3039+ set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
28953040
28963041 /* no need to invalidate: a not-present page won't be cached */
2897− update_mmu_cache(vma, address, pte);
3042+ update_mmu_cache(vma, fe->address, fe->pte);
3043+
3044+ return 0;
28983045 }
28993046
29003047 static unsigned long fault_around_bytes __read_mostly =
@@ -2961,57 +3108,66 @@ late_initcall(fault_around_debugfs); · 60 unmodified lines
29613108 * fault_around_pages() value (and therefore to page order). This way it's
29623109 * easier to guarantee that we don't cross page table boundaries.
29633110 */
2964−static void do_fault_around(struct vm_area_struct *vma, unsigned long address,
2965− pte_t *pte, pgoff_t pgoff, unsigned int flags)
3111+static int do_fault_around(struct fault_env *fe, pgoff_t start_pgoff)
29663112 {
2967− unsigned long start_addr, nr_pages, mask;
2968− pgoff_t max_pgoff;
2969− struct vm_fault vmf;
2970− int off;
3113+ unsigned long address = fe->address, nr_pages, mask;
3114+ pgoff_t end_pgoff;
3115+ int off, ret = 0;
29713116
29723117 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
29733118 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
29743119
2975− start_addr = max(address & mask, vma->vm_start);
2976− off = ((address - start_addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
2977− pte -= off;
2978− pgoff -= off;
3120+ fe->address = max(address & mask, fe->vma->vm_start);
3121+ off = ((address - fe->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
3122+ start_pgoff -= off;
29793123
29803124 /*
2981− * max_pgoff is either end of page table or end of vma
2982− * or fault_around_pages() from pgoff, depending what is nearest.
3125+ * end_pgoff is either end of page table or end of vma
3126+ * or fault_around_pages() from start_pgoff, depending what is nearest.
29833127 */
2984− max_pgoff = pgoff - ((start_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
3128+ end_pgoff = start_pgoff -
3129+ ((fe->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
29853130 PTRS_PER_PTE - 1;
2986− max_pgoff = min3(max_pgoff, vma_pages(vma) + vma->vm_pgoff - 1,
2987− pgoff + nr_pages - 1);
3131+ end_pgoff = min3(end_pgoff, vma_pages(fe->vma) + fe->vma->vm_pgoff - 1,
3132+ start_pgoff + nr_pages - 1);
29883133
2989− /* Check if it makes any sense to call ->map_pages */
2990− while (!pte_none(*pte)) {
2991− if (++pgoff > max_pgoff)
2992− return;
2993− start_addr += PAGE_SIZE;
2994− if (start_addr >= vma->vm_end)
2995− return;
2996− pte++;
3134+ if (pmd_none(*fe->pmd)) {
3135+ fe->prealloc_pte = pte_alloc_one(fe->vma->vm_mm, fe->address);
3136+ smp_wmb(); /* See comment in __pte_alloc() */
29973137 }
29983138
2999− vmf.virtual_address = (void __user *) start_addr;
3000− vmf.pte = pte;
3001− vmf.pgoff = pgoff;
3002− vmf.max_pgoff = max_pgoff;
3003− vmf.flags = flags;
3004− vmf.gfp_mask = __get_fault_gfp_mask(vma);
3005− vma->vm_ops->map_pages(vma, &vmf);
3139+ fe->vma->vm_ops->map_pages(fe, start_pgoff, end_pgoff);
3140+
3141+ /* preallocated pagetable is unused: free it */
3142+ if (fe->prealloc_pte) {
3143+ pte_free(fe->vma->vm_mm, fe->prealloc_pte);
3144+ fe->prealloc_pte = 0;
3145+ }
3146+ /* Huge page is mapped? Page fault is solved */
3147+ if (pmd_trans_huge(*fe->pmd)) {
3148+ ret = VM_FAULT_NOPAGE;
3149+ goto out;
3150+ }
3151+
3152+ /* ->map_pages() haven't done anything useful. Cold page cache? */
3153+ if (!fe->pte)
3154+ goto out;
3155+
3156+ /* check if the page fault is solved */
3157+ fe->pte -= (fe->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
3158+ if (!pte_none(*fe->pte))
3159+ ret = VM_FAULT_NOPAGE;
3160+ pte_unmap_unlock(fe->pte, fe->ptl);
3161+out:
3162+ fe->address = address;
3163+ fe->pte = NULL;
3164+ return ret;
30063165 }
30073166
3008−static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3009− unsigned long address, pmd_t *pmd,
3010− pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
3167+static int do_read_fault(struct fault_env *fe, pgoff_t pgoff)
30113168 {
3169+ struct vm_area_struct *vma = fe->vma;
30123170 struct page *fault_page;
3013− spinlock_t *ptl;
3014− pte_t *pte;
30153171 int ret = 0;
30163172
30173173 /*
@@ -3020,85 +3176,64 @@ static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma, · 2 unmodified lines
30203176 * something).
30213177 */
30223178 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
3023− pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3024− do_fault_around(vma, address, pte, pgoff, flags);
3025− if (!pte_same(*pte, orig_pte))
3026− goto unlock_out;
3027− pte_unmap_unlock(pte, ptl);
3179+ ret = do_fault_around(fe, pgoff);
3180+ if (ret)
3181+ return ret;
30283182 }
30293183
3030− ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page, NULL);
3184+ ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
30313185 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
30323186 return ret;
30333187
3034− pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3035− if (unlikely(!pte_same(*pte, orig_pte))) {
3036− pte_unmap_unlock(pte, ptl);
3037− unlock_page(fault_page);
3038− put_page(fault_page);
3039− return ret;
3040− }
3041− do_set_pte(vma, address, fault_page, pte, false, false);
3188+ ret |= alloc_set_pte(fe, NULL, fault_page);
3189+ if (fe->pte)
3190+ pte_unmap_unlock(fe->pte, fe->ptl);
30423191 unlock_page(fault_page);
3043−unlock_out:
3044− pte_unmap_unlock(pte, ptl);
3192+ if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3193+ put_page(fault_page);
30453194 return ret;
30463195 }
30473196
3048−static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3049− unsigned long address, pmd_t *pmd,
3050− pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
3197+static int do_cow_fault(struct fault_env *fe, pgoff_t pgoff)
30513198 {
3199+ struct vm_area_struct *vma = fe->vma;
30523200 struct page *fault_page, *new_page;
30533201 void *fault_entry;
30543202 struct mem_cgroup *memcg;
3055− spinlock_t *ptl;
3056− pte_t *pte;
30573203 int ret;
30583204
30593205 if (unlikely(anon_vma_prepare(vma)))
30603206 return VM_FAULT_OOM;
30613207
3062− new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
3208+ new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, fe->address);
30633209 if (!new_page)
30643210 return VM_FAULT_OOM;
30653211
3066− if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) {
3212+ if (mem_cgroup_try_charge(new_page, vma->vm_mm, GFP_KERNEL,
3213+ &memcg, false)) {
30673214 put_page(new_page);
30683215 return VM_FAULT_OOM;
30693216 }
30703217
3071− ret = __do_fault(vma, address, pgoff, flags, new_page, &fault_page,
3072− &fault_entry);
3218+ ret = __do_fault(fe, pgoff, new_page, &fault_page, &fault_entry);
30733219 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
30743220 goto uncharge_out;
30753221
30763222 if (!(ret & VM_FAULT_DAX_LOCKED))
3077− copy_user_highpage(new_page, fault_page, address, vma);
3223+ copy_user_highpage(new_page, fault_page, fe->address, vma);
30783224 __SetPageUptodate(new_page);
30793225
3080− pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3081− if (unlikely(!pte_same(*pte, orig_pte))) {
3082− pte_unmap_unlock(pte, ptl);
3083− if (!(ret & VM_FAULT_DAX_LOCKED)) {
3084− unlock_page(fault_page);
3085− put_page(fault_page);
3086− } else {
3087− dax_unlock_mapping_entry(vma->vm_file->f_mapping,
3088− pgoff);
3089− }
3090− goto uncharge_out;
3091− }
3092− do_set_pte(vma, address, new_page, pte, true, true);
3093− mem_cgroup_commit_charge(new_page, memcg, false, false);
3094− lru_cache_add_active_or_unevictable(new_page, vma);
3095− pte_unmap_unlock(pte, ptl);
3226+ ret |= alloc_set_pte(fe, memcg, new_page);
3227+ if (fe->pte)
3228+ pte_unmap_unlock(fe->pte, fe->ptl);
30963229 if (!(ret & VM_FAULT_DAX_LOCKED)) {
30973230 unlock_page(fault_page);
30983231 put_page(fault_page);
30993232 } else {
31003233 dax_unlock_mapping_entry(vma->vm_file->f_mapping, pgoff);
31013234 }
3235+ if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3236+ goto uncharge_out;
31023237 return ret;
31033238 uncharge_out:
31043239 mem_cgroup_cancel_charge(new_page, memcg, false);
@@ -3106,18 +3241,15 @@ uncharge_out: · 1 unmodified line
31063241 return ret;
31073242 }
31083243
3109−static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3110− unsigned long address, pmd_t *pmd,
3111− pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
3244+static int do_shared_fault(struct fault_env *fe, pgoff_t pgoff)
31123245 {
3246+ struct vm_area_struct *vma = fe->vma;
31133247 struct page *fault_page;
31143248 struct address_space *mapping;
3115− spinlock_t *ptl;
3116− pte_t *pte;
31173249 int dirtied = 0;
31183250 int ret, tmp;
31193251
3120− ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page, NULL);
3252+ ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
31213253 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
31223254 return ret;
31233255
@@ -3127,7 +3259,7 @@ static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma, · 3 unmodified lines
31273259 */
31283260 if (vma->vm_ops->page_mkwrite) {
31293261 unlock_page(fault_page);
3130− tmp = do_page_mkwrite(vma, fault_page, address);
3262+ tmp = do_page_mkwrite(vma, fault_page, fe->address);
31313263 if (unlikely(!tmp ||
31323264 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
31333265 put_page(fault_page);
@@ -3135,15 +3267,15 @@ static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma, · 1 unmodified line
31353267 }
31363268 }
31373269
3138− pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3139− if (unlikely(!pte_same(*pte, orig_pte))) {
3140− pte_unmap_unlock(pte, ptl);
3270+ ret |= alloc_set_pte(fe, NULL, fault_page);
3271+ if (fe->pte)
3272+ pte_unmap_unlock(fe->pte, fe->ptl);
3273+ if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3274+ VM_FAULT_RETRY))) {
31413275 unlock_page(fault_page);
31423276 put_page(fault_page);
31433277 return ret;
31443278 }
3145− do_set_pte(vma, address, fault_page, pte, true, false);
3146− pte_unmap_unlock(pte, ptl);
31473279
31483280 if (set_page_dirty(fault_page))
31493281 dirtied = 1;
@@ -3175,23 +3307,19 @@ static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma, · 25 unmodified lines
31753307 * The mmap_sem may have been released depending on flags and our
31763308 * return value. See filemap_fault() and __lock_page_or_retry().
31773309 */
3178−static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3179− unsigned long address, pte_t *page_table, pmd_t *pmd,
3180− unsigned int flags, pte_t orig_pte)
3310+static int do_fault(struct fault_env *fe)
31813311 {
3182− pgoff_t pgoff = linear_page_index(vma, address);
3312+ struct vm_area_struct *vma = fe->vma;
3313+ pgoff_t pgoff = linear_page_index(vma, fe->address);
31833314
3184− pte_unmap(page_table);
31853315 /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
31863316 if (!vma->vm_ops->fault)
31873317 return VM_FAULT_SIGBUS;
3188− if (!(flags & FAULT_FLAG_WRITE))
3189− return do_read_fault(mm, vma, address, pmd, pgoff, flags,
3190− orig_pte);
3318+ if (!(fe->flags & FAULT_FLAG_WRITE))
3319+ return do_read_fault(fe, pgoff);
31913320 if (!(vma->vm_flags & VM_SHARED))
3192− return do_cow_fault(mm, vma, address, pmd, pgoff, flags,
3193− orig_pte);
3194− return do_shared_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
3321+ return do_cow_fault(fe, pgoff);
3322+ return do_shared_fault(fe, pgoff);
31953323 }
31963324
31973325 static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
@@ -3209,11 +3337,10 @@ static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, · 11 unmodified lines
32093337 return mpol_misplaced(page, vma, addr);
32103338 }
32113339
3212−static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3213− unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3340+static int do_numa_page(struct fault_env *fe, pte_t pte)
32143341 {
3342+ struct vm_area_struct *vma = fe->vma;
32153343 struct page *page = NULL;
3216− spinlock_t *ptl;
32173344 int page_nid = -1;
32183345 int last_cpupid;
32193346 int target_nid;
@@ -3233,10 +3360,10 @@ static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 13 unmodified lines
32333360 * page table entry is not accessible, so there would be no
32343361 * concurrent hardware modifications to the PTE.
32353362 */
3236− ptl = pte_lockptr(mm, pmd);
3237− spin_lock(ptl);
3238− if (unlikely(!pte_same(*ptep, pte))) {
3239− pte_unmap_unlock(ptep, ptl);
3363+ fe->ptl = pte_lockptr(vma->vm_mm, fe->pmd);
3364+ spin_lock(fe->ptl);
3365+ if (unlikely(!pte_same(*fe->pte, pte))) {
3366+ pte_unmap_unlock(fe->pte, fe->ptl);
32403367 goto out;
32413368 }
32423369
@@ -3245,18 +3372,18 @@ static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 2 unmodified lines
32453372 pte = pte_mkyoung(pte);
32463373 if (was_writable)
32473374 pte = pte_mkwrite(pte);
3248− set_pte_at(mm, addr, ptep, pte);
3249− update_mmu_cache(vma, addr, ptep);
3375+ set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
3376+ update_mmu_cache(vma, fe->address, fe->pte);
32503377
3251− page = vm_normal_page(vma, addr, pte);
3378+ page = vm_normal_page(vma, fe->address, pte);
32523379 if (!page) {
3253− pte_unmap_unlock(ptep, ptl);
3380+ pte_unmap_unlock(fe->pte, fe->ptl);
32543381 return 0;
32553382 }
32563383
32573384 /* TODO: handle PTE-mapped THP */
32583385 if (PageCompound(page)) {
3259− pte_unmap_unlock(ptep, ptl);
3386+ pte_unmap_unlock(fe->pte, fe->ptl);
32603387 return 0;
32613388 }
32623389
@@ -3280,8 +3407,9 @@ static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, · 17 unmodified lines
32803407
32813408 last_cpupid = page_cpupid_last(page);
32823409 page_nid = page_to_nid(page);
3283− target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags);
3284− pte_unmap_unlock(ptep, ptl);
3410+ target_nid = numa_migrate_prep(page, vma, fe->address, page_nid,
3411+ &flags);
3412+ pte_unmap_unlock(fe->pte, fe->ptl);
32853413 if (target_nid == -1) {
32863414 put_page(page);
32873415 goto out;
@@ -3301,24 +3429,29 @@ out: · 13 unmodified lines
33013429 return 0;
33023430 }
33033431
3304−static int create_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
3305− unsigned long address, pmd_t *pmd, unsigned int flags)
3432+static int create_huge_pmd(struct fault_env *fe)
33063433 {
3434+ struct vm_area_struct *vma = fe->vma;
33073435 if (vma_is_anonymous(vma))
3308− return do_huge_pmd_anonymous_page(mm, vma, address, pmd, flags);
3436+ return do_huge_pmd_anonymous_page(fe);
33093437 if (vma->vm_ops->pmd_fault)
3310− return vma->vm_ops->pmd_fault(vma, address, pmd, flags);
3438+ return vma->vm_ops->pmd_fault(vma, fe->address, fe->pmd,
3439+ fe->flags);
33113440 return VM_FAULT_FALLBACK;
33123441 }
33133442
3314−static int wp_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
3315− unsigned long address, pmd_t *pmd, pmd_t orig_pmd,
3316− unsigned int flags)
3443+static int wp_huge_pmd(struct fault_env *fe, pmd_t orig_pmd)
33173444 {
3318− if (vma_is_anonymous(vma))
3319− return do_huge_pmd_wp_page(mm, vma, address, pmd, orig_pmd);
3320− if (vma->vm_ops->pmd_fault)
3321− return vma->vm_ops->pmd_fault(vma, address, pmd, flags);
3445+ if (vma_is_anonymous(fe->vma))
3446+ return do_huge_pmd_wp_page(fe, orig_pmd);
3447+ if (fe->vma->vm_ops->pmd_fault)
3448+ return fe->vma->vm_ops->pmd_fault(fe->vma, fe->address, fe->pmd,
3449+ fe->flags);
3450+
3451+ /* COW handled on pte level: split pmd */
3452+ VM_BUG_ON_VMA(fe->vma->vm_flags & VM_SHARED, fe->vma);
3453+ split_huge_pmd(fe->vma, fe->pmd, fe->address);
3454+
33223455 return VM_FAULT_FALLBACK;
33233456 }
33243457
@@ -3331,59 +3464,79 @@ static int wp_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma, · 6 unmodified lines
33313464 * with external mmu caches can use to update those (ie the Sparc or
33323465 * PowerPC hashed page tables that act as extended TLBs).
33333466 *
3334− * We enter with non-exclusive mmap_sem (to exclude vma changes,
3335− * but allow concurrent faults), and pte mapped but not yet locked.
3336− * We return with pte unmapped and unlocked.
3467+ * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
3468+ * concurrent faults).
33373469 *
3338− * The mmap_sem may have been released depending on flags and our
3339− * return value. See filemap_fault() and __lock_page_or_retry().
3470+ * The mmap_sem may have been released depending on flags and our return value.
3471+ * See filemap_fault() and __lock_page_or_retry().
33403472 */
3341−static int handle_pte_fault(struct mm_struct *mm,
3342− struct vm_area_struct *vma, unsigned long address,
3343− pte_t *pte, pmd_t *pmd, unsigned int flags)
3473+static int handle_pte_fault(struct fault_env *fe)
33443474 {
33453475 pte_t entry;
3346− spinlock_t *ptl;
33473476
3348− /*
3349− * some architectures can have larger ptes than wordsize,
3350− * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and CONFIG_32BIT=y,
3351− * so READ_ONCE or ACCESS_ONCE cannot guarantee atomic accesses.
3352− * The code below just needs a consistent view for the ifs and
3353− * we later double check anyway with the ptl lock held. So here
3354− * a barrier will do.
3355− */
3356− entry = *pte;
3357− barrier();
3358− if (!pte_present(entry)) {
3477+ if (unlikely(pmd_none(*fe->pmd))) {
3478+ /*
3479+ * Leave __pte_alloc() until later: because vm_ops->fault may
3480+ * want to allocate huge page, and if we expose page table
3481+ * for an instant, it will be difficult to retract from
3482+ * concurrent faults and from rmap lookups.
3483+ */
3484+ fe->pte = NULL;
3485+ } else {
3486+ /* See comment in pte_alloc_one_map() */
3487+ if (pmd_trans_unstable(fe->pmd) || pmd_devmap(*fe->pmd))
3488+ return 0;
3489+ /*
3490+ * A regular pmd is established and it can't morph into a huge
3491+ * pmd from under us anymore at this point because we hold the
3492+ * mmap_sem read mode and khugepaged takes it in write mode.
3493+ * So now it's safe to run pte_offset_map().
3494+ */
3495+ fe->pte = pte_offset_map(fe->pmd, fe->address);
3496+
3497+ entry = *fe->pte;
3498+
3499+ /*
3500+ * some architectures can have larger ptes than wordsize,
3501+ * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
3502+ * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee
3503+ * atomic accesses. The code below just needs a consistent
3504+ * view for the ifs and we later double check anyway with the
3505+ * ptl lock held. So here a barrier will do.
3506+ */
3507+ barrier();
33593508 if (pte_none(entry)) {
3360− if (vma_is_anonymous(vma))
3361− return do_anonymous_page(mm, vma, address,
3362− pte, pmd, flags);
3363− else
3364− return do_fault(mm, vma, address, pte, pmd,
3365− flags, entry);
3509+ pte_unmap(fe->pte);
3510+ fe->pte = NULL;
33663511 }
3367− return do_swap_page(mm, vma, address,
3368− pte, pmd, flags, entry);
33693512 }
33703513
3514+ if (!fe->pte) {
3515+ if (vma_is_anonymous(fe->vma))
3516+ return do_anonymous_page(fe);
3517+ else
3518+ return do_fault(fe);
3519+ }
3520+
3521+ if (!pte_present(entry))
3522+ return do_swap_page(fe, entry);
3523+
33713524 if (pte_protnone(entry))
3372− return do_numa_page(mm, vma, address, entry, pte, pmd);
3525+ return do_numa_page(fe, entry);
33733526
3374− ptl = pte_lockptr(mm, pmd);
3375− spin_lock(ptl);
3376− if (unlikely(!pte_same(*pte, entry)))
3527+ fe->ptl = pte_lockptr(fe->vma->vm_mm, fe->pmd);
3528+ spin_lock(fe->ptl);
3529+ if (unlikely(!pte_same(*fe->pte, entry)))
33773530 goto unlock;
3378− if (flags & FAULT_FLAG_WRITE) {
3531+ if (fe->flags & FAULT_FLAG_WRITE) {
33793532 if (!pte_write(entry))
3380− return do_wp_page(mm, vma, address,
3381− pte, pmd, ptl, entry);
3533+ return do_wp_page(fe, entry);
33823534 entry = pte_mkdirty(entry);
33833535 }
33843536 entry = pte_mkyoung(entry);
3385− if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
3386− update_mmu_cache(vma, address, pte);
3537+ if (ptep_set_access_flags(fe->vma, fe->address, fe->pte, entry,
3538+ fe->flags & FAULT_FLAG_WRITE)) {
3539+ update_mmu_cache(fe->vma, fe->address, fe->pte);
33873540 } else {
33883541 /*
33893542 * This is needed only for protection faults but the arch code
@@ -3391,11 +3544,11 @@ static int handle_pte_fault(struct mm_struct *mm, · 1 unmodified line
33913544 * This still avoids useless tlb flushes for .text page faults
33923545 * with threads.
33933546 */
3394− if (flags & FAULT_FLAG_WRITE)
3395− flush_tlb_fix_spurious_fault(vma, address);
3547+ if (fe->flags & FAULT_FLAG_WRITE)
3548+ flush_tlb_fix_spurious_fault(fe->vma, fe->address);
33963549 }
33973550 unlock:
3398− pte_unmap_unlock(pte, ptl);
3551+ pte_unmap_unlock(fe->pte, fe->ptl);
33993552 return 0;
34003553 }
34013554
@@ -3405,87 +3558,51 @@ unlock: · 3 unmodified lines
34053558 * The mmap_sem may have been released depending on flags and our
34063559 * return value. See filemap_fault() and __lock_page_or_retry().
34073560 */
3408−static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3409− unsigned long address, unsigned int flags)
3561+static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
3562+ unsigned int flags)
34103563 {
3564+ struct fault_env fe = {
3565+ .vma = vma,
3566+ .address = address,
3567+ .flags = flags,
3568+ };
3569+ struct mm_struct *mm = vma->vm_mm;
34113570 pgd_t *pgd;
34123571 pud_t *pud;
3413− pmd_t *pmd;
3414− pte_t *pte;
3415−
3416− if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
3417− flags & FAULT_FLAG_INSTRUCTION,
3418− flags & FAULT_FLAG_REMOTE))
3419− return VM_FAULT_SIGSEGV;
3420−
3421− if (unlikely(is_vm_hugetlb_page(vma)))
3422− return hugetlb_fault(mm, vma, address, flags);
34233572
34243573 pgd = pgd_offset(mm, address);
34253574 pud = pud_alloc(mm, pgd, address);
34263575 if (!pud)
34273576 return VM_FAULT_OOM;
3428− pmd = pmd_alloc(mm, pud, address);
3429− if (!pmd)
3577+ fe.pmd = pmd_alloc(mm, pud, address);
3578+ if (!fe.pmd)
34303579 return VM_FAULT_OOM;
3431− if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
3432− int ret = create_huge_pmd(mm, vma, address, pmd, flags);
3580+ if (pmd_none(*fe.pmd) && transparent_hugepage_enabled(vma)) {
3581+ int ret = create_huge_pmd(&fe);
34333582 if (!(ret & VM_FAULT_FALLBACK))
34343583 return ret;
34353584 } else {
3436− pmd_t orig_pmd = *pmd;
3585+ pmd_t orig_pmd = *fe.pmd;
34373586 int ret;
34383587
34393588 barrier();
34403589 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
3441− unsigned int dirty = flags & FAULT_FLAG_WRITE;
3442−
34433590 if (pmd_protnone(orig_pmd))
3444− return do_huge_pmd_numa_page(mm, vma, address,
3445− orig_pmd, pmd);
3591+ return do_huge_pmd_numa_page(&fe, orig_pmd);
34463592
3447− if (dirty && !pmd_write(orig_pmd)) {
3448− ret = wp_huge_pmd(mm, vma, address, pmd,
3449− orig_pmd, flags);
3593+ if ((fe.flags & FAULT_FLAG_WRITE) &&
3594+ !pmd_write(orig_pmd)) {
3595+ ret = wp_huge_pmd(&fe, orig_pmd);
34503596 if (!(ret & VM_FAULT_FALLBACK))
34513597 return ret;
34523598 } else {
3453− huge_pmd_set_accessed(mm, vma, address, pmd,
3454− orig_pmd, dirty);
3599+ huge_pmd_set_accessed(&fe, orig_pmd);
34553600 return 0;
34563601 }
34573602 }
34583603 }
34593604
3460− /*
3461− * Use pte_alloc() instead of pte_alloc_map, because we can't
3462− * run pte_offset_map on the pmd, if an huge pmd could
3463− * materialize from under us from a different thread.
3464− */
3465− if (unlikely(pte_alloc(mm, pmd, address)))
3466− return VM_FAULT_OOM;
3467− /*
3468− * If a huge pmd materialized under us just retry later. Use
3469− * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd
3470− * didn't become pmd_trans_huge under us and then back to pmd_none, as
3471− * a result of MADV_DONTNEED running immediately after a huge pmd fault
3472− * in a different thread of this mm, in turn leading to a misleading
3473− * pmd_trans_huge() retval. All we have to ensure is that it is a
3474− * regular pmd that we can walk with pte_offset_map() and we can do that
3475− * through an atomic read in C, which is what pmd_trans_unstable()
3476− * provides.
3477− */
3478− if (unlikely(pmd_trans_unstable(pmd) || pmd_devmap(*pmd)))
3479− return 0;
3480− /*
3481− * A regular pmd is established and it can't morph into a huge pmd
3482− * from under us anymore at this point because we hold the mmap_sem
3483− * read mode and khugepaged takes it in write mode. So now it's
3484− * safe to run pte_offset_map().
3485− */
3486− pte = pte_offset_map(pmd, address);
3487−
3488− return handle_pte_fault(mm, vma, address, pte, pmd, flags);
3605+ return handle_pte_fault(&fe);
34893606 }
34903607
34913608 /*
@@ -3494,15 +3611,15 @@ static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, · 2 unmodified lines
34943611 * The mmap_sem may have been released depending on flags and our
34953612 * return value. See filemap_fault() and __lock_page_or_retry().
34963613 */
3497−int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3498− unsigned long address, unsigned int flags)
3614+int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
3615+ unsigned int flags)
34993616 {
35003617 int ret;
35013618
35023619 __set_current_state(TASK_RUNNING);
35033620
35043621 count_vm_event(PGFAULT);
3505− mem_cgroup_count_vm_event(mm, PGFAULT);
3622+ mem_cgroup_count_vm_event(vma->vm_mm, PGFAULT);
35063623
35073624 /* do counter updates before entering really critical section. */
35083625 check_sync_rss_stat(current);
@@ -3514,7 +3631,15 @@ int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, · 5 unmodified lines
35143631 if (flags & FAULT_FLAG_USER)
35153632 mem_cgroup_oom_enable();
35163633
3517− ret = __handle_mm_fault(mm, vma, address, flags);
3634+ if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
3635+ flags & FAULT_FLAG_INSTRUCTION,
3636+ flags & FAULT_FLAG_REMOTE))
3637+ return VM_FAULT_SIGSEGV;
3638+
3639+ if (unlikely(is_vm_hugetlb_page(vma)))
3640+ ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
3641+ else
3642+ ret = __handle_mm_fault(vma, address, flags);
35183643
35193644 if (flags & FAULT_FLAG_USER) {
35203645 mem_cgroup_oom_disable();
mm/memory_hotplug.c+58−12
@@ -449,6 +449,25 @@ out_fail: · 448 unmodified lines
449449 return -1;
450450 }
451451
452+static struct zone * __meminit move_pfn_range(int zone_shift,
453+ unsigned long start_pfn, unsigned long end_pfn)
454+{
455+ struct zone *zone = page_zone(pfn_to_page(start_pfn));
456+ int ret = 0;
457+
458+ if (zone_shift < 0)
459+ ret = move_pfn_range_left(zone + zone_shift, zone,
460+ start_pfn, end_pfn);
461+ else if (zone_shift)
462+ ret = move_pfn_range_right(zone, zone + zone_shift,
463+ start_pfn, end_pfn);
464+
465+ if (ret)
466+ return NULL;
467+
468+ return zone + zone_shift;
469+}
470+
452471 static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
453472 unsigned long end_pfn)
454473 {
@@ -1028,6 +1047,37 @@ static void node_states_set_node(int node, struct memory_notify *arg) · 573 unmodified lines
10281047 node_set_state(node, N_MEMORY);
10291048 }
10301049
1050+int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
1051+ enum zone_type target)
1052+{
1053+ struct zone *zone = page_zone(pfn_to_page(pfn));
1054+ enum zone_type idx = zone_idx(zone);
1055+ int i;
1056+
1057+ if (idx < target) {
1058+ /* pages must be at end of current zone */
1059+ if (pfn + nr_pages != zone_end_pfn(zone))
1060+ return 0;
1061+
1062+ /* no zones in use between current zone and target */
1063+ for (i = idx + 1; i < target; i++)
1064+ if (zone_is_initialized(zone - idx + i))
1065+ return 0;
1066+ }
1067+
1068+ if (target < idx) {
1069+ /* pages must be at beginning of current zone */
1070+ if (pfn != zone->zone_start_pfn)
1071+ return 0;
1072+
1073+ /* no zones in use between current zone and target */
1074+ for (i = target + 1; i < idx; i++)
1075+ if (zone_is_initialized(zone - idx + i))
1076+ return 0;
1077+ }
1078+
1079+ return target - idx;
1080+}
10311081
10321082 /* Must be protected by mem_hotplug_begin() */
10331083 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
@@ -1039,6 +1089,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ · 5 unmodified lines
10391089 int nid;
10401090 int ret;
10411091 struct memory_notify arg;
1092+ int zone_shift = 0;
10421093
10431094 /*
10441095 * This doesn't need a lock to do pfn_to_page().
@@ -1052,19 +1103,14 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ · 7 unmodified lines
10521103 !can_online_high_movable(zone))
10531104 return -EINVAL;
10541105
1055− if (online_type == MMOP_ONLINE_KERNEL &&
1056− zone_idx(zone) == ZONE_MOVABLE) {
1057− if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
1058− return -EINVAL;
1059− }
1060− if (online_type == MMOP_ONLINE_MOVABLE &&
1061− zone_idx(zone) == ZONE_MOVABLE - 1) {
1062− if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
1063− return -EINVAL;
1064− }
1106+ if (online_type == MMOP_ONLINE_KERNEL)
1107+ zone_shift = zone_can_shift(pfn, nr_pages, ZONE_NORMAL);
1108+ else if (online_type == MMOP_ONLINE_MOVABLE)
1109+ zone_shift = zone_can_shift(pfn, nr_pages, ZONE_MOVABLE);
10651110
1066− /* Previous code may changed the zone of the pfn range */
1067− zone = page_zone(pfn_to_page(pfn));
1111+ zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
1112+ if (!zone)
1113+ return -EINVAL;
10681114
10691115 arg.start_pfn = pfn;
10701116 arg.nr_pages = nr_pages;
mm/mempolicy.c+3−1
@@ -512,6 +512,8 @@ static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr, · 511 unmodified lines
512512 }
513513 }
514514
515+ if (pmd_trans_unstable(pmd))
516+ return 0;
515517 retry:
516518 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
517519 for (; addr != end; pte++, addr += PAGE_SIZE) {
@@ -529,7 +531,7 @@ retry: · 11 unmodified lines
529531 nid = page_to_nid(page);
530532 if (node_isset(nid, *qp->nmask) == !!(flags & MPOL_MF_INVERT))
531533 continue;
532− if (PageTransCompound(page) && PageAnon(page)) {
534+ if (PageTransCompound(page)) {
533535 get_page(page);
534536 pte_unmap_unlock(pte, ptl);
535537 lock_page(page);
mm/migrate.c+209−53
@@ -31,6 +31,7 @@ · 30 unmodified lines
3131 #include <linux/vmalloc.h>
3232 #include <linux/security.h>
3333 #include <linux/backing-dev.h>
34+#include <linux/compaction.h>
3435 #include <linux/syscalls.h>
3536 #include <linux/hugetlb.h>
3637 #include <linux/hugetlb_cgroup.h>
@@ -73,6 +74,81 @@ int migrate_prep_local(void) · 36 unmodified lines
7374 return 0;
7475 }
7576
77+bool isolate_movable_page(struct page *page, isolate_mode_t mode)
78+{
79+ struct address_space *mapping;
80+
81+ /*
82+ * Avoid burning cycles with pages that are yet under __free_pages(),
83+ * or just got freed under us.
84+ *
85+ * In case we 'win' a race for a movable page being freed under us and
86+ * raise its refcount preventing __free_pages() from doing its job
87+ * the put_page() at the end of this block will take care of
88+ * release this page, thus avoiding a nasty leakage.
89+ */
90+ if (unlikely(!get_page_unless_zero(page)))
91+ goto out;
92+
93+ /*
94+ * Check PageMovable before holding a PG_lock because page's owner
95+ * assumes anybody doesn't touch PG_lock of newly allocated page
96+ * so unconditionally grapping the lock ruins page's owner side.
97+ */
98+ if (unlikely(!__PageMovable(page)))
99+ goto out_putpage;
100+ /*
101+ * As movable pages are not isolated from LRU lists, concurrent
102+ * compaction threads can race against page migration functions
103+ * as well as race against the releasing a page.
104+ *
105+ * In order to avoid having an already isolated movable page
106+ * being (wrongly) re-isolated while it is under migration,
107+ * or to avoid attempting to isolate pages being released,
108+ * lets be sure we have the page lock
109+ * before proceeding with the movable page isolation steps.
110+ */
111+ if (unlikely(!trylock_page(page)))
112+ goto out_putpage;
113+
114+ if (!PageMovable(page) || PageIsolated(page))
115+ goto out_no_isolated;
116+
117+ mapping = page_mapping(page);
118+ VM_BUG_ON_PAGE(!mapping, page);
119+
120+ if (!mapping->a_ops->isolate_page(page, mode))
121+ goto out_no_isolated;
122+
123+ /* Driver shouldn't use PG_isolated bit of page->flags */
124+ WARN_ON_ONCE(PageIsolated(page));
125+ __SetPageIsolated(page);
126+ unlock_page(page);
127+
128+ return true;
129+
130+out_no_isolated:
131+ unlock_page(page);
132+out_putpage:
133+ put_page(page);
134+out:
135+ return false;
136+}
137+
138+/* It should be called on page which is PG_movable */
139+void putback_movable_page(struct page *page)
140+{
141+ struct address_space *mapping;
142+
143+ VM_BUG_ON_PAGE(!PageLocked(page), page);
144+ VM_BUG_ON_PAGE(!PageMovable(page), page);
145+ VM_BUG_ON_PAGE(!PageIsolated(page), page);
146+
147+ mapping = page_mapping(page);
148+ mapping->a_ops->putback_page(page);
149+ __ClearPageIsolated(page);
150+}
151+
76152 /*
77153 * Put previously isolated pages back onto the appropriate lists
78154 * from where they were once taken off for compaction/migration.
@@ -94,10 +170,23 @@ void putback_movable_pages(struct list_head *l) · 15 unmodified lines
94170 list_del(&page->lru);
95171 dec_zone_page_state(page, NR_ISOLATED_ANON +
96172 page_is_file_cache(page));
97− if (unlikely(isolated_balloon_page(page)))
98− balloon_page_putback(page);
99− else
173+ /*
174+ * We isolated non-lru movable page so here we can use
175+ * __PageMovable because LRU page's mapping cannot have
176+ * PAGE_MAPPING_MOVABLE.
177+ */
178+ if (unlikely(__PageMovable(page))) {
179+ VM_BUG_ON_PAGE(!PageIsolated(page), page);
180+ lock_page(page);
181+ if (PageMovable(page))
182+ putback_movable_page(page);
183+ else
184+ __ClearPageIsolated(page);
185+ unlock_page(page);
186+ put_page(page);
187+ } else {
100188 putback_lru_page(page);
189+ }
101190 }
102191 }
103192
@@ -170,7 +259,7 @@ static int remove_migration_pte(struct page *new, struct vm_area_struct *vma, · 66 unmodified lines
170259 } else if (PageAnon(new))
171260 page_add_anon_rmap(new, vma, addr, false);
172261 else
173− page_add_file_rmap(new);
262+ page_add_file_rmap(new, false);
174263
175264 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
176265 mlock_vma_page(new);
@@ -594,7 +683,7 @@ EXPORT_SYMBOL(migrate_page_copy); · 417 unmodified lines
594683 ***********************************************************/
595684
596685 /*
597− * Common logic to directly migrate a single page suitable for
686+ * Common logic to directly migrate a single LRU page suitable for
598687 * pages that do not use PagePrivate/PagePrivate2.
599688 *
600689 * Pages are locked upon entry and exit.
@@ -757,33 +846,72 @@ static int move_to_new_page(struct page *newpage, struct page *page, · 156 unmodified lines
757846 enum migrate_mode mode)
758847 {
759848 struct address_space *mapping;
760− int rc;
849+ int rc = -EAGAIN;
850+ bool is_lru = !__PageMovable(page);
761851
762852 VM_BUG_ON_PAGE(!PageLocked(page), page);
763853 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
764854
765855 mapping = page_mapping(page);
766− if (!mapping)
767− rc = migrate_page(mapping, newpage, page, mode);
768− else if (mapping->a_ops->migratepage)
856+
857+ if (likely(is_lru)) {
858+ if (!mapping)
859+ rc = migrate_page(mapping, newpage, page, mode);
860+ else if (mapping->a_ops->migratepage)
861+ /*
862+ * Most pages have a mapping and most filesystems
863+ * provide a migratepage callback. Anonymous pages
864+ * are part of swap space which also has its own
865+ * migratepage callback. This is the most common path
866+ * for page migration.
867+ */
868+ rc = mapping->a_ops->migratepage(mapping, newpage,
869+ page, mode);
870+ else
871+ rc = fallback_migrate_page(mapping, newpage,
872+ page, mode);
873+ } else {
769874 /*
770− * Most pages have a mapping and most filesystems provide a
771− * migratepage callback. Anonymous pages are part of swap
772− * space which also has its own migratepage callback. This
773− * is the most common path for page migration.
875+ * In case of non-lru page, it could be released after
876+ * isolation step. In that case, we shouldn't try migration.
774877 */
775− rc = mapping->a_ops->migratepage(mapping, newpage, page, mode);
776− else
777− rc = fallback_migrate_page(mapping, newpage, page, mode);
878+ VM_BUG_ON_PAGE(!PageIsolated(page), page);
879+ if (!PageMovable(page)) {
880+ rc = MIGRATEPAGE_SUCCESS;
881+ __ClearPageIsolated(page);
882+ goto out;
883+ }
884+
885+ rc = mapping->a_ops->migratepage(mapping, newpage,
886+ page, mode);
887+ WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
888+ !PageIsolated(page));
889+ }
778890
779891 /*
780892 * When successful, old pagecache page->mapping must be cleared before
781893 * page is freed; but stats require that PageAnon be left as PageAnon.
782894 */
783895 if (rc == MIGRATEPAGE_SUCCESS) {
784− if (!PageAnon(page))
896+ if (__PageMovable(page)) {
897+ VM_BUG_ON_PAGE(!PageIsolated(page), page);
898+
899+ /*
900+ * We clear PG_movable under page_lock so any compactor
901+ * cannot try to migrate this page.
902+ */
903+ __ClearPageIsolated(page);
904+ }
905+
906+ /*
907+ * Anonymous and movable page->mapping will be cleard by
908+ * free_pages_prepare so don't reset it here for keeping
909+ * the type to work PageAnon, for example.
910+ */
911+ if (!PageMappingFlags(page))
785912 page->mapping = NULL;
786913 }
914+out:
787915 return rc;
788916 }
789917
@@ -793,6 +921,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage, · 3 unmodified lines
793921 int rc = -EAGAIN;
794922 int page_was_mapped = 0;
795923 struct anon_vma *anon_vma = NULL;
924+ bool is_lru = !__PageMovable(page);
796925
797926 if (!trylock_page(page)) {
798927 if (!force || mode == MIGRATE_ASYNC)
@@ -861,15 +990,8 @@ static int __unmap_and_move(struct page *page, struct page *newpage, · 62 unmodified lines
861990 if (unlikely(!trylock_page(newpage)))
862991 goto out_unlock;
863992
864− if (unlikely(isolated_balloon_page(page))) {
865− /*
866− * A ballooned page does not need any special attention from
867− * physical to virtual reverse mapping procedures.
868− * Skip any attempt to unmap PTEs or to remap swap cache,
869− * in order to avoid burning cycles at rmap level, and perform
870− * the page migration right away (proteced by page lock).
871− */
872− rc = balloon_page_migrate(newpage, page, mode);
993+ if (unlikely(!is_lru)) {
994+ rc = move_to_new_page(newpage, page, mode);
873995 goto out_unlock_both;
874996 }
875997
@@ -915,6 +1037,19 @@ out_unlock: · 39 unmodified lines
9151037 put_anon_vma(anon_vma);
9161038 unlock_page(page);
9171039 out:
1040+ /*
1041+ * If migration is successful, decrease refcount of the newpage
1042+ * which will not free the page because new page owner increased
1043+ * refcounter. As well, if it is LRU page, add the page to LRU
1044+ * list in here.
1045+ */
1046+ if (rc == MIGRATEPAGE_SUCCESS) {
1047+ if (unlikely(__PageMovable(newpage)))
1048+ put_page(newpage);
1049+ else
1050+ putback_lru_page(newpage);
1051+ }
1052+
9181053 return rc;
9191054 }
9201055
@@ -948,6 +1083,18 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page, · 27 unmodified lines
9481083
9491084 if (page_count(page) == 1) {
9501085 /* page was freed from under us. So we are done. */
1086+ ClearPageActive(page);
1087+ ClearPageUnevictable(page);
1088+ if (unlikely(__PageMovable(page))) {
1089+ lock_page(page);
1090+ if (!PageMovable(page))
1091+ __ClearPageIsolated(page);
1092+ unlock_page(page);
1093+ }
1094+ if (put_new_page)
1095+ put_new_page(newpage, private);
1096+ else
1097+ put_page(newpage);
9511098 goto out;
9521099 }
9531100
@@ -960,10 +1107,8 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page, · 6 unmodified lines
9601107 }
9611108
9621109 rc = __unmap_and_move(page, newpage, force, mode);
963− if (rc == MIGRATEPAGE_SUCCESS) {
964− put_new_page = NULL;
1110+ if (rc == MIGRATEPAGE_SUCCESS)
9651111 set_page_owner_migrate_reason(newpage, reason);
966− }
9671112
9681113 out:
9691114 if (rc != -EAGAIN) {
@@ -976,33 +1121,45 @@ out: · 6 unmodified lines
9761121 list_del(&page->lru);
9771122 dec_zone_page_state(page, NR_ISOLATED_ANON +
9781123 page_is_file_cache(page));
979− /* Soft-offlined page shouldn't go through lru cache list */
980− if (reason == MR_MEMORY_FAILURE && rc == MIGRATEPAGE_SUCCESS) {
1124+ }
1125+
1126+ /*
1127+ * If migration is successful, releases reference grabbed during
1128+ * isolation. Otherwise, restore the page to right list unless
1129+ * we want to retry.
1130+ */
1131+ if (rc == MIGRATEPAGE_SUCCESS) {
1132+ put_page(page);
1133+ if (reason == MR_MEMORY_FAILURE) {
9811134 /*
982− * With this release, we free successfully migrated
983− * page and set PG_HWPoison on just freed page
984− * intentionally. Although it's rather weird, it's how
985− * HWPoison flag works at the moment.
1135+ * Set PG_HWPoison on just freed page
1136+ * intentionally. Although it's rather weird,
1137+ * it's how HWPoison flag works at the moment.
9861138 */
987− put_page(page);
9881139 if (!test_set_page_hwpoison(page))
9891140 num_poisoned_pages_inc();
990− } else
991− putback_lru_page(page);
992− }
1141+ }
1142+ } else {
1143+ if (rc != -EAGAIN) {
1144+ if (likely(!__PageMovable(page))) {
1145+ putback_lru_page(page);
1146+ goto put_new;
1147+ }
9931148
994− /*
995− * If migration was not successful and there's a freeing callback, use
996− * it. Otherwise, putback_lru_page() will drop the reference grabbed
997− * during isolation.
998− */
999− if (put_new_page)
1000− put_new_page(newpage, private);
1001− else if (unlikely(__is_movable_balloon_page(newpage))) {
1002− /* drop our reference, page already in the balloon */
1003− put_page(newpage);
1004− } else
1005− putback_lru_page(newpage);
1149+ lock_page(page);
1150+ if (PageMovable(page))
1151+ putback_movable_page(page);
1152+ else
1153+ __ClearPageIsolated(page);
1154+ unlock_page(page);
1155+ put_page(page);
1156+ }
1157+put_new:
1158+ if (put_new_page)
1159+ put_new_page(newpage, private);
1160+ else
1161+ put_page(newpage);
1162+ }
10061163
10071164 if (result) {
10081165 if (rc)
@@ -1829,8 +1986,7 @@ fail_putback: · 820 unmodified lines
18291986 }
18301987
18311988 orig_entry = *pmd;
1832− entry = mk_pmd(new_page, vma->vm_page_prot);
1833− entry = pmd_mkhuge(entry);
1989+ entry = mk_huge_pmd(new_page, vma->vm_page_prot);
18341990 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
18351991
18361992 /*
mm/mmap.c+22−4
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525 #include <linux/personality.h>
2626 #include <linux/security.h>
2727 #include <linux/hugetlb.h>
28+#include <linux/shmem_fs.h>
2829 #include <linux/profile.h>
2930 #include <linux/export.h>
3031 #include <linux/mount.h>
@@ -675,6 +676,8 @@ again: remove_next = 1 + (end > next->vm_end); · 644 unmodified lines
675676 }
676677 }
677678
679+ vma_adjust_trans_huge(vma, start, end, adjust_next);
680+
678681 if (file) {
679682 mapping = file->f_mapping;
680683 root = &mapping->i_mmap;
@@ -695,8 +698,6 @@ again: remove_next = 1 + (end > next->vm_end); · 14 unmodified lines
695698 }
696699 }
697700
698− vma_adjust_trans_huge(vma, start, end, adjust_next);
699−
700701 anon_vma = vma->anon_vma;
701702 if (!anon_vma && adjust_next)
702703 anon_vma = next->anon_vma;
@@ -1897,8 +1898,19 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, · 1194 unmodified lines
18971898 return -ENOMEM;
18981899
18991900 get_area = current->mm->get_unmapped_area;
1900− if (file && file->f_op->get_unmapped_area)
1901− get_area = file->f_op->get_unmapped_area;
1901+ if (file) {
1902+ if (file->f_op->get_unmapped_area)
1903+ get_area = file->f_op->get_unmapped_area;
1904+ } else if (flags & MAP_SHARED) {
1905+ /*
1906+ * mmap_region() will call shmem_zero_setup() to create a file,
1907+ * so use shmem's get_unmapped_area in case it can be huge.
1908+ * do_mmap_pgoff() will clear pgoff, so match alignment.
1909+ */
1910+ pgoff = 0;
1911+ get_area = shmem_get_unmapped_area;
1912+ }
1913+
19021914 addr = get_area(file, addr, len, pgoff, flags);
19031915 if (IS_ERR_VALUE(addr))
19041916 return addr;
@@ -2591,6 +2603,12 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, · 686 unmodified lines
25912603 /* drop PG_Mlocked flag for over-mapped range */
25922604 for (tmp = vma; tmp->vm_start >= start + size;
25932605 tmp = tmp->vm_next) {
2606+ /*
2607+ * Split pmd and munlock page on the border
2608+ * of the range.
2609+ */
2610+ vma_adjust_trans_huge(tmp, start, start + size, 0);
2611+
25942612 munlock_vma_pages_range(tmp,
25952613 max(tmp->vm_start, start),
25962614 min(tmp->vm_end, start + size));
mm/mprotect.c+1−1
@@ -163,7 +163,7 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma, · 162 unmodified lines
163163 if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
164164 if (next - addr != HPAGE_PMD_SIZE) {
165165 split_huge_pmd(vma, pmd, addr);
166− if (pmd_none(*pmd))
166+ if (pmd_trans_unstable(pmd))
167167 continue;
168168 } else {
169169 int nr_ptes = change_huge_pmd(vma, pmd, addr,
mm/mremap.c+1−2
@@ -210,9 +210,8 @@ unsigned long move_page_tables(struct vm_area_struct *vma, · 209 unmodified lines
210210 }
211211 }
212212 split_huge_pmd(vma, old_pmd, old_addr);
213− if (pmd_none(*old_pmd))
213+ if (pmd_trans_unstable(old_pmd))
214214 continue;
215− VM_BUG_ON(pmd_trans_huge(*old_pmd));
216215 }
217216 if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr))
218217 break;
mm/nommu.c+2−1
@@ -1809,7 +1809,8 @@ int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · 1808 unmodified lines
18091809 }
18101810 EXPORT_SYMBOL(filemap_fault);
18111811
1812−void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
1812+void filemap_map_pages(struct fault_env *fe,
1813+ pgoff_t start_pgoff, pgoff_t end_pgoff)
18131814 {
18141815 BUG();
18151816 }
mm/oom_kill.c+37−28
@@ -274,7 +274,7 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc, · 273 unmodified lines
274274 #endif
275275
276276 enum oom_scan_t oom_scan_process_thread(struct oom_control *oc,
277− struct task_struct *task, unsigned long totalpages)
277+ struct task_struct *task)
278278 {
279279 if (oom_unkillable_task(task, NULL, oc->nodemask))
280280 return OOM_SCAN_CONTINUE;
@@ -311,7 +311,7 @@ static struct task_struct *select_bad_process(struct oom_control *oc, · 30 unmodified lines
311311 for_each_process(p) {
312312 unsigned int points;
313313
314− switch (oom_scan_process_thread(oc, p, totalpages)) {
314+ switch (oom_scan_process_thread(oc, p)) {
315315 case OOM_SCAN_SELECT:
316316 chosen = p;
317317 chosen_points = ULONG_MAX;
@@ -383,8 +383,7 @@ static void dump_tasks(struct mem_cgroup *memcg, const nodemask_t *nodemask) · 65 unmodified lines
383383 rcu_read_unlock();
384384 }
385385
386−static void dump_header(struct oom_control *oc, struct task_struct *p,
387− struct mem_cgroup *memcg)
386+static void dump_header(struct oom_control *oc, struct task_struct *p)
388387 {
389388 pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
390389 current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
@@ -392,12 +391,12 @@ static void dump_header(struct oom_control *oc, struct task_struct *p, · 1 unmodified line
392391
393392 cpuset_print_current_mems_allowed();
394393 dump_stack();
395− if (memcg)
396− mem_cgroup_print_oom_info(memcg, p);
394+ if (oc->memcg)
395+ mem_cgroup_print_oom_info(oc->memcg, p);
397396 else
398397 show_mem(SHOW_MEM_FILTER_NODES);
399398 if (sysctl_oom_dump_tasks)
400− dump_tasks(memcg, oc->nodemask);
399+ dump_tasks(oc->memcg, oc->nodemask);
401400 }
402401
403402 /*
@@ -453,7 +452,7 @@ static bool __oom_reap_task(struct task_struct *tsk) · 49 unmodified lines
453452 * We have to make sure to not race with the victim exit path
454453 * and cause premature new oom victim selection:
455454 * __oom_reap_task exit_mm
456− * atomic_inc_not_zero
455+ * mmget_not_zero
457456 * mmput
458457 * atomic_dec_and_test
459458 * exit_oom_victim
@@ -475,12 +474,22 @@ static bool __oom_reap_task(struct task_struct *tsk) · 15 unmodified lines
475474 if (!p)
476475 goto unlock_oom;
477476 mm = p->mm;
478− atomic_inc(&mm->mm_users);
477+ atomic_inc(&mm->mm_count);
479478 task_unlock(p);
480479
481480 if (!down_read_trylock(&mm->mmap_sem)) {
482481 ret = false;
483− goto unlock_oom;
482+ goto mm_drop;
483+ }
484+
485+ /*
486+ * increase mm_users only after we know we will reap something so
487+ * that the mmput_async is called only when we have reaped something
488+ * and delayed __mmput doesn't matter that much
489+ */
490+ if (!mmget_not_zero(mm)) {
491+ up_read(&mm->mmap_sem);
492+ goto mm_drop;
484493 }
485494
486495 tlb_gather_mmu(&tlb, mm, 0, -1);
@@ -522,15 +531,16 @@ static bool __oom_reap_task(struct task_struct *tsk) · 35 unmodified lines
522531 * to release its memory.
523532 */
524533 set_bit(MMF_OOM_REAPED, &mm->flags);
525−unlock_oom:
526− mutex_unlock(&oom_lock);
527534 /*
528535 * Drop our reference but make sure the mmput slow path is called from a
529536 * different context because we shouldn't risk we get stuck there and
530537 * put the oom_reaper out of the way.
531538 */
532− if (mm)
533− mmput_async(mm);
539+ mmput_async(mm);
540+mm_drop:
541+ mmdrop(mm);
542+unlock_oom:
543+ mutex_unlock(&oom_lock);
534544 return ret;
535545 }
536546
@@ -739,7 +749,7 @@ void oom_killer_enable(void) · 202 unmodified lines
739749 */
740750 void oom_kill_process(struct oom_control *oc, struct task_struct *p,
741751 unsigned int points, unsigned long totalpages,
742− struct mem_cgroup *memcg, const char *message)
752+ const char *message)
743753 {
744754 struct task_struct *victim = p;
745755 struct task_struct *child;
@@ -765,7 +775,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, · 19 unmodified lines
765775 task_unlock(p);
766776
767777 if (__ratelimit(&oom_rs))
768− dump_header(oc, p, memcg);
778+ dump_header(oc, p);
769779
770780 pr_err("%s: Kill process %d (%s) score %u or sacrifice child\n",
771781 message, task_pid_nr(p), p->comm, points);
@@ -786,8 +796,8 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, · 14 unmodified lines
786796 /*
787797 * oom_badness() returns 0 if the thread is unkillable
788798 */
789− child_points = oom_badness(child, memcg, oc->nodemask,
790− totalpages);
799+ child_points = oom_badness(child,
800+ oc->memcg, oc->nodemask, totalpages);
791801 if (child_points > victim_points) {
792802 put_task_struct(victim);
793803 victim = child;
@@ -865,8 +875,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, · 71 unmodified lines
865875 /*
866876 * Determines whether the kernel must panic because of the panic_on_oom sysctl.
867877 */
868−void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint,
869− struct mem_cgroup *memcg)
878+void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint)
870879 {
871880 if (likely(!sysctl_panic_on_oom))
872881 return;
@@ -882,7 +891,7 @@ void check_panic_on_oom(struct oom_control *oc, enum oom_constraint constraint, · 9 unmodified lines
882891 /* Do not panic for oom kills triggered by sysrq */
883892 if (is_sysrq_oom(oc))
884893 return;
885− dump_header(oc, NULL, memcg);
894+ dump_header(oc, NULL);
886895 panic("Out of memory: %s panic_on_oom is enabled\n",
887896 sysctl_panic_on_oom == 2 ? "compulsory" : "system-wide");
888897 }
@@ -957,13 +966,13 @@ bool out_of_memory(struct oom_control *oc) · 68 unmodified lines
957966 constraint = constrained_alloc(oc, &totalpages);
958967 if (constraint != CONSTRAINT_MEMORY_POLICY)
959968 oc->nodemask = NULL;
960− check_panic_on_oom(oc, constraint, NULL);
969+ check_panic_on_oom(oc, constraint);
961970
962971 if (sysctl_oom_kill_allocating_task && current->mm &&
963972 !oom_unkillable_task(current, NULL, oc->nodemask) &&
964973 current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
965974 get_task_struct(current);
966− oom_kill_process(oc, current, 0, totalpages, NULL,
975+ oom_kill_process(oc, current, 0, totalpages,
967976 "Out of memory (oom_kill_allocating_task)");
968977 return true;
969978 }
@@ -971,12 +980,11 @@ bool out_of_memory(struct oom_control *oc) · 1 unmodified line
971980 p = select_bad_process(oc, &points, totalpages);
972981 /* Found nothing?!?! Either we hang forever, or we panic. */
973982 if (!p && !is_sysrq_oom(oc)) {
974− dump_header(oc, NULL, NULL);
983+ dump_header(oc, NULL);
975984 panic("Out of memory and no killable processes...\n");
976985 }
977986 if (p && p != (void *)-1UL) {
978− oom_kill_process(oc, p, points, totalpages, NULL,
979− "Out of memory");
987+ oom_kill_process(oc, p, points, totalpages, "Out of memory");
980988 /*
981989 * Give the killed process a good chance to exit before trying
982990 * to allocate memory again.
@@ -988,14 +996,15 @@ bool out_of_memory(struct oom_control *oc) · 5 unmodified lines
988996
989997 /*
990998 * The pagefault handler calls here because it is out of memory, so kill a
991− * memory-hogging task. If any populated zone has ZONE_OOM_LOCKED set, a
992− * parallel oom killing is already in progress so do nothing.
999+ * memory-hogging task. If oom_lock is held by somebody else, a parallel oom
1000+ * killing is already in progress so do nothing.
9931001 */
9941002 void pagefault_out_of_memory(void)
9951003 {
9961004 struct oom_control oc = {
9971005 .zonelist = NULL,
9981006 .nodemask = NULL,
1007+ .memcg = NULL,
9991008 .gfp_mask = 0,
10001009 .order = 0,
10011010 };
mm/page-writeback.c+19−0
@@ -2563,6 +2563,7 @@ int set_page_dirty(struct page *page) · 2562 unmodified lines
25632563 {
25642564 struct address_space *mapping = page_mapping(page);
25652565
2566+ page = compound_head(page);
25662567 if (likely(mapping)) {
25672568 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
25682569 /*
@@ -2747,6 +2748,11 @@ int test_clear_page_writeback(struct page *page) · 178 unmodified lines
27472748 __wb_writeout_inc(wb);
27482749 }
27492750 }
2751+
2752+ if (mapping->host && !mapping_tagged(mapping,
2753+ PAGECACHE_TAG_WRITEBACK))
2754+ sb_clear_inode_writeback(mapping->host);
2755+
27502756 spin_unlock_irqrestore(&mapping->tree_lock, flags);
27512757 } else {
27522758 ret = TestClearPageWriteback(page);
@@ -2774,11 +2780,24 @@ int __test_set_page_writeback(struct page *page, bool keep_write) · 21 unmodified lines
27742780 spin_lock_irqsave(&mapping->tree_lock, flags);
27752781 ret = TestSetPageWriteback(page);
27762782 if (!ret) {
2783+ bool on_wblist;
2784+
2785+ on_wblist = mapping_tagged(mapping,
2786+ PAGECACHE_TAG_WRITEBACK);
2787+
27772788 radix_tree_tag_set(&mapping->page_tree,
27782789 page_index(page),
27792790 PAGECACHE_TAG_WRITEBACK);
27802791 if (bdi_cap_account_writeback(bdi))
27812792 __inc_wb_stat(inode_to_wb(inode), WB_WRITEBACK);
2793+
2794+ /*
2795+ * We can come through here when swapping anonymous
2796+ * pages, so we don't necessarily have an inode to track
2797+ * for sync.
2798+ */
2799+ if (mapping->host && !on_wblist)
2800+ sb_mark_inode_writeback(mapping->host);
27822801 }
27832802 if (!PageDirty(page))
27842803 radix_tree_tag_clear(&mapping->page_tree,
mm/page_alloc.c+62−102
@@ -63,6 +63,7 @@ · 62 unmodified lines
6363 #include <linux/sched/rt.h>
6464 #include <linux/page_owner.h>
6565 #include <linux/kthread.h>
66+#include <linux/memcontrol.h>
6667
6768 #include <asm/sections.h>
6869 #include <asm/tlbflush.h>
@@ -1006,6 +1007,8 @@ static __always_inline bool free_pages_prepare(struct page *page, · 937 unmodified lines
10061007
10071008 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
10081009
1010+ if (compound)
1011+ ClearPageDoubleMap(page);
10091012 for (i = 1; i < (1 << order); i++) {
10101013 if (compound)
10111014 bad += free_tail_pages_check(page, page + i);
@@ -1016,8 +1019,12 @@ static __always_inline bool free_pages_prepare(struct page *page, · 4 unmodified lines
10161019 (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
10171020 }
10181021 }
1019− if (PageAnonHead(page))
1022+ if (PageMappingFlags(page))
10201023 page->mapping = NULL;
1024+ if (memcg_kmem_enabled() && PageKmemcg(page)) {
1025+ memcg_kmem_uncharge(page, order);
1026+ __ClearPageKmemcg(page);
1027+ }
10211028 if (check_free)
10221029 bad += free_pages_check(page);
10231030 if (bad)
@@ -1724,6 +1731,19 @@ static bool check_new_pages(struct page *page, unsigned int order) · 700 unmodified lines
17241731 return false;
17251732 }
17261733
1734+inline void post_alloc_hook(struct page *page, unsigned int order,
1735+ gfp_t gfp_flags)
1736+{
1737+ set_page_private(page, 0);
1738+ set_page_refcounted(page);
1739+
1740+ arch_alloc_page(page, order);
1741+ kernel_map_pages(page, 1 << order, 1);
1742+ kernel_poison_pages(page, 1 << order, 1);
1743+ kasan_alloc_pages(page, order);
1744+ set_page_owner(page, order, gfp_flags);
1745+}
1746+
17271747 static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
17281748 unsigned int alloc_flags)
17291749 {
@@ -1736,13 +1756,7 @@ static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags · 6 unmodified lines
17361756 poisoned &= page_is_poisoned(p);
17371757 }
17381758
1739− set_page_private(page, 0);
1740− set_page_refcounted(page);
1741−
1742− arch_alloc_page(page, order);
1743− kernel_map_pages(page, 1 << order, 1);
1744− kernel_poison_pages(page, 1 << order, 1);
1745− kasan_alloc_pages(page, order);
1759+ post_alloc_hook(page, order, gfp_flags);
17461760
17471761 if (!free_pages_prezeroed(poisoned) && (gfp_flags & __GFP_ZERO))
17481762 for (i = 0; i < (1 << order); i++)
@@ -1751,8 +1765,6 @@ static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags · 2 unmodified lines
17511765 if (order && (gfp_flags & __GFP_COMP))
17521766 prep_compound_page(page, order);
17531767
1754− set_page_owner(page, order, gfp_flags);
1755−
17561768 /*
17571769 * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to
17581770 * allocate the page. The expectation is that the caller is taking
@@ -2461,7 +2473,6 @@ void free_hot_cold_page_list(struct list_head *list, bool cold) · 702 unmodified lines
24612473 void split_page(struct page *page, unsigned int order)
24622474 {
24632475 int i;
2464− gfp_t gfp_mask;
24652476
24662477 VM_BUG_ON_PAGE(PageCompound(page), page);
24672478 VM_BUG_ON_PAGE(!page_count(page), page);
@@ -2475,12 +2486,9 @@ void split_page(struct page *page, unsigned int order) · 7 unmodified lines
24752486 split_page(virt_to_page(page[0].shadow), order);
24762487 #endif
24772488
2478− gfp_mask = get_page_owner_gfp(page);
2479− set_page_owner(page, 0, gfp_mask);
2480− for (i = 1; i < (1 << order); i++) {
2489+ for (i = 1; i < (1 << order); i++)
24812490 set_page_refcounted(page + i);
2482− set_page_owner(page + i, 0, gfp_mask);
2483− }
2491+ split_page_owner(page, order);
24842492 }
24852493 EXPORT_SYMBOL_GPL(split_page);
24862494
@@ -2509,8 +2517,6 @@ int __isolate_free_page(struct page *page, unsigned int order) · 22 unmodified lines
25092517 zone->free_area[order].nr_free--;
25102518 rmv_page_order(page);
25112519
2512− set_page_owner(page, order, __GFP_MOVABLE);
2513−
25142520 /* Set the pageblock if the isolated page is at least a pageblock */
25152521 if (order >= pageblock_order - 1) {
25162522 struct page *endpage = page + (1 << order) - 1;
@@ -2526,33 +2532,6 @@ int __isolate_free_page(struct page *page, unsigned int order) · 9 unmodified lines
25262532 return 1UL << order;
25272533 }
25282534
2529−/*
2530− * Similar to split_page except the page is already free. As this is only
2531− * being used for migration, the migratetype of the block also changes.
2532− * As this is called with interrupts disabled, the caller is responsible
2533− * for calling arch_alloc_page() and kernel_map_page() after interrupts
2534− * are enabled.
2535− *
2536− * Note: this is probably too low level an operation for use in drivers.
2537− * Please consult with lkml before using this in your driver.
2538− */
2539−int split_free_page(struct page *page)
2540−{
2541− unsigned int order;
2542− int nr_pages;
2543−
2544− order = page_order(page);
2545−
2546− nr_pages = __isolate_free_page(page, order);
2547− if (!nr_pages)
2548− return 0;
2549−
2550− /* Split into individual pages */
2551− set_page_refcounted(page);
2552− split_page(page, order);
2553− return nr_pages;
2554−}
2555−
25562535 /*
25572536 * Update NUMA hit/miss statistics
25582537 *
@@ -3105,6 +3084,7 @@ __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order, · 546 unmodified lines
31053084 struct oom_control oc = {
31063085 .zonelist = ac->zonelist,
31073086 .nodemask = ac->nodemask,
3087+ .memcg = NULL,
31083088 .gfp_mask = gfp_mask,
31093089 .order = order,
31103090 };
@@ -3868,6 +3848,14 @@ no_zone: · 757 unmodified lines
38683848 }
38693849
38703850 out:
3851+ if (memcg_kmem_enabled() && (gfp_mask & __GFP_ACCOUNT) && page) {
3852+ if (unlikely(memcg_kmem_charge(page, gfp_mask, order))) {
3853+ __free_pages(page, order);
3854+ page = NULL;
3855+ } else
3856+ __SetPageKmemcg(page);
3857+ }
3858+
38713859 if (kmemcheck_enabled && page)
38723860 kmemcheck_pagealloc_alloc(page, order, gfp_mask);
38733861
@@ -4023,56 +4011,6 @@ void __free_page_frag(void *addr) · 149 unmodified lines
40234011 }
40244012 EXPORT_SYMBOL(__free_page_frag);
40254013
4026−/*
4027− * alloc_kmem_pages charges newly allocated pages to the kmem resource counter
4028− * of the current memory cgroup if __GFP_ACCOUNT is set, other than that it is
4029− * equivalent to alloc_pages.
4030− *
4031− * It should be used when the caller would like to use kmalloc, but since the
4032− * allocation is large, it has to fall back to the page allocator.
4033− */
4034−struct page *alloc_kmem_pages(gfp_t gfp_mask, unsigned int order)
4035−{
4036− struct page *page;
4037−
4038− page = alloc_pages(gfp_mask, order);
4039− if (page && memcg_kmem_charge(page, gfp_mask, order) != 0) {
4040− __free_pages(page, order);
4041− page = NULL;
4042− }
4043− return page;
4044−}
4045−
4046−struct page *alloc_kmem_pages_node(int nid, gfp_t gfp_mask, unsigned int order)
4047−{
4048− struct page *page;
4049−
4050− page = alloc_pages_node(nid, gfp_mask, order);
4051− if (page && memcg_kmem_charge(page, gfp_mask, order) != 0) {
4052− __free_pages(page, order);
4053− page = NULL;
4054− }
4055− return page;
4056−}
4057−
4058−/*
4059− * __free_kmem_pages and free_kmem_pages will free pages allocated with
4060− * alloc_kmem_pages.
4061− */
4062−void __free_kmem_pages(struct page *page, unsigned int order)
4063−{
4064− memcg_kmem_uncharge(page, order);
4065− __free_pages(page, order);
4066−}
4067−
4068−void free_kmem_pages(unsigned long addr, unsigned int order)
4069−{
4070− if (addr != 0) {
4071− VM_BUG_ON(!virt_addr_valid((void *)addr));
4072− __free_kmem_pages(virt_to_page((void *)addr), order);
4073− }
4074−}
4075−
40764014 static void *make_alloc_exact(unsigned long addr, unsigned int order,
40774015 size_t size)
40784016 {
@@ -4374,6 +4312,9 @@ void show_free_areas(unsigned int filter) · 295 unmodified lines
43744312 " unevictable:%lu dirty:%lu writeback:%lu unstable:%lu\n"
43754313 " slab_reclaimable:%lu slab_unreclaimable:%lu\n"
43764314 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
4315+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4316+ " anon_thp: %lu shmem_thp: %lu shmem_pmdmapped: %lu\n"
4317+#endif
43774318 " free:%lu free_pcp:%lu free_cma:%lu\n",
43784319 global_page_state(NR_ACTIVE_ANON),
43794320 global_page_state(NR_INACTIVE_ANON),
@@ -4391,6 +4332,11 @@ void show_free_areas(unsigned int filter) · 11 unmodified lines
43914332 global_page_state(NR_SHMEM),
43924333 global_page_state(NR_PAGETABLE),
43934334 global_page_state(NR_BOUNCE),
4335+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4336+ global_page_state(NR_ANON_THPS) * HPAGE_PMD_NR,
4337+ global_page_state(NR_SHMEM_THPS) * HPAGE_PMD_NR,
4338+ global_page_state(NR_SHMEM_PMDMAPPED) * HPAGE_PMD_NR,
4339+#endif
43944340 global_page_state(NR_FREE_PAGES),
43954341 free_pcp,
43964342 global_page_state(NR_FREE_CMA_PAGES));
@@ -4425,6 +4371,11 @@ void show_free_areas(unsigned int filter) · 28 unmodified lines
44254371 " writeback:%lukB"
44264372 " mapped:%lukB"
44274373 " shmem:%lukB"
4374+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4375+ " shmem_thp: %lukB"
4376+ " shmem_pmdmapped: %lukB"
4377+ " anon_thp: %lukB"
4378+#endif
44284379 " slab_reclaimable:%lukB"
44294380 " slab_unreclaimable:%lukB"
44304381 " kernel_stack:%lukB"
@@ -4457,6 +4408,12 @@ void show_free_areas(unsigned int filter) · 26 unmodified lines
44574408 K(zone_page_state(zone, NR_WRITEBACK)),
44584409 K(zone_page_state(zone, NR_FILE_MAPPED)),
44594410 K(zone_page_state(zone, NR_SHMEM)),
4411+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4412+ K(zone_page_state(zone, NR_SHMEM_THPS) * HPAGE_PMD_NR),
4413+ K(zone_page_state(zone, NR_SHMEM_PMDMAPPED)
4414+ * HPAGE_PMD_NR),
4415+ K(zone_page_state(zone, NR_ANON_THPS) * HPAGE_PMD_NR),
4416+#endif
44604417 K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
44614418 K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
44624419 zone_page_state(zone, NR_KERNEL_STACK) *
@@ -6467,15 +6424,18 @@ void __init free_area_init_nodes(unsigned long *max_zone_pfn) · 2004 unmodified lines
64676424 sizeof(arch_zone_lowest_possible_pfn));
64686425 memset(arch_zone_highest_possible_pfn, 0,
64696426 sizeof(arch_zone_highest_possible_pfn));
6470− arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
6471− arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
6472− for (i = 1; i < MAX_NR_ZONES; i++) {
6427+
6428+ start_pfn = find_min_pfn_with_active_regions();
6429+
6430+ for (i = 0; i < MAX_NR_ZONES; i++) {
64736431 if (i == ZONE_MOVABLE)
64746432 continue;
6475− arch_zone_lowest_possible_pfn[i] =
6476− arch_zone_highest_possible_pfn[i-1];
6477− arch_zone_highest_possible_pfn[i] =
6478− max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
6433+
6434+ end_pfn = max(max_zone_pfn[i], start_pfn);
6435+ arch_zone_lowest_possible_pfn[i] = start_pfn;
6436+ arch_zone_highest_possible_pfn[i] = end_pfn;
6437+
6438+ start_pfn = end_pfn;
64796439 }
64806440 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
64816441 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
mm/page_isolation.c+7−6
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 #include <linux/pageblock-flags.h>
88 #include <linux/memory.h>
99 #include <linux/hugetlb.h>
10+#include <linux/page_owner.h>
1011 #include "internal.h"
1112
1213 #define CREATE_TRACE_POINTS
@@ -80,7 +81,7 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype) · 67 unmodified lines
8081 {
8182 struct zone *zone;
8283 unsigned long flags, nr_pages;
83− struct page *isolated_page = NULL;
84+ bool isolated_page = false;
8485 unsigned int order;
8586 unsigned long page_idx, buddy_idx;
8687 struct page *buddy;
@@ -108,9 +109,7 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype) · 21 unmodified lines
108109 if (pfn_valid_within(page_to_pfn(buddy)) &&
109110 !is_migrate_isolate_page(buddy)) {
110111 __isolate_free_page(page, order);
111− kernel_map_pages(page, (1 << order), 1);
112− set_page_refcounted(page);
113− isolated_page = page;
112+ isolated_page = true;
114113 }
115114 }
116115 }
@@ -128,8 +127,10 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype) · 11 unmodified lines
128127 zone->nr_isolate_pageblock--;
129128 out:
130129 spin_unlock_irqrestore(&zone->lock, flags);
131− if (isolated_page)
132− __free_pages(isolated_page, order);
130+ if (isolated_page) {
131+ post_alloc_hook(page, order, __GFP_MOVABLE);
132+ __free_pages(page, order);
133+ }
133134 }
134135
135136 static inline struct page *
mm/page_owner.c+131−26
@@ -7,11 +7,22 @@ · 6 unmodified lines
77 #include <linux/page_owner.h>
88 #include <linux/jump_label.h>
99 #include <linux/migrate.h>
10+#include <linux/stackdepot.h>
11+
1012 #include "internal.h"
1113
14+/*
15+ * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
16+ * to use off stack temporal storage
17+ */
18+#define PAGE_OWNER_STACK_DEPTH (16)
19+
1220 static bool page_owner_disabled = true;
1321 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
1422
23+static depot_stack_handle_t dummy_handle;
24+static depot_stack_handle_t failure_handle;
25+
1526 static void init_early_allocated_pages(void);
1627
1728 static int early_page_owner_param(char *buf)
@@ -34,11 +45,41 @@ static bool need_page_owner(void) · 16 unmodified lines
3445 return true;
3546 }
3647
48+static noinline void register_dummy_stack(void)
49+{
50+ unsigned long entries[4];
51+ struct stack_trace dummy;
52+
53+ dummy.nr_entries = 0;
54+ dummy.max_entries = ARRAY_SIZE(entries);
55+ dummy.entries = &entries[0];
56+ dummy.skip = 0;
57+
58+ save_stack_trace(&dummy);
59+ dummy_handle = depot_save_stack(&dummy, GFP_KERNEL);
60+}
61+
62+static noinline void register_failure_stack(void)
63+{
64+ unsigned long entries[4];
65+ struct stack_trace failure;
66+
67+ failure.nr_entries = 0;
68+ failure.max_entries = ARRAY_SIZE(entries);
69+ failure.entries = &entries[0];
70+ failure.skip = 0;
71+
72+ save_stack_trace(&failure);
73+ failure_handle = depot_save_stack(&failure, GFP_KERNEL);
74+}
75+
3776 static void init_page_owner(void)
3877 {
3978 if (page_owner_disabled)
4079 return;
4180
81+ register_dummy_stack();
82+ register_failure_stack();
4283 static_branch_enable(&page_owner_inited);
4384 init_early_allocated_pages();
4485 }
@@ -61,25 +102,66 @@ void __reset_page_owner(struct page *page, unsigned int order) · 16 unmodified lines
61102 }
62103 }
63104
64−void __set_page_owner(struct page *page, unsigned int order, gfp_t gfp_mask)
105+static inline bool check_recursive_alloc(struct stack_trace *trace,
106+ unsigned long ip)
65107 {
66− struct page_ext *page_ext = lookup_page_ext(page);
108+ int i, count;
109+
110+ if (!trace->nr_entries)
111+ return false;
112+
113+ for (i = 0, count = 0; i < trace->nr_entries; i++) {
114+ if (trace->entries[i] == ip && ++count == 2)
115+ return true;
116+ }
117+
118+ return false;
119+}
67120
121+static noinline depot_stack_handle_t save_stack(gfp_t flags)
122+{
123+ unsigned long entries[PAGE_OWNER_STACK_DEPTH];
68124 struct stack_trace trace = {
69125 .nr_entries = 0,
70− .max_entries = ARRAY_SIZE(page_ext->trace_entries),
71− .entries = &page_ext->trace_entries[0],
72− .skip = 3,
126+ .entries = entries,
127+ .max_entries = PAGE_OWNER_STACK_DEPTH,
128+ .skip = 0
73129 };
130+ depot_stack_handle_t handle;
131+
132+ save_stack_trace(&trace);
133+ if (trace.nr_entries != 0 &&
134+ trace.entries[trace.nr_entries-1] == ULONG_MAX)
135+ trace.nr_entries--;
136+
137+ /*
138+ * We need to check recursion here because our request to stackdepot
139+ * could trigger memory allocation to save new entry. New memory
140+ * allocation would reach here and call depot_save_stack() again
141+ * if we don't catch it. There is still not enough memory in stackdepot
142+ * so it would try to allocate memory again and loop forever.
143+ */
144+ if (check_recursive_alloc(&trace, _RET_IP_))
145+ return dummy_handle;
146+
147+ handle = depot_save_stack(&trace, flags);
148+ if (!handle)
149+ handle = failure_handle;
150+
151+ return handle;
152+}
153+
154+noinline void __set_page_owner(struct page *page, unsigned int order,
155+ gfp_t gfp_mask)
156+{
157+ struct page_ext *page_ext = lookup_page_ext(page);
74158
75159 if (unlikely(!page_ext))
76160 return;
77161
78− save_stack_trace(&trace);
79−
162+ page_ext->handle = save_stack(gfp_mask);
80163 page_ext->order = order;
81164 page_ext->gfp_mask = gfp_mask;
82− page_ext->nr_entries = trace.nr_entries;
83165 page_ext->last_migrate_reason = -1;
84166
85167 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
@@ -94,34 +176,31 @@ void __set_page_owner_migrate_reason(struct page *page, int reason) · 8 unmodified lines
94176 page_ext->last_migrate_reason = reason;
95177 }
96178
97−gfp_t __get_page_owner_gfp(struct page *page)
179+void __split_page_owner(struct page *page, unsigned int order)
98180 {
181+ int i;
99182 struct page_ext *page_ext = lookup_page_ext(page);
183+
100184 if (unlikely(!page_ext))
101− /*
102− * The caller just returns 0 if no valid gfp
103− * So return 0 here too.
104− */
105− return 0;
185+ return;
106186
107− return page_ext->gfp_mask;
187+ page_ext->order = 0;
188+ for (i = 1; i < (1 << order); i++)
189+ __copy_page_owner(page, page + i);
108190 }
109191
110192 void __copy_page_owner(struct page *oldpage, struct page *newpage)
111193 {
112194 struct page_ext *old_ext = lookup_page_ext(oldpage);
113195 struct page_ext *new_ext = lookup_page_ext(newpage);
114− int i;
115196
116197 if (unlikely(!old_ext || !new_ext))
117198 return;
118199
119200 new_ext->order = old_ext->order;
120201 new_ext->gfp_mask = old_ext->gfp_mask;
121− new_ext->nr_entries = old_ext->nr_entries;
122−
123− for (i = 0; i < ARRAY_SIZE(new_ext->trace_entries); i++)
124− new_ext->trace_entries[i] = old_ext->trace_entries[i];
202+ new_ext->last_migrate_reason = old_ext->last_migrate_reason;
203+ new_ext->handle = old_ext->handle;
125204
126205 /*
127206 * We don't clear the bit on the oldpage as it's going to be freed
@@ -137,14 +216,18 @@ void __copy_page_owner(struct page *oldpage, struct page *newpage) · 9 unmodified lines
137216
138217 static ssize_t
139218 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
140− struct page *page, struct page_ext *page_ext)
219+ struct page *page, struct page_ext *page_ext,
220+ depot_stack_handle_t handle)
141221 {
142222 int ret;
143223 int pageblock_mt, page_mt;
144224 char *kbuf;
225+ unsigned long entries[PAGE_OWNER_STACK_DEPTH];
145226 struct stack_trace trace = {
146− .nr_entries = page_ext->nr_entries,
147− .entries = &page_ext->trace_entries[0],
227+ .nr_entries = 0,
228+ .entries = entries,
229+ .max_entries = PAGE_OWNER_STACK_DEPTH,
230+ .skip = 0
148231 };
149232
150233 kbuf = kmalloc(count, GFP_KERNEL);
@@ -173,6 +256,7 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, · 22 unmodified lines
173256 if (ret >= count)
174257 goto err;
175258
259+ depot_fetch_stack(handle, &trace);
176260 ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
177261 if (ret >= count)
178262 goto err;
@@ -203,10 +287,14 @@ err: · 24 unmodified lines
203287 void __dump_page_owner(struct page *page)
204288 {
205289 struct page_ext *page_ext = lookup_page_ext(page);
290+ unsigned long entries[PAGE_OWNER_STACK_DEPTH];
206291 struct stack_trace trace = {
207− .nr_entries = page_ext->nr_entries,
208− .entries = &page_ext->trace_entries[0],
292+ .nr_entries = 0,
293+ .entries = entries,
294+ .max_entries = PAGE_OWNER_STACK_DEPTH,
295+ .skip = 0
209296 };
297+ depot_stack_handle_t handle;
210298 gfp_t gfp_mask;
211299 int mt;
212300
@@ -222,6 +310,13 @@ void __dump_page_owner(struct page *page) · 9 unmodified lines
222310 return;
223311 }
224312
313+ handle = READ_ONCE(page_ext->handle);
314+ if (!handle) {
315+ pr_alert("page_owner info is not active (free page?)\n");
316+ return;
317+ }
318+
319+ depot_fetch_stack(handle, &trace);
225320 pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
226321 page_ext->order, migratetype_names[mt], gfp_mask, &gfp_mask);
227322 print_stack_trace(&trace, 0);
@@ -237,6 +332,7 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) · 9 unmodified lines
237332 unsigned long pfn;
238333 struct page *page;
239334 struct page_ext *page_ext;
335+ depot_stack_handle_t handle;
240336
241337 if (!static_branch_unlikely(&page_owner_inited))
242338 return -EINVAL;
@@ -285,10 +381,19 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) · 42 unmodified lines
285381 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
286382 continue;
287383
384+ /*
385+ * Access to page_ext->handle isn't synchronous so we should
386+ * be careful to access it.
387+ */
388+ handle = READ_ONCE(page_ext->handle);
389+ if (!handle)
390+ continue;
391+
288392 /* Record the next PFN to read in the file offset */
289393 *ppos = (pfn - min_low_pfn) + 1;
290394
291− return print_page_owner(buf, count, pfn, page, page_ext);
395+ return print_page_owner(buf, count, pfn, page,
396+ page_ext, handle);
292397 }
293398
294399 return 0;
mm/readahead.c+6−7
@@ -89,7 +89,7 @@ int read_cache_pages(struct address_space *mapping, struct list_head *pages, · 88 unmodified lines
8989 page = lru_to_page(pages);
9090 list_del(&page->lru);
9191 if (add_to_page_cache_lru(page, mapping, page->index,
92− mapping_gfp_constraint(mapping, GFP_KERNEL))) {
92+ readahead_gfp_mask(mapping))) {
9393 read_cache_pages_invalidate_page(mapping, page);
9494 continue;
9595 }
@@ -108,7 +108,7 @@ int read_cache_pages(struct address_space *mapping, struct list_head *pages, · 12 unmodified lines
108108 EXPORT_SYMBOL(read_cache_pages);
109109
110110 static int read_pages(struct address_space *mapping, struct file *filp,
111− struct list_head *pages, unsigned nr_pages)
111+ struct list_head *pages, unsigned int nr_pages, gfp_t gfp)
112112 {
113113 struct blk_plug plug;
114114 unsigned page_idx;
@@ -126,10 +126,8 @@ static int read_pages(struct address_space *mapping, struct file *filp, · 11 unmodified lines
126126 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
127127 struct page *page = lru_to_page(pages);
128128 list_del(&page->lru);
129− if (!add_to_page_cache_lru(page, mapping, page->index,
130− mapping_gfp_constraint(mapping, GFP_KERNEL))) {
129+ if (!add_to_page_cache_lru(page, mapping, page->index, gfp))
131130 mapping->a_ops->readpage(filp, page);
132− }
133131 put_page(page);
134132 }
135133 ret = 0;
@@ -159,6 +157,7 @@ int __do_page_cache_readahead(struct address_space *mapping, struct file *filp, · 23 unmodified lines
159157 int page_idx;
160158 int ret = 0;
161159 loff_t isize = i_size_read(inode);
160+ gfp_t gfp_mask = readahead_gfp_mask(mapping);
162161
163162 if (isize == 0)
164163 goto out;
@@ -180,7 +179,7 @@ int __do_page_cache_readahead(struct address_space *mapping, struct file *filp, · 15 unmodified lines
180179 if (page && !radix_tree_exceptional_entry(page))
181180 continue;
182181
183− page = page_cache_alloc_readahead(mapping);
182+ page = __page_cache_alloc(gfp_mask);
184183 if (!page)
185184 break;
186185 page->index = page_offset;
@@ -196,7 +195,7 @@ int __do_page_cache_readahead(struct address_space *mapping, struct file *filp, · 9 unmodified lines
196195 * will then handle the error.
197196 */
198197 if (ret)
199− read_pages(mapping, filp, &page_pool, ret);
198+ read_pages(mapping, filp, &page_pool, ret, gfp_mask);
200199 BUG_ON(!list_empty(&page_pool));
201200 out:
202201 return ret;
mm/rmap.c+57−21
@@ -1212,10 +1212,8 @@ void do_page_add_anon_rmap(struct page *page, · 1211 unmodified lines
12121212 * pte lock(a spinlock) is held, which implies preemption
12131213 * disabled.
12141214 */
1215− if (compound) {
1216− __inc_zone_page_state(page,
1217− NR_ANON_TRANSPARENT_HUGEPAGES);
1218− }
1215+ if (compound)
1216+ __inc_zone_page_state(page, NR_ANON_THPS);
12191217 __mod_zone_page_state(page_zone(page), NR_ANON_PAGES, nr);
12201218 }
12211219 if (unlikely(PageKsm(page)))
@@ -1253,7 +1251,7 @@ void page_add_new_anon_rmap(struct page *page, · 31 unmodified lines
12531251 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
12541252 /* increment count (starts at -1) */
12551253 atomic_set(compound_mapcount_ptr(page), 0);
1256− __inc_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1254+ __inc_zone_page_state(page, NR_ANON_THPS);
12571255 } else {
12581256 /* Anon THP always mapped first with PMD */
12591257 VM_BUG_ON_PAGE(PageTransCompound(page), page);
@@ -1270,18 +1268,42 @@ void page_add_new_anon_rmap(struct page *page, · 10 unmodified lines
12701268 *
12711269 * The caller needs to hold the pte lock.
12721270 */
1273−void page_add_file_rmap(struct page *page)
1271+void page_add_file_rmap(struct page *page, bool compound)
12741272 {
1273+ int i, nr = 1;
1274+
1275+ VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page);
12751276 lock_page_memcg(page);
1276− if (atomic_inc_and_test(&page->_mapcount)) {
1277− __inc_zone_page_state(page, NR_FILE_MAPPED);
1278− mem_cgroup_inc_page_stat(page, MEM_CGROUP_STAT_FILE_MAPPED);
1277+ if (compound && PageTransHuge(page)) {
1278+ for (i = 0, nr = 0; i < HPAGE_PMD_NR; i++) {
1279+ if (atomic_inc_and_test(&page[i]._mapcount))
1280+ nr++;
1281+ }
1282+ if (!atomic_inc_and_test(compound_mapcount_ptr(page)))
1283+ goto out;
1284+ VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
1285+ __inc_zone_page_state(page, NR_SHMEM_PMDMAPPED);
1286+ } else {
1287+ if (PageTransCompound(page)) {
1288+ VM_BUG_ON_PAGE(!PageLocked(page), page);
1289+ SetPageDoubleMap(compound_head(page));
1290+ if (PageMlocked(page))
1291+ clear_page_mlock(compound_head(page));
1292+ }
1293+ if (!atomic_inc_and_test(&page->_mapcount))
1294+ goto out;
12791295 }
1296+ __mod_zone_page_state(page_zone(page), NR_FILE_MAPPED, nr);
1297+ mem_cgroup_inc_page_stat(page, MEM_CGROUP_STAT_FILE_MAPPED);
1298+out:
12801299 unlock_page_memcg(page);
12811300 }
12821301
1283−static void page_remove_file_rmap(struct page *page)
1302+static void page_remove_file_rmap(struct page *page, bool compound)
12841303 {
1304+ int i, nr = 1;
1305+
1306+ VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page);
12851307 lock_page_memcg(page);
12861308
12871309 /* Hugepages are not counted in NR_FILE_MAPPED for now. */
@@ -1292,15 +1314,26 @@ static void page_remove_file_rmap(struct page *page) · 4 unmodified lines
12921314 }
12931315
12941316 /* page still mapped by someone else? */
1295− if (!atomic_add_negative(-1, &page->_mapcount))
1296− goto out;
1317+ if (compound && PageTransHuge(page)) {
1318+ for (i = 0, nr = 0; i < HPAGE_PMD_NR; i++) {
1319+ if (atomic_add_negative(-1, &page[i]._mapcount))
1320+ nr++;
1321+ }
1322+ if (!atomic_add_negative(-1, compound_mapcount_ptr(page)))
1323+ goto out;
1324+ VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
1325+ __dec_zone_page_state(page, NR_SHMEM_PMDMAPPED);
1326+ } else {
1327+ if (!atomic_add_negative(-1, &page->_mapcount))
1328+ goto out;
1329+ }
12971330
12981331 /*
12991332 * We use the irq-unsafe __{inc|mod}_zone_page_stat because
13001333 * these counters are not modified in interrupt context, and
13011334 * pte lock(a spinlock) is held, which implies preemption disabled.
13021335 */
1303− __dec_zone_page_state(page, NR_FILE_MAPPED);
1336+ __mod_zone_page_state(page_zone(page), NR_FILE_MAPPED, -nr);
13041337 mem_cgroup_dec_page_stat(page, MEM_CGROUP_STAT_FILE_MAPPED);
13051338
13061339 if (unlikely(PageMlocked(page)))
@@ -1323,7 +1356,7 @@ static void page_remove_anon_compound_rmap(struct page *page) · 16 unmodified lines
13231356 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
13241357 return;
13251358
1326− __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1359+ __dec_zone_page_state(page, NR_ANON_THPS);
13271360
13281361 if (TestClearPageDoubleMap(page)) {
13291362 /*
@@ -1356,11 +1389,8 @@ static void page_remove_anon_compound_rmap(struct page *page) · 26 unmodified lines
13561389 */
13571390 void page_remove_rmap(struct page *page, bool compound)
13581391 {
1359− if (!PageAnon(page)) {
1360− VM_BUG_ON_PAGE(compound && !PageHuge(page), page);
1361− page_remove_file_rmap(page);
1362− return;
1363− }
1392+ if (!PageAnon(page))
1393+ return page_remove_file_rmap(page, compound);
13641394
13651395 if (compound)
13661396 return page_remove_anon_compound_rmap(page);
@@ -1436,8 +1466,14 @@ static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma, · 69 unmodified lines
14361466 */
14371467 if (!(flags & TTU_IGNORE_MLOCK)) {
14381468 if (vma->vm_flags & VM_LOCKED) {
1439− /* Holding pte lock, we do *not* need mmap_sem here */
1440− mlock_vma_page(page);
1469+ /* PTE-mapped THP are never mlocked */
1470+ if (!PageTransCompound(page)) {
1471+ /*
1472+ * Holding pte lock, we do *not* need
1473+ * mmap_sem here
1474+ */
1475+ mlock_vma_page(page);
1476+ }
14411477 ret = SWAP_MLOCK;
14421478 goto out_unmap;
14431479 }
mm/shmem.c+824−94
@@ -32,6 +32,7 @@ · 31 unmodified lines
3232 #include <linux/export.h>
3333 #include <linux/swap.h>
3434 #include <linux/uio.h>
35+#include <linux/khugepaged.h>
3536
3637 static struct vfsmount *shm_mnt;
3738
@@ -97,14 +98,6 @@ struct shmem_falloc { · 59 unmodified lines
9798 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
9899 };
99100
100−/* Flag allocation requirements to shmem_getpage */
101−enum sgp_type {
102− SGP_READ, /* don't exceed i_size, don't allocate page */
103− SGP_CACHE, /* don't exceed i_size, may allocate page */
104− SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
105− SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
106−};
107−
108101 #ifdef CONFIG_TMPFS
109102 static unsigned long shmem_default_max_blocks(void)
110103 {
@@ -124,7 +117,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, · 13 unmodified lines
124117 struct page **pagep, enum sgp_type sgp,
125118 gfp_t gfp, struct mm_struct *fault_mm, int *fault_type);
126119
127−static inline int shmem_getpage(struct inode *inode, pgoff_t index,
120+int shmem_getpage(struct inode *inode, pgoff_t index,
128121 struct page **pagep, enum sgp_type sgp)
129122 {
130123 return shmem_getpage_gfp(inode, index, pagep, sgp,
@@ -173,10 +166,13 @@ static inline int shmem_reacct_size(unsigned long flags, · 42 unmodified lines
173166 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
174167 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
175168 */
176−static inline int shmem_acct_block(unsigned long flags)
169+static inline int shmem_acct_block(unsigned long flags, long pages)
177170 {
178− return (flags & VM_NORESERVE) ?
179− security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_SIZE)) : 0;
171+ if (!(flags & VM_NORESERVE))
172+ return 0;
173+
174+ return security_vm_enough_memory_mm(current->mm,
175+ pages * VM_ACCT(PAGE_SIZE));
180176 }
181177
182178 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
@@ -192,6 +188,7 @@ static const struct inode_operations shmem_inode_operations; · 9 unmodified lines
192188 static const struct inode_operations shmem_dir_inode_operations;
193189 static const struct inode_operations shmem_special_inode_operations;
194190 static const struct vm_operations_struct shmem_vm_ops;
191+static struct file_system_type shmem_fs_type;
195192
196193 static LIST_HEAD(shmem_swaplist);
197194 static DEFINE_MUTEX(shmem_swaplist_mutex);
@@ -249,6 +246,53 @@ static void shmem_recalc_inode(struct inode *inode) · 51 unmodified lines
249246 }
250247 }
251248
249+bool shmem_charge(struct inode *inode, long pages)
250+{
251+ struct shmem_inode_info *info = SHMEM_I(inode);
252+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
253+ unsigned long flags;
254+
255+ if (shmem_acct_block(info->flags, pages))
256+ return false;
257+ spin_lock_irqsave(&info->lock, flags);
258+ info->alloced += pages;
259+ inode->i_blocks += pages * BLOCKS_PER_PAGE;
260+ shmem_recalc_inode(inode);
261+ spin_unlock_irqrestore(&info->lock, flags);
262+ inode->i_mapping->nrpages += pages;
263+
264+ if (!sbinfo->max_blocks)
265+ return true;
266+ if (percpu_counter_compare(&sbinfo->used_blocks,
267+ sbinfo->max_blocks - pages) > 0) {
268+ inode->i_mapping->nrpages -= pages;
269+ spin_lock_irqsave(&info->lock, flags);
270+ info->alloced -= pages;
271+ shmem_recalc_inode(inode);
272+ spin_unlock_irqrestore(&info->lock, flags);
273+
274+ return false;
275+ }
276+ percpu_counter_add(&sbinfo->used_blocks, pages);
277+ return true;
278+}
279+
280+void shmem_uncharge(struct inode *inode, long pages)
281+{
282+ struct shmem_inode_info *info = SHMEM_I(inode);
283+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
284+ unsigned long flags;
285+
286+ spin_lock_irqsave(&info->lock, flags);
287+ info->alloced -= pages;
288+ inode->i_blocks -= pages * BLOCKS_PER_PAGE;
289+ shmem_recalc_inode(inode);
290+ spin_unlock_irqrestore(&info->lock, flags);
291+
292+ if (sbinfo->max_blocks)
293+ percpu_counter_sub(&sbinfo->used_blocks, pages);
294+}
295+
252296 /*
253297 * Replace item expected in radix tree by a new item, while holding tree lock.
254298 */
@@ -288,6 +332,199 @@ static bool shmem_confirm_swap(struct address_space *mapping, · 33 unmodified lines
288332 return item == swp_to_radix_entry(swap);
289333 }
290334
335+/*
336+ * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
337+ *
338+ * SHMEM_HUGE_NEVER:
339+ * disables huge pages for the mount;
340+ * SHMEM_HUGE_ALWAYS:
341+ * enables huge pages for the mount;
342+ * SHMEM_HUGE_WITHIN_SIZE:
343+ * only allocate huge pages if the page will be fully within i_size,
344+ * also respect fadvise()/madvise() hints;
345+ * SHMEM_HUGE_ADVISE:
346+ * only allocate huge pages if requested with fadvise()/madvise();
347+ */
348+
349+#define SHMEM_HUGE_NEVER 0
350+#define SHMEM_HUGE_ALWAYS 1
351+#define SHMEM_HUGE_WITHIN_SIZE 2
352+#define SHMEM_HUGE_ADVISE 3
353+
354+/*
355+ * Special values.
356+ * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
357+ *
358+ * SHMEM_HUGE_DENY:
359+ * disables huge on shm_mnt and all mounts, for emergency use;
360+ * SHMEM_HUGE_FORCE:
361+ * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
362+ *
363+ */
364+#define SHMEM_HUGE_DENY (-1)
365+#define SHMEM_HUGE_FORCE (-2)
366+
367+#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
368+/* ifdef here to avoid bloating shmem.o when not necessary */
369+
370+int shmem_huge __read_mostly;
371+
372+static int shmem_parse_huge(const char *str)
373+{
374+ if (!strcmp(str, "never"))
375+ return SHMEM_HUGE_NEVER;
376+ if (!strcmp(str, "always"))
377+ return SHMEM_HUGE_ALWAYS;
378+ if (!strcmp(str, "within_size"))
379+ return SHMEM_HUGE_WITHIN_SIZE;
380+ if (!strcmp(str, "advise"))
381+ return SHMEM_HUGE_ADVISE;
382+ if (!strcmp(str, "deny"))
383+ return SHMEM_HUGE_DENY;
384+ if (!strcmp(str, "force"))
385+ return SHMEM_HUGE_FORCE;
386+ return -EINVAL;
387+}
388+
389+static const char *shmem_format_huge(int huge)
390+{
391+ switch (huge) {
392+ case SHMEM_HUGE_NEVER:
393+ return "never";
394+ case SHMEM_HUGE_ALWAYS:
395+ return "always";
396+ case SHMEM_HUGE_WITHIN_SIZE:
397+ return "within_size";
398+ case SHMEM_HUGE_ADVISE:
399+ return "advise";
400+ case SHMEM_HUGE_DENY:
401+ return "deny";
402+ case SHMEM_HUGE_FORCE:
403+ return "force";
404+ default:
405+ VM_BUG_ON(1);
406+ return "bad_val";
407+ }
408+}
409+
410+static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
411+ struct shrink_control *sc, unsigned long nr_to_split)
412+{
413+ LIST_HEAD(list), *pos, *next;
414+ struct inode *inode;
415+ struct shmem_inode_info *info;
416+ struct page *page;
417+ unsigned long batch = sc ? sc->nr_to_scan : 128;
418+ int removed = 0, split = 0;
419+
420+ if (list_empty(&sbinfo->shrinklist))
421+ return SHRINK_STOP;
422+
423+ spin_lock(&sbinfo->shrinklist_lock);
424+ list_for_each_safe(pos, next, &sbinfo->shrinklist) {
425+ info = list_entry(pos, struct shmem_inode_info, shrinklist);
426+
427+ /* pin the inode */
428+ inode = igrab(&info->vfs_inode);
429+
430+ /* inode is about to be evicted */
431+ if (!inode) {
432+ list_del_init(&info->shrinklist);
433+ removed++;
434+ goto next;
435+ }
436+
437+ /* Check if there's anything to gain */
438+ if (round_up(inode->i_size, PAGE_SIZE) ==
439+ round_up(inode->i_size, HPAGE_PMD_SIZE)) {
440+ list_del_init(&info->shrinklist);
441+ removed++;
442+ iput(inode);
443+ goto next;
444+ }
445+
446+ list_move(&info->shrinklist, &list);
447+next:
448+ if (!--batch)
449+ break;
450+ }
451+ spin_unlock(&sbinfo->shrinklist_lock);
452+
453+ list_for_each_safe(pos, next, &list) {
454+ int ret;
455+
456+ info = list_entry(pos, struct shmem_inode_info, shrinklist);
457+ inode = &info->vfs_inode;
458+
459+ if (nr_to_split && split >= nr_to_split) {
460+ iput(inode);
461+ continue;
462+ }
463+
464+ page = find_lock_page(inode->i_mapping,
465+ (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
466+ if (!page)
467+ goto drop;
468+
469+ if (!PageTransHuge(page)) {
470+ unlock_page(page);
471+ put_page(page);
472+ goto drop;
473+ }
474+
475+ ret = split_huge_page(page);
476+ unlock_page(page);
477+ put_page(page);
478+
479+ if (ret) {
480+ /* split failed: leave it on the list */
481+ iput(inode);
482+ continue;
483+ }
484+
485+ split++;
486+drop:
487+ list_del_init(&info->shrinklist);
488+ removed++;
489+ iput(inode);
490+ }
491+
492+ spin_lock(&sbinfo->shrinklist_lock);
493+ list_splice_tail(&list, &sbinfo->shrinklist);
494+ sbinfo->shrinklist_len -= removed;
495+ spin_unlock(&sbinfo->shrinklist_lock);
496+
497+ return split;
498+}
499+
500+static long shmem_unused_huge_scan(struct super_block *sb,
501+ struct shrink_control *sc)
502+{
503+ struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
504+
505+ if (!READ_ONCE(sbinfo->shrinklist_len))
506+ return SHRINK_STOP;
507+
508+ return shmem_unused_huge_shrink(sbinfo, sc, 0);
509+}
510+
511+static long shmem_unused_huge_count(struct super_block *sb,
512+ struct shrink_control *sc)
513+{
514+ struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
515+ return READ_ONCE(sbinfo->shrinklist_len);
516+}
517+#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
518+
519+#define shmem_huge SHMEM_HUGE_DENY
520+
521+static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
522+ struct shrink_control *sc, unsigned long nr_to_split)
523+{
524+ return 0;
525+}
526+#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
527+
291528 /*
292529 * Like add_to_page_cache_locked, but error if expected item has gone.
293530 */
@@ -295,30 +532,57 @@ static int shmem_add_to_page_cache(struct page *page, · 1 unmodified line
295532 struct address_space *mapping,
296533 pgoff_t index, void *expected)
297534 {
298− int error;
535+ int error, nr = hpage_nr_pages(page);
299536
537+ VM_BUG_ON_PAGE(PageTail(page), page);
538+ VM_BUG_ON_PAGE(index != round_down(index, nr), page);
300539 VM_BUG_ON_PAGE(!PageLocked(page), page);
301540 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
541+ VM_BUG_ON(expected && PageTransHuge(page));
302542
303− get_page(page);
543+ page_ref_add(page, nr);
304544 page->mapping = mapping;
305545 page->index = index;
306546
307547 spin_lock_irq(&mapping->tree_lock);
308− if (!expected)
548+ if (PageTransHuge(page)) {
549+ void __rcu **results;
550+ pgoff_t idx;
551+ int i;
552+
553+ error = 0;
554+ if (radix_tree_gang_lookup_slot(&mapping->page_tree,
555+ &results, &idx, index, 1) &&
556+ idx < index + HPAGE_PMD_NR) {
557+ error = -EEXIST;
558+ }
559+
560+ if (!error) {
561+ for (i = 0; i < HPAGE_PMD_NR; i++) {
562+ error = radix_tree_insert(&mapping->page_tree,
563+ index + i, page + i);
564+ VM_BUG_ON(error);
565+ }
566+ count_vm_event(THP_FILE_ALLOC);
567+ }
568+ } else if (!expected) {
309569 error = radix_tree_insert(&mapping->page_tree, index, page);
310− else
570+ } else {
311571 error = shmem_radix_tree_replace(mapping, index, expected,
312572 page);
573+ }
574+
313575 if (!error) {
314− mapping->nrpages++;
315− __inc_zone_page_state(page, NR_FILE_PAGES);
316− __inc_zone_page_state(page, NR_SHMEM);
576+ mapping->nrpages += nr;
577+ if (PageTransHuge(page))
578+ __inc_zone_page_state(page, NR_SHMEM_THPS);
579+ __mod_zone_page_state(page_zone(page), NR_FILE_PAGES, nr);
580+ __mod_zone_page_state(page_zone(page), NR_SHMEM, nr);
317581 spin_unlock_irq(&mapping->tree_lock);
318582 } else {
319583 page->mapping = NULL;
320584 spin_unlock_irq(&mapping->tree_lock);
321− put_page(page);
585+ page_ref_sub(page, nr);
322586 }
323587 return error;
324588 }
@@ -331,6 +595,8 @@ static void shmem_delete_from_page_cache(struct page *page, void *radswap) · 6 unmodified lines
331595 struct address_space *mapping = page->mapping;
332596 int error;
333597
598+ VM_BUG_ON_PAGE(PageCompound(page), page);
599+
334600 spin_lock_irq(&mapping->tree_lock);
335601 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
336602 page->mapping = NULL;
@@ -510,10 +776,33 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · 173 unmodified lines
510776 continue;
511777 }
512778
779+ VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
780+
513781 if (!trylock_page(page))
514782 continue;
783+
784+ if (PageTransTail(page)) {
785+ /* Middle of THP: zero out the page */
786+ clear_highpage(page);
787+ unlock_page(page);
788+ continue;
789+ } else if (PageTransHuge(page)) {
790+ if (index == round_down(end, HPAGE_PMD_NR)) {
791+ /*
792+ * Range ends in the middle of THP:
793+ * zero out the page
794+ */
795+ clear_highpage(page);
796+ unlock_page(page);
797+ continue;
798+ }
799+ index += HPAGE_PMD_NR - 1;
800+ i += HPAGE_PMD_NR - 1;
801+ }
802+
515803 if (!unfalloc || !PageUptodate(page)) {
516− if (page->mapping == mapping) {
804+ VM_BUG_ON_PAGE(PageTail(page), page);
805+ if (page_mapping(page) == mapping) {
517806 VM_BUG_ON_PAGE(PageWriteback(page), page);
518807 truncate_inode_page(mapping, page);
519808 }
@@ -589,8 +878,36 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · 69 unmodified lines
589878 }
590879
591880 lock_page(page);
881+
882+ if (PageTransTail(page)) {
883+ /* Middle of THP: zero out the page */
884+ clear_highpage(page);
885+ unlock_page(page);
886+ /*
887+ * Partial thp truncate due 'start' in middle
888+ * of THP: don't need to look on these pages
889+ * again on !pvec.nr restart.
890+ */
891+ if (index != round_down(end, HPAGE_PMD_NR))
892+ start++;
893+ continue;
894+ } else if (PageTransHuge(page)) {
895+ if (index == round_down(end, HPAGE_PMD_NR)) {
896+ /*
897+ * Range ends in the middle of THP:
898+ * zero out the page
899+ */
900+ clear_highpage(page);
901+ unlock_page(page);
902+ continue;
903+ }
904+ index += HPAGE_PMD_NR - 1;
905+ i += HPAGE_PMD_NR - 1;
906+ }
907+
592908 if (!unfalloc || !PageUptodate(page)) {
593− if (page->mapping == mapping) {
909+ VM_BUG_ON_PAGE(PageTail(page), page);
910+ if (page_mapping(page) == mapping) {
594911 VM_BUG_ON_PAGE(PageWriteback(page), page);
595912 truncate_inode_page(mapping, page);
596913 } else {
@@ -607,10 +924,10 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · 10 unmodified lines
607924 index++;
608925 }
609926
610− spin_lock(&info->lock);
927+ spin_lock_irq(&info->lock);
611928 info->swapped -= nr_swaps_freed;
612929 shmem_recalc_inode(inode);
613− spin_unlock(&info->lock);
930+ spin_unlock_irq(&info->lock);
614931 }
615932
616933 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
@@ -627,9 +944,9 @@ static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry, · 10 unmodified lines
627944 struct shmem_inode_info *info = SHMEM_I(inode);
628945
629946 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
630− spin_lock(&info->lock);
947+ spin_lock_irq(&info->lock);
631948 shmem_recalc_inode(inode);
632− spin_unlock(&info->lock);
949+ spin_unlock_irq(&info->lock);
633950 }
634951 generic_fillattr(inode, stat);
635952 return 0;
@@ -639,6 +956,7 @@ static int shmem_setattr(struct dentry *dentry, struct iattr *attr) · 3 unmodified lines
639956 {
640957 struct inode *inode = d_inode(dentry);
641958 struct shmem_inode_info *info = SHMEM_I(inode);
959+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
642960 int error;
643961
644962 error = inode_change_ok(inode, attr);
@@ -674,6 +992,20 @@ static int shmem_setattr(struct dentry *dentry, struct iattr *attr) · 29 unmodified lines
674992 if (oldsize > holebegin)
675993 unmap_mapping_range(inode->i_mapping,
676994 holebegin, 0, 1);
995+
996+ /*
997+ * Part of the huge page can be beyond i_size: subject
998+ * to shrink under memory pressure.
999+ */
1000+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1001+ spin_lock(&sbinfo->shrinklist_lock);
1002+ if (list_empty(&info->shrinklist)) {
1003+ list_add_tail(&info->shrinklist,
1004+ &sbinfo->shrinklist);
1005+ sbinfo->shrinklist_len++;
1006+ }
1007+ spin_unlock(&sbinfo->shrinklist_lock);
1008+ }
6771009 }
6781010 }
6791011
@@ -686,11 +1018,20 @@ static int shmem_setattr(struct dentry *dentry, struct iattr *attr) · 6 unmodified lines
6861018 static void shmem_evict_inode(struct inode *inode)
6871019 {
6881020 struct shmem_inode_info *info = SHMEM_I(inode);
1021+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
6891022
6901023 if (inode->i_mapping->a_ops == &shmem_aops) {
6911024 shmem_unacct_size(info->flags, inode->i_size);
6921025 inode->i_size = 0;
6931026 shmem_truncate_range(inode, 0, (loff_t)-1);
1027+ if (!list_empty(&info->shrinklist)) {
1028+ spin_lock(&sbinfo->shrinklist_lock);
1029+ if (!list_empty(&info->shrinklist)) {
1030+ list_del_init(&info->shrinklist);
1031+ sbinfo->shrinklist_len--;
1032+ }
1033+ spin_unlock(&sbinfo->shrinklist_lock);
1034+ }
6941035 if (!list_empty(&info->swaplist)) {
6951036 mutex_lock(&shmem_swaplist_mutex);
6961037 list_del_init(&info->swaplist);
@@ -773,9 +1114,9 @@ static int shmem_unuse_inode(struct shmem_inode_info *info, · 76 unmodified lines
7731114 delete_from_swap_cache(*pagep);
7741115 set_page_dirty(*pagep);
7751116 if (!error) {
776− spin_lock(&info->lock);
1117+ spin_lock_irq(&info->lock);
7771118 info->swapped--;
778− spin_unlock(&info->lock);
1119+ spin_unlock_irq(&info->lock);
7791120 swap_free(swap);
7801121 }
7811122 }
@@ -848,6 +1189,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) · 66 unmodified lines
8481189 swp_entry_t swap;
8491190 pgoff_t index;
8501191
1192+ VM_BUG_ON_PAGE(PageCompound(page), page);
8511193 BUG_ON(!PageLocked(page));
8521194 mapping = page->mapping;
8531195 index = page->index;
@@ -922,10 +1264,10 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) · 68 unmodified lines
9221264 list_add_tail(&info->swaplist, &shmem_swaplist);
9231265
9241266 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
925− spin_lock(&info->lock);
1267+ spin_lock_irq(&info->lock);
9261268 shmem_recalc_inode(inode);
9271269 info->swapped++;
928− spin_unlock(&info->lock);
1270+ spin_unlock_irq(&info->lock);
9291271
9301272 swap_shmem_alloc(swap);
9311273 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
@@ -984,24 +1326,63 @@ static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) · 52 unmodified lines
9841326 #define vm_policy vm_private_data
9851327 #endif
9861328
1329+static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1330+ struct shmem_inode_info *info, pgoff_t index)
1331+{
1332+ /* Create a pseudo vma that just contains the policy */
1333+ vma->vm_start = 0;
1334+ /* Bias interleave by inode number to distribute better across nodes */
1335+ vma->vm_pgoff = index + info->vfs_inode.i_ino;
1336+ vma->vm_ops = NULL;
1337+ vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1338+}
1339+
1340+static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1341+{
1342+ /* Drop reference taken by mpol_shared_policy_lookup() */
1343+ mpol_cond_put(vma->vm_policy);
1344+}
1345+
9871346 static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
9881347 struct shmem_inode_info *info, pgoff_t index)
9891348 {
9901349 struct vm_area_struct pvma;
9911350 struct page *page;
9921351
993− /* Create a pseudo vma that just contains the policy */
994− pvma.vm_start = 0;
995− /* Bias interleave by inode number to distribute better across nodes */
996− pvma.vm_pgoff = index + info->vfs_inode.i_ino;
997− pvma.vm_ops = NULL;
998− pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
999−
1352+ shmem_pseudo_vma_init(&pvma, info, index);
10001353 page = swapin_readahead(swap, gfp, &pvma, 0);
1354+ shmem_pseudo_vma_destroy(&pvma);
10011355
1002− /* Drop reference taken by mpol_shared_policy_lookup() */
1003− mpol_cond_put(pvma.vm_policy);
1356+ return page;
1357+}
1358+
1359+static struct page *shmem_alloc_hugepage(gfp_t gfp,
1360+ struct shmem_inode_info *info, pgoff_t index)
1361+{
1362+ struct vm_area_struct pvma;
1363+ struct inode *inode = &info->vfs_inode;
1364+ struct address_space *mapping = inode->i_mapping;
1365+ pgoff_t idx, hindex = round_down(index, HPAGE_PMD_NR);
1366+ void __rcu **results;
1367+ struct page *page;
10041368
1369+ if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
1370+ return NULL;
1371+
1372+ rcu_read_lock();
1373+ if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx,
1374+ hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
1375+ rcu_read_unlock();
1376+ return NULL;
1377+ }
1378+ rcu_read_unlock();
1379+
1380+ shmem_pseudo_vma_init(&pvma, info, hindex);
1381+ page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1382+ HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1383+ shmem_pseudo_vma_destroy(&pvma);
1384+ if (page)
1385+ prep_transhuge_page(page);
10051386 return page;
10061387 }
10071388
@@ -1011,23 +1392,51 @@ static struct page *shmem_alloc_page(gfp_t gfp, · 3 unmodified lines
10111392 struct vm_area_struct pvma;
10121393 struct page *page;
10131394
1014− /* Create a pseudo vma that just contains the policy */
1015− pvma.vm_start = 0;
1016− /* Bias interleave by inode number to distribute better across nodes */
1017− pvma.vm_pgoff = index + info->vfs_inode.i_ino;
1018− pvma.vm_ops = NULL;
1019− pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1395+ shmem_pseudo_vma_init(&pvma, info, index);
1396+ page = alloc_page_vma(gfp, &pvma, 0);
1397+ shmem_pseudo_vma_destroy(&pvma);
1398+
1399+ return page;
1400+}
1401+
1402+static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
1403+ struct shmem_inode_info *info, struct shmem_sb_info *sbinfo,
1404+ pgoff_t index, bool huge)
1405+{
1406+ struct page *page;
1407+ int nr;
1408+ int err = -ENOSPC;
1409+
1410+ if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
1411+ huge = false;
1412+ nr = huge ? HPAGE_PMD_NR : 1;
1413+
1414+ if (shmem_acct_block(info->flags, nr))
1415+ goto failed;
1416+ if (sbinfo->max_blocks) {
1417+ if (percpu_counter_compare(&sbinfo->used_blocks,
1418+ sbinfo->max_blocks - nr) > 0)
1419+ goto unacct;
1420+ percpu_counter_add(&sbinfo->used_blocks, nr);
1421+ }
10201422
1021− page = alloc_pages_vma(gfp, 0, &pvma, 0, numa_node_id(), false);
1423+ if (huge)
1424+ page = shmem_alloc_hugepage(gfp, info, index);
1425+ else
1426+ page = shmem_alloc_page(gfp, info, index);
10221427 if (page) {
10231428 __SetPageLocked(page);
10241429 __SetPageSwapBacked(page);
1430+ return page;
10251431 }
10261432
1027− /* Drop reference taken by mpol_shared_policy_lookup() */
1028− mpol_cond_put(pvma.vm_policy);
1029−
1030− return page;
1433+ err = -ENOMEM;
1434+ if (sbinfo->max_blocks)
1435+ percpu_counter_add(&sbinfo->used_blocks, -nr);
1436+unacct:
1437+ shmem_unacct_blocks(info->flags, nr);
1438+failed:
1439+ return ERR_PTR(err);
10311440 }
10321441
10331442 /*
@@ -1132,12 +1541,16 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, · 98 unmodified lines
11321541 struct mem_cgroup *memcg;
11331542 struct page *page;
11341543 swp_entry_t swap;
1544+ enum sgp_type sgp_huge = sgp;
1545+ pgoff_t hindex = index;
11351546 int error;
11361547 int once = 0;
11371548 int alloced = 0;
11381549
11391550 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
11401551 return -EFBIG;
1552+ if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1553+ sgp = SGP_CACHE;
11411554 repeat:
11421555 swap.val = 0;
11431556 page = find_lock_entry(mapping, index);
@@ -1240,10 +1653,10 @@ repeat: · 96 unmodified lines
12401653
12411654 mem_cgroup_commit_charge(page, memcg, true, false);
12421655
1243− spin_lock(&info->lock);
1656+ spin_lock_irq(&info->lock);
12441657 info->swapped--;
12451658 shmem_recalc_inode(inode);
1246− spin_unlock(&info->lock);
1659+ spin_unlock_irq(&info->lock);
12471660
12481661 if (sgp == SGP_WRITE)
12491662 mark_page_accessed(page);
@@ -1253,51 +1666,111 @@ repeat: · 3 unmodified lines
12531666 swap_free(swap);
12541667
12551668 } else {
1256− if (shmem_acct_block(info->flags)) {
1257− error = -ENOSPC;
1258− goto failed;
1669+ /* shmem_symlink() */
1670+ if (mapping->a_ops != &shmem_aops)
1671+ goto alloc_nohuge;
1672+ if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1673+ goto alloc_nohuge;
1674+ if (shmem_huge == SHMEM_HUGE_FORCE)
1675+ goto alloc_huge;
1676+ switch (sbinfo->huge) {
1677+ loff_t i_size;
1678+ pgoff_t off;
1679+ case SHMEM_HUGE_NEVER:
1680+ goto alloc_nohuge;
1681+ case SHMEM_HUGE_WITHIN_SIZE:
1682+ off = round_up(index, HPAGE_PMD_NR);
1683+ i_size = round_up(i_size_read(inode), PAGE_SIZE);
1684+ if (i_size >= HPAGE_PMD_SIZE &&
1685+ i_size >> PAGE_SHIFT >= off)
1686+ goto alloc_huge;
1687+ /* fallthrough */
1688+ case SHMEM_HUGE_ADVISE:
1689+ if (sgp_huge == SGP_HUGE)
1690+ goto alloc_huge;
1691+ /* TODO: implement fadvise() hints */
1692+ goto alloc_nohuge;
12591693 }
1260− if (sbinfo->max_blocks) {
1261− if (percpu_counter_compare(&sbinfo->used_blocks,
1262− sbinfo->max_blocks) >= 0) {
1263− error = -ENOSPC;
1264− goto unacct;
1694+
1695+alloc_huge:
1696+ page = shmem_alloc_and_acct_page(gfp, info, sbinfo,
1697+ index, true);
1698+ if (IS_ERR(page)) {
1699+alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, info, sbinfo,
1700+ index, false);
1701+ }
1702+ if (IS_ERR(page)) {
1703+ int retry = 5;
1704+ error = PTR_ERR(page);
1705+ page = NULL;
1706+ if (error != -ENOSPC)
1707+ goto failed;
1708+ /*
1709+ * Try to reclaim some spece by splitting a huge page
1710+ * beyond i_size on the filesystem.
1711+ */
1712+ while (retry--) {
1713+ int ret;
1714+ ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1715+ if (ret == SHRINK_STOP)
1716+ break;
1717+ if (ret)
1718+ goto alloc_nohuge;
12651719 }
1266− percpu_counter_inc(&sbinfo->used_blocks);
1720+ goto failed;
12671721 }
12681722
1269− page = shmem_alloc_page(gfp, info, index);
1270− if (!page) {
1271− error = -ENOMEM;
1272− goto decused;
1273− }
1723+ if (PageTransHuge(page))
1724+ hindex = round_down(index, HPAGE_PMD_NR);
1725+ else
1726+ hindex = index;
1727+
12741728 if (sgp == SGP_WRITE)
12751729 __SetPageReferenced(page);
12761730
12771731 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
1278− false);
1732+ PageTransHuge(page));
12791733 if (error)
1280− goto decused;
1281− error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
1734+ goto unacct;
1735+ error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK,
1736+ compound_order(page));
12821737 if (!error) {
1283− error = shmem_add_to_page_cache(page, mapping, index,
1738+ error = shmem_add_to_page_cache(page, mapping, hindex,
12841739 NULL);
12851740 radix_tree_preload_end();
12861741 }
12871742 if (error) {
1288− mem_cgroup_cancel_charge(page, memcg, false);
1289− goto decused;
1743+ mem_cgroup_cancel_charge(page, memcg,
1744+ PageTransHuge(page));
1745+ goto unacct;
12901746 }
1291− mem_cgroup_commit_charge(page, memcg, false, false);
1747+ mem_cgroup_commit_charge(page, memcg, false,
1748+ PageTransHuge(page));
12921749 lru_cache_add_anon(page);
12931750
1294− spin_lock(&info->lock);
1295− info->alloced++;
1296− inode->i_blocks += BLOCKS_PER_PAGE;
1751+ spin_lock_irq(&info->lock);
1752+ info->alloced += 1 << compound_order(page);
1753+ inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
12971754 shmem_recalc_inode(inode);
1298− spin_unlock(&info->lock);
1755+ spin_unlock_irq(&info->lock);
12991756 alloced = true;
13001757
1758+ if (PageTransHuge(page) &&
1759+ DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1760+ hindex + HPAGE_PMD_NR - 1) {
1761+ /*
1762+ * Part of the huge page is beyond i_size: subject
1763+ * to shrink under memory pressure.
1764+ */
1765+ spin_lock(&sbinfo->shrinklist_lock);
1766+ if (list_empty(&info->shrinklist)) {
1767+ list_add_tail(&info->shrinklist,
1768+ &sbinfo->shrinklist);
1769+ sbinfo->shrinklist_len++;
1770+ }
1771+ spin_unlock(&sbinfo->shrinklist_lock);
1772+ }
1773+
13011774 /*
13021775 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
13031776 */
@@ -1309,10 +1782,15 @@ clear: · 5 unmodified lines
13091782 * but SGP_FALLOC on a page fallocated earlier must initialize
13101783 * it now, lest undo on failure cancel our earlier guarantee.
13111784 */
1312− if (sgp != SGP_WRITE) {
1313− clear_highpage(page);
1314− flush_dcache_page(page);
1315− SetPageUptodate(page);
1785+ if (sgp != SGP_WRITE && !PageUptodate(page)) {
1786+ struct page *head = compound_head(page);
1787+ int i;
1788+
1789+ for (i = 0; i < (1 << compound_order(head)); i++) {
1790+ clear_highpage(head + i);
1791+ flush_dcache_page(head + i);
1792+ }
1793+ SetPageUptodate(head);
13161794 }
13171795 }
13181796
@@ -1322,24 +1800,30 @@ clear: · 3 unmodified lines
13221800 if (alloced) {
13231801 ClearPageDirty(page);
13241802 delete_from_page_cache(page);
1325− spin_lock(&info->lock);
1803+ spin_lock_irq(&info->lock);
13261804 shmem_recalc_inode(inode);
1327− spin_unlock(&info->lock);
1805+ spin_unlock_irq(&info->lock);
13281806 }
13291807 error = -EINVAL;
13301808 goto unlock;
13311809 }
1332− *pagep = page;
1810+ *pagep = page + index - hindex;
13331811 return 0;
13341812
13351813 /*
13361814 * Error recovery.
13371815 */
1338−decused:
1339− if (sbinfo->max_blocks)
1340− percpu_counter_add(&sbinfo->used_blocks, -1);
13411816 unacct:
1342− shmem_unacct_blocks(info->flags, 1);
1817+ if (sbinfo->max_blocks)
1818+ percpu_counter_sub(&sbinfo->used_blocks,
1819+ 1 << compound_order(page));
1820+ shmem_unacct_blocks(info->flags, 1 << compound_order(page));
1821+
1822+ if (PageTransHuge(page)) {
1823+ unlock_page(page);
1824+ put_page(page);
1825+ goto alloc_nohuge;
1826+ }
13431827 failed:
13441828 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
13451829 error = -EEXIST;
@@ -1350,9 +1834,9 @@ unlock: · 4 unmodified lines
13501834 }
13511835 if (error == -ENOSPC && !once++) {
13521836 info = SHMEM_I(inode);
1353− spin_lock(&info->lock);
1837+ spin_lock_irq(&info->lock);
13541838 shmem_recalc_inode(inode);
1355− spin_unlock(&info->lock);
1839+ spin_unlock_irq(&info->lock);
13561840 goto repeat;
13571841 }
13581842 if (error == -EEXIST) /* from above or from radix_tree_insert */
@@ -1364,6 +1848,7 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · 5 unmodified lines
13641848 {
13651849 struct inode *inode = file_inode(vma->vm_file);
13661850 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
1851+ enum sgp_type sgp;
13671852 int error;
13681853 int ret = VM_FAULT_LOCKED;
13691854
@@ -1425,13 +1910,107 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · 55 unmodified lines
14251910 spin_unlock(&inode->i_lock);
14261911 }
14271912
1428− error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, SGP_CACHE,
1913+ sgp = SGP_CACHE;
1914+ if (vma->vm_flags & VM_HUGEPAGE)
1915+ sgp = SGP_HUGE;
1916+ else if (vma->vm_flags & VM_NOHUGEPAGE)
1917+ sgp = SGP_NOHUGE;
1918+
1919+ error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
14291920 gfp, vma->vm_mm, &ret);
14301921 if (error)
14311922 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
14321923 return ret;
14331924 }
14341925
1926+unsigned long shmem_get_unmapped_area(struct file *file,
1927+ unsigned long uaddr, unsigned long len,
1928+ unsigned long pgoff, unsigned long flags)
1929+{
1930+ unsigned long (*get_area)(struct file *,
1931+ unsigned long, unsigned long, unsigned long, unsigned long);
1932+ unsigned long addr;
1933+ unsigned long offset;
1934+ unsigned long inflated_len;
1935+ unsigned long inflated_addr;
1936+ unsigned long inflated_offset;
1937+
1938+ if (len > TASK_SIZE)
1939+ return -ENOMEM;
1940+
1941+ get_area = current->mm->get_unmapped_area;
1942+ addr = get_area(file, uaddr, len, pgoff, flags);
1943+
1944+ if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
1945+ return addr;
1946+ if (IS_ERR_VALUE(addr))
1947+ return addr;
1948+ if (addr & ~PAGE_MASK)
1949+ return addr;
1950+ if (addr > TASK_SIZE - len)
1951+ return addr;
1952+
1953+ if (shmem_huge == SHMEM_HUGE_DENY)
1954+ return addr;
1955+ if (len < HPAGE_PMD_SIZE)
1956+ return addr;
1957+ if (flags & MAP_FIXED)
1958+ return addr;
1959+ /*
1960+ * Our priority is to support MAP_SHARED mapped hugely;
1961+ * and support MAP_PRIVATE mapped hugely too, until it is COWed.
1962+ * But if caller specified an address hint, respect that as before.
1963+ */
1964+ if (uaddr)
1965+ return addr;
1966+
1967+ if (shmem_huge != SHMEM_HUGE_FORCE) {
1968+ struct super_block *sb;
1969+
1970+ if (file) {
1971+ VM_BUG_ON(file->f_op != &shmem_file_operations);
1972+ sb = file_inode(file)->i_sb;
1973+ } else {
1974+ /*
1975+ * Called directly from mm/mmap.c, or drivers/char/mem.c
1976+ * for "/dev/zero", to create a shared anonymous object.
1977+ */
1978+ if (IS_ERR(shm_mnt))
1979+ return addr;
1980+ sb = shm_mnt->mnt_sb;
1981+ }
1982+ if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER)
1983+ return addr;
1984+ }
1985+
1986+ offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
1987+ if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
1988+ return addr;
1989+ if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
1990+ return addr;
1991+
1992+ inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
1993+ if (inflated_len > TASK_SIZE)
1994+ return addr;
1995+ if (inflated_len < len)
1996+ return addr;
1997+
1998+ inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
1999+ if (IS_ERR_VALUE(inflated_addr))
2000+ return addr;
2001+ if (inflated_addr & ~PAGE_MASK)
2002+ return addr;
2003+
2004+ inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2005+ inflated_addr += offset - inflated_offset;
2006+ if (inflated_offset > offset)
2007+ inflated_addr += HPAGE_PMD_SIZE;
2008+
2009+ if (inflated_addr > TASK_SIZE - len)
2010+ return addr;
2011+ return inflated_addr;
2012+}
2013+
14352014 #ifdef CONFIG_NUMA
14362015 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
14372016 {
@@ -1456,7 +2035,7 @@ int shmem_lock(struct file *file, int lock, struct user_struct *user) · 18 unmodified lines
14562035 struct shmem_inode_info *info = SHMEM_I(inode);
14572036 int retval = -ENOMEM;
14582037
1459− spin_lock(&info->lock);
2038+ spin_lock_irq(&info->lock);
14602039 if (lock && !(info->flags & VM_LOCKED)) {
14612040 if (!user_shm_lock(inode->i_size, user))
14622041 goto out_nomem;
@@ -1471,7 +2050,7 @@ int shmem_lock(struct file *file, int lock, struct user_struct *user) · 8 unmodified lines
14712050 retval = 0;
14722051
14732052 out_nomem:
1474− spin_unlock(&info->lock);
2053+ spin_unlock_irq(&info->lock);
14752054 return retval;
14762055 }
14772056
@@ -1479,6 +2058,11 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma) · 1 unmodified line
14792058 {
14802059 file_accessed(file);
14812060 vma->vm_ops = &shmem_vm_ops;
2061+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
2062+ ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2063+ (vma->vm_end & HPAGE_PMD_MASK)) {
2064+ khugepaged_enter(vma, vma->vm_flags);
2065+ }
14822066 return 0;
14832067 }
14842068
@@ -1504,6 +2088,7 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode · 19 unmodified lines
15042088 spin_lock_init(&info->lock);
15052089 info->seals = F_SEAL_SEAL;
15062090 info->flags = flags & VM_NORESERVE;
2091+ INIT_LIST_HEAD(&info->shrinklist);
15072092 INIT_LIST_HEAD(&info->swaplist);
15082093 simple_xattrs_init(&info->xattrs);
15092094 cache_no_acl(inode);
@@ -1589,12 +2174,23 @@ shmem_write_end(struct file *file, struct address_space *mapping, · 79 unmodified lines
15892174 i_size_write(inode, pos + copied);
15902175
15912176 if (!PageUptodate(page)) {
2177+ struct page *head = compound_head(page);
2178+ if (PageTransCompound(page)) {
2179+ int i;
2180+
2181+ for (i = 0; i < HPAGE_PMD_NR; i++) {
2182+ if (head + i == page)
2183+ continue;
2184+ clear_highpage(head + i);
2185+ flush_dcache_page(head + i);
2186+ }
2187+ }
15922188 if (copied < PAGE_SIZE) {
15932189 unsigned from = pos & (PAGE_SIZE - 1);
15942190 zero_user_segments(page, 0, from,
15952191 from + copied, PAGE_SIZE);
15962192 }
1597− SetPageUptodate(page);
2193+ SetPageUptodate(head);
15982194 }
15992195 set_page_dirty(page);
16002196 unlock_page(page);
@@ -2860,11 +3456,24 @@ static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, · 1259 unmodified lines
28603456 sbinfo->gid = make_kgid(current_user_ns(), gid);
28613457 if (!gid_valid(sbinfo->gid))
28623458 goto bad_val;
3459+#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3460+ } else if (!strcmp(this_char, "huge")) {
3461+ int huge;
3462+ huge = shmem_parse_huge(value);
3463+ if (huge < 0)
3464+ goto bad_val;
3465+ if (!has_transparent_hugepage() &&
3466+ huge != SHMEM_HUGE_NEVER)
3467+ goto bad_val;
3468+ sbinfo->huge = huge;
3469+#endif
3470+#ifdef CONFIG_NUMA
28633471 } else if (!strcmp(this_char,"mpol")) {
28643472 mpol_put(mpol);
28653473 mpol = NULL;
28663474 if (mpol_parse_str(value, &mpol))
28673475 goto bad_val;
3476+#endif
28683477 } else {
28693478 pr_err("tmpfs: Bad mount option %s\n", this_char);
28703479 goto error;
@@ -2910,6 +3519,7 @@ static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) · 39 unmodified lines
29103519 goto out;
29113520
29123521 error = 0;
3522+ sbinfo->huge = config.huge;
29133523 sbinfo->max_blocks = config.max_blocks;
29143524 sbinfo->max_inodes = config.max_inodes;
29153525 sbinfo->free_inodes = config.max_inodes - inodes;
@@ -2943,6 +3553,11 @@ static int shmem_show_options(struct seq_file *seq, struct dentry *root) · 27 unmodified lines
29433553 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
29443554 seq_printf(seq, ",gid=%u",
29453555 from_kgid_munged(&init_user_ns, sbinfo->gid));
3556+#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3557+ /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3558+ if (sbinfo->huge)
3559+ seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3560+#endif
29463561 shmem_show_mpol(seq, sbinfo->mpol);
29473562 return 0;
29483563 }
@@ -3072,6 +3687,8 @@ int shmem_fill_super(struct super_block *sb, void *data, int silent) · 123 unmodified lines
30723687 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
30733688 goto failed;
30743689 sbinfo->free_inodes = sbinfo->max_inodes;
3690+ spin_lock_init(&sbinfo->shrinklist_lock);
3691+ INIT_LIST_HEAD(&sbinfo->shrinklist);
30753692
30763693 sb->s_maxbytes = MAX_LFS_FILESIZE;
30773694 sb->s_blocksize = PAGE_SIZE;
@@ -3161,6 +3778,7 @@ static const struct address_space_operations shmem_aops = { · 83 unmodified lines
31613778
31623779 static const struct file_operations shmem_file_operations = {
31633780 .mmap = shmem_mmap,
3781+ .get_unmapped_area = shmem_get_unmapped_area,
31643782 #ifdef CONFIG_TMPFS
31653783 .llseek = shmem_file_llseek,
31663784 .read_iter = shmem_file_read_iter,
@@ -3233,6 +3851,10 @@ static const struct super_operations shmem_ops = { · 66 unmodified lines
32333851 .evict_inode = shmem_evict_inode,
32343852 .drop_inode = generic_delete_inode,
32353853 .put_super = shmem_put_super,
3854+#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3855+ .nr_cached_objects = shmem_unused_huge_count,
3856+ .free_cached_objects = shmem_unused_huge_scan,
3857+#endif
32363858 };
32373859
32383860 static const struct vm_operations_struct shmem_vm_ops = {
@@ -3282,6 +3904,13 @@ int __init shmem_init(void) · 43 unmodified lines
32823904 pr_err("Could not kern_mount tmpfs\n");
32833905 goto out1;
32843906 }
3907+
3908+#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3909+ if (has_transparent_hugepage() && shmem_huge < SHMEM_HUGE_DENY)
3910+ SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3911+ else
3912+ shmem_huge = 0; /* just in case it was patched */
3913+#endif
32853914 return 0;
32863915
32873916 out1:
@@ -3293,6 +3922,91 @@ out3: · 5 unmodified lines
32933922 return error;
32943923 }
32953924
3925+#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
3926+static ssize_t shmem_enabled_show(struct kobject *kobj,
3927+ struct kobj_attribute *attr, char *buf)
3928+{
3929+ int values[] = {
3930+ SHMEM_HUGE_ALWAYS,
3931+ SHMEM_HUGE_WITHIN_SIZE,
3932+ SHMEM_HUGE_ADVISE,
3933+ SHMEM_HUGE_NEVER,
3934+ SHMEM_HUGE_DENY,
3935+ SHMEM_HUGE_FORCE,
3936+ };
3937+ int i, count;
3938+
3939+ for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3940+ const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3941+
3942+ count += sprintf(buf + count, fmt,
3943+ shmem_format_huge(values[i]));
3944+ }
3945+ buf[count - 1] = '\n';
3946+ return count;
3947+}
3948+
3949+static ssize_t shmem_enabled_store(struct kobject *kobj,
3950+ struct kobj_attribute *attr, const char *buf, size_t count)
3951+{
3952+ char tmp[16];
3953+ int huge;
3954+
3955+ if (count + 1 > sizeof(tmp))
3956+ return -EINVAL;
3957+ memcpy(tmp, buf, count);
3958+ tmp[count] = '\0';
3959+ if (count && tmp[count - 1] == '\n')
3960+ tmp[count - 1] = '\0';
3961+
3962+ huge = shmem_parse_huge(tmp);
3963+ if (huge == -EINVAL)
3964+ return -EINVAL;
3965+ if (!has_transparent_hugepage() &&
3966+ huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3967+ return -EINVAL;
3968+
3969+ shmem_huge = huge;
3970+ if (shmem_huge < SHMEM_HUGE_DENY)
3971+ SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3972+ return count;
3973+}
3974+
3975+struct kobj_attribute shmem_enabled_attr =
3976+ __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
3977+
3978+bool shmem_huge_enabled(struct vm_area_struct *vma)
3979+{
3980+ struct inode *inode = file_inode(vma->vm_file);
3981+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3982+ loff_t i_size;
3983+ pgoff_t off;
3984+
3985+ if (shmem_huge == SHMEM_HUGE_FORCE)
3986+ return true;
3987+ if (shmem_huge == SHMEM_HUGE_DENY)
3988+ return false;
3989+ switch (sbinfo->huge) {
3990+ case SHMEM_HUGE_NEVER:
3991+ return false;
3992+ case SHMEM_HUGE_ALWAYS:
3993+ return true;
3994+ case SHMEM_HUGE_WITHIN_SIZE:
3995+ off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3996+ i_size = round_up(i_size_read(inode), PAGE_SIZE);
3997+ if (i_size >= HPAGE_PMD_SIZE &&
3998+ i_size >> PAGE_SHIFT >= off)
3999+ return true;
4000+ case SHMEM_HUGE_ADVISE:
4001+ /* TODO: implement fadvise() hints */
4002+ return (vma->vm_flags & VM_HUGEPAGE);
4003+ default:
4004+ VM_BUG_ON(1);
4005+ return false;
4006+ }
4007+}
4008+#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
4009+
32964010 #else /* !CONFIG_SHMEM */
32974011
32984012 /*
@@ -3335,6 +4049,15 @@ void shmem_unlock_mapping(struct address_space *mapping) · 36 unmodified lines
33354049 {
33364050 }
33374051
4052+#ifdef CONFIG_MMU
4053+unsigned long shmem_get_unmapped_area(struct file *file,
4054+ unsigned long addr, unsigned long len,
4055+ unsigned long pgoff, unsigned long flags)
4056+{
4057+ return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
4058+}
4059+#endif
4060+
33384061 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
33394062 {
33404063 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
@@ -3461,6 +4184,13 @@ int shmem_zero_setup(struct vm_area_struct *vma) · 120 unmodified lines
34614184 fput(vma->vm_file);
34624185 vma->vm_file = file;
34634186 vma->vm_ops = &shmem_vm_ops;
4187+
4188+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
4189+ ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4190+ (vma->vm_end & HPAGE_PMD_MASK)) {
4191+ khugepaged_enter(vma, vma->vm_flags);
4192+ }
4193+
34644194 return 0;
34654195 }
34664196
mm/slab.c+26−64
@@ -1236,61 +1236,6 @@ static void __init set_up_node(struct kmem_cache *cachep, int index) · 1235 unmodified lines
12361236 }
12371237 }
12381238
1239−#ifdef CONFIG_SLAB_FREELIST_RANDOM
1240−static void freelist_randomize(struct rnd_state *state, freelist_idx_t *list,
1241− size_t count)
1242−{
1243− size_t i;
1244− unsigned int rand;
1245−
1246− for (i = 0; i < count; i++)
1247− list[i] = i;
1248−
1249− /* Fisher-Yates shuffle */
1250− for (i = count - 1; i > 0; i--) {
1251− rand = prandom_u32_state(state);
1252− rand %= (i + 1);
1253− swap(list[i], list[rand]);
1254− }
1255−}
1256−
1257−/* Create a random sequence per cache */
1258−static int cache_random_seq_create(struct kmem_cache *cachep, gfp_t gfp)
1259−{
1260− unsigned int seed, count = cachep->num;
1261− struct rnd_state state;
1262−
1263− if (count < 2)
1264− return 0;
1265−
1266− /* If it fails, we will just use the global lists */
1267− cachep->random_seq = kcalloc(count, sizeof(freelist_idx_t), gfp);
1268− if (!cachep->random_seq)
1269− return -ENOMEM;
1270−
1271− /* Get best entropy at this stage */
1272− get_random_bytes_arch(&seed, sizeof(seed));
1273− prandom_seed_state(&state, seed);
1274−
1275− freelist_randomize(&state, cachep->random_seq, count);
1276− return 0;
1277−}
1278−
1279−/* Destroy the per-cache random freelist sequence */
1280−static void cache_random_seq_destroy(struct kmem_cache *cachep)
1281−{
1282− kfree(cachep->random_seq);
1283− cachep->random_seq = NULL;
1284−}
1285−#else
1286−static inline int cache_random_seq_create(struct kmem_cache *cachep, gfp_t gfp)
1287−{
1288− return 0;
1289−}
1290−static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
1291−#endif /* CONFIG_SLAB_FREELIST_RANDOM */
1292−
1293−
12941239 /*
12951240 * Initialisation. Called after the page allocator have been initialised and
12961241 * before smp_init().
@@ -2535,7 +2480,7 @@ static void cache_init_objs_debug(struct kmem_cache *cachep, struct page *page) · 1238 unmodified lines
25352480 union freelist_init_state {
25362481 struct {
25372482 unsigned int pos;
2538− freelist_idx_t *list;
2483+ unsigned int *list;
25392484 unsigned int count;
25402485 unsigned int rand;
25412486 };
@@ -2554,7 +2499,7 @@ static bool freelist_state_initialize(union freelist_init_state *state, · 12 unmodified lines
25542499 unsigned int rand;
25552500
25562501 /* Use best entropy available to define a random shift */
2557− get_random_bytes_arch(&rand, sizeof(rand));
2502+ rand = get_random_int();
25582503
25592504 /* Use a random state if the pre-computed list is not available */
25602505 if (!cachep->random_seq) {
@@ -2576,13 +2521,20 @@ static freelist_idx_t next_random_slot(union freelist_init_state *state) · 15 unmodified lines
25762521 return (state->list[state->pos++] + state->rand) % state->count;
25772522 }
25782523
2524+/* Swap two freelist entries */
2525+static void swap_free_obj(struct page *page, unsigned int a, unsigned int b)
2526+{
2527+ swap(((freelist_idx_t *)page->freelist)[a],
2528+ ((freelist_idx_t *)page->freelist)[b]);
2529+}
2530+
25792531 /*
25802532 * Shuffle the freelist initialization state based on pre-computed lists.
25812533 * return true if the list was successfully shuffled, false otherwise.
25822534 */
25832535 static bool shuffle_freelist(struct kmem_cache *cachep, struct page *page)
25842536 {
2585− unsigned int objfreelist = 0, i, count = cachep->num;
2537+ unsigned int objfreelist = 0, i, rand, count = cachep->num;
25862538 union freelist_init_state state;
25872539 bool precomputed;
25882540
@@ -2607,7 +2559,15 @@ static bool shuffle_freelist(struct kmem_cache *cachep, struct page *page) · 18 unmodified lines
26072559 * Later use a pre-computed list for speed.
26082560 */
26092561 if (!precomputed) {
2610− freelist_randomize(&state.rnd_state, page->freelist, count);
2562+ for (i = 0; i < count; i++)
2563+ set_free_obj(page, i, i);
2564+
2565+ /* Fisher-Yates shuffle */
2566+ for (i = count - 1; i > 0; i--) {
2567+ rand = prandom_u32_state(&state.rnd_state);
2568+ rand %= (i + 1);
2569+ swap_free_obj(page, i, rand);
2570+ }
26112571 } else {
26122572 for (i = 0; i < count; i++)
26132573 set_free_obj(page, i, next_random_slot(&state));
@@ -2726,8 +2686,11 @@ static struct page *cache_grow_begin(struct kmem_cache *cachep, · 112 unmodified lines
27262686 * critical path in kmem_cache_alloc().
27272687 */
27282688 if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
2729− pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
2730− BUG();
2689+ gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK;
2690+ flags &= ~GFP_SLAB_BUG_MASK;
2691+ pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
2692+ invalid_mask, &invalid_mask, flags, &flags);
2693+ dump_stack();
27312694 }
27322695 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
27332696
@@ -3489,8 +3452,7 @@ static void free_block(struct kmem_cache *cachep, void **objpp, · 755 unmodified lines
34893452 n->free_objects -= cachep->num;
34903453
34913454 page = list_last_entry(&n->slabs_free, struct page, lru);
3492− list_del(&page->lru);
3493− list_add(&page->lru, list);
3455+ list_move(&page->lru, list);
34943456 }
34953457 }
34963458
@@ -3979,7 +3941,7 @@ static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp) · 482 unmodified lines
39793941 int shared = 0;
39803942 int batchcount = 0;
39813943
3982− err = cache_random_seq_create(cachep, gfp);
3944+ err = cache_random_seq_create(cachep, cachep->num, gfp);
39833945 if (err)
39843946 goto end;
39853947
mm/slab.h+26−4
@@ -42,6 +42,7 @@ struct kmem_cache { · 41 unmodified lines
4242 #include <linux/kmemcheck.h>
4343 #include <linux/kasan.h>
4444 #include <linux/kmemleak.h>
45+#include <linux/random.h>
4546
4647 /*
4748 * State of the slab allocator.
@@ -253,8 +254,7 @@ static __always_inline int memcg_charge_slab(struct page *page, · 205 unmodified lines
253254 if (is_root_cache(s))
254255 return 0;
255256
256− ret = __memcg_kmem_charge_memcg(page, gfp, order,
257− s->memcg_params.memcg);
257+ ret = memcg_kmem_charge_memcg(page, gfp, order, s->memcg_params.memcg);
258258 if (ret)
259259 return ret;
260260
@@ -268,6 +268,9 @@ static __always_inline int memcg_charge_slab(struct page *page, · 7 unmodified lines
268268 static __always_inline void memcg_uncharge_slab(struct page *page, int order,
269269 struct kmem_cache *s)
270270 {
271+ if (!memcg_kmem_enabled())
272+ return;
273+
271274 memcg_kmem_update_page_stat(page,
272275 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
273276 MEMCG_SLAB_RECLAIMABLE : MEMCG_SLAB_UNRECLAIMABLE,
@@ -390,7 +393,11 @@ static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, · 116 unmodified lines
390393 if (should_failslab(s, flags))
391394 return NULL;
392395
393− return memcg_kmem_get_cache(s, flags);
396+ if (memcg_kmem_enabled() &&
397+ ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))
398+ return memcg_kmem_get_cache(s);
399+
400+ return s;
394401 }
395402
396403 static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
@@ -407,7 +414,9 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, · 10 unmodified lines
407414 s->flags, flags);
408415 kasan_slab_alloc(s, object, flags);
409416 }
410− memcg_kmem_put_cache(s);
417+
418+ if (memcg_kmem_enabled())
419+ memcg_kmem_put_cache(s);
411420 }
412421
413422 #ifndef CONFIG_SLOB
@@ -464,4 +473,17 @@ int memcg_slab_show(struct seq_file *m, void *p); · 50 unmodified lines
464473
465474 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
466475
476+#ifdef CONFIG_SLAB_FREELIST_RANDOM
477+int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
478+ gfp_t gfp);
479+void cache_random_seq_destroy(struct kmem_cache *cachep);
480+#else
481+static inline int cache_random_seq_create(struct kmem_cache *cachep,
482+ unsigned int count, gfp_t gfp)
483+{
484+ return 0;
485+}
486+static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
487+#endif /* CONFIG_SLAB_FREELIST_RANDOM */
488+
467489 #endif /* MM_SLAB_H */
mm/slab_common.c+48−1
@@ -1012,7 +1012,7 @@ void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) · 1011 unmodified lines
10121012 struct page *page;
10131013
10141014 flags |= __GFP_COMP;
1015− page = alloc_kmem_pages(flags, order);
1015+ page = alloc_pages(flags, order);
10161016 ret = page ? page_address(page) : NULL;
10171017 kmemleak_alloc(ret, size, 1, flags);
10181018 kasan_kmalloc_large(ret, size, flags);
@@ -1030,6 +1030,53 @@ void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) · 11 unmodified lines
10301030 EXPORT_SYMBOL(kmalloc_order_trace);
10311031 #endif
10321032
1033+#ifdef CONFIG_SLAB_FREELIST_RANDOM
1034+/* Randomize a generic freelist */
1035+static void freelist_randomize(struct rnd_state *state, unsigned int *list,
1036+ size_t count)
1037+{
1038+ size_t i;
1039+ unsigned int rand;
1040+
1041+ for (i = 0; i < count; i++)
1042+ list[i] = i;
1043+
1044+ /* Fisher-Yates shuffle */
1045+ for (i = count - 1; i > 0; i--) {
1046+ rand = prandom_u32_state(state);
1047+ rand %= (i + 1);
1048+ swap(list[i], list[rand]);
1049+ }
1050+}
1051+
1052+/* Create a random sequence per cache */
1053+int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
1054+ gfp_t gfp)
1055+{
1056+ struct rnd_state state;
1057+
1058+ if (count < 2 || cachep->random_seq)
1059+ return 0;
1060+
1061+ cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp);
1062+ if (!cachep->random_seq)
1063+ return -ENOMEM;
1064+
1065+ /* Get best entropy at this stage of boot */
1066+ prandom_seed_state(&state, get_random_long());
1067+
1068+ freelist_randomize(&state, cachep->random_seq, count);
1069+ return 0;
1070+}
1071+
1072+/* Destroy the per-cache random freelist sequence */
1073+void cache_random_seq_destroy(struct kmem_cache *cachep)
1074+{
1075+ kfree(cachep->random_seq);
1076+ cachep->random_seq = NULL;
1077+}
1078+#endif /* CONFIG_SLAB_FREELIST_RANDOM */
1079+
10331080 #ifdef CONFIG_SLABINFO
10341081
10351082 #ifdef CONFIG_SLAB
mm/slub.c+133−12
@@ -1405,6 +1405,109 @@ static inline struct page *alloc_slab_page(struct kmem_cache *s, · 1404 unmodified lines
14051405 return page;
14061406 }
14071407
1408+#ifdef CONFIG_SLAB_FREELIST_RANDOM
1409+/* Pre-initialize the random sequence cache */
1410+static int init_cache_random_seq(struct kmem_cache *s)
1411+{
1412+ int err;
1413+ unsigned long i, count = oo_objects(s->oo);
1414+
1415+ err = cache_random_seq_create(s, count, GFP_KERNEL);
1416+ if (err) {
1417+ pr_err("SLUB: Unable to initialize free list for %s\n",
1418+ s->name);
1419+ return err;
1420+ }
1421+
1422+ /* Transform to an offset on the set of pages */
1423+ if (s->random_seq) {
1424+ for (i = 0; i < count; i++)
1425+ s->random_seq[i] *= s->size;
1426+ }
1427+ return 0;
1428+}
1429+
1430+/* Initialize each random sequence freelist per cache */
1431+static void __init init_freelist_randomization(void)
1432+{
1433+ struct kmem_cache *s;
1434+
1435+ mutex_lock(&slab_mutex);
1436+
1437+ list_for_each_entry(s, &slab_caches, list)
1438+ init_cache_random_seq(s);
1439+
1440+ mutex_unlock(&slab_mutex);
1441+}
1442+
1443+/* Get the next entry on the pre-computed freelist randomized */
1444+static void *next_freelist_entry(struct kmem_cache *s, struct page *page,
1445+ unsigned long *pos, void *start,
1446+ unsigned long page_limit,
1447+ unsigned long freelist_count)
1448+{
1449+ unsigned int idx;
1450+
1451+ /*
1452+ * If the target page allocation failed, the number of objects on the
1453+ * page might be smaller than the usual size defined by the cache.
1454+ */
1455+ do {
1456+ idx = s->random_seq[*pos];
1457+ *pos += 1;
1458+ if (*pos >= freelist_count)
1459+ *pos = 0;
1460+ } while (unlikely(idx >= page_limit));
1461+
1462+ return (char *)start + idx;
1463+}
1464+
1465+/* Shuffle the single linked freelist based on a random pre-computed sequence */
1466+static bool shuffle_freelist(struct kmem_cache *s, struct page *page)
1467+{
1468+ void *start;
1469+ void *cur;
1470+ void *next;
1471+ unsigned long idx, pos, page_limit, freelist_count;
1472+
1473+ if (page->objects < 2 || !s->random_seq)
1474+ return false;
1475+
1476+ freelist_count = oo_objects(s->oo);
1477+ pos = get_random_int() % freelist_count;
1478+
1479+ page_limit = page->objects * s->size;
1480+ start = fixup_red_left(s, page_address(page));
1481+
1482+ /* First entry is used as the base of the freelist */
1483+ cur = next_freelist_entry(s, page, &pos, start, page_limit,
1484+ freelist_count);
1485+ page->freelist = cur;
1486+
1487+ for (idx = 1; idx < page->objects; idx++) {
1488+ setup_object(s, page, cur);
1489+ next = next_freelist_entry(s, page, &pos, start, page_limit,
1490+ freelist_count);
1491+ set_freepointer(s, cur, next);
1492+ cur = next;
1493+ }
1494+ setup_object(s, page, cur);
1495+ set_freepointer(s, cur, NULL);
1496+
1497+ return true;
1498+}
1499+#else
1500+static inline int init_cache_random_seq(struct kmem_cache *s)
1501+{
1502+ return 0;
1503+}
1504+static inline void init_freelist_randomization(void) { }
1505+static inline bool shuffle_freelist(struct kmem_cache *s, struct page *page)
1506+{
1507+ return false;
1508+}
1509+#endif /* CONFIG_SLAB_FREELIST_RANDOM */
1510+
14081511 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
14091512 {
14101513 struct page *page;
@@ -1412,6 +1515,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) · 1 unmodified line
14121515 gfp_t alloc_gfp;
14131516 void *start, *p;
14141517 int idx, order;
1518+ bool shuffle;
14151519
14161520 flags &= gfp_allowed_mask;
14171521
@@ -1473,15 +1577,19 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) · 55 unmodified lines
14731577
14741578 kasan_poison_slab(page);
14751579
1476− for_each_object_idx(p, idx, s, start, page->objects) {
1477− setup_object(s, page, p);
1478− if (likely(idx < page->objects))
1479− set_freepointer(s, p, p + s->size);
1480− else
1481− set_freepointer(s, p, NULL);
1580+ shuffle = shuffle_freelist(s, page);
1581+
1582+ if (!shuffle) {
1583+ for_each_object_idx(p, idx, s, start, page->objects) {
1584+ setup_object(s, page, p);
1585+ if (likely(idx < page->objects))
1586+ set_freepointer(s, p, p + s->size);
1587+ else
1588+ set_freepointer(s, p, NULL);
1589+ }
1590+ page->freelist = fixup_red_left(s, start);
14821591 }
14831592
1484− page->freelist = fixup_red_left(s, start);
14851593 page->inuse = page->objects;
14861594 page->frozen = 1;
14871595
@@ -1504,8 +1612,10 @@ out: · 16 unmodified lines
15041612 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
15051613 {
15061614 if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
1507− pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
1508− BUG();
1615+ gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK;
1616+ flags &= ~GFP_SLAB_BUG_MASK;
1617+ pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
1618+ invalid_mask, &invalid_mask, flags, &flags);
15091619 }
15101620
15111621 return allocate_slab(s,
@@ -2867,7 +2977,7 @@ int build_detached_freelist(struct kmem_cache *s, size_t size, · 1355 unmodified lines
28672977 if (unlikely(!PageSlab(page))) {
28682978 BUG_ON(!PageCompound(page));
28692979 kfree_hook(object);
2870− __free_kmem_pages(page, compound_order(page));
2980+ __free_pages(page, compound_order(page));
28712981 p[size] = NULL; /* mark object processed */
28722982 return size;
28732983 }
@@ -3207,6 +3317,7 @@ static void free_kmem_cache_nodes(struct kmem_cache *s) · 333 unmodified lines
32073317
32083318 void __kmem_cache_release(struct kmem_cache *s)
32093319 {
3320+ cache_random_seq_destroy(s);
32103321 free_percpu(s->cpu_slab);
32113322 free_kmem_cache_nodes(s);
32123323 }
@@ -3431,6 +3542,13 @@ static int kmem_cache_open(struct kmem_cache *s, unsigned long flags) · 218 unmodified lines
34313542 #ifdef CONFIG_NUMA
34323543 s->remote_node_defrag_ratio = 1000;
34333544 #endif
3545+
3546+ /* Initialize the pre-computed randomized freelist if slab is up */
3547+ if (slab_state >= UP) {
3548+ if (init_cache_random_seq(s))
3549+ goto error;
3550+ }
3551+
34343552 if (!init_kmem_cache_nodes(s))
34353553 goto error;
34363554
@@ -3575,7 +3693,7 @@ static void *kmalloc_large_node(size_t size, gfp_t flags, int node) · 138 unmodified lines
35753693 void *ptr = NULL;
35763694
35773695 flags |= __GFP_COMP | __GFP_NOTRACK;
3578− page = alloc_kmem_pages_node(node, flags, get_order(size));
3696+ page = alloc_pages_node(node, flags, get_order(size));
35793697 if (page)
35803698 ptr = page_address(page);
35813699
@@ -3656,7 +3774,7 @@ void kfree(const void *x) · 74 unmodified lines
36563774 if (unlikely(!PageSlab(page))) {
36573775 BUG_ON(!PageCompound(page));
36583776 kfree_hook(x);
3659− __free_kmem_pages(page, compound_order(page));
3777+ __free_pages(page, compound_order(page));
36603778 return;
36613779 }
36623780 slab_free(page->slab_cache, page, object, NULL, 1, _RET_IP_);
@@ -3947,6 +4065,9 @@ void __init kmem_cache_init(void) · 284 unmodified lines
39474065 setup_kmalloc_cache_index_table();
39484066 create_kmalloc_caches(0);
39494067
4068+ /* Setup random freelists for each cache */
4069+ init_freelist_randomization();
4070+
39504071 #ifdef CONFIG_SMP
39514072 register_cpu_notifier(&slab_notifier);
39524073 #endif
mm/swap.c+2−0
@@ -292,6 +292,7 @@ static bool need_activate_page_drain(int cpu) · 291 unmodified lines
292292
293293 void activate_page(struct page *page)
294294 {
295+ page = compound_head(page);
295296 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
296297 struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
297298
@@ -316,6 +317,7 @@ void activate_page(struct page *page) · 18 unmodified lines
316317 {
317318 struct zone *zone = page_zone(page);
318319
320+ page = compound_head(page);
319321 spin_lock_irq(&zone->lru_lock);
320322 __activate_page(page, mem_cgroup_page_lruvec(page, zone), NULL);
321323 spin_unlock_irq(&zone->lru_lock);
mm/swapfile.c+1−1
@@ -2493,7 +2493,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · 2492 unmodified lines
24932493 goto bad_swap;
24942494 }
24952495 /* frontswap enabled? set up bit-per-page map for frontswap */
2496− if (frontswap_enabled)
2496+ if (IS_ENABLED(CONFIG_FRONTSWAP))
24972497 frontswap_map = vzalloc(BITS_TO_LONGS(maxpages) * sizeof(long));
24982498
24992499 if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {
mm/truncate.c+23−5
@@ -155,10 +155,14 @@ invalidate_complete_page(struct address_space *mapping, struct page *page) · 154 unmodified lines
155155
156156 int truncate_inode_page(struct address_space *mapping, struct page *page)
157157 {
158+ loff_t holelen;
159+ VM_BUG_ON_PAGE(PageTail(page), page);
160+
161+ holelen = PageTransHuge(page) ? HPAGE_PMD_SIZE : PAGE_SIZE;
158162 if (page_mapped(page)) {
159163 unmap_mapping_range(mapping,
160164 (loff_t)page->index << PAGE_SHIFT,
161− PAGE_SIZE, 0);
165+ holelen, 0);
162166 }
163167 return truncate_complete_page(mapping, page);
164168 }
@@ -279,7 +283,7 @@ void truncate_inode_pages_range(struct address_space *mapping, · 114 unmodified lines
279283
280284 if (!trylock_page(page))
281285 continue;
282− WARN_ON(page->index != index);
286+ WARN_ON(page_to_pgoff(page) != index);
283287 if (PageWriteback(page)) {
284288 unlock_page(page);
285289 continue;
@@ -367,7 +371,7 @@ void truncate_inode_pages_range(struct address_space *mapping, · 81 unmodified lines
367371 }
368372
369373 lock_page(page);
370− WARN_ON(page->index != index);
374+ WARN_ON(page_to_pgoff(page) != index);
371375 wait_on_page_writeback(page);
372376 truncate_inode_page(mapping, page);
373377 unlock_page(page);
@@ -487,7 +491,21 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping, · 113 unmodified lines
487491
488492 if (!trylock_page(page))
489493 continue;
490− WARN_ON(page->index != index);
494+
495+ WARN_ON(page_to_pgoff(page) != index);
496+
497+ /* Middle of THP: skip */
498+ if (PageTransTail(page)) {
499+ unlock_page(page);
500+ continue;
501+ } else if (PageTransHuge(page)) {
502+ index += HPAGE_PMD_NR - 1;
503+ i += HPAGE_PMD_NR - 1;
504+ /* 'end' is in the middle of THP */
505+ if (index == round_down(end, HPAGE_PMD_NR))
506+ continue;
507+ }
508+
491509 ret = invalidate_inode_page(page);
492510 unlock_page(page);
493511 /*
@@ -594,7 +612,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping, · 100 unmodified lines
594612 }
595613
596614 lock_page(page);
597− WARN_ON(page->index != index);
615+ WARN_ON(page_to_pgoff(page) != index);
598616 if (page->mapping != mapping) {
599617 unlock_page(page);
600618 continue;
mm/util.c+10−2
@@ -399,10 +399,12 @@ struct address_space *page_mapping(struct page *page) · 398 unmodified lines
399399 }
400400
401401 mapping = page->mapping;
402− if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
402+ if ((unsigned long)mapping & PAGE_MAPPING_ANON)
403403 return NULL;
404− return mapping;
404+
405+ return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
405406 }
407+EXPORT_SYMBOL(page_mapping);
406408
407409 /* Slow path of page_mapcount() for compound pages */
408410 int __page_mapcount(struct page *page)
@@ -410,6 +412,12 @@ int __page_mapcount(struct page *page) · 1 unmodified line
410412 int ret;
411413
412414 ret = atomic_read(&page->_mapcount) + 1;
415+ /*
416+ * For file THP page->_mapcount contains total number of mapping
417+ * of the page: no need to look into compound_mapcount.
418+ */
419+ if (!PageAnon(page) && !PageHuge(page))
420+ return ret;
413421 page = compound_head(page);
414422 ret += atomic_read(compound_mapcount_ptr(page)) + 1;
415423 if (PageDoubleMap(page))
mm/vmalloc.c+3−3
@@ -1501,7 +1501,7 @@ static void __vunmap(const void *addr, int deallocate_pages) · 1500 unmodified lines
15011501 struct page *page = area->pages[i];
15021502
15031503 BUG_ON(!page);
1504− __free_kmem_pages(page, 0);
1504+ __free_pages(page, 0);
15051505 }
15061506
15071507 kvfree(area->pages);
@@ -1629,9 +1629,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · 121 unmodified lines
16291629 struct page *page;
16301630
16311631 if (node == NUMA_NO_NODE)
1632− page = alloc_kmem_pages(alloc_mask, order);
1632+ page = alloc_pages(alloc_mask, order);
16331633 else
1634− page = alloc_kmem_pages_node(node, alloc_mask, order);
1634+ page = alloc_pages_node(node, alloc_mask, order);
16351635
16361636 if (unlikely(!page)) {
16371637 /* Successfully allocated i pages, free them in __vunmap() */
mm/vmscan.c+7−1
@@ -1055,8 +1055,14 @@ static unsigned long shrink_page_list(struct list_head *page_list, · 1054 unmodified lines
10551055
10561056 /* Adding to swap updated mapping */
10571057 mapping = page_mapping(page);
1058+ } else if (unlikely(PageTransHuge(page))) {
1059+ /* Split file THP */
1060+ if (split_huge_page_to_list(page, page_list))
1061+ goto keep_locked;
10581062 }
10591063
1064+ VM_BUG_ON_PAGE(PageTransHuge(page), page);
1065+
10601066 /*
10611067 * The page is mapped into the page tables of one or more
10621068 * processes. Try to unmap it here.
@@ -1254,7 +1260,7 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone, · 191 unmodified lines
12541260
12551261 list_for_each_entry_safe(page, next, page_list, lru) {
12561262 if (page_is_file_cache(page) && !PageDirty(page) &&
1257− !isolated_balloon_page(page)) {
1263+ !__PageMovable(page)) {
12581264 ClearPageActive(page);
12591265 list_move(&page->lru, &clean_pages);
12601266 }
mm/vmstat.c+7−1
@@ -718,7 +718,9 @@ const char * const vmstat_text[] = { · 717 unmodified lines
718718 "nr_dirtied",
719719 "nr_written",
720720 "nr_pages_scanned",
721−
721+#if IS_ENABLED(CONFIG_ZSMALLOC)
722+ "nr_zspages",
723+#endif
722724 #ifdef CONFIG_NUMA
723725 "numa_hit",
724726 "numa_miss",
@@ -731,6 +733,8 @@ const char * const vmstat_text[] = { · 6 unmodified lines
731733 "workingset_activate",
732734 "workingset_nodereclaim",
733735 "nr_anon_transparent_hugepages",
736+ "nr_shmem_hugepages",
737+ "nr_shmem_pmdmapped",
734738 "nr_free_cma",
735739
736740 /* enum writeback_stat_item counters */
@@ -815,6 +819,8 @@ const char * const vmstat_text[] = { · 78 unmodified lines
815819 "thp_fault_fallback",
816820 "thp_collapse_alloc",
817821 "thp_collapse_alloc_failed",
822+ "thp_file_alloc",
823+ "thp_file_mapped",
818824 "thp_split_page",
819825 "thp_split_page_failed",
820826 "thp_deferred_split_page",
mm/zsmalloc.c+945−405
@@ -16,32 +16,15 @@ · 15 unmodified lines
1616 * struct page(s) to form a zspage.
1717 *
1818 * Usage of struct page fields:
19− * page->private: points to the first component (0-order) page
20− * page->index (union with page->freelist): offset of the first object
21− * starting in this page. For the first page, this is
22− * always 0, so we use this field (aka freelist) to point
23− * to the first free object in zspage.
24− * page->lru: links together all component pages (except the first page)
25− * of a zspage
26− *
27− * For _first_ page only:
28− *
29− * page->private: refers to the component page after the first page
30− * If the page is first_page for huge object, it stores handle.
31− * Look at size_class->huge.
32− * page->freelist: points to the first free object in zspage.
33− * Free objects are linked together using in-place
34− * metadata.
35− * page->objects: maximum number of objects we can store in this
36− * zspage (class->zspage_order * PAGE_SIZE / class->size)
37− * page->lru: links together first pages of various zspages.
38− * Basically forming list of zspages in a fullness group.
39− * page->mapping: class index and fullness group of the zspage
40− * page->inuse: the number of objects that are used in this zspage
19+ * page->private: points to zspage
20+ * page->freelist(index): links together all component pages of a zspage
21+ * For the huge page, this is always 0, so we use this field
22+ * to store handle.
4123 *
4224 * Usage of struct page flags:
4325 * PG_private: identifies the first component page
4426 * PG_private2: identifies the last component page
27+ * PG_owner_priv_1: indentifies the huge component page
4528 *
4629 */
4730
@@ -66,6 +49,11 @@ · 18 unmodified lines
6649 #include <linux/debugfs.h>
6750 #include <linux/zsmalloc.h>
6851 #include <linux/zpool.h>
52+#include <linux/mount.h>
53+#include <linux/migrate.h>
54+#include <linux/pagemap.h>
55+
56+#define ZSPAGE_MAGIC 0x58
6957
7058 /*
7159 * This must be power of 2 and greater than of equal to sizeof(link_free).
@@ -88,9 +76,7 @@ · 16 unmodified lines
8876 * Object location (<PFN>, <obj_idx>) is encoded as
8977 * as single (unsigned long) handle value.
9078 *
91− * Note that object index <obj_idx> is relative to system
92− * page <PFN> it is stored in, so for each sub-page belonging
93− * to a zspage, obj_idx starts with 0.
79+ * Note that object index <obj_idx> starts from 0.
9480 *
9581 * This is made more complicated by various memory models and PAE.
9682 */
@@ -149,33 +135,29 @@ · 52 unmodified lines
149135 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
150136 * (reason above)
151137 */
152−#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
138+#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
153139
154140 /*
155141 * We do not maintain any list for completely empty or full pages
156142 */
157143 enum fullness_group {
158− ZS_ALMOST_FULL,
159− ZS_ALMOST_EMPTY,
160− _ZS_NR_FULLNESS_GROUPS,
161−
162144 ZS_EMPTY,
163− ZS_FULL
145+ ZS_ALMOST_EMPTY,
146+ ZS_ALMOST_FULL,
147+ ZS_FULL,
148+ NR_ZS_FULLNESS,
164149 };
165150
166151 enum zs_stat_type {
152+ CLASS_EMPTY,
153+ CLASS_ALMOST_EMPTY,
154+ CLASS_ALMOST_FULL,
155+ CLASS_FULL,
167156 OBJ_ALLOCATED,
168157 OBJ_USED,
169− CLASS_ALMOST_FULL,
170− CLASS_ALMOST_EMPTY,
158+ NR_ZS_STAT_TYPE,
171159 };
172160
173−#ifdef CONFIG_ZSMALLOC_STAT
174−#define NR_ZS_STAT_TYPE (CLASS_ALMOST_EMPTY + 1)
175−#else
176−#define NR_ZS_STAT_TYPE (OBJ_USED + 1)
177−#endif
178−
179161 struct zs_size_stat {
180162 unsigned long objs[NR_ZS_STAT_TYPE];
181163 };
@@ -184,6 +166,10 @@ struct zs_size_stat { · 2 unmodified lines
184166 static struct dentry *zs_stat_root;
185167 #endif
186168
169+#ifdef CONFIG_COMPACTION
170+static struct vfsmount *zsmalloc_mnt;
171+#endif
172+
187173 /*
188174 * number of size_classes
189175 */
@@ -207,35 +193,49 @@ static const int fullness_threshold_frac = 4; · 17 unmodified lines
207193
208194 struct size_class {
209195 spinlock_t lock;
210− struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
196+ struct list_head fullness_list[NR_ZS_FULLNESS];
211197 /*
212198 * Size of objects stored in this class. Must be multiple
213199 * of ZS_ALIGN.
214200 */
215201 int size;
216− unsigned int index;
217−
218− struct zs_size_stat stats;
219−
202+ int objs_per_zspage;
220203 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
221204 int pages_per_zspage;
222− /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
223− bool huge;
205+
206+ unsigned int index;
207+ struct zs_size_stat stats;
224208 };
225209
210+/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
211+static void SetPageHugeObject(struct page *page)
212+{
213+ SetPageOwnerPriv1(page);
214+}
215+
216+static void ClearPageHugeObject(struct page *page)
217+{
218+ ClearPageOwnerPriv1(page);
219+}
220+
221+static int PageHugeObject(struct page *page)
222+{
223+ return PageOwnerPriv1(page);
224+}
225+
226226 /*
227227 * Placed within free objects to form a singly linked list.
228− * For every zspage, first_page->freelist gives head of this list.
228+ * For every zspage, zspage->freeobj gives head of this list.
229229 *
230230 * This must be power of 2 and less than or equal to ZS_ALIGN
231231 */
232232 struct link_free {
233233 union {
234234 /*
235− * Position of next free chunk (encodes <PFN, obj_idx>)
235+ * Free object index;
236236 * It's valid for non-allocated object
237237 */
238− void *next;
238+ unsigned long next;
239239 /*
240240 * Handle of allocated object.
241241 */
@@ -248,6 +248,7 @@ struct zs_pool { · 6 unmodified lines
248248
249249 struct size_class **size_class;
250250 struct kmem_cache *handle_cachep;
251+ struct kmem_cache *zspage_cachep;
251252
252253 atomic_long_t pages_allocated;
253254
@@ -263,16 +264,36 @@ struct zs_pool { · 9 unmodified lines
263264 #ifdef CONFIG_ZSMALLOC_STAT
264265 struct dentry *stat_dentry;
265266 #endif
267+#ifdef CONFIG_COMPACTION
268+ struct inode *inode;
269+ struct work_struct free_work;
270+#endif
266271 };
267272
268273 /*
269274 * A zspage's class index and fullness group
270275 * are encoded in its (first)page->mapping
271276 */
272−#define CLASS_IDX_BITS 28
273−#define FULLNESS_BITS 4
274−#define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
275−#define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
277+#define FULLNESS_BITS 2
278+#define CLASS_BITS 8
279+#define ISOLATED_BITS 3
280+#define MAGIC_VAL_BITS 8
281+
282+struct zspage {
283+ struct {
284+ unsigned int fullness:FULLNESS_BITS;
285+ unsigned int class:CLASS_BITS;
286+ unsigned int isolated:ISOLATED_BITS;
287+ unsigned int magic:MAGIC_VAL_BITS;
288+ };
289+ unsigned int inuse;
290+ unsigned int freeobj;
291+ struct page *first_page;
292+ struct list_head list; /* fullness list */
293+#ifdef CONFIG_COMPACTION
294+ rwlock_t lock;
295+#endif
296+};
276297
277298 struct mapping_area {
278299 #ifdef CONFIG_PGTABLE_MAPPING
@@ -284,29 +305,74 @@ struct mapping_area { · 5 unmodified lines
284305 enum zs_mapmode vm_mm; /* mapping mode */
285306 };
286307
287−static int create_handle_cache(struct zs_pool *pool)
308+#ifdef CONFIG_COMPACTION
309+static int zs_register_migration(struct zs_pool *pool);
310+static void zs_unregister_migration(struct zs_pool *pool);
311+static void migrate_lock_init(struct zspage *zspage);
312+static void migrate_read_lock(struct zspage *zspage);
313+static void migrate_read_unlock(struct zspage *zspage);
314+static void kick_deferred_free(struct zs_pool *pool);
315+static void init_deferred_free(struct zs_pool *pool);
316+static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
317+#else
318+static int zsmalloc_mount(void) { return 0; }
319+static void zsmalloc_unmount(void) {}
320+static int zs_register_migration(struct zs_pool *pool) { return 0; }
321+static void zs_unregister_migration(struct zs_pool *pool) {}
322+static void migrate_lock_init(struct zspage *zspage) {}
323+static void migrate_read_lock(struct zspage *zspage) {}
324+static void migrate_read_unlock(struct zspage *zspage) {}
325+static void kick_deferred_free(struct zs_pool *pool) {}
326+static void init_deferred_free(struct zs_pool *pool) {}
327+static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
328+#endif
329+
330+static int create_cache(struct zs_pool *pool)
288331 {
289332 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
290333 0, 0, NULL);
291− return pool->handle_cachep ? 0 : 1;
334+ if (!pool->handle_cachep)
335+ return 1;
336+
337+ pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
338+ 0, 0, NULL);
339+ if (!pool->zspage_cachep) {
340+ kmem_cache_destroy(pool->handle_cachep);
341+ pool->handle_cachep = NULL;
342+ return 1;
343+ }
344+
345+ return 0;
292346 }
293347
294−static void destroy_handle_cache(struct zs_pool *pool)
348+static void destroy_cache(struct zs_pool *pool)
295349 {
296350 kmem_cache_destroy(pool->handle_cachep);
351+ kmem_cache_destroy(pool->zspage_cachep);
297352 }
298353
299−static unsigned long alloc_handle(struct zs_pool *pool, gfp_t gfp)
354+static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
300355 {
301356 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
302− gfp & ~__GFP_HIGHMEM);
357+ gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
303358 }
304359
305−static void free_handle(struct zs_pool *pool, unsigned long handle)
360+static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
306361 {
307362 kmem_cache_free(pool->handle_cachep, (void *)handle);
308363 }
309364
365+static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
366+{
367+ return kmem_cache_alloc(pool->zspage_cachep,
368+ flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
369+};
370+
371+static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
372+{
373+ kmem_cache_free(pool->zspage_cachep, zspage);
374+}
375+
310376 static void record_obj(unsigned long handle, unsigned long obj)
311377 {
312378 /*
@@ -409,38 +475,76 @@ static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage) · 96 unmodified lines
409475 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
410476 static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
411477
478+static bool is_zspage_isolated(struct zspage *zspage)
479+{
480+ return zspage->isolated;
481+}
482+
412483 static int is_first_page(struct page *page)
413484 {
414485 return PagePrivate(page);
415486 }
416487
417−static int is_last_page(struct page *page)
488+/* Protected by class->lock */
489+static inline int get_zspage_inuse(struct zspage *zspage)
490+{
491+ return zspage->inuse;
492+}
493+
494+static inline void set_zspage_inuse(struct zspage *zspage, int val)
495+{
496+ zspage->inuse = val;
497+}
498+
499+static inline void mod_zspage_inuse(struct zspage *zspage, int val)
500+{
501+ zspage->inuse += val;
502+}
503+
504+static inline struct page *get_first_page(struct zspage *zspage)
505+{
506+ struct page *first_page = zspage->first_page;
507+
508+ VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
509+ return first_page;
510+}
511+
512+static inline int get_first_obj_offset(struct page *page)
513+{
514+ return page->units;
515+}
516+
517+static inline void set_first_obj_offset(struct page *page, int offset)
518+{
519+ page->units = offset;
520+}
521+
522+static inline unsigned int get_freeobj(struct zspage *zspage)
523+{
524+ return zspage->freeobj;
525+}
526+
527+static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
418528 {
419− return PagePrivate2(page);
529+ zspage->freeobj = obj;
420530 }
421531
422−static void get_zspage_mapping(struct page *first_page,
532+static void get_zspage_mapping(struct zspage *zspage,
423533 unsigned int *class_idx,
424534 enum fullness_group *fullness)
425535 {
426− unsigned long m;
427− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
536+ BUG_ON(zspage->magic != ZSPAGE_MAGIC);
428537
429− m = (unsigned long)first_page->mapping;
430− *fullness = m & FULLNESS_MASK;
431− *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
538+ *fullness = zspage->fullness;
539+ *class_idx = zspage->class;
432540 }
433541
434−static void set_zspage_mapping(struct page *first_page,
542+static void set_zspage_mapping(struct zspage *zspage,
435543 unsigned int class_idx,
436544 enum fullness_group fullness)
437545 {
438− unsigned long m;
439− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
440−
441− m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
442− (fullness & FULLNESS_MASK);
443− first_page->mapping = (struct address_space *)m;
546+ zspage->class = class_idx;
547+ zspage->fullness = fullness;
444548 }
445549
446550 /*
@@ -464,23 +568,19 @@ static int get_size_class_index(int size) · 17 unmodified lines
464568 static inline void zs_stat_inc(struct size_class *class,
465569 enum zs_stat_type type, unsigned long cnt)
466570 {
467− if (type < NR_ZS_STAT_TYPE)
468− class->stats.objs[type] += cnt;
571+ class->stats.objs[type] += cnt;
469572 }
470573
471574 static inline void zs_stat_dec(struct size_class *class,
472575 enum zs_stat_type type, unsigned long cnt)
473576 {
474− if (type < NR_ZS_STAT_TYPE)
475− class->stats.objs[type] -= cnt;
577+ class->stats.objs[type] -= cnt;
476578 }
477579
478580 static inline unsigned long zs_stat_get(struct size_class *class,
479581 enum zs_stat_type type)
480582 {
481− if (type < NR_ZS_STAT_TYPE)
482− return class->stats.objs[type];
483− return 0;
583+ return class->stats.objs[type];
484584 }
485585
486586 #ifdef CONFIG_ZSMALLOC_STAT
@@ -624,6 +724,7 @@ static inline void zs_pool_stat_destroy(struct zs_pool *pool) · 137 unmodified lines
624724 }
625725 #endif
626726
727+
627728 /*
628729 * For each size class, zspages are divided into different groups
629730 * depending on how "full" they are. This was done so that we could
@@ -631,21 +732,20 @@ static inline void zs_pool_stat_destroy(struct zs_pool *pool) · 1 unmodified line
631732 * the pool (not yet implemented). This function returns fullness
632733 * status of the given page.
633734 */
634−static enum fullness_group get_fullness_group(struct page *first_page)
735+static enum fullness_group get_fullness_group(struct size_class *class,
736+ struct zspage *zspage)
635737 {
636− int inuse, max_objects;
738+ int inuse, objs_per_zspage;
637739 enum fullness_group fg;
638740
639− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
640−
641− inuse = first_page->inuse;
642− max_objects = first_page->objects;
741+ inuse = get_zspage_inuse(zspage);
742+ objs_per_zspage = class->objs_per_zspage;
643743
644744 if (inuse == 0)
645745 fg = ZS_EMPTY;
646− else if (inuse == max_objects)
746+ else if (inuse == objs_per_zspage)
647747 fg = ZS_FULL;
648− else if (inuse <= 3 * max_objects / fullness_threshold_frac)
748+ else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
649749 fg = ZS_ALMOST_EMPTY;
650750 else
651751 fg = ZS_ALMOST_FULL;
@@ -660,32 +760,25 @@ static enum fullness_group get_fullness_group(struct page *first_page) · 8 unmodified lines
660760 * identified by <class, fullness_group>.
661761 */
662762 static void insert_zspage(struct size_class *class,
663− enum fullness_group fullness,
664− struct page *first_page)
763+ struct zspage *zspage,
764+ enum fullness_group fullness)
665765 {
666− struct page **head;
667−
668− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
669−
670− if (fullness >= _ZS_NR_FULLNESS_GROUPS)
671− return;
672−
673− zs_stat_inc(class, fullness == ZS_ALMOST_EMPTY ?
674− CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
675−
676− head = &class->fullness_list[fullness];
677− if (!*head) {
678− *head = first_page;
679− return;
680− }
766+ struct zspage *head;
681767
768+ zs_stat_inc(class, fullness, 1);
769+ head = list_first_entry_or_null(&class->fullness_list[fullness],
770+ struct zspage, list);
682771 /*
683− * We want to see more ZS_FULL pages and less almost
684− * empty/full. Put pages with higher ->inuse first.
772+ * We want to see more ZS_FULL pages and less almost empty/full.
773+ * Put pages with higher ->inuse first.
685774 */
686− list_add_tail(&first_page->lru, &(*head)->lru);
687− if (first_page->inuse >= (*head)->inuse)
688− *head = first_page;
775+ if (head) {
776+ if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
777+ list_add(&zspage->list, &head->list);
778+ return;
779+ }
780+ }
781+ list_add(&zspage->list, &class->fullness_list[fullness]);
689782 }
690783
691784 /*
@@ -693,27 +786,14 @@ static void insert_zspage(struct size_class *class, · 1 unmodified line
693786 * by <class, fullness_group>.
694787 */
695788 static void remove_zspage(struct size_class *class,
696− enum fullness_group fullness,
697− struct page *first_page)
789+ struct zspage *zspage,
790+ enum fullness_group fullness)
698791 {
699− struct page **head;
700−
701− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
702−
703− if (fullness >= _ZS_NR_FULLNESS_GROUPS)
704− return;
705−
706− head = &class->fullness_list[fullness];
707− VM_BUG_ON_PAGE(!*head, first_page);
708− if (list_empty(&(*head)->lru))
709− *head = NULL;
710− else if (*head == first_page)
711− *head = (struct page *)list_entry((*head)->lru.next,
712− struct page, lru);
792+ VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
793+ VM_BUG_ON(is_zspage_isolated(zspage));
713794
714− list_del_init(&first_page->lru);
715− zs_stat_dec(class, fullness == ZS_ALMOST_EMPTY ?
716− CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
795+ list_del_init(&zspage->list);
796+ zs_stat_dec(class, fullness, 1);
717797 }
718798
719799 /*
@@ -726,19 +806,22 @@ static void remove_zspage(struct size_class *class, · 6 unmodified lines
726806 * fullness group.
727807 */
728808 static enum fullness_group fix_fullness_group(struct size_class *class,
729− struct page *first_page)
809+ struct zspage *zspage)
730810 {
731811 int class_idx;
732812 enum fullness_group currfg, newfg;
733813
734− get_zspage_mapping(first_page, &class_idx, &currfg);
735− newfg = get_fullness_group(first_page);
814+ get_zspage_mapping(zspage, &class_idx, &currfg);
815+ newfg = get_fullness_group(class, zspage);
736816 if (newfg == currfg)
737817 goto out;
738818
739− remove_zspage(class, currfg, first_page);
740− insert_zspage(class, newfg, first_page);
741− set_zspage_mapping(first_page, class_idx, newfg);
819+ if (!is_zspage_isolated(zspage)) {
820+ remove_zspage(class, zspage, currfg);
821+ insert_zspage(class, zspage, newfg);
822+ }
823+
824+ set_zspage_mapping(zspage, class_idx, newfg);
742825
743826 out:
744827 return newfg;
@@ -780,64 +863,49 @@ static int get_pages_per_zspage(int class_size) · 35 unmodified lines
780863 return max_usedpc_order;
781864 }
782865
783−/*
784− * A single 'zspage' is composed of many system pages which are
785− * linked together using fields in struct page. This function finds
786− * the first/head page, given any component page of a zspage.
787− */
788−static struct page *get_first_page(struct page *page)
866+static struct zspage *get_zspage(struct page *page)
789867 {
790− if (is_first_page(page))
791− return page;
792− else
793− return (struct page *)page_private(page);
868+ struct zspage *zspage = (struct zspage *)page->private;
869+
870+ BUG_ON(zspage->magic != ZSPAGE_MAGIC);
871+ return zspage;
794872 }
795873
796874 static struct page *get_next_page(struct page *page)
797875 {
798− struct page *next;
876+ if (unlikely(PageHugeObject(page)))
877+ return NULL;
799878
800− if (is_last_page(page))
801− next = NULL;
802− else if (is_first_page(page))
803− next = (struct page *)page_private(page);
804− else
805− next = list_entry(page->lru.next, struct page, lru);
879+ return page->freelist;
880+}
806881
807− return next;
882+/**
883+ * obj_to_location - get (<page>, <obj_idx>) from encoded object value
884+ * @page: page object resides in zspage
885+ * @obj_idx: object index
886+ */
887+static void obj_to_location(unsigned long obj, struct page **page,
888+ unsigned int *obj_idx)
889+{
890+ obj >>= OBJ_TAG_BITS;
891+ *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
892+ *obj_idx = (obj & OBJ_INDEX_MASK);
808893 }
809894
810−/*
811− * Encode <page, obj_idx> as a single handle value.
812− * We use the least bit of handle for tagging.
895+/**
896+ * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
897+ * @page: page object resides in zspage
898+ * @obj_idx: object index
813899 */
814−static void *location_to_obj(struct page *page, unsigned long obj_idx)
900+static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
815901 {
816902 unsigned long obj;
817903
818− if (!page) {
819− VM_BUG_ON(obj_idx);
820− return NULL;
821− }
822−
823904 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
824− obj |= ((obj_idx) & OBJ_INDEX_MASK);
905+ obj |= obj_idx & OBJ_INDEX_MASK;
825906 obj <<= OBJ_TAG_BITS;
826907
827− return (void *)obj;
828−}
829−
830−/*
831− * Decode <page, obj_idx> pair from the given object handle. We adjust the
832− * decoded obj_idx back to its original value since it was adjusted in
833− * location_to_obj().
834− */
835−static void obj_to_location(unsigned long obj, struct page **page,
836− unsigned long *obj_idx)
837−{
838− obj >>= OBJ_TAG_BITS;
839− *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
840− *obj_idx = (obj & OBJ_INDEX_MASK);
908+ return obj;
841909 }
842910
843911 static unsigned long handle_to_obj(unsigned long handle)
@@ -845,109 +913,147 @@ static unsigned long handle_to_obj(unsigned long handle) · 1 unmodified line
845913 return *(unsigned long *)handle;
846914 }
847915
848−static unsigned long obj_to_head(struct size_class *class, struct page *page,
849− void *obj)
916+static unsigned long obj_to_head(struct page *page, void *obj)
850917 {
851− if (class->huge) {
918+ if (unlikely(PageHugeObject(page))) {
852919 VM_BUG_ON_PAGE(!is_first_page(page), page);
853− return page_private(page);
920+ return page->index;
854921 } else
855922 return *(unsigned long *)obj;
856923 }
857924
858−static unsigned long obj_idx_to_offset(struct page *page,
859− unsigned long obj_idx, int class_size)
925+static inline int testpin_tag(unsigned long handle)
860926 {
861− unsigned long off = 0;
862−
863− if (!is_first_page(page))
864− off = page->index;
865−
866− return off + obj_idx * class_size;
927+ return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
867928 }
868929
869930 static inline int trypin_tag(unsigned long handle)
870931 {
871− unsigned long *ptr = (unsigned long *)handle;
872−
873− return !test_and_set_bit_lock(HANDLE_PIN_BIT, ptr);
932+ return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
874933 }
875934
876935 static void pin_tag(unsigned long handle)
877936 {
878− while (!trypin_tag(handle));
937+ bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
879938 }
880939
881940 static void unpin_tag(unsigned long handle)
882941 {
883− unsigned long *ptr = (unsigned long *)handle;
884−
885− clear_bit_unlock(HANDLE_PIN_BIT, ptr);
942+ bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
886943 }
887944
888945 static void reset_page(struct page *page)
889946 {
947+ __ClearPageMovable(page);
890948 clear_bit(PG_private, &page->flags);
891949 clear_bit(PG_private_2, &page->flags);
892950 set_page_private(page, 0);
893− page->mapping = NULL;
894− page->freelist = NULL;
895951 page_mapcount_reset(page);
952+ ClearPageHugeObject(page);
953+ page->freelist = NULL;
954+}
955+
956+/*
957+ * To prevent zspage destroy during migration, zspage freeing should
958+ * hold locks of all pages in the zspage.
959+ */
960+void lock_zspage(struct zspage *zspage)
961+{
962+ struct page *page = get_first_page(zspage);
963+
964+ do {
965+ lock_page(page);
966+ } while ((page = get_next_page(page)) != NULL);
896967 }
897968
898−static void free_zspage(struct page *first_page)
969+int trylock_zspage(struct zspage *zspage)
899970 {
900− struct page *nextp, *tmp, *head_extra;
971+ struct page *cursor, *fail;
901972
902− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
903− VM_BUG_ON_PAGE(first_page->inuse, first_page);
973+ for (cursor = get_first_page(zspage); cursor != NULL; cursor =
974+ get_next_page(cursor)) {
975+ if (!trylock_page(cursor)) {
976+ fail = cursor;
977+ goto unlock;
978+ }
979+ }
904980
905− head_extra = (struct page *)page_private(first_page);
981+ return 1;
982+unlock:
983+ for (cursor = get_first_page(zspage); cursor != fail; cursor =
984+ get_next_page(cursor))
985+ unlock_page(cursor);
906986
907− reset_page(first_page);
908− __free_page(first_page);
987+ return 0;
988+}
909989
910− /* zspage with only 1 system page */
911− if (!head_extra)
912− return;
990+static void __free_zspage(struct zs_pool *pool, struct size_class *class,
991+ struct zspage *zspage)
992+{
993+ struct page *page, *next;
994+ enum fullness_group fg;
995+ unsigned int class_idx;
913996
914− list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
915− list_del(&nextp->lru);
916− reset_page(nextp);
917− __free_page(nextp);
997+ get_zspage_mapping(zspage, &class_idx, &fg);
998+
999+ assert_spin_locked(&class->lock);
1000+
1001+ VM_BUG_ON(get_zspage_inuse(zspage));
1002+ VM_BUG_ON(fg != ZS_EMPTY);
1003+
1004+ next = page = get_first_page(zspage);
1005+ do {
1006+ VM_BUG_ON_PAGE(!PageLocked(page), page);
1007+ next = get_next_page(page);
1008+ reset_page(page);
1009+ unlock_page(page);
1010+ dec_zone_page_state(page, NR_ZSPAGES);
1011+ put_page(page);
1012+ page = next;
1013+ } while (page != NULL);
1014+
1015+ cache_free_zspage(pool, zspage);
1016+
1017+ zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1018+ class->size, class->pages_per_zspage));
1019+ atomic_long_sub(class->pages_per_zspage,
1020+ &pool->pages_allocated);
1021+}
1022+
1023+static void free_zspage(struct zs_pool *pool, struct size_class *class,
1024+ struct zspage *zspage)
1025+{
1026+ VM_BUG_ON(get_zspage_inuse(zspage));
1027+ VM_BUG_ON(list_empty(&zspage->list));
1028+
1029+ if (!trylock_zspage(zspage)) {
1030+ kick_deferred_free(pool);
1031+ return;
9181032 }
919− reset_page(head_extra);
920− __free_page(head_extra);
1033+
1034+ remove_zspage(class, zspage, ZS_EMPTY);
1035+ __free_zspage(pool, class, zspage);
9211036 }
9221037
9231038 /* Initialize a newly allocated zspage */
924−static void init_zspage(struct size_class *class, struct page *first_page)
1039+static void init_zspage(struct size_class *class, struct zspage *zspage)
9251040 {
1041+ unsigned int freeobj = 1;
9261042 unsigned long off = 0;
927− struct page *page = first_page;
928−
929− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
1043+ struct page *page = get_first_page(zspage);
9301044
9311045 while (page) {
9321046 struct page *next_page;
9331047 struct link_free *link;
934− unsigned int i = 1;
9351048 void *vaddr;
9361049
937− /*
938− * page->index stores offset of first object starting
939− * in the page. For the first page, this is always 0,
940− * so we use first_page->index (aka ->freelist) to store
941− * head of corresponding zspage's freelist.
942− */
943− if (page != first_page)
944− page->index = off;
1050+ set_first_obj_offset(page, off);
9451051
9461052 vaddr = kmap_atomic(page);
9471053 link = (struct link_free *)vaddr + off / sizeof(*link);
9481054
9491055 while ((off += class->size) < PAGE_SIZE) {
950− link->next = location_to_obj(page, i++);
1056+ link->next = freeobj++ << OBJ_TAG_BITS;
9511057 link += class->size / sizeof(*link);
9521058 }
9531059
@@ -957,87 +1063,112 @@ static void init_zspage(struct size_class *class, struct page *first_page) · 3 unmodified lines
9571063 * page (if present)
9581064 */
9591065 next_page = get_next_page(page);
960− link->next = location_to_obj(next_page, 0);
1066+ if (next_page) {
1067+ link->next = freeobj++ << OBJ_TAG_BITS;
1068+ } else {
1069+ /*
1070+ * Reset OBJ_TAG_BITS bit to last link to tell
1071+ * whether it's allocated object or not.
1072+ */
1073+ link->next = -1 << OBJ_TAG_BITS;
1074+ }
9611075 kunmap_atomic(vaddr);
9621076 page = next_page;
9631077 off %= PAGE_SIZE;
9641078 }
1079+
1080+ set_freeobj(zspage, 0);
9651081 }
9661082
967−/*
968− * Allocate a zspage for the given size class
969− */
970−static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
1083+static void create_page_chain(struct size_class *class, struct zspage *zspage,
1084+ struct page *pages[])
9711085 {
972− int i, error;
973− struct page *first_page = NULL, *uninitialized_var(prev_page);
1086+ int i;
1087+ struct page *page;
1088+ struct page *prev_page = NULL;
1089+ int nr_pages = class->pages_per_zspage;
9741090
9751091 /*
9761092 * Allocate individual pages and link them together as:
977− * 1. first page->private = first sub-page
978− * 2. all sub-pages are linked together using page->lru
979− * 3. each sub-page is linked to the first page using page->private
1093+ * 1. all pages are linked together using page->freelist
1094+ * 2. each sub-page point to zspage using page->private
9801095 *
981− * For each size class, First/Head pages are linked together using
982− * page->lru. Also, we set PG_private to identify the first page
983− * (i.e. no other sub-page has this flag set) and PG_private_2 to
984− * identify the last page.
1096+ * we set PG_private to identify the first page (i.e. no other sub-page
1097+ * has this flag set) and PG_private_2 to identify the last page.
9851098 */
986− error = -ENOMEM;
987− for (i = 0; i < class->pages_per_zspage; i++) {
988− struct page *page;
989−
990− page = alloc_page(flags);
991− if (!page)
992− goto cleanup;
993−
994− INIT_LIST_HEAD(&page->lru);
995− if (i == 0) { /* first page */
1099+ for (i = 0; i < nr_pages; i++) {
1100+ page = pages[i];
1101+ set_page_private(page, (unsigned long)zspage);
1102+ page->freelist = NULL;
1103+ if (i == 0) {
1104+ zspage->first_page = page;
9961105 SetPagePrivate(page);
997− set_page_private(page, 0);
998− first_page = page;
999− first_page->inuse = 0;
1106+ if (unlikely(class->objs_per_zspage == 1 &&
1107+ class->pages_per_zspage == 1))
1108+ SetPageHugeObject(page);
1109+ } else {
1110+ prev_page->freelist = page;
10001111 }
1001− if (i == 1)
1002− set_page_private(first_page, (unsigned long)page);
1003− if (i >= 1)
1004− set_page_private(page, (unsigned long)first_page);
1005− if (i >= 2)
1006− list_add(&page->lru, &prev_page->lru);
1007− if (i == class->pages_per_zspage - 1) /* last page */
1112+ if (i == nr_pages - 1)
10081113 SetPagePrivate2(page);
10091114 prev_page = page;
10101115 }
1116+}
1117+
1118+/*
1119+ * Allocate a zspage for the given size class
1120+ */
1121+static struct zspage *alloc_zspage(struct zs_pool *pool,
1122+ struct size_class *class,
1123+ gfp_t gfp)
1124+{
1125+ int i;
1126+ struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
1127+ struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1128+
1129+ if (!zspage)
1130+ return NULL;
10111131
1012− init_zspage(class, first_page);
1132+ memset(zspage, 0, sizeof(struct zspage));
1133+ zspage->magic = ZSPAGE_MAGIC;
1134+ migrate_lock_init(zspage);
10131135
1014− first_page->freelist = location_to_obj(first_page, 0);
1015− /* Maximum number of objects we can store in this zspage */
1016− first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
1136+ for (i = 0; i < class->pages_per_zspage; i++) {
1137+ struct page *page;
10171138
1018− error = 0; /* Success */
1139+ page = alloc_page(gfp);
1140+ if (!page) {
1141+ while (--i >= 0) {
1142+ dec_zone_page_state(pages[i], NR_ZSPAGES);
1143+ __free_page(pages[i]);
1144+ }
1145+ cache_free_zspage(pool, zspage);
1146+ return NULL;
1147+ }
10191148
1020−cleanup:
1021− if (unlikely(error) && first_page) {
1022− free_zspage(first_page);
1023− first_page = NULL;
1149+ inc_zone_page_state(page, NR_ZSPAGES);
1150+ pages[i] = page;
10241151 }
10251152
1026− return first_page;
1153+ create_page_chain(class, zspage, pages);
1154+ init_zspage(class, zspage);
1155+
1156+ return zspage;
10271157 }
10281158
1029−static struct page *find_get_zspage(struct size_class *class)
1159+static struct zspage *find_get_zspage(struct size_class *class)
10301160 {
10311161 int i;
1032− struct page *page;
1162+ struct zspage *zspage;
10331163
1034− for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
1035− page = class->fullness_list[i];
1036− if (page)
1164+ for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
1165+ zspage = list_first_entry_or_null(&class->fullness_list[i],
1166+ struct zspage, list);
1167+ if (zspage)
10371168 break;
10381169 }
10391170
1040− return page;
1171+ return zspage;
10411172 }
10421173
10431174 #ifdef CONFIG_PGTABLE_MAPPING
@@ -1242,11 +1373,9 @@ static bool can_merge(struct size_class *prev, int size, int pages_per_zspage) · 198 unmodified lines
12421373 return true;
12431374 }
12441375
1245−static bool zspage_full(struct page *first_page)
1376+static bool zspage_full(struct size_class *class, struct zspage *zspage)
12461377 {
1247− VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
1248−
1249− return first_page->inuse == first_page->objects;
1378+ return get_zspage_inuse(zspage) == class->objs_per_zspage;
12501379 }
12511380
12521381 unsigned long zs_get_total_pages(struct zs_pool *pool)
@@ -1272,8 +1401,10 @@ EXPORT_SYMBOL_GPL(zs_get_total_pages); · 19 unmodified lines
12721401 void *zs_map_object(struct zs_pool *pool, unsigned long handle,
12731402 enum zs_mapmode mm)
12741403 {
1404+ struct zspage *zspage;
12751405 struct page *page;
1276− unsigned long obj, obj_idx, off;
1406+ unsigned long obj, off;
1407+ unsigned int obj_idx;
12771408
12781409 unsigned int class_idx;
12791410 enum fullness_group fg;
@@ -1294,9 +1425,14 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle, · 14 unmodified lines
12941425
12951426 obj = handle_to_obj(handle);
12961427 obj_to_location(obj, &page, &obj_idx);
1297− get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1428+ zspage = get_zspage(page);
1429+
1430+ /* migration cannot move any subpage in this zspage */
1431+ migrate_read_lock(zspage);
1432+
1433+ get_zspage_mapping(zspage, &class_idx, &fg);
12981434 class = pool->size_class[class_idx];
1299− off = obj_idx_to_offset(page, obj_idx, class->size);
1435+ off = (class->size * obj_idx) & ~PAGE_MASK;
13001436
13011437 area = &get_cpu_var(zs_map_area);
13021438 area->vm_mm = mm;
@@ -1314,7 +1450,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle, · 11 unmodified lines
13141450
13151451 ret = __zs_map_object(area, pages, off, class->size);
13161452 out:
1317− if (!class->huge)
1453+ if (likely(!PageHugeObject(page)))
13181454 ret += ZS_HANDLE_SIZE;
13191455
13201456 return ret;
@@ -1323,8 +1459,10 @@ EXPORT_SYMBOL_GPL(zs_map_object); · 2 unmodified lines
13231459
13241460 void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
13251461 {
1462+ struct zspage *zspage;
13261463 struct page *page;
1327− unsigned long obj, obj_idx, off;
1464+ unsigned long obj, off;
1465+ unsigned int obj_idx;
13281466
13291467 unsigned int class_idx;
13301468 enum fullness_group fg;
@@ -1333,9 +1471,10 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle) · 2 unmodified lines
13331471
13341472 obj = handle_to_obj(handle);
13351473 obj_to_location(obj, &page, &obj_idx);
1336− get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1474+ zspage = get_zspage(page);
1475+ get_zspage_mapping(zspage, &class_idx, &fg);
13371476 class = pool->size_class[class_idx];
1338− off = obj_idx_to_offset(page, obj_idx, class->size);
1477+ off = (class->size * obj_idx) & ~PAGE_MASK;
13391478
13401479 area = this_cpu_ptr(&zs_map_area);
13411480 if (off + class->size <= PAGE_SIZE)
@@ -1350,38 +1489,50 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle) · 8 unmodified lines
13501489 __zs_unmap_object(area, pages, off, class->size);
13511490 }
13521491 put_cpu_var(zs_map_area);
1492+
1493+ migrate_read_unlock(zspage);
13531494 unpin_tag(handle);
13541495 }
13551496 EXPORT_SYMBOL_GPL(zs_unmap_object);
13561497
13571498 static unsigned long obj_malloc(struct size_class *class,
1358− struct page *first_page, unsigned long handle)
1499+ struct zspage *zspage, unsigned long handle)
13591500 {
1501+ int i, nr_page, offset;
13601502 unsigned long obj;
13611503 struct link_free *link;
13621504
13631505 struct page *m_page;
1364− unsigned long m_objidx, m_offset;
1506+ unsigned long m_offset;
13651507 void *vaddr;
13661508
13671509 handle |= OBJ_ALLOCATED_TAG;
1368− obj = (unsigned long)first_page->freelist;
1369− obj_to_location(obj, &m_page, &m_objidx);
1370− m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
1510+ obj = get_freeobj(zspage);
1511+
1512+ offset = obj * class->size;
1513+ nr_page = offset >> PAGE_SHIFT;
1514+ m_offset = offset & ~PAGE_MASK;
1515+ m_page = get_first_page(zspage);
1516+
1517+ for (i = 0; i < nr_page; i++)
1518+ m_page = get_next_page(m_page);
13711519
13721520 vaddr = kmap_atomic(m_page);
13731521 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
1374− first_page->freelist = link->next;
1375− if (!class->huge)
1522+ set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
1523+ if (likely(!PageHugeObject(m_page)))
13761524 /* record handle in the header of allocated chunk */
13771525 link->handle = handle;
13781526 else
1379− /* record handle in first_page->private */
1380− set_page_private(first_page, handle);
1527+ /* record handle to page->index */
1528+ zspage->first_page->index = handle;
1529+
13811530 kunmap_atomic(vaddr);
1382− first_page->inuse++;
1531+ mod_zspage_inuse(zspage, 1);
13831532 zs_stat_inc(class, OBJ_USED, 1);
13841533
1534+ obj = location_to_obj(m_page, obj);
1535+
13851536 return obj;
13861537 }
13871538
@@ -1399,12 +1550,13 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) · 11 unmodified lines
13991550 {
14001551 unsigned long handle, obj;
14011552 struct size_class *class;
1402− struct page *first_page;
1553+ enum fullness_group newfg;
1554+ struct zspage *zspage;
14031555
14041556 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
14051557 return 0;
14061558
1407− handle = alloc_handle(pool, gfp);
1559+ handle = cache_alloc_handle(pool, gfp);
14081560 if (!handle)
14091561 return 0;
14101562
@@ -1413,29 +1565,38 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) · 2 unmodified lines
14131565 class = pool->size_class[get_size_class_index(size)];
14141566
14151567 spin_lock(&class->lock);
1416− first_page = find_get_zspage(class);
1417−
1418− if (!first_page) {
1568+ zspage = find_get_zspage(class);
1569+ if (likely(zspage)) {
1570+ obj = obj_malloc(class, zspage, handle);
1571+ /* Now move the zspage to another fullness group, if required */
1572+ fix_fullness_group(class, zspage);
1573+ record_obj(handle, obj);
14191574 spin_unlock(&class->lock);
1420− first_page = alloc_zspage(class, gfp);
1421− if (unlikely(!first_page)) {
1422− free_handle(pool, handle);
1423− return 0;
1424− }
14251575
1426− set_zspage_mapping(first_page, class->index, ZS_EMPTY);
1427− atomic_long_add(class->pages_per_zspage,
1428− &pool->pages_allocated);
1576+ return handle;
1577+ }
14291578
1430− spin_lock(&class->lock);
1431− zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1432− class->size, class->pages_per_zspage));
1579+ spin_unlock(&class->lock);
1580+
1581+ zspage = alloc_zspage(pool, class, gfp);
1582+ if (!zspage) {
1583+ cache_free_handle(pool, handle);
1584+ return 0;
14331585 }
14341586
1435− obj = obj_malloc(class, first_page, handle);
1436− /* Now move the zspage to another fullness group, if required */
1437− fix_fullness_group(class, first_page);
1587+ spin_lock(&class->lock);
1588+ obj = obj_malloc(class, zspage, handle);
1589+ newfg = get_fullness_group(class, zspage);
1590+ insert_zspage(class, zspage, newfg);
1591+ set_zspage_mapping(zspage, class->index, newfg);
14381592 record_obj(handle, obj);
1593+ atomic_long_add(class->pages_per_zspage,
1594+ &pool->pages_allocated);
1595+ zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1596+ class->size, class->pages_per_zspage));
1597+
1598+ /* We completely set up zspage so mark them as movable */
1599+ SetZsPageMovable(pool, zspage);
14391600 spin_unlock(&class->lock);
14401601
14411602 return handle;
@@ -1445,36 +1606,38 @@ EXPORT_SYMBOL_GPL(zs_malloc); · 3 unmodified lines
14451606 static void obj_free(struct size_class *class, unsigned long obj)
14461607 {
14471608 struct link_free *link;
1448− struct page *first_page, *f_page;
1449− unsigned long f_objidx, f_offset;
1609+ struct zspage *zspage;
1610+ struct page *f_page;
1611+ unsigned long f_offset;
1612+ unsigned int f_objidx;
14501613 void *vaddr;
14511614
14521615 obj &= ~OBJ_ALLOCATED_TAG;
14531616 obj_to_location(obj, &f_page, &f_objidx);
1454− first_page = get_first_page(f_page);
1455−
1456− f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
1617+ f_offset = (class->size * f_objidx) & ~PAGE_MASK;
1618+ zspage = get_zspage(f_page);
14571619
14581620 vaddr = kmap_atomic(f_page);
14591621
14601622 /* Insert this object in containing zspage's freelist */
14611623 link = (struct link_free *)(vaddr + f_offset);
1462− link->next = first_page->freelist;
1463− if (class->huge)
1464− set_page_private(first_page, 0);
1624+ link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
14651625 kunmap_atomic(vaddr);
1466− first_page->freelist = (void *)obj;
1467− first_page->inuse--;
1626+ set_freeobj(zspage, f_objidx);
1627+ mod_zspage_inuse(zspage, -1);
14681628 zs_stat_dec(class, OBJ_USED, 1);
14691629 }
14701630
14711631 void zs_free(struct zs_pool *pool, unsigned long handle)
14721632 {
1473− struct page *first_page, *f_page;
1474− unsigned long obj, f_objidx;
1633+ struct zspage *zspage;
1634+ struct page *f_page;
1635+ unsigned long obj;
1636+ unsigned int f_objidx;
14751637 int class_idx;
14761638 struct size_class *class;
14771639 enum fullness_group fullness;
1640+ bool isolated;
14781641
14791642 if (unlikely(!handle))
14801643 return;
@@ -1482,25 +1645,31 @@ void zs_free(struct zs_pool *pool, unsigned long handle) · 1 unmodified line
14821645 pin_tag(handle);
14831646 obj = handle_to_obj(handle);
14841647 obj_to_location(obj, &f_page, &f_objidx);
1485− first_page = get_first_page(f_page);
1648+ zspage = get_zspage(f_page);
14861649
1487− get_zspage_mapping(first_page, &class_idx, &fullness);
1650+ migrate_read_lock(zspage);
1651+
1652+ get_zspage_mapping(zspage, &class_idx, &fullness);
14881653 class = pool->size_class[class_idx];
14891654
14901655 spin_lock(&class->lock);
14911656 obj_free(class, obj);
1492− fullness = fix_fullness_group(class, first_page);
1493− if (fullness == ZS_EMPTY) {
1494− zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1495− class->size, class->pages_per_zspage));
1496− atomic_long_sub(class->pages_per_zspage,
1497− &pool->pages_allocated);
1498− free_zspage(first_page);
1657+ fullness = fix_fullness_group(class, zspage);
1658+ if (fullness != ZS_EMPTY) {
1659+ migrate_read_unlock(zspage);
1660+ goto out;
14991661 }
1662+
1663+ isolated = is_zspage_isolated(zspage);
1664+ migrate_read_unlock(zspage);
1665+ /* If zspage is isolated, zs_page_putback will free the zspage */
1666+ if (likely(!isolated))
1667+ free_zspage(pool, class, zspage);
1668+out:
1669+
15001670 spin_unlock(&class->lock);
15011671 unpin_tag(handle);
1502−
1503− free_handle(pool, handle);
1672+ cache_free_handle(pool, handle);
15041673 }
15051674 EXPORT_SYMBOL_GPL(zs_free);
15061675
@@ -1508,7 +1677,7 @@ static void zs_object_copy(struct size_class *class, unsigned long dst, · 1 unmodified line
15081677 unsigned long src)
15091678 {
15101679 struct page *s_page, *d_page;
1511− unsigned long s_objidx, d_objidx;
1680+ unsigned int s_objidx, d_objidx;
15121681 unsigned long s_off, d_off;
15131682 void *s_addr, *d_addr;
15141683 int s_size, d_size, size;
@@ -1519,8 +1688,8 @@ static void zs_object_copy(struct size_class *class, unsigned long dst, · 4 unmodified lines
15191688 obj_to_location(src, &s_page, &s_objidx);
15201689 obj_to_location(dst, &d_page, &d_objidx);
15211690
1522− s_off = obj_idx_to_offset(s_page, s_objidx, class->size);
1523− d_off = obj_idx_to_offset(d_page, d_objidx, class->size);
1691+ s_off = (class->size * s_objidx) & ~PAGE_MASK;
1692+ d_off = (class->size * d_objidx) & ~PAGE_MASK;
15241693
15251694 if (s_off + class->size > PAGE_SIZE)
15261695 s_size = PAGE_SIZE - s_off;
@@ -1579,12 +1748,11 @@ static unsigned long find_alloced_obj(struct size_class *class, · 52 unmodified lines
15791748 unsigned long handle = 0;
15801749 void *addr = kmap_atomic(page);
15811750
1582− if (!is_first_page(page))
1583− offset = page->index;
1751+ offset = get_first_obj_offset(page);
15841752 offset += class->size * index;
15851753
15861754 while (offset < PAGE_SIZE) {
1587− head = obj_to_head(class, page, addr + offset);
1755+ head = obj_to_head(page, addr + offset);
15881756 if (head & OBJ_ALLOCATED_TAG) {
15891757 handle = head & ~OBJ_ALLOCATED_TAG;
15901758 if (trypin_tag(handle))
@@ -1601,7 +1769,7 @@ static unsigned long find_alloced_obj(struct size_class *class, · 10 unmodified lines
16011769 }
16021770
16031771 struct zs_compact_control {
1604− /* Source page for migration which could be a subpage of zspage. */
1772+ /* Source spage for migration which could be a subpage of zspage */
16051773 struct page *s_page;
16061774 /* Destination page for migration which should be a first page
16071775 * of zspage. */
@@ -1632,14 +1800,14 @@ static int migrate_zspage(struct zs_pool *pool, struct size_class *class, · 24 unmodified lines
16321800 }
16331801
16341802 /* Stop if there is no more space */
1635− if (zspage_full(d_page)) {
1803+ if (zspage_full(class, get_zspage(d_page))) {
16361804 unpin_tag(handle);
16371805 ret = -ENOMEM;
16381806 break;
16391807 }
16401808
16411809 used_obj = handle_to_obj(handle);
1642− free_obj = obj_malloc(class, d_page, handle);
1810+ free_obj = obj_malloc(class, get_zspage(d_page), handle);
16431811 zs_object_copy(class, free_obj, used_obj);
16441812 index++;
16451813 /*
@@ -1661,68 +1829,422 @@ static int migrate_zspage(struct zs_pool *pool, struct size_class *class, · 15 unmodified lines
16611829 return ret;
16621830 }
16631831
1664−static struct page *isolate_target_page(struct size_class *class)
1832+static struct zspage *isolate_zspage(struct size_class *class, bool source)
16651833 {
16661834 int i;
1667− struct page *page;
1835+ struct zspage *zspage;
1836+ enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
16681837
1669− for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
1670− page = class->fullness_list[i];
1671− if (page) {
1672− remove_zspage(class, i, page);
1673− break;
1838+ if (!source) {
1839+ fg[0] = ZS_ALMOST_FULL;
1840+ fg[1] = ZS_ALMOST_EMPTY;
1841+ }
1842+
1843+ for (i = 0; i < 2; i++) {
1844+ zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1845+ struct zspage, list);
1846+ if (zspage) {
1847+ VM_BUG_ON(is_zspage_isolated(zspage));
1848+ remove_zspage(class, zspage, fg[i]);
1849+ return zspage;
16741850 }
16751851 }
16761852
1677− return page;
1853+ return zspage;
16781854 }
16791855
16801856 /*
1681− * putback_zspage - add @first_page into right class's fullness list
1682− * @pool: target pool
1857+ * putback_zspage - add @zspage into right class's fullness list
16831858 * @class: destination class
1684− * @first_page: target page
1859+ * @zspage: target page
16851860 *
1686− * Return @fist_page's fullness_group
1861+ * Return @zspage's fullness_group
16871862 */
1688−static enum fullness_group putback_zspage(struct zs_pool *pool,
1689− struct size_class *class,
1690− struct page *first_page)
1863+static enum fullness_group putback_zspage(struct size_class *class,
1864+ struct zspage *zspage)
16911865 {
16921866 enum fullness_group fullness;
16931867
1694− fullness = get_fullness_group(first_page);
1695− insert_zspage(class, fullness, first_page);
1696− set_zspage_mapping(first_page, class->index, fullness);
1868+ VM_BUG_ON(is_zspage_isolated(zspage));
16971869
1698− if (fullness == ZS_EMPTY) {
1699− zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1700− class->size, class->pages_per_zspage));
1701− atomic_long_sub(class->pages_per_zspage,
1702− &pool->pages_allocated);
1870+ fullness = get_fullness_group(class, zspage);
1871+ insert_zspage(class, zspage, fullness);
1872+ set_zspage_mapping(zspage, class->index, fullness);
1873+
1874+ return fullness;
1875+}
1876+
1877+#ifdef CONFIG_COMPACTION
1878+static struct dentry *zs_mount(struct file_system_type *fs_type,
1879+ int flags, const char *dev_name, void *data)
1880+{
1881+ static const struct dentry_operations ops = {
1882+ .d_dname = simple_dname,
1883+ };
1884+
1885+ return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1886+}
1887+
1888+static struct file_system_type zsmalloc_fs = {
1889+ .name = "zsmalloc",
1890+ .mount = zs_mount,
1891+ .kill_sb = kill_anon_super,
1892+};
1893+
1894+static int zsmalloc_mount(void)
1895+{
1896+ int ret = 0;
17031897
1704− free_zspage(first_page);
1898+ zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1899+ if (IS_ERR(zsmalloc_mnt))
1900+ ret = PTR_ERR(zsmalloc_mnt);
1901+
1902+ return ret;
1903+}
1904+
1905+static void zsmalloc_unmount(void)
1906+{
1907+ kern_unmount(zsmalloc_mnt);
1908+}
1909+
1910+static void migrate_lock_init(struct zspage *zspage)
1911+{
1912+ rwlock_init(&zspage->lock);
1913+}
1914+
1915+static void migrate_read_lock(struct zspage *zspage)
1916+{
1917+ read_lock(&zspage->lock);
1918+}
1919+
1920+static void migrate_read_unlock(struct zspage *zspage)
1921+{
1922+ read_unlock(&zspage->lock);
1923+}
1924+
1925+static void migrate_write_lock(struct zspage *zspage)
1926+{
1927+ write_lock(&zspage->lock);
1928+}
1929+
1930+static void migrate_write_unlock(struct zspage *zspage)
1931+{
1932+ write_unlock(&zspage->lock);
1933+}
1934+
1935+/* Number of isolated subpage for *page migration* in this zspage */
1936+static void inc_zspage_isolation(struct zspage *zspage)
1937+{
1938+ zspage->isolated++;
1939+}
1940+
1941+static void dec_zspage_isolation(struct zspage *zspage)
1942+{
1943+ zspage->isolated--;
1944+}
1945+
1946+static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1947+ struct page *newpage, struct page *oldpage)
1948+{
1949+ struct page *page;
1950+ struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1951+ int idx = 0;
1952+
1953+ page = get_first_page(zspage);
1954+ do {
1955+ if (page == oldpage)
1956+ pages[idx] = newpage;
1957+ else
1958+ pages[idx] = page;
1959+ idx++;
1960+ } while ((page = get_next_page(page)) != NULL);
1961+
1962+ create_page_chain(class, zspage, pages);
1963+ set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1964+ if (unlikely(PageHugeObject(oldpage)))
1965+ newpage->index = oldpage->index;
1966+ __SetPageMovable(newpage, page_mapping(oldpage));
1967+}
1968+
1969+bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1970+{
1971+ struct zs_pool *pool;
1972+ struct size_class *class;
1973+ int class_idx;
1974+ enum fullness_group fullness;
1975+ struct zspage *zspage;
1976+ struct address_space *mapping;
1977+
1978+ /*
1979+ * Page is locked so zspage couldn't be destroyed. For detail, look at
1980+ * lock_zspage in free_zspage.
1981+ */
1982+ VM_BUG_ON_PAGE(!PageMovable(page), page);
1983+ VM_BUG_ON_PAGE(PageIsolated(page), page);
1984+
1985+ zspage = get_zspage(page);
1986+
1987+ /*
1988+ * Without class lock, fullness could be stale while class_idx is okay
1989+ * because class_idx is constant unless page is freed so we should get
1990+ * fullness again under class lock.
1991+ */
1992+ get_zspage_mapping(zspage, &class_idx, &fullness);
1993+ mapping = page_mapping(page);
1994+ pool = mapping->private_data;
1995+ class = pool->size_class[class_idx];
1996+
1997+ spin_lock(&class->lock);
1998+ if (get_zspage_inuse(zspage) == 0) {
1999+ spin_unlock(&class->lock);
2000+ return false;
17052001 }
17062002
1707− return fullness;
2003+ /* zspage is isolated for object migration */
2004+ if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2005+ spin_unlock(&class->lock);
2006+ return false;
2007+ }
2008+
2009+ /*
2010+ * If this is first time isolation for the zspage, isolate zspage from
2011+ * size_class to prevent further object allocation from the zspage.
2012+ */
2013+ if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2014+ get_zspage_mapping(zspage, &class_idx, &fullness);
2015+ remove_zspage(class, zspage, fullness);
2016+ }
2017+
2018+ inc_zspage_isolation(zspage);
2019+ spin_unlock(&class->lock);
2020+
2021+ return true;
17082022 }
17092023
1710−static struct page *isolate_source_page(struct size_class *class)
2024+int zs_page_migrate(struct address_space *mapping, struct page *newpage,
2025+ struct page *page, enum migrate_mode mode)
2026+{
2027+ struct zs_pool *pool;
2028+ struct size_class *class;
2029+ int class_idx;
2030+ enum fullness_group fullness;
2031+ struct zspage *zspage;
2032+ struct page *dummy;
2033+ void *s_addr, *d_addr, *addr;
2034+ int offset, pos;
2035+ unsigned long handle, head;
2036+ unsigned long old_obj, new_obj;
2037+ unsigned int obj_idx;
2038+ int ret = -EAGAIN;
2039+
2040+ VM_BUG_ON_PAGE(!PageMovable(page), page);
2041+ VM_BUG_ON_PAGE(!PageIsolated(page), page);
2042+
2043+ zspage = get_zspage(page);
2044+
2045+ /* Concurrent compactor cannot migrate any subpage in zspage */
2046+ migrate_write_lock(zspage);
2047+ get_zspage_mapping(zspage, &class_idx, &fullness);
2048+ pool = mapping->private_data;
2049+ class = pool->size_class[class_idx];
2050+ offset = get_first_obj_offset(page);
2051+
2052+ spin_lock(&class->lock);
2053+ if (!get_zspage_inuse(zspage)) {
2054+ ret = -EBUSY;
2055+ goto unlock_class;
2056+ }
2057+
2058+ pos = offset;
2059+ s_addr = kmap_atomic(page);
2060+ while (pos < PAGE_SIZE) {
2061+ head = obj_to_head(page, s_addr + pos);
2062+ if (head & OBJ_ALLOCATED_TAG) {
2063+ handle = head & ~OBJ_ALLOCATED_TAG;
2064+ if (!trypin_tag(handle))
2065+ goto unpin_objects;
2066+ }
2067+ pos += class->size;
2068+ }
2069+
2070+ /*
2071+ * Here, any user cannot access all objects in the zspage so let's move.
2072+ */
2073+ d_addr = kmap_atomic(newpage);
2074+ memcpy(d_addr, s_addr, PAGE_SIZE);
2075+ kunmap_atomic(d_addr);
2076+
2077+ for (addr = s_addr + offset; addr < s_addr + pos;
2078+ addr += class->size) {
2079+ head = obj_to_head(page, addr);
2080+ if (head & OBJ_ALLOCATED_TAG) {
2081+ handle = head & ~OBJ_ALLOCATED_TAG;
2082+ if (!testpin_tag(handle))
2083+ BUG();
2084+
2085+ old_obj = handle_to_obj(handle);
2086+ obj_to_location(old_obj, &dummy, &obj_idx);
2087+ new_obj = (unsigned long)location_to_obj(newpage,
2088+ obj_idx);
2089+ new_obj |= BIT(HANDLE_PIN_BIT);
2090+ record_obj(handle, new_obj);
2091+ }
2092+ }
2093+
2094+ replace_sub_page(class, zspage, newpage, page);
2095+ get_page(newpage);
2096+
2097+ dec_zspage_isolation(zspage);
2098+
2099+ /*
2100+ * Page migration is done so let's putback isolated zspage to
2101+ * the list if @page is final isolated subpage in the zspage.
2102+ */
2103+ if (!is_zspage_isolated(zspage))
2104+ putback_zspage(class, zspage);
2105+
2106+ reset_page(page);
2107+ put_page(page);
2108+ page = newpage;
2109+
2110+ ret = MIGRATEPAGE_SUCCESS;
2111+unpin_objects:
2112+ for (addr = s_addr + offset; addr < s_addr + pos;
2113+ addr += class->size) {
2114+ head = obj_to_head(page, addr);
2115+ if (head & OBJ_ALLOCATED_TAG) {
2116+ handle = head & ~OBJ_ALLOCATED_TAG;
2117+ if (!testpin_tag(handle))
2118+ BUG();
2119+ unpin_tag(handle);
2120+ }
2121+ }
2122+ kunmap_atomic(s_addr);
2123+unlock_class:
2124+ spin_unlock(&class->lock);
2125+ migrate_write_unlock(zspage);
2126+
2127+ return ret;
2128+}
2129+
2130+void zs_page_putback(struct page *page)
2131+{
2132+ struct zs_pool *pool;
2133+ struct size_class *class;
2134+ int class_idx;
2135+ enum fullness_group fg;
2136+ struct address_space *mapping;
2137+ struct zspage *zspage;
2138+
2139+ VM_BUG_ON_PAGE(!PageMovable(page), page);
2140+ VM_BUG_ON_PAGE(!PageIsolated(page), page);
2141+
2142+ zspage = get_zspage(page);
2143+ get_zspage_mapping(zspage, &class_idx, &fg);
2144+ mapping = page_mapping(page);
2145+ pool = mapping->private_data;
2146+ class = pool->size_class[class_idx];
2147+
2148+ spin_lock(&class->lock);
2149+ dec_zspage_isolation(zspage);
2150+ if (!is_zspage_isolated(zspage)) {
2151+ fg = putback_zspage(class, zspage);
2152+ /*
2153+ * Due to page_lock, we cannot free zspage immediately
2154+ * so let's defer.
2155+ */
2156+ if (fg == ZS_EMPTY)
2157+ schedule_work(&pool->free_work);
2158+ }
2159+ spin_unlock(&class->lock);
2160+}
2161+
2162+const struct address_space_operations zsmalloc_aops = {
2163+ .isolate_page = zs_page_isolate,
2164+ .migratepage = zs_page_migrate,
2165+ .putback_page = zs_page_putback,
2166+};
2167+
2168+static int zs_register_migration(struct zs_pool *pool)
2169+{
2170+ pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2171+ if (IS_ERR(pool->inode)) {
2172+ pool->inode = NULL;
2173+ return 1;
2174+ }
2175+
2176+ pool->inode->i_mapping->private_data = pool;
2177+ pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2178+ return 0;
2179+}
2180+
2181+static void zs_unregister_migration(struct zs_pool *pool)
2182+{
2183+ flush_work(&pool->free_work);
2184+ if (pool->inode)
2185+ iput(pool->inode);
2186+}
2187+
2188+/*
2189+ * Caller should hold page_lock of all pages in the zspage
2190+ * In here, we cannot use zspage meta data.
2191+ */
2192+static void async_free_zspage(struct work_struct *work)
17112193 {
17122194 int i;
1713− struct page *page = NULL;
2195+ struct size_class *class;
2196+ unsigned int class_idx;
2197+ enum fullness_group fullness;
2198+ struct zspage *zspage, *tmp;
2199+ LIST_HEAD(free_pages);
2200+ struct zs_pool *pool = container_of(work, struct zs_pool,
2201+ free_work);
17142202
1715− for (i = ZS_ALMOST_EMPTY; i >= ZS_ALMOST_FULL; i--) {
1716− page = class->fullness_list[i];
1717− if (!page)
2203+ for (i = 0; i < zs_size_classes; i++) {
2204+ class = pool->size_class[i];
2205+ if (class->index != i)
17182206 continue;
17192207
1720− remove_zspage(class, i, page);
1721− break;
2208+ spin_lock(&class->lock);
2209+ list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2210+ spin_unlock(&class->lock);
2211+ }
2212+
2213+
2214+ list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2215+ list_del(&zspage->list);
2216+ lock_zspage(zspage);
2217+
2218+ get_zspage_mapping(zspage, &class_idx, &fullness);
2219+ VM_BUG_ON(fullness != ZS_EMPTY);
2220+ class = pool->size_class[class_idx];
2221+ spin_lock(&class->lock);
2222+ __free_zspage(pool, pool->size_class[class_idx], zspage);
2223+ spin_unlock(&class->lock);
17222224 }
2225+};
2226+
2227+static void kick_deferred_free(struct zs_pool *pool)
2228+{
2229+ schedule_work(&pool->free_work);
2230+}
2231+
2232+static void init_deferred_free(struct zs_pool *pool)
2233+{
2234+ INIT_WORK(&pool->free_work, async_free_zspage);
2235+}
2236+
2237+static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2238+{
2239+ struct page *page = get_first_page(zspage);
17232240
1724− return page;
2241+ do {
2242+ WARN_ON(!trylock_page(page));
2243+ __SetPageMovable(page, pool->inode->i_mapping);
2244+ unlock_page(page);
2245+ } while ((page = get_next_page(page)) != NULL);
17252246 }
2247+#endif
17262248
17272249 /*
17282250 *
@@ -1748,20 +2270,20 @@ static unsigned long zs_can_compact(struct size_class *class) · 19 unmodified lines
17482270 static void __zs_compact(struct zs_pool *pool, struct size_class *class)
17492271 {
17502272 struct zs_compact_control cc;
1751− struct page *src_page;
1752− struct page *dst_page = NULL;
2273+ struct zspage *src_zspage;
2274+ struct zspage *dst_zspage = NULL;
17532275
17542276 spin_lock(&class->lock);
1755− while ((src_page = isolate_source_page(class))) {
2277+ while ((src_zspage = isolate_zspage(class, true))) {
17562278
17572279 if (!zs_can_compact(class))
17582280 break;
17592281
17602282 cc.index = 0;
1761− cc.s_page = src_page;
2283+ cc.s_page = get_first_page(src_zspage);
17622284
1763− while ((dst_page = isolate_target_page(class))) {
1764− cc.d_page = dst_page;
2285+ while ((dst_zspage = isolate_zspage(class, false))) {
2286+ cc.d_page = get_first_page(dst_zspage);
17652287 /*
17662288 * If there is no more space in dst_page, resched
17672289 * and see if anyone had allocated another zspage.
@@ -1769,23 +2291,25 @@ static void __zs_compact(struct zs_pool *pool, struct size_class *class) · 1 unmodified line
17692291 if (!migrate_zspage(pool, class, &cc))
17702292 break;
17712293
1772− putback_zspage(pool, class, dst_page);
2294+ putback_zspage(class, dst_zspage);
17732295 }
17742296
17752297 /* Stop if we couldn't find slot */
1776− if (dst_page == NULL)
2298+ if (dst_zspage == NULL)
17772299 break;
17782300
1779− putback_zspage(pool, class, dst_page);
1780− if (putback_zspage(pool, class, src_page) == ZS_EMPTY)
2301+ putback_zspage(class, dst_zspage);
2302+ if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
2303+ free_zspage(pool, class, src_zspage);
17812304 pool->stats.pages_compacted += class->pages_per_zspage;
2305+ }
17822306 spin_unlock(&class->lock);
17832307 cond_resched();
17842308 spin_lock(&class->lock);
17852309 }
17862310
1787− if (src_page)
1788− putback_zspage(pool, class, src_page);
2311+ if (src_zspage)
2312+ putback_zspage(class, src_zspage);
17892313
17902314 spin_unlock(&class->lock);
17912315 }
@@ -1892,6 +2416,7 @@ struct zs_pool *zs_create_pool(const char *name) · 100 unmodified lines
18922416 if (!pool)
18932417 return NULL;
18942418
2419+ init_deferred_free(pool);
18952420 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
18962421 GFP_KERNEL);
18972422 if (!pool->size_class) {
@@ -1903,7 +2428,7 @@ struct zs_pool *zs_create_pool(const char *name) · 5 unmodified lines
19032428 if (!pool->name)
19042429 goto err;
19052430
1906− if (create_handle_cache(pool))
2431+ if (create_cache(pool))
19072432 goto err;
19082433
19092434 /*
@@ -1914,6 +2439,7 @@ struct zs_pool *zs_create_pool(const char *name) · 4 unmodified lines
19142439 int size;
19152440 int pages_per_zspage;
19162441 struct size_class *class;
2442+ int fullness = 0;
19172443
19182444 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
19192445 if (size > ZS_MAX_ALLOC_SIZE)
@@ -1943,11 +2469,13 @@ struct zs_pool *zs_create_pool(const char *name) · 23 unmodified lines
19432469 class->size = size;
19442470 class->index = i;
19452471 class->pages_per_zspage = pages_per_zspage;
1946− if (pages_per_zspage == 1 &&
1947− get_maxobj_per_zspage(size, pages_per_zspage) == 1)
1948− class->huge = true;
2472+ class->objs_per_zspage = class->pages_per_zspage *
2473+ PAGE_SIZE / class->size;
19492474 spin_lock_init(&class->lock);
19502475 pool->size_class[i] = class;
2476+ for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2477+ fullness++)
2478+ INIT_LIST_HEAD(&class->fullness_list[fullness]);
19512479
19522480 prev_class = class;
19532481 }
@@ -1955,6 +2483,9 @@ struct zs_pool *zs_create_pool(const char *name) · 1 unmodified line
19552483 /* debug only, don't abort if it fails */
19562484 zs_pool_stat_create(pool, name);
19572485
2486+ if (zs_register_migration(pool))
2487+ goto err;
2488+
19582489 /*
19592490 * Not critical, we still can use the pool
19602491 * and user can trigger compaction manually.
@@ -1974,6 +2505,7 @@ void zs_destroy_pool(struct zs_pool *pool) · 13 unmodified lines
19742505 int i;
19752506
19762507 zs_unregister_shrinker(pool);
2508+ zs_unregister_migration(pool);
19772509 zs_pool_stat_destroy(pool);
19782510
19792511 for (i = 0; i < zs_size_classes; i++) {
@@ -1986,8 +2518,8 @@ void zs_destroy_pool(struct zs_pool *pool) · 6 unmodified lines
19862518 if (class->index != i)
19872519 continue;
19882520
1989− for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
1990− if (class->fullness_list[fg]) {
2521+ for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
2522+ if (!list_empty(&class->fullness_list[fg])) {
19912523 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
19922524 class->size, fg);
19932525 }
@@ -1995,7 +2527,7 @@ void zs_destroy_pool(struct zs_pool *pool) · 1 unmodified line
19952527 kfree(class);
19962528 }
19972529
1998− destroy_handle_cache(pool);
2530+ destroy_cache(pool);
19992531 kfree(pool->size_class);
20002532 kfree(pool->name);
20012533 kfree(pool);
@@ -2004,7 +2536,13 @@ EXPORT_SYMBOL_GPL(zs_destroy_pool); · 2 unmodified lines
20042536
20052537 static int __init zs_init(void)
20062538 {
2007− int ret = zs_register_cpu_notifier();
2539+ int ret;
2540+
2541+ ret = zsmalloc_mount();
2542+ if (ret)
2543+ goto out;
2544+
2545+ ret = zs_register_cpu_notifier();
20082546
20092547 if (ret)
20102548 goto notifier_fail;
@@ -2021,7 +2559,8 @@ static int __init zs_init(void) · 10 unmodified lines
20212559
20222560 notifier_fail:
20232561 zs_unregister_cpu_notifier();
2024−
2562+ zsmalloc_unmount();
2563+out:
20252564 return ret;
20262565 }
20272566
@@ -2030,6 +2569,7 @@ static void __exit zs_exit(void) · 2 unmodified lines
20302569 #ifdef CONFIG_ZPOOL
20312570 zpool_unregister_driver(&zs_zpool_driver);
20322571 #endif
2572+ zsmalloc_unmount();
20332573 zs_unregister_cpu_notifier();
20342574
20352575 zs_stat_exit();
net/unix/af_unix.c+1−0
@@ -769,6 +769,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern) · 768 unmodified lines
769769 lockdep_set_class(&sk->sk_receive_queue.lock,
770770 &af_unix_sk_receive_queue_lock_key);
771771
772+ sk->sk_allocation = GFP_KERNEL_ACCOUNT;
772773 sk->sk_write_space = unix_write_space;
773774 sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
774775 sk->sk_destruct = unix_sock_destructor;
scripts/bloat-o-meter+2−2
@@ -67,5 +67,5 @@ print("%-40s %7s %7s %+7s" % ("function", "old", "new", "delta")) · 66 unmodified lines
6767 for d, n in delta:
6868 if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
6969
70−print("Total: Before=%d, After=%d, chg %f%%" % \
71− (otot, ntot, (ntot - otot)*100/otot))
70+print("Total: Before=%d, After=%d, chg %+.2f%%" % \
71+ (otot, ntot, (ntot - otot)*100.0/otot))
scripts/tags.sh+3−0
@@ -185,6 +185,9 @@ regex_c=( · 184 unmodified lines
185185 '/\<CLEARPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/ClearPage\1/'
186186 '/\<__CLEARPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/__ClearPage\1/'
187187 '/\<TESTCLEARFLAG_FALSE(\([[:alnum:]_]*\).*/TestClearPage\1/'
188+ '/^PAGE_MAPCOUNT_OPS(\([[:alnum:]_]*\).*/Page\1/'
189+ '/^PAGE_MAPCOUNT_OPS(\([[:alnum:]_]*\).*/__SetPage\1/'
190+ '/^PAGE_MAPCOUNT_OPS(\([[:alnum:]_]*\).*/__ClearPage\1/'
188191 '/^TASK_PFA_TEST([^,]*, *\([[:alnum:]_]*\))/task_\1/'
189192 '/^TASK_PFA_SET([^,]*, *\([[:alnum:]_]*\))/task_set_\1/'
190193 '/^TASK_PFA_CLEAR([^,]*, *\([[:alnum:]_]*\))/task_clear_\1/'
tools/vm/page_owner_sort.c+7−2
@@ -79,12 +79,12 @@ static void add_list(char *buf, int len) · 78 unmodified lines
7979 }
8080 }
8181
82−#define BUF_SIZE 1024
82+#define BUF_SIZE (128 * 1024)
8383
8484 int main(int argc, char **argv)
8585 {
8686 FILE *fin, *fout;
87− char buf[BUF_SIZE];
87+ char *buf;
8888 int ret, i, count;
8989 struct block_list *list2;
9090 struct stat st;
@@ -107,6 +107,11 @@ int main(int argc, char **argv) · 16 unmodified lines
107107 max_size = st.st_size / 100; /* hack ... */
108108
109109 list = malloc(max_size * sizeof(*list));
110+ buf = malloc(BUF_SIZE);
111+ if (!list || !buf) {
112+ printf("Out of memory\n");
113+ exit(1);
114+ }
110115
111116 for ( ; ; ) {
112117 ret = read_block(buf, BUF_SIZE, fin);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t