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

TOMOYO Linux Cross Reference
Linux/lib/test_meminit.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ 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.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/test_meminit.c (Version linux-6.12-rc7) and /lib/test_meminit.c (Version linux-5.13.19)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * Test cases for SL[AOU]B/page initialization      3  * Test cases for SL[AOU]B/page initialization at alloc/free time.
  4  */                                                 4  */
  5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt         5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/mm.h>                               9 #include <linux/mm.h>
 10 #include <linux/module.h>                          10 #include <linux/module.h>
 11 #include <linux/slab.h>                            11 #include <linux/slab.h>
 12 #include <linux/string.h>                          12 #include <linux/string.h>
 13 #include <linux/vmalloc.h>                         13 #include <linux/vmalloc.h>
 14                                                    14 
 15 #define GARBAGE_INT (0x09A7BA9E)                   15 #define GARBAGE_INT (0x09A7BA9E)
 16 #define GARBAGE_BYTE (0x9E)                        16 #define GARBAGE_BYTE (0x9E)
 17                                                    17 
 18 #define REPORT_FAILURES_IN_FN() \                  18 #define REPORT_FAILURES_IN_FN() \
 19         do {    \                                  19         do {    \
 20                 if (failures)   \                  20                 if (failures)   \
 21                         pr_info("%s failed %d      21                         pr_info("%s failed %d out of %d times\n",       \
 22                                 __func__, fail     22                                 __func__, failures, num_tests);         \
 23                 else            \                  23                 else            \
 24                         pr_info("all %d tests      24                         pr_info("all %d tests in %s passed\n",          \
 25                                 num_tests, __f     25                                 num_tests, __func__);                   \
 26         } while (0)                                26         } while (0)
 27                                                    27 
 28 /* Calculate the number of uninitialized bytes     28 /* Calculate the number of uninitialized bytes in the buffer. */
 29 static int __init count_nonzero_bytes(void *pt     29 static int __init count_nonzero_bytes(void *ptr, size_t size)
 30 {                                                  30 {
 31         int i, ret = 0;                            31         int i, ret = 0;
 32         unsigned char *p = (unsigned char *)pt     32         unsigned char *p = (unsigned char *)ptr;
 33                                                    33 
 34         for (i = 0; i < size; i++)                 34         for (i = 0; i < size; i++)
 35                 if (p[i])                          35                 if (p[i])
 36                         ret++;                     36                         ret++;
 37         return ret;                                37         return ret;
 38 }                                                  38 }
 39                                                    39 
 40 /* Fill a buffer with garbage, skipping |skip|     40 /* Fill a buffer with garbage, skipping |skip| first bytes. */
 41 static void __init fill_with_garbage_skip(void     41 static void __init fill_with_garbage_skip(void *ptr, int size, size_t skip)
 42 {                                                  42 {
 43         unsigned int *p = (unsigned int *)((ch     43         unsigned int *p = (unsigned int *)((char *)ptr + skip);
 44         int i = 0;                                 44         int i = 0;
 45                                                    45 
 46         WARN_ON(skip > size);                      46         WARN_ON(skip > size);
 47         size -= skip;                              47         size -= skip;
 48                                                    48 
 49         while (size >= sizeof(*p)) {               49         while (size >= sizeof(*p)) {
 50                 p[i] = GARBAGE_INT;                50                 p[i] = GARBAGE_INT;
 51                 i++;                               51                 i++;
 52                 size -= sizeof(*p);                52                 size -= sizeof(*p);
 53         }                                          53         }
 54         if (size)                                  54         if (size)
 55                 memset(&p[i], GARBAGE_BYTE, si     55                 memset(&p[i], GARBAGE_BYTE, size);
 56 }                                                  56 }
 57                                                    57 
 58 static void __init fill_with_garbage(void *ptr     58 static void __init fill_with_garbage(void *ptr, size_t size)
 59 {                                                  59 {
 60         fill_with_garbage_skip(ptr, size, 0);      60         fill_with_garbage_skip(ptr, size, 0);
 61 }                                                  61 }
 62                                                    62 
 63 static int __init do_alloc_pages_order(int ord     63 static int __init do_alloc_pages_order(int order, int *total_failures)
 64 {                                                  64 {
 65         struct page *page;                         65         struct page *page;
 66         void *buf;                                 66         void *buf;
 67         size_t size = PAGE_SIZE << order;          67         size_t size = PAGE_SIZE << order;
 68                                                    68 
 69         page = alloc_pages(GFP_KERNEL, order);     69         page = alloc_pages(GFP_KERNEL, order);
 70         if (!page)                             << 
 71                 goto err;                      << 
 72         buf = page_address(page);                  70         buf = page_address(page);
 73         fill_with_garbage(buf, size);              71         fill_with_garbage(buf, size);
 74         __free_pages(page, order);                 72         __free_pages(page, order);
 75                                                    73 
 76         page = alloc_pages(GFP_KERNEL, order);     74         page = alloc_pages(GFP_KERNEL, order);
 77         if (!page)                             << 
 78                 goto err;                      << 
 79         buf = page_address(page);                  75         buf = page_address(page);
 80         if (count_nonzero_bytes(buf, size))        76         if (count_nonzero_bytes(buf, size))
 81                 (*total_failures)++;               77                 (*total_failures)++;
 82         fill_with_garbage(buf, size);              78         fill_with_garbage(buf, size);
 83         __free_pages(page, order);                 79         __free_pages(page, order);
 84         return 1;                                  80         return 1;
 85 err:                                           << 
 86         (*total_failures)++;                   << 
 87         return 1;                              << 
 88 }                                                  81 }
 89                                                    82 
 90 /* Test the page allocator by calling alloc_pa     83 /* Test the page allocator by calling alloc_pages with different orders. */
 91 static int __init test_pages(int *total_failur     84 static int __init test_pages(int *total_failures)
 92 {                                                  85 {
 93         int failures = 0, num_tests = 0;           86         int failures = 0, num_tests = 0;
 94         int i;                                     87         int i;
 95                                                    88 
 96         for (i = 0; i < NR_PAGE_ORDERS; i++)   !!  89         for (i = 0; i < 10; i++)
 97                 num_tests += do_alloc_pages_or     90                 num_tests += do_alloc_pages_order(i, &failures);
 98                                                    91 
 99         REPORT_FAILURES_IN_FN();                   92         REPORT_FAILURES_IN_FN();
100         *total_failures += failures;               93         *total_failures += failures;
101         return num_tests;                          94         return num_tests;
102 }                                                  95 }
103                                                    96 
104 /* Test kmalloc() with given parameters. */        97 /* Test kmalloc() with given parameters. */
105 static int __init do_kmalloc_size(size_t size,     98 static int __init do_kmalloc_size(size_t size, int *total_failures)
106 {                                                  99 {
107         void *buf;                                100         void *buf;
108                                                   101 
109         buf = kmalloc(size, GFP_KERNEL);          102         buf = kmalloc(size, GFP_KERNEL);
110         if (!buf)                              << 
111                 goto err;                      << 
112         fill_with_garbage(buf, size);             103         fill_with_garbage(buf, size);
113         kfree(buf);                               104         kfree(buf);
114                                                   105 
115         buf = kmalloc(size, GFP_KERNEL);          106         buf = kmalloc(size, GFP_KERNEL);
116         if (!buf)                              << 
117                 goto err;                      << 
118         if (count_nonzero_bytes(buf, size))       107         if (count_nonzero_bytes(buf, size))
119                 (*total_failures)++;              108                 (*total_failures)++;
120         fill_with_garbage(buf, size);             109         fill_with_garbage(buf, size);
121         kfree(buf);                               110         kfree(buf);
122         return 1;                                 111         return 1;
123 err:                                           << 
124         (*total_failures)++;                   << 
125         return 1;                              << 
126 }                                                 112 }
127                                                   113 
128 /* Test vmalloc() with given parameters. */       114 /* Test vmalloc() with given parameters. */
129 static int __init do_vmalloc_size(size_t size,    115 static int __init do_vmalloc_size(size_t size, int *total_failures)
130 {                                                 116 {
131         void *buf;                                117         void *buf;
132                                                   118 
133         buf = vmalloc(size);                      119         buf = vmalloc(size);
134         if (!buf)                              << 
135                 goto err;                      << 
136         fill_with_garbage(buf, size);             120         fill_with_garbage(buf, size);
137         vfree(buf);                               121         vfree(buf);
138                                                   122 
139         buf = vmalloc(size);                      123         buf = vmalloc(size);
140         if (!buf)                              << 
141                 goto err;                      << 
142         if (count_nonzero_bytes(buf, size))       124         if (count_nonzero_bytes(buf, size))
143                 (*total_failures)++;              125                 (*total_failures)++;
144         fill_with_garbage(buf, size);             126         fill_with_garbage(buf, size);
145         vfree(buf);                               127         vfree(buf);
146         return 1;                                 128         return 1;
147 err:                                           << 
148         (*total_failures)++;                   << 
149         return 1;                              << 
150 }                                                 129 }
151                                                   130 
152 /* Test kmalloc()/vmalloc() by allocating obje    131 /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
153 static int __init test_kvmalloc(int *total_fai    132 static int __init test_kvmalloc(int *total_failures)
154 {                                                 133 {
155         int failures = 0, num_tests = 0;          134         int failures = 0, num_tests = 0;
156         int i, size;                              135         int i, size;
157                                                   136 
158         for (i = 0; i < 20; i++) {                137         for (i = 0; i < 20; i++) {
159                 size = 1 << i;                    138                 size = 1 << i;
160                 num_tests += do_kmalloc_size(s    139                 num_tests += do_kmalloc_size(size, &failures);
161                 num_tests += do_vmalloc_size(s    140                 num_tests += do_vmalloc_size(size, &failures);
162         }                                         141         }
163                                                   142 
164         REPORT_FAILURES_IN_FN();                  143         REPORT_FAILURES_IN_FN();
165         *total_failures += failures;              144         *total_failures += failures;
166         return num_tests;                         145         return num_tests;
167 }                                                 146 }
168                                                   147 
169 #define CTOR_BYTES (sizeof(unsigned int))         148 #define CTOR_BYTES (sizeof(unsigned int))
170 #define CTOR_PATTERN (0x41414141)                 149 #define CTOR_PATTERN (0x41414141)
171 /* Initialize the first 4 bytes of the object.    150 /* Initialize the first 4 bytes of the object. */
172 static void test_ctor(void *obj)                  151 static void test_ctor(void *obj)
173 {                                                 152 {
174         *(unsigned int *)obj = CTOR_PATTERN;      153         *(unsigned int *)obj = CTOR_PATTERN;
175 }                                                 154 }
176                                                   155 
177 /*                                                156 /*
178  * Check the invariants for the buffer allocat    157  * Check the invariants for the buffer allocated from a slab cache.
179  * If the cache has a test constructor, the fi    158  * If the cache has a test constructor, the first 4 bytes of the object must
180  * always remain equal to CTOR_PATTERN.           159  * always remain equal to CTOR_PATTERN.
181  * If the cache isn't an RCU-typesafe one, or     160  * If the cache isn't an RCU-typesafe one, or if the allocation is done with
182  * __GFP_ZERO, then the object contents must b    161  * __GFP_ZERO, then the object contents must be zeroed after allocation.
183  * If the cache is an RCU-typesafe one, the ob    162  * If the cache is an RCU-typesafe one, the object contents must never be
184  * zeroed after the first use. This is checked    163  * zeroed after the first use. This is checked by memcmp() in
185  * do_kmem_cache_size().                          164  * do_kmem_cache_size().
186  */                                               165  */
187 static bool __init check_buf(void *buf, int si    166 static bool __init check_buf(void *buf, int size, bool want_ctor,
188                              bool want_rcu, bo    167                              bool want_rcu, bool want_zero)
189 {                                                 168 {
190         int bytes;                                169         int bytes;
191         bool fail = false;                        170         bool fail = false;
192                                                   171 
193         bytes = count_nonzero_bytes(buf, size)    172         bytes = count_nonzero_bytes(buf, size);
194         WARN_ON(want_ctor && want_zero);          173         WARN_ON(want_ctor && want_zero);
195         if (want_zero)                            174         if (want_zero)
196                 return bytes;                     175                 return bytes;
197         if (want_ctor) {                          176         if (want_ctor) {
198                 if (*(unsigned int *)buf != CT    177                 if (*(unsigned int *)buf != CTOR_PATTERN)
199                         fail = 1;                 178                         fail = 1;
200         } else {                                  179         } else {
201                 if (bytes)                        180                 if (bytes)
202                         fail = !want_rcu;         181                         fail = !want_rcu;
203         }                                         182         }
204         return fail;                              183         return fail;
205 }                                                 184 }
206                                                   185 
207 #define BULK_SIZE 100                             186 #define BULK_SIZE 100
208 static void *bulk_array[BULK_SIZE];               187 static void *bulk_array[BULK_SIZE];
209                                                   188 
210 /*                                                189 /*
211  * Test kmem_cache with given parameters:         190  * Test kmem_cache with given parameters:
212  *  want_ctor - use a constructor;                191  *  want_ctor - use a constructor;
213  *  want_rcu - use SLAB_TYPESAFE_BY_RCU;          192  *  want_rcu - use SLAB_TYPESAFE_BY_RCU;
214  *  want_zero - use __GFP_ZERO.                   193  *  want_zero - use __GFP_ZERO.
215  */                                               194  */
216 static int __init do_kmem_cache_size(size_t si    195 static int __init do_kmem_cache_size(size_t size, bool want_ctor,
217                                      bool want    196                                      bool want_rcu, bool want_zero,
218                                      int *tota    197                                      int *total_failures)
219 {                                                 198 {
220         struct kmem_cache *c;                     199         struct kmem_cache *c;
221         int iter;                                 200         int iter;
222         bool fail = false;                        201         bool fail = false;
223         gfp_t alloc_mask = GFP_KERNEL | (want_    202         gfp_t alloc_mask = GFP_KERNEL | (want_zero ? __GFP_ZERO : 0);
224         void *buf, *buf_copy;                     203         void *buf, *buf_copy;
225                                                   204 
226         c = kmem_cache_create("test_cache", si    205         c = kmem_cache_create("test_cache", size, 1,
227                               want_rcu ? SLAB_    206                               want_rcu ? SLAB_TYPESAFE_BY_RCU : 0,
228                               want_ctor ? test    207                               want_ctor ? test_ctor : NULL);
229         for (iter = 0; iter < 10; iter++) {       208         for (iter = 0; iter < 10; iter++) {
230                 /* Do a test of bulk allocatio    209                 /* Do a test of bulk allocations */
231                 if (!want_rcu && !want_ctor) {    210                 if (!want_rcu && !want_ctor) {
232                         int ret;                  211                         int ret;
233                                                   212 
234                         ret = kmem_cache_alloc    213                         ret = kmem_cache_alloc_bulk(c, alloc_mask, BULK_SIZE, bulk_array);
235                         if (!ret) {               214                         if (!ret) {
236                                 fail = true;      215                                 fail = true;
237                         } else {                  216                         } else {
238                                 int i;            217                                 int i;
239                                 for (i = 0; i     218                                 for (i = 0; i < ret; i++)
240                                         fail |    219                                         fail |= check_buf(bulk_array[i], size, want_ctor, want_rcu, want_zero);
241                                 kmem_cache_fre    220                                 kmem_cache_free_bulk(c, ret, bulk_array);
242                         }                         221                         }
243                 }                                 222                 }
244                                                   223 
245                 buf = kmem_cache_alloc(c, allo    224                 buf = kmem_cache_alloc(c, alloc_mask);
246                 /* Check that buf is zeroed, i    225                 /* Check that buf is zeroed, if it must be. */
247                 fail |= check_buf(buf, size, w    226                 fail |= check_buf(buf, size, want_ctor, want_rcu, want_zero);
248                 fill_with_garbage_skip(buf, si    227                 fill_with_garbage_skip(buf, size, want_ctor ? CTOR_BYTES : 0);
249                                                   228 
250                 if (!want_rcu) {                  229                 if (!want_rcu) {
251                         kmem_cache_free(c, buf    230                         kmem_cache_free(c, buf);
252                         continue;                 231                         continue;
253                 }                                 232                 }
254                                                   233 
255                 /*                                234                 /*
256                  * If this is an RCU cache, us    235                  * If this is an RCU cache, use a critical section to ensure we
257                  * can touch objects after the    236                  * can touch objects after they're freed.
258                  */                               237                  */
259                 rcu_read_lock();                  238                 rcu_read_lock();
260                 /*                                239                 /*
261                  * Copy the buffer to check th    240                  * Copy the buffer to check that it's not wiped on
262                  * free().                        241                  * free().
263                  */                               242                  */
264                 buf_copy = kmalloc(size, GFP_A    243                 buf_copy = kmalloc(size, GFP_ATOMIC);
265                 if (buf_copy)                     244                 if (buf_copy)
266                         memcpy(buf_copy, buf,     245                         memcpy(buf_copy, buf, size);
267                                                   246 
268                 kmem_cache_free(c, buf);          247                 kmem_cache_free(c, buf);
269                 /*                                248                 /*
270                  * Check that |buf| is intact     249                  * Check that |buf| is intact after kmem_cache_free().
271                  * |want_zero| is false, becau    250                  * |want_zero| is false, because we wrote garbage to
272                  * the buffer already.            251                  * the buffer already.
273                  */                               252                  */
274                 fail |= check_buf(buf, size, w    253                 fail |= check_buf(buf, size, want_ctor, want_rcu,
275                                   false);         254                                   false);
276                 if (buf_copy) {                   255                 if (buf_copy) {
277                         fail |= (bool)memcmp(b    256                         fail |= (bool)memcmp(buf, buf_copy, size);
278                         kfree(buf_copy);          257                         kfree(buf_copy);
279                 }                                 258                 }
280                 rcu_read_unlock();                259                 rcu_read_unlock();
281         }                                         260         }
282         kmem_cache_destroy(c);                    261         kmem_cache_destroy(c);
283                                                   262 
284         *total_failures += fail;                  263         *total_failures += fail;
285         return 1;                                 264         return 1;
286 }                                                 265 }
287                                                   266 
288 /*                                                267 /*
289  * Check that the data written to an RCU-alloc    268  * Check that the data written to an RCU-allocated object survives
290  * reallocation.                                  269  * reallocation.
291  */                                               270  */
292 static int __init do_kmem_cache_rcu_persistent    271 static int __init do_kmem_cache_rcu_persistent(int size, int *total_failures)
293 {                                                 272 {
294         struct kmem_cache *c;                     273         struct kmem_cache *c;
295         void *buf, *buf_contents, *saved_ptr;     274         void *buf, *buf_contents, *saved_ptr;
296         void **used_objects;                      275         void **used_objects;
297         int i, iter, maxiter = 1024;              276         int i, iter, maxiter = 1024;
298         bool fail = false;                        277         bool fail = false;
299                                                   278 
300         c = kmem_cache_create("test_cache", si    279         c = kmem_cache_create("test_cache", size, size, SLAB_TYPESAFE_BY_RCU,
301                               NULL);              280                               NULL);
302         buf = kmem_cache_alloc(c, GFP_KERNEL);    281         buf = kmem_cache_alloc(c, GFP_KERNEL);
303         if (!buf)                              << 
304                 goto out;                      << 
305         saved_ptr = buf;                          282         saved_ptr = buf;
306         fill_with_garbage(buf, size);             283         fill_with_garbage(buf, size);
307         buf_contents = kmalloc(size, GFP_KERNE    284         buf_contents = kmalloc(size, GFP_KERNEL);
308         if (!buf_contents) {                   !! 285         if (!buf_contents)
309                 kmem_cache_free(c, buf);       << 
310                 goto out;                         286                 goto out;
311         }                                      << 
312         used_objects = kmalloc_array(maxiter,     287         used_objects = kmalloc_array(maxiter, sizeof(void *), GFP_KERNEL);
313         if (!used_objects) {                      288         if (!used_objects) {
314                 kmem_cache_free(c, buf);       << 
315                 kfree(buf_contents);              289                 kfree(buf_contents);
316                 goto out;                         290                 goto out;
317         }                                         291         }
318         memcpy(buf_contents, buf, size);          292         memcpy(buf_contents, buf, size);
319         kmem_cache_free(c, buf);                  293         kmem_cache_free(c, buf);
320         /*                                        294         /*
321          * Run for a fixed number of iteration    295          * Run for a fixed number of iterations. If we never hit saved_ptr,
322          * assume the test passes.                296          * assume the test passes.
323          */                                       297          */
324         for (iter = 0; iter < maxiter; iter++)    298         for (iter = 0; iter < maxiter; iter++) {
325                 buf = kmem_cache_alloc(c, GFP_    299                 buf = kmem_cache_alloc(c, GFP_KERNEL);
326                 used_objects[iter] = buf;         300                 used_objects[iter] = buf;
327                 if (buf == saved_ptr) {           301                 if (buf == saved_ptr) {
328                         fail = memcmp(buf_cont    302                         fail = memcmp(buf_contents, buf, size);
329                         for (i = 0; i <= iter;    303                         for (i = 0; i <= iter; i++)
330                                 kmem_cache_fre    304                                 kmem_cache_free(c, used_objects[i]);
331                         goto free_out;            305                         goto free_out;
332                 }                                 306                 }
333         }                                         307         }
334                                                   308 
335         for (iter = 0; iter < maxiter; iter++) << 
336                 kmem_cache_free(c, used_object << 
337                                                << 
338 free_out:                                         309 free_out:
                                                   >> 310         kmem_cache_destroy(c);
339         kfree(buf_contents);                      311         kfree(buf_contents);
340         kfree(used_objects);                      312         kfree(used_objects);
341 out:                                              313 out:
342         kmem_cache_destroy(c);                 << 
343         *total_failures += fail;                  314         *total_failures += fail;
344         return 1;                                 315         return 1;
345 }                                                 316 }
346                                                   317 
347 static int __init do_kmem_cache_size_bulk(int     318 static int __init do_kmem_cache_size_bulk(int size, int *total_failures)
348 {                                                 319 {
349         struct kmem_cache *c;                     320         struct kmem_cache *c;
350         int i, iter, maxiter = 1024;              321         int i, iter, maxiter = 1024;
351         int num, bytes;                           322         int num, bytes;
352         bool fail = false;                        323         bool fail = false;
353         void *objects[10];                        324         void *objects[10];
354                                                   325 
355         c = kmem_cache_create("test_cache", si    326         c = kmem_cache_create("test_cache", size, size, 0, NULL);
356         for (iter = 0; (iter < maxiter) && !fa    327         for (iter = 0; (iter < maxiter) && !fail; iter++) {
357                 num = kmem_cache_alloc_bulk(c,    328                 num = kmem_cache_alloc_bulk(c, GFP_KERNEL, ARRAY_SIZE(objects),
358                                             ob    329                                             objects);
359                 for (i = 0; i < num; i++) {       330                 for (i = 0; i < num; i++) {
360                         bytes = count_nonzero_    331                         bytes = count_nonzero_bytes(objects[i], size);
361                         if (bytes)                332                         if (bytes)
362                                 fail = true;      333                                 fail = true;
363                         fill_with_garbage(obje    334                         fill_with_garbage(objects[i], size);
364                 }                                 335                 }
365                                                   336 
366                 if (num)                          337                 if (num)
367                         kmem_cache_free_bulk(c    338                         kmem_cache_free_bulk(c, num, objects);
368         }                                         339         }
369         kmem_cache_destroy(c);                 << 
370         *total_failures += fail;                  340         *total_failures += fail;
371         return 1;                                 341         return 1;
372 }                                                 342 }
373                                                   343 
374 /*                                                344 /*
375  * Test kmem_cache allocation by creating cach    345  * Test kmem_cache allocation by creating caches of different sizes, with and
376  * without constructors, with and without SLAB    346  * without constructors, with and without SLAB_TYPESAFE_BY_RCU.
377  */                                               347  */
378 static int __init test_kmemcache(int *total_fa    348 static int __init test_kmemcache(int *total_failures)
379 {                                                 349 {
380         int failures = 0, num_tests = 0;          350         int failures = 0, num_tests = 0;
381         int i, flags, size;                       351         int i, flags, size;
382         bool ctor, rcu, zero;                     352         bool ctor, rcu, zero;
383                                                   353 
384         for (i = 0; i < 10; i++) {                354         for (i = 0; i < 10; i++) {
385                 size = 8 << i;                    355                 size = 8 << i;
386                 for (flags = 0; flags < 8; fla    356                 for (flags = 0; flags < 8; flags++) {
387                         ctor = flags & 1;         357                         ctor = flags & 1;
388                         rcu = flags & 2;          358                         rcu = flags & 2;
389                         zero = flags & 4;         359                         zero = flags & 4;
390                         if (ctor & zero)          360                         if (ctor & zero)
391                                 continue;         361                                 continue;
392                         num_tests += do_kmem_c    362                         num_tests += do_kmem_cache_size(size, ctor, rcu, zero,
393                                                   363                                                         &failures);
394                 }                                 364                 }
395                 num_tests += do_kmem_cache_siz    365                 num_tests += do_kmem_cache_size_bulk(size, &failures);
396         }                                         366         }
397         REPORT_FAILURES_IN_FN();                  367         REPORT_FAILURES_IN_FN();
398         *total_failures += failures;              368         *total_failures += failures;
399         return num_tests;                         369         return num_tests;
400 }                                                 370 }
401                                                   371 
402 /* Test the behavior of SLAB_TYPESAFE_BY_RCU c    372 /* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */
403 static int __init test_rcu_persistent(int *tot    373 static int __init test_rcu_persistent(int *total_failures)
404 {                                                 374 {
405         int failures = 0, num_tests = 0;          375         int failures = 0, num_tests = 0;
406         int i, size;                              376         int i, size;
407                                                   377 
408         for (i = 0; i < 10; i++) {                378         for (i = 0; i < 10; i++) {
409                 size = 8 << i;                    379                 size = 8 << i;
410                 num_tests += do_kmem_cache_rcu    380                 num_tests += do_kmem_cache_rcu_persistent(size, &failures);
411         }                                         381         }
412         REPORT_FAILURES_IN_FN();                  382         REPORT_FAILURES_IN_FN();
413         *total_failures += failures;              383         *total_failures += failures;
414         return num_tests;                         384         return num_tests;
415 }                                                 385 }
416                                                   386 
417 /*                                                387 /*
418  * Run the tests. Each test function returns t    388  * Run the tests. Each test function returns the number of executed tests and
419  * updates |failures| with the number of faile    389  * updates |failures| with the number of failed tests.
420  */                                               390  */
421 static int __init test_meminit_init(void)         391 static int __init test_meminit_init(void)
422 {                                                 392 {
423         int failures = 0, num_tests = 0;          393         int failures = 0, num_tests = 0;
424                                                   394 
425         num_tests += test_pages(&failures);       395         num_tests += test_pages(&failures);
426         num_tests += test_kvmalloc(&failures);    396         num_tests += test_kvmalloc(&failures);
427         num_tests += test_kmemcache(&failures)    397         num_tests += test_kmemcache(&failures);
428         num_tests += test_rcu_persistent(&fail    398         num_tests += test_rcu_persistent(&failures);
429                                                   399 
430         if (failures == 0)                        400         if (failures == 0)
431                 pr_info("all %d tests passed!\    401                 pr_info("all %d tests passed!\n", num_tests);
432         else                                      402         else
433                 pr_info("failures: %d out of %    403                 pr_info("failures: %d out of %d\n", failures, num_tests);
434                                                   404 
435         return failures ? -EINVAL : 0;            405         return failures ? -EINVAL : 0;
436 }                                                 406 }
437 module_init(test_meminit_init);                   407 module_init(test_meminit_init);
438                                                   408 
439 MODULE_DESCRIPTION("Test cases for SL[AOU]B/pa << 
440 MODULE_LICENSE("GPL");                            409 MODULE_LICENSE("GPL");
441                                                   410 

~ [ 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