android_kernel_samsung_sm8650/mm
Andrea Arcangeli 25db7c13d8 UPSTREAM: mm/rmap: support move to different root anon_vma in folio_move_anon_rmap()
Patch series "userfaultfd move option", v6.

This patch series introduces UFFDIO_MOVE feature to userfaultfd, which has
long been implemented and maintained by Andrea in his local tree [1], but
was not upstreamed due to lack of use cases where this approach would be
better than allocating a new page and copying the contents.  Previous
upstraming attempts could be found at [6] and [7].

UFFDIO_COPY performs ~20% better than UFFDIO_MOVE when the application
needs pages to be allocated [2].  However, with UFFDIO_MOVE, if pages are
available (in userspace) for recycling, as is usually the case in heap
compaction algorithms, then we can avoid the page allocation and memcpy
(done by UFFDIO_COPY).  Also, since the pages are recycled in the
userspace, we avoid the need to release (via madvise) the pages back to
the kernel [3].  We see over 40% reduction (on a Google pixel 6 device) in
the compacting thread's completion time by using UFFDIO_MOVE vs.
UFFDIO_COPY.  This was measured using a benchmark that emulates a heap
compaction implementation using userfaultfd (to allow concurrent accesses
by application threads).  More details of the usecase are explained in
[3].

Furthermore, UFFDIO_MOVE enables moving swapped-out pages without
touching them within the same vma. Today, it can only be done by mremap,
however it forces splitting the vma.

TODOs for follow-up improvements:
- cross-mm support. Known differences from single-mm and missing pieces:
	- memcg recharging (might need to isolate pages in the process)
	- mm counters
	- cross-mm deposit table moves
	- cross-mm test
	- document the address space where src and dest reside in struct
	  uffdio_move

- TLB flush batching.  Will require extensive changes to PTL locking in
  move_pages_pte().  OTOH that might let us reuse parts of mremap code.

This patch (of 5):

For now, folio_move_anon_rmap() was only used to move a folio to a
different anon_vma after fork(), whereby the root anon_vma stayed
unchanged.  For that, it was sufficient to hold the folio lock when
calling folio_move_anon_rmap().

However, we want to make use of folio_move_anon_rmap() to move folios
between VMAs that have a different root anon_vma.  As folio_referenced()
performs an RMAP walk without holding the folio lock but only holding the
anon_vma in read mode, holding the folio lock is insufficient.

When moving to an anon_vma with a different root anon_vma, we'll have to
hold both, the folio lock and the anon_vma lock in write mode.
Consequently, whenever we succeeded in folio_lock_anon_vma_read() to
read-lock the anon_vma, we have to re-check if the mapping was changed in
the meantime.  If that was the case, we have to retry.

Note that folio_move_anon_rmap() must only be called if the anon page is
exclusive to a process, and must not be called on KSM folios.

This is a preparation for UFFDIO_MOVE, which will hold the folio lock, the
anon_vma lock in write mode, and the mmap_lock in read mode.

Link: https://lkml.kernel.org/r/20231206103702.3873743-1-surenb@google.com
Link: https://lkml.kernel.org/r/20231206103702.3873743-2-surenb@google.com
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Acked-by: Peter Xu <peterx@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Brian Geffon <bgeffon@google.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: kernel-team@android.com
Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
Cc: Lokesh Gidra <lokeshgidra@google.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Nicolas Geoffray <ngeoffray@google.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: ZhangPeng <zhangpeng362@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

(cherry picked from commit 880a99b60d467eefd96322e27b0a8c0b805dfa43)

