[IMPROVE] create slot_manager
[kernel/swap-modules.git] / kprobe / dbi_insn_slots.c
1 /*
2  *  Kernel Probes (KProbes)
3  *  kernel/kprobes.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) IBM Corporation, 2002, 2004
20  */
21
22 /*
23  *  Dynamic Binary Instrumentation Module based on KProbes
24  *  modules/kprobe/dbi_insn_slots.c
25  *
26  * This program is free software; you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation; either version 2 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program; if not, write to the Free Software
38  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39  *
40  * Copyright (C) Samsung Electronics, 2006-2012
41  *
42  * 2008-2009    Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
43  *              Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
44  * 2010         Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
45  * 2012-2013    Vyacheslav Cherkashin <v.cherkashin@samsung.com> new memory allocator for slots
46  */
47
48 #include "dbi_insn_slots.h"
49 #include <linux/module.h>
50 #include <linux/rculist.h>
51 #include <linux/slab.h>
52 #include <linux/spinlock.h>
53
54 struct chunk {
55         unsigned long *data;
56         unsigned long first_available;
57         unsigned long count_available;
58
59         spinlock_t    lock;
60         unsigned long size;
61         unsigned long *index;
62 };
63
64 struct fixed_alloc
65 {
66         struct hlist_node hlist;
67         struct chunk chunk;
68 };
69
70 static void chunk_init(struct chunk *chunk, void *data, size_t size, size_t size_block)
71 {
72         unsigned long i;
73         unsigned long *p;
74
75         spin_lock_init(&chunk->lock);
76         chunk->data = (unsigned long *)data;
77         chunk->first_available = 0;
78         chunk->count_available = size / size_block;
79         chunk->size = chunk->count_available;
80
81         chunk->index = kmalloc(sizeof(*chunk->index)*chunk->count_available, GFP_ATOMIC);
82
83         p = chunk->index;
84         for (i = 0; i != chunk->count_available; ++p) {
85                 *p = ++i;
86         }
87 }
88
89 static void chunk_uninit(struct chunk *chunk)
90 {
91         kfree(chunk->index);
92 }
93
94 static void* chunk_allocate(struct chunk *chunk, size_t size_block)
95 {
96         unsigned long *ret;
97
98         if (!chunk->count_available) {
99                 return NULL;
100         }
101
102         spin_lock(&chunk->lock);
103         ret = chunk->data + chunk->first_available*size_block;
104         chunk->first_available = chunk->index[chunk->first_available];
105         --chunk->count_available;
106         spin_unlock(&chunk->lock);
107
108         return ret;
109 }
110
111 static void chunk_deallocate(struct chunk *chunk, void *p, size_t size_block)
112 {
113         unsigned long idx = ((unsigned long *)p - chunk->data)/size_block;
114
115         spin_lock(&chunk->lock);
116         chunk->index[idx] = chunk->first_available;
117         chunk->first_available = idx;
118         ++chunk->count_available;
119         spin_unlock(&chunk->lock);
120 }
121
122 static inline int chunk_check_ptr(struct chunk *chunk, void *p, size_t size)
123 {
124         if (( chunk->data                             <= (unsigned long *)p) &&
125             ((chunk->data + size/sizeof(chunk->data))  > (unsigned long *)p)) {
126                 return 1;
127         }
128
129         return 0;
130 }
131
132 static inline int chunk_free(struct chunk *chunk)
133 {
134         return (chunk->count_available == chunk->size);
135 }
136
137 static struct fixed_alloc *create_fixed_alloc(struct slot_manager *sm)
138 {
139         void *data;
140         struct fixed_alloc *fa;
141
142         fa = kmalloc(sizeof(*fa), GFP_ATOMIC);
143         if (fa == NULL) {
144                 return NULL;
145         }
146
147         data = sm->alloc(sm);
148         if(data == NULL) {
149                 kfree(fa);
150                 return NULL;
151         }
152
153         chunk_init(&fa->chunk, data, PAGE_SIZE/sizeof(unsigned long), sm->slot_size);
154
155         return fa;
156 }
157
158 static void free_fixed_alloc(struct slot_manager *sm, struct fixed_alloc *fa)
159 {
160         chunk_uninit(&fa->chunk);
161         sm->free(sm, fa->chunk.data);
162         kfree(fa);
163 }
164
165
166 void *alloc_insn_slot(struct slot_manager *sm)
167 {
168         void *free_slot;
169         struct fixed_alloc *fa;
170         struct hlist_node *pos;
171
172         hlist_for_each_entry_rcu(fa, pos, &sm->page_list, hlist) {
173                 free_slot = chunk_allocate(&fa->chunk, sm->slot_size);
174                 if (free_slot)
175                         return free_slot;
176         }
177
178         fa = create_fixed_alloc(sm);
179         if(fa == NULL)
180                 return NULL;
181
182         INIT_HLIST_NODE(&fa->hlist);
183         hlist_add_head_rcu(&fa->hlist, &sm->page_list);
184
185         return chunk_allocate(&fa->chunk, sm->slot_size);
186 }
187 EXPORT_SYMBOL_GPL(alloc_insn_slot);
188
189 void free_insn_slot(struct slot_manager *sm, void *slot)
190 {
191         struct fixed_alloc *fa;
192         struct hlist_node *pos;
193
194         hlist_for_each_entry_rcu(fa, pos, &sm->page_list, hlist) {
195                 if (!chunk_check_ptr(&fa->chunk, slot, PAGE_SIZE))
196                         continue;
197
198                 chunk_deallocate(&fa->chunk, slot, sm->slot_size);
199
200                 if (chunk_free(&fa->chunk)) {
201                         hlist_del_rcu(&fa->hlist);
202                         free_fixed_alloc(sm, fa);
203                 }
204
205                 return;
206         }
207
208         panic("free_insn_slot: slot=%p is not data base\n", slot);
209 }
210 EXPORT_SYMBOL_GPL(free_insn_slot);