Linux 6.9-rc1
[linux-2.6-microblaze.git] / lib / test_vmalloc.c
index 4f2f2d1..4ddf769 100644 (file)
@@ -38,6 +38,9 @@ __param(int, test_loop_count, 1000000,
 __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"
@@ -50,6 +53,7 @@ __param(int, run_test_mask, INT_MAX,
                "\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. */
 );
 
@@ -80,7 +84,7 @@ static int random_size_align_alloc_test(void)
        int i;
 
        for (i = 0; i < test_loop_count; i++) {
-               rnd = prandom_u32();
+               rnd = get_random_u8();
 
                /*
                 * Maximum 1024 pages, if PAGE_SIZE is 4096.
@@ -113,7 +117,7 @@ static int align_shift_alloc_test(void)
        int i;
 
        for (i = 0; i < BITS_PER_LONG; i++) {
-               align = ((unsigned long) 1) << i;
+               align = 1UL << i;
 
                ptr = __vmalloc_node(PAGE_SIZE, align, GFP_KERNEL|__GFP_ZERO, 0,
                                __builtin_return_address(0));
@@ -151,9 +155,7 @@ static int random_size_alloc_test(void)
        int i;
 
        for (i = 0; i < test_loop_count; i++) {
-               n = prandom_u32();
-               n = (n % 100) + 1;
-
+               n = get_random_u32_inclusive(1, 100);
                p = vmalloc(n * PAGE_SIZE);
 
                if (!p)
@@ -266,7 +268,10 @@ static int fix_size_alloc_test(void)
        int i;
 
        for (i = 0; i < test_loop_count; i++) {
-               ptr = vmalloc((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE);
+               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;
@@ -293,16 +298,12 @@ pcpu_alloc_test(void)
                return -1;
 
        for (i = 0; i < 35000; i++) {
-               unsigned int r;
-
-               r = prandom_u32();
-               size = (r % (PAGE_SIZE / 4)) + 1;
+               size = get_random_u32_inclusive(1, PAGE_SIZE / 4);
 
                /*
                 * Maximum PAGE_SIZE
                 */
-               r = prandom_u32();
-               align = 1 << ((r % 11) + 1);
+               align = 1 << get_random_u32_inclusive(1, 11);
 
                pcpu[i] = __alloc_percpu(size, align);
                if (!pcpu[i])
@@ -334,7 +335,7 @@ kvfree_rcu_1_arg_vmalloc_test(void)
                        return -1;
 
                p->array[0] = 'a';
-               kvfree_rcu(p);
+               kvfree_rcu_mightsleep(p);
        }
 
        return 0;
@@ -358,6 +359,41 @@ kvfree_rcu_2_arg_vmalloc_test(void)
        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 = kcalloc(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);
@@ -374,6 +410,7 @@ static struct test_case_desc test_case_array[] = {
        { "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. */
 };
 
@@ -393,14 +430,11 @@ static struct test_driver {
 
 static void shuffle_array(int *arr, int n)
 {
-       unsigned int rnd;
        int i, j;
 
        for (i = n - 1; i > 0; i--)  {
-               rnd = prandom_u32();
-
                /* Cut the range. */
-               j = rnd % i;
+               j = get_random_u32_below(i);
 
                /* Swap indexes. */
                swap(arr[i], arr[j]);
@@ -467,7 +501,7 @@ static int test_func(void *private)
 }
 
 static int
-init_test_configurtion(void)
+init_test_configuration(void)
 {
        /*
         * A maximum number of workers is defined as hard-coded
@@ -497,7 +531,7 @@ static void do_concurrent_test(void)
        /*
         * Set some basic configurations plus sanity check.
         */
-       ret = init_test_configurtion();
+       ret = init_test_configuration();
        if (ret < 0)
                return;
 
@@ -566,12 +600,7 @@ static int vmalloc_test_init(void)
        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");