linux/lib/test_vmalloc.c
Linus Torvalds 7fa8a8ee94 - Nick Piggin's "shoot lazy tlbs" series, to improve the peformance of
switching from a user process to a kernel thread.
 
 - More folio conversions from Kefeng Wang, Zhang Peng and Pankaj Raghav.
 
 - zsmalloc performance improvements from Sergey Senozhatsky.
 
 - Yue Zhao has found and fixed some data race issues around the
   alteration of memcg userspace tunables.
 
 - VFS rationalizations from Christoph Hellwig:
 
   - removal of most of the callers of write_one_page().
 
   - make __filemap_get_folio()'s return value more useful
 
 - Luis Chamberlain has changed tmpfs so it no longer requires swap
   backing.  Use `mount -o noswap'.
 
 - Qi Zheng has made the slab shrinkers operate locklessly, providing
   some scalability benefits.
 
 - Keith Busch has improved dmapool's performance, making part of its
   operations O(1) rather than O(n).
 
 - Peter Xu adds the UFFD_FEATURE_WP_UNPOPULATED feature to userfaultd,
   permitting userspace to wr-protect anon memory unpopulated ptes.
 
 - Kirill Shutemov has changed MAX_ORDER's meaning to be inclusive rather
   than exclusive, and has fixed a bunch of errors which were caused by its
   unintuitive meaning.
 
 - Axel Rasmussen give userfaultfd the UFFDIO_CONTINUE_MODE_WP feature,
   which causes minor faults to install a write-protected pte.
 
 - Vlastimil Babka has done some maintenance work on vma_merge():
   cleanups to the kernel code and improvements to our userspace test
   harness.
 
 - Cleanups to do_fault_around() by Lorenzo Stoakes.
 
 - Mike Rapoport has moved a lot of initialization code out of various
   mm/ files and into mm/mm_init.c.
 
 - Lorenzo Stoakes removd vmf_insert_mixed_prot(), which was added for
   DRM, but DRM doesn't use it any more.
 
 - Lorenzo has also coverted read_kcore() and vread() to use iterators
   and has thereby removed the use of bounce buffers in some cases.
 
 - Lorenzo has also contributed further cleanups of vma_merge().
 
 - Chaitanya Prakash provides some fixes to the mmap selftesting code.
 
 - Matthew Wilcox changes xfs and afs so they no longer take sleeping
   locks in ->map_page(), a step towards RCUification of pagefaults.
 
 - Suren Baghdasaryan has improved mmap_lock scalability by switching to
   per-VMA locking.
 
 - Frederic Weisbecker has reworked the percpu cache draining so that it
   no longer causes latency glitches on cpu isolated workloads.
 
 - Mike Rapoport cleans up and corrects the ARCH_FORCE_MAX_ORDER Kconfig
   logic.
 
 - Liu Shixin has changed zswap's initialization so we no longer waste a
   chunk of memory if zswap is not being used.
 
 - Yosry Ahmed has improved the performance of memcg statistics flushing.
 
 - David Stevens has fixed several issues involving khugepaged,
   userfaultfd and shmem.
 
 - Christoph Hellwig has provided some cleanup work to zram's IO-related
   code paths.
 
 - David Hildenbrand has fixed up some issues in the selftest code's
   testing of our pte state changing.
 
 - Pankaj Raghav has made page_endio() unneeded and has removed it.
 
 - Peter Xu contributed some rationalizations of the userfaultfd
   selftests.
 
 - Yosry Ahmed has fixed an issue around memcg's page recalim accounting.
 
 - Chaitanya Prakash has fixed some arm-related issues in the
   selftests/mm code.
 
 - Longlong Xia has improved the way in which KSM handles hwpoisoned
   pages.
 
 - Peter Xu fixes a few issues with uffd-wp at fork() time.
 
 - Stefan Roesch has changed KSM so that it may now be used on a
   per-process and per-cgroup basis.
 -----BEGIN PGP SIGNATURE-----
 
 iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCZEr3zQAKCRDdBJ7gKXxA
 jlLoAP0fpQBipwFxED0Us4SKQfupV6z4caXNJGPeay7Aj11/kQD/aMRC2uPfgr96
 eMG3kwn2pqkB9ST2QpkaRbxA//eMbQY=
 =J+Dj
 -----END PGP SIGNATURE-----

Merge tag 'mm-stable-2023-04-27-15-30' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Pull MM updates from Andrew Morton:

 - Nick Piggin's "shoot lazy tlbs" series, to improve the peformance of
   switching from a user process to a kernel thread.

 - More folio conversions from Kefeng Wang, Zhang Peng and Pankaj
   Raghav.

 - zsmalloc performance improvements from Sergey Senozhatsky.

 - Yue Zhao has found and fixed some data race issues around the
   alteration of memcg userspace tunables.

 - VFS rationalizations from Christoph Hellwig:
     - removal of most of the callers of write_one_page()
     - make __filemap_get_folio()'s return value more useful

 - Luis Chamberlain has changed tmpfs so it no longer requires swap
   backing. Use `mount -o noswap'.

 - Qi Zheng has made the slab shrinkers operate locklessly, providing
   some scalability benefits.

 - Keith Busch has improved dmapool's performance, making part of its
   operations O(1) rather than O(n).

 - Peter Xu adds the UFFD_FEATURE_WP_UNPOPULATED feature to userfaultd,
   permitting userspace to wr-protect anon memory unpopulated ptes.

 - Kirill Shutemov has changed MAX_ORDER's meaning to be inclusive
   rather than exclusive, and has fixed a bunch of errors which were
   caused by its unintuitive meaning.

 - Axel Rasmussen give userfaultfd the UFFDIO_CONTINUE_MODE_WP feature,
   which causes minor faults to install a write-protected pte.

 - Vlastimil Babka has done some maintenance work on vma_merge():
   cleanups to the kernel code and improvements to our userspace test
   harness.

 - Cleanups to do_fault_around() by Lorenzo Stoakes.

 - Mike Rapoport has moved a lot of initialization code out of various
   mm/ files and into mm/mm_init.c.

 - Lorenzo Stoakes removd vmf_insert_mixed_prot(), which was added for
   DRM, but DRM doesn't use it any more.

 - Lorenzo has also coverted read_kcore() and vread() to use iterators
   and has thereby removed the use of bounce buffers in some cases.

 - Lorenzo has also contributed further cleanups of vma_merge().

 - Chaitanya Prakash provides some fixes to the mmap selftesting code.

 - Matthew Wilcox changes xfs and afs so they no longer take sleeping
   locks in ->map_page(), a step towards RCUification of pagefaults.

 - Suren Baghdasaryan has improved mmap_lock scalability by switching to
   per-VMA locking.

 - Frederic Weisbecker has reworked the percpu cache draining so that it
   no longer causes latency glitches on cpu isolated workloads.

 - Mike Rapoport cleans up and corrects the ARCH_FORCE_MAX_ORDER Kconfig
   logic.

 - Liu Shixin has changed zswap's initialization so we no longer waste a
   chunk of memory if zswap is not being used.

 - Yosry Ahmed has improved the performance of memcg statistics
   flushing.

 - David Stevens has fixed several issues involving khugepaged,
   userfaultfd and shmem.

 - Christoph Hellwig has provided some cleanup work to zram's IO-related
   code paths.

 - David Hildenbrand has fixed up some issues in the selftest code's
   testing of our pte state changing.

 - Pankaj Raghav has made page_endio() unneeded and has removed it.

 - Peter Xu contributed some rationalizations of the userfaultfd
   selftests.

 - Yosry Ahmed has fixed an issue around memcg's page recalim
   accounting.

 - Chaitanya Prakash has fixed some arm-related issues in the
   selftests/mm code.

 - Longlong Xia has improved the way in which KSM handles hwpoisoned
   pages.

 - Peter Xu fixes a few issues with uffd-wp at fork() time.

 - Stefan Roesch has changed KSM so that it may now be used on a
   per-process and per-cgroup basis.

