blob: f7fb5d135930fa46e8f57a85ce67b856a6e23347 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Kernel Probes (KProbes)
4 * kernel/kprobes.c
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) IBM Corporation, 2002, 2004
7 *
8 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
9 * Probes initial implementation (includes suggestions from
10 * Rusty Russell).
11 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
12 * hlists and exceptions notifier as suggested by Andi Kleen.
13 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
14 * interface to access function arguments.
15 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
16 * exceptions notifier to be first on the priority list.
Hien Nguyenb94cce92005-06-23 00:09:19 -070017 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
18 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
19 * <prasanna@in.ibm.com> added function-return probes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/hash.h>
23#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080024#include <linux/slab.h>
Randy Dunlape3869792007-05-08 00:27:01 -070025#include <linux/stddef.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070027#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070028#include <linux/kallsyms.h>
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080029#include <linux/freezer.h>
Srinivasa Ds346fd592007-02-20 13:57:54 -080030#include <linux/seq_file.h>
31#include <linux/debugfs.h>
Masami Hiramatsub2be84d2010-02-25 08:34:15 -050032#include <linux/sysctl.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070033#include <linux/kdebug.h>
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -050034#include <linux/memory.h>
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -050035#include <linux/ftrace.h>
Masami Hiramatsuafd66252010-02-25 08:34:07 -050036#include <linux/cpu.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040037#include <linux/jump_label.h>
Adrian Hunter69e49082020-05-12 15:19:11 +030038#include <linux/perf_event.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070039
Christoph Hellwigbfd45be2016-10-11 13:52:22 -070040#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/cacheflush.h>
42#include <asm/errno.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define KPROBE_HASH_BITS 6
46#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
47
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070048
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070049static int kprobes_initialized;
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +090050/* kprobe_table can be accessed by
51 * - Normal hlist traversal and RCU add/del under kprobe_mutex is held.
52 * Or
53 * - RCU hlist traversal under disabling preempt (breakpoint handlers)
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
56
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070057/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070058static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070059
Masami Hiramatsu43948f52010-10-25 22:18:01 +090060/* This protects kprobe_table and optimizing_list */
61static DEFINE_MUTEX(kprobe_mutex);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080062static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070063
Naveen N. Rao290e3072017-04-19 18:21:01 +053064kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
65 unsigned int __unused)
Naveen N. Rao49e0b462017-04-19 18:21:00 +053066{
67 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
68}
69
Masami Hiramatsu376e2422014-04-17 17:17:05 +090070/* Blacklist -- list of struct kprobe_blacklist_entry */
71static LIST_HEAD(kprobe_blacklist);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070072
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080073#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070074/*
75 * kprobe->ainsn.insn points to the copy of the instruction to be
76 * single-stepped. x86_64, POWER4 and above have no-exec support and
77 * stepping on the instruction on a vmalloced/kmalloced/data page
78 * is a recipe for disaster
79 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070080struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -040081 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070082 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -070083 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070084 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080085 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050086 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070087};
88
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050089#define KPROBE_INSN_PAGE_SIZE(slots) \
90 (offsetof(struct kprobe_insn_page, slot_used) + \
91 (sizeof(char) * (slots)))
92
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050093static int slots_per_page(struct kprobe_insn_cache *c)
94{
95 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
96}
97
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -080098enum kprobe_slot_state {
99 SLOT_CLEAN = 0,
100 SLOT_DIRTY = 1,
101 SLOT_USED = 2,
102};
103
Masami Hiramatsu63fef142017-08-18 17:24:00 +0900104void __weak *alloc_insn_page(void)
Heiko Carstensaf963972013-09-11 14:24:13 -0700105{
106 return module_alloc(PAGE_SIZE);
107}
108
Masami Hiramatsuc93f5cf2017-05-25 19:38:17 +0900109void __weak free_insn_page(void *page)
Heiko Carstensaf963972013-09-11 14:24:13 -0700110{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030111 module_memfree(page);
Heiko Carstensaf963972013-09-11 14:24:13 -0700112}
113
Heiko Carstensc802d642013-09-11 14:24:11 -0700114struct kprobe_insn_cache kprobe_insn_slots = {
115 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700116 .alloc = alloc_insn_page,
117 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300118 .sym = KPROBE_INSN_PAGE_SYM,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500119 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
120 .insn_size = MAX_INSN_SIZE,
121 .nr_garbage = 0,
122};
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900123static int collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800124
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700125/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800126 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700127 * We allocate an executable page if there's no room on existing ones.
128 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900129kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700130{
131 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700132 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700133
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900134 /* Since the slot array is not protected by rcu, we need a mutex */
Heiko Carstensc802d642013-09-11 14:24:11 -0700135 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700136 retry:
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900137 rcu_read_lock();
138 list_for_each_entry_rcu(kip, &c->pages, list) {
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500139 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700140 int i;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500141 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800142 if (kip->slot_used[i] == SLOT_CLEAN) {
143 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700144 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700145 slot = kip->insns + (i * c->insn_size);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900146 rcu_read_unlock();
Heiko Carstensc802d642013-09-11 14:24:11 -0700147 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700148 }
149 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500150 /* kip->nused is broken. Fix it. */
151 kip->nused = slots_per_page(c);
152 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700153 }
154 }
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900155 rcu_read_unlock();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700156
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800157 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500158 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800159 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500160
161 /* All out of space. Need to allocate a new page. */
162 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700163 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700164 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700165
166 /*
167 * Use module_alloc so this page is within +/- 2GB of where the
168 * kernel image and loaded module images reside. This is required
169 * so x86_64 can correctly handle the %rip-relative fixups.
170 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700171 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700172 if (!kip->insns) {
173 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700174 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700175 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400176 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500177 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800178 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700179 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800180 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700181 kip->cache = c;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900182 list_add_rcu(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700183 slot = kip->insns;
Adrian Hunter69e49082020-05-12 15:19:11 +0300184
185 /* Record the perf ksymbol register event after adding the page */
186 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns,
187 PAGE_SIZE, false, c->sym);
Heiko Carstensc802d642013-09-11 14:24:11 -0700188out:
189 mutex_unlock(&c->mutex);
190 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800191}
192
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800193/* Return 1 if all garbages are collected, otherwise 0. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900194static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800195{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800196 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800197 kip->nused--;
198 if (kip->nused == 0) {
199 /*
200 * Page is no longer in use. Free it unless
201 * it's the last one. We keep the last one
202 * so as not to have to set it up again the
203 * next time somebody inserts a probe.
204 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500205 if (!list_is_singular(&kip->list)) {
Adrian Hunter69e49082020-05-12 15:19:11 +0300206 /*
207 * Record perf ksymbol unregister event before removing
208 * the page.
209 */
210 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
211 (unsigned long)kip->insns, PAGE_SIZE, true,
212 kip->cache->sym);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900213 list_del_rcu(&kip->list);
214 synchronize_rcu();
Heiko Carstensaf963972013-09-11 14:24:13 -0700215 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800216 kfree(kip);
217 }
218 return 1;
219 }
220 return 0;
221}
222
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900223static int collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800224{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400225 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800226
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500227 /* Ensure no-one is interrupted on the garbages */
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -0800228 synchronize_rcu();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800229
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500230 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800231 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800232 if (kip->ngarbage == 0)
233 continue;
234 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500235 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900236 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800237 break;
238 }
239 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500240 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800241 return 0;
242}
243
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900244void __free_insn_slot(struct kprobe_insn_cache *c,
245 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500246{
247 struct kprobe_insn_page *kip;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900248 long idx;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500249
Heiko Carstensc802d642013-09-11 14:24:11 -0700250 mutex_lock(&c->mutex);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900251 rcu_read_lock();
252 list_for_each_entry_rcu(kip, &c->pages, list) {
253 idx = ((long)slot - (long)kip->insns) /
254 (c->insn_size * sizeof(kprobe_opcode_t));
255 if (idx >= 0 && idx < slots_per_page(c))
Heiko Carstensc802d642013-09-11 14:24:11 -0700256 goto out;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900257 }
258 /* Could not find this slot. */
259 WARN_ON(1);
260 kip = NULL;
261out:
262 rcu_read_unlock();
263 /* Mark and sweep: this may sleep */
264 if (kip) {
265 /* Check double free */
266 WARN_ON(kip->slot_used[idx] != SLOT_USED);
267 if (dirty) {
268 kip->slot_used[idx] = SLOT_DIRTY;
269 kip->ngarbage++;
270 if (++c->nr_garbage > slots_per_page(c))
271 collect_garbage_slots(c);
272 } else {
273 collect_one_slot(kip, idx);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500274 }
275 }
Heiko Carstensc802d642013-09-11 14:24:11 -0700276 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500277}
278
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900279/*
280 * Check given address is on the page of kprobe instruction slots.
281 * This will be used for checking whether the address on a stack
282 * is on a text area or not.
283 */
284bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
285{
286 struct kprobe_insn_page *kip;
287 bool ret = false;
288
289 rcu_read_lock();
290 list_for_each_entry_rcu(kip, &c->pages, list) {
291 if (addr >= (unsigned long)kip->insns &&
292 addr < (unsigned long)kip->insns + PAGE_SIZE) {
293 ret = true;
294 break;
295 }
296 }
297 rcu_read_unlock();
298
299 return ret;
300}
301
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300302int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
303 unsigned long *value, char *type, char *sym)
304{
305 struct kprobe_insn_page *kip;
306 int ret = -ERANGE;
307
308 rcu_read_lock();
309 list_for_each_entry_rcu(kip, &c->pages, list) {
310 if ((*symnum)--)
311 continue;
312 strlcpy(sym, c->sym, KSYM_NAME_LEN);
313 *type = 't';
314 *value = (unsigned long)kip->insns;
315 ret = 0;
316 break;
317 }
318 rcu_read_unlock();
319
320 return ret;
321}
322
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500323#ifdef CONFIG_OPTPROBES
324/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700325struct kprobe_insn_cache kprobe_optinsn_slots = {
326 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700327 .alloc = alloc_insn_page,
328 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300329 .sym = KPROBE_OPTINSN_PAGE_SYM,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500330 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
331 /* .insn_size is initialized later */
332 .nr_garbage = 0,
333};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500334#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800335#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700336
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800337/* We have preemption disabled.. so it is safe to use __ versions */
338static inline void set_kprobe_instance(struct kprobe *kp)
339{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600340 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800341}
342
343static inline void reset_kprobe_instance(void)
344{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600345 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800346}
347
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800348/*
349 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800350 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800351 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800352 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800353 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900354struct kprobe *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800357 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Masami Hiramatsu6743ad42020-05-12 17:02:33 +0900360 hlist_for_each_entry_rcu(p, head, hlist,
361 lockdep_is_held(&kprobe_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (p->addr == addr)
363 return p;
364 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return NULL;
367}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900368NOKPROBE_SYMBOL(get_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900370static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500371
372/* Return true if the kprobe is an aggregator */
373static inline int kprobe_aggrprobe(struct kprobe *p)
374{
375 return p->pre_handler == aggr_pre_handler;
376}
377
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900378/* Return true(!0) if the kprobe is unused */
379static inline int kprobe_unused(struct kprobe *p)
380{
381 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
382 list_empty(&p->list);
383}
384
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500385/*
386 * Keep all fields in the kprobe consistent
387 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900388static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500389{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900390 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
391 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500392}
393
394#ifdef CONFIG_OPTPROBES
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500395/* NOTE: change this value only with kprobe_mutex held */
396static bool kprobes_allow_optimization;
397
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500398/*
399 * Call all pre_handler on the list, but ignores its return value.
400 * This must be called from arch-dep optimized caller.
401 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900402void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500403{
404 struct kprobe *kp;
405
406 list_for_each_entry_rcu(kp, &p->list, list) {
407 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
408 set_kprobe_instance(kp);
Naveen N. Rao4f3a8712017-10-17 13:48:34 +0530409 kp->pre_handler(kp, regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500410 }
411 reset_kprobe_instance();
412 }
413}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900414NOKPROBE_SYMBOL(opt_pre_handler);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500415
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900416/* Free optimized instructions and optimized_kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900417static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900418{
419 struct optimized_kprobe *op;
420
421 op = container_of(p, struct optimized_kprobe, kp);
422 arch_remove_optimized_kprobe(op);
423 arch_remove_kprobe(p);
424 kfree(op);
425}
426
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500427/* Return true(!0) if the kprobe is ready for optimization. */
428static inline int kprobe_optready(struct kprobe *p)
429{
430 struct optimized_kprobe *op;
431
432 if (kprobe_aggrprobe(p)) {
433 op = container_of(p, struct optimized_kprobe, kp);
434 return arch_prepared_optinsn(&op->optinsn);
435 }
436
437 return 0;
438}
439
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900440/* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
441static inline int kprobe_disarmed(struct kprobe *p)
442{
443 struct optimized_kprobe *op;
444
445 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
446 if (!kprobe_aggrprobe(p))
447 return kprobe_disabled(p);
448
449 op = container_of(p, struct optimized_kprobe, kp);
450
451 return kprobe_disabled(p) && list_empty(&op->list);
452}
453
454/* Return true(!0) if the probe is queued on (un)optimizing lists */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900455static int kprobe_queued(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900456{
457 struct optimized_kprobe *op;
458
459 if (kprobe_aggrprobe(p)) {
460 op = container_of(p, struct optimized_kprobe, kp);
461 if (!list_empty(&op->list))
462 return 1;
463 }
464 return 0;
465}
466
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500467/*
468 * Return an optimized kprobe whose optimizing code replaces
469 * instructions including addr (exclude breakpoint).
470 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900471static struct kprobe *get_optimized_kprobe(unsigned long addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500472{
473 int i;
474 struct kprobe *p = NULL;
475 struct optimized_kprobe *op;
476
477 /* Don't check i == 0, since that is a breakpoint case. */
478 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
479 p = get_kprobe((void *)(addr - i));
480
481 if (p && kprobe_optready(p)) {
482 op = container_of(p, struct optimized_kprobe, kp);
483 if (arch_within_optimized_kprobe(op, addr))
484 return p;
485 }
486
487 return NULL;
488}
489
490/* Optimization staging list, protected by kprobe_mutex */
491static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900492static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900493static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500494
495static void kprobe_optimizer(struct work_struct *work);
496static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
497#define OPTIMIZE_DELAY 5
498
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900499/*
500 * Optimize (replace a breakpoint with a jump) kprobes listed on
501 * optimizing_list.
502 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900503static void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500504{
Andrea Righif1c6ece2019-08-12 20:43:02 +0200505 lockdep_assert_held(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500506 /*
507 * The optimization/unoptimization refers online_cpus via
508 * stop_machine() and cpu-hotplug modifies online_cpus.
509 * And same time, text_mutex will be held in cpu-hotplug and here.
510 * This combination can cause a deadlock (cpu-hotplug try to lock
511 * text_mutex but stop_machine can not be done because online_cpus
512 * has been changed)
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200513 * To avoid this deadlock, caller must have locked cpu hotplug
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500514 * for preventing cpu-hotplug outside of text_mutex locking.
515 */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200516 lockdep_assert_cpus_held();
517
518 /* Optimization never be done when disarmed */
519 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
520 list_empty(&optimizing_list))
521 return;
522
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900523 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900524}
525
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900526/*
527 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
528 * if need) kprobes listed on unoptimizing_list.
529 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900530static void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900531{
532 struct optimized_kprobe *op, *tmp;
533
Andrea Righif1c6ece2019-08-12 20:43:02 +0200534 lockdep_assert_held(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200535 /* See comment in do_optimize_kprobes() */
536 lockdep_assert_cpus_held();
537
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900538 /* Unoptimization must be done anytime */
539 if (list_empty(&unoptimizing_list))
540 return;
541
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900542 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900543 /* Loop free_list for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900544 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900545 /* Switching from detour code to origin */
546 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900547 /* Disarm probes if marked disabled */
548 if (kprobe_disabled(&op->kp))
549 arch_disarm_kprobe(&op->kp);
550 if (kprobe_unused(&op->kp)) {
551 /*
552 * Remove unused probes from hash list. After waiting
553 * for synchronization, these probes are reclaimed.
554 * (reclaiming is done by do_free_cleaned_kprobes.)
555 */
556 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900557 } else
558 list_del_init(&op->list);
559 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900560}
561
562/* Reclaim all kprobes on the free_list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900563static void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900564{
565 struct optimized_kprobe *op, *tmp;
566
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900567 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900568 list_del_init(&op->list);
Masami Hiramatsucbdd96f2018-09-11 19:21:09 +0900569 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
570 /*
571 * This must not happen, but if there is a kprobe
572 * still in use, keep it on kprobes hash list.
573 */
574 continue;
575 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900576 free_aggr_kprobe(&op->kp);
577 }
578}
579
580/* Start optimizer after OPTIMIZE_DELAY passed */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900581static void kick_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900582{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800583 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900584}
585
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900586/* Kprobe jump optimizer */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900587static void kprobe_optimizer(struct work_struct *work)
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900588{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900589 mutex_lock(&kprobe_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200590 cpus_read_lock();
Andrea Righif1c6ece2019-08-12 20:43:02 +0200591 mutex_lock(&text_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900592
593 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900594 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
595 * kprobes before waiting for quiesence period.
596 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900597 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900598
599 /*
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900600 * Step 2: Wait for quiesence period to ensure all potentially
601 * preempted tasks to have normally scheduled. Because optprobe
602 * may modify multiple instructions, there is a chance that Nth
603 * instruction is preempted. In that case, such tasks can return
604 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
605 * Note that on non-preemptive kernel, this is transparently converted
606 * to synchronoze_sched() to wait for all interrupts to have completed.
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900607 */
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900608 synchronize_rcu_tasks();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900609
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900610 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900611 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900612
613 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900614 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900615
Andrea Righif1c6ece2019-08-12 20:43:02 +0200616 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200617 cpus_read_unlock();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900618
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900619 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900620 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900621 kick_kprobe_optimizer();
Masami Hiramatsu1a0aa992020-05-12 17:02:56 +0900622
623 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900624}
625
626/* Wait for completing optimization and unoptimization */
Thomas Gleixner30e7d8942017-05-17 10:19:49 +0200627void wait_for_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900628{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800629 mutex_lock(&kprobe_mutex);
630
631 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
632 mutex_unlock(&kprobe_mutex);
633
634 /* this will also make optimizing_work execute immmediately */
635 flush_delayed_work(&optimizing_work);
636 /* @optimizing_work might not have been queued yet, relax */
637 cpu_relax();
638
639 mutex_lock(&kprobe_mutex);
640 }
641
642 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500643}
644
Masami Hiramatsue4add242020-01-07 23:42:24 +0900645static bool optprobe_queued_unopt(struct optimized_kprobe *op)
646{
647 struct optimized_kprobe *_op;
648
649 list_for_each_entry(_op, &unoptimizing_list, list) {
650 if (op == _op)
651 return true;
652 }
653
654 return false;
655}
656
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500657/* Optimize kprobe if p is ready to be optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900658static void optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500659{
660 struct optimized_kprobe *op;
661
662 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500663 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500664 (kprobe_disabled(p) || kprobes_all_disarmed))
665 return;
666
Masami Hiramatsu059053a2018-06-20 01:10:27 +0900667 /* kprobes with post_handler can not be optimized */
668 if (p->post_handler)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500669 return;
670
671 op = container_of(p, struct optimized_kprobe, kp);
672
673 /* Check there is no other kprobes at the optimized instructions */
674 if (arch_check_optimized_kprobe(op) < 0)
675 return;
676
677 /* Check if it is already optimized. */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900678 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
679 if (optprobe_queued_unopt(op)) {
680 /* This is under unoptimizing. Just dequeue the probe */
681 list_del_init(&op->list);
682 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500683 return;
Masami Hiramatsue4add242020-01-07 23:42:24 +0900684 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500685 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900686
Masami Hiramatsue4add242020-01-07 23:42:24 +0900687 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
688 if (WARN_ON_ONCE(!list_empty(&op->list)))
689 return;
690
691 list_add(&op->list, &optimizing_list);
692 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900693}
694
695/* Short cut to direct unoptimizing */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900696static void force_unoptimize_kprobe(struct optimized_kprobe *op)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900697{
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200698 lockdep_assert_cpus_held();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900699 arch_unoptimize_kprobe(op);
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900700 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500701}
702
703/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900704static void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500705{
706 struct optimized_kprobe *op;
707
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900708 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
709 return; /* This is not an optprobe nor optimized */
710
711 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900712 if (!kprobe_optimized(p))
713 return;
714
715 if (!list_empty(&op->list)) {
716 if (optprobe_queued_unopt(op)) {
717 /* Queued in unoptimizing queue */
718 if (force) {
719 /*
720 * Forcibly unoptimize the kprobe here, and queue it
721 * in the freeing list for release afterwards.
722 */
723 force_unoptimize_kprobe(op);
724 list_move(&op->list, &freeing_list);
725 }
726 } else {
727 /* Dequeue from the optimizing queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500728 list_del_init(&op->list);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900729 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900730 }
731 return;
732 }
733
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900734 /* Optimized kprobe case */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900735 if (force) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900736 /* Forcibly update the code: this is a special case */
737 force_unoptimize_kprobe(op);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900738 } else {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900739 list_add(&op->list, &unoptimizing_list);
740 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500741 }
742}
743
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900744/* Cancel unoptimizing for reusing */
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900745static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900746{
747 struct optimized_kprobe *op;
748
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900749 /*
750 * Unused kprobe MUST be on the way of delayed unoptimizing (means
751 * there is still a relative jump) and disabled.
752 */
753 op = container_of(ap, struct optimized_kprobe, kp);
Masami Hiramatsu44585152018-04-28 21:36:33 +0900754 WARN_ON_ONCE(list_empty(&op->list));
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900755 /* Enable the probe again */
756 ap->flags &= ~KPROBE_FLAG_DISABLED;
757 /* Optimize it again (remove from op->list) */
Masami Hiramatsu5f843ed2019-04-15 15:01:25 +0900758 if (!kprobe_optready(ap))
759 return -EINVAL;
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900760
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900761 optimize_kprobe(ap);
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900762 return 0;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900763}
764
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500765/* Remove optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900766static void kill_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500767{
768 struct optimized_kprobe *op;
769
770 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900771 if (!list_empty(&op->list))
772 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500773 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900774 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900775
776 if (kprobe_unused(p)) {
777 /* Enqueue if it is unused */
778 list_add(&op->list, &freeing_list);
779 /*
780 * Remove unused probes from the hash list. After waiting
781 * for synchronization, this probe is reclaimed.
782 * (reclaiming is done by do_free_cleaned_kprobes().)
783 */
784 hlist_del_rcu(&op->kp.hlist);
785 }
786
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900787 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500788 arch_remove_optimized_kprobe(op);
789}
790
Masami Hiramatsua4602462017-04-19 18:22:25 +0530791static inline
792void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
793{
794 if (!kprobe_ftrace(p))
795 arch_prepare_optimized_kprobe(op, p);
796}
797
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500798/* Try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900799static void prepare_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500800{
801 struct optimized_kprobe *op;
802
803 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsua4602462017-04-19 18:22:25 +0530804 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500805}
806
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500807/* Allocate new optimized_kprobe and try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900808static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500809{
810 struct optimized_kprobe *op;
811
812 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
813 if (!op)
814 return NULL;
815
816 INIT_LIST_HEAD(&op->list);
817 op->kp.addr = p->addr;
Masami Hiramatsua4602462017-04-19 18:22:25 +0530818 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500819
820 return &op->kp;
821}
822
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900823static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500824
825/*
826 * Prepare an optimized_kprobe and optimize it
827 * NOTE: p must be a normal registered kprobe
828 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900829static void try_to_optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500830{
831 struct kprobe *ap;
832 struct optimized_kprobe *op;
833
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900834 /* Impossible to optimize ftrace-based kprobe */
835 if (kprobe_ftrace(p))
836 return;
837
Masami Hiramatsu25764282012-06-05 19:28:26 +0900838 /* For preparing optimization, jump_label_text_reserved() is called */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200839 cpus_read_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +0900840 jump_label_lock();
841 mutex_lock(&text_mutex);
842
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500843 ap = alloc_aggr_kprobe(p);
844 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900845 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500846
847 op = container_of(ap, struct optimized_kprobe, kp);
848 if (!arch_prepared_optinsn(&op->optinsn)) {
849 /* If failed to setup optimizing, fallback to kprobe */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900850 arch_remove_optimized_kprobe(op);
851 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900852 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500853 }
854
855 init_aggr_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900856 optimize_kprobe(ap); /* This just kicks optimizer thread */
857
858out:
859 mutex_unlock(&text_mutex);
860 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200861 cpus_read_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500862}
863
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500864#ifdef CONFIG_SYSCTL
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900865static void optimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500866{
867 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500868 struct kprobe *p;
869 unsigned int i;
870
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900871 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500872 /* If optimization is already allowed, just return */
873 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900874 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500875
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200876 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500877 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500878 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
879 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900880 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500881 if (!kprobe_disabled(p))
882 optimize_kprobe(p);
883 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200884 cpus_read_unlock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500885 printk(KERN_INFO "Kprobes globally optimized\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900886out:
887 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500888}
889
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900890static void unoptimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500891{
892 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500893 struct kprobe *p;
894 unsigned int i;
895
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900896 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500897 /* If optimization is already prohibited, just return */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900898 if (!kprobes_allow_optimization) {
899 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500900 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900901 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500902
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200903 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500904 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500905 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
906 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900907 hlist_for_each_entry(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500908 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900909 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500910 }
911 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200912 cpus_read_unlock();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900913 mutex_unlock(&kprobe_mutex);
914
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900915 /* Wait for unoptimizing completion */
916 wait_for_kprobe_optimizer();
917 printk(KERN_INFO "Kprobes globally unoptimized\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500918}
919
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900920static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500921int sysctl_kprobes_optimization;
922int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200923 void *buffer, size_t *length,
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500924 loff_t *ppos)
925{
926 int ret;
927
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900928 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500929 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
930 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
931
932 if (sysctl_kprobes_optimization)
933 optimize_all_kprobes();
934 else
935 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900936 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500937
938 return ret;
939}
940#endif /* CONFIG_SYSCTL */
941
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900942/* Put a breakpoint for a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900943static void __arm_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500944{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900945 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500946
947 /* Check collision with other optimized kprobes */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900948 _p = get_optimized_kprobe((unsigned long)p->addr);
949 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900950 /* Fallback to unoptimized kprobe */
951 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500952
953 arch_arm_kprobe(p);
954 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
955}
956
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900957/* Remove the breakpoint of a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900958static void __disarm_kprobe(struct kprobe *p, bool reopt)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500959{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900960 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500961
Wang Nan69d54b92015-02-13 14:40:26 -0800962 /* Try to unoptimize */
963 unoptimize_kprobe(p, kprobes_all_disarmed);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500964
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900965 if (!kprobe_queued(p)) {
966 arch_disarm_kprobe(p);
967 /* If another kprobe was blocked, optimize it. */
968 _p = get_optimized_kprobe((unsigned long)p->addr);
969 if (unlikely(_p) && reopt)
970 optimize_kprobe(_p);
971 }
972 /* TODO: reoptimize others after unoptimized this probe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500973}
974
975#else /* !CONFIG_OPTPROBES */
976
977#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900978#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500979#define kill_optimized_kprobe(p) do {} while (0)
980#define prepare_optimized_kprobe(p) do {} while (0)
981#define try_to_optimize_kprobe(p) do {} while (0)
982#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900983#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
984#define kprobe_disarmed(p) kprobe_disabled(p)
985#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500986
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900987static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900988{
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900989 /*
990 * If the optimized kprobe is NOT supported, the aggr kprobe is
991 * released at the same time that the last aggregated kprobe is
992 * unregistered.
993 * Thus there should be no chance to reuse unused kprobe.
994 */
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900995 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900996 return -EINVAL;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900997}
998
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900999static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001000{
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001001 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001002 kfree(p);
1003}
1004
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001005static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001006{
1007 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1008}
1009#endif /* CONFIG_OPTPROBES */
1010
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001011#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001012static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +09001013 .func = kprobe_ftrace_handler,
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001014 .flags = FTRACE_OPS_FL_SAVE_REGS,
1015};
1016
1017static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1018 .func = kprobe_ftrace_handler,
Masami Hiramatsu1d70be32014-11-21 05:25:23 -05001019 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001020};
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001021
1022static int kprobe_ipmodify_enabled;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001023static int kprobe_ftrace_enabled;
1024
1025/* Must ensure p->addr is really on ftrace */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001026static int prepare_kprobe(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001027{
1028 if (!kprobe_ftrace(p))
1029 return arch_prepare_kprobe(p);
1030
1031 return arch_prepare_kprobe_ftrace(p);
1032}
1033
1034/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001035static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1036 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001037{
Jessica Yu12310e342018-01-10 00:51:23 +01001038 int ret = 0;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001039
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001040 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001041 if (ret) {
Masami Hiramatsu44585152018-04-28 21:36:33 +09001042 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1043 p->addr, ret);
Jessica Yu12310e342018-01-10 00:51:23 +01001044 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001045 }
Jessica Yu12310e342018-01-10 00:51:23 +01001046
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001047 if (*cnt == 0) {
1048 ret = register_ftrace_function(ops);
Jessica Yu12310e342018-01-10 00:51:23 +01001049 if (ret) {
1050 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1051 goto err_ftrace;
1052 }
1053 }
1054
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001055 (*cnt)++;
Jessica Yu12310e342018-01-10 00:51:23 +01001056 return ret;
1057
1058err_ftrace:
1059 /*
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001060 * At this point, sinec ops is not registered, we should be sefe from
1061 * registering empty filter.
Jessica Yu12310e342018-01-10 00:51:23 +01001062 */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001063 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001064 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001065}
1066
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001067static int arm_kprobe_ftrace(struct kprobe *p)
1068{
1069 bool ipmodify = (p->post_handler != NULL);
1070
1071 return __arm_kprobe_ftrace(p,
1072 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1073 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1074}
1075
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001076/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001077static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1078 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001079{
Jessica Yu297f9232018-01-10 00:51:24 +01001080 int ret = 0;
1081
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001082 if (*cnt == 1) {
1083 ret = unregister_ftrace_function(ops);
Jessica Yu297f9232018-01-10 00:51:24 +01001084 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1085 return ret;
1086 }
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001087
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001088 (*cnt)--;
Jessica Yu297f9232018-01-10 00:51:24 +01001089
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001090 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Masami Hiramatsu44585152018-04-28 21:36:33 +09001091 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1092 p->addr, ret);
Jessica Yu297f9232018-01-10 00:51:24 +01001093 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001094}
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001095
1096static int disarm_kprobe_ftrace(struct kprobe *p)
1097{
1098 bool ipmodify = (p->post_handler != NULL);
1099
1100 return __disarm_kprobe_ftrace(p,
1101 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1102 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1103}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001104#else /* !CONFIG_KPROBES_ON_FTRACE */
Muchun Song10de7952020-08-06 01:20:46 +08001105static inline int prepare_kprobe(struct kprobe *p)
1106{
1107 return arch_prepare_kprobe(p);
1108}
1109
1110static inline int arm_kprobe_ftrace(struct kprobe *p)
1111{
1112 return -ENODEV;
1113}
1114
1115static inline int disarm_kprobe_ftrace(struct kprobe *p)
1116{
1117 return -ENODEV;
1118}
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001119#endif
1120
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001121/* Arm a kprobe with text_mutex */
Jessica Yu12310e342018-01-10 00:51:23 +01001122static int arm_kprobe(struct kprobe *kp)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001123{
Jessica Yu12310e342018-01-10 00:51:23 +01001124 if (unlikely(kprobe_ftrace(kp)))
1125 return arm_kprobe_ftrace(kp);
1126
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001127 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001128 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001129 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001130 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001131 cpus_read_unlock();
Jessica Yu12310e342018-01-10 00:51:23 +01001132
1133 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001134}
1135
1136/* Disarm a kprobe with text_mutex */
Jessica Yu297f9232018-01-10 00:51:24 +01001137static int disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001138{
Jessica Yu297f9232018-01-10 00:51:24 +01001139 if (unlikely(kprobe_ftrace(kp)))
1140 return disarm_kprobe_ftrace(kp);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001141
1142 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001143 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001144 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001145 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001146 cpus_read_unlock();
Jessica Yu297f9232018-01-10 00:51:24 +01001147
1148 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001149}
1150
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001151/*
1152 * Aggregate handlers for multiple kprobes support - these handlers
1153 * take care of invoking the individual kprobe handlers on p->list
1154 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001155static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001156{
1157 struct kprobe *kp;
1158
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001159 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001160 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001161 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001162 if (kp->pre_handler(kp, regs))
1163 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001164 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001165 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001166 }
1167 return 0;
1168}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001169NOKPROBE_SYMBOL(aggr_pre_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001170
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001171static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1172 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001173{
1174 struct kprobe *kp;
1175
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001176 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001177 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001178 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001179 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001180 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001181 }
1182 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001183}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001184NOKPROBE_SYMBOL(aggr_post_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001185
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001186static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1187 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001188{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001189 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001190
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001191 /*
1192 * if we faulted "during" the execution of a user specified
1193 * probe handler, invoke just that probe's fault handler
1194 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001195 if (cur && cur->fault_handler) {
1196 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001197 return 1;
1198 }
1199 return 0;
1200}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001201NOKPROBE_SYMBOL(aggr_fault_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001202
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001203/* Walks the list and increments nmissed count for multiprobe case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001204void kprobes_inc_nmissed_count(struct kprobe *p)
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001205{
1206 struct kprobe *kp;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001207 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001208 p->nmissed++;
1209 } else {
1210 list_for_each_entry_rcu(kp, &p->list, list)
1211 kp->nmissed++;
1212 }
1213 return;
1214}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001215NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001216
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001217static void free_rp_inst_rcu(struct rcu_head *head)
1218{
1219 struct kretprobe_instance *ri = container_of(head, struct kretprobe_instance, rcu);
1220
1221 if (refcount_dec_and_test(&ri->rph->ref))
1222 kfree(ri->rph);
1223 kfree(ri);
1224}
1225NOKPROBE_SYMBOL(free_rp_inst_rcu);
1226
Masami Hiramatsub3388172020-08-29 22:02:47 +09001227static void recycle_rp_inst(struct kretprobe_instance *ri)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001228{
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001229 struct kretprobe *rp = get_kretprobe(ri);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001230
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001231 if (likely(rp)) {
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001232 freelist_add(&ri->freelist, &rp->freelist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001233 } else
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001234 call_rcu(&ri->rcu, free_rp_inst_rcu);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001235}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001236NOKPROBE_SYMBOL(recycle_rp_inst);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001237
Masami Hiramatsu319f0ce2020-08-29 22:03:02 +09001238static struct kprobe kprobe_busy = {
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001239 .addr = (void *) get_kprobe,
1240};
1241
1242void kprobe_busy_begin(void)
1243{
1244 struct kprobe_ctlblk *kcb;
1245
1246 preempt_disable();
1247 __this_cpu_write(current_kprobe, &kprobe_busy);
1248 kcb = get_kprobe_ctlblk();
1249 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1250}
1251
1252void kprobe_busy_end(void)
1253{
1254 __this_cpu_write(current_kprobe, NULL);
1255 preempt_enable();
1256}
1257
Hien Nguyenb94cce92005-06-23 00:09:19 -07001258/*
bibo maoc6fd91f2006-03-26 01:38:20 -08001259 * This function is called from finish_task_switch when task tk becomes dead,
1260 * so that we can recycle any function-return probe instances associated
1261 * with this task. These left over instances represent probed functions
1262 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001263 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001264void kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001265{
bibo,mao62c27be2006-10-02 02:17:33 -07001266 struct kretprobe_instance *ri;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001267 struct llist_node *node;
Rusty Lynch802eae72005-06-27 15:17:08 -07001268
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001269 /* Early boot, not yet initialized. */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001270 if (unlikely(!kprobes_initialized))
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001271 return;
1272
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001273 kprobe_busy_begin();
1274
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001275 node = __llist_del_all(&tk->kretprobe_instances);
1276 while (node) {
1277 ri = container_of(node, struct kretprobe_instance, llist);
1278 node = node->next;
1279
1280 recycle_rp_inst(ri);
bibo,mao62c27be2006-10-02 02:17:33 -07001281 }
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001282
1283 kprobe_busy_end();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001284}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001285NOKPROBE_SYMBOL(kprobe_flush_task);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001286
Hien Nguyenb94cce92005-06-23 00:09:19 -07001287static inline void free_rp_inst(struct kretprobe *rp)
1288{
1289 struct kretprobe_instance *ri;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001290 struct freelist_node *node;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001291 int count = 0;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001292
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001293 node = rp->freelist.head;
1294 while (node) {
1295 ri = container_of(node, struct kretprobe_instance, freelist);
1296 node = node->next;
1297
Hien Nguyenb94cce92005-06-23 00:09:19 -07001298 kfree(ri);
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001299 count++;
1300 }
1301
1302 if (refcount_sub_and_test(count, &rp->rph->ref)) {
1303 kfree(rp->rph);
1304 rp->rph = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001305 }
1306}
1307
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001308/* Add the new probe to ap->list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001309static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001310{
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001311 if (p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001312 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001313
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001314 list_add_rcu(&p->list, &ap->list);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001315 if (p->post_handler && !ap->post_handler)
1316 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001317
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001318 return 0;
1319}
1320
1321/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001322 * Fill in the required fields of the "manager kprobe". Replace the
1323 * earlier kprobe in the hlist with the manager kprobe
1324 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001325static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001326{
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001327 /* Copy p's insn slot to ap */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001328 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001329 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001330 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001331 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001332 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001333 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001334 /* We don't care the kprobe which has gone. */
1335 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001336 ap->post_handler = aggr_post_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001337
1338 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001339 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001340
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001341 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001342 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001343}
1344
1345/*
1346 * This is the second or subsequent kprobe at the address - handle
1347 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001348 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001349static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001350{
1351 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001352 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001353
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001354 cpus_read_lock();
1355
Masami Hiramatsu25764282012-06-05 19:28:26 +09001356 /* For preparing optimization, jump_label_text_reserved() is called */
1357 jump_label_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001358 mutex_lock(&text_mutex);
1359
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001360 if (!kprobe_aggrprobe(orig_p)) {
1361 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1362 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001363 if (!ap) {
1364 ret = -ENOMEM;
1365 goto out;
1366 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001367 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001368 } else if (kprobe_unused(ap)) {
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001369 /* This probe is going to die. Rescue it */
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001370 ret = reuse_unused_kprobe(ap);
1371 if (ret)
1372 goto out;
1373 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001374
1375 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001376 /*
1377 * Attempting to insert new probe at the same location that
1378 * had a probe in the module vaddr area which already
1379 * freed. So, the instruction slot has already been
1380 * released. We need a new slot for the new probe.
1381 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001382 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001383 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001384 /*
1385 * Even if fail to allocate new slot, don't need to
1386 * free aggr_probe. It will be used next time, or
1387 * freed by unregister_kprobe.
1388 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001389 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001390
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001391 /* Prepare optimized instructions if possible. */
1392 prepare_optimized_kprobe(ap);
1393
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001394 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001395 * Clear gone flag to prevent allocating new slot again, and
1396 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001397 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001398 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1399 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001400 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001401
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001402 /* Copy ap's insn slot to p */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001403 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001404 ret = add_new_kprobe(ap, p);
1405
1406out:
1407 mutex_unlock(&text_mutex);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001408 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001409 cpus_read_unlock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001410
1411 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1412 ap->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01001413 if (!kprobes_all_disarmed) {
Masami Hiramatsu25764282012-06-05 19:28:26 +09001414 /* Arm the breakpoint again. */
Jessica Yu12310e342018-01-10 00:51:23 +01001415 ret = arm_kprobe(ap);
1416 if (ret) {
1417 ap->flags |= KPROBE_FLAG_DISABLED;
1418 list_del_rcu(&p->list);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001419 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001420 }
1421 }
Masami Hiramatsu25764282012-06-05 19:28:26 +09001422 }
1423 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001424}
1425
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001426bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1427{
1428 /* The __kprobes marked functions and entry code must not be probed */
1429 return addr >= (unsigned long)__kprobes_text_start &&
1430 addr < (unsigned long)__kprobes_text_end;
1431}
1432
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001433static bool __within_kprobe_blacklist(unsigned long addr)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001434{
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001435 struct kprobe_blacklist_entry *ent;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001436
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001437 if (arch_within_kprobe_blacklist(addr))
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001438 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001439 /*
1440 * If there exists a kprobe_blacklist, verify and
1441 * fail any probe registration in the prohibited area
1442 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001443 list_for_each_entry(ent, &kprobe_blacklist, list) {
1444 if (addr >= ent->start_addr && addr < ent->end_addr)
1445 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001446 }
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001447 return false;
1448}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001449
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001450bool within_kprobe_blacklist(unsigned long addr)
1451{
1452 char symname[KSYM_NAME_LEN], *p;
1453
1454 if (__within_kprobe_blacklist(addr))
1455 return true;
1456
1457 /* Check if the address is on a suffixed-symbol */
1458 if (!lookup_symbol_name(addr, symname)) {
1459 p = strchr(symname, '.');
1460 if (!p)
1461 return false;
1462 *p = '\0';
1463 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1464 if (addr)
1465 return __within_kprobe_blacklist(addr);
1466 }
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001467 return false;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001468}
1469
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001470/*
1471 * If we have a symbol_name argument, look it up and add the offset field
1472 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001473 * This returns encoded errors if it fails to look up symbol or invalid
1474 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001475 */
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301476static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1477 const char *symbol_name, unsigned int offset)
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001478{
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301479 if ((symbol_name && addr) || (!symbol_name && !addr))
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001480 goto invalid;
1481
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301482 if (symbol_name) {
Linus Torvalds7246f602017-05-05 11:36:44 -07001483 addr = kprobe_lookup_name(symbol_name, offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001484 if (!addr)
1485 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001486 }
1487
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301488 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001489 if (addr)
1490 return addr;
1491
1492invalid:
1493 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001494}
1495
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301496static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1497{
1498 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1499}
1500
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301501/* Check passed kprobe is valid and return kprobe in kprobe_table. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001502static struct kprobe *__get_valid_kprobe(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301503{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001504 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301505
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001506 lockdep_assert_held(&kprobe_mutex);
1507
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001508 ap = get_kprobe(p->addr);
1509 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301510 return NULL;
1511
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001512 if (p != ap) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001513 list_for_each_entry(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301514 if (list_p == p)
1515 /* kprobe p is a valid probe */
1516 goto valid;
1517 return NULL;
1518 }
1519valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001520 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301521}
1522
1523/* Return error if the kprobe is being re-registered */
1524static inline int check_kprobe_rereg(struct kprobe *p)
1525{
1526 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301527
1528 mutex_lock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001529 if (__get_valid_kprobe(p))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301530 ret = -EINVAL;
1531 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001532
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301533 return ret;
1534}
1535
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001536int __weak arch_check_ftrace_location(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001538 unsigned long ftrace_addr;
1539
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001540 ftrace_addr = ftrace_location((unsigned long)p->addr);
1541 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001542#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001543 /* Given address is not on the instruction boundary */
1544 if ((unsigned long)p->addr != ftrace_addr)
1545 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001546 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001547#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001548 return -EINVAL;
1549#endif
1550 }
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001551 return 0;
1552}
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001553
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001554static int check_kprobe_address_safe(struct kprobe *p,
1555 struct module **probed_mod)
1556{
1557 int ret;
1558
1559 ret = arch_check_ftrace_location(p);
1560 if (ret)
1561 return ret;
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001562 jump_label_lock();
1563 preempt_disable();
1564
1565 /* Ensure it is not in reserved area nor out of text */
1566 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001567 within_kprobe_blacklist((unsigned long) p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001568 jump_label_text_reserved(p->addr, p->addr) ||
1569 find_bug((unsigned long)p->addr)) {
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001570 ret = -EINVAL;
1571 goto out;
1572 }
1573
1574 /* Check if are we probing a module */
1575 *probed_mod = __module_text_address((unsigned long) p->addr);
1576 if (*probed_mod) {
1577 /*
1578 * We must hold a refcount of the probed module while updating
1579 * its code to prohibit unexpected unloading.
1580 */
1581 if (unlikely(!try_module_get(*probed_mod))) {
1582 ret = -ENOENT;
1583 goto out;
1584 }
1585
1586 /*
1587 * If the module freed .init.text, we couldn't insert
1588 * kprobes in there.
1589 */
1590 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1591 (*probed_mod)->state != MODULE_STATE_COMING) {
1592 module_put(*probed_mod);
1593 *probed_mod = NULL;
1594 ret = -ENOENT;
1595 }
1596 }
1597out:
1598 preempt_enable();
1599 jump_label_unlock();
1600
1601 return ret;
1602}
1603
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001604int register_kprobe(struct kprobe *p)
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001605{
1606 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001607 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001608 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001609 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001611 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001612 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001613 if (IS_ERR(addr))
1614 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001615 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001616
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301617 ret = check_kprobe_rereg(p);
1618 if (ret)
1619 return ret;
1620
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001621 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1622 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001623 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001624 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001625
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001626 ret = check_kprobe_address_safe(p, &probed_mod);
1627 if (ret)
1628 return ret;
1629
1630 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001631
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001632 old_p = get_kprobe(p->addr);
1633 if (old_p) {
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001634 /* Since this may unoptimize old_p, locking text_mutex. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001635 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto out;
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001639 cpus_read_lock();
1640 /* Prevent text modification */
1641 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001642 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001643 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001644 cpus_read_unlock();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001645 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001646 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001647
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001648 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001649 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1651
Jessica Yu12310e342018-01-10 00:51:23 +01001652 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1653 ret = arm_kprobe(p);
1654 if (ret) {
1655 hlist_del_rcu(&p->hlist);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001656 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001657 goto out;
1658 }
1659 }
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001660
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001661 /* Try to optimize kprobe */
1662 try_to_optimize_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001664 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001665
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001666 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001667 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return ret;
1670}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001671EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001673/* Check if all probes on the aggrprobe are disabled */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001674static int aggr_kprobe_disabled(struct kprobe *ap)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001675{
1676 struct kprobe *kp;
1677
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001678 lockdep_assert_held(&kprobe_mutex);
1679
1680 list_for_each_entry(kp, &ap->list, list)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001681 if (!kprobe_disabled(kp))
1682 /*
1683 * There is an active probe on the list.
1684 * We can't disable this ap.
1685 */
1686 return 0;
1687
1688 return 1;
1689}
1690
1691/* Disable one kprobe: Make sure called under kprobe_mutex is locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001692static struct kprobe *__disable_kprobe(struct kprobe *p)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001693{
1694 struct kprobe *orig_p;
Jessica Yu297f9232018-01-10 00:51:24 +01001695 int ret;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001696
1697 /* Get an original kprobe for return */
1698 orig_p = __get_valid_kprobe(p);
1699 if (unlikely(orig_p == NULL))
Jessica Yu297f9232018-01-10 00:51:24 +01001700 return ERR_PTR(-EINVAL);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001701
1702 if (!kprobe_disabled(p)) {
1703 /* Disable probe if it is a child probe */
1704 if (p != orig_p)
1705 p->flags |= KPROBE_FLAG_DISABLED;
1706
1707 /* Try to disarm and disable this/parent probe */
1708 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Wang Nan69d54b92015-02-13 14:40:26 -08001709 /*
1710 * If kprobes_all_disarmed is set, orig_p
1711 * should have already been disarmed, so
1712 * skip unneed disarming process.
1713 */
Jessica Yu297f9232018-01-10 00:51:24 +01001714 if (!kprobes_all_disarmed) {
1715 ret = disarm_kprobe(orig_p, true);
1716 if (ret) {
1717 p->flags &= ~KPROBE_FLAG_DISABLED;
1718 return ERR_PTR(ret);
1719 }
1720 }
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001721 orig_p->flags |= KPROBE_FLAG_DISABLED;
1722 }
1723 }
1724
1725 return orig_p;
1726}
1727
Masami Hiramatsu98616682008-04-28 02:14:28 -07001728/*
1729 * Unregister a kprobe without a scheduler synchronization.
1730 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001731static int __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001732{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001733 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001734
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001735 /* Disable kprobe. This will disarm it if needed. */
1736 ap = __disable_kprobe(p);
Jessica Yu297f9232018-01-10 00:51:24 +01001737 if (IS_ERR(ap))
1738 return PTR_ERR(ap);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001739
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001740 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001741 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001742 * This probe is an independent(and non-optimized) kprobe
1743 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001744 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001745 goto disarmed;
1746
1747 /* Following process expects this probe is an aggrprobe */
1748 WARN_ON(!kprobe_aggrprobe(ap));
1749
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001750 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1751 /*
1752 * !disarmed could be happen if the probe is under delayed
1753 * unoptimizing.
1754 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001755 goto disarmed;
1756 else {
1757 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001758 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001759 list_for_each_entry(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001760 if ((list_p != p) && (list_p->post_handler))
1761 goto noclean;
1762 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001763 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001764 }
1765noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001766 /*
1767 * Remove from the aggrprobe: this path will do nothing in
1768 * __unregister_kprobe_bottom().
1769 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001770 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001771 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1772 /*
1773 * Try to optimize this probe again, because post
1774 * handler may have been changed.
1775 */
1776 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001777 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001778 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001779
1780disarmed:
1781 hlist_del_rcu(&ap->hlist);
1782 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001783}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001784
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001785static void __unregister_kprobe_bottom(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001786{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001787 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001788
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001789 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001790 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001791 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001792 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001793 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001794 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001795 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001796 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001797 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001798 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001801int register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001802{
1803 int i, ret = 0;
1804
1805 if (num <= 0)
1806 return -EINVAL;
1807 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001808 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001809 if (ret < 0) {
1810 if (i > 0)
1811 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001812 break;
1813 }
1814 }
1815 return ret;
1816}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001817EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001818
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001819void unregister_kprobe(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001820{
1821 unregister_kprobes(&p, 1);
1822}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001823EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001824
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001825void unregister_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001826{
1827 int i;
1828
1829 if (num <= 0)
1830 return;
1831 mutex_lock(&kprobe_mutex);
1832 for (i = 0; i < num; i++)
1833 if (__unregister_kprobe_top(kps[i]) < 0)
1834 kps[i]->addr = NULL;
1835 mutex_unlock(&kprobe_mutex);
1836
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001837 synchronize_rcu();
Masami Hiramatsu98616682008-04-28 02:14:28 -07001838 for (i = 0; i < num; i++)
1839 if (kps[i]->addr)
1840 __unregister_kprobe_bottom(kps[i]);
1841}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001842EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001843
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301844int __weak kprobe_exceptions_notify(struct notifier_block *self,
1845 unsigned long val, void *data)
Naveen N. Raofc62d022017-02-08 01:24:14 +05301846{
1847 return NOTIFY_DONE;
1848}
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301849NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Naveen N. Raofc62d022017-02-08 01:24:14 +05301850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851static struct notifier_block kprobe_exceptions_nb = {
1852 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001853 .priority = 0x7fffffff /* we need to be notified first */
1854};
1855
Michael Ellerman3d7e3382007-07-19 01:48:11 -07001856unsigned long __weak arch_deref_entry_point(void *entry)
1857{
1858 return (unsigned long)entry;
1859}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001861#ifdef CONFIG_KRETPROBES
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001862
1863unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
1864 void *trampoline_address,
1865 void *frame_pointer)
1866{
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001867 kprobe_opcode_t *correct_ret_addr = NULL;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001868 struct kretprobe_instance *ri = NULL;
1869 struct llist_node *first, *node;
1870 struct kretprobe *rp;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001871
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001872 /* Find all nodes for this frame. */
1873 first = node = current->kretprobe_instances.first;
1874 while (node) {
1875 ri = container_of(node, struct kretprobe_instance, llist);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001876
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001877 BUG_ON(ri->fp != frame_pointer);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001878
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001879 if (ri->ret_addr != trampoline_address) {
1880 correct_ret_addr = ri->ret_addr;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001881 /*
1882 * This is the real return address. Any other
1883 * instances associated with this task are for
1884 * other calls deeper on the call stack
1885 */
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001886 goto found;
1887 }
1888
1889 node = node->next;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001890 }
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001891 pr_err("Oops! Kretprobe fails to find correct return address.\n");
1892 BUG_ON(1);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001893
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001894found:
1895 /* Unlink all nodes for this frame. */
1896 current->kretprobe_instances.first = node->next;
1897 node->next = NULL;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001898
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001899 /* Run them.. */
1900 while (first) {
1901 ri = container_of(first, struct kretprobe_instance, llist);
1902 first = first->next;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001903
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001904 rp = get_kretprobe(ri);
1905 if (rp && rp->handler) {
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001906 struct kprobe *prev = kprobe_running();
1907
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001908 __this_cpu_write(current_kprobe, &rp->kp);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001909 ri->ret_addr = correct_ret_addr;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001910 rp->handler(ri, regs);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001911 __this_cpu_write(current_kprobe, prev);
1912 }
1913
Masami Hiramatsub3388172020-08-29 22:02:47 +09001914 recycle_rp_inst(ri);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001915 }
1916
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001917 return (unsigned long)correct_ret_addr;
1918}
1919NOKPROBE_SYMBOL(__kretprobe_trampoline_handler)
1920
Adrian Bunke65cefe2006-02-03 03:03:42 -08001921/*
1922 * This kprobe pre_handler is registered with every kretprobe. When probe
1923 * hits it will set up the return probe.
1924 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001925static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Adrian Bunke65cefe2006-02-03 03:03:42 -08001926{
1927 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001928 struct kretprobe_instance *ri;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001929 struct freelist_node *fn;
Adrian Bunke65cefe2006-02-03 03:03:42 -08001930
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001931 fn = freelist_try_get(&rp->freelist);
1932 if (!fn) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001933 rp->nmissed++;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001934 return 0;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001935 }
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001936
1937 ri = container_of(fn, struct kretprobe_instance, freelist);
1938
1939 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
1940 freelist_add(&ri->freelist, &rp->freelist);
1941 return 0;
1942 }
1943
1944 arch_prepare_kretprobe(ri, regs);
1945
1946 __llist_add(&ri->llist, &current->kretprobe_instances);
1947
Adrian Bunke65cefe2006-02-03 03:03:42 -08001948 return 0;
1949}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001950NOKPROBE_SYMBOL(pre_handler_kretprobe);
Adrian Bunke65cefe2006-02-03 03:03:42 -08001951
Naveen N. Rao659b9572017-07-07 22:37:24 +05301952bool __weak arch_kprobe_on_func_entry(unsigned long offset)
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301953{
1954 return !offset;
1955}
1956
Naveen N. Rao659b9572017-07-07 22:37:24 +05301957bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301958{
1959 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
1960
1961 if (IS_ERR(kp_addr))
1962 return false;
1963
1964 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) ||
Naveen N. Rao659b9572017-07-07 22:37:24 +05301965 !arch_kprobe_on_func_entry(offset))
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301966 return false;
1967
1968 return true;
1969}
1970
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001971int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001972{
1973 int ret = 0;
1974 struct kretprobe_instance *inst;
1975 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001976 void *addr;
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301977
Naveen N. Rao659b9572017-07-07 22:37:24 +05301978 if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset))
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301979 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001980
1981 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001982 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001983 if (IS_ERR(addr))
1984 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001985
1986 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1987 if (kretprobe_blacklist[i].addr == addr)
1988 return -EINVAL;
1989 }
1990 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001991
1992 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07001993 rp->kp.post_handler = NULL;
1994 rp->kp.fault_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001995
1996 /* Pre-allocate memory for max kretprobe instances */
1997 if (rp->maxactive <= 0) {
Thomas Gleixner92616602019-07-26 23:19:41 +02001998#ifdef CONFIG_PREEMPTION
Heiko Carstensc2ef6662009-12-21 13:02:24 +01001999 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07002000#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05302001 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07002002#endif
2003 }
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002004 rp->freelist.head = NULL;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002005 rp->rph = kzalloc(sizeof(struct kretprobe_holder), GFP_KERNEL);
2006 if (!rp->rph)
2007 return -ENOMEM;
2008
2009 rp->rph->rp = rp;
Hien Nguyenb94cce92005-06-23 00:09:19 -07002010 for (i = 0; i < rp->maxactive; i++) {
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002011 inst = kzalloc(sizeof(struct kretprobe_instance) +
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08002012 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002013 if (inst == NULL) {
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002014 refcount_set(&rp->rph->ref, i);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002015 free_rp_inst(rp);
2016 return -ENOMEM;
2017 }
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002018 inst->rph = rp->rph;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002019 freelist_add(&inst->freelist, &rp->freelist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002020 }
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002021 refcount_set(&rp->rph->ref, i);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002022
2023 rp->nmissed = 0;
2024 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002025 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002026 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002027 free_rp_inst(rp);
2028 return ret;
2029}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002030EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002031
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002032int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002033{
2034 int ret = 0, i;
2035
2036 if (num <= 0)
2037 return -EINVAL;
2038 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002039 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07002040 if (ret < 0) {
2041 if (i > 0)
2042 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002043 break;
2044 }
2045 }
2046 return ret;
2047}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002048EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002049
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002050void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002051{
2052 unregister_kretprobes(&rp, 1);
2053}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002054EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002055
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002056void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002057{
2058 int i;
2059
2060 if (num <= 0)
2061 return;
2062 mutex_lock(&kprobe_mutex);
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002063 for (i = 0; i < num; i++) {
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002064 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2065 rps[i]->kp.addr = NULL;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002066 rps[i]->rph->rp = NULL;
2067 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002068 mutex_unlock(&kprobe_mutex);
2069
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08002070 synchronize_rcu();
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002071 for (i = 0; i < num; i++) {
2072 if (rps[i]->kp.addr) {
2073 __unregister_kprobe_bottom(&rps[i]->kp);
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002074 free_rp_inst(rps[i]);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002075 }
2076 }
2077}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002078EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002079
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002080#else /* CONFIG_KRETPROBES */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002081int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002082{
2083 return -ENOSYS;
2084}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002085EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002086
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002087int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002088{
2089 return -ENOSYS;
2090}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002091EXPORT_SYMBOL_GPL(register_kretprobes);
2092
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002093void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002094{
2095}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002096EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002097
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002098void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002099{
2100}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002101EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002102
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002103static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002104{
2105 return 0;
2106}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002107NOKPROBE_SYMBOL(pre_handler_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002108
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002109#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002110
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002111/* Set the kprobe gone and remove its instruction buffer. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002112static void kill_kprobe(struct kprobe *p)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002113{
2114 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002115
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002116 lockdep_assert_held(&kprobe_mutex);
2117
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002118 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002119 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002120 /*
2121 * If this is an aggr_kprobe, we have to list all the
2122 * chained probes and mark them GONE.
2123 */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002124 list_for_each_entry(kp, &p->list, list)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002125 kp->flags |= KPROBE_FLAG_GONE;
2126 p->post_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002127 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002128 }
2129 /*
2130 * Here, we can remove insn_slot safely, because no thread calls
2131 * the original probed function (which will be freed soon) any more.
2132 */
2133 arch_remove_kprobe(p);
Muchun Song0cb2f132020-07-28 14:45:36 +08002134
2135 /*
2136 * The module is going away. We should disarm the kprobe which
Masami Hiramatsubcb53202020-09-01 00:12:07 +09002137 * is using ftrace, because ftrace framework is still available at
2138 * MODULE_STATE_GOING notification.
Muchun Song0cb2f132020-07-28 14:45:36 +08002139 */
Masami Hiramatsubcb53202020-09-01 00:12:07 +09002140 if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
Muchun Song0cb2f132020-07-28 14:45:36 +08002141 disarm_kprobe_ftrace(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002142}
2143
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002144/* Disable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002145int disable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002146{
2147 int ret = 0;
Jessica Yu297f9232018-01-10 00:51:24 +01002148 struct kprobe *p;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002149
2150 mutex_lock(&kprobe_mutex);
2151
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09002152 /* Disable this kprobe */
Jessica Yu297f9232018-01-10 00:51:24 +01002153 p = __disable_kprobe(kp);
2154 if (IS_ERR(p))
2155 ret = PTR_ERR(p);
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002156
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002157 mutex_unlock(&kprobe_mutex);
2158 return ret;
2159}
2160EXPORT_SYMBOL_GPL(disable_kprobe);
2161
2162/* Enable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002163int enable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002164{
2165 int ret = 0;
2166 struct kprobe *p;
2167
2168 mutex_lock(&kprobe_mutex);
2169
2170 /* Check whether specified probe is valid. */
2171 p = __get_valid_kprobe(kp);
2172 if (unlikely(p == NULL)) {
2173 ret = -EINVAL;
2174 goto out;
2175 }
2176
2177 if (kprobe_gone(kp)) {
2178 /* This kprobe has gone, we couldn't enable it. */
2179 ret = -EINVAL;
2180 goto out;
2181 }
2182
2183 if (p != kp)
2184 kp->flags &= ~KPROBE_FLAG_DISABLED;
2185
2186 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2187 p->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01002188 ret = arm_kprobe(p);
2189 if (ret)
2190 p->flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002191 }
2192out:
2193 mutex_unlock(&kprobe_mutex);
2194 return ret;
2195}
2196EXPORT_SYMBOL_GPL(enable_kprobe);
2197
Masami Hiramatsu44585152018-04-28 21:36:33 +09002198/* Caller must NOT call this in usual path. This is only for critical case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002199void dump_kprobe(struct kprobe *kp)
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002200{
Masami Hiramatsu44585152018-04-28 21:36:33 +09002201 pr_err("Dumping kprobe:\n");
2202 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2203 kp->symbol_name, kp->offset, kp->addr);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002204}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002205NOKPROBE_SYMBOL(dump_kprobe);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002206
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002207int kprobe_add_ksym_blacklist(unsigned long entry)
2208{
2209 struct kprobe_blacklist_entry *ent;
2210 unsigned long offset = 0, size = 0;
2211
2212 if (!kernel_text_address(entry) ||
2213 !kallsyms_lookup_size_offset(entry, &size, &offset))
2214 return -EINVAL;
2215
2216 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2217 if (!ent)
2218 return -ENOMEM;
2219 ent->start_addr = entry;
2220 ent->end_addr = entry + size;
2221 INIT_LIST_HEAD(&ent->list);
2222 list_add_tail(&ent->list, &kprobe_blacklist);
2223
2224 return (int)size;
2225}
2226
2227/* Add all symbols in given area into kprobe blacklist */
2228int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2229{
2230 unsigned long entry;
2231 int ret = 0;
2232
2233 for (entry = start; entry < end; entry += ret) {
2234 ret = kprobe_add_ksym_blacklist(entry);
2235 if (ret < 0)
2236 return ret;
2237 if (ret == 0) /* In case of alias symbol */
2238 ret = 1;
2239 }
2240 return 0;
2241}
2242
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002243/* Remove all symbols in given area from kprobe blacklist */
2244static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2245{
2246 struct kprobe_blacklist_entry *ent, *n;
2247
2248 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2249 if (ent->start_addr < start || ent->start_addr >= end)
2250 continue;
2251 list_del(&ent->list);
2252 kfree(ent);
2253 }
2254}
2255
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002256static void kprobe_remove_ksym_blacklist(unsigned long entry)
2257{
2258 kprobe_remove_area_blacklist(entry, entry + 1);
2259}
2260
Adrian Hunterd002b8b2020-05-28 11:00:58 +03002261int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2262 char *type, char *sym)
2263{
2264 return -ERANGE;
2265}
2266
2267int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2268 char *sym)
2269{
2270#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2271 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2272 return 0;
2273#ifdef CONFIG_OPTPROBES
2274 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2275 return 0;
2276#endif
2277#endif
2278 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2279 return 0;
2280 return -ERANGE;
2281}
2282
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002283int __init __weak arch_populate_kprobe_blacklist(void)
2284{
2285 return 0;
2286}
2287
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002288/*
2289 * Lookup and populate the kprobe_blacklist.
2290 *
2291 * Unlike the kretprobe blacklist, we'll need to determine
2292 * the range of addresses that belong to the said functions,
2293 * since a kprobe need not necessarily be at the beginning
2294 * of a function.
2295 */
2296static int __init populate_kprobe_blacklist(unsigned long *start,
2297 unsigned long *end)
2298{
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002299 unsigned long entry;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002300 unsigned long *iter;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002301 int ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002302
2303 for (iter = start; iter < end; iter++) {
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002304 entry = arch_deref_entry_point((void *)*iter);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002305 ret = kprobe_add_ksym_blacklist(entry);
2306 if (ret == -EINVAL)
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002307 continue;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002308 if (ret < 0)
2309 return ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002310 }
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002311
2312 /* Symbols in __kprobes_text are blacklisted */
2313 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2314 (unsigned long)__kprobes_text_end);
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002315 if (ret)
2316 return ret;
2317
2318 /* Symbols in noinstr section are blacklisted */
2319 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2320 (unsigned long)__noinstr_text_end);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002321
2322 return ret ? : arch_populate_kprobe_blacklist();
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002323}
2324
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002325static void add_module_kprobe_blacklist(struct module *mod)
2326{
2327 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002328 int i;
2329
2330 if (mod->kprobe_blacklist) {
2331 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2332 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2333 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002334
2335 start = (unsigned long)mod->kprobes_text_start;
2336 if (start) {
2337 end = start + mod->kprobes_text_size;
2338 kprobe_add_area_blacklist(start, end);
2339 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002340
2341 start = (unsigned long)mod->noinstr_text_start;
2342 if (start) {
2343 end = start + mod->noinstr_text_size;
2344 kprobe_add_area_blacklist(start, end);
2345 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002346}
2347
2348static void remove_module_kprobe_blacklist(struct module *mod)
2349{
2350 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002351 int i;
2352
2353 if (mod->kprobe_blacklist) {
2354 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2355 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2356 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002357
2358 start = (unsigned long)mod->kprobes_text_start;
2359 if (start) {
2360 end = start + mod->kprobes_text_size;
2361 kprobe_remove_area_blacklist(start, end);
2362 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002363
2364 start = (unsigned long)mod->noinstr_text_start;
2365 if (start) {
2366 end = start + mod->noinstr_text_size;
2367 kprobe_remove_area_blacklist(start, end);
2368 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002369}
2370
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002371/* Module notifier call back, checking kprobes on the module */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002372static int kprobes_module_callback(struct notifier_block *nb,
2373 unsigned long val, void *data)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002374{
2375 struct module *mod = data;
2376 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002377 struct kprobe *p;
2378 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002379 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002380
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002381 if (val == MODULE_STATE_COMING) {
2382 mutex_lock(&kprobe_mutex);
2383 add_module_kprobe_blacklist(mod);
2384 mutex_unlock(&kprobe_mutex);
2385 }
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002386 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002387 return NOTIFY_DONE;
2388
2389 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002390 * When MODULE_STATE_GOING was notified, both of module .text and
2391 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2392 * notified, only .init.text section would be freed. We need to
2393 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002394 */
2395 mutex_lock(&kprobe_mutex);
2396 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2397 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002398 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002399 if (within_module_init((unsigned long)p->addr, mod) ||
2400 (checkcore &&
2401 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002402 /*
2403 * The vaddr this probe is installed will soon
2404 * be vfreed buy not synced to disk. Hence,
2405 * disarming the breakpoint isn't needed.
Steven Rostedt (VMware)545a0282017-05-16 14:58:35 -04002406 *
2407 * Note, this will also move any optimized probes
2408 * that are pending to be removed from their
2409 * corresponding lists to the freeing_list and
2410 * will not be touched by the delayed
2411 * kprobe_optimizer work handler.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002412 */
2413 kill_kprobe(p);
2414 }
2415 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002416 if (val == MODULE_STATE_GOING)
2417 remove_module_kprobe_blacklist(mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002418 mutex_unlock(&kprobe_mutex);
2419 return NOTIFY_DONE;
2420}
2421
2422static struct notifier_block kprobe_module_nb = {
2423 .notifier_call = kprobes_module_callback,
2424 .priority = 0
2425};
2426
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002427/* Markers of _kprobe_blacklist section */
2428extern unsigned long __start_kprobe_blacklist[];
2429extern unsigned long __stop_kprobe_blacklist[];
2430
Masami Hiramatsu82d083a2020-09-10 17:55:05 +09002431void kprobe_free_init_mem(void)
2432{
2433 void *start = (void *)(&__init_begin);
2434 void *end = (void *)(&__init_end);
2435 struct hlist_head *head;
2436 struct kprobe *p;
2437 int i;
2438
2439 mutex_lock(&kprobe_mutex);
2440
2441 /* Kill all kprobes on initmem */
2442 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2443 head = &kprobe_table[i];
2444 hlist_for_each_entry(p, head, hlist) {
2445 if (start <= (void *)p->addr && (void *)p->addr < end)
2446 kill_kprobe(p);
2447 }
2448 }
2449
2450 mutex_unlock(&kprobe_mutex);
2451}
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453static int __init init_kprobes(void)
2454{
2455 int i, err = 0;
2456
2457 /* FIXME allocate the probe table, currently defined statically */
2458 /* initialize all list heads */
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002459 for (i = 0; i < KPROBE_TABLE_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 INIT_HLIST_HEAD(&kprobe_table[i]);
2461
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002462 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2463 __stop_kprobe_blacklist);
2464 if (err) {
2465 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2466 pr_err("Please take care of using kprobes.\n");
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002467 }
2468
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002469 if (kretprobe_blacklist_size) {
2470 /* lookup the function address from its name */
2471 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05302472 kretprobe_blacklist[i].addr =
Naveen N. Rao290e3072017-04-19 18:21:01 +05302473 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002474 if (!kretprobe_blacklist[i].addr)
2475 printk("kretprobe: lookup failed: %s\n",
2476 kretprobe_blacklist[i].name);
2477 }
2478 }
2479
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002480#if defined(CONFIG_OPTPROBES)
2481#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002482 /* Init kprobe_optinsn_slots */
2483 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2484#endif
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002485 /* By default, kprobes can be optimized */
2486 kprobes_allow_optimization = true;
2487#endif
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002488
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002489 /* By default, kprobes are armed */
2490 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002491
Rusty Lynch67729262005-07-05 18:54:50 -07002492 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002493 if (!err)
2494 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002495 if (!err)
2496 err = register_module_notifier(&kprobe_module_nb);
2497
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002498 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07002499
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01002500 if (!err)
2501 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return err;
2503}
Masami Hiramatsu36dadef2020-09-10 21:38:39 +09002504early_initcall(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Srinivasa Ds346fd592007-02-20 13:57:54 -08002506#ifdef CONFIG_DEBUG_FS
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002507static void report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002508 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002509{
2510 char *kprobe_type;
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002511 void *addr = p->addr;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002512
2513 if (p->pre_handler == pre_handler_kretprobe)
2514 kprobe_type = "r";
Srinivasa Ds346fd592007-02-20 13:57:54 -08002515 else
2516 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002517
Kees Cook60f7bb62020-07-02 15:20:22 -07002518 if (!kallsyms_show_value(pi->file->f_cred))
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002519 addr = NULL;
2520
Srinivasa Ds346fd592007-02-20 13:57:54 -08002521 if (sym)
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002522 seq_printf(pi, "%px %s %s+0x%x %s ",
2523 addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002524 (modname ? modname : " "));
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002525 else /* try to use %pS */
2526 seq_printf(pi, "%px %s %pS ",
2527 addr, kprobe_type, p->addr);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002528
2529 if (!pp)
2530 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002531 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002532 (kprobe_gone(p) ? "[GONE]" : ""),
2533 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002534 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2535 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002536}
2537
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002538static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002539{
2540 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2541}
2542
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002543static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002544{
2545 (*pos)++;
2546 if (*pos >= KPROBE_TABLE_SIZE)
2547 return NULL;
2548 return pos;
2549}
2550
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002551static void kprobe_seq_stop(struct seq_file *f, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002552{
2553 /* Nothing to do */
2554}
2555
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002556static int show_kprobe_addr(struct seq_file *pi, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002557{
2558 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002559 struct kprobe *p, *kp;
2560 const char *sym = NULL;
2561 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002562 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002563 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002564
2565 head = &kprobe_table[i];
2566 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002567 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002568 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002569 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002570 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002571 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002572 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002573 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002574 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002575 }
2576 preempt_enable();
2577 return 0;
2578}
2579
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002580static const struct seq_operations kprobes_sops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002581 .start = kprobe_seq_start,
2582 .next = kprobe_seq_next,
2583 .stop = kprobe_seq_stop,
2584 .show = show_kprobe_addr
2585};
2586
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002587DEFINE_SEQ_ATTRIBUTE(kprobes);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002588
Masami Hiramatsu63724742014-04-17 17:18:49 +09002589/* kprobes/blacklist -- shows which functions can not be probed */
2590static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2591{
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002592 mutex_lock(&kprobe_mutex);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002593 return seq_list_start(&kprobe_blacklist, *pos);
2594}
2595
2596static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2597{
2598 return seq_list_next(v, &kprobe_blacklist, pos);
2599}
2600
2601static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2602{
2603 struct kprobe_blacklist_entry *ent =
2604 list_entry(v, struct kprobe_blacklist_entry, list);
2605
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002606 /*
2607 * If /proc/kallsyms is not showing kernel address, we won't
2608 * show them here either.
2609 */
Kees Cook60f7bb62020-07-02 15:20:22 -07002610 if (!kallsyms_show_value(m->file->f_cred))
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002611 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2612 (void *)ent->start_addr);
2613 else
2614 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2615 (void *)ent->end_addr, (void *)ent->start_addr);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002616 return 0;
2617}
2618
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002619static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2620{
2621 mutex_unlock(&kprobe_mutex);
2622}
2623
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002624static const struct seq_operations kprobe_blacklist_sops = {
Masami Hiramatsu63724742014-04-17 17:18:49 +09002625 .start = kprobe_blacklist_seq_start,
2626 .next = kprobe_blacklist_seq_next,
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002627 .stop = kprobe_blacklist_seq_stop,
Masami Hiramatsu63724742014-04-17 17:18:49 +09002628 .show = kprobe_blacklist_seq_show,
2629};
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002630DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002631
Jessica Yu12310e342018-01-10 00:51:23 +01002632static int arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002633{
2634 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002635 struct kprobe *p;
Jessica Yu12310e342018-01-10 00:51:23 +01002636 unsigned int i, total = 0, errors = 0;
2637 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002638
2639 mutex_lock(&kprobe_mutex);
2640
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002641 /* If kprobes are armed, just return */
2642 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002643 goto already_enabled;
2644
Wang Nan977ad482015-02-13 14:40:24 -08002645 /*
2646 * optimize_kprobe() called by arm_kprobe() checks
2647 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2648 * arm_kprobe.
2649 */
2650 kprobes_all_disarmed = false;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002651 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002652 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2653 head = &kprobe_table[i];
Jessica Yu12310e342018-01-10 00:51:23 +01002654 /* Arm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002655 hlist_for_each_entry(p, head, hlist) {
Jessica Yu12310e342018-01-10 00:51:23 +01002656 if (!kprobe_disabled(p)) {
2657 err = arm_kprobe(p);
2658 if (err) {
2659 errors++;
2660 ret = err;
2661 }
2662 total++;
2663 }
2664 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002665 }
2666
Jessica Yu12310e342018-01-10 00:51:23 +01002667 if (errors)
2668 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2669 errors, total);
2670 else
2671 pr_info("Kprobes globally enabled\n");
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002672
2673already_enabled:
2674 mutex_unlock(&kprobe_mutex);
Jessica Yu12310e342018-01-10 00:51:23 +01002675 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002676}
2677
Jessica Yu297f9232018-01-10 00:51:24 +01002678static int disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002679{
2680 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002681 struct kprobe *p;
Jessica Yu297f9232018-01-10 00:51:24 +01002682 unsigned int i, total = 0, errors = 0;
2683 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002684
2685 mutex_lock(&kprobe_mutex);
2686
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002687 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002688 if (kprobes_all_disarmed) {
2689 mutex_unlock(&kprobe_mutex);
Jessica Yu297f9232018-01-10 00:51:24 +01002690 return 0;
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002691 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002692
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002693 kprobes_all_disarmed = true;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002694
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002695 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2696 head = &kprobe_table[i];
Jessica Yu297f9232018-01-10 00:51:24 +01002697 /* Disarm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002698 hlist_for_each_entry(p, head, hlist) {
Jessica Yu297f9232018-01-10 00:51:24 +01002699 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2700 err = disarm_kprobe(p, false);
2701 if (err) {
2702 errors++;
2703 ret = err;
2704 }
2705 total++;
2706 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002707 }
2708 }
Jessica Yu297f9232018-01-10 00:51:24 +01002709
2710 if (errors)
2711 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2712 errors, total);
2713 else
2714 pr_info("Kprobes globally disabled\n");
2715
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002716 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002717
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002718 /* Wait for disarming all kprobes by optimizer */
2719 wait_for_kprobe_optimizer();
Jessica Yu297f9232018-01-10 00:51:24 +01002720
2721 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002722}
2723
2724/*
2725 * XXX: The debugfs bool file interface doesn't allow for callbacks
2726 * when the bool state is switched. We can reuse that facility when
2727 * available
2728 */
2729static ssize_t read_enabled_file_bool(struct file *file,
2730 char __user *user_buf, size_t count, loff_t *ppos)
2731{
2732 char buf[3];
2733
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002734 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002735 buf[0] = '1';
2736 else
2737 buf[0] = '0';
2738 buf[1] = '\n';
2739 buf[2] = 0x00;
2740 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2741}
2742
2743static ssize_t write_enabled_file_bool(struct file *file,
2744 const char __user *user_buf, size_t count, loff_t *ppos)
2745{
2746 char buf[32];
Stephen Boydefeb1562012-01-12 17:17:11 -08002747 size_t buf_size;
Jessica Yu12310e342018-01-10 00:51:23 +01002748 int ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002749
2750 buf_size = min(count, (sizeof(buf)-1));
2751 if (copy_from_user(buf, user_buf, buf_size))
2752 return -EFAULT;
2753
Mathias Krause10fb46d2013-07-03 15:05:39 -07002754 buf[buf_size] = '\0';
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002755 switch (buf[0]) {
2756 case 'y':
2757 case 'Y':
2758 case '1':
Jessica Yu12310e342018-01-10 00:51:23 +01002759 ret = arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002760 break;
2761 case 'n':
2762 case 'N':
2763 case '0':
Jessica Yu297f9232018-01-10 00:51:24 +01002764 ret = disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002765 break;
Mathias Krause10fb46d2013-07-03 15:05:39 -07002766 default:
2767 return -EINVAL;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002768 }
2769
Jessica Yu12310e342018-01-10 00:51:23 +01002770 if (ret)
2771 return ret;
2772
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002773 return count;
2774}
2775
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002776static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002777 .read = read_enabled_file_bool,
2778 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002779 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002780};
2781
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002782static int __init debugfs_kprobe_init(void)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002783{
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002784 struct dentry *dir;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002785 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002786
2787 dir = debugfs_create_dir("kprobes", NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002788
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002789 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002790
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002791 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002792
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002793 debugfs_create_file("blacklist", 0400, dir, NULL,
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002794 &kprobe_blacklist_fops);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002795
Srinivasa Ds346fd592007-02-20 13:57:54 -08002796 return 0;
2797}
2798
2799late_initcall(debugfs_kprobe_init);
2800#endif /* CONFIG_DEBUG_FS */