blob: 6903ccf35595f560350a26d03e3eb4369fbb128b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Matthew Wilcox1366c372016-03-17 14:21:45 -07002#include <stdlib.h>
3#include <string.h>
4#include <malloc.h>
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -08005#include <pthread.h>
Matthew Wilcox1366c372016-03-17 14:21:45 -07006#include <unistd.h>
7#include <assert.h>
8
Matthew Wilcox12ea6532016-12-16 14:53:45 -05009#include <linux/gfp.h>
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080010#include <linux/poison.h>
Matthew Wilcox1366c372016-03-17 14:21:45 -070011#include <linux/slab.h>
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080012#include <linux/radix-tree.h>
Matthew Wilcox1366c372016-03-17 14:21:45 -070013#include <urcu/uatomic.h>
14
15int nr_allocated;
Matthew Wilcox847d3572016-12-14 15:08:02 -080016int preempt_count;
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050017int kmalloc_verbose;
Rehas Sachdeva73bc0292017-01-04 11:55:00 -050018int test_verbose;
Matthew Wilcox1366c372016-03-17 14:21:45 -070019
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080020struct kmem_cache {
21 pthread_mutex_t lock;
22 int size;
23 int nr_objs;
24 void *objs;
25 void (*ctor)(void *);
26};
27
Matthew Wilcox1366c372016-03-17 14:21:45 -070028void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
29{
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080030 struct radix_tree_node *node;
Matthew Wilcox31023cd2016-12-14 15:07:59 -080031
32 if (flags & __GFP_NOWARN)
33 return NULL;
34
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080035 pthread_mutex_lock(&cachep->lock);
36 if (cachep->nr_objs) {
37 cachep->nr_objs--;
38 node = cachep->objs;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050039 cachep->objs = node->parent;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080040 pthread_mutex_unlock(&cachep->lock);
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050041 node->parent = NULL;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080042 } else {
43 pthread_mutex_unlock(&cachep->lock);
44 node = malloc(cachep->size);
45 if (cachep->ctor)
46 cachep->ctor(node);
47 }
48
Matthew Wilcox1366c372016-03-17 14:21:45 -070049 uatomic_inc(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050050 if (kmalloc_verbose)
51 printf("Allocating %p from slab\n", node);
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080052 return node;
Matthew Wilcox1366c372016-03-17 14:21:45 -070053}
54
55void kmem_cache_free(struct kmem_cache *cachep, void *objp)
56{
57 assert(objp);
58 uatomic_dec(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050059 if (kmalloc_verbose)
60 printf("Freeing %p to slab\n", objp);
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080061 pthread_mutex_lock(&cachep->lock);
62 if (cachep->nr_objs > 10) {
63 memset(objp, POISON_FREE, cachep->size);
64 free(objp);
65 } else {
66 struct radix_tree_node *node = objp;
67 cachep->nr_objs++;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050068 node->parent = cachep->objs;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080069 cachep->objs = node;
70 }
71 pthread_mutex_unlock(&cachep->lock);
Matthew Wilcox1366c372016-03-17 14:21:45 -070072}
73
Matthew Wilcoxde1af8f2016-12-14 15:09:25 -080074void *kmalloc(size_t size, gfp_t gfp)
75{
76 void *ret = malloc(size);
77 uatomic_inc(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050078 if (kmalloc_verbose)
79 printf("Allocating %p from malloc\n", ret);
Matthew Wilcoxde1af8f2016-12-14 15:09:25 -080080 return ret;
81}
82
83void kfree(void *p)
84{
85 if (!p)
86 return;
87 uatomic_dec(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050088 if (kmalloc_verbose)
89 printf("Freeing %p to malloc\n", p);
Matthew Wilcoxde1af8f2016-12-14 15:09:25 -080090 free(p);
91}
92
Matthew Wilcox1366c372016-03-17 14:21:45 -070093struct kmem_cache *
94kmem_cache_create(const char *name, size_t size, size_t offset,
95 unsigned long flags, void (*ctor)(void *))
96{
97 struct kmem_cache *ret = malloc(sizeof(*ret));
98
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080099 pthread_mutex_init(&ret->lock, NULL);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700100 ret->size = size;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -0800101 ret->nr_objs = 0;
102 ret->objs = NULL;
Matthew Wilcox1366c372016-03-17 14:21:45 -0700103 ret->ctor = ctor;
104 return ret;
105}