linux/tools/testing/selftests/mm
Lorenzo Stoakes (ARM) 97d34aa65c mm/secretmem: properly account locked pages
secretmem accounts folios by treating memory as if it were mlock()'d and
thus limited by the RLIMIT_MEMLOCK limit.

However the folios are unevictable and remain so until the inode is
evicted, eliminating usual mlock() semantics - mapping folios then
unmapping them does not clear their unevictable state, since it depends on
AS_UNEVICTABLE, not PG_mlocked.

A user can therefore easily work around the RLIMIT_MEMLOCK limit - simply
map then unmap and VmLck no longer counts the secretmem range.  Worse,
folios are not accounted in the process's RSS, meaning the OOM killer
won't know to kill the process.

Repeatedly mapping/unmapping (or forking) can then result in the
consumption of all available system memory with unevictable folios and
cause system instability.

A secretmem fd can be passed between processes and over fork so a
per-process limit simply does not make sense, so follow the precedent set
by io_uring, perf, skbuff, iommufd and xdp by tracking the number of
locked pages in user_struct->locked_vm.

Since the scope tracked is actually inode lifetime, the RLIMIT_MEMLOCK
applies per-user not per-process, so it doesn't make sense to bypass for
users with CAP_IPC_LOCK, therefore remove this bypass.

There is simply no reason to carry on marking the mapping as mlock()'d
since it's misleading and the lifecycle is now correctly handled, so
remove this too.

Note that secretmem does not support any form of truncation (including
hole punching) and the folios are unreclaimable, so the folios need only
be accounted on fault and unaccounted on inode destruction.

__secretmem_account_pages() is more or less a duplicate of the code that
io_uring etc.  use, but since this is a bug fix that needs backporting,
defer any de-duplication efforts to a follow-up.

test_mlock_limit() asserts mlock_future_ok() on mmap(), however this has
been removed, so remove the test altogether for the fix.  A new test will
be sent separately for upstream.