* tag 'mm-stable-2023-04-27-15-30' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (369 commits)
  mm,unmap: avoid flushing TLB in batch if PTE is inaccessible
  shmem: restrict noswap option to initial user namespace
  mm/khugepaged: fix conflicting mods to collapse_file()
  sparse: remove unnecessary 0 values from rc
  mm: move 'mmap_min_addr' logic from callers into vm_unmapped_area()
  hugetlb: pte_alloc_huge() to replace huge pte_alloc_map()
  maple_tree: fix allocation in mas_sparse_area()
  mm: do not increment pgfault stats when page fault handler retries
  zsmalloc: allow only one active pool compaction context
  selftests/mm: add new selftests for KSM
  mm: add new KSM process and sysfs knobs
  mm: add new api to enable ksm per process
  mm: shrinkers: fix debugfs file permissions
  mm: don't check VMA write permissions if the PTE/PMD indicates write permissions
  migrate_pages_batch: fix statistics for longterm pin retry
  userfaultfd: use helper function range_in_vma()
  lib/show_mem.c: use for_each_populated_zone() simplify code
  mm: correct arg in reclaim_pages()/reclaim_clean_pages_from_list()
  fs/buffer: convert create_page_buffers to folio_create_buffers
  fs/buffer: add folio_create_empty_buffers helper
  ...
