summaryrefslogtreecommitdiff
path: root/kprobe/swap_slots.c
blob: e1d7e4a02a644a352786f9a7edaecf3ad7dfb7f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *  Kernel Probes (KProbes)
 *  kernel/kprobes.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) IBM Corporation, 2002, 2004
 */

/*
 *  Dynamic Binary Instrumentation Module based on KProbes
 *  modules/kprobe/swap_slots.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) Samsung Electronics, 2006-2012
 *
 * 2008-2009    Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
 *              Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
 * 2010         Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
 * 2012-2013    Vyacheslav Cherkashin <v.cherkashin@samsung.com> new memory allocator for slots
 */


#include <linux/module.h>
#include <linux/rculist.h>
#include <linux/slab.h>
#include <linux/spinlock.h>

#include "swap_slots.h"
#include "dbi_kprobes_deps.h"


struct chunk {
	unsigned long *data;
	unsigned long first_available;
	unsigned long count_available;

	spinlock_t    lock;
	unsigned long size;
	unsigned long *index;
};

struct fixed_alloc
{
	struct hlist_node hlist;
	struct chunk chunk;
};

static void chunk_init(struct chunk *chunk, void *data, size_t size, size_t size_block)
{
	unsigned long i;
	unsigned long *p;

	spin_lock_init(&chunk->lock);
	chunk->data = (unsigned long *)data;
	chunk->first_available = 0;
	chunk->count_available = size / size_block;
	chunk->size = chunk->count_available;

	chunk->index = kmalloc(sizeof(*chunk->index)*chunk->count_available, GFP_ATOMIC);

	p = chunk->index;
	for (i = 0; i != chunk->count_available; ++p) {
		*p = ++i;
	}
}

static void chunk_uninit(struct chunk *chunk)
{
	kfree(chunk->index);
}

static void* chunk_allocate(struct chunk *chunk, size_t size_block)
{
	unsigned long *ret;

	if (!chunk->count_available) {
		return NULL;
	}

	spin_lock(&chunk->lock);
	ret = chunk->data + chunk->first_available*size_block;
	chunk->first_available = chunk->index[chunk->first_available];
	--chunk->count_available;
	spin_unlock(&chunk->lock);

	return ret;
}

static void chunk_deallocate(struct chunk *chunk, void *p, size_t size_block)
{
	unsigned long idx = ((unsigned long *)p - chunk->data)/size_block;

	spin_lock(&chunk->lock);
	chunk->index[idx] = chunk->first_available;
	chunk->first_available = idx;
	++chunk->count_available;
	spin_unlock(&chunk->lock);
}

static inline int chunk_check_ptr(struct chunk *chunk, void *p, size_t size)
{
	if (( chunk->data                             <= (unsigned long *)p) &&
	    ((chunk->data + size/sizeof(chunk->data))  > (unsigned long *)p)) {
		return 1;
	}

	return 0;
}

static inline int chunk_free(struct chunk *chunk)
{
	return (chunk->count_available == chunk->size);
}

static struct fixed_alloc *create_fixed_alloc(struct slot_manager *sm)
{
	void *data;
	struct fixed_alloc *fa;

	fa = kmalloc(sizeof(*fa), GFP_ATOMIC);
	if (fa == NULL) {
		return NULL;
	}

	data = sm->alloc(sm);
	if(data == NULL) {
		kfree(fa);
		return NULL;
	}

	chunk_init(&fa->chunk, data, PAGE_SIZE/sizeof(unsigned long), sm->slot_size);

	return fa;
}

static void free_fixed_alloc(struct slot_manager *sm, struct fixed_alloc *fa)
{
	chunk_uninit(&fa->chunk);
	sm->free(sm, fa->chunk.data);
	kfree(fa);
}


void *swap_slot_alloc(struct slot_manager *sm)
{
	void *free_slot;
	struct fixed_alloc *fa;
	DECLARE_NODE_PTR_FOR_HLIST(pos);

	swap_hlist_for_each_entry_rcu(fa, pos, &sm->page_list, hlist) {
		free_slot = chunk_allocate(&fa->chunk, sm->slot_size);
		if (free_slot)
			return free_slot;
	}

	fa = create_fixed_alloc(sm);
	if(fa == NULL)
		return NULL;

	INIT_HLIST_NODE(&fa->hlist);
	hlist_add_head_rcu(&fa->hlist, &sm->page_list);

	return chunk_allocate(&fa->chunk, sm->slot_size);
}
EXPORT_SYMBOL_GPL(swap_slot_alloc);

void swap_slot_free(struct slot_manager *sm, void *slot)
{
	struct fixed_alloc *fa;
	DECLARE_NODE_PTR_FOR_HLIST(pos);

	if (slot == NULL)
		return;

	swap_hlist_for_each_entry_rcu(fa, pos, &sm->page_list, hlist) {
		if (!chunk_check_ptr(&fa->chunk, slot, PAGE_SIZE))
			continue;

		chunk_deallocate(&fa->chunk, slot, sm->slot_size);

		if (chunk_free(&fa->chunk)) {
			hlist_del_rcu(&fa->hlist);
			free_fixed_alloc(sm, fa);
		}

		return;
	}

	panic("%s: slot=%p is not data base\n", __func__, slot);
}
EXPORT_SYMBOL_GPL(swap_slot_free);