android_kernel_xiaomi_sm8350/mm
Aneesh Kumar K.V 23a17bf13e
BACKPORT: mm/mremap: hold the rmap lock in write mode when moving page table entries.
To avoid a race between rmap walk and mremap, mremap does
take_rmap_locks().  The lock was taken to ensure that rmap walk don't miss
a page table entry due to PTE moves via move_pagetables().  The kernel
does further optimization of this lock such that if we are going to find
the newly added vma after the old vma, the rmap lock is not taken.  This
is because rmap walk would find the vmas in the same order and if we don't
find the page table attached to older vma we would find it with the new
vma which we would iterate later.

As explained in commit eb66ae0308 ("mremap: properly flush TLB before
releasing the page") mremap is special in that it doesn't take ownership
of the page.  The optimized version for PUD/PMD aligned mremap also
doesn't hold the ptl lock.  This can result in stale TLB entries as show
below.

This patch updates the rmap locking requirement in mremap to handle the race condition
explained below with optimized mremap::

Optmized PMD move

    CPU 1                           CPU 2                                   CPU 3

    mremap(old_addr, new_addr)      page_shrinker/try_to_unmap_one

    mmap_write_lock_killable()

                                    addr = old_addr
                                    lock(pte_ptl)
    lock(pmd_ptl)
    pmd = *old_pmd
    pmd_clear(old_pmd)
    flush_tlb_range(old_addr)

    *new_pmd = pmd
                                                                            *new_addr = 10; and fills
                                                                            TLB with new addr
                                                                            and old pfn

    unlock(pmd_ptl)
                                    ptep_clear_flush()
                                    old pfn is free.
                                                                            Stale TLB entry

Optimized PUD move also suffers from a similar race.  Both the above race
condition can be fixed if we force mremap path to take rmap lock.

Link: https://lkml.kernel.org/r/20210616045239.370802-7-aneesh.kumar@linux.ibm.com
Fixes: 2c91bd4a4e ("mm: speed up mremap by 20x on large regions")
Fixes: c49dd3401802 ("mm: speedup mremap on 1GB or larger regions")
Link: https://lore.kernel.org/linux-mm/CAHk-=wgXVR04eBNtxQfevontWnP6FDm+oj5vauQXP3S-huwbPw@mail.gmail.com
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Acked-by: Hugh Dickins <hughd@google.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Joel Fernandes <joel@joelfernandes.org>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Kirill A. Shutemov <kirill@shutemov.name>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit 97113eb39fa7972722ff490b947d8af023e1f6a2)

[Kalesh Singh: Resolve some trivial conflicts in mm/mremap.c]

