incoming
Subsystems (102)
Andrew Morton
maintainerThis is the rest of the -mm tree, less 66 patches which are dependent on things which are (or were recently) in linux-next. I'll trickle that material over next week.
192 patches, based on 7cf3dead1ad70c72edb03e2d98e1f3dcd332cdb2 plus the June 28 sendings.
Subsystems affected by this patch series:
mm/hugetlb
mm/userfaultfd
mm/vmscan
mm/kconfig
mm/proc
mm/z3fold
mm/zbud
mm/ras
mm/mempolicy
mm/memblock
mm/migration
mm/thp
mm/nommu
mm/kconfig
mm/madvise
mm/memory-hotplug
mm/zswap
mm/zsmalloc
mm/zram
mm/cleanups
mm/kfence
mm/hmm
procfs
sysctl
misc
core-kernel
lib
lz4
checkpatch
init
kprobes
nilfs2
hfs
signals
exec
kcov
selftests
compress/decompress
ipc
Subsystem: mm/hugetlb
Muchun Song <[email protected]>:
Patch series "Free some vmemmap pages of HugeTLB page", v23:
mm: memory_hotplug: factor out bootmem core functions to bootmem_info.c
mm: hugetlb: introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
mm: hugetlb: gather discrete indexes of tail page
mm: hugetlb: free the vmemmap pages associated with each HugeTLB page
mm: hugetlb: defer freeing of HugeTLB pages
mm: hugetlb: alloc the vmemmap pages associated with each HugeTLB page
mm: hugetlb: add a kernel parameter hugetlb_free_vmemmap
mm: memory_hotplug: disable memmap_on_memory when hugetlb_free_vmemmap enabled
mm: hugetlb: introduce nr_free_vmemmap_pages in the struct hstate
Shixin Liu <[email protected]>:
mm/debug_vm_pgtable: move {pmd/pud}_huge_tests out of CONFIG_TRANSPARENT_HUGEPAGE mm/debug_vm_pgtable: remove redundant pfn_{pmd/pte}() and fix one comment mistake
Miaohe Lin <[email protected]>:
Patch series "Cleanup and fixup for huge_memory:, v3:
mm/huge_memory.c: remove dedicated macro HPAGE_CACHE_INDEX_MASK mm/huge_memory.c: use page->deferred_list
mm/huge_memory.c: add missing read-only THP checking in transparent_hugepage_enabled() mm/huge_memory.c: remove unnecessary tlb_remove_page_size() for huge zero pmd mm/huge_memory.c: don't discard hugepage if other processes are mapping it
Christophe Leroy <[email protected]>:
Patch series "Subject: [PATCH v2 0/5] Implement huge VMAP and VMALLOC on powerpc 8xx", v2:
mm/hugetlb: change parameters of arch_make_huge_pte()
mm/pgtable: add stubs for {pmd/pub}_{set/clear}_huge
mm/vmalloc: enable mapping of huge pages at pte level in vmap
mm/vmalloc: enable mapping of huge pages at pte level in vmalloc powerpc/8xx: add support for huge pages on VMAP and VMALLOC
Nanyong Sun <[email protected]>:
khugepaged: selftests: remove debug_cow
Mina Almasry <[email protected]>:
mm, hugetlb: fix racy resv_huge_pages underflow on UFFDIO_COPY
Muchun Song <[email protected]>:
Patch series "Split huge PMD mapping of vmemmap pages", v4:
mm: sparsemem: split the huge PMD mapping of vmemmap pages
mm: sparsemem: use huge PMD mapping for vmemmap pages
mm: hugetlb: introduce CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON
Mike Kravetz <[email protected]>:
Patch series "Fix prep_compound_gigantic_page ref count adjustment":
hugetlb: remove prep_compound_huge_page cleanup
hugetlb: address ref count racing in prep_compound_gigantic_page
Naoya Horiguchi <[email protected]>:
mm/hwpoison: disable pcp for page_handle_poison()
Subsystem: mm/userfaultfd
Peter Xu <[email protected]>:
Patch series "userfaultfd/selftests: A few cleanups", v2:
userfaultfd/selftests: use user mode only userfaultfd/selftests: remove the time() check on delayed uffd userfaultfd/selftests: dropping VERIFY check in locking_thread userfaultfd/selftests: only dump counts if mode enabled userfaultfd/selftests: unify error handling
Patch series "mm/uffd: Misc fix for uffd-wp and one more test":
mm/thp: simplify copying of huge zero page pmd when fork mm/userfaultfd: fix uffd-wp special cases for fork() mm/userfaultfd: fail uffd-wp registration if not supported mm/pagemap: export uffd-wp protection information userfaultfd/selftests: add pagemap uffd-wp test
Axel Rasmussen <[email protected]>:
Patch series "userfaultfd: add minor fault handling for shmem", v6:
userfaultfd/shmem: combine shmem_{mcopy_atomic,mfill_zeropage}_pte userfaultfd/shmem: support minor fault registration for shmem userfaultfd/shmem: support UFFDIO_CONTINUE for shmem userfaultfd/shmem: advertise shmem minor fault support userfaultfd/shmem: modify shmem_mfill_atomic_pte to use install_pte() userfaultfd/selftests: use memfd_create for shmem test type userfaultfd/selftests: create alias mappings in the shmem test userfaultfd/selftests: reinitialize test context in each test userfaultfd/selftests: exercise minor fault handling shmem support
Subsystem: mm/vmscan
Yu Zhao <[email protected]>:
mm/vmscan.c: fix potential deadlock in reclaim_pages() include/trace/events/vmscan.h: remove mm_vmscan_inactive_list_is_low
Miaohe Lin <[email protected]>:
mm: workingset: define macro WORKINGSET_SHIFT
Subsystem: mm/kconfig
Kefeng Wang <[email protected]>:
mm/kconfig: move HOLES_IN_ZONE into mm
Subsystem: mm/proc
Mike Rapoport <[email protected]>:
docs: proc.rst: meminfo: briefly describe gaps in memory accounting
David Hildenbrand <[email protected]>:
Patch series "fs/proc/kcore: don't read offline sections, logically offline pages and hwpoisoned pages", v3:
fs/proc/kcore: drop KCORE_REMAP and KCORE_OTHER
fs/proc/kcore: pfn_is_ram check only applies to KCORE_RAM
fs/proc/kcore: don't read offline sections, logically offline pages and hwpoisoned pages
mm: introduce page_offline_(begin|end|freeze|thaw) to synchronize setting PageOffline()
virtio-mem: use page_offline_(start|end) when setting PageOffline()
fs/proc/kcore: use page_offline_(freeze|thaw)
Subsystem: mm/z3fold
Miaohe Lin <[email protected]>:
Patch series "Cleanup and fixup for z3fold":
mm/z3fold: define macro NCHUNKS as TOTAL_CHUNKS - ZHDR_CHUNKS mm/z3fold: avoid possible underflow in z3fold_alloc() mm/z3fold: remove magic number in z3fold_create_pool() mm/z3fold: remove unused function handle_to_z3fold_header() mm/z3fold: fix potential memory leak in z3fold_destroy_pool() mm/z3fold: use release_z3fold_page_locked() to release locked z3fold page
Subsystem: mm/zbud
Miaohe Lin <[email protected]>:
Patch series "Cleanups for zbud", v2:
mm/zbud: reuse unbuddied[0] as buddied in zbud_pool mm/zbud: don't export any zbud API
Subsystem: mm/ras
YueHaibing <[email protected]>:
mm/compaction: use DEVICE_ATTR_WO macro
Liu Xiang <[email protected]>:
mm: compaction: remove duplicate !list_empty(&sublist) check
Wonhyuk Yang <[email protected]>:
mm/compaction: fix 'limit' in fast_isolate_freepages
Subsystem: mm/mempolicy
Feng Tang <[email protected]>:
Patch series "mm/mempolicy: some fix and semantics cleanup", v4:
mm/mempolicy: cleanup nodemask intersection check for oom mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED policy mm/mempolicy: unify the parameter sanity check for mbind and set_mempolicy
Yang Shi <[email protected]>:
mm: mempolicy: don't have to split pmd for huge zero page
Ben Widawsky <[email protected]>:
mm/mempolicy: use unified 'nodes' for bind/interleave/prefer policies
Subsystem: mm/memblock
Mike Rapoport <[email protected]>:
Patch series "arm64: drop pfn_valid_within() and simplify pfn_valid()", v4:
include/linux/mmzone.h: add documentation for pfn_valid()
memblock: update initialization of reserved pages
arm64: decouple check whether pfn is in linear map from pfn_valid()
arm64: drop pfn_valid_within() and simplify pfn_valid()
Anshuman Khandual <[email protected]>:
arm64/mm: drop HAVE_ARCH_PFN_VALID
Subsystem: mm/migration
Muchun Song <[email protected]>:
mm: migrate: fix missing update page_private to hugetlb_page_subpool
Subsystem: mm/thp
Collin Fijalkovich <[email protected]>:
mm, thp: relax the VM_DENYWRITE constraint on file-backed THPs
Yang Shi <[email protected]>:
mm: memory: add orig_pmd to struct vm_fault
mm: memory: make numa_migrate_prep() non-static
mm: thp: refactor NUMA fault handling
mm: migrate: account THP NUMA migration counters correctly
mm: migrate: don't split THP for misplaced NUMA page
mm: migrate: check mapcount for THP instead of refcount
mm: thp: skip make PMD PROT_NONE if THP migration is not supported
Anshuman Khandual <[email protected]>:
mm/thp: make ARCH_ENABLE_SPLIT_PMD_PTLOCK dependent on PGTABLE_LEVELS > 2
Yang Shi <[email protected]>:
mm: rmap: make try_to_unmap() void function
Hugh Dickins <[email protected]>:
mm/thp: remap_page() is only needed on anonymous THP
mm: hwpoison_user_mappings() try_to_unmap() with TTU_SYNC
"Matthew Wilcox (Oracle)" <[email protected]>:
mm/thp: fix strncpy warning
Subsystem: mm/nommu
Chen Li <[email protected]>:
nommu: remove __GFP_HIGHMEM in vmalloc/vzalloc
Liam Howlett <[email protected]>:
mm/nommu: unexport do_munmap()
Subsystem: mm/kconfig
Kefeng Wang <[email protected]>:
mm: generalize ZONE_[DMA|DMA32]
Subsystem: mm/madvise
David Hildenbrand <[email protected]>:
Patch series "mm/madvise: introduce MADV_POPULATE_(READ|WRITE) to prefault page tables", v2:
mm: make variable names for populate_vma_page_range() consistent mm/madvise: introduce MADV_POPULATE_(READ|WRITE) to prefault page tables
MAINTAINERS: add tools/testing/selftests/vm/ to MEMORY MANAGEMENT selftests/vm: add protection_keys_32 / protection_keys_64 to gitignore selftests/vm: add test for MADV_POPULATE_(READ|WRITE)
Subsystem: mm/memory-hotplug
Liam Mark <[email protected]>:
mm/memory_hotplug: rate limit page migration warnings
Oscar Salvador <[email protected]>:
mm,memory_hotplug: drop unneeded locking
Subsystem: mm/zswap
Miaohe Lin <[email protected]>:
Patch series "Cleanup and fixup for zswap":
mm/zswap.c: remove unused function zswap_debugfs_exit() mm/zswap.c: avoid unnecessary copy-in at map time mm/zswap.c: fix two bugs in zswap_writeback_entry()
Subsystem: mm/zsmalloc
Zhaoyang Huang <[email protected]>:
mm: zram: amend SLAB_RECLAIM_ACCOUNT on zspage_cachep
Miaohe Lin <[email protected]>:
Patch series "Cleanup for zsmalloc":
mm/zsmalloc.c: remove confusing code in obj_free() mm/zsmalloc.c: improve readability for async_free_zspage()
Subsystem: mm/zram
Yue Hu <[email protected]>:
zram: move backing_dev under macro CONFIG_ZRAM_WRITEBACK
Subsystem: mm/cleanups
Hyeonggon Yoo <[email protected]>:
mm: fix typos and grammar error in comments
Anshuman Khandual <[email protected]>:
mm: define default value for FIRST_USER_ADDRESS
Zhen Lei <[email protected]>:
mm: fix spelling mistakes
Mel Gorman <[email protected]>:
Patch series "Clean W=1 build warnings for mm/":
mm/vmscan: remove kerneldoc-like comment from isolate_lru_pages mm/vmalloc: include header for prototype of set_iounmap_nonlazy mm/page_alloc: make should_fail_alloc_page() static mm/mapping_dirty_helpers: remove double Note in kerneldoc mm/memcontrol.c: fix kerneldoc comment for mem_cgroup_calculate_protection mm/memory_hotplug: fix kerneldoc comment for __try_online_node mm/memory_hotplug: fix kerneldoc comment for __remove_memory mm/zbud: add kerneldoc fields for zbud_pool
mm/z3fold: add kerneldoc fields for z3fold_pool
mm/swap: make swap_address_space an inline function mm/mmap_lock: remove dead code for !CONFIG_TRACING configurations mm/page_alloc: move prototype for find_suitable_fallback mm/swap: make NODE_DATA an inline function on CONFIG_FLATMEM
Anshuman Khandual <[email protected]>:
mm/thp: define default pmd_pgtable()
Subsystem: mm/kfence
Marco Elver <[email protected]>:
kfence: unconditionally use unbound work queue
Subsystem: mm/hmm
Alistair Popple <[email protected]>:
Patch series "Add support for SVM atomics in Nouveau", v11:
mm: remove special swap entry functions
mm/swapops: rework swap entry manipulation code mm/rmap: split try_to_munlock from try_to_unmap mm/rmap: split migration into its own function
mm: rename migrate_pgmap_owner
mm/memory.c: allow different return codes for copy_nonpresent_pte()
mm: device exclusive memory access
mm: selftests for exclusive device memory
nouveau/svm: refactor nouveau_range_fault
nouveau/svm: implement atomic SVM access
Subsystem: procfs
Marcelo Henrique Cerri <[email protected]>:
proc: Avoid mixing integer types in mem_rw()
ZHOUFENG <[email protected]>:
fs/proc/kcore.c: add mmap interface
Kalesh Singh <[email protected]>:
procfs: allow reading fdinfo with PTRACE_MODE_READ procfs/dmabuf: add inode number to /proc/*/fdinfo
Subsystem: sysctl
Jiapeng Chong <[email protected]>:
sysctl: remove redundant assignment to first
Subsystem: misc
Andy Shevchenko <[email protected]>:
drm: include only needed headers in ascii85.h
Subsystem: core-kernel
Andy Shevchenko <[email protected]>:
kernel.h: split out panic and oops helpers
Subsystem: lib
Zhen Lei <[email protected]>:
lib: decompress_bunzip2: remove an unneeded semicolon
Andy Shevchenko <[email protected]>:
Patch series "lib/string_helpers: get rid of ugly *_escape_mem_ascii()", v3:
lib/string_helpers: switch to use BIT() macro lib/string_helpers: move ESCAPE_NP check inside 'else' branch in a loop lib/string_helpers: drop indentation level in string_escape_mem() lib/string_helpers: introduce ESCAPE_NA for escaping non-ASCII lib/string_helpers: introduce ESCAPE_NAP to escape non-ASCII and non-printable lib/string_helpers: allow to append additional characters to be escaped lib/test-string_helpers: print flags in hexadecimal format lib/test-string_helpers: get rid of trailing comma in terminators lib/test-string_helpers: add test cases for new features
MAINTAINERS: add myself as designated reviewer for generic string library
seq_file: introduce seq_escape_mem()
seq_file: add seq_escape_str() as replica of string_escape_str()
seq_file: convert seq_escape() to use seq_escape_str()
nfsd: avoid non-flexible API in seq_quote_mem()
seq_file: drop unused *_escape_mem_ascii()
Trent Piepho <[email protected]>:
lib/math/rational.c: fix divide by zero
lib/math/rational: add Kunit test cases
Zhen Lei <[email protected]>:
lib/decompressors: fix spelling mistakes
lib/mpi: fix spelling mistakes
Alexey Dobriyan <[email protected]>:
lib: memscan() fixlet
lib: uninline simple_strtoull()
Matteo Croce <[email protected]>:
lib/test_string.c: allow module removal
Andy Shevchenko <[email protected]>:
kernel.h: split out kstrtox() and simple_strtox() to a separate header
Subsystem: lz4
Rajat Asthana <[email protected]>:
lz4_decompress: declare LZ4_decompress_safe_withPrefix64k static
Dimitri John Ledkov <[email protected]>:
lib/decompress_unlz4.c: correctly handle zero-padding around initrds.
Subsystem: checkpatch
Guenter Roeck <[email protected]>:
checkpatch: scripts/spdxcheck.py now requires python3
Joe Perches <[email protected]>:
checkpatch: improve the indented label test
Guenter Roeck <[email protected]>:
checkpatch: do not complain about positive return values starting with EPOLL
Subsystem: init
Andrew Halaney <[email protected]>:
init: print out unknown kernel parameters
Subsystem: kprobes
Barry Song <[email protected]>:
kprobes: remove duplicated strong free_insn_page in x86 and s390
Subsystem: nilfs2
Colin Ian King <[email protected]>:
nilfs2: remove redundant continue statement in a while-loop
Subsystem: hfs
Zhen Lei <[email protected]>:
hfsplus: remove unnecessary oom message
Chung-Chiang Cheng <[email protected]>:
hfsplus: report create_date to kstat.btime
Subsystem: signals
Al Viro <[email protected]>:
x86: signal: don't do sas_ss_reset() until we are certain that sigframe won't be abandoned
Subsystem: exec
Alexey Dobriyan <[email protected]>:
exec: remove checks in __register_bimfmt()
Subsystem: kcov
Marco Elver <[email protected]>:
kcov: add __no_sanitize_coverage to fix noinstr for all architectures
Subsystem: selftests
Dave Hansen <[email protected]>:
Patch series "selftests/vm/pkeys: Bug fixes and a new test":
selftests/vm/pkeys: fix alloc_random_pkey() to make it really, really random selftests/vm/pkeys: handle negative sys_pkey_alloc() return code selftests/vm/pkeys: refill shadow register after implicit kernel write selftests/vm/pkeys: exercise x86 XSAVE init state
Subsystem: compress/decompress
Yu Kuai <[email protected]>:
lib/decompressors: remove set but not used variabled 'level'
Subsystem: ipc
Vasily Averin <[email protected]>:
Patch series "ipc: allocations cleanup", v2:
ipc sem: use kvmalloc for sem_undo allocation
ipc: use kmalloc for msg_queue and shmid_kernel
Manfred Spraul <[email protected]>:
ipc/sem.c: use READ_ONCE()/WRITE_ONCE() for use_global_lock ipc/util.c: use binary search for max_idx
Documentation/admin-guide/kernel-parameters.txt | 35 Documentation/admin-guide/mm/hugetlbpage.rst | 11 Documentation/admin-guide/mm/memory-hotplug.rst | 13 Documentation/admin-guide/mm/pagemap.rst | 2 Documentation/admin-guide/mm/userfaultfd.rst | 3 Documentation/core-api/kernel-api.rst | 7 Documentation/filesystems/proc.rst | 48 Documentation/vm/hmm.rst | 19 Documentation/vm/unevictable-lru.rst | 33 MAINTAINERS | 10 arch/alpha/Kconfig | 5 arch/alpha/include/asm/pgalloc.h | 1 arch/alpha/include/asm/pgtable.h | 1 arch/alpha/include/uapi/asm/mman.h | 3 arch/alpha/kernel/setup.c | 2 arch/arc/include/asm/pgalloc.h | 2 arch/arc/include/asm/pgtable.h | 8 arch/arm/Kconfig | 3 arch/arm/include/asm/pgalloc.h | 1 arch/arm64/Kconfig | 15 arch/arm64/include/asm/hugetlb.h | 3 arch/arm64/include/asm/memory.h | 2 arch/arm64/include/asm/page.h | 4 arch/arm64/include/asm/pgalloc.h | 1 arch/arm64/include/asm/pgtable.h | 2 arch/arm64/kernel/setup.c | 1 arch/arm64/kvm/mmu.c | 2 arch/arm64/mm/hugetlbpage.c | 5 arch/arm64/mm/init.c | 51 arch/arm64/mm/ioremap.c | 4 arch/arm64/mm/mmu.c | 22 arch/csky/include/asm/pgalloc.h | 2 arch/csky/include/asm/pgtable.h | 1 arch/hexagon/include/asm/pgtable.h | 4 arch/ia64/Kconfig | 7 arch/ia64/include/asm/pal.h | 1 arch/ia64/include/asm/pgalloc.h | 1 arch/ia64/include/asm/pgtable.h | 1 arch/m68k/Kconfig | 5 arch/m68k/include/asm/mcf_pgalloc.h | 2 arch/m68k/include/asm/mcf_pgtable.h | 2 arch/m68k/include/asm/motorola_pgalloc.h | 1 arch/m68k/include/asm/motorola_pgtable.h | 2 arch/m68k/include/asm/pgtable_mm.h | 1 arch/m68k/include/asm/sun3_pgalloc.h | 1 arch/microblaze/Kconfig | 4 arch/microblaze/include/asm/pgalloc.h | 2 arch/microblaze/include/asm/pgtable.h | 2 arch/mips/Kconfig | 10 arch/mips/include/asm/pgalloc.h | 1 arch/mips/include/asm/pgtable-32.h | 1 arch/mips/include/asm/pgtable-64.h | 1 arch/mips/include/uapi/asm/mman.h | 3 arch/mips/kernel/relocate.c | 1 arch/mips/sgi-ip22/ip22-reset.c | 1 arch/mips/sgi-ip32/ip32-reset.c | 1 arch/nds32/include/asm/pgalloc.h | 5 arch/nios2/include/asm/pgalloc.h | 1 arch/nios2/include/asm/pgtable.h | 2 arch/openrisc/include/asm/pgalloc.h | 2 arch/openrisc/include/asm/pgtable.h | 1 arch/parisc/include/asm/pgalloc.h | 1 arch/parisc/include/asm/pgtable.h | 2 arch/parisc/include/uapi/asm/mman.h | 3 arch/parisc/kernel/pdc_chassis.c | 1 arch/powerpc/Kconfig | 6 arch/powerpc/include/asm/book3s/pgtable.h | 1 arch/powerpc/include/asm/nohash/32/hugetlb-8xx.h | 5 arch/powerpc/include/asm/nohash/32/mmu-8xx.h | 43 arch/powerpc/include/asm/nohash/32/pgtable.h | 1 arch/powerpc/include/asm/nohash/64/pgtable.h | 2 arch/powerpc/include/asm/pgalloc.h | 5 arch/powerpc/include/asm/pgtable.h | 6 arch/powerpc/kernel/setup-common.c | 1 arch/powerpc/platforms/Kconfig.cputype | 1 arch/riscv/Kconfig | 5 arch/riscv/include/asm/pgalloc.h | 2 arch/riscv/include/asm/pgtable.h | 2 arch/s390/Kconfig | 6 arch/s390/include/asm/pgalloc.h | 3 arch/s390/include/asm/pgtable.h | 5 arch/s390/kernel/ipl.c | 1 arch/s390/kernel/kprobes.c | 5 arch/s390/mm/pgtable.c | 2 arch/sh/include/asm/pgalloc.h | 1 arch/sh/include/asm/pgtable.h | 2 arch/sparc/Kconfig | 5 arch/sparc/include/asm/pgalloc_32.h | 1 arch/sparc/include/asm/pgalloc_64.h | 1 arch/sparc/include/asm/pgtable_32.h | 3 arch/sparc/include/asm/pgtable_64.h | 8 arch/sparc/kernel/sstate.c | 1 arch/sparc/mm/hugetlbpage.c | 6 arch/sparc/mm/init_64.c | 1 arch/um/drivers/mconsole_kern.c | 1 arch/um/include/asm/pgalloc.h | 1 arch/um/include/asm/pgtable-2level.h | 1 arch/um/include/asm/pgtable-3level.h | 1 arch/um/kernel/um_arch.c | 1 arch/x86/Kconfig | 17 arch/x86/include/asm/desc.h | 1 arch/x86/include/asm/pgalloc.h | 2 arch/x86/include/asm/pgtable_types.h | 2 arch/x86/kernel/cpu/mshyperv.c | 1 arch/x86/kernel/kprobes/core.c | 6 arch/x86/kernel/setup.c | 1 arch/x86/mm/init_64.c | 21 arch/x86/mm/pgtable.c | 34 arch/x86/purgatory/purgatory.c | 2 arch/x86/xen/enlighten.c | 1 arch/xtensa/include/asm/pgalloc.h | 2 arch/xtensa/include/asm/pgtable.h | 1 arch/xtensa/include/uapi/asm/mman.h | 3 arch/xtensa/platforms/iss/setup.c | 1 drivers/block/zram/zram_drv.h | 2 drivers/bus/brcmstb_gisb.c | 1 drivers/char/ipmi/ipmi_msghandler.c | 1 drivers/clk/analogbits/wrpll-cln28hpc.c | 4 drivers/edac/altera_edac.c | 1 drivers/firmware/google/gsmi.c | 1 drivers/gpu/drm/nouveau/include/nvif/if000c.h | 1 drivers/gpu/drm/nouveau/nouveau_svm.c | 162 ++- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h | 1 drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgp100.c | 6 drivers/hv/vmbus_drv.c | 1 drivers/hwtracing/coresight/coresight-cpu-debug.c | 1 drivers/leds/trigger/ledtrig-activity.c | 1 drivers/leds/trigger/ledtrig-heartbeat.c | 1 drivers/leds/trigger/ledtrig-panic.c | 1 drivers/misc/bcm-vk/bcm_vk_dev.c | 1 drivers/misc/ibmasm/heartbeat.c | 1 drivers/misc/pvpanic/pvpanic.c | 1 drivers/net/ipa/ipa_smp2p.c | 1 drivers/parisc/power.c | 1 drivers/power/reset/ltc2952-poweroff.c | 1 drivers/remoteproc/remoteproc_core.c | 1 drivers/s390/char/con3215.c | 1 drivers/s390/char/con3270.c | 1 drivers/s390/char/sclp.c | 1 drivers/s390/char/sclp_con.c | 1 drivers/s390/char/sclp_vt220.c | 1 drivers/s390/char/zcore.c | 1 drivers/soc/bcm/brcmstb/pm/pm-arm.c | 1 drivers/staging/olpc_dcon/olpc_dcon.c | 1 drivers/video/fbdev/hyperv_fb.c | 1 drivers/virtio/virtio_mem.c | 2 fs/Kconfig | 15 fs/exec.c | 3 fs/hfsplus/inode.c | 5 fs/hfsplus/xattr.c | 1 fs/nfsd/nfs4state.c | 2 fs/nilfs2/btree.c | 1 fs/open.c | 13 fs/proc/base.c | 6 fs/proc/fd.c | 20 fs/proc/kcore.c | 136 ++ fs/proc/task_mmu.c | 34 fs/seq_file.c | 43 fs/userfaultfd.c | 15 include/asm-generic/bug.h | 3 include/linux/ascii85.h | 3 include/linux/bootmem_info.h | 68 + include/linux/compat.h | 2 include/linux/compiler-clang.h | 17 include/linux/compiler-gcc.h | 6 include/linux/compiler_types.h | 2 include/linux/huge_mm.h | 74 - include/linux/hugetlb.h | 80 + include/linux/hugetlb_cgroup.h | 19 include/linux/kcore.h | 3 include/linux/kernel.h | 227 ---- include/linux/kprobes.h | 1 include/linux/kstrtox.h | 155 ++ include/linux/memblock.h | 4 include/linux/memory_hotplug.h | 27 include/linux/mempolicy.h | 9 include/linux/memremap.h | 2 include/linux/migrate.h | 27 include/linux/mm.h | 18 include/linux/mm_types.h | 2 include/linux/mmu_notifier.h | 26 include/linux/mmzone.h | 27 include/linux/mpi.h | 4 include/linux/page-flags.h | 22 include/linux/panic.h | 98 + include/linux/panic_notifier.h | 12 include/linux/pgtable.h | 44 include/linux/rmap.h | 13 include/linux/seq_file.h | 10 include/linux/shmem_fs.h | 19 include/linux/signal.h | 2 include/linux/string.h | 7 include/linux/string_helpers.h | 31 include/linux/sunrpc/cache.h | 1 include/linux/swap.h | 19 include/linux/swapops.h | 171 +-- include/linux/thread_info.h | 1 include/linux/userfaultfd_k.h | 5 include/linux/vmalloc.h | 15 include/linux/zbud.h | 23 include/trace/events/vmscan.h | 41 include/uapi/asm-generic/mman-common.h | 3 include/uapi/linux/mempolicy.h | 1 include/uapi/linux/userfaultfd.h | 7 init/main.c | 42 ipc/msg.c | 6 ipc/sem.c | 25 ipc/shm.c | 6 ipc/util.c | 44 ipc/util.h | 3 kernel/hung_task.c | 1 kernel/kexec_core.c | 1 kernel/kprobes.c | 2 kernel/panic.c | 1 kernel/rcu/tree.c | 2 kernel/signal.c | 14 kernel/sysctl.c | 4 kernel/trace/trace.c | 1 lib/Kconfig.debug | 12 lib/decompress_bunzip2.c | 6 lib/decompress_unlz4.c | 8 lib/decompress_unlzo.c | 3 lib/decompress_unxz.c | 2 lib/decompress_unzstd.c | 4 lib/kstrtox.c | 5 lib/lz4/lz4_decompress.c | 2 lib/math/Makefile | 1 lib/math/rational-test.c | 56 + lib/math/rational.c | 16 lib/mpi/longlong.h | 4 lib/mpi/mpicoder.c | 6 lib/mpi/mpiutil.c | 2 lib/parser.c | 1 lib/string.c | 2 lib/string_helpers.c | 142 +- lib/test-string_helpers.c | 157 ++- lib/test_hmm.c | 127 ++ lib/test_hmm_uapi.h | 2 lib/test_string.c | 5 lib/vsprintf.c | 1 lib/xz/xz_dec_bcj.c | 2 lib/xz/xz_dec_lzma2.c | 8 lib/zlib_inflate/inffast.c | 2 lib/zstd/huf.h | 2 mm/Kconfig | 16 mm/Makefile | 2 mm/bootmem_info.c | 127 ++ mm/compaction.c | 20 mm/debug_vm_pgtable.c | 109 -- mm/gup.c | 58 + mm/hmm.c | 12 mm/huge_memory.c | 269 ++--- mm/hugetlb.c | 369 +++++-- mm/hugetlb_vmemmap.c | 332 ++++++ mm/hugetlb_vmemmap.h | 53 - mm/internal.h | 29 mm/kfence/core.c | 4 mm/khugepaged.c | 20 mm/madvise.c | 66 + mm/mapping_dirty_helpers.c | 2 mm/memblock.c | 28 mm/memcontrol.c | 4 mm/memory-failure.c | 38 mm/memory.c | 239 +++- mm/memory_hotplug.c | 161 --- mm/mempolicy.c | 323 ++---- mm/migrate.c | 268 +---- mm/mlock.c | 12 mm/mmap_lock.c | 59 - mm/mprotect.c | 18 mm/nommu.c | 5 mm/oom_kill.c | 2 mm/page_alloc.c | 5 mm/page_vma_mapped.c | 15 mm/rmap.c | 644 +++++++++--- mm/shmem.c | 125 -- mm/sparse-vmemmap.c | 432 +++++++- mm/sparse.c | 1 mm/swap.c | 2 mm/swapfile.c | 2 mm/userfaultfd.c | 249 ++-- mm/util.c | 40 mm/vmalloc.c | 37 mm/vmscan.c | 20 mm/workingset.c | 10 mm/z3fold.c | 39 mm/zbud.c | 235 ++-- mm/zsmalloc.c | 5 mm/zswap.c | 26 scripts/checkpatch.pl | 16 tools/testing/selftests/vm/.gitignore | 3 tools/testing/selftests/vm/Makefile | 5 tools/testing/selftests/vm/hmm-tests.c | 158 +++ tools/testing/selftests/vm/khugepaged.c | 4 tools/testing/selftests/vm/madv_populate.c | 342 ++++++ tools/testing/selftests/vm/pkey-x86.h | 1 tools/testing/selftests/vm/protection_keys.c | 85 + tools/testing/selftests/vm/run_vmtests.sh | 16 tools/testing/selftests/vm/userfaultfd.c | 1094 ++++++++++----------- 299 files changed, 6277 insertions(+), 3183 deletions(-)
Andrew Morton
maintainerPATCH 1/192 mm: memory_hotplug: factor out bootmem core functions to bootmem_info.c
From: Muchun Song <[email protected]>
Subject: mm: memory_hotplug: factor out bootmem core functions to bootmem_info.c
Patch series "Free some vmemmap pages of HugeTLB page", v23.
This patch series will free some vmemmap pages(struct page structures) associated with each HugeTLB page when preallocated to save memory.
In order to reduce the difficulty of the first version of code review. In this version, we disable PMD/huge page mapping of vmemmap if this feature was enabled. This acutely eliminates a bunch of the complex code doing page table manipulation. When this patch series is solid, we cam add the code of vmemmap page table manipulation in the future.
The struct page structures (page structs) are used to describe a physical page frame. By default, there is an one-to-one mapping from a page frame to it's corresponding page struct.
The HugeTLB pages consist of multiple base page size pages and is supported by many architectures. See hugetlbpage.rst in the Documentation directory for more details. On the x86 architecture, HugeTLB pages of size 2MB and 1GB are currently supported. Since the base page size on x86 is 4KB, a 2MB HugeTLB page consists of 512 base pages and a 1GB HugeTLB page consists of 4096 base pages. For each base page, there is a corresponding page struct.
Within the HugeTLB subsystem, only the first 4 page structs are used to contain unique information about a HugeTLB page. HUGETLB_CGROUP_MIN_ORDER provides this upper limit. The only 'useful' information in the remaining page structs is the compound_head field, and this field is the same for all tail pages.
By removing redundant page structs for HugeTLB pages, memory can returned to the buddy allocator for other uses.
When the system boot up, every 2M HugeTLB has 512 struct page structs which size is 8 pages(sizeof(struct page) * 512 / PAGE_SIZE).
HugeTLB struct pages(8 pages) page frame(8 pages)
+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | +-----------+ +-----------+ | | | 1 | -------------> | 1 | | | +-----------+ +-----------+ | | | 2 | -------------> | 2 | | | +-----------+ +-----------+ | | | 3 | -------------> | 3 | | | +-----------+ +-----------+ | | | 4 | -------------> | 4 | | 2MB | +-----------+ +-----------+ | | | 5 | -------------> | 5 | | | +-----------+ +-----------+ | | | 6 | -------------> | 6 | | | +-----------+ +-----------+ | | | 7 | -------------> | 7 | | | +-----------+ +-----------+ | | | | | | +-----------+
The value of page->compound_head is the same for all tail pages. The first page of page structs (page 0) associated with the HugeTLB page contains the 4 page structs necessary to describe the HugeTLB. The only use of the remaining pages of page structs (page 1 to page 7) is to point to page->compound_head. Therefore, we can remap pages 2 to 7 to page 1. Only 2 pages of page structs will be used for each HugeTLB page. This will allow us to free the remaining 6 pages to the buddy allocator.
Here is how things look after remapping.
HugeTLB struct pages(8 pages) page frame(8 pages)
+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | +-----------+ +-----------+ | | | 1 | -------------> | 1 | | | +-----------+ +-----------+ | | | 2 | ----------------^ ^ ^ ^ ^ ^ | | +-----------+ | | | | | | | | 3 | ------------------+ | | | | | | +-----------+ | | | | | | | 4 | --------------------+ | | | | 2MB | +-----------+ | | | | | | 5 | ----------------------+ | | | | +-----------+ | | | | | 6 | ------------------------+ | | | +-----------+ | | | | 7 | --------------------------+ | | +-----------+ | | | | | | +-----------+
When a HugeTLB is freed to the buddy system, we should allocate 6 pages for vmemmap pages and restore the previous mapping relationship.
Apart from 2MB HugeTLB page, we also have 1GB HugeTLB page. It is similar to the 2MB HugeTLB page. We also can use this approach to free the vmemmap pages.
In this case, for the 1GB HugeTLB page, we can save 4094 pages. This is a very substantial gain. On our server, run some SPDK/QEMU applications which will use 1024GB HugeTLB page. With this feature enabled, we can save ~16GB (1G hugepage)/~12GB (2MB hugepage) memory.
Because there are vmemmap page tables reconstruction on the freeing/allocating path, it increases some overhead. Here are some overhead analysis.
1) Allocating 10240 2MB HugeTLB pages.
a) With this patch series applied:
# time echo 10240 > /proc/sys/vm/nr_hugepages
real 0m0.166s user 0m0.000s sys 0m0.166s
# bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; }
kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency = hist(nsecs -
@start[tid]); delete(@start[tid]); }'
Attaching 2 probes...@latency:
[8K, 16K) 5476 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [16K, 32K) 4760 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [32K, 64K) 4 | |
b) Without this patch series:
# time echo 10240 > /proc/sys/vm/nr_hugepages
real 0m0.067s user 0m0.000s sys 0m0.067s
# bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; }
kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency = hist(nsecs -
@start[tid]); delete(@start[tid]); }'
Attaching 2 probes...@latency:
[4K, 8K) 10147 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [8K, 16K) 93 | |
Summarize: this feature is about ~2x slower than before.
2) Freeing 10240 2MB HugeTLB pages.
a) With this patch series applied:
# time echo 0 > /proc/sys/vm/nr_hugepages
real 0m0.213s user 0m0.000s sys 0m0.213s
# bpftrace -e 'kprobe:free_pool_huge_page { @start[tid] = nsecs; }
kretprobe:free_pool_huge_page /@start[tid]/ { @latency = hist(nsecs -
@start[tid]); delete(@start[tid]); }'
Attaching 2 probes...@latency:
[8K, 16K) 6 | | [16K, 32K) 10227 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [32K, 64K) 7 | |
b) Without this patch series:
# time echo 0 > /proc/sys/vm/nr_hugepages
real 0m0.081s user 0m0.000s sys 0m0.081s
# bpftrace -e 'kprobe:free_pool_huge_page { @start[tid] = nsecs; }
kretprobe:free_pool_huge_page /@start[tid]/ { @latency = hist(nsecs -
@start[tid]); delete(@start[tid]); }'
Attaching 2 probes...@latency:
[4K, 8K) 6805 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [8K, 16K) 3427 |@@@@@@@@@@@@@@@@@@@@@@@@@@ | [16K, 32K) 8 | |
Summary: The overhead of __free_hugepage is about ~2-3x slower than before.
Although the overhead has increased, the overhead is not significant. Like Mike said, "However, remember that the majority of use cases create HugeTLB pages at or shortly after boot time and add them to the pool. So, additional overhead is at pool creation time. There is no change to 'normal run time' operations of getting a page from or returning a page to the pool (think page fault/unmap)".
Despite the overhead and in addition to the memory gains from this series. The following data is obtained by Joao Martins. Very thanks to his effort.
There's an additional benefit which is page (un)pinners will see an improvement and Joao presumes because there are fewer memmap pages and thus the tail/head pages are staying in cache more often.
Out of the box Joao saw (when comparing linux-next against linux-next + this series) with gup_test and pinning a 16G HugeTLB file (with 1G pages):
get_user_pages(): ~32k -> ~9k unpin_user_pages(): ~75k -> ~70k
Usually any tight loop fetching compound_head(), or reading tail pages data (e.g. compound_head) benefit a lot. There's some unpinning inefficiencies Joao was fixing[2], but with that in added it shows even more:
unpin_user_pages(): ~27k -> ~3.8k
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
This patch (of 9):
Move bootmem info registration common API to individual bootmem_info.c. And we will use {get,put}_page_bootmem() to initialize the page for the vmemmap pages or free the vmemmap pages to buddy in the later patch. So move them out of CONFIG_MEMORY_HOTPLUG_SPARSE. This is just code movement without any functional change.
---
arch/sparc/mm/init_64.c | 1 arch/x86/mm/init_64.c | 3 include/linux/bootmem_info.h | 40 +++++++++ include/linux/memory_hotplug.h | 27 ------ mm/Makefile | 1 mm/bootmem_info.c | 127 +++++++++++++++++++++++++++++++ mm/memory_hotplug.c | 116 ---------------------------- mm/sparse.c | 1 8 files changed, 172 insertions(+), 144 deletions(-)
arch/sparc/mm/init_64.c renamed from arch/sparc/mm/init_64.c~mm-memory_hotplug-factor-out-bootmem-core-functions-to-bootmem_infoc +1 -0
@@ -27,6 +27,7 @@#include <linux/percpu.h> #include <linux/mmzone.h> #include <linux/gfp.h> +#include <linux/bootmem_info.h> #include <asm/head.h> #include <asm/page.h>
arch/x86/mm/init_64.c renamed from arch/x86/mm/init_64.c~mm-memory_hotplug-factor-out-bootmem-core-functions-to-bootmem_infoc +2 -1
@@ -33,6 +33,7 @@#include <linux/nmi.h> #include <linux/gfp.h> #include <linux/kcore.h> +#include <linux/bootmem_info.h> #include <asm/processor.h> #include <asm/bios_ebda.h>
@@ -1623,7 +1624,7 @@ int __meminit vmemmap_populate(unsignedreturn err; } -#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE ) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE ) +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void register_page_bootmem_memmap (unsigned long section_nr,struct page *start_page,unsigned long nr_pages) {
include/linux/bootmem_info.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_BOOTMEM_INFO_H +#define __LINUX_BOOTMEM_INFO_H + +#include <linux/mmzone.h> + +/* +* Types for free bootmem stored in page->lru.next. These have to be in +* some random range in unsigned long space for debugging purposes. +*/ +enum { +MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE =12 , +SECTION_INFO =MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE , +MIX_SECTION_INFO , +NODE_INFO , +MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE =NODE_INFO , +}; + +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE +void __init register_page_bootmem_info_node (struct pglist_data *pgdat); + +void get_page_bootmem (unsigned long info,struct page *page, +unsigned long type); +void put_page_bootmem (struct page *page); +#else +static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) +{ +} + +static inline void put_page_bootmem (struct page *page) +{ +} + +static inline void get_page_bootmem (unsigned long info,struct page *page, +unsigned long type) +{ +} +#endif + +#endif /* __LINUX_BOOTMEM_INFO_H */
include/linux/memory_hotplug.h renamed from include/linux/memory_hotplug.h~mm-memory_hotplug-factor-out-bootmem-core-functions-to-bootmem_infoc +0 -27
@@ -18,18 +18,6 @@ struct vmem_altmap;#ifdef CONFIG_MEMORY_HOTPLUG struct page *pfn_to_online_page (unsigned long pfn); -/* -* Types for free bootmem stored in page->lru.next. These have to be in -* some random range in unsigned long space for debugging purposes. -*/ -enum { -MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE =12 , -SECTION_INFO =MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE , -MIX_SECTION_INFO , -NODE_INFO , -MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE =NODE_INFO , -}; -/* Types for control the zone type of onlined and offlined memory */ enum { /* Offline the memory. */
@@ -222,17 +210,6 @@ static inline void arch_refresh_nodedata#endif /* CONFIG_NUMA */ #endif /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */ -#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE -extern void __init register_page_bootmem_info_node (struct pglist_data *pgdat); -#else -static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) -{ -} -#endif -extern void put_page_bootmem (struct page *page); -extern void get_page_bootmem (unsigned long ingo,struct page *page, -unsigned long type); -void get_online_mems (void );void put_online_mems (void );
@@ -260,10 +237,6 @@ static inline void zone_span_writelock(sstatic inline void zone_span_writeunlock (struct zone *zone) {}static inline void zone_seqlock_init (struct zone *zone) {} -static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) -{ -} -static inline int try_online_node (int nid) {return 0 ;
mm/bootmem_info.c new file +127 -0
127 lines not on this page · raw message
mm/Makefile renamed from mm/Makefile~mm-memory_hotplug-factor-out-bootmem-core-functions-to-bootmem_infoc +1 -0
@@ -125,3 +125,4 @@ obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += m obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o obj-$(CONFIG_IO_MAPPING) += io-mapping.o +obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
mm/memory_hotplug.c renamed from mm/memory_hotplug.c~mm-memory_hotplug-factor-out-bootmem-core-functions-to-bootmem_infoc +0 -116
122 lines not on this page · raw message
mm/sparse.c renamed from mm/sparse.c~mm-memory_hotplug-factor-out-bootmem-core-functions-to-bootmem_infoc +1 -0
@@ -13,6 +13,7 @@#include <linux/vmalloc.h> #include <linux/swap.h> #include <linux/swapops.h> +#include <linux/bootmem_info.h> #include "internal.h" #include <asm/dma.h>
_
Andrew Morton
maintainerPATCH 2/192 mm: hugetlb: introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
The option HUGETLB_PAGE_FREE_VMEMMAP allows for the freeing of some vmemmap pages associated with pre-allocated HugeTLB pages. For example, on X86_64 6 vmemmap pages of size 4KB each can be saved for each 2MB HugeTLB page. 4094 vmemmap pages of size 4KB each can be saved for each 1GB HugeTLB page.
When a HugeTLB page is allocated or freed, the vmemmap array representing the range associated with the page will need to be remapped. When a page is allocated, vmemmap pages are freed after remapping. When a page is freed, previously discarded vmemmap pages must be allocated before remapping.
The config option is introduced early so that supporting code can be written to depend on the option. The initial version of the code only provides support for x86-64.
If config HAVE_BOOTMEM_INFO_NODE is enabled, the freeing vmemmap page code denpend on it to free vmemmap pages. Otherwise, just use free_reserved_page() to free vmemmmap pages. The routine register_page_bootmem_info() is used to register bootmem info. Therefore, make sure register_page_bootmem_info is enabled if HUGETLB_PAGE_FREE_VMEMMAP is defined.
---
arch/x86/mm/init_64.c | 2 +- fs/Kconfig | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/mm/init_64.c renamed from arch/x86/mm/init_64.c~mm-hugetlb-introduce-a-new-config-hugetlb_page_free_vmemmap +1 -1
@@ -1270,7 +1270,7 @@ static struct kcore_list kcore_vsyscall;static void __init register_page_bootmem_info (void ) { -#ifdef CONFIG_NUMA +#if defined(CONFIG_NUMA ) || defined(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP )int i;for_each_online_node (i)
fs/Kconfig renamed from fs/Kconfig~mm-hugetlb-introduce-a-new-config-hugetlb_page_free_vmemmap +5 -0
@@ -240,6 +240,11 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + def_bool HUGETLB_PAGE + depends on X86_64 + depends on SPARSEMEM_VMEMMAP + config MEMFD_CREATE def_bool TMPFS || HUGETLBFS
_
Andrew Morton
maintainerPATCH 3/192 mm: hugetlb: gather discrete indexes of tail page
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: gather discrete indexes of tail page
For HugeTLB page, there are more metadata to save in the struct page. But the head struct page cannot meet our needs, so we have to abuse other tail struct page to store the metadata. In order to avoid conflicts caused by subsequent use of more tail struct pages, we can gather these discrete indexes of tail struct page. In this case, it will be easier to add a new tail page index later.
---
include/linux/hugetlb.h | 21 +++++++++++++++++++-- include/linux/hugetlb_cgroup.h | 19 +++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-)
include/linux/hugetlb_cgroup.h renamed from include/linux/hugetlb_cgroup.h~mm-hugetlb-gather-discrete-indexes-of-tail-page +11 -8
@@ -21,15 +21,16 @@ struct hugetlb_cgroup;struct resv_map ;struct file_region ; +#ifdef CONFIG_CGROUP_HUGETLB /* * Minimum page order trackable by hugetlb cgroup. * At least 4 pages are necessary for all the tracking information. -* The second tail page (hpage[2]) is the fault usage cgroup. -* The third tail page (hpage[3]) is the reservation usage cgroup. +* The second tail page (hpage[SUBPAGE_INDEX_CGROUP]) is the fault +* usage cgroup. The third tail page (hpage[SUBPAGE_INDEX_CGROUP_RSVD]) +* is the reservation usage cgroup. */ -#define HUGETLB_CGROUP_MIN_ORDER 2 +#define HUGETLB_CGROUP_MIN_ORDER order_base_2(__MAX_CGROUP_SUBPAGE_INDEX + 1) -#ifdef CONFIG_CGROUP_HUGETLB enum hugetlb_memory_event {HUGETLB_MAX , HUGETLB_NR_MEMORY_EVENTS,
@@ -66,9 +67,9 @@ __hugetlb_cgroup_from_page(struct page *if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return NULL ;if (rsvd) -return (struct hugetlb_cgroup *)page[3 ].private; +return (void *)page_private (page +SUBPAGE_INDEX_CGROUP_RSVD );else -return (struct hugetlb_cgroup *)page[2 ].private; +return (void *)page_private (page +SUBPAGE_INDEX_CGROUP ); }static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page)
@@ -90,9 +91,11 @@ static inline int __set_hugetlb_cgroup(sif (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return -1 ;if (rsvd) - page[3 ].private = (unsigned long )h_cg; +set_page_private (page +SUBPAGE_INDEX_CGROUP_RSVD , + (unsigned long )h_cg);else - page[2 ].private = (unsigned long )h_cg; +set_page_private (page +SUBPAGE_INDEX_CGROUP , + (unsigned long )h_cg);return 0 ; }
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-hugetlb-gather-discrete-indexes-of-tail-page +19 -2
@@ -29,6 +29,23 @@ typedef struct { unsigned long pd; } hug#include <linux/shm.h> #include <asm/tlbflush.h> +/* +* For HugeTLB page, there are more metadata to save in the struct page. But +* the head struct page cannot meet our needs, so we have to abuse other tail +* struct page to store the metadata. In order to avoid conflicts caused by +* subsequent use of more tail struct pages, we gather these discrete indexes +* of tail struct page here. +*/ +enum { +SUBPAGE_INDEX_SUBPOOL =1 ,/* reuse page->private */ +#ifdef CONFIG_CGROUP_HUGETLB +SUBPAGE_INDEX_CGROUP ,/* reuse page->private */ +SUBPAGE_INDEX_CGROUP_RSVD ,/* reuse page->private */ + __MAX_CGROUP_SUBPAGE_INDEX =SUBPAGE_INDEX_CGROUP_RSVD , +#endif + __NR_USED_SUBPAGE, +}; +struct hugepage_subpool {spinlock_t lock;long count;
@@ -635,13 +652,13 @@ extern unsigned int default_hstate_idx;*/ static inline struct hugepage_subpool *hugetlb_page_subpool (struct page *hpage) { -return (struct hugepage_subpool *)(hpage+1 )->private; +return (void *)page_private (hpage +SUBPAGE_INDEX_SUBPOOL ); }static inline void hugetlb_set_page_subpool (struct page *hpage,struct hugepage_subpool *subpool) { -set_page_private (hpage+1 , (unsigned long )subpool); +set_page_private (hpage +SUBPAGE_INDEX_SUBPOOL , (unsigned long )subpool); }static inline struct hstate *hstate_file (struct file *f)
_
Andrew Morton
maintainerPATCH 4/192 mm: hugetlb: free the vmemmap pages associated with each HugeTLB page
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: free the vmemmap pages associated with each HugeTLB page
Every HugeTLB has more than one struct page structure. We __know__ that we only use the first 4 (__NR_USED_SUBPAGE) struct page structures to store metadata associated with each HugeTLB.
There are a lot of struct page structures associated with each HugeTLB page. For tail pages, the value of compound_head is the same. So we can reuse first page of tail page structures. We map the virtual addresses of the remaining pages of tail page structures to the first tail page struct, and then free these page frames. Therefore, we need to reserve two pages as vmemmap areas.
When we allocate a HugeTLB page from the buddy, we can free some vmemmap pages associated with each HugeTLB page. It is more appropriate to do it in the prep_new_huge_page().
The free_vmemmap_pages_per_hpage(), which indicates how many vmemmap pages associated with a HugeTLB page can be freed, returns zero for now, which means the feature is disabled. We will enable it once all the infrastructure is there.
---
include/linux/bootmem_info.h | 28 ++++ include/linux/mm.h | 3 mm/Makefile | 1 mm/hugetlb.c | 22 +-- mm/hugetlb_vmemmap.c | 218 +++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 20 +++ mm/sparse-vmemmap.c | 194 +++++++++++++++++++++++++++++ 7 files changed, 473 insertions(+), 13 deletions(-)
include/linux/bootmem_info.h renamed from include/linux/bootmem_info.h~mm-hugetlb-free-the-vmemmap-pages-associated-with-each-hugetlb-page +27 -1
@@ -2,7 +2,7 @@#ifndef __LINUX_BOOTMEM_INFO_H#define __LINUX_BOOTMEM_INFO_H -#include <linux/mmzone.h> +#include <linux/mm.h> /* * Types for free bootmem stored in page->lru.next. These have to be in
@@ -22,6 +22,27 @@ void __init register_page_bootmem_info_nvoid get_page_bootmem (unsigned long info,struct page *page,unsigned long type);void put_page_bootmem (struct page *page); + +/* +* Any memory allocated via the memblock allocator and not via the +* buddy will be marked reserved already in the memmap. For those +* pages, we can call this function to free it to buddy allocator. +*/ +static inline void free_bootmem_page (struct page *page) +{ +unsigned long magic = (unsigned long )page->freelist; + +/* +* The reserve_bootmem_region sets the reserved flag on bootmem +* pages. +*/ +VM_BUG_ON_PAGE (page_ref_count (page) !=2 , page); + +if (magic ==SECTION_INFO || magic ==MIX_SECTION_INFO ) +put_page_bootmem (page); +else +VM_BUG_ON_PAGE (1 , page); +}#else static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) {
@@ -35,6 +56,11 @@ static inline void get_page_bootmem(unsi unsigned long type) { } + +static inline void free_bootmem_page (struct page *page) +{ +free_reserved_page (page); +}#endif #endif /* __LINUX_BOOTMEM_INFO_H */
include/linux/mm.h renamed from include/linux/mm.h~mm-hugetlb-free-the-vmemmap-pages-associated-with-each-hugetlb-page +3 -0
@@ -3076,6 +3076,9 @@ static inline void print_vma_addr(char * }#endif +void vmemmap_remap_free (unsigned long start,unsigned long end, +unsigned long reuse); +void *sparse_buffer_alloc (unsigned long size);struct page *__populate_section_memmap (unsigned long pfn,unsigned long nr_pages,int nid,struct vmem_altmap *altmap);
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-free-the-vmemmap-pages-associated-with-each-hugetlb-page +10 -12
@@ -41,6 +41,7 @@#include <linux/node.h> #include <linux/page_owner.h> #include "internal.h" +#include "hugetlb_vmemmap.h" int hugetlb_max_hstate__read_mostly ;unsigned int default_hstate_idx;
@@ -1493,8 +1494,9 @@ static void __prep_account_new_huge_page h->nr_huge_pages_node[nid]++; } -static void __prep_new_huge_page (struct page *page) +static void __prep_new_huge_page (struct hstate *h,struct page *page) { +free_huge_page_vmemmap (h, page);INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );hugetlb_set_page_subpool (page,NULL );
@@ -1504,7 +1506,7 @@ static void __prep_new_huge_page(structstatic void prep_new_huge_page (struct hstate *h,struct page *page,int nid) { -__prep_new_huge_page (page); +__prep_new_huge_page (h, page);spin_lock_irq (&hugetlb_lock);__prep_account_new_huge_page (h, nid);spin_unlock_irq (&hugetlb_lock);
@@ -2351,14 +2353,15 @@ static int alloc_and_dissolve_huge_page(/* * Before dissolving the page, we need to allocate a new one for the -* pool to remain stable. Using alloc_buddy_huge_page() allows us to -* not having to deal with prep_new_huge_page() and avoids dealing of any -* counters. This simplifies and let us do the whole thing under the -* lock. +* pool to remain stable. Here, we allocate the page and 'prep' it +* by doing everything but actually updating counters and adding to +* the pool. This simplifies and let us do most of the processing +* under the lock. */ new_page =alloc_buddy_huge_page (h, gfp_mask, nid,NULL ,NULL );if (!new_page)return -ENOMEM ; +__prep_new_huge_page (h, new_page); retry:spin_lock_irq (&hugetlb_lock);
@@ -2397,14 +2400,9 @@ retry:remove_hugetlb_page (h, old_page, false);/* -* new_page needs to be initialized with the standard hugetlb -* state. This is normally done by prep_new_huge_page() but -* that takes hugetlb_lock which is already held so we need to -* open code it here. * Reference count trick is needed because allocator gives us * referenced page but the pool requires pages with 0 refcount. */ -__prep_new_huge_page (new_page);__prep_account_new_huge_page (h, nid);page_ref_dec (new_page);enqueue_huge_page (h, new_page);
@@ -2420,7 +2418,7 @@ retry: free_new:spin_unlock_irq (&hugetlb_lock); -__free_pages (new_page,huge_page_order (h)); +update_and_free_page (h, new_page);return ret; }
mm/hugetlb_vmemmap.c new file +218 -0
218 lines not on this page · raw message
mm/hugetlb_vmemmap.h new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Free some vmemmap pages of HugeTLB +* +* Copyright (c) 2020, Bytedance. All rights reserved. +* +* Author: Muchun Song <[email protected]> +*/ +#ifndef _LINUX_HUGETLB_VMEMMAP_H +#define _LINUX_HUGETLB_VMEMMAP_H +#include <linux/hugetlb.h> + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +void free_huge_page_vmemmap (struct hstate *h,struct page *head); +#else +static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +} +#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ +#endif /* _LINUX_HUGETLB_VMEMMAP_H */
mm/Makefile renamed from mm/Makefile~mm-hugetlb-free-the-vmemmap-pages-associated-with-each-hugetlb-page +1 -0
@@ -75,6 +75,7 @@ obj-$(CONFIG_FRONTSWAP) += frontswap.o obj-$(CONFIG_ZSWAP) += zswap.o obj-$(CONFIG_HAS_DMA) += dmapool.o obj-$(CONFIG_HUGETLBFS) += hugetlb.o +obj-$(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP) += hugetlb_vmemmap.o obj-$(CONFIG_NUMA) += mempolicy.o obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o
mm/sparse-vmemmap.c renamed from mm/sparse-vmemmap.c~mm-hugetlb-free-the-vmemmap-pages-associated-with-each-hugetlb-page +194 -0
202 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 5/192 mm: hugetlb: defer freeing of HugeTLB pages
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: defer freeing of HugeTLB pages
In the subsequent patch, we should allocate the vmemmap pages when freeing a HugeTLB page. But update_and_free_page() can be called under any context, so we cannot use GFP_KERNEL to allocate vmemmap pages. However, we can defer the actual freeing in a kworker to prevent from using GFP_ATOMIC to allocate the vmemmap pages.
The __update_and_free_page() is where the call to allocate vmemmmap pages will be inserted.
---
mm/hugetlb.c | 83 +++++++++++++++++++++++++++++++++++++---- mm/hugetlb_vmemmap.c | 12 ----- mm/hugetlb_vmemmap.h | 17 ++++++++ 3 files changed, 93 insertions(+), 19 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-defer-freeing-of-hugetlb-pages +76 -7
@@ -1376,7 +1376,7 @@ static void remove_hugetlb_page(struct h h->nr_huge_pages_node[nid]--; } -static void update_and_free_page (struct hstate *h,struct page *page) +static void __update_and_free_page (struct hstate *h,struct page *page) {int i;struct page *subpage = page;
@@ -1399,12 +1399,79 @@ static void update_and_free_page(struct } } +/* +* As update_and_free_page() can be called under any context, so we cannot +* use GFP_KERNEL to allocate vmemmap pages. However, we can defer the +* actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate +* the vmemmap pages. +* +* free_hpage_workfn() locklessly retrieves the linked list of pages to be +* freed and frees them one-by-one. As the page->mapping pointer is going +* to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node +* structure of a lockless linked list of huge pages to be freed. +*/ +static LLIST_HEAD (hpage_freelist); + +static void free_hpage_workfn (struct work_struct *work) +{ +struct llist_node *node; + + node =llist_del_all (&hpage_freelist); + +while (node) { +struct page *page; +struct hstate *h; + + page =container_of ((struct address_space **)node, + struct page, mapping); + node = node->next; + page->mapping =NULL ; +/* +* The VM_BUG_ON_PAGE(!PageHuge(page), page) in page_hstate() +* is going to trigger because a previous call to +* remove_hugetlb_page() will set_compound_page_dtor(page, +* NULL_COMPOUND_DTOR), so do not use page_hstate() directly. +*/ + h =size_to_hstate (page_size (page)); + +__update_and_free_page (h, page); + +cond_resched (); + } +} +static DECLARE_WORK(free_hpage_work, free_hpage_workfn); + +static inline void flush_free_hpage_work (struct hstate *h) +{ +if (free_vmemmap_pages_per_hpage (h)) +flush_work (&free_hpage_work); +} + +static void update_and_free_page (struct hstate *h,struct page *page, +bool atomic) +{ +if (!free_vmemmap_pages_per_hpage (h) || !atomic) { +__update_and_free_page (h, page); +return ; + } + +/* +* Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages. +* +* Only call schedule_work() if hpage_freelist is previously +* empty. Otherwise, schedule_work() had been called but the workfn +* hasn't retrieved the list yet. +*/ +if (llist_add ((struct llist_node *)&page->mapping, &hpage_freelist)) +schedule_work (&free_hpage_work); +} +static void update_and_free_pages_bulk (struct hstate *h,struct list_head *list) {struct page *page, *t_page;list_for_each_entry_safe (page, t_page, list, lru) { -update_and_free_page (h, page); +update_and_free_page (h, page, false);cond_resched (); } }
@@ -1471,12 +1538,12 @@ void free_huge_page(struct page *page)if (HPageTemporary (page)) {remove_hugetlb_page (h, page, false);spin_unlock_irqrestore (&hugetlb_lock, flags); -update_and_free_page (h, page); +update_and_free_page (h, page, true); }else if (h->surplus_huge_pages_node[nid]) {/* remove the page from active list */ remove_hugetlb_page (h, page, true);spin_unlock_irqrestore (&hugetlb_lock, flags); -update_and_free_page (h, page); +update_and_free_page (h, page, true); }else {arch_clear_hugepage_flags (page);enqueue_huge_page (h, page);
@@ -1795,7 +1862,7 @@ retry:remove_hugetlb_page (h, head, false); h->max_huge_pages--;spin_unlock_irq (&hugetlb_lock); -update_and_free_page (h, head); +update_and_free_page (h, head, false);return 0 ; } out:
@@ -2411,14 +2478,14 @@ retry:* Pages have been replaced, we can safely free the old one. */ spin_unlock_irq (&hugetlb_lock); -update_and_free_page (h, old_page); +update_and_free_page (h, old_page, false); }return ret; free_new:spin_unlock_irq (&hugetlb_lock); -update_and_free_page (h, new_page); +update_and_free_page (h, new_page, false);return ret; }
@@ -2832,6 +2899,7 @@ static int set_max_huge_pages(struct hst* pages in hstate via the proc/sysfs interfaces. */ mutex_lock (&h->resize_lock); +flush_free_hpage_work (h);spin_lock_irq (&hugetlb_lock);/*
@@ -2941,6 +3009,7 @@ static int set_max_huge_pages(struct hst/* free the pages after dropping lock */ spin_unlock_irq (&hugetlb_lock);update_and_free_pages_bulk (h, &page_list); +flush_free_hpage_work (h);spin_lock_irq (&hugetlb_lock);while (count <persistent_huge_pages (h)) {
mm/hugetlb_vmemmap.c renamed from mm/hugetlb_vmemmap.c~mm-hugetlb-defer-freeing-of-hugetlb-pages +0 -12
@@ -180,18 +180,6 @@#define RESERVE_VMEMMAP_NR 2U#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) -/* -* How many vmemmap pages associated with a HugeTLB page that can be freed -* to the buddy allocator. -* -* Todo: Returns zero for now, which means the feature is disabled. We will -* enable it once all the infrastructure is there. -*/ -static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) -{ -return 0 ; -} -static inline unsigned long free_vmemmap_pages_size_per_hpage (struct hstate *h) {return (unsigned long )free_vmemmap_pages_per_hpage (h) <<PAGE_SHIFT ;
mm/hugetlb_vmemmap.h renamed from mm/hugetlb_vmemmap.h~mm-hugetlb-defer-freeing-of-hugetlb-pages +17 -0
@@ -12,9 +12,26 @@#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP void free_huge_page_vmemmap (struct hstate *h,struct page *head); + +/* +* How many vmemmap pages associated with a HugeTLB page that can be freed +* to the buddy allocator. +* +* Todo: Returns zero for now, which means the feature is disabled. We will +* enable it once all the infrastructure is there. +*/ +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) +{ +return 0 ; +}#else static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) { } + +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) +{ +return 0 ; +}#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */
_
Andrew Morton
maintainerPATCH 6/192 mm: hugetlb: alloc the vmemmap pages associated with each HugeTLB page
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: alloc the vmemmap pages associated with each HugeTLB page
When we free a HugeTLB page to the buddy allocator, we need to allocate the vmemmap pages associated with it. However, we may not be able to allocate the vmemmap pages when the system is under memory pressure. In this case, we just refuse to free the HugeTLB page. This changes behavior in some corner cases as listed below:
1) Failing to free a huge page triggered by the user (decrease nr_pages).
User needs to try again later.
2) Failing to free a surplus huge page when freed by the application.
Try again later when freeing a huge page next time.
3) Failing to dissolve a free huge page on ZONE_MOVABLE via offline_pages().
This can happen when we have plenty of ZONE_MOVABLE memory, but not enough kernel memory to allocate vmemmmap pages. We may even be able to migrate huge page contents, but will not be able to dissolve the source huge page. This will prevent an offline operation and is unfortunate as memory offlining is expected to succeed on movable zones. Users that depend on memory hotplug to succeed for movable zones should carefully consider whether the memory savings gained from this feature are worth the risk of possibly not being able to offline memory in certain situations.
4) Failing to dissolve a huge page on CMA/ZONE_MOVABLE via alloc_contig_range() - once we have that handling in place. Mainly affects CMA and virtio-mem.
Similar to 3). virito-mem will handle migration errors gracefully. CMA might be able to fallback on other free areas within the CMA region.
Vmemmap pages are allocated from the page freeing context. In order for those allocations to be not disruptive (e.g. trigger oom killer) __GFP_NORETRY is used. hugetlb_lock is dropped for the allocation because a non sleeping allocation would be too fragile and it could fail too easily under memory pressure. GFP_ATOMIC or other modes to access memory reserves is not used because we want to prevent consuming reserves under heavy hugetlb freeing.
---
Documentation/admin-guide/mm/hugetlbpage.rst | 8 + Documentation/admin-guide/mm/memory-hotplug.rst | 13 + include/linux/hugetlb.h | 3 include/linux/mm.h | 2 mm/hugetlb.c | 98 +++++++++++--- mm/hugetlb_vmemmap.c | 34 ++++ mm/hugetlb_vmemmap.h | 6 mm/migrate.c | 5 mm/sparse-vmemmap.c | 75 ++++++++++ 9 files changed, 227 insertions(+), 17 deletions(-)
Documentation/admin-guide/mm/hugetlbpage.rst renamed from Documentation/admin-guide/mm/hugetlbpage.rst~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +8 -0
@@ -60,6 +60,10 @@ HugePages_Surp the pool above the value in ``/proc/sys/vm/nr_hugepages``. The maximum number of surplus huge pages is controlled by ``/proc/sys/vm/nr_overcommit_hugepages``. + Note: When the feature of freeing unused vmemmap pages associated + with each hugetlb page is enabled, the number of surplus huge pages + may be temporarily larger than the maximum number of surplus huge + pages when the system is under memory pressure. Hugepagesize is the default hugepage size (in Kb). Hugetlb
@@ -80,6 +84,10 @@ returned to the huge page pool when free privileges can dynamically allocate more or free some persistent huge pages by increasing or decreasing the value of ``nr_hugepages``. +Note: When the feature of freeing unused vmemmap pages associated with each +hugetlb page is enabled, we can fail to free the huge pages triggered by +the user when ths system is under memory pressure. Please try again later. + Pages that are used as huge pages are reserved inside the kernel and cannot be used for other purposes. Huge pages cannot be swapped out under memory pressure.
Documentation/admin-guide/mm/memory-hotplug.rst renamed from Documentation/admin-guide/mm/memory-hotplug.rst~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +13 -0
@@ -357,6 +357,19 @@ creates ZONE_MOVABLE as following. Unfortunately, there is no information to show which memory block belongs to ZONE_MOVABLE. This is TBD. + Memory offlining can fail when dissolving a free huge page on ZONE_MOVABLE + and the feature of freeing unused vmemmap pages associated with each hugetlb + page is enabled. + + This can happen when we have plenty of ZONE_MOVABLE memory, but not enough + kernel memory to allocate vmemmmap pages. We may even be able to migrate + huge page contents, but will not be able to dissolve the source huge page. + This will prevent an offline operation and is unfortunate as memory offlining + is expected to succeed on movable zones. Users that depend on memory hotplug + to succeed for movable zones should carefully consider whether the memory + savings gained from this feature are worth the risk of possibly not being + able to offline memory in certain situations. + .. note:: Techniques that rely on long-term pinnings of memory (especially, RDMA and vfio) are fundamentally problematic with ZONE_MOVABLE and, therefore, memory
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +3 -0
@@ -532,12 +532,14 @@ unsigned long hugetlb_get_unmapped_area(* modifications require hugetlb_lock. * HPG_freed - Set when page is on the free lists. * Synchronization: hugetlb_lock held for examination and modification. +* HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed. */ enum hugetlb_page_flags { HPG_restore_reserve =0 , HPG_migratable, HPG_temporary, HPG_freed, + HPG_vmemmap_optimized, __NR_HPAGEFLAGS, };
@@ -583,6 +585,7 @@ HPAGEFLAG(RestoreReserve, restore_reserv HPAGEFLAG(Migratable, migratable) HPAGEFLAG(Temporary, temporary) HPAGEFLAG(Freed, freed) +HPAGEFLAG(VmemmapOptimized, vmemmap_optimized)#ifdef CONFIG_HUGETLB_PAGE
include/linux/mm.h renamed from include/linux/mm.h~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +2 -0
@@ -3078,6 +3078,8 @@ static inline void print_vma_addr(char *void vmemmap_remap_free (unsigned long start,unsigned long end,unsigned long reuse); +int vmemmap_remap_alloc (unsigned long start,unsigned long end, +unsigned long reuse,gfp_t gfp_mask);void *sparse_buffer_alloc (unsigned long size); struct page * __populate_section_memmap(unsigned long pfn,
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +83 -15
131 lines not on this page · raw message
mm/hugetlb_vmemmap.c renamed from mm/hugetlb_vmemmap.c~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +34 -0
@@ -185,6 +185,38 @@ static inline unsigned long free_vmemmapreturn (unsigned long )free_vmemmap_pages_per_hpage (h) <<PAGE_SHIFT ; } +/* +* Previously discarded vmemmap pages will be allocated and remapping +* after this function returns zero. +*/ +int alloc_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +int ret; +unsigned long vmemmap_addr = (unsigned long )head; +unsigned long vmemmap_end, vmemmap_reuse; + +if (!HPageVmemmapOptimized (head)) +return 0 ; + + vmemmap_addr +=RESERVE_VMEMMAP_SIZE ; + vmemmap_end = vmemmap_addr +free_vmemmap_pages_size_per_hpage (h); + vmemmap_reuse = vmemmap_addr -PAGE_SIZE ; +/* +* The pages which the vmemmap virtual address range [@vmemmap_addr, +* @vmemmap_end) are mapped to are freed to the buddy allocator, and +* the range is mapped to the page which @vmemmap_reuse is mapped to. +* When a HugeTLB page is freed to the buddy allocator, previously +* discarded vmemmap pages must be allocated and remapping. +*/ + ret =vmemmap_remap_alloc (vmemmap_addr, vmemmap_end, vmemmap_reuse, +GFP_KERNEL | __GFP_NORETRY | __GFP_THISNODE); + +if (!ret) +ClearHPageVmemmapOptimized (head); + +return ret; +} +void free_huge_page_vmemmap (struct hstate *h,struct page *head) {unsigned long vmemmap_addr = (unsigned long )head;
@@ -203,4 +235,6 @@ void free_huge_page_vmemmap(struct hstat* which the range [@vmemmap_addr, @vmemmap_end] is mapped to. */ vmemmap_remap_free (vmemmap_addr, vmemmap_end, vmemmap_reuse); + +SetHPageVmemmapOptimized (head); }
mm/hugetlb_vmemmap.h renamed from mm/hugetlb_vmemmap.h~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +6 -0
@@ -11,6 +11,7 @@#include <linux/hugetlb.h> #ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +int alloc_huge_page_vmemmap (struct hstate *h,struct page *head);void free_huge_page_vmemmap (struct hstate *h,struct page *head);/*
@@ -25,6 +26,11 @@ static inline unsigned int free_vmemmap_ return 0; }#else +static inline int alloc_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +return 0 ; +} +static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) { }
mm/migrate.c renamed from mm/migrate.c~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +4 -1
@@ -626,7 +626,10 @@ void migrate_page_states(struct page *neif (PageSwapCache (page))ClearPageSwapCache (page);ClearPagePrivate (page); -set_page_private (page,0 ); + +/* page->private contains hugetlb specific flags */ +if (!PageHuge (page)) +set_page_private (page,0 );/* * If any waiters have accumulated on the new page then
mm/sparse-vmemmap.c renamed from mm/sparse-vmemmap.c~mm-hugetlb-alloc-the-vmemmap-pages-associated-with-each-hugetlb-page +74 -1
87 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 7/192 mm: hugetlb: add a kernel parameter hugetlb_free_vmemmap
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: add a kernel parameter hugetlb_free_vmemmap
Add a kernel parameter hugetlb_free_vmemmap to enable the feature of freeing unused vmemmap pages associated with each hugetlb page on boot.
We disable PMD mapping of vmemmap pages for x86-64 arch when this feature is enabled. Because vmemmap_remap_free() depends on vmemmap being base page mapped.
---
Documentation/admin-guide/kernel-parameters.txt | 17 +++++++++ Documentation/admin-guide/mm/hugetlbpage.rst | 3 + arch/x86/mm/init_64.c | 8 +++- include/linux/hugetlb.h | 19 +++++++++++ mm/hugetlb_vmemmap.c | 24 ++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-)
arch/x86/mm/init_64.c renamed from arch/x86/mm/init_64.c~mm-hugetlb-add-a-kernel-parameter-hugetlb_free_vmemmap +6 -2
@@ -34,6 +34,7 @@#include <linux/gfp.h> #include <linux/kcore.h> #include <linux/bootmem_info.h> +#include <linux/hugetlb.h> #include <asm/processor.h> #include <asm/bios_ebda.h>
@@ -1609,7 +1610,8 @@ int __meminit vmemmap_populate(unsignedVM_BUG_ON (!IS_ALIGNED (start,PAGE_SIZE ));VM_BUG_ON (!IS_ALIGNED (end,PAGE_SIZE )); -if (end - start <PAGES_PER_SECTION *sizeof (struct page )) +if ((is_hugetlb_free_vmemmap_enabled () && !altmap) || + end - start <PAGES_PER_SECTION *sizeof (struct page )) err =vmemmap_populate_basepages (start, end, node,NULL );else if (boot_cpu_has (X86_FEATURE_PSE )) err =vmemmap_populate_hugepages (start, end, node, altmap);
@@ -1637,6 +1639,8 @@ void register_page_bootmem_memmap(unsignpmd_t *pmd;unsigned int nr_pmd_pages;struct page *page; +bool base_mapping = !boot_cpu_has (X86_FEATURE_PSE ) || +is_hugetlb_free_vmemmap_enabled ();for (; addr < end; addr = next) {pte_t *pte =NULL ;
@@ -1662,7 +1666,7 @@ void register_page_bootmem_memmap(unsign }get_page_bootmem (section_nr,pud_page (*pud),MIX_SECTION_INFO ); -if (!boot_cpu_has (X86_FEATURE_PSE )) { +if (base_mapping) { next = (addr +PAGE_SIZE ) &PAGE_MASK ; pmd =pmd_offset (pud, addr);if (pmd_none (*pmd))
Documentation/admin-guide/kernel-parameters.txt renamed from Documentation/admin-guide/kernel-parameters.txt~mm-hugetlb-add-a-kernel-parameter-hugetlb_free_vmemmap +17 -0
@@ -1567,6 +1567,23 @@ Documentation/admin-guide/mm/hugetlbpage.rst. Format: size[KMG] + hugetlb_free_vmemmap= + [KNL] Reguires CONFIG_HUGETLB_PAGE_FREE_VMEMMAP + enabled. + Allows heavy hugetlb users to free up some more + memory (6 * PAGE_SIZE for each 2MB hugetlb page). + This feauture is not free though. Large page + tables are not used to back vmemmap pages which + can lead to a performance degradation for some + workloads. Also there will be memory allocation + required when hugetlb pages are freed from the + pool which can lead to corner cases under heavy + memory pressure. + Format: { on | off (default) } + + on: enable the feature + off: disable the feature + hung_task_panic= [KNL] Should the hung task detector generate panics. Format: 0 | 1
Documentation/admin-guide/mm/hugetlbpage.rst renamed from Documentation/admin-guide/mm/hugetlbpage.rst~mm-hugetlb-add-a-kernel-parameter-hugetlb_free_vmemmap +3 -0
@@ -153,6 +153,9 @@ default_hugepagesz will all result in 256 2M huge pages being allocated. Valid default huge page size is architecture dependent. +hugetlb_free_vmemmap + When CONFIG_HUGETLB_PAGE_FREE_VMEMMAP is set, this enables freeing + unused vmemmap pages associated with each HugeTLB page. When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages`` indicates the current number of pre-allocated huge pages of the default size.
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-hugetlb-add-a-kernel-parameter-hugetlb_free_vmemmap +19 -0
@@ -892,6 +892,20 @@ static inline void huge_ptep_modify_prot }#endif +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +extern bool hugetlb_free_vmemmap_enabled; + +static inline bool is_hugetlb_free_vmemmap_enabled (void ) +{ +return hugetlb_free_vmemmap_enabled; +} +#else +static inline bool is_hugetlb_free_vmemmap_enabled (void ) +{ +return false; +} +#endif +#else /* CONFIG_HUGETLB_PAGE */ struct hstate {};
@@ -1046,6 +1060,11 @@ static inline void set_huge_swap_pte_at( pte_t *ptep, pte_t pte, unsigned long sz) { } + +static inline bool is_hugetlb_free_vmemmap_enabled (void ) +{ +return false; +}#endif /* CONFIG_HUGETLB_PAGE */ static inline spinlock_t *huge_pte_lock(struct hstate *h,
mm/hugetlb_vmemmap.c renamed from mm/hugetlb_vmemmap.c~mm-hugetlb-add-a-kernel-parameter-hugetlb_free_vmemmap +24 -0
@@ -168,6 +168,8 @@* (last) level. So this type of HugeTLB page can be optimized only when its * size of the struct page structs is greater than 2 pages. */ +#define pr_fmt (fmt) "HugeTLB: " fmt +#include "hugetlb_vmemmap.h" /*
@@ -180,6 +182,28 @@#define RESERVE_VMEMMAP_NR 2U#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) +bool hugetlb_free_vmemmap_enabled; + +static int __init early_hugetlb_free_vmemmap_param (char *buf) +{ +/* We cannot optimize if a "struct page" crosses page boundaries. */ +if ((!is_power_of_2 (sizeof (struct page )))) { +pr_warn ("cannot free vmemmap pages because \"struct page\" crosses page boundaries\n" ); +return 0 ; + } + +if (!buf) +return -EINVAL ; + +if (!strcmp (buf,"on" )) + hugetlb_free_vmemmap_enabled = true; +else if (strcmp (buf,"off" )) +return -EINVAL ; + +return 0 ; +} +early_param ("hugetlb_free_vmemmap" , early_hugetlb_free_vmemmap_param); +static inline unsigned long free_vmemmap_pages_size_per_hpage (struct hstate *h) {return (unsigned long )free_vmemmap_pages_per_hpage (h) <<PAGE_SHIFT ;
_
Andrew Morton
maintainerPATCH 8/192 mm: memory_hotplug: disable memmap_on_memory when hugetlb_free_vmemmap enabled
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: memory_hotplug: disable memmap_on_memory when hugetlb_free_vmemmap enabled
The parameter of memory_hotplug.memmap_on_memory is not compatible with hugetlb_free_vmemmap. So disable it when hugetlb_free_vmemmap is enabled.
---
Documentation/admin-guide/kernel-parameters.txt | 8 ++++++++ mm/memory_hotplug.c | 1 + 2 files changed, 9 insertions(+)
Documentation/admin-guide/kernel-parameters.txt renamed from Documentation/admin-guide/kernel-parameters.txt~mm-memory_hotplug-disable-memmap_on_memory-when-hugetlb_free_vmemmap-enabled +8 -0
@@ -1584,6 +1584,10 @@ on: enable the feature off: disable the feature + This is not compatible with memory_hotplug.memmap_on_memory. + If both parameters are enabled, hugetlb_free_vmemmap takes + precedence over memory_hotplug.memmap_on_memory. + hung_task_panic= [KNL] Should the hung task detector generate panics. Format: 0 | 1
@@ -2850,6 +2854,10 @@ Note that even when enabled, there are a few cases where the feature is not effective. + This is not compatible with hugetlb_free_vmemmap. If + both parameters are enabled, hugetlb_free_vmemmap takes + precedence over memory_hotplug.memmap_on_memory. + memtest= [KNL,X86,ARM,PPC,RISCV] Enable memtest Format: <integer> default : 0 <disable>
mm/memory_hotplug.c renamed from mm/memory_hotplug.c~mm-memory_hotplug-disable-memmap_on_memory-when-hugetlb_free_vmemmap-enabled +1 -0
@@ -1056,6 +1056,7 @@ bool mhp_supports_memmap_on_memory(unsig* populate a single PMD. */ return memmap_on_memory && + !is_hugetlb_free_vmemmap_enabled () &&IS_ENABLED (CONFIG_MHP_MEMMAP_ON_MEMORY ) && size ==memory_block_size_bytes () && IS_ALIGNED(vmemmap_size, PMD_SIZE) &&
_
Andrew Morton
maintainerPATCH 9/192 mm: hugetlb: introduce nr_free_vmemmap_pages in the struct hstate
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: introduce nr_free_vmemmap_pages in the struct hstate
All the infrastructure is ready, so we introduce nr_free_vmemmap_pages field in the hstate to indicate how many vmemmap pages associated with a HugeTLB page that can be freed to buddy allocator. And initialize it in the hugetlb_vmemmap_init(). This patch is actual enablement of the feature.
There are only (RESERVE_VMEMMAP_SIZE / sizeof(struct page)) struct page structs that can be used when CONFIG_HUGETLB_PAGE_FREE_VMEMMAP, so add a BUILD_BUG_ON to catch invalid usage of the tail struct page.
---
include/linux/hugetlb.h | 3 +++ mm/hugetlb.c | 1 + mm/hugetlb_vmemmap.c | 33 +++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 10 ++++++---- 4 files changed, 43 insertions(+), 4 deletions(-)
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-hugetlb-introduce-nr_free_vmemmap_pages-in-the-struct-hstate +3 -0
@@ -608,6 +608,9 @@ struct hstate {unsigned int nr_huge_pages_node[MAX_NUMNODES ];unsigned int free_huge_pages_node[MAX_NUMNODES ];unsigned int surplus_huge_pages_node[MAX_NUMNODES ]; +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +unsigned int nr_free_vmemmap_pages; +#endif #ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ struct cftype cgroup_files_dfl[7 ];
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-introduce-nr_free_vmemmap_pages-in-the-struct-hstate +1 -0
@@ -3585,6 +3585,7 @@ void __init hugetlb_add_hstate(unsigned h->next_nid_to_free = first_memory_node;snprintf (h->name,HSTATE_NAME_LEN ,"hugepages-%lukB" ,huge_page_size (h)/1024 ); +hugetlb_vmemmap_init (h); parsed_hstate = h; }
mm/hugetlb_vmemmap.c renamed from mm/hugetlb_vmemmap.c~mm-hugetlb-introduce-nr_free_vmemmap_pages-in-the-struct-hstate +33 -0
@@ -262,3 +262,36 @@ void free_huge_page_vmemmap(struct hstatSetHPageVmemmapOptimized (head); } + +void __init hugetlb_vmemmap_init (struct hstate *h) +{ +unsigned int nr_pages =pages_per_huge_page (h); +unsigned int vmemmap_pages; + +/* +* There are only (RESERVE_VMEMMAP_SIZE / sizeof(struct page)) struct +* page structs that can be used when CONFIG_HUGETLB_PAGE_FREE_VMEMMAP, +* so add a BUILD_BUG_ON to catch invalid usage of the tail struct page. +*/ +BUILD_BUG_ON (__NR_USED_SUBPAGE >= +RESERVE_VMEMMAP_SIZE /sizeof (struct page )); + +if (!hugetlb_free_vmemmap_enabled) +return ; + + vmemmap_pages = (nr_pages *sizeof (struct page )) >>PAGE_SHIFT ; +/* +* The head page and the first tail page are not to be freed to buddy +* allocator, the other pages will map to the first tail page, so they +* can be freed. +* +* Could RESERVE_VMEMMAP_NR be greater than @vmemmap_pages? It is true +* on some architectures (e.g. aarch64). See Documentation/arm64/ +* hugetlbpage.rst for more details. +*/ +if (likely (vmemmap_pages >RESERVE_VMEMMAP_NR )) + h->nr_free_vmemmap_pages = vmemmap_pages -RESERVE_VMEMMAP_NR ; + +pr_info ("can free %d vmemmap pages for %s\n" , h->nr_free_vmemmap_pages, + h->name); +}
mm/hugetlb_vmemmap.h renamed from mm/hugetlb_vmemmap.h~mm-hugetlb-introduce-nr_free_vmemmap_pages-in-the-struct-hstate +6 -4
@@ -13,17 +13,15 @@#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP int alloc_huge_page_vmemmap (struct hstate *h,struct page *head);void free_huge_page_vmemmap (struct hstate *h,struct page *head); +void hugetlb_vmemmap_init (struct hstate *h);/* * How many vmemmap pages associated with a HugeTLB page that can be freed * to the buddy allocator. -* -* Todo: Returns zero for now, which means the feature is disabled. We will -* enable it once all the infrastructure is there. */ static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) { -return 0 ; +return h->nr_free_vmemmap_pages; }#else static inline int alloc_huge_page_vmemmap (struct hstate *h,struct page *head)
@@ -35,6 +33,10 @@ static inline void free_huge_page_vmemma { } +static inline void hugetlb_vmemmap_init (struct hstate *h) +{ +} +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) {return 0 ;
_
Andrew Morton
maintainerPATCH 10/192 mm/debug_vm_pgtable: move {pmd/pud}_huge_tests out of CONFIG_TRANSPARENT_HUGEPAGE
In reply to Andrew MortonFrom: Shixin Liu <[email protected]>
Subject: mm/debug_vm_pgtable: move {pmd/pud}_huge_tests out of CONFIG_TRANSPARENT_HUGEPAGE
The functions {pmd/pud}_set_huge and {pmd/pud}_clear_huge are not dependent on THP. Hence move {pmd/pud}_huge_tests out of CONFIG_TRANSPARENT_HUGEPAGE.
---
mm/debug_vm_pgtable.c | 91 +++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 52 deletions(-)
mm/debug_vm_pgtable.c renamed from mm/debug_vm_pgtable.c~mm-debug_vm_pgtable-move-pmd-pud_huge_tests-out-of-config_transparent_hugepage +39 -52
@@ -248,29 +248,6 @@ static void __init pmd_leaf_tests(unsignWARN_ON (!pmd_leaf (pmd)); } -#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP -static void __init pmd_huge_tests (pmd_t *pmdp,unsigned long pfn,pgprot_t prot) -{ -pmd_t pmd; - -if (!arch_vmap_pmd_supported (prot)) -return ; - -pr_debug ("Validating PMD huge\n" ); -/* -* X86 defined pmd_set_huge() verifies that the given -* PMD is not a populated non-leaf entry. -*/ -WRITE_ONCE (*pmdp,__pmd (0 )); -WARN_ON (!pmd_set_huge (pmdp,__pfn_to_phys (pfn), prot)); -WARN_ON (!pmd_clear_huge (pmdp)); - pmd =READ_ONCE (*pmdp); -WARN_ON (!pmd_none (pmd)); -} -#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */ -static void __init pmd_huge_tests (pmd_t *pmdp,unsigned long pfn,pgprot_t prot) { } -#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ -static void __init pmd_savedwrite_tests (unsigned long pfn,pgprot_t prot) {pmd_t pmd;
@@ -395,30 +372,6 @@ static void __init pud_leaf_tests(unsign pud =pud_mkhuge (pud);WARN_ON (!pud_leaf (pud)); } - -#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP -static void __init pud_huge_tests (pud_t *pudp,unsigned long pfn,pgprot_t prot) -{ -pud_t pud; - -if (!arch_vmap_pud_supported (prot)) -return ; - -pr_debug ("Validating PUD huge\n" ); -/* -* X86 defined pud_set_huge() verifies that the given -* PUD is not a populated non-leaf entry. -*/ -WRITE_ONCE (*pudp,__pud (0 )); -WARN_ON (!pud_set_huge (pudp,__pfn_to_phys (pfn), prot)); -WARN_ON (!pud_clear_huge (pudp)); - pud =READ_ONCE (*pudp); -WARN_ON (!pud_none (pud)); -} -#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ -static void __init pud_huge_tests (pud_t *pudp,unsigned long pfn,pgprot_t prot) { } -#endif /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ -#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ static void __init pud_basic_tests (struct mm_struct *mm,unsigned long pfn,int idx) { } static void __init pud_advanced_tests(struct mm_struct *mm,
@@ -428,9 +381,6 @@ static void __init pud_advanced_tests(st { }static void __init pud_leaf_tests (unsigned long pfn,pgprot_t prot) { } -static void __init pud_huge_tests (pud_t *pudp,unsigned long pfn,pgprot_t prot) -{ -}#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ static void __init pmd_basic_tests (unsigned long pfn,int idx) { }
@@ -449,14 +399,51 @@ static void __init pud_advanced_tests(st }static void __init pmd_leaf_tests (unsigned long pfn,pgprot_t prot) { }static void __init pud_leaf_tests (unsigned long pfn,pgprot_t prot) { } +static void __init pmd_savedwrite_tests (unsigned long pfn,pgprot_t prot) { } +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP static void __init pmd_huge_tests (pmd_t *pmdp,unsigned long pfn,pgprot_t prot) { +pmd_t pmd; + +if (!arch_vmap_pmd_supported (prot)) +return ; + +pr_debug ("Validating PMD huge\n" ); +/* +* X86 defined pmd_set_huge() verifies that the given +* PMD is not a populated non-leaf entry. +*/ +WRITE_ONCE (*pmdp,__pmd (0 )); +WARN_ON (!pmd_set_huge (pmdp,__pfn_to_phys (pfn), prot)); +WARN_ON (!pmd_clear_huge (pmdp)); + pmd =READ_ONCE (*pmdp); +WARN_ON (!pmd_none (pmd)); } +static void __init pud_huge_tests (pud_t *pudp,unsigned long pfn,pgprot_t prot) { +pud_t pud; + +if (!arch_vmap_pud_supported (prot)) +return ; + +pr_debug ("Validating PUD huge\n" ); +/* +* X86 defined pud_set_huge() verifies that the given +* PUD is not a populated non-leaf entry. +*/ +WRITE_ONCE (*pudp,__pud (0 )); +WARN_ON (!pud_set_huge (pudp,__pfn_to_phys (pfn), prot)); +WARN_ON (!pud_clear_huge (pudp)); + pud =READ_ONCE (*pudp); +WARN_ON (!pud_none (pud)); } -static void __init pmd_savedwrite_tests (unsigned long pfn,pgprot_t prot) { } -#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ +static void __init pmd_huge_tests (pmd_t *pmdp,unsigned long pfn,pgprot_t prot) { } +static void __init pud_huge_tests (pud_t *pudp,unsigned long pfn,pgprot_t prot) { } +#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ static void __init p4d_basic_tests (unsigned long pfn,pgprot_t prot) {
_
Andrew Morton
maintainerPATCH 11/192 mm/debug_vm_pgtable: remove redundant pfn_{pmd/pte}() and fix one comment mistake
In reply to Andrew MortonFrom: Shixin Liu <[email protected]>
Subject: mm/debug_vm_pgtable: remove redundant pfn_{pmd/pte}() and fix one comment mistake
Remove redundant pfn_{pmd/pte}() in {pmd/pte}_advanced_tests() and adjust pfn_pud() in pud_advanced_tests() to make it similar with other two functions.
In addition, the branch condition should be CONFIG_TRANSPARENT_HUGEPAGE instead of CONFIG_ARCH_HAS_PTE_DEVMAP.
---
mm/debug_vm_pgtable.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/debug_vm_pgtable.c renamed from mm/debug_vm_pgtable.c~mm-debug_vm_pgtable-remove-redundant-pfn_pmd-pte-and-fix-one-comment-mistake +3 -3
@@ -91,7 +91,7 @@ static void __init pte_advanced_tests(st unsigned long pfn, unsigned long vaddr, pgprot_t prot) { - pte_t pte = pfn_pte(pfn, prot); + pte_t pte;/* * Architectures optimize set_pte_at by avoiding TLB flush.
@@ -778,12 +778,12 @@ static void __init pmd_swap_soft_dirty_tWARN_ON (!pmd_swp_soft_dirty (pmd_swp_mksoft_dirty (pmd))); WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd))); } -#else /* !CONFIG_ARCH_HAS_PTE_DEVMAP */ +#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ static void __init pmd_soft_dirty_tests (unsigned long pfn,pgprot_t prot) { }static void __init pmd_swap_soft_dirty_tests (unsigned long pfn,pgprot_t prot) { } -#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ static void __init pte_swap_tests (unsigned long pfn,pgprot_t prot) {
_
Andrew Morton
maintainerPATCH 12/192 mm/huge_memory.c: remove dedicated macro HPAGE_CACHE_INDEX_MASK
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/huge_memory.c: remove dedicated macro HPAGE_CACHE_INDEX_MASK
Patch series "Cleanup and fixup for huge_memory:, v3.
This series contains cleanups to remove dedicated macro and remove unnecessary tlb_remove_page_size() for huge zero pmd. Also this adds missing read-only THP checking for transparent_hugepage_enabled() and avoids discarding hugepage if other processes are mapping it. More details can be found in the respective changelogs.
Thi patch (of 5):
Rewrite the pgoff checking logic to remove macro HPAGE_CACHE_INDEX_MASK which is only used here to simplify the code.
---
include/linux/huge_mm.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
include/linux/huge_mm.h renamed from include/linux/huge_mm.h~mm-huge_memoryc-remove-dedicated-macro-hpage_cache_index_mask +2 -4
@@ -152,15 +152,13 @@ static inline bool __transparent_hugepagbool transparent_hugepage_enabled (struct vm_area_struct *vma); -#define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1) -static inline bool transhuge_vma_suitable (struct vm_area_struct *vma,unsigned long haddr) {/* Don't have to check pgoff for anonymous vma */ if (!vma_is_anonymous (vma)) { -if (((vma->vm_start >>PAGE_SHIFT ) &HPAGE_CACHE_INDEX_MASK ) != - (vma->vm_pgoff &HPAGE_CACHE_INDEX_MASK )) +if (!IS_ALIGNED ((vma->vm_start >>PAGE_SHIFT ) - vma->vm_pgoff, +HPAGE_PMD_NR ))return false; }
_
Andrew Morton
maintainerPATCH 13/192 mm/huge_memory.c: use page->deferred_list
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/huge_memory.c: use page->deferred_list
Now that we can represent the location of ->deferred_list instead of ->mapping + ->index, make use of it to improve readability.
---
mm/huge_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memoryc-use-page-deferred_list +2 -2
@@ -2870,7 +2870,7 @@ static unsigned long deferred_split_scanspin_lock_irqsave (&ds_queue->split_queue_lock, flags);/* Take pin on all head pages to avoid freeing them under us */ list_for_each_safe (pos, next, &ds_queue->split_queue) { - page = list_entry((void *)pos, struct page, mapping); + page = list_entry((void *)pos, struct page, deferred_list); page =compound_head (page);if (get_page_unless_zero (page)) {list_move (page_deferred_list (page), &list);
@@ -2885,7 +2885,7 @@ static unsigned long deferred_split_scanspin_unlock_irqrestore (&ds_queue->split_queue_lock, flags);list_for_each_safe (pos, next, &list) { - page = list_entry((void *)pos, struct page, mapping); + page = list_entry((void *)pos, struct page, deferred_list);if (!trylock_page (page)) goto next;/* split_huge_page() removes page from list on success */
_
Andrew Morton
maintainerPATCH 14/192 mm/huge_memory.c: add missing read-only THP checking in transparent_hugepage_enabled()
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/huge_memory.c: add missing read-only THP checking in transparent_hugepage_enabled()
Since commit 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS"), read-only THP file mapping is supported. But it forgot to add checking for it in transparent_hugepage_enabled(). To fix it, we add checking for read-only THP file mapping and also introduce helper transhuge_vma_enabled() to check whether thp is enabled for specified vma to reduce duplicated code. We rename transparent_hugepage_enabled to transparent_hugepage_active to make the code easier to follow as suggested by David Hildenbrand.
---
fs/proc/task_mmu.c | 2 - include/linux/huge_mm.h | 57 +++++++++++++++++++++++--------------- mm/huge_memory.c | 11 ++++++- mm/khugepaged.c | 4 -- mm/shmem.c | 3 -- 5 files changed, 48 insertions(+), 29 deletions(-)
fs/proc/task_mmu.c renamed from fs/proc/task_mmu.c~mm-huge_memoryc-add-missing-read-only-thp-checking-in-transparent_hugepage_enabled +1 -1
@@ -832,7 +832,7 @@ static int show_smap(struct seq_file *m,__show_smap (m, &mss, false);seq_printf (m,"THPeligible: %d\n" , -transparent_hugepage_enabled (vma)); +transparent_hugepage_active (vma));if (arch_pkeys_enabled ())seq_printf (m,"ProtectionKey: %8u\n" ,vma_pkey (vma));
include/linux/huge_mm.h renamed from include/linux/huge_mm.h~mm-huge_memoryc-add-missing-read-only-thp-checking-in-transparent_hugepage_enabled +35 -22
94 lines not on this page · raw message
mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memoryc-add-missing-read-only-thp-checking-in-transparent_hugepage_enabled +10 -1
@@ -64,7 +64,14 @@ static atomic_t huge_zero_refcount;struct page *huge_zero_page__read_mostly ;unsigned long huge_zero_pfn__read_mostly = ~0UL ; -bool transparent_hugepage_enabled (struct vm_area_struct *vma) +static inline bool file_thp_enabled (struct vm_area_struct *vma) +{ +return transhuge_vma_enabled (vma, vma->vm_flags) && vma->vm_file && + !inode_is_open_for_write (vma->vm_file->f_inode) && + (vma->vm_flags &VM_EXEC ); +} + +bool transparent_hugepage_active (struct vm_area_struct *vma) {/* The addr is used to check if the vma size fits */ unsigned long addr = (vma->vm_end &HPAGE_PMD_MASK ) -HPAGE_PMD_SIZE ;
@@ -75,6 +82,8 @@ bool transparent_hugepage_enabled(structreturn __transparent_hugepage_enabled (vma);if (vma_is_shmem (vma))return shmem_huge_enabled (vma); +if (IS_ENABLED (CONFIG_READ_ONLY_THP_FOR_FS )) +return file_thp_enabled (vma);return false; }
mm/khugepaged.c renamed from mm/khugepaged.c~mm-huge_memoryc-add-missing-read-only-thp-checking-in-transparent_hugepage_enabled +1 -3
@@ -442,9 +442,7 @@ static inline int khugepaged_test_exit(sstatic bool hugepage_vma_check (struct vm_area_struct *vma,unsigned long vm_flags) { -/* Explicitly disabled through madvise. */ -if ((vm_flags &VM_NOHUGEPAGE ) || -test_bit (MMF_DISABLE_THP , &vma->vm_mm->flags)) +if (!transhuge_vma_enabled (vma, vm_flags))return false;/* Enabled via shmem mount options or sysfs settings. */
mm/shmem.c renamed from mm/shmem.c~mm-huge_memoryc-add-missing-read-only-thp-checking-in-transparent_hugepage_enabled +1 -2
@@ -4040,8 +4040,7 @@ bool shmem_huge_enabled(struct vm_area_sloff_t i_size;pgoff_t off; -if ((vma->vm_flags &VM_NOHUGEPAGE ) || -test_bit (MMF_DISABLE_THP , &vma->vm_mm->flags)) +if (!transhuge_vma_enabled (vma, vma->vm_flags))return false;if (shmem_huge ==SHMEM_HUGE_FORCE )return true;
_
Andrew Morton
maintainerPATCH 15/192 mm/huge_memory.c: remove unnecessary tlb_remove_page_size() for huge zero pmd
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/huge_memory.c: remove unnecessary tlb_remove_page_size() for huge zero pmd
Commit aa88b68c3b1d ("thp: keep huge zero page pinned until tlb flush") introduced tlb_remove_page() for huge zero page to keep it pinned until flush is complete and prevents the page from being split under us. But huge zero page is kept pinned until all relevant mm_users reach zero since the commit 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter"). So tlb_remove_page_size() for huge zero pmd is unnecessary now.
---
mm/huge_memory.c | 3 --- 1 file changed, 3 deletions(-)
mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memoryc-remove-unnecessary-tlb_remove_page_size-for-huge-zero-pmd +0 -3
@@ -1686,12 +1686,9 @@ int zap_huge_pmd(struct mmu_gather *tlb,if (arch_needs_pgtable_deposit ())zap_deposited_table (tlb->mm, pmd);spin_unlock (ptl); -if (is_huge_zero_pmd (orig_pmd)) -tlb_remove_page_size (tlb,pmd_page (orig_pmd),HPAGE_PMD_SIZE ); }else if (is_huge_zero_pmd (orig_pmd)) {zap_deposited_table (tlb->mm, pmd);spin_unlock (ptl); -tlb_remove_page_size (tlb,pmd_page (orig_pmd),HPAGE_PMD_SIZE ); }else {struct page *page =NULL ;int flush_needed =1 ;
_
Andrew Morton
maintainerPATCH 16/192 mm/huge_memory.c: don't discard hugepage if other processes are mapping it
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/huge_memory.c: don't discard hugepage if other processes are mapping it
If other processes are mapping any other subpages of the hugepage, i.e. in pte-mapped thp case, page_mapcount() will return 1 incorrectly. Then we would discard the page while other processes are still mapping it. Fix it by using total_mapcount() which can tell whether other processes are still mapping it.
---
mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memoryc-dont-discard-hugepage-if-other-processes-are-mapping-it +1 -1
@@ -1613,7 +1613,7 @@ bool madvise_free_huge_pmd(struct mmu_ga* If other processes are mapping this page, we couldn't discard * the page unless they all do MADV_FREE so let's skip the page. */ -if (page_mapcount (page) !=1 ) +if (total_mapcount (page) !=1 ) goto out;if (!trylock_page (page))
_
Andrew Morton
maintainerPATCH 17/192 mm/hugetlb: change parameters of arch_make_huge_pte()
In reply to Andrew MortonFrom: Christophe Leroy <[email protected]>
Subject: mm/hugetlb: change parameters of arch_make_huge_pte()
Patch series "Subject: [PATCH v2 0/5] Implement huge VMAP and VMALLOC on powerpc 8xx", v2.
This series implements huge VMAP and VMALLOC on powerpc 8xx.
Powerpc 8xx has 4 page sizes:
- 4k
- 16k
- 512k
- 8M
At the time being, vmalloc and vmap only support huge pages which are leaf at PMD level.
Here the PMD level is 4M, it doesn't correspond to any supported page size.
For now, implement use of 16k and 512k pages which is done at PTE level.
Support of 8M pages will be implemented later, it requires use of hugepd tables.
To allow this, the architecture provides two functions:
- arch_vmap_pte_range_map_size() which tells vmap_pte_range() what page size to use. A stub returning PAGE_SIZE is provided when the architecture doesn't provide this function.
- arch_vmap_pte_supported_shift() which tells __vmalloc_node_range() what page shift to use for a given area size. A stub returning PAGE_SHIFT is provided when the architecture doesn't provide this function.
This patch (of 5):
At the time being, arch_make_huge_pte() has the following prototype:
pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
struct page *page, int writable);
vma is used to get the pages shift or size.
vma is also used on Sparc to get vm_flags.
page is not used.
writable is not used.
In order to use this function without a vma, replace vma by shift and flags. Also remove the used parameters.
---
arch/arm64/include/asm/hugetlb.h | 3 +-- arch/arm64/mm/hugetlbpage.c | 5 ++--- arch/powerpc/include/asm/nohash/32/hugetlb-8xx.h | 5 ++--- arch/sparc/include/asm/pgtable_64.h | 3 +-- arch/sparc/mm/hugetlbpage.c | 6 ++---- include/linux/hugetlb.h | 4 ++-- mm/hugetlb.c | 6 ++++-- mm/migrate.c | 4 +++- 8 files changed, 17 insertions(+), 19 deletions(-)
arch/arm64/include/asm/hugetlb.h renamed from arch/arm64/include/asm/hugetlb.h~mm-hugetlb-change-parameters-of-arch_make_huge_pte +1 -2
@@ -23,8 +23,7 @@ static inline void arch_clear_hugepage_f }#define arch_clear_hugepage_flags arch_clear_hugepage_flags -extern pte_t arch_make_huge_pte (pte_t entry,struct vm_area_struct *vma, -struct page *page,int writable); +pte_t arch_make_huge_pte (pte_t entry,unsigned int shift,vm_flags_t flags);#define arch_make_huge_pte arch_make_huge_pte#define __HAVE_ARCH_HUGE_SET_HUGE_PTE_AT extern void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
arch/arm64/mm/hugetlbpage.c renamed from arch/arm64/mm/hugetlbpage.c~mm-hugetlb-change-parameters-of-arch_make_huge_pte +2 -3
@@ -339,10 +339,9 @@ pte_t *huge_pte_offset(struct mm_structreturn NULL ; } -pte_t arch_make_huge_pte (pte_t entry,struct vm_area_struct *vma, -struct page *page,int writable) +pte_t arch_make_huge_pte (pte_t entry,unsigned int shift,vm_flags_t flags) { -size_t pagesize =huge_page_size (hstate_vma (vma)); +size_t pagesize =1UL << shift;if (pagesize ==CONT_PTE_SIZE ) { entry =pte_mkcont (entry);
arch/powerpc/include/asm/nohash/32/hugetlb-8xx.h renamed from arch/powerpc/include/asm/nohash/32/hugetlb-8xx.h~mm-hugetlb-change-parameters-of-arch_make_huge_pte +2 -3
@@ -66,10 +66,9 @@ static inline void huge_ptep_set_wrprote }#ifdef CONFIG_PPC_4K_PAGES -static inline pte_t arch_make_huge_pte (pte_t entry,struct vm_area_struct *vma, -struct page *page,int writable) +static inline pte_t arch_make_huge_pte (pte_t entry,unsigned int shift,vm_flags_t flags) { -size_t size =huge_page_size (hstate_vma (vma)); +size_t size =1UL << shift;if (size ==SZ_16K )return __pte (pte_val (entry) & ~_PAGE_HUGE);
arch/sparc/include/asm/pgtable_64.h renamed from arch/sparc/include/asm/pgtable_64.h~mm-hugetlb-change-parameters-of-arch_make_huge_pte +1 -2
@@ -377,8 +377,7 @@ static inline pgprot_t pgprot_noncached(#define pgprot_noncached pgprot_noncached#if defined(CONFIG_HUGETLB_PAGE ) || defined(CONFIG_TRANSPARENT_HUGEPAGE ) -extern pte_t arch_make_huge_pte (pte_t entry,struct vm_area_struct *vma, -struct page *page,int writable); +pte_t arch_make_huge_pte (pte_t entry,unsigned int shift,vm_flags_t flags);#define arch_make_huge_pte arch_make_huge_ptestatic inline unsigned long __pte_default_huge_mask (void ) {
arch/sparc/mm/hugetlbpage.c renamed from arch/sparc/mm/hugetlbpage.c~mm-hugetlb-change-parameters-of-arch_make_huge_pte +2 -4
@@ -177,10 +177,8 @@ static pte_t hugepage_shift_to_tte(pte_treturn sun4u_hugepage_shift_to_tte (entry, shift); } -pte_t arch_make_huge_pte (pte_t entry,struct vm_area_struct *vma, -struct page *page,int writeable) +pte_t arch_make_huge_pte (pte_t entry,unsigned int shift,vm_flags_t flags) { -unsigned int shift =huge_page_shift (hstate_vma (vma));pte_t pte; pte =hugepage_shift_to_tte (entry, shift);
@@ -188,7 +186,7 @@ pte_t arch_make_huge_pte(pte_t entry, st#ifdef CONFIG_SPARC64 /* If this vma has ADI enabled on it, turn on TTE.mcd */ -if (vma->vm_flags &VM_SPARC_ADI ) +if (flags &VM_SPARC_ADI )return pte_mkmcd (pte);else return pte_mknotmcd (pte);
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-hugetlb-change-parameters-of-arch_make_huge_pte +2 -2
@@ -741,8 +741,8 @@ static inline void arch_clear_hugepage_f#endif #ifndef arch_make_huge_pte -static inline pte_t arch_make_huge_pte (pte_t entry,struct vm_area_struct *vma, -struct page *page,int writable) +static inline pte_t arch_make_huge_pte (pte_t entry,unsigned int shift, +vm_flags_t flags) {return entry; }
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-change-parameters-of-arch_make_huge_pte +4 -2
@@ -4060,6 +4060,7 @@ static pte_t make_huge_pte(struct vm_are int writable) {pte_t entry; +unsigned int shift =huge_page_shift (hstate_vma (vma));if (writable) { entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
@@ -4070,7 +4071,7 @@ static pte_t make_huge_pte(struct vm_are } entry =pte_mkyoung (entry); entry =pte_mkhuge (entry); - entry =arch_make_huge_pte (entry, vma, page, writable); + entry =arch_make_huge_pte (entry, shift, vma->vm_flags);return entry; }
@@ -5468,10 +5469,11 @@ unsigned long hugetlb_change_protection( }if (!huge_pte_none (pte)) {pte_t old_pte; +unsigned int shift =huge_page_shift (hstate_vma (vma)); old_pte =huge_ptep_modify_prot_start (vma, address, ptep); pte =pte_mkhuge (huge_pte_modify (old_pte, newprot)); - pte =arch_make_huge_pte (pte, vma,NULL ,0 ); + pte =arch_make_huge_pte (pte, shift, vma->vm_flags);huge_ptep_modify_prot_commit (vma, address, ptep, old_pte, pte); pages++; }
mm/migrate.c renamed from mm/migrate.c~mm-hugetlb-change-parameters-of-arch_make_huge_pte +3 -1
@@ -226,8 +226,10 @@ static bool remove_migration_pte(struct#ifdef CONFIG_HUGETLB_PAGE if (PageHuge (new)) { +unsigned int shift =huge_page_shift (hstate_vma (vma)); + pte =pte_mkhuge (pte); - pte =arch_make_huge_pte (pte, vma, new,0 ); + pte =arch_make_huge_pte (pte, shift, vma->vm_flags);set_huge_pte_at (vma->vm_mm, pvmw.address, pvmw.pte, pte);if (PageAnon (new))hugepage_add_anon_rmap (new, vma, pvmw.address);
_
Andrew Morton
maintainerPATCH 18/192 mm/pgtable: add stubs for {pmd/pub}_{set/clear}_huge
In reply to Andrew MortonFrom: Christophe Leroy <[email protected]>
Subject: mm/pgtable: add stubs for {pmd/pub}_{set/clear}_huge
For architectures with no PMD and/or no PUD, add stubs similar to what we have for architectures without P4D.
---
arch/arm64/mm/mmu.c | 20 ++++++++++++-------- arch/x86/mm/pgtable.c | 34 +++++++++++++++++++--------------- include/linux/pgtable.h | 26 +++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 24 deletions(-)
arch/arm64/mm/mmu.c renamed from arch/arm64/mm/mmu.c~mm-pgtable-add-stubs-for-pmd-pub_set-clear_huge +12 -8
@@ -1338,6 +1338,7 @@ void *__init fixmap_remap_fdt(phys_addr_return dt_virt; } +#if CONFIG_PGTABLE_LEVELS >3 int pud_set_huge (pud_t *pudp,phys_addr_t phys,pgprot_t prot) {pud_t new_pud =pfn_pud (__phys_to_pfn (phys),mk_pud_sect_prot (prot));
@@ -1352,6 +1353,16 @@ int pud_set_huge(pud_t *pudp, phys_addr_return 1 ; } +int pud_clear_huge (pud_t *pudp) +{ +if (!pud_sect (READ_ONCE (*pudp))) +return 0 ; +pud_clear (pudp); +return 1 ; +} +#endif + +#if CONFIG_PGTABLE_LEVELS >2 int pmd_set_huge (pmd_t *pmdp,phys_addr_t phys,pgprot_t prot) {pmd_t new_pmd =pfn_pmd (__phys_to_pfn (phys),mk_pmd_sect_prot (prot));
@@ -1366,14 +1377,6 @@ int pmd_set_huge(pmd_t *pmdp, phys_addr_return 1 ; } -int pud_clear_huge (pud_t *pudp) -{ -if (!pud_sect (READ_ONCE (*pudp))) -return 0 ; -pud_clear (pudp); -return 1 ; -} -int pmd_clear_huge (pmd_t *pmdp) {if (!pmd_sect (READ_ONCE (*pmdp)))
@@ -1381,6 +1384,7 @@ int pmd_clear_huge(pmd_t *pmdp)pmd_clear (pmdp); return 1; } +#endif int pmd_free_pte_page (pmd_t *pmdp,unsigned long addr) {
arch/x86/mm/pgtable.c renamed from arch/x86/mm/pgtable.c~mm-pgtable-add-stubs-for-pmd-pub_set-clear_huge +19 -15
@@ -682,6 +682,7 @@ int p4d_clear_huge(p4d_t *p4d) }#endif +#if CONFIG_PGTABLE_LEVELS >3 /** * pud_set_huge - setup kernel PUD mapping *
@@ -721,6 +722,23 @@ int pud_set_huge(pud_t *pud, phys_addr_t }/** +* pud_clear_huge - clear kernel PUD mapping when it is set +* +* Returns 1 on success and 0 on failure (no PUD map is found). +*/ +int pud_clear_huge (pud_t *pud) +{ +if (pud_large (*pud)) { +pud_clear (pud); +return 1 ; + } + +return 0 ; +} +#endif + +#if CONFIG_PGTABLE_LEVELS >2 +/** * pmd_set_huge - setup kernel PMD mapping * * See text over pud_set_huge() above.
@@ -751,21 +769,6 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t }/** -* pud_clear_huge - clear kernel PUD mapping when it is set -* -* Returns 1 on success and 0 on failure (no PUD map is found). -*/ -int pud_clear_huge (pud_t *pud) -{ -if (pud_large (*pud)) { -pud_clear (pud); -return 1 ; - } - -return 0 ; -} - -/** * pmd_clear_huge - clear kernel PMD mapping when it is set * * Returns 1 on success and 0 on failure (no PMD map is found).
@@ -779,6 +782,7 @@ int pmd_clear_huge(pmd_t *pmd) return 0; } +#endif #ifdef CONFIG_X86_64 /**
include/linux/pgtable.h renamed from include/linux/pgtable.h~mm-pgtable-add-stubs-for-pmd-pub_set-clear_huge +25 -1
@@ -1379,10 +1379,34 @@ static inline int p4d_clear_huge(p4d_t * }#endif /* !__PAGETABLE_P4D_FOLDED */ +#ifndef __PAGETABLE_PUD_FOLDEDint pud_set_huge (pud_t *pud,phys_addr_t addr,pgprot_t prot); -int pmd_set_huge (pmd_t *pmd,phys_addr_t addr,pgprot_t prot);int pud_clear_huge (pud_t *pud); +#else +static inline int pud_set_huge (pud_t *pud,phys_addr_t addr,pgprot_t prot) +{ +return 0 ; +} +static inline int pud_clear_huge (pud_t *pud) +{ +return 0 ; +} +#endif /* !__PAGETABLE_PUD_FOLDED */ + +#ifndef __PAGETABLE_PMD_FOLDED +int pmd_set_huge (pmd_t *pmd,phys_addr_t addr,pgprot_t prot);int pmd_clear_huge (pmd_t *pmd); +#else +static inline int pmd_set_huge (pmd_t *pmd,phys_addr_t addr,pgprot_t prot) +{ +return 0 ; +} +static inline int pmd_clear_huge (pmd_t *pmd) +{ +return 0 ; +} +#endif /* !__PAGETABLE_PMD_FOLDED */ +int p4d_free_pud_page (p4d_t *p4d,unsigned long addr);int pud_free_pmd_page (pud_t *pud,unsigned long addr);int pmd_free_pte_page (pmd_t *pmd,unsigned long addr);
_
Andrew Morton
maintainerPATCH 19/192 mm/vmalloc: enable mapping of huge pages at pte level in vmap
In reply to Andrew MortonFrom: Christophe Leroy <[email protected]>
Subject: mm/vmalloc: enable mapping of huge pages at pte level in vmap
On some architectures like powerpc, there are huge pages that are mapped at pte level.
Enable it in vmap.
For that, architectures can provide arch_vmap_pte_range_map_size() that returns the size of pages to map at pte level.
---
include/linux/vmalloc.h | 8 ++++++++ mm/vmalloc.c | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-)
include/linux/vmalloc.h renamed from include/linux/vmalloc.h~mm-vmalloc-enable-mapping-of-huge-pages-at-pte-level-in-vmap +8 -0
@@ -104,6 +104,14 @@ static inline bool arch_vmap_pmd_support }#endif +#ifndef arch_vmap_pte_range_map_size +static inline unsigned long arch_vmap_pte_range_map_size (unsigned long addr,unsigned long end, +u64 pfn,unsigned int max_page_shift) +{ +return PAGE_SIZE ; +} +#endif +/* * Highlevel APIs for driver use */
mm/vmalloc.c renamed from mm/vmalloc.c~mm-vmalloc-enable-mapping-of-huge-pages-at-pte-level-in-vmap +18 -3
@@ -36,6 +36,7 @@#include <linux/overflow.h> #include <linux/pgtable.h> #include <linux/uaccess.h> +#include <linux/hugetlb.h> #include <asm/tlbflush.h> #include <asm/shmparam.h>
@@ -83,10 +84,11 @@ static void free_work(struct work_struct/*** Page table manipulation functions ***/ static int vmap_pte_range (pmd_t *pmd,unsigned long addr,unsigned long end,phys_addr_t phys_addr,pgprot_t prot, -pgtbl_mod_mask *mask) +unsigned int max_page_shift,pgtbl_mod_mask *mask) {pte_t *pte;u64 pfn; +unsigned long size =PAGE_SIZE ; pfn = phys_addr >>PAGE_SHIFT ; pte =pte_alloc_kernel_track (pmd, addr, mask);
@@ -94,9 +96,22 @@ static int vmap_pte_range(pmd_t *pmd, unreturn -ENOMEM ;do {BUG_ON (!pte_none (*pte)); + +#ifdef CONFIG_HUGETLB_PAGE + size =arch_vmap_pte_range_map_size (addr, end, pfn, max_page_shift); +if (size !=PAGE_SIZE ) { +pte_t entry =pfn_pte (pfn, prot); + + entry =pte_mkhuge (entry); + entry =arch_make_huge_pte (entry,ilog2 (size),0 ); +set_huge_pte_at (&init_mm, addr, pte, entry); + pfn +=PFN_DOWN (size); +continue ; + } +#endif set_pte_at (&init_mm, addr, pte,pfn_pte (pfn, prot)); pfn++; - }while (pte++, addr +=PAGE_SIZE , addr != end); + }while (pte +=PFN_DOWN (size), addr += size, addr != end); *mask |=PGTBL_PTE_MODIFIED ;return 0 ; }
@@ -145,7 +160,7 @@ static int vmap_pmd_range(pud_t *pud, uncontinue ; } -if (vmap_pte_range (pmd, addr, next, phys_addr, prot, mask)) +if (vmap_pte_range (pmd, addr, next, phys_addr, prot, max_page_shift, mask))return -ENOMEM ; }while (pmd++, phys_addr += (next - addr), addr = next, addr != end);return 0 ;
_
Andrew Morton
maintainerPATCH 20/192 mm/vmalloc: enable mapping of huge pages at pte level in vmalloc
In reply to Andrew MortonFrom: Christophe Leroy <[email protected]>
Subject: mm/vmalloc: enable mapping of huge pages at pte level in vmalloc
On some architectures like powerpc, there are huge pages that are mapped at pte level.
Enable it in vmalloc.
For that, architectures can provide arch_vmap_pte_supported_shift() that returns the shift for pages to map at pte level.
---
include/linux/vmalloc.h | 7 +++++++ mm/vmalloc.c | 13 +++++++------ 2 files changed, 14 insertions(+), 6 deletions(-)
include/linux/vmalloc.h renamed from include/linux/vmalloc.h~mm-vmalloc-enable-mapping-of-huge-pages-at-pte-level-in-vmalloc +7 -0
@@ -112,6 +112,13 @@ static inline unsigned long arch_vmap_pt }#endif +#ifndef arch_vmap_pte_supported_shift +static inline int arch_vmap_pte_supported_shift (unsigned long size) +{ +return PAGE_SHIFT ; +} +#endif +/* * Highlevel APIs for driver use */
mm/vmalloc.c renamed from mm/vmalloc.c~mm-vmalloc-enable-mapping-of-huge-pages-at-pte-level-in-vmalloc +7 -6
@@ -2927,8 +2927,7 @@ void *__vmalloc_node_range(unsigned longreturn NULL ; } -if (vmap_allow_huge && !(vm_flags &VM_NO_HUGE_VMAP ) && -arch_vmap_pmd_supported (prot)) { +if (vmap_allow_huge && !(vm_flags &VM_NO_HUGE_VMAP )) {unsigned long size_per_node;/*
@@ -2941,11 +2940,13 @@ void *__vmalloc_node_range(unsigned long size_per_node = size;if (node ==NUMA_NO_NODE ) size_per_node /=num_online_nodes (); -if (size_per_node >=PMD_SIZE ) { +if (arch_vmap_pmd_supported (prot) && size_per_node >=PMD_SIZE ) shift =PMD_SHIFT ; - align =max (real_align,1UL << shift); - size =ALIGN (real_size,1UL << shift); - } +else + shift =arch_vmap_pte_supported_shift (size_per_node); + + align =max (real_align,1UL << shift); + size =ALIGN (real_size,1UL << shift); } again:
_
Andrew Morton
maintainerPATCH 21/192 powerpc/8xx: add support for huge pages on VMAP and VMALLOC
In reply to Andrew MortonFrom: Christophe Leroy <[email protected]>
Subject: powerpc/8xx: add support for huge pages on VMAP and VMALLOC
powerpc 8xx has 4 page sizes:
- 4k
- 16k
- 512k
- 8M
At the time being, vmalloc and vmap only support huge pages which are leaf at PMD level.
Here the PMD level is 4M, it doesn't correspond to any supported page size.
For now, implement use of 16k and 512k pages which is done at PTE level.
Support of 8M pages will be implemented later, it requires vmalloc to support hugepd tables.
---
arch/powerpc/Kconfig | 2 arch/powerpc/include/asm/nohash/32/mmu-8xx.h | 43 +++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/mmu-8xx.h renamed from arch/powerpc/include/asm/nohash/32/mmu-8xx.h~powerpc-8xx-add-support-for-huge-pages-on-vmap-and-vmalloc +43 -0
@@ -178,6 +178,7 @@#ifndef __ASSEMBLY__#include <linux/mmdebug.h> +#include <linux/sizes.h> void mmu_pin_tlb (unsigned long top,bool readonly);
@@ -225,6 +226,48 @@ static inline unsigned int mmu_psize_to_BUG (); } +static inline bool arch_vmap_try_size (unsigned long addr,unsigned long end,u64 pfn, +unsigned int max_page_shift,unsigned long size) +{ +if (end - addr < size) +return false; + +if ((1UL << max_page_shift) < size) +return false; + +if (!IS_ALIGNED (addr, size)) +return false; + +if (!IS_ALIGNED (PFN_PHYS (pfn), size)) +return false; + +return true; +} + +static inline unsigned long arch_vmap_pte_range_map_size (unsigned long addr,unsigned long end, +u64 pfn,unsigned int max_page_shift) +{ +if (arch_vmap_try_size (addr, end, pfn, max_page_shift,SZ_512K )) +return SZ_512K ; +if (PAGE_SIZE ==SZ_16K ) +return SZ_16K ; +if (arch_vmap_try_size (addr, end, pfn, max_page_shift,SZ_16K )) +return SZ_16K ; +return PAGE_SIZE ; +} +#define arch_vmap_pte_range_map_size arch_vmap_pte_range_map_size + +static inline int arch_vmap_pte_supported_shift (unsigned long size) +{ +if (size >=SZ_512K ) +return 19 ; +else if (size >=SZ_16K ) +return 14 ; +else +return PAGE_SHIFT ; +} +#define arch_vmap_pte_supported_shift arch_vmap_pte_supported_shift +/* patch sites */ extern s32 patch__itlbmiss_exit_1, patch__dtlbmiss_exit_1;extern s32 patch__itlbmiss_perf, patch__dtlbmiss_perf;
arch/powerpc/Kconfig renamed from arch/powerpc/Kconfig~powerpc-8xx-add-support-for-huge-pages-on-vmap-and-vmalloc +1 -1
@@ -187,7 +187,7 @@ config PPC select GENERIC_VDSO_TIME_NS select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP - select HAVE_ARCH_HUGE_VMAP if PPC_BOOK3S_64 && PPC_RADIX_MMU + select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if PPC32 && PPC_PAGE_SHIFT <= 14
_
Andrew Morton
maintainerPATCH 22/192 khugepaged: selftests: remove debug_cow
In reply to Andrew MortonFrom: Nanyong Sun <[email protected]>
Subject: khugepaged: selftests: remove debug_cow
The debug_cow attribute had been removed since commit 4958e4d86ecb01 ("mm:
thp: remove debug_cow switch"), so remove it in selftest code too, otherwise the khugepaged test will fail.
---
tools/testing/selftests/vm/khugepaged.c | 4 ---- 1 file changed, 4 deletions(-)
tools/testing/selftests/vm/khugepaged.c renamed from tools/testing/selftests/vm/khugepaged.c~khugepaged-selftests-remove-debug_cow +0 -4
@@ -86,7 +86,6 @@ struct settings {enum thp_enabled thp_enabled;enum thp_defrag thp_defrag;enum shmem_enabled shmem_enabled; -bool debug_cow;bool use_zero_page;struct khugepaged_settings khugepaged; };
@@ -95,7 +94,6 @@ static struct settings default_settings .thp_enabled = THP_MADVISE, .thp_defrag = THP_DEFRAG_ALWAYS, .shmem_enabled = SHMEM_NEVER, - .debug_cow = 0, .use_zero_page = 0, .khugepaged = { .defrag = 1,
@@ -268,7 +266,6 @@ static void write_settings(struct settinwrite_string ("defrag" , thp_defrag_strings[settings->thp_defrag]);write_string ("shmem_enabled" , shmem_enabled_strings[settings->shmem_enabled]); -write_num ("debug_cow" , settings->debug_cow);write_num ("use_zero_page" , settings->use_zero_page);write_num ("khugepaged/defrag" , khugepaged->defrag);
@@ -304,7 +301,6 @@ static void save_settings(void) .thp_defrag = read_string("defrag", thp_defrag_strings), .shmem_enabled = read_string("shmem_enabled", shmem_enabled_strings), - .debug_cow = read_num("debug_cow"), .use_zero_page = read_num("use_zero_page"), }; saved_settings.khugepaged = (struct khugepaged_settings ) {
_
Andrew Morton
maintainerPATCH 23/192 mm, hugetlb: fix racy resv_huge_pages underflow on UFFDIO_COPY
In reply to Andrew MortonFrom: Mina Almasry <[email protected]>
Subject: mm, hugetlb: fix racy resv_huge_pages underflow on UFFDIO_COPY
On UFFDIO_COPY, if we fail to copy the page contents while holding the hugetlb_fault_mutex, we will drop the mutex and return to the caller after allocating a page that consumed a reservation. In this case there may be a fault that double consumes the reservation. To handle this, we free the allocated page, fix the reservations, and allocate a temporary hugetlb page and return that to the caller. When the caller does the copy outside of the lock, we again check the cache, and allocate a page consuming the reservation, and copy over the contents.
Test:
Hacked the code locally such that resv_huge_pages underflows produce a warning and the copy_huge_page_from_user() always fails, then:
./tools/testing/selftests/vm/userfaultfd hugetlb_shared 10
2 /tmp/kokonut_test/huge/userfaultfd_test && echo test success ./tools/testing/selftests/vm/userfaultfd hugetlb 10 2 /tmp/kokonut_test/huge/userfaultfd_test && echo test success
Both tests succeed and produce no warnings. After the test runs number of free/resv hugepages is correct.
---
include/linux/migrate.h | 4 +++ mm/hugetlb.c | 48 +++++++++++++++++++++++++++++------- mm/migrate.c | 2 - mm/userfaultfd.c | 50 -------------------------------------- 4 files changed, 45 insertions(+), 59 deletions(-)
include/linux/migrate.h renamed from include/linux/migrate.h~mm-hugetlb-fix-racy-resv_huge_pages-underflow-on-uffdio_copy +4 -0
@@ -51,6 +51,7 @@ extern int migrate_huge_page_move_mappin struct page *newpage, struct page *page);extern int migrate_page_move_mapping (struct address_space *mapping,struct page *newpage,struct page *page,int extra_count); +extern void copy_huge_page (struct page *dst,struct page *src);#else static inline void putback_movable_pages (struct list_head *l) {}
@@ -77,6 +78,9 @@ static inline int migrate_huge_page_move return -ENOSYS; } +static inline void copy_huge_page (struct page *dst,struct page *src) +{ +}#endif /* CONFIG_MIGRATION */ #ifdef CONFIG_COMPACTION
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-fix-racy-resv_huge_pages-underflow-on-uffdio_copy +39 -9
77 lines not on this page · raw message
mm/migrate.c renamed from mm/migrate.c~mm-hugetlb-fix-racy-resv_huge_pages-underflow-on-uffdio_copy +1 -1
@@ -553,7 +553,7 @@ static void __copy_gigantic_page(struct } } -static void copy_huge_page (struct page *dst,struct page *src) +void copy_huge_page (struct page *dst,struct page *src) {int i;int nr_pages;
mm/userfaultfd.c renamed from mm/userfaultfd.c~mm-hugetlb-fix-racy-resv_huge_pages-underflow-on-uffdio_copy +1 -49
69 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 24/192 mm: sparsemem: split the huge PMD mapping of vmemmap pages
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: sparsemem: split the huge PMD mapping of vmemmap pages
Patch series "Split huge PMD mapping of vmemmap pages", v4.
In order to reduce the difficulty of code review in series[1]. We disable huge PMD mapping of vmemmap pages when that feature is enabled. In this series, we do not disable huge PMD mapping of vmemmap pages anymore. We will split huge PMD mapping when needed. When HugeTLB pages are freed from the pool we do not attempt coalasce and move back to a PMD mapping because it is much more complex.
[1] https://lore.kernel.org/linux-doc/[email protected]/
This patch (of 3):
In [1], PMD mappings of vmemmap pages were disabled if the the feature hugetlb_free_vmemmap was enabled. This was done to simplify the initial implementation of vmmemap freeing for hugetlb pages. Now, remove this simplification by allowing PMD mapping and switching to PTE mappings as needed for allocated hugetlb pages.
When a hugetlb page is allocated, the vmemmap page tables are walked to free vmemmap pages. During this walk, split huge PMD mappings to PTE mappings as required. In the unlikely case PTE pages can not be allocated, return error(ENOMEM) and do not optimize vmemmap of the hugetlb page.
When HugeTLB pages are freed from the pool, we do not attempt to coalesce and move back to a PMD mapping because it is much more complex.
[1] https://lkml.kernel.org/r/[email protected]
---
include/linux/mm.h | 4 - mm/hugetlb_vmemmap.c | 5 - mm/sparse-vmemmap.c | 163 +++++++++++++++++++++++++++++++---------- 3 files changed, 129 insertions(+), 43 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~mm-sparsemem-split-the-huge-pmd-mapping-of-vmemmap-pages +2 -2
@@ -3076,8 +3076,8 @@ static inline void print_vma_addr(char * }#endif -void vmemmap_remap_free (unsigned long start,unsigned long end, -unsigned long reuse); +int vmemmap_remap_free (unsigned long start,unsigned long end, +unsigned long reuse);int vmemmap_remap_alloc (unsigned long start,unsigned long end,unsigned long reuse,gfp_t gfp_mask);
mm/hugetlb_vmemmap.c renamed from mm/hugetlb_vmemmap.c~mm-sparsemem-split-the-huge-pmd-mapping-of-vmemmap-pages +2 -3
@@ -258,9 +258,8 @@ void free_huge_page_vmemmap(struct hstat* to the page which @vmemmap_reuse is mapped to, then free the pages * which the range [@vmemmap_addr, @vmemmap_end] is mapped to. */ -vmemmap_remap_free (vmemmap_addr, vmemmap_end, vmemmap_reuse); - -SetHPageVmemmapOptimized (head); +if (!vmemmap_remap_free (vmemmap_addr, vmemmap_end, vmemmap_reuse)) +SetHPageVmemmapOptimized (head); }void __init hugetlb_vmemmap_init (struct hstate *h)
mm/sparse-vmemmap.c renamed from mm/sparse-vmemmap.c~mm-sparsemem-split-the-huge-pmd-mapping-of-vmemmap-pages +125 -38
279 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 25/192 mm: sparsemem: use huge PMD mapping for vmemmap pages
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: sparsemem: use huge PMD mapping for vmemmap pages
The preparation of splitting huge PMD mapping of vmemmap pages is ready, so switch the mapping from PTE to PMD.
---
Documentation/admin-guide/kernel-parameters.txt | 7 --- arch/x86/mm/init_64.c | 8 +--- include/linux/hugetlb.h | 25 +++----------- mm/memory_hotplug.c | 2 - 4 files changed, 9 insertions(+), 33 deletions(-)
arch/x86/mm/init_64.c renamed from arch/x86/mm/init_64.c~mm-sparsemem-use-huge-pmd-mapping-for-vmemmap-pages +2 -6
@@ -34,7 +34,6 @@#include <linux/gfp.h> #include <linux/kcore.h> #include <linux/bootmem_info.h> -#include <linux/hugetlb.h> #include <asm/processor.h> #include <asm/bios_ebda.h>
@@ -1610,8 +1609,7 @@ int __meminit vmemmap_populate(unsignedVM_BUG_ON (!IS_ALIGNED (start,PAGE_SIZE ));VM_BUG_ON (!IS_ALIGNED (end,PAGE_SIZE )); -if ((is_hugetlb_free_vmemmap_enabled () && !altmap) || - end - start <PAGES_PER_SECTION *sizeof (struct page )) +if (end - start <PAGES_PER_SECTION *sizeof (struct page )) err =vmemmap_populate_basepages (start, end, node,NULL );else if (boot_cpu_has (X86_FEATURE_PSE )) err =vmemmap_populate_hugepages (start, end, node, altmap);
@@ -1639,8 +1637,6 @@ void register_page_bootmem_memmap(unsignpmd_t *pmd;unsigned int nr_pmd_pages;struct page *page; -bool base_mapping = !boot_cpu_has (X86_FEATURE_PSE ) || -is_hugetlb_free_vmemmap_enabled ();for (; addr < end; addr = next) {pte_t *pte =NULL ;
@@ -1666,7 +1662,7 @@ void register_page_bootmem_memmap(unsign }get_page_bootmem (section_nr,pud_page (*pud),MIX_SECTION_INFO ); -if (base_mapping) { +if (!boot_cpu_has (X86_FEATURE_PSE )) { next = (addr +PAGE_SIZE ) &PAGE_MASK ; pmd =pmd_offset (pud, addr);if (pmd_none (*pmd))
Documentation/admin-guide/kernel-parameters.txt renamed from Documentation/admin-guide/kernel-parameters.txt~mm-sparsemem-use-huge-pmd-mapping-for-vmemmap-pages +0 -7
@@ -1572,13 +1572,6 @@ enabled. Allows heavy hugetlb users to free up some more memory (6 * PAGE_SIZE for each 2MB hugetlb page). - This feauture is not free though. Large page - tables are not used to back vmemmap pages which - can lead to a performance degradation for some - workloads. Also there will be memory allocation - required when hugetlb pages are freed from the - pool which can lead to corner cases under heavy - memory pressure. Format: { on | off (default) } on: enable the feature
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-sparsemem-use-huge-pmd-mapping-for-vmemmap-pages +6 -19
@@ -895,20 +895,6 @@ static inline void huge_ptep_modify_prot }#endif -#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP -extern bool hugetlb_free_vmemmap_enabled; - -static inline bool is_hugetlb_free_vmemmap_enabled (void ) -{ -return hugetlb_free_vmemmap_enabled; -} -#else -static inline bool is_hugetlb_free_vmemmap_enabled (void ) -{ -return false; -} -#endif -#else /* CONFIG_HUGETLB_PAGE */ struct hstate {};
@@ -1063,13 +1049,14 @@ static inline void set_huge_swap_pte_at( pte_t *ptep, pte_t pte, unsigned long sz) { } - -static inline bool is_hugetlb_free_vmemmap_enabled (void ) -{ -return false; -}#endif /* CONFIG_HUGETLB_PAGE */ +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +extern bool hugetlb_free_vmemmap_enabled; +#else +#define hugetlb_free_vmemmap_enabled false +#endif +static inline spinlock_t *huge_pte_lock (struct hstate *h,struct mm_struct *mm,pte_t *pte) {
mm/memory_hotplug.c renamed from mm/memory_hotplug.c~mm-sparsemem-use-huge-pmd-mapping-for-vmemmap-pages +1 -1
@@ -1056,7 +1056,7 @@ bool mhp_supports_memmap_on_memory(unsig* populate a single PMD. */ return memmap_on_memory && - !is_hugetlb_free_vmemmap_enabled () && + !hugetlb_free_vmemmap_enabled &&IS_ENABLED (CONFIG_MHP_MEMMAP_ON_MEMORY ) && size ==memory_block_size_bytes () && IS_ALIGNED(vmemmap_size, PMD_SIZE) &&
_
Andrew Morton
maintainerPATCH 26/192 mm: hugetlb: introduce CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: hugetlb: introduce CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON
When using HUGETLB_PAGE_FREE_VMEMMAP, the freeing unused vmemmap pages associated with each HugeTLB page is default off. Now the vmemmap is PMD mapped. So there is no side effect when this feature is enabled with no HugeTLB pages in the system. Someone may want to enable this feature in the compiler time instead of using boot command line. So add a config to make it default on when someone do not want to enable it via command line.
---
Documentation/admin-guide/kernel-parameters.txt | 3 +++ fs/Kconfig | 10 ++++++++++ mm/hugetlb_vmemmap.c | 6 ++++-- 3 files changed, 17 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt renamed from Documentation/admin-guide/kernel-parameters.txt~mm-hugetlb-introduce-config_hugetlb_page_free_vmemmap_default_on +3 -0
@@ -1577,6 +1577,9 @@ on: enable the feature off: disable the feature + Built with CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON=y, + the default is on. + This is not compatible with memory_hotplug.memmap_on_memory. If both parameters are enabled, hugetlb_free_vmemmap takes precedence over memory_hotplug.memmap_on_memory.
fs/Kconfig renamed from fs/Kconfig~mm-hugetlb-introduce-config_hugetlb_page_free_vmemmap_default_on +10 -0
@@ -245,6 +245,16 @@ config HUGETLB_PAGE_FREE_VMEMMAP depends on X86_64 depends on SPARSEMEM_VMEMMAP +config HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON + bool "Default freeing vmemmap pages of HugeTLB to on" + default n + depends on HUGETLB_PAGE_FREE_VMEMMAP + help + When using HUGETLB_PAGE_FREE_VMEMMAP, the freeing unused vmemmap + pages associated with each HugeTLB page is default off. Say Y here + to enable freeing vmemmap pages of HugeTLB by default. It can then + be disabled on the command line via hugetlb_free_vmemmap=off. + config MEMFD_CREATE def_bool TMPFS || HUGETLBFS
mm/hugetlb_vmemmap.c renamed from mm/hugetlb_vmemmap.c~mm-hugetlb-introduce-config_hugetlb_page_free_vmemmap_default_on +4 -2
@@ -182,7 +182,7 @@#define RESERVE_VMEMMAP_NR 2U#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) -bool hugetlb_free_vmemmap_enabled; +bool hugetlb_free_vmemmap_enabled =IS_ENABLED (CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON );static int __init early_hugetlb_free_vmemmap_param (char *buf) {
@@ -197,7 +197,9 @@ static int __init early_hugetlb_free_vmeif (!strcmp (buf,"on" )) hugetlb_free_vmemmap_enabled = true; -else if (strcmp (buf,"off" )) +else if (!strcmp (buf,"off" )) + hugetlb_free_vmemmap_enabled = false; +else return -EINVAL ;return 0 ;
_
Andrew Morton
maintainerPATCH 27/192 hugetlb: remove prep_compound_huge_page cleanup
In reply to Andrew MortonFrom: Mike Kravetz <[email protected]>
Subject: hugetlb: remove prep_compound_huge_page cleanup
Patch series "Fix prep_compound_gigantic_page ref count adjustment".
These patches address the possible race between prep_compound_gigantic_page and __page_cache_add_speculative as described by Jann Horn in [1].
The first patch simply removes the unnecessary/obsolete helper routine prep_compound_huge_page to make the actual fix a little simpler.
The second patch is the actual fix and has a detailed explanation in the commit message.
This potential issue has existed for almost 10 years and I am unaware of anyone actually hitting the race. I did not cc stable, but would be happy to squash the patches and send to stable if anyone thinks that is a good idea.
[1] https://lore.kernel.org/linux-mm/CAG48ez23q0Jy9cuVnwAe7t_fdhMk2S7N5Hdi-GLcCeq5bsfLxw@mail.gmail.com/
This patch (of 2):
I could not think of a reliable way to recreate the issue for testing. Rather, I 'simulated errors' to exercise all the error paths.
The routine prep_compound_huge_page is a simple wrapper to call either prep_compound_gigantic_page or prep_compound_page. However, it is only called from gather_bootmem_prealloc which only processes gigantic pages. Eliminate the routine and call prep_compound_gigantic_page directly.
---
mm/hugetlb.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~hugetlb-remove-prep_compound_huge_page-cleanup +10 -19
@@ -1320,8 +1320,6 @@ static struct page *alloc_gigantic_page( return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask); } -static void prep_new_huge_page (struct hstate *h,struct page *page,int nid); -static void prep_compound_gigantic_page (struct page *page,unsigned int order);#else /* !CONFIG_CONTIG_ALLOC */ static struct page *alloc_gigantic_page (struct hstate *h,gfp_t gfp_mask,int nid,nodemask_t *nodemask)
@@ -2759,16 +2757,10 @@ found:return 1 ; } -static void __init prep_compound_huge_page (struct page *page, -unsigned int order) -{ -if (unlikely (order > (MAX_ORDER -1 ))) -prep_compound_gigantic_page (page, order); -else -prep_compound_page (page, order); -} - -/* Put bootmem huge pages into the standard lists after mem_map is up */ +/* +* Put bootmem huge pages into the standard lists after mem_map is up. +* Note: This only applies to gigantic (order > MAX_ORDER) pages. +*/ static void __init gather_bootmem_prealloc (void ) {struct huge_bootmem_page *m;
@@ -2777,20 +2769,19 @@ static void __init gather_bootmem_preallstruct page *page =virt_to_page (m);struct hstate *h = m->hstate; +VM_BUG_ON (!hstate_is_gigantic (h));WARN_ON (page_count (page) !=1 ); -prep_compound_huge_page (page,huge_page_order (h)); +prep_compound_gigantic_page (page,huge_page_order (h));WARN_ON (PageReserved (page));prep_new_huge_page (h, page,page_to_nid (page));put_page (page);/* free it into the hugepage allocator */ /* -* If we had gigantic hugepages allocated at boot time, we need -* to restore the 'stolen' pages to totalram_pages in order to -* fix confusing memory reports from free(1) and another -* side-effects, like CommitLimit going negative. +* We need to restore the 'stolen' pages to totalram_pages +* in order to fix confusing memory reports from free(1) and +* other side-effects, like CommitLimit going negative. */ -if (hstate_is_gigantic (h)) -adjust_managed_page_count (page,pages_per_huge_page (h)); +adjust_managed_page_count (page,pages_per_huge_page (h));cond_resched (); } }
_
Andrew Morton
maintainerPATCH 28/192 hugetlb: address ref count racing in prep_compound_gigantic_page
In reply to Andrew MortonFrom: Mike Kravetz <[email protected]>
Subject: hugetlb: address ref count racing in prep_compound_gigantic_page
In [1], Jann Horn points out a possible race between prep_compound_gigantic_page and __page_cache_add_speculative. The root cause of the possible race is prep_compound_gigantic_page uncondittionally setting the ref count of pages to zero. It does this because prep_compound_gigantic_page is handed a 'group' of pages from an allocator and needs to convert that group of pages to a compound page. The ref count of each page in this 'group' is one as set by the allocator. However, the ref count of compound page tail pages must be zero.
The potential race comes about when ref counted pages are returned from the allocator. When this happens, other mm code could also take a reference on the page. __page_cache_add_speculative is one such example. Therefore, prep_compound_gigantic_page can not just set the ref count of pages to zero as it does today. Doing so would lose the reference taken by any other code. This would lead to BUGs in code checking ref counts and could possibly even lead to memory corruption.
There are two possible ways to address this issue.
1) Make all allocators of gigantic groups of pages be able to return a properly constructed compound page.
2) Make prep_compound_gigantic_page be more careful when constructing a compound page.
This patch takes approach 2.
In prep_compound_gigantic_page, use cmpxchg to only set ref count to zero if it is one. If the cmpxchg fails, call synchronize_rcu() in the hope that the extra ref count will be driopped during a rcu grace period. This is not a performance critical code path and the wait should be accceptable. If the ref count is still inflated after the grace period, then undo any modifications made and return an error.
Currently prep_compound_gigantic_page is type void and does not return errors. Modify the two callers to check for and handle error returns. On error, the caller must free the 'group' of pages as they can not be used to form a gigantic page. After freeing pages, the runtime caller (alloc_fresh_huge_page) will retry the allocation once. Boot time allocations can not be retried.
The routine prep_compound_page also unconditionally sets the ref count of compound page tail pages to zero. However, in this case the buddy allocator is constructing a compound page from freshly allocated pages. The ref count on those freshly allocated pages is already zero, so the set_page_count(p, 0) is unnecessary and could lead to confusion. Just remove it.
[1] https://lore.kernel.org/linux-mm/CAG48ez23q0Jy9cuVnwAe7t_fdhMk2S7N5Hdi-GLcCeq5bsfLxw@mail.gmail.com/
---
mm/hugetlb.c | 72 ++++++++++++++++++++++++++++++++++++++++------ mm/page_alloc.c | 1 2 files changed, 64 insertions(+), 9 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~hugetlb-address-ref-count-racing-in-prep_compound_gigantic_page +64 -8
@@ -1623,9 +1623,9 @@ static void prep_new_huge_page(struct hsspin_unlock_irq (&hugetlb_lock); } -static void prep_compound_gigantic_page (struct page *page,unsigned int order) +static bool prep_compound_gigantic_page (struct page *page,unsigned int order) { -int i; +int i, j;int nr_pages =1 << order;struct page *p = page +1 ;
@@ -1647,11 +1647,48 @@ static void prep_compound_gigantic_page(* after get_user_pages(). */ __ClearPageReserved (p); +/* +* Subtle and very unlikely +* +* Gigantic 'page allocators' such as memblock or cma will +* return a set of pages with each page ref counted. We need +* to turn this set of pages into a compound page with tail +* page ref counts set to zero. Code such as speculative page +* cache adding could take a ref on a 'to be' tail page. +* We need to respect any increased ref count, and only set +* the ref count to zero if count is currently 1. If count +* is not 1, we call synchronize_rcu in the hope that a rcu +* grace period will cause ref count to drop and then retry. +* If count is still inflated on retry we return an error and +* must discard the pages. +*/ +if (!page_ref_freeze (p,1 )) { +pr_info ("HugeTLB unexpected inflated ref count on freshly allocated page\n" ); +synchronize_rcu (); +if (!page_ref_freeze (p,1 )) + goto out_error; + }set_page_count (p,0 );set_compound_head (p, page); }atomic_set (compound_mapcount_ptr (page),-1 );atomic_set (compound_pincount_ptr (page),0 ); +return true; + +out_error: +/* undo tail page modifications made above */ + p = page +1 ; +for (j =1 ; j < i; j++, p =mem_map_next (p, page, j)) { +clear_compound_head (p); +set_page_refcounted (p); + } +/* need to clear PG_reserved on remaining tail pages */ +for (; j < nr_pages; j++, p =mem_map_next (p, page, j)) +__ClearPageReserved (p); +set_compound_order (page,0 ); + page[1 ].compound_nr =0 ; +__ClearPageHead (page); +return false; }/*
@@ -1771,7 +1808,9 @@ static struct page *alloc_fresh_huge_pag nodemask_t *node_alloc_noretry) {struct page *page; +bool retry = false; +retry:if (hstate_is_gigantic (h)) page =alloc_gigantic_page (h, gfp_mask, nid, nmask);else
@@ -1780,8 +1819,21 @@ static struct page *alloc_fresh_huge_pagif (!page)return NULL ; -if (hstate_is_gigantic (h)) -prep_compound_gigantic_page (page,huge_page_order (h)); +if (hstate_is_gigantic (h)) { +if (!prep_compound_gigantic_page (page,huge_page_order (h))) { +/* +* Rare failure to convert pages to compound page. +* Free pages and try again - ONCE! +*/ +free_gigantic_page (page,huge_page_order (h)); +if (!retry) { + retry = true; + goto retry; + } +pr_warn ("HugeTLB page can not be used due to unexpected inflated ref count\n" ); +return NULL ; + } + }prep_new_huge_page (h, page,page_to_nid (page));return page;
@@ -2771,10 +2823,14 @@ static void __init gather_bootmem_preallVM_BUG_ON (!hstate_is_gigantic (h));WARN_ON (page_count (page) !=1 ); -prep_compound_gigantic_page (page,huge_page_order (h)); -WARN_ON (PageReserved (page)); -prep_new_huge_page (h, page,page_to_nid (page)); -put_page (page);/* free it into the hugepage allocator */ +if (prep_compound_gigantic_page (page,huge_page_order (h))) { +WARN_ON (PageReserved (page)); +prep_new_huge_page (h, page,page_to_nid (page)); +put_page (page);/* add to the hugepage allocator */ + }else { +free_gigantic_page (page,huge_page_order (h)); +pr_warn ("HugeTLB page can not be used due to unexpected inflated ref count\n" ); + }/* * We need to restore the 'stolen' pages to totalram_pages
mm/page_alloc.c renamed from mm/page_alloc.c~hugetlb-address-ref-count-racing-in-prep_compound_gigantic_page +0 -1
@@ -754,7 +754,6 @@ void prep_compound_page(struct page *pag__SetPageHead (page);for (i =1 ; i < nr_pages; i++) {struct page *p = page + i; -set_page_count (p,0 ); p->mapping =TAIL_MAPPING ;set_compound_head (p, page); }
_
Andrew Morton
maintainerPATCH 29/192 mm/hwpoison: disable pcp for page_handle_poison()
In reply to Andrew MortonFrom: Naoya Horiguchi <[email protected]>
Subject: mm/hwpoison: disable pcp for page_handle_poison()
Recent changes by patch "mm/page_alloc: allow high-order pages to be stored on the per-cpu lists" makes kernels determine whether to use pcp by pcp_allowed_order(), which breaks soft-offline for hugetlb pages.
Soft-offline dissolves a migration source page, then removes it from buddy free list, so it's assumed that any subpage of the soft-offlined hugepage are recognized as a buddy page just after returning from dissolve_free_huge_page(). pcp_allowed_order() returns true for hugetlb, so this assumption is no longer true.
So disable pcp during dissolve_free_huge_page() and take_page_off_buddy() to prevent soft-offlined hugepages from linking to pcp lists. Soft-offline should not be common events so the impact on performance should be minimal. And I think that the optimization of Mel's patch could benefit to hugetlb so zone_pcp_disable() is called only in hwpoison context.
---
mm/memory-failure.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
mm/memory-failure.c renamed from mm/memory-failure.c~mm-hwpoison-disable-pcp-for-page_handle_poison +16 -3
@@ -66,6 +66,19 @@ int sysctl_memory_failure_recovery __reaatomic_long_t num_poisoned_pages__read_mostly =ATOMIC_LONG_INIT (0 ); +static bool __page_handle_poison (struct page *page) +{ +bool ret; + +zone_pcp_disable (page_zone (page)); + ret =dissolve_free_huge_page (page); +if (!ret) + ret =take_page_off_buddy (page); +zone_pcp_enable (page_zone (page)); + +return ret; +} +static bool page_handle_poison (struct page *page,bool hugepage_or_freepage,bool release) {if (hugepage_or_freepage) {
@@ -73,7 +86,7 @@ static bool page_handle_poison(struct pa* Doing this check for free pages is also fine since dissolve_free_huge_page * returns 0 for non-hugetlb pages as well. */ -if (dissolve_free_huge_page (page) || !take_page_off_buddy (page)) +if (!__page_handle_poison (page))/* * We could fail to take off the target page from buddy * for example due to racy page allocation, but that's
@@ -985,7 +998,7 @@ static int me_huge_page(struct page *p,*/ if (PageAnon (hpage))put_page (hpage); -if (!dissolve_free_huge_page (p) &&take_page_off_buddy (p)) { +if (__page_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }
@@ -1446,7 +1459,7 @@ static int memory_failure_hugetlb(unsign }unlock_page (head); res =MF_FAILED ; -if (!dissolve_free_huge_page (p) &&take_page_off_buddy (p)) { +if (__page_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }
_
Andrew Morton
maintainerPATCH 30/192 userfaultfd/selftests: use user mode only
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: userfaultfd/selftests: use user mode only
Patch series "userfaultfd/selftests: A few cleanups", v2.
I wanted to cleanup userfaultfd.c fault handling for a long time. If it's not cleaned, when the new code grows the file it'll also grow the size that needs to be cleaned... This is my attempt to cleanup the userfaultfd selftest on fault handling, to use an err() macro instead of either fprintf() or perror() then another exit() call.
The huge cleanup is done in the last patch. The first 4 patches are some other standalone cleanups for the same file, so I put them together.
This patch (of 5):
Userfaultfd selftest does not need to handle kernel initiated fault. Set user mode so it can be run even if unprivileged_userfaultfd=0 (which is the default).
---
tools/testing/selftests/vm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-use-user-mode-only +1 -1
@@ -831,7 +831,7 @@ static int userfaultfd_open_ext(uint64_t {struct uffdio_api uffdio_api; - uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); + uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK |UFFD_USER_MODE_ONLY );if (uffd <0 ) {fprintf (stderr,"userfaultfd syscall not available in this kernel\n" );
_
Andrew Morton
maintainerPATCH 31/192 userfaultfd/selftests: remove the time() check on delayed uffd
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: userfaultfd/selftests: remove the time() check on delayed uffd
There seems to have no guarantee that time() will return the same for the two calls even if there's no delay, e.g. when a fault is accidentally crossing the changing of a second. Meanwhile, this message is also not helping that much since delay could happen with a lot of reasons, e.g., schedule latency of resolving thread. It may not mean an issue with uffd.
Neither do I saw this error triggered either in the past runs. Even if it triggers, it'll be drown in all the rest of test logs. Remove it.
---
tools/testing/selftests/vm/userfaultfd.c | 8 -------- 1 file changed, 8 deletions(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-remove-the-time-check-on-delayed-uffd +0 -8
@@ -395,7 +395,6 @@ static void *locking_thread(void *arg)unsigned long long count;char randstate[64 ];unsigned int seed; -time_t start;if (bounces &BOUNCE_RANDOM ) { seed = (unsigned int )time (NULL ) - bounces;
@@ -432,7 +431,6 @@ static void *locking_thread(void *arg) page_nr +=1 ; page_nr %= nr_pages; - start =time (NULL );if (bounces &BOUNCE_VERIFY ) { count = *area_count (area_dst, page_nr);if (!count) {
@@ -495,12 +493,6 @@ static void *locking_thread(void *arg) count++; *area_count (area_dst, page_nr) = count_verify[page_nr] = count;pthread_mutex_unlock (area_mutex (area_dst, page_nr)); - -if (time (NULL ) - start >1 ) -fprintf (stderr, -"userfault too slow %ld " -"possible false positive with overcommit\n" , -time (NULL ) - start); }return NULL ;
_
Andrew Morton
maintainerPATCH 32/192 userfaultfd/selftests: dropping VERIFY check in locking_thread
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: userfaultfd/selftests: dropping VERIFY check in locking_thread
It tries to check against all zeros and looped for quite a few times. However after that we'll verify the same page with count_verify, while count_verify can never be zero. So it means if it's a zero page we'll detect it anyways with below code.
There's yet another place we conditionally check the fault flag - just do it unconditionally.
---
tools/testing/selftests/vm/userfaultfd.c | 55 --------------------- 1 file changed, 1 insertion(+), 54 deletions(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-dropping-verify-check-in-locking_thread +1 -54
@@ -430,58 +430,6 @@ static void *locking_thread(void *arg) }else page_nr +=1 ; page_nr %= nr_pages; - -if (bounces &BOUNCE_VERIFY ) { - count = *area_count (area_dst, page_nr); -if (!count) { -fprintf (stderr, -"page_nr %lu wrong count %Lu %Lu\n" , - page_nr, count, - count_verify[page_nr]); -exit (1 ); - } - - -/* -* We can't use bcmp (or memcmp) because that -* returns 0 erroneously if the memory is -* changing under it (even if the end of the -* page is never changing and always -* different). -*/ -#if 1 -if (!my_bcmp (area_dst + page_nr * page_size, zeropage, - page_size)) { -fprintf (stderr, -"my_bcmp page_nr %lu wrong count %Lu %Lu\n" , - page_nr, count, count_verify[page_nr]); -exit (1 ); - } -#else -unsigned long loops; - - loops =0 ; -/* uncomment the below line to test with mutex */ -/* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */ -while (!bcmp (area_dst + page_nr * page_size, zeropage, - page_size)) { - loops +=1 ; -if (loops >10 ) -break ; - } -/* uncomment below line to test with mutex */ -/* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */ -if (loops) { -fprintf (stderr, -"page_nr %lu all zero thread %lu %p %lu\n" , - page_nr, cpu, area_dst + page_nr * page_size, - loops); -if (loops >10 ) -exit (1 ); - } -#endif - } -pthread_mutex_lock (area_mutex (area_dst, page_nr)); count = *area_count (area_dst, page_nr);if (count != count_verify[page_nr]) {
@@ -613,8 +561,7 @@ static void uffd_handle_page_fault(struc stats->minor_faults++; }else {/* Missing page faults */ -if (bounces &BOUNCE_VERIFY && - msg->arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_WRITE ) { +if (msg->arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_WRITE ) {fprintf (stderr,"unexpected write fault\n" );exit (1 ); }
_
Andrew Morton
maintainerPATCH 33/192 userfaultfd/selftests: only dump counts if mode enabled
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: userfaultfd/selftests: only dump counts if mode enabled
WP and MINOR modes are conditionally enabled on specific memory types. This patch avoids dumping tons of zeros for those cases when the modes are not supported at all.
---
tools/testing/selftests/vm/userfaultfd.c | 30 ++++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-only-dump-counts-if-mode-enabled +20 -10
@@ -171,16 +171,26 @@ static void uffd_stats_report(struct uff minor_total += stats[i].minor_faults; } -printf ("userfaults: %llu missing (" , miss_total); -for (i =0 ; i < n_cpus; i++) -printf ("%lu+" , stats[i].missing_faults); -printf ("\b), %llu wp (" , wp_total); -for (i =0 ; i < n_cpus; i++) -printf ("%lu+" , stats[i].wp_faults); -printf ("\b), %llu minor (" , minor_total); -for (i =0 ; i < n_cpus; i++) -printf ("%lu+" , stats[i].minor_faults); -printf ("\b)\n" ); +printf ("userfaults: " ); +if (miss_total) { +printf ("%llu missing (" , miss_total); +for (i =0 ; i < n_cpus; i++) +printf ("%lu+" , stats[i].missing_faults); +printf ("\b) " ); + } +if (wp_total) { +printf ("%llu wp (" , wp_total); +for (i =0 ; i < n_cpus; i++) +printf ("%lu+" , stats[i].wp_faults); +printf ("\b) " ); + } +if (minor_total) { +printf ("%llu minor (" , minor_total); +for (i =0 ; i < n_cpus; i++) +printf ("%lu+" , stats[i].minor_faults); +printf ("\b)" ); + } +printf ("\n" ); }static int anon_release_pages (char *rel_area)
_
Andrew Morton
maintainerPATCH 34/192 userfaultfd/selftests: unify error handling
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: userfaultfd/selftests: unify error handling
Introduce err()/_err() and replace all the different ways to fail the program, mostly "fprintf" and "perror" with tons of exit() calls. Always stop the test program at any failure.
---
tools/testing/selftests/vm/userfaultfd.c | 556 +++++++-------------- 1 file changed, 187 insertions(+), 369 deletions(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-unify-error-handling +187 -369
@@ -140,11 +140,18 @@ static void usage(void)exit (1 ); } -#define uffd_error (code, fmt, ...) \ - do { \ - fprintf(stderr, fmt, ##__VA_ARGS__); \ - fprintf(stderr, ": %" PRId64 "\n", (int64_t)(code)); \ - exit(1); \ +#define _err (fmt, ...) \ + do { \ + int ret = errno; \ + fprintf(stderr, "ERROR: " fmt, ##__VA_ARGS__); \ + fprintf(stderr, " (errno=%d, line=%d)\n", \ + ret, __LINE__); \ + } while (0) + +#define err (fmt, ...) \ + do { \ + _err(fmt, ##__VA_ARGS__); \ + exit(1); \ } while (0) static void uffd_stats_reset(struct uffd_stats *uffd_stats,
@@ -193,44 +200,28 @@ static void uffd_stats_report(struct uffprintf ("\n" ); } -static int anon_release_pages (char *rel_area) +static void anon_release_pages (char *rel_area) { -int ret =0 ; - -if (madvise (rel_area, nr_pages * page_size,MADV_DONTNEED )) { -perror ("madvise" ); - ret =1 ; - } - -return ret; +if (madvise (rel_area, nr_pages * page_size,MADV_DONTNEED )) +err ("madvise(MADV_DONTNEED) failed" ); }static void anon_allocate_area (void **alloc_area) { -if (posix_memalign (alloc_area, page_size, nr_pages * page_size)) { -fprintf (stderr,"out of memory\n" ); - *alloc_area =NULL ; - } +if (posix_memalign (alloc_area, page_size, nr_pages * page_size)) +err ("posix_memalign() failed" ); }static void noop_alias_mapping (__u64 *start,size_t len,unsigned long offset) { } -/* HugeTLB memory */ -static int hugetlb_release_pages (char *rel_area) +static void hugetlb_release_pages (char *rel_area) { -int ret =0 ; -if (fallocate (huge_fd,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , - rel_area == huge_fd_off0 ?0 : - nr_pages * page_size, - nr_pages * page_size)) { -perror ("fallocate" ); - ret =1 ; - } - -return ret; + rel_area == huge_fd_off0 ?0 : nr_pages * page_size, + nr_pages * page_size)) +err ("fallocate() failed" ); }static void hugetlb_allocate_area (void **alloc_area)
@@ -243,20 +234,16 @@ static void hugetlb_allocate_area(void *MAP_HUGETLB , huge_fd, *alloc_area == area_src ?0 : nr_pages * page_size); -if (*alloc_area ==MAP_FAILED ) { -perror ("mmap of hugetlbfs file failed" ); - goto fail; - } +if (*alloc_area ==MAP_FAILED ) +err ("mmap of hugetlbfs file failed" );if (map_shared) { area_alias =mmap (NULL , nr_pages * page_size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_HUGETLB , huge_fd, *alloc_area == area_src ?0 : nr_pages * page_size); -if (area_alias ==MAP_FAILED ) { -perror ("mmap of hugetlb file alias failed" ); - goto fail_munmap; - } +if (area_alias ==MAP_FAILED ) +err ("mmap of hugetlb file alias failed" ); }if (*alloc_area == area_src) {
@@ -267,16 +254,6 @@ static void hugetlb_allocate_area(void * }if (area_alias) *alloc_area_alias = area_alias; - -return ; - -fail_munmap: -if (munmap (*alloc_area, nr_pages * page_size) <0 ) { -perror ("hugetlb munmap" ); -exit (1 ); - } -fail: - *alloc_area =NULL ; }static void hugetlb_alias_mapping (__u64 *start,size_t len,unsigned long offset)
@@ -292,33 +269,24 @@ static void hugetlb_alias_mapping(__u64 *start = (unsigned long ) area_dst_alias + offset; } -/* Shared memory */ -static int shmem_release_pages (char *rel_area) +static void shmem_release_pages (char *rel_area) { -int ret =0 ; - -if (madvise (rel_area, nr_pages * page_size,MADV_REMOVE )) { -perror ("madvise" ); - ret =1 ; - } - -return ret; +if (madvise (rel_area, nr_pages * page_size,MADV_REMOVE )) +err ("madvise(MADV_REMOVE) failed" ); }static void shmem_allocate_area (void **alloc_area) { *alloc_area =mmap (NULL , nr_pages * page_size,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_SHARED ,-1 ,0 ); -if (*alloc_area ==MAP_FAILED ) { -fprintf (stderr,"shared memory mmap failed\n" ); - *alloc_area =NULL ; - } +if (*alloc_area ==MAP_FAILED ) +err ("mmap of memfd failed" ); }struct uffd_test_ops {unsigned long expected_ioctls;void (*allocate_area)(void **alloc_area); -int (*release_pages)(char *rel_area); +void (*release_pages)(char *rel_area);void (*alias_mapping)(__u64 *start,size_t len,unsigned long offset); };
@@ -373,11 +341,8 @@ static void wp_range(int ufd, __u64 star/* Undo write-protect, do wakeup after that */ prms.mode = wp ?UFFDIO_WRITEPROTECT_MODE_WP :0 ; -if (ioctl (ufd,UFFDIO_WRITEPROTECT , &prms)) { -fprintf (stderr,"clear WP failed for address 0x%" PRIx64"\n" , - (uint64_t )start); -exit (1 ); - } +if (ioctl (ufd,UFFDIO_WRITEPROTECT , &prms)) +err ("clear WP failed: address=0x%" PRIx64, (uint64_t )start); }static void continue_range (int ufd,__u64 start,__u64 len)
@@ -388,12 +353,9 @@ static void continue_range(int ufd, __u6 req.range.len = len; req.mode =0 ; -if (ioctl (ufd,UFFDIO_CONTINUE , &req)) { -fprintf (stderr, -"UFFDIO_CONTINUE failed for address 0x%" PRIx64"\n" , - (uint64_t )start); -exit (1 ); - } +if (ioctl (ufd,UFFDIO_CONTINUE , &req)) +err ("UFFDIO_CONTINUE failed for address 0x%" PRIx64, + (uint64_t )start); }static void *locking_thread (void *arg)
@@ -412,10 +374,8 @@ static void *locking_thread(void *arg) seed += cpu;bzero (&rand,sizeof (rand));bzero (&randstate,sizeof (randstate)); -if (initstate_r (seed, randstate,sizeof (randstate), &rand)) { -fprintf (stderr,"srandom_r error\n" ); -exit (1 ); - } +if (initstate_r (seed, randstate,sizeof (randstate), &rand)) +err ("initstate_r failed" ); }else { page_nr = -bounces;if (!(bounces &BOUNCE_RACINGFAULTS ))
@@ -424,16 +384,12 @@ static void *locking_thread(void *arg)while (!finished) {if (bounces &BOUNCE_RANDOM ) { -if (random_r (&rand, &rand_nr)) { -fprintf (stderr,"random_r 1 error\n" ); -exit (1 ); - } +if (random_r (&rand, &rand_nr)) +err ("random_r failed" ); page_nr = rand_nr;if (sizeof (page_nr) >sizeof (rand_nr)) { -if (random_r (&rand, &rand_nr)) { -fprintf (stderr,"random_r 2 error\n" ); -exit (1 ); - } +if (random_r (&rand, &rand_nr)) +err ("random_r failed" ); page_nr |= (((unsigned long ) rand_nr) <<16 ) <<16 ; }
@@ -442,12 +398,9 @@ static void *locking_thread(void *arg) page_nr %= nr_pages;pthread_mutex_lock (area_mutex (area_dst, page_nr)); count = *area_count (area_dst, page_nr); -if (count != count_verify[page_nr]) { -fprintf (stderr, -"page_nr %lu memory corruption %Lu %Lu\n" , - page_nr, count, - count_verify[page_nr]);exit (1 ); - } +if (count != count_verify[page_nr]) +err ("page_nr %lu memory corruption %llu %llu" , + page_nr, count, count_verify[page_nr]); count++; *area_count (area_dst, page_nr) = count_verify[page_nr] = count;pthread_mutex_unlock (area_mutex (area_dst, page_nr));
@@ -464,22 +417,21 @@ static void retry_copy_page(int ufd, str offset);if (ioctl (ufd,UFFDIO_COPY , uffdio_copy)) {/* real retval in ufdio_copy.copy */ -if (uffdio_copy->copy != -EEXIST ) { -uffd_error (uffdio_copy->copy, -"UFFDIO_COPY retry error" ); - } - }else -uffd_error (uffdio_copy->copy,"UFFDIO_COPY retry unexpected" ); +if (uffdio_copy->copy != -EEXIST ) +err ("UFFDIO_COPY retry error: %" PRId64, + (int64_t )uffdio_copy->copy); + }else { +err ("UFFDIO_COPY retry unexpected: %" PRId64, + (int64_t )uffdio_copy->copy); + } }static int __copy_page (int ufd,unsigned long offset,bool retry) {struct uffdio_copy uffdio_copy; -if (offset >= nr_pages * page_size) { -fprintf (stderr,"unexpected offset %lu\n" , offset); -exit (1 ); - } +if (offset >= nr_pages * page_size) +err ("unexpected offset %lu\n" , offset); uffdio_copy.dst = (unsigned long ) area_dst + offset; uffdio_copy.src = (unsigned long ) area_src + offset; uffdio_copy.len = page_size;
@@ -491,9 +443,10 @@ static int __copy_page(int ufd, unsignedif (ioctl (ufd,UFFDIO_COPY , &uffdio_copy)) {/* real retval in ufdio_copy.copy */ if (uffdio_copy.copy != -EEXIST ) -uffd_error (uffdio_copy.copy,"UFFDIO_COPY error" ); +err ("UFFDIO_COPY error: %" PRId64, + (int64_t )uffdio_copy.copy); }else if (uffdio_copy.copy != page_size) { -uffd_error (uffdio_copy.copy,"UFFDIO_COPY unexpected copy" ); +err ("UFFDIO_COPY error: %" PRId64, (int64_t )uffdio_copy.copy); }else {if (test_uffdio_copy_eexist && retry) { test_uffdio_copy_eexist = false;
@@ -522,11 +475,10 @@ static int uffd_read_msg(int ufd, structif (ret <0 ) {if (errno ==EAGAIN )return 1 ; -perror ("blocking read error" ); +err ("blocking read error" ); }else { -fprintf (stderr,"short read\n" ); +err ("short read" ); } -exit (1 ); }return 0 ;
@@ -537,10 +489,8 @@ static void uffd_handle_page_fault(struc {unsigned long offset; -if (msg->event !=UFFD_EVENT_PAGEFAULT ) { -fprintf (stderr,"unexpected msg event %u\n" , msg->event); -exit (1 ); - } +if (msg->event !=UFFD_EVENT_PAGEFAULT ) +err ("unexpected msg event %u" , msg->event);if (msg->arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_WP ) {/* Write protect page faults */
@@ -571,10 +521,8 @@ static void uffd_handle_page_fault(struc stats->minor_faults++; }else {/* Missing page faults */ -if (msg->arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_WRITE ) { -fprintf (stderr,"unexpected write fault\n" ); -exit (1 ); - } +if (msg->arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_WRITE ) +err ("unexpected write fault" ); offset = (char *)(unsigned long )msg->arg.pagefault.address - area_dst; offset &= ~(page_size-1 );
@@ -601,32 +549,20 @@ static void *uffd_poll_thread(void *arg)for (;;) { ret =poll (pollfd,2 ,-1 ); -if (!ret) { -fprintf (stderr,"poll error %d\n" , ret); -exit (1 ); - } -if (ret <0 ) { -perror ("poll" ); -exit (1 ); - } +if (ret <=0 ) +err ("poll error: %d" , ret);if (pollfd[1 ].revents &POLLIN ) { -if (read (pollfd[1 ].fd, &tmp_chr,1 ) !=1 ) { -fprintf (stderr,"read pipefd error\n" ); -exit (1 ); - } +if (read (pollfd[1 ].fd, &tmp_chr,1 ) !=1 ) +err ("read pipefd error" );break ; } -if (!(pollfd[0 ].revents &POLLIN )) { -fprintf (stderr,"pollfd[0].revents %d\n" , - pollfd[0 ].revents); -exit (1 ); - } +if (!(pollfd[0 ].revents &POLLIN )) +err ("pollfd[0].revents %d" , pollfd[0 ].revents);if (uffd_read_msg (uffd, &msg))continue ;switch (msg.event) {default : -fprintf (stderr,"unexpected msg event %u\n" , - msg.event);exit (1 ); +err ("unexpected msg event %u\n" , msg.event);break ;case UFFD_EVENT_PAGEFAULT :uffd_handle_page_fault (&msg, stats);
@@ -640,10 +576,8 @@ static void *uffd_poll_thread(void *arg) uffd_reg.range.start = msg.arg.remove.start; uffd_reg.range.len = msg.arg.remove.end - msg.arg.remove.start; -if (ioctl (uffd,UFFDIO_UNREGISTER , &uffd_reg.range)) { -fprintf (stderr,"remove failure\n" ); -exit (1 ); - } +if (ioctl (uffd,UFFDIO_UNREGISTER , &uffd_reg.range)) +err ("remove failure" );break ;case UFFD_EVENT_REMAP : area_dst = (char *)(unsigned long )msg.arg.remap.to;
@@ -746,9 +680,7 @@ static int stress(struct uffd_stats *uff* UFFDIO_COPY without writing zero pages into area_dst * because the background threads already completed). */ -if (uffd_test_ops->release_pages (area_src)) -return 1 ; - + uffd_test_ops->release_pages (area_src); finished =1 ;for (cpu =0 ; cpu < nr_cpus; cpu++)
@@ -758,10 +690,8 @@ static int stress(struct uffd_stats *ufffor (cpu =0 ; cpu < nr_cpus; cpu++) {char c;if (bounces &BOUNCE_POLL ) { -if (write (pipefd[cpu*2 +1 ], &c,1 ) !=1 ) { -fprintf (stderr,"pipefd write error\n" ); -return 1 ; - } +if (write (pipefd[cpu*2 +1 ], &c,1 ) !=1 ) +err ("pipefd write error" );if (pthread_join (uffd_threads[cpu], (void *)&uffd_stats[cpu]))return 1 ;
@@ -861,10 +791,8 @@ static int faulting_process(int signal_tmemset (&act,0 ,sizeof (act)); act.sa_sigaction = sighndl; act.sa_flags =SA_SIGINFO ; -if (sigaction (SIGBUS , &act,0 )) { -perror ("sigaction" ); -return 1 ; - } +if (sigaction (SIGBUS , &act,0 )) +err ("sigaction" ); lastnr = (unsigned long )-1 ; }
@@ -874,10 +802,8 @@ static int faulting_process(int signal_tif (signal_test) {if (sigsetjmp (*sigbuf,1 ) !=0 ) { -if (steps ==1 && nr == lastnr) { -fprintf (stderr,"Signal repeated\n" ); -return 1 ; - } +if (steps ==1 && nr == lastnr) +err ("Signal repeated" ); lastnr = nr;if (signal_test ==1 ) {
@@ -902,12 +828,9 @@ static int faulting_process(int signal_t } count = *area_count (area_dst, nr); -if (count != count_verify[nr]) { -fprintf (stderr, -"nr %lu memory corruption %Lu %Lu\n" , - nr, count, - count_verify[nr]); - } +if (count != count_verify[nr]) +err ("nr %lu memory corruption %llu %llu\n" , + nr, count, count_verify[nr]);/* * Trigger write protection if there is by writing * the same value back.
@@ -923,18 +846,14 @@ static int faulting_process(int signal_t area_dst =mremap (area_dst, nr_pages * page_size, nr_pages * page_size,MREMAP_MAYMOVE |MREMAP_FIXED , area_src); -if (area_dst ==MAP_FAILED ) { -perror ("mremap" ); -exit (1 ); - } +if (area_dst ==MAP_FAILED ) +err ("mremap" );for (; nr < nr_pages; nr++) { count = *area_count (area_dst, nr);if (count != count_verify[nr]) { -fprintf (stderr, -"nr %lu memory corruption %Lu %Lu\n" , - nr, count, - count_verify[nr]);exit (1 ); +err ("nr %lu memory corruption %llu %llu\n" , + nr, count, count_verify[nr]); }/* * Trigger write protection if there is by writing
@@ -943,15 +862,11 @@ static int faulting_process(int signal_t *area_count (area_dst, nr) = count; } -if (uffd_test_ops->release_pages (area_dst)) -return 1 ; + uffd_test_ops->release_pages (area_dst); -for (nr =0 ; nr < nr_pages; nr++) { -if (my_bcmp (area_dst + nr * page_size, zeropage, page_size)) { -fprintf (stderr,"nr %lu is not zero\n" , nr); -exit (1 ); - } - } +for (nr =0 ; nr < nr_pages; nr++) +if (my_bcmp (area_dst + nr * page_size, zeropage, page_size)) +err ("nr %lu is not zero" , nr);return 0 ; }
@@ -964,13 +879,12 @@ static void retry_uffdio_zeropage(int uf uffdio_zeropage->range.len, offset);if (ioctl (ufd,UFFDIO_ZEROPAGE , uffdio_zeropage)) { -if (uffdio_zeropage->zeropage != -EEXIST ) { -uffd_error (uffdio_zeropage->zeropage, -"UFFDIO_ZEROPAGE retry error" ); - } +if (uffdio_zeropage->zeropage != -EEXIST ) +err ("UFFDIO_ZEROPAGE error: %" PRId64, + (int64_t )uffdio_zeropage->zeropage); }else { -uffd_error (uffdio_zeropage->zeropage, -"UFFDIO_ZEROPAGE retry unexpected" ); +err ("UFFDIO_ZEROPAGE error: %" PRId64, + (int64_t )uffdio_zeropage->zeropage); } }
@@ -983,10 +897,8 @@ static int __uffdio_zeropage(int ufd, un has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE); -if (offset >= nr_pages * page_size) { -fprintf (stderr,"unexpected offset %lu\n" , offset); -exit (1 ); - } +if (offset >= nr_pages * page_size) +err ("unexpected offset %lu" , offset); uffdio_zeropage.range.start = (unsigned long ) area_dst + offset; uffdio_zeropage.range.len = page_size; uffdio_zeropage.mode =0 ;
@@ -994,14 +906,13 @@ static int __uffdio_zeropage(int ufd, un res = uffdio_zeropage.zeropage;if (ret) {/* real retval in ufdio_zeropage.zeropage */ -if (has_zeropage) { -uffd_error (res,"UFFDIO_ZEROPAGE %s" , - res == -EEXIST ?"-EEXIST" :"error" ); - }else if (res != -EINVAL ) -uffd_error (res,"UFFDIO_ZEROPAGE not -EINVAL" ); +if (has_zeropage) +err ("UFFDIO_ZEROPAGE error: %" PRId64, (int64_t )res); +else if (res != -EINVAL ) +err ("UFFDIO_ZEROPAGE not -EINVAL" ); }else if (has_zeropage) {if (res != page_size) { -uffd_error (res,"UFFDIO_ZEROPAGE unexpected" ); +err ("UFFDIO_ZEROPAGE unexpected size" ); }else {if (test_uffdio_zeropage_eexist && retry) { test_uffdio_zeropage_eexist = false;
@@ -1011,7 +922,7 @@ static int __uffdio_zeropage(int ufd, unreturn 1 ; } }else -uffd_error (res,"UFFDIO_ZEROPAGE succeeded" ); +err ("UFFDIO_ZEROPAGE succeeded" );return 0 ; }
@@ -1030,8 +941,7 @@ static int userfaultfd_zeropage_test(voiprintf ("testing UFFDIO_ZEROPAGE: " );fflush (stdout); -if (uffd_test_ops->release_pages (area_dst)) -return 1 ; + uffd_test_ops->release_pages (area_dst);if (userfaultfd_open (0 ))return 1 ;
@@ -1040,25 +950,16 @@ static int userfaultfd_zeropage_test(voi uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;if (test_uffdio_wp) uffdio_register.mode |=UFFDIO_REGISTER_MODE_WP ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) { -fprintf (stderr,"register failure\n" ); -exit (1 ); - } +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure" ); expected_ioctls = uffd_test_ops->expected_ioctls; -if ((uffdio_register.ioctls & expected_ioctls) != - expected_ioctls) { -fprintf (stderr, -"unexpected missing ioctl for anon memory\n" ); -exit (1 ); - } +if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) +err ("unexpected missing ioctl for anon memory" ); -if (uffdio_zeropage (uffd,0 )) { -if (my_bcmp (area_dst, zeropage, page_size)) { -fprintf (stderr,"zeropage is not zero\n" ); -exit (1 ); - } - } +if (uffdio_zeropage (uffd,0 )) +if (my_bcmp (area_dst, zeropage, page_size)) +err ("zeropage is not zero" );close (uffd);printf ("done.\n" );
@@ -1078,8 +979,7 @@ static int userfaultfd_events_test(void)printf ("testing events (fork, remap, remove): " );fflush (stdout); -if (uffd_test_ops->release_pages (area_dst)) -return 1 ; + uffd_test_ops->release_pages (area_dst); features =UFFD_FEATURE_EVENT_FORK |UFFD_FEATURE_EVENT_REMAP |UFFD_FEATURE_EVENT_REMOVE ;
@@ -1092,41 +992,28 @@ static int userfaultfd_events_test(void) uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;if (test_uffdio_wp) uffdio_register.mode |=UFFDIO_REGISTER_MODE_WP ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) { -fprintf (stderr,"register failure\n" ); -exit (1 ); - } +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure" ); expected_ioctls = uffd_test_ops->expected_ioctls; -if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) { -fprintf (stderr,"unexpected missing ioctl for anon memory\n" ); -exit (1 ); - } +if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) +err ("unexpected missing ioctl for anon memory" ); -if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) { -perror ("uffd_poll_thread create" ); -exit (1 ); - } +if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) +err ("uffd_poll_thread create" ); pid =fork (); -if (pid <0 ) { -perror ("fork" ); -exit (1 ); - } +if (pid <0 ) +err ("fork" );if (!pid)exit (faulting_process (0 ));waitpid (pid, &err,0 ); -if (err) { -fprintf (stderr,"faulting process failed\n" ); -exit (1 ); - } - -if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) { -perror ("pipe write" ); -exit (1 ); - } +if (err) +err ("faulting process failed" ); +if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) +err ("pipe write" );if (pthread_join (uffd_mon,NULL ))return 1 ;
@@ -1151,8 +1038,7 @@ static int userfaultfd_sig_test(void)printf ("testing signal delivery: " );fflush (stdout); -if (uffd_test_ops->release_pages (area_dst)) -return 1 ; + uffd_test_ops->release_pages (area_dst); features =UFFD_FEATURE_EVENT_FORK |UFFD_FEATURE_SIGBUS ;if (userfaultfd_open (features))
@@ -1164,57 +1050,41 @@ static int userfaultfd_sig_test(void) uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;if (test_uffdio_wp) uffdio_register.mode |=UFFDIO_REGISTER_MODE_WP ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) { -fprintf (stderr,"register failure\n" ); -exit (1 ); - } +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure" ); expected_ioctls = uffd_test_ops->expected_ioctls; -if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) { -fprintf (stderr,"unexpected missing ioctl for anon memory\n" ); -exit (1 ); - } +if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) +err ("unexpected missing ioctl for anon memory" ); -if (faulting_process (1 )) { -fprintf (stderr,"faulting process failed\n" ); -exit (1 ); - } +if (faulting_process (1 )) +err ("faulting process failed" ); -if (uffd_test_ops->release_pages (area_dst)) -return 1 ; + uffd_test_ops->release_pages (area_dst); -if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) { -perror ("uffd_poll_thread create" ); -exit (1 ); - } +if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) +err ("uffd_poll_thread create" ); pid =fork (); -if (pid <0 ) { -perror ("fork" ); -exit (1 ); - } +if (pid <0 ) +err ("fork" );if (!pid)exit (faulting_process (2 ));waitpid (pid, &err,0 ); -if (err) { -fprintf (stderr,"faulting process failed\n" ); -exit (1 ); - } - -if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) { -perror ("pipe write" ); -exit (1 ); - } +if (err) +err ("faulting process failed" ); +if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) +err ("pipe write" );if (pthread_join (uffd_mon, (void **)&userfaults))return 1 ;printf ("done.\n" );if (userfaults) -fprintf (stderr,"Signal test failed, userfaults: %ld\n" , - userfaults); +err ("Signal test failed, userfaults: %ld" , userfaults);close (uffd); +return userfaults !=0 ; }
@@ -1236,8 +1106,7 @@ static int userfaultfd_minor_test(void)printf ("testing minor faults: " );fflush (stdout); -if (uffd_test_ops->release_pages (area_dst)) -return 1 ; + uffd_test_ops->release_pages (area_dst);if (userfaultfd_open_ext (&features))return 1 ;
@@ -1251,17 +1120,13 @@ static int userfaultfd_minor_test(void) uffdio_register.range.start = (unsigned long )area_dst_alias; uffdio_register.range.len = nr_pages * page_size; uffdio_register.mode =UFFDIO_REGISTER_MODE_MINOR ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) { -fprintf (stderr,"register failure\n" ); -exit (1 ); - } +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure" ); expected_ioctls = uffd_test_ops->expected_ioctls; expected_ioctls |=1 << _UFFDIO_CONTINUE; -if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) { -fprintf (stderr,"unexpected missing ioctl(s)\n" ); -exit (1 ); - } +if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) +err ("unexpected missing ioctl(s)" );/* * After registering with UFFD, populate the non-UFFD-registered side of
@@ -1272,10 +1137,8 @@ static int userfaultfd_minor_test(void) page_size); } -if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) { -perror ("uffd_poll_thread create" ); -exit (1 ); - } +if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) +err ("uffd_poll_thread create" );/* * Read each of the pages back using the UFFD-registered mapping. We
@@ -1284,26 +1147,19 @@ static int userfaultfd_minor_test(void)* page's contents, and then issuing a CONTINUE ioctl. */ -if (posix_memalign (&expected_page, page_size, page_size)) { -fprintf (stderr,"out of memory\n" ); -return 1 ; - } +if (posix_memalign (&expected_page, page_size, page_size)) +err ("out of memory" );for (p =0 ; p < nr_pages; ++p) { expected_byte = ~((uint8_t )(p % ((uint8_t )-1 )));memset (expected_page, expected_byte, page_size);if (my_bcmp (expected_page, area_dst_alias + (p * page_size), - page_size)) { -fprintf (stderr, -"unexpected page contents after minor fault\n" ); -exit (1 ); - } + page_size)) +err ("unexpected page contents after minor fault" ); } -if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) { -perror ("pipe write" ); -exit (1 ); - } +if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) +err ("pipe write" );if (pthread_join (uffd_mon,NULL ))return 1 ;
@@ -1321,7 +1177,6 @@ static int userfaultfd_stress(void)unsigned long nr;struct uffdio_register uffdio_register;unsigned long cpu; -int err;struct uffd_stats uffd_stats[nr_cpus]; uffd_test_ops->allocate_area ((void **)&area_src);
@@ -1366,10 +1221,8 @@ static int userfaultfd_stress(void) } } -if (posix_memalign (&area, page_size, page_size)) { -fprintf (stderr,"out of memory\n" ); -return 1 ; - } +if (posix_memalign (&area, page_size, page_size)) +err ("out of memory" ); zeropage = area;bzero (zeropage, page_size);
@@ -1378,7 +1231,6 @@ static int userfaultfd_stress(void)pthread_attr_init (&attr);pthread_attr_setstacksize (&attr,16 *1024 *1024 ); - err =0 ;while (bounces--) {unsigned long expected_ioctls;
@@ -1407,25 +1259,18 @@ static int userfaultfd_stress(void) uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;if (test_uffdio_wp) uffdio_register.mode |=UFFDIO_REGISTER_MODE_WP ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) { -fprintf (stderr,"register failure\n" ); -return 1 ; - } +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure" ); expected_ioctls = uffd_test_ops->expected_ioctls;if ((uffdio_register.ioctls & expected_ioctls) != - expected_ioctls) { -fprintf (stderr, -"unexpected missing ioctl for anon memory\n" ); -return 1 ; - } + expected_ioctls) +err ("unexpected missing ioctl for anon memory" );if (area_dst_alias) { uffdio_register.range.start = (unsigned long ) area_dst_alias; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) { -fprintf (stderr,"register failure alias\n" ); -return 1 ; - } +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure alias" ); }/*
@@ -1452,8 +1297,7 @@ static int userfaultfd_stress(void)* MADV_DONTNEED only after the UFFDIO_REGISTER, so it's * required to MADV_DONTNEED here. */ -if (uffd_test_ops->release_pages (area_dst)) -return 1 ; + uffd_test_ops->release_pages (area_dst);uffd_stats_reset (uffd_stats, nr_cpus);
@@ -1467,33 +1311,22 @@ static int userfaultfd_stress(void) nr_pages * page_size, false);/* unregister */ -if (ioctl (uffd,UFFDIO_UNREGISTER , &uffdio_register.range)) { -fprintf (stderr,"unregister failure\n" ); -return 1 ; - } +if (ioctl (uffd,UFFDIO_UNREGISTER , &uffdio_register.range)) +err ("unregister failure" );if (area_dst_alias) { uffdio_register.range.start = (unsigned long ) area_dst;if (ioctl (uffd,UFFDIO_UNREGISTER , - &uffdio_register.range)) { -fprintf (stderr,"unregister failure alias\n" ); -return 1 ; - } + &uffdio_register.range)) +err ("unregister failure alias" ); }/* verification */ -if (bounces &BOUNCE_VERIFY ) { -for (nr =0 ; nr < nr_pages; nr++) { -if (*area_count (area_dst, nr) != count_verify[nr]) { -fprintf (stderr, -"error area_count %Lu %Lu %lu\n" , - *area_count (area_src, nr), - count_verify[nr], - nr); - err =1 ; - bounces =0 ; - } - } - } +if (bounces &BOUNCE_VERIFY ) +for (nr =0 ; nr < nr_pages; nr++) +if (*area_count (area_dst, nr) != count_verify[nr]) +err ("error area_count %llu %llu %lu\n" , + *area_count (area_src, nr), + count_verify[nr], nr);/* prepare next bounce */ tmp_area = area_src;
@@ -1507,9 +1340,6 @@ static int userfaultfd_stress(void)uffd_stats_report (uffd_stats, nr_cpus); } -if (err) -return err; -close (uffd);return userfaultfd_zeropage_test () ||userfaultfd_sig_test () ||userfaultfd_events_test () ||userfaultfd_minor_test ();
@@ -1560,7 +1390,7 @@ static void set_test_type(const char *ty test_type =TEST_SHMEM ; uffd_test_ops = &shmem_uffd_test_ops; }else { -fprintf (stderr,"Unknown test type: %s\n" , type);exit (1 ); +err ("Unknown test type: %s" , type); }if (test_type ==TEST_HUGETLB )
@@ -1568,15 +1398,11 @@ static void set_test_type(const char *tyelse page_size =sysconf (_SC_PAGE_SIZE); -if (!page_size) { -fprintf (stderr,"Unable to determine page size\n" ); -exit (2 ); - } +if (!page_size) +err ("Unable to determine page size" );if ((unsigned long )area_count (NULL ,0 ) +sizeof (unsigned long long ) *2 - > page_size) { -fprintf (stderr,"Impossible to run this test\n" ); -exit (2 ); - } + > page_size) +err ("Impossible to run this test" ); }static void sigalrm (int sig)
@@ -1593,10 +1419,8 @@ int main(int argc, char **argv)if (argc <4 )usage (); -if (signal (SIGALRM , sigalrm) ==SIG_ERR ) { -fprintf (stderr,"failed to arm SIGALRM" ); -exit (1 ); - } +if (signal (SIGALRM , sigalrm) ==SIG_ERR ) +err ("failed to arm SIGALRM" );alarm (ALARM_INTERVAL_SECS );set_test_type (argv[1 ]);
@@ -1605,13 +1429,13 @@ int main(int argc, char **argv) nr_pages_per_cpu =atol (argv[2 ]) *1024 *1024 / page_size / nr_cpus;if (!nr_pages_per_cpu) { -fprintf (stderr,"invalid MiB\n" ); +_err ("invalid MiB" );usage (); } bounces =atoi (argv[3 ]);if (bounces <=0 ) { -fprintf (stderr,"invalid bounces\n" ); +_err ("invalid bounces" );usage (); } nr_pages = nr_pages_per_cpu * nr_cpus;
@@ -1620,16 +1444,10 @@ int main(int argc, char **argv)if (argc <5 )usage (); huge_fd =open (argv[4 ],O_CREAT |O_RDWR ,0755 ); -if (huge_fd <0 ) { -fprintf (stderr,"Open of %s failed" , argv[3 ]); -perror ("open" ); -exit (1 ); - } -if (ftruncate (huge_fd,0 )) { -fprintf (stderr,"ftruncate %s to size 0 failed" , argv[3 ]); -perror ("ftruncate" ); -exit (1 ); - } +if (huge_fd <0 ) +err ("Open of %s failed" , argv[4 ]); +if (ftruncate (huge_fd,0 )) +err ("ftruncate %s to size 0 failed" , argv[4 ]); }printf ("nr_pages: %lu, nr_pages_per_cpu: %lu\n" , nr_pages, nr_pages_per_cpu);
_
Andrew Morton
maintainerPATCH 35/192 mm/thp: simplify copying of huge zero page pmd when fork
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: mm/thp: simplify copying of huge zero page pmd when fork
Patch series "mm/uffd: Misc fix for uffd-wp and one more test".
This series tries to fix some corner case bugs for uffd-wp on either thp or fork(). Then it introduced a new test with pagemap/pageout.
Patch layout:
Patch 1: cleanup for THP, it'll slightly simplify the follow up patches Patch 2-4: misc fixes for uffd-wp here and there; please refer to each patch Patch 5: add pagemap support for uffd-wp Patch 6: add pagemap/pageout test for uffd-wp
The last test introduced can also verify some of the fixes in previous patches, as the test will fail without the fixes. However it's not easy to verify all the changes in patch 2-4, but hopefully they can still be properly reviewed.
Note that if considering the ongoing uffd-wp shmem & hugetlbfs work, patch 5 will be incomplete as it's missing e.g. hugetlbfs part or the special swap pte detection. However that's not needed in this series, and since that series is still during review, this series does not depend on that one (the last test only runs with anonymous memory, not file-backed). So this series can be merged even before that series.
This patch (of 6):
Huge zero page is handled in a special path in copy_huge_pmd(), however it should share most codes with a normal thp page. Trying to share more code with it by removing the special path. The only leftover so far is the huge zero page refcounting (mm_get_huge_zero_page()), because that's separately done with a global counter.
This prepares for a future patch to modify the huge pmd to be installed, so that we don't need to duplicate it explicitly into huge zero page case too.
---
mm/huge_memory.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
mm/huge_memory.c renamed from mm/huge_memory.c~mm-thp-simplify-copying-of-huge-zero-page-pmd-when-fork +3 -6
@@ -1088,17 +1088,13 @@ int copy_huge_pmd(struct mm_struct *dst_* a page table. */ if (is_huge_zero_pmd (pmd)) { -struct page *zero_page;/* * get_huge_zero_page() will never allocate a new page here, * since we already have a zero page to copy. It just takes a * reference. */ - zero_page =mm_get_huge_zero_page (dst_mm); -set_huge_zero_page (pgtable, dst_mm, vma, addr, dst_pmd, - zero_page); - ret =0 ; - goto out_unlock; +mm_get_huge_zero_page (dst_mm); + goto out_zero_page; } src_page =pmd_page (pmd);
@@ -1122,6 +1118,7 @@ int copy_huge_pmd(struct mm_struct *dst_get_page (src_page);page_dup_rmap (src_page, true);add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR ); +out_zero_page:mm_inc_nr_ptes (dst_mm);pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable);
_
Andrew Morton
maintainerPATCH 36/192 mm/userfaultfd: fix uffd-wp special cases for fork()
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: mm/userfaultfd: fix uffd-wp special cases for fork()
We tried to do something similar in b569a1760782 ("userfaultfd: wp: drop _PAGE_UFFD_WP properly when fork") previously, but it's not doing it all right.. A few fixes around the code path:
1. We were referencing VM_UFFD_WP vm_flags on the _old_ vma rather than the new vma. That's overlooked in b569a1760782, so it won't work as expected. Thanks to the recent rework on fork code (7a4830c380f3a8b3), we can easily get the new vma now, so switch the checks to that.
2. Dropping the uffd-wp bit in copy_huge_pmd() could be wrong if the huge pmd is a migration huge pmd. When it happens, instead of using pmd_uffd_wp(), we should use pmd_swp_uffd_wp(). The fix is simply to handle them separately.
3. Forget to carry over uffd-wp bit for a write migration huge pmd entry. This also happens in copy_huge_pmd(), where we converted a write huge migration entry into a read one.
4. In copy_nonpresent_pte(), drop uffd-wp if necessary for swap ptes.
5. In copy_present_page() when COW is enforced when fork(), we also need to pass over the uffd-wp bit if VM_UFFD_WP is armed on the new vma, and when the pte to be copied has uffd-wp bit set.
Remove the comment in copy_present_pte() about this. It won't help a huge lot to only comment there, but comment everywhere would be an overkill. Let's assume the commit messages would help.
---
include/linux/huge_mm.h | 2 +- include/linux/swapops.h | 2 ++ mm/huge_memory.c | 27 ++++++++++++++------------- mm/memory.c | 25 +++++++++++++------------ 4 files changed, 30 insertions(+), 26 deletions(-)
include/linux/huge_mm.h renamed from include/linux/huge_mm.h~mm-userfaultfd-fix-uffd-wp-special-cases-for-fork +1 -1
@@ -10,7 +10,7 @@vm_fault_t do_huge_pmd_anonymous_page (struct vm_fault *vmf);int copy_huge_pmd (struct mm_struct *dst_mm,struct mm_struct *src_mm,pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr, -struct vm_area_struct *vma); +struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma);void huge_pmd_set_accessed (struct vm_fault *vmf,pmd_t orig_pmd); int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
include/linux/swapops.h renamed from include/linux/swapops.h~mm-userfaultfd-fix-uffd-wp-special-cases-for-fork +2 -0
@@ -265,6 +265,8 @@ static inline swp_entry_t pmd_to_swp_entif (pmd_swp_soft_dirty (pmd)) pmd =pmd_swp_clear_soft_dirty (pmd); +if (pmd_swp_uffd_wp (pmd)) + pmd =pmd_swp_clear_uffd_wp (pmd); arch_entry =__pmd_to_swp_entry (pmd);return swp_entry (__swp_type (arch_entry),__swp_offset (arch_entry)); }
mm/huge_memory.c renamed from mm/huge_memory.c~mm-userfaultfd-fix-uffd-wp-special-cases-for-fork +14 -13
@@ -1026,7 +1026,7 @@ struct page *follow_devmap_pmd(struct vmint copy_huge_pmd (struct mm_struct *dst_mm,struct mm_struct *src_mm,pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr, -struct vm_area_struct *vma) +struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma) {spinlock_t *dst_ptl, *src_ptl;struct page *src_page;
@@ -1035,7 +1035,7 @@ int copy_huge_pmd(struct mm_struct *dst_int ret = -ENOMEM ;/* Skip if can be re-fill on fault */ -if (!vma_is_anonymous (vma)) +if (!vma_is_anonymous (dst_vma))return 0 ; pgtable =pte_alloc_one (dst_mm);
@@ -1049,14 +1049,6 @@ int copy_huge_pmd(struct mm_struct *dst_ ret = -EAGAIN ; pmd = *src_pmd; -/* -* Make sure the _PAGE_UFFD_WP bit is cleared if the new VMA -* does not have the VM_UFFD_WP, which means that the uffd -* fork event is not enabled. -*/ -if (!(vma->vm_flags &VM_UFFD_WP )) - pmd =pmd_clear_uffd_wp (pmd); -#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION if (unlikely (is_swap_pmd (pmd))) {swp_entry_t entry =pmd_to_swp_entry (pmd);
@@ -1067,11 +1059,15 @@ int copy_huge_pmd(struct mm_struct *dst_ pmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*src_pmd)) pmd =pmd_swp_mksoft_dirty (pmd); +if (pmd_swp_uffd_wp (*src_pmd)) + pmd =pmd_swp_mkuffd_wp (pmd);set_pmd_at (src_mm, addr, src_pmd, pmd); }add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR );mm_inc_nr_ptes (dst_mm);pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable); +if (!userfaultfd_wp (dst_vma)) + pmd =pmd_swp_clear_uffd_wp (pmd);set_pmd_at (dst_mm, addr, dst_pmd, pmd); ret =0 ; goto out_unlock;
@@ -1107,11 +1103,11 @@ int copy_huge_pmd(struct mm_struct *dst_* best effort that the pinned pages won't be replaced by another * random page during the coming copy-on-write. */ -if (unlikely (page_needs_cow_for_dma (vma, src_page))) { +if (unlikely (page_needs_cow_for_dma (src_vma, src_page))) {pte_free (dst_mm, pgtable);spin_unlock (src_ptl);spin_unlock (dst_ptl); -__split_huge_pmd (vma, src_pmd, addr, false,NULL ); +__split_huge_pmd (src_vma, src_pmd, addr, false,NULL );return -EAGAIN ; }
@@ -1121,8 +1117,9 @@ int copy_huge_pmd(struct mm_struct *dst_ out_zero_page:mm_inc_nr_ptes (dst_mm);pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable); -pmdp_set_wrprotect (src_mm, addr, src_pmd); +if (!userfaultfd_wp (dst_vma)) + pmd =pmd_clear_uffd_wp (pmd); pmd =pmd_mkold (pmd_wrprotect (pmd));set_pmd_at (dst_mm, addr, dst_pmd, pmd);
@@ -1835,6 +1832,8 @@ int change_huge_pmd(struct vm_area_struc newpmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*pmd)) newpmd =pmd_swp_mksoft_dirty (newpmd); +if (pmd_swp_uffd_wp (*pmd)) + newpmd =pmd_swp_mkuffd_wp (newpmd);set_pmd_at (mm, addr, pmd, newpmd); } goto unlock;
@@ -3245,6 +3244,8 @@ void remove_migration_pmd(struct page_vm pmde =pmd_mksoft_dirty (pmde);if (is_write_migration_entry (entry)) pmde =maybe_pmd_mkwrite (pmde, vma); +if (pmd_swp_uffd_wp (*pvmw->pmd)) + pmde =pmd_wrprotect (pmd_mkuffd_wp (pmde));flush_cache_range (vma, mmun_start, mmun_start +HPAGE_PMD_SIZE );if (PageAnon (new))
mm/memory.c renamed from mm/memory.c~mm-userfaultfd-fix-uffd-wp-special-cases-for-fork +13 -12
@@ -707,10 +707,10 @@ out:static unsigned long copy_nonpresent_pte (struct mm_struct *dst_mm,struct mm_struct *src_mm, -pte_t *dst_pte,pte_t *src_pte,struct vm_area_struct *vma, -unsigned long addr,int *rss) +pte_t *dst_pte,pte_t *src_pte,struct vm_area_struct *dst_vma, +struct vm_area_struct *src_vma,unsigned long addr,int *rss) { -unsigned long vm_flags = vma->vm_flags; +unsigned long vm_flags = dst_vma->vm_flags;pte_t pte = *src_pte;struct page *page;swp_entry_t entry =pte_to_swp_entry (pte);
@@ -779,6 +779,8 @@ copy_nonpresent_pte(struct mm_struct *dsset_pte_at (src_mm, addr, src_pte, pte); } } +if (!userfaultfd_wp (dst_vma)) + pte =pte_swp_clear_uffd_wp (pte);set_pte_at (dst_mm, addr, dst_pte, pte);return 0 ; }
@@ -844,6 +846,9 @@ copy_present_page(struct vm_area_struct/* All done, just insert the new page copy in the child */ pte =mk_pte (new_page, dst_vma->vm_page_prot); pte =maybe_mkwrite (pte_mkdirty (pte), dst_vma); +if (userfaultfd_pte_wp (dst_vma, *src_pte)) +/* Uffd-wp needs to be delivered to dest pte as well */ + pte =pte_wrprotect (pte_mkuffd_wp (pte));set_pte_at (dst_vma->vm_mm, addr, dst_pte, pte);return 0 ; }
@@ -893,12 +898,7 @@ copy_present_pte(struct vm_area_struct * pte =pte_mkclean (pte); pte =pte_mkold (pte); -/* -* Make sure the _PAGE_UFFD_WP bit is cleared if the new VMA -* does not have the VM_UFFD_WP, which means that the uffd -* fork event is not enabled. -*/ -if (!(vm_flags &VM_UFFD_WP )) +if (!userfaultfd_wp (dst_vma)) pte =pte_clear_uffd_wp (pte);set_pte_at (dst_vma->vm_mm, addr, dst_pte, pte);
@@ -973,7 +973,8 @@ again:if (unlikely (!pte_present (*src_pte))) { entry.val =copy_nonpresent_pte (dst_mm, src_mm, dst_pte, src_pte, - src_vma, addr, rss); + dst_vma, src_vma, + addr, rss);if (entry.val)break ; progress +=8 ;
@@ -1050,8 +1051,8 @@ copy_pmd_range(struct vm_area_struct *ds || pmd_devmap(*src_pmd)) {int err;VM_BUG_ON_VMA (next-addr !=HPAGE_PMD_SIZE , src_vma); - err =copy_huge_pmd (dst_mm, src_mm, - dst_pmd, src_pmd, addr, src_vma); + err =copy_huge_pmd (dst_mm, src_mm, dst_pmd, src_pmd, + addr, dst_vma, src_vma);if (err == -ENOMEM )return -ENOMEM ;if (!err)
_
Andrew Morton
maintainerPATCH 37/192 mm/userfaultfd: fail uffd-wp registration if not supported
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: mm/userfaultfd: fail uffd-wp registration if not supported
We should fail uffd-wp registration immediately if the arch does not even have CONFIG_HAVE_ARCH_USERFAULTFD_WP defined. That'll block also relevant ioctls on e.g. UFFDIO_WRITEPROTECT because that'll check against VM_UFFD_WP, which can only be applied with a success registration.
Remove the WP feature bit too for those archs when handling UFFDIO_API ioctl.
---
fs/userfaultfd.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
fs/userfaultfd.c renamed from fs/userfaultfd.c~mm-userfaultfd-fail-uffd-wp-registeration-if-not-supported +8 -1
@@ -1304,8 +1304,12 @@ static int userfaultfd_register(struct u vm_flags =0 ;if (uffdio_register.mode &UFFDIO_REGISTER_MODE_MISSING ) vm_flags |=VM_UFFD_MISSING ; -if (uffdio_register.mode &UFFDIO_REGISTER_MODE_WP ) +if (uffdio_register.mode &UFFDIO_REGISTER_MODE_WP ) { +#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP + goto out; +#endif vm_flags |=VM_UFFD_WP ; + }if (uffdio_register.mode &UFFDIO_REGISTER_MODE_MINOR ) {#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR goto out;
@@ -1943,6 +1947,9 @@ static int userfaultfd_api(struct userfa#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR uffdio_api.features &= ~UFFD_FEATURE_MINOR_HUGETLBFS ;#endif +#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP + uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP ; +#endif uffdio_api.ioctls =UFFD_API_IOCTLS ; ret = -EFAULT ;if (copy_to_user (buf, &uffdio_api,sizeof (uffdio_api)))
_
Andrew Morton
maintainerPATCH 38/192 mm/pagemap: export uffd-wp protection information
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: mm/pagemap: export uffd-wp protection information
Export the PTE/PMD status of uffd-wp to pagemap too.
---
Documentation/admin-guide/mm/pagemap.rst | 2 ++ fs/proc/task_mmu.c | 9 +++++++++ 2 files changed, 11 insertions(+)
Documentation/admin-guide/mm/pagemap.rst renamed from Documentation/admin-guide/mm/pagemap.rst~mm-pagemap-export-uffd-wp-protection-information +2 -0
@@ -21,6 +21,8 @@ There are four components to pagemap: * Bit 55 pte is soft-dirty (see :ref:`Documentation/admin-guide/mm/soft-dirty.rst <soft_dirty>`) * Bit 56 page exclusively mapped (since 4.2) + * Bit 57 pte is uffd-wp write-protected (since 5.13) (see + :ref:`Documentation/admin-guide/mm/userfaultfd.rst <userfaultfd>`) * Bits 57-60 zero * Bit 61 page is file-page or shared-anon (since 3.5) * Bit 62 page swapped
fs/proc/task_mmu.c renamed from fs/proc/task_mmu.c~mm-pagemap-export-uffd-wp-protection-information +9 -0
@@ -1302,6 +1302,7 @@ struct pagemapread {#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)#define PM_SOFT_DIRTY BIT_ULL(55)#define PM_MMAP_EXCLUSIVE BIT_ULL(56) +#define PM_UFFD_WP BIT_ULL(57)#define PM_FILE BIT_ULL(61)#define PM_SWAP BIT_ULL(62)#define PM_PRESENT BIT_ULL(63)
@@ -1375,10 +1376,14 @@ static pagemap_entry_t pte_to_pagemap_en page =vm_normal_page (vma, addr, pte);if (pte_soft_dirty (pte)) flags |=PM_SOFT_DIRTY ; +if (pte_uffd_wp (pte)) + flags |=PM_UFFD_WP ; }else if (is_swap_pte (pte)) {swp_entry_t entry;if (pte_swp_soft_dirty (pte)) flags |=PM_SOFT_DIRTY ; +if (pte_swp_uffd_wp (pte)) + flags |=PM_UFFD_WP ; entry =pte_to_swp_entry (pte);if (pm->show_pfn) frame = swp_type(entry) |
@@ -1426,6 +1431,8 @@ static int pagemap_pmd_range(pmd_t *pmdp flags |=PM_PRESENT ;if (pmd_soft_dirty (pmd)) flags |=PM_SOFT_DIRTY ; +if (pmd_uffd_wp (pmd)) + flags |=PM_UFFD_WP ;if (pm->show_pfn) frame =pmd_pfn (pmd) + ((addr & ~PMD_MASK ) >>PAGE_SHIFT );
@@ -1444,6 +1451,8 @@ static int pagemap_pmd_range(pmd_t *pmdp flags |=PM_SWAP ;if (pmd_swp_soft_dirty (pmd)) flags |=PM_SOFT_DIRTY ; +if (pmd_swp_uffd_wp (pmd)) + flags |=PM_UFFD_WP ;VM_BUG_ON (!is_pmd_migration_entry (pmd)); page =migration_entry_to_page (entry); }
_
Andrew Morton
maintainerPATCH 39/192 userfaultfd/selftests: add pagemap uffd-wp test
In reply to Andrew MortonFrom: Peter Xu <[email protected]>
Subject: userfaultfd/selftests: add pagemap uffd-wp test
Add one anonymous specific test to start using pagemap. With pagemap support, we can directly read the uffd-wp bit from pgtable without triggering any fault, so it's easier to do sanity checks in unit tests.
Meanwhile this test also leverages the newly introduced MADV_PAGEOUT madvise function to test swap ptes with uffd-wp bit set, and across fork()s.
---
tools/testing/selftests/vm/userfaultfd.c | 154 +++++++++++++++++++++ 1 file changed, 154 insertions(+)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-add-pagemap-uffd-wp-test +154 -0
@@ -1170,6 +1170,144 @@ static int userfaultfd_minor_test(void)return stats.missing_faults !=0 || stats.minor_faults != nr_pages; } +#define BIT_ULL (nr) (1ULL << (nr)) +#define PM_SOFT_DIRTY BIT_ULL(55) +#define PM_MMAP_EXCLUSIVE BIT_ULL(56) +#define PM_UFFD_WP BIT_ULL(57) +#define PM_FILE BIT_ULL(61) +#define PM_SWAP BIT_ULL(62) +#define PM_PRESENT BIT_ULL(63) + +static int pagemap_open (void ) +{ +int fd =open ("/proc/self/pagemap" ,O_RDONLY ); + +if (fd <0 ) +err ("open pagemap" ); + +return fd; +} + +static uint64_t pagemap_read_vaddr (int fd,void *vaddr) +{ +uint64_t value; +int ret; + + ret =pread (fd, &value,sizeof (uint64_t ), + ((uint64_t )vaddr >>12 ) *sizeof (uint64_t )); +if (ret !=sizeof (uint64_t )) +err ("pread() on pagemap failed" ); + +return value; +} + +/* This macro let __LINE__ works in err() */ +#define pagemap_check_wp (value, wp) do { \ + if (!!(value & PM_UFFD_WP) != wp) \ + err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \ + } while (0) + +static int pagemap_test_fork (bool present) +{ +pid_t child =fork (); +uint64_t value; +int fd, result; + +if (!child) { +/* Open the pagemap fd of the child itself */ + fd =pagemap_open (); + value =pagemap_read_vaddr (fd, area_dst); +/* +* After fork() uffd-wp bit should be gone as long as we're +* without UFFD_FEATURE_EVENT_FORK +*/ +pagemap_check_wp (value, false); +/* Succeed */ +exit (0 ); + } +waitpid (child, &result,0 ); +return result; +} + +static void userfaultfd_pagemap_test (unsigned int test_pgsize) +{ +struct uffdio_register uffdio_register; +int pagemap_fd; +uint64_t value; + +/* Pagemap tests uffd-wp only */ +if (!test_uffdio_wp) +return ; + +/* Not enough memory to test this page size */ +if (test_pgsize > nr_pages * page_size) +return ; + +printf ("testing uffd-wp with pagemap (pgsize=%u): " , test_pgsize); +/* Flush so it doesn't flush twice in parent/child later */ +fflush (stdout); + + uffd_test_ops->release_pages (area_dst); + +if (test_pgsize > page_size) { +/* This is a thp test */ +if (madvise (area_dst, nr_pages * page_size,MADV_HUGEPAGE )) +err ("madvise(MADV_HUGEPAGE) failed" ); + }else if (test_pgsize == page_size) { +/* This is normal page test; force no thp */ +if (madvise (area_dst, nr_pages * page_size,MADV_NOHUGEPAGE )) +err ("madvise(MADV_NOHUGEPAGE) failed" ); + } + +if (userfaultfd_open (0 )) +err ("userfaultfd_open" ); + + uffdio_register.range.start = (unsigned long ) area_dst; + uffdio_register.range.len = nr_pages * page_size; + uffdio_register.mode =UFFDIO_REGISTER_MODE_WP ; +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failed" ); + + pagemap_fd =pagemap_open (); + +/* Touch the page */ + *area_dst =1 ; +wp_range (uffd, (uint64_t )area_dst, test_pgsize, true); + value =pagemap_read_vaddr (pagemap_fd, area_dst); +pagemap_check_wp (value, true); +/* Make sure uffd-wp bit dropped when fork */ +if (pagemap_test_fork (true)) +err ("Detected stall uffd-wp bit in child" ); + +/* Exclusive required or PAGEOUT won't work */ +if (!(value &PM_MMAP_EXCLUSIVE )) +err ("multiple mapping detected: 0x%" PRIx64, value); + +if (madvise (area_dst, test_pgsize,MADV_PAGEOUT )) +err ("madvise(MADV_PAGEOUT) failed" ); + +/* Uffd-wp should persist even swapped out */ + value =pagemap_read_vaddr (pagemap_fd, area_dst); +pagemap_check_wp (value, true); +/* Make sure uffd-wp bit dropped when fork */ +if (pagemap_test_fork (false)) +err ("Detected stall uffd-wp bit in child" ); + +/* Unprotect; this tests swap pte modifications */ +wp_range (uffd, (uint64_t )area_dst, page_size, false); + value =pagemap_read_vaddr (pagemap_fd, area_dst); +pagemap_check_wp (value, false); + +/* Fault in the page from disk */ + *area_dst =2 ; + value =pagemap_read_vaddr (pagemap_fd, area_dst); +pagemap_check_wp (value, false); + +close (pagemap_fd); +close (uffd); +printf ("done\n" ); +} +static int userfaultfd_stress (void ) {void *area;
@@ -1341,6 +1479,22 @@ static int userfaultfd_stress(void) }close (uffd); + +if (test_type ==TEST_ANON ) { +/* +* shmem/hugetlb won't be able to run since they have different +* behavior on fork() (file-backed memory normally drops ptes +* directly when fork), meanwhile the pagemap test will verify +* pgtable entry of fork()ed child. +*/ +userfaultfd_pagemap_test (page_size); +/* +* Hard-code for x86_64 for now for 2M THP, as x86_64 is +* currently the only one that supports uffd-wp +*/ +userfaultfd_pagemap_test (page_size *512 ); + } +return userfaultfd_zeropage_test () ||userfaultfd_sig_test () ||userfaultfd_events_test () ||userfaultfd_minor_test (); }
_
Andrew Morton
maintainerPATCH 40/192 userfaultfd/shmem: combine shmem_{mcopy_atomic,mfill_zeropage}_pte
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/shmem: combine shmem_{mcopy_atomic,mfill_zeropage}_pte
Patch series "userfaultfd: add minor fault handling for shmem", v6.
Overview
========
See the series which added minor faults for hugetlbfs [3] for a detailed overview of minor fault handling in general. This series adds the same support for shmem-backed areas.
This series is structured as follows:
- Commits 1 and 2 are cleanups.
- Commits 3 and 4 implement the new feature (minor fault handling for shmem).
- Commit 5 advertises that the feature is now available since at this point it's fully implemented.
- Commit 6 is a final cleanup, modifying an existing code path to re-use a new helper we've introduced.
- Commits 7, 8, 9, 10 update the userfaultfd selftest to exercise the feature.
Use Case
========
In some cases it is useful to have VM memory backed by tmpfs instead of hugetlbfs. So, this feature will be used to support the same VM live migration use case described in my original series.
Additionally, Android folks (Lokesh Gidra <[email protected]>) hope to optimize the Android Runtime garbage collector using this feature:
"The plan is to use userfaultfd for concurrently compacting the heap. With this feature, the heap can be shared-mapped at another location where the GC-thread(s) could continue the compaction operation without the need to invoke userfault ioctl(UFFDIO_COPY) each time. OTOH, if and when Java threads get faults on the heap, UFFDIO_CONTINUE can be used to resume execution. Furthermore, this feature enables updating references in the 'non-moving' portion of the heap efficiently. Without this feature, uneccessary page copying (ioctl(UFFDIO_COPY)) would be required."
[1] https://lore.kernel.org/patchwork/cover/1388144/
[2] https://lore.kernel.org/patchwork/patch/1408161/
[3] https://lore.kernel.org/linux-fsdevel/[email protected]/T/#t
This patch (of 9):
Previously, we did a dance where we had one calling path in userfaultfd.c (mfill_atomic_pte), but then we split it into two in shmem_fs.h (shmem_{mcopy_atomic,mfill_zeropage}_pte), and then rejoined into a single shared function in shmem.c (shmem_mfill_atomic_pte).
This is all a bit overly complex. Just call the single combined shmem function directly, allowing us to clean up various branches, boilerplate, etc.
While we're touching this function, two other small cleanup changes:
- offset is equivalent to pgoff, so we can get rid of offset entirely.
- Split two VM_BUG_ON cases into two statements. This means the line number reported when the BUG is hit specifies exactly which condition was true.
---
include/linux/shmem_fs.h | 19 +++++-------- mm/shmem.c | 52 +++++++++++-------------------------- mm/userfaultfd.c | 10 ++----- 3 files changed, 27 insertions(+), 54 deletions(-)
include/linux/shmem_fs.h renamed from include/linux/shmem_fs.h~userfaultfd-shmem-combine-shmem_mcopy_atomicmfill_zeropage_pte +8 -11
@@ -122,21 +122,18 @@ static inline bool shmem_file(struct filextern bool shmem_charge (struct inode *inode,long pages);extern void shmem_uncharge (struct inode *inode,long pages); +#ifdef CONFIG_USERFAULTFD #ifdef CONFIG_SHMEM -extern int shmem_mcopy_atomic_pte (struct mm_struct *dst_mm,pmd_t *dst_pmd, +extern int shmem_mfill_atomic_pte (struct mm_struct *dst_mm,pmd_t *dst_pmd,struct vm_area_struct *dst_vma,unsigned long dst_addr,unsigned long src_addr, +bool zeropage,struct page **pagep); -extern int shmem_mfill_zeropage_pte (struct mm_struct *dst_mm, -pmd_t *dst_pmd, -struct vm_area_struct *dst_vma, -unsigned long dst_addr); -#else -#define shmem_mcopy_atomic_pte (dst_mm, dst_pte, dst_vma, dst_addr, \ - src_addr, pagep) ({ BUG(); 0; }) -#define shmem_mfill_zeropage_pte (dst_mm, dst_pmd, dst_vma, \ - dst_addr) ({ BUG(); 0; }) -#endif +#else /* !CONFIG_SHMEM */ +#define shmem_mfill_atomic_pte (dst_mm, dst_pmd, dst_vma, dst_addr, \ + src_addr, zeropage, pagep) ({ BUG(); 0; }) +#endif /* CONFIG_SHMEM */ +#endif /* CONFIG_USERFAULTFD */ #endif
mm/shmem.c renamed from mm/shmem.c~userfaultfd-shmem-combine-shmem_mcopy_atomicmfill_zeropage_pte +16 -36
100 lines not on this page · raw message
mm/userfaultfd.c renamed from mm/userfaultfd.c~userfaultfd-shmem-combine-shmem_mcopy_atomicmfill_zeropage_pte +3 -7
@@ -392,13 +392,9 @@ static __always_inline ssize_t mfill_ato dst_vma, dst_addr); }else {VM_WARN_ON_ONCE (wp_copy); -if (!zeropage) - err =shmem_mcopy_atomic_pte (dst_mm, dst_pmd, - dst_vma, dst_addr, - src_addr, page); -else - err =shmem_mfill_zeropage_pte (dst_mm, dst_pmd, - dst_vma, dst_addr); + err =shmem_mfill_atomic_pte (dst_mm, dst_pmd, dst_vma, + dst_addr, src_addr, zeropage, + page); }return err;
_
Andrew Morton
maintainerPATCH 41/192 userfaultfd/shmem: support minor fault registration for shmem
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/shmem: support minor fault registration for shmem
This patch allows shmem-backed VMAs to be registered for minor faults. Minor faults are appropriately relayed to userspace in the fault path, for VMAs with the relevant flag.
This commit doesn't hook up the UFFDIO_CONTINUE ioctl for shmem-backed minor faults, though, so userspace doesn't yet have a way to resolve such faults.
Because of this, we also don't yet advertise this as a supported feature. That will be done in a separate commit when the feature is fully implemented.
---
fs/userfaultfd.c | 3 +-- mm/memory.c | 8 +++++--- mm/shmem.c | 12 +++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-)
fs/userfaultfd.c renamed from fs/userfaultfd.c~userfaultfd-shmem-support-minor-fault-registration-for-shmem +1 -2
@@ -1267,8 +1267,7 @@ static inline bool vma_can_userfault(str }if (vm_flags &VM_UFFD_MINOR ) { -/* FIXME: Add minor fault interception for shmem. */ -if (!is_vm_hugetlb_page (vma)) +if (!(is_vm_hugetlb_page (vma) ||vma_is_shmem (vma)))return false; }
mm/memory.c renamed from mm/memory.c~userfaultfd-shmem-support-minor-fault-registration-for-shmem +5 -3
@@ -4026,9 +4026,11 @@ static vm_fault_t do_read_fault(struct v* something). */ if (vma->vm_ops->map_pages && fault_around_bytes >>PAGE_SHIFT >1 ) { - ret =do_fault_around (vmf); -if (ret) -return ret; +if (likely (!userfaultfd_minor (vmf->vma))) { + ret =do_fault_around (vmf); +if (ret) +return ret; + } } ret =__do_fault (vmf);
mm/shmem.c renamed from mm/shmem.c~userfaultfd-shmem-support-minor-fault-registration-for-shmem +11 -1
@@ -1797,7 +1797,7 @@ unlock:* vm. If we swap it in we mark it dirty since we also free the swap * entry since a page cannot live in both the swap and page cache. * -* vmf and fault_type are only supplied by shmem_fault: +* vma, vmf, and fault_type are only supplied by shmem_fault: * otherwise they are NULL. */ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
@@ -1832,6 +1832,16 @@ repeat: page =pagecache_get_page (mapping, index,FGP_ENTRY |FGP_HEAD |FGP_LOCK ,0 ); + +if (page && vma &&userfaultfd_minor (vma)) { +if (!xa_is_value (page)) { +unlock_page (page); +put_page (page); + } + *fault_type =handle_userfault (vmf,VM_UFFD_MINOR ); +return 0 ; + } +if (xa_is_value (page)) { error =shmem_swapin_page (inode, index, &page, sgp, gfp, vma, fault_type);
_
Andrew Morton
maintainerPATCH 42/192 userfaultfd/shmem: support UFFDIO_CONTINUE for shmem
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/shmem: support UFFDIO_CONTINUE for shmem
With this change, userspace can resolve a minor fault within a shmem-backed area with a UFFDIO_CONTINUE ioctl. The semantics for this match those for hugetlbfs - we look up the existing page in the page cache, and install a PTE for it.
This commit introduces a new helper: mfill_atomic_install_pte.
Why handle UFFDIO_CONTINUE for shmem in mm/userfaultfd.c, instead of in shmem.c? The existing userfault implementation only relies on shmem.c for VM_SHARED VMAs. However, minor fault handling / CONTINUE work just fine for !VM_SHARED VMAs as well. We'd prefer to handle CONTINUE for shmem in one place, regardless of shared/private (to reduce code duplication).
Why add a new mfill_atomic_install_pte helper? A problem we have with continue is that shmem_mfill_atomic_pte() and mcopy_atomic_pte() are *close* to what we want, but not exactly. We do want to setup the PTEs in a CONTINUE operation, but we don't want to e.g. allocate a new page, charge it (e.g. to the shmem inode), manipulate various flags, etc. Also we have the problem stated above: shmem_mfill_atomic_pte() and mcopy_atomic_pte() both handle one-half of the problem (shared / private) continue cares about. So, introduce mcontinue_atomic_pte(), to handle all of the shmem continue cases. Introduce the helper so it doesn't duplicate code with mcopy_atomic_pte().
In a future commit, shmem_mfill_atomic_pte() will also be modified to use this new helper. However, since this is a bigger refactor, it seems most clear to do it as a separate change.
---
mm/userfaultfd.c | 172 +++++++++++++++++++++++++++++++++------------ 1 file changed, 127 insertions(+), 45 deletions(-)
mm/userfaultfd.c renamed from mm/userfaultfd.c~userfaultfd-shmem-support-uffdio_continue-for-shmem +127 -45
@@ -48,6 +48,83 @@ struct vm_area_struct *find_dst_vma(strureturn dst_vma; } +/* +* Install PTEs, to map dst_addr (within dst_vma) to page. +* +* This function handles MCOPY_ATOMIC_CONTINUE (which is always file-backed), +* whether or not dst_vma is VM_SHARED. It also handles the more general +* MCOPY_ATOMIC_NORMAL case, when dst_vma is *not* VM_SHARED (it may be file +* backed, or not). +* +* Note that MCOPY_ATOMIC_NORMAL for a VM_SHARED dst_vma is handled by +* shmem_mcopy_atomic_pte instead. +*/ +static int mfill_atomic_install_pte (struct mm_struct *dst_mm,pmd_t *dst_pmd, +struct vm_area_struct *dst_vma, +unsigned long dst_addr,struct page *page, +bool newly_allocated,bool wp_copy) +{ +int ret; +pte_t _dst_pte, *dst_pte; +bool writable = dst_vma->vm_flags &VM_WRITE ; +bool vm_shared = dst_vma->vm_flags &VM_SHARED ; +bool page_in_cache = page->mapping; +spinlock_t *ptl; +struct inode *inode; +pgoff_t offset, max_off; + + _dst_pte =mk_pte (page, dst_vma->vm_page_prot); +if (page_in_cache && !vm_shared) + writable = false; +if (writable || !page_in_cache) + _dst_pte =pte_mkdirty (_dst_pte); +if (writable) { +if (wp_copy) + _dst_pte =pte_mkuffd_wp (_dst_pte); +else + _dst_pte =pte_mkwrite (_dst_pte); + } + + dst_pte =pte_offset_map_lock (dst_mm, dst_pmd, dst_addr, &ptl); + +if (vma_is_shmem (dst_vma)) { +/* serialize against truncate with the page table lock */ + inode = dst_vma->vm_file->f_inode; + offset =linear_page_index (dst_vma, dst_addr); + max_off =DIV_ROUND_UP (i_size_read (inode),PAGE_SIZE ); + ret = -EFAULT ; +if (unlikely (offset >= max_off)) + goto out_unlock; + } + + ret = -EEXIST ; +if (!pte_none (*dst_pte)) + goto out_unlock; + +if (page_in_cache) +page_add_file_rmap (page, false); +else +page_add_new_anon_rmap (page, dst_vma, dst_addr, false); + +/* +* Must happen after rmap, as mm_counter() checks mapping (via +* PageAnon()), which is set by __page_set_anon_rmap(). +*/ +inc_mm_counter (dst_mm,mm_counter (page)); + +if (newly_allocated) +lru_cache_add_inactive_or_unevictable (page, dst_vma); + +set_pte_at (dst_mm, dst_addr, dst_pte, _dst_pte); + +/* No need to invalidate - it was non-present before */ +update_mmu_cache (dst_vma, dst_addr, dst_pte); + ret =0 ; +out_unlock: +pte_unmap_unlock (dst_pte, ptl); +return ret; +} + static int mcopy_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, struct vm_area_struct *dst_vma,
@@ -56,13 +133,9 @@ static int mcopy_atomic_pte(struct mm_ststruct page **pagep, bool wp_copy) { -pte_t _dst_pte, *dst_pte; -spinlock_t *ptl; void *page_kaddr;int ret;struct page *page; -pgoff_t offset, max_off; -struct inode *inode;if (!*pagep) { ret = -ENOMEM ;
@@ -99,43 +172,12 @@ static int mcopy_atomic_pte(struct mm_stif (mem_cgroup_charge (page, dst_mm,GFP_KERNEL )) goto out_release; - _dst_pte =pte_mkdirty (mk_pte (page, dst_vma->vm_page_prot)); -if (dst_vma->vm_flags &VM_WRITE ) { -if (wp_copy) - _dst_pte =pte_mkuffd_wp (_dst_pte); -else - _dst_pte =pte_mkwrite (_dst_pte); - } - - dst_pte =pte_offset_map_lock (dst_mm, dst_pmd, dst_addr, &ptl); -if (dst_vma->vm_file) { -/* the shmem MAP_PRIVATE case requires checking the i_size */ - inode = dst_vma->vm_file->f_inode; - offset =linear_page_index (dst_vma, dst_addr); - max_off =DIV_ROUND_UP (i_size_read (inode),PAGE_SIZE ); - ret = -EFAULT ; -if (unlikely (offset >= max_off)) - goto out_release_uncharge_unlock; - } - ret = -EEXIST ; -if (!pte_none (*dst_pte)) - goto out_release_uncharge_unlock; - -inc_mm_counter (dst_mm,MM_ANONPAGES ); -page_add_new_anon_rmap (page, dst_vma, dst_addr, false); -lru_cache_add_inactive_or_unevictable (page, dst_vma); - -set_pte_at (dst_mm, dst_addr, dst_pte, _dst_pte); - -/* No need to invalidate - it was non-present before */ -update_mmu_cache (dst_vma, dst_addr, dst_pte); - -pte_unmap_unlock (dst_pte, ptl); - ret =0 ; + ret =mfill_atomic_install_pte (dst_mm, dst_pmd, dst_vma, dst_addr, + page, true, wp_copy); +if (ret) + goto out_release; out:return ret; -out_release_uncharge_unlock: -pte_unmap_unlock (dst_pte, ptl); out_release:put_page (page); goto out;
@@ -176,6 +218,41 @@ out_unlock:return ret; } +/* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */ +static int mcontinue_atomic_pte (struct mm_struct *dst_mm, +pmd_t *dst_pmd, +struct vm_area_struct *dst_vma, +unsigned long dst_addr, +bool wp_copy) +{ +struct inode *inode =file_inode (dst_vma->vm_file); +pgoff_t pgoff =linear_page_index (dst_vma, dst_addr); +struct page *page; +int ret; + + ret =shmem_getpage (inode, pgoff, &page,SGP_READ ); +if (ret) + goto out; +if (!page) { + ret = -EFAULT ; + goto out; + } + + ret =mfill_atomic_install_pte (dst_mm, dst_pmd, dst_vma, dst_addr, + page, false, wp_copy); +if (ret) + goto out_release; + +unlock_page (page); + ret =0 ; +out: +return ret; +out_release: +unlock_page (page); +put_page (page); + goto out; +} +static pmd_t *mm_alloc_pmd (struct mm_struct *mm,unsigned long address) {pgd_t *pgd;
@@ -367,11 +444,16 @@ static __always_inline ssize_t mfill_atounsigned long dst_addr, unsigned long src_addr, struct page **page, - bool zeropage, + enum mcopy_atomic_mode mode, bool wp_copy) { ssize_t err; +if (mode ==MCOPY_ATOMIC_CONTINUE ) { +return mcontinue_atomic_pte (dst_mm, dst_pmd, dst_vma, dst_addr, + wp_copy); + } +/* * The normal page fault path for a shmem will invoke the * fault, fill the hole in the file and COW it right away. The
@@ -383,7 +465,7 @@ static __always_inline ssize_t mfill_ato* and not in the radix tree. */ if (!(dst_vma->vm_flags &VM_SHARED )) { -if (!zeropage) +if (mode ==MCOPY_ATOMIC_NORMAL ) err =mcopy_atomic_pte (dst_mm, dst_pmd, dst_vma, dst_addr, src_addr, page, wp_copy);
@@ -393,7 +475,8 @@ static __always_inline ssize_t mfill_ato }else {VM_WARN_ON_ONCE (wp_copy); err =shmem_mfill_atomic_pte (dst_mm, dst_pmd, dst_vma, - dst_addr, src_addr, zeropage, + dst_addr, src_addr, + mode !=MCOPY_ATOMIC_NORMAL , page); }
@@ -415,7 +498,6 @@ static __always_inline ssize_t __mcopy_along copied;struct page *page;bool wp_copy; -bool zeropage = (mcopy_mode ==MCOPY_ATOMIC_ZEROPAGE );/* * Sanitize the command parameters:
@@ -478,7 +560,7 @@ retry:if (!vma_is_anonymous (dst_vma) && !vma_is_shmem (dst_vma)) goto out_unlock; -if (mcopy_mode ==MCOPY_ATOMIC_CONTINUE ) +if (!vma_is_shmem (dst_vma) && mcopy_mode ==MCOPY_ATOMIC_CONTINUE ) goto out_unlock;/*
@@ -526,7 +608,7 @@ retry:BUG_ON (pmd_trans_huge (*dst_pmd)); err =mfill_atomic_pte (dst_mm, dst_pmd, dst_vma, dst_addr, - src_addr, &page, zeropage, wp_copy); + src_addr, &page, mcopy_mode, wp_copy);cond_resched ();if (unlikely (err == -ENOENT )) {
_
Andrew Morton
maintainerPATCH 43/192 userfaultfd/shmem: advertise shmem minor fault support
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/shmem: advertise shmem minor fault support
Now that the feature is fully implemented (the faulting path hooks exist so userspace is notified, and the ioctl to resolve such faults is available), advertise this as a supported feature.
---
Documentation/admin-guide/mm/userfaultfd.rst | 3 ++- fs/userfaultfd.c | 3 ++- include/uapi/linux/userfaultfd.h | 7 ++++++- 3 files changed, 10 insertions(+), 3 deletions(-)
Documentation/admin-guide/mm/userfaultfd.rst renamed from Documentation/admin-guide/mm/userfaultfd.rst~userfaultfd-shmem-advertise-shmem-minor-fault-support +2 -1
@@ -77,7 +77,8 @@ events, except page fault notifications, - ``UFFD_FEATURE_MINOR_HUGETLBFS`` indicates that the kernel supports ``UFFDIO_REGISTER_MODE_MINOR`` registration for hugetlbfs virtual memory - areas. + areas. ``UFFD_FEATURE_MINOR_SHMEM`` is the analogous feature indicating + support for shmem virtual memory areas. The userland application should set the feature flags it intends to use when invoking the ``UFFDIO_API`` ioctl, to request that those features be
fs/userfaultfd.c renamed from fs/userfaultfd.c~userfaultfd-shmem-advertise-shmem-minor-fault-support +2 -1
@@ -1944,7 +1944,8 @@ static int userfaultfd_api(struct userfa/* report all available features and ioctls to userland */ uffdio_api.features =UFFD_API_FEATURES ;#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR - uffdio_api.features &= ~UFFD_FEATURE_MINOR_HUGETLBFS ; + uffdio_api.features &= + ~(UFFD_FEATURE_MINOR_HUGETLBFS |UFFD_FEATURE_MINOR_SHMEM );#endif #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP ;
include/uapi/linux/userfaultfd.h renamed from include/uapi/linux/userfaultfd.h~userfaultfd-shmem-advertise-shmem-minor-fault-support +6 -1
@@ -31,7 +31,8 @@ UFFD_FEATURE_MISSING_SHMEM | \ UFFD_FEATURE_SIGBUS | \ UFFD_FEATURE_THREAD_ID | \ - UFFD_FEATURE_MINOR_HUGETLBFS) + UFFD_FEATURE_MINOR_HUGETLBFS | \ + UFFD_FEATURE_MINOR_SHMEM)#define UFFD_API_IOCTLS \ ((__u64)1 << _UFFDIO_REGISTER | \ (__u64)1 << _UFFDIO_UNREGISTER | \
@@ -185,6 +186,9 @@ struct uffdio_api {* UFFD_FEATURE_MINOR_HUGETLBFS indicates that minor faults * can be intercepted (via REGISTER_MODE_MINOR) for * hugetlbfs-backed pages. +* +* UFFD_FEATURE_MINOR_SHMEM indicates the same support as +* UFFD_FEATURE_MINOR_HUGETLBFS, but for shmem-backed pages instead. */ #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0)#define UFFD_FEATURE_EVENT_FORK (1<<1)
@@ -196,6 +200,7 @@ struct uffdio_api {#define UFFD_FEATURE_SIGBUS (1<<7)#define UFFD_FEATURE_THREAD_ID (1<<8)#define UFFD_FEATURE_MINOR_HUGETLBFS (1<<9) +#define UFFD_FEATURE_MINOR_SHMEM (1<<10)__u64 features;__u64 ioctls;
_
Andrew Morton
maintainerPATCH 44/192 userfaultfd/shmem: modify shmem_mfill_atomic_pte to use install_pte()
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/shmem: modify shmem_mfill_atomic_pte to use install_pte()
In a previous commit, we added the mfill_atomic_install_pte() helper. This helper does the job of setting up PTEs for an existing page, to map it into a given VMA. It deals with both the anon and shmem cases, as well as the shared and private cases.
In other words, shmem_mfill_atomic_pte() duplicates a case it already handles. So, expose it, and let shmem_mfill_atomic_pte() use it directly, to reduce code duplication.
This requires that we refactor shmem_mfill_atomic_pte() a bit:
Instead of doing accounting (shmem_recalc_inode() et al) part-way through the PTE setup, do it afterward. This frees up mfill_atomic_install_pte() from having to care about this accounting, and means we don't need to e.g. shmem_uncharge() in the error path.
A side effect is this switches shmem_mfill_atomic_pte() to use lru_cache_add_inactive_or_unevictable() instead of just lru_cache_add(). This wrapper does some extra accounting in an exceptional case, if appropriate, so it's actually the more correct thing to use.
---
include/linux/userfaultfd_k.h | 5 ++ mm/shmem.c | 58 ++++++-------------------------- mm/userfaultfd.c | 17 +++------ 3 files changed, 23 insertions(+), 57 deletions(-)
include/linux/userfaultfd_k.h renamed from include/linux/userfaultfd_k.h~userfaultfd-shmem-modify-shmem_mfill_atomic_pte-to-use-install_pte +5 -0
@@ -53,6 +53,11 @@ enum mcopy_atomic_mode { MCOPY_ATOMIC_CONTINUE, }; +extern int mfill_atomic_install_pte (struct mm_struct *dst_mm,pmd_t *dst_pmd, +struct vm_area_struct *dst_vma, +unsigned long dst_addr,struct page *page, +bool newly_allocated,bool wp_copy); +extern ssize_t mcopy_atomic (struct mm_struct *dst_mm,unsigned long dst_start,unsigned long src_start,unsigned long len,bool *mmap_changing,__u64 mode);
mm/shmem.c renamed from mm/shmem.c~userfaultfd-shmem-modify-shmem_mfill_atomic_pte-to-use-install_pte +12 -46
104 lines not on this page · raw message
mm/userfaultfd.c renamed from mm/userfaultfd.c~userfaultfd-shmem-modify-shmem_mfill_atomic_pte-to-use-install_pte +6 -11
@@ -51,18 +51,13 @@ struct vm_area_struct *find_dst_vma(stru/* * Install PTEs, to map dst_addr (within dst_vma) to page. * -* This function handles MCOPY_ATOMIC_CONTINUE (which is always file-backed), -* whether or not dst_vma is VM_SHARED. It also handles the more general -* MCOPY_ATOMIC_NORMAL case, when dst_vma is *not* VM_SHARED (it may be file -* backed, or not). -* -* Note that MCOPY_ATOMIC_NORMAL for a VM_SHARED dst_vma is handled by -* shmem_mcopy_atomic_pte instead. +* This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem +* and anon, and for both shared and private VMAs. */ -static int mfill_atomic_install_pte (struct mm_struct *dst_mm,pmd_t *dst_pmd, -struct vm_area_struct *dst_vma, -unsigned long dst_addr,struct page *page, -bool newly_allocated,bool wp_copy) +int mfill_atomic_install_pte (struct mm_struct *dst_mm,pmd_t *dst_pmd, +struct vm_area_struct *dst_vma, +unsigned long dst_addr,struct page *page, +bool newly_allocated,bool wp_copy) {int ret;pte_t _dst_pte, *dst_pte;
_
Andrew Morton
maintainerPATCH 45/192 userfaultfd/selftests: use memfd_create for shmem test type
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/selftests: use memfd_create for shmem test type
This is a preparatory commit. In the future, we want to be able to setup alias mappings for area_src and area_dst in the shmem test, like we do in the hugetlb_shared test. With a VMA obtained via mmap(MAP_ANONYMOUS | MAP_SHARED), it isn't clear how to do this.
So, mmap() with an fd, so we can create alias mappings. Use memfd_create instead of actually passing in a tmpfs path like hugetlb does, since it's more convenient / simpler to run, and works just as well.
Future commits will:
1. Setup the alias mappings.
2. Extend our tests to actually take advantage of this, to test new userfaultfd behavior being introduced in this series.
Also, a small fix in the area we're changing: when the hugetlb setup fails in main(), pass in the right argv[] so we actually print out the hugetlb file path.
---
tools/testing/selftests/vm/userfaultfd.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-use-memfd_create-for-shmem-test-type +15 -1
@@ -85,6 +85,7 @@ static bool test_uffdio_wp = false;static bool test_uffdio_minor = false;static bool map_shared; +static int shm_fd;static int huge_fd;static char *huge_fd_off0;static unsigned long long *count_verify;
@@ -277,8 +278,11 @@ static void shmem_release_pages(char *restatic void shmem_allocate_area (void **alloc_area) { +unsigned long offset = + alloc_area == (void **)&area_src ?0 : nr_pages * page_size; + *alloc_area =mmap (NULL , nr_pages * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_SHARED ,-1 ,0 ); +MAP_SHARED , shm_fd, offset);if (*alloc_area ==MAP_FAILED )err ("mmap of memfd failed" ); }
@@ -1602,6 +1606,16 @@ int main(int argc, char **argv)err ("Open of %s failed" , argv[4 ]);if (ftruncate (huge_fd,0 ))err ("ftruncate %s to size 0 failed" , argv[4 ]); + }else if (test_type ==TEST_SHMEM ) { + shm_fd =memfd_create (argv[0 ],0 ); +if (shm_fd <0 ) +err ("memfd_create" ); +if (ftruncate (shm_fd, nr_pages * page_size *2 )) +err ("ftruncate" ); +if (fallocate (shm_fd, +FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE ,0 , + nr_pages * page_size *2 )) +err ("fallocate" ); }printf ("nr_pages: %lu, nr_pages_per_cpu: %lu\n" , nr_pages, nr_pages_per_cpu);
_
Andrew Morton
maintainerPATCH 46/192 userfaultfd/selftests: create alias mappings in the shmem test
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/selftests: create alias mappings in the shmem test
Previously, we just allocated two shm areas: area_src and area_dst. With this commit, change this so we also allocate area_src_alias, and area_dst_alias.
area_*_alias and area_* (respectively) point to the same underlying physical pages, but are different VMAs. In a future commit in this series, we'll leverage this setup to exercise minor fault handling support for shmem, just like we do in the hugetlb_shared test.
---
tools/testing/selftests/vm/userfaultfd.c | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-create-alias-mappings-in-the-shmem-test +19 -3
@@ -278,13 +278,29 @@ static void shmem_release_pages(char *restatic void shmem_allocate_area (void **alloc_area) { -unsigned long offset = - alloc_area == (void **)&area_src ?0 : nr_pages * page_size; +void *area_alias =NULL ; +bool is_src = alloc_area == (void **)&area_src; +unsigned long offset = is_src ?0 : nr_pages * page_size; *alloc_area =mmap (NULL , nr_pages * page_size,PROT_READ |PROT_WRITE ,MAP_SHARED , shm_fd, offset);if (*alloc_area ==MAP_FAILED )err ("mmap of memfd failed" ); + + area_alias =mmap (NULL , nr_pages * page_size,PROT_READ |PROT_WRITE , +MAP_SHARED , shm_fd, offset); +if (area_alias ==MAP_FAILED ) +err ("mmap of memfd alias failed" ); + +if (is_src) + area_src_alias = area_alias; +else + area_dst_alias = area_alias; +} + +static void shmem_alias_mapping (__u64 *start,size_t len,unsigned long offset) +{ + *start = (unsigned long )area_dst_alias + offset; }struct uffd_test_ops {
@@ -314,7 +330,7 @@ static struct uffd_test_ops shmem_uffd_t .expected_ioctls = SHMEM_EXPECTED_IOCTLS, .allocate_area = shmem_allocate_area, .release_pages = shmem_release_pages, - .alias_mapping = noop_alias_mapping, + .alias_mapping = shmem_alias_mapping, };static struct uffd_test_ops hugetlb_uffd_test_ops = {
_
Andrew Morton
maintainerPATCH 47/192 userfaultfd/selftests: reinitialize test context in each test
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/selftests: reinitialize test context in each test
Currently, the context (fds, mmap-ed areas, etc.) are global. Each test mutates this state in some way, in some cases really "clobbering it" (e.g., the events test mremap-ing area_dst over the top of area_src, or the minor faults tests overwriting the count_verify values in the test areas). We run the tests in a particular order, each test is careful to make the right assumptions about its starting state, etc.
But, this is fragile. It's better for a test's success or failure to not depend on what some other prior test case did to the global state.
To that end, clear and reinitialize the test context at the start of each test case, so whatever prior test cases did doesn't affect future tests.
This is particularly relevant to this series because the events test's mremap of area_dst screws up assumptions the minor fault test was relying on. This wasn't a problem for hugetlb, as we don't mremap in that case.
---
tools/testing/selftests/vm/userfaultfd.c | 222 +++++++++++---------- 1 file changed, 117 insertions(+), 105 deletions(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-reinitialize-test-context-in-each-test +117 -105
@@ -89,7 +89,8 @@ static int shm_fd;static int huge_fd;static char *huge_fd_off0;static unsigned long long *count_verify; -static int uffd, uffd_flags, finished, *pipefd; +static int uffd =-1 ; +static int uffd_flags, finished, *pipefd;static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;static char *zeropage;pthread_attr_t attr;
@@ -342,6 +343,111 @@ static struct uffd_test_ops hugetlb_uffdstatic struct uffd_test_ops *uffd_test_ops; +static void userfaultfd_open (uint64_t *features) +{ +struct uffdio_api uffdio_api; + + uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK |UFFD_USER_MODE_ONLY ); +if (uffd <0 ) +err ("userfaultfd syscall not available in this kernel" ); + uffd_flags =fcntl (uffd,F_GETFD ,NULL ); + + uffdio_api.api =UFFD_API ; + uffdio_api.features = *features; +if (ioctl (uffd,UFFDIO_API , &uffdio_api)) +err ("UFFDIO_API failed.\nPlease make sure to " +"run with either root or ptrace capability." ); +if (uffdio_api.api !=UFFD_API ) +err ("UFFDIO_API error: %" PRIu64, (uint64_t )uffdio_api.api); + + *features = uffdio_api.features; +} + +static inline void munmap_area (void **area) +{ +if (*area) +if (munmap (*area, nr_pages * page_size)) +err ("munmap" ); + + *area =NULL ; +} + +static void uffd_test_ctx_clear (void ) +{ +size_t i; + +if (pipefd) { +for (i =0 ; i < nr_cpus *2 ; ++i) { +if (close (pipefd[i])) +err ("close pipefd" ); + } +free (pipefd); + pipefd =NULL ; + } + +if (count_verify) { +free (count_verify); + count_verify =NULL ; + } + +if (uffd !=-1 ) { +if (close (uffd)) +err ("close uffd" ); + uffd =-1 ; + } + + huge_fd_off0 =NULL ; +munmap_area ((void **)&area_src); +munmap_area ((void **)&area_src_alias); +munmap_area ((void **)&area_dst); +munmap_area ((void **)&area_dst_alias); +} + +static void uffd_test_ctx_init_ext (uint64_t *features) +{ +unsigned long nr, cpu; + +uffd_test_ctx_clear (); + + uffd_test_ops->allocate_area ((void **)&area_src); + uffd_test_ops->allocate_area ((void **)&area_dst); + + uffd_test_ops->release_pages (area_src); + uffd_test_ops->release_pages (area_dst); + +userfaultfd_open (features); + + count_verify =malloc (nr_pages *sizeof (unsigned long long )); +if (!count_verify) +err ("count_verify" ); + +for (nr =0 ; nr < nr_pages; nr++) { + *area_mutex (area_src, nr) = + (pthread_mutex_t )PTHREAD_MUTEX_INITIALIZER ; + count_verify[nr] = *area_count (area_src, nr) =1 ; +/* +* In the transition between 255 to 256, powerpc will +* read out of order in my_bcmp and see both bytes as +* zero, so leave a placeholder below always non-zero +* after the count, to avoid my_bcmp to trigger false +* positives. +*/ + *(area_count (area_src, nr) +1 ) =1 ; + } + + pipefd =malloc (sizeof (int ) * nr_cpus *2 ); +if (!pipefd) +err ("pipefd" ); +for (cpu =0 ; cpu < nr_cpus; cpu++) +if (pipe2 (&pipefd[cpu *2 ],O_CLOEXEC |O_NONBLOCK )) +err ("pipe" ); +} + +static inline void uffd_test_ctx_init (uint64_t features) +{ +uffd_test_ctx_init_ext (&features); +} +static int my_bcmp (char *str1,char *str2,size_t n) {unsigned long i;
@@ -726,40 +832,6 @@ static int stress(struct uffd_stats *uffreturn 0 ; } -static int userfaultfd_open_ext (uint64_t *features) -{ -struct uffdio_api uffdio_api; - - uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK |UFFD_USER_MODE_ONLY ); -if (uffd <0 ) { -fprintf (stderr, -"userfaultfd syscall not available in this kernel\n" ); -return 1 ; - } - uffd_flags =fcntl (uffd,F_GETFD ,NULL ); - - uffdio_api.api =UFFD_API ; - uffdio_api.features = *features; -if (ioctl (uffd,UFFDIO_API , &uffdio_api)) { -fprintf (stderr,"UFFDIO_API failed.\nPlease make sure to " -"run with either root or ptrace capability.\n" ); -return 1 ; - } -if (uffdio_api.api !=UFFD_API ) { -fprintf (stderr,"UFFDIO_API error: %" PRIu64"\n" , - (uint64_t )uffdio_api.api); -return 1 ; - } - - *features = uffdio_api.features; -return 0 ; -} - -static int userfaultfd_open (uint64_t features) -{ -return userfaultfd_open_ext (&features); -} -sigjmp_buf jbuf, *sigbuf;static void sighndl (int sig,siginfo_t *siginfo,void *ptr)
@@ -868,6 +940,8 @@ static int faulting_process(int signal_t MREMAP_MAYMOVE | MREMAP_FIXED, area_src);if (area_dst ==MAP_FAILED )err ("mremap" ); +/* Reset area_src since we just clobbered it */ + area_src =NULL ;for (; nr < nr_pages; nr++) { count = *area_count (area_dst, nr);
@@ -961,10 +1035,8 @@ static int userfaultfd_zeropage_test(voiprintf ("testing UFFDIO_ZEROPAGE: " );fflush (stdout); - uffd_test_ops->release_pages (area_dst); +uffd_test_ctx_init (0 ); -if (userfaultfd_open (0 )) -return 1 ; uffdio_register.range.start = (unsigned long ) area_dst; uffdio_register.range.len = nr_pages * page_size; uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;
@@ -981,7 +1053,6 @@ static int userfaultfd_zeropage_test(voiif (my_bcmp (area_dst, zeropage, page_size))err ("zeropage is not zero" ); -close (uffd);printf ("done.\n" );return 0 ; }
@@ -999,12 +1070,10 @@ static int userfaultfd_events_test(void)printf ("testing events (fork, remap, remove): " );fflush (stdout); - uffd_test_ops->release_pages (area_dst); - features =UFFD_FEATURE_EVENT_FORK |UFFD_FEATURE_EVENT_REMAP |UFFD_FEATURE_EVENT_REMOVE ; -if (userfaultfd_open (features)) -return 1 ; +uffd_test_ctx_init (features); +fcntl (uffd,F_SETFL , uffd_flags |O_NONBLOCK ); uffdio_register.range.start = (unsigned long ) area_dst;
@@ -1037,8 +1106,6 @@ static int userfaultfd_events_test(void)if (pthread_join (uffd_mon,NULL ))return 1 ; -close (uffd); -uffd_stats_report (&stats,1 );return stats.missing_faults != nr_pages;
@@ -1058,11 +1125,9 @@ static int userfaultfd_sig_test(void)printf ("testing signal delivery: " );fflush (stdout); - uffd_test_ops->release_pages (area_dst); - features =UFFD_FEATURE_EVENT_FORK |UFFD_FEATURE_SIGBUS ; -if (userfaultfd_open (features)) -return 1 ; +uffd_test_ctx_init (features); +fcntl (uffd,F_SETFL , uffd_flags |O_NONBLOCK ); uffdio_register.range.start = (unsigned long ) area_dst;
@@ -1103,7 +1168,6 @@ static int userfaultfd_sig_test(void)printf ("done.\n" );if (userfaults)err ("Signal test failed, userfaults: %ld" , userfaults); -close (uffd);return userfaults !=0 ; }
@@ -1126,10 +1190,7 @@ static int userfaultfd_minor_test(void)printf ("testing minor faults: " );fflush (stdout); - uffd_test_ops->release_pages (area_dst); - -if (userfaultfd_open_ext (&features)) -return 1 ; +uffd_test_ctx_init_ext (&features);/* If kernel reports the feature isn't supported, skip the test. */ if (!(features &UFFD_FEATURE_MINOR_HUGETLBFS )) {printf ("skipping test due to lack of feature support\n" );
@@ -1183,8 +1244,6 @@ static int userfaultfd_minor_test(void)if (pthread_join (uffd_mon,NULL ))return 1 ; -close (uffd); -uffd_stats_report (&stats,1 );return stats.missing_faults !=0 || stats.minor_faults != nr_pages;
@@ -1267,7 +1326,7 @@ static void userfaultfd_pagemap_test(uns/* Flush so it doesn't flush twice in parent/child later */ fflush (stdout); - uffd_test_ops->release_pages (area_dst); +uffd_test_ctx_init (0 );if (test_pgsize > page_size) {/* This is a thp test */
@@ -1279,9 +1338,6 @@ static void userfaultfd_pagemap_test(unserr ("madvise(MADV_NOHUGEPAGE) failed" ); } -if (userfaultfd_open (0 )) -err ("userfaultfd_open" ); - uffdio_register.range.start = (unsigned long ) area_dst; uffdio_register.range.len = nr_pages * page_size; uffdio_register.mode =UFFDIO_REGISTER_MODE_WP ;
@@ -1324,7 +1380,6 @@ static void userfaultfd_pagemap_test(unspagemap_check_wp (value, false);close (pagemap_fd); -close (uffd);printf ("done\n" ); }
@@ -1334,50 +1389,9 @@ static int userfaultfd_stress(void)char *tmp_area;unsigned long nr;struct uffdio_register uffdio_register; -unsigned long cpu;struct uffd_stats uffd_stats[nr_cpus]; - uffd_test_ops->allocate_area ((void **)&area_src); -if (!area_src) -return 1 ; - uffd_test_ops->allocate_area ((void **)&area_dst); -if (!area_dst) -return 1 ; - -if (userfaultfd_open (0 )) -return 1 ; - - count_verify =malloc (nr_pages *sizeof (unsigned long long )); -if (!count_verify) { -perror ("count_verify" ); -return 1 ; - } - -for (nr =0 ; nr < nr_pages; nr++) { - *area_mutex (area_src, nr) = (pthread_mutex_t ) -PTHREAD_MUTEX_INITIALIZER ; - count_verify[nr] = *area_count (area_src, nr) =1 ; -/* -* In the transition between 255 to 256, powerpc will -* read out of order in my_bcmp and see both bytes as -* zero, so leave a placeholder below always non-zero -* after the count, to avoid my_bcmp to trigger false -* positives. -*/ - *(area_count (area_src, nr) +1 ) =1 ; - } - - pipefd =malloc (sizeof (int ) * nr_cpus *2 ); -if (!pipefd) { -perror ("pipefd" ); -return 1 ; - } -for (cpu =0 ; cpu < nr_cpus; cpu++) { -if (pipe2 (&pipefd[cpu*2 ],O_CLOEXEC |O_NONBLOCK )) { -perror ("pipe" ); -return 1 ; - } - } +uffd_test_ctx_init (0 );if (posix_memalign (&area, page_size, page_size))err ("out of memory" );
@@ -1498,8 +1512,6 @@ static int userfaultfd_stress(void)uffd_stats_report (uffd_stats, nr_cpus); } -close (uffd); -if (test_type ==TEST_ANON ) {/* * shmem/hugetlb won't be able to run since they have different
_
Andrew Morton
maintainerPATCH 48/192 userfaultfd/selftests: exercise minor fault handling shmem support
In reply to Andrew MortonFrom: Axel Rasmussen <[email protected]>
Subject: userfaultfd/selftests: exercise minor fault handling shmem support
Enable test_uffdio_minor for test_type == TEST_SHMEM, and modify the test slightly to pass in / check for the right feature flags.
---
tools/testing/selftests/vm/userfaultfd.c | 29 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-)
tools/testing/selftests/vm/userfaultfd.c renamed from tools/testing/selftests/vm/userfaultfd.c~userfaultfd-selftests-exercise-minor-fault-handling-shmem-support +25 -4
@@ -474,6 +474,7 @@ static void wp_range(int ufd, __u64 starstatic void continue_range (int ufd,__u64 start,__u64 len) {struct uffdio_continue req; +int ret; req.range.start = start; req.range.len = len;
@@ -482,6 +483,17 @@ static void continue_range(int ufd, __u6if (ioctl (ufd,UFFDIO_CONTINUE , &req))err ("UFFDIO_CONTINUE failed for address 0x%" PRIx64, (uint64_t )start); + +/* +* Error handling within the kernel for continue is subtly different +* from copy or zeropage, so it may be a source of bugs. Trigger an +* error (-EEXIST) on purpose, to verify doing so doesn't cause a BUG. +*/ + req.mapped =0 ; + ret =ioctl (ufd,UFFDIO_CONTINUE , &req); +if (ret >=0 || req.mapped != -EEXIST ) +err ("failed to exercise UFFDIO_CONTINUE error handling, ret=%d, mapped=%" PRId64, + ret, (int64_t ) req.mapped); }static void *locking_thread (void *arg)
@@ -1182,7 +1194,7 @@ static int userfaultfd_minor_test(void)void *expected_page;char c;struct uffd_stats stats = {0 }; -uint64_t features =UFFD_FEATURE_MINOR_HUGETLBFS ; +uint64_t req_features, features_out;if (!test_uffdio_minor)return 0 ;
@@ -1190,9 +1202,17 @@ static int userfaultfd_minor_test(void)printf ("testing minor faults: " );fflush (stdout); -uffd_test_ctx_init_ext (&features); -/* If kernel reports the feature isn't supported, skip the test. */ -if (!(features &UFFD_FEATURE_MINOR_HUGETLBFS )) { +if (test_type ==TEST_HUGETLB ) + req_features =UFFD_FEATURE_MINOR_HUGETLBFS ; +else if (test_type ==TEST_SHMEM ) + req_features =UFFD_FEATURE_MINOR_SHMEM ; +else +return 1 ; + + features_out = req_features; +uffd_test_ctx_init_ext (&features_out); +/* If kernel reports required features aren't supported, skip test. */ +if ((features_out & req_features) != req_features) {printf ("skipping test due to lack of feature support\n" );fflush (stdout);return 0 ;
@@ -1575,6 +1595,7 @@ static void set_test_type(const char *ty map_shared = true; test_type =TEST_SHMEM ; uffd_test_ops = &shmem_uffd_test_ops; + test_uffdio_minor = true; }else {err ("Unknown test type: %s" , type); }
_
Andrew Morton
maintainerPATCH 49/192 mm/vmscan.c: fix potential deadlock in reclaim_pages()
In reply to Andrew MortonFrom: Yu Zhao <[email protected]>
Subject: mm/vmscan.c: fix potential deadlock in reclaim_pages()
Theoretically without the protect from memalloc_noreclaim_save() and memalloc_noreclaim_restore(), reclaim_pages() can go into the block I/O layer recursively and deadlock.
Querying 'reclaim_pages' in our kernel crash databases didn't yield any results. So the deadlock seems unlikely to happen. A possible explanation is that the only user of reclaim_pages(), i.e., MADV_PAGEOUT, is usually called before memory pressure builds up, e.g., on Android and Chrome OS. Under such a condition, allocations in the block I/O layer can be fulfilled without diverting to direct reclaim and therefore the recursion is avoided.
---
mm/vmscan.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscanc-fix-potential-deadlock-in-reclaim_pages +15 -0
@@ -1701,6 +1701,7 @@ unsigned int reclaim_clean_pages_from_liunsigned int nr_reclaimed;struct page *page, *next;LIST_HEAD (clean_pages); +unsigned int noreclaim_flag;list_for_each_entry_safe (page, next, page_list, lru) { if (!PageHuge(page) && page_is_file_lru(page) &&
@@ -1711,8 +1712,17 @@ unsigned int reclaim_clean_pages_from_li } } +/* +* We should be safe here since we are only dealing with file pages and +* we are not kswapd and therefore cannot write dirty file pages. But +* call memalloc_noreclaim_save() anyway, just in case these conditions +* change in the future. +*/ + noreclaim_flag =memalloc_noreclaim_save (); nr_reclaimed =shrink_page_list (&clean_pages, zone->zone_pgdat, &sc, &stat, true); +memalloc_noreclaim_restore (noreclaim_flag); +list_splice (&clean_pages, page_list);mod_node_page_state (zone->zone_pgdat,NR_ISOLATED_FILE , -(long )nr_reclaimed);
@@ -2306,6 +2316,7 @@ unsigned long reclaim_pages(struct list_LIST_HEAD (node_page_list);struct reclaim_stat dummy_stat;struct page *page; +unsigned int noreclaim_flag;struct scan_control sc = { .gfp_mask =GFP_KERNEL , .priority =DEF_PRIORITY ,
@@ -2314,6 +2325,8 @@ unsigned long reclaim_pages(struct list_ .may_swap = 1, }; + noreclaim_flag =memalloc_noreclaim_save (); +while (!list_empty (page_list)) { page =lru_to_page (page_list);if (nid ==NUMA_NO_NODE ) {
@@ -2350,6 +2363,8 @@ unsigned long reclaim_pages(struct list_ } } +memalloc_noreclaim_restore (noreclaim_flag); +return nr_reclaimed; }
_
Andrew Morton
maintainerPATCH 50/192 include/trace/events/vmscan.h: remove mm_vmscan_inactive_list_is_low
In reply to Andrew MortonFrom: Yu Zhao <[email protected]>
Subject: include/trace/events/vmscan.h: remove mm_vmscan_inactive_list_is_low
mm_vmscan_inactive_list_is_low has no users after commit b91ac374346b ("mm: vmscan: enforce inactive:active ratio at the reclaim root").
Remove it.
---
include/trace/events/vmscan.h | 41 -------------------------------- 1 file changed, 41 deletions(-)
include/trace/events/vmscan.h renamed from include/trace/events/vmscan.h~include-trace-events-vmscanh-remove-mm_vmscan_inactive_list_is_low +0 -41
@@ -423,47 +423,6 @@ TRACE_EVENT(mm_vmscan_lru_shrink_active, show_reclaim_flags(__entry->reclaim_flags)) ); -TRACE_EVENT (mm_vmscan_inactive_list_is_low, - - TP_PROTO(int nid, int reclaim_idx, - unsigned long total_inactive, unsigned long inactive, - unsigned long total_active, unsigned long active, - unsigned long ratio, int file), - -TP_ARGS (nid, reclaim_idx, total_inactive, inactive, total_active, active, ratio, file), - -TP_STRUCT__entry ( - __field(int, nid) - __field(int, reclaim_idx) - __field(unsigned long, total_inactive) - __field(unsigned long, inactive) - __field(unsigned long, total_active) - __field(unsigned long, active) - __field(unsigned long, ratio) -__field (int, reclaim_flags) - ), - -TP_fast_assign ( - __entry->nid = nid; - __entry->reclaim_idx = reclaim_idx; - __entry->total_inactive = total_inactive; - __entry->inactive = inactive; - __entry->total_active = total_active; - __entry->active = active; - __entry->ratio = ratio; - __entry->reclaim_flags =trace_reclaim_flags (file) & - RECLAIM_WB_LRU; - ), - -TP_printk ("nid=%d reclaim_idx=%d total_inactive=%ld inactive=%ld total_active=%ld active=%ld ratio=%ld flags=%s" , - __entry->nid, - __entry->reclaim_idx, - __entry->total_inactive, __entry->inactive, - __entry->total_active, __entry->active, - __entry->ratio, -show_reclaim_flags (__entry->reclaim_flags)) -); - TRACE_EVENT(mm_vmscan_node_reclaim_begin, TP_PROTO(int nid, int order, gfp_t gfp_flags),
_
Andrew Morton
maintainerPATCH 51/192 mm: workingset: define macro WORKINGSET_SHIFT
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm: workingset: define macro WORKINGSET_SHIFT
The magic number 1 is used in several places in workingset.c. Define a macro WORKINGSET_SHIFT for it to improve code readability.
---
mm/workingset.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
mm/workingset.c renamed from mm/workingset.c~mm-workingset-define-macro-workingset_shift +6 -4
@@ -168,8 +168,10 @@* refault distance will immediately activate the refaulting page. */ +#define WORKINGSET_SHIFT 1#define EVICTION_SHIFT ((BITS_PER_LONG - BITS_PER_XA_VALUE) + \ - 1 + NODES_SHIFT + MEM_CGROUP_ID_SHIFT) + WORKINGSET_SHIFT + NODES_SHIFT + \ + MEM_CGROUP_ID_SHIFT)#define EVICTION_MASK (~0UL >> EVICTION_SHIFT)/*
@@ -189,7 +191,7 @@ static void *pack_shadow(int memcgid, pg eviction &=EVICTION_MASK ; eviction = (eviction <<MEM_CGROUP_ID_SHIFT ) | memcgid; eviction = (eviction <<NODES_SHIFT ) | pgdat->node_id; - eviction = (eviction <<1 ) | workingset; + eviction = (eviction <<WORKINGSET_SHIFT ) | workingset;return xa_mk_value (eviction); }
@@ -201,8 +203,8 @@ static void unpack_shadow(void *shadow,int memcgid, nid;bool workingset; - workingset = entry &1 ; - entry >>=1 ; + workingset = entry & ((1UL <<WORKINGSET_SHIFT ) -1 ); + entry >>=WORKINGSET_SHIFT ; nid = entry & ((1UL <<NODES_SHIFT ) -1 ); entry >>=NODES_SHIFT ; memcgid = entry & ((1UL <<MEM_CGROUP_ID_SHIFT ) -1 );
_
Andrew Morton
maintainerPATCH 52/192 mm/kconfig: move HOLES_IN_ZONE into mm
In reply to Andrew MortonFrom: Kefeng Wang <[email protected]>
Subject: mm/kconfig: move HOLES_IN_ZONE into mm
commit a55749639dc1 ("ia64: drop marked broken DISCONTIGMEM and VIRTUAL_MEM_MAP") drop VIRTUAL_MEM_MAP, so there is no need HOLES_IN_ZONE on ia64.
Also move HOLES_IN_ZONE into mm/Kconfig, select it if architecture needs this feature.
---
arch/arm64/Kconfig | 4 +--- arch/ia64/Kconfig | 3 --- arch/mips/Kconfig | 3 --- mm/Kconfig | 3 +++ 4 files changed, 4 insertions(+), 9 deletions(-)
arch/arm64/Kconfig renamed from arch/arm64/Kconfig~mm-move-holes_in_zone-into-mm +1 -3
@@ -201,6 +201,7 @@ config ARM64 select HAVE_KPROBES select HAVE_KRETPROBES select HAVE_GENERIC_VDSO + select HOLES_IN_ZONE select IOMMU_DMA if IOMMU_SUPPORT select IRQ_DOMAIN select IRQ_FORCED_THREADING
@@ -1052,9 +1053,6 @@ config NEED_PER_CPU_EMBED_FIRST_CHUNK def_bool y depends on NUMA -config HOLES_IN_ZONE - def_bool y - source "kernel/Kconfig.hz" config ARCH_SPARSEMEM_ENABLE
arch/ia64/Kconfig renamed from arch/ia64/Kconfig~mm-move-holes_in_zone-into-mm +0 -3
@@ -308,9 +308,6 @@ config NODES_SHIFT MAX_NUMNODES will be 2^(This value). If in doubt, use the default. -config HOLES_IN_ZONE - bool - config HAVE_ARCH_NODEDATA_EXTENSION def_bool y depends on NUMA
arch/mips/Kconfig renamed from arch/mips/Kconfig~mm-move-holes_in_zone-into-mm +0 -3
@@ -1233,9 +1233,6 @@ config HAVE_PLAT_MEMCPY config ISA_DMA_API bool -config HOLES_IN_ZONE - bool - config SYS_SUPPORTS_RELOCATABLE bool help
mm/Kconfig renamed from mm/Kconfig~mm-move-holes_in_zone-into-mm +3 -0
@@ -96,6 +96,9 @@ config HAVE_FAST_GUP depends on MMU bool +config HOLES_IN_ZONE + bool + # Don't discard allocated memory used to track "memory" and "reserved" memblocks # after early boot, so it can still be used to test for validity of memory. # Also, memblocks are updated with memory hot(un)plug.
_
Andrew Morton
maintainerPATCH 53/192 docs: proc.rst: meminfo: briefly describe gaps in memory accounting
In reply to Andrew MortonFrom: Mike Rapoport <[email protected]>
Subject: docs: proc.rst: meminfo: briefly describe gaps in memory accounting
Add a paragraph that explains that it may happen that the counters in /proc/meminfo do not add up to the overall memory usage.
---
Documentation/filesystems/proc.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
Documentation/filesystems/proc.rst renamed from Documentation/filesystems/proc.rst~docs-procrst-meminfo-briefly-describe-gaps-in-memory-accounting +9 -2
@@ -933,8 +933,15 @@ meminfo ~~~~~~~ Provides information about distribution and utilization of memory. This -varies by architecture and compile options. The following is from a -16GB PIII, which has highmem enabled. You may not have all of these fields. +varies by architecture and compile options. Some of the counters reported +here overlap. The memory reported by the non overlapping counters may not +add up to the overall memory usage and the difference for some workloads +can be substantial. In many cases there are other means to find out +additional memory using subsystem specific interfaces, for instance +/proc/net/sockstat for TCP memory allocations. + +The following is from a 16GB PIII, which has highmem enabled. +You may not have all of these fields. ::
_
Andrew Morton
maintainerPATCH 54/192 fs/proc/kcore: drop KCORE_REMAP and KCORE_OTHER
In reply to Andrew MortonFrom: David Hildenbrand <[email protected]>
Subject: fs/proc/kcore: drop KCORE_REMAP and KCORE_OTHER
Patch series "fs/proc/kcore: don't read offline sections, logically offline pages and hwpoisoned pages", v3.
Looking for places where the kernel might unconditionally read PageOffline() pages, I stumbled over /proc/kcore; turns out /proc/kcore needs some more love to not touch some other pages we really don't want to read -- i.e., hwpoisoned ones.
Examples for PageOffline() pages are pages inflated in a balloon, memory unplugged via virtio-mem, and partially-present sections in memory added by the Hyper-V balloon.
When reading pages inflated in a balloon, we essentially produce unnecessary load in the hypervisor; holes in partially present sections in case of Hyper-V are not accessible and already were a problem for /proc/vmcore, fixed in makedumpfile by detecting PageOffline() pages. In the future, virtio-mem might disallow reading unplugged memory -- marked as PageOffline() -- in some environments, resulting in undefined behavior when accessed; therefore, I'm trying to identify and rework all these (corner) cases.
With this series, there is really only access via /dev/mem, /proc/vmcore and kdb left after I ripped out /dev/kmem. kdb is an advanced corner-case use case -- we won't care for now if someone explicitly tries to do nasty things by reading from/writing to physical addresses we better not touch. /dev/mem is a use case we won't support for virtio-mem, at least for now, so we'll simply disallow mapping any virtio-mem memory via /dev/mem next. /proc/vmcore is really only a problem when dumping the old kernel via something that's not makedumpfile (read: basically never), however, we'll try sanitizing that as well in the second kernel in the future.
Tested via kcore_dump:
https://github.com/schlafwandler/kcore_dump
This patch (of 6):
Commit db779ef67ffe ("proc/kcore: Remove unused kclist_add_remap()") removed the last user of KCORE_REMAP.
Commit 595dd46ebfc1 ("vfs/proc/kcore, x86/mm/kcore: Fix SMAP fault when dumping vsyscall user page") removed the last user of KCORE_OTHER.
Let's drop both types. While at it, also drop vaddr in "struct kcore_list", used by KCORE_REMAP only.
---
fs/proc/kcore.c | 7 ++----- include/linux/kcore.h | 3 --- 2 files changed, 2 insertions(+), 8 deletions(-)
fs/proc/kcore.c renamed from fs/proc/kcore.c~fs-proc-kcore-drop-kcore_remap-and-kcore_other +2 -5
@@ -380,11 +380,8 @@ read_kcore(struct file *file, char __use phdr->p_type =PT_LOAD ; phdr->p_flags =PF_R |PF_W |PF_X ; phdr->p_offset =kc_vaddr_to_offset (m->addr) + data_offset; -if (m->type ==KCORE_REMAP ) - phdr->p_vaddr = (size_t )m->vaddr; -else - phdr->p_vaddr = (size_t )m->addr; -if (m->type ==KCORE_RAM || m->type ==KCORE_REMAP ) + phdr->p_vaddr = (size_t )m->addr; +if (m->type ==KCORE_RAM ) phdr->p_paddr =__pa (m->addr);else if (m->type ==KCORE_TEXT ) phdr->p_paddr =__pa_symbol (m->addr);
include/linux/kcore.h renamed from include/linux/kcore.h~fs-proc-kcore-drop-kcore_remap-and-kcore_other +0 -3
@@ -11,14 +11,11 @@ enum kcore_type {KCORE_RAM ,KCORE_VMEMMAP , KCORE_USER, -KCORE_OTHER , - KCORE_REMAP, };struct kcore_list {struct list_head list;unsigned long addr; -unsigned long vaddr;size_t size;int type; };
_
Andrew Morton
maintainerPATCH 55/192 fs/proc/kcore: pfn_is_ram check only applies to KCORE_RAM
In reply to Andrew MortonFrom: David Hildenbrand <[email protected]>
Subject: fs/proc/kcore: pfn_is_ram check only applies to KCORE_RAM
Let's resturcture the code, using switch-case, and checking pfn_is_ram() only when we are dealing with KCORE_RAM.
---
fs/proc/kcore.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-)
fs/proc/kcore.c renamed from fs/proc/kcore.c~fs-proc-kcore-pfn_is_ram-check-only-applies-to-kcore_ram +27 -8
@@ -483,25 +483,36 @@ read_kcore(struct file *file, char __use goto out; } m =NULL ;/* skip the list anchor */ - }else if (!pfn_is_ram (__pa (start) >>PAGE_SHIFT )) { -if (clear_user (buffer, tsz)) { - ret = -EFAULT ; - goto out; - } - }else if (m->type ==KCORE_VMALLOC ) { + goto skip; + } + +switch (m->type) { +case KCORE_VMALLOC :vread (buf, (char *)start, tsz);/* we have to zero-fill user buffer even if no read */ if (copy_to_user (buffer, buf, tsz)) { ret = -EFAULT ; goto out; } - }else if (m->type ==KCORE_USER ) { +break ; +case KCORE_USER :/* User page is handled prior to normal kernel page: */ if (copy_to_user (buffer, (char *)start, tsz)) { ret = -EFAULT ; goto out; } - }else { +break ; +case KCORE_RAM : +if (!pfn_is_ram (__pa (start) >>PAGE_SHIFT )) { +if (clear_user (buffer, tsz)) { + ret = -EFAULT ; + goto out; + } +break ; + } + fallthrough; +case KCORE_VMEMMAP : +case KCORE_TEXT :if (kern_addr_valid (start)) {/* * Using bounce buffer to bypass the
@@ -525,7 +536,15 @@ read_kcore(struct file *file, char __use goto out; } } +break ; +default : +pr_warn_once ("Unhandled KCORE type: %d\n" , m->type); +if (clear_user (buffer, tsz)) { + ret = -EFAULT ; + goto out; + } } +skip: buflen -= tsz; *fpos += tsz; buffer += tsz;
_
Andrew Morton
maintainerPATCH 56/192 fs/proc/kcore: don't read offline sections, logically offline pages and hwpoisoned pages
In reply to Andrew MortonFrom: David Hildenbrand <[email protected]>
Subject: fs/proc/kcore: don't read offline sections, logically offline pages and hwpoisoned pages
Let's avoid reading:
1) Offline memory sections: the content of offline memory sections is stale as the memory is effectively unused by the kernel. On s390x with standby memory, offline memory sections (belonging to offline storage increments) are not accessible. With virtio-mem and the hyper-v balloon, we can have unavailable memory chunks that should not be accessed inside offline memory sections. Last but not least, offline memory sections might contain hwpoisoned pages which we can no longer identify because the memmap is stale.
2) PG_offline pages: logically offline pages that are documented as "The content of these pages is effectively stale. Such pages should not be touched (read/write/dump/save) except by their owner.". Examples include pages inflated in a balloon or unavailble memory ranges inside hotplugged memory sections with virtio-mem or the hyper-v balloon.
3) PG_hwpoison pages: Reading pages marked as hwpoisoned can be fatal. As documented: "Accessing is not safe since it may cause another machine check. Don't touch!"
Introduce is_page_hwpoison(), adding a comment that it is inherently racy but best we can really do.
Reading /proc/kcore now performs similar checks as when reading /proc/vmcore for kdump via makedumpfile: problematic pages are exclude. It's also similar to hibernation code, however, we don't skip hwpoisoned pages when processing pages in kernel/power/snapshot.c:saveable_page() yet.
Note 1: we can race against memory offlining code, especially memory going offline and getting unplugged: however, we will properly tear down the identity mapping and handle faults gracefully when accessing this memory from kcore code.
Note 2: we can race against drivers setting PageOffline() and turning memory inaccessible in the hypervisor. We'll handle this in a follow-up patch.
---
fs/proc/kcore.c | 14 +++++++++++++- include/linux/page-flags.h | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-)
fs/proc/kcore.c renamed from fs/proc/kcore.c~fs-proc-kcore-dont-read-offline-sections-logically-offline-pages-and-hwpoisoned-pages +13 -1
@@ -465,6 +465,9 @@ read_kcore(struct file *file, char __use m =NULL ;while (buflen) { +struct page *page; +unsigned long pfn; +/* * If this is the first iteration or the address is not within * the previous entry, search for a matching entry.
@@ -503,7 +506,16 @@ read_kcore(struct file *file, char __use }break ;case KCORE_RAM : -if (!pfn_is_ram (__pa (start) >>PAGE_SHIFT )) { + pfn =__pa (start) >>PAGE_SHIFT ; + page =pfn_to_online_page (pfn); + +/* +* Don't read offline sections, logically offline pages +* (e.g., inflated in a balloon), hwpoisoned pages, +* and explicitly excluded physical ranges. +*/ +if (!page ||PageOffline (page) || +is_page_hwpoison (page) || !pfn_is_ram (pfn)) {if (clear_user (buffer, tsz)) { ret = -EFAULT ; goto out;
include/linux/page-flags.h renamed from include/linux/page-flags.h~fs-proc-kcore-dont-read-offline-sections-logically-offline-pages-and-hwpoisoned-pages +12 -0
@@ -695,6 +695,18 @@ PAGEFLAG_FALSE(DoubleMap)#endif /* +* Check if a page is currently marked HWPoisoned. Note that this check is +* best effort only and inherently racy: there is no way to synchronize with +* failing hardware. +*/ +static inline bool is_page_hwpoison (struct page *page) +{ +if (PageHWPoison (page)) +return true; +return PageHuge (page) &&PageHWPoison (compound_head (page)); +} + +/* * For pages that are never mapped to userspace (and aren't PageSlab), * page_type may be used. Because it is initialised to -1, we invert the * sense of the bit, so __SetPageFoo *clears* the bit used for PageFoo, and
_
Andrew Morton
maintainerPATCH 57/192 mm: introduce page_offline_(begin|end|freeze|thaw) to synchronize setting PageOffline()
In reply to Andrew MortonFrom: David Hildenbrand <[email protected]>
Subject: mm: introduce page_offline_(begin|end|freeze|thaw) to synchronize setting PageOffline()
A driver might set a page logically offline -- PageOffline() -- and turn the page inaccessible in the hypervisor; after that, access to page content can be fatal. One example is virtio-mem; while unplugged memory -- marked as PageOffline() can currently be read in the hypervisor, this will no longer be the case in the future; for example, when having a virtio-mem device backed by huge pages in the hypervisor.
Some special PFN walkers -- i.e., /proc/kcore -- read content of random pages after checking PageOffline(); however, these PFN walkers can race with drivers that set PageOffline().
Let's introduce page_offline_(begin|end|freeze|thaw) for synchronizing.
page_offline_freeze()/page_offline_thaw() allows for a subsystem to synchronize with such drivers, achieving that a page cannot be set PageOffline() while frozen.
page_offline_begin()/page_offline_end() is used by drivers that care about such races when setting a page PageOffline().
For simplicity, use a rwsem for now; neither drivers nor users are performance sensitive.
---
include/linux/page-flags.h | 10 ++++++++ mm/util.c | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)
include/linux/page-flags.h renamed from include/linux/page-flags.h~mm-introduce-page_offline_beginendfreezethaw-to-synchronize-setting-pageoffline +10 -0
@@ -769,9 +769,19 @@ PAGE_TYPE_OPS(Buddy, buddy)* relies on this feature is aware that re-onlining the memory block will * require to re-set the pages PageOffline() and not giving them to the * buddy via online_page_callback_t. +* +* There are drivers that mark a page PageOffline() and expect there won't be +* any further access to page content. PFN walkers that read content of random +* pages should check PageOffline() and synchronize with such drivers using +* page_offline_freeze()/page_offline_thaw(). */ PAGE_TYPE_OPS(Offline, offline) +extern void page_offline_freeze (void ); +extern void page_offline_thaw (void ); +extern void page_offline_begin (void ); +extern void page_offline_end (void ); +/* * Marks pages in use as page tables. */
mm/util.c renamed from mm/util.c~mm-introduce-page_offline_beginendfreezethaw-to-synchronize-setting-pageoffline +40 -0
@@ -1010,3 +1010,43 @@ void mem_dump_obj(void *object) }EXPORT_SYMBOL_GPL (mem_dump_obj);#endif + +/* +* A driver might set a page logically offline -- PageOffline() -- and +* turn the page inaccessible in the hypervisor; after that, access to page +* content can be fatal. +* +* Some special PFN walkers -- i.e., /proc/kcore -- read content of random +* pages after checking PageOffline(); however, these PFN walkers can race +* with drivers that set PageOffline(). +* +* page_offline_freeze()/page_offline_thaw() allows for a subsystem to +* synchronize with such drivers, achieving that a page cannot be set +* PageOffline() while frozen. +* +* page_offline_begin()/page_offline_end() is used by drivers that care about +* such races when setting a page PageOffline(). +*/ +static DECLARE_RWSEM (page_offline_rwsem); + +void page_offline_freeze (void ) +{ +down_read (&page_offline_rwsem); +} + +void page_offline_thaw (void ) +{ +up_read (&page_offline_rwsem); +} + +void page_offline_begin (void ) +{ +down_write (&page_offline_rwsem); +} +EXPORT_SYMBOL (page_offline_begin); + +void page_offline_end (void ) +{ +up_write (&page_offline_rwsem); +} +EXPORT_SYMBOL (page_offline_end);
_
Andrew Morton
maintainerPATCH 58/192 virtio-mem: use page_offline_(start|end) when setting PageOffline()
In reply to Andrew MortonFrom: David Hildenbrand <[email protected]>
Subject: virtio-mem: use page_offline_(start|end) when setting PageOffline()
Let's properly use page_offline_(start|end) to synchronize setting PageOffline(), so we won't have valid page access to unplugged memory regions from /proc/kcore.
Existing balloon implementations usually allow reading inflated memory; doing so might result in unnecessary overhead in the hypervisor, which is currently the case with virtio-mem.
For future virtio-mem use cases, it will be different when using shmem, huge pages, !anonymous private mappings, ... as backing storage for a VM. virtio-mem unplugged memory must no longer be accessed and access might result in undefined behavior. There will be a virtio spec extension to document this change, including a new feature flag indicating the changed behavior. We really don't want to race against PFN walkers reading random page content.
---
drivers/virtio/virtio_mem.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/virtio/virtio_mem.c renamed from drivers/virtio/virtio_mem.c~virtio-mem-use-page_offline_startend-when-setting-pageoffline +2 -0
@@ -1065,6 +1065,7 @@ static int virtio_mem_memory_notifier_cbstatic void virtio_mem_set_fake_offline (unsigned long pfn,unsigned long nr_pages,bool onlined) { +page_offline_begin ();for (; nr_pages--; pfn++) {struct page *page =pfn_to_page (pfn);
@@ -1075,6 +1076,7 @@ static void virtio_mem_set_fake_offline(ClearPageReserved (page); } } +page_offline_end (); }/*
_
Andrew Morton
maintainerPATCH 59/192 fs/proc/kcore: use page_offline_(freeze|thaw)
In reply to Andrew MortonFrom: David Hildenbrand <[email protected]>
Subject: fs/proc/kcore: use page_offline_(freeze|thaw)
Let's properly synchronize with drivers that set PageOffline(). Unfreeze/thaw every now and then, so drivers that want to set PageOffline() can make progress.
---
fs/proc/kcore.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
fs/proc/kcore.c renamed from fs/proc/kcore.c~fs-proc-kcore-use-page_offline_freezethaw +13 -0
@@ -313,6 +313,7 @@ read_kcore(struct file *file, char __use {char *buf = file->private_data;size_t phdrs_offset, notes_offset, data_offset; +size_t page_offline_frozen =1 ;size_t phdrs_len, notes_len;struct kcore_list *m;size_t tsz;
@@ -322,6 +323,11 @@ read_kcore(struct file *file, char __useint ret =0 ;down_read (&kclist_lock); +/* +* Don't race against drivers that set PageOffline() and expect no +* further page access. +*/ +page_offline_freeze ();get_kcore_size (&nphdr, &phdrs_len, ¬es_len, &data_offset); phdrs_offset =sizeof (struct elfhdr );
@@ -480,6 +486,12 @@ read_kcore(struct file *file, char __use } } +if (page_offline_frozen++ %MAX_ORDER_NR_PAGES ==0 ) { +page_offline_thaw (); +cond_resched (); +page_offline_freeze (); + } +if (&m->list == &kclist_head) {if (clear_user (buffer, tsz)) { ret = -EFAULT ;
@@ -565,6 +577,7 @@ skip: } out: +page_offline_thaw ();up_read (&kclist_lock);if (ret)return ret;
_
Andrew Morton
maintainerPATCH 60/192 mm/z3fold: define macro NCHUNKS as TOTAL_CHUNKS - ZHDR_CHUNKS
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/z3fold: define macro NCHUNKS as TOTAL_CHUNKS - ZHDR_CHUNKS
Patch series "Cleanup and fixup for z3fold".
This series contains cleanups to remove unused function, redefine macro to improve readability and so on. Also this fixes several bugs in z3fold, such as memory leak in z3fold_destroy_pool(). More details can be found in the respective changelogs.
This patch (of 6):
To improve code readability, we could define macro NCHUNKS as TOTAL_CHUNKS
- ZHDR_CHUNKS. No functional change intended.
---
mm/z3fold.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/z3fold.c renamed from mm/z3fold.c~mm-z3fold-define-macro-nchunks-as-total_chunks-zhdr_chunks +1 -1
@@ -62,7 +62,7 @@#define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)#define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)#define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT) -#define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT) +#define NCHUNKS (TOTAL_CHUNKS - ZHDR_CHUNKS)#define BUDDY_MASK (0x3)#define BUDDY_SHIFT 2
_
Andrew Morton
maintainerPATCH 61/192 mm/z3fold: avoid possible underflow in z3fold_alloc()
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/z3fold: avoid possible underflow in z3fold_alloc()
It is not enough to just make sure the z3fold header is not larger than the page size. When z3fold header is equal to PAGE_SIZE, we would underflow when check alloc size against PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE in z3fold_alloc(). Make sure there has remaining spaces for its buddy to fix this theoretical issue.
---
mm/z3fold.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
mm/z3fold.c renamed from mm/z3fold.c~mm-z3fold-avoid-possible-underflow-in-z3fold_alloc +5 -2
@@ -1803,8 +1803,11 @@ static int __init init_z3fold(void) {int ret; -/* Make sure the z3fold header is not larger than the page size */ -BUILD_BUG_ON (ZHDR_SIZE_ALIGNED >PAGE_SIZE ); +/* +* Make sure the z3fold header is not larger than the page size and +* there has remaining spaces for its buddy. +*/ +BUILD_BUG_ON (ZHDR_SIZE_ALIGNED >PAGE_SIZE -CHUNK_SIZE ); ret =z3fold_mount ();if (ret)return ret;
_
Andrew Morton
maintainerPATCH 62/192 mm/z3fold: remove magic number in z3fold_create_pool()
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/z3fold: remove magic number in z3fold_create_pool()
It's meaningless to pass a magic number 2 to __alloc_percpu() as there is a minimum alignment size of PCPU_MIN_ALLOC_SIZE (> 2) in it. Also there is no special alignment requirement for unbuddied. So we could replace this magic number with nature alignment, i.e. __alignof__(struct list_head), to improve readability.
---
mm/z3fold.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/z3fold.c renamed from mm/z3fold.c~mm-z3fold-remove-magic-number-in-z3fold_create_pool +2 -1
@@ -998,7 +998,8 @@ static struct z3fold_pool *z3fold_create goto out_c;spin_lock_init (&pool->lock);spin_lock_init (&pool->stale_lock); - pool->unbuddied =__alloc_percpu (sizeof (struct list_head )*NCHUNKS ,2 ); + pool->unbuddied =__alloc_percpu (sizeof (struct list_head ) *NCHUNKS , + __alignof__(struct list_head ));if (!pool->unbuddied) goto out_pool;for_each_possible_cpu (cpu) {
_
Andrew Morton
maintainerPATCH 63/192 mm/z3fold: remove unused function handle_to_z3fold_header()
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/z3fold: remove unused function handle_to_z3fold_header()
handle_to_z3fold_header() is unused now. So we can remove it. As a result, get_z3fold_header() becomes the only caller of __get_z3fold_header() and the argument lock is always true. Therefore we could further fold the __get_z3fold_header() into get_z3fold_header() with lock = true.
---
mm/z3fold.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-)
mm/z3fold.c renamed from mm/z3fold.c~mm-z3fold-remove-unused-function-handle_to_z3fold_header +4 -18
@@ -253,9 +253,8 @@ static inline void z3fold_page_unlock(stspin_unlock (&zhdr->page_lock); } - -static inline struct z3fold_header *__get_z3fold_header (unsigned long handle, -bool lock) +/* return locked z3fold page if it's not headless */ +static inline struct z3fold_header *get_z3fold_header (unsigned long handle) {struct z3fold_buddy_slots *slots;struct z3fold_header *zhdr;
@@ -269,13 +268,12 @@ static inline struct z3fold_header *__geread_lock (&slots->lock); addr = *(unsigned long *)handle; zhdr = (struct z3fold_header *)(addr &PAGE_MASK ); -if (lock) - locked =z3fold_page_trylock (zhdr); + locked =z3fold_page_trylock (zhdr);read_unlock (&slots->lock);if (locked)break ;cpu_relax (); - }while (lock); + }while (true); }else { zhdr = (struct z3fold_header *)(handle &PAGE_MASK ); }
@@ -283,18 +281,6 @@ static inline struct z3fold_header *__gereturn zhdr; } -/* Returns the z3fold page where a given handle is stored */ -static inline struct z3fold_header *handle_to_z3fold_header (unsigned long h) -{ -return __get_z3fold_header (h, false); -} - -/* return locked z3fold page if it's not headless */ -static inline struct z3fold_header *get_z3fold_header (unsigned long h) -{ -return __get_z3fold_header (h, true); -} -static inline void put_z3fold_header (struct z3fold_header *zhdr) {struct page *page =virt_to_page (zhdr);
_
Andrew Morton
maintainerPATCH 64/192 mm/z3fold: fix potential memory leak in z3fold_destroy_pool()
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/z3fold: fix potential memory leak in z3fold_destroy_pool()
There is a memory leak in z3fold_destroy_pool() as it forgets to free_percpu pool->unbuddied. Call free_percpu for pool->unbuddied to fix this issue.
---
mm/z3fold.c | 1 + 1 file changed, 1 insertion(+)
mm/z3fold.c renamed from mm/z3fold.c~mm-z3fold-fix-potential-memory-leak-in-z3fold_destroy_pool +1 -0
@@ -1046,6 +1046,7 @@ static void z3fold_destroy_pool(struct zdestroy_workqueue (pool->compact_wq);destroy_workqueue (pool->release_wq);z3fold_unregister_migration (pool); +free_percpu (pool->unbuddied);kfree (pool); }
_
Andrew Morton
maintainerPATCH 65/192 mm/z3fold: use release_z3fold_page_locked() to release locked z3fold page
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/z3fold: use release_z3fold_page_locked() to release locked z3fold page
We should use release_z3fold_page_locked() to release z3fold page when it's locked, although it looks harmless to use release_z3fold_page() now.
---
mm/z3fold.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/z3fold.c renamed from mm/z3fold.c~mm-z3fold-use-release_z3fold_page_locked-to-release-locked-z3fold-page +1 -1
@@ -1370,7 +1370,7 @@ static int z3fold_reclaim_page(struct z3if (zhdr->foreign_handles ||test_and_set_bit (PAGE_CLAIMED , &page->private)) {if (kref_put (&zhdr->refcount, - release_z3fold_page)) + release_z3fold_page_locked))atomic64_dec (&pool->pages_nr);else z3fold_page_unlock (zhdr);
_
Andrew Morton
maintainerPATCH 66/192 mm/zbud: reuse unbuddied[0] as buddied in zbud_pool
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/zbud: reuse unbuddied[0] as buddied in zbud_pool
Patch series "Cleanups for zbud", v2.
This series contains just cleanups to save some possible memory in zbud_pool and avoid exporting any unneeded zbud API. More details can be found in the respective changelogs
This patch (of 2):
Since commit 9d8c5b5284e4 ("mm: zbud: fix condition check on allocation size"), zbud_pool.unbuddied[0] is always unused. We can reuse it as buddied field to save some possible memory.
---
mm/zbud.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
mm/zbud.c renamed from mm/zbud.c~mm-zbud-reuse-unbuddied-as-buddied-in-zbud_pool +8 -2
@@ -93,8 +93,14 @@*/ struct zbud_pool {spinlock_t lock; -struct list_head unbuddied[NCHUNKS ]; -struct list_head buddied; +union { +/* +* Reuse unbuddied[0] as buddied on the ground that +* unbuddied[0] is unused. +*/ +struct list_head buddied; +struct list_head unbuddied[NCHUNKS ]; + };struct list_head lru;u64 pages_nr;const struct zbud_ops *ops;
_
Andrew Morton
maintainerPATCH 67/192 mm/zbud: don't export any zbud API
In reply to Andrew MortonFrom: Miaohe Lin <[email protected]>
Subject: mm/zbud: don't export any zbud API
The zbud doesn't need to export any API and it is meant to be used via zpool API since the commit 12d79d64bfd3 ("mm/zpool: update zswap to use zpool"). So we can remove the unneeded zbud.h and move down zpool API to avoid any forward declaration.
---
MAINTAINERS | 1 include/linux/zbud.h | 23 ---- mm/Kconfig | 1 mm/zbud.c | 223 ++++++++++++++++++++--------------------- 4 files changed, 110 insertions(+), 138 deletions(-)
include/linux/zbud.h deleted +0 -23
@@ -1,23 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ZBUD_H_ -#define _ZBUD_H_ - -#include <linux/types.h> - -struct zbud_pool ; - -struct zbud_ops { -int (*evict)(struct zbud_pool *pool,unsigned long handle); -}; - -struct zbud_pool *zbud_create_pool (gfp_t gfp,const struct zbud_ops *ops); -void zbud_destroy_pool (struct zbud_pool *pool); -int zbud_alloc (struct zbud_pool *pool,size_t size,gfp_t gfp, -unsigned long *handle); -void zbud_free (struct zbud_pool *pool,unsigned long handle); -int zbud_reclaim_page (struct zbud_pool *pool,unsigned int retries); -void *zbud_map (struct zbud_pool *pool,unsigned long handle); -void zbud_unmap (struct zbud_pool *pool,unsigned long handle); -u64 zbud_get_pool_size (struct zbud_pool *pool); - -#endif /* _ZBUD_H_ */
MAINTAINERS renamed from MAINTAINERS~mm-zbud-dont-export-any-zbud-api +0 -1
@@ -20172,7 +20172,6 @@ M: Seth Jennings <[email protected]> M: Dan Streetman <[email protected]> L: [email protected] S: Maintained -F: include/linux/zbud.h F: mm/zbud.c ZD1211RW WIRELESS DRIVER
mm/Kconfig renamed from mm/Kconfig~mm-zbud-dont-export-any-zbud-api +1 -0
@@ -674,6 +674,7 @@ config ZPOOL config ZBUD tristate "Low (Up to 2x) density storage for compressed pages" + depends on ZPOOL help A special purpose allocator for storing compressed pages. It is designed to store up to two compressed pages per physical
mm/zbud.c renamed from mm/zbud.c~mm-zbud-dont-export-any-zbud-api +109 -114
315 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 68/192 mm/compaction: use DEVICE_ATTR_WO macro
In reply to Andrew MortonFrom: YueHaibing <[email protected]>
Subject: mm/compaction: use DEVICE_ATTR_WO macro
Use DEVICE_ATTR_WO helper instead of plain DEVICE_ATTR, which makes the code a bit shorter and easier to read.
---
mm/compaction.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c renamed from mm/compaction.c~mm-compaction-use-device_attr_wo-macro +4 -4
@@ -2722,9 +2722,9 @@ int sysctl_compaction_handler(struct ctl }#if defined(CONFIG_SYSFS ) && defined(CONFIG_NUMA ) -static ssize_t sysfs_compact_node (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) +static ssize_t compact_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) {int nid = dev->id;
@@ -2737,7 +2737,7 @@ static ssize_t sysfs_compact_node(structreturn count; } -static DEVICE_ATTR(compact, 0200, NULL, sysfs_compact_node); +static DEVICE_ATTR_WO (compact);int compaction_register_node (struct node *node) {
_
Andrew Morton
maintainerPATCH 69/192 mm: compaction: remove duplicate !list_empty(&sublist) check
In reply to Andrew MortonFrom: Liu Xiang <[email protected]>
Subject: mm: compaction: remove duplicate !list_empty(&sublist) check
The list_splice_tail(&sublist, freelist) also do !list_empty(&sublist) check, so remove the duplicate call.
---
mm/compaction.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
mm/compaction.c renamed from mm/compaction.c~mm-compaction-remove-duplicate-list_emptysublist-check +2 -4
@@ -1297,8 +1297,7 @@ move_freelist_head(struct list_head *freif (!list_is_last (freelist, &freepage->lru)) {list_cut_before (&sublist, freelist, &freepage->lru); -if (!list_empty (&sublist)) -list_splice_tail (&sublist, freelist); +list_splice_tail (&sublist, freelist); } }
@@ -1315,8 +1314,7 @@ move_freelist_tail(struct list_head *freif (!list_is_first (freelist, &freepage->lru)) {list_cut_position (&sublist, freelist, &freepage->lru); -if (!list_empty (&sublist)) -list_splice_tail (&sublist, freelist); +list_splice_tail (&sublist, freelist); } }
_
Andrew Morton
maintainerPATCH 70/192 mm/compaction: fix 'limit' in fast_isolate_freepages
In reply to Andrew MortonFrom: Wonhyuk Yang <[email protected]>
Subject: mm/compaction: fix 'limit' in fast_isolate_freepages
Because of 'min(1, ...)', fast_isolate_freepages set 'limit' to 0 or 1. This takes away the opportunities of find candinate pages. So, by making enough scans available, increases the probability of finding the appropriate freepage.
Tested it on the thpscale and the results are as follows.
5.12.0 5.12.0
valnilla patched
Amean fault-both-1 598.15 ( 0.00%) 592.56 ( 0.93%)
Amean fault-both-3 1494.47 ( 0.00%) 1514.35 ( -1.33%)
Amean fault-both-5 2519.48 ( 0.00%) 2471.76 ( 1.89%)
Amean fault-both-7 3173.85 ( 0.00%) 3079.19 ( 2.98%)
Amean fault-both-12 8063.83 ( 0.00%) 7858.29 ( 2.55%)
Amean fault-both-18 8781.20 ( 0.00%) 7827.70 * 10.86%*
Amean fault-both-24 12576.44 ( 0.00%) 12250.20 ( 2.59%)
Amean fault-both-30 18503.27 ( 0.00%) 17528.11 * 5.27%*
Amean fault-both-32 16133.69 ( 0.00%) 13874.24 * 14.00%* 5.12.0 5.12.0
vanilla patchedOps Compaction migrate scanned 6547133.00 5963901.00
Ops Compaction free scanned 32452453.00 26609101.00
5.12 5.12
vanilla patchedDuration User 27.99 28.84
Duration System 244.08 236.76
Duration Elapsed 78.27 78.38
---
mm/compaction.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/compaction.c renamed from mm/compaction.c~mm-compaction-fix-limit-in-fast_isolate_freepages +3 -3
@@ -1378,7 +1378,7 @@ static int next_search_order(struct compstatic unsigned long fast_isolate_freepages (struct compact_control *cc) { -unsigned int limit =min (1U ,freelist_scan_limit (cc) >>1 ); +unsigned int limit =max (1U ,freelist_scan_limit (cc) >>1 );unsigned int nr_scanned =0 ;unsigned long low_pfn, min_pfn, highest =0 ;unsigned long nr_isolated =0 ;
@@ -1490,11 +1490,11 @@ fast_isolate_freepages(struct compact_cospin_unlock_irqrestore (&cc->zone->lock, flags);/* -* Smaller scan on next order so the total scan ig related +* Smaller scan on next order so the total scan is related * to freelist_scan_limit. */ if (order_scanned >= limit) - limit =min (1U , limit >>1 ); + limit =max (1U , limit >>1 ); }if (!page) {
_
Andrew Morton
maintainerPATCH 71/192 mm/mempolicy: cleanup nodemask intersection check for oom
In reply to Andrew MortonFrom: Feng Tang <[email protected]>
Subject: mm/mempolicy: cleanup nodemask intersection check for oom
Patch series "mm/mempolicy: some fix and semantics cleanup", v4.
Current memory policy code has some confusing and ambiguous part about MPOL_LOCAL policy, as it is handled as a faked MPOL_PREFERRED one, and there are many places having to distinguish them. Also the nodemask intersection check needs cleanup to be more explicit for OOM use, and handle MPOL_INTERLEAVE correctly. This patchset cleans up these and unifies the parameter sanity check for mbind() and set_mempolicy().
This patch (of 3):
mempolicy_nodemask_intersects seem to be a general purpose mempolicy function. In fact it is partially tailored for the OOM purpose instead. The oom proper is the only existing user so rename the function to make that purpose explicit.
While at it drop the MPOL_INTERLEAVE as those allocations never has a nodemask defined (see alloc_page_interleave) so this is a dead code and a confusing one because MPOL_INTERLEAVE is a hint rather than a hard requirement so it shouldn't be considered during the OOM.
The final code can be reduced to a check for MPOL_BIND which is the only memory policy that is a hard requirement and thus relevant to a constrained OOM logic.
---
include/linux/mempolicy.h | 2 +- mm/mempolicy.c | 34 +++++++++------------------------- mm/oom_kill.c | 2 +- 3 files changed, 11 insertions(+), 27 deletions(-)
include/linux/mempolicy.h renamed from include/linux/mempolicy.h~mm-mempolicy-cleanup-nodemask-intersection-check-for-oom +1 -1
@@ -150,7 +150,7 @@ extern int huge_node(struct vm_area_stru unsigned long addr, gfp_t gfp_flags, struct mempolicy **mpol, nodemask_t **nodemask);extern bool init_nodemask_of_mempolicy (nodemask_t *mask); -extern bool mempolicy_nodemask_intersects (struct task_struct *tsk, +extern bool mempolicy_in_oom_domain (struct task_struct *tsk,const nodemask_t *mask);extern nodemask_t *policy_nodemask (gfp_t gfp,struct mempolicy *policy);
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-cleanup-nodemask-intersection-check-for-oom +9 -25
@@ -2094,16 +2094,16 @@ bool init_nodemask_of_mempolicy(nodemask#endif /* -* mempolicy_nodemask_intersects +* mempolicy_in_oom_domain * -* If tsk's mempolicy is "default" [NULL], return 'true' to indicate default -* policy. Otherwise, check for intersection between mask and the policy -* nodemask for 'bind' or 'interleave' policy. For 'preferred' or 'local' -* policy, always return true since it may allocate elsewhere on fallback. +* If tsk's mempolicy is "bind", check for intersection between mask and +* the policy nodemask. Otherwise, return true for all other policies +* including "interleave", as a tsk with "interleave" policy may have +* memory allocated from all nodes in system. * * Takes task_lock(tsk) to prevent freeing of its mempolicy. */ -bool mempolicy_nodemask_intersects (struct task_struct *tsk, +bool mempolicy_in_oom_domain (struct task_struct *tsk,const nodemask_t *mask) {struct mempolicy *mempolicy;
@@ -2111,29 +2111,13 @@ bool mempolicy_nodemask_intersects(strucif (!mask)return ret; +task_lock (tsk); mempolicy = tsk->mempolicy; -if (!mempolicy) - goto out; - -switch (mempolicy->mode) { -case MPOL_PREFERRED : -/* -* MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to -* allocate from, they may fallback to other nodes when oom. -* Thus, it's possible for tsk to have allocated memory from -* nodes in mask. -*/ -break ; -case MPOL_BIND : -case MPOL_INTERLEAVE : +if (mempolicy && mempolicy->mode ==MPOL_BIND ) ret =nodes_intersects (mempolicy->v.nodes, *mask); -break ; -default : -BUG (); - } -out:task_unlock (tsk); +return ret; }
mm/oom_kill.c renamed from mm/oom_kill.c~mm-mempolicy-cleanup-nodemask-intersection-check-for-oom +1 -1
@@ -104,7 +104,7 @@ static bool oom_cpuset_eligible(struct t* mempolicy intersects current, otherwise it may be * needlessly killed. */ - ret =mempolicy_nodemask_intersects (tsk, mask); + ret =mempolicy_in_oom_domain (tsk, mask); }else {/* * This is not a mempolicy constrained oom, so only
_
Andrew Morton
maintainerPATCH 72/192 mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED policy
In reply to Andrew MortonFrom: Feng Tang <[email protected]>
Subject: mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED policy
MPOL_LOCAL policy has been setup as a real policy, but it is still handled like a faked POL_PREFERRED policy with one internal MPOL_F_LOCAL flag bit set, and there are many places having to judge the real 'prefer' or the 'local' policy, which are quite confusing.
In current code, there are 4 cases that MPOL_LOCAL are used:
1. user specifies 'local' policy
2. user specifies 'prefer' policy, but with empty nodemask
3. system 'default' policy is used
4. 'prefer' policy + valid 'preferred' node with MPOL_F_STATIC_NODES flag set, and when it is 'rebind' to a nodemask which doesn't contains the 'preferred' node, it will perform as 'local' policy
So make 'local' a real policy instead of a fake 'prefer' one, and kill MPOL_F_LOCAL bit, which can greatly reduce the confusion for code reading.
For case 4, the logic of mpol_rebind_preferred() is confusing, as Michal Hocko pointed out:
: I do believe that rebinding preferred policy is just bogus and it should : be dropped altogether on the ground that a preference is a mere hint from : userspace where to start the allocation. Unless I am missing something : cpusets will be always authoritative for the final placement. The : preferred node just acts as a starting point and it should be really : preserved when cpusets changes. Otherwise we have a very subtle behavior : corner cases.
So dump all the tricky transformation between 'prefer' and 'local', and just record the new nodemask of rebinding.
---
include/uapi/linux/mempolicy.h | 1 mm/mempolicy.c | 136 ++++++++++++------------------- 2 files changed, 56 insertions(+), 81 deletions(-)
include/uapi/linux/mempolicy.h renamed from include/uapi/linux/mempolicy.h~mm-mempolicy-dont-handle-mpol_local-like-a-fake-mpol_preferred-policy +0 -1
@@ -60,7 +60,6 @@ enum {* are never OR'ed into the mode in mempolicy API arguments. */ #define MPOL_F_SHARED (1 << 0)/* identify shared policies */ -#define MPOL_F_LOCAL (1 << 1)/* preferred local allocation */ #define MPOL_F_MOF (1 << 3)/* this policy wants migrate on fault */ #define MPOL_F_MORON (1 << 4)/* Migrate On protnone Reference On Node */
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-dont-handle-mpol_local-like-a-fake-mpol_preferred-policy +56 -80
285 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 73/192 mm/mempolicy: unify the parameter sanity check for mbind and set_mempolicy
In reply to Andrew MortonFrom: Feng Tang <[email protected]>
Subject: mm/mempolicy: unify the parameter sanity check for mbind and set_mempolicy
Currently the kernel_mbind() and kernel_set_mempolicy() do almost the same operation for parameter sanity check.
Add a helper function to unify the code to reduce the redundancy, and make it easier for changing the sanity check code in future.
[thanks to David Rientjes for suggesting using helper function instead of macro].
---
mm/mempolicy.c | 48 +++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-)
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-unify-the-parameter-sanity-check-for-mbind-and-set_mempolicy +30 -18
@@ -1441,26 +1441,38 @@ static int copy_nodes_to_user(unsigned lreturn copy_to_user (mask,nodes_addr (*nodes), copy) ? -EFAULT :0 ; } +/* Basic parameter sanity check used by both mbind() and set_mempolicy() */ +static inline int sanitize_mpol_flags (int *mode,unsigned short *flags) +{ + *flags = *mode &MPOL_MODE_FLAGS ; + *mode &= ~MPOL_MODE_FLAGS ; +if ((unsigned int )(*mode) >=MPOL_MAX ) +return -EINVAL ; +if ((*flags &MPOL_F_STATIC_NODES ) && (*flags &MPOL_F_RELATIVE_NODES )) +return -EINVAL ; + +return 0 ; +} +static long kernel_mbind (unsigned long start,unsigned long len,unsigned long mode,const unsigned long __user *nmask,unsigned long maxnode,unsigned int flags) { +unsigned short mode_flags;nodemask_t nodes; +int lmode = mode;int err; -unsigned short mode_flags; start =untagged_addr (start); - mode_flags = mode &MPOL_MODE_FLAGS ; - mode &= ~MPOL_MODE_FLAGS ; -if (mode >=MPOL_MAX ) -return -EINVAL ; -if ((mode_flags &MPOL_F_STATIC_NODES ) && - (mode_flags &MPOL_F_RELATIVE_NODES )) -return -EINVAL ; + err =sanitize_mpol_flags (&lmode, &mode_flags); +if (err) +return err; + err =get_nodes (&nodes, nmask, maxnode);if (err)return err; -return do_mbind (start, len, mode, mode_flags, &nodes, flags); + +return do_mbind (start, len, lmode, mode_flags, &nodes, flags); } SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
@@ -1474,20 +1486,20 @@ SYSCALL_DEFINE6(mbind, unsigned long, ststatic long kernel_set_mempolicy (int mode,const unsigned long __user *nmask,unsigned long maxnode) { -int err; +unsigned short mode_flags;nodemask_t nodes; -unsigned short flags; +int lmode = mode; +int err; + + err =sanitize_mpol_flags (&lmode, &mode_flags); +if (err) +return err; - flags = mode &MPOL_MODE_FLAGS ; - mode &= ~MPOL_MODE_FLAGS ; -if ((unsigned int )mode >=MPOL_MAX ) -return -EINVAL ; -if ((flags &MPOL_F_STATIC_NODES ) && (flags &MPOL_F_RELATIVE_NODES )) -return -EINVAL ; err =get_nodes (&nodes, nmask, maxnode);if (err)return err; -return do_set_mempolicy (mode, flags, &nodes); + +return do_set_mempolicy (lmode, mode_flags, &nodes); } SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask,
_
Andrew Morton
maintainerPATCH 74/192 mm: mempolicy: don't have to split pmd for huge zero page
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: mempolicy: don't have to split pmd for huge zero page
When trying to migrate pages to obey mempolicy, the huge zero page is split by inserting base zero pfn to all PTEs, then the page table walk fallback to PTE level and just skips zero page. Skipping zero page for mempolicy has been the behavior of kernel since v2.6.16 due to commit f4598c8b3678 ("[PATCH] migration: make sure there is no attempt to migrate reserved pages."). So it seems pointless to split huge zero page, it could be just skipped like base zero page.
Set ACTION_CONTINUE to prevent the walk_page_range() split the pmd for this case.
---
mm/mempolicy.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-dont-have-to-split-pmd-for-huge-zero-page +5 -4
@@ -437,7 +437,8 @@ static inline bool queue_pages_required(/* * queue_pages_pmd() has four possible return values: -* 0 - pages are placed on the right node or queued successfully. +* 0 - pages are placed on the right node or queued successfully, or +* special page is met, i.e. huge zero page. * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were * specified. * 2 - THP was split.
@@ -461,8 +462,7 @@ static int queue_pages_pmd(pmd_t *pmd, s page =pmd_page (*pmd);if (is_huge_zero_page (page)) {spin_unlock (ptl); -__split_huge_pmd (walk->vma, pmd, addr, false,NULL ); - ret =2 ; + walk->action =ACTION_CONTINUE ; goto out; }if (!queue_pages_required (page, qp))
@@ -489,7 +489,8 @@ out:* and move them to the pagelist if they do. * * queue_pages_pte_range() has three possible return values: -* 0 - pages are placed on the right node or queued successfully. +* 0 - pages are placed on the right node or queued successfully, or +* special page is met, i.e. zero page. * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were * specified. * -EIO - only MPOL_MF_STRICT was specified and an existing page was already
_
Andrew Morton
maintainerPATCH 75/192 mm/mempolicy: use unified 'nodes' for bind/interleave/prefer policies
In reply to Andrew MortonFrom: Ben Widawsky <[email protected]>
Subject: mm/mempolicy: use unified 'nodes' for bind/interleave/prefer policies
Current structure 'mempolicy' uses a union to store the node info for bind/interleave/perfer policies.
union {
short preferred_node; /* preferred */
nodemask_t nodes; /* interleave/bind */
/* undefined for default */
} v;Since preferred node can also be represented by a nodemask_t with only ont bit set, unify these policies with using one nodemask_t 'nodes', which can remove a union, simplify the code and make it easier to support future's new policy's node info.
---
include/linux/mempolicy.h | 7 -- mm/mempolicy.c | 96 ++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 57 deletions(-)
include/linux/mempolicy.h renamed from include/linux/mempolicy.h~mm-mempolicy-use-unified-nodes-for-bind-interleave-prefer-policies +2 -5
@@ -46,11 +46,8 @@ struct mempolicy {atomic_t refcnt;unsigned short mode;/* See MPOL_* above */ unsigned short flags;/* See set_mempolicy() MPOL_F_* above */ -union { -short preferred_node;/* preferred */ -nodemask_t nodes;/* interleave/bind */ -/* undefined for default */ - } v; +nodemask_t nodes;/* interleave/bind/perfer */ +union {nodemask_t cpuset_mems_allowed;/* relative to these nodes */ nodemask_t user_nodemask;/* nodemask passed by user */
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-use-unified-nodes-for-bind-interleave-prefer-policies +44 -52
290 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 76/192 include/linux/mmzone.h: add documentation for pfn_valid()
In reply to Andrew MortonFrom: Mike Rapoport <[email protected]>
Subject: include/linux/mmzone.h: add documentation for pfn_valid()
Patch series "arm64: drop pfn_valid_within() and simplify pfn_valid()", v4.
These patches aim to remove CONFIG_HOLES_IN_ZONE and essentially hardwire pfn_valid_within() to 1.
The idea is to mark NOMAP pages as reserved in the memory map and restore the intended semantics of pfn_valid() to designate availability of struct page for a pfn.
With this the core mm will be able to cope with the fact that it cannot use NOMAP pages and the holes created by NOMAP ranges within MAX_ORDER blocks will be treated correctly even without the need for pfn_valid_within.
This patch (of 4):
Add comment describing the semantics of pfn_valid() that clarifies that pfn_valid() only checks for availability of a memory map entry (i.e. struct page) for a PFN rather than availability of usable memory backing that PFN.
The most "generic" version of pfn_valid() used by the configurations with SPARSEMEM enabled resides in include/linux/mmzone.h so this is the most suitable place for documentation about semantics of pfn_valid().
---
include/linux/mmzone.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/mmzone.h renamed from include/linux/mmzone.h~include-linux-mmzoneh-add-documentation-for-pfn_valid +11 -0
@@ -1445,6 +1445,17 @@ static inline int pfn_section_valid(stru#endif #ifndef CONFIG_HAVE_ARCH_PFN_VALID +/** +* pfn_valid - check if there is a valid memory map entry for a PFN +* @pfn: the page frame number to check +* +* Check if there is a valid memory map entry aka struct page for the @pfn. +* Note, that availability of the memory map entry does not imply that +* there is actual usable memory at that @pfn. The struct page may +* represent a hole or an unusable page frame. +* +* Return: 1 for PFNs that have memory map entries and 0 otherwise +*/ static inline int pfn_valid (unsigned long pfn) {struct mem_section *ms;
_
Andrew Morton
maintainerPATCH 77/192 memblock: update initialization of reserved pages
In reply to Andrew MortonFrom: Mike Rapoport <[email protected]>
Subject: memblock: update initialization of reserved pages
The struct pages representing a reserved memory region are initialized using reserve_bootmem_range() function. This function is called for each reserved region just before the memory is freed from memblock to the buddy page allocator.
The struct pages for MEMBLOCK_NOMAP regions are kept with the default values set by the memory map initialization which makes it necessary to have a special treatment for such pages in pfn_valid() and pfn_valid_within().
Split out initialization of the reserved pages to a function with a meaningful name and treat the MEMBLOCK_NOMAP regions the same way as the reserved regions and mark struct pages for the NOMAP regions as PageReserved.
---
include/linux/memblock.h | 4 +++- mm/memblock.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-)
include/linux/memblock.h renamed from include/linux/memblock.h~memblock-update-initialization-of-reserved-pages +3 -1
@@ -30,7 +30,9 @@ extern unsigned long long max_possible_p* @MEMBLOCK_NONE: no special request * @MEMBLOCK_HOTPLUG: hotpluggable region * @MEMBLOCK_MIRROR: mirrored region -* @MEMBLOCK_NOMAP: don't add to kernel direct mapping +* @MEMBLOCK_NOMAP: don't add to kernel direct mapping and treat as +* reserved in the memory map; refer to memblock_mark_nomap() description +* for further details */ enum memblock_flags {MEMBLOCK_NONE =0x0 ,/* No special request */
mm/memblock.c renamed from mm/memblock.c~memblock-update-initialization-of-reserved-pages +26 -2
@@ -906,6 +906,11 @@ int __init_memblock memblock_mark_mirror* @base: the base phys addr of the region * @size: the size of the region * +* The memory regions marked with %MEMBLOCK_NOMAP will not be added to the +* direct mapping of the physical memory. These regions will still be +* covered by the memory map. The struct page representing NOMAP memory +* frames in the memory map will be PageReserved() +* * Return: 0 on success, -errno on failure. */ int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
@@ -2002,6 +2007,26 @@ static unsigned long __init __free_memorreturn end_pfn - start_pfn; } +static void __init memmap_init_reserved_pages (void ) +{ +struct memblock_region *region; +phys_addr_t start, end; +u64 i; + +/* initialize struct pages for the reserved regions */ +for_each_reserved_mem_range (i, &start, &end) +reserve_bootmem_region (start, end); + +/* and also treat struct pages for the NOMAP regions as PageReserved */ +for_each_mem_region (region) { +if (memblock_is_nomap (region)) { + start = region->base; + end = start + region->size; +reserve_bootmem_region (start, end); + } + } +} +static unsigned long __init free_low_memory_core_early (void ) {unsigned long count =0 ;
@@ -2010,8 +2035,7 @@ static unsigned long __init free_low_memmemblock_clear_hotplug (0 ,-1 ); -for_each_reserved_mem_range (i, &start, &end) -reserve_bootmem_region (start, end); +memmap_init_reserved_pages ();/* * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
_
Andrew Morton
maintainerPATCH 78/192 arm64: decouple check whether pfn is in linear map from pfn_valid()
In reply to Andrew MortonFrom: Mike Rapoport <[email protected]>
Subject: arm64: decouple check whether pfn is in linear map from pfn_valid()
The intended semantics of pfn_valid() is to verify whether there is a struct page for the pfn in question and nothing else.
Yet, on arm64 it is used to distinguish memory areas that are mapped in the linear map vs those that require ioremap() to access them.
Introduce a dedicated pfn_is_map_memory() wrapper for memblock_is_map_memory() to perform such check and use it where appropriate.
Using a wrapper allows to avoid cyclic include dependencies.
While here also update style of pfn_valid() so that both pfn_valid() and pfn_is_map_memory() declarations will be consistent.
---
arch/arm64/include/asm/memory.h | 2 +- arch/arm64/include/asm/page.h | 3 ++- arch/arm64/kvm/mmu.c | 2 +- arch/arm64/mm/init.c | 12 ++++++++++++ arch/arm64/mm/ioremap.c | 4 ++-- arch/arm64/mm/mmu.c | 2 +- 6 files changed, 19 insertions(+), 6 deletions(-)
arch/arm64/include/asm/memory.h renamed from arch/arm64/include/asm/memory.h~arm64-decouple-check-whether-pfn-is-in-linear-map-from-pfn_valid +1 -1
@@ -369,7 +369,7 @@ static inline void *phys_to_virt(phys_ad#define virt_addr_valid (addr) ({ \ __typeof__(addr) __addr = __tag_reset(addr); \ - __is_lm_address(__addr) && pfn_valid(virt_to_pfn(__addr)); \ + __is_lm_address(__addr) && pfn_is_map_memory(virt_to_pfn(__addr)); \ })void dump_mem_limit (void );
arch/arm64/include/asm/page.h renamed from arch/arm64/include/asm/page.h~arm64-decouple-check-whether-pfn-is-in-linear-map-from-pfn_valid +2 -1
@@ -37,7 +37,8 @@ void copy_highpage(struct page *to, strutypedef struct page *pgtable_t ; -extern int pfn_valid (unsigned long ); +int pfn_valid (unsigned long pfn); +int pfn_is_map_memory (unsigned long pfn);#include <asm/memory.h>
arch/arm64/kvm/mmu.c renamed from arch/arm64/kvm/mmu.c~arm64-decouple-check-whether-pfn-is-in-linear-map-from-pfn_valid +1 -1
@@ -85,7 +85,7 @@ void kvm_flush_remote_tlbs(struct kvm *kstatic bool kvm_is_device_pfn (unsigned long pfn) { -return !pfn_valid (pfn); +return !pfn_is_map_memory (pfn); }static void *stage2_memcache_zalloc_page (void *arg)
arch/arm64/mm/init.c renamed from arch/arm64/mm/init.c~arm64-decouple-check-whether-pfn-is-in-linear-map-from-pfn_valid +12 -0
@@ -256,6 +256,18 @@ int pfn_valid(unsigned long pfn) }EXPORT_SYMBOL (pfn_valid); +int pfn_is_map_memory (unsigned long pfn) +{ +phys_addr_t addr =PFN_PHYS (pfn); + +/* avoid false positives for bogus PFNs, see comment in pfn_valid() */ +if (PHYS_PFN (addr) != pfn) +return 0 ; + +return memblock_is_map_memory (addr); +} +EXPORT_SYMBOL (pfn_is_map_memory); +static phys_addr_t memory_limit =PHYS_ADDR_MAX ;/*
arch/arm64/mm/ioremap.c renamed from arch/arm64/mm/ioremap.c~arm64-decouple-check-whether-pfn-is-in-linear-map-from-pfn_valid +2 -2
@@ -43,7 +43,7 @@ static void __iomem *__ioremap_caller(ph/* * Don't allow RAM to be mapped. */ -if (WARN_ON (pfn_valid (__phys_to_pfn (phys_addr)))) +if (WARN_ON (pfn_is_map_memory (__phys_to_pfn (phys_addr))))return NULL ; area =get_vm_area_caller (size,VM_IOREMAP , caller);
@@ -84,7 +84,7 @@ EXPORT_SYMBOL(iounmap);void __iomem *ioremap_cache (phys_addr_t phys_addr,size_t size) {/* For normal memory we already have a cacheable mapping. */ -if (pfn_valid (__phys_to_pfn (phys_addr))) +if (pfn_is_map_memory (__phys_to_pfn (phys_addr)))return (void __iomem *)__phys_to_virt (phys_addr); return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
arch/arm64/mm/mmu.c renamed from arch/arm64/mm/mmu.c~arm64-decouple-check-whether-pfn-is-in-linear-map-from-pfn_valid +1 -1
@@ -82,7 +82,7 @@ void set_swapper_pgd(pgd_t *pgdp, pgd_tpgprot_t phys_mem_access_prot (struct file *file,unsigned long pfn,unsigned long size,pgprot_t vma_prot) { -if (!pfn_valid (pfn)) +if (!pfn_is_map_memory (pfn))return pgprot_noncached (vma_prot);else if (file->f_flags &O_SYNC )return pgprot_writecombine (vma_prot);
_
Andrew Morton
maintainerPATCH 79/192 arm64: drop pfn_valid_within() and simplify pfn_valid()
In reply to Andrew MortonFrom: Mike Rapoport <[email protected]>
Subject: arm64: drop pfn_valid_within() and simplify pfn_valid()
The arm64's version of pfn_valid() differs from the generic because of two reasons:
* Parts of the memory map are freed during boot. This makes it necessary to verify that there is actual physical memory that corresponds to a pfn which is done by querying memblock.
* There are NOMAP memory regions. These regions are not mapped in the linear map and until the previous commit the struct pages representing these areas had default values.
As the consequence of absence of the special treatment of NOMAP regions in the memory map it was necessary to use memblock_is_map_memory() in pfn_valid() and to have pfn_valid_within() aliased to pfn_valid() so that generic mm functionality would not treat a NOMAP page as a normal page.
Since the NOMAP regions are now marked as PageReserved(), pfn walkers and the rest of core mm will treat them as unusable memory and thus pfn_valid_within() is no longer required at all and can be disabled on arm64.
pfn_valid() can be slightly simplified by replacing memblock_is_map_memory() with memblock_is_memory().
---
arch/arm64/Kconfig | 1 - arch/arm64/mm/init.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)
arch/arm64/Kconfig renamed from arch/arm64/Kconfig~arm64-drop-pfn_valid_within-and-simplify-pfn_valid +0 -1
@@ -201,7 +201,6 @@ config ARM64 select HAVE_KPROBES select HAVE_KRETPROBES select HAVE_GENERIC_VDSO - select HOLES_IN_ZONE select IOMMU_DMA if IOMMU_SUPPORT select IRQ_DOMAIN select IRQ_FORCED_THREADING
arch/arm64/mm/init.c renamed from arch/arm64/mm/init.c~arm64-drop-pfn_valid_within-and-simplify-pfn_valid +1 -1
@@ -252,7 +252,7 @@ int pfn_valid(unsigned long pfn)if (!early_section (ms))return pfn_section_valid (ms, pfn); -return memblock_is_map_memory (addr); +return memblock_is_memory (addr); }EXPORT_SYMBOL (pfn_valid);
_
Andrew Morton
maintainerPATCH 80/192 arm64/mm: drop HAVE_ARCH_PFN_VALID
In reply to Andrew MortonFrom: Anshuman Khandual <[email protected]>
Subject: arm64/mm: drop HAVE_ARCH_PFN_VALID
CONFIG_SPARSEMEM_VMEMMAP is now the only available memory model on arm64 platforms and free_unused_memmap() would just return without creating any holes in the memmap mapping. There is no need for any special handling in pfn_valid() and HAVE_ARCH_PFN_VALID can just be dropped. This also moves the pfn upper bits sanity check into generic pfn_valid().
---
arch/arm64/Kconfig | 1 arch/arm64/include/asm/page.h | 1 arch/arm64/mm/init.c | 37 -------------------------------- include/linux/mmzone.h | 9 +++++++ 4 files changed, 9 insertions(+), 39 deletions(-)
arch/arm64/include/asm/page.h renamed from arch/arm64/include/asm/page.h~arm64-mm-drop-have_arch_pfn_valid +0 -1
@@ -37,7 +37,6 @@ void copy_highpage(struct page *to, strutypedef struct page *pgtable_t ; -int pfn_valid (unsigned long pfn);int pfn_is_map_memory (unsigned long pfn);#include <asm/memory.h>
arch/arm64/Kconfig renamed from arch/arm64/Kconfig~arm64-mm-drop-have_arch_pfn_valid +0 -1
@@ -154,7 +154,6 @@ config ARM64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT - select HAVE_ARCH_PFN_VALID select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_SECCOMP_FILTER
arch/arm64/mm/init.c renamed from arch/arm64/mm/init.c~arm64-mm-drop-have_arch_pfn_valid +0 -37
@@ -219,43 +219,6 @@ static void __init zone_sizes_init(unsigfree_area_init (max_zone_pfns); } -int pfn_valid (unsigned long pfn) -{ -phys_addr_t addr =PFN_PHYS (pfn); -struct mem_section *ms; - -/* -* Ensure the upper PAGE_SHIFT bits are clear in the -* pfn. Else it might lead to false positives when -* some of the upper bits are set, but the lower bits -* match a valid pfn. -*/ -if (PHYS_PFN (addr) != pfn) -return 0 ; - -if (pfn_to_section_nr (pfn) >=NR_MEM_SECTIONS ) -return 0 ; - - ms =__pfn_to_section (pfn); -if (!valid_section (ms)) -return 0 ; - -/* -* ZONE_DEVICE memory does not have the memblock entries. -* memblock_is_map_memory() check for ZONE_DEVICE based -* addresses will always fail. Even the normal hotplugged -* memory will never have MEMBLOCK_NOMAP flag set in their -* memblock entries. Skip memblock search for all non early -* memory sections covering all of hotplug memory including -* both normal and ZONE_DEVICE based. -*/ -if (!early_section (ms)) -return pfn_section_valid (ms, pfn); - -return memblock_is_memory (addr); -} -EXPORT_SYMBOL (pfn_valid); -int pfn_is_map_memory (unsigned long pfn) {phys_addr_t addr =PFN_PHYS (pfn);
include/linux/mmzone.h renamed from include/linux/mmzone.h~arm64-mm-drop-have_arch_pfn_valid +9 -0
@@ -1460,6 +1460,15 @@ static inline int pfn_valid(unsigned lon {struct mem_section *ms; +/* +* Ensure the upper PAGE_SHIFT bits are clear in the +* pfn. Else it might lead to false positives when +* some of the upper bits are set, but the lower bits +* match a valid pfn. +*/ +if (PHYS_PFN (PFN_PHYS (pfn)) != pfn) +return 0 ; +if (pfn_to_section_nr (pfn) >=NR_MEM_SECTIONS )return 0 ; ms =__nr_to_section (pfn_to_section_nr (pfn));
_
Andrew Morton
maintainerPATCH 81/192 mm: migrate: fix missing update page_private to hugetlb_page_subpool
In reply to Andrew MortonFrom: Muchun Song <[email protected]>
Subject: mm: migrate: fix missing update page_private to hugetlb_page_subpool
Since commit d6995da31122 ("hugetlb: use page.private for hugetlb specific page flags") converts page.private for hugetlb specific page flags. We should use hugetlb_page_subpool() to get the subpool pointer instead of page_private().
This 'could' prevent the migration of hugetlb pages. page_private(hpage) is now used for hugetlb page specific flags. At migration time, the only flag which could be set is HPageVmemmapOptimized. This flag will only be set if the new vmemmap reduction feature is enabled. In addition, !page_mapping() implies an anonymous mapping. So, this will prevent migration of hugetb pages in anonymous mappings if the vmemmap reduction feature is enabled.
In addition, that if statement checked for the rare race condition of a page being migrated while in the process of being freed. Since that check is now wrong, we could leak hugetlb subpool usage counts.
The commit forgot to update it in the page migration routine. So fix it.
---
include/linux/hugetlb.h | 5 +++++ mm/migrate.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-migrate-fix-missing-update-page_private-to-hugetlb_page_subpool +5 -0
@@ -898,6 +898,11 @@ static inline void huge_ptep_modify_prot#else /* CONFIG_HUGETLB_PAGE */ struct hstate {}; +static inline struct hugepage_subpool *hugetlb_page_subpool (struct page *hpage) +{ +return NULL ; +} +static inline int isolate_or_dissolve_huge_page (struct page *page,struct list_head *list) {
mm/migrate.c renamed from mm/migrate.c~mm-migrate-fix-missing-update-page_private-to-hugetlb_page_subpool +1 -1
@@ -1293,7 +1293,7 @@ static int unmap_and_move_huge_page(new_* page_mapping() set, hugetlbfs specific move page routine will not * be called and we could leak usage counts for subpools. */ -if (page_private (hpage) && !page_mapping (hpage)) { +if (hugetlb_page_subpool (hpage) && !page_mapping (hpage)) { rc = -EBUSY ; goto out_unlock; }
_
Andrew Morton
maintainerPATCH 82/192 mm, thp: relax the VM_DENYWRITE constraint on file-backed THPs
In reply to Andrew MortonFrom: Collin Fijalkovich <[email protected]>
Subject: mm, thp: relax the VM_DENYWRITE constraint on file-backed THPs
Transparent huge pages are supported for read-only non-shmem files, but are only used for vmas with VM_DENYWRITE. This condition ensures that file THPs are protected from writes while an application is running (ETXTBSY). Any existing file THPs are then dropped from the page cache when a file is opened for write in do_dentry_open(). Since sys_mmap ignores MAP_DENYWRITE, this constrains the use of file THPs to vmas produced by execve().
Systems that make heavy use of shared libraries (e.g. Android) are unable to apply VM_DENYWRITE through the dynamic linker, preventing them from benefiting from the resultant reduced contention on the TLB.
This patch reduces the constraint on file THPs allowing use with any executable mapping from a file not opened for write (see inode_is_open_for_write()). It also introduces additional conditions to ensure that files opened for write will never be backed by file THPs.
Restricting the use of THPs to executable mappings eliminates the risk that a read-only file later opened for write would encounter significant latencies due to page cache truncation.
The ld linker flag '-z max-page-size=(hugepage size)' can be used to produce executables with the necessary layout. The dynamic linker must map these file's segments at a hugepage size aligned vma for the mapping to be backed with THPs.
Comparison of the performance characteristics of 4KB and 2MB-backed libraries follows; the Android dex2oat tool was used to AOT compile an example application on a single ARM core.
4KB Pages:
==========
count event_name # count / runtime 598,995,035,942 cpu-cycles # 1.800861 GHz 81,195,620,851 raw-stall-frontend # 244.112 M/sec 347,754,466,597 iTLB-loads # 1.046 G/sec 2,970,248,900 iTLB-load-misses # 0.854122% miss rate
Total test time: 332.854998 seconds.
2MB Pages:
==========
count event_name # count / runtime
592,872,663,047 cpu-cycles # 1.800358 GHz
76,485,624,143 raw-stall-frontend # 232.261 M/sec
350,478,413,710 iTLB-loads # 1.064 G/sec
803,233,322 iTLB-load-misses # 0.229182% miss rateTotal test time: 329.826087 seconds
A check of /proc/$(pidof dex2oat64)/smaps shows THPs in use:
/apex/com.android.art/lib64/libart.so
FilePmdMapped: 4096 kB
/apex/com.android.art/lib64/libart-compiler.so
FilePmdMapped: 2048 kB
---
fs/open.c | 13 +++++++++++-- mm/khugepaged.c | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-)
fs/open.c renamed from fs/open.c~mm-thp-relax-the-vm_denywrite-constraint-on-file-backed-thps +11 -2
@@ -852,8 +852,17 @@ static int do_dentry_open(struct file *f* XXX: Huge page cache doesn't support writing yet. Drop all page * cache for this file before processing writes. */ -if ((f->f_mode &FMODE_WRITE ) &&filemap_nr_thps (inode->i_mapping)) -truncate_pagecache (inode,0 ); +if (f->f_mode &FMODE_WRITE ) { +/* +* Paired with smp_mb() in collapse_file() to ensure nr_thps +* is up to date and the update to i_writecount by +* get_write_access() is visible. Ensures subsequent insertion +* of THPs into the page cache will fail. +*/ +smp_mb (); +if (filemap_nr_thps (inode->i_mapping)) +truncate_pagecache (inode,0 ); + }return 0 ;
mm/khugepaged.c renamed from mm/khugepaged.c~mm-thp-relax-the-vm_denywrite-constraint-on-file-backed-thps +15 -1
@@ -457,7 +457,8 @@ static bool hugepage_vma_check(struct vm/* Read-only file mappings need to be aligned for THP to work. */ if (IS_ENABLED (CONFIG_READ_ONLY_THP_FOR_FS ) && vma->vm_file && - (vm_flags &VM_DENYWRITE )) { + !inode_is_open_for_write (vma->vm_file->f_inode) && + (vm_flags &VM_EXEC )) {return IS_ALIGNED ((vma->vm_start >>PAGE_SHIFT ) - vma->vm_pgoff,HPAGE_PMD_NR ); }
@@ -1862,6 +1863,19 @@ out_unlock:else {__mod_lruvec_page_state (new_page,NR_FILE_THPS , nr);filemap_nr_thps_inc (mapping); +/* +* Paired with smp_mb() in do_dentry_open() to ensure +* i_writecount is up to date and the update to nr_thps is +* visible. Ensures the page cache will be truncated if the +* file is opened writable. +*/ +smp_mb (); +if (inode_is_open_for_write (mapping->host)) { + result =SCAN_FAIL ; +__mod_lruvec_page_state (new_page,NR_FILE_THPS , -nr); +filemap_nr_thps_dec (mapping); + goto xa_locked; + } }if (nr_none) {
_
Andrew Morton
maintainerPATCH 83/192 mm: memory: add orig_pmd to struct vm_fault
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: memory: add orig_pmd to struct vm_fault
Pach series "mm: thp: use generic THP migration for NUMA hinting fault", v3.
When the THP NUMA fault support was added THP migration was not supported yet. So the ad hoc THP migration was implemented in NUMA fault handling. Since v4.14 THP migration has been supported so it doesn't make too much sense to still keep another THP migration implementation rather than using the generic migration code. It is definitely a maintenance burden to keep two THP migration implementation for different code paths and it is more error prone. Using the generic THP migration implementation allows us remove the duplicate code and some hacks needed by the old ad hoc implementation.
A quick grep shows x86_64, PowerPC (book3s), ARM64 ans S390 support both THP and NUMA balancing. The most of them support THP migration except for S390. Zi Yan tried to add THP migration support for S390 before but it was not accepted due to the design of S390 PMD. For the discussion, please see: https://lkml.org/lkml/2018/4/27/953.
Per the discussion with Gerald Schaefer in v1 it is acceptible to skip huge PMD for S390 for now.
I saw there were some hacks about gup from git history, but I didn't figure out if they have been removed or not since I just found FOLL_NUMA code in the current gup implementation and they seems useful.
Patch #1 ~ #2 are preparation patches.
Patch #3 is the real meat.
Patch #4 ~ #6 keep consistent counters and behaviors with before. Patch #7 skips change huge PMD to prot_none if thp migration is not supported.
Test
----
Did some tests to measure the latency of do_huge_pmd_numa_page. The test VM has 80 vcpus and 64G memory. The test would create 2 processes to consume 128G memory together which would incur memory pressure to cause THP splits. And it also creates 80 processes to hog cpu, and the memory consumer processes are bound to different nodes periodically in order to increase NUMA faults.
The below test script is used:
echo 3 > /proc/sys/vm/drop_caches
# Run stress-ng for 24 hours
./stress-ng/stress-ng --vm 2 --vm-bytes 64G --timeout 24h & PID=$!
./stress-ng/stress-ng --cpu $NR_CPUS --timeout 24h &
# Wait for vm stressors forked
sleep 5
PID_1=pgrep -P $PID | awk 'NR == 1'
PID_2=pgrep -P $PID | awk 'NR == 2'
JOB1=pgrep -P $PID_1
JOB2=pgrep -P $PID_2
# Bind load jobs to different nodes periodically to force generate
# cross node memory access
while [ -d "/proc/$PID" ]
do
taskset -apc 8 $JOB1
taskset -apc 8 $JOB2
sleep 300
taskset -apc 58 $JOB1
taskset -apc 58 $JOB2
sleep 300
doneWith the above test the histogram of latency of do_huge_pmd_numa_page is as shown below. Since the number of do_huge_pmd_numa_page varies drastically for each run (should be due to scheduler), so I converted the raw number to percentage.
patched base
@us[stress-ng]:
[0] 3.57% 0.16% [1] 55.68% 18.36% [2, 4) 10.46% 40.44% [4, 8) 7.26% 17.82% [8, 16) 21.12% 13.41% [16, 32) 1.06% 4.27% [32, 64) 0.56% 4.07% [64, 128) 0.16% 0.35% [128, 256) < 0.1% < 0.1% [256, 512) < 0.1% < 0.1% [512, 1K) < 0.1% < 0.1% [1K, 2K) < 0.1% < 0.1% [2K, 4K) < 0.1% < 0.1% [4K, 8K) < 0.1% < 0.1% [8K, 16K) < 0.1% < 0.1% [16K, 32K) < 0.1% < 0.1% [32K, 64K) < 0.1% < 0.1%
Per the result, patched kernel is even slightly better than the base kernel. I think this is because the lock contention against THP split is less than base kernel due to the refactor.
To exclude the affect from THP split, I also did test w/o memory pressure. No obvious regression is spotted. The below is the test result *w/o* memory pressure.
patched base
@us[stress-ng]:
[0] 7.97% 18.4% [1] 69.63% 58.24% [2, 4) 4.18% 2.63% [4, 8) 0.22% 0.17% [8, 16) 1.03% 0.92% [16, 32) 0.14% < 0.1% [32, 64) < 0.1% < 0.1% [64, 128) < 0.1% < 0.1% [128, 256) < 0.1% < 0.1% [256, 512) 0.45% 1.19% [512, 1K) 15.45% 17.27% [1K, 2K) < 0.1% < 0.1% [2K, 4K) < 0.1% < 0.1% [4K, 8K) < 0.1% < 0.1% [8K, 16K) 0.86% 0.88% [16K, 32K) < 0.1% 0.15% [32K, 64K) < 0.1% < 0.1% [64K, 128K) < 0.1% < 0.1% [128K, 256K) < 0.1% < 0.1%
The series also survived a series of tests that exercise NUMA balancing migrations by Mel.
This patch (of 7):
Add orig_pmd to struct vm_fault so the "orig_pmd" parameter used by huge page fault could be removed, just like its PTE counterpart does.
---
include/linux/huge_mm.h | 9 ++++----- include/linux/mm.h | 7 ++++++- mm/huge_memory.c | 9 ++++++--- mm/memory.c | 26 +++++++++++++------------- 4 files changed, 29 insertions(+), 22 deletions(-)
include/linux/huge_mm.h renamed from include/linux/huge_mm.h~mm-memory-add-orig_pmd-to-struct-vm_fault +4 -5
@@ -11,7 +11,7 @@ vm_fault_t do_huge_pmd_anonymous_page(stint copy_huge_pmd (struct mm_struct *dst_mm,struct mm_struct *src_mm,pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr,struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma); -void huge_pmd_set_accessed (struct vm_fault *vmf,pmd_t orig_pmd); +void huge_pmd_set_accessed (struct vm_fault *vmf);int copy_huge_pud (struct mm_struct *dst_mm,struct mm_struct *src_mm,pud_t *dst_pud,pud_t *src_pud,unsigned long addr,struct vm_area_struct *vma);
@@ -24,7 +24,7 @@ static inline void huge_pud_set_accessed }#endif -vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf,pmd_t orig_pmd); +vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf);struct page *follow_trans_huge_pmd (struct vm_area_struct *vma,unsigned long addr,pmd_t *pmd,unsigned int flags);
@@ -288,7 +288,7 @@ struct page *follow_devmap_pmd(struct vmstruct page *follow_devmap_pud (struct vm_area_struct *vma,unsigned long addr,pud_t *pud,int flags,struct dev_pagemap **pgmap); -vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf,pmd_t orig_pmd); +vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf);extern struct page *huge_zero_page;extern unsigned long huge_zero_pfn;
@@ -441,8 +441,7 @@ static inline spinlock_t *pud_trans_hugereturn NULL ; } -static inline vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf, -pmd_t orig_pmd) +static inline vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf) {return 0 ; }
include/linux/mm.h renamed from include/linux/mm.h~mm-memory-add-orig_pmd-to-struct-vm_fault +6 -1
@@ -550,7 +550,12 @@ struct vm_fault {pud_t *pud;/* Pointer to pud entry matching * the 'address' */ -pte_t orig_pte;/* Value of PTE at the time of fault */ +union { +pte_t orig_pte;/* Value of PTE at the time of fault */ +pmd_t orig_pmd;/* Value of PMD at the time of fault, +* used by PMD fault only. +*/ + };struct page *cow_page;/* Page handler may use for COW fault */ struct page *page;/* ->fault handlers should return a
mm/huge_memory.c renamed from mm/huge_memory.c~mm-memory-add-orig_pmd-to-struct-vm_fault +6 -3
@@ -1257,11 +1257,12 @@ unlock: }#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ -void huge_pmd_set_accessed (struct vm_fault *vmf,pmd_t orig_pmd) +void huge_pmd_set_accessed (struct vm_fault *vmf) {pmd_t entry;unsigned long haddr;bool write = vmf->flags &FAULT_FLAG_WRITE ; +pmd_t orig_pmd = vmf->orig_pmd; vmf->ptl =pmd_lock (vmf->vma->vm_mm, vmf->pmd);if (unlikely (!pmd_same (*vmf->pmd, orig_pmd)))
@@ -1278,11 +1279,12 @@ unlock:spin_unlock (vmf->ptl); } -vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf,pmd_t orig_pmd) +vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma;struct page *page;unsigned long haddr = vmf->address &HPAGE_PMD_MASK ; +pmd_t orig_pmd = vmf->orig_pmd; vmf->ptl =pmd_lockptr (vma->vm_mm, vmf->pmd);VM_BUG_ON_VMA (!vma->anon_vma, vma);
@@ -1418,9 +1420,10 @@ out: }/* NUMA hinting page fault entry point for trans huge pmds */ -vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf,pmd_t pmd) +vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma; +pmd_t pmd = vmf->orig_pmd;struct anon_vma *anon_vma =NULL ;struct page *page;unsigned long haddr = vmf->address &HPAGE_PMD_MASK ;
mm/memory.c renamed from mm/memory.c~mm-memory-add-orig_pmd-to-struct-vm_fault +13 -13
@@ -4298,12 +4298,12 @@ static inline vm_fault_t create_huge_pmd }/* `inline' is required to avoid gcc 4.1.2 build error */ -static inline vm_fault_t wp_huge_pmd (struct vm_fault *vmf,pmd_t orig_pmd) +static inline vm_fault_t wp_huge_pmd (struct vm_fault *vmf) {if (vma_is_anonymous (vmf->vma)) { -if (userfaultfd_huge_pmd_wp (vmf->vma, orig_pmd)) +if (userfaultfd_huge_pmd_wp (vmf->vma, vmf->orig_pmd))return handle_userfault (vmf,VM_UFFD_WP ); -return do_huge_pmd_wp_page (vmf, orig_pmd); +return do_huge_pmd_wp_page (vmf); }if (vmf->vma->vm_ops->huge_fault) {vm_fault_t ret = vmf->vma->vm_ops->huge_fault (vmf,PE_SIZE_PMD );
@@ -4530,26 +4530,26 @@ retry_pud:if (!(ret &VM_FAULT_FALLBACK ))return ret; }else { -pmd_t orig_pmd = *vmf.pmd; + vmf.orig_pmd = *vmf.pmd;barrier (); -if (unlikely (is_swap_pmd (orig_pmd))) { +if (unlikely (is_swap_pmd (vmf.orig_pmd))) {VM_BUG_ON (thp_migration_supported () && - !is_pmd_migration_entry (orig_pmd)); -if (is_pmd_migration_entry (orig_pmd)) + !is_pmd_migration_entry (vmf.orig_pmd)); +if (is_pmd_migration_entry (vmf.orig_pmd))pmd_migration_entry_wait (mm, vmf.pmd);return 0 ; } -if (pmd_trans_huge (orig_pmd) ||pmd_devmap (orig_pmd)) { -if (pmd_protnone (orig_pmd) &&vma_is_accessible (vma)) -return do_huge_pmd_numa_page (&vmf, orig_pmd); +if (pmd_trans_huge (vmf.orig_pmd) ||pmd_devmap (vmf.orig_pmd)) { +if (pmd_protnone (vmf.orig_pmd) &&vma_is_accessible (vma)) +return do_huge_pmd_numa_page (&vmf); -if (dirty && !pmd_write (orig_pmd)) { - ret =wp_huge_pmd (&vmf, orig_pmd); +if (dirty && !pmd_write (vmf.orig_pmd)) { + ret =wp_huge_pmd (&vmf);if (!(ret &VM_FAULT_FALLBACK ))return ret; }else { -huge_pmd_set_accessed (&vmf, orig_pmd); +huge_pmd_set_accessed (&vmf);return 0 ; } }
_
Andrew Morton
maintainerPATCH 84/192 mm: memory: make numa_migrate_prep() non-static
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: memory: make numa_migrate_prep() non-static
The numa_migrate_prep() will be used by huge NUMA fault as well in the following patch, make it non-static.
---
mm/internal.h | 3 +++ mm/memory.c | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-)
mm/internal.h renamed from mm/internal.h~mm-memory-make-numa_migrate_prep-non-static +3 -0
@@ -672,4 +672,7 @@ int vmap_pages_range_noflush(unsigned lovoid vunmap_range_noflush (unsigned long start,unsigned long end); +int numa_migrate_prep (struct page *page,struct vm_area_struct *vma, +unsigned long addr,int page_nid,int *flags); +#endif /* __MM_INTERNAL_H */
mm/memory.c renamed from mm/memory.c~mm-memory-make-numa_migrate_prep-non-static +2 -3
@@ -4175,9 +4175,8 @@ static vm_fault_t do_fault(struct vm_faureturn ret; } -static int numa_migrate_prep (struct page *page,struct vm_area_struct *vma, -unsigned long addr,int page_nid, -int *flags) +int numa_migrate_prep (struct page *page,struct vm_area_struct *vma, +unsigned long addr,int page_nid,int *flags) {get_page (page);
_
Andrew Morton
maintainerPATCH 85/192 mm: thp: refactor NUMA fault handling
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: thp: refactor NUMA fault handling
When the THP NUMA fault support was added THP migration was not supported yet. So the ad hoc THP migration was implemented in NUMA fault handling. Since v4.14 THP migration has been supported so it doesn't make too much sense to still keep another THP migration implementation rather than using the generic migration code.
This patch reworks the NUMA fault handling to use generic migration implementation to migrate misplaced page. There is no functional change.
After the refactor the flow of NUMA fault handling looks just like its PTE counterpart:
Acquire ptl
Prepare for migration (elevate page refcount)
Release ptl
Isolate page from lru and elevate page refcount
Migrate the misplaced THP
If migration fails just restore the old normal PMD.
In the old code anon_vma lock was needed to serialize THP migration against THP split, but since then the THP code has been reworked a lot, it seems anon_vma lock is not required anymore to avoid the race.
The page refcount elevation when holding ptl should prevent from THP split.
Use migrate_misplaced_page() for both base page and THP NUMA hinting fault and remove all the dead and duplicate code.
---
include/linux/migrate.h | 23 ---- mm/huge_memory.c | 145 +++++++++---------------------- mm/internal.h | 18 --- mm/migrate.c | 177 +++++++------------------------------- 4 files changed, 77 insertions(+), 286 deletions(-)
include/linux/migrate.h renamed from include/linux/migrate.h~mm-thp-refactor-numa-fault-handling +0 -23
@@ -99,14 +99,9 @@ static inline void __ClearPageMovable(st#endif #ifdef CONFIG_NUMA_BALANCING -extern bool pmd_trans_migrating (pmd_t pmd);extern int migrate_misplaced_page (struct page *page,struct vm_area_struct *vma,int node);#else -static inline bool pmd_trans_migrating (pmd_t pmd) -{ -return false; -}static inline int migrate_misplaced_page (struct page *page,struct vm_area_struct *vma,int node) {
@@ -114,24 +109,6 @@ static inline int migrate_misplaced_page }#endif /* CONFIG_NUMA_BALANCING */ -#if defined(CONFIG_NUMA_BALANCING ) && defined(CONFIG_TRANSPARENT_HUGEPAGE ) -extern int migrate_misplaced_transhuge_page (struct mm_struct *mm, -struct vm_area_struct *vma, -pmd_t *pmd,pmd_t entry, -unsigned long address, -struct page *page,int node); -#else -static inline int migrate_misplaced_transhuge_page (struct mm_struct *mm, -struct vm_area_struct *vma, -pmd_t *pmd,pmd_t entry, -unsigned long address, -struct page *page,int node) -{ -return -EAGAIN ; -} -#endif /* CONFIG_NUMA_BALANCING && CONFIG_TRANSPARENT_HUGEPAGE*/ - -#ifdef CONFIG_MIGRATION /*
mm/huge_memory.c renamed from mm/huge_memory.c~mm-thp-refactor-numa-fault-handling +44 -101
181 lines not on this page · raw message
mm/internal.h renamed from mm/internal.h~mm-thp-refactor-numa-fault-handling +0 -18
@@ -369,23 +369,6 @@ extern unsigned int munlock_vma_page(str*/ extern void clear_page_mlock (struct page *page); -/* -* mlock_migrate_page - called only from migrate_misplaced_transhuge_page() -* (because that does not go through the full procedure of migration ptes): -* to migrate the Mlocked page flag; update statistics. -*/ -static inline void mlock_migrate_page (struct page *newpage,struct page *page) -{ -if (TestClearPageMlocked (page)) { -int nr_pages =thp_nr_pages (page); - -/* Holding pmd lock, no change in irq context: __mod is safe */ -__mod_zone_page_state (page_zone (page),NR_MLOCK , -nr_pages); -SetPageMlocked (newpage); -__mod_zone_page_state (page_zone (newpage),NR_MLOCK , nr_pages); - } -} -extern pmd_t maybe_pmd_mkwrite (pmd_t pmd,struct vm_area_struct *vma);/*
@@ -461,7 +444,6 @@ static inline struct file *maybe_unlock_#else /* !CONFIG_MMU */ static inline void clear_page_mlock (struct page *page) { }static inline void mlock_vma_page (struct page *page) { } -static inline void mlock_migrate_page (struct page *new,struct page *old) { }static inline void vunmap_range_noflush (unsigned long start,unsigned long end) { }
mm/migrate.c renamed from mm/migrate.c~mm-thp-refactor-numa-fault-handling +33 -144
207 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 86/192 mm: migrate: account THP NUMA migration counters correctly
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: migrate: account THP NUMA migration counters correctly
Now both base page and THP NUMA migration is done via migrate_misplaced_page(), keep the counters correctly for THP.
---
mm/migrate.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
mm/migrate.c renamed from mm/migrate.c~mm-migrate-account-thp-numa-migration-counters-correctly +4 -3
@@ -2117,6 +2117,7 @@ int migrate_misplaced_page(struct page *LIST_HEAD (migratepages);new_page_t *new;bool compound; +unsigned int nr_pages =thp_nr_pages (page);/* * PTE mapped THP or HugeTLB page can't reach here so the page could
@@ -2155,13 +2156,13 @@ int migrate_misplaced_page(struct page *if (nr_remaining) {if (!list_empty (&migratepages)) {list_del (&page->lru); -dec_node_page_state (page,NR_ISOLATED_ANON + -page_is_file_lru (page)); +mod_node_page_state (page_pgdat (page),NR_ISOLATED_ANON + +page_is_file_lru (page), -nr_pages);putback_lru_page (page); } isolated =0 ; }else -count_vm_numa_event (NUMA_PAGE_MIGRATE ); +count_vm_numa_events (NUMA_PAGE_MIGRATE , nr_pages);BUG_ON (!list_empty (&migratepages));return isolated;
_
Andrew Morton
maintainerPATCH 87/192 mm: migrate: don't split THP for misplaced NUMA page
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: migrate: don't split THP for misplaced NUMA page
The old behavior didn't split THP if migration is failed due to lack of memory on the target node. But the THP migration does split THP, so keep the old behavior for misplaced NUMA page migration.
---
mm/migrate.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/migrate.c renamed from mm/migrate.c~mm-migrate-dont-split-thp-for-misplaced-numa-page +3 -1
@@ -1423,6 +1423,7 @@ int migrate_pages(struct list_head *fromint swapwrite = current->flags &PF_SWAPWRITE ;int rc, nr_subpages;LIST_HEAD (ret_pages); +bool nosplit = (reason ==MR_NUMA_MISPLACED );trace_mm_migrate_pages_start (mode, reason);
@@ -1494,8 +1495,9 @@ retry:/* * When memory is low, don't bother to try to migrate * other pages, just exit. +* THP NUMA faulting doesn't split THP to retry. */ -if (is_thp) { +if (is_thp && !nosplit) {if (!try_split_thp (page, &page2, from)) { nr_thp_split++; goto retry;
_
Andrew Morton
maintainerPATCH 88/192 mm: migrate: check mapcount for THP instead of refcount
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: migrate: check mapcount for THP instead of refcount
The generic migration path will check refcount, so no need check refcount here. But the old code actually prevents from migrating shared THP (mapped by multiple processes), so bail out early if mapcount is > 1 to keep the behavior.
---
mm/migrate.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
mm/migrate.c renamed from mm/migrate.c~mm-migrate-check-mapcount-for-thp-instead-of-refcount +4 -12
@@ -2073,6 +2073,10 @@ static int numamigrate_isolate_page(pg_dVM_BUG_ON_PAGE (compound_order (page) && !PageTransHuge (page), page); +/* Do not migrate THP mapped by multiple processes */ +if (PageTransHuge (page) &&total_mapcount (page) >1 ) +return 0 ; +/* Avoid migrating to a node that is nearly full */ if (!migrate_balanced_pgdat (pgdat,compound_nr (page)))return 0 ;
@@ -2080,18 +2084,6 @@ static int numamigrate_isolate_page(pg_dif (isolate_lru_page (page))return 0 ; -/* -* migrate_misplaced_transhuge_page() skips page migration's usual -* check on page_count(), so we must do it here, now that the page -* has been isolated: a GUP pin, or any other pin, prevents migration. -* The expected page count is 3: 1 for page's mapcount and 1 for the -* caller's pin and 1 for the reference taken by isolate_lru_page(). -*/ -if (PageTransHuge (page) &&page_count (page) !=3 ) { -putback_lru_page (page); -return 0 ; - } - page_lru =page_is_file_lru (page);mod_node_page_state (page_pgdat (page),NR_ISOLATED_ANON + page_lru,thp_nr_pages (page));
_
Andrew Morton
maintainerPATCH 89/192 mm: thp: skip make PMD PROT_NONE if THP migration is not supported
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: thp: skip make PMD PROT_NONE if THP migration is not supported
A quick grep shows x86_64, PowerPC (book3s), ARM64 and S390 support both NUMA balancing and THP. But S390 doesn't support THP migration so NUMA balancing actually can't migrate any misplaced pages.
Skip make PMD PROT_NONE for such case otherwise CPU cycles may be wasted by pointless NUMA hinting faults on S390.
---
mm/huge_memory.c | 4 ++++ 1 file changed, 4 insertions(+)
mm/huge_memory.c renamed from mm/huge_memory.c~mm-thp-skip-make-pmd-prot_none-if-thp-migration-is-not-supported +4 -0
@@ -1742,6 +1742,7 @@ bool move_huge_pmd(struct vm_area_struct* Returns * - 0 if PMD could not be locked * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary +* or if prot_numa but THP migration is not supported * - HPAGE_PMD_NR if protections changed and TLB flush necessary */ int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
@@ -1756,6 +1757,9 @@ int change_huge_pmd(struct vm_area_strucbool uffd_wp = cp_flags &MM_CP_UFFD_WP ;bool uffd_wp_resolve = cp_flags &MM_CP_UFFD_WP_RESOLVE ; +if (prot_numa && !thp_migration_supported ()) +return 1 ; + ptl =__pmd_trans_huge_lock (pmd, vma);if (!ptl)return 0 ;
_
Andrew Morton
maintainerPATCH 90/192 mm/thp: make ARCH_ENABLE_SPLIT_PMD_PTLOCK dependent on PGTABLE_LEVELS > 2
In reply to Andrew MortonFrom: Anshuman Khandual <[email protected]>
Subject: mm/thp: make ARCH_ENABLE_SPLIT_PMD_PTLOCK dependent on PGTABLE_LEVELS > 2
ARCH_ENABLE_SPLIT_PMD_PTLOCK is irrelevant unless there are more than two page table levels including PMD (also per Documentation/vm/split_page_table_lock.rst). Make this dependency explicit on remaining platforms i.e x86 and s390 where ARCH_ENABLE_SPLIT_PMD_PTLOCK is subscribed.
---
arch/s390/Kconfig | 2 +- arch/x86/Kconfig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/s390/Kconfig renamed from arch/s390/Kconfig~mm-thp-make-arch_enable_split_pmd_ptlock-dependent-on-pgtable_levels-2 +1 -1
@@ -62,7 +62,7 @@ config S390 select ARCH_BINFMT_ELF_STATE select ARCH_ENABLE_MEMORY_HOTPLUG if SPARSEMEM select ARCH_ENABLE_MEMORY_HOTREMOVE - select ARCH_ENABLE_SPLIT_PMD_PTLOCK + select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2 select ARCH_HAS_DEBUG_VM_PGTABLE select ARCH_HAS_DEBUG_WX select ARCH_HAS_DEVMEM_IS_ALLOWED
arch/x86/Kconfig renamed from arch/x86/Kconfig~mm-thp-make-arch_enable_split_pmd_ptlock-dependent-on-pgtable_levels-2 +1 -1
@@ -63,7 +63,7 @@ config X86 select ARCH_ENABLE_HUGEPAGE_MIGRATION if X86_64 && HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG if X86_64 || (X86_32 && HIGHMEM) select ARCH_ENABLE_MEMORY_HOTREMOVE if MEMORY_HOTPLUG - select ARCH_ENABLE_SPLIT_PMD_PTLOCK if X86_64 || X86_PAE + select ARCH_ENABLE_SPLIT_PMD_PTLOCK if (PGTABLE_LEVELS > 2) && (X86_64 || X86_PAE) select ARCH_ENABLE_THP_MIGRATION if X86_64 && TRANSPARENT_HUGEPAGE select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI select ARCH_HAS_CACHE_LINE_SIZE
_
Andrew Morton
maintainerPATCH 91/192 mm: rmap: make try_to_unmap() void function
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: rmap: make try_to_unmap() void function
Currently try_to_unmap() return bool value by checking page_mapcount(), however this may return false positive since page_mapcount() doesn't check all subpages of compound page. The total_mapcount() could be used instead, but its cost is higher since it traverses all subpages.
Actually the most callers of try_to_unmap() don't care about the return value at all. So just need check if page is still mapped by page_mapped() when necessary. And page_mapped() does bail out early when it finds mapped subpage.
---
include/linux/rmap.h | 2 +- mm/memory-failure.c | 15 +++++++-------- mm/rmap.c | 15 ++++----------- mm/vmscan.c | 3 ++- 4 files changed, 14 insertions(+), 21 deletions(-)
include/linux/rmap.h renamed from include/linux/rmap.h~mm-rmap-make-try_to_unmap-void-function +1 -1
@@ -195,7 +195,7 @@ static inline void page_dup_rmap(structint page_referenced (struct page *,int is_locked,struct mem_cgroup *memcg,unsigned long *vm_flags); -bool try_to_unmap (struct page *,enum ttu_flags flags); +void try_to_unmap (struct page *,enum ttu_flags flags);/* Avoid racy checks */ #define PVMW_SYNC (1 << 0)
mm/memory-failure.c renamed from mm/memory-failure.c~mm-rmap-make-try_to_unmap-void-function +7 -8
@@ -1269,7 +1269,7 @@ static bool hwpoison_user_mappings(strucenum ttu_flags ttu =TTU_IGNORE_MLOCK ;struct address_space *mapping;LIST_HEAD (tokill); -bool unmap_success = true; +bool unmap_success;int kill =1 , forcekill;struct page *hpage = *hpagep;bool mlocked =PageMlocked (hpage);
@@ -1332,7 +1332,7 @@ static bool hwpoison_user_mappings(struccollect_procs (hpage, &tokill, flags &MF_ACTION_REQUIRED );if (!PageHuge (hpage)) { - unmap_success =try_to_unmap (hpage, ttu); +try_to_unmap (hpage, ttu); }else {if (!PageAnon (hpage)) {/*
@@ -1344,17 +1344,16 @@ static bool hwpoison_user_mappings(struc*/ mapping =hugetlb_page_mapping_lock_write (hpage);if (mapping) { - unmap_success =try_to_unmap (hpage, - ttu|TTU_RMAP_LOCKED ); +try_to_unmap (hpage, ttu|TTU_RMAP_LOCKED );i_mmap_unlock_write (mapping); - }else { + }else pr_info ("Memory failure: %#lx: could not lock mapping for mapped huge page\n" , pfn); - unmap_success = false; - } }else { - unmap_success =try_to_unmap (hpage, ttu); +try_to_unmap (hpage, ttu); } } + + unmap_success = !page_mapped (hpage);if (!unmap_success)pr_err ("Memory failure: %#lx: failed to unmap page (mapcount=%d)\n" , pfn,page_mapcount (hpage));
mm/rmap.c renamed from mm/rmap.c~mm-rmap-make-try_to_unmap-void-function +4 -11
@@ -1405,7 +1405,7 @@ static bool try_to_unmap_one(struct page/* * When racing against e.g. zap_pte_range() on another cpu, * in between its ptep_get_and_clear_full() and page_remove_rmap(), -* try_to_unmap() may return false when it is about to become true, +* try_to_unmap() may return before page_mapped() has become false, * if page table locking is skipped: use TTU_SYNC to wait for that. */ if (flags &TTU_SYNC )
@@ -1756,9 +1756,10 @@ static int page_not_mapped(struct page ** Tries to remove all the page table entries which are mapping this * page, used in the pageout path. Caller must hold the page lock. * -* If unmap is successful, return true. Otherwise, false. +* It is the caller's responsibility to check if the page is still +* mapped when needed (use TTU_SYNC to prevent accounting races). */ -bool try_to_unmap (struct page *page,enum ttu_flags flags) +void try_to_unmap (struct page *page,enum ttu_flags flags) {struct rmap_walk_control rwc = { .rmap_one = try_to_unmap_one,
@@ -1783,14 +1784,6 @@ bool try_to_unmap(struct page *page, enurmap_walk_locked (page, &rwc); else rmap_walk(page, &rwc); - -/* -* When racing against e.g. zap_pte_range() on another cpu, -* in between its ptep_get_and_clear_full() and page_remove_rmap(), -* try_to_unmap() may return false when it is about to become true, -* if page table locking is skipped: use TTU_SYNC to wait for that. -*/ -return !page_mapcount (page); }/**
mm/vmscan.c renamed from mm/vmscan.c~mm-rmap-make-try_to_unmap-void-function +2 -1
@@ -1499,7 +1499,8 @@ static unsigned int shrink_page_list(strif (unlikely (PageTransHuge (page))) flags |=TTU_SPLIT_HUGE_PMD ; -if (!try_to_unmap (page, flags)) { +try_to_unmap (page, flags); +if (page_mapped (page)) { stat->nr_unmap_fail += nr_pages;if (!was_swapbacked &&PageSwapBacked (page)) stat->nr_lazyfree_fail += nr_pages;
_