~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/test_vmalloc.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/test_vmalloc.c (Architecture alpha) and /lib/test_vmalloc.c (Architecture i386)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2                                                     2 
  3 /*                                                  3 /*
  4  * Test module for stress and analyze performa      4  * Test module for stress and analyze performance of vmalloc allocator.
  5  * (C) 2018 Uladzislau Rezki (Sony) <urezki@gm      5  * (C) 2018 Uladzislau Rezki (Sony) <urezki@gmail.com>
  6  */                                                 6  */
  7 #include <linux/init.h>                             7 #include <linux/init.h>
  8 #include <linux/kernel.h>                           8 #include <linux/kernel.h>
  9 #include <linux/module.h>                           9 #include <linux/module.h>
 10 #include <linux/vmalloc.h>                         10 #include <linux/vmalloc.h>
 11 #include <linux/random.h>                          11 #include <linux/random.h>
 12 #include <linux/kthread.h>                         12 #include <linux/kthread.h>
 13 #include <linux/moduleparam.h>                     13 #include <linux/moduleparam.h>
 14 #include <linux/completion.h>                      14 #include <linux/completion.h>
 15 #include <linux/delay.h>                           15 #include <linux/delay.h>
 16 #include <linux/rwsem.h>                           16 #include <linux/rwsem.h>
 17 #include <linux/mm.h>                              17 #include <linux/mm.h>
 18 #include <linux/rcupdate.h>                        18 #include <linux/rcupdate.h>
 19 #include <linux/slab.h>                            19 #include <linux/slab.h>
 20                                                    20 
 21 #define __param(type, name, init, msg)             21 #define __param(type, name, init, msg)          \
 22         static type name = init;                   22         static type name = init;                                \
 23         module_param(name, type, 0444);            23         module_param(name, type, 0444);                 \
 24         MODULE_PARM_DESC(name, msg)                24         MODULE_PARM_DESC(name, msg)                             \
 25                                                    25 
 26 __param(int, nr_threads, 0,                        26 __param(int, nr_threads, 0,
 27         "Number of workers to perform tests(mi     27         "Number of workers to perform tests(min: 1 max: USHRT_MAX)");
 28                                                    28 
 29 __param(bool, sequential_test_order, false,        29 __param(bool, sequential_test_order, false,
 30         "Use sequential stress tests order");      30         "Use sequential stress tests order");
 31                                                    31 
 32 __param(int, test_repeat_count, 1,                 32 __param(int, test_repeat_count, 1,
 33         "Set test repeat counter");                33         "Set test repeat counter");
 34                                                    34 
 35 __param(int, test_loop_count, 1000000,             35 __param(int, test_loop_count, 1000000,
 36         "Set test loop counter");                  36         "Set test loop counter");
 37                                                    37 
 38 __param(int, nr_pages, 0,                          38 __param(int, nr_pages, 0,
 39         "Set number of pages for fix_size_allo     39         "Set number of pages for fix_size_alloc_test(default: 1)");
 40                                                    40 
 41 __param(bool, use_huge, false,                     41 __param(bool, use_huge, false,
 42         "Use vmalloc_huge in fix_size_alloc_te     42         "Use vmalloc_huge in fix_size_alloc_test");
 43                                                    43 
 44 __param(int, run_test_mask, INT_MAX,               44 __param(int, run_test_mask, INT_MAX,
 45         "Set tests specified in the mask.\n\n"     45         "Set tests specified in the mask.\n\n"
 46                 "\t\tid: 1,    name: fix_size_     46                 "\t\tid: 1,    name: fix_size_alloc_test\n"
 47                 "\t\tid: 2,    name: full_fit_     47                 "\t\tid: 2,    name: full_fit_alloc_test\n"
 48                 "\t\tid: 4,    name: long_busy     48                 "\t\tid: 4,    name: long_busy_list_alloc_test\n"
 49                 "\t\tid: 8,    name: random_si     49                 "\t\tid: 8,    name: random_size_alloc_test\n"
 50                 "\t\tid: 16,   name: fix_align     50                 "\t\tid: 16,   name: fix_align_alloc_test\n"
 51                 "\t\tid: 32,   name: random_si     51                 "\t\tid: 32,   name: random_size_align_alloc_test\n"
 52                 "\t\tid: 64,   name: align_shi     52                 "\t\tid: 64,   name: align_shift_alloc_test\n"
 53                 "\t\tid: 128,  name: pcpu_allo     53                 "\t\tid: 128,  name: pcpu_alloc_test\n"
 54                 "\t\tid: 256,  name: kvfree_rc     54                 "\t\tid: 256,  name: kvfree_rcu_1_arg_vmalloc_test\n"
 55                 "\t\tid: 512,  name: kvfree_rc     55                 "\t\tid: 512,  name: kvfree_rcu_2_arg_vmalloc_test\n"
 56                 "\t\tid: 1024, name: vm_map_ra     56                 "\t\tid: 1024, name: vm_map_ram_test\n"
 57                 /* Add a new test case descrip     57                 /* Add a new test case description here. */
 58 );                                                 58 );
 59                                                    59 
 60 /*                                                 60 /*
 61  * Read write semaphore for synchronization of     61  * Read write semaphore for synchronization of setup
 62  * phase that is done in main thread and worke     62  * phase that is done in main thread and workers.
 63  */                                                63  */
 64 static DECLARE_RWSEM(prepare_for_test_rwsem);      64 static DECLARE_RWSEM(prepare_for_test_rwsem);
 65                                                    65 
 66 /*                                                 66 /*
 67  * Completion tracking for worker threads.         67  * Completion tracking for worker threads.
 68  */                                                68  */
 69 static DECLARE_COMPLETION(test_all_done_comp);     69 static DECLARE_COMPLETION(test_all_done_comp);
 70 static atomic_t test_n_undone = ATOMIC_INIT(0)     70 static atomic_t test_n_undone = ATOMIC_INIT(0);
 71                                                    71 
 72 static inline void                                 72 static inline void
 73 test_report_one_done(void)                         73 test_report_one_done(void)
 74 {                                                  74 {
 75         if (atomic_dec_and_test(&test_n_undone     75         if (atomic_dec_and_test(&test_n_undone))
 76                 complete(&test_all_done_comp);     76                 complete(&test_all_done_comp);
 77 }                                                  77 }
 78                                                    78 
 79 static int random_size_align_alloc_test(void)      79 static int random_size_align_alloc_test(void)
 80 {                                                  80 {
 81         unsigned long size, align;                 81         unsigned long size, align;
 82         unsigned int rnd;                          82         unsigned int rnd;
 83         void *ptr;                                 83         void *ptr;
 84         int i;                                     84         int i;
 85                                                    85 
 86         for (i = 0; i < test_loop_count; i++)      86         for (i = 0; i < test_loop_count; i++) {
 87                 rnd = get_random_u8();             87                 rnd = get_random_u8();
 88                                                    88 
 89                 /*                                 89                 /*
 90                  * Maximum 1024 pages, if PAGE     90                  * Maximum 1024 pages, if PAGE_SIZE is 4096.
 91                  */                                91                  */
 92                 align = 1 << (rnd % 23);           92                 align = 1 << (rnd % 23);
 93                                                    93 
 94                 /*                                 94                 /*
 95                  * Maximum 10 pages.               95                  * Maximum 10 pages.
 96                  */                                96                  */
 97                 size = ((rnd % 10) + 1) * PAGE     97                 size = ((rnd % 10) + 1) * PAGE_SIZE;
 98                                                    98 
 99                 ptr = __vmalloc_node(size, ali     99                 ptr = __vmalloc_node(size, align, GFP_KERNEL | __GFP_ZERO, 0,
100                                 __builtin_retu    100                                 __builtin_return_address(0));
101                 if (!ptr)                         101                 if (!ptr)
102                         return -1;                102                         return -1;
103                                                   103 
104                 vfree(ptr);                       104                 vfree(ptr);
105         }                                         105         }
106                                                   106 
107         return 0;                                 107         return 0;
108 }                                                 108 }
109                                                   109 
110 /*                                                110 /*
111  * This test case is supposed to be failed.       111  * This test case is supposed to be failed.
112  */                                               112  */
113 static int align_shift_alloc_test(void)           113 static int align_shift_alloc_test(void)
114 {                                                 114 {
115         unsigned long align;                      115         unsigned long align;
116         void *ptr;                                116         void *ptr;
117         int i;                                    117         int i;
118                                                   118 
119         for (i = 0; i < BITS_PER_LONG; i++) {     119         for (i = 0; i < BITS_PER_LONG; i++) {
120                 align = 1UL << i;                 120                 align = 1UL << i;
121                                                   121 
122                 ptr = __vmalloc_node(PAGE_SIZE    122                 ptr = __vmalloc_node(PAGE_SIZE, align, GFP_KERNEL|__GFP_ZERO, 0,
123                                 __builtin_retu    123                                 __builtin_return_address(0));
124                 if (!ptr)                         124                 if (!ptr)
125                         return -1;                125                         return -1;
126                                                   126 
127                 vfree(ptr);                       127                 vfree(ptr);
128         }                                         128         }
129                                                   129 
130         return 0;                                 130         return 0;
131 }                                                 131 }
132                                                   132 
133 static int fix_align_alloc_test(void)             133 static int fix_align_alloc_test(void)
134 {                                                 134 {
135         void *ptr;                                135         void *ptr;
136         int i;                                    136         int i;
137                                                   137 
138         for (i = 0; i < test_loop_count; i++)     138         for (i = 0; i < test_loop_count; i++) {
139                 ptr = __vmalloc_node(5 * PAGE_    139                 ptr = __vmalloc_node(5 * PAGE_SIZE, THREAD_ALIGN << 1,
140                                 GFP_KERNEL | _    140                                 GFP_KERNEL | __GFP_ZERO, 0,
141                                 __builtin_retu    141                                 __builtin_return_address(0));
142                 if (!ptr)                         142                 if (!ptr)
143                         return -1;                143                         return -1;
144                                                   144 
145                 vfree(ptr);                       145                 vfree(ptr);
146         }                                         146         }
147                                                   147 
148         return 0;                                 148         return 0;
149 }                                                 149 }
150                                                   150 
151 static int random_size_alloc_test(void)           151 static int random_size_alloc_test(void)
152 {                                                 152 {
153         unsigned int n;                           153         unsigned int n;
154         void *p;                                  154         void *p;
155         int i;                                    155         int i;
156                                                   156 
157         for (i = 0; i < test_loop_count; i++)     157         for (i = 0; i < test_loop_count; i++) {
158                 n = get_random_u32_inclusive(1    158                 n = get_random_u32_inclusive(1, 100);
159                 p = vmalloc(n * PAGE_SIZE);       159                 p = vmalloc(n * PAGE_SIZE);
160                                                   160 
161                 if (!p)                           161                 if (!p)
162                         return -1;                162                         return -1;
163                                                   163 
164                 *((__u8 *)p) = 1;                 164                 *((__u8 *)p) = 1;
165                 vfree(p);                         165                 vfree(p);
166         }                                         166         }
167                                                   167 
168         return 0;                                 168         return 0;
169 }                                                 169 }
170                                                   170 
171 static int long_busy_list_alloc_test(void)        171 static int long_busy_list_alloc_test(void)
172 {                                                 172 {
173         void *ptr_1, *ptr_2;                      173         void *ptr_1, *ptr_2;
174         void **ptr;                               174         void **ptr;
175         int rv = -1;                              175         int rv = -1;
176         int i;                                    176         int i;
177                                                   177 
178         ptr = vmalloc(sizeof(void *) * 15000);    178         ptr = vmalloc(sizeof(void *) * 15000);
179         if (!ptr)                                 179         if (!ptr)
180                 return rv;                        180                 return rv;
181                                                   181 
182         for (i = 0; i < 15000; i++)               182         for (i = 0; i < 15000; i++)
183                 ptr[i] = vmalloc(1 * PAGE_SIZE    183                 ptr[i] = vmalloc(1 * PAGE_SIZE);
184                                                   184 
185         for (i = 0; i < test_loop_count; i++)     185         for (i = 0; i < test_loop_count; i++) {
186                 ptr_1 = vmalloc(100 * PAGE_SIZ    186                 ptr_1 = vmalloc(100 * PAGE_SIZE);
187                 if (!ptr_1)                       187                 if (!ptr_1)
188                         goto leave;               188                         goto leave;
189                                                   189 
190                 ptr_2 = vmalloc(1 * PAGE_SIZE)    190                 ptr_2 = vmalloc(1 * PAGE_SIZE);
191                 if (!ptr_2) {                     191                 if (!ptr_2) {
192                         vfree(ptr_1);             192                         vfree(ptr_1);
193                         goto leave;               193                         goto leave;
194                 }                                 194                 }
195                                                   195 
196                 *((__u8 *)ptr_1) = 0;             196                 *((__u8 *)ptr_1) = 0;
197                 *((__u8 *)ptr_2) = 1;             197                 *((__u8 *)ptr_2) = 1;
198                                                   198 
199                 vfree(ptr_1);                     199                 vfree(ptr_1);
200                 vfree(ptr_2);                     200                 vfree(ptr_2);
201         }                                         201         }
202                                                   202 
203         /*  Success */                            203         /*  Success */
204         rv = 0;                                   204         rv = 0;
205                                                   205 
206 leave:                                            206 leave:
207         for (i = 0; i < 15000; i++)               207         for (i = 0; i < 15000; i++)
208                 vfree(ptr[i]);                    208                 vfree(ptr[i]);
209                                                   209 
210         vfree(ptr);                               210         vfree(ptr);
211         return rv;                                211         return rv;
212 }                                                 212 }
213                                                   213 
214 static int full_fit_alloc_test(void)              214 static int full_fit_alloc_test(void)
215 {                                                 215 {
216         void **ptr, **junk_ptr, *tmp;             216         void **ptr, **junk_ptr, *tmp;
217         int junk_length;                          217         int junk_length;
218         int rv = -1;                              218         int rv = -1;
219         int i;                                    219         int i;
220                                                   220 
221         junk_length = fls(num_online_cpus());     221         junk_length = fls(num_online_cpus());
222         junk_length *= (32 * 1024 * 1024 / PAG    222         junk_length *= (32 * 1024 * 1024 / PAGE_SIZE);
223                                                   223 
224         ptr = vmalloc(sizeof(void *) * junk_le    224         ptr = vmalloc(sizeof(void *) * junk_length);
225         if (!ptr)                                 225         if (!ptr)
226                 return rv;                        226                 return rv;
227                                                   227 
228         junk_ptr = vmalloc(sizeof(void *) * ju    228         junk_ptr = vmalloc(sizeof(void *) * junk_length);
229         if (!junk_ptr) {                          229         if (!junk_ptr) {
230                 vfree(ptr);                       230                 vfree(ptr);
231                 return rv;                        231                 return rv;
232         }                                         232         }
233                                                   233 
234         for (i = 0; i < junk_length; i++) {       234         for (i = 0; i < junk_length; i++) {
235                 ptr[i] = vmalloc(1 * PAGE_SIZE    235                 ptr[i] = vmalloc(1 * PAGE_SIZE);
236                 junk_ptr[i] = vmalloc(1 * PAGE    236                 junk_ptr[i] = vmalloc(1 * PAGE_SIZE);
237         }                                         237         }
238                                                   238 
239         for (i = 0; i < junk_length; i++)         239         for (i = 0; i < junk_length; i++)
240                 vfree(junk_ptr[i]);               240                 vfree(junk_ptr[i]);
241                                                   241 
242         for (i = 0; i < test_loop_count; i++)     242         for (i = 0; i < test_loop_count; i++) {
243                 tmp = vmalloc(1 * PAGE_SIZE);     243                 tmp = vmalloc(1 * PAGE_SIZE);
244                                                   244 
245                 if (!tmp)                         245                 if (!tmp)
246                         goto error;               246                         goto error;
247                                                   247 
248                 *((__u8 *)tmp) = 1;               248                 *((__u8 *)tmp) = 1;
249                 vfree(tmp);                       249                 vfree(tmp);
250         }                                         250         }
251                                                   251 
252         /* Success */                             252         /* Success */
253         rv = 0;                                   253         rv = 0;
254                                                   254 
255 error:                                            255 error:
256         for (i = 0; i < junk_length; i++)         256         for (i = 0; i < junk_length; i++)
257                 vfree(ptr[i]);                    257                 vfree(ptr[i]);
258                                                   258 
259         vfree(ptr);                               259         vfree(ptr);
260         vfree(junk_ptr);                          260         vfree(junk_ptr);
261                                                   261 
262         return rv;                                262         return rv;
263 }                                                 263 }
264                                                   264 
265 static int fix_size_alloc_test(void)              265 static int fix_size_alloc_test(void)
266 {                                                 266 {
267         void *ptr;                                267         void *ptr;
268         int i;                                    268         int i;
269                                                   269 
270         for (i = 0; i < test_loop_count; i++)     270         for (i = 0; i < test_loop_count; i++) {
271                 if (use_huge)                     271                 if (use_huge)
272                         ptr = vmalloc_huge((nr    272                         ptr = vmalloc_huge((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE, GFP_KERNEL);
273                 else                              273                 else
274                         ptr = vmalloc((nr_page    274                         ptr = vmalloc((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE);
275                                                   275 
276                 if (!ptr)                         276                 if (!ptr)
277                         return -1;                277                         return -1;
278                                                   278 
279                 *((__u8 *)ptr) = 0;               279                 *((__u8 *)ptr) = 0;
280                                                   280 
281                 vfree(ptr);                       281                 vfree(ptr);
282         }                                         282         }
283                                                   283 
284         return 0;                                 284         return 0;
285 }                                                 285 }
286                                                   286 
287 static int                                        287 static int
288 pcpu_alloc_test(void)                             288 pcpu_alloc_test(void)
289 {                                                 289 {
290         int rv = 0;                               290         int rv = 0;
291 #ifndef CONFIG_NEED_PER_CPU_KM                    291 #ifndef CONFIG_NEED_PER_CPU_KM
292         void __percpu **pcpu;                     292         void __percpu **pcpu;
293         size_t size, align;                       293         size_t size, align;
294         int i;                                    294         int i;
295                                                   295 
296         pcpu = vmalloc(sizeof(void __percpu *)    296         pcpu = vmalloc(sizeof(void __percpu *) * 35000);
297         if (!pcpu)                                297         if (!pcpu)
298                 return -1;                        298                 return -1;
299                                                   299 
300         for (i = 0; i < 35000; i++) {             300         for (i = 0; i < 35000; i++) {
301                 size = get_random_u32_inclusiv    301                 size = get_random_u32_inclusive(1, PAGE_SIZE / 4);
302                                                   302 
303                 /*                                303                 /*
304                  * Maximum PAGE_SIZE              304                  * Maximum PAGE_SIZE
305                  */                               305                  */
306                 align = 1 << get_random_u32_in    306                 align = 1 << get_random_u32_inclusive(1, 11);
307                                                   307 
308                 pcpu[i] = __alloc_percpu(size,    308                 pcpu[i] = __alloc_percpu(size, align);
309                 if (!pcpu[i])                     309                 if (!pcpu[i])
310                         rv = -1;                  310                         rv = -1;
311         }                                         311         }
312                                                   312 
313         for (i = 0; i < 35000; i++)               313         for (i = 0; i < 35000; i++)
314                 free_percpu(pcpu[i]);             314                 free_percpu(pcpu[i]);
315                                                   315 
316         vfree(pcpu);                              316         vfree(pcpu);
317 #endif                                            317 #endif
318         return rv;                                318         return rv;
319 }                                                 319 }
320                                                   320 
321 struct test_kvfree_rcu {                          321 struct test_kvfree_rcu {
322         struct rcu_head rcu;                      322         struct rcu_head rcu;
323         unsigned char array[20];                  323         unsigned char array[20];
324 };                                                324 };
325                                                   325 
326 static int                                        326 static int
327 kvfree_rcu_1_arg_vmalloc_test(void)               327 kvfree_rcu_1_arg_vmalloc_test(void)
328 {                                                 328 {
329         struct test_kvfree_rcu *p;                329         struct test_kvfree_rcu *p;
330         int i;                                    330         int i;
331                                                   331 
332         for (i = 0; i < test_loop_count; i++)     332         for (i = 0; i < test_loop_count; i++) {
333                 p = vmalloc(1 * PAGE_SIZE);       333                 p = vmalloc(1 * PAGE_SIZE);
334                 if (!p)                           334                 if (!p)
335                         return -1;                335                         return -1;
336                                                   336 
337                 p->array[0] = 'a';                337                 p->array[0] = 'a';
338                 kvfree_rcu_mightsleep(p);         338                 kvfree_rcu_mightsleep(p);
339         }                                         339         }
340                                                   340 
341         return 0;                                 341         return 0;
342 }                                                 342 }
343                                                   343 
344 static int                                        344 static int
345 kvfree_rcu_2_arg_vmalloc_test(void)               345 kvfree_rcu_2_arg_vmalloc_test(void)
346 {                                                 346 {
347         struct test_kvfree_rcu *p;                347         struct test_kvfree_rcu *p;
348         int i;                                    348         int i;
349                                                   349 
350         for (i = 0; i < test_loop_count; i++)     350         for (i = 0; i < test_loop_count; i++) {
351                 p = vmalloc(1 * PAGE_SIZE);       351                 p = vmalloc(1 * PAGE_SIZE);
352                 if (!p)                           352                 if (!p)
353                         return -1;                353                         return -1;
354                                                   354 
355                 p->array[0] = 'a';                355                 p->array[0] = 'a';
356                 kvfree_rcu(p, rcu);               356                 kvfree_rcu(p, rcu);
357         }                                         357         }
358                                                   358 
359         return 0;                                 359         return 0;
360 }                                                 360 }
361                                                   361 
362 static int                                        362 static int
363 vm_map_ram_test(void)                             363 vm_map_ram_test(void)
364 {                                                 364 {
365         unsigned long nr_allocated;               365         unsigned long nr_allocated;
366         unsigned int map_nr_pages;                366         unsigned int map_nr_pages;
367         unsigned char *v_ptr;                     367         unsigned char *v_ptr;
368         struct page **pages;                      368         struct page **pages;
369         int i;                                    369         int i;
370                                                   370 
371         map_nr_pages = nr_pages > 0 ? nr_pages    371         map_nr_pages = nr_pages > 0 ? nr_pages:1;
372         pages = kcalloc(map_nr_pages, sizeof(s    372         pages = kcalloc(map_nr_pages, sizeof(struct page *), GFP_KERNEL);
373         if (!pages)                               373         if (!pages)
374                 return -1;                        374                 return -1;
375                                                   375 
376         nr_allocated = alloc_pages_bulk_array(    376         nr_allocated = alloc_pages_bulk_array(GFP_KERNEL, map_nr_pages, pages);
377         if (nr_allocated != map_nr_pages)         377         if (nr_allocated != map_nr_pages)
378                 goto cleanup;                     378                 goto cleanup;
379                                                   379 
380         /* Run the test loop. */                  380         /* Run the test loop. */
381         for (i = 0; i < test_loop_count; i++)     381         for (i = 0; i < test_loop_count; i++) {
382                 v_ptr = vm_map_ram(pages, map_    382                 v_ptr = vm_map_ram(pages, map_nr_pages, NUMA_NO_NODE);
383                 *v_ptr = 'a';                     383                 *v_ptr = 'a';
384                 vm_unmap_ram(v_ptr, map_nr_pag    384                 vm_unmap_ram(v_ptr, map_nr_pages);
385         }                                         385         }
386                                                   386 
387 cleanup:                                          387 cleanup:
388         for (i = 0; i < nr_allocated; i++)        388         for (i = 0; i < nr_allocated; i++)
389                 __free_page(pages[i]);            389                 __free_page(pages[i]);
390                                                   390 
391         kfree(pages);                             391         kfree(pages);
392                                                   392 
393         /* 0 indicates success. */                393         /* 0 indicates success. */
394         return nr_allocated != map_nr_pages;      394         return nr_allocated != map_nr_pages;
395 }                                                 395 }
396                                                   396 
397 struct test_case_desc {                           397 struct test_case_desc {
398         const char *test_name;                    398         const char *test_name;
399         int (*test_func)(void);                   399         int (*test_func)(void);
400 };                                                400 };
401                                                   401 
402 static struct test_case_desc test_case_array[]    402 static struct test_case_desc test_case_array[] = {
403         { "fix_size_alloc_test", fix_size_allo    403         { "fix_size_alloc_test", fix_size_alloc_test },
404         { "full_fit_alloc_test", full_fit_allo    404         { "full_fit_alloc_test", full_fit_alloc_test },
405         { "long_busy_list_alloc_test", long_bu    405         { "long_busy_list_alloc_test", long_busy_list_alloc_test },
406         { "random_size_alloc_test", random_siz    406         { "random_size_alloc_test", random_size_alloc_test },
407         { "fix_align_alloc_test", fix_align_al    407         { "fix_align_alloc_test", fix_align_alloc_test },
408         { "random_size_align_alloc_test", rand    408         { "random_size_align_alloc_test", random_size_align_alloc_test },
409         { "align_shift_alloc_test", align_shif    409         { "align_shift_alloc_test", align_shift_alloc_test },
410         { "pcpu_alloc_test", pcpu_alloc_test }    410         { "pcpu_alloc_test", pcpu_alloc_test },
411         { "kvfree_rcu_1_arg_vmalloc_test", kvf    411         { "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test },
412         { "kvfree_rcu_2_arg_vmalloc_test", kvf    412         { "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test },
413         { "vm_map_ram_test", vm_map_ram_test }    413         { "vm_map_ram_test", vm_map_ram_test },
414         /* Add a new test case here. */           414         /* Add a new test case here. */
415 };                                                415 };
416                                                   416 
417 struct test_case_data {                           417 struct test_case_data {
418         int test_failed;                          418         int test_failed;
419         int test_passed;                          419         int test_passed;
420         u64 time;                                 420         u64 time;
421 };                                                421 };
422                                                   422 
423 static struct test_driver {                       423 static struct test_driver {
424         struct task_struct *task;                 424         struct task_struct *task;
425         struct test_case_data data[ARRAY_SIZE(    425         struct test_case_data data[ARRAY_SIZE(test_case_array)];
426                                                   426 
427         unsigned long start;                      427         unsigned long start;
428         unsigned long stop;                       428         unsigned long stop;
429 } *tdriver;                                       429 } *tdriver;
430                                                   430 
431 static void shuffle_array(int *arr, int n)        431 static void shuffle_array(int *arr, int n)
432 {                                                 432 {
433         int i, j;                                 433         int i, j;
434                                                   434 
435         for (i = n - 1; i > 0; i--)  {            435         for (i = n - 1; i > 0; i--)  {
436                 /* Cut the range. */              436                 /* Cut the range. */
437                 j = get_random_u32_below(i);      437                 j = get_random_u32_below(i);
438                                                   438 
439                 /* Swap indexes. */               439                 /* Swap indexes. */
440                 swap(arr[i], arr[j]);             440                 swap(arr[i], arr[j]);
441         }                                         441         }
442 }                                                 442 }
443                                                   443 
444 static int test_func(void *private)               444 static int test_func(void *private)
445 {                                                 445 {
446         struct test_driver *t = private;          446         struct test_driver *t = private;
447         int random_array[ARRAY_SIZE(test_case_    447         int random_array[ARRAY_SIZE(test_case_array)];
448         int index, i, j;                          448         int index, i, j;
449         ktime_t kt;                               449         ktime_t kt;
450         u64 delta;                                450         u64 delta;
451                                                   451 
452         for (i = 0; i < ARRAY_SIZE(test_case_a    452         for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
453                 random_array[i] = i;              453                 random_array[i] = i;
454                                                   454 
455         if (!sequential_test_order)               455         if (!sequential_test_order)
456                 shuffle_array(random_array, AR    456                 shuffle_array(random_array, ARRAY_SIZE(test_case_array));
457                                                   457 
458         /*                                        458         /*
459          * Block until initialization is done.    459          * Block until initialization is done.
460          */                                       460          */
461         down_read(&prepare_for_test_rwsem);       461         down_read(&prepare_for_test_rwsem);
462                                                   462 
463         t->start = get_cycles();                  463         t->start = get_cycles();
464         for (i = 0; i < ARRAY_SIZE(test_case_a    464         for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
465                 index = random_array[i];          465                 index = random_array[i];
466                                                   466 
467                 /*                                467                 /*
468                  * Skip tests if run_test_mask    468                  * Skip tests if run_test_mask has been specified.
469                  */                               469                  */
470                 if (!((run_test_mask & (1 << i    470                 if (!((run_test_mask & (1 << index)) >> index))
471                         continue;                 471                         continue;
472                                                   472 
473                 kt = ktime_get();                 473                 kt = ktime_get();
474                 for (j = 0; j < test_repeat_co    474                 for (j = 0; j < test_repeat_count; j++) {
475                         if (!test_case_array[i    475                         if (!test_case_array[index].test_func())
476                                 t->data[index]    476                                 t->data[index].test_passed++;
477                         else                      477                         else
478                                 t->data[index]    478                                 t->data[index].test_failed++;
479                 }                                 479                 }
480                                                   480 
481                 /*                                481                 /*
482                  * Take an average time that t    482                  * Take an average time that test took.
483                  */                               483                  */
484                 delta = (u64) ktime_us_delta(k    484                 delta = (u64) ktime_us_delta(ktime_get(), kt);
485                 do_div(delta, (u32) test_repea    485                 do_div(delta, (u32) test_repeat_count);
486                                                   486 
487                 t->data[index].time = delta;      487                 t->data[index].time = delta;
488         }                                         488         }
489         t->stop = get_cycles();                   489         t->stop = get_cycles();
490                                                   490 
491         up_read(&prepare_for_test_rwsem);         491         up_read(&prepare_for_test_rwsem);
492         test_report_one_done();                   492         test_report_one_done();
493                                                   493 
494         /*                                        494         /*
495          * Wait for the kthread_stop() call.      495          * Wait for the kthread_stop() call.
496          */                                       496          */
497         while (!kthread_should_stop())            497         while (!kthread_should_stop())
498                 msleep(10);                       498                 msleep(10);
499                                                   499 
500         return 0;                                 500         return 0;
501 }                                                 501 }
502                                                   502 
503 static int                                        503 static int
504 init_test_configuration(void)                     504 init_test_configuration(void)
505 {                                                 505 {
506         /*                                        506         /*
507          * A maximum number of workers is defi    507          * A maximum number of workers is defined as hard-coded
508          * value and set to USHRT_MAX. We add     508          * value and set to USHRT_MAX. We add such gap just in
509          * case and for potential heavy stress    509          * case and for potential heavy stressing.
510          */                                       510          */
511         nr_threads = clamp(nr_threads, 1, (int    511         nr_threads = clamp(nr_threads, 1, (int) USHRT_MAX);
512                                                   512 
513         /* Allocate the space for test instanc    513         /* Allocate the space for test instances. */
514         tdriver = kvcalloc(nr_threads, sizeof(    514         tdriver = kvcalloc(nr_threads, sizeof(*tdriver), GFP_KERNEL);
515         if (tdriver == NULL)                      515         if (tdriver == NULL)
516                 return -1;                        516                 return -1;
517                                                   517 
518         if (test_repeat_count <= 0)               518         if (test_repeat_count <= 0)
519                 test_repeat_count = 1;            519                 test_repeat_count = 1;
520                                                   520 
521         if (test_loop_count <= 0)                 521         if (test_loop_count <= 0)
522                 test_loop_count = 1;              522                 test_loop_count = 1;
523                                                   523 
524         return 0;                                 524         return 0;
525 }                                                 525 }
526                                                   526 
527 static void do_concurrent_test(void)              527 static void do_concurrent_test(void)
528 {                                                 528 {
529         int i, ret;                               529         int i, ret;
530                                                   530 
531         /*                                        531         /*
532          * Set some basic configurations plus     532          * Set some basic configurations plus sanity check.
533          */                                       533          */
534         ret = init_test_configuration();          534         ret = init_test_configuration();
535         if (ret < 0)                              535         if (ret < 0)
536                 return;                           536                 return;
537                                                   537 
538         /*                                        538         /*
539          * Put on hold all workers.               539          * Put on hold all workers.
540          */                                       540          */
541         down_write(&prepare_for_test_rwsem);      541         down_write(&prepare_for_test_rwsem);
542                                                   542 
543         for (i = 0; i < nr_threads; i++) {        543         for (i = 0; i < nr_threads; i++) {
544                 struct test_driver *t = &tdriv    544                 struct test_driver *t = &tdriver[i];
545                                                   545 
546                 t->task = kthread_run(test_fun    546                 t->task = kthread_run(test_func, t, "vmalloc_test/%d", i);
547                                                   547 
548                 if (!IS_ERR(t->task))             548                 if (!IS_ERR(t->task))
549                         /* Success. */            549                         /* Success. */
550                         atomic_inc(&test_n_und    550                         atomic_inc(&test_n_undone);
551                 else                              551                 else
552                         pr_err("Failed to star    552                         pr_err("Failed to start %d kthread\n", i);
553         }                                         553         }
554                                                   554 
555         /*                                        555         /*
556          * Now let the workers do their job.      556          * Now let the workers do their job.
557          */                                       557          */
558         up_write(&prepare_for_test_rwsem);        558         up_write(&prepare_for_test_rwsem);
559                                                   559 
560         /*                                        560         /*
561          * Sleep quiet until all workers are d    561          * Sleep quiet until all workers are done with 1 second
562          * interval. Since the test can take a    562          * interval. Since the test can take a lot of time we
563          * can run into a stack trace of the h    563          * can run into a stack trace of the hung task. That is
564          * why we go with completion_timeout a    564          * why we go with completion_timeout and HZ value.
565          */                                       565          */
566         do {                                      566         do {
567                 ret = wait_for_completion_time    567                 ret = wait_for_completion_timeout(&test_all_done_comp, HZ);
568         } while (!ret);                           568         } while (!ret);
569                                                   569 
570         for (i = 0; i < nr_threads; i++) {        570         for (i = 0; i < nr_threads; i++) {
571                 struct test_driver *t = &tdriv    571                 struct test_driver *t = &tdriver[i];
572                 int j;                            572                 int j;
573                                                   573 
574                 if (!IS_ERR(t->task))             574                 if (!IS_ERR(t->task))
575                         kthread_stop(t->task);    575                         kthread_stop(t->task);
576                                                   576 
577                 for (j = 0; j < ARRAY_SIZE(tes    577                 for (j = 0; j < ARRAY_SIZE(test_case_array); j++) {
578                         if (!((run_test_mask &    578                         if (!((run_test_mask & (1 << j)) >> j))
579                                 continue;         579                                 continue;
580                                                   580 
581                         pr_info(                  581                         pr_info(
582                                 "Summary: %s p    582                                 "Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n",
583                                 test_case_arra    583                                 test_case_array[j].test_name,
584                                 t->data[j].tes    584                                 t->data[j].test_passed,
585                                 t->data[j].tes    585                                 t->data[j].test_failed,
586                                 test_repeat_co    586                                 test_repeat_count, test_loop_count,
587                                 t->data[j].tim    587                                 t->data[j].time);
588                 }                                 588                 }
589                                                   589 
590                 pr_info("All test took worker%    590                 pr_info("All test took worker%d=%lu cycles\n",
591                         i, t->stop - t->start)    591                         i, t->stop - t->start);
592         }                                         592         }
593                                                   593 
594         kvfree(tdriver);                          594         kvfree(tdriver);
595 }                                                 595 }
596                                                   596 
597 static int vmalloc_test_init(void)                597 static int vmalloc_test_init(void)
598 {                                                 598 {
599         do_concurrent_test();                     599         do_concurrent_test();
600         return -EAGAIN; /* Fail will directly     600         return -EAGAIN; /* Fail will directly unload the module */
601 }                                                 601 }
602                                                   602 
603 module_init(vmalloc_test_init)                    603 module_init(vmalloc_test_init)
604                                                   604 
605 MODULE_LICENSE("GPL");                            605 MODULE_LICENSE("GPL");
606 MODULE_AUTHOR("Uladzislau Rezki");                606 MODULE_AUTHOR("Uladzislau Rezki");
607 MODULE_DESCRIPTION("vmalloc test module");        607 MODULE_DESCRIPTION("vmalloc test module");
608                                                   608 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php