Bug: 151772539
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Change-Id: I5b7235e982ea2efdc155018271fbaf2711fac4c1
2025-02-20 04:17:49 +02:00
..
kasan Merge android11-5.4.233+ (4716ccc) into msm-5.4 2023-05-30 11:16:10 +05:30
backing-dev.c
balloon_compaction.c
cleancache.c
cma.c Merge android11-5.4.265+ (a34cc1d) into msm-5.4 2024-02-29 15:10:56 +05:30
cma.h
cma_debug.c
compaction.c Merge tag 'ASB-2024-06-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-06-22 17:58:09 +03:00
debug.c
debug_page_ref.c
dmapool.c
early_ioremap.c
fadvise.c
failslab.c
filemap.c Merge android11-5.4.259+ (81334f2) into msm-5.4 2023-12-18 15:40:18 +05:30
frame_vector.c
frontswap.c treewide: Remove uninitialized_var() usage 2023-06-09 10:29:01 +02:00
gup.c Merge android11-5.4.233+ (4716ccc) into msm-5.4 2023-05-30 11:16:10 +05:30
gup_benchmark.c
highmem.c
hmm.c
huge_memory.c Merge android11-5.4.242+(e699d54) into msm-5.4 2023-07-26 14:21:25 +05:30
hugetlb.c Merge android11-5.4.233+ (4716ccc) into msm-5.4 2023-05-30 11:16:10 +05:30
hugetlb_cgroup.c
hwpoison-inject.c
init-mm.c
internal.h
interval_tree.c
Kconfig
Kconfig.debug
khugepaged.c Merge tag 'LA.UM.9.14.r1-21600-LAHAINA.QSSI14.0' of https://git.codelinaro.org/clo/la/kernel/msm-5.4 into android13-5.4-lahaina 2023-06-08 11:39:35 +03:00
kmemleak-test.c
kmemleak.c
ksm.c Merge android11-5.4.249+ (d57e792) into msm-5.4 2023-09-28 16:45:28 +05:30
list_lru.c
maccess.c
madvise.c Merge tag 'ASB-2024-11-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-11-08 15:36:32 +00:00
Makefile Merge tag 'ASB-2024-05-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-05-07 20:29:15 +03:00
memblock.c This is the 5.4.232 stable release 2023-02-22 12:30:48 +00:00
memcontrol.c memcg_write_event_control(): fix a user-triggerable oops 2024-09-04 13:14:50 +02:00
memfd.c
memory-failure.c Merge 5.4.274 into android11-5.4-lts 2024-04-18 07:23:10 +00:00
memory.c Merge tag 'ASB-2024-12-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-12-17 03:24:53 +02:00
memory_hotplug.c
mempolicy.c Merge android11-5.4.249+ (d57e792) into msm-5.4 2023-09-28 16:45:28 +05:30
mempool.c
memremap.c
memtest.c memtest: use {READ,WRITE}_ONCE in memory scanning 2024-04-13 12:51:27 +02:00
migrate.c Merge tag 'ASB-2024-06-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-06-22 17:58:09 +03:00
mincore.c
mlock.c Merge tag 'ASB-2024-05-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-05-07 20:29:15 +03:00
mm_init.c
mmap.c Merge tag 'ASB-2024-05-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-05-07 20:29:15 +03:00
mmu_context.c
mmu_gather.c
mmu_notifier.c
mmzone.c
mprotect.c Merge tag 'ASB-2024-05-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-05-07 20:29:15 +03:00
mremap.c BACKPORT: mm/mremap: hold the rmap lock in write mode when moving page table entries. 2025-02-20 04:17:49 +02:00
msync.c
nommu.c
oom_kill.c
OWNERS
page-writeback.c mm: avoid overflows in dirty throttling logic 2024-08-19 05:33:42 +02:00
page_alloc.c Merge tag 'ASB-2024-06-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-06-22 17:58:09 +03:00
page_counter.c
page_ext.c
page_idle.c
page_io.c
page_isolation.c
page_owner.c
page_poison.c
page_vma_mapped.c
pagewalk.c
percpu-internal.h
percpu-km.c
percpu-stats.c
percpu-vm.c
percpu.c treewide: Remove uninitialized_var() usage 2023-06-09 10:29:01 +02:00
pgsize_migration.c ANDROID: 16K: Introduce pgsize_migration_inline.h 2024-10-10 16:13:30 -07:00
pgtable-generic.c
process_vm_access.c
readahead.c vfs: fix readahead(2) on block devices 2023-11-20 10:30:08 +01:00
rmap.c Merge android11-5.4.242+(e699d54) into msm-5.4 2023-07-26 14:21:25 +05:30
rodata_test.c
shmem.c Merge tag 'ASB-2025-02-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2025-02-06 22:53:05 +02:00
showmem.c
shuffle.c
shuffle.h
slab.c
slab.h
slab_common.c Merge tag 'ASB-2024-12-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2024-12-17 03:24:53 +02:00
slob.c
slub.c mm: slub: Declare slab_owner_ops only when SLUB DEBUG is enabled 2024-01-18 01:32:07 -08:00
sparse-vmemmap.c
sparse.c
swap.c Merge android11-5.4.249+ (d57e792) into msm-5.4 2023-09-28 16:45:28 +05:30
swap_cgroup.c
swap_slots.c
swap_state.c
swapfile.c This is the 5.4.285 stable release 2024-11-09 11:29:17 +00:00
truncate.c
usercopy.c
userfaultfd.c userfaultfd: fix mmap_changing checking in mfill_atomic_hugetlb 2024-03-01 13:13:33 +01:00
util.c mm: only enforce minimum stack gap size if it's sensible 2024-11-08 16:20:35 +01:00
vmacache.c
vmalloc.c
vmpressure.c
vmscan.c Merge tag 'ASB-2025-02-05_11-5.4' of https://android.googlesource.com/kernel/common into android13-5.4-lahaina 2025-02-06 22:53:05 +02:00
vmstat.c
workingset.c
z3fold.c
zbud.c
zpool.c
zsmalloc.c
zswap.c