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