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