mirror of
https://github.com/torvalds/linux.git
synced 2026-04-24 17:42:27 -04:00
This follow-up patch completes centralization of kselftest.h and ksefltest_harness.h includes in remaining seltests files, replacing all relative paths with a non-relative paths using shared -I include path in lib.mk Tested with gcc-13.3 and clang-18.1, and cross-compiled successfully on riscv, arm64, x86_64 and powerpc arch. [reddybalavignesh9979@gmail.com: add selftests include path for kselftest.h] Link: https://lkml.kernel.org/r/20251017090201.317521-1-reddybalavignesh9979@gmail.com Link: https://lkml.kernel.org/r/20251016104409.68985-1-reddybalavignesh9979@gmail.com Signed-off-by: Bala-Vignesh-Reddy <reddybalavignesh9979@gmail.com> Suggested-by: Andrew Morton <akpm@linux-foundation.org> Link: https://lore.kernel.org/lkml/20250820143954.33d95635e504e94df01930d0@linux-foundation.org/ Reviewed-by: Wei Yang <richard.weiyang@gmail.com> Cc: David Hildenbrand <david@redhat.com> Cc: David S. Miller <davem@davemloft.net> Cc: Eric Dumazet <edumazet@google.com> Cc: Günther Noack <gnoack@google.com> Cc: Jakub Kacinski <kuba@kernel.org> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mickael Salaun <mic@digikod.net> Cc: Ming Lei <ming.lei@redhat.com> Cc: Paolo Abeni <pabeni@redhat.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Simon Horman <horms@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
127 lines
2.8 KiB
C
127 lines
2.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* A test case that must run on a system with one and only one huge page available.
|
|
* # echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
|
|
*
|
|
* During setup, the test allocates the only available page, and starts three threads:
|
|
* - thread1:
|
|
* * madvise(MADV_DONTNEED) on the allocated huge page
|
|
* - thread 2:
|
|
* * Write to the allocated huge page
|
|
* - thread 3:
|
|
* * Try to allocated an extra huge page (which must not available)
|
|
*
|
|
* The test fails if thread3 is able to allocate a page.
|
|
*
|
|
* Touching the first page after thread3's allocation will raise a SIGBUS
|
|
*
|
|
* Author: Breno Leitao <leitao@debian.org>
|
|
*/
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#include "vm_util.h"
|
|
#include "kselftest.h"
|
|
|
|
#define INLOOP_ITER 100
|
|
|
|
size_t mmap_size;
|
|
char *huge_ptr;
|
|
|
|
/* Touch the memory while it is being madvised() */
|
|
void *touch(void *unused)
|
|
{
|
|
for (int i = 0; i < INLOOP_ITER; i++)
|
|
huge_ptr[0] = '.';
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void *madv(void *unused)
|
|
{
|
|
for (int i = 0; i < INLOOP_ITER; i++)
|
|
madvise(huge_ptr, mmap_size, MADV_DONTNEED);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* We got here, and there must be no huge page available for mapping
|
|
* The other hugepage should be flipping from used <-> reserved, because
|
|
* of madvise(DONTNEED).
|
|
*/
|
|
void *map_extra(void *unused)
|
|
{
|
|
void *ptr;
|
|
|
|
for (int i = 0; i < INLOOP_ITER; i++) {
|
|
ptr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
|
|
-1, 0);
|
|
|
|
if ((long)ptr != -1) {
|
|
/* Touching the other page now will cause a SIGBUG
|
|
* huge_ptr[0] = '1';
|
|
*/
|
|
return ptr;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
pthread_t thread1, thread2, thread3;
|
|
unsigned long free_hugepages;
|
|
void *ret;
|
|
|
|
/*
|
|
* On kernel 6.7, we are able to reproduce the problem with ~10
|
|
* interactions
|
|
*/
|
|
int max = 10;
|
|
|
|
free_hugepages = get_free_hugepages();
|
|
|
|
if (free_hugepages != 1) {
|
|
ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
|
|
free_hugepages);
|
|
}
|
|
|
|
mmap_size = default_huge_page_size();
|
|
|
|
while (max--) {
|
|
huge_ptr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
|
|
-1, 0);
|
|
|
|
if ((unsigned long)huge_ptr == -1) {
|
|
ksft_test_result_fail("Failed to allocate huge page\n");
|
|
return KSFT_FAIL;
|
|
}
|
|
|
|
pthread_create(&thread1, NULL, madv, NULL);
|
|
pthread_create(&thread2, NULL, touch, NULL);
|
|
pthread_create(&thread3, NULL, map_extra, NULL);
|
|
|
|
pthread_join(thread1, NULL);
|
|
pthread_join(thread2, NULL);
|
|
pthread_join(thread3, &ret);
|
|
|
|
if (ret) {
|
|
ksft_test_result_fail("Unexpected huge page allocation\n");
|
|
return KSFT_FAIL;
|
|
}
|
|
|
|
/* Unmap and restart */
|
|
munmap(huge_ptr, mmap_size);
|
|
}
|
|
|
|
return KSFT_PASS;
|
|
}
|