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

TOMOYO Linux Cross Reference
Linux/tools/testing/radix-tree/idr-test.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ 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 /tools/testing/radix-tree/idr-test.c (Version linux-6.11-rc3) and /tools/testing/radix-tree/idr-test.c (Version linux-5.5.19)


  1 // SPDX-License-Identifier: GPL-2.0-only            1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*                                                  2 /*
  3  * idr-test.c: Test the IDR API                     3  * idr-test.c: Test the IDR API
  4  * Copyright (c) 2016 Matthew Wilcox <willy@in      4  * Copyright (c) 2016 Matthew Wilcox <willy@infradead.org>
  5  */                                                 5  */
  6 #include <linux/bitmap.h>                           6 #include <linux/bitmap.h>
  7 #include <linux/idr.h>                              7 #include <linux/idr.h>
  8 #include <linux/slab.h>                             8 #include <linux/slab.h>
  9 #include <linux/kernel.h>                           9 #include <linux/kernel.h>
 10 #include <linux/errno.h>                           10 #include <linux/errno.h>
 11                                                    11 
 12 #include "test.h"                                  12 #include "test.h"
 13                                                    13 
 14 #define DUMMY_PTR       ((void *)0x10)             14 #define DUMMY_PTR       ((void *)0x10)
 15                                                    15 
 16 int item_idr_free(int id, void *p, void *data)     16 int item_idr_free(int id, void *p, void *data)
 17 {                                                  17 {
 18         struct item *item = p;                     18         struct item *item = p;
 19         assert(item->index == id);                 19         assert(item->index == id);
 20         free(p);                                   20         free(p);
 21                                                    21 
 22         return 0;                                  22         return 0;
 23 }                                                  23 }
 24                                                    24 
 25 void item_idr_remove(struct idr *idr, int id)      25 void item_idr_remove(struct idr *idr, int id)
 26 {                                                  26 {
 27         struct item *item = idr_find(idr, id);     27         struct item *item = idr_find(idr, id);
 28         assert(item->index == id);                 28         assert(item->index == id);
 29         idr_remove(idr, id);                       29         idr_remove(idr, id);
 30         free(item);                                30         free(item);
 31 }                                                  31 }
 32                                                    32 
 33 void idr_alloc_test(void)                          33 void idr_alloc_test(void)
 34 {                                                  34 {
 35         unsigned long i;                           35         unsigned long i;
 36         DEFINE_IDR(idr);                           36         DEFINE_IDR(idr);
 37                                                    37 
 38         assert(idr_alloc_cyclic(&idr, DUMMY_PT     38         assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0, 0x4000, GFP_KERNEL) == 0);
 39         assert(idr_alloc_cyclic(&idr, DUMMY_PT     39         assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0x3ffd, 0x4000, GFP_KERNEL) == 0x3ffd);
 40         idr_remove(&idr, 0x3ffd);                  40         idr_remove(&idr, 0x3ffd);
 41         idr_remove(&idr, 0);                       41         idr_remove(&idr, 0);
 42                                                    42 
 43         for (i = 0x3ffe; i < 0x4003; i++) {        43         for (i = 0x3ffe; i < 0x4003; i++) {
 44                 int id;                            44                 int id;
 45                 struct item *item;                 45                 struct item *item;
 46                                                    46 
 47                 if (i < 0x4000)                    47                 if (i < 0x4000)
 48                         item = item_create(i,      48                         item = item_create(i, 0);
 49                 else                               49                 else
 50                         item = item_create(i -     50                         item = item_create(i - 0x3fff, 0);
 51                                                    51 
 52                 id = idr_alloc_cyclic(&idr, it     52                 id = idr_alloc_cyclic(&idr, item, 1, 0x4000, GFP_KERNEL);
 53                 assert(id == item->index);         53                 assert(id == item->index);
 54         }                                          54         }
 55                                                    55 
 56         idr_for_each(&idr, item_idr_free, &idr     56         idr_for_each(&idr, item_idr_free, &idr);
 57         idr_destroy(&idr);                         57         idr_destroy(&idr);
 58 }                                                  58 }
 59                                                    59 
 60 void idr_replace_test(void)                        60 void idr_replace_test(void)
 61 {                                                  61 {
 62         DEFINE_IDR(idr);                           62         DEFINE_IDR(idr);
 63                                                    63 
 64         idr_alloc(&idr, (void *)-1, 10, 11, GF     64         idr_alloc(&idr, (void *)-1, 10, 11, GFP_KERNEL);
 65         idr_replace(&idr, &idr, 10);               65         idr_replace(&idr, &idr, 10);
 66                                                    66 
 67         idr_destroy(&idr);                         67         idr_destroy(&idr);
 68 }                                                  68 }
 69                                                    69 
 70 /*                                                 70 /*
 71  * Unlike the radix tree, you can put a NULL p     71  * Unlike the radix tree, you can put a NULL pointer -- with care -- into
 72  * the IDR.  Some interfaces, like idr_find()      72  * the IDR.  Some interfaces, like idr_find() do not distinguish between
 73  * "present, value is NULL" and "not present",     73  * "present, value is NULL" and "not present", but that's exactly what some
 74  * users want.                                     74  * users want.
 75  */                                                75  */
 76 void idr_null_test(void)                           76 void idr_null_test(void)
 77 {                                                  77 {
 78         int i;                                     78         int i;
 79         DEFINE_IDR(idr);                           79         DEFINE_IDR(idr);
 80                                                    80 
 81         assert(idr_is_empty(&idr));                81         assert(idr_is_empty(&idr));
 82                                                    82 
 83         assert(idr_alloc(&idr, NULL, 0, 0, GFP     83         assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
 84         assert(!idr_is_empty(&idr));               84         assert(!idr_is_empty(&idr));
 85         idr_remove(&idr, 0);                       85         idr_remove(&idr, 0);
 86         assert(idr_is_empty(&idr));                86         assert(idr_is_empty(&idr));
 87                                                    87 
 88         assert(idr_alloc(&idr, NULL, 0, 0, GFP     88         assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
 89         assert(!idr_is_empty(&idr));               89         assert(!idr_is_empty(&idr));
 90         idr_destroy(&idr);                         90         idr_destroy(&idr);
 91         assert(idr_is_empty(&idr));                91         assert(idr_is_empty(&idr));
 92                                                    92 
 93         for (i = 0; i < 10; i++) {                 93         for (i = 0; i < 10; i++) {
 94                 assert(idr_alloc(&idr, NULL, 0     94                 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == i);
 95         }                                          95         }
 96                                                    96 
 97         assert(idr_replace(&idr, DUMMY_PTR, 3)     97         assert(idr_replace(&idr, DUMMY_PTR, 3) == NULL);
 98         assert(idr_replace(&idr, DUMMY_PTR, 4)     98         assert(idr_replace(&idr, DUMMY_PTR, 4) == NULL);
 99         assert(idr_replace(&idr, NULL, 4) == D     99         assert(idr_replace(&idr, NULL, 4) == DUMMY_PTR);
100         assert(idr_replace(&idr, DUMMY_PTR, 11    100         assert(idr_replace(&idr, DUMMY_PTR, 11) == ERR_PTR(-ENOENT));
101         idr_remove(&idr, 5);                      101         idr_remove(&idr, 5);
102         assert(idr_alloc(&idr, NULL, 0, 0, GFP    102         assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 5);
103         idr_remove(&idr, 5);                      103         idr_remove(&idr, 5);
104                                                   104 
105         for (i = 0; i < 9; i++) {                 105         for (i = 0; i < 9; i++) {
106                 idr_remove(&idr, i);              106                 idr_remove(&idr, i);
107                 assert(!idr_is_empty(&idr));      107                 assert(!idr_is_empty(&idr));
108         }                                         108         }
109         idr_remove(&idr, 8);                      109         idr_remove(&idr, 8);
110         assert(!idr_is_empty(&idr));              110         assert(!idr_is_empty(&idr));
111         idr_remove(&idr, 9);                      111         idr_remove(&idr, 9);
112         assert(idr_is_empty(&idr));               112         assert(idr_is_empty(&idr));
113                                                   113 
114         assert(idr_alloc(&idr, NULL, 0, 0, GFP    114         assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
115         assert(idr_replace(&idr, DUMMY_PTR, 3)    115         assert(idr_replace(&idr, DUMMY_PTR, 3) == ERR_PTR(-ENOENT));
116         assert(idr_replace(&idr, DUMMY_PTR, 0)    116         assert(idr_replace(&idr, DUMMY_PTR, 0) == NULL);
117         assert(idr_replace(&idr, NULL, 0) == D    117         assert(idr_replace(&idr, NULL, 0) == DUMMY_PTR);
118                                                   118 
119         idr_destroy(&idr);                        119         idr_destroy(&idr);
120         assert(idr_is_empty(&idr));               120         assert(idr_is_empty(&idr));
121                                                   121 
122         for (i = 1; i < 10; i++) {                122         for (i = 1; i < 10; i++) {
123                 assert(idr_alloc(&idr, NULL, 1    123                 assert(idr_alloc(&idr, NULL, 1, 0, GFP_KERNEL) == i);
124         }                                         124         }
125                                                   125 
126         idr_destroy(&idr);                        126         idr_destroy(&idr);
127         assert(idr_is_empty(&idr));               127         assert(idr_is_empty(&idr));
128 }                                                 128 }
129                                                   129 
130 void idr_nowait_test(void)                        130 void idr_nowait_test(void)
131 {                                                 131 {
132         unsigned int i;                           132         unsigned int i;
133         DEFINE_IDR(idr);                          133         DEFINE_IDR(idr);
134                                                   134 
135         idr_preload(GFP_KERNEL);                  135         idr_preload(GFP_KERNEL);
136                                                   136 
137         for (i = 0; i < 3; i++) {                 137         for (i = 0; i < 3; i++) {
138                 struct item *item = item_creat    138                 struct item *item = item_create(i, 0);
139                 assert(idr_alloc(&idr, item, i    139                 assert(idr_alloc(&idr, item, i, i + 1, GFP_NOWAIT) == i);
140         }                                         140         }
141                                                   141 
142         idr_preload_end();                        142         idr_preload_end();
143                                                   143 
144         idr_for_each(&idr, item_idr_free, &idr    144         idr_for_each(&idr, item_idr_free, &idr);
145         idr_destroy(&idr);                        145         idr_destroy(&idr);
146 }                                                 146 }
147                                                   147 
148 void idr_get_next_test(int base)                  148 void idr_get_next_test(int base)
149 {                                                 149 {
150         unsigned long i;                          150         unsigned long i;
151         int nextid;                               151         int nextid;
152         DEFINE_IDR(idr);                          152         DEFINE_IDR(idr);
153         idr_init_base(&idr, base);                153         idr_init_base(&idr, base);
154                                                   154 
155         int indices[] = {4, 7, 9, 15, 65, 128,    155         int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0};
156                                                   156 
157         for(i = 0; indices[i]; i++) {             157         for(i = 0; indices[i]; i++) {
158                 struct item *item = item_creat    158                 struct item *item = item_create(indices[i], 0);
159                 assert(idr_alloc(&idr, item, i    159                 assert(idr_alloc(&idr, item, indices[i], indices[i+1],
160                                  GFP_KERNEL) =    160                                  GFP_KERNEL) == indices[i]);
161         }                                         161         }
162                                                   162 
163         for(i = 0, nextid = 0; indices[i]; i++    163         for(i = 0, nextid = 0; indices[i]; i++) {
164                 idr_get_next(&idr, &nextid);      164                 idr_get_next(&idr, &nextid);
165                 assert(nextid == indices[i]);     165                 assert(nextid == indices[i]);
166                 nextid++;                         166                 nextid++;
167         }                                         167         }
168                                                   168 
169         idr_for_each(&idr, item_idr_free, &idr    169         idr_for_each(&idr, item_idr_free, &idr);
170         idr_destroy(&idr);                        170         idr_destroy(&idr);
171 }                                                 171 }
172                                                   172 
173 int idr_u32_cb(int id, void *ptr, void *data)     173 int idr_u32_cb(int id, void *ptr, void *data)
174 {                                                 174 {
175         BUG_ON(id < 0);                           175         BUG_ON(id < 0);
176         BUG_ON(ptr != DUMMY_PTR);                 176         BUG_ON(ptr != DUMMY_PTR);
177         return 0;                                 177         return 0;
178 }                                                 178 }
179                                                   179 
180 void idr_u32_test1(struct idr *idr, u32 handle    180 void idr_u32_test1(struct idr *idr, u32 handle)
181 {                                                 181 {
182         static bool warned = false;               182         static bool warned = false;
183         u32 id = handle;                          183         u32 id = handle;
184         int sid = 0;                              184         int sid = 0;
185         void *ptr;                                185         void *ptr;
186                                                   186 
187         BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &    187         BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL));
188         BUG_ON(id != handle);                     188         BUG_ON(id != handle);
189         BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &    189         BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL) != -ENOSPC);
190         BUG_ON(id != handle);                     190         BUG_ON(id != handle);
191         if (!warned && id > INT_MAX)              191         if (!warned && id > INT_MAX)
192                 printk("vvv Ignore these warni    192                 printk("vvv Ignore these warnings\n");
193         ptr = idr_get_next(idr, &sid);            193         ptr = idr_get_next(idr, &sid);
194         if (id > INT_MAX) {                       194         if (id > INT_MAX) {
195                 BUG_ON(ptr != NULL);              195                 BUG_ON(ptr != NULL);
196                 BUG_ON(sid != 0);                 196                 BUG_ON(sid != 0);
197         } else {                                  197         } else {
198                 BUG_ON(ptr != DUMMY_PTR);         198                 BUG_ON(ptr != DUMMY_PTR);
199                 BUG_ON(sid != id);                199                 BUG_ON(sid != id);
200         }                                         200         }
201         idr_for_each(idr, idr_u32_cb, NULL);      201         idr_for_each(idr, idr_u32_cb, NULL);
202         if (!warned && id > INT_MAX) {            202         if (!warned && id > INT_MAX) {
203                 printk("^^^ Warnings over\n");    203                 printk("^^^ Warnings over\n");
204                 warned = true;                    204                 warned = true;
205         }                                         205         }
206         BUG_ON(idr_remove(idr, id) != DUMMY_PT    206         BUG_ON(idr_remove(idr, id) != DUMMY_PTR);
207         BUG_ON(!idr_is_empty(idr));               207         BUG_ON(!idr_is_empty(idr));
208 }                                                 208 }
209                                                   209 
210 void idr_u32_test(int base)                       210 void idr_u32_test(int base)
211 {                                                 211 {
212         DEFINE_IDR(idr);                          212         DEFINE_IDR(idr);
213         idr_init_base(&idr, base);                213         idr_init_base(&idr, base);
214         idr_u32_test1(&idr, 10);                  214         idr_u32_test1(&idr, 10);
215         idr_u32_test1(&idr, 0x7fffffff);          215         idr_u32_test1(&idr, 0x7fffffff);
216         idr_u32_test1(&idr, 0x80000000);          216         idr_u32_test1(&idr, 0x80000000);
217         idr_u32_test1(&idr, 0x80000001);          217         idr_u32_test1(&idr, 0x80000001);
218         idr_u32_test1(&idr, 0xffe00000);          218         idr_u32_test1(&idr, 0xffe00000);
219         idr_u32_test1(&idr, 0xffffffff);          219         idr_u32_test1(&idr, 0xffffffff);
220 }                                                 220 }
221                                                   221 
222 static void idr_align_test(struct idr *idr)       222 static void idr_align_test(struct idr *idr)
223 {                                                 223 {
224         char name[] = "Motorola 68000";           224         char name[] = "Motorola 68000";
225         int i, id;                                225         int i, id;
226         void *entry;                              226         void *entry;
227                                                   227 
228         for (i = 0; i < 9; i++) {                 228         for (i = 0; i < 9; i++) {
229                 BUG_ON(idr_alloc(idr, &name[i]    229                 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i);
230                 idr_for_each_entry(idr, entry,    230                 idr_for_each_entry(idr, entry, id);
231         }                                         231         }
232         idr_destroy(idr);                         232         idr_destroy(idr);
233                                                   233 
234         for (i = 1; i < 10; i++) {                234         for (i = 1; i < 10; i++) {
235                 BUG_ON(idr_alloc(idr, &name[i]    235                 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 1);
236                 idr_for_each_entry(idr, entry,    236                 idr_for_each_entry(idr, entry, id);
237         }                                         237         }
238         idr_destroy(idr);                         238         idr_destroy(idr);
239                                                   239 
240         for (i = 2; i < 11; i++) {                240         for (i = 2; i < 11; i++) {
241                 BUG_ON(idr_alloc(idr, &name[i]    241                 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 2);
242                 idr_for_each_entry(idr, entry,    242                 idr_for_each_entry(idr, entry, id);
243         }                                         243         }
244         idr_destroy(idr);                         244         idr_destroy(idr);
245                                                   245 
246         for (i = 3; i < 12; i++) {                246         for (i = 3; i < 12; i++) {
247                 BUG_ON(idr_alloc(idr, &name[i]    247                 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 3);
248                 idr_for_each_entry(idr, entry,    248                 idr_for_each_entry(idr, entry, id);
249         }                                         249         }
250         idr_destroy(idr);                         250         idr_destroy(idr);
251                                                   251 
252         for (i = 0; i < 8; i++) {                 252         for (i = 0; i < 8; i++) {
253                 BUG_ON(idr_alloc(idr, &name[i]    253                 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0);
254                 BUG_ON(idr_alloc(idr, &name[i     254                 BUG_ON(idr_alloc(idr, &name[i + 1], 0, 0, GFP_KERNEL) != 1);
255                 idr_for_each_entry(idr, entry,    255                 idr_for_each_entry(idr, entry, id);
256                 idr_remove(idr, 1);               256                 idr_remove(idr, 1);
257                 idr_for_each_entry(idr, entry,    257                 idr_for_each_entry(idr, entry, id);
258                 idr_remove(idr, 0);               258                 idr_remove(idr, 0);
259                 BUG_ON(!idr_is_empty(idr));       259                 BUG_ON(!idr_is_empty(idr));
260         }                                         260         }
261                                                   261 
262         for (i = 0; i < 8; i++) {                 262         for (i = 0; i < 8; i++) {
263                 BUG_ON(idr_alloc(idr, NULL, 0,    263                 BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 0);
264                 idr_for_each_entry(idr, entry,    264                 idr_for_each_entry(idr, entry, id);
265                 idr_replace(idr, &name[i], 0);    265                 idr_replace(idr, &name[i], 0);
266                 idr_for_each_entry(idr, entry,    266                 idr_for_each_entry(idr, entry, id);
267                 BUG_ON(idr_find(idr, 0) != &na    267                 BUG_ON(idr_find(idr, 0) != &name[i]);
268                 idr_remove(idr, 0);               268                 idr_remove(idr, 0);
269         }                                         269         }
270                                                   270 
271         for (i = 0; i < 8; i++) {                 271         for (i = 0; i < 8; i++) {
272                 BUG_ON(idr_alloc(idr, &name[i]    272                 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0);
273                 BUG_ON(idr_alloc(idr, NULL, 0,    273                 BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 1);
274                 idr_remove(idr, 1);               274                 idr_remove(idr, 1);
275                 idr_for_each_entry(idr, entry,    275                 idr_for_each_entry(idr, entry, id);
276                 idr_replace(idr, &name[i + 1],    276                 idr_replace(idr, &name[i + 1], 0);
277                 idr_for_each_entry(idr, entry,    277                 idr_for_each_entry(idr, entry, id);
278                 idr_remove(idr, 0);               278                 idr_remove(idr, 0);
279         }                                         279         }
280 }                                                 280 }
281                                                   281 
282 DEFINE_IDR(find_idr);                             282 DEFINE_IDR(find_idr);
283                                                   283 
284 static void *idr_throbber(void *arg)              284 static void *idr_throbber(void *arg)
285 {                                                 285 {
286         time_t start = time(NULL);                286         time_t start = time(NULL);
287         int id = *(int *)arg;                     287         int id = *(int *)arg;
288                                                   288 
289         rcu_register_thread();                    289         rcu_register_thread();
290         do {                                      290         do {
291                 idr_alloc(&find_idr, xa_mk_val    291                 idr_alloc(&find_idr, xa_mk_value(id), id, id + 1, GFP_KERNEL);
292                 idr_remove(&find_idr, id);        292                 idr_remove(&find_idr, id);
293         } while (time(NULL) < start + 10);        293         } while (time(NULL) < start + 10);
294         rcu_unregister_thread();                  294         rcu_unregister_thread();
295                                                   295 
296         return NULL;                              296         return NULL;
297 }                                                 297 }
298                                                   298 
299 /*                                             << 
300  * There are always either 1 or 2 objects in t << 
301  * or we find something at an ID we didn't exp << 
302  */                                            << 
303 void idr_find_test_1(int anchor_id, int throbb    299 void idr_find_test_1(int anchor_id, int throbber_id)
304 {                                                 300 {
305         pthread_t throbber;                       301         pthread_t throbber;
306         time_t start = time(NULL);                302         time_t start = time(NULL);
307                                                   303 
                                                   >> 304         pthread_create(&throbber, NULL, idr_throbber, &throbber_id);
                                                   >> 305 
308         BUG_ON(idr_alloc(&find_idr, xa_mk_valu    306         BUG_ON(idr_alloc(&find_idr, xa_mk_value(anchor_id), anchor_id,
309                                 anchor_id + 1,    307                                 anchor_id + 1, GFP_KERNEL) != anchor_id);
310                                                   308 
311         pthread_create(&throbber, NULL, idr_th << 
312                                                << 
313         rcu_read_lock();                       << 
314         do {                                      309         do {
315                 int id = 0;                       310                 int id = 0;
316                 void *entry = idr_get_next(&fi    311                 void *entry = idr_get_next(&find_idr, &id);
317                 rcu_read_unlock();             !! 312                 BUG_ON(entry != xa_mk_value(id));
318                 if ((id != anchor_id && id !=  << 
319                     entry != xa_mk_value(id))  << 
320                         printf("%s(%d, %d): %p << 
321                                 throbber_id, e << 
322                         abort();               << 
323                 }                              << 
324                 rcu_read_lock();               << 
325         } while (time(NULL) < start + 11);        313         } while (time(NULL) < start + 11);
326         rcu_read_unlock();                     << 
327                                                   314 
328         pthread_join(throbber, NULL);             315         pthread_join(throbber, NULL);
329                                                   316 
330         idr_remove(&find_idr, anchor_id);         317         idr_remove(&find_idr, anchor_id);
331         BUG_ON(!idr_is_empty(&find_idr));         318         BUG_ON(!idr_is_empty(&find_idr));
332 }                                                 319 }
333                                                   320 
334 void idr_find_test(void)                          321 void idr_find_test(void)
335 {                                                 322 {
336         idr_find_test_1(100000, 0);               323         idr_find_test_1(100000, 0);
337         idr_find_test_1(0, 100000);               324         idr_find_test_1(0, 100000);
338 }                                                 325 }
339                                                   326 
340 void idr_checks(void)                             327 void idr_checks(void)
341 {                                                 328 {
342         unsigned long i;                          329         unsigned long i;
343         DEFINE_IDR(idr);                          330         DEFINE_IDR(idr);
344                                                   331 
345         for (i = 0; i < 10000; i++) {             332         for (i = 0; i < 10000; i++) {
346                 struct item *item = item_creat    333                 struct item *item = item_create(i, 0);
347                 assert(idr_alloc(&idr, item, 0    334                 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
348         }                                         335         }
349                                                   336 
350         assert(idr_alloc(&idr, DUMMY_PTR, 5, 3    337         assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
351                                                   338 
352         for (i = 0; i < 5000; i++)                339         for (i = 0; i < 5000; i++)
353                 item_idr_remove(&idr, i);         340                 item_idr_remove(&idr, i);
354                                                   341 
355         idr_remove(&idr, 3);                      342         idr_remove(&idr, 3);
356                                                   343 
357         idr_for_each(&idr, item_idr_free, &idr    344         idr_for_each(&idr, item_idr_free, &idr);
358         idr_destroy(&idr);                        345         idr_destroy(&idr);
359                                                   346 
360         assert(idr_is_empty(&idr));               347         assert(idr_is_empty(&idr));
361                                                   348 
362         idr_remove(&idr, 3);                      349         idr_remove(&idr, 3);
363         idr_remove(&idr, 0);                      350         idr_remove(&idr, 0);
364                                                   351 
365         assert(idr_alloc(&idr, DUMMY_PTR, 0, 0    352         assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0);
366         idr_remove(&idr, 1);                      353         idr_remove(&idr, 1);
367         for (i = 1; i < RADIX_TREE_MAP_SIZE; i    354         for (i = 1; i < RADIX_TREE_MAP_SIZE; i++)
368                 assert(idr_alloc(&idr, DUMMY_P    355                 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i);
369         idr_remove(&idr, 1 << 30);                356         idr_remove(&idr, 1 << 30);
370         idr_destroy(&idr);                        357         idr_destroy(&idr);
371                                                   358 
372         for (i = INT_MAX - 3UL; i < INT_MAX +     359         for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
373                 struct item *item = item_creat    360                 struct item *item = item_create(i, 0);
374                 assert(idr_alloc(&idr, item, i    361                 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
375         }                                         362         }
376         assert(idr_alloc(&idr, DUMMY_PTR, i -     363         assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
377         assert(idr_alloc(&idr, DUMMY_PTR, i -     364         assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
378                                                   365 
379         idr_for_each(&idr, item_idr_free, &idr    366         idr_for_each(&idr, item_idr_free, &idr);
380         idr_destroy(&idr);                        367         idr_destroy(&idr);
381         idr_destroy(&idr);                        368         idr_destroy(&idr);
382                                                   369 
383         assert(idr_is_empty(&idr));               370         assert(idr_is_empty(&idr));
384                                                   371 
385         idr_set_cursor(&idr, INT_MAX - 3UL);      372         idr_set_cursor(&idr, INT_MAX - 3UL);
386         for (i = INT_MAX - 3UL; i < INT_MAX +     373         for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
387                 struct item *item;                374                 struct item *item;
388                 unsigned int id;                  375                 unsigned int id;
389                 if (i <= INT_MAX)                 376                 if (i <= INT_MAX)
390                         item = item_create(i,     377                         item = item_create(i, 0);
391                 else                              378                 else
392                         item = item_create(i -    379                         item = item_create(i - INT_MAX - 1, 0);
393                                                   380 
394                 id = idr_alloc_cyclic(&idr, it    381                 id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
395                 assert(id == item->index);        382                 assert(id == item->index);
396         }                                         383         }
397                                                   384 
398         idr_for_each(&idr, item_idr_free, &idr    385         idr_for_each(&idr, item_idr_free, &idr);
399         idr_destroy(&idr);                        386         idr_destroy(&idr);
400         assert(idr_is_empty(&idr));               387         assert(idr_is_empty(&idr));
401                                                   388 
402         for (i = 1; i < 10000; i++) {             389         for (i = 1; i < 10000; i++) {
403                 struct item *item = item_creat    390                 struct item *item = item_create(i, 0);
404                 assert(idr_alloc(&idr, item, 1    391                 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
405         }                                         392         }
406                                                   393 
407         idr_for_each(&idr, item_idr_free, &idr    394         idr_for_each(&idr, item_idr_free, &idr);
408         idr_destroy(&idr);                        395         idr_destroy(&idr);
409                                                   396 
410         idr_replace_test();                       397         idr_replace_test();
411         idr_alloc_test();                         398         idr_alloc_test();
412         idr_null_test();                          399         idr_null_test();
413         idr_nowait_test();                        400         idr_nowait_test();
414         idr_get_next_test(0);                     401         idr_get_next_test(0);
415         idr_get_next_test(1);                     402         idr_get_next_test(1);
416         idr_get_next_test(4);                     403         idr_get_next_test(4);
417         idr_u32_test(4);                          404         idr_u32_test(4);
418         idr_u32_test(1);                          405         idr_u32_test(1);
419         idr_u32_test(0);                          406         idr_u32_test(0);
420         idr_align_test(&idr);                     407         idr_align_test(&idr);
421         idr_find_test();                          408         idr_find_test();
422 }                                                 409 }
423                                                   410 
424 #define module_init(x)                            411 #define module_init(x)
425 #define module_exit(x)                            412 #define module_exit(x)
426 #define MODULE_AUTHOR(x)                          413 #define MODULE_AUTHOR(x)
427 #define MODULE_DESCRIPTION(X)                  << 
428 #define MODULE_LICENSE(x)                         414 #define MODULE_LICENSE(x)
429 #define dump_stack()    assert(0)                 415 #define dump_stack()    assert(0)
430 void ida_dump(struct ida *);                      416 void ida_dump(struct ida *);
431                                                   417 
432 #include "../../../lib/test_ida.c"                418 #include "../../../lib/test_ida.c"
433                                                   419 
434 /*                                                420 /*
435  * Check that we get the correct error when we    421  * Check that we get the correct error when we run out of memory doing
436  * allocations.  In userspace, GFP_NOWAIT will    422  * allocations.  In userspace, GFP_NOWAIT will always fail an allocation.
437  * The first test is for not having a bitmap a    423  * The first test is for not having a bitmap available, and the second test
438  * is for not being able to allocate a level o    424  * is for not being able to allocate a level of the radix tree.
439  */                                               425  */
440 void ida_check_nomem(void)                        426 void ida_check_nomem(void)
441 {                                                 427 {
442         DEFINE_IDA(ida);                          428         DEFINE_IDA(ida);
443         int id;                                   429         int id;
444                                                   430 
445         id = ida_alloc_min(&ida, 256, GFP_NOWA    431         id = ida_alloc_min(&ida, 256, GFP_NOWAIT);
446         IDA_BUG_ON(&ida, id != -ENOMEM);          432         IDA_BUG_ON(&ida, id != -ENOMEM);
447         id = ida_alloc_min(&ida, 1UL << 30, GF    433         id = ida_alloc_min(&ida, 1UL << 30, GFP_NOWAIT);
448         IDA_BUG_ON(&ida, id != -ENOMEM);          434         IDA_BUG_ON(&ida, id != -ENOMEM);
449         IDA_BUG_ON(&ida, !ida_is_empty(&ida));    435         IDA_BUG_ON(&ida, !ida_is_empty(&ida));
450 }                                                 436 }
451                                                   437 
452 /*                                                438 /*
453  * Check handling of conversions between excep    439  * Check handling of conversions between exceptional entries and full bitmaps.
454  */                                               440  */
455 void ida_check_conv_user(void)                    441 void ida_check_conv_user(void)
456 {                                                 442 {
457         DEFINE_IDA(ida);                          443         DEFINE_IDA(ida);
458         unsigned long i;                          444         unsigned long i;
459                                                   445 
460         for (i = 0; i < 1000000; i++) {           446         for (i = 0; i < 1000000; i++) {
461                 int id = ida_alloc(&ida, GFP_N    447                 int id = ida_alloc(&ida, GFP_NOWAIT);
462                 if (id == -ENOMEM) {              448                 if (id == -ENOMEM) {
463                         IDA_BUG_ON(&ida, ((i %    449                         IDA_BUG_ON(&ida, ((i % IDA_BITMAP_BITS) !=
464                                           BITS    450                                           BITS_PER_XA_VALUE) &&
465                                          ((i %    451                                          ((i % IDA_BITMAP_BITS) != 0));
466                         id = ida_alloc(&ida, G    452                         id = ida_alloc(&ida, GFP_KERNEL);
467                 } else {                          453                 } else {
468                         IDA_BUG_ON(&ida, (i %     454                         IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) ==
469                                         BITS_P    455                                         BITS_PER_XA_VALUE);
470                 }                                 456                 }
471                 IDA_BUG_ON(&ida, id != i);        457                 IDA_BUG_ON(&ida, id != i);
472         }                                         458         }
473         ida_destroy(&ida);                        459         ida_destroy(&ida);
474 }                                                 460 }
475                                                   461 
476 void ida_check_random(void)                       462 void ida_check_random(void)
477 {                                                 463 {
478         DEFINE_IDA(ida);                          464         DEFINE_IDA(ida);
479         DECLARE_BITMAP(bitmap, 2048);             465         DECLARE_BITMAP(bitmap, 2048);
480         unsigned int i;                           466         unsigned int i;
481         time_t s = time(NULL);                    467         time_t s = time(NULL);
482                                                   468 
483  repeat:                                          469  repeat:
484         memset(bitmap, 0, sizeof(bitmap));        470         memset(bitmap, 0, sizeof(bitmap));
485         for (i = 0; i < 100000; i++) {            471         for (i = 0; i < 100000; i++) {
486                 int i = rand();                   472                 int i = rand();
487                 int bit = i & 2047;               473                 int bit = i & 2047;
488                 if (test_bit(bit, bitmap)) {      474                 if (test_bit(bit, bitmap)) {
489                         __clear_bit(bit, bitma    475                         __clear_bit(bit, bitmap);
490                         ida_free(&ida, bit);      476                         ida_free(&ida, bit);
491                 } else {                          477                 } else {
492                         __set_bit(bit, bitmap)    478                         __set_bit(bit, bitmap);
493                         IDA_BUG_ON(&ida, ida_a    479                         IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL)
494                                         != bit    480                                         != bit);
495                 }                                 481                 }
496         }                                         482         }
497         ida_destroy(&ida);                        483         ida_destroy(&ida);
498         if (time(NULL) < s + 10)                  484         if (time(NULL) < s + 10)
499                 goto repeat;                      485                 goto repeat;
500 }                                                 486 }
501                                                   487 
502 void ida_simple_get_remove_test(void)             488 void ida_simple_get_remove_test(void)
503 {                                                 489 {
504         DEFINE_IDA(ida);                          490         DEFINE_IDA(ida);
505         unsigned long i;                          491         unsigned long i;
506                                                   492 
507         for (i = 0; i < 10000; i++) {             493         for (i = 0; i < 10000; i++) {
508                 assert(ida_simple_get(&ida, 0,    494                 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
509         }                                         495         }
510         assert(ida_simple_get(&ida, 5, 30, GFP    496         assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
511                                                   497 
512         for (i = 0; i < 10000; i++) {             498         for (i = 0; i < 10000; i++) {
513                 ida_simple_remove(&ida, i);       499                 ida_simple_remove(&ida, i);
514         }                                         500         }
515         assert(ida_is_empty(&ida));               501         assert(ida_is_empty(&ida));
516                                                   502 
517         ida_destroy(&ida);                        503         ida_destroy(&ida);
518 }                                                 504 }
519                                                   505 
520 void user_ida_checks(void)                        506 void user_ida_checks(void)
521 {                                                 507 {
522         radix_tree_cpu_dead(1);                   508         radix_tree_cpu_dead(1);
523                                                   509 
524         ida_check_nomem();                        510         ida_check_nomem();
525         ida_check_conv_user();                    511         ida_check_conv_user();
526         ida_check_random();                       512         ida_check_random();
527         ida_simple_get_remove_test();             513         ida_simple_get_remove_test();
528                                                   514 
529         radix_tree_cpu_dead(1);                   515         radix_tree_cpu_dead(1);
530 }                                                 516 }
531                                                   517 
532 static void *ida_random_fn(void *arg)             518 static void *ida_random_fn(void *arg)
533 {                                                 519 {
534         rcu_register_thread();                    520         rcu_register_thread();
535         ida_check_random();                       521         ida_check_random();
536         rcu_unregister_thread();                  522         rcu_unregister_thread();
537         return NULL;                              523         return NULL;
538 }                                                 524 }
539                                                   525 
540 static void *ida_leak_fn(void *arg)            << 
541 {                                              << 
542         struct ida *ida = arg;                 << 
543         time_t s = time(NULL);                 << 
544         int i, ret;                            << 
545                                                << 
546         rcu_register_thread();                 << 
547                                                << 
548         do for (i = 0; i < 1000; i++) {        << 
549                 ret = ida_alloc_range(ida, 128 << 
550                 if (ret >= 0)                  << 
551                         ida_free(ida, 128);    << 
552         } while (time(NULL) < s + 2);          << 
553                                                << 
554         rcu_unregister_thread();               << 
555         return NULL;                           << 
556 }                                              << 
557                                                << 
558 void ida_thread_tests(void)                       526 void ida_thread_tests(void)
559 {                                                 527 {
560         DEFINE_IDA(ida);                       << 
561         pthread_t threads[20];                    528         pthread_t threads[20];
562         int i;                                    529         int i;
563                                                   530 
564         for (i = 0; i < ARRAY_SIZE(threads); i    531         for (i = 0; i < ARRAY_SIZE(threads); i++)
565                 if (pthread_create(&threads[i]    532                 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
566                         perror("creating ida t    533                         perror("creating ida thread");
567                         exit(1);                  534                         exit(1);
568                 }                                 535                 }
569                                                   536 
570         while (i--)                               537         while (i--)
571                 pthread_join(threads[i], NULL)    538                 pthread_join(threads[i], NULL);
572                                                << 
573         for (i = 0; i < ARRAY_SIZE(threads); i << 
574                 if (pthread_create(&threads[i] << 
575                         perror("creating ida t << 
576                         exit(1);               << 
577                 }                              << 
578                                                << 
579         while (i--)                            << 
580                 pthread_join(threads[i], NULL) << 
581         assert(ida_is_empty(&ida));            << 
582 }                                                 539 }
583                                                   540 
584 void ida_tests(void)                              541 void ida_tests(void)
585 {                                                 542 {
586         user_ida_checks();                        543         user_ida_checks();
587         ida_checks();                             544         ida_checks();
588         ida_exit();                               545         ida_exit();
589         ida_thread_tests();                       546         ida_thread_tests();
590 }                                                 547 }
591                                                   548 
592 int __weak main(void)                             549 int __weak main(void)
593 {                                                 550 {
594         rcu_register_thread();                 << 
595         radix_tree_init();                        551         radix_tree_init();
596         idr_checks();                             552         idr_checks();
597         ida_tests();                              553         ida_tests();
598         radix_tree_cpu_dead(1);                   554         radix_tree_cpu_dead(1);
599         rcu_barrier();                            555         rcu_barrier();
600         if (nr_allocated)                         556         if (nr_allocated)
601                 printf("nr_allocated = %d\n",     557                 printf("nr_allocated = %d\n", nr_allocated);
602         rcu_unregister_thread();               << 
603         return 0;                                 558         return 0;
604 }                                                 559 }
605                                                   560 

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