]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Kernel Probes (KProbes) | |
4 | * kernel/kprobes.c | |
5 | * | |
1da177e4 LT |
6 | * Copyright (C) IBM Corporation, 2002, 2004 |
7 | * | |
8 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel | |
9 | * Probes initial implementation (includes suggestions from | |
10 | * Rusty Russell). | |
11 | * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with | |
12 | * hlists and exceptions notifier as suggested by Andi Kleen. | |
13 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes | |
14 | * interface to access function arguments. | |
15 | * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes | |
16 | * exceptions notifier to be first on the priority list. | |
b94cce92 HN |
17 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
18 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi | |
19 | * <prasanna@in.ibm.com> added function-return probes. | |
1da177e4 LT |
20 | */ |
21 | #include <linux/kprobes.h> | |
1da177e4 LT |
22 | #include <linux/hash.h> |
23 | #include <linux/init.h> | |
4e57b681 | 24 | #include <linux/slab.h> |
e3869792 | 25 | #include <linux/stddef.h> |
9984de1a | 26 | #include <linux/export.h> |
9ec4b1f3 | 27 | #include <linux/moduleloader.h> |
3a872d89 | 28 | #include <linux/kallsyms.h> |
b4c6c34a | 29 | #include <linux/freezer.h> |
346fd59b SD |
30 | #include <linux/seq_file.h> |
31 | #include <linux/debugfs.h> | |
b2be84df | 32 | #include <linux/sysctl.h> |
1eeb66a1 | 33 | #include <linux/kdebug.h> |
4460fdad | 34 | #include <linux/memory.h> |
4554dbcb | 35 | #include <linux/ftrace.h> |
afd66255 | 36 | #include <linux/cpu.h> |
bf5438fc | 37 | #include <linux/jump_label.h> |
bf8f6e5b | 38 | |
bfd45be0 | 39 | #include <asm/sections.h> |
1da177e4 LT |
40 | #include <asm/cacheflush.h> |
41 | #include <asm/errno.h> | |
7c0f6ba6 | 42 | #include <linux/uaccess.h> |
1da177e4 LT |
43 | |
44 | #define KPROBE_HASH_BITS 6 | |
45 | #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) | |
46 | ||
3a872d89 | 47 | |
ef53d9c5 | 48 | static int kprobes_initialized; |
7e6a71d8 MH |
49 | /* kprobe_table can be accessed by |
50 | * - Normal hlist traversal and RCU add/del under kprobe_mutex is held. | |
51 | * Or | |
52 | * - RCU hlist traversal under disabling preempt (breakpoint handlers) | |
53 | */ | |
1da177e4 | 54 | static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; |
b94cce92 | 55 | static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; |
1da177e4 | 56 | |
bf8f6e5b | 57 | /* NOTE: change this value only with kprobe_mutex held */ |
e579abeb | 58 | static bool kprobes_all_disarmed; |
bf8f6e5b | 59 | |
43948f50 MH |
60 | /* This protects kprobe_table and optimizing_list */ |
61 | static DEFINE_MUTEX(kprobe_mutex); | |
e6584523 | 62 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; |
ef53d9c5 | 63 | static struct { |
ec484608 | 64 | raw_spinlock_t lock ____cacheline_aligned_in_smp; |
ef53d9c5 S |
65 | } kretprobe_table_locks[KPROBE_TABLE_SIZE]; |
66 | ||
290e3070 NR |
67 | kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, |
68 | unsigned int __unused) | |
49e0b465 NR |
69 | { |
70 | return ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); | |
71 | } | |
72 | ||
ec484608 | 73 | static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash) |
ef53d9c5 S |
74 | { |
75 | return &(kretprobe_table_locks[hash].lock); | |
76 | } | |
1da177e4 | 77 | |
376e2424 MH |
78 | /* Blacklist -- list of struct kprobe_blacklist_entry */ |
79 | static LIST_HEAD(kprobe_blacklist); | |
3d8d996e | 80 | |
2d14e39d | 81 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
9ec4b1f3 AM |
82 | /* |
83 | * kprobe->ainsn.insn points to the copy of the instruction to be | |
84 | * single-stepped. x86_64, POWER4 and above have no-exec support and | |
85 | * stepping on the instruction on a vmalloced/kmalloced/data page | |
86 | * is a recipe for disaster | |
87 | */ | |
9ec4b1f3 | 88 | struct kprobe_insn_page { |
c5cb5a2d | 89 | struct list_head list; |
9ec4b1f3 | 90 | kprobe_opcode_t *insns; /* Page of instruction slots */ |
af96397d | 91 | struct kprobe_insn_cache *cache; |
9ec4b1f3 | 92 | int nused; |
b4c6c34a | 93 | int ngarbage; |
4610ee1d | 94 | char slot_used[]; |
9ec4b1f3 AM |
95 | }; |
96 | ||
4610ee1d MH |
97 | #define KPROBE_INSN_PAGE_SIZE(slots) \ |
98 | (offsetof(struct kprobe_insn_page, slot_used) + \ | |
99 | (sizeof(char) * (slots))) | |
100 | ||
4610ee1d MH |
101 | static int slots_per_page(struct kprobe_insn_cache *c) |
102 | { | |
103 | return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t)); | |
104 | } | |
105 | ||
ab40c5c6 MH |
106 | enum kprobe_slot_state { |
107 | SLOT_CLEAN = 0, | |
108 | SLOT_DIRTY = 1, | |
109 | SLOT_USED = 2, | |
110 | }; | |
111 | ||
63fef14f | 112 | void __weak *alloc_insn_page(void) |
af96397d HC |
113 | { |
114 | return module_alloc(PAGE_SIZE); | |
115 | } | |
116 | ||
c93f5cf5 | 117 | void __weak free_insn_page(void *page) |
af96397d | 118 | { |
be1f221c | 119 | module_memfree(page); |
af96397d HC |
120 | } |
121 | ||
c802d64a HC |
122 | struct kprobe_insn_cache kprobe_insn_slots = { |
123 | .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex), | |
af96397d HC |
124 | .alloc = alloc_insn_page, |
125 | .free = free_insn_page, | |
4610ee1d MH |
126 | .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages), |
127 | .insn_size = MAX_INSN_SIZE, | |
128 | .nr_garbage = 0, | |
129 | }; | |
55479f64 | 130 | static int collect_garbage_slots(struct kprobe_insn_cache *c); |
b4c6c34a | 131 | |
9ec4b1f3 | 132 | /** |
12941560 | 133 | * __get_insn_slot() - Find a slot on an executable page for an instruction. |
9ec4b1f3 AM |
134 | * We allocate an executable page if there's no room on existing ones. |
135 | */ | |
55479f64 | 136 | kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c) |
9ec4b1f3 AM |
137 | { |
138 | struct kprobe_insn_page *kip; | |
c802d64a | 139 | kprobe_opcode_t *slot = NULL; |
9ec4b1f3 | 140 | |
5b485629 | 141 | /* Since the slot array is not protected by rcu, we need a mutex */ |
c802d64a | 142 | mutex_lock(&c->mutex); |
6f716acd | 143 | retry: |
5b485629 MH |
144 | rcu_read_lock(); |
145 | list_for_each_entry_rcu(kip, &c->pages, list) { | |
4610ee1d | 146 | if (kip->nused < slots_per_page(c)) { |
9ec4b1f3 | 147 | int i; |
4610ee1d | 148 | for (i = 0; i < slots_per_page(c); i++) { |
ab40c5c6 MH |
149 | if (kip->slot_used[i] == SLOT_CLEAN) { |
150 | kip->slot_used[i] = SLOT_USED; | |
9ec4b1f3 | 151 | kip->nused++; |
c802d64a | 152 | slot = kip->insns + (i * c->insn_size); |
5b485629 | 153 | rcu_read_unlock(); |
c802d64a | 154 | goto out; |
9ec4b1f3 AM |
155 | } |
156 | } | |
4610ee1d MH |
157 | /* kip->nused is broken. Fix it. */ |
158 | kip->nused = slots_per_page(c); | |
159 | WARN_ON(1); | |
9ec4b1f3 AM |
160 | } |
161 | } | |
5b485629 | 162 | rcu_read_unlock(); |
9ec4b1f3 | 163 | |
b4c6c34a | 164 | /* If there are any garbage slots, collect it and try again. */ |
4610ee1d | 165 | if (c->nr_garbage && collect_garbage_slots(c) == 0) |
b4c6c34a | 166 | goto retry; |
4610ee1d MH |
167 | |
168 | /* All out of space. Need to allocate a new page. */ | |
169 | kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL); | |
6f716acd | 170 | if (!kip) |
c802d64a | 171 | goto out; |
9ec4b1f3 AM |
172 | |
173 | /* | |
174 | * Use module_alloc so this page is within +/- 2GB of where the | |
175 | * kernel image and loaded module images reside. This is required | |
176 | * so x86_64 can correctly handle the %rip-relative fixups. | |
177 | */ | |
af96397d | 178 | kip->insns = c->alloc(); |
9ec4b1f3 AM |
179 | if (!kip->insns) { |
180 | kfree(kip); | |
c802d64a | 181 | goto out; |
9ec4b1f3 | 182 | } |
c5cb5a2d | 183 | INIT_LIST_HEAD(&kip->list); |
4610ee1d | 184 | memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c)); |
ab40c5c6 | 185 | kip->slot_used[0] = SLOT_USED; |
9ec4b1f3 | 186 | kip->nused = 1; |
b4c6c34a | 187 | kip->ngarbage = 0; |
af96397d | 188 | kip->cache = c; |
5b485629 | 189 | list_add_rcu(&kip->list, &c->pages); |
c802d64a HC |
190 | slot = kip->insns; |
191 | out: | |
192 | mutex_unlock(&c->mutex); | |
193 | return slot; | |
12941560 MH |
194 | } |
195 | ||
b4c6c34a | 196 | /* Return 1 if all garbages are collected, otherwise 0. */ |
55479f64 | 197 | static int collect_one_slot(struct kprobe_insn_page *kip, int idx) |
b4c6c34a | 198 | { |
ab40c5c6 | 199 | kip->slot_used[idx] = SLOT_CLEAN; |
b4c6c34a MH |
200 | kip->nused--; |
201 | if (kip->nused == 0) { | |
202 | /* | |
203 | * Page is no longer in use. Free it unless | |
204 | * it's the last one. We keep the last one | |
205 | * so as not to have to set it up again the | |
206 | * next time somebody inserts a probe. | |
207 | */ | |
4610ee1d | 208 | if (!list_is_singular(&kip->list)) { |
5b485629 MH |
209 | list_del_rcu(&kip->list); |
210 | synchronize_rcu(); | |
af96397d | 211 | kip->cache->free(kip->insns); |
b4c6c34a MH |
212 | kfree(kip); |
213 | } | |
214 | return 1; | |
215 | } | |
216 | return 0; | |
217 | } | |
218 | ||
55479f64 | 219 | static int collect_garbage_slots(struct kprobe_insn_cache *c) |
b4c6c34a | 220 | { |
c5cb5a2d | 221 | struct kprobe_insn_page *kip, *next; |
b4c6c34a | 222 | |
615d0ebb | 223 | /* Ensure no-one is interrupted on the garbages */ |
ae8b7ce7 | 224 | synchronize_rcu(); |
b4c6c34a | 225 | |
4610ee1d | 226 | list_for_each_entry_safe(kip, next, &c->pages, list) { |
b4c6c34a | 227 | int i; |
b4c6c34a MH |
228 | if (kip->ngarbage == 0) |
229 | continue; | |
230 | kip->ngarbage = 0; /* we will collect all garbages */ | |
4610ee1d | 231 | for (i = 0; i < slots_per_page(c); i++) { |
5b485629 | 232 | if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i)) |
b4c6c34a MH |
233 | break; |
234 | } | |
235 | } | |
4610ee1d | 236 | c->nr_garbage = 0; |
b4c6c34a MH |
237 | return 0; |
238 | } | |
239 | ||
55479f64 MH |
240 | void __free_insn_slot(struct kprobe_insn_cache *c, |
241 | kprobe_opcode_t *slot, int dirty) | |
9ec4b1f3 AM |
242 | { |
243 | struct kprobe_insn_page *kip; | |
5b485629 | 244 | long idx; |
9ec4b1f3 | 245 | |
c802d64a | 246 | mutex_lock(&c->mutex); |
5b485629 MH |
247 | rcu_read_lock(); |
248 | list_for_each_entry_rcu(kip, &c->pages, list) { | |
249 | idx = ((long)slot - (long)kip->insns) / | |
250 | (c->insn_size * sizeof(kprobe_opcode_t)); | |
251 | if (idx >= 0 && idx < slots_per_page(c)) | |
c802d64a | 252 | goto out; |
9ec4b1f3 | 253 | } |
5b485629 | 254 | /* Could not find this slot. */ |
4610ee1d | 255 | WARN_ON(1); |
5b485629 | 256 | kip = NULL; |
c802d64a | 257 | out: |
5b485629 MH |
258 | rcu_read_unlock(); |
259 | /* Mark and sweep: this may sleep */ | |
260 | if (kip) { | |
261 | /* Check double free */ | |
262 | WARN_ON(kip->slot_used[idx] != SLOT_USED); | |
263 | if (dirty) { | |
264 | kip->slot_used[idx] = SLOT_DIRTY; | |
265 | kip->ngarbage++; | |
266 | if (++c->nr_garbage > slots_per_page(c)) | |
267 | collect_garbage_slots(c); | |
268 | } else { | |
269 | collect_one_slot(kip, idx); | |
270 | } | |
271 | } | |
c802d64a | 272 | mutex_unlock(&c->mutex); |
4610ee1d | 273 | } |
6f716acd | 274 | |
5b485629 MH |
275 | /* |
276 | * Check given address is on the page of kprobe instruction slots. | |
277 | * This will be used for checking whether the address on a stack | |
278 | * is on a text area or not. | |
279 | */ | |
280 | bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr) | |
281 | { | |
282 | struct kprobe_insn_page *kip; | |
283 | bool ret = false; | |
284 | ||
285 | rcu_read_lock(); | |
286 | list_for_each_entry_rcu(kip, &c->pages, list) { | |
287 | if (addr >= (unsigned long)kip->insns && | |
288 | addr < (unsigned long)kip->insns + PAGE_SIZE) { | |
289 | ret = true; | |
290 | break; | |
291 | } | |
292 | } | |
293 | rcu_read_unlock(); | |
294 | ||
295 | return ret; | |
296 | } | |
297 | ||
afd66255 MH |
298 | #ifdef CONFIG_OPTPROBES |
299 | /* For optimized_kprobe buffer */ | |
c802d64a HC |
300 | struct kprobe_insn_cache kprobe_optinsn_slots = { |
301 | .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex), | |
af96397d HC |
302 | .alloc = alloc_insn_page, |
303 | .free = free_insn_page, | |
afd66255 MH |
304 | .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages), |
305 | /* .insn_size is initialized later */ | |
306 | .nr_garbage = 0, | |
307 | }; | |
afd66255 | 308 | #endif |
2d14e39d | 309 | #endif |
9ec4b1f3 | 310 | |
e6584523 AM |
311 | /* We have preemption disabled.. so it is safe to use __ versions */ |
312 | static inline void set_kprobe_instance(struct kprobe *kp) | |
313 | { | |
b76834bc | 314 | __this_cpu_write(kprobe_instance, kp); |
e6584523 AM |
315 | } |
316 | ||
317 | static inline void reset_kprobe_instance(void) | |
318 | { | |
b76834bc | 319 | __this_cpu_write(kprobe_instance, NULL); |
e6584523 AM |
320 | } |
321 | ||
3516a460 AM |
322 | /* |
323 | * This routine is called either: | |
49a2a1b8 | 324 | * - under the kprobe_mutex - during kprobe_[un]register() |
3516a460 | 325 | * OR |
d217d545 | 326 | * - with preemption disabled - from arch/xxx/kernel/kprobes.c |
3516a460 | 327 | */ |
820aede0 | 328 | struct kprobe *get_kprobe(void *addr) |
1da177e4 LT |
329 | { |
330 | struct hlist_head *head; | |
3516a460 | 331 | struct kprobe *p; |
1da177e4 LT |
332 | |
333 | head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; | |
6743ad43 MH |
334 | hlist_for_each_entry_rcu(p, head, hlist, |
335 | lockdep_is_held(&kprobe_mutex)) { | |
1da177e4 LT |
336 | if (p->addr == addr) |
337 | return p; | |
338 | } | |
afd66255 | 339 | |
1da177e4 LT |
340 | return NULL; |
341 | } | |
820aede0 | 342 | NOKPROBE_SYMBOL(get_kprobe); |
1da177e4 | 343 | |
820aede0 | 344 | static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs); |
afd66255 MH |
345 | |
346 | /* Return true if the kprobe is an aggregator */ | |
347 | static inline int kprobe_aggrprobe(struct kprobe *p) | |
348 | { | |
349 | return p->pre_handler == aggr_pre_handler; | |
350 | } | |
351 | ||
6274de49 MH |
352 | /* Return true(!0) if the kprobe is unused */ |
353 | static inline int kprobe_unused(struct kprobe *p) | |
354 | { | |
355 | return kprobe_aggrprobe(p) && kprobe_disabled(p) && | |
356 | list_empty(&p->list); | |
357 | } | |
358 | ||
afd66255 MH |
359 | /* |
360 | * Keep all fields in the kprobe consistent | |
361 | */ | |
6d8e40a8 | 362 | static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p) |
afd66255 | 363 | { |
6d8e40a8 MH |
364 | memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t)); |
365 | memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn)); | |
afd66255 MH |
366 | } |
367 | ||
368 | #ifdef CONFIG_OPTPROBES | |
b2be84df MH |
369 | /* NOTE: change this value only with kprobe_mutex held */ |
370 | static bool kprobes_allow_optimization; | |
371 | ||
afd66255 MH |
372 | /* |
373 | * Call all pre_handler on the list, but ignores its return value. | |
374 | * This must be called from arch-dep optimized caller. | |
375 | */ | |
820aede0 | 376 | void opt_pre_handler(struct kprobe *p, struct pt_regs *regs) |
afd66255 MH |
377 | { |
378 | struct kprobe *kp; | |
379 | ||
380 | list_for_each_entry_rcu(kp, &p->list, list) { | |
381 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { | |
382 | set_kprobe_instance(kp); | |
4f3a8714 | 383 | kp->pre_handler(kp, regs); |
afd66255 MH |
384 | } |
385 | reset_kprobe_instance(); | |
386 | } | |
387 | } | |
820aede0 | 388 | NOKPROBE_SYMBOL(opt_pre_handler); |
afd66255 | 389 | |
6274de49 | 390 | /* Free optimized instructions and optimized_kprobe */ |
55479f64 | 391 | static void free_aggr_kprobe(struct kprobe *p) |
6274de49 MH |
392 | { |
393 | struct optimized_kprobe *op; | |
394 | ||
395 | op = container_of(p, struct optimized_kprobe, kp); | |
396 | arch_remove_optimized_kprobe(op); | |
397 | arch_remove_kprobe(p); | |
398 | kfree(op); | |
399 | } | |
400 | ||
afd66255 MH |
401 | /* Return true(!0) if the kprobe is ready for optimization. */ |
402 | static inline int kprobe_optready(struct kprobe *p) | |
403 | { | |
404 | struct optimized_kprobe *op; | |
405 | ||
406 | if (kprobe_aggrprobe(p)) { | |
407 | op = container_of(p, struct optimized_kprobe, kp); | |
408 | return arch_prepared_optinsn(&op->optinsn); | |
409 | } | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
6274de49 MH |
414 | /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */ |
415 | static inline int kprobe_disarmed(struct kprobe *p) | |
416 | { | |
417 | struct optimized_kprobe *op; | |
418 | ||
419 | /* If kprobe is not aggr/opt probe, just return kprobe is disabled */ | |
420 | if (!kprobe_aggrprobe(p)) | |
421 | return kprobe_disabled(p); | |
422 | ||
423 | op = container_of(p, struct optimized_kprobe, kp); | |
424 | ||
425 | return kprobe_disabled(p) && list_empty(&op->list); | |
426 | } | |
427 | ||
428 | /* Return true(!0) if the probe is queued on (un)optimizing lists */ | |
55479f64 | 429 | static int kprobe_queued(struct kprobe *p) |
6274de49 MH |
430 | { |
431 | struct optimized_kprobe *op; | |
432 | ||
433 | if (kprobe_aggrprobe(p)) { | |
434 | op = container_of(p, struct optimized_kprobe, kp); | |
435 | if (!list_empty(&op->list)) | |
436 | return 1; | |
437 | } | |
438 | return 0; | |
439 | } | |
440 | ||
afd66255 MH |
441 | /* |
442 | * Return an optimized kprobe whose optimizing code replaces | |
443 | * instructions including addr (exclude breakpoint). | |
444 | */ | |
55479f64 | 445 | static struct kprobe *get_optimized_kprobe(unsigned long addr) |
afd66255 MH |
446 | { |
447 | int i; | |
448 | struct kprobe *p = NULL; | |
449 | struct optimized_kprobe *op; | |
450 | ||
451 | /* Don't check i == 0, since that is a breakpoint case. */ | |
452 | for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++) | |
453 | p = get_kprobe((void *)(addr - i)); | |
454 | ||
455 | if (p && kprobe_optready(p)) { | |
456 | op = container_of(p, struct optimized_kprobe, kp); | |
457 | if (arch_within_optimized_kprobe(op, addr)) | |
458 | return p; | |
459 | } | |
460 | ||
461 | return NULL; | |
462 | } | |
463 | ||
464 | /* Optimization staging list, protected by kprobe_mutex */ | |
465 | static LIST_HEAD(optimizing_list); | |
6274de49 | 466 | static LIST_HEAD(unoptimizing_list); |
7b959fc5 | 467 | static LIST_HEAD(freeing_list); |
afd66255 MH |
468 | |
469 | static void kprobe_optimizer(struct work_struct *work); | |
470 | static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer); | |
471 | #define OPTIMIZE_DELAY 5 | |
472 | ||
61f4e13f MH |
473 | /* |
474 | * Optimize (replace a breakpoint with a jump) kprobes listed on | |
475 | * optimizing_list. | |
476 | */ | |
55479f64 | 477 | static void do_optimize_kprobes(void) |
afd66255 | 478 | { |
f1c6ece2 | 479 | lockdep_assert_held(&text_mutex); |
afd66255 MH |
480 | /* |
481 | * The optimization/unoptimization refers online_cpus via | |
482 | * stop_machine() and cpu-hotplug modifies online_cpus. | |
483 | * And same time, text_mutex will be held in cpu-hotplug and here. | |
484 | * This combination can cause a deadlock (cpu-hotplug try to lock | |
485 | * text_mutex but stop_machine can not be done because online_cpus | |
486 | * has been changed) | |
2d1e38f5 | 487 | * To avoid this deadlock, caller must have locked cpu hotplug |
afd66255 MH |
488 | * for preventing cpu-hotplug outside of text_mutex locking. |
489 | */ | |
2d1e38f5 TG |
490 | lockdep_assert_cpus_held(); |
491 | ||
492 | /* Optimization never be done when disarmed */ | |
493 | if (kprobes_all_disarmed || !kprobes_allow_optimization || | |
494 | list_empty(&optimizing_list)) | |
495 | return; | |
496 | ||
cd7ebe22 | 497 | arch_optimize_kprobes(&optimizing_list); |
61f4e13f MH |
498 | } |
499 | ||
6274de49 MH |
500 | /* |
501 | * Unoptimize (replace a jump with a breakpoint and remove the breakpoint | |
502 | * if need) kprobes listed on unoptimizing_list. | |
503 | */ | |
55479f64 | 504 | static void do_unoptimize_kprobes(void) |
6274de49 MH |
505 | { |
506 | struct optimized_kprobe *op, *tmp; | |
507 | ||
f1c6ece2 | 508 | lockdep_assert_held(&text_mutex); |
2d1e38f5 TG |
509 | /* See comment in do_optimize_kprobes() */ |
510 | lockdep_assert_cpus_held(); | |
511 | ||
6274de49 MH |
512 | /* Unoptimization must be done anytime */ |
513 | if (list_empty(&unoptimizing_list)) | |
514 | return; | |
515 | ||
7b959fc5 | 516 | arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list); |
f984ba4e | 517 | /* Loop free_list for disarming */ |
7b959fc5 | 518 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
f66c0447 MH |
519 | /* Switching from detour code to origin */ |
520 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; | |
6274de49 MH |
521 | /* Disarm probes if marked disabled */ |
522 | if (kprobe_disabled(&op->kp)) | |
523 | arch_disarm_kprobe(&op->kp); | |
524 | if (kprobe_unused(&op->kp)) { | |
525 | /* | |
526 | * Remove unused probes from hash list. After waiting | |
527 | * for synchronization, these probes are reclaimed. | |
528 | * (reclaiming is done by do_free_cleaned_kprobes.) | |
529 | */ | |
530 | hlist_del_rcu(&op->kp.hlist); | |
6274de49 MH |
531 | } else |
532 | list_del_init(&op->list); | |
533 | } | |
6274de49 MH |
534 | } |
535 | ||
536 | /* Reclaim all kprobes on the free_list */ | |
55479f64 | 537 | static void do_free_cleaned_kprobes(void) |
6274de49 MH |
538 | { |
539 | struct optimized_kprobe *op, *tmp; | |
540 | ||
7b959fc5 | 541 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
6274de49 | 542 | list_del_init(&op->list); |
cbdd96f5 MH |
543 | if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) { |
544 | /* | |
545 | * This must not happen, but if there is a kprobe | |
546 | * still in use, keep it on kprobes hash list. | |
547 | */ | |
548 | continue; | |
549 | } | |
6274de49 MH |
550 | free_aggr_kprobe(&op->kp); |
551 | } | |
552 | } | |
553 | ||
554 | /* Start optimizer after OPTIMIZE_DELAY passed */ | |
55479f64 | 555 | static void kick_kprobe_optimizer(void) |
6274de49 | 556 | { |
ad72b3be | 557 | schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY); |
6274de49 MH |
558 | } |
559 | ||
61f4e13f | 560 | /* Kprobe jump optimizer */ |
55479f64 | 561 | static void kprobe_optimizer(struct work_struct *work) |
61f4e13f | 562 | { |
72ef3794 | 563 | mutex_lock(&kprobe_mutex); |
2d1e38f5 | 564 | cpus_read_lock(); |
f1c6ece2 | 565 | mutex_lock(&text_mutex); |
61f4e13f MH |
566 | /* Lock modules while optimizing kprobes */ |
567 | mutex_lock(&module_mutex); | |
61f4e13f MH |
568 | |
569 | /* | |
6274de49 MH |
570 | * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed) |
571 | * kprobes before waiting for quiesence period. | |
572 | */ | |
7b959fc5 | 573 | do_unoptimize_kprobes(); |
6274de49 MH |
574 | |
575 | /* | |
a30b85df MH |
576 | * Step 2: Wait for quiesence period to ensure all potentially |
577 | * preempted tasks to have normally scheduled. Because optprobe | |
578 | * may modify multiple instructions, there is a chance that Nth | |
579 | * instruction is preempted. In that case, such tasks can return | |
580 | * to 2nd-Nth byte of jump instruction. This wait is for avoiding it. | |
581 | * Note that on non-preemptive kernel, this is transparently converted | |
582 | * to synchronoze_sched() to wait for all interrupts to have completed. | |
61f4e13f | 583 | */ |
a30b85df | 584 | synchronize_rcu_tasks(); |
61f4e13f | 585 | |
6274de49 | 586 | /* Step 3: Optimize kprobes after quiesence period */ |
61f4e13f | 587 | do_optimize_kprobes(); |
6274de49 MH |
588 | |
589 | /* Step 4: Free cleaned kprobes after quiesence period */ | |
7b959fc5 | 590 | do_free_cleaned_kprobes(); |
6274de49 | 591 | |
afd66255 | 592 | mutex_unlock(&module_mutex); |
f1c6ece2 | 593 | mutex_unlock(&text_mutex); |
2d1e38f5 | 594 | cpus_read_unlock(); |
6274de49 | 595 | |
cd7ebe22 | 596 | /* Step 5: Kick optimizer again if needed */ |
f984ba4e | 597 | if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) |
cd7ebe22 | 598 | kick_kprobe_optimizer(); |
1a0aa991 MH |
599 | |
600 | mutex_unlock(&kprobe_mutex); | |
6274de49 MH |
601 | } |
602 | ||
603 | /* Wait for completing optimization and unoptimization */ | |
30e7d894 | 604 | void wait_for_kprobe_optimizer(void) |
6274de49 | 605 | { |
ad72b3be TH |
606 | mutex_lock(&kprobe_mutex); |
607 | ||
608 | while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) { | |
609 | mutex_unlock(&kprobe_mutex); | |
610 | ||
611 | /* this will also make optimizing_work execute immmediately */ | |
612 | flush_delayed_work(&optimizing_work); | |
613 | /* @optimizing_work might not have been queued yet, relax */ | |
614 | cpu_relax(); | |
615 | ||
616 | mutex_lock(&kprobe_mutex); | |
617 | } | |
618 | ||
619 | mutex_unlock(&kprobe_mutex); | |
afd66255 MH |
620 | } |
621 | ||
e4add247 MH |
622 | static bool optprobe_queued_unopt(struct optimized_kprobe *op) |
623 | { | |
624 | struct optimized_kprobe *_op; | |
625 | ||
626 | list_for_each_entry(_op, &unoptimizing_list, list) { | |
627 | if (op == _op) | |
628 | return true; | |
629 | } | |
630 | ||
631 | return false; | |
632 | } | |
633 | ||
afd66255 | 634 | /* Optimize kprobe if p is ready to be optimized */ |
55479f64 | 635 | static void optimize_kprobe(struct kprobe *p) |
afd66255 MH |
636 | { |
637 | struct optimized_kprobe *op; | |
638 | ||
639 | /* Check if the kprobe is disabled or not ready for optimization. */ | |
b2be84df | 640 | if (!kprobe_optready(p) || !kprobes_allow_optimization || |
afd66255 MH |
641 | (kprobe_disabled(p) || kprobes_all_disarmed)) |
642 | return; | |
643 | ||
059053a2 MH |
644 | /* kprobes with post_handler can not be optimized */ |
645 | if (p->post_handler) | |
afd66255 MH |
646 | return; |
647 | ||
648 | op = container_of(p, struct optimized_kprobe, kp); | |
649 | ||
650 | /* Check there is no other kprobes at the optimized instructions */ | |
651 | if (arch_check_optimized_kprobe(op) < 0) | |
652 | return; | |
653 | ||
654 | /* Check if it is already optimized. */ | |
e4add247 MH |
655 | if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) { |
656 | if (optprobe_queued_unopt(op)) { | |
657 | /* This is under unoptimizing. Just dequeue the probe */ | |
658 | list_del_init(&op->list); | |
659 | } | |
afd66255 | 660 | return; |
e4add247 | 661 | } |
afd66255 | 662 | op->kp.flags |= KPROBE_FLAG_OPTIMIZED; |
6274de49 | 663 | |
e4add247 MH |
664 | /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */ |
665 | if (WARN_ON_ONCE(!list_empty(&op->list))) | |
666 | return; | |
667 | ||
668 | list_add(&op->list, &optimizing_list); | |
669 | kick_kprobe_optimizer(); | |
6274de49 MH |
670 | } |
671 | ||
672 | /* Short cut to direct unoptimizing */ | |
55479f64 | 673 | static void force_unoptimize_kprobe(struct optimized_kprobe *op) |
6274de49 | 674 | { |
2d1e38f5 | 675 | lockdep_assert_cpus_held(); |
6274de49 | 676 | arch_unoptimize_kprobe(op); |
f66c0447 | 677 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
afd66255 MH |
678 | } |
679 | ||
680 | /* Unoptimize a kprobe if p is optimized */ | |
55479f64 | 681 | static void unoptimize_kprobe(struct kprobe *p, bool force) |
afd66255 MH |
682 | { |
683 | struct optimized_kprobe *op; | |
684 | ||
6274de49 MH |
685 | if (!kprobe_aggrprobe(p) || kprobe_disarmed(p)) |
686 | return; /* This is not an optprobe nor optimized */ | |
687 | ||
688 | op = container_of(p, struct optimized_kprobe, kp); | |
e4add247 | 689 | if (!kprobe_optimized(p)) |
6274de49 | 690 | return; |
6274de49 | 691 | |
6274de49 | 692 | if (!list_empty(&op->list)) { |
e4add247 MH |
693 | if (optprobe_queued_unopt(op)) { |
694 | /* Queued in unoptimizing queue */ | |
695 | if (force) { | |
696 | /* | |
697 | * Forcibly unoptimize the kprobe here, and queue it | |
698 | * in the freeing list for release afterwards. | |
699 | */ | |
700 | force_unoptimize_kprobe(op); | |
701 | list_move(&op->list, &freeing_list); | |
702 | } | |
703 | } else { | |
704 | /* Dequeue from the optimizing queue */ | |
705 | list_del_init(&op->list); | |
706 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; | |
707 | } | |
6274de49 MH |
708 | return; |
709 | } | |
e4add247 | 710 | |
6274de49 | 711 | /* Optimized kprobe case */ |
e4add247 | 712 | if (force) { |
6274de49 MH |
713 | /* Forcibly update the code: this is a special case */ |
714 | force_unoptimize_kprobe(op); | |
e4add247 | 715 | } else { |
6274de49 MH |
716 | list_add(&op->list, &unoptimizing_list); |
717 | kick_kprobe_optimizer(); | |
afd66255 MH |
718 | } |
719 | } | |
720 | ||
0490cd1f | 721 | /* Cancel unoptimizing for reusing */ |
819319fc | 722 | static int reuse_unused_kprobe(struct kprobe *ap) |
0490cd1f MH |
723 | { |
724 | struct optimized_kprobe *op; | |
725 | ||
0490cd1f MH |
726 | /* |
727 | * Unused kprobe MUST be on the way of delayed unoptimizing (means | |
728 | * there is still a relative jump) and disabled. | |
729 | */ | |
730 | op = container_of(ap, struct optimized_kprobe, kp); | |
4458515b | 731 | WARN_ON_ONCE(list_empty(&op->list)); |
0490cd1f MH |
732 | /* Enable the probe again */ |
733 | ap->flags &= ~KPROBE_FLAG_DISABLED; | |
734 | /* Optimize it again (remove from op->list) */ | |
5f843ed4 MH |
735 | if (!kprobe_optready(ap)) |
736 | return -EINVAL; | |
819319fc | 737 | |
0490cd1f | 738 | optimize_kprobe(ap); |
819319fc | 739 | return 0; |
0490cd1f MH |
740 | } |
741 | ||
afd66255 | 742 | /* Remove optimized instructions */ |
55479f64 | 743 | static void kill_optimized_kprobe(struct kprobe *p) |
afd66255 MH |
744 | { |
745 | struct optimized_kprobe *op; | |
746 | ||
747 | op = container_of(p, struct optimized_kprobe, kp); | |
6274de49 MH |
748 | if (!list_empty(&op->list)) |
749 | /* Dequeue from the (un)optimization queue */ | |
afd66255 | 750 | list_del_init(&op->list); |
6274de49 | 751 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
7b959fc5 MH |
752 | |
753 | if (kprobe_unused(p)) { | |
754 | /* Enqueue if it is unused */ | |
755 | list_add(&op->list, &freeing_list); | |
756 | /* | |
757 | * Remove unused probes from the hash list. After waiting | |
758 | * for synchronization, this probe is reclaimed. | |
759 | * (reclaiming is done by do_free_cleaned_kprobes().) | |
760 | */ | |
761 | hlist_del_rcu(&op->kp.hlist); | |
762 | } | |
763 | ||
6274de49 | 764 | /* Don't touch the code, because it is already freed. */ |
afd66255 MH |
765 | arch_remove_optimized_kprobe(op); |
766 | } | |
767 | ||
a460246c MH |
768 | static inline |
769 | void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p) | |
770 | { | |
771 | if (!kprobe_ftrace(p)) | |
772 | arch_prepare_optimized_kprobe(op, p); | |
773 | } | |
774 | ||
afd66255 | 775 | /* Try to prepare optimized instructions */ |
55479f64 | 776 | static void prepare_optimized_kprobe(struct kprobe *p) |
afd66255 MH |
777 | { |
778 | struct optimized_kprobe *op; | |
779 | ||
780 | op = container_of(p, struct optimized_kprobe, kp); | |
a460246c | 781 | __prepare_optimized_kprobe(op, p); |
afd66255 MH |
782 | } |
783 | ||
afd66255 | 784 | /* Allocate new optimized_kprobe and try to prepare optimized instructions */ |
55479f64 | 785 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
afd66255 MH |
786 | { |
787 | struct optimized_kprobe *op; | |
788 | ||
789 | op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL); | |
790 | if (!op) | |
791 | return NULL; | |
792 | ||
793 | INIT_LIST_HEAD(&op->list); | |
794 | op->kp.addr = p->addr; | |
a460246c | 795 | __prepare_optimized_kprobe(op, p); |
afd66255 MH |
796 | |
797 | return &op->kp; | |
798 | } | |
799 | ||
55479f64 | 800 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p); |
afd66255 MH |
801 | |
802 | /* | |
803 | * Prepare an optimized_kprobe and optimize it | |
804 | * NOTE: p must be a normal registered kprobe | |
805 | */ | |
55479f64 | 806 | static void try_to_optimize_kprobe(struct kprobe *p) |
afd66255 MH |
807 | { |
808 | struct kprobe *ap; | |
809 | struct optimized_kprobe *op; | |
810 | ||
ae6aa16f MH |
811 | /* Impossible to optimize ftrace-based kprobe */ |
812 | if (kprobe_ftrace(p)) | |
813 | return; | |
814 | ||
25764288 | 815 | /* For preparing optimization, jump_label_text_reserved() is called */ |
2d1e38f5 | 816 | cpus_read_lock(); |
25764288 MH |
817 | jump_label_lock(); |
818 | mutex_lock(&text_mutex); | |
819 | ||
afd66255 MH |
820 | ap = alloc_aggr_kprobe(p); |
821 | if (!ap) | |
25764288 | 822 | goto out; |
afd66255 MH |
823 | |
824 | op = container_of(ap, struct optimized_kprobe, kp); | |
825 | if (!arch_prepared_optinsn(&op->optinsn)) { | |
826 | /* If failed to setup optimizing, fallback to kprobe */ | |
6274de49 MH |
827 | arch_remove_optimized_kprobe(op); |
828 | kfree(op); | |
25764288 | 829 | goto out; |
afd66255 MH |
830 | } |
831 | ||
832 | init_aggr_kprobe(ap, p); | |
25764288 MH |
833 | optimize_kprobe(ap); /* This just kicks optimizer thread */ |
834 | ||
835 | out: | |
836 | mutex_unlock(&text_mutex); | |
837 | jump_label_unlock(); | |
2d1e38f5 | 838 | cpus_read_unlock(); |
afd66255 MH |
839 | } |
840 | ||
b2be84df | 841 | #ifdef CONFIG_SYSCTL |
55479f64 | 842 | static void optimize_all_kprobes(void) |
b2be84df MH |
843 | { |
844 | struct hlist_head *head; | |
b2be84df MH |
845 | struct kprobe *p; |
846 | unsigned int i; | |
847 | ||
5c51543b | 848 | mutex_lock(&kprobe_mutex); |
b2be84df MH |
849 | /* If optimization is already allowed, just return */ |
850 | if (kprobes_allow_optimization) | |
5c51543b | 851 | goto out; |
b2be84df | 852 | |
2d1e38f5 | 853 | cpus_read_lock(); |
b2be84df | 854 | kprobes_allow_optimization = true; |
b2be84df MH |
855 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
856 | head = &kprobe_table[i]; | |
7e6a71d8 | 857 | hlist_for_each_entry(p, head, hlist) |
b2be84df MH |
858 | if (!kprobe_disabled(p)) |
859 | optimize_kprobe(p); | |
860 | } | |
2d1e38f5 | 861 | cpus_read_unlock(); |
b2be84df | 862 | printk(KERN_INFO "Kprobes globally optimized\n"); |
5c51543b MH |
863 | out: |
864 | mutex_unlock(&kprobe_mutex); | |
b2be84df MH |
865 | } |
866 | ||
55479f64 | 867 | static void unoptimize_all_kprobes(void) |
b2be84df MH |
868 | { |
869 | struct hlist_head *head; | |
b2be84df MH |
870 | struct kprobe *p; |
871 | unsigned int i; | |
872 | ||
5c51543b | 873 | mutex_lock(&kprobe_mutex); |
b2be84df | 874 | /* If optimization is already prohibited, just return */ |
5c51543b MH |
875 | if (!kprobes_allow_optimization) { |
876 | mutex_unlock(&kprobe_mutex); | |
b2be84df | 877 | return; |
5c51543b | 878 | } |
b2be84df | 879 | |
2d1e38f5 | 880 | cpus_read_lock(); |
b2be84df | 881 | kprobes_allow_optimization = false; |
b2be84df MH |
882 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
883 | head = &kprobe_table[i]; | |
7e6a71d8 | 884 | hlist_for_each_entry(p, head, hlist) { |
b2be84df | 885 | if (!kprobe_disabled(p)) |
6274de49 | 886 | unoptimize_kprobe(p, false); |
b2be84df MH |
887 | } |
888 | } | |
2d1e38f5 | 889 | cpus_read_unlock(); |
5c51543b MH |
890 | mutex_unlock(&kprobe_mutex); |
891 | ||
6274de49 MH |
892 | /* Wait for unoptimizing completion */ |
893 | wait_for_kprobe_optimizer(); | |
894 | printk(KERN_INFO "Kprobes globally unoptimized\n"); | |
b2be84df MH |
895 | } |
896 | ||
5c51543b | 897 | static DEFINE_MUTEX(kprobe_sysctl_mutex); |
b2be84df MH |
898 | int sysctl_kprobes_optimization; |
899 | int proc_kprobes_optimization_handler(struct ctl_table *table, int write, | |
32927393 | 900 | void *buffer, size_t *length, |
b2be84df MH |
901 | loff_t *ppos) |
902 | { | |
903 | int ret; | |
904 | ||
5c51543b | 905 | mutex_lock(&kprobe_sysctl_mutex); |
b2be84df MH |
906 | sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0; |
907 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); | |
908 | ||
909 | if (sysctl_kprobes_optimization) | |
910 | optimize_all_kprobes(); | |
911 | else | |
912 | unoptimize_all_kprobes(); | |
5c51543b | 913 | mutex_unlock(&kprobe_sysctl_mutex); |
b2be84df MH |
914 | |
915 | return ret; | |
916 | } | |
917 | #endif /* CONFIG_SYSCTL */ | |
918 | ||
6274de49 | 919 | /* Put a breakpoint for a probe. Must be called with text_mutex locked */ |
55479f64 | 920 | static void __arm_kprobe(struct kprobe *p) |
afd66255 | 921 | { |
6d8e40a8 | 922 | struct kprobe *_p; |
afd66255 MH |
923 | |
924 | /* Check collision with other optimized kprobes */ | |
6d8e40a8 MH |
925 | _p = get_optimized_kprobe((unsigned long)p->addr); |
926 | if (unlikely(_p)) | |
6274de49 MH |
927 | /* Fallback to unoptimized kprobe */ |
928 | unoptimize_kprobe(_p, true); | |
afd66255 MH |
929 | |
930 | arch_arm_kprobe(p); | |
931 | optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */ | |
932 | } | |
933 | ||
6274de49 | 934 | /* Remove the breakpoint of a probe. Must be called with text_mutex locked */ |
55479f64 | 935 | static void __disarm_kprobe(struct kprobe *p, bool reopt) |
afd66255 | 936 | { |
6d8e40a8 | 937 | struct kprobe *_p; |
afd66255 | 938 | |
69d54b91 WN |
939 | /* Try to unoptimize */ |
940 | unoptimize_kprobe(p, kprobes_all_disarmed); | |
afd66255 | 941 | |
6274de49 MH |
942 | if (!kprobe_queued(p)) { |
943 | arch_disarm_kprobe(p); | |
944 | /* If another kprobe was blocked, optimize it. */ | |
945 | _p = get_optimized_kprobe((unsigned long)p->addr); | |
946 | if (unlikely(_p) && reopt) | |
947 | optimize_kprobe(_p); | |
948 | } | |
949 | /* TODO: reoptimize others after unoptimized this probe */ | |
afd66255 MH |
950 | } |
951 | ||
952 | #else /* !CONFIG_OPTPROBES */ | |
953 | ||
954 | #define optimize_kprobe(p) do {} while (0) | |
6274de49 | 955 | #define unoptimize_kprobe(p, f) do {} while (0) |
afd66255 MH |
956 | #define kill_optimized_kprobe(p) do {} while (0) |
957 | #define prepare_optimized_kprobe(p) do {} while (0) | |
958 | #define try_to_optimize_kprobe(p) do {} while (0) | |
959 | #define __arm_kprobe(p) arch_arm_kprobe(p) | |
6274de49 MH |
960 | #define __disarm_kprobe(p, o) arch_disarm_kprobe(p) |
961 | #define kprobe_disarmed(p) kprobe_disabled(p) | |
962 | #define wait_for_kprobe_optimizer() do {} while (0) | |
afd66255 | 963 | |
819319fc | 964 | static int reuse_unused_kprobe(struct kprobe *ap) |
0490cd1f | 965 | { |
819319fc MH |
966 | /* |
967 | * If the optimized kprobe is NOT supported, the aggr kprobe is | |
968 | * released at the same time that the last aggregated kprobe is | |
969 | * unregistered. | |
970 | * Thus there should be no chance to reuse unused kprobe. | |
971 | */ | |
0490cd1f | 972 | printk(KERN_ERR "Error: There should be no unused kprobe here.\n"); |
819319fc | 973 | return -EINVAL; |
0490cd1f MH |
974 | } |
975 | ||
55479f64 | 976 | static void free_aggr_kprobe(struct kprobe *p) |
afd66255 | 977 | { |
6274de49 | 978 | arch_remove_kprobe(p); |
afd66255 MH |
979 | kfree(p); |
980 | } | |
981 | ||
55479f64 | 982 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
afd66255 MH |
983 | { |
984 | return kzalloc(sizeof(struct kprobe), GFP_KERNEL); | |
985 | } | |
986 | #endif /* CONFIG_OPTPROBES */ | |
987 | ||
e7dbfe34 | 988 | #ifdef CONFIG_KPROBES_ON_FTRACE |
ae6aa16f | 989 | static struct ftrace_ops kprobe_ftrace_ops __read_mostly = { |
0bc11ed5 MH |
990 | .func = kprobe_ftrace_handler, |
991 | .flags = FTRACE_OPS_FL_SAVE_REGS, | |
992 | }; | |
993 | ||
994 | static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = { | |
e5253896 | 995 | .func = kprobe_ftrace_handler, |
1d70be34 | 996 | .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY, |
ae6aa16f | 997 | }; |
0bc11ed5 MH |
998 | |
999 | static int kprobe_ipmodify_enabled; | |
ae6aa16f MH |
1000 | static int kprobe_ftrace_enabled; |
1001 | ||
1002 | /* Must ensure p->addr is really on ftrace */ | |
55479f64 | 1003 | static int prepare_kprobe(struct kprobe *p) |
ae6aa16f MH |
1004 | { |
1005 | if (!kprobe_ftrace(p)) | |
1006 | return arch_prepare_kprobe(p); | |
1007 | ||
1008 | return arch_prepare_kprobe_ftrace(p); | |
1009 | } | |
1010 | ||
1011 | /* Caller must lock kprobe_mutex */ | |
0bc11ed5 MH |
1012 | static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
1013 | int *cnt) | |
ae6aa16f | 1014 | { |
12310e34 | 1015 | int ret = 0; |
ae6aa16f | 1016 | |
0bc11ed5 | 1017 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0); |
12310e34 | 1018 | if (ret) { |
4458515b MH |
1019 | pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n", |
1020 | p->addr, ret); | |
12310e34 JY |
1021 | return ret; |
1022 | } | |
1023 | ||
0bc11ed5 MH |
1024 | if (*cnt == 0) { |
1025 | ret = register_ftrace_function(ops); | |
12310e34 JY |
1026 | if (ret) { |
1027 | pr_debug("Failed to init kprobe-ftrace (%d)\n", ret); | |
1028 | goto err_ftrace; | |
1029 | } | |
ae6aa16f | 1030 | } |
12310e34 | 1031 | |
0bc11ed5 | 1032 | (*cnt)++; |
12310e34 JY |
1033 | return ret; |
1034 | ||
1035 | err_ftrace: | |
1036 | /* | |
0bc11ed5 MH |
1037 | * At this point, sinec ops is not registered, we should be sefe from |
1038 | * registering empty filter. | |
12310e34 | 1039 | */ |
0bc11ed5 | 1040 | ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
12310e34 | 1041 | return ret; |
ae6aa16f MH |
1042 | } |
1043 | ||
0bc11ed5 MH |
1044 | static int arm_kprobe_ftrace(struct kprobe *p) |
1045 | { | |
1046 | bool ipmodify = (p->post_handler != NULL); | |
1047 | ||
1048 | return __arm_kprobe_ftrace(p, | |
1049 | ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, | |
1050 | ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); | |
1051 | } | |
1052 | ||
ae6aa16f | 1053 | /* Caller must lock kprobe_mutex */ |
0bc11ed5 MH |
1054 | static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
1055 | int *cnt) | |
ae6aa16f | 1056 | { |
297f9233 | 1057 | int ret = 0; |
ae6aa16f | 1058 | |
0bc11ed5 MH |
1059 | if (*cnt == 1) { |
1060 | ret = unregister_ftrace_function(ops); | |
297f9233 JY |
1061 | if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret)) |
1062 | return ret; | |
ae6aa16f | 1063 | } |
297f9233 | 1064 | |
0bc11ed5 | 1065 | (*cnt)--; |
297f9233 | 1066 | |
0bc11ed5 | 1067 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
4458515b MH |
1068 | WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n", |
1069 | p->addr, ret); | |
297f9233 | 1070 | return ret; |
ae6aa16f | 1071 | } |
0bc11ed5 MH |
1072 | |
1073 | static int disarm_kprobe_ftrace(struct kprobe *p) | |
1074 | { | |
1075 | bool ipmodify = (p->post_handler != NULL); | |
1076 | ||
1077 | return __disarm_kprobe_ftrace(p, | |
1078 | ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, | |
1079 | ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); | |
1080 | } | |
e7dbfe34 | 1081 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
ae6aa16f | 1082 | #define prepare_kprobe(p) arch_prepare_kprobe(p) |
12310e34 | 1083 | #define arm_kprobe_ftrace(p) (-ENODEV) |
297f9233 | 1084 | #define disarm_kprobe_ftrace(p) (-ENODEV) |
ae6aa16f MH |
1085 | #endif |
1086 | ||
201517a7 | 1087 | /* Arm a kprobe with text_mutex */ |
12310e34 | 1088 | static int arm_kprobe(struct kprobe *kp) |
201517a7 | 1089 | { |
12310e34 JY |
1090 | if (unlikely(kprobe_ftrace(kp))) |
1091 | return arm_kprobe_ftrace(kp); | |
1092 | ||
2d1e38f5 | 1093 | cpus_read_lock(); |
201517a7 | 1094 | mutex_lock(&text_mutex); |
afd66255 | 1095 | __arm_kprobe(kp); |
201517a7 | 1096 | mutex_unlock(&text_mutex); |
2d1e38f5 | 1097 | cpus_read_unlock(); |
12310e34 JY |
1098 | |
1099 | return 0; | |
201517a7 MH |
1100 | } |
1101 | ||
1102 | /* Disarm a kprobe with text_mutex */ | |
297f9233 | 1103 | static int disarm_kprobe(struct kprobe *kp, bool reopt) |
201517a7 | 1104 | { |
297f9233 JY |
1105 | if (unlikely(kprobe_ftrace(kp))) |
1106 | return disarm_kprobe_ftrace(kp); | |
2d1e38f5 TG |
1107 | |
1108 | cpus_read_lock(); | |
201517a7 | 1109 | mutex_lock(&text_mutex); |
ae6aa16f | 1110 | __disarm_kprobe(kp, reopt); |
201517a7 | 1111 | mutex_unlock(&text_mutex); |
2d1e38f5 | 1112 | cpus_read_unlock(); |
297f9233 JY |
1113 | |
1114 | return 0; | |
201517a7 MH |
1115 | } |
1116 | ||
64f562c6 AM |
1117 | /* |
1118 | * Aggregate handlers for multiple kprobes support - these handlers | |
1119 | * take care of invoking the individual kprobe handlers on p->list | |
1120 | */ | |
820aede0 | 1121 | static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs) |
64f562c6 AM |
1122 | { |
1123 | struct kprobe *kp; | |
1124 | ||
3516a460 | 1125 | list_for_each_entry_rcu(kp, &p->list, list) { |
de5bd88d | 1126 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { |
e6584523 | 1127 | set_kprobe_instance(kp); |
8b0914ea PP |
1128 | if (kp->pre_handler(kp, regs)) |
1129 | return 1; | |
64f562c6 | 1130 | } |
e6584523 | 1131 | reset_kprobe_instance(); |
64f562c6 AM |
1132 | } |
1133 | return 0; | |
1134 | } | |
820aede0 | 1135 | NOKPROBE_SYMBOL(aggr_pre_handler); |
64f562c6 | 1136 | |
820aede0 MH |
1137 | static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, |
1138 | unsigned long flags) | |
64f562c6 AM |
1139 | { |
1140 | struct kprobe *kp; | |
1141 | ||
3516a460 | 1142 | list_for_each_entry_rcu(kp, &p->list, list) { |
de5bd88d | 1143 | if (kp->post_handler && likely(!kprobe_disabled(kp))) { |
e6584523 | 1144 | set_kprobe_instance(kp); |
64f562c6 | 1145 | kp->post_handler(kp, regs, flags); |
e6584523 | 1146 | reset_kprobe_instance(); |
64f562c6 AM |
1147 | } |
1148 | } | |
64f562c6 | 1149 | } |
820aede0 | 1150 | NOKPROBE_SYMBOL(aggr_post_handler); |
64f562c6 | 1151 | |
820aede0 MH |
1152 | static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, |
1153 | int trapnr) | |
64f562c6 | 1154 | { |
b76834bc | 1155 | struct kprobe *cur = __this_cpu_read(kprobe_instance); |
e6584523 | 1156 | |
64f562c6 AM |
1157 | /* |
1158 | * if we faulted "during" the execution of a user specified | |
1159 | * probe handler, invoke just that probe's fault handler | |
1160 | */ | |
e6584523 AM |
1161 | if (cur && cur->fault_handler) { |
1162 | if (cur->fault_handler(cur, regs, trapnr)) | |
64f562c6 AM |
1163 | return 1; |
1164 | } | |
1165 | return 0; | |
1166 | } | |
820aede0 | 1167 | NOKPROBE_SYMBOL(aggr_fault_handler); |
64f562c6 | 1168 | |
bf8d5c52 | 1169 | /* Walks the list and increments nmissed count for multiprobe case */ |
820aede0 | 1170 | void kprobes_inc_nmissed_count(struct kprobe *p) |
bf8d5c52 KA |
1171 | { |
1172 | struct kprobe *kp; | |
afd66255 | 1173 | if (!kprobe_aggrprobe(p)) { |
bf8d5c52 KA |
1174 | p->nmissed++; |
1175 | } else { | |
1176 | list_for_each_entry_rcu(kp, &p->list, list) | |
1177 | kp->nmissed++; | |
1178 | } | |
1179 | return; | |
1180 | } | |
820aede0 | 1181 | NOKPROBE_SYMBOL(kprobes_inc_nmissed_count); |
bf8d5c52 | 1182 | |
820aede0 MH |
1183 | void recycle_rp_inst(struct kretprobe_instance *ri, |
1184 | struct hlist_head *head) | |
b94cce92 | 1185 | { |
ef53d9c5 S |
1186 | struct kretprobe *rp = ri->rp; |
1187 | ||
b94cce92 HN |
1188 | /* remove rp inst off the rprobe_inst_table */ |
1189 | hlist_del(&ri->hlist); | |
ef53d9c5 S |
1190 | INIT_HLIST_NODE(&ri->hlist); |
1191 | if (likely(rp)) { | |
ec484608 | 1192 | raw_spin_lock(&rp->lock); |
ef53d9c5 | 1193 | hlist_add_head(&ri->hlist, &rp->free_instances); |
ec484608 | 1194 | raw_spin_unlock(&rp->lock); |
b94cce92 HN |
1195 | } else |
1196 | /* Unregistering */ | |
99219a3f | 1197 | hlist_add_head(&ri->hlist, head); |
b94cce92 | 1198 | } |
820aede0 | 1199 | NOKPROBE_SYMBOL(recycle_rp_inst); |
b94cce92 | 1200 | |
820aede0 | 1201 | void kretprobe_hash_lock(struct task_struct *tsk, |
ef53d9c5 | 1202 | struct hlist_head **head, unsigned long *flags) |
635c17c2 | 1203 | __acquires(hlist_lock) |
ef53d9c5 S |
1204 | { |
1205 | unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS); | |
ec484608 | 1206 | raw_spinlock_t *hlist_lock; |
ef53d9c5 S |
1207 | |
1208 | *head = &kretprobe_inst_table[hash]; | |
1209 | hlist_lock = kretprobe_table_lock_ptr(hash); | |
ec484608 | 1210 | raw_spin_lock_irqsave(hlist_lock, *flags); |
ef53d9c5 | 1211 | } |
820aede0 | 1212 | NOKPROBE_SYMBOL(kretprobe_hash_lock); |
ef53d9c5 | 1213 | |
820aede0 MH |
1214 | static void kretprobe_table_lock(unsigned long hash, |
1215 | unsigned long *flags) | |
635c17c2 | 1216 | __acquires(hlist_lock) |
b94cce92 | 1217 | { |
ec484608 TG |
1218 | raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash); |
1219 | raw_spin_lock_irqsave(hlist_lock, *flags); | |
ef53d9c5 | 1220 | } |
820aede0 | 1221 | NOKPROBE_SYMBOL(kretprobe_table_lock); |
ef53d9c5 | 1222 | |
820aede0 MH |
1223 | void kretprobe_hash_unlock(struct task_struct *tsk, |
1224 | unsigned long *flags) | |
635c17c2 | 1225 | __releases(hlist_lock) |
ef53d9c5 S |
1226 | { |
1227 | unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS); | |
ec484608 | 1228 | raw_spinlock_t *hlist_lock; |
ef53d9c5 S |
1229 | |
1230 | hlist_lock = kretprobe_table_lock_ptr(hash); | |
ec484608 | 1231 | raw_spin_unlock_irqrestore(hlist_lock, *flags); |
ef53d9c5 | 1232 | } |
820aede0 | 1233 | NOKPROBE_SYMBOL(kretprobe_hash_unlock); |
ef53d9c5 | 1234 | |
820aede0 MH |
1235 | static void kretprobe_table_unlock(unsigned long hash, |
1236 | unsigned long *flags) | |
635c17c2 | 1237 | __releases(hlist_lock) |
ef53d9c5 | 1238 | { |
ec484608 TG |
1239 | raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash); |
1240 | raw_spin_unlock_irqrestore(hlist_lock, *flags); | |
b94cce92 | 1241 | } |
820aede0 | 1242 | NOKPROBE_SYMBOL(kretprobe_table_unlock); |
b94cce92 | 1243 | |
9b38cc70 JO |
1244 | struct kprobe kprobe_busy = { |
1245 | .addr = (void *) get_kprobe, | |
1246 | }; | |
1247 | ||
1248 | void kprobe_busy_begin(void) | |
1249 | { | |
1250 | struct kprobe_ctlblk *kcb; | |
1251 | ||
1252 | preempt_disable(); | |
1253 | __this_cpu_write(current_kprobe, &kprobe_busy); | |
1254 | kcb = get_kprobe_ctlblk(); | |
1255 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
1256 | } | |
1257 | ||
1258 | void kprobe_busy_end(void) | |
1259 | { | |
1260 | __this_cpu_write(current_kprobe, NULL); | |
1261 | preempt_enable(); | |
1262 | } | |
1263 | ||
b94cce92 | 1264 | /* |
c6fd91f0 | 1265 | * This function is called from finish_task_switch when task tk becomes dead, |
1266 | * so that we can recycle any function-return probe instances associated | |
1267 | * with this task. These left over instances represent probed functions | |
1268 | * that have been called but will never return. | |
b94cce92 | 1269 | */ |
820aede0 | 1270 | void kprobe_flush_task(struct task_struct *tk) |
b94cce92 | 1271 | { |
62c27be0 | 1272 | struct kretprobe_instance *ri; |
99219a3f | 1273 | struct hlist_head *head, empty_rp; |
b67bfe0d | 1274 | struct hlist_node *tmp; |
ef53d9c5 | 1275 | unsigned long hash, flags = 0; |
802eae7c | 1276 | |
ef53d9c5 S |
1277 | if (unlikely(!kprobes_initialized)) |
1278 | /* Early boot. kretprobe_table_locks not yet initialized. */ | |
1279 | return; | |
1280 | ||
9b38cc70 JO |
1281 | kprobe_busy_begin(); |
1282 | ||
d496aab5 | 1283 | INIT_HLIST_HEAD(&empty_rp); |
ef53d9c5 S |
1284 | hash = hash_ptr(tk, KPROBE_HASH_BITS); |
1285 | head = &kretprobe_inst_table[hash]; | |
1286 | kretprobe_table_lock(hash, &flags); | |
b67bfe0d | 1287 | hlist_for_each_entry_safe(ri, tmp, head, hlist) { |
62c27be0 | 1288 | if (ri->task == tk) |
99219a3f | 1289 | recycle_rp_inst(ri, &empty_rp); |
62c27be0 | 1290 | } |
ef53d9c5 | 1291 | kretprobe_table_unlock(hash, &flags); |
b67bfe0d | 1292 | hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { |
99219a3f | 1293 | hlist_del(&ri->hlist); |
1294 | kfree(ri); | |
1295 | } | |
9b38cc70 JO |
1296 | |
1297 | kprobe_busy_end(); | |
b94cce92 | 1298 | } |
820aede0 | 1299 | NOKPROBE_SYMBOL(kprobe_flush_task); |
b94cce92 | 1300 | |
b94cce92 HN |
1301 | static inline void free_rp_inst(struct kretprobe *rp) |
1302 | { | |
1303 | struct kretprobe_instance *ri; | |
b67bfe0d | 1304 | struct hlist_node *next; |
4c4308cb | 1305 | |
b67bfe0d | 1306 | hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) { |
ef53d9c5 | 1307 | hlist_del(&ri->hlist); |
b94cce92 HN |
1308 | kfree(ri); |
1309 | } | |
1310 | } | |
1311 | ||
820aede0 | 1312 | static void cleanup_rp_inst(struct kretprobe *rp) |
4a296e07 | 1313 | { |
ef53d9c5 | 1314 | unsigned long flags, hash; |
4a296e07 | 1315 | struct kretprobe_instance *ri; |
b67bfe0d | 1316 | struct hlist_node *next; |
ef53d9c5 S |
1317 | struct hlist_head *head; |
1318 | ||
4a296e07 | 1319 | /* No race here */ |
ef53d9c5 S |
1320 | for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) { |
1321 | kretprobe_table_lock(hash, &flags); | |
1322 | head = &kretprobe_inst_table[hash]; | |
b67bfe0d | 1323 | hlist_for_each_entry_safe(ri, next, head, hlist) { |
ef53d9c5 S |
1324 | if (ri->rp == rp) |
1325 | ri->rp = NULL; | |
1326 | } | |
1327 | kretprobe_table_unlock(hash, &flags); | |
4a296e07 | 1328 | } |
4a296e07 MH |
1329 | free_rp_inst(rp); |
1330 | } | |
820aede0 | 1331 | NOKPROBE_SYMBOL(cleanup_rp_inst); |
4a296e07 | 1332 | |
059053a2 | 1333 | /* Add the new probe to ap->list */ |
55479f64 | 1334 | static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) |
8b0914ea | 1335 | { |
059053a2 | 1336 | if (p->post_handler) |
6274de49 | 1337 | unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ |
afd66255 | 1338 | |
059053a2 | 1339 | list_add_rcu(&p->list, &ap->list); |
b918e5e6 MH |
1340 | if (p->post_handler && !ap->post_handler) |
1341 | ap->post_handler = aggr_post_handler; | |
de5bd88d | 1342 | |
8b0914ea PP |
1343 | return 0; |
1344 | } | |
1345 | ||
64f562c6 AM |
1346 | /* |
1347 | * Fill in the required fields of the "manager kprobe". Replace the | |
1348 | * earlier kprobe in the hlist with the manager kprobe | |
1349 | */ | |
55479f64 | 1350 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) |
64f562c6 | 1351 | { |
afd66255 | 1352 | /* Copy p's insn slot to ap */ |
8b0914ea | 1353 | copy_kprobe(p, ap); |
a9ad965e | 1354 | flush_insn_slot(ap); |
64f562c6 | 1355 | ap->addr = p->addr; |
afd66255 | 1356 | ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED; |
64f562c6 | 1357 | ap->pre_handler = aggr_pre_handler; |
64f562c6 | 1358 | ap->fault_handler = aggr_fault_handler; |
e8386a0c MH |
1359 | /* We don't care the kprobe which has gone. */ |
1360 | if (p->post_handler && !kprobe_gone(p)) | |
36721656 | 1361 | ap->post_handler = aggr_post_handler; |
64f562c6 AM |
1362 | |
1363 | INIT_LIST_HEAD(&ap->list); | |
afd66255 | 1364 | INIT_HLIST_NODE(&ap->hlist); |
64f562c6 | 1365 | |
afd66255 | 1366 | list_add_rcu(&p->list, &ap->list); |
adad0f33 | 1367 | hlist_replace_rcu(&p->hlist, &ap->hlist); |
64f562c6 AM |
1368 | } |
1369 | ||
1370 | /* | |
1371 | * This is the second or subsequent kprobe at the address - handle | |
1372 | * the intricacies | |
64f562c6 | 1373 | */ |
55479f64 | 1374 | static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) |
64f562c6 AM |
1375 | { |
1376 | int ret = 0; | |
6d8e40a8 | 1377 | struct kprobe *ap = orig_p; |
64f562c6 | 1378 | |
2d1e38f5 TG |
1379 | cpus_read_lock(); |
1380 | ||
25764288 MH |
1381 | /* For preparing optimization, jump_label_text_reserved() is called */ |
1382 | jump_label_lock(); | |
25764288 MH |
1383 | mutex_lock(&text_mutex); |
1384 | ||
6d8e40a8 MH |
1385 | if (!kprobe_aggrprobe(orig_p)) { |
1386 | /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */ | |
1387 | ap = alloc_aggr_kprobe(orig_p); | |
25764288 MH |
1388 | if (!ap) { |
1389 | ret = -ENOMEM; | |
1390 | goto out; | |
1391 | } | |
6d8e40a8 | 1392 | init_aggr_kprobe(ap, orig_p); |
819319fc | 1393 | } else if (kprobe_unused(ap)) { |
0490cd1f | 1394 | /* This probe is going to die. Rescue it */ |
819319fc MH |
1395 | ret = reuse_unused_kprobe(ap); |
1396 | if (ret) | |
1397 | goto out; | |
1398 | } | |
b918e5e6 MH |
1399 | |
1400 | if (kprobe_gone(ap)) { | |
e8386a0c MH |
1401 | /* |
1402 | * Attempting to insert new probe at the same location that | |
1403 | * had a probe in the module vaddr area which already | |
1404 | * freed. So, the instruction slot has already been | |
1405 | * released. We need a new slot for the new probe. | |
1406 | */ | |
b918e5e6 | 1407 | ret = arch_prepare_kprobe(ap); |
e8386a0c | 1408 | if (ret) |
b918e5e6 MH |
1409 | /* |
1410 | * Even if fail to allocate new slot, don't need to | |
1411 | * free aggr_probe. It will be used next time, or | |
1412 | * freed by unregister_kprobe. | |
1413 | */ | |
25764288 | 1414 | goto out; |
de5bd88d | 1415 | |
afd66255 MH |
1416 | /* Prepare optimized instructions if possible. */ |
1417 | prepare_optimized_kprobe(ap); | |
1418 | ||
e8386a0c | 1419 | /* |
de5bd88d MH |
1420 | * Clear gone flag to prevent allocating new slot again, and |
1421 | * set disabled flag because it is not armed yet. | |
e8386a0c | 1422 | */ |
de5bd88d MH |
1423 | ap->flags = (ap->flags & ~KPROBE_FLAG_GONE) |
1424 | | KPROBE_FLAG_DISABLED; | |
e8386a0c | 1425 | } |
b918e5e6 | 1426 | |
afd66255 | 1427 | /* Copy ap's insn slot to p */ |
b918e5e6 | 1428 | copy_kprobe(ap, p); |
25764288 MH |
1429 | ret = add_new_kprobe(ap, p); |
1430 | ||
1431 | out: | |
1432 | mutex_unlock(&text_mutex); | |
25764288 | 1433 | jump_label_unlock(); |
2d1e38f5 | 1434 | cpus_read_unlock(); |
25764288 MH |
1435 | |
1436 | if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) { | |
1437 | ap->flags &= ~KPROBE_FLAG_DISABLED; | |
12310e34 | 1438 | if (!kprobes_all_disarmed) { |
25764288 | 1439 | /* Arm the breakpoint again. */ |
12310e34 JY |
1440 | ret = arm_kprobe(ap); |
1441 | if (ret) { | |
1442 | ap->flags |= KPROBE_FLAG_DISABLED; | |
1443 | list_del_rcu(&p->list); | |
ae8b7ce7 | 1444 | synchronize_rcu(); |
12310e34 JY |
1445 | } |
1446 | } | |
25764288 MH |
1447 | } |
1448 | return ret; | |
64f562c6 AM |
1449 | } |
1450 | ||
be8f2743 MH |
1451 | bool __weak arch_within_kprobe_blacklist(unsigned long addr) |
1452 | { | |
1453 | /* The __kprobes marked functions and entry code must not be probed */ | |
1454 | return addr >= (unsigned long)__kprobes_text_start && | |
1455 | addr < (unsigned long)__kprobes_text_end; | |
1456 | } | |
1457 | ||
6143c6fb | 1458 | static bool __within_kprobe_blacklist(unsigned long addr) |
d0aaff97 | 1459 | { |
376e2424 | 1460 | struct kprobe_blacklist_entry *ent; |
3d8d996e | 1461 | |
be8f2743 | 1462 | if (arch_within_kprobe_blacklist(addr)) |
376e2424 | 1463 | return true; |
3d8d996e SD |
1464 | /* |
1465 | * If there exists a kprobe_blacklist, verify and | |
1466 | * fail any probe registration in the prohibited area | |
1467 | */ | |
376e2424 MH |
1468 | list_for_each_entry(ent, &kprobe_blacklist, list) { |
1469 | if (addr >= ent->start_addr && addr < ent->end_addr) | |
1470 | return true; | |
3d8d996e | 1471 | } |
6143c6fb MH |
1472 | return false; |
1473 | } | |
376e2424 | 1474 | |
6143c6fb MH |
1475 | bool within_kprobe_blacklist(unsigned long addr) |
1476 | { | |
1477 | char symname[KSYM_NAME_LEN], *p; | |
1478 | ||
1479 | if (__within_kprobe_blacklist(addr)) | |
1480 | return true; | |
1481 | ||
1482 | /* Check if the address is on a suffixed-symbol */ | |
1483 | if (!lookup_symbol_name(addr, symname)) { | |
1484 | p = strchr(symname, '.'); | |
1485 | if (!p) | |
1486 | return false; | |
1487 | *p = '\0'; | |
1488 | addr = (unsigned long)kprobe_lookup_name(symname, 0); | |
1489 | if (addr) | |
1490 | return __within_kprobe_blacklist(addr); | |
1491 | } | |
376e2424 | 1492 | return false; |
d0aaff97 PP |
1493 | } |
1494 | ||
b2a5cd69 MH |
1495 | /* |
1496 | * If we have a symbol_name argument, look it up and add the offset field | |
1497 | * to it. This way, we can specify a relative address to a symbol. | |
bc81d48d MH |
1498 | * This returns encoded errors if it fails to look up symbol or invalid |
1499 | * combination of parameters. | |
b2a5cd69 | 1500 | */ |
1d585e70 NR |
1501 | static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, |
1502 | const char *symbol_name, unsigned int offset) | |
b2a5cd69 | 1503 | { |
1d585e70 | 1504 | if ((symbol_name && addr) || (!symbol_name && !addr)) |
bc81d48d MH |
1505 | goto invalid; |
1506 | ||
1d585e70 | 1507 | if (symbol_name) { |
7246f600 | 1508 | addr = kprobe_lookup_name(symbol_name, offset); |
bc81d48d MH |
1509 | if (!addr) |
1510 | return ERR_PTR(-ENOENT); | |
b2a5cd69 MH |
1511 | } |
1512 | ||
1d585e70 | 1513 | addr = (kprobe_opcode_t *)(((char *)addr) + offset); |
bc81d48d MH |
1514 | if (addr) |
1515 | return addr; | |
1516 | ||
1517 | invalid: | |
1518 | return ERR_PTR(-EINVAL); | |
b2a5cd69 MH |
1519 | } |
1520 | ||
1d585e70 NR |
1521 | static kprobe_opcode_t *kprobe_addr(struct kprobe *p) |
1522 | { | |
1523 | return _kprobe_addr(p->addr, p->symbol_name, p->offset); | |
1524 | } | |
1525 | ||
1f0ab409 | 1526 | /* Check passed kprobe is valid and return kprobe in kprobe_table. */ |
55479f64 | 1527 | static struct kprobe *__get_valid_kprobe(struct kprobe *p) |
1f0ab409 | 1528 | { |
6d8e40a8 | 1529 | struct kprobe *ap, *list_p; |
1f0ab409 | 1530 | |
7e6a71d8 MH |
1531 | lockdep_assert_held(&kprobe_mutex); |
1532 | ||
6d8e40a8 MH |
1533 | ap = get_kprobe(p->addr); |
1534 | if (unlikely(!ap)) | |
1f0ab409 AM |
1535 | return NULL; |
1536 | ||
6d8e40a8 | 1537 | if (p != ap) { |
7e6a71d8 | 1538 | list_for_each_entry(list_p, &ap->list, list) |
1f0ab409 AM |
1539 | if (list_p == p) |
1540 | /* kprobe p is a valid probe */ | |
1541 | goto valid; | |
1542 | return NULL; | |
1543 | } | |
1544 | valid: | |
6d8e40a8 | 1545 | return ap; |
1f0ab409 AM |
1546 | } |
1547 | ||
1548 | /* Return error if the kprobe is being re-registered */ | |
1549 | static inline int check_kprobe_rereg(struct kprobe *p) | |
1550 | { | |
1551 | int ret = 0; | |
1f0ab409 AM |
1552 | |
1553 | mutex_lock(&kprobe_mutex); | |
6d8e40a8 | 1554 | if (__get_valid_kprobe(p)) |
1f0ab409 AM |
1555 | ret = -EINVAL; |
1556 | mutex_unlock(&kprobe_mutex); | |
6d8e40a8 | 1557 | |
1f0ab409 AM |
1558 | return ret; |
1559 | } | |
1560 | ||
f7f242ff | 1561 | int __weak arch_check_ftrace_location(struct kprobe *p) |
1da177e4 | 1562 | { |
ae6aa16f MH |
1563 | unsigned long ftrace_addr; |
1564 | ||
ae6aa16f MH |
1565 | ftrace_addr = ftrace_location((unsigned long)p->addr); |
1566 | if (ftrace_addr) { | |
e7dbfe34 | 1567 | #ifdef CONFIG_KPROBES_ON_FTRACE |
ae6aa16f MH |
1568 | /* Given address is not on the instruction boundary */ |
1569 | if ((unsigned long)p->addr != ftrace_addr) | |
1570 | return -EILSEQ; | |
ae6aa16f | 1571 | p->flags |= KPROBE_FLAG_FTRACE; |
e7dbfe34 | 1572 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
ae6aa16f MH |
1573 | return -EINVAL; |
1574 | #endif | |
1575 | } | |
f7f242ff HC |
1576 | return 0; |
1577 | } | |
1578 | ||
1579 | static int check_kprobe_address_safe(struct kprobe *p, | |
1580 | struct module **probed_mod) | |
1581 | { | |
1582 | int ret; | |
1f0ab409 | 1583 | |
f7f242ff HC |
1584 | ret = arch_check_ftrace_location(p); |
1585 | if (ret) | |
1586 | return ret; | |
91bad2f8 | 1587 | jump_label_lock(); |
de31c3ca | 1588 | preempt_disable(); |
f7fa6ef0 MH |
1589 | |
1590 | /* Ensure it is not in reserved area nor out of text */ | |
ec30c5f3 | 1591 | if (!kernel_text_address((unsigned long) p->addr) || |
376e2424 | 1592 | within_kprobe_blacklist((unsigned long) p->addr) || |
e336b402 MH |
1593 | jump_label_text_reserved(p->addr, p->addr) || |
1594 | find_bug((unsigned long)p->addr)) { | |
f986a499 | 1595 | ret = -EINVAL; |
f7fa6ef0 | 1596 | goto out; |
f986a499 | 1597 | } |
b3e55c72 | 1598 | |
f7fa6ef0 MH |
1599 | /* Check if are we probing a module */ |
1600 | *probed_mod = __module_text_address((unsigned long) p->addr); | |
1601 | if (*probed_mod) { | |
6f716acd | 1602 | /* |
e8386a0c MH |
1603 | * We must hold a refcount of the probed module while updating |
1604 | * its code to prohibit unexpected unloading. | |
df019b1d | 1605 | */ |
f7fa6ef0 MH |
1606 | if (unlikely(!try_module_get(*probed_mod))) { |
1607 | ret = -ENOENT; | |
1608 | goto out; | |
1609 | } | |
de31c3ca | 1610 | |
f24659d9 MH |
1611 | /* |
1612 | * If the module freed .init.text, we couldn't insert | |
1613 | * kprobes in there. | |
1614 | */ | |
f7fa6ef0 MH |
1615 | if (within_module_init((unsigned long)p->addr, *probed_mod) && |
1616 | (*probed_mod)->state != MODULE_STATE_COMING) { | |
1617 | module_put(*probed_mod); | |
1618 | *probed_mod = NULL; | |
1619 | ret = -ENOENT; | |
f24659d9 | 1620 | } |
df019b1d | 1621 | } |
f7fa6ef0 | 1622 | out: |
a189d035 | 1623 | preempt_enable(); |
de31c3ca | 1624 | jump_label_unlock(); |
1da177e4 | 1625 | |
f7fa6ef0 MH |
1626 | return ret; |
1627 | } | |
1628 | ||
55479f64 | 1629 | int register_kprobe(struct kprobe *p) |
f7fa6ef0 MH |
1630 | { |
1631 | int ret; | |
1632 | struct kprobe *old_p; | |
1633 | struct module *probed_mod; | |
1634 | kprobe_opcode_t *addr; | |
1635 | ||
1636 | /* Adjust probe address from symbol */ | |
1637 | addr = kprobe_addr(p); | |
1638 | if (IS_ERR(addr)) | |
1639 | return PTR_ERR(addr); | |
1640 | p->addr = addr; | |
1641 | ||
1642 | ret = check_kprobe_rereg(p); | |
1643 | if (ret) | |
1644 | return ret; | |
1645 | ||
1646 | /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ | |
1647 | p->flags &= KPROBE_FLAG_DISABLED; | |
3516a460 | 1648 | p->nmissed = 0; |
9861668f | 1649 | INIT_LIST_HEAD(&p->list); |
afd66255 | 1650 | |
f7fa6ef0 MH |
1651 | ret = check_kprobe_address_safe(p, &probed_mod); |
1652 | if (ret) | |
1653 | return ret; | |
1654 | ||
1655 | mutex_lock(&kprobe_mutex); | |
afd66255 | 1656 | |
64f562c6 AM |
1657 | old_p = get_kprobe(p->addr); |
1658 | if (old_p) { | |
afd66255 | 1659 | /* Since this may unoptimize old_p, locking text_mutex. */ |
64f562c6 | 1660 | ret = register_aggr_kprobe(old_p, p); |
1da177e4 LT |
1661 | goto out; |
1662 | } | |
1da177e4 | 1663 | |
2d1e38f5 TG |
1664 | cpus_read_lock(); |
1665 | /* Prevent text modification */ | |
1666 | mutex_lock(&text_mutex); | |
ae6aa16f | 1667 | ret = prepare_kprobe(p); |
25764288 | 1668 | mutex_unlock(&text_mutex); |
2d1e38f5 | 1669 | cpus_read_unlock(); |
6f716acd | 1670 | if (ret) |
afd66255 | 1671 | goto out; |
49a2a1b8 | 1672 | |
64f562c6 | 1673 | INIT_HLIST_NODE(&p->hlist); |
3516a460 | 1674 | hlist_add_head_rcu(&p->hlist, |
1da177e4 LT |
1675 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
1676 | ||
12310e34 JY |
1677 | if (!kprobes_all_disarmed && !kprobe_disabled(p)) { |
1678 | ret = arm_kprobe(p); | |
1679 | if (ret) { | |
1680 | hlist_del_rcu(&p->hlist); | |
ae8b7ce7 | 1681 | synchronize_rcu(); |
12310e34 JY |
1682 | goto out; |
1683 | } | |
1684 | } | |
afd66255 MH |
1685 | |
1686 | /* Try to optimize kprobe */ | |
1687 | try_to_optimize_kprobe(p); | |
1da177e4 | 1688 | out: |
7a7d1cf9 | 1689 | mutex_unlock(&kprobe_mutex); |
49a2a1b8 | 1690 | |
e8386a0c | 1691 | if (probed_mod) |
df019b1d | 1692 | module_put(probed_mod); |
e8386a0c | 1693 | |
1da177e4 LT |
1694 | return ret; |
1695 | } | |
99081ab5 | 1696 | EXPORT_SYMBOL_GPL(register_kprobe); |
1da177e4 | 1697 | |
6f0f1dd7 | 1698 | /* Check if all probes on the aggrprobe are disabled */ |
55479f64 | 1699 | static int aggr_kprobe_disabled(struct kprobe *ap) |
6f0f1dd7 MH |
1700 | { |
1701 | struct kprobe *kp; | |
1702 | ||
7e6a71d8 MH |
1703 | lockdep_assert_held(&kprobe_mutex); |
1704 | ||
1705 | list_for_each_entry(kp, &ap->list, list) | |
6f0f1dd7 MH |
1706 | if (!kprobe_disabled(kp)) |
1707 | /* | |
1708 | * There is an active probe on the list. | |
1709 | * We can't disable this ap. | |
1710 | */ | |
1711 | return 0; | |
1712 | ||
1713 | return 1; | |
1714 | } | |
1715 | ||
1716 | /* Disable one kprobe: Make sure called under kprobe_mutex is locked */ | |
55479f64 | 1717 | static struct kprobe *__disable_kprobe(struct kprobe *p) |
6f0f1dd7 MH |
1718 | { |
1719 | struct kprobe *orig_p; | |
297f9233 | 1720 | int ret; |
6f0f1dd7 MH |
1721 | |
1722 | /* Get an original kprobe for return */ | |
1723 | orig_p = __get_valid_kprobe(p); | |
1724 | if (unlikely(orig_p == NULL)) | |
297f9233 | 1725 | return ERR_PTR(-EINVAL); |
6f0f1dd7 MH |
1726 | |
1727 | if (!kprobe_disabled(p)) { | |
1728 | /* Disable probe if it is a child probe */ | |
1729 | if (p != orig_p) | |
1730 | p->flags |= KPROBE_FLAG_DISABLED; | |
1731 | ||
1732 | /* Try to disarm and disable this/parent probe */ | |
1733 | if (p == orig_p || aggr_kprobe_disabled(orig_p)) { | |
69d54b91 WN |
1734 | /* |
1735 | * If kprobes_all_disarmed is set, orig_p | |
1736 | * should have already been disarmed, so | |
1737 | * skip unneed disarming process. | |
1738 | */ | |
297f9233 JY |
1739 | if (!kprobes_all_disarmed) { |
1740 | ret = disarm_kprobe(orig_p, true); | |
1741 | if (ret) { | |
1742 | p->flags &= ~KPROBE_FLAG_DISABLED; | |
1743 | return ERR_PTR(ret); | |
1744 | } | |
1745 | } | |
6f0f1dd7 MH |
1746 | orig_p->flags |= KPROBE_FLAG_DISABLED; |
1747 | } | |
1748 | } | |
1749 | ||
1750 | return orig_p; | |
1751 | } | |
1752 | ||
de5bd88d MH |
1753 | /* |
1754 | * Unregister a kprobe without a scheduler synchronization. | |
1755 | */ | |
55479f64 | 1756 | static int __unregister_kprobe_top(struct kprobe *p) |
de5bd88d | 1757 | { |
6d8e40a8 | 1758 | struct kprobe *ap, *list_p; |
de5bd88d | 1759 | |
6f0f1dd7 MH |
1760 | /* Disable kprobe. This will disarm it if needed. */ |
1761 | ap = __disable_kprobe(p); | |
297f9233 JY |
1762 | if (IS_ERR(ap)) |
1763 | return PTR_ERR(ap); | |
de5bd88d | 1764 | |
6f0f1dd7 | 1765 | if (ap == p) |
bf8f6e5b | 1766 | /* |
6f0f1dd7 MH |
1767 | * This probe is an independent(and non-optimized) kprobe |
1768 | * (not an aggrprobe). Remove from the hash list. | |
bf8f6e5b | 1769 | */ |
6f0f1dd7 MH |
1770 | goto disarmed; |
1771 | ||
1772 | /* Following process expects this probe is an aggrprobe */ | |
1773 | WARN_ON(!kprobe_aggrprobe(ap)); | |
1774 | ||
6274de49 MH |
1775 | if (list_is_singular(&ap->list) && kprobe_disarmed(ap)) |
1776 | /* | |
1777 | * !disarmed could be happen if the probe is under delayed | |
1778 | * unoptimizing. | |
1779 | */ | |
6f0f1dd7 MH |
1780 | goto disarmed; |
1781 | else { | |
1782 | /* If disabling probe has special handlers, update aggrprobe */ | |
e8386a0c | 1783 | if (p->post_handler && !kprobe_gone(p)) { |
7e6a71d8 | 1784 | list_for_each_entry(list_p, &ap->list, list) { |
9861668f MH |
1785 | if ((list_p != p) && (list_p->post_handler)) |
1786 | goto noclean; | |
1787 | } | |
6d8e40a8 | 1788 | ap->post_handler = NULL; |
9861668f MH |
1789 | } |
1790 | noclean: | |
6f0f1dd7 MH |
1791 | /* |
1792 | * Remove from the aggrprobe: this path will do nothing in | |
1793 | * __unregister_kprobe_bottom(). | |
1794 | */ | |
49a2a1b8 | 1795 | list_del_rcu(&p->list); |
6f0f1dd7 MH |
1796 | if (!kprobe_disabled(ap) && !kprobes_all_disarmed) |
1797 | /* | |
1798 | * Try to optimize this probe again, because post | |
1799 | * handler may have been changed. | |
1800 | */ | |
1801 | optimize_kprobe(ap); | |
49a2a1b8 | 1802 | } |
9861668f | 1803 | return 0; |
6f0f1dd7 MH |
1804 | |
1805 | disarmed: | |
1806 | hlist_del_rcu(&ap->hlist); | |
1807 | return 0; | |
9861668f | 1808 | } |
3516a460 | 1809 | |
55479f64 | 1810 | static void __unregister_kprobe_bottom(struct kprobe *p) |
9861668f | 1811 | { |
6d8e40a8 | 1812 | struct kprobe *ap; |
b3e55c72 | 1813 | |
e8386a0c | 1814 | if (list_empty(&p->list)) |
6274de49 | 1815 | /* This is an independent kprobe */ |
0498b635 | 1816 | arch_remove_kprobe(p); |
e8386a0c | 1817 | else if (list_is_singular(&p->list)) { |
6274de49 | 1818 | /* This is the last child of an aggrprobe */ |
6d8e40a8 | 1819 | ap = list_entry(p->list.next, struct kprobe, list); |
e8386a0c | 1820 | list_del(&p->list); |
6d8e40a8 | 1821 | free_aggr_kprobe(ap); |
9861668f | 1822 | } |
6274de49 | 1823 | /* Otherwise, do nothing. */ |
9861668f MH |
1824 | } |
1825 | ||
55479f64 | 1826 | int register_kprobes(struct kprobe **kps, int num) |
9861668f MH |
1827 | { |
1828 | int i, ret = 0; | |
1829 | ||
1830 | if (num <= 0) | |
1831 | return -EINVAL; | |
1832 | for (i = 0; i < num; i++) { | |
49ad2fd7 | 1833 | ret = register_kprobe(kps[i]); |
67dddaad MH |
1834 | if (ret < 0) { |
1835 | if (i > 0) | |
1836 | unregister_kprobes(kps, i); | |
9861668f | 1837 | break; |
36721656 | 1838 | } |
49a2a1b8 | 1839 | } |
9861668f MH |
1840 | return ret; |
1841 | } | |
99081ab5 | 1842 | EXPORT_SYMBOL_GPL(register_kprobes); |
9861668f | 1843 | |
55479f64 | 1844 | void unregister_kprobe(struct kprobe *p) |
9861668f MH |
1845 | { |
1846 | unregister_kprobes(&p, 1); | |
1847 | } | |
99081ab5 | 1848 | EXPORT_SYMBOL_GPL(unregister_kprobe); |
9861668f | 1849 | |
55479f64 | 1850 | void unregister_kprobes(struct kprobe **kps, int num) |
9861668f MH |
1851 | { |
1852 | int i; | |
1853 | ||
1854 | if (num <= 0) | |
1855 | return; | |
1856 | mutex_lock(&kprobe_mutex); | |
1857 | for (i = 0; i < num; i++) | |
1858 | if (__unregister_kprobe_top(kps[i]) < 0) | |
1859 | kps[i]->addr = NULL; | |
1860 | mutex_unlock(&kprobe_mutex); | |
1861 | ||
ae8b7ce7 | 1862 | synchronize_rcu(); |
9861668f MH |
1863 | for (i = 0; i < num; i++) |
1864 | if (kps[i]->addr) | |
1865 | __unregister_kprobe_bottom(kps[i]); | |
1da177e4 | 1866 | } |
99081ab5 | 1867 | EXPORT_SYMBOL_GPL(unregister_kprobes); |
1da177e4 | 1868 | |
5f6bee34 NR |
1869 | int __weak kprobe_exceptions_notify(struct notifier_block *self, |
1870 | unsigned long val, void *data) | |
fc62d020 NR |
1871 | { |
1872 | return NOTIFY_DONE; | |
1873 | } | |
5f6bee34 | 1874 | NOKPROBE_SYMBOL(kprobe_exceptions_notify); |
fc62d020 | 1875 | |
1da177e4 | 1876 | static struct notifier_block kprobe_exceptions_nb = { |
3d5631e0 AK |
1877 | .notifier_call = kprobe_exceptions_notify, |
1878 | .priority = 0x7fffffff /* we need to be notified first */ | |
1879 | }; | |
1880 | ||
3d7e3382 ME |
1881 | unsigned long __weak arch_deref_entry_point(void *entry) |
1882 | { | |
1883 | return (unsigned long)entry; | |
1884 | } | |
1da177e4 | 1885 | |
9edddaa2 | 1886 | #ifdef CONFIG_KRETPROBES |
e65cefe8 AB |
1887 | /* |
1888 | * This kprobe pre_handler is registered with every kretprobe. When probe | |
1889 | * hits it will set up the return probe. | |
1890 | */ | |
820aede0 | 1891 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
e65cefe8 AB |
1892 | { |
1893 | struct kretprobe *rp = container_of(p, struct kretprobe, kp); | |
ef53d9c5 S |
1894 | unsigned long hash, flags = 0; |
1895 | struct kretprobe_instance *ri; | |
e65cefe8 | 1896 | |
f96f5678 MH |
1897 | /* |
1898 | * To avoid deadlocks, prohibit return probing in NMI contexts, | |
1899 | * just skip the probe and increase the (inexact) 'nmissed' | |
1900 | * statistical counter, so that the user is informed that | |
1901 | * something happened: | |
1902 | */ | |
1903 | if (unlikely(in_nmi())) { | |
1904 | rp->nmissed++; | |
1905 | return 0; | |
1906 | } | |
1907 | ||
1908 | /* TODO: consider to only swap the RA after the last pre_handler fired */ | |
ef53d9c5 | 1909 | hash = hash_ptr(current, KPROBE_HASH_BITS); |
ec484608 | 1910 | raw_spin_lock_irqsave(&rp->lock, flags); |
4c4308cb | 1911 | if (!hlist_empty(&rp->free_instances)) { |
4c4308cb | 1912 | ri = hlist_entry(rp->free_instances.first, |
ef53d9c5 S |
1913 | struct kretprobe_instance, hlist); |
1914 | hlist_del(&ri->hlist); | |
ec484608 | 1915 | raw_spin_unlock_irqrestore(&rp->lock, flags); |
ef53d9c5 | 1916 | |
4c4308cb CH |
1917 | ri->rp = rp; |
1918 | ri->task = current; | |
f47cd9b5 | 1919 | |
55ca6140 JL |
1920 | if (rp->entry_handler && rp->entry_handler(ri, regs)) { |
1921 | raw_spin_lock_irqsave(&rp->lock, flags); | |
1922 | hlist_add_head(&ri->hlist, &rp->free_instances); | |
1923 | raw_spin_unlock_irqrestore(&rp->lock, flags); | |
f47cd9b5 | 1924 | return 0; |
55ca6140 | 1925 | } |
f47cd9b5 | 1926 | |
4c4308cb CH |
1927 | arch_prepare_kretprobe(ri, regs); |
1928 | ||
1929 | /* XXX(hch): why is there no hlist_move_head? */ | |
ef53d9c5 S |
1930 | INIT_HLIST_NODE(&ri->hlist); |
1931 | kretprobe_table_lock(hash, &flags); | |
1932 | hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]); | |
1933 | kretprobe_table_unlock(hash, &flags); | |
1934 | } else { | |
4c4308cb | 1935 | rp->nmissed++; |
ec484608 | 1936 | raw_spin_unlock_irqrestore(&rp->lock, flags); |
ef53d9c5 | 1937 | } |
e65cefe8 AB |
1938 | return 0; |
1939 | } | |
820aede0 | 1940 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
e65cefe8 | 1941 | |
659b957f | 1942 | bool __weak arch_kprobe_on_func_entry(unsigned long offset) |
90ec5e89 NR |
1943 | { |
1944 | return !offset; | |
1945 | } | |
1946 | ||
659b957f | 1947 | bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset) |
1d585e70 NR |
1948 | { |
1949 | kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset); | |
1950 | ||
1951 | if (IS_ERR(kp_addr)) | |
1952 | return false; | |
1953 | ||
1954 | if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) || | |
659b957f | 1955 | !arch_kprobe_on_func_entry(offset)) |
1d585e70 NR |
1956 | return false; |
1957 | ||
1958 | return true; | |
1959 | } | |
1960 | ||
55479f64 | 1961 | int register_kretprobe(struct kretprobe *rp) |
b94cce92 HN |
1962 | { |
1963 | int ret = 0; | |
1964 | struct kretprobe_instance *inst; | |
1965 | int i; | |
b2a5cd69 | 1966 | void *addr; |
90ec5e89 | 1967 | |
659b957f | 1968 | if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset)) |
90ec5e89 | 1969 | return -EINVAL; |
f438d914 MH |
1970 | |
1971 | if (kretprobe_blacklist_size) { | |
b2a5cd69 | 1972 | addr = kprobe_addr(&rp->kp); |
bc81d48d MH |
1973 | if (IS_ERR(addr)) |
1974 | return PTR_ERR(addr); | |
f438d914 MH |
1975 | |
1976 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { | |
1977 | if (kretprobe_blacklist[i].addr == addr) | |
1978 | return -EINVAL; | |
1979 | } | |
1980 | } | |
b94cce92 HN |
1981 | |
1982 | rp->kp.pre_handler = pre_handler_kretprobe; | |
7522a842 AM |
1983 | rp->kp.post_handler = NULL; |
1984 | rp->kp.fault_handler = NULL; | |
b94cce92 HN |
1985 | |
1986 | /* Pre-allocate memory for max kretprobe instances */ | |
1987 | if (rp->maxactive <= 0) { | |
92616606 | 1988 | #ifdef CONFIG_PREEMPTION |
c2ef6661 | 1989 | rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus()); |
b94cce92 | 1990 | #else |
4dae560f | 1991 | rp->maxactive = num_possible_cpus(); |
b94cce92 HN |
1992 | #endif |
1993 | } | |
ec484608 | 1994 | raw_spin_lock_init(&rp->lock); |
b94cce92 HN |
1995 | INIT_HLIST_HEAD(&rp->free_instances); |
1996 | for (i = 0; i < rp->maxactive; i++) { | |
f47cd9b5 AS |
1997 | inst = kmalloc(sizeof(struct kretprobe_instance) + |
1998 | rp->data_size, GFP_KERNEL); | |
b94cce92 HN |
1999 | if (inst == NULL) { |
2000 | free_rp_inst(rp); | |
2001 | return -ENOMEM; | |
2002 | } | |
ef53d9c5 S |
2003 | INIT_HLIST_NODE(&inst->hlist); |
2004 | hlist_add_head(&inst->hlist, &rp->free_instances); | |
b94cce92 HN |
2005 | } |
2006 | ||
2007 | rp->nmissed = 0; | |
2008 | /* Establish function entry probe point */ | |
49ad2fd7 | 2009 | ret = register_kprobe(&rp->kp); |
4a296e07 | 2010 | if (ret != 0) |
b94cce92 HN |
2011 | free_rp_inst(rp); |
2012 | return ret; | |
2013 | } | |
99081ab5 | 2014 | EXPORT_SYMBOL_GPL(register_kretprobe); |
b94cce92 | 2015 | |
55479f64 | 2016 | int register_kretprobes(struct kretprobe **rps, int num) |
4a296e07 MH |
2017 | { |
2018 | int ret = 0, i; | |
2019 | ||
2020 | if (num <= 0) | |
2021 | return -EINVAL; | |
2022 | for (i = 0; i < num; i++) { | |
49ad2fd7 | 2023 | ret = register_kretprobe(rps[i]); |
67dddaad MH |
2024 | if (ret < 0) { |
2025 | if (i > 0) | |
2026 | unregister_kretprobes(rps, i); | |
4a296e07 MH |
2027 | break; |
2028 | } | |
2029 | } | |
2030 | return ret; | |
2031 | } | |
99081ab5 | 2032 | EXPORT_SYMBOL_GPL(register_kretprobes); |
4a296e07 | 2033 | |
55479f64 | 2034 | void unregister_kretprobe(struct kretprobe *rp) |
4a296e07 MH |
2035 | { |
2036 | unregister_kretprobes(&rp, 1); | |
2037 | } | |
99081ab5 | 2038 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
4a296e07 | 2039 | |
55479f64 | 2040 | void unregister_kretprobes(struct kretprobe **rps, int num) |
4a296e07 MH |
2041 | { |
2042 | int i; | |
2043 | ||
2044 | if (num <= 0) | |
2045 | return; | |
2046 | mutex_lock(&kprobe_mutex); | |
2047 | for (i = 0; i < num; i++) | |
2048 | if (__unregister_kprobe_top(&rps[i]->kp) < 0) | |
2049 | rps[i]->kp.addr = NULL; | |
2050 | mutex_unlock(&kprobe_mutex); | |
2051 | ||
ae8b7ce7 | 2052 | synchronize_rcu(); |
4a296e07 MH |
2053 | for (i = 0; i < num; i++) { |
2054 | if (rps[i]->kp.addr) { | |
2055 | __unregister_kprobe_bottom(&rps[i]->kp); | |
2056 | cleanup_rp_inst(rps[i]); | |
2057 | } | |
2058 | } | |
2059 | } | |
99081ab5 | 2060 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
4a296e07 | 2061 | |
9edddaa2 | 2062 | #else /* CONFIG_KRETPROBES */ |
55479f64 | 2063 | int register_kretprobe(struct kretprobe *rp) |
b94cce92 HN |
2064 | { |
2065 | return -ENOSYS; | |
2066 | } | |
99081ab5 | 2067 | EXPORT_SYMBOL_GPL(register_kretprobe); |
b94cce92 | 2068 | |
55479f64 | 2069 | int register_kretprobes(struct kretprobe **rps, int num) |
346fd59b | 2070 | { |
4a296e07 | 2071 | return -ENOSYS; |
346fd59b | 2072 | } |
99081ab5 MH |
2073 | EXPORT_SYMBOL_GPL(register_kretprobes); |
2074 | ||
55479f64 | 2075 | void unregister_kretprobe(struct kretprobe *rp) |
b94cce92 | 2076 | { |
4a296e07 | 2077 | } |
99081ab5 | 2078 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
b94cce92 | 2079 | |
55479f64 | 2080 | void unregister_kretprobes(struct kretprobe **rps, int num) |
4a296e07 MH |
2081 | { |
2082 | } | |
99081ab5 | 2083 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
4c4308cb | 2084 | |
820aede0 | 2085 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
4a296e07 MH |
2086 | { |
2087 | return 0; | |
b94cce92 | 2088 | } |
820aede0 | 2089 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
b94cce92 | 2090 | |
4a296e07 MH |
2091 | #endif /* CONFIG_KRETPROBES */ |
2092 | ||
e8386a0c | 2093 | /* Set the kprobe gone and remove its instruction buffer. */ |
55479f64 | 2094 | static void kill_kprobe(struct kprobe *p) |
e8386a0c MH |
2095 | { |
2096 | struct kprobe *kp; | |
de5bd88d | 2097 | |
7e6a71d8 MH |
2098 | lockdep_assert_held(&kprobe_mutex); |
2099 | ||
e8386a0c | 2100 | p->flags |= KPROBE_FLAG_GONE; |
afd66255 | 2101 | if (kprobe_aggrprobe(p)) { |
e8386a0c MH |
2102 | /* |
2103 | * If this is an aggr_kprobe, we have to list all the | |
2104 | * chained probes and mark them GONE. | |
2105 | */ | |
7e6a71d8 | 2106 | list_for_each_entry(kp, &p->list, list) |
e8386a0c MH |
2107 | kp->flags |= KPROBE_FLAG_GONE; |
2108 | p->post_handler = NULL; | |
afd66255 | 2109 | kill_optimized_kprobe(p); |
e8386a0c MH |
2110 | } |
2111 | /* | |
2112 | * Here, we can remove insn_slot safely, because no thread calls | |
2113 | * the original probed function (which will be freed soon) any more. | |
2114 | */ | |
2115 | arch_remove_kprobe(p); | |
0cb2f137 MS |
2116 | |
2117 | /* | |
2118 | * The module is going away. We should disarm the kprobe which | |
2119 | * is using ftrace. | |
2120 | */ | |
2121 | if (kprobe_ftrace(p)) | |
2122 | disarm_kprobe_ftrace(p); | |
e8386a0c MH |
2123 | } |
2124 | ||
c0614829 | 2125 | /* Disable one kprobe */ |
55479f64 | 2126 | int disable_kprobe(struct kprobe *kp) |
c0614829 MH |
2127 | { |
2128 | int ret = 0; | |
297f9233 | 2129 | struct kprobe *p; |
c0614829 MH |
2130 | |
2131 | mutex_lock(&kprobe_mutex); | |
2132 | ||
6f0f1dd7 | 2133 | /* Disable this kprobe */ |
297f9233 JY |
2134 | p = __disable_kprobe(kp); |
2135 | if (IS_ERR(p)) | |
2136 | ret = PTR_ERR(p); | |
c0614829 | 2137 | |
c0614829 MH |
2138 | mutex_unlock(&kprobe_mutex); |
2139 | return ret; | |
2140 | } | |
2141 | EXPORT_SYMBOL_GPL(disable_kprobe); | |
2142 | ||
2143 | /* Enable one kprobe */ | |
55479f64 | 2144 | int enable_kprobe(struct kprobe *kp) |
c0614829 MH |
2145 | { |
2146 | int ret = 0; | |
2147 | struct kprobe *p; | |
2148 | ||
2149 | mutex_lock(&kprobe_mutex); | |
2150 | ||
2151 | /* Check whether specified probe is valid. */ | |
2152 | p = __get_valid_kprobe(kp); | |
2153 | if (unlikely(p == NULL)) { | |
2154 | ret = -EINVAL; | |
2155 | goto out; | |
2156 | } | |
2157 | ||
2158 | if (kprobe_gone(kp)) { | |
2159 | /* This kprobe has gone, we couldn't enable it. */ | |
2160 | ret = -EINVAL; | |
2161 | goto out; | |
2162 | } | |
2163 | ||
2164 | if (p != kp) | |
2165 | kp->flags &= ~KPROBE_FLAG_DISABLED; | |
2166 | ||
2167 | if (!kprobes_all_disarmed && kprobe_disabled(p)) { | |
2168 | p->flags &= ~KPROBE_FLAG_DISABLED; | |
12310e34 JY |
2169 | ret = arm_kprobe(p); |
2170 | if (ret) | |
2171 | p->flags |= KPROBE_FLAG_DISABLED; | |
c0614829 MH |
2172 | } |
2173 | out: | |
2174 | mutex_unlock(&kprobe_mutex); | |
2175 | return ret; | |
2176 | } | |
2177 | EXPORT_SYMBOL_GPL(enable_kprobe); | |
2178 | ||
4458515b | 2179 | /* Caller must NOT call this in usual path. This is only for critical case */ |
820aede0 | 2180 | void dump_kprobe(struct kprobe *kp) |
24851d24 | 2181 | { |
4458515b MH |
2182 | pr_err("Dumping kprobe:\n"); |
2183 | pr_err("Name: %s\nOffset: %x\nAddress: %pS\n", | |
2184 | kp->symbol_name, kp->offset, kp->addr); | |
24851d24 | 2185 | } |
820aede0 | 2186 | NOKPROBE_SYMBOL(dump_kprobe); |
24851d24 | 2187 | |
fb1a59fa MH |
2188 | int kprobe_add_ksym_blacklist(unsigned long entry) |
2189 | { | |
2190 | struct kprobe_blacklist_entry *ent; | |
2191 | unsigned long offset = 0, size = 0; | |
2192 | ||
2193 | if (!kernel_text_address(entry) || | |
2194 | !kallsyms_lookup_size_offset(entry, &size, &offset)) | |
2195 | return -EINVAL; | |
2196 | ||
2197 | ent = kmalloc(sizeof(*ent), GFP_KERNEL); | |
2198 | if (!ent) | |
2199 | return -ENOMEM; | |
2200 | ent->start_addr = entry; | |
2201 | ent->end_addr = entry + size; | |
2202 | INIT_LIST_HEAD(&ent->list); | |
2203 | list_add_tail(&ent->list, &kprobe_blacklist); | |
2204 | ||
2205 | return (int)size; | |
2206 | } | |
2207 | ||
2208 | /* Add all symbols in given area into kprobe blacklist */ | |
2209 | int kprobe_add_area_blacklist(unsigned long start, unsigned long end) | |
2210 | { | |
2211 | unsigned long entry; | |
2212 | int ret = 0; | |
2213 | ||
2214 | for (entry = start; entry < end; entry += ret) { | |
2215 | ret = kprobe_add_ksym_blacklist(entry); | |
2216 | if (ret < 0) | |
2217 | return ret; | |
2218 | if (ret == 0) /* In case of alias symbol */ | |
2219 | ret = 1; | |
2220 | } | |
2221 | return 0; | |
2222 | } | |
2223 | ||
1e6769b0 MH |
2224 | /* Remove all symbols in given area from kprobe blacklist */ |
2225 | static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end) | |
2226 | { | |
2227 | struct kprobe_blacklist_entry *ent, *n; | |
2228 | ||
2229 | list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) { | |
2230 | if (ent->start_addr < start || ent->start_addr >= end) | |
2231 | continue; | |
2232 | list_del(&ent->list); | |
2233 | kfree(ent); | |
2234 | } | |
2235 | } | |
2236 | ||
16db6264 MH |
2237 | static void kprobe_remove_ksym_blacklist(unsigned long entry) |
2238 | { | |
2239 | kprobe_remove_area_blacklist(entry, entry + 1); | |
2240 | } | |
2241 | ||
fb1a59fa MH |
2242 | int __init __weak arch_populate_kprobe_blacklist(void) |
2243 | { | |
2244 | return 0; | |
2245 | } | |
2246 | ||
376e2424 MH |
2247 | /* |
2248 | * Lookup and populate the kprobe_blacklist. | |
2249 | * | |
2250 | * Unlike the kretprobe blacklist, we'll need to determine | |
2251 | * the range of addresses that belong to the said functions, | |
2252 | * since a kprobe need not necessarily be at the beginning | |
2253 | * of a function. | |
2254 | */ | |
2255 | static int __init populate_kprobe_blacklist(unsigned long *start, | |
2256 | unsigned long *end) | |
2257 | { | |
fb1a59fa | 2258 | unsigned long entry; |
376e2424 | 2259 | unsigned long *iter; |
fb1a59fa | 2260 | int ret; |
376e2424 MH |
2261 | |
2262 | for (iter = start; iter < end; iter++) { | |
d81b4253 | 2263 | entry = arch_deref_entry_point((void *)*iter); |
fb1a59fa MH |
2264 | ret = kprobe_add_ksym_blacklist(entry); |
2265 | if (ret == -EINVAL) | |
376e2424 | 2266 | continue; |
fb1a59fa MH |
2267 | if (ret < 0) |
2268 | return ret; | |
376e2424 | 2269 | } |
fb1a59fa MH |
2270 | |
2271 | /* Symbols in __kprobes_text are blacklisted */ | |
2272 | ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start, | |
2273 | (unsigned long)__kprobes_text_end); | |
66e9b071 TG |
2274 | if (ret) |
2275 | return ret; | |
2276 | ||
2277 | /* Symbols in noinstr section are blacklisted */ | |
2278 | ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start, | |
2279 | (unsigned long)__noinstr_text_end); | |
fb1a59fa MH |
2280 | |
2281 | return ret ? : arch_populate_kprobe_blacklist(); | |
376e2424 MH |
2282 | } |
2283 | ||
1e6769b0 MH |
2284 | static void add_module_kprobe_blacklist(struct module *mod) |
2285 | { | |
2286 | unsigned long start, end; | |
16db6264 MH |
2287 | int i; |
2288 | ||
2289 | if (mod->kprobe_blacklist) { | |
2290 | for (i = 0; i < mod->num_kprobe_blacklist; i++) | |
2291 | kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]); | |
2292 | } | |
1e6769b0 MH |
2293 | |
2294 | start = (unsigned long)mod->kprobes_text_start; | |
2295 | if (start) { | |
2296 | end = start + mod->kprobes_text_size; | |
2297 | kprobe_add_area_blacklist(start, end); | |
2298 | } | |
66e9b071 TG |
2299 | |
2300 | start = (unsigned long)mod->noinstr_text_start; | |
2301 | if (start) { | |
2302 | end = start + mod->noinstr_text_size; | |
2303 | kprobe_add_area_blacklist(start, end); | |
2304 | } | |
1e6769b0 MH |
2305 | } |
2306 | ||
2307 | static void remove_module_kprobe_blacklist(struct module *mod) | |
2308 | { | |
2309 | unsigned long start, end; | |
16db6264 MH |
2310 | int i; |
2311 | ||
2312 | if (mod->kprobe_blacklist) { | |
2313 | for (i = 0; i < mod->num_kprobe_blacklist; i++) | |
2314 | kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]); | |
2315 | } | |
1e6769b0 MH |
2316 | |
2317 | start = (unsigned long)mod->kprobes_text_start; | |
2318 | if (start) { | |
2319 | end = start + mod->kprobes_text_size; | |
2320 | kprobe_remove_area_blacklist(start, end); | |
2321 | } | |
66e9b071 TG |
2322 | |
2323 | start = (unsigned long)mod->noinstr_text_start; | |
2324 | if (start) { | |
2325 | end = start + mod->noinstr_text_size; | |
2326 | kprobe_remove_area_blacklist(start, end); | |
2327 | } | |
1e6769b0 MH |
2328 | } |
2329 | ||
e8386a0c | 2330 | /* Module notifier call back, checking kprobes on the module */ |
55479f64 MH |
2331 | static int kprobes_module_callback(struct notifier_block *nb, |
2332 | unsigned long val, void *data) | |
e8386a0c MH |
2333 | { |
2334 | struct module *mod = data; | |
2335 | struct hlist_head *head; | |
e8386a0c MH |
2336 | struct kprobe *p; |
2337 | unsigned int i; | |
f24659d9 | 2338 | int checkcore = (val == MODULE_STATE_GOING); |
e8386a0c | 2339 | |
1e6769b0 MH |
2340 | if (val == MODULE_STATE_COMING) { |
2341 | mutex_lock(&kprobe_mutex); | |
2342 | add_module_kprobe_blacklist(mod); | |
2343 | mutex_unlock(&kprobe_mutex); | |
2344 | } | |
f24659d9 | 2345 | if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE) |
e8386a0c MH |
2346 | return NOTIFY_DONE; |
2347 | ||
2348 | /* | |
f24659d9 MH |
2349 | * When MODULE_STATE_GOING was notified, both of module .text and |
2350 | * .init.text sections would be freed. When MODULE_STATE_LIVE was | |
2351 | * notified, only .init.text section would be freed. We need to | |
2352 | * disable kprobes which have been inserted in the sections. | |
e8386a0c MH |
2353 | */ |
2354 | mutex_lock(&kprobe_mutex); | |
2355 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | |
2356 | head = &kprobe_table[i]; | |
7e6a71d8 | 2357 | hlist_for_each_entry(p, head, hlist) |
f24659d9 MH |
2358 | if (within_module_init((unsigned long)p->addr, mod) || |
2359 | (checkcore && | |
2360 | within_module_core((unsigned long)p->addr, mod))) { | |
e8386a0c MH |
2361 | /* |
2362 | * The vaddr this probe is installed will soon | |
2363 | * be vfreed buy not synced to disk. Hence, | |
2364 | * disarming the breakpoint isn't needed. | |
545a0281 SRV |
2365 | * |
2366 | * Note, this will also move any optimized probes | |
2367 | * that are pending to be removed from their | |
2368 | * corresponding lists to the freeing_list and | |
2369 | * will not be touched by the delayed | |
2370 | * kprobe_optimizer work handler. | |
e8386a0c MH |
2371 | */ |
2372 | kill_kprobe(p); | |
2373 | } | |
2374 | } | |
1e6769b0 MH |
2375 | if (val == MODULE_STATE_GOING) |
2376 | remove_module_kprobe_blacklist(mod); | |
e8386a0c MH |
2377 | mutex_unlock(&kprobe_mutex); |
2378 | return NOTIFY_DONE; | |
2379 | } | |
2380 | ||
2381 | static struct notifier_block kprobe_module_nb = { | |
2382 | .notifier_call = kprobes_module_callback, | |
2383 | .priority = 0 | |
2384 | }; | |
2385 | ||
376e2424 MH |
2386 | /* Markers of _kprobe_blacklist section */ |
2387 | extern unsigned long __start_kprobe_blacklist[]; | |
2388 | extern unsigned long __stop_kprobe_blacklist[]; | |
2389 | ||
1da177e4 LT |
2390 | static int __init init_kprobes(void) |
2391 | { | |
2392 | int i, err = 0; | |
2393 | ||
2394 | /* FIXME allocate the probe table, currently defined statically */ | |
2395 | /* initialize all list heads */ | |
b94cce92 | 2396 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
1da177e4 | 2397 | INIT_HLIST_HEAD(&kprobe_table[i]); |
b94cce92 | 2398 | INIT_HLIST_HEAD(&kretprobe_inst_table[i]); |
ec484608 | 2399 | raw_spin_lock_init(&(kretprobe_table_locks[i].lock)); |
b94cce92 | 2400 | } |
1da177e4 | 2401 | |
376e2424 MH |
2402 | err = populate_kprobe_blacklist(__start_kprobe_blacklist, |
2403 | __stop_kprobe_blacklist); | |
2404 | if (err) { | |
2405 | pr_err("kprobes: failed to populate blacklist: %d\n", err); | |
2406 | pr_err("Please take care of using kprobes.\n"); | |
3d8d996e SD |
2407 | } |
2408 | ||
f438d914 MH |
2409 | if (kretprobe_blacklist_size) { |
2410 | /* lookup the function address from its name */ | |
2411 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { | |
49e0b465 | 2412 | kretprobe_blacklist[i].addr = |
290e3070 | 2413 | kprobe_lookup_name(kretprobe_blacklist[i].name, 0); |
f438d914 MH |
2414 | if (!kretprobe_blacklist[i].addr) |
2415 | printk("kretprobe: lookup failed: %s\n", | |
2416 | kretprobe_blacklist[i].name); | |
2417 | } | |
2418 | } | |
2419 | ||
b2be84df MH |
2420 | #if defined(CONFIG_OPTPROBES) |
2421 | #if defined(__ARCH_WANT_KPROBES_INSN_SLOT) | |
afd66255 MH |
2422 | /* Init kprobe_optinsn_slots */ |
2423 | kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE; | |
2424 | #endif | |
b2be84df MH |
2425 | /* By default, kprobes can be optimized */ |
2426 | kprobes_allow_optimization = true; | |
2427 | #endif | |
afd66255 | 2428 | |
e579abeb MH |
2429 | /* By default, kprobes are armed */ |
2430 | kprobes_all_disarmed = false; | |
bf8f6e5b | 2431 | |
6772926b | 2432 | err = arch_init_kprobes(); |
802eae7c RL |
2433 | if (!err) |
2434 | err = register_die_notifier(&kprobe_exceptions_nb); | |
e8386a0c MH |
2435 | if (!err) |
2436 | err = register_module_notifier(&kprobe_module_nb); | |
2437 | ||
ef53d9c5 | 2438 | kprobes_initialized = (err == 0); |
802eae7c | 2439 | |
8c1c9356 AM |
2440 | if (!err) |
2441 | init_test_probes(); | |
1da177e4 LT |
2442 | return err; |
2443 | } | |
65fc965c | 2444 | subsys_initcall(init_kprobes); |
1da177e4 | 2445 | |
346fd59b | 2446 | #ifdef CONFIG_DEBUG_FS |
55479f64 | 2447 | static void report_probe(struct seq_file *pi, struct kprobe *p, |
afd66255 | 2448 | const char *sym, int offset, char *modname, struct kprobe *pp) |
346fd59b SD |
2449 | { |
2450 | char *kprobe_type; | |
81365a94 | 2451 | void *addr = p->addr; |
346fd59b SD |
2452 | |
2453 | if (p->pre_handler == pre_handler_kretprobe) | |
2454 | kprobe_type = "r"; | |
346fd59b SD |
2455 | else |
2456 | kprobe_type = "k"; | |
afd66255 | 2457 | |
81365a94 MH |
2458 | if (!kallsyms_show_value()) |
2459 | addr = NULL; | |
2460 | ||
346fd59b | 2461 | if (sym) |
81365a94 MH |
2462 | seq_printf(pi, "%px %s %s+0x%x %s ", |
2463 | addr, kprobe_type, sym, offset, | |
afd66255 | 2464 | (modname ? modname : " ")); |
81365a94 MH |
2465 | else /* try to use %pS */ |
2466 | seq_printf(pi, "%px %s %pS ", | |
2467 | addr, kprobe_type, p->addr); | |
afd66255 MH |
2468 | |
2469 | if (!pp) | |
2470 | pp = p; | |
ae6aa16f | 2471 | seq_printf(pi, "%s%s%s%s\n", |
afd66255 MH |
2472 | (kprobe_gone(p) ? "[GONE]" : ""), |
2473 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""), | |
ae6aa16f MH |
2474 | (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""), |
2475 | (kprobe_ftrace(pp) ? "[FTRACE]" : "")); | |
346fd59b SD |
2476 | } |
2477 | ||
55479f64 | 2478 | static void *kprobe_seq_start(struct seq_file *f, loff_t *pos) |
346fd59b SD |
2479 | { |
2480 | return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL; | |
2481 | } | |
2482 | ||
55479f64 | 2483 | static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos) |
346fd59b SD |
2484 | { |
2485 | (*pos)++; | |
2486 | if (*pos >= KPROBE_TABLE_SIZE) | |
2487 | return NULL; | |
2488 | return pos; | |
2489 | } | |
2490 | ||
55479f64 | 2491 | static void kprobe_seq_stop(struct seq_file *f, void *v) |
346fd59b SD |
2492 | { |
2493 | /* Nothing to do */ | |
2494 | } | |
2495 | ||
55479f64 | 2496 | static int show_kprobe_addr(struct seq_file *pi, void *v) |
346fd59b SD |
2497 | { |
2498 | struct hlist_head *head; | |
346fd59b SD |
2499 | struct kprobe *p, *kp; |
2500 | const char *sym = NULL; | |
2501 | unsigned int i = *(loff_t *) v; | |
ffb45122 | 2502 | unsigned long offset = 0; |
ab767865 | 2503 | char *modname, namebuf[KSYM_NAME_LEN]; |
346fd59b SD |
2504 | |
2505 | head = &kprobe_table[i]; | |
2506 | preempt_disable(); | |
b67bfe0d | 2507 | hlist_for_each_entry_rcu(p, head, hlist) { |
ffb45122 | 2508 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, |
346fd59b | 2509 | &offset, &modname, namebuf); |
afd66255 | 2510 | if (kprobe_aggrprobe(p)) { |
346fd59b | 2511 | list_for_each_entry_rcu(kp, &p->list, list) |
afd66255 | 2512 | report_probe(pi, kp, sym, offset, modname, p); |
346fd59b | 2513 | } else |
afd66255 | 2514 | report_probe(pi, p, sym, offset, modname, NULL); |
346fd59b SD |
2515 | } |
2516 | preempt_enable(); | |
2517 | return 0; | |
2518 | } | |
2519 | ||
eac2cece | 2520 | static const struct seq_operations kprobes_sops = { |
346fd59b SD |
2521 | .start = kprobe_seq_start, |
2522 | .next = kprobe_seq_next, | |
2523 | .stop = kprobe_seq_stop, | |
2524 | .show = show_kprobe_addr | |
2525 | }; | |
2526 | ||
eac2cece | 2527 | DEFINE_SEQ_ATTRIBUTE(kprobes); |
346fd59b | 2528 | |
63724740 MH |
2529 | /* kprobes/blacklist -- shows which functions can not be probed */ |
2530 | static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos) | |
2531 | { | |
4fdd8887 | 2532 | mutex_lock(&kprobe_mutex); |
63724740 MH |
2533 | return seq_list_start(&kprobe_blacklist, *pos); |
2534 | } | |
2535 | ||
2536 | static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
2537 | { | |
2538 | return seq_list_next(v, &kprobe_blacklist, pos); | |
2539 | } | |
2540 | ||
2541 | static int kprobe_blacklist_seq_show(struct seq_file *m, void *v) | |
2542 | { | |
2543 | struct kprobe_blacklist_entry *ent = | |
2544 | list_entry(v, struct kprobe_blacklist_entry, list); | |
2545 | ||
ffb9bd68 MH |
2546 | /* |
2547 | * If /proc/kallsyms is not showing kernel address, we won't | |
2548 | * show them here either. | |
2549 | */ | |
2550 | if (!kallsyms_show_value()) | |
2551 | seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL, | |
2552 | (void *)ent->start_addr); | |
2553 | else | |
2554 | seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr, | |
2555 | (void *)ent->end_addr, (void *)ent->start_addr); | |
63724740 MH |
2556 | return 0; |
2557 | } | |
2558 | ||
4fdd8887 MH |
2559 | static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v) |
2560 | { | |
2561 | mutex_unlock(&kprobe_mutex); | |
2562 | } | |
2563 | ||
eac2cece | 2564 | static const struct seq_operations kprobe_blacklist_sops = { |
63724740 MH |
2565 | .start = kprobe_blacklist_seq_start, |
2566 | .next = kprobe_blacklist_seq_next, | |
4fdd8887 | 2567 | .stop = kprobe_blacklist_seq_stop, |
63724740 MH |
2568 | .show = kprobe_blacklist_seq_show, |
2569 | }; | |
eac2cece | 2570 | DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist); |
63724740 | 2571 | |
12310e34 | 2572 | static int arm_all_kprobes(void) |
bf8f6e5b AM |
2573 | { |
2574 | struct hlist_head *head; | |
bf8f6e5b | 2575 | struct kprobe *p; |
12310e34 JY |
2576 | unsigned int i, total = 0, errors = 0; |
2577 | int err, ret = 0; | |
bf8f6e5b AM |
2578 | |
2579 | mutex_lock(&kprobe_mutex); | |
2580 | ||
e579abeb MH |
2581 | /* If kprobes are armed, just return */ |
2582 | if (!kprobes_all_disarmed) | |
bf8f6e5b AM |
2583 | goto already_enabled; |
2584 | ||
977ad481 WN |
2585 | /* |
2586 | * optimize_kprobe() called by arm_kprobe() checks | |
2587 | * kprobes_all_disarmed, so set kprobes_all_disarmed before | |
2588 | * arm_kprobe. | |
2589 | */ | |
2590 | kprobes_all_disarmed = false; | |
afd66255 | 2591 | /* Arming kprobes doesn't optimize kprobe itself */ |
bf8f6e5b AM |
2592 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
2593 | head = &kprobe_table[i]; | |
12310e34 | 2594 | /* Arm all kprobes on a best-effort basis */ |
7e6a71d8 | 2595 | hlist_for_each_entry(p, head, hlist) { |
12310e34 JY |
2596 | if (!kprobe_disabled(p)) { |
2597 | err = arm_kprobe(p); | |
2598 | if (err) { | |
2599 | errors++; | |
2600 | ret = err; | |
2601 | } | |
2602 | total++; | |
2603 | } | |
2604 | } | |
bf8f6e5b AM |
2605 | } |
2606 | ||
12310e34 JY |
2607 | if (errors) |
2608 | pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n", | |
2609 | errors, total); | |
2610 | else | |
2611 | pr_info("Kprobes globally enabled\n"); | |
bf8f6e5b AM |
2612 | |
2613 | already_enabled: | |
2614 | mutex_unlock(&kprobe_mutex); | |
12310e34 | 2615 | return ret; |
bf8f6e5b AM |
2616 | } |
2617 | ||
297f9233 | 2618 | static int disarm_all_kprobes(void) |
bf8f6e5b AM |
2619 | { |
2620 | struct hlist_head *head; | |
bf8f6e5b | 2621 | struct kprobe *p; |
297f9233 JY |
2622 | unsigned int i, total = 0, errors = 0; |
2623 | int err, ret = 0; | |
bf8f6e5b AM |
2624 | |
2625 | mutex_lock(&kprobe_mutex); | |
2626 | ||
e579abeb | 2627 | /* If kprobes are already disarmed, just return */ |
6274de49 MH |
2628 | if (kprobes_all_disarmed) { |
2629 | mutex_unlock(&kprobe_mutex); | |
297f9233 | 2630 | return 0; |
6274de49 | 2631 | } |
bf8f6e5b | 2632 | |
e579abeb | 2633 | kprobes_all_disarmed = true; |
afd66255 | 2634 | |
bf8f6e5b AM |
2635 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
2636 | head = &kprobe_table[i]; | |
297f9233 | 2637 | /* Disarm all kprobes on a best-effort basis */ |
7e6a71d8 | 2638 | hlist_for_each_entry(p, head, hlist) { |
297f9233 JY |
2639 | if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) { |
2640 | err = disarm_kprobe(p, false); | |
2641 | if (err) { | |
2642 | errors++; | |
2643 | ret = err; | |
2644 | } | |
2645 | total++; | |
2646 | } | |
bf8f6e5b AM |
2647 | } |
2648 | } | |
297f9233 JY |
2649 | |
2650 | if (errors) | |
2651 | pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n", | |
2652 | errors, total); | |
2653 | else | |
2654 | pr_info("Kprobes globally disabled\n"); | |
2655 | ||
bf8f6e5b | 2656 | mutex_unlock(&kprobe_mutex); |
bf8f6e5b | 2657 | |
6274de49 MH |
2658 | /* Wait for disarming all kprobes by optimizer */ |
2659 | wait_for_kprobe_optimizer(); | |
297f9233 JY |
2660 | |
2661 | return ret; | |
bf8f6e5b AM |
2662 | } |
2663 | ||
2664 | /* | |
2665 | * XXX: The debugfs bool file interface doesn't allow for callbacks | |
2666 | * when the bool state is switched. We can reuse that facility when | |
2667 | * available | |
2668 | */ | |
2669 | static ssize_t read_enabled_file_bool(struct file *file, | |
2670 | char __user *user_buf, size_t count, loff_t *ppos) | |
2671 | { | |
2672 | char buf[3]; | |
2673 | ||
e579abeb | 2674 | if (!kprobes_all_disarmed) |
bf8f6e5b AM |
2675 | buf[0] = '1'; |
2676 | else | |
2677 | buf[0] = '0'; | |
2678 | buf[1] = '\n'; | |
2679 | buf[2] = 0x00; | |
2680 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
2681 | } | |
2682 | ||
2683 | static ssize_t write_enabled_file_bool(struct file *file, | |
2684 | const char __user *user_buf, size_t count, loff_t *ppos) | |
2685 | { | |
2686 | char buf[32]; | |
efeb156e | 2687 | size_t buf_size; |
12310e34 | 2688 | int ret = 0; |
bf8f6e5b AM |
2689 | |
2690 | buf_size = min(count, (sizeof(buf)-1)); | |
2691 | if (copy_from_user(buf, user_buf, buf_size)) | |
2692 | return -EFAULT; | |
2693 | ||
10fb46d5 | 2694 | buf[buf_size] = '\0'; |
bf8f6e5b AM |
2695 | switch (buf[0]) { |
2696 | case 'y': | |
2697 | case 'Y': | |
2698 | case '1': | |
12310e34 | 2699 | ret = arm_all_kprobes(); |
bf8f6e5b AM |
2700 | break; |
2701 | case 'n': | |
2702 | case 'N': | |
2703 | case '0': | |
297f9233 | 2704 | ret = disarm_all_kprobes(); |
bf8f6e5b | 2705 | break; |
10fb46d5 MK |
2706 | default: |
2707 | return -EINVAL; | |
bf8f6e5b AM |
2708 | } |
2709 | ||
12310e34 JY |
2710 | if (ret) |
2711 | return ret; | |
2712 | ||
bf8f6e5b AM |
2713 | return count; |
2714 | } | |
2715 | ||
828c0950 | 2716 | static const struct file_operations fops_kp = { |
bf8f6e5b AM |
2717 | .read = read_enabled_file_bool, |
2718 | .write = write_enabled_file_bool, | |
6038f373 | 2719 | .llseek = default_llseek, |
bf8f6e5b AM |
2720 | }; |
2721 | ||
55479f64 | 2722 | static int __init debugfs_kprobe_init(void) |
346fd59b | 2723 | { |
8c0fd1fa | 2724 | struct dentry *dir; |
bf8f6e5b | 2725 | unsigned int value = 1; |
346fd59b SD |
2726 | |
2727 | dir = debugfs_create_dir("kprobes", NULL); | |
346fd59b | 2728 | |
eac2cece | 2729 | debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops); |
346fd59b | 2730 | |
8c0fd1fa | 2731 | debugfs_create_file("enabled", 0600, dir, &value, &fops_kp); |
63724740 | 2732 | |
8c0fd1fa | 2733 | debugfs_create_file("blacklist", 0400, dir, NULL, |
eac2cece | 2734 | &kprobe_blacklist_fops); |
bf8f6e5b | 2735 | |
346fd59b SD |
2736 | return 0; |
2737 | } | |
2738 | ||
2739 | late_initcall(debugfs_kprobe_init); | |
2740 | #endif /* CONFIG_DEBUG_FS */ |