blob: d750e025d1ff4579cabc367cf224228a45eb9b1e [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];
Hien Nguyenb94cce92005-06-23 00:09:19 -070056static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070058/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070059static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070060
Masami Hiramatsu43948f52010-10-25 22:18:01 +090061/* This protects kprobe_table and optimizing_list */
62static DEFINE_MUTEX(kprobe_mutex);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080063static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070064static struct {
Thomas Gleixnerec4846082009-07-25 16:09:17 +020065 raw_spinlock_t lock ____cacheline_aligned_in_smp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070066} kretprobe_table_locks[KPROBE_TABLE_SIZE];
67
Naveen N. Rao290e3072017-04-19 18:21:01 +053068kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
69 unsigned int __unused)
Naveen N. Rao49e0b462017-04-19 18:21:00 +053070{
71 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
72}
73
Thomas Gleixnerec4846082009-07-25 16:09:17 +020074static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070075{
76 return &(kretprobe_table_locks[hash].lock);
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Masami Hiramatsu376e2422014-04-17 17:17:05 +090079/* Blacklist -- list of struct kprobe_blacklist_entry */
80static LIST_HEAD(kprobe_blacklist);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070081
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080082#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070083/*
84 * kprobe->ainsn.insn points to the copy of the instruction to be
85 * single-stepped. x86_64, POWER4 and above have no-exec support and
86 * stepping on the instruction on a vmalloced/kmalloced/data page
87 * is a recipe for disaster
88 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070089struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -040090 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070091 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -070092 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070093 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080094 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050095 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070096};
97
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050098#define KPROBE_INSN_PAGE_SIZE(slots) \
99 (offsetof(struct kprobe_insn_page, slot_used) + \
100 (sizeof(char) * (slots)))
101
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500102static int slots_per_page(struct kprobe_insn_cache *c)
103{
104 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
105}
106
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800107enum kprobe_slot_state {
108 SLOT_CLEAN = 0,
109 SLOT_DIRTY = 1,
110 SLOT_USED = 2,
111};
112
Masami Hiramatsu63fef142017-08-18 17:24:00 +0900113void __weak *alloc_insn_page(void)
Heiko Carstensaf963972013-09-11 14:24:13 -0700114{
115 return module_alloc(PAGE_SIZE);
116}
117
Masami Hiramatsuc93f5cf2017-05-25 19:38:17 +0900118void __weak free_insn_page(void *page)
Heiko Carstensaf963972013-09-11 14:24:13 -0700119{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030120 module_memfree(page);
Heiko Carstensaf963972013-09-11 14:24:13 -0700121}
122
Heiko Carstensc802d642013-09-11 14:24:11 -0700123struct kprobe_insn_cache kprobe_insn_slots = {
124 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700125 .alloc = alloc_insn_page,
126 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300127 .sym = KPROBE_INSN_PAGE_SYM,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500128 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
129 .insn_size = MAX_INSN_SIZE,
130 .nr_garbage = 0,
131};
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900132static int collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800133
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700134/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800135 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700136 * We allocate an executable page if there's no room on existing ones.
137 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900138kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700139{
140 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700141 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700142
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900143 /* Since the slot array is not protected by rcu, we need a mutex */
Heiko Carstensc802d642013-09-11 14:24:11 -0700144 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700145 retry:
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900146 rcu_read_lock();
147 list_for_each_entry_rcu(kip, &c->pages, list) {
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500148 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700149 int i;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500150 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800151 if (kip->slot_used[i] == SLOT_CLEAN) {
152 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700153 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700154 slot = kip->insns + (i * c->insn_size);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900155 rcu_read_unlock();
Heiko Carstensc802d642013-09-11 14:24:11 -0700156 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700157 }
158 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500159 /* kip->nused is broken. Fix it. */
160 kip->nused = slots_per_page(c);
161 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700162 }
163 }
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900164 rcu_read_unlock();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700165
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800166 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500167 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800168 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500169
170 /* All out of space. Need to allocate a new page. */
171 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700172 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700173 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700174
175 /*
176 * Use module_alloc so this page is within +/- 2GB of where the
177 * kernel image and loaded module images reside. This is required
178 * so x86_64 can correctly handle the %rip-relative fixups.
179 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700180 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700181 if (!kip->insns) {
182 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700183 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700184 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400185 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500186 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800187 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700188 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800189 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700190 kip->cache = c;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900191 list_add_rcu(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700192 slot = kip->insns;
Adrian Hunter69e49082020-05-12 15:19:11 +0300193
194 /* Record the perf ksymbol register event after adding the page */
195 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns,
196 PAGE_SIZE, false, c->sym);
Heiko Carstensc802d642013-09-11 14:24:11 -0700197out:
198 mutex_unlock(&c->mutex);
199 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800200}
201
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800202/* Return 1 if all garbages are collected, otherwise 0. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900203static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800204{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800205 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800206 kip->nused--;
207 if (kip->nused == 0) {
208 /*
209 * Page is no longer in use. Free it unless
210 * it's the last one. We keep the last one
211 * so as not to have to set it up again the
212 * next time somebody inserts a probe.
213 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500214 if (!list_is_singular(&kip->list)) {
Adrian Hunter69e49082020-05-12 15:19:11 +0300215 /*
216 * Record perf ksymbol unregister event before removing
217 * the page.
218 */
219 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
220 (unsigned long)kip->insns, PAGE_SIZE, true,
221 kip->cache->sym);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900222 list_del_rcu(&kip->list);
223 synchronize_rcu();
Heiko Carstensaf963972013-09-11 14:24:13 -0700224 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800225 kfree(kip);
226 }
227 return 1;
228 }
229 return 0;
230}
231
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900232static int collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800233{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400234 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800235
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500236 /* Ensure no-one is interrupted on the garbages */
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -0800237 synchronize_rcu();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800238
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500239 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800240 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800241 if (kip->ngarbage == 0)
242 continue;
243 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500244 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900245 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800246 break;
247 }
248 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500249 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800250 return 0;
251}
252
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900253void __free_insn_slot(struct kprobe_insn_cache *c,
254 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500255{
256 struct kprobe_insn_page *kip;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900257 long idx;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500258
Heiko Carstensc802d642013-09-11 14:24:11 -0700259 mutex_lock(&c->mutex);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900260 rcu_read_lock();
261 list_for_each_entry_rcu(kip, &c->pages, list) {
262 idx = ((long)slot - (long)kip->insns) /
263 (c->insn_size * sizeof(kprobe_opcode_t));
264 if (idx >= 0 && idx < slots_per_page(c))
Heiko Carstensc802d642013-09-11 14:24:11 -0700265 goto out;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900266 }
267 /* Could not find this slot. */
268 WARN_ON(1);
269 kip = NULL;
270out:
271 rcu_read_unlock();
272 /* Mark and sweep: this may sleep */
273 if (kip) {
274 /* Check double free */
275 WARN_ON(kip->slot_used[idx] != SLOT_USED);
276 if (dirty) {
277 kip->slot_used[idx] = SLOT_DIRTY;
278 kip->ngarbage++;
279 if (++c->nr_garbage > slots_per_page(c))
280 collect_garbage_slots(c);
281 } else {
282 collect_one_slot(kip, idx);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500283 }
284 }
Heiko Carstensc802d642013-09-11 14:24:11 -0700285 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500286}
287
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900288/*
289 * Check given address is on the page of kprobe instruction slots.
290 * This will be used for checking whether the address on a stack
291 * is on a text area or not.
292 */
293bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
294{
295 struct kprobe_insn_page *kip;
296 bool ret = false;
297
298 rcu_read_lock();
299 list_for_each_entry_rcu(kip, &c->pages, list) {
300 if (addr >= (unsigned long)kip->insns &&
301 addr < (unsigned long)kip->insns + PAGE_SIZE) {
302 ret = true;
303 break;
304 }
305 }
306 rcu_read_unlock();
307
308 return ret;
309}
310
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300311int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
312 unsigned long *value, char *type, char *sym)
313{
314 struct kprobe_insn_page *kip;
315 int ret = -ERANGE;
316
317 rcu_read_lock();
318 list_for_each_entry_rcu(kip, &c->pages, list) {
319 if ((*symnum)--)
320 continue;
321 strlcpy(sym, c->sym, KSYM_NAME_LEN);
322 *type = 't';
323 *value = (unsigned long)kip->insns;
324 ret = 0;
325 break;
326 }
327 rcu_read_unlock();
328
329 return ret;
330}
331
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500332#ifdef CONFIG_OPTPROBES
333/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700334struct kprobe_insn_cache kprobe_optinsn_slots = {
335 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700336 .alloc = alloc_insn_page,
337 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300338 .sym = KPROBE_OPTINSN_PAGE_SYM,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500339 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
340 /* .insn_size is initialized later */
341 .nr_garbage = 0,
342};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500343#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800344#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700345
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800346/* We have preemption disabled.. so it is safe to use __ versions */
347static inline void set_kprobe_instance(struct kprobe *kp)
348{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600349 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800350}
351
352static inline void reset_kprobe_instance(void)
353{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600354 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800355}
356
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800357/*
358 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800359 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800360 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800361 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800362 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900363struct kprobe *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800366 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Masami Hiramatsu6743ad42020-05-12 17:02:33 +0900369 hlist_for_each_entry_rcu(p, head, hlist,
370 lockdep_is_held(&kprobe_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (p->addr == addr)
372 return p;
373 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return NULL;
376}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900377NOKPROBE_SYMBOL(get_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900379static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500380
381/* Return true if the kprobe is an aggregator */
382static inline int kprobe_aggrprobe(struct kprobe *p)
383{
384 return p->pre_handler == aggr_pre_handler;
385}
386
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900387/* Return true(!0) if the kprobe is unused */
388static inline int kprobe_unused(struct kprobe *p)
389{
390 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
391 list_empty(&p->list);
392}
393
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500394/*
395 * Keep all fields in the kprobe consistent
396 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900397static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500398{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900399 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
400 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500401}
402
403#ifdef CONFIG_OPTPROBES
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500404/* NOTE: change this value only with kprobe_mutex held */
405static bool kprobes_allow_optimization;
406
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500407/*
408 * Call all pre_handler on the list, but ignores its return value.
409 * This must be called from arch-dep optimized caller.
410 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900411void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500412{
413 struct kprobe *kp;
414
415 list_for_each_entry_rcu(kp, &p->list, list) {
416 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
417 set_kprobe_instance(kp);
Naveen N. Rao4f3a8712017-10-17 13:48:34 +0530418 kp->pre_handler(kp, regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500419 }
420 reset_kprobe_instance();
421 }
422}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900423NOKPROBE_SYMBOL(opt_pre_handler);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500424
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900425/* Free optimized instructions and optimized_kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900426static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900427{
428 struct optimized_kprobe *op;
429
430 op = container_of(p, struct optimized_kprobe, kp);
431 arch_remove_optimized_kprobe(op);
432 arch_remove_kprobe(p);
433 kfree(op);
434}
435
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500436/* Return true(!0) if the kprobe is ready for optimization. */
437static inline int kprobe_optready(struct kprobe *p)
438{
439 struct optimized_kprobe *op;
440
441 if (kprobe_aggrprobe(p)) {
442 op = container_of(p, struct optimized_kprobe, kp);
443 return arch_prepared_optinsn(&op->optinsn);
444 }
445
446 return 0;
447}
448
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900449/* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
450static inline int kprobe_disarmed(struct kprobe *p)
451{
452 struct optimized_kprobe *op;
453
454 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
455 if (!kprobe_aggrprobe(p))
456 return kprobe_disabled(p);
457
458 op = container_of(p, struct optimized_kprobe, kp);
459
460 return kprobe_disabled(p) && list_empty(&op->list);
461}
462
463/* Return true(!0) if the probe is queued on (un)optimizing lists */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900464static int kprobe_queued(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900465{
466 struct optimized_kprobe *op;
467
468 if (kprobe_aggrprobe(p)) {
469 op = container_of(p, struct optimized_kprobe, kp);
470 if (!list_empty(&op->list))
471 return 1;
472 }
473 return 0;
474}
475
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500476/*
477 * Return an optimized kprobe whose optimizing code replaces
478 * instructions including addr (exclude breakpoint).
479 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900480static struct kprobe *get_optimized_kprobe(unsigned long addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500481{
482 int i;
483 struct kprobe *p = NULL;
484 struct optimized_kprobe *op;
485
486 /* Don't check i == 0, since that is a breakpoint case. */
487 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
488 p = get_kprobe((void *)(addr - i));
489
490 if (p && kprobe_optready(p)) {
491 op = container_of(p, struct optimized_kprobe, kp);
492 if (arch_within_optimized_kprobe(op, addr))
493 return p;
494 }
495
496 return NULL;
497}
498
499/* Optimization staging list, protected by kprobe_mutex */
500static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900501static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900502static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500503
504static void kprobe_optimizer(struct work_struct *work);
505static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
506#define OPTIMIZE_DELAY 5
507
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900508/*
509 * Optimize (replace a breakpoint with a jump) kprobes listed on
510 * optimizing_list.
511 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900512static void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500513{
Andrea Righif1c6ece2019-08-12 20:43:02 +0200514 lockdep_assert_held(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500515 /*
516 * The optimization/unoptimization refers online_cpus via
517 * stop_machine() and cpu-hotplug modifies online_cpus.
518 * And same time, text_mutex will be held in cpu-hotplug and here.
519 * This combination can cause a deadlock (cpu-hotplug try to lock
520 * text_mutex but stop_machine can not be done because online_cpus
521 * has been changed)
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200522 * To avoid this deadlock, caller must have locked cpu hotplug
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500523 * for preventing cpu-hotplug outside of text_mutex locking.
524 */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200525 lockdep_assert_cpus_held();
526
527 /* Optimization never be done when disarmed */
528 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
529 list_empty(&optimizing_list))
530 return;
531
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900532 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900533}
534
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900535/*
536 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
537 * if need) kprobes listed on unoptimizing_list.
538 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900539static void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900540{
541 struct optimized_kprobe *op, *tmp;
542
Andrea Righif1c6ece2019-08-12 20:43:02 +0200543 lockdep_assert_held(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200544 /* See comment in do_optimize_kprobes() */
545 lockdep_assert_cpus_held();
546
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900547 /* Unoptimization must be done anytime */
548 if (list_empty(&unoptimizing_list))
549 return;
550
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900551 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900552 /* Loop free_list for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900553 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900554 /* Switching from detour code to origin */
555 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900556 /* Disarm probes if marked disabled */
557 if (kprobe_disabled(&op->kp))
558 arch_disarm_kprobe(&op->kp);
559 if (kprobe_unused(&op->kp)) {
560 /*
561 * Remove unused probes from hash list. After waiting
562 * for synchronization, these probes are reclaimed.
563 * (reclaiming is done by do_free_cleaned_kprobes.)
564 */
565 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900566 } else
567 list_del_init(&op->list);
568 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900569}
570
571/* Reclaim all kprobes on the free_list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900572static void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900573{
574 struct optimized_kprobe *op, *tmp;
575
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900576 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900577 list_del_init(&op->list);
Masami Hiramatsucbdd96f2018-09-11 19:21:09 +0900578 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
579 /*
580 * This must not happen, but if there is a kprobe
581 * still in use, keep it on kprobes hash list.
582 */
583 continue;
584 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900585 free_aggr_kprobe(&op->kp);
586 }
587}
588
589/* Start optimizer after OPTIMIZE_DELAY passed */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900590static void kick_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900591{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800592 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900593}
594
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900595/* Kprobe jump optimizer */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900596static void kprobe_optimizer(struct work_struct *work)
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900597{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900598 mutex_lock(&kprobe_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200599 cpus_read_lock();
Andrea Righif1c6ece2019-08-12 20:43:02 +0200600 mutex_lock(&text_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900601
602 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900603 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
604 * kprobes before waiting for quiesence period.
605 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900606 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900607
608 /*
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900609 * Step 2: Wait for quiesence period to ensure all potentially
610 * preempted tasks to have normally scheduled. Because optprobe
611 * may modify multiple instructions, there is a chance that Nth
612 * instruction is preempted. In that case, such tasks can return
613 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
614 * Note that on non-preemptive kernel, this is transparently converted
615 * to synchronoze_sched() to wait for all interrupts to have completed.
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900616 */
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900617 synchronize_rcu_tasks();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900618
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900619 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900620 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900621
622 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900623 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900624
Andrea Righif1c6ece2019-08-12 20:43:02 +0200625 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200626 cpus_read_unlock();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900627
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900628 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900629 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900630 kick_kprobe_optimizer();
Masami Hiramatsu1a0aa992020-05-12 17:02:56 +0900631
632 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900633}
634
635/* Wait for completing optimization and unoptimization */
Thomas Gleixner30e7d8942017-05-17 10:19:49 +0200636void wait_for_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900637{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800638 mutex_lock(&kprobe_mutex);
639
640 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
641 mutex_unlock(&kprobe_mutex);
642
643 /* this will also make optimizing_work execute immmediately */
644 flush_delayed_work(&optimizing_work);
645 /* @optimizing_work might not have been queued yet, relax */
646 cpu_relax();
647
648 mutex_lock(&kprobe_mutex);
649 }
650
651 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500652}
653
Masami Hiramatsue4add242020-01-07 23:42:24 +0900654static bool optprobe_queued_unopt(struct optimized_kprobe *op)
655{
656 struct optimized_kprobe *_op;
657
658 list_for_each_entry(_op, &unoptimizing_list, list) {
659 if (op == _op)
660 return true;
661 }
662
663 return false;
664}
665
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500666/* Optimize kprobe if p is ready to be optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900667static void optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500668{
669 struct optimized_kprobe *op;
670
671 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500672 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500673 (kprobe_disabled(p) || kprobes_all_disarmed))
674 return;
675
Masami Hiramatsu059053a2018-06-20 01:10:27 +0900676 /* kprobes with post_handler can not be optimized */
677 if (p->post_handler)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500678 return;
679
680 op = container_of(p, struct optimized_kprobe, kp);
681
682 /* Check there is no other kprobes at the optimized instructions */
683 if (arch_check_optimized_kprobe(op) < 0)
684 return;
685
686 /* Check if it is already optimized. */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900687 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
688 if (optprobe_queued_unopt(op)) {
689 /* This is under unoptimizing. Just dequeue the probe */
690 list_del_init(&op->list);
691 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500692 return;
Masami Hiramatsue4add242020-01-07 23:42:24 +0900693 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500694 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900695
Masami Hiramatsue4add242020-01-07 23:42:24 +0900696 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
697 if (WARN_ON_ONCE(!list_empty(&op->list)))
698 return;
699
700 list_add(&op->list, &optimizing_list);
701 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900702}
703
704/* Short cut to direct unoptimizing */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900705static void force_unoptimize_kprobe(struct optimized_kprobe *op)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900706{
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200707 lockdep_assert_cpus_held();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900708 arch_unoptimize_kprobe(op);
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900709 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500710}
711
712/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900713static void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500714{
715 struct optimized_kprobe *op;
716
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900717 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
718 return; /* This is not an optprobe nor optimized */
719
720 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900721 if (!kprobe_optimized(p))
722 return;
723
724 if (!list_empty(&op->list)) {
725 if (optprobe_queued_unopt(op)) {
726 /* Queued in unoptimizing queue */
727 if (force) {
728 /*
729 * Forcibly unoptimize the kprobe here, and queue it
730 * in the freeing list for release afterwards.
731 */
732 force_unoptimize_kprobe(op);
733 list_move(&op->list, &freeing_list);
734 }
735 } else {
736 /* Dequeue from the optimizing queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500737 list_del_init(&op->list);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900738 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900739 }
740 return;
741 }
742
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900743 /* Optimized kprobe case */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900744 if (force) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900745 /* Forcibly update the code: this is a special case */
746 force_unoptimize_kprobe(op);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900747 } else {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900748 list_add(&op->list, &unoptimizing_list);
749 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500750 }
751}
752
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900753/* Cancel unoptimizing for reusing */
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900754static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900755{
756 struct optimized_kprobe *op;
757
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900758 /*
759 * Unused kprobe MUST be on the way of delayed unoptimizing (means
760 * there is still a relative jump) and disabled.
761 */
762 op = container_of(ap, struct optimized_kprobe, kp);
Masami Hiramatsu44585152018-04-28 21:36:33 +0900763 WARN_ON_ONCE(list_empty(&op->list));
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900764 /* Enable the probe again */
765 ap->flags &= ~KPROBE_FLAG_DISABLED;
766 /* Optimize it again (remove from op->list) */
Masami Hiramatsu5f843ed2019-04-15 15:01:25 +0900767 if (!kprobe_optready(ap))
768 return -EINVAL;
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900769
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900770 optimize_kprobe(ap);
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900771 return 0;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900772}
773
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500774/* Remove optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900775static void kill_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500776{
777 struct optimized_kprobe *op;
778
779 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900780 if (!list_empty(&op->list))
781 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500782 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900783 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900784
785 if (kprobe_unused(p)) {
786 /* Enqueue if it is unused */
787 list_add(&op->list, &freeing_list);
788 /*
789 * Remove unused probes from the hash list. After waiting
790 * for synchronization, this probe is reclaimed.
791 * (reclaiming is done by do_free_cleaned_kprobes().)
792 */
793 hlist_del_rcu(&op->kp.hlist);
794 }
795
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900796 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500797 arch_remove_optimized_kprobe(op);
798}
799
Masami Hiramatsua4602462017-04-19 18:22:25 +0530800static inline
801void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
802{
803 if (!kprobe_ftrace(p))
804 arch_prepare_optimized_kprobe(op, p);
805}
806
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500807/* Try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900808static void prepare_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500809{
810 struct optimized_kprobe *op;
811
812 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsua4602462017-04-19 18:22:25 +0530813 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500814}
815
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500816/* Allocate new optimized_kprobe and try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900817static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500818{
819 struct optimized_kprobe *op;
820
821 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
822 if (!op)
823 return NULL;
824
825 INIT_LIST_HEAD(&op->list);
826 op->kp.addr = p->addr;
Masami Hiramatsua4602462017-04-19 18:22:25 +0530827 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500828
829 return &op->kp;
830}
831
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900832static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500833
834/*
835 * Prepare an optimized_kprobe and optimize it
836 * NOTE: p must be a normal registered kprobe
837 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900838static void try_to_optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500839{
840 struct kprobe *ap;
841 struct optimized_kprobe *op;
842
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900843 /* Impossible to optimize ftrace-based kprobe */
844 if (kprobe_ftrace(p))
845 return;
846
Masami Hiramatsu25764282012-06-05 19:28:26 +0900847 /* For preparing optimization, jump_label_text_reserved() is called */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200848 cpus_read_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +0900849 jump_label_lock();
850 mutex_lock(&text_mutex);
851
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500852 ap = alloc_aggr_kprobe(p);
853 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900854 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500855
856 op = container_of(ap, struct optimized_kprobe, kp);
857 if (!arch_prepared_optinsn(&op->optinsn)) {
858 /* If failed to setup optimizing, fallback to kprobe */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900859 arch_remove_optimized_kprobe(op);
860 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900861 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500862 }
863
864 init_aggr_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900865 optimize_kprobe(ap); /* This just kicks optimizer thread */
866
867out:
868 mutex_unlock(&text_mutex);
869 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200870 cpus_read_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500871}
872
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500873#ifdef CONFIG_SYSCTL
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900874static void optimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500875{
876 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500877 struct kprobe *p;
878 unsigned int i;
879
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900880 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500881 /* If optimization is already allowed, just return */
882 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900883 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500884
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200885 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500886 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500887 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
888 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900889 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500890 if (!kprobe_disabled(p))
891 optimize_kprobe(p);
892 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200893 cpus_read_unlock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500894 printk(KERN_INFO "Kprobes globally optimized\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900895out:
896 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500897}
898
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900899static void unoptimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500900{
901 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500902 struct kprobe *p;
903 unsigned int i;
904
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900905 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500906 /* If optimization is already prohibited, just return */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900907 if (!kprobes_allow_optimization) {
908 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500909 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900910 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500911
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200912 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500913 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500914 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
915 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900916 hlist_for_each_entry(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500917 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900918 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500919 }
920 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200921 cpus_read_unlock();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900922 mutex_unlock(&kprobe_mutex);
923
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900924 /* Wait for unoptimizing completion */
925 wait_for_kprobe_optimizer();
926 printk(KERN_INFO "Kprobes globally unoptimized\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500927}
928
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900929static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500930int sysctl_kprobes_optimization;
931int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200932 void *buffer, size_t *length,
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500933 loff_t *ppos)
934{
935 int ret;
936
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900937 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500938 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
939 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
940
941 if (sysctl_kprobes_optimization)
942 optimize_all_kprobes();
943 else
944 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900945 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500946
947 return ret;
948}
949#endif /* CONFIG_SYSCTL */
950
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900951/* Put a breakpoint for a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900952static void __arm_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500953{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900954 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500955
956 /* Check collision with other optimized kprobes */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900957 _p = get_optimized_kprobe((unsigned long)p->addr);
958 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900959 /* Fallback to unoptimized kprobe */
960 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500961
962 arch_arm_kprobe(p);
963 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
964}
965
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900966/* Remove the breakpoint of a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900967static void __disarm_kprobe(struct kprobe *p, bool reopt)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500968{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900969 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500970
Wang Nan69d54b92015-02-13 14:40:26 -0800971 /* Try to unoptimize */
972 unoptimize_kprobe(p, kprobes_all_disarmed);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500973
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900974 if (!kprobe_queued(p)) {
975 arch_disarm_kprobe(p);
976 /* If another kprobe was blocked, optimize it. */
977 _p = get_optimized_kprobe((unsigned long)p->addr);
978 if (unlikely(_p) && reopt)
979 optimize_kprobe(_p);
980 }
981 /* TODO: reoptimize others after unoptimized this probe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500982}
983
984#else /* !CONFIG_OPTPROBES */
985
986#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900987#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500988#define kill_optimized_kprobe(p) do {} while (0)
989#define prepare_optimized_kprobe(p) do {} while (0)
990#define try_to_optimize_kprobe(p) do {} while (0)
991#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900992#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
993#define kprobe_disarmed(p) kprobe_disabled(p)
994#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500995
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900996static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900997{
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900998 /*
999 * If the optimized kprobe is NOT supported, the aggr kprobe is
1000 * released at the same time that the last aggregated kprobe is
1001 * unregistered.
1002 * Thus there should be no chance to reuse unused kprobe.
1003 */
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001004 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001005 return -EINVAL;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001006}
1007
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001008static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001009{
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001010 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001011 kfree(p);
1012}
1013
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001014static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001015{
1016 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1017}
1018#endif /* CONFIG_OPTPROBES */
1019
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001020#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001021static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +09001022 .func = kprobe_ftrace_handler,
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001023 .flags = FTRACE_OPS_FL_SAVE_REGS,
1024};
1025
1026static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1027 .func = kprobe_ftrace_handler,
Masami Hiramatsu1d70be32014-11-21 05:25:23 -05001028 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001029};
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001030
1031static int kprobe_ipmodify_enabled;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001032static int kprobe_ftrace_enabled;
1033
1034/* Must ensure p->addr is really on ftrace */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001035static int prepare_kprobe(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001036{
1037 if (!kprobe_ftrace(p))
1038 return arch_prepare_kprobe(p);
1039
1040 return arch_prepare_kprobe_ftrace(p);
1041}
1042
1043/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001044static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1045 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001046{
Jessica Yu12310e342018-01-10 00:51:23 +01001047 int ret = 0;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001048
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001049 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001050 if (ret) {
Masami Hiramatsu44585152018-04-28 21:36:33 +09001051 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1052 p->addr, ret);
Jessica Yu12310e342018-01-10 00:51:23 +01001053 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001054 }
Jessica Yu12310e342018-01-10 00:51:23 +01001055
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001056 if (*cnt == 0) {
1057 ret = register_ftrace_function(ops);
Jessica Yu12310e342018-01-10 00:51:23 +01001058 if (ret) {
1059 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1060 goto err_ftrace;
1061 }
1062 }
1063
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001064 (*cnt)++;
Jessica Yu12310e342018-01-10 00:51:23 +01001065 return ret;
1066
1067err_ftrace:
1068 /*
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001069 * At this point, sinec ops is not registered, we should be sefe from
1070 * registering empty filter.
Jessica Yu12310e342018-01-10 00:51:23 +01001071 */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001072 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001073 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001074}
1075
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001076static int arm_kprobe_ftrace(struct kprobe *p)
1077{
1078 bool ipmodify = (p->post_handler != NULL);
1079
1080 return __arm_kprobe_ftrace(p,
1081 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1082 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1083}
1084
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001085/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001086static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1087 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001088{
Jessica Yu297f9232018-01-10 00:51:24 +01001089 int ret = 0;
1090
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001091 if (*cnt == 1) {
1092 ret = unregister_ftrace_function(ops);
Jessica Yu297f9232018-01-10 00:51:24 +01001093 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1094 return ret;
1095 }
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001096
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001097 (*cnt)--;
Jessica Yu297f9232018-01-10 00:51:24 +01001098
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001099 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Masami Hiramatsu44585152018-04-28 21:36:33 +09001100 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1101 p->addr, ret);
Jessica Yu297f9232018-01-10 00:51:24 +01001102 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001103}
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001104
1105static int disarm_kprobe_ftrace(struct kprobe *p)
1106{
1107 bool ipmodify = (p->post_handler != NULL);
1108
1109 return __disarm_kprobe_ftrace(p,
1110 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1111 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1112}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001113#else /* !CONFIG_KPROBES_ON_FTRACE */
Muchun Song10de7952020-08-06 01:20:46 +08001114static inline int prepare_kprobe(struct kprobe *p)
1115{
1116 return arch_prepare_kprobe(p);
1117}
1118
1119static inline int arm_kprobe_ftrace(struct kprobe *p)
1120{
1121 return -ENODEV;
1122}
1123
1124static inline int disarm_kprobe_ftrace(struct kprobe *p)
1125{
1126 return -ENODEV;
1127}
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001128#endif
1129
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001130/* Arm a kprobe with text_mutex */
Jessica Yu12310e342018-01-10 00:51:23 +01001131static int arm_kprobe(struct kprobe *kp)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001132{
Jessica Yu12310e342018-01-10 00:51:23 +01001133 if (unlikely(kprobe_ftrace(kp)))
1134 return arm_kprobe_ftrace(kp);
1135
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001136 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001137 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001138 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001139 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001140 cpus_read_unlock();
Jessica Yu12310e342018-01-10 00:51:23 +01001141
1142 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001143}
1144
1145/* Disarm a kprobe with text_mutex */
Jessica Yu297f9232018-01-10 00:51:24 +01001146static int disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001147{
Jessica Yu297f9232018-01-10 00:51:24 +01001148 if (unlikely(kprobe_ftrace(kp)))
1149 return disarm_kprobe_ftrace(kp);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001150
1151 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001152 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001153 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001154 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001155 cpus_read_unlock();
Jessica Yu297f9232018-01-10 00:51:24 +01001156
1157 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001158}
1159
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001160/*
1161 * Aggregate handlers for multiple kprobes support - these handlers
1162 * take care of invoking the individual kprobe handlers on p->list
1163 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001164static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001165{
1166 struct kprobe *kp;
1167
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001168 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001169 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001170 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001171 if (kp->pre_handler(kp, regs))
1172 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001173 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001174 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001175 }
1176 return 0;
1177}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001178NOKPROBE_SYMBOL(aggr_pre_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001179
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001180static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1181 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001182{
1183 struct kprobe *kp;
1184
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001185 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001186 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001187 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001188 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001189 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001190 }
1191 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001192}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001193NOKPROBE_SYMBOL(aggr_post_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001194
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001195static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1196 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001197{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001198 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001199
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001200 /*
1201 * if we faulted "during" the execution of a user specified
1202 * probe handler, invoke just that probe's fault handler
1203 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001204 if (cur && cur->fault_handler) {
1205 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001206 return 1;
1207 }
1208 return 0;
1209}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001210NOKPROBE_SYMBOL(aggr_fault_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001211
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001212/* Walks the list and increments nmissed count for multiprobe case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001213void kprobes_inc_nmissed_count(struct kprobe *p)
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001214{
1215 struct kprobe *kp;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001216 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001217 p->nmissed++;
1218 } else {
1219 list_for_each_entry_rcu(kp, &p->list, list)
1220 kp->nmissed++;
1221 }
1222 return;
1223}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001224NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001225
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001226void recycle_rp_inst(struct kretprobe_instance *ri,
1227 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001228{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001229 struct kretprobe *rp = ri->rp;
1230
Hien Nguyenb94cce92005-06-23 00:09:19 -07001231 /* remove rp inst off the rprobe_inst_table */
1232 hlist_del(&ri->hlist);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001233 INIT_HLIST_NODE(&ri->hlist);
1234 if (likely(rp)) {
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001235 raw_spin_lock(&rp->lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001236 hlist_add_head(&ri->hlist, &rp->free_instances);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001237 raw_spin_unlock(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001238 } else
1239 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -07001240 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001241}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001242NOKPROBE_SYMBOL(recycle_rp_inst);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001243
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001244void kretprobe_hash_lock(struct task_struct *tsk,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001245 struct hlist_head **head, unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001246__acquires(hlist_lock)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001247{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001248 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001249 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001250
1251 *head = &kretprobe_inst_table[hash];
1252 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001253 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001254}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001255NOKPROBE_SYMBOL(kretprobe_hash_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001256
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001257static void kretprobe_table_lock(unsigned long hash,
1258 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001259__acquires(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001260{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001261 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1262 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001263}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001264NOKPROBE_SYMBOL(kretprobe_table_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001265
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001266void kretprobe_hash_unlock(struct task_struct *tsk,
1267 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001268__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001269{
1270 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001271 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001272
1273 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001274 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001275}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001276NOKPROBE_SYMBOL(kretprobe_hash_unlock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001277
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001278static void kretprobe_table_unlock(unsigned long hash,
1279 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001280__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001281{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001282 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1283 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001284}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001285NOKPROBE_SYMBOL(kretprobe_table_unlock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001286
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001287struct kprobe kprobe_busy = {
1288 .addr = (void *) get_kprobe,
1289};
1290
1291void kprobe_busy_begin(void)
1292{
1293 struct kprobe_ctlblk *kcb;
1294
1295 preempt_disable();
1296 __this_cpu_write(current_kprobe, &kprobe_busy);
1297 kcb = get_kprobe_ctlblk();
1298 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1299}
1300
1301void kprobe_busy_end(void)
1302{
1303 __this_cpu_write(current_kprobe, NULL);
1304 preempt_enable();
1305}
1306
Hien Nguyenb94cce92005-06-23 00:09:19 -07001307/*
bibo maoc6fd91f2006-03-26 01:38:20 -08001308 * This function is called from finish_task_switch when task tk becomes dead,
1309 * so that we can recycle any function-return probe instances associated
1310 * with this task. These left over instances represent probed functions
1311 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001312 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001313void kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001314{
bibo,mao62c27be2006-10-02 02:17:33 -07001315 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -07001316 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001317 struct hlist_node *tmp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001318 unsigned long hash, flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -07001319
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001320 if (unlikely(!kprobes_initialized))
1321 /* Early boot. kretprobe_table_locks not yet initialized. */
1322 return;
1323
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001324 kprobe_busy_begin();
1325
Ananth N Mavinakayanahallid496aab2012-01-20 14:34:04 -08001326 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001327 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1328 head = &kretprobe_inst_table[hash];
1329 kretprobe_table_lock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001330 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -07001331 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -07001332 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -07001333 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001334 kretprobe_table_unlock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001335 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -07001336 hlist_del(&ri->hlist);
1337 kfree(ri);
1338 }
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001339
1340 kprobe_busy_end();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001341}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001342NOKPROBE_SYMBOL(kprobe_flush_task);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001343
Hien Nguyenb94cce92005-06-23 00:09:19 -07001344static inline void free_rp_inst(struct kretprobe *rp)
1345{
1346 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001347 struct hlist_node *next;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001348
Sasha Levinb67bfe02013-02-27 17:06:00 -08001349 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001350 hlist_del(&ri->hlist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001351 kfree(ri);
1352 }
1353}
1354
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001355static void cleanup_rp_inst(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001356{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001357 unsigned long flags, hash;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001358 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001359 struct hlist_node *next;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001360 struct hlist_head *head;
1361
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001362 /* No race here */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001363 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1364 kretprobe_table_lock(hash, &flags);
1365 head = &kretprobe_inst_table[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001366 hlist_for_each_entry_safe(ri, next, head, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001367 if (ri->rp == rp)
1368 ri->rp = NULL;
1369 }
1370 kretprobe_table_unlock(hash, &flags);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001371 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001372 free_rp_inst(rp);
1373}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001374NOKPROBE_SYMBOL(cleanup_rp_inst);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001375
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001376/* Add the new probe to ap->list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001377static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001378{
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001379 if (p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001380 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001381
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001382 list_add_rcu(&p->list, &ap->list);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001383 if (p->post_handler && !ap->post_handler)
1384 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001385
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001386 return 0;
1387}
1388
1389/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001390 * Fill in the required fields of the "manager kprobe". Replace the
1391 * earlier kprobe in the hlist with the manager kprobe
1392 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001393static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001394{
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001395 /* Copy p's insn slot to ap */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001396 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001397 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001398 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001399 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001400 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001401 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001402 /* We don't care the kprobe which has gone. */
1403 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001404 ap->post_handler = aggr_post_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001405
1406 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001407 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001408
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001409 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001410 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001411}
1412
1413/*
1414 * This is the second or subsequent kprobe at the address - handle
1415 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001416 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001417static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001418{
1419 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001420 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001421
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001422 cpus_read_lock();
1423
Masami Hiramatsu25764282012-06-05 19:28:26 +09001424 /* For preparing optimization, jump_label_text_reserved() is called */
1425 jump_label_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001426 mutex_lock(&text_mutex);
1427
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001428 if (!kprobe_aggrprobe(orig_p)) {
1429 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1430 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001431 if (!ap) {
1432 ret = -ENOMEM;
1433 goto out;
1434 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001435 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001436 } else if (kprobe_unused(ap)) {
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001437 /* This probe is going to die. Rescue it */
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001438 ret = reuse_unused_kprobe(ap);
1439 if (ret)
1440 goto out;
1441 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001442
1443 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001444 /*
1445 * Attempting to insert new probe at the same location that
1446 * had a probe in the module vaddr area which already
1447 * freed. So, the instruction slot has already been
1448 * released. We need a new slot for the new probe.
1449 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001450 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001451 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001452 /*
1453 * Even if fail to allocate new slot, don't need to
1454 * free aggr_probe. It will be used next time, or
1455 * freed by unregister_kprobe.
1456 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001457 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001458
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001459 /* Prepare optimized instructions if possible. */
1460 prepare_optimized_kprobe(ap);
1461
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001462 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001463 * Clear gone flag to prevent allocating new slot again, and
1464 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001465 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001466 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1467 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001468 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001469
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001470 /* Copy ap's insn slot to p */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001471 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001472 ret = add_new_kprobe(ap, p);
1473
1474out:
1475 mutex_unlock(&text_mutex);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001476 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001477 cpus_read_unlock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001478
1479 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1480 ap->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01001481 if (!kprobes_all_disarmed) {
Masami Hiramatsu25764282012-06-05 19:28:26 +09001482 /* Arm the breakpoint again. */
Jessica Yu12310e342018-01-10 00:51:23 +01001483 ret = arm_kprobe(ap);
1484 if (ret) {
1485 ap->flags |= KPROBE_FLAG_DISABLED;
1486 list_del_rcu(&p->list);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001487 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001488 }
1489 }
Masami Hiramatsu25764282012-06-05 19:28:26 +09001490 }
1491 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001492}
1493
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001494bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1495{
1496 /* The __kprobes marked functions and entry code must not be probed */
1497 return addr >= (unsigned long)__kprobes_text_start &&
1498 addr < (unsigned long)__kprobes_text_end;
1499}
1500
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001501static bool __within_kprobe_blacklist(unsigned long addr)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001502{
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001503 struct kprobe_blacklist_entry *ent;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001504
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001505 if (arch_within_kprobe_blacklist(addr))
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001506 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001507 /*
1508 * If there exists a kprobe_blacklist, verify and
1509 * fail any probe registration in the prohibited area
1510 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001511 list_for_each_entry(ent, &kprobe_blacklist, list) {
1512 if (addr >= ent->start_addr && addr < ent->end_addr)
1513 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001514 }
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001515 return false;
1516}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001517
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001518bool within_kprobe_blacklist(unsigned long addr)
1519{
1520 char symname[KSYM_NAME_LEN], *p;
1521
1522 if (__within_kprobe_blacklist(addr))
1523 return true;
1524
1525 /* Check if the address is on a suffixed-symbol */
1526 if (!lookup_symbol_name(addr, symname)) {
1527 p = strchr(symname, '.');
1528 if (!p)
1529 return false;
1530 *p = '\0';
1531 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1532 if (addr)
1533 return __within_kprobe_blacklist(addr);
1534 }
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001535 return false;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001536}
1537
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001538/*
1539 * If we have a symbol_name argument, look it up and add the offset field
1540 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001541 * This returns encoded errors if it fails to look up symbol or invalid
1542 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001543 */
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301544static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1545 const char *symbol_name, unsigned int offset)
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001546{
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301547 if ((symbol_name && addr) || (!symbol_name && !addr))
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001548 goto invalid;
1549
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301550 if (symbol_name) {
Linus Torvalds7246f602017-05-05 11:36:44 -07001551 addr = kprobe_lookup_name(symbol_name, offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001552 if (!addr)
1553 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001554 }
1555
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301556 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001557 if (addr)
1558 return addr;
1559
1560invalid:
1561 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001562}
1563
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301564static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1565{
1566 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1567}
1568
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301569/* Check passed kprobe is valid and return kprobe in kprobe_table. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001570static struct kprobe *__get_valid_kprobe(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301571{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001572 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301573
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001574 lockdep_assert_held(&kprobe_mutex);
1575
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001576 ap = get_kprobe(p->addr);
1577 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301578 return NULL;
1579
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001580 if (p != ap) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001581 list_for_each_entry(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301582 if (list_p == p)
1583 /* kprobe p is a valid probe */
1584 goto valid;
1585 return NULL;
1586 }
1587valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001588 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301589}
1590
1591/* Return error if the kprobe is being re-registered */
1592static inline int check_kprobe_rereg(struct kprobe *p)
1593{
1594 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301595
1596 mutex_lock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001597 if (__get_valid_kprobe(p))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301598 ret = -EINVAL;
1599 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001600
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301601 return ret;
1602}
1603
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001604int __weak arch_check_ftrace_location(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001606 unsigned long ftrace_addr;
1607
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001608 ftrace_addr = ftrace_location((unsigned long)p->addr);
1609 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001610#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001611 /* Given address is not on the instruction boundary */
1612 if ((unsigned long)p->addr != ftrace_addr)
1613 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001614 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001615#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001616 return -EINVAL;
1617#endif
1618 }
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001619 return 0;
1620}
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001621
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001622static int check_kprobe_address_safe(struct kprobe *p,
1623 struct module **probed_mod)
1624{
1625 int ret;
1626
1627 ret = arch_check_ftrace_location(p);
1628 if (ret)
1629 return ret;
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001630 jump_label_lock();
1631 preempt_disable();
1632
1633 /* Ensure it is not in reserved area nor out of text */
1634 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001635 within_kprobe_blacklist((unsigned long) p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001636 jump_label_text_reserved(p->addr, p->addr) ||
1637 find_bug((unsigned long)p->addr)) {
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001638 ret = -EINVAL;
1639 goto out;
1640 }
1641
1642 /* Check if are we probing a module */
1643 *probed_mod = __module_text_address((unsigned long) p->addr);
1644 if (*probed_mod) {
1645 /*
1646 * We must hold a refcount of the probed module while updating
1647 * its code to prohibit unexpected unloading.
1648 */
1649 if (unlikely(!try_module_get(*probed_mod))) {
1650 ret = -ENOENT;
1651 goto out;
1652 }
1653
1654 /*
1655 * If the module freed .init.text, we couldn't insert
1656 * kprobes in there.
1657 */
1658 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1659 (*probed_mod)->state != MODULE_STATE_COMING) {
1660 module_put(*probed_mod);
1661 *probed_mod = NULL;
1662 ret = -ENOENT;
1663 }
1664 }
1665out:
1666 preempt_enable();
1667 jump_label_unlock();
1668
1669 return ret;
1670}
1671
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001672int register_kprobe(struct kprobe *p)
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001673{
1674 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001675 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001676 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001677 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001679 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001680 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001681 if (IS_ERR(addr))
1682 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001683 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001684
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301685 ret = check_kprobe_rereg(p);
1686 if (ret)
1687 return ret;
1688
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001689 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1690 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001691 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001692 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001693
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001694 ret = check_kprobe_address_safe(p, &probed_mod);
1695 if (ret)
1696 return ret;
1697
1698 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001699
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001700 old_p = get_kprobe(p->addr);
1701 if (old_p) {
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001702 /* Since this may unoptimize old_p, locking text_mutex. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001703 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto out;
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001707 cpus_read_lock();
1708 /* Prevent text modification */
1709 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001710 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001711 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001712 cpus_read_unlock();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001713 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001714 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001715
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001716 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001717 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1719
Jessica Yu12310e342018-01-10 00:51:23 +01001720 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1721 ret = arm_kprobe(p);
1722 if (ret) {
1723 hlist_del_rcu(&p->hlist);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001724 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001725 goto out;
1726 }
1727 }
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001728
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001729 /* Try to optimize kprobe */
1730 try_to_optimize_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001732 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001733
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001734 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001735 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return ret;
1738}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001739EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001741/* Check if all probes on the aggrprobe are disabled */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001742static int aggr_kprobe_disabled(struct kprobe *ap)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001743{
1744 struct kprobe *kp;
1745
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001746 lockdep_assert_held(&kprobe_mutex);
1747
1748 list_for_each_entry(kp, &ap->list, list)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001749 if (!kprobe_disabled(kp))
1750 /*
1751 * There is an active probe on the list.
1752 * We can't disable this ap.
1753 */
1754 return 0;
1755
1756 return 1;
1757}
1758
1759/* Disable one kprobe: Make sure called under kprobe_mutex is locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001760static struct kprobe *__disable_kprobe(struct kprobe *p)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001761{
1762 struct kprobe *orig_p;
Jessica Yu297f9232018-01-10 00:51:24 +01001763 int ret;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001764
1765 /* Get an original kprobe for return */
1766 orig_p = __get_valid_kprobe(p);
1767 if (unlikely(orig_p == NULL))
Jessica Yu297f9232018-01-10 00:51:24 +01001768 return ERR_PTR(-EINVAL);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001769
1770 if (!kprobe_disabled(p)) {
1771 /* Disable probe if it is a child probe */
1772 if (p != orig_p)
1773 p->flags |= KPROBE_FLAG_DISABLED;
1774
1775 /* Try to disarm and disable this/parent probe */
1776 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Wang Nan69d54b92015-02-13 14:40:26 -08001777 /*
1778 * If kprobes_all_disarmed is set, orig_p
1779 * should have already been disarmed, so
1780 * skip unneed disarming process.
1781 */
Jessica Yu297f9232018-01-10 00:51:24 +01001782 if (!kprobes_all_disarmed) {
1783 ret = disarm_kprobe(orig_p, true);
1784 if (ret) {
1785 p->flags &= ~KPROBE_FLAG_DISABLED;
1786 return ERR_PTR(ret);
1787 }
1788 }
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001789 orig_p->flags |= KPROBE_FLAG_DISABLED;
1790 }
1791 }
1792
1793 return orig_p;
1794}
1795
Masami Hiramatsu98616682008-04-28 02:14:28 -07001796/*
1797 * Unregister a kprobe without a scheduler synchronization.
1798 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001799static int __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001800{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001801 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001802
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001803 /* Disable kprobe. This will disarm it if needed. */
1804 ap = __disable_kprobe(p);
Jessica Yu297f9232018-01-10 00:51:24 +01001805 if (IS_ERR(ap))
1806 return PTR_ERR(ap);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001807
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001808 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001809 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001810 * This probe is an independent(and non-optimized) kprobe
1811 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001812 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001813 goto disarmed;
1814
1815 /* Following process expects this probe is an aggrprobe */
1816 WARN_ON(!kprobe_aggrprobe(ap));
1817
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001818 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1819 /*
1820 * !disarmed could be happen if the probe is under delayed
1821 * unoptimizing.
1822 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001823 goto disarmed;
1824 else {
1825 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001826 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001827 list_for_each_entry(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001828 if ((list_p != p) && (list_p->post_handler))
1829 goto noclean;
1830 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001831 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001832 }
1833noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001834 /*
1835 * Remove from the aggrprobe: this path will do nothing in
1836 * __unregister_kprobe_bottom().
1837 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001838 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001839 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1840 /*
1841 * Try to optimize this probe again, because post
1842 * handler may have been changed.
1843 */
1844 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001845 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001846 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001847
1848disarmed:
1849 hlist_del_rcu(&ap->hlist);
1850 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001851}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001852
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001853static void __unregister_kprobe_bottom(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001854{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001855 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001856
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001857 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001858 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001859 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001860 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001861 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001862 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001863 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001864 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001865 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001866 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001869int register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001870{
1871 int i, ret = 0;
1872
1873 if (num <= 0)
1874 return -EINVAL;
1875 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001876 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001877 if (ret < 0) {
1878 if (i > 0)
1879 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001880 break;
1881 }
1882 }
1883 return ret;
1884}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001885EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001886
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001887void unregister_kprobe(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001888{
1889 unregister_kprobes(&p, 1);
1890}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001891EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001892
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001893void unregister_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001894{
1895 int i;
1896
1897 if (num <= 0)
1898 return;
1899 mutex_lock(&kprobe_mutex);
1900 for (i = 0; i < num; i++)
1901 if (__unregister_kprobe_top(kps[i]) < 0)
1902 kps[i]->addr = NULL;
1903 mutex_unlock(&kprobe_mutex);
1904
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001905 synchronize_rcu();
Masami Hiramatsu98616682008-04-28 02:14:28 -07001906 for (i = 0; i < num; i++)
1907 if (kps[i]->addr)
1908 __unregister_kprobe_bottom(kps[i]);
1909}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001910EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001911
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301912int __weak kprobe_exceptions_notify(struct notifier_block *self,
1913 unsigned long val, void *data)
Naveen N. Raofc62d022017-02-08 01:24:14 +05301914{
1915 return NOTIFY_DONE;
1916}
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301917NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Naveen N. Raofc62d022017-02-08 01:24:14 +05301918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919static struct notifier_block kprobe_exceptions_nb = {
1920 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001921 .priority = 0x7fffffff /* we need to be notified first */
1922};
1923
Michael Ellerman3d7e3382007-07-19 01:48:11 -07001924unsigned long __weak arch_deref_entry_point(void *entry)
1925{
1926 return (unsigned long)entry;
1927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001929#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -08001930/*
1931 * This kprobe pre_handler is registered with every kretprobe. When probe
1932 * hits it will set up the return probe.
1933 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001934static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Adrian Bunke65cefe2006-02-03 03:03:42 -08001935{
1936 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001937 unsigned long hash, flags = 0;
1938 struct kretprobe_instance *ri;
Adrian Bunke65cefe2006-02-03 03:03:42 -08001939
Masami Hiramatsuf96f5672014-08-04 03:10:16 +00001940 /*
1941 * To avoid deadlocks, prohibit return probing in NMI contexts,
1942 * just skip the probe and increase the (inexact) 'nmissed'
1943 * statistical counter, so that the user is informed that
1944 * something happened:
1945 */
1946 if (unlikely(in_nmi())) {
1947 rp->nmissed++;
1948 return 0;
1949 }
1950
1951 /* TODO: consider to only swap the RA after the last pre_handler fired */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001952 hash = hash_ptr(current, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001953 raw_spin_lock_irqsave(&rp->lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001954 if (!hlist_empty(&rp->free_instances)) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001955 ri = hlist_entry(rp->free_instances.first,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001956 struct kretprobe_instance, hlist);
1957 hlist_del(&ri->hlist);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001958 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001959
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001960 ri->rp = rp;
1961 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001962
Jiang Liu55ca6142012-02-03 15:37:16 -08001963 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
1964 raw_spin_lock_irqsave(&rp->lock, flags);
1965 hlist_add_head(&ri->hlist, &rp->free_instances);
1966 raw_spin_unlock_irqrestore(&rp->lock, flags);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001967 return 0;
Jiang Liu55ca6142012-02-03 15:37:16 -08001968 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001969
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001970 arch_prepare_kretprobe(ri, regs);
1971
1972 /* XXX(hch): why is there no hlist_move_head? */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001973 INIT_HLIST_NODE(&ri->hlist);
1974 kretprobe_table_lock(hash, &flags);
1975 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
1976 kretprobe_table_unlock(hash, &flags);
1977 } else {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001978 rp->nmissed++;
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001979 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001980 }
Adrian Bunke65cefe2006-02-03 03:03:42 -08001981 return 0;
1982}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001983NOKPROBE_SYMBOL(pre_handler_kretprobe);
Adrian Bunke65cefe2006-02-03 03:03:42 -08001984
Naveen N. Rao659b9572017-07-07 22:37:24 +05301985bool __weak arch_kprobe_on_func_entry(unsigned long offset)
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301986{
1987 return !offset;
1988}
1989
Naveen N. Rao659b9572017-07-07 22:37:24 +05301990bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301991{
1992 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
1993
1994 if (IS_ERR(kp_addr))
1995 return false;
1996
1997 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) ||
Naveen N. Rao659b9572017-07-07 22:37:24 +05301998 !arch_kprobe_on_func_entry(offset))
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301999 return false;
2000
2001 return true;
2002}
2003
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002004int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002005{
2006 int ret = 0;
2007 struct kretprobe_instance *inst;
2008 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08002009 void *addr;
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05302010
Naveen N. Rao659b9572017-07-07 22:37:24 +05302011 if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset))
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05302012 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002013
2014 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08002015 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09002016 if (IS_ERR(addr))
2017 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002018
2019 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2020 if (kretprobe_blacklist[i].addr == addr)
2021 return -EINVAL;
2022 }
2023 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07002024
2025 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07002026 rp->kp.post_handler = NULL;
2027 rp->kp.fault_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07002028
2029 /* Pre-allocate memory for max kretprobe instances */
2030 if (rp->maxactive <= 0) {
Thomas Gleixner92616602019-07-26 23:19:41 +02002031#ifdef CONFIG_PREEMPTION
Heiko Carstensc2ef6662009-12-21 13:02:24 +01002032 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07002033#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05302034 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07002035#endif
2036 }
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002037 raw_spin_lock_init(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002038 INIT_HLIST_HEAD(&rp->free_instances);
2039 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08002040 inst = kmalloc(sizeof(struct kretprobe_instance) +
2041 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002042 if (inst == NULL) {
2043 free_rp_inst(rp);
2044 return -ENOMEM;
2045 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002046 INIT_HLIST_NODE(&inst->hlist);
2047 hlist_add_head(&inst->hlist, &rp->free_instances);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002048 }
2049
2050 rp->nmissed = 0;
2051 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002052 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002053 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002054 free_rp_inst(rp);
2055 return ret;
2056}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002057EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002058
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002059int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002060{
2061 int ret = 0, i;
2062
2063 if (num <= 0)
2064 return -EINVAL;
2065 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002066 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07002067 if (ret < 0) {
2068 if (i > 0)
2069 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002070 break;
2071 }
2072 }
2073 return ret;
2074}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002075EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002076
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002077void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002078{
2079 unregister_kretprobes(&rp, 1);
2080}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002081EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002082
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002083void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002084{
2085 int i;
2086
2087 if (num <= 0)
2088 return;
2089 mutex_lock(&kprobe_mutex);
2090 for (i = 0; i < num; i++)
2091 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2092 rps[i]->kp.addr = NULL;
2093 mutex_unlock(&kprobe_mutex);
2094
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08002095 synchronize_rcu();
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002096 for (i = 0; i < num; i++) {
2097 if (rps[i]->kp.addr) {
2098 __unregister_kprobe_bottom(&rps[i]->kp);
2099 cleanup_rp_inst(rps[i]);
2100 }
2101 }
2102}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002103EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002104
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002105#else /* CONFIG_KRETPROBES */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002106int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002107{
2108 return -ENOSYS;
2109}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002110EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002111
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002112int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002113{
2114 return -ENOSYS;
2115}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002116EXPORT_SYMBOL_GPL(register_kretprobes);
2117
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002118void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002119{
2120}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002121EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002122
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002123void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002124{
2125}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002126EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002127
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002128static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002129{
2130 return 0;
2131}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002132NOKPROBE_SYMBOL(pre_handler_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002133
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002134#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002135
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002136/* Set the kprobe gone and remove its instruction buffer. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002137static void kill_kprobe(struct kprobe *p)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002138{
2139 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002140
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002141 lockdep_assert_held(&kprobe_mutex);
2142
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002143 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002144 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002145 /*
2146 * If this is an aggr_kprobe, we have to list all the
2147 * chained probes and mark them GONE.
2148 */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002149 list_for_each_entry(kp, &p->list, list)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002150 kp->flags |= KPROBE_FLAG_GONE;
2151 p->post_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002152 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002153 }
2154 /*
2155 * Here, we can remove insn_slot safely, because no thread calls
2156 * the original probed function (which will be freed soon) any more.
2157 */
2158 arch_remove_kprobe(p);
Muchun Song0cb2f132020-07-28 14:45:36 +08002159
2160 /*
2161 * The module is going away. We should disarm the kprobe which
Masami Hiramatsu30313132020-09-01 00:12:07 +09002162 * is using ftrace, because ftrace framework is still available at
2163 * MODULE_STATE_GOING notification.
Muchun Song0cb2f132020-07-28 14:45:36 +08002164 */
Masami Hiramatsu30313132020-09-01 00:12:07 +09002165 if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
Muchun Song0cb2f132020-07-28 14:45:36 +08002166 disarm_kprobe_ftrace(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002167}
2168
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002169/* Disable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002170int disable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002171{
2172 int ret = 0;
Jessica Yu297f9232018-01-10 00:51:24 +01002173 struct kprobe *p;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002174
2175 mutex_lock(&kprobe_mutex);
2176
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09002177 /* Disable this kprobe */
Jessica Yu297f9232018-01-10 00:51:24 +01002178 p = __disable_kprobe(kp);
2179 if (IS_ERR(p))
2180 ret = PTR_ERR(p);
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002181
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002182 mutex_unlock(&kprobe_mutex);
2183 return ret;
2184}
2185EXPORT_SYMBOL_GPL(disable_kprobe);
2186
2187/* Enable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002188int enable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002189{
2190 int ret = 0;
2191 struct kprobe *p;
2192
2193 mutex_lock(&kprobe_mutex);
2194
2195 /* Check whether specified probe is valid. */
2196 p = __get_valid_kprobe(kp);
2197 if (unlikely(p == NULL)) {
2198 ret = -EINVAL;
2199 goto out;
2200 }
2201
2202 if (kprobe_gone(kp)) {
2203 /* This kprobe has gone, we couldn't enable it. */
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207
2208 if (p != kp)
2209 kp->flags &= ~KPROBE_FLAG_DISABLED;
2210
2211 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2212 p->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01002213 ret = arm_kprobe(p);
2214 if (ret)
2215 p->flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002216 }
2217out:
2218 mutex_unlock(&kprobe_mutex);
2219 return ret;
2220}
2221EXPORT_SYMBOL_GPL(enable_kprobe);
2222
Masami Hiramatsu44585152018-04-28 21:36:33 +09002223/* Caller must NOT call this in usual path. This is only for critical case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002224void dump_kprobe(struct kprobe *kp)
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002225{
Masami Hiramatsu44585152018-04-28 21:36:33 +09002226 pr_err("Dumping kprobe:\n");
2227 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2228 kp->symbol_name, kp->offset, kp->addr);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002229}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002230NOKPROBE_SYMBOL(dump_kprobe);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002231
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002232int kprobe_add_ksym_blacklist(unsigned long entry)
2233{
2234 struct kprobe_blacklist_entry *ent;
2235 unsigned long offset = 0, size = 0;
2236
2237 if (!kernel_text_address(entry) ||
2238 !kallsyms_lookup_size_offset(entry, &size, &offset))
2239 return -EINVAL;
2240
2241 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2242 if (!ent)
2243 return -ENOMEM;
2244 ent->start_addr = entry;
2245 ent->end_addr = entry + size;
2246 INIT_LIST_HEAD(&ent->list);
2247 list_add_tail(&ent->list, &kprobe_blacklist);
2248
2249 return (int)size;
2250}
2251
2252/* Add all symbols in given area into kprobe blacklist */
2253int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2254{
2255 unsigned long entry;
2256 int ret = 0;
2257
2258 for (entry = start; entry < end; entry += ret) {
2259 ret = kprobe_add_ksym_blacklist(entry);
2260 if (ret < 0)
2261 return ret;
2262 if (ret == 0) /* In case of alias symbol */
2263 ret = 1;
2264 }
2265 return 0;
2266}
2267
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002268/* Remove all symbols in given area from kprobe blacklist */
2269static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2270{
2271 struct kprobe_blacklist_entry *ent, *n;
2272
2273 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2274 if (ent->start_addr < start || ent->start_addr >= end)
2275 continue;
2276 list_del(&ent->list);
2277 kfree(ent);
2278 }
2279}
2280
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002281static void kprobe_remove_ksym_blacklist(unsigned long entry)
2282{
2283 kprobe_remove_area_blacklist(entry, entry + 1);
2284}
2285
Adrian Hunterd002b8b2020-05-28 11:00:58 +03002286int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2287 char *type, char *sym)
2288{
2289 return -ERANGE;
2290}
2291
2292int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2293 char *sym)
2294{
2295#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2296 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2297 return 0;
2298#ifdef CONFIG_OPTPROBES
2299 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2300 return 0;
2301#endif
2302#endif
2303 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2304 return 0;
2305 return -ERANGE;
2306}
2307
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002308int __init __weak arch_populate_kprobe_blacklist(void)
2309{
2310 return 0;
2311}
2312
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002313/*
2314 * Lookup and populate the kprobe_blacklist.
2315 *
2316 * Unlike the kretprobe blacklist, we'll need to determine
2317 * the range of addresses that belong to the said functions,
2318 * since a kprobe need not necessarily be at the beginning
2319 * of a function.
2320 */
2321static int __init populate_kprobe_blacklist(unsigned long *start,
2322 unsigned long *end)
2323{
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002324 unsigned long entry;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002325 unsigned long *iter;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002326 int ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002327
2328 for (iter = start; iter < end; iter++) {
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002329 entry = arch_deref_entry_point((void *)*iter);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002330 ret = kprobe_add_ksym_blacklist(entry);
2331 if (ret == -EINVAL)
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002332 continue;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002333 if (ret < 0)
2334 return ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002335 }
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002336
2337 /* Symbols in __kprobes_text are blacklisted */
2338 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2339 (unsigned long)__kprobes_text_end);
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002340 if (ret)
2341 return ret;
2342
2343 /* Symbols in noinstr section are blacklisted */
2344 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2345 (unsigned long)__noinstr_text_end);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002346
2347 return ret ? : arch_populate_kprobe_blacklist();
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002348}
2349
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002350static void add_module_kprobe_blacklist(struct module *mod)
2351{
2352 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002353 int i;
2354
2355 if (mod->kprobe_blacklist) {
2356 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2357 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2358 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002359
2360 start = (unsigned long)mod->kprobes_text_start;
2361 if (start) {
2362 end = start + mod->kprobes_text_size;
2363 kprobe_add_area_blacklist(start, end);
2364 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002365
2366 start = (unsigned long)mod->noinstr_text_start;
2367 if (start) {
2368 end = start + mod->noinstr_text_size;
2369 kprobe_add_area_blacklist(start, end);
2370 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002371}
2372
2373static void remove_module_kprobe_blacklist(struct module *mod)
2374{
2375 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002376 int i;
2377
2378 if (mod->kprobe_blacklist) {
2379 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2380 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2381 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002382
2383 start = (unsigned long)mod->kprobes_text_start;
2384 if (start) {
2385 end = start + mod->kprobes_text_size;
2386 kprobe_remove_area_blacklist(start, end);
2387 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002388
2389 start = (unsigned long)mod->noinstr_text_start;
2390 if (start) {
2391 end = start + mod->noinstr_text_size;
2392 kprobe_remove_area_blacklist(start, end);
2393 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002394}
2395
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002396/* Module notifier call back, checking kprobes on the module */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002397static int kprobes_module_callback(struct notifier_block *nb,
2398 unsigned long val, void *data)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002399{
2400 struct module *mod = data;
2401 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002402 struct kprobe *p;
2403 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002404 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002405
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002406 if (val == MODULE_STATE_COMING) {
2407 mutex_lock(&kprobe_mutex);
2408 add_module_kprobe_blacklist(mod);
2409 mutex_unlock(&kprobe_mutex);
2410 }
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002411 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002412 return NOTIFY_DONE;
2413
2414 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002415 * When MODULE_STATE_GOING was notified, both of module .text and
2416 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2417 * notified, only .init.text section would be freed. We need to
2418 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002419 */
2420 mutex_lock(&kprobe_mutex);
2421 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2422 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002423 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002424 if (within_module_init((unsigned long)p->addr, mod) ||
2425 (checkcore &&
2426 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002427 /*
2428 * The vaddr this probe is installed will soon
2429 * be vfreed buy not synced to disk. Hence,
2430 * disarming the breakpoint isn't needed.
Steven Rostedt (VMware)545a0282017-05-16 14:58:35 -04002431 *
2432 * Note, this will also move any optimized probes
2433 * that are pending to be removed from their
2434 * corresponding lists to the freeing_list and
2435 * will not be touched by the delayed
2436 * kprobe_optimizer work handler.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002437 */
2438 kill_kprobe(p);
2439 }
2440 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002441 if (val == MODULE_STATE_GOING)
2442 remove_module_kprobe_blacklist(mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002443 mutex_unlock(&kprobe_mutex);
2444 return NOTIFY_DONE;
2445}
2446
2447static struct notifier_block kprobe_module_nb = {
2448 .notifier_call = kprobes_module_callback,
2449 .priority = 0
2450};
2451
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002452/* Markers of _kprobe_blacklist section */
2453extern unsigned long __start_kprobe_blacklist[];
2454extern unsigned long __stop_kprobe_blacklist[];
2455
Masami Hiramatsu82d083a2020-09-10 17:55:05 +09002456void kprobe_free_init_mem(void)
2457{
2458 void *start = (void *)(&__init_begin);
2459 void *end = (void *)(&__init_end);
2460 struct hlist_head *head;
2461 struct kprobe *p;
2462 int i;
2463
2464 mutex_lock(&kprobe_mutex);
2465
2466 /* Kill all kprobes on initmem */
2467 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2468 head = &kprobe_table[i];
2469 hlist_for_each_entry(p, head, hlist) {
2470 if (start <= (void *)p->addr && (void *)p->addr < end)
2471 kill_kprobe(p);
2472 }
2473 }
2474
2475 mutex_unlock(&kprobe_mutex);
2476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478static int __init init_kprobes(void)
2479{
2480 int i, err = 0;
2481
2482 /* FIXME allocate the probe table, currently defined statically */
2483 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002484 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002486 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002487 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
Hien Nguyenb94cce92005-06-23 00:09:19 -07002488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002490 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2491 __stop_kprobe_blacklist);
2492 if (err) {
2493 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2494 pr_err("Please take care of using kprobes.\n");
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002495 }
2496
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002497 if (kretprobe_blacklist_size) {
2498 /* lookup the function address from its name */
2499 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05302500 kretprobe_blacklist[i].addr =
Naveen N. Rao290e3072017-04-19 18:21:01 +05302501 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002502 if (!kretprobe_blacklist[i].addr)
2503 printk("kretprobe: lookup failed: %s\n",
2504 kretprobe_blacklist[i].name);
2505 }
2506 }
2507
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002508#if defined(CONFIG_OPTPROBES)
2509#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002510 /* Init kprobe_optinsn_slots */
2511 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2512#endif
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002513 /* By default, kprobes can be optimized */
2514 kprobes_allow_optimization = true;
2515#endif
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002516
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002517 /* By default, kprobes are armed */
2518 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002519
Rusty Lynch67729262005-07-05 18:54:50 -07002520 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002521 if (!err)
2522 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002523 if (!err)
2524 err = register_module_notifier(&kprobe_module_nb);
2525
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002526 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07002527
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01002528 if (!err)
2529 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return err;
2531}
Masami Hiramatsu65fc9652019-06-03 22:04:42 +09002532subsys_initcall(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Srinivasa Ds346fd592007-02-20 13:57:54 -08002534#ifdef CONFIG_DEBUG_FS
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002535static void report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002536 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002537{
2538 char *kprobe_type;
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002539 void *addr = p->addr;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002540
2541 if (p->pre_handler == pre_handler_kretprobe)
2542 kprobe_type = "r";
Srinivasa Ds346fd592007-02-20 13:57:54 -08002543 else
2544 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002545
Kees Cook60f7bb62020-07-02 15:20:22 -07002546 if (!kallsyms_show_value(pi->file->f_cred))
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002547 addr = NULL;
2548
Srinivasa Ds346fd592007-02-20 13:57:54 -08002549 if (sym)
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002550 seq_printf(pi, "%px %s %s+0x%x %s ",
2551 addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002552 (modname ? modname : " "));
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002553 else /* try to use %pS */
2554 seq_printf(pi, "%px %s %pS ",
2555 addr, kprobe_type, p->addr);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002556
2557 if (!pp)
2558 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002559 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002560 (kprobe_gone(p) ? "[GONE]" : ""),
2561 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002562 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2563 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002564}
2565
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002566static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002567{
2568 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2569}
2570
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002571static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002572{
2573 (*pos)++;
2574 if (*pos >= KPROBE_TABLE_SIZE)
2575 return NULL;
2576 return pos;
2577}
2578
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002579static void kprobe_seq_stop(struct seq_file *f, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002580{
2581 /* Nothing to do */
2582}
2583
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002584static int show_kprobe_addr(struct seq_file *pi, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002585{
2586 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002587 struct kprobe *p, *kp;
2588 const char *sym = NULL;
2589 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002590 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002591 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002592
2593 head = &kprobe_table[i];
2594 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002595 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002596 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002597 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002598 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002599 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002600 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002601 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002602 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002603 }
2604 preempt_enable();
2605 return 0;
2606}
2607
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002608static const struct seq_operations kprobes_sops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002609 .start = kprobe_seq_start,
2610 .next = kprobe_seq_next,
2611 .stop = kprobe_seq_stop,
2612 .show = show_kprobe_addr
2613};
2614
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002615DEFINE_SEQ_ATTRIBUTE(kprobes);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002616
Masami Hiramatsu63724742014-04-17 17:18:49 +09002617/* kprobes/blacklist -- shows which functions can not be probed */
2618static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2619{
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002620 mutex_lock(&kprobe_mutex);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002621 return seq_list_start(&kprobe_blacklist, *pos);
2622}
2623
2624static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2625{
2626 return seq_list_next(v, &kprobe_blacklist, pos);
2627}
2628
2629static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2630{
2631 struct kprobe_blacklist_entry *ent =
2632 list_entry(v, struct kprobe_blacklist_entry, list);
2633
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002634 /*
2635 * If /proc/kallsyms is not showing kernel address, we won't
2636 * show them here either.
2637 */
Kees Cook60f7bb62020-07-02 15:20:22 -07002638 if (!kallsyms_show_value(m->file->f_cred))
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002639 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2640 (void *)ent->start_addr);
2641 else
2642 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2643 (void *)ent->end_addr, (void *)ent->start_addr);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002644 return 0;
2645}
2646
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002647static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2648{
2649 mutex_unlock(&kprobe_mutex);
2650}
2651
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002652static const struct seq_operations kprobe_blacklist_sops = {
Masami Hiramatsu63724742014-04-17 17:18:49 +09002653 .start = kprobe_blacklist_seq_start,
2654 .next = kprobe_blacklist_seq_next,
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002655 .stop = kprobe_blacklist_seq_stop,
Masami Hiramatsu63724742014-04-17 17:18:49 +09002656 .show = kprobe_blacklist_seq_show,
2657};
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002658DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002659
Jessica Yu12310e342018-01-10 00:51:23 +01002660static int arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002661{
2662 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002663 struct kprobe *p;
Jessica Yu12310e342018-01-10 00:51:23 +01002664 unsigned int i, total = 0, errors = 0;
2665 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002666
2667 mutex_lock(&kprobe_mutex);
2668
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002669 /* If kprobes are armed, just return */
2670 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002671 goto already_enabled;
2672
Wang Nan977ad482015-02-13 14:40:24 -08002673 /*
2674 * optimize_kprobe() called by arm_kprobe() checks
2675 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2676 * arm_kprobe.
2677 */
2678 kprobes_all_disarmed = false;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002679 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002680 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2681 head = &kprobe_table[i];
Jessica Yu12310e342018-01-10 00:51:23 +01002682 /* Arm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002683 hlist_for_each_entry(p, head, hlist) {
Jessica Yu12310e342018-01-10 00:51:23 +01002684 if (!kprobe_disabled(p)) {
2685 err = arm_kprobe(p);
2686 if (err) {
2687 errors++;
2688 ret = err;
2689 }
2690 total++;
2691 }
2692 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002693 }
2694
Jessica Yu12310e342018-01-10 00:51:23 +01002695 if (errors)
2696 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2697 errors, total);
2698 else
2699 pr_info("Kprobes globally enabled\n");
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002700
2701already_enabled:
2702 mutex_unlock(&kprobe_mutex);
Jessica Yu12310e342018-01-10 00:51:23 +01002703 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002704}
2705
Jessica Yu297f9232018-01-10 00:51:24 +01002706static int disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002707{
2708 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002709 struct kprobe *p;
Jessica Yu297f9232018-01-10 00:51:24 +01002710 unsigned int i, total = 0, errors = 0;
2711 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002712
2713 mutex_lock(&kprobe_mutex);
2714
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002715 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002716 if (kprobes_all_disarmed) {
2717 mutex_unlock(&kprobe_mutex);
Jessica Yu297f9232018-01-10 00:51:24 +01002718 return 0;
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002719 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002720
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002721 kprobes_all_disarmed = true;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002722
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002723 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2724 head = &kprobe_table[i];
Jessica Yu297f9232018-01-10 00:51:24 +01002725 /* Disarm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002726 hlist_for_each_entry(p, head, hlist) {
Jessica Yu297f9232018-01-10 00:51:24 +01002727 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2728 err = disarm_kprobe(p, false);
2729 if (err) {
2730 errors++;
2731 ret = err;
2732 }
2733 total++;
2734 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002735 }
2736 }
Jessica Yu297f9232018-01-10 00:51:24 +01002737
2738 if (errors)
2739 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2740 errors, total);
2741 else
2742 pr_info("Kprobes globally disabled\n");
2743
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002744 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002745
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002746 /* Wait for disarming all kprobes by optimizer */
2747 wait_for_kprobe_optimizer();
Jessica Yu297f9232018-01-10 00:51:24 +01002748
2749 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002750}
2751
2752/*
2753 * XXX: The debugfs bool file interface doesn't allow for callbacks
2754 * when the bool state is switched. We can reuse that facility when
2755 * available
2756 */
2757static ssize_t read_enabled_file_bool(struct file *file,
2758 char __user *user_buf, size_t count, loff_t *ppos)
2759{
2760 char buf[3];
2761
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002762 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002763 buf[0] = '1';
2764 else
2765 buf[0] = '0';
2766 buf[1] = '\n';
2767 buf[2] = 0x00;
2768 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2769}
2770
2771static ssize_t write_enabled_file_bool(struct file *file,
2772 const char __user *user_buf, size_t count, loff_t *ppos)
2773{
2774 char buf[32];
Stephen Boydefeb1562012-01-12 17:17:11 -08002775 size_t buf_size;
Jessica Yu12310e342018-01-10 00:51:23 +01002776 int ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002777
2778 buf_size = min(count, (sizeof(buf)-1));
2779 if (copy_from_user(buf, user_buf, buf_size))
2780 return -EFAULT;
2781
Mathias Krause10fb46d2013-07-03 15:05:39 -07002782 buf[buf_size] = '\0';
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002783 switch (buf[0]) {
2784 case 'y':
2785 case 'Y':
2786 case '1':
Jessica Yu12310e342018-01-10 00:51:23 +01002787 ret = arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002788 break;
2789 case 'n':
2790 case 'N':
2791 case '0':
Jessica Yu297f9232018-01-10 00:51:24 +01002792 ret = disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002793 break;
Mathias Krause10fb46d2013-07-03 15:05:39 -07002794 default:
2795 return -EINVAL;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002796 }
2797
Jessica Yu12310e342018-01-10 00:51:23 +01002798 if (ret)
2799 return ret;
2800
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002801 return count;
2802}
2803
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002804static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002805 .read = read_enabled_file_bool,
2806 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002807 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002808};
2809
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002810static int __init debugfs_kprobe_init(void)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002811{
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002812 struct dentry *dir;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002813 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002814
2815 dir = debugfs_create_dir("kprobes", NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002816
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002817 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002818
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002819 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002820
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002821 debugfs_create_file("blacklist", 0400, dir, NULL,
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002822 &kprobe_blacklist_fops);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002823
Srinivasa Ds346fd592007-02-20 13:57:54 -08002824 return 0;
2825}
2826
2827late_initcall(debugfs_kprobe_init);
2828#endif /* CONFIG_DEBUG_FS */