174579d8dSLorenzo Stoakes // SPDX-License-Identifier: GPL-2.0
274579d8dSLorenzo Stoakes #include <stdlib.h>
374579d8dSLorenzo Stoakes #include <string.h>
474579d8dSLorenzo Stoakes #include <malloc.h>
574579d8dSLorenzo Stoakes #include <pthread.h>
674579d8dSLorenzo Stoakes #include <unistd.h>
774579d8dSLorenzo Stoakes #include <assert.h>
874579d8dSLorenzo Stoakes
974579d8dSLorenzo Stoakes #include <linux/gfp.h>
1074579d8dSLorenzo Stoakes #include <linux/poison.h>
1174579d8dSLorenzo Stoakes #include <linux/slab.h>
1274579d8dSLorenzo Stoakes #include <linux/radix-tree.h>
1374579d8dSLorenzo Stoakes #include <urcu/uatomic.h>
1474579d8dSLorenzo Stoakes
1574579d8dSLorenzo Stoakes int nr_allocated;
1674579d8dSLorenzo Stoakes int preempt_count;
1774579d8dSLorenzo Stoakes int test_verbose;
1874579d8dSLorenzo Stoakes
1974579d8dSLorenzo Stoakes struct kmem_cache {
2074579d8dSLorenzo Stoakes pthread_mutex_t lock;
2174579d8dSLorenzo Stoakes unsigned int size;
2274579d8dSLorenzo Stoakes unsigned int align;
2374579d8dSLorenzo Stoakes int nr_objs;
2474579d8dSLorenzo Stoakes void *objs;
2574579d8dSLorenzo Stoakes void (*ctor)(void *);
2674579d8dSLorenzo Stoakes unsigned int non_kernel;
2774579d8dSLorenzo Stoakes unsigned long nr_allocated;
2874579d8dSLorenzo Stoakes unsigned long nr_tallocated;
29*617f8e4dSSidhartha Kumar bool exec_callback;
30*617f8e4dSSidhartha Kumar void (*callback)(void *);
31*617f8e4dSSidhartha Kumar void *private;
3274579d8dSLorenzo Stoakes };
3374579d8dSLorenzo Stoakes
kmem_cache_set_callback(struct kmem_cache * cachep,void (* callback)(void *))34*617f8e4dSSidhartha Kumar void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *))
35*617f8e4dSSidhartha Kumar {
36*617f8e4dSSidhartha Kumar cachep->callback = callback;
37*617f8e4dSSidhartha Kumar }
38*617f8e4dSSidhartha Kumar
kmem_cache_set_private(struct kmem_cache * cachep,void * private)39*617f8e4dSSidhartha Kumar void kmem_cache_set_private(struct kmem_cache *cachep, void *private)
40*617f8e4dSSidhartha Kumar {
41*617f8e4dSSidhartha Kumar cachep->private = private;
42*617f8e4dSSidhartha Kumar }
43*617f8e4dSSidhartha Kumar
kmem_cache_set_non_kernel(struct kmem_cache * cachep,unsigned int val)4474579d8dSLorenzo Stoakes void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val)
4574579d8dSLorenzo Stoakes {
4674579d8dSLorenzo Stoakes cachep->non_kernel = val;
4774579d8dSLorenzo Stoakes }
4874579d8dSLorenzo Stoakes
kmem_cache_get_alloc(struct kmem_cache * cachep)4974579d8dSLorenzo Stoakes unsigned long kmem_cache_get_alloc(struct kmem_cache *cachep)
5074579d8dSLorenzo Stoakes {
5174579d8dSLorenzo Stoakes return cachep->size * cachep->nr_allocated;
5274579d8dSLorenzo Stoakes }
5374579d8dSLorenzo Stoakes
kmem_cache_nr_allocated(struct kmem_cache * cachep)5474579d8dSLorenzo Stoakes unsigned long kmem_cache_nr_allocated(struct kmem_cache *cachep)
5574579d8dSLorenzo Stoakes {
5674579d8dSLorenzo Stoakes return cachep->nr_allocated;
5774579d8dSLorenzo Stoakes }
5874579d8dSLorenzo Stoakes
kmem_cache_nr_tallocated(struct kmem_cache * cachep)5974579d8dSLorenzo Stoakes unsigned long kmem_cache_nr_tallocated(struct kmem_cache *cachep)
6074579d8dSLorenzo Stoakes {
6174579d8dSLorenzo Stoakes return cachep->nr_tallocated;
6274579d8dSLorenzo Stoakes }
6374579d8dSLorenzo Stoakes
kmem_cache_zero_nr_tallocated(struct kmem_cache * cachep)6474579d8dSLorenzo Stoakes void kmem_cache_zero_nr_tallocated(struct kmem_cache *cachep)
6574579d8dSLorenzo Stoakes {
6674579d8dSLorenzo Stoakes cachep->nr_tallocated = 0;
6774579d8dSLorenzo Stoakes }
6874579d8dSLorenzo Stoakes
kmem_cache_alloc_lru(struct kmem_cache * cachep,struct list_lru * lru,int gfp)6974579d8dSLorenzo Stoakes void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
7074579d8dSLorenzo Stoakes int gfp)
7174579d8dSLorenzo Stoakes {
7274579d8dSLorenzo Stoakes void *p;
7374579d8dSLorenzo Stoakes
74*617f8e4dSSidhartha Kumar if (cachep->exec_callback) {
75*617f8e4dSSidhartha Kumar if (cachep->callback)
76*617f8e4dSSidhartha Kumar cachep->callback(cachep->private);
77*617f8e4dSSidhartha Kumar cachep->exec_callback = false;
78*617f8e4dSSidhartha Kumar }
79*617f8e4dSSidhartha Kumar
8074579d8dSLorenzo Stoakes if (!(gfp & __GFP_DIRECT_RECLAIM)) {
81*617f8e4dSSidhartha Kumar if (!cachep->non_kernel) {
82*617f8e4dSSidhartha Kumar cachep->exec_callback = true;
8374579d8dSLorenzo Stoakes return NULL;
84*617f8e4dSSidhartha Kumar }
8574579d8dSLorenzo Stoakes
8674579d8dSLorenzo Stoakes cachep->non_kernel--;
8774579d8dSLorenzo Stoakes }
8874579d8dSLorenzo Stoakes
8974579d8dSLorenzo Stoakes pthread_mutex_lock(&cachep->lock);
9074579d8dSLorenzo Stoakes if (cachep->nr_objs) {
9174579d8dSLorenzo Stoakes struct radix_tree_node *node = cachep->objs;
9274579d8dSLorenzo Stoakes cachep->nr_objs--;
9374579d8dSLorenzo Stoakes cachep->objs = node->parent;
9474579d8dSLorenzo Stoakes pthread_mutex_unlock(&cachep->lock);
9574579d8dSLorenzo Stoakes node->parent = NULL;
9674579d8dSLorenzo Stoakes p = node;
9774579d8dSLorenzo Stoakes } else {
9874579d8dSLorenzo Stoakes pthread_mutex_unlock(&cachep->lock);
9974579d8dSLorenzo Stoakes if (cachep->align)
10074579d8dSLorenzo Stoakes posix_memalign(&p, cachep->align, cachep->size);
10174579d8dSLorenzo Stoakes else
10274579d8dSLorenzo Stoakes p = malloc(cachep->size);
10374579d8dSLorenzo Stoakes if (cachep->ctor)
10474579d8dSLorenzo Stoakes cachep->ctor(p);
10574579d8dSLorenzo Stoakes else if (gfp & __GFP_ZERO)
10674579d8dSLorenzo Stoakes memset(p, 0, cachep->size);
10774579d8dSLorenzo Stoakes }
10874579d8dSLorenzo Stoakes
10974579d8dSLorenzo Stoakes uatomic_inc(&cachep->nr_allocated);
11074579d8dSLorenzo Stoakes uatomic_inc(&nr_allocated);
11174579d8dSLorenzo Stoakes uatomic_inc(&cachep->nr_tallocated);
11274579d8dSLorenzo Stoakes if (kmalloc_verbose)
11374579d8dSLorenzo Stoakes printf("Allocating %p from slab\n", p);
11474579d8dSLorenzo Stoakes return p;
11574579d8dSLorenzo Stoakes }
11674579d8dSLorenzo Stoakes
__kmem_cache_free_locked(struct kmem_cache * cachep,void * objp)11774579d8dSLorenzo Stoakes void __kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
11874579d8dSLorenzo Stoakes {
11974579d8dSLorenzo Stoakes assert(objp);
12074579d8dSLorenzo Stoakes if (cachep->nr_objs > 10 || cachep->align) {
12174579d8dSLorenzo Stoakes memset(objp, POISON_FREE, cachep->size);
12274579d8dSLorenzo Stoakes free(objp);
12374579d8dSLorenzo Stoakes } else {
12474579d8dSLorenzo Stoakes struct radix_tree_node *node = objp;
12574579d8dSLorenzo Stoakes cachep->nr_objs++;
12674579d8dSLorenzo Stoakes node->parent = cachep->objs;
12774579d8dSLorenzo Stoakes cachep->objs = node;
12874579d8dSLorenzo Stoakes }
12974579d8dSLorenzo Stoakes }
13074579d8dSLorenzo Stoakes
kmem_cache_free_locked(struct kmem_cache * cachep,void * objp)13174579d8dSLorenzo Stoakes void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
13274579d8dSLorenzo Stoakes {
13374579d8dSLorenzo Stoakes uatomic_dec(&nr_allocated);
13474579d8dSLorenzo Stoakes uatomic_dec(&cachep->nr_allocated);
13574579d8dSLorenzo Stoakes if (kmalloc_verbose)
13674579d8dSLorenzo Stoakes printf("Freeing %p to slab\n", objp);
13774579d8dSLorenzo Stoakes __kmem_cache_free_locked(cachep, objp);
13874579d8dSLorenzo Stoakes }
13974579d8dSLorenzo Stoakes
kmem_cache_free(struct kmem_cache * cachep,void * objp)14074579d8dSLorenzo Stoakes void kmem_cache_free(struct kmem_cache *cachep, void *objp)
14174579d8dSLorenzo Stoakes {
14274579d8dSLorenzo Stoakes pthread_mutex_lock(&cachep->lock);
14374579d8dSLorenzo Stoakes kmem_cache_free_locked(cachep, objp);
14474579d8dSLorenzo Stoakes pthread_mutex_unlock(&cachep->lock);
14574579d8dSLorenzo Stoakes }
14674579d8dSLorenzo Stoakes
kmem_cache_free_bulk(struct kmem_cache * cachep,size_t size,void ** list)14774579d8dSLorenzo Stoakes void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
14874579d8dSLorenzo Stoakes {
14974579d8dSLorenzo Stoakes if (kmalloc_verbose)
15074579d8dSLorenzo Stoakes pr_debug("Bulk free %p[0-%lu]\n", list, size - 1);
15174579d8dSLorenzo Stoakes
15274579d8dSLorenzo Stoakes pthread_mutex_lock(&cachep->lock);
15374579d8dSLorenzo Stoakes for (int i = 0; i < size; i++)
15474579d8dSLorenzo Stoakes kmem_cache_free_locked(cachep, list[i]);
15574579d8dSLorenzo Stoakes pthread_mutex_unlock(&cachep->lock);
15674579d8dSLorenzo Stoakes }
15774579d8dSLorenzo Stoakes
kmem_cache_shrink(struct kmem_cache * cachep)15874579d8dSLorenzo Stoakes void kmem_cache_shrink(struct kmem_cache *cachep)
15974579d8dSLorenzo Stoakes {
16074579d8dSLorenzo Stoakes }
16174579d8dSLorenzo Stoakes
kmem_cache_alloc_bulk(struct kmem_cache * cachep,gfp_t gfp,size_t size,void ** p)16274579d8dSLorenzo Stoakes int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
16374579d8dSLorenzo Stoakes void **p)
16474579d8dSLorenzo Stoakes {
16574579d8dSLorenzo Stoakes size_t i;
16674579d8dSLorenzo Stoakes
16774579d8dSLorenzo Stoakes if (kmalloc_verbose)
16874579d8dSLorenzo Stoakes pr_debug("Bulk alloc %lu\n", size);
16974579d8dSLorenzo Stoakes
17074579d8dSLorenzo Stoakes pthread_mutex_lock(&cachep->lock);
17174579d8dSLorenzo Stoakes if (cachep->nr_objs >= size) {
17274579d8dSLorenzo Stoakes struct radix_tree_node *node;
17374579d8dSLorenzo Stoakes
17474579d8dSLorenzo Stoakes for (i = 0; i < size; i++) {
17574579d8dSLorenzo Stoakes if (!(gfp & __GFP_DIRECT_RECLAIM)) {
17674579d8dSLorenzo Stoakes if (!cachep->non_kernel)
17774579d8dSLorenzo Stoakes break;
17874579d8dSLorenzo Stoakes cachep->non_kernel--;
17974579d8dSLorenzo Stoakes }
18074579d8dSLorenzo Stoakes
18174579d8dSLorenzo Stoakes node = cachep->objs;
18274579d8dSLorenzo Stoakes cachep->nr_objs--;
18374579d8dSLorenzo Stoakes cachep->objs = node->parent;
18474579d8dSLorenzo Stoakes p[i] = node;
18574579d8dSLorenzo Stoakes node->parent = NULL;
18674579d8dSLorenzo Stoakes }
18774579d8dSLorenzo Stoakes pthread_mutex_unlock(&cachep->lock);
18874579d8dSLorenzo Stoakes } else {
18974579d8dSLorenzo Stoakes pthread_mutex_unlock(&cachep->lock);
19074579d8dSLorenzo Stoakes for (i = 0; i < size; i++) {
19174579d8dSLorenzo Stoakes if (!(gfp & __GFP_DIRECT_RECLAIM)) {
19274579d8dSLorenzo Stoakes if (!cachep->non_kernel)
19374579d8dSLorenzo Stoakes break;
19474579d8dSLorenzo Stoakes cachep->non_kernel--;
19574579d8dSLorenzo Stoakes }
19674579d8dSLorenzo Stoakes
19774579d8dSLorenzo Stoakes if (cachep->align) {
19874579d8dSLorenzo Stoakes posix_memalign(&p[i], cachep->align,
19974579d8dSLorenzo Stoakes cachep->size);
20074579d8dSLorenzo Stoakes } else {
20174579d8dSLorenzo Stoakes p[i] = malloc(cachep->size);
20274579d8dSLorenzo Stoakes if (!p[i])
20374579d8dSLorenzo Stoakes break;
20474579d8dSLorenzo Stoakes }
20574579d8dSLorenzo Stoakes if (cachep->ctor)
20674579d8dSLorenzo Stoakes cachep->ctor(p[i]);
20774579d8dSLorenzo Stoakes else if (gfp & __GFP_ZERO)
20874579d8dSLorenzo Stoakes memset(p[i], 0, cachep->size);
20974579d8dSLorenzo Stoakes }
21074579d8dSLorenzo Stoakes }
21174579d8dSLorenzo Stoakes
21274579d8dSLorenzo Stoakes if (i < size) {
21374579d8dSLorenzo Stoakes size = i;
21474579d8dSLorenzo Stoakes pthread_mutex_lock(&cachep->lock);
21574579d8dSLorenzo Stoakes for (i = 0; i < size; i++)
21674579d8dSLorenzo Stoakes __kmem_cache_free_locked(cachep, p[i]);
21774579d8dSLorenzo Stoakes pthread_mutex_unlock(&cachep->lock);
21874579d8dSLorenzo Stoakes return 0;
21974579d8dSLorenzo Stoakes }
22074579d8dSLorenzo Stoakes
22174579d8dSLorenzo Stoakes for (i = 0; i < size; i++) {
22274579d8dSLorenzo Stoakes uatomic_inc(&nr_allocated);
22374579d8dSLorenzo Stoakes uatomic_inc(&cachep->nr_allocated);
22474579d8dSLorenzo Stoakes uatomic_inc(&cachep->nr_tallocated);
22574579d8dSLorenzo Stoakes if (kmalloc_verbose)
22674579d8dSLorenzo Stoakes printf("Allocating %p from slab\n", p[i]);
22774579d8dSLorenzo Stoakes }
22874579d8dSLorenzo Stoakes
22974579d8dSLorenzo Stoakes return size;
23074579d8dSLorenzo Stoakes }
23174579d8dSLorenzo Stoakes
23274579d8dSLorenzo Stoakes struct kmem_cache *
kmem_cache_create(const char * name,unsigned int size,unsigned int align,unsigned int flags,void (* ctor)(void *))23374579d8dSLorenzo Stoakes kmem_cache_create(const char *name, unsigned int size, unsigned int align,
23474579d8dSLorenzo Stoakes unsigned int flags, void (*ctor)(void *))
23574579d8dSLorenzo Stoakes {
23674579d8dSLorenzo Stoakes struct kmem_cache *ret = malloc(sizeof(*ret));
23774579d8dSLorenzo Stoakes
23874579d8dSLorenzo Stoakes pthread_mutex_init(&ret->lock, NULL);
23974579d8dSLorenzo Stoakes ret->size = size;
24074579d8dSLorenzo Stoakes ret->align = align;
24174579d8dSLorenzo Stoakes ret->nr_objs = 0;
24274579d8dSLorenzo Stoakes ret->nr_allocated = 0;
24374579d8dSLorenzo Stoakes ret->nr_tallocated = 0;
24474579d8dSLorenzo Stoakes ret->objs = NULL;
24574579d8dSLorenzo Stoakes ret->ctor = ctor;
24674579d8dSLorenzo Stoakes ret->non_kernel = 0;
247*617f8e4dSSidhartha Kumar ret->exec_callback = false;
248*617f8e4dSSidhartha Kumar ret->callback = NULL;
249*617f8e4dSSidhartha Kumar ret->private = NULL;
25074579d8dSLorenzo Stoakes return ret;
25174579d8dSLorenzo Stoakes }
25274579d8dSLorenzo Stoakes
25374579d8dSLorenzo Stoakes /*
25474579d8dSLorenzo Stoakes * Test the test infrastructure for kem_cache_alloc/free and bulk counterparts.
25574579d8dSLorenzo Stoakes */
test_kmem_cache_bulk(void)25674579d8dSLorenzo Stoakes void test_kmem_cache_bulk(void)
25774579d8dSLorenzo Stoakes {
25874579d8dSLorenzo Stoakes int i;
25974579d8dSLorenzo Stoakes void *list[12];
26074579d8dSLorenzo Stoakes static struct kmem_cache *test_cache, *test_cache2;
26174579d8dSLorenzo Stoakes
26274579d8dSLorenzo Stoakes /*
26374579d8dSLorenzo Stoakes * Testing the bulk allocators without aligned kmem_cache to force the
26474579d8dSLorenzo Stoakes * bulk alloc/free to reuse
26574579d8dSLorenzo Stoakes */
26674579d8dSLorenzo Stoakes test_cache = kmem_cache_create("test_cache", 256, 0, SLAB_PANIC, NULL);
26774579d8dSLorenzo Stoakes
26874579d8dSLorenzo Stoakes for (i = 0; i < 5; i++)
26974579d8dSLorenzo Stoakes list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
27074579d8dSLorenzo Stoakes
27174579d8dSLorenzo Stoakes for (i = 0; i < 5; i++)
27274579d8dSLorenzo Stoakes kmem_cache_free(test_cache, list[i]);
27374579d8dSLorenzo Stoakes assert(test_cache->nr_objs == 5);
27474579d8dSLorenzo Stoakes
27574579d8dSLorenzo Stoakes kmem_cache_alloc_bulk(test_cache, __GFP_DIRECT_RECLAIM, 5, list);
27674579d8dSLorenzo Stoakes kmem_cache_free_bulk(test_cache, 5, list);
27774579d8dSLorenzo Stoakes
27874579d8dSLorenzo Stoakes for (i = 0; i < 12 ; i++)
27974579d8dSLorenzo Stoakes list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
28074579d8dSLorenzo Stoakes
28174579d8dSLorenzo Stoakes for (i = 0; i < 12; i++)
28274579d8dSLorenzo Stoakes kmem_cache_free(test_cache, list[i]);
28374579d8dSLorenzo Stoakes
28474579d8dSLorenzo Stoakes /* The last free will not be kept around */
28574579d8dSLorenzo Stoakes assert(test_cache->nr_objs == 11);
28674579d8dSLorenzo Stoakes
28774579d8dSLorenzo Stoakes /* Aligned caches will immediately free */
28874579d8dSLorenzo Stoakes test_cache2 = kmem_cache_create("test_cache2", 128, 128, SLAB_PANIC, NULL);
28974579d8dSLorenzo Stoakes
29074579d8dSLorenzo Stoakes kmem_cache_alloc_bulk(test_cache2, __GFP_DIRECT_RECLAIM, 10, list);
29174579d8dSLorenzo Stoakes kmem_cache_free_bulk(test_cache2, 10, list);
29274579d8dSLorenzo Stoakes assert(!test_cache2->nr_objs);
29374579d8dSLorenzo Stoakes
29474579d8dSLorenzo Stoakes
29574579d8dSLorenzo Stoakes }
296