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