blob: 789002da7766cb14bfb1c9ae1cd7f2a0b3f4662e [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>
Peter Zijlstra6333e8f2020-08-18 15:57:43 +020039#include <linux/static_call.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070040
Christoph Hellwigbfd45be2016-10-11 13:52:22 -070041#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/cacheflush.h>
43#include <asm/errno.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080044#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define KPROBE_HASH_BITS 6
47#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
48
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070049
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070050static int kprobes_initialized;
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +090051/* kprobe_table can be accessed by
52 * - Normal hlist traversal and RCU add/del under kprobe_mutex is held.
53 * Or
54 * - RCU hlist traversal under disabling preempt (breakpoint handlers)
55 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070057static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070059/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070060static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070061
Masami Hiramatsu43948f52010-10-25 22:18:01 +090062/* This protects kprobe_table and optimizing_list */
63static DEFINE_MUTEX(kprobe_mutex);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080064static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070065static struct {
Thomas Gleixnerec4846082009-07-25 16:09:17 +020066 raw_spinlock_t lock ____cacheline_aligned_in_smp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070067} kretprobe_table_locks[KPROBE_TABLE_SIZE];
68
Naveen N. Rao290e3072017-04-19 18:21:01 +053069kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
70 unsigned int __unused)
Naveen N. Rao49e0b462017-04-19 18:21:00 +053071{
72 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
73}
74
Thomas Gleixnerec4846082009-07-25 16:09:17 +020075static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070076{
77 return &(kretprobe_table_locks[hash].lock);
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Masami Hiramatsu376e2422014-04-17 17:17:05 +090080/* Blacklist -- list of struct kprobe_blacklist_entry */
81static LIST_HEAD(kprobe_blacklist);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070082
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080083#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070084/*
85 * kprobe->ainsn.insn points to the copy of the instruction to be
86 * single-stepped. x86_64, POWER4 and above have no-exec support and
87 * stepping on the instruction on a vmalloced/kmalloced/data page
88 * is a recipe for disaster
89 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070090struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -040091 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070092 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -070093 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070094 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080095 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050096 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070097};
98
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050099#define KPROBE_INSN_PAGE_SIZE(slots) \
100 (offsetof(struct kprobe_insn_page, slot_used) + \
101 (sizeof(char) * (slots)))
102
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500103static int slots_per_page(struct kprobe_insn_cache *c)
104{
105 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
106}
107
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800108enum kprobe_slot_state {
109 SLOT_CLEAN = 0,
110 SLOT_DIRTY = 1,
111 SLOT_USED = 2,
112};
113
Masami Hiramatsu63fef142017-08-18 17:24:00 +0900114void __weak *alloc_insn_page(void)
Heiko Carstensaf963972013-09-11 14:24:13 -0700115{
116 return module_alloc(PAGE_SIZE);
117}
118
Masami Hiramatsuc93f5cf2017-05-25 19:38:17 +0900119void __weak free_insn_page(void *page)
Heiko Carstensaf963972013-09-11 14:24:13 -0700120{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030121 module_memfree(page);
Heiko Carstensaf963972013-09-11 14:24:13 -0700122}
123
Heiko Carstensc802d642013-09-11 14:24:11 -0700124struct kprobe_insn_cache kprobe_insn_slots = {
125 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700126 .alloc = alloc_insn_page,
127 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300128 .sym = KPROBE_INSN_PAGE_SYM,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500129 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
130 .insn_size = MAX_INSN_SIZE,
131 .nr_garbage = 0,
132};
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900133static int collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800134
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700135/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800136 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700137 * We allocate an executable page if there's no room on existing ones.
138 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900139kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700140{
141 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700142 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700143
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900144 /* Since the slot array is not protected by rcu, we need a mutex */
Heiko Carstensc802d642013-09-11 14:24:11 -0700145 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700146 retry:
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900147 rcu_read_lock();
148 list_for_each_entry_rcu(kip, &c->pages, list) {
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500149 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700150 int i;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500151 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800152 if (kip->slot_used[i] == SLOT_CLEAN) {
153 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700154 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700155 slot = kip->insns + (i * c->insn_size);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900156 rcu_read_unlock();
Heiko Carstensc802d642013-09-11 14:24:11 -0700157 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700158 }
159 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500160 /* kip->nused is broken. Fix it. */
161 kip->nused = slots_per_page(c);
162 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700163 }
164 }
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900165 rcu_read_unlock();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700166
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800167 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500168 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800169 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500170
171 /* All out of space. Need to allocate a new page. */
172 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700173 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700174 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700175
176 /*
177 * Use module_alloc so this page is within +/- 2GB of where the
178 * kernel image and loaded module images reside. This is required
179 * so x86_64 can correctly handle the %rip-relative fixups.
180 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700181 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700182 if (!kip->insns) {
183 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700184 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700185 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400186 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500187 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800188 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700189 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800190 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700191 kip->cache = c;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900192 list_add_rcu(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700193 slot = kip->insns;
Adrian Hunter69e49082020-05-12 15:19:11 +0300194
195 /* Record the perf ksymbol register event after adding the page */
196 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns,
197 PAGE_SIZE, false, c->sym);
Heiko Carstensc802d642013-09-11 14:24:11 -0700198out:
199 mutex_unlock(&c->mutex);
200 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800201}
202
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800203/* Return 1 if all garbages are collected, otherwise 0. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900204static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800205{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800206 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800207 kip->nused--;
208 if (kip->nused == 0) {
209 /*
210 * Page is no longer in use. Free it unless
211 * it's the last one. We keep the last one
212 * so as not to have to set it up again the
213 * next time somebody inserts a probe.
214 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500215 if (!list_is_singular(&kip->list)) {
Adrian Hunter69e49082020-05-12 15:19:11 +0300216 /*
217 * Record perf ksymbol unregister event before removing
218 * the page.
219 */
220 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
221 (unsigned long)kip->insns, PAGE_SIZE, true,
222 kip->cache->sym);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900223 list_del_rcu(&kip->list);
224 synchronize_rcu();
Heiko Carstensaf963972013-09-11 14:24:13 -0700225 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800226 kfree(kip);
227 }
228 return 1;
229 }
230 return 0;
231}
232
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900233static int collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800234{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400235 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800236
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500237 /* Ensure no-one is interrupted on the garbages */
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -0800238 synchronize_rcu();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800239
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500240 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800241 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800242 if (kip->ngarbage == 0)
243 continue;
244 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500245 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900246 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800247 break;
248 }
249 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500250 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800251 return 0;
252}
253
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900254void __free_insn_slot(struct kprobe_insn_cache *c,
255 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500256{
257 struct kprobe_insn_page *kip;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900258 long idx;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500259
Heiko Carstensc802d642013-09-11 14:24:11 -0700260 mutex_lock(&c->mutex);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900261 rcu_read_lock();
262 list_for_each_entry_rcu(kip, &c->pages, list) {
263 idx = ((long)slot - (long)kip->insns) /
264 (c->insn_size * sizeof(kprobe_opcode_t));
265 if (idx >= 0 && idx < slots_per_page(c))
Heiko Carstensc802d642013-09-11 14:24:11 -0700266 goto out;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900267 }
268 /* Could not find this slot. */
269 WARN_ON(1);
270 kip = NULL;
271out:
272 rcu_read_unlock();
273 /* Mark and sweep: this may sleep */
274 if (kip) {
275 /* Check double free */
276 WARN_ON(kip->slot_used[idx] != SLOT_USED);
277 if (dirty) {
278 kip->slot_used[idx] = SLOT_DIRTY;
279 kip->ngarbage++;
280 if (++c->nr_garbage > slots_per_page(c))
281 collect_garbage_slots(c);
282 } else {
283 collect_one_slot(kip, idx);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500284 }
285 }
Heiko Carstensc802d642013-09-11 14:24:11 -0700286 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500287}
288
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900289/*
290 * Check given address is on the page of kprobe instruction slots.
291 * This will be used for checking whether the address on a stack
292 * is on a text area or not.
293 */
294bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
295{
296 struct kprobe_insn_page *kip;
297 bool ret = false;
298
299 rcu_read_lock();
300 list_for_each_entry_rcu(kip, &c->pages, list) {
301 if (addr >= (unsigned long)kip->insns &&
302 addr < (unsigned long)kip->insns + PAGE_SIZE) {
303 ret = true;
304 break;
305 }
306 }
307 rcu_read_unlock();
308
309 return ret;
310}
311
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300312int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
313 unsigned long *value, char *type, char *sym)
314{
315 struct kprobe_insn_page *kip;
316 int ret = -ERANGE;
317
318 rcu_read_lock();
319 list_for_each_entry_rcu(kip, &c->pages, list) {
320 if ((*symnum)--)
321 continue;
322 strlcpy(sym, c->sym, KSYM_NAME_LEN);
323 *type = 't';
324 *value = (unsigned long)kip->insns;
325 ret = 0;
326 break;
327 }
328 rcu_read_unlock();
329
330 return ret;
331}
332
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500333#ifdef CONFIG_OPTPROBES
334/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700335struct kprobe_insn_cache kprobe_optinsn_slots = {
336 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700337 .alloc = alloc_insn_page,
338 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300339 .sym = KPROBE_OPTINSN_PAGE_SYM,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500340 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
341 /* .insn_size is initialized later */
342 .nr_garbage = 0,
343};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500344#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800345#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700346
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800347/* We have preemption disabled.. so it is safe to use __ versions */
348static inline void set_kprobe_instance(struct kprobe *kp)
349{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600350 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800351}
352
353static inline void reset_kprobe_instance(void)
354{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600355 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800356}
357
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800358/*
359 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800360 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800361 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800362 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800363 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900364struct kprobe *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800367 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Masami Hiramatsu6743ad42020-05-12 17:02:33 +0900370 hlist_for_each_entry_rcu(p, head, hlist,
371 lockdep_is_held(&kprobe_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (p->addr == addr)
373 return p;
374 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return NULL;
377}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900378NOKPROBE_SYMBOL(get_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900380static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500381
382/* Return true if the kprobe is an aggregator */
383static inline int kprobe_aggrprobe(struct kprobe *p)
384{
385 return p->pre_handler == aggr_pre_handler;
386}
387
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900388/* Return true(!0) if the kprobe is unused */
389static inline int kprobe_unused(struct kprobe *p)
390{
391 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
392 list_empty(&p->list);
393}
394
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500395/*
396 * Keep all fields in the kprobe consistent
397 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900398static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500399{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900400 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
401 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500402}
403
404#ifdef CONFIG_OPTPROBES
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500405/* NOTE: change this value only with kprobe_mutex held */
406static bool kprobes_allow_optimization;
407
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500408/*
409 * Call all pre_handler on the list, but ignores its return value.
410 * This must be called from arch-dep optimized caller.
411 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900412void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500413{
414 struct kprobe *kp;
415
416 list_for_each_entry_rcu(kp, &p->list, list) {
417 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
418 set_kprobe_instance(kp);
Naveen N. Rao4f3a8712017-10-17 13:48:34 +0530419 kp->pre_handler(kp, regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500420 }
421 reset_kprobe_instance();
422 }
423}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900424NOKPROBE_SYMBOL(opt_pre_handler);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500425
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900426/* Free optimized instructions and optimized_kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900427static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900428{
429 struct optimized_kprobe *op;
430
431 op = container_of(p, struct optimized_kprobe, kp);
432 arch_remove_optimized_kprobe(op);
433 arch_remove_kprobe(p);
434 kfree(op);
435}
436
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500437/* Return true(!0) if the kprobe is ready for optimization. */
438static inline int kprobe_optready(struct kprobe *p)
439{
440 struct optimized_kprobe *op;
441
442 if (kprobe_aggrprobe(p)) {
443 op = container_of(p, struct optimized_kprobe, kp);
444 return arch_prepared_optinsn(&op->optinsn);
445 }
446
447 return 0;
448}
449
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900450/* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
451static inline int kprobe_disarmed(struct kprobe *p)
452{
453 struct optimized_kprobe *op;
454
455 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
456 if (!kprobe_aggrprobe(p))
457 return kprobe_disabled(p);
458
459 op = container_of(p, struct optimized_kprobe, kp);
460
461 return kprobe_disabled(p) && list_empty(&op->list);
462}
463
464/* Return true(!0) if the probe is queued on (un)optimizing lists */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900465static int kprobe_queued(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900466{
467 struct optimized_kprobe *op;
468
469 if (kprobe_aggrprobe(p)) {
470 op = container_of(p, struct optimized_kprobe, kp);
471 if (!list_empty(&op->list))
472 return 1;
473 }
474 return 0;
475}
476
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500477/*
478 * Return an optimized kprobe whose optimizing code replaces
479 * instructions including addr (exclude breakpoint).
480 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900481static struct kprobe *get_optimized_kprobe(unsigned long addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500482{
483 int i;
484 struct kprobe *p = NULL;
485 struct optimized_kprobe *op;
486
487 /* Don't check i == 0, since that is a breakpoint case. */
488 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
489 p = get_kprobe((void *)(addr - i));
490
491 if (p && kprobe_optready(p)) {
492 op = container_of(p, struct optimized_kprobe, kp);
493 if (arch_within_optimized_kprobe(op, addr))
494 return p;
495 }
496
497 return NULL;
498}
499
500/* Optimization staging list, protected by kprobe_mutex */
501static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900502static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900503static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500504
505static void kprobe_optimizer(struct work_struct *work);
506static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
507#define OPTIMIZE_DELAY 5
508
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900509/*
510 * Optimize (replace a breakpoint with a jump) kprobes listed on
511 * optimizing_list.
512 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900513static void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500514{
Andrea Righif1c6ece2019-08-12 20:43:02 +0200515 lockdep_assert_held(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500516 /*
517 * The optimization/unoptimization refers online_cpus via
518 * stop_machine() and cpu-hotplug modifies online_cpus.
519 * And same time, text_mutex will be held in cpu-hotplug and here.
520 * This combination can cause a deadlock (cpu-hotplug try to lock
521 * text_mutex but stop_machine can not be done because online_cpus
522 * has been changed)
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200523 * To avoid this deadlock, caller must have locked cpu hotplug
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500524 * for preventing cpu-hotplug outside of text_mutex locking.
525 */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200526 lockdep_assert_cpus_held();
527
528 /* Optimization never be done when disarmed */
529 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
530 list_empty(&optimizing_list))
531 return;
532
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900533 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900534}
535
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900536/*
537 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
538 * if need) kprobes listed on unoptimizing_list.
539 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900540static void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900541{
542 struct optimized_kprobe *op, *tmp;
543
Andrea Righif1c6ece2019-08-12 20:43:02 +0200544 lockdep_assert_held(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200545 /* See comment in do_optimize_kprobes() */
546 lockdep_assert_cpus_held();
547
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900548 /* Unoptimization must be done anytime */
549 if (list_empty(&unoptimizing_list))
550 return;
551
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900552 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900553 /* Loop free_list for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900554 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900555 /* Switching from detour code to origin */
556 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900557 /* Disarm probes if marked disabled */
558 if (kprobe_disabled(&op->kp))
559 arch_disarm_kprobe(&op->kp);
560 if (kprobe_unused(&op->kp)) {
561 /*
562 * Remove unused probes from hash list. After waiting
563 * for synchronization, these probes are reclaimed.
564 * (reclaiming is done by do_free_cleaned_kprobes.)
565 */
566 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900567 } else
568 list_del_init(&op->list);
569 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900570}
571
572/* Reclaim all kprobes on the free_list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900573static void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900574{
575 struct optimized_kprobe *op, *tmp;
576
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900577 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900578 list_del_init(&op->list);
Masami Hiramatsucbdd96f2018-09-11 19:21:09 +0900579 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
580 /*
581 * This must not happen, but if there is a kprobe
582 * still in use, keep it on kprobes hash list.
583 */
584 continue;
585 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900586 free_aggr_kprobe(&op->kp);
587 }
588}
589
590/* Start optimizer after OPTIMIZE_DELAY passed */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900591static void kick_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900592{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800593 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900594}
595
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900596/* Kprobe jump optimizer */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900597static void kprobe_optimizer(struct work_struct *work)
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900598{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900599 mutex_lock(&kprobe_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200600 cpus_read_lock();
Andrea Righif1c6ece2019-08-12 20:43:02 +0200601 mutex_lock(&text_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900602
603 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900604 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
605 * kprobes before waiting for quiesence period.
606 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900607 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900608
609 /*
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900610 * Step 2: Wait for quiesence period to ensure all potentially
611 * preempted tasks to have normally scheduled. Because optprobe
612 * may modify multiple instructions, there is a chance that Nth
613 * instruction is preempted. In that case, such tasks can return
614 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
615 * Note that on non-preemptive kernel, this is transparently converted
616 * to synchronoze_sched() to wait for all interrupts to have completed.
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900617 */
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900618 synchronize_rcu_tasks();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900619
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900620 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900621 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900622
623 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900624 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900625
Andrea Righif1c6ece2019-08-12 20:43:02 +0200626 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200627 cpus_read_unlock();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900628
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900629 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900630 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900631 kick_kprobe_optimizer();
Masami Hiramatsu1a0aa992020-05-12 17:02:56 +0900632
633 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900634}
635
636/* Wait for completing optimization and unoptimization */
Thomas Gleixner30e7d8942017-05-17 10:19:49 +0200637void wait_for_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900638{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800639 mutex_lock(&kprobe_mutex);
640
641 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
642 mutex_unlock(&kprobe_mutex);
643
644 /* this will also make optimizing_work execute immmediately */
645 flush_delayed_work(&optimizing_work);
646 /* @optimizing_work might not have been queued yet, relax */
647 cpu_relax();
648
649 mutex_lock(&kprobe_mutex);
650 }
651
652 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500653}
654
Masami Hiramatsue4add242020-01-07 23:42:24 +0900655static bool optprobe_queued_unopt(struct optimized_kprobe *op)
656{
657 struct optimized_kprobe *_op;
658
659 list_for_each_entry(_op, &unoptimizing_list, list) {
660 if (op == _op)
661 return true;
662 }
663
664 return false;
665}
666
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500667/* Optimize kprobe if p is ready to be optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900668static void optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500669{
670 struct optimized_kprobe *op;
671
672 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500673 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500674 (kprobe_disabled(p) || kprobes_all_disarmed))
675 return;
676
Masami Hiramatsu059053a2018-06-20 01:10:27 +0900677 /* kprobes with post_handler can not be optimized */
678 if (p->post_handler)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500679 return;
680
681 op = container_of(p, struct optimized_kprobe, kp);
682
683 /* Check there is no other kprobes at the optimized instructions */
684 if (arch_check_optimized_kprobe(op) < 0)
685 return;
686
687 /* Check if it is already optimized. */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900688 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
689 if (optprobe_queued_unopt(op)) {
690 /* This is under unoptimizing. Just dequeue the probe */
691 list_del_init(&op->list);
692 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500693 return;
Masami Hiramatsue4add242020-01-07 23:42:24 +0900694 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500695 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900696
Masami Hiramatsue4add242020-01-07 23:42:24 +0900697 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
698 if (WARN_ON_ONCE(!list_empty(&op->list)))
699 return;
700
701 list_add(&op->list, &optimizing_list);
702 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900703}
704
705/* Short cut to direct unoptimizing */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900706static void force_unoptimize_kprobe(struct optimized_kprobe *op)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900707{
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200708 lockdep_assert_cpus_held();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900709 arch_unoptimize_kprobe(op);
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900710 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500711}
712
713/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900714static void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500715{
716 struct optimized_kprobe *op;
717
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900718 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
719 return; /* This is not an optprobe nor optimized */
720
721 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900722 if (!kprobe_optimized(p))
723 return;
724
725 if (!list_empty(&op->list)) {
726 if (optprobe_queued_unopt(op)) {
727 /* Queued in unoptimizing queue */
728 if (force) {
729 /*
730 * Forcibly unoptimize the kprobe here, and queue it
731 * in the freeing list for release afterwards.
732 */
733 force_unoptimize_kprobe(op);
734 list_move(&op->list, &freeing_list);
735 }
736 } else {
737 /* Dequeue from the optimizing queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500738 list_del_init(&op->list);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900739 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900740 }
741 return;
742 }
743
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900744 /* Optimized kprobe case */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900745 if (force) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900746 /* Forcibly update the code: this is a special case */
747 force_unoptimize_kprobe(op);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900748 } else {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900749 list_add(&op->list, &unoptimizing_list);
750 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500751 }
752}
753
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900754/* Cancel unoptimizing for reusing */
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900755static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900756{
757 struct optimized_kprobe *op;
758
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900759 /*
760 * Unused kprobe MUST be on the way of delayed unoptimizing (means
761 * there is still a relative jump) and disabled.
762 */
763 op = container_of(ap, struct optimized_kprobe, kp);
Masami Hiramatsu44585152018-04-28 21:36:33 +0900764 WARN_ON_ONCE(list_empty(&op->list));
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900765 /* Enable the probe again */
766 ap->flags &= ~KPROBE_FLAG_DISABLED;
767 /* Optimize it again (remove from op->list) */
Masami Hiramatsu5f843ed2019-04-15 15:01:25 +0900768 if (!kprobe_optready(ap))
769 return -EINVAL;
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900770
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900771 optimize_kprobe(ap);
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900772 return 0;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900773}
774
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500775/* Remove optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900776static void kill_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500777{
778 struct optimized_kprobe *op;
779
780 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900781 if (!list_empty(&op->list))
782 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500783 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900784 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900785
786 if (kprobe_unused(p)) {
787 /* Enqueue if it is unused */
788 list_add(&op->list, &freeing_list);
789 /*
790 * Remove unused probes from the hash list. After waiting
791 * for synchronization, this probe is reclaimed.
792 * (reclaiming is done by do_free_cleaned_kprobes().)
793 */
794 hlist_del_rcu(&op->kp.hlist);
795 }
796
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900797 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500798 arch_remove_optimized_kprobe(op);
799}
800
Masami Hiramatsua4602462017-04-19 18:22:25 +0530801static inline
802void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
803{
804 if (!kprobe_ftrace(p))
805 arch_prepare_optimized_kprobe(op, p);
806}
807
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500808/* Try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900809static void prepare_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500810{
811 struct optimized_kprobe *op;
812
813 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsua4602462017-04-19 18:22:25 +0530814 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500815}
816
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500817/* Allocate new optimized_kprobe and try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900818static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500819{
820 struct optimized_kprobe *op;
821
822 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
823 if (!op)
824 return NULL;
825
826 INIT_LIST_HEAD(&op->list);
827 op->kp.addr = p->addr;
Masami Hiramatsua4602462017-04-19 18:22:25 +0530828 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500829
830 return &op->kp;
831}
832
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900833static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500834
835/*
836 * Prepare an optimized_kprobe and optimize it
837 * NOTE: p must be a normal registered kprobe
838 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900839static void try_to_optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500840{
841 struct kprobe *ap;
842 struct optimized_kprobe *op;
843
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900844 /* Impossible to optimize ftrace-based kprobe */
845 if (kprobe_ftrace(p))
846 return;
847
Masami Hiramatsu25764282012-06-05 19:28:26 +0900848 /* For preparing optimization, jump_label_text_reserved() is called */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200849 cpus_read_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +0900850 jump_label_lock();
851 mutex_lock(&text_mutex);
852
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500853 ap = alloc_aggr_kprobe(p);
854 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900855 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500856
857 op = container_of(ap, struct optimized_kprobe, kp);
858 if (!arch_prepared_optinsn(&op->optinsn)) {
859 /* If failed to setup optimizing, fallback to kprobe */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900860 arch_remove_optimized_kprobe(op);
861 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900862 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500863 }
864
865 init_aggr_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900866 optimize_kprobe(ap); /* This just kicks optimizer thread */
867
868out:
869 mutex_unlock(&text_mutex);
870 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200871 cpus_read_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500872}
873
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500874#ifdef CONFIG_SYSCTL
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900875static void optimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500876{
877 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500878 struct kprobe *p;
879 unsigned int i;
880
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900881 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500882 /* If optimization is already allowed, just return */
883 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900884 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500885
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200886 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500887 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500888 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
889 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900890 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500891 if (!kprobe_disabled(p))
892 optimize_kprobe(p);
893 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200894 cpus_read_unlock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500895 printk(KERN_INFO "Kprobes globally optimized\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900896out:
897 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500898}
899
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900900static void unoptimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500901{
902 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500903 struct kprobe *p;
904 unsigned int i;
905
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900906 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500907 /* If optimization is already prohibited, just return */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900908 if (!kprobes_allow_optimization) {
909 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500910 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900911 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500912
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200913 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500914 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500915 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
916 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900917 hlist_for_each_entry(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500918 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900919 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500920 }
921 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200922 cpus_read_unlock();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900923 mutex_unlock(&kprobe_mutex);
924
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900925 /* Wait for unoptimizing completion */
926 wait_for_kprobe_optimizer();
927 printk(KERN_INFO "Kprobes globally unoptimized\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500928}
929
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900930static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500931int sysctl_kprobes_optimization;
932int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200933 void *buffer, size_t *length,
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500934 loff_t *ppos)
935{
936 int ret;
937
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900938 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500939 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
940 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
941
942 if (sysctl_kprobes_optimization)
943 optimize_all_kprobes();
944 else
945 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900946 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500947
948 return ret;
949}
950#endif /* CONFIG_SYSCTL */
951
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900952/* Put a breakpoint for a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900953static void __arm_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500954{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900955 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500956
957 /* Check collision with other optimized kprobes */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900958 _p = get_optimized_kprobe((unsigned long)p->addr);
959 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900960 /* Fallback to unoptimized kprobe */
961 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500962
963 arch_arm_kprobe(p);
964 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
965}
966
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900967/* Remove the breakpoint of a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900968static void __disarm_kprobe(struct kprobe *p, bool reopt)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500969{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900970 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500971
Wang Nan69d54b92015-02-13 14:40:26 -0800972 /* Try to unoptimize */
973 unoptimize_kprobe(p, kprobes_all_disarmed);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500974
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900975 if (!kprobe_queued(p)) {
976 arch_disarm_kprobe(p);
977 /* If another kprobe was blocked, optimize it. */
978 _p = get_optimized_kprobe((unsigned long)p->addr);
979 if (unlikely(_p) && reopt)
980 optimize_kprobe(_p);
981 }
982 /* TODO: reoptimize others after unoptimized this probe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500983}
984
985#else /* !CONFIG_OPTPROBES */
986
987#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900988#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500989#define kill_optimized_kprobe(p) do {} while (0)
990#define prepare_optimized_kprobe(p) do {} while (0)
991#define try_to_optimize_kprobe(p) do {} while (0)
992#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900993#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
994#define kprobe_disarmed(p) kprobe_disabled(p)
995#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500996
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900997static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900998{
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900999 /*
1000 * If the optimized kprobe is NOT supported, the aggr kprobe is
1001 * released at the same time that the last aggregated kprobe is
1002 * unregistered.
1003 * Thus there should be no chance to reuse unused kprobe.
1004 */
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001005 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001006 return -EINVAL;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001007}
1008
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001009static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001010{
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001011 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001012 kfree(p);
1013}
1014
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001015static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001016{
1017 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1018}
1019#endif /* CONFIG_OPTPROBES */
1020
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001021#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001022static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +09001023 .func = kprobe_ftrace_handler,
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001024 .flags = FTRACE_OPS_FL_SAVE_REGS,
1025};
1026
1027static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1028 .func = kprobe_ftrace_handler,
Masami Hiramatsu1d70be32014-11-21 05:25:23 -05001029 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001030};
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001031
1032static int kprobe_ipmodify_enabled;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001033static int kprobe_ftrace_enabled;
1034
1035/* Must ensure p->addr is really on ftrace */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001036static int prepare_kprobe(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001037{
1038 if (!kprobe_ftrace(p))
1039 return arch_prepare_kprobe(p);
1040
1041 return arch_prepare_kprobe_ftrace(p);
1042}
1043
1044/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001045static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1046 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001047{
Jessica Yu12310e342018-01-10 00:51:23 +01001048 int ret = 0;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001049
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001050 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001051 if (ret) {
Masami Hiramatsu44585152018-04-28 21:36:33 +09001052 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1053 p->addr, ret);
Jessica Yu12310e342018-01-10 00:51:23 +01001054 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001055 }
Jessica Yu12310e342018-01-10 00:51:23 +01001056
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001057 if (*cnt == 0) {
1058 ret = register_ftrace_function(ops);
Jessica Yu12310e342018-01-10 00:51:23 +01001059 if (ret) {
1060 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1061 goto err_ftrace;
1062 }
1063 }
1064
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001065 (*cnt)++;
Jessica Yu12310e342018-01-10 00:51:23 +01001066 return ret;
1067
1068err_ftrace:
1069 /*
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001070 * At this point, sinec ops is not registered, we should be sefe from
1071 * registering empty filter.
Jessica Yu12310e342018-01-10 00:51:23 +01001072 */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001073 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001074 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001075}
1076
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001077static int arm_kprobe_ftrace(struct kprobe *p)
1078{
1079 bool ipmodify = (p->post_handler != NULL);
1080
1081 return __arm_kprobe_ftrace(p,
1082 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1083 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1084}
1085
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001086/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001087static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1088 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001089{
Jessica Yu297f9232018-01-10 00:51:24 +01001090 int ret = 0;
1091
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001092 if (*cnt == 1) {
1093 ret = unregister_ftrace_function(ops);
Jessica Yu297f9232018-01-10 00:51:24 +01001094 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1095 return ret;
1096 }
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001097
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001098 (*cnt)--;
Jessica Yu297f9232018-01-10 00:51:24 +01001099
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001100 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Masami Hiramatsu44585152018-04-28 21:36:33 +09001101 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1102 p->addr, ret);
Jessica Yu297f9232018-01-10 00:51:24 +01001103 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001104}
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001105
1106static int disarm_kprobe_ftrace(struct kprobe *p)
1107{
1108 bool ipmodify = (p->post_handler != NULL);
1109
1110 return __disarm_kprobe_ftrace(p,
1111 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1112 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1113}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001114#else /* !CONFIG_KPROBES_ON_FTRACE */
Muchun Song10de7952020-08-06 01:20:46 +08001115static inline int prepare_kprobe(struct kprobe *p)
1116{
1117 return arch_prepare_kprobe(p);
1118}
1119
1120static inline int arm_kprobe_ftrace(struct kprobe *p)
1121{
1122 return -ENODEV;
1123}
1124
1125static inline int disarm_kprobe_ftrace(struct kprobe *p)
1126{
1127 return -ENODEV;
1128}
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001129#endif
1130
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001131/* Arm a kprobe with text_mutex */
Jessica Yu12310e342018-01-10 00:51:23 +01001132static int arm_kprobe(struct kprobe *kp)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001133{
Jessica Yu12310e342018-01-10 00:51:23 +01001134 if (unlikely(kprobe_ftrace(kp)))
1135 return arm_kprobe_ftrace(kp);
1136
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001137 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001138 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001139 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001140 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001141 cpus_read_unlock();
Jessica Yu12310e342018-01-10 00:51:23 +01001142
1143 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001144}
1145
1146/* Disarm a kprobe with text_mutex */
Jessica Yu297f9232018-01-10 00:51:24 +01001147static int disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001148{
Jessica Yu297f9232018-01-10 00:51:24 +01001149 if (unlikely(kprobe_ftrace(kp)))
1150 return disarm_kprobe_ftrace(kp);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001151
1152 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001153 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001154 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001155 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001156 cpus_read_unlock();
Jessica Yu297f9232018-01-10 00:51:24 +01001157
1158 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001159}
1160
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001161/*
1162 * Aggregate handlers for multiple kprobes support - these handlers
1163 * take care of invoking the individual kprobe handlers on p->list
1164 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001165static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001166{
1167 struct kprobe *kp;
1168
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001169 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001170 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001171 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001172 if (kp->pre_handler(kp, regs))
1173 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001174 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001175 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001176 }
1177 return 0;
1178}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001179NOKPROBE_SYMBOL(aggr_pre_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001180
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001181static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1182 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001183{
1184 struct kprobe *kp;
1185
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001186 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001187 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001188 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001189 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001190 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001191 }
1192 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001193}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001194NOKPROBE_SYMBOL(aggr_post_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001195
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001196static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1197 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001198{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001199 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001200
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001201 /*
1202 * if we faulted "during" the execution of a user specified
1203 * probe handler, invoke just that probe's fault handler
1204 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001205 if (cur && cur->fault_handler) {
1206 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001207 return 1;
1208 }
1209 return 0;
1210}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001211NOKPROBE_SYMBOL(aggr_fault_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001212
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001213/* Walks the list and increments nmissed count for multiprobe case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001214void kprobes_inc_nmissed_count(struct kprobe *p)
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001215{
1216 struct kprobe *kp;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001217 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001218 p->nmissed++;
1219 } else {
1220 list_for_each_entry_rcu(kp, &p->list, list)
1221 kp->nmissed++;
1222 }
1223 return;
1224}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001225NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001226
Masami Hiramatsub3388172020-08-29 22:02:47 +09001227static void recycle_rp_inst(struct kretprobe_instance *ri)
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
Masami Hiramatsub3388172020-08-29 22:02:47 +09001239 kfree_rcu(ri, rcu);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001240}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001241NOKPROBE_SYMBOL(recycle_rp_inst);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001242
Masami Hiramatsu319f0ce2020-08-29 22:03:02 +09001243static void kretprobe_hash_lock(struct task_struct *tsk,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001244 struct hlist_head **head, unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001245__acquires(hlist_lock)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001246{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001247 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001248 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001249
1250 *head = &kretprobe_inst_table[hash];
1251 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001252 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001253}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001254NOKPROBE_SYMBOL(kretprobe_hash_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001255
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001256static void kretprobe_table_lock(unsigned long hash,
1257 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001258__acquires(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001259{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001260 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1261 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001262}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001263NOKPROBE_SYMBOL(kretprobe_table_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001264
Masami Hiramatsu319f0ce2020-08-29 22:03:02 +09001265static void kretprobe_hash_unlock(struct task_struct *tsk,
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001266 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001267__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001268{
1269 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001270 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001271
1272 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001273 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001274}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001275NOKPROBE_SYMBOL(kretprobe_hash_unlock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001276
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001277static void kretprobe_table_unlock(unsigned long hash,
1278 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001279__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001280{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001281 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1282 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001283}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001284NOKPROBE_SYMBOL(kretprobe_table_unlock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001285
Masami Hiramatsu319f0ce2020-08-29 22:03:02 +09001286static struct kprobe kprobe_busy = {
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001287 .addr = (void *) get_kprobe,
1288};
1289
1290void kprobe_busy_begin(void)
1291{
1292 struct kprobe_ctlblk *kcb;
1293
1294 preempt_disable();
1295 __this_cpu_write(current_kprobe, &kprobe_busy);
1296 kcb = get_kprobe_ctlblk();
1297 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1298}
1299
1300void kprobe_busy_end(void)
1301{
1302 __this_cpu_write(current_kprobe, NULL);
1303 preempt_enable();
1304}
1305
Hien Nguyenb94cce92005-06-23 00:09:19 -07001306/*
bibo maoc6fd91f2006-03-26 01:38:20 -08001307 * This function is called from finish_task_switch when task tk becomes dead,
1308 * so that we can recycle any function-return probe instances associated
1309 * with this task. These left over instances represent probed functions
1310 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001311 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001312void kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001313{
bibo,mao62c27be2006-10-02 02:17:33 -07001314 struct kretprobe_instance *ri;
Masami Hiramatsub3388172020-08-29 22:02:47 +09001315 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001316 struct hlist_node *tmp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001317 unsigned long hash, flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -07001318
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001319 if (unlikely(!kprobes_initialized))
1320 /* Early boot. kretprobe_table_locks not yet initialized. */
1321 return;
1322
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001323 kprobe_busy_begin();
1324
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001325 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1326 head = &kretprobe_inst_table[hash];
1327 kretprobe_table_lock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001328 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -07001329 if (ri->task == tk)
Masami Hiramatsub3388172020-08-29 22:02:47 +09001330 recycle_rp_inst(ri);
bibo,mao62c27be2006-10-02 02:17:33 -07001331 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001332 kretprobe_table_unlock(hash, &flags);
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001333
1334 kprobe_busy_end();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001335}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001336NOKPROBE_SYMBOL(kprobe_flush_task);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001337
Hien Nguyenb94cce92005-06-23 00:09:19 -07001338static inline void free_rp_inst(struct kretprobe *rp)
1339{
1340 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001341 struct hlist_node *next;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001342
Sasha Levinb67bfe02013-02-27 17:06:00 -08001343 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001344 hlist_del(&ri->hlist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001345 kfree(ri);
1346 }
1347}
1348
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001349static void cleanup_rp_inst(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001350{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001351 unsigned long flags, hash;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001352 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001353 struct hlist_node *next;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001354 struct hlist_head *head;
1355
Masami Hiramatsue03b4a02020-08-29 22:02:36 +09001356 /* To avoid recursive kretprobe by NMI, set kprobe busy here */
1357 kprobe_busy_begin();
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001358 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1359 kretprobe_table_lock(hash, &flags);
1360 head = &kretprobe_inst_table[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001361 hlist_for_each_entry_safe(ri, next, head, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001362 if (ri->rp == rp)
1363 ri->rp = NULL;
1364 }
1365 kretprobe_table_unlock(hash, &flags);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001366 }
Masami Hiramatsue03b4a02020-08-29 22:02:36 +09001367 kprobe_busy_end();
1368
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001369 free_rp_inst(rp);
1370}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001371NOKPROBE_SYMBOL(cleanup_rp_inst);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001372
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001373/* Add the new probe to ap->list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001374static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001375{
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001376 if (p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001377 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001378
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001379 list_add_rcu(&p->list, &ap->list);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001380 if (p->post_handler && !ap->post_handler)
1381 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001382
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001383 return 0;
1384}
1385
1386/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001387 * Fill in the required fields of the "manager kprobe". Replace the
1388 * earlier kprobe in the hlist with the manager kprobe
1389 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001390static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001391{
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001392 /* Copy p's insn slot to ap */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001393 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001394 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001395 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001396 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001397 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001398 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001399 /* We don't care the kprobe which has gone. */
1400 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001401 ap->post_handler = aggr_post_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001402
1403 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001404 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001405
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001406 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001407 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001408}
1409
1410/*
1411 * This is the second or subsequent kprobe at the address - handle
1412 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001413 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001414static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001415{
1416 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001417 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001418
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001419 cpus_read_lock();
1420
Masami Hiramatsu25764282012-06-05 19:28:26 +09001421 /* For preparing optimization, jump_label_text_reserved() is called */
1422 jump_label_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001423 mutex_lock(&text_mutex);
1424
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001425 if (!kprobe_aggrprobe(orig_p)) {
1426 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1427 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001428 if (!ap) {
1429 ret = -ENOMEM;
1430 goto out;
1431 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001432 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001433 } else if (kprobe_unused(ap)) {
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001434 /* This probe is going to die. Rescue it */
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001435 ret = reuse_unused_kprobe(ap);
1436 if (ret)
1437 goto out;
1438 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001439
1440 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001441 /*
1442 * Attempting to insert new probe at the same location that
1443 * had a probe in the module vaddr area which already
1444 * freed. So, the instruction slot has already been
1445 * released. We need a new slot for the new probe.
1446 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001447 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001448 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001449 /*
1450 * Even if fail to allocate new slot, don't need to
1451 * free aggr_probe. It will be used next time, or
1452 * freed by unregister_kprobe.
1453 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001454 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001455
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001456 /* Prepare optimized instructions if possible. */
1457 prepare_optimized_kprobe(ap);
1458
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001459 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001460 * Clear gone flag to prevent allocating new slot again, and
1461 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001462 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001463 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1464 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001465 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001466
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001467 /* Copy ap's insn slot to p */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001468 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001469 ret = add_new_kprobe(ap, p);
1470
1471out:
1472 mutex_unlock(&text_mutex);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001473 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001474 cpus_read_unlock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001475
1476 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1477 ap->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01001478 if (!kprobes_all_disarmed) {
Masami Hiramatsu25764282012-06-05 19:28:26 +09001479 /* Arm the breakpoint again. */
Jessica Yu12310e342018-01-10 00:51:23 +01001480 ret = arm_kprobe(ap);
1481 if (ret) {
1482 ap->flags |= KPROBE_FLAG_DISABLED;
1483 list_del_rcu(&p->list);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001484 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001485 }
1486 }
Masami Hiramatsu25764282012-06-05 19:28:26 +09001487 }
1488 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001489}
1490
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001491bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1492{
1493 /* The __kprobes marked functions and entry code must not be probed */
1494 return addr >= (unsigned long)__kprobes_text_start &&
1495 addr < (unsigned long)__kprobes_text_end;
1496}
1497
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001498static bool __within_kprobe_blacklist(unsigned long addr)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001499{
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001500 struct kprobe_blacklist_entry *ent;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001501
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001502 if (arch_within_kprobe_blacklist(addr))
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001503 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001504 /*
1505 * If there exists a kprobe_blacklist, verify and
1506 * fail any probe registration in the prohibited area
1507 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001508 list_for_each_entry(ent, &kprobe_blacklist, list) {
1509 if (addr >= ent->start_addr && addr < ent->end_addr)
1510 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001511 }
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001512 return false;
1513}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001514
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001515bool within_kprobe_blacklist(unsigned long addr)
1516{
1517 char symname[KSYM_NAME_LEN], *p;
1518
1519 if (__within_kprobe_blacklist(addr))
1520 return true;
1521
1522 /* Check if the address is on a suffixed-symbol */
1523 if (!lookup_symbol_name(addr, symname)) {
1524 p = strchr(symname, '.');
1525 if (!p)
1526 return false;
1527 *p = '\0';
1528 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1529 if (addr)
1530 return __within_kprobe_blacklist(addr);
1531 }
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001532 return false;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001533}
1534
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001535/*
1536 * If we have a symbol_name argument, look it up and add the offset field
1537 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001538 * This returns encoded errors if it fails to look up symbol or invalid
1539 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001540 */
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301541static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1542 const char *symbol_name, unsigned int offset)
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001543{
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301544 if ((symbol_name && addr) || (!symbol_name && !addr))
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001545 goto invalid;
1546
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301547 if (symbol_name) {
Linus Torvalds7246f602017-05-05 11:36:44 -07001548 addr = kprobe_lookup_name(symbol_name, offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001549 if (!addr)
1550 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001551 }
1552
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301553 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001554 if (addr)
1555 return addr;
1556
1557invalid:
1558 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001559}
1560
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301561static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1562{
1563 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1564}
1565
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301566/* Check passed kprobe is valid and return kprobe in kprobe_table. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001567static struct kprobe *__get_valid_kprobe(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301568{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001569 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301570
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001571 lockdep_assert_held(&kprobe_mutex);
1572
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001573 ap = get_kprobe(p->addr);
1574 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301575 return NULL;
1576
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001577 if (p != ap) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001578 list_for_each_entry(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301579 if (list_p == p)
1580 /* kprobe p is a valid probe */
1581 goto valid;
1582 return NULL;
1583 }
1584valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001585 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301586}
1587
1588/* Return error if the kprobe is being re-registered */
1589static inline int check_kprobe_rereg(struct kprobe *p)
1590{
1591 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301592
1593 mutex_lock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001594 if (__get_valid_kprobe(p))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301595 ret = -EINVAL;
1596 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001597
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301598 return ret;
1599}
1600
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001601int __weak arch_check_ftrace_location(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001603 unsigned long ftrace_addr;
1604
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001605 ftrace_addr = ftrace_location((unsigned long)p->addr);
1606 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001607#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001608 /* Given address is not on the instruction boundary */
1609 if ((unsigned long)p->addr != ftrace_addr)
1610 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001611 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001612#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001613 return -EINVAL;
1614#endif
1615 }
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001616 return 0;
1617}
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001618
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001619static int check_kprobe_address_safe(struct kprobe *p,
1620 struct module **probed_mod)
1621{
1622 int ret;
1623
1624 ret = arch_check_ftrace_location(p);
1625 if (ret)
1626 return ret;
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001627 jump_label_lock();
1628 preempt_disable();
1629
1630 /* Ensure it is not in reserved area nor out of text */
1631 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001632 within_kprobe_blacklist((unsigned long) p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001633 jump_label_text_reserved(p->addr, p->addr) ||
Peter Zijlstra6333e8f2020-08-18 15:57:43 +02001634 static_call_text_reserved(p->addr, p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001635 find_bug((unsigned long)p->addr)) {
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001636 ret = -EINVAL;
1637 goto out;
1638 }
1639
1640 /* Check if are we probing a module */
1641 *probed_mod = __module_text_address((unsigned long) p->addr);
1642 if (*probed_mod) {
1643 /*
1644 * We must hold a refcount of the probed module while updating
1645 * its code to prohibit unexpected unloading.
1646 */
1647 if (unlikely(!try_module_get(*probed_mod))) {
1648 ret = -ENOENT;
1649 goto out;
1650 }
1651
1652 /*
1653 * If the module freed .init.text, we couldn't insert
1654 * kprobes in there.
1655 */
1656 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1657 (*probed_mod)->state != MODULE_STATE_COMING) {
1658 module_put(*probed_mod);
1659 *probed_mod = NULL;
1660 ret = -ENOENT;
1661 }
1662 }
1663out:
1664 preempt_enable();
1665 jump_label_unlock();
1666
1667 return ret;
1668}
1669
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001670int register_kprobe(struct kprobe *p)
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001671{
1672 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001673 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001674 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001675 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001677 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001678 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001679 if (IS_ERR(addr))
1680 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001681 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001682
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301683 ret = check_kprobe_rereg(p);
1684 if (ret)
1685 return ret;
1686
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001687 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1688 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001689 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001690 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001691
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001692 ret = check_kprobe_address_safe(p, &probed_mod);
1693 if (ret)
1694 return ret;
1695
1696 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001697
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001698 old_p = get_kprobe(p->addr);
1699 if (old_p) {
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001700 /* Since this may unoptimize old_p, locking text_mutex. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001701 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto out;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001705 cpus_read_lock();
1706 /* Prevent text modification */
1707 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001708 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001709 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001710 cpus_read_unlock();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001711 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001712 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001713
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001714 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001715 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1717
Jessica Yu12310e342018-01-10 00:51:23 +01001718 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1719 ret = arm_kprobe(p);
1720 if (ret) {
1721 hlist_del_rcu(&p->hlist);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001722 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001723 goto out;
1724 }
1725 }
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001726
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001727 /* Try to optimize kprobe */
1728 try_to_optimize_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001730 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001731
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001732 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001733 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return ret;
1736}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001737EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001739/* Check if all probes on the aggrprobe are disabled */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001740static int aggr_kprobe_disabled(struct kprobe *ap)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001741{
1742 struct kprobe *kp;
1743
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001744 lockdep_assert_held(&kprobe_mutex);
1745
1746 list_for_each_entry(kp, &ap->list, list)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001747 if (!kprobe_disabled(kp))
1748 /*
1749 * There is an active probe on the list.
1750 * We can't disable this ap.
1751 */
1752 return 0;
1753
1754 return 1;
1755}
1756
1757/* Disable one kprobe: Make sure called under kprobe_mutex is locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001758static struct kprobe *__disable_kprobe(struct kprobe *p)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001759{
1760 struct kprobe *orig_p;
Jessica Yu297f9232018-01-10 00:51:24 +01001761 int ret;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001762
1763 /* Get an original kprobe for return */
1764 orig_p = __get_valid_kprobe(p);
1765 if (unlikely(orig_p == NULL))
Jessica Yu297f9232018-01-10 00:51:24 +01001766 return ERR_PTR(-EINVAL);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001767
1768 if (!kprobe_disabled(p)) {
1769 /* Disable probe if it is a child probe */
1770 if (p != orig_p)
1771 p->flags |= KPROBE_FLAG_DISABLED;
1772
1773 /* Try to disarm and disable this/parent probe */
1774 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Wang Nan69d54b92015-02-13 14:40:26 -08001775 /*
1776 * If kprobes_all_disarmed is set, orig_p
1777 * should have already been disarmed, so
1778 * skip unneed disarming process.
1779 */
Jessica Yu297f9232018-01-10 00:51:24 +01001780 if (!kprobes_all_disarmed) {
1781 ret = disarm_kprobe(orig_p, true);
1782 if (ret) {
1783 p->flags &= ~KPROBE_FLAG_DISABLED;
1784 return ERR_PTR(ret);
1785 }
1786 }
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001787 orig_p->flags |= KPROBE_FLAG_DISABLED;
1788 }
1789 }
1790
1791 return orig_p;
1792}
1793
Masami Hiramatsu98616682008-04-28 02:14:28 -07001794/*
1795 * Unregister a kprobe without a scheduler synchronization.
1796 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001797static int __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001798{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001799 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001800
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001801 /* Disable kprobe. This will disarm it if needed. */
1802 ap = __disable_kprobe(p);
Jessica Yu297f9232018-01-10 00:51:24 +01001803 if (IS_ERR(ap))
1804 return PTR_ERR(ap);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001805
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001806 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001807 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001808 * This probe is an independent(and non-optimized) kprobe
1809 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001810 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001811 goto disarmed;
1812
1813 /* Following process expects this probe is an aggrprobe */
1814 WARN_ON(!kprobe_aggrprobe(ap));
1815
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001816 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1817 /*
1818 * !disarmed could be happen if the probe is under delayed
1819 * unoptimizing.
1820 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001821 goto disarmed;
1822 else {
1823 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001824 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001825 list_for_each_entry(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001826 if ((list_p != p) && (list_p->post_handler))
1827 goto noclean;
1828 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001829 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001830 }
1831noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001832 /*
1833 * Remove from the aggrprobe: this path will do nothing in
1834 * __unregister_kprobe_bottom().
1835 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001836 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001837 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1838 /*
1839 * Try to optimize this probe again, because post
1840 * handler may have been changed.
1841 */
1842 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001843 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001844 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001845
1846disarmed:
1847 hlist_del_rcu(&ap->hlist);
1848 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001849}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001850
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001851static void __unregister_kprobe_bottom(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001852{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001853 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001854
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001855 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001856 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001857 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001858 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001859 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001860 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001861 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001862 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001863 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001864 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001867int register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001868{
1869 int i, ret = 0;
1870
1871 if (num <= 0)
1872 return -EINVAL;
1873 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001874 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001875 if (ret < 0) {
1876 if (i > 0)
1877 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001878 break;
1879 }
1880 }
1881 return ret;
1882}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001883EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001884
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001885void unregister_kprobe(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001886{
1887 unregister_kprobes(&p, 1);
1888}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001889EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001890
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001891void unregister_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001892{
1893 int i;
1894
1895 if (num <= 0)
1896 return;
1897 mutex_lock(&kprobe_mutex);
1898 for (i = 0; i < num; i++)
1899 if (__unregister_kprobe_top(kps[i]) < 0)
1900 kps[i]->addr = NULL;
1901 mutex_unlock(&kprobe_mutex);
1902
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001903 synchronize_rcu();
Masami Hiramatsu98616682008-04-28 02:14:28 -07001904 for (i = 0; i < num; i++)
1905 if (kps[i]->addr)
1906 __unregister_kprobe_bottom(kps[i]);
1907}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001908EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001909
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301910int __weak kprobe_exceptions_notify(struct notifier_block *self,
1911 unsigned long val, void *data)
Naveen N. Raofc62d022017-02-08 01:24:14 +05301912{
1913 return NOTIFY_DONE;
1914}
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301915NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Naveen N. Raofc62d022017-02-08 01:24:14 +05301916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917static struct notifier_block kprobe_exceptions_nb = {
1918 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001919 .priority = 0x7fffffff /* we need to be notified first */
1920};
1921
Michael Ellerman3d7e3382007-07-19 01:48:11 -07001922unsigned long __weak arch_deref_entry_point(void *entry)
1923{
1924 return (unsigned long)entry;
1925}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001927#ifdef CONFIG_KRETPROBES
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001928
1929unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
1930 void *trampoline_address,
1931 void *frame_pointer)
1932{
1933 struct kretprobe_instance *ri = NULL, *last = NULL;
Masami Hiramatsub3388172020-08-29 22:02:47 +09001934 struct hlist_head *head;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001935 struct hlist_node *tmp;
1936 unsigned long flags;
1937 kprobe_opcode_t *correct_ret_addr = NULL;
1938 bool skipped = false;
1939
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001940 kretprobe_hash_lock(current, &head, &flags);
1941
1942 /*
1943 * It is possible to have multiple instances associated with a given
1944 * task either because multiple functions in the call path have
1945 * return probes installed on them, and/or more than one
1946 * return probe was registered for a target function.
1947 *
1948 * We can handle this because:
1949 * - instances are always pushed into the head of the list
1950 * - when multiple return probes are registered for the same
1951 * function, the (chronologically) first instance's ret_addr
1952 * will be the real return address, and all the rest will
1953 * point to kretprobe_trampoline.
1954 */
1955 hlist_for_each_entry(ri, head, hlist) {
1956 if (ri->task != current)
1957 /* another task is sharing our hash bucket */
1958 continue;
1959 /*
1960 * Return probes must be pushed on this hash list correct
1961 * order (same as return order) so that it can be popped
1962 * correctly. However, if we find it is pushed it incorrect
1963 * order, this means we find a function which should not be
1964 * probed, because the wrong order entry is pushed on the
1965 * path of processing other kretprobe itself.
1966 */
1967 if (ri->fp != frame_pointer) {
1968 if (!skipped)
1969 pr_warn("kretprobe is stacked incorrectly. Trying to fixup.\n");
1970 skipped = true;
1971 continue;
1972 }
1973
1974 correct_ret_addr = ri->ret_addr;
1975 if (skipped)
1976 pr_warn("%ps must be blacklisted because of incorrect kretprobe order\n",
1977 ri->rp->kp.addr);
1978
1979 if (correct_ret_addr != trampoline_address)
1980 /*
1981 * This is the real return address. Any other
1982 * instances associated with this task are for
1983 * other calls deeper on the call stack
1984 */
1985 break;
1986 }
1987
Masami Hiramatsu319f0ce2020-08-29 22:03:02 +09001988 BUG_ON(!correct_ret_addr || (correct_ret_addr == trampoline_address));
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001989 last = ri;
1990
1991 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
1992 if (ri->task != current)
1993 /* another task is sharing our hash bucket */
1994 continue;
1995 if (ri->fp != frame_pointer)
1996 continue;
1997
1998 if (ri->rp && ri->rp->handler) {
1999 struct kprobe *prev = kprobe_running();
2000
2001 __this_cpu_write(current_kprobe, &ri->rp->kp);
2002 ri->ret_addr = correct_ret_addr;
2003 ri->rp->handler(ri, regs);
2004 __this_cpu_write(current_kprobe, prev);
2005 }
2006
Masami Hiramatsub3388172020-08-29 22:02:47 +09002007 recycle_rp_inst(ri);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09002008
2009 if (ri == last)
2010 break;
2011 }
2012
2013 kretprobe_hash_unlock(current, &flags);
2014
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09002015 return (unsigned long)correct_ret_addr;
2016}
2017NOKPROBE_SYMBOL(__kretprobe_trampoline_handler)
2018
Adrian Bunke65cefe2006-02-03 03:03:42 -08002019/*
2020 * This kprobe pre_handler is registered with every kretprobe. When probe
2021 * hits it will set up the return probe.
2022 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002023static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Adrian Bunke65cefe2006-02-03 03:03:42 -08002024{
2025 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002026 unsigned long hash, flags = 0;
2027 struct kretprobe_instance *ri;
Adrian Bunke65cefe2006-02-03 03:03:42 -08002028
Masami Hiramatsuf96f5672014-08-04 03:10:16 +00002029 /* TODO: consider to only swap the RA after the last pre_handler fired */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002030 hash = hash_ptr(current, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002031 raw_spin_lock_irqsave(&rp->lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07002032 if (!hlist_empty(&rp->free_instances)) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07002033 ri = hlist_entry(rp->free_instances.first,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002034 struct kretprobe_instance, hlist);
2035 hlist_del(&ri->hlist);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002036 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002037
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07002038 ri->rp = rp;
2039 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08002040
Jiang Liu55ca6142012-02-03 15:37:16 -08002041 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
2042 raw_spin_lock_irqsave(&rp->lock, flags);
2043 hlist_add_head(&ri->hlist, &rp->free_instances);
2044 raw_spin_unlock_irqrestore(&rp->lock, flags);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08002045 return 0;
Jiang Liu55ca6142012-02-03 15:37:16 -08002046 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08002047
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07002048 arch_prepare_kretprobe(ri, regs);
2049
2050 /* XXX(hch): why is there no hlist_move_head? */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002051 INIT_HLIST_NODE(&ri->hlist);
2052 kretprobe_table_lock(hash, &flags);
2053 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
2054 kretprobe_table_unlock(hash, &flags);
2055 } else {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07002056 rp->nmissed++;
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002057 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002058 }
Adrian Bunke65cefe2006-02-03 03:03:42 -08002059 return 0;
2060}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002061NOKPROBE_SYMBOL(pre_handler_kretprobe);
Adrian Bunke65cefe2006-02-03 03:03:42 -08002062
Naveen N. Rao659b9572017-07-07 22:37:24 +05302063bool __weak arch_kprobe_on_func_entry(unsigned long offset)
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05302064{
2065 return !offset;
2066}
2067
Naveen N. Rao659b9572017-07-07 22:37:24 +05302068bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
Naveen N. Rao1d585e72017-03-08 13:56:06 +05302069{
2070 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
2071
2072 if (IS_ERR(kp_addr))
2073 return false;
2074
2075 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) ||
Naveen N. Rao659b9572017-07-07 22:37:24 +05302076 !arch_kprobe_on_func_entry(offset))
Naveen N. Rao1d585e72017-03-08 13:56:06 +05302077 return false;
2078
2079 return true;
2080}
2081
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002082int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002083{
2084 int ret = 0;
2085 struct kretprobe_instance *inst;
2086 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08002087 void *addr;
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05302088
Naveen N. Rao659b9572017-07-07 22:37:24 +05302089 if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset))
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05302090 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002091
2092 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08002093 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09002094 if (IS_ERR(addr))
2095 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002096
2097 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2098 if (kretprobe_blacklist[i].addr == addr)
2099 return -EINVAL;
2100 }
2101 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07002102
2103 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07002104 rp->kp.post_handler = NULL;
2105 rp->kp.fault_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07002106
2107 /* Pre-allocate memory for max kretprobe instances */
2108 if (rp->maxactive <= 0) {
Thomas Gleixner92616602019-07-26 23:19:41 +02002109#ifdef CONFIG_PREEMPTION
Heiko Carstensc2ef6662009-12-21 13:02:24 +01002110 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07002111#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05302112 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07002113#endif
2114 }
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002115 raw_spin_lock_init(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002116 INIT_HLIST_HEAD(&rp->free_instances);
2117 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08002118 inst = kmalloc(sizeof(struct kretprobe_instance) +
2119 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002120 if (inst == NULL) {
2121 free_rp_inst(rp);
2122 return -ENOMEM;
2123 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002124 INIT_HLIST_NODE(&inst->hlist);
2125 hlist_add_head(&inst->hlist, &rp->free_instances);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002126 }
2127
2128 rp->nmissed = 0;
2129 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002130 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002131 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002132 free_rp_inst(rp);
2133 return ret;
2134}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002135EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002136
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002137int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002138{
2139 int ret = 0, i;
2140
2141 if (num <= 0)
2142 return -EINVAL;
2143 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002144 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07002145 if (ret < 0) {
2146 if (i > 0)
2147 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002148 break;
2149 }
2150 }
2151 return ret;
2152}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002153EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002154
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002155void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002156{
2157 unregister_kretprobes(&rp, 1);
2158}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002159EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002160
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002161void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002162{
2163 int i;
2164
2165 if (num <= 0)
2166 return;
2167 mutex_lock(&kprobe_mutex);
2168 for (i = 0; i < num; i++)
2169 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2170 rps[i]->kp.addr = NULL;
2171 mutex_unlock(&kprobe_mutex);
2172
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08002173 synchronize_rcu();
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002174 for (i = 0; i < num; i++) {
2175 if (rps[i]->kp.addr) {
2176 __unregister_kprobe_bottom(&rps[i]->kp);
2177 cleanup_rp_inst(rps[i]);
2178 }
2179 }
2180}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002181EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002182
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002183#else /* CONFIG_KRETPROBES */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002184int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002185{
2186 return -ENOSYS;
2187}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002188EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002189
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002190int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002191{
2192 return -ENOSYS;
2193}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002194EXPORT_SYMBOL_GPL(register_kretprobes);
2195
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002196void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002197{
2198}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002199EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002200
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002201void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002202{
2203}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002204EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002205
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002206static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002207{
2208 return 0;
2209}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002210NOKPROBE_SYMBOL(pre_handler_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002211
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002212#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002213
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002214/* Set the kprobe gone and remove its instruction buffer. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002215static void kill_kprobe(struct kprobe *p)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002216{
2217 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002218
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002219 lockdep_assert_held(&kprobe_mutex);
2220
Muchun Songb03990922020-09-18 21:20:21 -07002221 if (WARN_ON_ONCE(kprobe_gone(p)))
2222 return;
2223
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002224 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002225 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002226 /*
2227 * If this is an aggr_kprobe, we have to list all the
2228 * chained probes and mark them GONE.
2229 */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002230 list_for_each_entry(kp, &p->list, list)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002231 kp->flags |= KPROBE_FLAG_GONE;
2232 p->post_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002233 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002234 }
2235 /*
2236 * Here, we can remove insn_slot safely, because no thread calls
2237 * the original probed function (which will be freed soon) any more.
2238 */
2239 arch_remove_kprobe(p);
Muchun Song0cb2f132020-07-28 14:45:36 +08002240
2241 /*
2242 * The module is going away. We should disarm the kprobe which
Masami Hiramatsu30313132020-09-01 00:12:07 +09002243 * is using ftrace, because ftrace framework is still available at
2244 * MODULE_STATE_GOING notification.
Muchun Song0cb2f132020-07-28 14:45:36 +08002245 */
Masami Hiramatsu30313132020-09-01 00:12:07 +09002246 if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
Muchun Song0cb2f132020-07-28 14:45:36 +08002247 disarm_kprobe_ftrace(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002248}
2249
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002250/* Disable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002251int disable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002252{
2253 int ret = 0;
Jessica Yu297f9232018-01-10 00:51:24 +01002254 struct kprobe *p;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002255
2256 mutex_lock(&kprobe_mutex);
2257
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09002258 /* Disable this kprobe */
Jessica Yu297f9232018-01-10 00:51:24 +01002259 p = __disable_kprobe(kp);
2260 if (IS_ERR(p))
2261 ret = PTR_ERR(p);
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002262
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002263 mutex_unlock(&kprobe_mutex);
2264 return ret;
2265}
2266EXPORT_SYMBOL_GPL(disable_kprobe);
2267
2268/* Enable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002269int enable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002270{
2271 int ret = 0;
2272 struct kprobe *p;
2273
2274 mutex_lock(&kprobe_mutex);
2275
2276 /* Check whether specified probe is valid. */
2277 p = __get_valid_kprobe(kp);
2278 if (unlikely(p == NULL)) {
2279 ret = -EINVAL;
2280 goto out;
2281 }
2282
2283 if (kprobe_gone(kp)) {
2284 /* This kprobe has gone, we couldn't enable it. */
2285 ret = -EINVAL;
2286 goto out;
2287 }
2288
2289 if (p != kp)
2290 kp->flags &= ~KPROBE_FLAG_DISABLED;
2291
2292 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2293 p->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01002294 ret = arm_kprobe(p);
2295 if (ret)
2296 p->flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002297 }
2298out:
2299 mutex_unlock(&kprobe_mutex);
2300 return ret;
2301}
2302EXPORT_SYMBOL_GPL(enable_kprobe);
2303
Masami Hiramatsu44585152018-04-28 21:36:33 +09002304/* Caller must NOT call this in usual path. This is only for critical case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002305void dump_kprobe(struct kprobe *kp)
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002306{
Masami Hiramatsu44585152018-04-28 21:36:33 +09002307 pr_err("Dumping kprobe:\n");
2308 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2309 kp->symbol_name, kp->offset, kp->addr);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002310}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002311NOKPROBE_SYMBOL(dump_kprobe);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002312
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002313int kprobe_add_ksym_blacklist(unsigned long entry)
2314{
2315 struct kprobe_blacklist_entry *ent;
2316 unsigned long offset = 0, size = 0;
2317
2318 if (!kernel_text_address(entry) ||
2319 !kallsyms_lookup_size_offset(entry, &size, &offset))
2320 return -EINVAL;
2321
2322 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2323 if (!ent)
2324 return -ENOMEM;
2325 ent->start_addr = entry;
2326 ent->end_addr = entry + size;
2327 INIT_LIST_HEAD(&ent->list);
2328 list_add_tail(&ent->list, &kprobe_blacklist);
2329
2330 return (int)size;
2331}
2332
2333/* Add all symbols in given area into kprobe blacklist */
2334int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2335{
2336 unsigned long entry;
2337 int ret = 0;
2338
2339 for (entry = start; entry < end; entry += ret) {
2340 ret = kprobe_add_ksym_blacklist(entry);
2341 if (ret < 0)
2342 return ret;
2343 if (ret == 0) /* In case of alias symbol */
2344 ret = 1;
2345 }
2346 return 0;
2347}
2348
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002349/* Remove all symbols in given area from kprobe blacklist */
2350static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2351{
2352 struct kprobe_blacklist_entry *ent, *n;
2353
2354 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2355 if (ent->start_addr < start || ent->start_addr >= end)
2356 continue;
2357 list_del(&ent->list);
2358 kfree(ent);
2359 }
2360}
2361
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002362static void kprobe_remove_ksym_blacklist(unsigned long entry)
2363{
2364 kprobe_remove_area_blacklist(entry, entry + 1);
2365}
2366
Adrian Hunterd002b8b2020-05-28 11:00:58 +03002367int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2368 char *type, char *sym)
2369{
2370 return -ERANGE;
2371}
2372
2373int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2374 char *sym)
2375{
2376#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2377 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2378 return 0;
2379#ifdef CONFIG_OPTPROBES
2380 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2381 return 0;
2382#endif
2383#endif
2384 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2385 return 0;
2386 return -ERANGE;
2387}
2388
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002389int __init __weak arch_populate_kprobe_blacklist(void)
2390{
2391 return 0;
2392}
2393
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002394/*
2395 * Lookup and populate the kprobe_blacklist.
2396 *
2397 * Unlike the kretprobe blacklist, we'll need to determine
2398 * the range of addresses that belong to the said functions,
2399 * since a kprobe need not necessarily be at the beginning
2400 * of a function.
2401 */
2402static int __init populate_kprobe_blacklist(unsigned long *start,
2403 unsigned long *end)
2404{
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002405 unsigned long entry;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002406 unsigned long *iter;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002407 int ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002408
2409 for (iter = start; iter < end; iter++) {
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002410 entry = arch_deref_entry_point((void *)*iter);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002411 ret = kprobe_add_ksym_blacklist(entry);
2412 if (ret == -EINVAL)
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002413 continue;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002414 if (ret < 0)
2415 return ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002416 }
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002417
2418 /* Symbols in __kprobes_text are blacklisted */
2419 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2420 (unsigned long)__kprobes_text_end);
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002421 if (ret)
2422 return ret;
2423
2424 /* Symbols in noinstr section are blacklisted */
2425 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2426 (unsigned long)__noinstr_text_end);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002427
2428 return ret ? : arch_populate_kprobe_blacklist();
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002429}
2430
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002431static void add_module_kprobe_blacklist(struct module *mod)
2432{
2433 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002434 int i;
2435
2436 if (mod->kprobe_blacklist) {
2437 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2438 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2439 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002440
2441 start = (unsigned long)mod->kprobes_text_start;
2442 if (start) {
2443 end = start + mod->kprobes_text_size;
2444 kprobe_add_area_blacklist(start, end);
2445 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002446
2447 start = (unsigned long)mod->noinstr_text_start;
2448 if (start) {
2449 end = start + mod->noinstr_text_size;
2450 kprobe_add_area_blacklist(start, end);
2451 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002452}
2453
2454static void remove_module_kprobe_blacklist(struct module *mod)
2455{
2456 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002457 int i;
2458
2459 if (mod->kprobe_blacklist) {
2460 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2461 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2462 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002463
2464 start = (unsigned long)mod->kprobes_text_start;
2465 if (start) {
2466 end = start + mod->kprobes_text_size;
2467 kprobe_remove_area_blacklist(start, end);
2468 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002469
2470 start = (unsigned long)mod->noinstr_text_start;
2471 if (start) {
2472 end = start + mod->noinstr_text_size;
2473 kprobe_remove_area_blacklist(start, end);
2474 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002475}
2476
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002477/* Module notifier call back, checking kprobes on the module */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002478static int kprobes_module_callback(struct notifier_block *nb,
2479 unsigned long val, void *data)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002480{
2481 struct module *mod = data;
2482 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002483 struct kprobe *p;
2484 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002485 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002486
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002487 if (val == MODULE_STATE_COMING) {
2488 mutex_lock(&kprobe_mutex);
2489 add_module_kprobe_blacklist(mod);
2490 mutex_unlock(&kprobe_mutex);
2491 }
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002492 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002493 return NOTIFY_DONE;
2494
2495 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002496 * When MODULE_STATE_GOING was notified, both of module .text and
2497 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2498 * notified, only .init.text section would be freed. We need to
2499 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002500 */
2501 mutex_lock(&kprobe_mutex);
2502 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2503 head = &kprobe_table[i];
Muchun Songb03990922020-09-18 21:20:21 -07002504 hlist_for_each_entry(p, head, hlist) {
2505 if (kprobe_gone(p))
2506 continue;
2507
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002508 if (within_module_init((unsigned long)p->addr, mod) ||
2509 (checkcore &&
2510 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002511 /*
2512 * The vaddr this probe is installed will soon
2513 * be vfreed buy not synced to disk. Hence,
2514 * disarming the breakpoint isn't needed.
Steven Rostedt (VMware)545a0282017-05-16 14:58:35 -04002515 *
2516 * Note, this will also move any optimized probes
2517 * that are pending to be removed from their
2518 * corresponding lists to the freeing_list and
2519 * will not be touched by the delayed
2520 * kprobe_optimizer work handler.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002521 */
2522 kill_kprobe(p);
2523 }
Muchun Songb03990922020-09-18 21:20:21 -07002524 }
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002525 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002526 if (val == MODULE_STATE_GOING)
2527 remove_module_kprobe_blacklist(mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002528 mutex_unlock(&kprobe_mutex);
2529 return NOTIFY_DONE;
2530}
2531
2532static struct notifier_block kprobe_module_nb = {
2533 .notifier_call = kprobes_module_callback,
2534 .priority = 0
2535};
2536
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002537/* Markers of _kprobe_blacklist section */
2538extern unsigned long __start_kprobe_blacklist[];
2539extern unsigned long __stop_kprobe_blacklist[];
2540
Masami Hiramatsu82d083a2020-09-10 17:55:05 +09002541void kprobe_free_init_mem(void)
2542{
2543 void *start = (void *)(&__init_begin);
2544 void *end = (void *)(&__init_end);
2545 struct hlist_head *head;
2546 struct kprobe *p;
2547 int i;
2548
2549 mutex_lock(&kprobe_mutex);
2550
2551 /* Kill all kprobes on initmem */
2552 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2553 head = &kprobe_table[i];
2554 hlist_for_each_entry(p, head, hlist) {
2555 if (start <= (void *)p->addr && (void *)p->addr < end)
2556 kill_kprobe(p);
2557 }
2558 }
2559
2560 mutex_unlock(&kprobe_mutex);
2561}
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static int __init init_kprobes(void)
2564{
2565 int i, err = 0;
2566
2567 /* FIXME allocate the probe table, currently defined statically */
2568 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002569 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002571 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002572 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
Hien Nguyenb94cce92005-06-23 00:09:19 -07002573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002575 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2576 __stop_kprobe_blacklist);
2577 if (err) {
2578 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2579 pr_err("Please take care of using kprobes.\n");
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002580 }
2581
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002582 if (kretprobe_blacklist_size) {
2583 /* lookup the function address from its name */
2584 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05302585 kretprobe_blacklist[i].addr =
Naveen N. Rao290e3072017-04-19 18:21:01 +05302586 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002587 if (!kretprobe_blacklist[i].addr)
2588 printk("kretprobe: lookup failed: %s\n",
2589 kretprobe_blacklist[i].name);
2590 }
2591 }
2592
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002593#if defined(CONFIG_OPTPROBES)
2594#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002595 /* Init kprobe_optinsn_slots */
2596 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2597#endif
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002598 /* By default, kprobes can be optimized */
2599 kprobes_allow_optimization = true;
2600#endif
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002601
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002602 /* By default, kprobes are armed */
2603 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002604
Rusty Lynch67729262005-07-05 18:54:50 -07002605 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002606 if (!err)
2607 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002608 if (!err)
2609 err = register_module_notifier(&kprobe_module_nb);
2610
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002611 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07002612
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01002613 if (!err)
2614 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return err;
2616}
Masami Hiramatsu65fc9652019-06-03 22:04:42 +09002617subsys_initcall(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Srinivasa Ds346fd592007-02-20 13:57:54 -08002619#ifdef CONFIG_DEBUG_FS
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002620static void report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002621 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002622{
2623 char *kprobe_type;
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002624 void *addr = p->addr;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002625
2626 if (p->pre_handler == pre_handler_kretprobe)
2627 kprobe_type = "r";
Srinivasa Ds346fd592007-02-20 13:57:54 -08002628 else
2629 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002630
Kees Cook60f7bb62020-07-02 15:20:22 -07002631 if (!kallsyms_show_value(pi->file->f_cred))
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002632 addr = NULL;
2633
Srinivasa Ds346fd592007-02-20 13:57:54 -08002634 if (sym)
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002635 seq_printf(pi, "%px %s %s+0x%x %s ",
2636 addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002637 (modname ? modname : " "));
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002638 else /* try to use %pS */
2639 seq_printf(pi, "%px %s %pS ",
2640 addr, kprobe_type, p->addr);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002641
2642 if (!pp)
2643 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002644 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002645 (kprobe_gone(p) ? "[GONE]" : ""),
2646 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002647 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2648 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002649}
2650
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002651static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002652{
2653 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2654}
2655
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002656static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002657{
2658 (*pos)++;
2659 if (*pos >= KPROBE_TABLE_SIZE)
2660 return NULL;
2661 return pos;
2662}
2663
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002664static void kprobe_seq_stop(struct seq_file *f, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002665{
2666 /* Nothing to do */
2667}
2668
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002669static int show_kprobe_addr(struct seq_file *pi, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002670{
2671 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002672 struct kprobe *p, *kp;
2673 const char *sym = NULL;
2674 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002675 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002676 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002677
2678 head = &kprobe_table[i];
2679 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002680 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002681 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002682 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002683 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002684 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002685 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002686 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002687 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002688 }
2689 preempt_enable();
2690 return 0;
2691}
2692
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002693static const struct seq_operations kprobes_sops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002694 .start = kprobe_seq_start,
2695 .next = kprobe_seq_next,
2696 .stop = kprobe_seq_stop,
2697 .show = show_kprobe_addr
2698};
2699
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002700DEFINE_SEQ_ATTRIBUTE(kprobes);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002701
Masami Hiramatsu63724742014-04-17 17:18:49 +09002702/* kprobes/blacklist -- shows which functions can not be probed */
2703static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2704{
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002705 mutex_lock(&kprobe_mutex);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002706 return seq_list_start(&kprobe_blacklist, *pos);
2707}
2708
2709static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2710{
2711 return seq_list_next(v, &kprobe_blacklist, pos);
2712}
2713
2714static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2715{
2716 struct kprobe_blacklist_entry *ent =
2717 list_entry(v, struct kprobe_blacklist_entry, list);
2718
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002719 /*
2720 * If /proc/kallsyms is not showing kernel address, we won't
2721 * show them here either.
2722 */
Kees Cook60f7bb62020-07-02 15:20:22 -07002723 if (!kallsyms_show_value(m->file->f_cred))
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002724 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2725 (void *)ent->start_addr);
2726 else
2727 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2728 (void *)ent->end_addr, (void *)ent->start_addr);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002729 return 0;
2730}
2731
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002732static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2733{
2734 mutex_unlock(&kprobe_mutex);
2735}
2736
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002737static const struct seq_operations kprobe_blacklist_sops = {
Masami Hiramatsu63724742014-04-17 17:18:49 +09002738 .start = kprobe_blacklist_seq_start,
2739 .next = kprobe_blacklist_seq_next,
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002740 .stop = kprobe_blacklist_seq_stop,
Masami Hiramatsu63724742014-04-17 17:18:49 +09002741 .show = kprobe_blacklist_seq_show,
2742};
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002743DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002744
Jessica Yu12310e342018-01-10 00:51:23 +01002745static int arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002746{
2747 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002748 struct kprobe *p;
Jessica Yu12310e342018-01-10 00:51:23 +01002749 unsigned int i, total = 0, errors = 0;
2750 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002751
2752 mutex_lock(&kprobe_mutex);
2753
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002754 /* If kprobes are armed, just return */
2755 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002756 goto already_enabled;
2757
Wang Nan977ad482015-02-13 14:40:24 -08002758 /*
2759 * optimize_kprobe() called by arm_kprobe() checks
2760 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2761 * arm_kprobe.
2762 */
2763 kprobes_all_disarmed = false;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002764 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002765 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2766 head = &kprobe_table[i];
Jessica Yu12310e342018-01-10 00:51:23 +01002767 /* Arm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002768 hlist_for_each_entry(p, head, hlist) {
Jessica Yu12310e342018-01-10 00:51:23 +01002769 if (!kprobe_disabled(p)) {
2770 err = arm_kprobe(p);
2771 if (err) {
2772 errors++;
2773 ret = err;
2774 }
2775 total++;
2776 }
2777 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002778 }
2779
Jessica Yu12310e342018-01-10 00:51:23 +01002780 if (errors)
2781 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2782 errors, total);
2783 else
2784 pr_info("Kprobes globally enabled\n");
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002785
2786already_enabled:
2787 mutex_unlock(&kprobe_mutex);
Jessica Yu12310e342018-01-10 00:51:23 +01002788 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002789}
2790
Jessica Yu297f9232018-01-10 00:51:24 +01002791static int disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002792{
2793 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002794 struct kprobe *p;
Jessica Yu297f9232018-01-10 00:51:24 +01002795 unsigned int i, total = 0, errors = 0;
2796 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002797
2798 mutex_lock(&kprobe_mutex);
2799
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002800 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002801 if (kprobes_all_disarmed) {
2802 mutex_unlock(&kprobe_mutex);
Jessica Yu297f9232018-01-10 00:51:24 +01002803 return 0;
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002804 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002805
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002806 kprobes_all_disarmed = true;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002807
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002808 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2809 head = &kprobe_table[i];
Jessica Yu297f9232018-01-10 00:51:24 +01002810 /* Disarm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002811 hlist_for_each_entry(p, head, hlist) {
Jessica Yu297f9232018-01-10 00:51:24 +01002812 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2813 err = disarm_kprobe(p, false);
2814 if (err) {
2815 errors++;
2816 ret = err;
2817 }
2818 total++;
2819 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002820 }
2821 }
Jessica Yu297f9232018-01-10 00:51:24 +01002822
2823 if (errors)
2824 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2825 errors, total);
2826 else
2827 pr_info("Kprobes globally disabled\n");
2828
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002829 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002830
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002831 /* Wait for disarming all kprobes by optimizer */
2832 wait_for_kprobe_optimizer();
Jessica Yu297f9232018-01-10 00:51:24 +01002833
2834 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002835}
2836
2837/*
2838 * XXX: The debugfs bool file interface doesn't allow for callbacks
2839 * when the bool state is switched. We can reuse that facility when
2840 * available
2841 */
2842static ssize_t read_enabled_file_bool(struct file *file,
2843 char __user *user_buf, size_t count, loff_t *ppos)
2844{
2845 char buf[3];
2846
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002847 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002848 buf[0] = '1';
2849 else
2850 buf[0] = '0';
2851 buf[1] = '\n';
2852 buf[2] = 0x00;
2853 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2854}
2855
2856static ssize_t write_enabled_file_bool(struct file *file,
2857 const char __user *user_buf, size_t count, loff_t *ppos)
2858{
2859 char buf[32];
Stephen Boydefeb1562012-01-12 17:17:11 -08002860 size_t buf_size;
Jessica Yu12310e342018-01-10 00:51:23 +01002861 int ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002862
2863 buf_size = min(count, (sizeof(buf)-1));
2864 if (copy_from_user(buf, user_buf, buf_size))
2865 return -EFAULT;
2866
Mathias Krause10fb46d2013-07-03 15:05:39 -07002867 buf[buf_size] = '\0';
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002868 switch (buf[0]) {
2869 case 'y':
2870 case 'Y':
2871 case '1':
Jessica Yu12310e342018-01-10 00:51:23 +01002872 ret = arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002873 break;
2874 case 'n':
2875 case 'N':
2876 case '0':
Jessica Yu297f9232018-01-10 00:51:24 +01002877 ret = disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002878 break;
Mathias Krause10fb46d2013-07-03 15:05:39 -07002879 default:
2880 return -EINVAL;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002881 }
2882
Jessica Yu12310e342018-01-10 00:51:23 +01002883 if (ret)
2884 return ret;
2885
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002886 return count;
2887}
2888
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002889static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002890 .read = read_enabled_file_bool,
2891 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002892 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002893};
2894
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002895static int __init debugfs_kprobe_init(void)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002896{
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002897 struct dentry *dir;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002898 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002899
2900 dir = debugfs_create_dir("kprobes", NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002901
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002902 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002903
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002904 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002905
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002906 debugfs_create_file("blacklist", 0400, dir, NULL,
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002907 &kprobe_blacklist_fops);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002908
Srinivasa Ds346fd592007-02-20 13:57:54 -08002909 return 0;
2910}
2911
2912late_initcall(debugfs_kprobe_init);
2913#endif /* CONFIG_DEBUG_FS */