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

TOMOYO Linux Cross Reference
Linux/tools/testing/shared/linux.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 /tools/testing/shared/linux.c (Architecture m68k) and /tools/testing/shared/linux.c (Architecture alpha)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 #include <stdlib.h>                                 2 #include <stdlib.h>
  3 #include <string.h>                                 3 #include <string.h>
  4 #include <malloc.h>                                 4 #include <malloc.h>
  5 #include <pthread.h>                                5 #include <pthread.h>
  6 #include <unistd.h>                                 6 #include <unistd.h>
  7 #include <assert.h>                                 7 #include <assert.h>
  8                                                     8 
  9 #include <linux/gfp.h>                              9 #include <linux/gfp.h>
 10 #include <linux/poison.h>                          10 #include <linux/poison.h>
 11 #include <linux/slab.h>                            11 #include <linux/slab.h>
 12 #include <linux/radix-tree.h>                      12 #include <linux/radix-tree.h>
 13 #include <urcu/uatomic.h>                          13 #include <urcu/uatomic.h>
 14                                                    14 
 15 int nr_allocated;                                  15 int nr_allocated;
 16 int preempt_count;                                 16 int preempt_count;
 17 int test_verbose;                                  17 int test_verbose;
 18                                                    18 
 19 struct kmem_cache {                                19 struct kmem_cache {
 20         pthread_mutex_t lock;                      20         pthread_mutex_t lock;
 21         unsigned int size;                         21         unsigned int size;
 22         unsigned int align;                        22         unsigned int align;
 23         int nr_objs;                               23         int nr_objs;
 24         void *objs;                                24         void *objs;
 25         void (*ctor)(void *);                      25         void (*ctor)(void *);
 26         unsigned int non_kernel;                   26         unsigned int non_kernel;
 27         unsigned long nr_allocated;                27         unsigned long nr_allocated;
 28         unsigned long nr_tallocated;               28         unsigned long nr_tallocated;
 29         bool exec_callback;                        29         bool exec_callback;
 30         void (*callback)(void *);                  30         void (*callback)(void *);
 31         void *private;                             31         void *private;
 32 };                                                 32 };
 33                                                    33 
 34 void kmem_cache_set_callback(struct kmem_cache     34 void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *))
 35 {                                                  35 {
 36         cachep->callback = callback;               36         cachep->callback = callback;
 37 }                                                  37 }
 38                                                    38 
 39 void kmem_cache_set_private(struct kmem_cache      39 void kmem_cache_set_private(struct kmem_cache *cachep, void *private)
 40 {                                                  40 {
 41         cachep->private = private;                 41         cachep->private = private;
 42 }                                                  42 }
 43                                                    43 
 44 void kmem_cache_set_non_kernel(struct kmem_cac     44 void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val)
 45 {                                                  45 {
 46         cachep->non_kernel = val;                  46         cachep->non_kernel = val;
 47 }                                                  47 }
 48                                                    48 
 49 unsigned long kmem_cache_get_alloc(struct kmem     49 unsigned long kmem_cache_get_alloc(struct kmem_cache *cachep)
 50 {                                                  50 {
 51         return cachep->size * cachep->nr_alloc     51         return cachep->size * cachep->nr_allocated;
 52 }                                                  52 }
 53                                                    53 
 54 unsigned long kmem_cache_nr_allocated(struct k     54 unsigned long kmem_cache_nr_allocated(struct kmem_cache *cachep)
 55 {                                                  55 {
 56         return cachep->nr_allocated;               56         return cachep->nr_allocated;
 57 }                                                  57 }
 58                                                    58 
 59 unsigned long kmem_cache_nr_tallocated(struct      59 unsigned long kmem_cache_nr_tallocated(struct kmem_cache *cachep)
 60 {                                                  60 {
 61         return cachep->nr_tallocated;              61         return cachep->nr_tallocated;
 62 }                                                  62 }
 63                                                    63 
 64 void kmem_cache_zero_nr_tallocated(struct kmem     64 void kmem_cache_zero_nr_tallocated(struct kmem_cache *cachep)
 65 {                                                  65 {
 66         cachep->nr_tallocated = 0;                 66         cachep->nr_tallocated = 0;
 67 }                                                  67 }
 68                                                    68 
 69 void *kmem_cache_alloc_lru(struct kmem_cache *     69 void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
 70                 int gfp)                           70                 int gfp)
 71 {                                                  71 {
 72         void *p;                                   72         void *p;
 73                                                    73 
 74         if (cachep->exec_callback) {               74         if (cachep->exec_callback) {
 75                 if (cachep->callback)              75                 if (cachep->callback)
 76                         cachep->callback(cache     76                         cachep->callback(cachep->private);
 77                 cachep->exec_callback = false;     77                 cachep->exec_callback = false;
 78         }                                          78         }
 79                                                    79 
 80         if (!(gfp & __GFP_DIRECT_RECLAIM)) {       80         if (!(gfp & __GFP_DIRECT_RECLAIM)) {
 81                 if (!cachep->non_kernel) {         81                 if (!cachep->non_kernel) {
 82                         cachep->exec_callback      82                         cachep->exec_callback = true;
 83                         return NULL;               83                         return NULL;
 84                 }                                  84                 }
 85                                                    85 
 86                 cachep->non_kernel--;              86                 cachep->non_kernel--;
 87         }                                          87         }
 88                                                    88 
 89         pthread_mutex_lock(&cachep->lock);         89         pthread_mutex_lock(&cachep->lock);
 90         if (cachep->nr_objs) {                     90         if (cachep->nr_objs) {
 91                 struct radix_tree_node *node =     91                 struct radix_tree_node *node = cachep->objs;
 92                 cachep->nr_objs--;                 92                 cachep->nr_objs--;
 93                 cachep->objs = node->parent;       93                 cachep->objs = node->parent;
 94                 pthread_mutex_unlock(&cachep->     94                 pthread_mutex_unlock(&cachep->lock);
 95                 node->parent = NULL;               95                 node->parent = NULL;
 96                 p = node;                          96                 p = node;
 97         } else {                                   97         } else {
 98                 pthread_mutex_unlock(&cachep->     98                 pthread_mutex_unlock(&cachep->lock);
 99                 if (cachep->align)                 99                 if (cachep->align)
100                         posix_memalign(&p, cac    100                         posix_memalign(&p, cachep->align, cachep->size);
101                 else                              101                 else
102                         p = malloc(cachep->siz    102                         p = malloc(cachep->size);
103                 if (cachep->ctor)                 103                 if (cachep->ctor)
104                         cachep->ctor(p);          104                         cachep->ctor(p);
105                 else if (gfp & __GFP_ZERO)        105                 else if (gfp & __GFP_ZERO)
106                         memset(p, 0, cachep->s    106                         memset(p, 0, cachep->size);
107         }                                         107         }
108                                                   108 
109         uatomic_inc(&cachep->nr_allocated);       109         uatomic_inc(&cachep->nr_allocated);
110         uatomic_inc(&nr_allocated);               110         uatomic_inc(&nr_allocated);
111         uatomic_inc(&cachep->nr_tallocated);      111         uatomic_inc(&cachep->nr_tallocated);
112         if (kmalloc_verbose)                      112         if (kmalloc_verbose)
113                 printf("Allocating %p from sla    113                 printf("Allocating %p from slab\n", p);
114         return p;                                 114         return p;
115 }                                                 115 }
116                                                   116 
117 void __kmem_cache_free_locked(struct kmem_cach    117 void __kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
118 {                                                 118 {
119         assert(objp);                             119         assert(objp);
120         if (cachep->nr_objs > 10 || cachep->al    120         if (cachep->nr_objs > 10 || cachep->align) {
121                 memset(objp, POISON_FREE, cach    121                 memset(objp, POISON_FREE, cachep->size);
122                 free(objp);                       122                 free(objp);
123         } else {                                  123         } else {
124                 struct radix_tree_node *node =    124                 struct radix_tree_node *node = objp;
125                 cachep->nr_objs++;                125                 cachep->nr_objs++;
126                 node->parent = cachep->objs;      126                 node->parent = cachep->objs;
127                 cachep->objs = node;              127                 cachep->objs = node;
128         }                                         128         }
129 }                                                 129 }
130                                                   130 
131 void kmem_cache_free_locked(struct kmem_cache     131 void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
132 {                                                 132 {
133         uatomic_dec(&nr_allocated);               133         uatomic_dec(&nr_allocated);
134         uatomic_dec(&cachep->nr_allocated);       134         uatomic_dec(&cachep->nr_allocated);
135         if (kmalloc_verbose)                      135         if (kmalloc_verbose)
136                 printf("Freeing %p to slab\n",    136                 printf("Freeing %p to slab\n", objp);
137         __kmem_cache_free_locked(cachep, objp)    137         __kmem_cache_free_locked(cachep, objp);
138 }                                                 138 }
139                                                   139 
140 void kmem_cache_free(struct kmem_cache *cachep    140 void kmem_cache_free(struct kmem_cache *cachep, void *objp)
141 {                                                 141 {
142         pthread_mutex_lock(&cachep->lock);        142         pthread_mutex_lock(&cachep->lock);
143         kmem_cache_free_locked(cachep, objp);     143         kmem_cache_free_locked(cachep, objp);
144         pthread_mutex_unlock(&cachep->lock);      144         pthread_mutex_unlock(&cachep->lock);
145 }                                                 145 }
146                                                   146 
147 void kmem_cache_free_bulk(struct kmem_cache *c    147 void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
148 {                                                 148 {
149         if (kmalloc_verbose)                      149         if (kmalloc_verbose)
150                 pr_debug("Bulk free %p[0-%lu]\    150                 pr_debug("Bulk free %p[0-%lu]\n", list, size - 1);
151                                                   151 
152         pthread_mutex_lock(&cachep->lock);        152         pthread_mutex_lock(&cachep->lock);
153         for (int i = 0; i < size; i++)            153         for (int i = 0; i < size; i++)
154                 kmem_cache_free_locked(cachep,    154                 kmem_cache_free_locked(cachep, list[i]);
155         pthread_mutex_unlock(&cachep->lock);      155         pthread_mutex_unlock(&cachep->lock);
156 }                                                 156 }
157                                                   157 
158 void kmem_cache_shrink(struct kmem_cache *cach    158 void kmem_cache_shrink(struct kmem_cache *cachep)
159 {                                                 159 {
160 }                                                 160 }
161                                                   161 
162 int kmem_cache_alloc_bulk(struct kmem_cache *c    162 int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
163                           void **p)               163                           void **p)
164 {                                                 164 {
165         size_t i;                                 165         size_t i;
166                                                   166 
167         if (kmalloc_verbose)                      167         if (kmalloc_verbose)
168                 pr_debug("Bulk alloc %lu\n", s    168                 pr_debug("Bulk alloc %lu\n", size);
169                                                   169 
170         pthread_mutex_lock(&cachep->lock);        170         pthread_mutex_lock(&cachep->lock);
171         if (cachep->nr_objs >= size) {            171         if (cachep->nr_objs >= size) {
172                 struct radix_tree_node *node;     172                 struct radix_tree_node *node;
173                                                   173 
174                 for (i = 0; i < size; i++) {      174                 for (i = 0; i < size; i++) {
175                         if (!(gfp & __GFP_DIRE    175                         if (!(gfp & __GFP_DIRECT_RECLAIM)) {
176                                 if (!cachep->n    176                                 if (!cachep->non_kernel)
177                                         break;    177                                         break;
178                                 cachep->non_ke    178                                 cachep->non_kernel--;
179                         }                         179                         }
180                                                   180 
181                         node = cachep->objs;      181                         node = cachep->objs;
182                         cachep->nr_objs--;        182                         cachep->nr_objs--;
183                         cachep->objs = node->p    183                         cachep->objs = node->parent;
184                         p[i] = node;              184                         p[i] = node;
185                         node->parent = NULL;      185                         node->parent = NULL;
186                 }                                 186                 }
187                 pthread_mutex_unlock(&cachep->    187                 pthread_mutex_unlock(&cachep->lock);
188         } else {                                  188         } else {
189                 pthread_mutex_unlock(&cachep->    189                 pthread_mutex_unlock(&cachep->lock);
190                 for (i = 0; i < size; i++) {      190                 for (i = 0; i < size; i++) {
191                         if (!(gfp & __GFP_DIRE    191                         if (!(gfp & __GFP_DIRECT_RECLAIM)) {
192                                 if (!cachep->n    192                                 if (!cachep->non_kernel)
193                                         break;    193                                         break;
194                                 cachep->non_ke    194                                 cachep->non_kernel--;
195                         }                         195                         }
196                                                   196 
197                         if (cachep->align) {      197                         if (cachep->align) {
198                                 posix_memalign    198                                 posix_memalign(&p[i], cachep->align,
199                                                   199                                                cachep->size);
200                         } else {                  200                         } else {
201                                 p[i] = malloc(    201                                 p[i] = malloc(cachep->size);
202                                 if (!p[i])        202                                 if (!p[i])
203                                         break;    203                                         break;
204                         }                         204                         }
205                         if (cachep->ctor)         205                         if (cachep->ctor)
206                                 cachep->ctor(p    206                                 cachep->ctor(p[i]);
207                         else if (gfp & __GFP_Z    207                         else if (gfp & __GFP_ZERO)
208                                 memset(p[i], 0    208                                 memset(p[i], 0, cachep->size);
209                 }                                 209                 }
210         }                                         210         }
211                                                   211 
212         if (i < size) {                           212         if (i < size) {
213                 size = i;                         213                 size = i;
214                 pthread_mutex_lock(&cachep->lo    214                 pthread_mutex_lock(&cachep->lock);
215                 for (i = 0; i < size; i++)        215                 for (i = 0; i < size; i++)
216                         __kmem_cache_free_lock    216                         __kmem_cache_free_locked(cachep, p[i]);
217                 pthread_mutex_unlock(&cachep->    217                 pthread_mutex_unlock(&cachep->lock);
218                 return 0;                         218                 return 0;
219         }                                         219         }
220                                                   220 
221         for (i = 0; i < size; i++) {              221         for (i = 0; i < size; i++) {
222                 uatomic_inc(&nr_allocated);       222                 uatomic_inc(&nr_allocated);
223                 uatomic_inc(&cachep->nr_alloca    223                 uatomic_inc(&cachep->nr_allocated);
224                 uatomic_inc(&cachep->nr_talloc    224                 uatomic_inc(&cachep->nr_tallocated);
225                 if (kmalloc_verbose)              225                 if (kmalloc_verbose)
226                         printf("Allocating %p     226                         printf("Allocating %p from slab\n", p[i]);
227         }                                         227         }
228                                                   228 
229         return size;                              229         return size;
230 }                                                 230 }
231                                                   231 
232 struct kmem_cache *                               232 struct kmem_cache *
233 kmem_cache_create(const char *name, unsigned i    233 kmem_cache_create(const char *name, unsigned int size, unsigned int align,
234                 unsigned int flags, void (*cto    234                 unsigned int flags, void (*ctor)(void *))
235 {                                                 235 {
236         struct kmem_cache *ret = malloc(sizeof    236         struct kmem_cache *ret = malloc(sizeof(*ret));
237                                                   237 
238         pthread_mutex_init(&ret->lock, NULL);     238         pthread_mutex_init(&ret->lock, NULL);
239         ret->size = size;                         239         ret->size = size;
240         ret->align = align;                       240         ret->align = align;
241         ret->nr_objs = 0;                         241         ret->nr_objs = 0;
242         ret->nr_allocated = 0;                    242         ret->nr_allocated = 0;
243         ret->nr_tallocated = 0;                   243         ret->nr_tallocated = 0;
244         ret->objs = NULL;                         244         ret->objs = NULL;
245         ret->ctor = ctor;                         245         ret->ctor = ctor;
246         ret->non_kernel = 0;                      246         ret->non_kernel = 0;
247         ret->exec_callback = false;               247         ret->exec_callback = false;
248         ret->callback = NULL;                     248         ret->callback = NULL;
249         ret->private = NULL;                      249         ret->private = NULL;
250         return ret;                               250         return ret;
251 }                                                 251 }
252                                                   252 
253 /*                                                253 /*
254  * Test the test infrastructure for kem_cache_    254  * Test the test infrastructure for kem_cache_alloc/free and bulk counterparts.
255  */                                               255  */
256 void test_kmem_cache_bulk(void)                   256 void test_kmem_cache_bulk(void)
257 {                                                 257 {
258         int i;                                    258         int i;
259         void *list[12];                           259         void *list[12];
260         static struct kmem_cache *test_cache,     260         static struct kmem_cache *test_cache, *test_cache2;
261                                                   261 
262         /*                                        262         /*
263          * Testing the bulk allocators without    263          * Testing the bulk allocators without aligned kmem_cache to force the
264          * bulk alloc/free to reuse               264          * bulk alloc/free to reuse
265          */                                       265          */
266         test_cache = kmem_cache_create("test_c    266         test_cache = kmem_cache_create("test_cache", 256, 0, SLAB_PANIC, NULL);
267                                                   267 
268         for (i = 0; i < 5; i++)                   268         for (i = 0; i < 5; i++)
269                 list[i] = kmem_cache_alloc(tes    269                 list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
270                                                   270 
271         for (i = 0; i < 5; i++)                   271         for (i = 0; i < 5; i++)
272                 kmem_cache_free(test_cache, li    272                 kmem_cache_free(test_cache, list[i]);
273         assert(test_cache->nr_objs == 5);         273         assert(test_cache->nr_objs == 5);
274                                                   274 
275         kmem_cache_alloc_bulk(test_cache, __GF    275         kmem_cache_alloc_bulk(test_cache, __GFP_DIRECT_RECLAIM, 5, list);
276         kmem_cache_free_bulk(test_cache, 5, li    276         kmem_cache_free_bulk(test_cache, 5, list);
277                                                   277 
278         for (i = 0; i < 12 ; i++)                 278         for (i = 0; i < 12 ; i++)
279                 list[i] = kmem_cache_alloc(tes    279                 list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
280                                                   280 
281         for (i = 0; i < 12; i++)                  281         for (i = 0; i < 12; i++)
282                 kmem_cache_free(test_cache, li    282                 kmem_cache_free(test_cache, list[i]);
283                                                   283 
284         /* The last free will not be kept arou    284         /* The last free will not be kept around */
285         assert(test_cache->nr_objs == 11);        285         assert(test_cache->nr_objs == 11);
286                                                   286 
287         /* Aligned caches will immediately fre    287         /* Aligned caches will immediately free */
288         test_cache2 = kmem_cache_create("test_    288         test_cache2 = kmem_cache_create("test_cache2", 128, 128, SLAB_PANIC, NULL);
289                                                   289 
290         kmem_cache_alloc_bulk(test_cache2, __G    290         kmem_cache_alloc_bulk(test_cache2, __GFP_DIRECT_RECLAIM, 10, list);
291         kmem_cache_free_bulk(test_cache2, 10,     291         kmem_cache_free_bulk(test_cache2, 10, list);
292         assert(!test_cache2->nr_objs);            292         assert(!test_cache2->nr_objs);
293                                                   293 
294                                                   294 
295 }                                                 295 }
296                                                   296 

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