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