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