2023-04-27 19:42:02 -07:00

613 lines
12 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* Test module for stress and analyze performance of vmalloc allocator.
* (C) 2018 Uladzislau Rezki (Sony) <urezki@gmail.com>
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/vmalloc.h>
#include <linux/random.h>
#include <linux/kthread.h>
#include <linux/moduleparam.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/rwsem.h>
#include <linux/mm.h>
#include <linux/rcupdate.h>
#include <linux/slab.h>
#define __param(type, name, init, msg) \
static type name = init; \
module_param(name, type, 0444); \
MODULE_PARM_DESC(name, msg) \
__param(int, nr_threads, 0,
"Number of workers to perform tests(min: 1 max: USHRT_MAX)");
__param(bool, sequential_test_order, false,
"Use sequential stress tests order");
__param(int, test_repeat_count, 1,
"Set test repeat counter");
__param(int, test_loop_count, 1000000,
"Set test loop counter");
__param(int, nr_pages, 0,
"Set number of pages for fix_size_alloc_test(default: 1)");
__param(bool, use_huge, false,
"Use vmalloc_huge in fix_size_alloc_test");
__param(int, run_test_mask, INT_MAX,
"Set tests specified in the mask.\n\n"
"\t\tid: 1, name: fix_size_alloc_test\n"
"\t\tid: 2, name: full_fit_alloc_test\n"
"\t\tid: 4, name: long_busy_list_alloc_test\n"
"\t\tid: 8, name: random_size_alloc_test\n"
"\t\tid: 16, name: fix_align_alloc_test\n"
"\t\tid: 32, name: random_size_align_alloc_test\n"
"\t\tid: 64, name: align_shift_alloc_test\n"
"\t\tid: 128, name: pcpu_alloc_test\n"
"\t\tid: 256, name: kvfree_rcu_1_arg_vmalloc_test\n"
"\t\tid: 512, name: kvfree_rcu_2_arg_vmalloc_test\n"
"\t\tid: 1024, name: vm_map_ram_test\n"
/* Add a new test case description here. */
);
/*
* Read write semaphore for synchronization of setup
* phase that is done in main thread and workers.
*/
static DECLARE_RWSEM(prepare_for_test_rwsem);
/*
* Completion tracking for worker threads.
*/
static DECLARE_COMPLETION(test_all_done_comp);
static atomic_t test_n_undone = ATOMIC_INIT(0);
static inline void
test_report_one_done(void)
{
if (atomic_dec_and_test(&test_n_undone))
complete(&test_all_done_comp);
}
static int random_size_align_alloc_test(void)
{
unsigned long size, align;
unsigned int rnd;
void *ptr;
int i;
for (i = 0; i < test_loop_count; i++) {
rnd = get_random_u8();
/*
* Maximum 1024 pages, if PAGE_SIZE is 4096.
*/
align = 1 << (rnd % 23);
/*
* Maximum 10 pages.
*/
size = ((rnd % 10) + 1) * PAGE_SIZE;
ptr = __vmalloc_node(size, align, GFP_KERNEL | __GFP_ZERO, 0,
__builtin_return_address(0));
if (!ptr)
return -1;
vfree(ptr);
}
return 0;
}
/*
* This test case is supposed to be failed.
*/
static int align_shift_alloc_test(void)
{
unsigned long align;
void *ptr;
int i;
for (i = 0; i < BITS_PER_LONG; i++) {
align = ((unsigned long) 1) << i;
ptr = __vmalloc_node(PAGE_SIZE, align, GFP_KERNEL|__GFP_ZERO, 0,
__builtin_return_address(0));
if (!ptr)
return -1;
vfree(ptr);
}
return 0;
}
static int fix_align_alloc_test(void)
{
void *ptr;
int i;
for (i = 0; i < test_loop_count; i++) {
ptr = __vmalloc_node(5 * PAGE_SIZE, THREAD_ALIGN << 1,
GFP_KERNEL | __GFP_ZERO, 0,
__builtin_return_address(0));
if (!ptr)
return -1;
vfree(ptr);
}
return 0;
}
static int random_size_alloc_test(void)
{
unsigned int n;
void *p;
int i;
for (i = 0; i < test_loop_count; i++) {
n = get_random_u32_inclusive(1, 100);
p = vmalloc(n * PAGE_SIZE);
if (!p)
return -1;
*((__u8 *)p) = 1;
vfree(p);
}
return 0;
}
static int long_busy_list_alloc_test(void)
{
void *ptr_1, *ptr_2;
void **ptr;
int rv = -1;
int i;
ptr = vmalloc(sizeof(void *) * 15000);
if (!ptr)
return rv;
for (i = 0; i < 15000; i++)
ptr[i] = vmalloc(1 * PAGE_SIZE);
for (i = 0; i < test_loop_count; i++) {
ptr_1 = vmalloc(100 * PAGE_SIZE);
if (!ptr_1)
goto leave;
ptr_2 = vmalloc(1 * PAGE_SIZE);
if (!ptr_2) {
vfree(ptr_1);
goto leave;
}
*((__u8 *)ptr_1) = 0;
*((__u8 *)ptr_2) = 1;
vfree(ptr_1);
vfree(ptr_2);
}
/* Success */
rv = 0;
leave:
for (i = 0; i < 15000; i++)
vfree(ptr[i]);
vfree(ptr);
return rv;
}
static int full_fit_alloc_test(void)
{
void **ptr, **junk_ptr, *tmp;
int junk_length;
int rv = -1;
int i;
junk_length = fls(num_online_cpus());
junk_length *= (32 * 1024 * 1024 / PAGE_SIZE);
ptr = vmalloc(sizeof(void *) * junk_length);
if (!ptr)
return rv;
junk_ptr = vmalloc(sizeof(void *) * junk_length);
if (!junk_ptr) {
vfree(ptr);
return rv;
}
for (i = 0; i < junk_length; i++) {
ptr[i] = vmalloc(1 * PAGE_SIZE);
junk_ptr[i] = vmalloc(1 * PAGE_SIZE);
}
for (i = 0; i < junk_length; i++)
vfree(junk_ptr[i]);
for (i = 0; i < test_loop_count; i++) {
tmp = vmalloc(1 * PAGE_SIZE);
if (!tmp)
goto error;
*((__u8 *)tmp) = 1;
vfree(tmp);
}
/* Success */
rv = 0;
error:
for (i = 0; i < junk_length; i++)
vfree(ptr[i]);
vfree(ptr);
vfree(junk_ptr);
return rv;
}
static int fix_size_alloc_test(void)
{
void *ptr;
int i;
for (i = 0; i < test_loop_count; i++) {
if (use_huge)
ptr = vmalloc_huge((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE, GFP_KERNEL);
else
ptr = vmalloc((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE);
if (!ptr)
return -1;
*((__u8 *)ptr) = 0;
vfree(ptr);
}
return 0;
}
static int
pcpu_alloc_test(void)
{
int rv = 0;
#ifndef CONFIG_NEED_PER_CPU_KM
void __percpu **pcpu;
size_t size, align;
int i;
pcpu = vmalloc(sizeof(void __percpu *) * 35000);
if (!pcpu)
return -1;
for (i = 0; i < 35000; i++) {
size = get_random_u32_inclusive(1, PAGE_SIZE / 4);
/*
* Maximum PAGE_SIZE
*/
align = 1 << get_random_u32_inclusive(1, 11);
pcpu[i] = __alloc_percpu(size, align);
if (!pcpu[i])
rv = -1;
}
for (i = 0; i < 35000; i++)
free_percpu(pcpu[i]);
vfree(pcpu);
#endif
return rv;
}
struct test_kvfree_rcu {
struct rcu_head rcu;
unsigned char array[20];
};
static int
kvfree_rcu_1_arg_vmalloc_test(void)
{
struct test_kvfree_rcu *p;
int i;
for (i = 0; i < test_loop_count; i++) {
p = vmalloc(1 * PAGE_SIZE);
if (!p)
return -1;
p->array[0] = 'a';
kvfree_rcu_mightsleep(p);
}
return 0;
}
static int
kvfree_rcu_2_arg_vmalloc_test(void)
{
struct test_kvfree_rcu *p;
int i;
for (i = 0; i < test_loop_count; i++) {
p = vmalloc(1 * PAGE_SIZE);
if (!p)
return -1;
p->array[0] = 'a';
kvfree_rcu(p, rcu);
}
return 0;
}
static int
vm_map_ram_test(void)
{
unsigned long nr_allocated;
unsigned int map_nr_pages;
unsigned char *v_ptr;
struct page **pages;
int i;
map_nr_pages = nr_pages > 0 ? nr_pages:1;
pages = kmalloc(map_nr_pages * sizeof(struct page), GFP_KERNEL);
if (!pages)
return -1;
nr_allocated = alloc_pages_bulk_array(GFP_KERNEL, map_nr_pages, pages);
if (nr_allocated != map_nr_pages)
goto cleanup;
/* Run the test loop. */
for (i = 0; i < test_loop_count; i++) {
v_ptr = vm_map_ram(pages, map_nr_pages, NUMA_NO_NODE);
*v_ptr = 'a';
vm_unmap_ram(v_ptr, map_nr_pages);
}
cleanup:
for (i = 0; i < nr_allocated; i++)
__free_page(pages[i]);
kfree(pages);
/* 0 indicates success. */
return nr_allocated != map_nr_pages;
}
struct test_case_desc {
const char *test_name;
int (*test_func)(void);
};
static struct test_case_desc test_case_array[] = {
{ "fix_size_alloc_test", fix_size_alloc_test },
{ "full_fit_alloc_test", full_fit_alloc_test },
{ "long_busy_list_alloc_test", long_busy_list_alloc_test },
{ "random_size_alloc_test", random_size_alloc_test },
{ "fix_align_alloc_test", fix_align_alloc_test },
{ "random_size_align_alloc_test", random_size_align_alloc_test },
{ "align_shift_alloc_test", align_shift_alloc_test },
{ "pcpu_alloc_test", pcpu_alloc_test },
{ "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test },
{ "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test },
{ "vm_map_ram_test", vm_map_ram_test },
/* Add a new test case here. */
};
struct test_case_data {
int test_failed;
int test_passed;
u64 time;
};
static struct test_driver {
struct task_struct *task;
struct test_case_data data[ARRAY_SIZE(test_case_array)];
unsigned long start;
unsigned long stop;
} *tdriver;
static void shuffle_array(int *arr, int n)
{
int i, j;
for (i = n - 1; i > 0; i--) {
/* Cut the range. */
j = get_random_u32_below(i);
/* Swap indexes. */
swap(arr[i], arr[j]);
}
}
static int test_func(void *private)
{
struct test_driver *t = private;
int random_array[ARRAY_SIZE(test_case_array)];
int index, i, j;
ktime_t kt;
u64 delta;
for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
random_array[i] = i;
if (!sequential_test_order)
shuffle_array(random_array, ARRAY_SIZE(test_case_array));
/*
* Block until initialization is done.
*/
down_read(&prepare_for_test_rwsem);
t->start = get_cycles();
for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
index = random_array[i];
/*
* Skip tests if run_test_mask has been specified.
*/
if (!((run_test_mask & (1 << index)) >> index))
continue;
kt = ktime_get();
for (j = 0; j < test_repeat_count; j++) {
if (!test_case_array[index].test_func())
t->data[index].test_passed++;
else
t->data[index].test_failed++;
}
/*
* Take an average time that test took.
*/
delta = (u64) ktime_us_delta(ktime_get(), kt);
do_div(delta, (u32) test_repeat_count);
t->data[index].time = delta;
}
t->stop = get_cycles();
up_read(&prepare_for_test_rwsem);
test_report_one_done();
/*
* Wait for the kthread_stop() call.
*/
while (!kthread_should_stop())
msleep(10);
return 0;
}
static int
init_test_configurtion(void)
{
/*
* A maximum number of workers is defined as hard-coded
* value and set to USHRT_MAX. We add such gap just in
* case and for potential heavy stressing.
*/
nr_threads = clamp(nr_threads, 1, (int) USHRT_MAX);
/* Allocate the space for test instances. */
tdriver = kvcalloc(nr_threads, sizeof(*tdriver), GFP_KERNEL);
if (tdriver == NULL)
return -1;
if (test_repeat_count <= 0)
test_repeat_count = 1;
if (test_loop_count <= 0)
test_loop_count = 1;
return 0;
}
static void do_concurrent_test(void)
{
int i, ret;
/*
* Set some basic configurations plus sanity check.
*/
ret = init_test_configurtion();
if (ret < 0)
return;
/*
* Put on hold all workers.
*/
down_write(&prepare_for_test_rwsem);
for (i = 0; i < nr_threads; i++) {
struct test_driver *t = &tdriver[i];
t->task = kthread_run(test_func, t, "vmalloc_test/%d", i);
if (!IS_ERR(t->task))
/* Success. */
atomic_inc(&test_n_undone);
else
pr_err("Failed to start %d kthread\n", i);
}
/*
* Now let the workers do their job.
*/
up_write(&prepare_for_test_rwsem);
/*
* Sleep quiet until all workers are done with 1 second
* interval. Since the test can take a lot of time we
* can run into a stack trace of the hung task. That is
* why we go with completion_timeout and HZ value.
*/
do {
ret = wait_for_completion_timeout(&test_all_done_comp, HZ);
} while (!ret);
for (i = 0; i < nr_threads; i++) {
struct test_driver *t = &tdriver[i];
int j;
if (!IS_ERR(t->task))
kthread_stop(t->task);
for (j = 0; j < ARRAY_SIZE(test_case_array); j++) {
if (!((run_test_mask & (1 << j)) >> j))
continue;
pr_info(
"Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n",
test_case_array[j].test_name,
t->data[j].test_passed,
t->data[j].test_failed,
test_repeat_count, test_loop_count,
t->data[j].time);
}
pr_info("All test took worker%d=%lu cycles\n",
i, t->stop - t->start);
}
kvfree(tdriver);
}
static int vmalloc_test_init(void)
{
do_concurrent_test();
return -EAGAIN; /* Fail will directly unload the module */
}
static void vmalloc_test_exit(void)
{
}
module_init(vmalloc_test_init)
module_exit(vmalloc_test_exit)
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Uladzislau Rezki");
MODULE_DESCRIPTION("vmalloc test module");