blob: e5087e692b425ded06b3bbc593c4a5941f0fe7a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
3 * kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation (includes suggestions from
23 * Rusty Russell).
24 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
25 * hlists and exceptions notifier as suggested by Andi Kleen.
26 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
27 * interface to access function arguments.
28 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
29 * exceptions notifier to be first on the priority list.
Hien Nguyenb94cce92005-06-23 00:09:19 -070030 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
31 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
32 * <prasanna@in.ibm.com> added function-return probes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/hash.h>
36#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080037#include <linux/slab.h>
Randy Dunlape3869792007-05-08 00:27:01 -070038#include <linux/stddef.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040039#include <linux/export.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070040#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070041#include <linux/kallsyms.h>
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080042#include <linux/freezer.h>
Srinivasa Ds346fd592007-02-20 13:57:54 -080043#include <linux/seq_file.h>
44#include <linux/debugfs.h>
Masami Hiramatsub2be84d2010-02-25 08:34:15 -050045#include <linux/sysctl.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070046#include <linux/kdebug.h>
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -050047#include <linux/memory.h>
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -050048#include <linux/ftrace.h>
Masami Hiramatsuafd66252010-02-25 08:34:07 -050049#include <linux/cpu.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040050#include <linux/jump_label.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070051
Christoph Hellwigbfd45be2016-10-11 13:52:22 -070052#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/cacheflush.h>
54#include <asm/errno.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080055#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define KPROBE_HASH_BITS 6
58#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
59
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070060
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070061static int kprobes_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070063static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070065/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070066static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070067
Masami Hiramatsu43948f52010-10-25 22:18:01 +090068/* This protects kprobe_table and optimizing_list */
69static DEFINE_MUTEX(kprobe_mutex);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080070static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070071static struct {
Thomas Gleixnerec4846082009-07-25 16:09:17 +020072 raw_spinlock_t lock ____cacheline_aligned_in_smp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070073} kretprobe_table_locks[KPROBE_TABLE_SIZE];
74
Naveen N. Rao49e0b462017-04-19 18:21:00 +053075kprobe_opcode_t * __weak kprobe_lookup_name(const char *name)
76{
77 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
78}
79
Thomas Gleixnerec4846082009-07-25 16:09:17 +020080static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070081{
82 return &(kretprobe_table_locks[hash].lock);
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Masami Hiramatsu376e2422014-04-17 17:17:05 +090085/* Blacklist -- list of struct kprobe_blacklist_entry */
86static LIST_HEAD(kprobe_blacklist);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070087
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080088#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070089/*
90 * kprobe->ainsn.insn points to the copy of the instruction to be
91 * single-stepped. x86_64, POWER4 and above have no-exec support and
92 * stepping on the instruction on a vmalloced/kmalloced/data page
93 * is a recipe for disaster
94 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070095struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -040096 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070097 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -070098 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070099 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800100 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500101 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700102};
103
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500104#define KPROBE_INSN_PAGE_SIZE(slots) \
105 (offsetof(struct kprobe_insn_page, slot_used) + \
106 (sizeof(char) * (slots)))
107
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500108static int slots_per_page(struct kprobe_insn_cache *c)
109{
110 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
111}
112
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800113enum kprobe_slot_state {
114 SLOT_CLEAN = 0,
115 SLOT_DIRTY = 1,
116 SLOT_USED = 2,
117};
118
Heiko Carstensaf963972013-09-11 14:24:13 -0700119static void *alloc_insn_page(void)
120{
121 return module_alloc(PAGE_SIZE);
122}
123
124static void free_insn_page(void *page)
125{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030126 module_memfree(page);
Heiko Carstensaf963972013-09-11 14:24:13 -0700127}
128
Heiko Carstensc802d642013-09-11 14:24:11 -0700129struct kprobe_insn_cache kprobe_insn_slots = {
130 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700131 .alloc = alloc_insn_page,
132 .free = free_insn_page,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500133 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
134 .insn_size = MAX_INSN_SIZE,
135 .nr_garbage = 0,
136};
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900137static int collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800138
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700139/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800140 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700141 * We allocate an executable page if there's no room on existing ones.
142 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900143kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700144{
145 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700146 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700147
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900148 /* Since the slot array is not protected by rcu, we need a mutex */
Heiko Carstensc802d642013-09-11 14:24:11 -0700149 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700150 retry:
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900151 rcu_read_lock();
152 list_for_each_entry_rcu(kip, &c->pages, list) {
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500153 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700154 int i;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500155 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800156 if (kip->slot_used[i] == SLOT_CLEAN) {
157 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700158 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700159 slot = kip->insns + (i * c->insn_size);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900160 rcu_read_unlock();
Heiko Carstensc802d642013-09-11 14:24:11 -0700161 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700162 }
163 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500164 /* kip->nused is broken. Fix it. */
165 kip->nused = slots_per_page(c);
166 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700167 }
168 }
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900169 rcu_read_unlock();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700170
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800171 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500172 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800173 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500174
175 /* All out of space. Need to allocate a new page. */
176 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700177 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700178 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700179
180 /*
181 * Use module_alloc so this page is within +/- 2GB of where the
182 * kernel image and loaded module images reside. This is required
183 * so x86_64 can correctly handle the %rip-relative fixups.
184 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700185 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700186 if (!kip->insns) {
187 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700188 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700189 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400190 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500191 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800192 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700193 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800194 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700195 kip->cache = c;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900196 list_add_rcu(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700197 slot = kip->insns;
198out:
199 mutex_unlock(&c->mutex);
200 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800201}
202
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800203/* Return 1 if all garbages are collected, otherwise 0. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900204static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800205{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800206 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800207 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 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500215 if (!list_is_singular(&kip->list)) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900216 list_del_rcu(&kip->list);
217 synchronize_rcu();
Heiko Carstensaf963972013-09-11 14:24:13 -0700218 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800219 kfree(kip);
220 }
221 return 1;
222 }
223 return 0;
224}
225
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900226static int collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800227{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400228 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800229
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500230 /* Ensure no-one is interrupted on the garbages */
231 synchronize_sched();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800232
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500233 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800234 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800235 if (kip->ngarbage == 0)
236 continue;
237 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500238 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900239 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800240 break;
241 }
242 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500243 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800244 return 0;
245}
246
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900247void __free_insn_slot(struct kprobe_insn_cache *c,
248 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500249{
250 struct kprobe_insn_page *kip;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900251 long idx;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500252
Heiko Carstensc802d642013-09-11 14:24:11 -0700253 mutex_lock(&c->mutex);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900254 rcu_read_lock();
255 list_for_each_entry_rcu(kip, &c->pages, list) {
256 idx = ((long)slot - (long)kip->insns) /
257 (c->insn_size * sizeof(kprobe_opcode_t));
258 if (idx >= 0 && idx < slots_per_page(c))
Heiko Carstensc802d642013-09-11 14:24:11 -0700259 goto out;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900260 }
261 /* Could not find this slot. */
262 WARN_ON(1);
263 kip = NULL;
264out:
265 rcu_read_unlock();
266 /* Mark and sweep: this may sleep */
267 if (kip) {
268 /* Check double free */
269 WARN_ON(kip->slot_used[idx] != SLOT_USED);
270 if (dirty) {
271 kip->slot_used[idx] = SLOT_DIRTY;
272 kip->ngarbage++;
273 if (++c->nr_garbage > slots_per_page(c))
274 collect_garbage_slots(c);
275 } else {
276 collect_one_slot(kip, idx);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500277 }
278 }
Heiko Carstensc802d642013-09-11 14:24:11 -0700279 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500280}
281
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900282/*
283 * Check given address is on the page of kprobe instruction slots.
284 * This will be used for checking whether the address on a stack
285 * is on a text area or not.
286 */
287bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
288{
289 struct kprobe_insn_page *kip;
290 bool ret = false;
291
292 rcu_read_lock();
293 list_for_each_entry_rcu(kip, &c->pages, list) {
294 if (addr >= (unsigned long)kip->insns &&
295 addr < (unsigned long)kip->insns + PAGE_SIZE) {
296 ret = true;
297 break;
298 }
299 }
300 rcu_read_unlock();
301
302 return ret;
303}
304
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500305#ifdef CONFIG_OPTPROBES
306/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700307struct kprobe_insn_cache kprobe_optinsn_slots = {
308 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700309 .alloc = alloc_insn_page,
310 .free = free_insn_page,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500311 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
312 /* .insn_size is initialized later */
313 .nr_garbage = 0,
314};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500315#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800316#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700317
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800318/* We have preemption disabled.. so it is safe to use __ versions */
319static inline void set_kprobe_instance(struct kprobe *kp)
320{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600321 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800322}
323
324static inline void reset_kprobe_instance(void)
325{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600326 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800327}
328
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800329/*
330 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800331 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800332 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800333 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800334 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900335struct kprobe *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800338 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800341 hlist_for_each_entry_rcu(p, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (p->addr == addr)
343 return p;
344 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return NULL;
347}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900348NOKPROBE_SYMBOL(get_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900350static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500351
352/* Return true if the kprobe is an aggregator */
353static inline int kprobe_aggrprobe(struct kprobe *p)
354{
355 return p->pre_handler == aggr_pre_handler;
356}
357
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900358/* Return true(!0) if the kprobe is unused */
359static inline int kprobe_unused(struct kprobe *p)
360{
361 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
362 list_empty(&p->list);
363}
364
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500365/*
366 * Keep all fields in the kprobe consistent
367 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900368static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500369{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900370 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
371 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500372}
373
374#ifdef CONFIG_OPTPROBES
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500375/* NOTE: change this value only with kprobe_mutex held */
376static bool kprobes_allow_optimization;
377
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500378/*
379 * Call all pre_handler on the list, but ignores its return value.
380 * This must be called from arch-dep optimized caller.
381 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900382void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500383{
384 struct kprobe *kp;
385
386 list_for_each_entry_rcu(kp, &p->list, list) {
387 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
388 set_kprobe_instance(kp);
389 kp->pre_handler(kp, regs);
390 }
391 reset_kprobe_instance();
392 }
393}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900394NOKPROBE_SYMBOL(opt_pre_handler);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500395
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900396/* Free optimized instructions and optimized_kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900397static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900398{
399 struct optimized_kprobe *op;
400
401 op = container_of(p, struct optimized_kprobe, kp);
402 arch_remove_optimized_kprobe(op);
403 arch_remove_kprobe(p);
404 kfree(op);
405}
406
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500407/* Return true(!0) if the kprobe is ready for optimization. */
408static inline int kprobe_optready(struct kprobe *p)
409{
410 struct optimized_kprobe *op;
411
412 if (kprobe_aggrprobe(p)) {
413 op = container_of(p, struct optimized_kprobe, kp);
414 return arch_prepared_optinsn(&op->optinsn);
415 }
416
417 return 0;
418}
419
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900420/* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
421static inline int kprobe_disarmed(struct kprobe *p)
422{
423 struct optimized_kprobe *op;
424
425 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
426 if (!kprobe_aggrprobe(p))
427 return kprobe_disabled(p);
428
429 op = container_of(p, struct optimized_kprobe, kp);
430
431 return kprobe_disabled(p) && list_empty(&op->list);
432}
433
434/* Return true(!0) if the probe is queued on (un)optimizing lists */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900435static int kprobe_queued(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900436{
437 struct optimized_kprobe *op;
438
439 if (kprobe_aggrprobe(p)) {
440 op = container_of(p, struct optimized_kprobe, kp);
441 if (!list_empty(&op->list))
442 return 1;
443 }
444 return 0;
445}
446
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500447/*
448 * Return an optimized kprobe whose optimizing code replaces
449 * instructions including addr (exclude breakpoint).
450 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900451static struct kprobe *get_optimized_kprobe(unsigned long addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500452{
453 int i;
454 struct kprobe *p = NULL;
455 struct optimized_kprobe *op;
456
457 /* Don't check i == 0, since that is a breakpoint case. */
458 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
459 p = get_kprobe((void *)(addr - i));
460
461 if (p && kprobe_optready(p)) {
462 op = container_of(p, struct optimized_kprobe, kp);
463 if (arch_within_optimized_kprobe(op, addr))
464 return p;
465 }
466
467 return NULL;
468}
469
470/* Optimization staging list, protected by kprobe_mutex */
471static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900472static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900473static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500474
475static void kprobe_optimizer(struct work_struct *work);
476static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
477#define OPTIMIZE_DELAY 5
478
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900479/*
480 * Optimize (replace a breakpoint with a jump) kprobes listed on
481 * optimizing_list.
482 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900483static void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500484{
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900485 /* Optimization never be done when disarmed */
486 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
487 list_empty(&optimizing_list))
488 return;
489
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500490 /*
491 * The optimization/unoptimization refers online_cpus via
492 * stop_machine() and cpu-hotplug modifies online_cpus.
493 * And same time, text_mutex will be held in cpu-hotplug and here.
494 * This combination can cause a deadlock (cpu-hotplug try to lock
495 * text_mutex but stop_machine can not be done because online_cpus
496 * has been changed)
497 * To avoid this deadlock, we need to call get_online_cpus()
498 * for preventing cpu-hotplug outside of text_mutex locking.
499 */
500 get_online_cpus();
501 mutex_lock(&text_mutex);
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900502 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500503 mutex_unlock(&text_mutex);
504 put_online_cpus();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900505}
506
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900507/*
508 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
509 * if need) kprobes listed on unoptimizing_list.
510 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900511static void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900512{
513 struct optimized_kprobe *op, *tmp;
514
515 /* Unoptimization must be done anytime */
516 if (list_empty(&unoptimizing_list))
517 return;
518
519 /* Ditto to do_optimize_kprobes */
520 get_online_cpus();
521 mutex_lock(&text_mutex);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900522 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900523 /* Loop free_list for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900524 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900525 /* Disarm probes if marked disabled */
526 if (kprobe_disabled(&op->kp))
527 arch_disarm_kprobe(&op->kp);
528 if (kprobe_unused(&op->kp)) {
529 /*
530 * Remove unused probes from hash list. After waiting
531 * for synchronization, these probes are reclaimed.
532 * (reclaiming is done by do_free_cleaned_kprobes.)
533 */
534 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900535 } else
536 list_del_init(&op->list);
537 }
538 mutex_unlock(&text_mutex);
539 put_online_cpus();
540}
541
542/* Reclaim all kprobes on the free_list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900543static void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900544{
545 struct optimized_kprobe *op, *tmp;
546
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900547 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900548 BUG_ON(!kprobe_unused(&op->kp));
549 list_del_init(&op->list);
550 free_aggr_kprobe(&op->kp);
551 }
552}
553
554/* Start optimizer after OPTIMIZE_DELAY passed */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900555static void kick_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900556{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800557 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900558}
559
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900560/* Kprobe jump optimizer */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900561static void kprobe_optimizer(struct work_struct *work)
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900562{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900563 mutex_lock(&kprobe_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900564 /* Lock modules while optimizing kprobes */
565 mutex_lock(&module_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900566
567 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900568 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
569 * kprobes before waiting for quiesence period.
570 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900571 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900572
573 /*
574 * Step 2: Wait for quiesence period to ensure all running interrupts
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900575 * are done. Because optprobe may modify multiple instructions
576 * there is a chance that Nth instruction is interrupted. In that
577 * case, running interrupt can return to 2nd-Nth byte of jump
578 * instruction. This wait is for avoiding it.
579 */
580 synchronize_sched();
581
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900582 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900583 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900584
585 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900586 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900587
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500588 mutex_unlock(&module_mutex);
Steven Rostedt72ef3792012-06-05 19:28:14 +0900589 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900590
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900591 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900592 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900593 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900594}
595
596/* Wait for completing optimization and unoptimization */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900597static void wait_for_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900598{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800599 mutex_lock(&kprobe_mutex);
600
601 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
602 mutex_unlock(&kprobe_mutex);
603
604 /* this will also make optimizing_work execute immmediately */
605 flush_delayed_work(&optimizing_work);
606 /* @optimizing_work might not have been queued yet, relax */
607 cpu_relax();
608
609 mutex_lock(&kprobe_mutex);
610 }
611
612 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500613}
614
615/* Optimize kprobe if p is ready to be optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900616static void optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500617{
618 struct optimized_kprobe *op;
619
620 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500621 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500622 (kprobe_disabled(p) || kprobes_all_disarmed))
623 return;
624
625 /* Both of break_handler and post_handler are not supported. */
626 if (p->break_handler || p->post_handler)
627 return;
628
629 op = container_of(p, struct optimized_kprobe, kp);
630
631 /* Check there is no other kprobes at the optimized instructions */
632 if (arch_check_optimized_kprobe(op) < 0)
633 return;
634
635 /* Check if it is already optimized. */
636 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED)
637 return;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500638 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900639
640 if (!list_empty(&op->list))
641 /* This is under unoptimizing. Just dequeue the probe */
642 list_del_init(&op->list);
643 else {
644 list_add(&op->list, &optimizing_list);
645 kick_kprobe_optimizer();
646 }
647}
648
649/* Short cut to direct unoptimizing */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900650static void force_unoptimize_kprobe(struct optimized_kprobe *op)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900651{
652 get_online_cpus();
653 arch_unoptimize_kprobe(op);
654 put_online_cpus();
655 if (kprobe_disabled(&op->kp))
656 arch_disarm_kprobe(&op->kp);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500657}
658
659/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900660static void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500661{
662 struct optimized_kprobe *op;
663
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900664 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
665 return; /* This is not an optprobe nor optimized */
666
667 op = container_of(p, struct optimized_kprobe, kp);
668 if (!kprobe_optimized(p)) {
669 /* Unoptimized or unoptimizing case */
670 if (force && !list_empty(&op->list)) {
671 /*
672 * Only if this is unoptimizing kprobe and forced,
673 * forcibly unoptimize it. (No need to unoptimize
674 * unoptimized kprobe again :)
675 */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500676 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900677 force_unoptimize_kprobe(op);
678 }
679 return;
680 }
681
682 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
683 if (!list_empty(&op->list)) {
684 /* Dequeue from the optimization queue */
685 list_del_init(&op->list);
686 return;
687 }
688 /* Optimized kprobe case */
689 if (force)
690 /* Forcibly update the code: this is a special case */
691 force_unoptimize_kprobe(op);
692 else {
693 list_add(&op->list, &unoptimizing_list);
694 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500695 }
696}
697
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900698/* Cancel unoptimizing for reusing */
699static void reuse_unused_kprobe(struct kprobe *ap)
700{
701 struct optimized_kprobe *op;
702
703 BUG_ON(!kprobe_unused(ap));
704 /*
705 * Unused kprobe MUST be on the way of delayed unoptimizing (means
706 * there is still a relative jump) and disabled.
707 */
708 op = container_of(ap, struct optimized_kprobe, kp);
709 if (unlikely(list_empty(&op->list)))
710 printk(KERN_WARNING "Warning: found a stray unused "
711 "aggrprobe@%p\n", ap->addr);
712 /* Enable the probe again */
713 ap->flags &= ~KPROBE_FLAG_DISABLED;
714 /* Optimize it again (remove from op->list) */
715 BUG_ON(!kprobe_optready(ap));
716 optimize_kprobe(ap);
717}
718
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500719/* Remove optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900720static void kill_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500721{
722 struct optimized_kprobe *op;
723
724 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900725 if (!list_empty(&op->list))
726 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500727 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900728 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900729
730 if (kprobe_unused(p)) {
731 /* Enqueue if it is unused */
732 list_add(&op->list, &freeing_list);
733 /*
734 * Remove unused probes from the hash list. After waiting
735 * for synchronization, this probe is reclaimed.
736 * (reclaiming is done by do_free_cleaned_kprobes().)
737 */
738 hlist_del_rcu(&op->kp.hlist);
739 }
740
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900741 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500742 arch_remove_optimized_kprobe(op);
743}
744
Masami Hiramatsua4602462017-04-19 18:22:25 +0530745static inline
746void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
747{
748 if (!kprobe_ftrace(p))
749 arch_prepare_optimized_kprobe(op, p);
750}
751
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500752/* Try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900753static void prepare_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500754{
755 struct optimized_kprobe *op;
756
757 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsua4602462017-04-19 18:22:25 +0530758 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500759}
760
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500761/* Allocate new optimized_kprobe and try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900762static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500763{
764 struct optimized_kprobe *op;
765
766 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
767 if (!op)
768 return NULL;
769
770 INIT_LIST_HEAD(&op->list);
771 op->kp.addr = p->addr;
Masami Hiramatsua4602462017-04-19 18:22:25 +0530772 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500773
774 return &op->kp;
775}
776
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900777static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500778
779/*
780 * Prepare an optimized_kprobe and optimize it
781 * NOTE: p must be a normal registered kprobe
782 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900783static void try_to_optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500784{
785 struct kprobe *ap;
786 struct optimized_kprobe *op;
787
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900788 /* Impossible to optimize ftrace-based kprobe */
789 if (kprobe_ftrace(p))
790 return;
791
Masami Hiramatsu25764282012-06-05 19:28:26 +0900792 /* For preparing optimization, jump_label_text_reserved() is called */
793 jump_label_lock();
794 mutex_lock(&text_mutex);
795
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500796 ap = alloc_aggr_kprobe(p);
797 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900798 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500799
800 op = container_of(ap, struct optimized_kprobe, kp);
801 if (!arch_prepared_optinsn(&op->optinsn)) {
802 /* If failed to setup optimizing, fallback to kprobe */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900803 arch_remove_optimized_kprobe(op);
804 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900805 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500806 }
807
808 init_aggr_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900809 optimize_kprobe(ap); /* This just kicks optimizer thread */
810
811out:
812 mutex_unlock(&text_mutex);
813 jump_label_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500814}
815
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500816#ifdef CONFIG_SYSCTL
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900817static void optimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500818{
819 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500820 struct kprobe *p;
821 unsigned int i;
822
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900823 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500824 /* If optimization is already allowed, just return */
825 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900826 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500827
828 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500829 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
830 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800831 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500832 if (!kprobe_disabled(p))
833 optimize_kprobe(p);
834 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500835 printk(KERN_INFO "Kprobes globally optimized\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900836out:
837 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500838}
839
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900840static void unoptimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500841{
842 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500843 struct kprobe *p;
844 unsigned int i;
845
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900846 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500847 /* If optimization is already prohibited, just return */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900848 if (!kprobes_allow_optimization) {
849 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500850 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900851 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500852
853 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500854 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
855 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800856 hlist_for_each_entry_rcu(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500857 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900858 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500859 }
860 }
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900861 mutex_unlock(&kprobe_mutex);
862
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900863 /* Wait for unoptimizing completion */
864 wait_for_kprobe_optimizer();
865 printk(KERN_INFO "Kprobes globally unoptimized\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500866}
867
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900868static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500869int sysctl_kprobes_optimization;
870int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
871 void __user *buffer, size_t *length,
872 loff_t *ppos)
873{
874 int ret;
875
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900876 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500877 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
878 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
879
880 if (sysctl_kprobes_optimization)
881 optimize_all_kprobes();
882 else
883 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900884 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500885
886 return ret;
887}
888#endif /* CONFIG_SYSCTL */
889
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900890/* Put a breakpoint for a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900891static void __arm_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500892{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900893 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500894
895 /* Check collision with other optimized kprobes */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900896 _p = get_optimized_kprobe((unsigned long)p->addr);
897 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900898 /* Fallback to unoptimized kprobe */
899 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500900
901 arch_arm_kprobe(p);
902 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
903}
904
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900905/* Remove the breakpoint of a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900906static void __disarm_kprobe(struct kprobe *p, bool reopt)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500907{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900908 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500909
Wang Nan69d54b92015-02-13 14:40:26 -0800910 /* Try to unoptimize */
911 unoptimize_kprobe(p, kprobes_all_disarmed);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500912
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900913 if (!kprobe_queued(p)) {
914 arch_disarm_kprobe(p);
915 /* If another kprobe was blocked, optimize it. */
916 _p = get_optimized_kprobe((unsigned long)p->addr);
917 if (unlikely(_p) && reopt)
918 optimize_kprobe(_p);
919 }
920 /* TODO: reoptimize others after unoptimized this probe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500921}
922
923#else /* !CONFIG_OPTPROBES */
924
925#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900926#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500927#define kill_optimized_kprobe(p) do {} while (0)
928#define prepare_optimized_kprobe(p) do {} while (0)
929#define try_to_optimize_kprobe(p) do {} while (0)
930#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900931#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
932#define kprobe_disarmed(p) kprobe_disabled(p)
933#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500934
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900935/* There should be no unused kprobes can be reused without optimization */
936static void reuse_unused_kprobe(struct kprobe *ap)
937{
938 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
939 BUG_ON(kprobe_unused(ap));
940}
941
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900942static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500943{
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900944 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500945 kfree(p);
946}
947
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900948static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500949{
950 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
951}
952#endif /* CONFIG_OPTPROBES */
953
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900954#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900955static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +0900956 .func = kprobe_ftrace_handler,
Masami Hiramatsu1d70be32014-11-21 05:25:23 -0500957 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900958};
959static int kprobe_ftrace_enabled;
960
961/* Must ensure p->addr is really on ftrace */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900962static int prepare_kprobe(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900963{
964 if (!kprobe_ftrace(p))
965 return arch_prepare_kprobe(p);
966
967 return arch_prepare_kprobe_ftrace(p);
968}
969
970/* Caller must lock kprobe_mutex */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900971static void arm_kprobe_ftrace(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900972{
973 int ret;
974
975 ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
976 (unsigned long)p->addr, 0, 0);
977 WARN(ret < 0, "Failed to arm kprobe-ftrace at %p (%d)\n", p->addr, ret);
978 kprobe_ftrace_enabled++;
979 if (kprobe_ftrace_enabled == 1) {
980 ret = register_ftrace_function(&kprobe_ftrace_ops);
981 WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret);
982 }
983}
984
985/* Caller must lock kprobe_mutex */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900986static void disarm_kprobe_ftrace(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900987{
988 int ret;
989
990 kprobe_ftrace_enabled--;
991 if (kprobe_ftrace_enabled == 0) {
992 ret = unregister_ftrace_function(&kprobe_ftrace_ops);
993 WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret);
994 }
995 ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
996 (unsigned long)p->addr, 1, 0);
997 WARN(ret < 0, "Failed to disarm kprobe-ftrace at %p (%d)\n", p->addr, ret);
998}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900999#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001000#define prepare_kprobe(p) arch_prepare_kprobe(p)
1001#define arm_kprobe_ftrace(p) do {} while (0)
1002#define disarm_kprobe_ftrace(p) do {} while (0)
1003#endif
1004
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001005/* Arm a kprobe with text_mutex */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001006static void arm_kprobe(struct kprobe *kp)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001007{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001008 if (unlikely(kprobe_ftrace(kp))) {
1009 arm_kprobe_ftrace(kp);
1010 return;
1011 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001012 /*
1013 * Here, since __arm_kprobe() doesn't use stop_machine(),
1014 * this doesn't cause deadlock on text_mutex. So, we don't
1015 * need get_online_cpus().
1016 */
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001017 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001018 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001019 mutex_unlock(&text_mutex);
1020}
1021
1022/* Disarm a kprobe with text_mutex */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001023static void disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001024{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001025 if (unlikely(kprobe_ftrace(kp))) {
1026 disarm_kprobe_ftrace(kp);
1027 return;
1028 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001029 /* Ditto */
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001030 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001031 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001032 mutex_unlock(&text_mutex);
1033}
1034
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001035/*
1036 * Aggregate handlers for multiple kprobes support - these handlers
1037 * take care of invoking the individual kprobe handlers on p->list
1038 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001039static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001040{
1041 struct kprobe *kp;
1042
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001043 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001044 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001045 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001046 if (kp->pre_handler(kp, regs))
1047 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001048 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001049 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001050 }
1051 return 0;
1052}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001053NOKPROBE_SYMBOL(aggr_pre_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001054
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001055static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1056 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001057{
1058 struct kprobe *kp;
1059
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001060 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001061 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001062 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001063 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001064 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001065 }
1066 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001067}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001068NOKPROBE_SYMBOL(aggr_post_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001069
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001070static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1071 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001072{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001073 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001074
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001075 /*
1076 * if we faulted "during" the execution of a user specified
1077 * probe handler, invoke just that probe's fault handler
1078 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001079 if (cur && cur->fault_handler) {
1080 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001081 return 1;
1082 }
1083 return 0;
1084}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001085NOKPROBE_SYMBOL(aggr_fault_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001086
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001087static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001088{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001089 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001090 int ret = 0;
1091
1092 if (cur && cur->break_handler) {
1093 if (cur->break_handler(cur, regs))
1094 ret = 1;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001095 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001096 reset_kprobe_instance();
1097 return ret;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001098}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001099NOKPROBE_SYMBOL(aggr_break_handler);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001100
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001101/* Walks the list and increments nmissed count for multiprobe case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001102void kprobes_inc_nmissed_count(struct kprobe *p)
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001103{
1104 struct kprobe *kp;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001105 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001106 p->nmissed++;
1107 } else {
1108 list_for_each_entry_rcu(kp, &p->list, list)
1109 kp->nmissed++;
1110 }
1111 return;
1112}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001113NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001114
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001115void recycle_rp_inst(struct kretprobe_instance *ri,
1116 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001117{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001118 struct kretprobe *rp = ri->rp;
1119
Hien Nguyenb94cce92005-06-23 00:09:19 -07001120 /* remove rp inst off the rprobe_inst_table */
1121 hlist_del(&ri->hlist);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001122 INIT_HLIST_NODE(&ri->hlist);
1123 if (likely(rp)) {
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001124 raw_spin_lock(&rp->lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001125 hlist_add_head(&ri->hlist, &rp->free_instances);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001126 raw_spin_unlock(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001127 } else
1128 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -07001129 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001130}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001131NOKPROBE_SYMBOL(recycle_rp_inst);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001132
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001133void kretprobe_hash_lock(struct task_struct *tsk,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001134 struct hlist_head **head, unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001135__acquires(hlist_lock)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001136{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001137 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001138 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001139
1140 *head = &kretprobe_inst_table[hash];
1141 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001142 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001143}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001144NOKPROBE_SYMBOL(kretprobe_hash_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001145
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001146static void kretprobe_table_lock(unsigned long hash,
1147 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001148__acquires(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001149{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001150 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1151 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001152}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001153NOKPROBE_SYMBOL(kretprobe_table_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001154
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001155void kretprobe_hash_unlock(struct task_struct *tsk,
1156 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001157__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001158{
1159 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001160 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001161
1162 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001163 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001164}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001165NOKPROBE_SYMBOL(kretprobe_hash_unlock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001166
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001167static void kretprobe_table_unlock(unsigned long hash,
1168 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001169__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001170{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001171 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1172 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001173}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001174NOKPROBE_SYMBOL(kretprobe_table_unlock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001175
Hien Nguyenb94cce92005-06-23 00:09:19 -07001176/*
bibo maoc6fd91f2006-03-26 01:38:20 -08001177 * This function is called from finish_task_switch when task tk becomes dead,
1178 * so that we can recycle any function-return probe instances associated
1179 * with this task. These left over instances represent probed functions
1180 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001181 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001182void kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001183{
bibo,mao62c27be2006-10-02 02:17:33 -07001184 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -07001185 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001186 struct hlist_node *tmp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001187 unsigned long hash, flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -07001188
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001189 if (unlikely(!kprobes_initialized))
1190 /* Early boot. kretprobe_table_locks not yet initialized. */
1191 return;
1192
Ananth N Mavinakayanahallid496aab2012-01-20 14:34:04 -08001193 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001194 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1195 head = &kretprobe_inst_table[hash];
1196 kretprobe_table_lock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001197 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -07001198 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -07001199 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -07001200 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001201 kretprobe_table_unlock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001202 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -07001203 hlist_del(&ri->hlist);
1204 kfree(ri);
1205 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001206}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001207NOKPROBE_SYMBOL(kprobe_flush_task);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001208
Hien Nguyenb94cce92005-06-23 00:09:19 -07001209static inline void free_rp_inst(struct kretprobe *rp)
1210{
1211 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001212 struct hlist_node *next;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001213
Sasha Levinb67bfe02013-02-27 17:06:00 -08001214 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001215 hlist_del(&ri->hlist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001216 kfree(ri);
1217 }
1218}
1219
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001220static void cleanup_rp_inst(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001221{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001222 unsigned long flags, hash;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001223 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001224 struct hlist_node *next;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001225 struct hlist_head *head;
1226
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001227 /* No race here */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001228 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1229 kretprobe_table_lock(hash, &flags);
1230 head = &kretprobe_inst_table[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001231 hlist_for_each_entry_safe(ri, next, head, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001232 if (ri->rp == rp)
1233 ri->rp = NULL;
1234 }
1235 kretprobe_table_unlock(hash, &flags);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001236 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001237 free_rp_inst(rp);
1238}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001239NOKPROBE_SYMBOL(cleanup_rp_inst);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001240
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001241/*
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001242* Add the new probe to ap->list. Fail if this is the
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001243* second jprobe at the address - two jprobes can't coexist
1244*/
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001245static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001246{
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001247 BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001248
1249 if (p->break_handler || p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001250 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001251
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001252 if (p->break_handler) {
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001253 if (ap->break_handler)
mao, bibo36721652006-06-26 00:25:22 -07001254 return -EEXIST;
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001255 list_add_tail_rcu(&p->list, &ap->list);
1256 ap->break_handler = aggr_break_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001257 } else
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001258 list_add_rcu(&p->list, &ap->list);
1259 if (p->post_handler && !ap->post_handler)
1260 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001261
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001262 return 0;
1263}
1264
1265/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001266 * Fill in the required fields of the "manager kprobe". Replace the
1267 * earlier kprobe in the hlist with the manager kprobe
1268 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001269static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001270{
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001271 /* Copy p's insn slot to ap */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001272 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001273 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001274 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001275 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001276 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001277 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001278 /* We don't care the kprobe which has gone. */
1279 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001280 ap->post_handler = aggr_post_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001281 if (p->break_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001282 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001283
1284 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001285 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001286
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001287 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001288 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001289}
1290
1291/*
1292 * This is the second or subsequent kprobe at the address - handle
1293 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001294 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001295static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001296{
1297 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001298 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001299
Masami Hiramatsu25764282012-06-05 19:28:26 +09001300 /* For preparing optimization, jump_label_text_reserved() is called */
1301 jump_label_lock();
1302 /*
1303 * Get online CPUs to avoid text_mutex deadlock.with stop machine,
1304 * which is invoked by unoptimize_kprobe() in add_new_kprobe()
1305 */
1306 get_online_cpus();
1307 mutex_lock(&text_mutex);
1308
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001309 if (!kprobe_aggrprobe(orig_p)) {
1310 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1311 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001312 if (!ap) {
1313 ret = -ENOMEM;
1314 goto out;
1315 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001316 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001317 } else if (kprobe_unused(ap))
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001318 /* This probe is going to die. Rescue it */
1319 reuse_unused_kprobe(ap);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001320
1321 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001322 /*
1323 * Attempting to insert new probe at the same location that
1324 * had a probe in the module vaddr area which already
1325 * freed. So, the instruction slot has already been
1326 * released. We need a new slot for the new probe.
1327 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001328 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001329 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001330 /*
1331 * Even if fail to allocate new slot, don't need to
1332 * free aggr_probe. It will be used next time, or
1333 * freed by unregister_kprobe.
1334 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001335 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001336
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001337 /* Prepare optimized instructions if possible. */
1338 prepare_optimized_kprobe(ap);
1339
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001340 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001341 * Clear gone flag to prevent allocating new slot again, and
1342 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001343 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001344 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1345 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001346 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001347
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001348 /* Copy ap's insn slot to p */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001349 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001350 ret = add_new_kprobe(ap, p);
1351
1352out:
1353 mutex_unlock(&text_mutex);
1354 put_online_cpus();
1355 jump_label_unlock();
1356
1357 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1358 ap->flags &= ~KPROBE_FLAG_DISABLED;
1359 if (!kprobes_all_disarmed)
1360 /* Arm the breakpoint again. */
1361 arm_kprobe(ap);
1362 }
1363 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001364}
1365
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001366bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1367{
1368 /* The __kprobes marked functions and entry code must not be probed */
1369 return addr >= (unsigned long)__kprobes_text_start &&
1370 addr < (unsigned long)__kprobes_text_end;
1371}
1372
Andy Lutomirskie5779e82015-07-30 20:32:40 -07001373bool within_kprobe_blacklist(unsigned long addr)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001374{
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001375 struct kprobe_blacklist_entry *ent;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001376
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001377 if (arch_within_kprobe_blacklist(addr))
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001378 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001379 /*
1380 * If there exists a kprobe_blacklist, verify and
1381 * fail any probe registration in the prohibited area
1382 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001383 list_for_each_entry(ent, &kprobe_blacklist, list) {
1384 if (addr >= ent->start_addr && addr < ent->end_addr)
1385 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001386 }
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001387
1388 return false;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001389}
1390
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001391/*
1392 * If we have a symbol_name argument, look it up and add the offset field
1393 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001394 * This returns encoded errors if it fails to look up symbol or invalid
1395 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001396 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001397static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001398{
1399 kprobe_opcode_t *addr = p->addr;
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001400
1401 if ((p->symbol_name && p->addr) ||
1402 (!p->symbol_name && !p->addr))
1403 goto invalid;
1404
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001405 if (p->symbol_name) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05301406 addr = kprobe_lookup_name(p->symbol_name);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001407 if (!addr)
1408 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001409 }
1410
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001411 addr = (kprobe_opcode_t *)(((char *)addr) + p->offset);
1412 if (addr)
1413 return addr;
1414
1415invalid:
1416 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001417}
1418
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301419/* Check passed kprobe is valid and return kprobe in kprobe_table. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001420static struct kprobe *__get_valid_kprobe(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301421{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001422 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301423
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001424 ap = get_kprobe(p->addr);
1425 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301426 return NULL;
1427
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001428 if (p != ap) {
1429 list_for_each_entry_rcu(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301430 if (list_p == p)
1431 /* kprobe p is a valid probe */
1432 goto valid;
1433 return NULL;
1434 }
1435valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001436 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301437}
1438
1439/* Return error if the kprobe is being re-registered */
1440static inline int check_kprobe_rereg(struct kprobe *p)
1441{
1442 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301443
1444 mutex_lock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001445 if (__get_valid_kprobe(p))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301446 ret = -EINVAL;
1447 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001448
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301449 return ret;
1450}
1451
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001452int __weak arch_check_ftrace_location(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001454 unsigned long ftrace_addr;
1455
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001456 ftrace_addr = ftrace_location((unsigned long)p->addr);
1457 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001458#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001459 /* Given address is not on the instruction boundary */
1460 if ((unsigned long)p->addr != ftrace_addr)
1461 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001462 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001463#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001464 return -EINVAL;
1465#endif
1466 }
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001467 return 0;
1468}
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001469
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001470static int check_kprobe_address_safe(struct kprobe *p,
1471 struct module **probed_mod)
1472{
1473 int ret;
1474
1475 ret = arch_check_ftrace_location(p);
1476 if (ret)
1477 return ret;
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001478 jump_label_lock();
1479 preempt_disable();
1480
1481 /* Ensure it is not in reserved area nor out of text */
1482 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001483 within_kprobe_blacklist((unsigned long) p->addr) ||
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001484 jump_label_text_reserved(p->addr, p->addr)) {
1485 ret = -EINVAL;
1486 goto out;
1487 }
1488
1489 /* Check if are we probing a module */
1490 *probed_mod = __module_text_address((unsigned long) p->addr);
1491 if (*probed_mod) {
1492 /*
1493 * We must hold a refcount of the probed module while updating
1494 * its code to prohibit unexpected unloading.
1495 */
1496 if (unlikely(!try_module_get(*probed_mod))) {
1497 ret = -ENOENT;
1498 goto out;
1499 }
1500
1501 /*
1502 * If the module freed .init.text, we couldn't insert
1503 * kprobes in there.
1504 */
1505 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1506 (*probed_mod)->state != MODULE_STATE_COMING) {
1507 module_put(*probed_mod);
1508 *probed_mod = NULL;
1509 ret = -ENOENT;
1510 }
1511 }
1512out:
1513 preempt_enable();
1514 jump_label_unlock();
1515
1516 return ret;
1517}
1518
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001519int register_kprobe(struct kprobe *p)
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001520{
1521 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001522 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001523 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001524 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001526 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001527 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001528 if (IS_ERR(addr))
1529 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001530 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001531
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301532 ret = check_kprobe_rereg(p);
1533 if (ret)
1534 return ret;
1535
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001536 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1537 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001538 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001539 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001540
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001541 ret = check_kprobe_address_safe(p, &probed_mod);
1542 if (ret)
1543 return ret;
1544
1545 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001546
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001547 old_p = get_kprobe(p->addr);
1548 if (old_p) {
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001549 /* Since this may unoptimize old_p, locking text_mutex. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001550 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto out;
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Masami Hiramatsu25764282012-06-05 19:28:26 +09001554 mutex_lock(&text_mutex); /* Avoiding text modification */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001555 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001556 mutex_unlock(&text_mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001557 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001558 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001559
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001560 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001561 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1563
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001564 if (!kprobes_all_disarmed && !kprobe_disabled(p))
Masami Hiramatsu25764282012-06-05 19:28:26 +09001565 arm_kprobe(p);
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001566
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001567 /* Try to optimize kprobe */
1568 try_to_optimize_kprobe(p);
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001571 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001572
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001573 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001574 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return ret;
1577}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001578EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001580/* Check if all probes on the aggrprobe are disabled */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001581static int aggr_kprobe_disabled(struct kprobe *ap)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001582{
1583 struct kprobe *kp;
1584
1585 list_for_each_entry_rcu(kp, &ap->list, list)
1586 if (!kprobe_disabled(kp))
1587 /*
1588 * There is an active probe on the list.
1589 * We can't disable this ap.
1590 */
1591 return 0;
1592
1593 return 1;
1594}
1595
1596/* Disable one kprobe: Make sure called under kprobe_mutex is locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001597static struct kprobe *__disable_kprobe(struct kprobe *p)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001598{
1599 struct kprobe *orig_p;
1600
1601 /* Get an original kprobe for return */
1602 orig_p = __get_valid_kprobe(p);
1603 if (unlikely(orig_p == NULL))
1604 return NULL;
1605
1606 if (!kprobe_disabled(p)) {
1607 /* Disable probe if it is a child probe */
1608 if (p != orig_p)
1609 p->flags |= KPROBE_FLAG_DISABLED;
1610
1611 /* Try to disarm and disable this/parent probe */
1612 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Wang Nan69d54b92015-02-13 14:40:26 -08001613 /*
1614 * If kprobes_all_disarmed is set, orig_p
1615 * should have already been disarmed, so
1616 * skip unneed disarming process.
1617 */
1618 if (!kprobes_all_disarmed)
1619 disarm_kprobe(orig_p, true);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001620 orig_p->flags |= KPROBE_FLAG_DISABLED;
1621 }
1622 }
1623
1624 return orig_p;
1625}
1626
Masami Hiramatsu98616682008-04-28 02:14:28 -07001627/*
1628 * Unregister a kprobe without a scheduler synchronization.
1629 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001630static int __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001631{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001632 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001633
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001634 /* Disable kprobe. This will disarm it if needed. */
1635 ap = __disable_kprobe(p);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001636 if (ap == NULL)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001637 return -EINVAL;
1638
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001639 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001640 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001641 * This probe is an independent(and non-optimized) kprobe
1642 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001643 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001644 goto disarmed;
1645
1646 /* Following process expects this probe is an aggrprobe */
1647 WARN_ON(!kprobe_aggrprobe(ap));
1648
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001649 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1650 /*
1651 * !disarmed could be happen if the probe is under delayed
1652 * unoptimizing.
1653 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001654 goto disarmed;
1655 else {
1656 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001657 if (p->break_handler && !kprobe_gone(p))
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001658 ap->break_handler = NULL;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001659 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001660 list_for_each_entry_rcu(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001661 if ((list_p != p) && (list_p->post_handler))
1662 goto noclean;
1663 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001664 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001665 }
1666noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001667 /*
1668 * Remove from the aggrprobe: this path will do nothing in
1669 * __unregister_kprobe_bottom().
1670 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001671 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001672 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1673 /*
1674 * Try to optimize this probe again, because post
1675 * handler may have been changed.
1676 */
1677 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001678 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001679 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001680
1681disarmed:
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001682 BUG_ON(!kprobe_disarmed(ap));
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001683 hlist_del_rcu(&ap->hlist);
1684 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001685}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001686
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001687static void __unregister_kprobe_bottom(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001688{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001689 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001690
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001691 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001692 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001693 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001694 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001695 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001696 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001697 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001698 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001699 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001700 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001703int register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001704{
1705 int i, ret = 0;
1706
1707 if (num <= 0)
1708 return -EINVAL;
1709 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001710 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001711 if (ret < 0) {
1712 if (i > 0)
1713 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001714 break;
1715 }
1716 }
1717 return ret;
1718}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001719EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001720
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001721void unregister_kprobe(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001722{
1723 unregister_kprobes(&p, 1);
1724}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001725EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001726
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001727void unregister_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001728{
1729 int i;
1730
1731 if (num <= 0)
1732 return;
1733 mutex_lock(&kprobe_mutex);
1734 for (i = 0; i < num; i++)
1735 if (__unregister_kprobe_top(kps[i]) < 0)
1736 kps[i]->addr = NULL;
1737 mutex_unlock(&kprobe_mutex);
1738
1739 synchronize_sched();
1740 for (i = 0; i < num; i++)
1741 if (kps[i]->addr)
1742 __unregister_kprobe_bottom(kps[i]);
1743}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001744EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001745
Naveen N. Raofc62d022017-02-08 01:24:14 +05301746int __weak __kprobes kprobe_exceptions_notify(struct notifier_block *self,
1747 unsigned long val, void *data)
1748{
1749 return NOTIFY_DONE;
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752static struct notifier_block kprobe_exceptions_nb = {
1753 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001754 .priority = 0x7fffffff /* we need to be notified first */
1755};
1756
Michael Ellerman3d7e3382007-07-19 01:48:11 -07001757unsigned long __weak arch_deref_entry_point(void *entry)
1758{
1759 return (unsigned long)entry;
1760}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001762int register_jprobes(struct jprobe **jps, int num)
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001763{
1764 struct jprobe *jp;
1765 int ret = 0, i;
1766
1767 if (num <= 0)
1768 return -EINVAL;
1769 for (i = 0; i < num; i++) {
Namhyung Kim05662bd2010-09-15 10:04:27 +09001770 unsigned long addr, offset;
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001771 jp = jps[i];
1772 addr = arch_deref_entry_point(jp->entry);
1773
Namhyung Kim05662bd2010-09-15 10:04:27 +09001774 /* Verify probepoint is a function entry point */
1775 if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
1776 offset == 0) {
1777 jp->kp.pre_handler = setjmp_pre_handler;
1778 jp->kp.break_handler = longjmp_break_handler;
1779 ret = register_kprobe(&jp->kp);
1780 } else
1781 ret = -EINVAL;
Namhyung Kimedbaadb2010-09-15 10:04:26 +09001782
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001783 if (ret < 0) {
1784 if (i > 0)
1785 unregister_jprobes(jps, i);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001786 break;
1787 }
1788 }
1789 return ret;
1790}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001791EXPORT_SYMBOL_GPL(register_jprobes);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001792
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001793int register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001795 return register_jprobes(&jp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001797EXPORT_SYMBOL_GPL(register_jprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001799void unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001801 unregister_jprobes(&jp, 1);
1802}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001803EXPORT_SYMBOL_GPL(unregister_jprobe);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001804
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001805void unregister_jprobes(struct jprobe **jps, int num)
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001806{
1807 int i;
1808
1809 if (num <= 0)
1810 return;
1811 mutex_lock(&kprobe_mutex);
1812 for (i = 0; i < num; i++)
1813 if (__unregister_kprobe_top(&jps[i]->kp) < 0)
1814 jps[i]->kp.addr = NULL;
1815 mutex_unlock(&kprobe_mutex);
1816
1817 synchronize_sched();
1818 for (i = 0; i < num; i++) {
1819 if (jps[i]->kp.addr)
1820 __unregister_kprobe_bottom(&jps[i]->kp);
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001823EXPORT_SYMBOL_GPL(unregister_jprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001825#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -08001826/*
1827 * This kprobe pre_handler is registered with every kretprobe. When probe
1828 * hits it will set up the return probe.
1829 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001830static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Adrian Bunke65cefe2006-02-03 03:03:42 -08001831{
1832 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001833 unsigned long hash, flags = 0;
1834 struct kretprobe_instance *ri;
Adrian Bunke65cefe2006-02-03 03:03:42 -08001835
Masami Hiramatsuf96f5672014-08-04 03:10:16 +00001836 /*
1837 * To avoid deadlocks, prohibit return probing in NMI contexts,
1838 * just skip the probe and increase the (inexact) 'nmissed'
1839 * statistical counter, so that the user is informed that
1840 * something happened:
1841 */
1842 if (unlikely(in_nmi())) {
1843 rp->nmissed++;
1844 return 0;
1845 }
1846
1847 /* TODO: consider to only swap the RA after the last pre_handler fired */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001848 hash = hash_ptr(current, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001849 raw_spin_lock_irqsave(&rp->lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001850 if (!hlist_empty(&rp->free_instances)) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001851 ri = hlist_entry(rp->free_instances.first,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001852 struct kretprobe_instance, hlist);
1853 hlist_del(&ri->hlist);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001854 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001855
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001856 ri->rp = rp;
1857 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001858
Jiang Liu55ca6142012-02-03 15:37:16 -08001859 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
1860 raw_spin_lock_irqsave(&rp->lock, flags);
1861 hlist_add_head(&ri->hlist, &rp->free_instances);
1862 raw_spin_unlock_irqrestore(&rp->lock, flags);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001863 return 0;
Jiang Liu55ca6142012-02-03 15:37:16 -08001864 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001865
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001866 arch_prepare_kretprobe(ri, regs);
1867
1868 /* XXX(hch): why is there no hlist_move_head? */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001869 INIT_HLIST_NODE(&ri->hlist);
1870 kretprobe_table_lock(hash, &flags);
1871 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
1872 kretprobe_table_unlock(hash, &flags);
1873 } else {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001874 rp->nmissed++;
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001875 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001876 }
Adrian Bunke65cefe2006-02-03 03:03:42 -08001877 return 0;
1878}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001879NOKPROBE_SYMBOL(pre_handler_kretprobe);
Adrian Bunke65cefe2006-02-03 03:03:42 -08001880
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001881int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001882{
1883 int ret = 0;
1884 struct kretprobe_instance *inst;
1885 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001886 void *addr;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001887
1888 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001889 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001890 if (IS_ERR(addr))
1891 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001892
1893 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1894 if (kretprobe_blacklist[i].addr == addr)
1895 return -EINVAL;
1896 }
1897 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001898
1899 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07001900 rp->kp.post_handler = NULL;
1901 rp->kp.fault_handler = NULL;
1902 rp->kp.break_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001903
1904 /* Pre-allocate memory for max kretprobe instances */
1905 if (rp->maxactive <= 0) {
1906#ifdef CONFIG_PREEMPT
Heiko Carstensc2ef6662009-12-21 13:02:24 +01001907 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07001908#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05301909 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001910#endif
1911 }
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001912 raw_spin_lock_init(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001913 INIT_HLIST_HEAD(&rp->free_instances);
1914 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001915 inst = kmalloc(sizeof(struct kretprobe_instance) +
1916 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001917 if (inst == NULL) {
1918 free_rp_inst(rp);
1919 return -ENOMEM;
1920 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001921 INIT_HLIST_NODE(&inst->hlist);
1922 hlist_add_head(&inst->hlist, &rp->free_instances);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001923 }
1924
1925 rp->nmissed = 0;
1926 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001927 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001928 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001929 free_rp_inst(rp);
1930 return ret;
1931}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001932EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001933
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001934int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001935{
1936 int ret = 0, i;
1937
1938 if (num <= 0)
1939 return -EINVAL;
1940 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001941 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001942 if (ret < 0) {
1943 if (i > 0)
1944 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001945 break;
1946 }
1947 }
1948 return ret;
1949}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001950EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001951
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001952void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001953{
1954 unregister_kretprobes(&rp, 1);
1955}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001956EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001957
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001958void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001959{
1960 int i;
1961
1962 if (num <= 0)
1963 return;
1964 mutex_lock(&kprobe_mutex);
1965 for (i = 0; i < num; i++)
1966 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
1967 rps[i]->kp.addr = NULL;
1968 mutex_unlock(&kprobe_mutex);
1969
1970 synchronize_sched();
1971 for (i = 0; i < num; i++) {
1972 if (rps[i]->kp.addr) {
1973 __unregister_kprobe_bottom(&rps[i]->kp);
1974 cleanup_rp_inst(rps[i]);
1975 }
1976 }
1977}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001978EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001979
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001980#else /* CONFIG_KRETPROBES */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001981int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001982{
1983 return -ENOSYS;
1984}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001985EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001986
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001987int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001988{
1989 return -ENOSYS;
1990}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001991EXPORT_SYMBOL_GPL(register_kretprobes);
1992
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001993void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001994{
1995}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001996EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001997
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001998void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001999{
2000}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002001EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002002
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002003static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002004{
2005 return 0;
2006}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002007NOKPROBE_SYMBOL(pre_handler_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002008
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002009#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002010
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002011/* Set the kprobe gone and remove its instruction buffer. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002012static void kill_kprobe(struct kprobe *p)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002013{
2014 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002015
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002016 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002017 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002018 /*
2019 * If this is an aggr_kprobe, we have to list all the
2020 * chained probes and mark them GONE.
2021 */
2022 list_for_each_entry_rcu(kp, &p->list, list)
2023 kp->flags |= KPROBE_FLAG_GONE;
2024 p->post_handler = NULL;
2025 p->break_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002026 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002027 }
2028 /*
2029 * Here, we can remove insn_slot safely, because no thread calls
2030 * the original probed function (which will be freed soon) any more.
2031 */
2032 arch_remove_kprobe(p);
2033}
2034
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002035/* Disable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002036int disable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002037{
2038 int ret = 0;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002039
2040 mutex_lock(&kprobe_mutex);
2041
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09002042 /* Disable this kprobe */
2043 if (__disable_kprobe(kp) == NULL)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002044 ret = -EINVAL;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002045
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002046 mutex_unlock(&kprobe_mutex);
2047 return ret;
2048}
2049EXPORT_SYMBOL_GPL(disable_kprobe);
2050
2051/* Enable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002052int enable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002053{
2054 int ret = 0;
2055 struct kprobe *p;
2056
2057 mutex_lock(&kprobe_mutex);
2058
2059 /* Check whether specified probe is valid. */
2060 p = __get_valid_kprobe(kp);
2061 if (unlikely(p == NULL)) {
2062 ret = -EINVAL;
2063 goto out;
2064 }
2065
2066 if (kprobe_gone(kp)) {
2067 /* This kprobe has gone, we couldn't enable it. */
2068 ret = -EINVAL;
2069 goto out;
2070 }
2071
2072 if (p != kp)
2073 kp->flags &= ~KPROBE_FLAG_DISABLED;
2074
2075 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2076 p->flags &= ~KPROBE_FLAG_DISABLED;
2077 arm_kprobe(p);
2078 }
2079out:
2080 mutex_unlock(&kprobe_mutex);
2081 return ret;
2082}
2083EXPORT_SYMBOL_GPL(enable_kprobe);
2084
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002085void dump_kprobe(struct kprobe *kp)
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002086{
2087 printk(KERN_WARNING "Dumping kprobe:\n");
2088 printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
2089 kp->symbol_name, kp->addr, kp->offset);
2090}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002091NOKPROBE_SYMBOL(dump_kprobe);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002092
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002093/*
2094 * Lookup and populate the kprobe_blacklist.
2095 *
2096 * Unlike the kretprobe blacklist, we'll need to determine
2097 * the range of addresses that belong to the said functions,
2098 * since a kprobe need not necessarily be at the beginning
2099 * of a function.
2100 */
2101static int __init populate_kprobe_blacklist(unsigned long *start,
2102 unsigned long *end)
2103{
2104 unsigned long *iter;
2105 struct kprobe_blacklist_entry *ent;
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002106 unsigned long entry, offset = 0, size = 0;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002107
2108 for (iter = start; iter < end; iter++) {
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002109 entry = arch_deref_entry_point((void *)*iter);
2110
2111 if (!kernel_text_address(entry) ||
2112 !kallsyms_lookup_size_offset(entry, &size, &offset)) {
2113 pr_err("Failed to find blacklist at %p\n",
2114 (void *)entry);
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002115 continue;
2116 }
2117
2118 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2119 if (!ent)
2120 return -ENOMEM;
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002121 ent->start_addr = entry;
2122 ent->end_addr = entry + size;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002123 INIT_LIST_HEAD(&ent->list);
2124 list_add_tail(&ent->list, &kprobe_blacklist);
2125 }
2126 return 0;
2127}
2128
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002129/* Module notifier call back, checking kprobes on the module */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002130static int kprobes_module_callback(struct notifier_block *nb,
2131 unsigned long val, void *data)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002132{
2133 struct module *mod = data;
2134 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002135 struct kprobe *p;
2136 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002137 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002138
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002139 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002140 return NOTIFY_DONE;
2141
2142 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002143 * When MODULE_STATE_GOING was notified, both of module .text and
2144 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2145 * notified, only .init.text section would be freed. We need to
2146 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002147 */
2148 mutex_lock(&kprobe_mutex);
2149 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2150 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002151 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002152 if (within_module_init((unsigned long)p->addr, mod) ||
2153 (checkcore &&
2154 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002155 /*
2156 * The vaddr this probe is installed will soon
2157 * be vfreed buy not synced to disk. Hence,
2158 * disarming the breakpoint isn't needed.
2159 */
2160 kill_kprobe(p);
2161 }
2162 }
2163 mutex_unlock(&kprobe_mutex);
2164 return NOTIFY_DONE;
2165}
2166
2167static struct notifier_block kprobe_module_nb = {
2168 .notifier_call = kprobes_module_callback,
2169 .priority = 0
2170};
2171
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002172/* Markers of _kprobe_blacklist section */
2173extern unsigned long __start_kprobe_blacklist[];
2174extern unsigned long __stop_kprobe_blacklist[];
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176static int __init init_kprobes(void)
2177{
2178 int i, err = 0;
2179
2180 /* FIXME allocate the probe table, currently defined statically */
2181 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002182 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002184 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002185 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
Hien Nguyenb94cce92005-06-23 00:09:19 -07002186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002188 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2189 __stop_kprobe_blacklist);
2190 if (err) {
2191 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2192 pr_err("Please take care of using kprobes.\n");
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002193 }
2194
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002195 if (kretprobe_blacklist_size) {
2196 /* lookup the function address from its name */
2197 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05302198 kretprobe_blacklist[i].addr =
2199 kprobe_lookup_name(kretprobe_blacklist[i].name);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002200 if (!kretprobe_blacklist[i].addr)
2201 printk("kretprobe: lookup failed: %s\n",
2202 kretprobe_blacklist[i].name);
2203 }
2204 }
2205
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002206#if defined(CONFIG_OPTPROBES)
2207#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002208 /* Init kprobe_optinsn_slots */
2209 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2210#endif
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002211 /* By default, kprobes can be optimized */
2212 kprobes_allow_optimization = true;
2213#endif
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002214
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002215 /* By default, kprobes are armed */
2216 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002217
Rusty Lynch67729262005-07-05 18:54:50 -07002218 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002219 if (!err)
2220 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002221 if (!err)
2222 err = register_module_notifier(&kprobe_module_nb);
2223
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002224 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07002225
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01002226 if (!err)
2227 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return err;
2229}
2230
Srinivasa Ds346fd592007-02-20 13:57:54 -08002231#ifdef CONFIG_DEBUG_FS
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002232static void report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002233 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002234{
2235 char *kprobe_type;
2236
2237 if (p->pre_handler == pre_handler_kretprobe)
2238 kprobe_type = "r";
2239 else if (p->pre_handler == setjmp_pre_handler)
2240 kprobe_type = "j";
2241 else
2242 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002243
Srinivasa Ds346fd592007-02-20 13:57:54 -08002244 if (sym)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002245 seq_printf(pi, "%p %s %s+0x%x %s ",
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002246 p->addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002247 (modname ? modname : " "));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002248 else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002249 seq_printf(pi, "%p %s %p ",
2250 p->addr, kprobe_type, p->addr);
2251
2252 if (!pp)
2253 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002254 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002255 (kprobe_gone(p) ? "[GONE]" : ""),
2256 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002257 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2258 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002259}
2260
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002261static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002262{
2263 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2264}
2265
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002266static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002267{
2268 (*pos)++;
2269 if (*pos >= KPROBE_TABLE_SIZE)
2270 return NULL;
2271 return pos;
2272}
2273
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002274static void kprobe_seq_stop(struct seq_file *f, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002275{
2276 /* Nothing to do */
2277}
2278
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002279static int show_kprobe_addr(struct seq_file *pi, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002280{
2281 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002282 struct kprobe *p, *kp;
2283 const char *sym = NULL;
2284 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002285 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002286 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002287
2288 head = &kprobe_table[i];
2289 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002290 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002291 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002292 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002293 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002294 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002295 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002296 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002297 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002298 }
2299 preempt_enable();
2300 return 0;
2301}
2302
James Morris88e9d342009-09-22 16:43:43 -07002303static const struct seq_operations kprobes_seq_ops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002304 .start = kprobe_seq_start,
2305 .next = kprobe_seq_next,
2306 .stop = kprobe_seq_stop,
2307 .show = show_kprobe_addr
2308};
2309
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002310static int kprobes_open(struct inode *inode, struct file *filp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002311{
2312 return seq_open(filp, &kprobes_seq_ops);
2313}
2314
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002315static const struct file_operations debugfs_kprobes_operations = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002316 .open = kprobes_open,
2317 .read = seq_read,
2318 .llseek = seq_lseek,
2319 .release = seq_release,
2320};
2321
Masami Hiramatsu63724742014-04-17 17:18:49 +09002322/* kprobes/blacklist -- shows which functions can not be probed */
2323static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2324{
2325 return seq_list_start(&kprobe_blacklist, *pos);
2326}
2327
2328static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2329{
2330 return seq_list_next(v, &kprobe_blacklist, pos);
2331}
2332
2333static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2334{
2335 struct kprobe_blacklist_entry *ent =
2336 list_entry(v, struct kprobe_blacklist_entry, list);
2337
2338 seq_printf(m, "0x%p-0x%p\t%ps\n", (void *)ent->start_addr,
2339 (void *)ent->end_addr, (void *)ent->start_addr);
2340 return 0;
2341}
2342
2343static const struct seq_operations kprobe_blacklist_seq_ops = {
2344 .start = kprobe_blacklist_seq_start,
2345 .next = kprobe_blacklist_seq_next,
2346 .stop = kprobe_seq_stop, /* Reuse void function */
2347 .show = kprobe_blacklist_seq_show,
2348};
2349
2350static int kprobe_blacklist_open(struct inode *inode, struct file *filp)
2351{
2352 return seq_open(filp, &kprobe_blacklist_seq_ops);
2353}
2354
2355static const struct file_operations debugfs_kprobe_blacklist_ops = {
2356 .open = kprobe_blacklist_open,
2357 .read = seq_read,
2358 .llseek = seq_lseek,
2359 .release = seq_release,
2360};
2361
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002362static void arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002363{
2364 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002365 struct kprobe *p;
2366 unsigned int i;
2367
2368 mutex_lock(&kprobe_mutex);
2369
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002370 /* If kprobes are armed, just return */
2371 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002372 goto already_enabled;
2373
Wang Nan977ad482015-02-13 14:40:24 -08002374 /*
2375 * optimize_kprobe() called by arm_kprobe() checks
2376 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2377 * arm_kprobe.
2378 */
2379 kprobes_all_disarmed = false;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002380 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002381 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2382 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002383 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002384 if (!kprobe_disabled(p))
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002385 arm_kprobe(p);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002386 }
2387
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002388 printk(KERN_INFO "Kprobes globally enabled\n");
2389
2390already_enabled:
2391 mutex_unlock(&kprobe_mutex);
2392 return;
2393}
2394
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002395static void disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002396{
2397 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002398 struct kprobe *p;
2399 unsigned int i;
2400
2401 mutex_lock(&kprobe_mutex);
2402
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002403 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002404 if (kprobes_all_disarmed) {
2405 mutex_unlock(&kprobe_mutex);
2406 return;
2407 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002408
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002409 kprobes_all_disarmed = true;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002410 printk(KERN_INFO "Kprobes globally disabled\n");
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002411
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002412 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2413 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002414 hlist_for_each_entry_rcu(p, head, hlist) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002415 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002416 disarm_kprobe(p, false);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002417 }
2418 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002419 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002420
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002421 /* Wait for disarming all kprobes by optimizer */
2422 wait_for_kprobe_optimizer();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002423}
2424
2425/*
2426 * XXX: The debugfs bool file interface doesn't allow for callbacks
2427 * when the bool state is switched. We can reuse that facility when
2428 * available
2429 */
2430static ssize_t read_enabled_file_bool(struct file *file,
2431 char __user *user_buf, size_t count, loff_t *ppos)
2432{
2433 char buf[3];
2434
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002435 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002436 buf[0] = '1';
2437 else
2438 buf[0] = '0';
2439 buf[1] = '\n';
2440 buf[2] = 0x00;
2441 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2442}
2443
2444static ssize_t write_enabled_file_bool(struct file *file,
2445 const char __user *user_buf, size_t count, loff_t *ppos)
2446{
2447 char buf[32];
Stephen Boydefeb1562012-01-12 17:17:11 -08002448 size_t buf_size;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002449
2450 buf_size = min(count, (sizeof(buf)-1));
2451 if (copy_from_user(buf, user_buf, buf_size))
2452 return -EFAULT;
2453
Mathias Krause10fb46d2013-07-03 15:05:39 -07002454 buf[buf_size] = '\0';
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002455 switch (buf[0]) {
2456 case 'y':
2457 case 'Y':
2458 case '1':
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002459 arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002460 break;
2461 case 'n':
2462 case 'N':
2463 case '0':
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002464 disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002465 break;
Mathias Krause10fb46d2013-07-03 15:05:39 -07002466 default:
2467 return -EINVAL;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002468 }
2469
2470 return count;
2471}
2472
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002473static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002474 .read = read_enabled_file_bool,
2475 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002476 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002477};
2478
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002479static int __init debugfs_kprobe_init(void)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002480{
2481 struct dentry *dir, *file;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002482 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002483
2484 dir = debugfs_create_dir("kprobes", NULL);
2485 if (!dir)
2486 return -ENOMEM;
2487
Randy Dunlape3869792007-05-08 00:27:01 -07002488 file = debugfs_create_file("list", 0444, dir, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002489 &debugfs_kprobes_operations);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002490 if (!file)
2491 goto error;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002492
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002493 file = debugfs_create_file("enabled", 0600, dir,
2494 &value, &fops_kp);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002495 if (!file)
2496 goto error;
2497
2498 file = debugfs_create_file("blacklist", 0444, dir, NULL,
2499 &debugfs_kprobe_blacklist_ops);
2500 if (!file)
2501 goto error;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002502
Srinivasa Ds346fd592007-02-20 13:57:54 -08002503 return 0;
Masami Hiramatsu63724742014-04-17 17:18:49 +09002504
2505error:
2506 debugfs_remove(dir);
2507 return -ENOMEM;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002508}
2509
2510late_initcall(debugfs_kprobe_init);
2511#endif /* CONFIG_DEBUG_FS */
2512
2513module_init(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002515/* defined in arch/.../kernel/kprobes.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516EXPORT_SYMBOL_GPL(jprobe_return);