Link: https://lore.kernel.org/20260826-secretmem-accounting-v3-1-94cb04399510@kernel.org
Fixes: 1507f51255 ("mm: introduce memfd_secret system call to create "secret" memory areas")
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reported-by: Daehyeon Ko <4ncienth@gmail.com>
Closes: https://lore.kernel.org/linux-mm/20260813225328.2010303-1-4ncienth@gmail.com/
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Tested-by: Daehyeon Ko <4ncienth@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: David S. Miller <davem@davemloft.net>
Cc: Hagen Paul Pfeifer <hagen@jauu.net>
Cc: Jakub Kacinski <kuba@kernel.org>
Cc: James Bottomley <james.bottomley@HansenPartnership.com>
Cc: Jesper Dangaard Brouer <hawk@kernel.org>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Stanislav Fomichev <sdf@fomichev.me>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2026-09-03 10:56:10 -07:00
..
page_frag mm/page_frag: reject invalid CPUs in page_frag_test 2026-06-21 11:37:37 -07:00
.gitignore selftests/mm: use pattern matching in .gitignore 2026-08-24 18:43:02 -07:00
charge_reserved_hugetlb.sh selftests/mm: fix hugetlb pathname construction in charge_reserved_hugetlb.sh 2026-06-21 11:37:32 -07:00
check_config.sh selftests/mm: rename local_config.h to local_config.h_gen 2026-08-24 18:43:01 -07:00
compaction_test.c selftests/mm: read memory information without popen 2026-08-24 18:43:01 -07:00
config selftests/mm: specify requirement for PROC_MEM_ALWAYS_FORCE=y 2026-04-27 05:54:26 -07:00
cow.c selftests/mm: skip COW tmpfile cases when fallocate() is unsupported 2026-08-24 18:43:09 -07:00
droppable.c selftests: mm: fix and speedup "droppable" test 2026-06-21 11:37:36 -07:00
folio_split_race_test.c selftests: mm: extend the check_huge() to support mTHP check 2026-08-24 18:43:07 -07:00
guard-regions.c selftests/mm: skip guard hole-punch test if MADV_REMOVE is unsupported 2026-08-24 18:43:09 -07:00
gup_longterm.c selftests/mm: rename local_config.h to local_config.h_gen 2026-08-24 18:43:01 -07:00
gup_test.c selftests/mm: gup_test: add setup of HugeTLB pages 2026-06-21 11:37:26 -07:00
hmm-tests.c selftests/mm: factor out hmm_buffer_alloc() to consolidate buffer setup 2026-08-06 18:56:56 -07:00
hugepage_settings.c selftests/mm: handle EINVAL when configuring gigantic hugepages 2026-08-04 19:18:47 -07:00
hugepage_settings.h selftests/mm: move read_file(), read_num() and write_num() to vm_util 2026-06-21 11:37:24 -07:00
hugetlb_dio.c selftests/mm: hugepage_dio: add setup of HugeTLB pages 2026-06-21 11:37:26 -07:00
hugetlb_fault_after_madv.c selftests/mm: hugetlb_fault_after_madv: add setup of HugeTLB pages 2026-06-21 11:37:26 -07:00
hugetlb_madv_vs_map.c selftests/mm: hugetlb_madv_vs_map: add setup of HugeTLB pages 2026-06-21 11:37:27 -07:00
hugetlb_reparenting_test.sh selftests/mm: fix cgroup task placement and drop memory.current checks in hugetlb_reparenting_test.sh 2026-06-21 11:37:33 -07:00
hugetlb-madvise.c selftests/mm: hugetlb-madvise: add setup of HugeTLB pages 2026-06-21 11:37:27 -07:00
hugetlb-mmap.c selftests/mm: hugetlb-mmap: add setup of HugeTLB pages 2026-06-21 11:37:27 -07:00
hugetlb-mremap.c selftests/mm: ensure destination is hugetlb-backed in hugetlb-mremap 2026-06-21 11:37:34 -07:00
hugetlb-read-hwpoison.c selftests/mm: hugetlb-read-hwpoison: use kselftest framework 2026-06-21 11:37:20 -07:00
hugetlb-shm.c selftests/mm: hugetlb-shm: add setup of HugeTLB pages 2026-06-21 11:37:28 -07:00
hugetlb-soft-offline.c selftests/mm: hugetlb-soft-offline: add setup of HugeTLB pages 2026-06-21 11:37:28 -07:00
khugepaged.c selftests/mm: check stat() return value in khugepaged get_finfo() 2026-08-24 18:43:27 -07:00
ksft_compaction.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_cow.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_gup_test.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_hmm.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_hugetlb.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_hugevm.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_kmemleak_confirm.sh selftests/mm: kmemleak: drop stale min_unref_scans default from comments 2026-08-24 18:43:18 -07:00
ksft_kmemleak_dedup.sh selftests/mm: add kmemleak verbose dedup test 2026-05-28 21:05:09 -07:00
ksft_ksm_numa.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_ksm.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_madv_guard.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_madv_populate.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_mdwe.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_memfd_secret.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_memory_failure.sh selftests/mm: add memory failure anonymous page test 2026-02-12 15:43:01 -08:00
ksft_migration.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_mkdirty.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_mlock.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_mmap.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_mremap.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_page_frag.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_pagemap.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_pfnmap.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_pkey.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_process_madv.sh selftests/mm: fix ksft_process_madv.sh test category 2026-07-01 19:02:52 -07:00
ksft_process_mrelease.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_rmap.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_soft_dirty.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_thp.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_userfaultfd.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_vma_merge.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksft_vmalloc.sh selftests/mm: have the harness run each test category separately 2026-01-31 14:22:53 -08:00
ksm_functional_tests.c selftests/mm: ksm-functional-tests: fix partial write handling 2026-06-02 15:22:15 -07:00
ksm_tests.c selftests/mm: fix ternary operator precedence in ksm_tests 2026-08-04 19:18:48 -07:00
madv_populate.c selftests/mm: fix incorrect mmap() error handling with NULL instead of MAP_FAILED 2026-06-21 11:37:31 -07:00
Makefile selftests: mm: test kmemleak's N-consecutive-scan leak confirmation 2026-08-24 18:43:18 -07:00
map_fixed_noreplace.c selftests: complete kselftest include centralization 2025-11-27 14:24:31 -08:00
map_populate.c selftests: complete kselftest include centralization 2025-11-27 14:24:31 -08:00
mdwe_test.c selftests: complete kselftest include centralization 2025-11-27 14:24:31 -08:00
memfd_secret.c mm/secretmem: properly account locked pages 2026-09-03 10:56:10 -07:00
memory-failure.c selftests/mm: skip hard dirty page-cache test on NFS 2026-08-24 18:43:09 -07:00
merge.c tools/testing/selftests/mm: test anonymous page offset merge behaviour 2026-08-24 18:42:54 -07:00
migration.c selftests/mm: retry migration failures for the full runtime 2026-08-24 18:43:10 -07:00
mkdirty.c selftests: complete kselftest include centralization 2025-11-27 14:24:31 -08:00
mlock-random-test.c selftests/mm: fix potential wild pointer access of getline due to missing init 2026-07-28 17:37:34 -07:00
mlock2-tests.c selftests/mm: verify droppable mappings cannot be locked 2026-05-28 21:04:48 -07:00
mlock2.h selftests/mm/mlock: print error on failure 2025-03-16 22:06:40 -07:00
mrelease_test.c selftests: complete kselftest include centralization 2025-11-27 14:24:31 -08:00
mremap_dontunmap.c selftests/mm: fix BUG_ON checking wrong variable in mremap_dontunmap 2026-08-06 18:57:14 -07:00
mremap_test.c selftests/mm: simplify byte pattern checking in mremap_test 2026-05-28 21:04:53 -07:00
mseal_helpers.h selftests/mm: mseal, self_elf: rename TEST_END_CHECK to REPORT_TEST_PASS 2024-07-03 19:30:07 -07:00
mseal_test.c selftests/mm: drop duplicate test_seal_mprotect_two_vma_with_gap() call 2026-08-24 18:43:08 -07:00
on-fault-limit.c selftests: complete kselftest include centralization 2025-11-27 14:24:31 -08:00
pagemap_ioctl.c selftests/mm: drop redundant open() in mprotect_tests() 2026-08-24 18:43:26 -07:00
pfnmap.c selftests/mm: report SKIP in pfnmap if a check fails 2026-01-31 14:22:42 -08:00
pkey_sighandler_tests.c selftests/mm: fix clone cleartid race in pkey sighandler tests 2026-07-30 19:40:30 -07:00
pkey_util.c selftests/mm: move pkey selftest helpers to pkey_util.c 2026-07-30 19:40:29 -07:00
pkey-arm64.h selftests/mm: Fix resv_sz when parsing arm64 signal frame 2026-05-19 11:54:03 +01:00
pkey-helpers.h selftests/mm: move pkey selftest helpers to pkey_util.c 2026-07-30 19:40:29 -07:00
pkey-powerpc.h selftests/mm: add missing mmap() return checks in pkey tests 2026-07-30 19:40:30 -07:00
pkey-x86.h selftests/mm: ensure pkey-*.h define inline functions only 2025-01-13 22:40:55 -08:00
prctl_thp_disable.c selftests: mm: extend the check_huge() to support mTHP check 2026-08-24 18:43:07 -07:00
process_madv.c mm/madvise: reject invalid process_madvise() advice for zero-length vectors 2026-05-28 21:04:57 -07:00
protection_keys.c selftests/mm: add missing mmap() return checks in pkey tests 2026-07-30 19:40:30 -07:00
rmap.c ksm: add mremap selftests for ksm_rmap_walk 2026-08-04 19:18:45 -07:00
run_vmtests.sh selftests: mm: add mTHP collapse test cases 2026-08-24 18:43:07 -07:00
settings selftests/mm: increase timeout from 180 to 900 seconds 2025-06-19 20:48:01 -07:00
soft-dirty.c selftests/mm: fix unchecked ftruncate return value in soft-dirty test 2026-08-24 18:43:27 -07:00
split_huge_page_test.c selftests: mm: move gather_after_split_folio_orders() into vm_util.c file 2026-08-24 18:43:07 -07:00
test_hmm.sh selftests/mm: give scripts execute permission 2023-07-14 09:24:31 -07:00
test_page_frag.sh mm: page_frag: add a test module for page_frag 2024-11-11 10:56:26 -08:00
test_vmalloc.sh lib/test_vmalloc.c: minor fixes to test_vmalloc.c 2026-01-20 19:24:47 -08:00
thuge-gen.c selftests/mm: thuge-gen: fix test_shmget() for PAGE_SIZE check 2026-08-15 21:12:24 -07:00
transhuge-stress.c selftests/mm: rename thp_settings.[ch] to hugepage_settings.[ch] 2026-06-21 11:37:23 -07:00
uffd-common.c selftests: mm: extend the check_huge() to support mTHP check 2026-08-24 18:43:07 -07:00
uffd-common.h selftests/mm: move HugeTLB helpers to hugepage_settings 2026-06-21 11:37:23 -07:00
uffd-stress.c selftests/mm: skip uffd-stress test when nr_pages_per_cpu is zero 2026-06-21 11:37:34 -07:00
uffd-unit-tests.c selftests/mm: add userfaultfd RWP tests 2026-08-04 19:18:39 -07:00
uffd-wp-mremap.c selftests/mm: skip uffd-wp-mremap if UFFD write-protect is unsupported 2026-06-21 11:37:34 -07:00
va_high_addr_switch.c selftests/mm: va_high_addr_switch: add setup of HugeTLB pages 2026-06-21 11:37:30 -07:00
va_high_addr_switch.sh selftests/mm: va_high_addr_switch.sh: drop huge pages setup 2026-06-21 11:37:30 -07:00
vm_util.c selftests/mm: fix read_file() return value check 2026-08-24 18:43:11 -07:00
vm_util.h selftests: mm: move gather_after_split_folio_orders() into vm_util.c file 2026-08-24 18:43:07 -07:00
write_hugetlb_memory.sh selftests/mm: switch to bash from sh 2024-01-25 23:52:20 -08:00
write_to_hugetlbfs.c selftests/mm/write_to_hugetlbfs: parse -s as size_t 2026-01-20 19:24:50 -08:00