blob: 058c0be3464beea117c6c5d2722cb569dcb94d44 [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>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070038
Christoph Hellwigbfd45be2016-10-11 13:52:22 -070039#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/cacheflush.h>
41#include <asm/errno.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define KPROBE_HASH_BITS 6
45#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
46
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070047
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070048static int kprobes_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070050static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070052/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070053static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070054
Masami Hiramatsu43948f52010-10-25 22:18:01 +090055/* This protects kprobe_table and optimizing_list */
56static DEFINE_MUTEX(kprobe_mutex);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080057static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070058static struct {
Thomas Gleixnerec4846082009-07-25 16:09:17 +020059 raw_spinlock_t lock ____cacheline_aligned_in_smp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070060} kretprobe_table_locks[KPROBE_TABLE_SIZE];
61
Naveen N. Rao290e3072017-04-19 18:21:01 +053062kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
63 unsigned int __unused)
Naveen N. Rao49e0b462017-04-19 18:21:00 +053064{
65 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
66}
67
Thomas Gleixnerec4846082009-07-25 16:09:17 +020068static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070069{
70 return &(kretprobe_table_locks[hash].lock);
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Masami Hiramatsu376e2422014-04-17 17:17:05 +090073/* Blacklist -- list of struct kprobe_blacklist_entry */
74static LIST_HEAD(kprobe_blacklist);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070075
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080076#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070077/*
78 * kprobe->ainsn.insn points to the copy of the instruction to be
79 * single-stepped. x86_64, POWER4 and above have no-exec support and
80 * stepping on the instruction on a vmalloced/kmalloced/data page
81 * is a recipe for disaster
82 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070083struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -040084 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070085 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -070086 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070087 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080088 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050089 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070090};
91
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050092#define KPROBE_INSN_PAGE_SIZE(slots) \
93 (offsetof(struct kprobe_insn_page, slot_used) + \
94 (sizeof(char) * (slots)))
95
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050096static int slots_per_page(struct kprobe_insn_cache *c)
97{
98 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
99}
100
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800101enum kprobe_slot_state {
102 SLOT_CLEAN = 0,
103 SLOT_DIRTY = 1,
104 SLOT_USED = 2,
105};
106
Masami Hiramatsu63fef142017-08-18 17:24:00 +0900107void __weak *alloc_insn_page(void)
Heiko Carstensaf963972013-09-11 14:24:13 -0700108{
109 return module_alloc(PAGE_SIZE);
110}
111
Masami Hiramatsuc93f5cf2017-05-25 19:38:17 +0900112void __weak free_insn_page(void *page)
Heiko Carstensaf963972013-09-11 14:24:13 -0700113{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030114 module_memfree(page);
Heiko Carstensaf963972013-09-11 14:24:13 -0700115}
116
Heiko Carstensc802d642013-09-11 14:24:11 -0700117struct kprobe_insn_cache kprobe_insn_slots = {
118 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700119 .alloc = alloc_insn_page,
120 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300121 .sym = KPROBE_INSN_PAGE_SYM,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500122 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
123 .insn_size = MAX_INSN_SIZE,
124 .nr_garbage = 0,
125};
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900126static int collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800127
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700128/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800129 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700130 * We allocate an executable page if there's no room on existing ones.
131 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900132kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700133{
134 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700135 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700136
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900137 /* Since the slot array is not protected by rcu, we need a mutex */
Heiko Carstensc802d642013-09-11 14:24:11 -0700138 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700139 retry:
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900140 rcu_read_lock();
141 list_for_each_entry_rcu(kip, &c->pages, list) {
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500142 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700143 int i;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500144 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800145 if (kip->slot_used[i] == SLOT_CLEAN) {
146 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700147 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700148 slot = kip->insns + (i * c->insn_size);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900149 rcu_read_unlock();
Heiko Carstensc802d642013-09-11 14:24:11 -0700150 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700151 }
152 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500153 /* kip->nused is broken. Fix it. */
154 kip->nused = slots_per_page(c);
155 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700156 }
157 }
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900158 rcu_read_unlock();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700159
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800160 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500161 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800162 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500163
164 /* All out of space. Need to allocate a new page. */
165 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700166 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700167 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700168
169 /*
170 * Use module_alloc so this page is within +/- 2GB of where the
171 * kernel image and loaded module images reside. This is required
172 * so x86_64 can correctly handle the %rip-relative fixups.
173 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700174 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700175 if (!kip->insns) {
176 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700177 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700178 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400179 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500180 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800181 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700182 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800183 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700184 kip->cache = c;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900185 list_add_rcu(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700186 slot = kip->insns;
187out:
188 mutex_unlock(&c->mutex);
189 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800190}
191
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800192/* Return 1 if all garbages are collected, otherwise 0. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900193static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800194{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800195 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800196 kip->nused--;
197 if (kip->nused == 0) {
198 /*
199 * Page is no longer in use. Free it unless
200 * it's the last one. We keep the last one
201 * so as not to have to set it up again the
202 * next time somebody inserts a probe.
203 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500204 if (!list_is_singular(&kip->list)) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900205 list_del_rcu(&kip->list);
206 synchronize_rcu();
Heiko Carstensaf963972013-09-11 14:24:13 -0700207 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800208 kfree(kip);
209 }
210 return 1;
211 }
212 return 0;
213}
214
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900215static int collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800216{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400217 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800218
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500219 /* Ensure no-one is interrupted on the garbages */
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -0800220 synchronize_rcu();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800221
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500222 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800223 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800224 if (kip->ngarbage == 0)
225 continue;
226 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500227 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900228 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800229 break;
230 }
231 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500232 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800233 return 0;
234}
235
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900236void __free_insn_slot(struct kprobe_insn_cache *c,
237 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500238{
239 struct kprobe_insn_page *kip;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900240 long idx;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500241
Heiko Carstensc802d642013-09-11 14:24:11 -0700242 mutex_lock(&c->mutex);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900243 rcu_read_lock();
244 list_for_each_entry_rcu(kip, &c->pages, list) {
245 idx = ((long)slot - (long)kip->insns) /
246 (c->insn_size * sizeof(kprobe_opcode_t));
247 if (idx >= 0 && idx < slots_per_page(c))
Heiko Carstensc802d642013-09-11 14:24:11 -0700248 goto out;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900249 }
250 /* Could not find this slot. */
251 WARN_ON(1);
252 kip = NULL;
253out:
254 rcu_read_unlock();
255 /* Mark and sweep: this may sleep */
256 if (kip) {
257 /* Check double free */
258 WARN_ON(kip->slot_used[idx] != SLOT_USED);
259 if (dirty) {
260 kip->slot_used[idx] = SLOT_DIRTY;
261 kip->ngarbage++;
262 if (++c->nr_garbage > slots_per_page(c))
263 collect_garbage_slots(c);
264 } else {
265 collect_one_slot(kip, idx);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500266 }
267 }
Heiko Carstensc802d642013-09-11 14:24:11 -0700268 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500269}
270
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900271/*
272 * Check given address is on the page of kprobe instruction slots.
273 * This will be used for checking whether the address on a stack
274 * is on a text area or not.
275 */
276bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
277{
278 struct kprobe_insn_page *kip;
279 bool ret = false;
280
281 rcu_read_lock();
282 list_for_each_entry_rcu(kip, &c->pages, list) {
283 if (addr >= (unsigned long)kip->insns &&
284 addr < (unsigned long)kip->insns + PAGE_SIZE) {
285 ret = true;
286 break;
287 }
288 }
289 rcu_read_unlock();
290
291 return ret;
292}
293
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300294int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
295 unsigned long *value, char *type, char *sym)
296{
297 struct kprobe_insn_page *kip;
298 int ret = -ERANGE;
299
300 rcu_read_lock();
301 list_for_each_entry_rcu(kip, &c->pages, list) {
302 if ((*symnum)--)
303 continue;
304 strlcpy(sym, c->sym, KSYM_NAME_LEN);
305 *type = 't';
306 *value = (unsigned long)kip->insns;
307 ret = 0;
308 break;
309 }
310 rcu_read_unlock();
311
312 return ret;
313}
314
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500315#ifdef CONFIG_OPTPROBES
316/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700317struct kprobe_insn_cache kprobe_optinsn_slots = {
318 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700319 .alloc = alloc_insn_page,
320 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300321 .sym = KPROBE_OPTINSN_PAGE_SYM,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500322 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
323 /* .insn_size is initialized later */
324 .nr_garbage = 0,
325};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500326#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800327#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700328
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800329/* We have preemption disabled.. so it is safe to use __ versions */
330static inline void set_kprobe_instance(struct kprobe *kp)
331{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600332 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800333}
334
335static inline void reset_kprobe_instance(void)
336{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600337 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800338}
339
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800340/*
341 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800342 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800343 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800344 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800345 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900346struct kprobe *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800349 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800352 hlist_for_each_entry_rcu(p, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (p->addr == addr)
354 return p;
355 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return NULL;
358}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900359NOKPROBE_SYMBOL(get_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900361static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500362
363/* Return true if the kprobe is an aggregator */
364static inline int kprobe_aggrprobe(struct kprobe *p)
365{
366 return p->pre_handler == aggr_pre_handler;
367}
368
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900369/* Return true(!0) if the kprobe is unused */
370static inline int kprobe_unused(struct kprobe *p)
371{
372 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
373 list_empty(&p->list);
374}
375
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500376/*
377 * Keep all fields in the kprobe consistent
378 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900379static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500380{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900381 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
382 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500383}
384
385#ifdef CONFIG_OPTPROBES
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500386/* NOTE: change this value only with kprobe_mutex held */
387static bool kprobes_allow_optimization;
388
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500389/*
390 * Call all pre_handler on the list, but ignores its return value.
391 * This must be called from arch-dep optimized caller.
392 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900393void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500394{
395 struct kprobe *kp;
396
397 list_for_each_entry_rcu(kp, &p->list, list) {
398 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
399 set_kprobe_instance(kp);
Naveen N. Rao4f3a8712017-10-17 13:48:34 +0530400 kp->pre_handler(kp, regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500401 }
402 reset_kprobe_instance();
403 }
404}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900405NOKPROBE_SYMBOL(opt_pre_handler);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500406
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900407/* Free optimized instructions and optimized_kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900408static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900409{
410 struct optimized_kprobe *op;
411
412 op = container_of(p, struct optimized_kprobe, kp);
413 arch_remove_optimized_kprobe(op);
414 arch_remove_kprobe(p);
415 kfree(op);
416}
417
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500418/* Return true(!0) if the kprobe is ready for optimization. */
419static inline int kprobe_optready(struct kprobe *p)
420{
421 struct optimized_kprobe *op;
422
423 if (kprobe_aggrprobe(p)) {
424 op = container_of(p, struct optimized_kprobe, kp);
425 return arch_prepared_optinsn(&op->optinsn);
426 }
427
428 return 0;
429}
430
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900431/* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
432static inline int kprobe_disarmed(struct kprobe *p)
433{
434 struct optimized_kprobe *op;
435
436 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
437 if (!kprobe_aggrprobe(p))
438 return kprobe_disabled(p);
439
440 op = container_of(p, struct optimized_kprobe, kp);
441
442 return kprobe_disabled(p) && list_empty(&op->list);
443}
444
445/* Return true(!0) if the probe is queued on (un)optimizing lists */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900446static int kprobe_queued(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900447{
448 struct optimized_kprobe *op;
449
450 if (kprobe_aggrprobe(p)) {
451 op = container_of(p, struct optimized_kprobe, kp);
452 if (!list_empty(&op->list))
453 return 1;
454 }
455 return 0;
456}
457
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500458/*
459 * Return an optimized kprobe whose optimizing code replaces
460 * instructions including addr (exclude breakpoint).
461 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900462static struct kprobe *get_optimized_kprobe(unsigned long addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500463{
464 int i;
465 struct kprobe *p = NULL;
466 struct optimized_kprobe *op;
467
468 /* Don't check i == 0, since that is a breakpoint case. */
469 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
470 p = get_kprobe((void *)(addr - i));
471
472 if (p && kprobe_optready(p)) {
473 op = container_of(p, struct optimized_kprobe, kp);
474 if (arch_within_optimized_kprobe(op, addr))
475 return p;
476 }
477
478 return NULL;
479}
480
481/* Optimization staging list, protected by kprobe_mutex */
482static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900483static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900484static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500485
486static void kprobe_optimizer(struct work_struct *work);
487static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
488#define OPTIMIZE_DELAY 5
489
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900490/*
491 * Optimize (replace a breakpoint with a jump) kprobes listed on
492 * optimizing_list.
493 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900494static void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500495{
Andrea Righif1c6ece2019-08-12 20:43:02 +0200496 lockdep_assert_held(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500497 /*
498 * The optimization/unoptimization refers online_cpus via
499 * stop_machine() and cpu-hotplug modifies online_cpus.
500 * And same time, text_mutex will be held in cpu-hotplug and here.
501 * This combination can cause a deadlock (cpu-hotplug try to lock
502 * text_mutex but stop_machine can not be done because online_cpus
503 * has been changed)
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200504 * To avoid this deadlock, caller must have locked cpu hotplug
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500505 * for preventing cpu-hotplug outside of text_mutex locking.
506 */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200507 lockdep_assert_cpus_held();
508
509 /* Optimization never be done when disarmed */
510 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
511 list_empty(&optimizing_list))
512 return;
513
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900514 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900515}
516
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900517/*
518 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
519 * if need) kprobes listed on unoptimizing_list.
520 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900521static void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900522{
523 struct optimized_kprobe *op, *tmp;
524
Andrea Righif1c6ece2019-08-12 20:43:02 +0200525 lockdep_assert_held(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200526 /* See comment in do_optimize_kprobes() */
527 lockdep_assert_cpus_held();
528
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900529 /* Unoptimization must be done anytime */
530 if (list_empty(&unoptimizing_list))
531 return;
532
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900533 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900534 /* Loop free_list for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900535 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900536 /* Switching from detour code to origin */
537 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900538 /* Disarm probes if marked disabled */
539 if (kprobe_disabled(&op->kp))
540 arch_disarm_kprobe(&op->kp);
541 if (kprobe_unused(&op->kp)) {
542 /*
543 * Remove unused probes from hash list. After waiting
544 * for synchronization, these probes are reclaimed.
545 * (reclaiming is done by do_free_cleaned_kprobes.)
546 */
547 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900548 } else
549 list_del_init(&op->list);
550 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900551}
552
553/* Reclaim all kprobes on the free_list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900554static void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900555{
556 struct optimized_kprobe *op, *tmp;
557
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900558 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900559 list_del_init(&op->list);
Masami Hiramatsucbdd96f2018-09-11 19:21:09 +0900560 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
561 /*
562 * This must not happen, but if there is a kprobe
563 * still in use, keep it on kprobes hash list.
564 */
565 continue;
566 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900567 free_aggr_kprobe(&op->kp);
568 }
569}
570
571/* Start optimizer after OPTIMIZE_DELAY passed */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900572static void kick_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900573{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800574 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900575}
576
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900577/* Kprobe jump optimizer */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900578static void kprobe_optimizer(struct work_struct *work)
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900579{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900580 mutex_lock(&kprobe_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200581 cpus_read_lock();
Andrea Righif1c6ece2019-08-12 20:43:02 +0200582 mutex_lock(&text_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900583 /* Lock modules while optimizing kprobes */
584 mutex_lock(&module_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900585
586 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900587 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
588 * kprobes before waiting for quiesence period.
589 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900590 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900591
592 /*
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900593 * Step 2: Wait for quiesence period to ensure all potentially
594 * preempted tasks to have normally scheduled. Because optprobe
595 * may modify multiple instructions, there is a chance that Nth
596 * instruction is preempted. In that case, such tasks can return
597 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
598 * Note that on non-preemptive kernel, this is transparently converted
599 * to synchronoze_sched() to wait for all interrupts to have completed.
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900600 */
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900601 synchronize_rcu_tasks();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900602
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900603 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900604 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900605
606 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900607 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900608
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500609 mutex_unlock(&module_mutex);
Andrea Righif1c6ece2019-08-12 20:43:02 +0200610 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200611 cpus_read_unlock();
Steven Rostedt72ef3792012-06-05 19:28:14 +0900612 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900613
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900614 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900615 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900616 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900617}
618
619/* Wait for completing optimization and unoptimization */
Thomas Gleixner30e7d8942017-05-17 10:19:49 +0200620void wait_for_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900621{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800622 mutex_lock(&kprobe_mutex);
623
624 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
625 mutex_unlock(&kprobe_mutex);
626
627 /* this will also make optimizing_work execute immmediately */
628 flush_delayed_work(&optimizing_work);
629 /* @optimizing_work might not have been queued yet, relax */
630 cpu_relax();
631
632 mutex_lock(&kprobe_mutex);
633 }
634
635 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500636}
637
Masami Hiramatsue4add242020-01-07 23:42:24 +0900638static bool optprobe_queued_unopt(struct optimized_kprobe *op)
639{
640 struct optimized_kprobe *_op;
641
642 list_for_each_entry(_op, &unoptimizing_list, list) {
643 if (op == _op)
644 return true;
645 }
646
647 return false;
648}
649
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500650/* Optimize kprobe if p is ready to be optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900651static void optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500652{
653 struct optimized_kprobe *op;
654
655 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500656 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500657 (kprobe_disabled(p) || kprobes_all_disarmed))
658 return;
659
Masami Hiramatsu059053a2018-06-20 01:10:27 +0900660 /* kprobes with post_handler can not be optimized */
661 if (p->post_handler)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500662 return;
663
664 op = container_of(p, struct optimized_kprobe, kp);
665
666 /* Check there is no other kprobes at the optimized instructions */
667 if (arch_check_optimized_kprobe(op) < 0)
668 return;
669
670 /* Check if it is already optimized. */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900671 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
672 if (optprobe_queued_unopt(op)) {
673 /* This is under unoptimizing. Just dequeue the probe */
674 list_del_init(&op->list);
675 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500676 return;
Masami Hiramatsue4add242020-01-07 23:42:24 +0900677 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500678 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900679
Masami Hiramatsue4add242020-01-07 23:42:24 +0900680 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
681 if (WARN_ON_ONCE(!list_empty(&op->list)))
682 return;
683
684 list_add(&op->list, &optimizing_list);
685 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900686}
687
688/* Short cut to direct unoptimizing */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900689static void force_unoptimize_kprobe(struct optimized_kprobe *op)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900690{
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200691 lockdep_assert_cpus_held();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900692 arch_unoptimize_kprobe(op);
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900693 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900694 if (kprobe_disabled(&op->kp))
695 arch_disarm_kprobe(&op->kp);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500696}
697
698/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900699static void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500700{
701 struct optimized_kprobe *op;
702
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900703 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
704 return; /* This is not an optprobe nor optimized */
705
706 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900707 if (!kprobe_optimized(p))
708 return;
709
710 if (!list_empty(&op->list)) {
711 if (optprobe_queued_unopt(op)) {
712 /* Queued in unoptimizing queue */
713 if (force) {
714 /*
715 * Forcibly unoptimize the kprobe here, and queue it
716 * in the freeing list for release afterwards.
717 */
718 force_unoptimize_kprobe(op);
719 list_move(&op->list, &freeing_list);
720 }
721 } else {
722 /* Dequeue from the optimizing queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500723 list_del_init(&op->list);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900724 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900725 }
726 return;
727 }
728
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900729 /* Optimized kprobe case */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900730 if (force) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900731 /* Forcibly update the code: this is a special case */
732 force_unoptimize_kprobe(op);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900733 } else {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900734 list_add(&op->list, &unoptimizing_list);
735 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500736 }
737}
738
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900739/* Cancel unoptimizing for reusing */
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900740static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900741{
742 struct optimized_kprobe *op;
743
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900744 /*
745 * Unused kprobe MUST be on the way of delayed unoptimizing (means
746 * there is still a relative jump) and disabled.
747 */
748 op = container_of(ap, struct optimized_kprobe, kp);
Masami Hiramatsu44585152018-04-28 21:36:33 +0900749 WARN_ON_ONCE(list_empty(&op->list));
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900750 /* Enable the probe again */
751 ap->flags &= ~KPROBE_FLAG_DISABLED;
752 /* Optimize it again (remove from op->list) */
Masami Hiramatsu5f843ed2019-04-15 15:01:25 +0900753 if (!kprobe_optready(ap))
754 return -EINVAL;
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900755
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900756 optimize_kprobe(ap);
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900757 return 0;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900758}
759
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500760/* Remove optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900761static void kill_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500762{
763 struct optimized_kprobe *op;
764
765 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900766 if (!list_empty(&op->list))
767 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500768 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900769 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900770
771 if (kprobe_unused(p)) {
772 /* Enqueue if it is unused */
773 list_add(&op->list, &freeing_list);
774 /*
775 * Remove unused probes from the hash list. After waiting
776 * for synchronization, this probe is reclaimed.
777 * (reclaiming is done by do_free_cleaned_kprobes().)
778 */
779 hlist_del_rcu(&op->kp.hlist);
780 }
781
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900782 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500783 arch_remove_optimized_kprobe(op);
784}
785
Masami Hiramatsua4602462017-04-19 18:22:25 +0530786static inline
787void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
788{
789 if (!kprobe_ftrace(p))
790 arch_prepare_optimized_kprobe(op, p);
791}
792
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500793/* Try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900794static void prepare_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500795{
796 struct optimized_kprobe *op;
797
798 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsua4602462017-04-19 18:22:25 +0530799 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500800}
801
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500802/* Allocate new optimized_kprobe and try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900803static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500804{
805 struct optimized_kprobe *op;
806
807 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
808 if (!op)
809 return NULL;
810
811 INIT_LIST_HEAD(&op->list);
812 op->kp.addr = p->addr;
Masami Hiramatsua4602462017-04-19 18:22:25 +0530813 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500814
815 return &op->kp;
816}
817
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900818static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500819
820/*
821 * Prepare an optimized_kprobe and optimize it
822 * NOTE: p must be a normal registered kprobe
823 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900824static void try_to_optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500825{
826 struct kprobe *ap;
827 struct optimized_kprobe *op;
828
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900829 /* Impossible to optimize ftrace-based kprobe */
830 if (kprobe_ftrace(p))
831 return;
832
Masami Hiramatsu25764282012-06-05 19:28:26 +0900833 /* For preparing optimization, jump_label_text_reserved() is called */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200834 cpus_read_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +0900835 jump_label_lock();
836 mutex_lock(&text_mutex);
837
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500838 ap = alloc_aggr_kprobe(p);
839 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900840 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500841
842 op = container_of(ap, struct optimized_kprobe, kp);
843 if (!arch_prepared_optinsn(&op->optinsn)) {
844 /* If failed to setup optimizing, fallback to kprobe */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900845 arch_remove_optimized_kprobe(op);
846 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900847 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500848 }
849
850 init_aggr_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900851 optimize_kprobe(ap); /* This just kicks optimizer thread */
852
853out:
854 mutex_unlock(&text_mutex);
855 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200856 cpus_read_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500857}
858
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500859#ifdef CONFIG_SYSCTL
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900860static void optimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500861{
862 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500863 struct kprobe *p;
864 unsigned int i;
865
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900866 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500867 /* If optimization is already allowed, just return */
868 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900869 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500870
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200871 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500872 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500873 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
874 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800875 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500876 if (!kprobe_disabled(p))
877 optimize_kprobe(p);
878 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200879 cpus_read_unlock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500880 printk(KERN_INFO "Kprobes globally optimized\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900881out:
882 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500883}
884
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900885static void unoptimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500886{
887 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500888 struct kprobe *p;
889 unsigned int i;
890
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900891 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500892 /* If optimization is already prohibited, just return */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900893 if (!kprobes_allow_optimization) {
894 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500895 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900896 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500897
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200898 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500899 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500900 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
901 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800902 hlist_for_each_entry_rcu(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500903 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900904 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500905 }
906 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200907 cpus_read_unlock();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900908 mutex_unlock(&kprobe_mutex);
909
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900910 /* Wait for unoptimizing completion */
911 wait_for_kprobe_optimizer();
912 printk(KERN_INFO "Kprobes globally unoptimized\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500913}
914
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900915static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500916int sysctl_kprobes_optimization;
917int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200918 void *buffer, size_t *length,
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500919 loff_t *ppos)
920{
921 int ret;
922
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900923 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500924 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
925 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
926
927 if (sysctl_kprobes_optimization)
928 optimize_all_kprobes();
929 else
930 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900931 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500932
933 return ret;
934}
935#endif /* CONFIG_SYSCTL */
936
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900937/* Put a breakpoint for a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900938static void __arm_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500939{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900940 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500941
942 /* Check collision with other optimized kprobes */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900943 _p = get_optimized_kprobe((unsigned long)p->addr);
944 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900945 /* Fallback to unoptimized kprobe */
946 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500947
948 arch_arm_kprobe(p);
949 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
950}
951
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900952/* Remove the breakpoint of a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900953static void __disarm_kprobe(struct kprobe *p, bool reopt)
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
Wang Nan69d54b92015-02-13 14:40:26 -0800957 /* Try to unoptimize */
958 unoptimize_kprobe(p, kprobes_all_disarmed);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500959
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900960 if (!kprobe_queued(p)) {
961 arch_disarm_kprobe(p);
962 /* If another kprobe was blocked, optimize it. */
963 _p = get_optimized_kprobe((unsigned long)p->addr);
964 if (unlikely(_p) && reopt)
965 optimize_kprobe(_p);
966 }
967 /* TODO: reoptimize others after unoptimized this probe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500968}
969
970#else /* !CONFIG_OPTPROBES */
971
972#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900973#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500974#define kill_optimized_kprobe(p) do {} while (0)
975#define prepare_optimized_kprobe(p) do {} while (0)
976#define try_to_optimize_kprobe(p) do {} while (0)
977#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900978#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
979#define kprobe_disarmed(p) kprobe_disabled(p)
980#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500981
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900982static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900983{
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900984 /*
985 * If the optimized kprobe is NOT supported, the aggr kprobe is
986 * released at the same time that the last aggregated kprobe is
987 * unregistered.
988 * Thus there should be no chance to reuse unused kprobe.
989 */
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900990 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900991 return -EINVAL;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900992}
993
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900994static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500995{
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900996 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500997 kfree(p);
998}
999
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001000static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001001{
1002 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1003}
1004#endif /* CONFIG_OPTPROBES */
1005
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001006#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001007static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +09001008 .func = kprobe_ftrace_handler,
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001009 .flags = FTRACE_OPS_FL_SAVE_REGS,
1010};
1011
1012static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1013 .func = kprobe_ftrace_handler,
Masami Hiramatsu1d70be32014-11-21 05:25:23 -05001014 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001015};
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001016
1017static int kprobe_ipmodify_enabled;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001018static int kprobe_ftrace_enabled;
1019
1020/* Must ensure p->addr is really on ftrace */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001021static int prepare_kprobe(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001022{
1023 if (!kprobe_ftrace(p))
1024 return arch_prepare_kprobe(p);
1025
1026 return arch_prepare_kprobe_ftrace(p);
1027}
1028
1029/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001030static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1031 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001032{
Jessica Yu12310e342018-01-10 00:51:23 +01001033 int ret = 0;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001034
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001035 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001036 if (ret) {
Masami Hiramatsu44585152018-04-28 21:36:33 +09001037 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1038 p->addr, ret);
Jessica Yu12310e342018-01-10 00:51:23 +01001039 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001040 }
Jessica Yu12310e342018-01-10 00:51:23 +01001041
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001042 if (*cnt == 0) {
1043 ret = register_ftrace_function(ops);
Jessica Yu12310e342018-01-10 00:51:23 +01001044 if (ret) {
1045 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1046 goto err_ftrace;
1047 }
1048 }
1049
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001050 (*cnt)++;
Jessica Yu12310e342018-01-10 00:51:23 +01001051 return ret;
1052
1053err_ftrace:
1054 /*
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001055 * At this point, sinec ops is not registered, we should be sefe from
1056 * registering empty filter.
Jessica Yu12310e342018-01-10 00:51:23 +01001057 */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001058 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001059 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001060}
1061
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001062static int arm_kprobe_ftrace(struct kprobe *p)
1063{
1064 bool ipmodify = (p->post_handler != NULL);
1065
1066 return __arm_kprobe_ftrace(p,
1067 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1068 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1069}
1070
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001071/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001072static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1073 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001074{
Jessica Yu297f9232018-01-10 00:51:24 +01001075 int ret = 0;
1076
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001077 if (*cnt == 1) {
1078 ret = unregister_ftrace_function(ops);
Jessica Yu297f9232018-01-10 00:51:24 +01001079 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1080 return ret;
1081 }
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001082
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001083 (*cnt)--;
Jessica Yu297f9232018-01-10 00:51:24 +01001084
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001085 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Masami Hiramatsu44585152018-04-28 21:36:33 +09001086 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1087 p->addr, ret);
Jessica Yu297f9232018-01-10 00:51:24 +01001088 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001089}
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001090
1091static int disarm_kprobe_ftrace(struct kprobe *p)
1092{
1093 bool ipmodify = (p->post_handler != NULL);
1094
1095 return __disarm_kprobe_ftrace(p,
1096 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1097 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1098}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001099#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001100#define prepare_kprobe(p) arch_prepare_kprobe(p)
Jessica Yu12310e342018-01-10 00:51:23 +01001101#define arm_kprobe_ftrace(p) (-ENODEV)
Jessica Yu297f9232018-01-10 00:51:24 +01001102#define disarm_kprobe_ftrace(p) (-ENODEV)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001103#endif
1104
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001105/* Arm a kprobe with text_mutex */
Jessica Yu12310e342018-01-10 00:51:23 +01001106static int arm_kprobe(struct kprobe *kp)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001107{
Jessica Yu12310e342018-01-10 00:51:23 +01001108 if (unlikely(kprobe_ftrace(kp)))
1109 return arm_kprobe_ftrace(kp);
1110
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001111 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001112 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001113 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001114 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001115 cpus_read_unlock();
Jessica Yu12310e342018-01-10 00:51:23 +01001116
1117 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001118}
1119
1120/* Disarm a kprobe with text_mutex */
Jessica Yu297f9232018-01-10 00:51:24 +01001121static int disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001122{
Jessica Yu297f9232018-01-10 00:51:24 +01001123 if (unlikely(kprobe_ftrace(kp)))
1124 return disarm_kprobe_ftrace(kp);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001125
1126 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001127 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001128 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001129 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001130 cpus_read_unlock();
Jessica Yu297f9232018-01-10 00:51:24 +01001131
1132 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001133}
1134
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001135/*
1136 * Aggregate handlers for multiple kprobes support - these handlers
1137 * take care of invoking the individual kprobe handlers on p->list
1138 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001139static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001140{
1141 struct kprobe *kp;
1142
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001143 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001144 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001145 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001146 if (kp->pre_handler(kp, regs))
1147 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001148 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001149 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001150 }
1151 return 0;
1152}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001153NOKPROBE_SYMBOL(aggr_pre_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001154
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001155static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1156 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001157{
1158 struct kprobe *kp;
1159
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001160 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001161 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001162 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001163 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001164 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001165 }
1166 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001167}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001168NOKPROBE_SYMBOL(aggr_post_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001169
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001170static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1171 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001172{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001173 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001174
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001175 /*
1176 * if we faulted "during" the execution of a user specified
1177 * probe handler, invoke just that probe's fault handler
1178 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001179 if (cur && cur->fault_handler) {
1180 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001181 return 1;
1182 }
1183 return 0;
1184}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001185NOKPROBE_SYMBOL(aggr_fault_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001186
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001187/* Walks the list and increments nmissed count for multiprobe case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001188void kprobes_inc_nmissed_count(struct kprobe *p)
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001189{
1190 struct kprobe *kp;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001191 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001192 p->nmissed++;
1193 } else {
1194 list_for_each_entry_rcu(kp, &p->list, list)
1195 kp->nmissed++;
1196 }
1197 return;
1198}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001199NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001200
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001201void recycle_rp_inst(struct kretprobe_instance *ri,
1202 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001203{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001204 struct kretprobe *rp = ri->rp;
1205
Hien Nguyenb94cce92005-06-23 00:09:19 -07001206 /* remove rp inst off the rprobe_inst_table */
1207 hlist_del(&ri->hlist);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001208 INIT_HLIST_NODE(&ri->hlist);
1209 if (likely(rp)) {
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001210 raw_spin_lock(&rp->lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001211 hlist_add_head(&ri->hlist, &rp->free_instances);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001212 raw_spin_unlock(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001213 } else
1214 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -07001215 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001216}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001217NOKPROBE_SYMBOL(recycle_rp_inst);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001218
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001219void kretprobe_hash_lock(struct task_struct *tsk,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001220 struct hlist_head **head, unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001221__acquires(hlist_lock)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001222{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001223 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001224 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001225
1226 *head = &kretprobe_inst_table[hash];
1227 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001228 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001229}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001230NOKPROBE_SYMBOL(kretprobe_hash_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001231
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001232static void kretprobe_table_lock(unsigned long hash,
1233 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001234__acquires(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001235{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001236 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1237 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001238}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001239NOKPROBE_SYMBOL(kretprobe_table_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001240
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001241void kretprobe_hash_unlock(struct task_struct *tsk,
1242 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001243__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001244{
1245 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001246 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001247
1248 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001249 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001250}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001251NOKPROBE_SYMBOL(kretprobe_hash_unlock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001252
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001253static void kretprobe_table_unlock(unsigned long hash,
1254 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001255__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001256{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001257 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1258 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001259}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001260NOKPROBE_SYMBOL(kretprobe_table_unlock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001261
Hien Nguyenb94cce92005-06-23 00:09:19 -07001262/*
bibo maoc6fd91f2006-03-26 01:38:20 -08001263 * This function is called from finish_task_switch when task tk becomes dead,
1264 * so that we can recycle any function-return probe instances associated
1265 * with this task. These left over instances represent probed functions
1266 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001267 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001268void kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001269{
bibo,mao62c27be2006-10-02 02:17:33 -07001270 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -07001271 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001272 struct hlist_node *tmp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001273 unsigned long hash, flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -07001274
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001275 if (unlikely(!kprobes_initialized))
1276 /* Early boot. kretprobe_table_locks not yet initialized. */
1277 return;
1278
Ananth N Mavinakayanahallid496aab2012-01-20 14:34:04 -08001279 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001280 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1281 head = &kretprobe_inst_table[hash];
1282 kretprobe_table_lock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001283 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -07001284 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -07001285 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -07001286 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001287 kretprobe_table_unlock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001288 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -07001289 hlist_del(&ri->hlist);
1290 kfree(ri);
1291 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001292}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001293NOKPROBE_SYMBOL(kprobe_flush_task);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001294
Hien Nguyenb94cce92005-06-23 00:09:19 -07001295static inline void free_rp_inst(struct kretprobe *rp)
1296{
1297 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001298 struct hlist_node *next;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001299
Sasha Levinb67bfe02013-02-27 17:06:00 -08001300 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001301 hlist_del(&ri->hlist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001302 kfree(ri);
1303 }
1304}
1305
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001306static void cleanup_rp_inst(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001307{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001308 unsigned long flags, hash;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001309 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001310 struct hlist_node *next;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001311 struct hlist_head *head;
1312
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001313 /* No race here */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001314 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1315 kretprobe_table_lock(hash, &flags);
1316 head = &kretprobe_inst_table[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001317 hlist_for_each_entry_safe(ri, next, head, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001318 if (ri->rp == rp)
1319 ri->rp = NULL;
1320 }
1321 kretprobe_table_unlock(hash, &flags);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001322 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001323 free_rp_inst(rp);
1324}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001325NOKPROBE_SYMBOL(cleanup_rp_inst);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001326
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001327/* Add the new probe to ap->list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001328static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001329{
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001330 if (p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001331 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001332
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001333 list_add_rcu(&p->list, &ap->list);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001334 if (p->post_handler && !ap->post_handler)
1335 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001336
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001337 return 0;
1338}
1339
1340/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001341 * Fill in the required fields of the "manager kprobe". Replace the
1342 * earlier kprobe in the hlist with the manager kprobe
1343 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001344static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001345{
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001346 /* Copy p's insn slot to ap */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001347 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001348 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001349 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001350 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001351 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001352 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001353 /* We don't care the kprobe which has gone. */
1354 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001355 ap->post_handler = aggr_post_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001356
1357 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001358 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001359
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001360 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001361 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001362}
1363
1364/*
1365 * This is the second or subsequent kprobe at the address - handle
1366 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001367 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001368static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001369{
1370 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001371 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001372
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001373 cpus_read_lock();
1374
Masami Hiramatsu25764282012-06-05 19:28:26 +09001375 /* For preparing optimization, jump_label_text_reserved() is called */
1376 jump_label_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001377 mutex_lock(&text_mutex);
1378
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001379 if (!kprobe_aggrprobe(orig_p)) {
1380 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1381 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001382 if (!ap) {
1383 ret = -ENOMEM;
1384 goto out;
1385 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001386 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001387 } else if (kprobe_unused(ap)) {
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001388 /* This probe is going to die. Rescue it */
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001389 ret = reuse_unused_kprobe(ap);
1390 if (ret)
1391 goto out;
1392 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001393
1394 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001395 /*
1396 * Attempting to insert new probe at the same location that
1397 * had a probe in the module vaddr area which already
1398 * freed. So, the instruction slot has already been
1399 * released. We need a new slot for the new probe.
1400 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001401 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001402 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001403 /*
1404 * Even if fail to allocate new slot, don't need to
1405 * free aggr_probe. It will be used next time, or
1406 * freed by unregister_kprobe.
1407 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001408 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001409
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001410 /* Prepare optimized instructions if possible. */
1411 prepare_optimized_kprobe(ap);
1412
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001413 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001414 * Clear gone flag to prevent allocating new slot again, and
1415 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001416 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001417 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1418 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001419 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001420
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001421 /* Copy ap's insn slot to p */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001422 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001423 ret = add_new_kprobe(ap, p);
1424
1425out:
1426 mutex_unlock(&text_mutex);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001427 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001428 cpus_read_unlock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001429
1430 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1431 ap->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01001432 if (!kprobes_all_disarmed) {
Masami Hiramatsu25764282012-06-05 19:28:26 +09001433 /* Arm the breakpoint again. */
Jessica Yu12310e342018-01-10 00:51:23 +01001434 ret = arm_kprobe(ap);
1435 if (ret) {
1436 ap->flags |= KPROBE_FLAG_DISABLED;
1437 list_del_rcu(&p->list);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001438 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001439 }
1440 }
Masami Hiramatsu25764282012-06-05 19:28:26 +09001441 }
1442 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001443}
1444
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001445bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1446{
1447 /* The __kprobes marked functions and entry code must not be probed */
1448 return addr >= (unsigned long)__kprobes_text_start &&
1449 addr < (unsigned long)__kprobes_text_end;
1450}
1451
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001452static bool __within_kprobe_blacklist(unsigned long addr)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001453{
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001454 struct kprobe_blacklist_entry *ent;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001455
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001456 if (arch_within_kprobe_blacklist(addr))
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001457 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001458 /*
1459 * If there exists a kprobe_blacklist, verify and
1460 * fail any probe registration in the prohibited area
1461 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001462 list_for_each_entry(ent, &kprobe_blacklist, list) {
1463 if (addr >= ent->start_addr && addr < ent->end_addr)
1464 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001465 }
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001466 return false;
1467}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001468
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001469bool within_kprobe_blacklist(unsigned long addr)
1470{
1471 char symname[KSYM_NAME_LEN], *p;
1472
1473 if (__within_kprobe_blacklist(addr))
1474 return true;
1475
1476 /* Check if the address is on a suffixed-symbol */
1477 if (!lookup_symbol_name(addr, symname)) {
1478 p = strchr(symname, '.');
1479 if (!p)
1480 return false;
1481 *p = '\0';
1482 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1483 if (addr)
1484 return __within_kprobe_blacklist(addr);
1485 }
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001486 return false;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001487}
1488
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001489/*
1490 * If we have a symbol_name argument, look it up and add the offset field
1491 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001492 * This returns encoded errors if it fails to look up symbol or invalid
1493 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001494 */
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301495static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1496 const char *symbol_name, unsigned int offset)
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001497{
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301498 if ((symbol_name && addr) || (!symbol_name && !addr))
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001499 goto invalid;
1500
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301501 if (symbol_name) {
Linus Torvalds7246f602017-05-05 11:36:44 -07001502 addr = kprobe_lookup_name(symbol_name, offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001503 if (!addr)
1504 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001505 }
1506
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301507 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001508 if (addr)
1509 return addr;
1510
1511invalid:
1512 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001513}
1514
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301515static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1516{
1517 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1518}
1519
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301520/* Check passed kprobe is valid and return kprobe in kprobe_table. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001521static struct kprobe *__get_valid_kprobe(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301522{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001523 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301524
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001525 ap = get_kprobe(p->addr);
1526 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301527 return NULL;
1528
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001529 if (p != ap) {
1530 list_for_each_entry_rcu(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301531 if (list_p == p)
1532 /* kprobe p is a valid probe */
1533 goto valid;
1534 return NULL;
1535 }
1536valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001537 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301538}
1539
1540/* Return error if the kprobe is being re-registered */
1541static inline int check_kprobe_rereg(struct kprobe *p)
1542{
1543 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301544
1545 mutex_lock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001546 if (__get_valid_kprobe(p))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301547 ret = -EINVAL;
1548 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001549
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301550 return ret;
1551}
1552
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001553int __weak arch_check_ftrace_location(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001555 unsigned long ftrace_addr;
1556
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001557 ftrace_addr = ftrace_location((unsigned long)p->addr);
1558 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001559#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001560 /* Given address is not on the instruction boundary */
1561 if ((unsigned long)p->addr != ftrace_addr)
1562 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001563 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001564#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001565 return -EINVAL;
1566#endif
1567 }
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001568 return 0;
1569}
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001570
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001571static int check_kprobe_address_safe(struct kprobe *p,
1572 struct module **probed_mod)
1573{
1574 int ret;
1575
1576 ret = arch_check_ftrace_location(p);
1577 if (ret)
1578 return ret;
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001579 jump_label_lock();
1580 preempt_disable();
1581
1582 /* Ensure it is not in reserved area nor out of text */
1583 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001584 within_kprobe_blacklist((unsigned long) p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001585 jump_label_text_reserved(p->addr, p->addr) ||
1586 find_bug((unsigned long)p->addr)) {
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001587 ret = -EINVAL;
1588 goto out;
1589 }
1590
1591 /* Check if are we probing a module */
1592 *probed_mod = __module_text_address((unsigned long) p->addr);
1593 if (*probed_mod) {
1594 /*
1595 * We must hold a refcount of the probed module while updating
1596 * its code to prohibit unexpected unloading.
1597 */
1598 if (unlikely(!try_module_get(*probed_mod))) {
1599 ret = -ENOENT;
1600 goto out;
1601 }
1602
1603 /*
1604 * If the module freed .init.text, we couldn't insert
1605 * kprobes in there.
1606 */
1607 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1608 (*probed_mod)->state != MODULE_STATE_COMING) {
1609 module_put(*probed_mod);
1610 *probed_mod = NULL;
1611 ret = -ENOENT;
1612 }
1613 }
1614out:
1615 preempt_enable();
1616 jump_label_unlock();
1617
1618 return ret;
1619}
1620
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001621int register_kprobe(struct kprobe *p)
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001622{
1623 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001624 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001625 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001626 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001628 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001629 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001630 if (IS_ERR(addr))
1631 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001632 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001633
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301634 ret = check_kprobe_rereg(p);
1635 if (ret)
1636 return ret;
1637
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001638 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1639 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001640 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001641 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001642
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001643 ret = check_kprobe_address_safe(p, &probed_mod);
1644 if (ret)
1645 return ret;
1646
1647 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001648
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001649 old_p = get_kprobe(p->addr);
1650 if (old_p) {
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001651 /* Since this may unoptimize old_p, locking text_mutex. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001652 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 goto out;
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001656 cpus_read_lock();
1657 /* Prevent text modification */
1658 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001659 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001660 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001661 cpus_read_unlock();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001662 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001663 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001664
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001665 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001666 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1668
Jessica Yu12310e342018-01-10 00:51:23 +01001669 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1670 ret = arm_kprobe(p);
1671 if (ret) {
1672 hlist_del_rcu(&p->hlist);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001673 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001674 goto out;
1675 }
1676 }
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001677
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001678 /* Try to optimize kprobe */
1679 try_to_optimize_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001681 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001682
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001683 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001684 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return ret;
1687}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001688EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001690/* Check if all probes on the aggrprobe are disabled */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001691static int aggr_kprobe_disabled(struct kprobe *ap)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001692{
1693 struct kprobe *kp;
1694
1695 list_for_each_entry_rcu(kp, &ap->list, list)
1696 if (!kprobe_disabled(kp))
1697 /*
1698 * There is an active probe on the list.
1699 * We can't disable this ap.
1700 */
1701 return 0;
1702
1703 return 1;
1704}
1705
1706/* Disable one kprobe: Make sure called under kprobe_mutex is locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001707static struct kprobe *__disable_kprobe(struct kprobe *p)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001708{
1709 struct kprobe *orig_p;
Jessica Yu297f9232018-01-10 00:51:24 +01001710 int ret;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001711
1712 /* Get an original kprobe for return */
1713 orig_p = __get_valid_kprobe(p);
1714 if (unlikely(orig_p == NULL))
Jessica Yu297f9232018-01-10 00:51:24 +01001715 return ERR_PTR(-EINVAL);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001716
1717 if (!kprobe_disabled(p)) {
1718 /* Disable probe if it is a child probe */
1719 if (p != orig_p)
1720 p->flags |= KPROBE_FLAG_DISABLED;
1721
1722 /* Try to disarm and disable this/parent probe */
1723 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Wang Nan69d54b92015-02-13 14:40:26 -08001724 /*
1725 * If kprobes_all_disarmed is set, orig_p
1726 * should have already been disarmed, so
1727 * skip unneed disarming process.
1728 */
Jessica Yu297f9232018-01-10 00:51:24 +01001729 if (!kprobes_all_disarmed) {
1730 ret = disarm_kprobe(orig_p, true);
1731 if (ret) {
1732 p->flags &= ~KPROBE_FLAG_DISABLED;
1733 return ERR_PTR(ret);
1734 }
1735 }
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001736 orig_p->flags |= KPROBE_FLAG_DISABLED;
1737 }
1738 }
1739
1740 return orig_p;
1741}
1742
Masami Hiramatsu98616682008-04-28 02:14:28 -07001743/*
1744 * Unregister a kprobe without a scheduler synchronization.
1745 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001746static int __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001747{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001748 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001749
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001750 /* Disable kprobe. This will disarm it if needed. */
1751 ap = __disable_kprobe(p);
Jessica Yu297f9232018-01-10 00:51:24 +01001752 if (IS_ERR(ap))
1753 return PTR_ERR(ap);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001754
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001755 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001756 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001757 * This probe is an independent(and non-optimized) kprobe
1758 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001759 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001760 goto disarmed;
1761
1762 /* Following process expects this probe is an aggrprobe */
1763 WARN_ON(!kprobe_aggrprobe(ap));
1764
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001765 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1766 /*
1767 * !disarmed could be happen if the probe is under delayed
1768 * unoptimizing.
1769 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001770 goto disarmed;
1771 else {
1772 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001773 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001774 list_for_each_entry_rcu(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001775 if ((list_p != p) && (list_p->post_handler))
1776 goto noclean;
1777 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001778 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001779 }
1780noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001781 /*
1782 * Remove from the aggrprobe: this path will do nothing in
1783 * __unregister_kprobe_bottom().
1784 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001785 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001786 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1787 /*
1788 * Try to optimize this probe again, because post
1789 * handler may have been changed.
1790 */
1791 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001792 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001793 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001794
1795disarmed:
1796 hlist_del_rcu(&ap->hlist);
1797 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001798}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001799
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001800static void __unregister_kprobe_bottom(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001801{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001802 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001803
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001804 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001805 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001806 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001807 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001808 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001809 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001810 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001811 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001812 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001813 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001816int register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001817{
1818 int i, ret = 0;
1819
1820 if (num <= 0)
1821 return -EINVAL;
1822 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001823 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001824 if (ret < 0) {
1825 if (i > 0)
1826 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001827 break;
1828 }
1829 }
1830 return ret;
1831}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001832EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001833
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001834void unregister_kprobe(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001835{
1836 unregister_kprobes(&p, 1);
1837}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001838EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001839
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001840void unregister_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001841{
1842 int i;
1843
1844 if (num <= 0)
1845 return;
1846 mutex_lock(&kprobe_mutex);
1847 for (i = 0; i < num; i++)
1848 if (__unregister_kprobe_top(kps[i]) < 0)
1849 kps[i]->addr = NULL;
1850 mutex_unlock(&kprobe_mutex);
1851
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001852 synchronize_rcu();
Masami Hiramatsu98616682008-04-28 02:14:28 -07001853 for (i = 0; i < num; i++)
1854 if (kps[i]->addr)
1855 __unregister_kprobe_bottom(kps[i]);
1856}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001857EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001858
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301859int __weak kprobe_exceptions_notify(struct notifier_block *self,
1860 unsigned long val, void *data)
Naveen N. Raofc62d022017-02-08 01:24:14 +05301861{
1862 return NOTIFY_DONE;
1863}
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301864NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Naveen N. Raofc62d022017-02-08 01:24:14 +05301865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866static struct notifier_block kprobe_exceptions_nb = {
1867 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001868 .priority = 0x7fffffff /* we need to be notified first */
1869};
1870
Michael Ellerman3d7e3382007-07-19 01:48:11 -07001871unsigned long __weak arch_deref_entry_point(void *entry)
1872{
1873 return (unsigned long)entry;
1874}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001876#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -08001877/*
1878 * This kprobe pre_handler is registered with every kretprobe. When probe
1879 * hits it will set up the return probe.
1880 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001881static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Adrian Bunke65cefe2006-02-03 03:03:42 -08001882{
1883 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001884 unsigned long hash, flags = 0;
1885 struct kretprobe_instance *ri;
Adrian Bunke65cefe2006-02-03 03:03:42 -08001886
Masami Hiramatsuf96f5672014-08-04 03:10:16 +00001887 /*
1888 * To avoid deadlocks, prohibit return probing in NMI contexts,
1889 * just skip the probe and increase the (inexact) 'nmissed'
1890 * statistical counter, so that the user is informed that
1891 * something happened:
1892 */
1893 if (unlikely(in_nmi())) {
1894 rp->nmissed++;
1895 return 0;
1896 }
1897
1898 /* TODO: consider to only swap the RA after the last pre_handler fired */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001899 hash = hash_ptr(current, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001900 raw_spin_lock_irqsave(&rp->lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001901 if (!hlist_empty(&rp->free_instances)) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001902 ri = hlist_entry(rp->free_instances.first,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001903 struct kretprobe_instance, hlist);
1904 hlist_del(&ri->hlist);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001905 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001906
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001907 ri->rp = rp;
1908 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001909
Jiang Liu55ca6142012-02-03 15:37:16 -08001910 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
1911 raw_spin_lock_irqsave(&rp->lock, flags);
1912 hlist_add_head(&ri->hlist, &rp->free_instances);
1913 raw_spin_unlock_irqrestore(&rp->lock, flags);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001914 return 0;
Jiang Liu55ca6142012-02-03 15:37:16 -08001915 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001916
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001917 arch_prepare_kretprobe(ri, regs);
1918
1919 /* XXX(hch): why is there no hlist_move_head? */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001920 INIT_HLIST_NODE(&ri->hlist);
1921 kretprobe_table_lock(hash, &flags);
1922 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
1923 kretprobe_table_unlock(hash, &flags);
1924 } else {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001925 rp->nmissed++;
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001926 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001927 }
Adrian Bunke65cefe2006-02-03 03:03:42 -08001928 return 0;
1929}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001930NOKPROBE_SYMBOL(pre_handler_kretprobe);
Adrian Bunke65cefe2006-02-03 03:03:42 -08001931
Naveen N. Rao659b9572017-07-07 22:37:24 +05301932bool __weak arch_kprobe_on_func_entry(unsigned long offset)
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301933{
1934 return !offset;
1935}
1936
Naveen N. Rao659b9572017-07-07 22:37:24 +05301937bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301938{
1939 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
1940
1941 if (IS_ERR(kp_addr))
1942 return false;
1943
1944 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) ||
Naveen N. Rao659b9572017-07-07 22:37:24 +05301945 !arch_kprobe_on_func_entry(offset))
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301946 return false;
1947
1948 return true;
1949}
1950
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001951int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001952{
1953 int ret = 0;
1954 struct kretprobe_instance *inst;
1955 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001956 void *addr;
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301957
Naveen N. Rao659b9572017-07-07 22:37:24 +05301958 if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset))
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301959 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001960
1961 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001962 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001963 if (IS_ERR(addr))
1964 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001965
1966 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1967 if (kretprobe_blacklist[i].addr == addr)
1968 return -EINVAL;
1969 }
1970 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001971
1972 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07001973 rp->kp.post_handler = NULL;
1974 rp->kp.fault_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001975
1976 /* Pre-allocate memory for max kretprobe instances */
1977 if (rp->maxactive <= 0) {
Thomas Gleixner92616602019-07-26 23:19:41 +02001978#ifdef CONFIG_PREEMPTION
Heiko Carstensc2ef6662009-12-21 13:02:24 +01001979 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07001980#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05301981 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001982#endif
1983 }
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001984 raw_spin_lock_init(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001985 INIT_HLIST_HEAD(&rp->free_instances);
1986 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001987 inst = kmalloc(sizeof(struct kretprobe_instance) +
1988 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001989 if (inst == NULL) {
1990 free_rp_inst(rp);
1991 return -ENOMEM;
1992 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001993 INIT_HLIST_NODE(&inst->hlist);
1994 hlist_add_head(&inst->hlist, &rp->free_instances);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001995 }
1996
1997 rp->nmissed = 0;
1998 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001999 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002000 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002001 free_rp_inst(rp);
2002 return ret;
2003}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002004EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002005
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002006int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002007{
2008 int ret = 0, i;
2009
2010 if (num <= 0)
2011 return -EINVAL;
2012 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002013 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07002014 if (ret < 0) {
2015 if (i > 0)
2016 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002017 break;
2018 }
2019 }
2020 return ret;
2021}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002022EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002023
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002024void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002025{
2026 unregister_kretprobes(&rp, 1);
2027}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002028EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002029
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002030void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002031{
2032 int i;
2033
2034 if (num <= 0)
2035 return;
2036 mutex_lock(&kprobe_mutex);
2037 for (i = 0; i < num; i++)
2038 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2039 rps[i]->kp.addr = NULL;
2040 mutex_unlock(&kprobe_mutex);
2041
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08002042 synchronize_rcu();
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002043 for (i = 0; i < num; i++) {
2044 if (rps[i]->kp.addr) {
2045 __unregister_kprobe_bottom(&rps[i]->kp);
2046 cleanup_rp_inst(rps[i]);
2047 }
2048 }
2049}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002050EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002051
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002052#else /* CONFIG_KRETPROBES */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002053int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002054{
2055 return -ENOSYS;
2056}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002057EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002058
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002059int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002060{
2061 return -ENOSYS;
2062}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002063EXPORT_SYMBOL_GPL(register_kretprobes);
2064
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002065void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002066{
2067}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002068EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002069
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002070void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002071{
2072}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002073EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002074
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002075static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002076{
2077 return 0;
2078}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002079NOKPROBE_SYMBOL(pre_handler_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002080
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002081#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002082
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002083/* Set the kprobe gone and remove its instruction buffer. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002084static void kill_kprobe(struct kprobe *p)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002085{
2086 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002087
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002088 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002089 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002090 /*
2091 * If this is an aggr_kprobe, we have to list all the
2092 * chained probes and mark them GONE.
2093 */
2094 list_for_each_entry_rcu(kp, &p->list, list)
2095 kp->flags |= KPROBE_FLAG_GONE;
2096 p->post_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002097 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002098 }
2099 /*
2100 * Here, we can remove insn_slot safely, because no thread calls
2101 * the original probed function (which will be freed soon) any more.
2102 */
2103 arch_remove_kprobe(p);
2104}
2105
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002106/* Disable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002107int disable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002108{
2109 int ret = 0;
Jessica Yu297f9232018-01-10 00:51:24 +01002110 struct kprobe *p;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002111
2112 mutex_lock(&kprobe_mutex);
2113
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09002114 /* Disable this kprobe */
Jessica Yu297f9232018-01-10 00:51:24 +01002115 p = __disable_kprobe(kp);
2116 if (IS_ERR(p))
2117 ret = PTR_ERR(p);
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002118
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002119 mutex_unlock(&kprobe_mutex);
2120 return ret;
2121}
2122EXPORT_SYMBOL_GPL(disable_kprobe);
2123
2124/* Enable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002125int enable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002126{
2127 int ret = 0;
2128 struct kprobe *p;
2129
2130 mutex_lock(&kprobe_mutex);
2131
2132 /* Check whether specified probe is valid. */
2133 p = __get_valid_kprobe(kp);
2134 if (unlikely(p == NULL)) {
2135 ret = -EINVAL;
2136 goto out;
2137 }
2138
2139 if (kprobe_gone(kp)) {
2140 /* This kprobe has gone, we couldn't enable it. */
2141 ret = -EINVAL;
2142 goto out;
2143 }
2144
2145 if (p != kp)
2146 kp->flags &= ~KPROBE_FLAG_DISABLED;
2147
2148 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2149 p->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01002150 ret = arm_kprobe(p);
2151 if (ret)
2152 p->flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002153 }
2154out:
2155 mutex_unlock(&kprobe_mutex);
2156 return ret;
2157}
2158EXPORT_SYMBOL_GPL(enable_kprobe);
2159
Masami Hiramatsu44585152018-04-28 21:36:33 +09002160/* Caller must NOT call this in usual path. This is only for critical case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002161void dump_kprobe(struct kprobe *kp)
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002162{
Masami Hiramatsu44585152018-04-28 21:36:33 +09002163 pr_err("Dumping kprobe:\n");
2164 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2165 kp->symbol_name, kp->offset, kp->addr);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002166}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002167NOKPROBE_SYMBOL(dump_kprobe);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002168
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002169int kprobe_add_ksym_blacklist(unsigned long entry)
2170{
2171 struct kprobe_blacklist_entry *ent;
2172 unsigned long offset = 0, size = 0;
2173
2174 if (!kernel_text_address(entry) ||
2175 !kallsyms_lookup_size_offset(entry, &size, &offset))
2176 return -EINVAL;
2177
2178 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2179 if (!ent)
2180 return -ENOMEM;
2181 ent->start_addr = entry;
2182 ent->end_addr = entry + size;
2183 INIT_LIST_HEAD(&ent->list);
2184 list_add_tail(&ent->list, &kprobe_blacklist);
2185
2186 return (int)size;
2187}
2188
2189/* Add all symbols in given area into kprobe blacklist */
2190int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2191{
2192 unsigned long entry;
2193 int ret = 0;
2194
2195 for (entry = start; entry < end; entry += ret) {
2196 ret = kprobe_add_ksym_blacklist(entry);
2197 if (ret < 0)
2198 return ret;
2199 if (ret == 0) /* In case of alias symbol */
2200 ret = 1;
2201 }
2202 return 0;
2203}
2204
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002205/* Remove all symbols in given area from kprobe blacklist */
2206static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2207{
2208 struct kprobe_blacklist_entry *ent, *n;
2209
2210 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2211 if (ent->start_addr < start || ent->start_addr >= end)
2212 continue;
2213 list_del(&ent->list);
2214 kfree(ent);
2215 }
2216}
2217
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002218static void kprobe_remove_ksym_blacklist(unsigned long entry)
2219{
2220 kprobe_remove_area_blacklist(entry, entry + 1);
2221}
2222
Adrian Hunterd002b8b2020-05-28 11:00:58 +03002223int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2224 char *type, char *sym)
2225{
2226 return -ERANGE;
2227}
2228
2229int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2230 char *sym)
2231{
2232#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2233 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2234 return 0;
2235#ifdef CONFIG_OPTPROBES
2236 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2237 return 0;
2238#endif
2239#endif
2240 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2241 return 0;
2242 return -ERANGE;
2243}
2244
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002245int __init __weak arch_populate_kprobe_blacklist(void)
2246{
2247 return 0;
2248}
2249
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002250/*
2251 * Lookup and populate the kprobe_blacklist.
2252 *
2253 * Unlike the kretprobe blacklist, we'll need to determine
2254 * the range of addresses that belong to the said functions,
2255 * since a kprobe need not necessarily be at the beginning
2256 * of a function.
2257 */
2258static int __init populate_kprobe_blacklist(unsigned long *start,
2259 unsigned long *end)
2260{
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002261 unsigned long entry;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002262 unsigned long *iter;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002263 int ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002264
2265 for (iter = start; iter < end; iter++) {
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002266 entry = arch_deref_entry_point((void *)*iter);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002267 ret = kprobe_add_ksym_blacklist(entry);
2268 if (ret == -EINVAL)
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002269 continue;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002270 if (ret < 0)
2271 return ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002272 }
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002273
2274 /* Symbols in __kprobes_text are blacklisted */
2275 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2276 (unsigned long)__kprobes_text_end);
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002277 if (ret)
2278 return ret;
2279
2280 /* Symbols in noinstr section are blacklisted */
2281 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2282 (unsigned long)__noinstr_text_end);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002283
2284 return ret ? : arch_populate_kprobe_blacklist();
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002285}
2286
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002287static void add_module_kprobe_blacklist(struct module *mod)
2288{
2289 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002290 int i;
2291
2292 if (mod->kprobe_blacklist) {
2293 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2294 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2295 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002296
2297 start = (unsigned long)mod->kprobes_text_start;
2298 if (start) {
2299 end = start + mod->kprobes_text_size;
2300 kprobe_add_area_blacklist(start, end);
2301 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002302
2303 start = (unsigned long)mod->noinstr_text_start;
2304 if (start) {
2305 end = start + mod->noinstr_text_size;
2306 kprobe_add_area_blacklist(start, end);
2307 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002308}
2309
2310static void remove_module_kprobe_blacklist(struct module *mod)
2311{
2312 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002313 int i;
2314
2315 if (mod->kprobe_blacklist) {
2316 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2317 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2318 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002319
2320 start = (unsigned long)mod->kprobes_text_start;
2321 if (start) {
2322 end = start + mod->kprobes_text_size;
2323 kprobe_remove_area_blacklist(start, end);
2324 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002325
2326 start = (unsigned long)mod->noinstr_text_start;
2327 if (start) {
2328 end = start + mod->noinstr_text_size;
2329 kprobe_remove_area_blacklist(start, end);
2330 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002331}
2332
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002333/* Module notifier call back, checking kprobes on the module */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002334static int kprobes_module_callback(struct notifier_block *nb,
2335 unsigned long val, void *data)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002336{
2337 struct module *mod = data;
2338 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002339 struct kprobe *p;
2340 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002341 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002342
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002343 if (val == MODULE_STATE_COMING) {
2344 mutex_lock(&kprobe_mutex);
2345 add_module_kprobe_blacklist(mod);
2346 mutex_unlock(&kprobe_mutex);
2347 }
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002348 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002349 return NOTIFY_DONE;
2350
2351 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002352 * When MODULE_STATE_GOING was notified, both of module .text and
2353 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2354 * notified, only .init.text section would be freed. We need to
2355 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002356 */
2357 mutex_lock(&kprobe_mutex);
2358 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2359 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002360 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002361 if (within_module_init((unsigned long)p->addr, mod) ||
2362 (checkcore &&
2363 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002364 /*
2365 * The vaddr this probe is installed will soon
2366 * be vfreed buy not synced to disk. Hence,
2367 * disarming the breakpoint isn't needed.
Steven Rostedt (VMware)545a0282017-05-16 14:58:35 -04002368 *
2369 * Note, this will also move any optimized probes
2370 * that are pending to be removed from their
2371 * corresponding lists to the freeing_list and
2372 * will not be touched by the delayed
2373 * kprobe_optimizer work handler.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002374 */
2375 kill_kprobe(p);
2376 }
2377 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002378 if (val == MODULE_STATE_GOING)
2379 remove_module_kprobe_blacklist(mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002380 mutex_unlock(&kprobe_mutex);
2381 return NOTIFY_DONE;
2382}
2383
2384static struct notifier_block kprobe_module_nb = {
2385 .notifier_call = kprobes_module_callback,
2386 .priority = 0
2387};
2388
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002389/* Markers of _kprobe_blacklist section */
2390extern unsigned long __start_kprobe_blacklist[];
2391extern unsigned long __stop_kprobe_blacklist[];
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393static int __init init_kprobes(void)
2394{
2395 int i, err = 0;
2396
2397 /* FIXME allocate the probe table, currently defined statically */
2398 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002399 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002401 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002402 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
Hien Nguyenb94cce92005-06-23 00:09:19 -07002403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002405 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2406 __stop_kprobe_blacklist);
2407 if (err) {
2408 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2409 pr_err("Please take care of using kprobes.\n");
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002410 }
2411
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002412 if (kretprobe_blacklist_size) {
2413 /* lookup the function address from its name */
2414 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05302415 kretprobe_blacklist[i].addr =
Naveen N. Rao290e3072017-04-19 18:21:01 +05302416 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002417 if (!kretprobe_blacklist[i].addr)
2418 printk("kretprobe: lookup failed: %s\n",
2419 kretprobe_blacklist[i].name);
2420 }
2421 }
2422
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002423#if defined(CONFIG_OPTPROBES)
2424#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002425 /* Init kprobe_optinsn_slots */
2426 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2427#endif
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002428 /* By default, kprobes can be optimized */
2429 kprobes_allow_optimization = true;
2430#endif
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002431
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002432 /* By default, kprobes are armed */
2433 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002434
Rusty Lynch67729262005-07-05 18:54:50 -07002435 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002436 if (!err)
2437 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002438 if (!err)
2439 err = register_module_notifier(&kprobe_module_nb);
2440
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002441 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07002442
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01002443 if (!err)
2444 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return err;
2446}
Masami Hiramatsu65fc9652019-06-03 22:04:42 +09002447subsys_initcall(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Srinivasa Ds346fd592007-02-20 13:57:54 -08002449#ifdef CONFIG_DEBUG_FS
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002450static void report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002451 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002452{
2453 char *kprobe_type;
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002454 void *addr = p->addr;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002455
2456 if (p->pre_handler == pre_handler_kretprobe)
2457 kprobe_type = "r";
Srinivasa Ds346fd592007-02-20 13:57:54 -08002458 else
2459 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002460
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002461 if (!kallsyms_show_value())
2462 addr = NULL;
2463
Srinivasa Ds346fd592007-02-20 13:57:54 -08002464 if (sym)
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002465 seq_printf(pi, "%px %s %s+0x%x %s ",
2466 addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002467 (modname ? modname : " "));
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002468 else /* try to use %pS */
2469 seq_printf(pi, "%px %s %pS ",
2470 addr, kprobe_type, p->addr);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002471
2472 if (!pp)
2473 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002474 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002475 (kprobe_gone(p) ? "[GONE]" : ""),
2476 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002477 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2478 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002479}
2480
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002481static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002482{
2483 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2484}
2485
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002486static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002487{
2488 (*pos)++;
2489 if (*pos >= KPROBE_TABLE_SIZE)
2490 return NULL;
2491 return pos;
2492}
2493
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002494static void kprobe_seq_stop(struct seq_file *f, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002495{
2496 /* Nothing to do */
2497}
2498
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002499static int show_kprobe_addr(struct seq_file *pi, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002500{
2501 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002502 struct kprobe *p, *kp;
2503 const char *sym = NULL;
2504 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002505 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002506 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002507
2508 head = &kprobe_table[i];
2509 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002510 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002511 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002512 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002513 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002514 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002515 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002516 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002517 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002518 }
2519 preempt_enable();
2520 return 0;
2521}
2522
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002523static const struct seq_operations kprobes_sops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002524 .start = kprobe_seq_start,
2525 .next = kprobe_seq_next,
2526 .stop = kprobe_seq_stop,
2527 .show = show_kprobe_addr
2528};
2529
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002530DEFINE_SEQ_ATTRIBUTE(kprobes);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002531
Masami Hiramatsu63724742014-04-17 17:18:49 +09002532/* kprobes/blacklist -- shows which functions can not be probed */
2533static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2534{
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002535 mutex_lock(&kprobe_mutex);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002536 return seq_list_start(&kprobe_blacklist, *pos);
2537}
2538
2539static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2540{
2541 return seq_list_next(v, &kprobe_blacklist, pos);
2542}
2543
2544static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2545{
2546 struct kprobe_blacklist_entry *ent =
2547 list_entry(v, struct kprobe_blacklist_entry, list);
2548
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002549 /*
2550 * If /proc/kallsyms is not showing kernel address, we won't
2551 * show them here either.
2552 */
2553 if (!kallsyms_show_value())
2554 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2555 (void *)ent->start_addr);
2556 else
2557 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2558 (void *)ent->end_addr, (void *)ent->start_addr);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002559 return 0;
2560}
2561
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002562static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2563{
2564 mutex_unlock(&kprobe_mutex);
2565}
2566
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002567static const struct seq_operations kprobe_blacklist_sops = {
Masami Hiramatsu63724742014-04-17 17:18:49 +09002568 .start = kprobe_blacklist_seq_start,
2569 .next = kprobe_blacklist_seq_next,
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002570 .stop = kprobe_blacklist_seq_stop,
Masami Hiramatsu63724742014-04-17 17:18:49 +09002571 .show = kprobe_blacklist_seq_show,
2572};
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002573DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002574
Jessica Yu12310e342018-01-10 00:51:23 +01002575static int arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002576{
2577 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002578 struct kprobe *p;
Jessica Yu12310e342018-01-10 00:51:23 +01002579 unsigned int i, total = 0, errors = 0;
2580 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002581
2582 mutex_lock(&kprobe_mutex);
2583
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002584 /* If kprobes are armed, just return */
2585 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002586 goto already_enabled;
2587
Wang Nan977ad482015-02-13 14:40:24 -08002588 /*
2589 * optimize_kprobe() called by arm_kprobe() checks
2590 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2591 * arm_kprobe.
2592 */
2593 kprobes_all_disarmed = false;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002594 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002595 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2596 head = &kprobe_table[i];
Jessica Yu12310e342018-01-10 00:51:23 +01002597 /* Arm all kprobes on a best-effort basis */
2598 hlist_for_each_entry_rcu(p, head, hlist) {
2599 if (!kprobe_disabled(p)) {
2600 err = arm_kprobe(p);
2601 if (err) {
2602 errors++;
2603 ret = err;
2604 }
2605 total++;
2606 }
2607 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002608 }
2609
Jessica Yu12310e342018-01-10 00:51:23 +01002610 if (errors)
2611 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2612 errors, total);
2613 else
2614 pr_info("Kprobes globally enabled\n");
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002615
2616already_enabled:
2617 mutex_unlock(&kprobe_mutex);
Jessica Yu12310e342018-01-10 00:51:23 +01002618 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002619}
2620
Jessica Yu297f9232018-01-10 00:51:24 +01002621static int disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002622{
2623 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002624 struct kprobe *p;
Jessica Yu297f9232018-01-10 00:51:24 +01002625 unsigned int i, total = 0, errors = 0;
2626 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002627
2628 mutex_lock(&kprobe_mutex);
2629
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002630 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002631 if (kprobes_all_disarmed) {
2632 mutex_unlock(&kprobe_mutex);
Jessica Yu297f9232018-01-10 00:51:24 +01002633 return 0;
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002634 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002635
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002636 kprobes_all_disarmed = true;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002637
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002638 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2639 head = &kprobe_table[i];
Jessica Yu297f9232018-01-10 00:51:24 +01002640 /* Disarm all kprobes on a best-effort basis */
Sasha Levinb67bfe02013-02-27 17:06:00 -08002641 hlist_for_each_entry_rcu(p, head, hlist) {
Jessica Yu297f9232018-01-10 00:51:24 +01002642 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2643 err = disarm_kprobe(p, false);
2644 if (err) {
2645 errors++;
2646 ret = err;
2647 }
2648 total++;
2649 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002650 }
2651 }
Jessica Yu297f9232018-01-10 00:51:24 +01002652
2653 if (errors)
2654 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2655 errors, total);
2656 else
2657 pr_info("Kprobes globally disabled\n");
2658
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002659 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002660
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002661 /* Wait for disarming all kprobes by optimizer */
2662 wait_for_kprobe_optimizer();
Jessica Yu297f9232018-01-10 00:51:24 +01002663
2664 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002665}
2666
2667/*
2668 * XXX: The debugfs bool file interface doesn't allow for callbacks
2669 * when the bool state is switched. We can reuse that facility when
2670 * available
2671 */
2672static ssize_t read_enabled_file_bool(struct file *file,
2673 char __user *user_buf, size_t count, loff_t *ppos)
2674{
2675 char buf[3];
2676
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002677 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002678 buf[0] = '1';
2679 else
2680 buf[0] = '0';
2681 buf[1] = '\n';
2682 buf[2] = 0x00;
2683 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2684}
2685
2686static ssize_t write_enabled_file_bool(struct file *file,
2687 const char __user *user_buf, size_t count, loff_t *ppos)
2688{
2689 char buf[32];
Stephen Boydefeb1562012-01-12 17:17:11 -08002690 size_t buf_size;
Jessica Yu12310e342018-01-10 00:51:23 +01002691 int ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002692
2693 buf_size = min(count, (sizeof(buf)-1));
2694 if (copy_from_user(buf, user_buf, buf_size))
2695 return -EFAULT;
2696
Mathias Krause10fb46d2013-07-03 15:05:39 -07002697 buf[buf_size] = '\0';
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002698 switch (buf[0]) {
2699 case 'y':
2700 case 'Y':
2701 case '1':
Jessica Yu12310e342018-01-10 00:51:23 +01002702 ret = arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002703 break;
2704 case 'n':
2705 case 'N':
2706 case '0':
Jessica Yu297f9232018-01-10 00:51:24 +01002707 ret = disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002708 break;
Mathias Krause10fb46d2013-07-03 15:05:39 -07002709 default:
2710 return -EINVAL;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002711 }
2712
Jessica Yu12310e342018-01-10 00:51:23 +01002713 if (ret)
2714 return ret;
2715
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002716 return count;
2717}
2718
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002719static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002720 .read = read_enabled_file_bool,
2721 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002722 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002723};
2724
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002725static int __init debugfs_kprobe_init(void)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002726{
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002727 struct dentry *dir;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002728 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002729
2730 dir = debugfs_create_dir("kprobes", NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002731
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002732 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002733
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002734 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002735
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002736 debugfs_create_file("blacklist", 0400, dir, NULL,
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002737 &kprobe_blacklist_fops);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002738
Srinivasa Ds346fd592007-02-20 13:57:54 -08002739 return 0;
2740}
2741
2742late_initcall(debugfs_kprobe_init);
2743#endif /* CONFIG_DEBUG_FS */