Bug: 274911254
Change-Id: Iad9619c0273e050af26356f66ae9fc88b56d68bd
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Lokesh Gidra <lokeshgidra@google.com>
2024-04-22 18:09:14 +00:00
..
damon Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
kasan Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
kfence mm,kfence: decouple kfence from page granularity mapping judgement 2023-12-03 07:32:08 +01:00
kmsan UPSTREAM: mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush() 2023-05-31 15:20:12 +00:00
backing-dev.c writeback, cgroup: fix null-ptr-deref write in bdi_split_work_to_wbs 2023-04-26 14:28:39 +02:00
balloon_compaction.c mm: Convert all PageMovable users to movable_operations 2022-08-02 12:34:03 -04:00
bootmem_info.c bootmem: remove the vmemmap pages from kmemleak in put_page_bootmem 2022-08-28 14:02:45 -07:00
cleancache.c ANDROID: Revert "mm: remove cleancache" 2023-04-26 17:01:50 +00:00
cma_debug.c mm/cma_debug: show complete cma name in debugfs directories 2022-09-11 20:25:50 -07:00
cma_sysfs.c
cma.c ANDROID: vendor_hooks: Export cma_alloc trace points 2024-03-08 04:04:18 +00:00
cma.h
compaction.c ANDROID: mm: vh for compaction begin/end 2023-10-17 18:15:01 +00:00
debug_page_ref.c
debug_vm_pgtable.c
debug.c UPSTREAM: mm: export dump_mm() 2023-06-07 14:24:58 +00:00
dmapool.c
early_ioremap.c
fadvise.c UPSTREAM: mm: support POSIX_FADV_NOREUSE 2023-04-12 16:02:15 +00:00
failslab.c mm: fix unexpected changes to {failslab|fail_page_alloc}.attr 2022-11-22 18:50:44 -08:00
filemap.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
folio-compat.c mm: remove try_to_free_swap() 2022-10-03 14:02:53 -07:00
frontswap.c frontswap: don't call ->init if no ops are registered 2022-09-26 12:14:34 -07:00
gup_test.c mm: rename is_pinnable_page() to is_longterm_pinnable_page() 2022-07-17 17:14:27 -07:00
gup_test.h
gup.c BACKPORT: mm: always expand the stack with the mmap write lock held 2023-07-27 11:47:21 +00:00
highmem.c highmem: fix kmap_to_page() for kmap_local_page() addresses 2022-10-12 18:51:51 -07:00
hmm.c BACKPORT: FROMGIT: mm: enable page walking API to lock vmas during the walk 2023-08-16 16:55:02 +00:00
huge_memory.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
hugetlb_cgroup.c hugetlb_cgroup: use helper for_each_hstate and hstate_index 2022-09-11 20:25:53 -07:00
hugetlb_vmemmap.c mm: hugetlb_vmemmap: fix a race between vmemmap pmd split 2023-09-19 12:27:56 +02:00
hugetlb_vmemmap.h mm: hugetlb_vmemmap: improve hugetlb_vmemmap code readability 2022-08-08 18:06:43 -07:00
hugetlb.c This is the 6.1.68 stable release 2024-01-05 08:40:52 +00:00
hwpoison-inject.c mm/hwpoison: add __init/__exit annotations to module init/exit funcs 2022-10-03 14:03:05 -07:00
init-mm.c UPSTREAM: mm: add per-VMA lock and helper functions to control it 2023-06-07 14:24:59 +00:00
internal.h Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
interval_tree.c
io-mapping.c
ioremap.c
Kconfig BACKPORT: mm: introduce new 'lock_mm_and_find_vma()' page fault helper 2023-07-26 09:57:34 +00:00
Kconfig.debug Merge 6.1.34 into android14-6.1-lts 2023-06-14 19:49:28 +00:00
khugepaged.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
kmemleak.c BACKPORT: mm/kmemleak: fix UAF bug in kmemleak_scan() 2023-05-15 19:08:33 +00:00
ksm.c BACKPORT: FROMGIT: mm: enable page walking API to lock vmas during the walk 2023-08-16 16:55:02 +00:00
list_lru.c
maccess.c mm: Fix copy_from_user_nofault(). 2023-06-28 11:12:17 +02:00
madvise.c Merge 6.1.50 into android14-6.1-lts 2023-09-18 09:52:46 +00:00
Makefile ANDROID: mm: introduce page_pinner 2023-05-16 21:34:27 +00:00
mapping_dirty_helpers.c
memblock.c ANDROID: GKI: Export memblock_free to drivers 2023-03-01 21:29:20 +00:00
memcontrol.c FROMGIT: mm: memcg: don't periodically flush stats when memcg is disabled 2024-01-30 20:40:37 +00:00
memfd.c memfd: check for non-NULL file_seals in memfd_create() syscall 2023-06-28 11:12:27 +02:00
memory_hotplug.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
memory-failure.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
memory-tiers.c memory tier: release the new_memtier in find_create_memory_tier() 2023-03-10 09:34:27 +01:00
memory.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
mempolicy.c Merge 6.1.62 into android14-6.1-lts 2023-11-27 14:58:20 +00:00
mempool.c
memremap.c UPSTREAM: mm: introduce __vm_flags_mod and use it in untrack_pfn 2023-06-07 14:24:58 +00:00
memtest.c
migrate_device.c BACKPORT: FROMGIT: mm: enable page walking API to lock vmas during the walk 2023-08-16 16:55:02 +00:00
migrate.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
mincore.c BACKPORT: FROMGIT: mm: enable page walking API to lock vmas during the walk 2023-08-16 16:55:02 +00:00
mlock.c FROMGIT: mm: lock vma explicitly before doing vm_flags_reset and vm_flags_reset_once 2023-08-16 16:55:02 +00:00
mm_init.c mm: multi-gen LRU: groundwork 2022-09-26 19:46:09 -07:00
mm_slot.h mm: introduce common struct mm_slot 2022-10-03 14:02:43 -07:00
mmap_lock.c
mmap.c Merge branch 'android14-6.1' into branch 'android14-6.1-lts' 2024-01-11 08:06:52 +00:00
mmu_gather.c mm/khugepaged: fix GUP-fast interaction by sending IPI 2022-11-30 14:49:42 -08:00
mmu_notifier.c
mmzone.c ANDROID: mm: Create hooks for ZONE_MOVABLE allocs 2023-04-26 17:01:52 +00:00
mprotect.c FROMGIT: mm: lock vma explicitly before doing vm_flags_reset and vm_flags_reset_once 2023-08-16 16:55:02 +00:00
mremap.c UPSTREAM: mm/mremap: write-lock VMA while remapping it to a new address range 2023-06-07 14:25:00 +00:00
msync.c mm/msync: use vma_find() instead of vma linked list 2022-09-26 19:46:25 -07:00
nommu.c BACKPORT: mm: always expand the stack with the mmap write lock held 2023-07-27 11:47:21 +00:00
oom_kill.c BACKPORT: mm: update mark_victim tracepoints fields 2024-04-08 18:00:16 +00:00
page_alloc.c ANDROID: add reclaim tune parameter functions 2024-04-08 17:39:38 +00:00
page_counter.c mm: page_counter: remove unneeded atomic ops for low/min 2022-09-11 20:26:01 -07:00
page_ext.c ANDROID: mm: introduce page_pinner 2023-05-16 21:34:27 +00:00
page_idle.c
page_io.c use less confusing names for iov_iter direction initializers 2023-02-09 11:28:04 +01:00
page_isolation.c BACKPORT: FROMGIT: mm: add alloc_contig_migrate_range allocation statistics 2024-03-13 16:15:27 +00:00
page_owner.c ANDROID: mm: Export page_owner_inited and __set_page_owner 2023-08-29 23:06:13 +00:00
page_pinner.c ANDROID: page_pinner: add missing page_pinner_put_page 2023-05-16 21:34:27 +00:00
page_poison.c
page_reporting.c ANDROID: KVM: arm64: balloon: Notify hyp before reporting free pages to host 2022-12-15 16:13:01 +00:00
page_reporting.h
page_table_check.c mm: page_table_check: Ensure user pages are not slab pages 2023-06-14 11:15:29 +02:00
page_vma_mapped.c mm/swap: add swp_offset_pfn() to fetch PFN from swap entry 2022-09-26 19:46:05 -07:00
page-writeback.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
pagewalk.c BACKPORT: FROMGIT: mm: enable page walking API to lock vmas during the walk 2023-08-16 16:55:02 +00:00
percpu-internal.h
percpu-km.c
percpu-stats.c
percpu-vm.c
percpu.c ANDROID: mm: Export pcpu_nr_pages 2023-03-23 18:20:34 +00:00
pgalloc-track.h
pgtable-generic.c
process_vm_access.c use less confusing names for iov_iter direction initializers 2023-02-09 11:28:04 +01:00
ptdump.c mm: pagewalk: Fix race between unmap and page walker 2022-09-03 10:13:13 -07:00
readahead.c Merge "Merge tag 'android14-6.1.68_r00' into branch 'android14-6.1'" into android14-6.1 2024-01-24 17:34:59 +00:00
rmap.c UPSTREAM: mm/rmap: support move to different root anon_vma in folio_move_anon_rmap() 2024-04-22 18:09:14 +00:00
rodata_test.c mm/rodata_test: use PAGE_ALIGNED() helper 2022-10-03 14:03:05 -07:00
secretmem.c UPSTREAM: mm: replace vma->vm_flags direct modifications with modifier calls 2023-06-07 14:24:57 +00:00
shmem.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
shrinker_debug.c mm: shrinkers: fix deadlock in shrinker debugfs 2023-02-22 12:59:46 +01:00
shuffle.c mm/shuffle: convert module_param_call to module_param_cb 2022-10-03 14:03:07 -07:00
shuffle.h
slab_common.c ANDROID: vendor_hooks: mm: add hook to count the number pages 2023-11-30 18:19:39 +00:00
slab.c Merge 6.1.22 into android14-6.1 2023-03-31 08:15:39 +00:00
slab.h ANDROID: mm: add get_each_object_track function 2023-03-23 18:21:15 +00:00
slob.c Merge branch 'slab/for-6.1/kmalloc_size_roundup' into slab/for-next 2022-09-29 11:30:55 +02:00
slub.c ANDROID: vendor_hooks: mm: add hook to count the number pages 2023-11-14 23:07:42 +00:00
sparse-vmemmap.c mm: hugetlb_vmemmap: move vmemmap code related to HugeTLB to hugetlb_vmemmap.c 2022-08-08 18:06:42 -07:00
sparse.c mm: memory_hotplug: enumerate all supported section flags 2022-07-03 18:08:49 -07:00
swap_cgroup.c mm: memcontrol: don't allocate cgroup swap arrays when memcg is disabled 2022-10-03 14:03:36 -07:00
swap_slots.c mm/swap: convert put_swap_page() to put_swap_folio() 2022-10-03 14:02:46 -07:00
swap_state.c FROMGIT: swap: remove remnants of polling from read_swap_cache_async 2023-07-19 18:48:39 +00:00
swap.c ANDROID: mm: lru_cache_disable skips lru cache drainnig 2023-12-06 18:54:20 +00:00
swap.h FROMGIT: swap: remove remnants of polling from read_swap_cache_async 2023-07-19 18:48:39 +00:00
swapfile.c ANDROID: vendor hooks: Add hooks to support bootloader based hibernation 2023-06-07 14:25:04 +00:00
truncate.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
usercopy.c mm: Fix copy_from_user_nofault(). 2023-06-28 11:12:17 +02:00
userfaultfd.c mm/shmem: use page_mapping() to detect page cache for uffd continue 2022-11-08 15:57:23 -08:00
util.c Merge tag 'android14-6.1.57_r00' into branch 'android14-6.1' 2023-11-27 16:18:59 +00:00
vmalloc.c This is the 6.1.53 stable release 2023-09-18 09:57:37 +00:00
vmpressure.c net-memcg: Fix scope of sockmem pressure indicators 2023-09-13 09:42:33 +02:00
vmscan.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
vmstat.c UPSTREAM: mm: introduce per-VMA lock statistics 2023-06-07 14:25:01 +00:00
workingset.c Reapply "Merge tag 'android14-6.1.75_r00' into android14-6.1" 2024-04-02 19:49:12 +00:00
z3fold.c mm: Convert all PageMovable users to movable_operations 2022-08-02 12:34:03 -04:00
zbud.c
zpool.c
zsmalloc.c Merge 6.1.47 into android14-6.1-lts 2023-09-13 19:35:46 +00:00
zswap.c zswap: do not shrink if cgroup may not zswap 2023-06-21 16:00:54 +02:00