blob: 4a904cc56d68f922dbf807fb54f9593905a6f7f0 [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;
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +090049/* kprobe_table can be accessed by
50 * - Normal hlist traversal and RCU add/del under kprobe_mutex is held.
51 * Or
52 * - RCU hlist traversal under disabling preempt (breakpoint handlers)
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070055static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070057/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070058static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070059
Masami Hiramatsu43948f52010-10-25 22:18:01 +090060/* This protects kprobe_table and optimizing_list */
61static DEFINE_MUTEX(kprobe_mutex);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080062static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070063static struct {
Thomas Gleixnerec4846082009-07-25 16:09:17 +020064 raw_spinlock_t lock ____cacheline_aligned_in_smp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070065} kretprobe_table_locks[KPROBE_TABLE_SIZE];
66
Naveen N. Rao290e3072017-04-19 18:21:01 +053067kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
68 unsigned int __unused)
Naveen N. Rao49e0b462017-04-19 18:21:00 +053069{
70 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
71}
72
Thomas Gleixnerec4846082009-07-25 16:09:17 +020073static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070074{
75 return &(kretprobe_table_locks[hash].lock);
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Masami Hiramatsu376e2422014-04-17 17:17:05 +090078/* Blacklist -- list of struct kprobe_blacklist_entry */
79static LIST_HEAD(kprobe_blacklist);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070080
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080081#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070082/*
83 * kprobe->ainsn.insn points to the copy of the instruction to be
84 * single-stepped. x86_64, POWER4 and above have no-exec support and
85 * stepping on the instruction on a vmalloced/kmalloced/data page
86 * is a recipe for disaster
87 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070088struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -040089 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070090 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -070091 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070092 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080093 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050094 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070095};
96
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050097#define KPROBE_INSN_PAGE_SIZE(slots) \
98 (offsetof(struct kprobe_insn_page, slot_used) + \
99 (sizeof(char) * (slots)))
100
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500101static int slots_per_page(struct kprobe_insn_cache *c)
102{
103 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
104}
105
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800106enum kprobe_slot_state {
107 SLOT_CLEAN = 0,
108 SLOT_DIRTY = 1,
109 SLOT_USED = 2,
110};
111
Masami Hiramatsu63fef142017-08-18 17:24:00 +0900112void __weak *alloc_insn_page(void)
Heiko Carstensaf963972013-09-11 14:24:13 -0700113{
114 return module_alloc(PAGE_SIZE);
115}
116
Masami Hiramatsuc93f5cf2017-05-25 19:38:17 +0900117void __weak free_insn_page(void *page)
Heiko Carstensaf963972013-09-11 14:24:13 -0700118{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030119 module_memfree(page);
Heiko Carstensaf963972013-09-11 14:24:13 -0700120}
121
Heiko Carstensc802d642013-09-11 14:24:11 -0700122struct kprobe_insn_cache kprobe_insn_slots = {
123 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700124 .alloc = alloc_insn_page,
125 .free = free_insn_page,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500126 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
127 .insn_size = MAX_INSN_SIZE,
128 .nr_garbage = 0,
129};
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900130static int collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800131
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700132/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800133 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700134 * We allocate an executable page if there's no room on existing ones.
135 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900136kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700137{
138 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700139 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700140
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900141 /* Since the slot array is not protected by rcu, we need a mutex */
Heiko Carstensc802d642013-09-11 14:24:11 -0700142 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700143 retry:
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900144 rcu_read_lock();
145 list_for_each_entry_rcu(kip, &c->pages, list) {
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500146 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700147 int i;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500148 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800149 if (kip->slot_used[i] == SLOT_CLEAN) {
150 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700151 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700152 slot = kip->insns + (i * c->insn_size);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900153 rcu_read_unlock();
Heiko Carstensc802d642013-09-11 14:24:11 -0700154 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700155 }
156 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500157 /* kip->nused is broken. Fix it. */
158 kip->nused = slots_per_page(c);
159 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700160 }
161 }
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900162 rcu_read_unlock();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700163
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800164 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500165 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800166 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500167
168 /* All out of space. Need to allocate a new page. */
169 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700170 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700171 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700172
173 /*
174 * Use module_alloc so this page is within +/- 2GB of where the
175 * kernel image and loaded module images reside. This is required
176 * so x86_64 can correctly handle the %rip-relative fixups.
177 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700178 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700179 if (!kip->insns) {
180 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700181 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700182 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400183 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500184 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800185 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700186 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800187 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700188 kip->cache = c;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900189 list_add_rcu(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700190 slot = kip->insns;
191out:
192 mutex_unlock(&c->mutex);
193 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800194}
195
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800196/* Return 1 if all garbages are collected, otherwise 0. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900197static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800198{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800199 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800200 kip->nused--;
201 if (kip->nused == 0) {
202 /*
203 * Page is no longer in use. Free it unless
204 * it's the last one. We keep the last one
205 * so as not to have to set it up again the
206 * next time somebody inserts a probe.
207 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500208 if (!list_is_singular(&kip->list)) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900209 list_del_rcu(&kip->list);
210 synchronize_rcu();
Heiko Carstensaf963972013-09-11 14:24:13 -0700211 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800212 kfree(kip);
213 }
214 return 1;
215 }
216 return 0;
217}
218
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900219static int collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800220{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400221 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800222
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500223 /* Ensure no-one is interrupted on the garbages */
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -0800224 synchronize_rcu();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800225
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500226 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800227 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800228 if (kip->ngarbage == 0)
229 continue;
230 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500231 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900232 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800233 break;
234 }
235 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500236 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800237 return 0;
238}
239
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900240void __free_insn_slot(struct kprobe_insn_cache *c,
241 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500242{
243 struct kprobe_insn_page *kip;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900244 long idx;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500245
Heiko Carstensc802d642013-09-11 14:24:11 -0700246 mutex_lock(&c->mutex);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900247 rcu_read_lock();
248 list_for_each_entry_rcu(kip, &c->pages, list) {
249 idx = ((long)slot - (long)kip->insns) /
250 (c->insn_size * sizeof(kprobe_opcode_t));
251 if (idx >= 0 && idx < slots_per_page(c))
Heiko Carstensc802d642013-09-11 14:24:11 -0700252 goto out;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900253 }
254 /* Could not find this slot. */
255 WARN_ON(1);
256 kip = NULL;
257out:
258 rcu_read_unlock();
259 /* Mark and sweep: this may sleep */
260 if (kip) {
261 /* Check double free */
262 WARN_ON(kip->slot_used[idx] != SLOT_USED);
263 if (dirty) {
264 kip->slot_used[idx] = SLOT_DIRTY;
265 kip->ngarbage++;
266 if (++c->nr_garbage > slots_per_page(c))
267 collect_garbage_slots(c);
268 } else {
269 collect_one_slot(kip, idx);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500270 }
271 }
Heiko Carstensc802d642013-09-11 14:24:11 -0700272 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500273}
274
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900275/*
276 * Check given address is on the page of kprobe instruction slots.
277 * This will be used for checking whether the address on a stack
278 * is on a text area or not.
279 */
280bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
281{
282 struct kprobe_insn_page *kip;
283 bool ret = false;
284
285 rcu_read_lock();
286 list_for_each_entry_rcu(kip, &c->pages, list) {
287 if (addr >= (unsigned long)kip->insns &&
288 addr < (unsigned long)kip->insns + PAGE_SIZE) {
289 ret = true;
290 break;
291 }
292 }
293 rcu_read_unlock();
294
295 return ret;
296}
297
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500298#ifdef CONFIG_OPTPROBES
299/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700300struct kprobe_insn_cache kprobe_optinsn_slots = {
301 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700302 .alloc = alloc_insn_page,
303 .free = free_insn_page,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500304 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
305 /* .insn_size is initialized later */
306 .nr_garbage = 0,
307};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500308#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800309#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700310
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800311/* We have preemption disabled.. so it is safe to use __ versions */
312static inline void set_kprobe_instance(struct kprobe *kp)
313{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600314 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800315}
316
317static inline void reset_kprobe_instance(void)
318{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600319 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800320}
321
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800322/*
323 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800324 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800325 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800326 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800327 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900328struct kprobe *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800331 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Masami Hiramatsu6743ad42020-05-12 17:02:33 +0900334 hlist_for_each_entry_rcu(p, head, hlist,
335 lockdep_is_held(&kprobe_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (p->addr == addr)
337 return p;
338 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return NULL;
341}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900342NOKPROBE_SYMBOL(get_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900344static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500345
346/* Return true if the kprobe is an aggregator */
347static inline int kprobe_aggrprobe(struct kprobe *p)
348{
349 return p->pre_handler == aggr_pre_handler;
350}
351
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900352/* Return true(!0) if the kprobe is unused */
353static inline int kprobe_unused(struct kprobe *p)
354{
355 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
356 list_empty(&p->list);
357}
358
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500359/*
360 * Keep all fields in the kprobe consistent
361 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900362static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500363{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900364 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
365 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500366}
367
368#ifdef CONFIG_OPTPROBES
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500369/* NOTE: change this value only with kprobe_mutex held */
370static bool kprobes_allow_optimization;
371
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500372/*
373 * Call all pre_handler on the list, but ignores its return value.
374 * This must be called from arch-dep optimized caller.
375 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900376void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500377{
378 struct kprobe *kp;
379
380 list_for_each_entry_rcu(kp, &p->list, list) {
381 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
382 set_kprobe_instance(kp);
Naveen N. Rao4f3a8712017-10-17 13:48:34 +0530383 kp->pre_handler(kp, regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500384 }
385 reset_kprobe_instance();
386 }
387}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900388NOKPROBE_SYMBOL(opt_pre_handler);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500389
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900390/* Free optimized instructions and optimized_kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900391static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900392{
393 struct optimized_kprobe *op;
394
395 op = container_of(p, struct optimized_kprobe, kp);
396 arch_remove_optimized_kprobe(op);
397 arch_remove_kprobe(p);
398 kfree(op);
399}
400
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500401/* Return true(!0) if the kprobe is ready for optimization. */
402static inline int kprobe_optready(struct kprobe *p)
403{
404 struct optimized_kprobe *op;
405
406 if (kprobe_aggrprobe(p)) {
407 op = container_of(p, struct optimized_kprobe, kp);
408 return arch_prepared_optinsn(&op->optinsn);
409 }
410
411 return 0;
412}
413
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900414/* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
415static inline int kprobe_disarmed(struct kprobe *p)
416{
417 struct optimized_kprobe *op;
418
419 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
420 if (!kprobe_aggrprobe(p))
421 return kprobe_disabled(p);
422
423 op = container_of(p, struct optimized_kprobe, kp);
424
425 return kprobe_disabled(p) && list_empty(&op->list);
426}
427
428/* Return true(!0) if the probe is queued on (un)optimizing lists */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900429static int kprobe_queued(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900430{
431 struct optimized_kprobe *op;
432
433 if (kprobe_aggrprobe(p)) {
434 op = container_of(p, struct optimized_kprobe, kp);
435 if (!list_empty(&op->list))
436 return 1;
437 }
438 return 0;
439}
440
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500441/*
442 * Return an optimized kprobe whose optimizing code replaces
443 * instructions including addr (exclude breakpoint).
444 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900445static struct kprobe *get_optimized_kprobe(unsigned long addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500446{
447 int i;
448 struct kprobe *p = NULL;
449 struct optimized_kprobe *op;
450
451 /* Don't check i == 0, since that is a breakpoint case. */
452 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
453 p = get_kprobe((void *)(addr - i));
454
455 if (p && kprobe_optready(p)) {
456 op = container_of(p, struct optimized_kprobe, kp);
457 if (arch_within_optimized_kprobe(op, addr))
458 return p;
459 }
460
461 return NULL;
462}
463
464/* Optimization staging list, protected by kprobe_mutex */
465static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900466static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900467static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500468
469static void kprobe_optimizer(struct work_struct *work);
470static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
471#define OPTIMIZE_DELAY 5
472
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900473/*
474 * Optimize (replace a breakpoint with a jump) kprobes listed on
475 * optimizing_list.
476 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900477static void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500478{
Andrea Righif1c6ece2019-08-12 20:43:02 +0200479 lockdep_assert_held(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500480 /*
481 * The optimization/unoptimization refers online_cpus via
482 * stop_machine() and cpu-hotplug modifies online_cpus.
483 * And same time, text_mutex will be held in cpu-hotplug and here.
484 * This combination can cause a deadlock (cpu-hotplug try to lock
485 * text_mutex but stop_machine can not be done because online_cpus
486 * has been changed)
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200487 * To avoid this deadlock, caller must have locked cpu hotplug
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500488 * for preventing cpu-hotplug outside of text_mutex locking.
489 */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200490 lockdep_assert_cpus_held();
491
492 /* Optimization never be done when disarmed */
493 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
494 list_empty(&optimizing_list))
495 return;
496
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900497 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900498}
499
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900500/*
501 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
502 * if need) kprobes listed on unoptimizing_list.
503 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900504static void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900505{
506 struct optimized_kprobe *op, *tmp;
507
Andrea Righif1c6ece2019-08-12 20:43:02 +0200508 lockdep_assert_held(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200509 /* See comment in do_optimize_kprobes() */
510 lockdep_assert_cpus_held();
511
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900512 /* Unoptimization must be done anytime */
513 if (list_empty(&unoptimizing_list))
514 return;
515
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900516 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900517 /* Loop free_list for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900518 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900519 /* Switching from detour code to origin */
520 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900521 /* Disarm probes if marked disabled */
522 if (kprobe_disabled(&op->kp))
523 arch_disarm_kprobe(&op->kp);
524 if (kprobe_unused(&op->kp)) {
525 /*
526 * Remove unused probes from hash list. After waiting
527 * for synchronization, these probes are reclaimed.
528 * (reclaiming is done by do_free_cleaned_kprobes.)
529 */
530 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900531 } else
532 list_del_init(&op->list);
533 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900534}
535
536/* Reclaim all kprobes on the free_list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900537static void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900538{
539 struct optimized_kprobe *op, *tmp;
540
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900541 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900542 list_del_init(&op->list);
Masami Hiramatsucbdd96f2018-09-11 19:21:09 +0900543 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
544 /*
545 * This must not happen, but if there is a kprobe
546 * still in use, keep it on kprobes hash list.
547 */
548 continue;
549 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900550 free_aggr_kprobe(&op->kp);
551 }
552}
553
554/* Start optimizer after OPTIMIZE_DELAY passed */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900555static void kick_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900556{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800557 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900558}
559
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900560/* Kprobe jump optimizer */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900561static void kprobe_optimizer(struct work_struct *work)
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900562{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900563 mutex_lock(&kprobe_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200564 cpus_read_lock();
Andrea Righif1c6ece2019-08-12 20:43:02 +0200565 mutex_lock(&text_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900566 /* Lock modules while optimizing kprobes */
567 mutex_lock(&module_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900568
569 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900570 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
571 * kprobes before waiting for quiesence period.
572 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900573 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900574
575 /*
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900576 * Step 2: Wait for quiesence period to ensure all potentially
577 * preempted tasks to have normally scheduled. Because optprobe
578 * may modify multiple instructions, there is a chance that Nth
579 * instruction is preempted. In that case, such tasks can return
580 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
581 * Note that on non-preemptive kernel, this is transparently converted
582 * to synchronoze_sched() to wait for all interrupts to have completed.
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900583 */
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900584 synchronize_rcu_tasks();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900585
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900586 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900587 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900588
589 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900590 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900591
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500592 mutex_unlock(&module_mutex);
Andrea Righif1c6ece2019-08-12 20:43:02 +0200593 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200594 cpus_read_unlock();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900595
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900596 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900597 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900598 kick_kprobe_optimizer();
Masami Hiramatsu1a0aa992020-05-12 17:02:56 +0900599
600 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900601}
602
603/* Wait for completing optimization and unoptimization */
Thomas Gleixner30e7d8942017-05-17 10:19:49 +0200604void wait_for_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900605{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800606 mutex_lock(&kprobe_mutex);
607
608 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
609 mutex_unlock(&kprobe_mutex);
610
611 /* this will also make optimizing_work execute immmediately */
612 flush_delayed_work(&optimizing_work);
613 /* @optimizing_work might not have been queued yet, relax */
614 cpu_relax();
615
616 mutex_lock(&kprobe_mutex);
617 }
618
619 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500620}
621
Masami Hiramatsue4add242020-01-07 23:42:24 +0900622static bool optprobe_queued_unopt(struct optimized_kprobe *op)
623{
624 struct optimized_kprobe *_op;
625
626 list_for_each_entry(_op, &unoptimizing_list, list) {
627 if (op == _op)
628 return true;
629 }
630
631 return false;
632}
633
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500634/* Optimize kprobe if p is ready to be optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900635static void optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500636{
637 struct optimized_kprobe *op;
638
639 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500640 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500641 (kprobe_disabled(p) || kprobes_all_disarmed))
642 return;
643
Masami Hiramatsu059053a2018-06-20 01:10:27 +0900644 /* kprobes with post_handler can not be optimized */
645 if (p->post_handler)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500646 return;
647
648 op = container_of(p, struct optimized_kprobe, kp);
649
650 /* Check there is no other kprobes at the optimized instructions */
651 if (arch_check_optimized_kprobe(op) < 0)
652 return;
653
654 /* Check if it is already optimized. */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900655 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
656 if (optprobe_queued_unopt(op)) {
657 /* This is under unoptimizing. Just dequeue the probe */
658 list_del_init(&op->list);
659 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500660 return;
Masami Hiramatsue4add242020-01-07 23:42:24 +0900661 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500662 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900663
Masami Hiramatsue4add242020-01-07 23:42:24 +0900664 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
665 if (WARN_ON_ONCE(!list_empty(&op->list)))
666 return;
667
668 list_add(&op->list, &optimizing_list);
669 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900670}
671
672/* Short cut to direct unoptimizing */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900673static void force_unoptimize_kprobe(struct optimized_kprobe *op)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900674{
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200675 lockdep_assert_cpus_held();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900676 arch_unoptimize_kprobe(op);
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900677 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500678}
679
680/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900681static void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500682{
683 struct optimized_kprobe *op;
684
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900685 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
686 return; /* This is not an optprobe nor optimized */
687
688 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900689 if (!kprobe_optimized(p))
690 return;
691
692 if (!list_empty(&op->list)) {
693 if (optprobe_queued_unopt(op)) {
694 /* Queued in unoptimizing queue */
695 if (force) {
696 /*
697 * Forcibly unoptimize the kprobe here, and queue it
698 * in the freeing list for release afterwards.
699 */
700 force_unoptimize_kprobe(op);
701 list_move(&op->list, &freeing_list);
702 }
703 } else {
704 /* Dequeue from the optimizing queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500705 list_del_init(&op->list);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900706 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900707 }
708 return;
709 }
710
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900711 /* Optimized kprobe case */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900712 if (force) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900713 /* Forcibly update the code: this is a special case */
714 force_unoptimize_kprobe(op);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900715 } else {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900716 list_add(&op->list, &unoptimizing_list);
717 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500718 }
719}
720
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900721/* Cancel unoptimizing for reusing */
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900722static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900723{
724 struct optimized_kprobe *op;
725
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900726 /*
727 * Unused kprobe MUST be on the way of delayed unoptimizing (means
728 * there is still a relative jump) and disabled.
729 */
730 op = container_of(ap, struct optimized_kprobe, kp);
Masami Hiramatsu44585152018-04-28 21:36:33 +0900731 WARN_ON_ONCE(list_empty(&op->list));
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900732 /* Enable the probe again */
733 ap->flags &= ~KPROBE_FLAG_DISABLED;
734 /* Optimize it again (remove from op->list) */
Masami Hiramatsu5f843ed2019-04-15 15:01:25 +0900735 if (!kprobe_optready(ap))
736 return -EINVAL;
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900737
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900738 optimize_kprobe(ap);
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900739 return 0;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900740}
741
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500742/* Remove optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900743static void kill_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500744{
745 struct optimized_kprobe *op;
746
747 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900748 if (!list_empty(&op->list))
749 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500750 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900751 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900752
753 if (kprobe_unused(p)) {
754 /* Enqueue if it is unused */
755 list_add(&op->list, &freeing_list);
756 /*
757 * Remove unused probes from the hash list. After waiting
758 * for synchronization, this probe is reclaimed.
759 * (reclaiming is done by do_free_cleaned_kprobes().)
760 */
761 hlist_del_rcu(&op->kp.hlist);
762 }
763
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900764 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500765 arch_remove_optimized_kprobe(op);
766}
767
Masami Hiramatsua4602462017-04-19 18:22:25 +0530768static inline
769void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
770{
771 if (!kprobe_ftrace(p))
772 arch_prepare_optimized_kprobe(op, p);
773}
774
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500775/* Try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900776static void prepare_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500777{
778 struct optimized_kprobe *op;
779
780 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsua4602462017-04-19 18:22:25 +0530781 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500782}
783
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500784/* Allocate new optimized_kprobe and try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900785static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500786{
787 struct optimized_kprobe *op;
788
789 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
790 if (!op)
791 return NULL;
792
793 INIT_LIST_HEAD(&op->list);
794 op->kp.addr = p->addr;
Masami Hiramatsua4602462017-04-19 18:22:25 +0530795 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500796
797 return &op->kp;
798}
799
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900800static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500801
802/*
803 * Prepare an optimized_kprobe and optimize it
804 * NOTE: p must be a normal registered kprobe
805 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900806static void try_to_optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500807{
808 struct kprobe *ap;
809 struct optimized_kprobe *op;
810
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900811 /* Impossible to optimize ftrace-based kprobe */
812 if (kprobe_ftrace(p))
813 return;
814
Masami Hiramatsu25764282012-06-05 19:28:26 +0900815 /* For preparing optimization, jump_label_text_reserved() is called */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200816 cpus_read_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +0900817 jump_label_lock();
818 mutex_lock(&text_mutex);
819
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500820 ap = alloc_aggr_kprobe(p);
821 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900822 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500823
824 op = container_of(ap, struct optimized_kprobe, kp);
825 if (!arch_prepared_optinsn(&op->optinsn)) {
826 /* If failed to setup optimizing, fallback to kprobe */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900827 arch_remove_optimized_kprobe(op);
828 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900829 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500830 }
831
832 init_aggr_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900833 optimize_kprobe(ap); /* This just kicks optimizer thread */
834
835out:
836 mutex_unlock(&text_mutex);
837 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200838 cpus_read_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500839}
840
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500841#ifdef CONFIG_SYSCTL
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900842static void optimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500843{
844 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500845 struct kprobe *p;
846 unsigned int i;
847
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900848 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500849 /* If optimization is already allowed, just return */
850 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900851 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500852
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200853 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500854 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500855 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
856 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900857 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500858 if (!kprobe_disabled(p))
859 optimize_kprobe(p);
860 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200861 cpus_read_unlock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500862 printk(KERN_INFO "Kprobes globally optimized\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900863out:
864 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500865}
866
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900867static void unoptimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500868{
869 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500870 struct kprobe *p;
871 unsigned int i;
872
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900873 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500874 /* If optimization is already prohibited, just return */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900875 if (!kprobes_allow_optimization) {
876 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500877 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900878 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500879
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200880 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500881 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500882 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
883 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900884 hlist_for_each_entry(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500885 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900886 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500887 }
888 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200889 cpus_read_unlock();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900890 mutex_unlock(&kprobe_mutex);
891
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900892 /* Wait for unoptimizing completion */
893 wait_for_kprobe_optimizer();
894 printk(KERN_INFO "Kprobes globally unoptimized\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500895}
896
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900897static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500898int sysctl_kprobes_optimization;
899int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200900 void *buffer, size_t *length,
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500901 loff_t *ppos)
902{
903 int ret;
904
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900905 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500906 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
907 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
908
909 if (sysctl_kprobes_optimization)
910 optimize_all_kprobes();
911 else
912 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900913 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500914
915 return ret;
916}
917#endif /* CONFIG_SYSCTL */
918
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900919/* Put a breakpoint for a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900920static void __arm_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500921{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900922 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500923
924 /* Check collision with other optimized kprobes */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900925 _p = get_optimized_kprobe((unsigned long)p->addr);
926 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900927 /* Fallback to unoptimized kprobe */
928 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500929
930 arch_arm_kprobe(p);
931 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
932}
933
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900934/* Remove the breakpoint of a probe. Must be called with text_mutex locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900935static void __disarm_kprobe(struct kprobe *p, bool reopt)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500936{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900937 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500938
Wang Nan69d54b92015-02-13 14:40:26 -0800939 /* Try to unoptimize */
940 unoptimize_kprobe(p, kprobes_all_disarmed);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500941
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900942 if (!kprobe_queued(p)) {
943 arch_disarm_kprobe(p);
944 /* If another kprobe was blocked, optimize it. */
945 _p = get_optimized_kprobe((unsigned long)p->addr);
946 if (unlikely(_p) && reopt)
947 optimize_kprobe(_p);
948 }
949 /* TODO: reoptimize others after unoptimized this probe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500950}
951
952#else /* !CONFIG_OPTPROBES */
953
954#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900955#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500956#define kill_optimized_kprobe(p) do {} while (0)
957#define prepare_optimized_kprobe(p) do {} while (0)
958#define try_to_optimize_kprobe(p) do {} while (0)
959#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900960#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
961#define kprobe_disarmed(p) kprobe_disabled(p)
962#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500963
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900964static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900965{
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900966 /*
967 * If the optimized kprobe is NOT supported, the aggr kprobe is
968 * released at the same time that the last aggregated kprobe is
969 * unregistered.
970 * Thus there should be no chance to reuse unused kprobe.
971 */
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900972 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900973 return -EINVAL;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900974}
975
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900976static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500977{
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900978 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500979 kfree(p);
980}
981
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900982static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500983{
984 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
985}
986#endif /* CONFIG_OPTPROBES */
987
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900988#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900989static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +0900990 .func = kprobe_ftrace_handler,
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +0900991 .flags = FTRACE_OPS_FL_SAVE_REGS,
992};
993
994static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
995 .func = kprobe_ftrace_handler,
Masami Hiramatsu1d70be32014-11-21 05:25:23 -0500996 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900997};
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +0900998
999static int kprobe_ipmodify_enabled;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001000static int kprobe_ftrace_enabled;
1001
1002/* Must ensure p->addr is really on ftrace */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001003static int prepare_kprobe(struct kprobe *p)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001004{
1005 if (!kprobe_ftrace(p))
1006 return arch_prepare_kprobe(p);
1007
1008 return arch_prepare_kprobe_ftrace(p);
1009}
1010
1011/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001012static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1013 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001014{
Jessica Yu12310e342018-01-10 00:51:23 +01001015 int ret = 0;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001016
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001017 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001018 if (ret) {
Masami Hiramatsu44585152018-04-28 21:36:33 +09001019 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1020 p->addr, ret);
Jessica Yu12310e342018-01-10 00:51:23 +01001021 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001022 }
Jessica Yu12310e342018-01-10 00:51:23 +01001023
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001024 if (*cnt == 0) {
1025 ret = register_ftrace_function(ops);
Jessica Yu12310e342018-01-10 00:51:23 +01001026 if (ret) {
1027 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1028 goto err_ftrace;
1029 }
1030 }
1031
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001032 (*cnt)++;
Jessica Yu12310e342018-01-10 00:51:23 +01001033 return ret;
1034
1035err_ftrace:
1036 /*
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001037 * At this point, sinec ops is not registered, we should be sefe from
1038 * registering empty filter.
Jessica Yu12310e342018-01-10 00:51:23 +01001039 */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001040 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001041 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001042}
1043
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001044static int arm_kprobe_ftrace(struct kprobe *p)
1045{
1046 bool ipmodify = (p->post_handler != NULL);
1047
1048 return __arm_kprobe_ftrace(p,
1049 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1050 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1051}
1052
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001053/* Caller must lock kprobe_mutex */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001054static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1055 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001056{
Jessica Yu297f9232018-01-10 00:51:24 +01001057 int ret = 0;
1058
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001059 if (*cnt == 1) {
1060 ret = unregister_ftrace_function(ops);
Jessica Yu297f9232018-01-10 00:51:24 +01001061 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1062 return ret;
1063 }
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001064
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001065 (*cnt)--;
Jessica Yu297f9232018-01-10 00:51:24 +01001066
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001067 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Masami Hiramatsu44585152018-04-28 21:36:33 +09001068 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1069 p->addr, ret);
Jessica Yu297f9232018-01-10 00:51:24 +01001070 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001071}
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001072
1073static int disarm_kprobe_ftrace(struct kprobe *p)
1074{
1075 bool ipmodify = (p->post_handler != NULL);
1076
1077 return __disarm_kprobe_ftrace(p,
1078 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1079 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1080}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001081#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001082#define prepare_kprobe(p) arch_prepare_kprobe(p)
Jessica Yu12310e342018-01-10 00:51:23 +01001083#define arm_kprobe_ftrace(p) (-ENODEV)
Jessica Yu297f9232018-01-10 00:51:24 +01001084#define disarm_kprobe_ftrace(p) (-ENODEV)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001085#endif
1086
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001087/* Arm a kprobe with text_mutex */
Jessica Yu12310e342018-01-10 00:51:23 +01001088static int arm_kprobe(struct kprobe *kp)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001089{
Jessica Yu12310e342018-01-10 00:51:23 +01001090 if (unlikely(kprobe_ftrace(kp)))
1091 return arm_kprobe_ftrace(kp);
1092
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001093 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001094 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001095 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001096 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001097 cpus_read_unlock();
Jessica Yu12310e342018-01-10 00:51:23 +01001098
1099 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001100}
1101
1102/* Disarm a kprobe with text_mutex */
Jessica Yu297f9232018-01-10 00:51:24 +01001103static int disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001104{
Jessica Yu297f9232018-01-10 00:51:24 +01001105 if (unlikely(kprobe_ftrace(kp)))
1106 return disarm_kprobe_ftrace(kp);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001107
1108 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001109 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001110 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001111 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001112 cpus_read_unlock();
Jessica Yu297f9232018-01-10 00:51:24 +01001113
1114 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001115}
1116
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001117/*
1118 * Aggregate handlers for multiple kprobes support - these handlers
1119 * take care of invoking the individual kprobe handlers on p->list
1120 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001121static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001122{
1123 struct kprobe *kp;
1124
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001125 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001126 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001127 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001128 if (kp->pre_handler(kp, regs))
1129 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001130 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001131 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001132 }
1133 return 0;
1134}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001135NOKPROBE_SYMBOL(aggr_pre_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001136
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001137static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1138 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001139{
1140 struct kprobe *kp;
1141
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001142 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001143 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001144 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001145 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001146 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001147 }
1148 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001149}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001150NOKPROBE_SYMBOL(aggr_post_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001151
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001152static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1153 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001154{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001155 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001156
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001157 /*
1158 * if we faulted "during" the execution of a user specified
1159 * probe handler, invoke just that probe's fault handler
1160 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001161 if (cur && cur->fault_handler) {
1162 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001163 return 1;
1164 }
1165 return 0;
1166}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001167NOKPROBE_SYMBOL(aggr_fault_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001168
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001169/* Walks the list and increments nmissed count for multiprobe case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001170void kprobes_inc_nmissed_count(struct kprobe *p)
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001171{
1172 struct kprobe *kp;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001173 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001174 p->nmissed++;
1175 } else {
1176 list_for_each_entry_rcu(kp, &p->list, list)
1177 kp->nmissed++;
1178 }
1179 return;
1180}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001181NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001182
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001183void recycle_rp_inst(struct kretprobe_instance *ri,
1184 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001185{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001186 struct kretprobe *rp = ri->rp;
1187
Hien Nguyenb94cce92005-06-23 00:09:19 -07001188 /* remove rp inst off the rprobe_inst_table */
1189 hlist_del(&ri->hlist);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001190 INIT_HLIST_NODE(&ri->hlist);
1191 if (likely(rp)) {
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001192 raw_spin_lock(&rp->lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001193 hlist_add_head(&ri->hlist, &rp->free_instances);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001194 raw_spin_unlock(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001195 } else
1196 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -07001197 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001198}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001199NOKPROBE_SYMBOL(recycle_rp_inst);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001200
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001201void kretprobe_hash_lock(struct task_struct *tsk,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001202 struct hlist_head **head, unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001203__acquires(hlist_lock)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001204{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001205 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001206 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001207
1208 *head = &kretprobe_inst_table[hash];
1209 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001210 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001211}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001212NOKPROBE_SYMBOL(kretprobe_hash_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001213
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001214static void kretprobe_table_lock(unsigned long hash,
1215 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001216__acquires(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001217{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001218 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1219 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001220}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001221NOKPROBE_SYMBOL(kretprobe_table_lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001222
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001223void kretprobe_hash_unlock(struct task_struct *tsk,
1224 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001225__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001226{
1227 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001228 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001229
1230 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001231 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001232}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001233NOKPROBE_SYMBOL(kretprobe_hash_unlock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001234
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001235static void kretprobe_table_unlock(unsigned long hash,
1236 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001237__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001238{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001239 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1240 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001241}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001242NOKPROBE_SYMBOL(kretprobe_table_unlock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001243
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001244struct kprobe kprobe_busy = {
1245 .addr = (void *) get_kprobe,
1246};
1247
1248void kprobe_busy_begin(void)
1249{
1250 struct kprobe_ctlblk *kcb;
1251
1252 preempt_disable();
1253 __this_cpu_write(current_kprobe, &kprobe_busy);
1254 kcb = get_kprobe_ctlblk();
1255 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1256}
1257
1258void kprobe_busy_end(void)
1259{
1260 __this_cpu_write(current_kprobe, NULL);
1261 preempt_enable();
1262}
1263
Hien Nguyenb94cce92005-06-23 00:09:19 -07001264/*
bibo maoc6fd91f2006-03-26 01:38:20 -08001265 * This function is called from finish_task_switch when task tk becomes dead,
1266 * so that we can recycle any function-return probe instances associated
1267 * with this task. These left over instances represent probed functions
1268 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001269 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001270void kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001271{
bibo,mao62c27be2006-10-02 02:17:33 -07001272 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -07001273 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001274 struct hlist_node *tmp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001275 unsigned long hash, flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -07001276
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001277 if (unlikely(!kprobes_initialized))
1278 /* Early boot. kretprobe_table_locks not yet initialized. */
1279 return;
1280
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001281 kprobe_busy_begin();
1282
Ananth N Mavinakayanahallid496aab2012-01-20 14:34:04 -08001283 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001284 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1285 head = &kretprobe_inst_table[hash];
1286 kretprobe_table_lock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001287 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -07001288 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -07001289 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -07001290 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001291 kretprobe_table_unlock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001292 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -07001293 hlist_del(&ri->hlist);
1294 kfree(ri);
1295 }
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001296
1297 kprobe_busy_end();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001298}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001299NOKPROBE_SYMBOL(kprobe_flush_task);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001300
Hien Nguyenb94cce92005-06-23 00:09:19 -07001301static inline void free_rp_inst(struct kretprobe *rp)
1302{
1303 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001304 struct hlist_node *next;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001305
Sasha Levinb67bfe02013-02-27 17:06:00 -08001306 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001307 hlist_del(&ri->hlist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001308 kfree(ri);
1309 }
1310}
1311
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001312static void cleanup_rp_inst(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001313{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001314 unsigned long flags, hash;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001315 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001316 struct hlist_node *next;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001317 struct hlist_head *head;
1318
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001319 /* No race here */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001320 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1321 kretprobe_table_lock(hash, &flags);
1322 head = &kretprobe_inst_table[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001323 hlist_for_each_entry_safe(ri, next, head, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001324 if (ri->rp == rp)
1325 ri->rp = NULL;
1326 }
1327 kretprobe_table_unlock(hash, &flags);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001328 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001329 free_rp_inst(rp);
1330}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001331NOKPROBE_SYMBOL(cleanup_rp_inst);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001332
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001333/* Add the new probe to ap->list */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001334static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001335{
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001336 if (p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001337 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001338
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001339 list_add_rcu(&p->list, &ap->list);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001340 if (p->post_handler && !ap->post_handler)
1341 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001342
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001343 return 0;
1344}
1345
1346/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001347 * Fill in the required fields of the "manager kprobe". Replace the
1348 * earlier kprobe in the hlist with the manager kprobe
1349 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001350static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001351{
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001352 /* Copy p's insn slot to ap */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001353 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001354 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001355 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001356 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001357 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001358 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001359 /* We don't care the kprobe which has gone. */
1360 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001361 ap->post_handler = aggr_post_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001362
1363 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001364 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001365
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001366 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001367 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001368}
1369
1370/*
1371 * This is the second or subsequent kprobe at the address - handle
1372 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001373 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001374static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001375{
1376 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001377 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001378
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001379 cpus_read_lock();
1380
Masami Hiramatsu25764282012-06-05 19:28:26 +09001381 /* For preparing optimization, jump_label_text_reserved() is called */
1382 jump_label_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001383 mutex_lock(&text_mutex);
1384
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001385 if (!kprobe_aggrprobe(orig_p)) {
1386 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1387 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001388 if (!ap) {
1389 ret = -ENOMEM;
1390 goto out;
1391 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001392 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001393 } else if (kprobe_unused(ap)) {
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001394 /* This probe is going to die. Rescue it */
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001395 ret = reuse_unused_kprobe(ap);
1396 if (ret)
1397 goto out;
1398 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001399
1400 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001401 /*
1402 * Attempting to insert new probe at the same location that
1403 * had a probe in the module vaddr area which already
1404 * freed. So, the instruction slot has already been
1405 * released. We need a new slot for the new probe.
1406 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001407 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001408 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001409 /*
1410 * Even if fail to allocate new slot, don't need to
1411 * free aggr_probe. It will be used next time, or
1412 * freed by unregister_kprobe.
1413 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001414 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001415
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001416 /* Prepare optimized instructions if possible. */
1417 prepare_optimized_kprobe(ap);
1418
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001419 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001420 * Clear gone flag to prevent allocating new slot again, and
1421 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001422 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001423 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1424 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001425 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001426
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001427 /* Copy ap's insn slot to p */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001428 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001429 ret = add_new_kprobe(ap, p);
1430
1431out:
1432 mutex_unlock(&text_mutex);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001433 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001434 cpus_read_unlock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001435
1436 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1437 ap->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01001438 if (!kprobes_all_disarmed) {
Masami Hiramatsu25764282012-06-05 19:28:26 +09001439 /* Arm the breakpoint again. */
Jessica Yu12310e342018-01-10 00:51:23 +01001440 ret = arm_kprobe(ap);
1441 if (ret) {
1442 ap->flags |= KPROBE_FLAG_DISABLED;
1443 list_del_rcu(&p->list);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001444 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001445 }
1446 }
Masami Hiramatsu25764282012-06-05 19:28:26 +09001447 }
1448 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001449}
1450
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001451bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1452{
1453 /* The __kprobes marked functions and entry code must not be probed */
1454 return addr >= (unsigned long)__kprobes_text_start &&
1455 addr < (unsigned long)__kprobes_text_end;
1456}
1457
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001458static bool __within_kprobe_blacklist(unsigned long addr)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001459{
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001460 struct kprobe_blacklist_entry *ent;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001461
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001462 if (arch_within_kprobe_blacklist(addr))
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001463 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001464 /*
1465 * If there exists a kprobe_blacklist, verify and
1466 * fail any probe registration in the prohibited area
1467 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001468 list_for_each_entry(ent, &kprobe_blacklist, list) {
1469 if (addr >= ent->start_addr && addr < ent->end_addr)
1470 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001471 }
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001472 return false;
1473}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001474
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001475bool within_kprobe_blacklist(unsigned long addr)
1476{
1477 char symname[KSYM_NAME_LEN], *p;
1478
1479 if (__within_kprobe_blacklist(addr))
1480 return true;
1481
1482 /* Check if the address is on a suffixed-symbol */
1483 if (!lookup_symbol_name(addr, symname)) {
1484 p = strchr(symname, '.');
1485 if (!p)
1486 return false;
1487 *p = '\0';
1488 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1489 if (addr)
1490 return __within_kprobe_blacklist(addr);
1491 }
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001492 return false;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001493}
1494
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001495/*
1496 * If we have a symbol_name argument, look it up and add the offset field
1497 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001498 * This returns encoded errors if it fails to look up symbol or invalid
1499 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001500 */
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301501static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1502 const char *symbol_name, unsigned int offset)
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001503{
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301504 if ((symbol_name && addr) || (!symbol_name && !addr))
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001505 goto invalid;
1506
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301507 if (symbol_name) {
Linus Torvalds7246f602017-05-05 11:36:44 -07001508 addr = kprobe_lookup_name(symbol_name, offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001509 if (!addr)
1510 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001511 }
1512
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301513 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001514 if (addr)
1515 return addr;
1516
1517invalid:
1518 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001519}
1520
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301521static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1522{
1523 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1524}
1525
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301526/* Check passed kprobe is valid and return kprobe in kprobe_table. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001527static struct kprobe *__get_valid_kprobe(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301528{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001529 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301530
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001531 lockdep_assert_held(&kprobe_mutex);
1532
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001533 ap = get_kprobe(p->addr);
1534 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301535 return NULL;
1536
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001537 if (p != ap) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001538 list_for_each_entry(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301539 if (list_p == p)
1540 /* kprobe p is a valid probe */
1541 goto valid;
1542 return NULL;
1543 }
1544valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001545 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301546}
1547
1548/* Return error if the kprobe is being re-registered */
1549static inline int check_kprobe_rereg(struct kprobe *p)
1550{
1551 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301552
1553 mutex_lock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001554 if (__get_valid_kprobe(p))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301555 ret = -EINVAL;
1556 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001557
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301558 return ret;
1559}
1560
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001561int __weak arch_check_ftrace_location(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001563 unsigned long ftrace_addr;
1564
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001565 ftrace_addr = ftrace_location((unsigned long)p->addr);
1566 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001567#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001568 /* Given address is not on the instruction boundary */
1569 if ((unsigned long)p->addr != ftrace_addr)
1570 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001571 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001572#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001573 return -EINVAL;
1574#endif
1575 }
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001576 return 0;
1577}
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001578
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001579static int check_kprobe_address_safe(struct kprobe *p,
1580 struct module **probed_mod)
1581{
1582 int ret;
1583
1584 ret = arch_check_ftrace_location(p);
1585 if (ret)
1586 return ret;
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001587 jump_label_lock();
1588 preempt_disable();
1589
1590 /* Ensure it is not in reserved area nor out of text */
1591 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001592 within_kprobe_blacklist((unsigned long) p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001593 jump_label_text_reserved(p->addr, p->addr) ||
1594 find_bug((unsigned long)p->addr)) {
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001595 ret = -EINVAL;
1596 goto out;
1597 }
1598
1599 /* Check if are we probing a module */
1600 *probed_mod = __module_text_address((unsigned long) p->addr);
1601 if (*probed_mod) {
1602 /*
1603 * We must hold a refcount of the probed module while updating
1604 * its code to prohibit unexpected unloading.
1605 */
1606 if (unlikely(!try_module_get(*probed_mod))) {
1607 ret = -ENOENT;
1608 goto out;
1609 }
1610
1611 /*
1612 * If the module freed .init.text, we couldn't insert
1613 * kprobes in there.
1614 */
1615 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1616 (*probed_mod)->state != MODULE_STATE_COMING) {
1617 module_put(*probed_mod);
1618 *probed_mod = NULL;
1619 ret = -ENOENT;
1620 }
1621 }
1622out:
1623 preempt_enable();
1624 jump_label_unlock();
1625
1626 return ret;
1627}
1628
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001629int register_kprobe(struct kprobe *p)
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001630{
1631 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001632 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001633 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001634 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001636 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001637 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001638 if (IS_ERR(addr))
1639 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001640 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001641
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301642 ret = check_kprobe_rereg(p);
1643 if (ret)
1644 return ret;
1645
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001646 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1647 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001648 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001649 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001650
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001651 ret = check_kprobe_address_safe(p, &probed_mod);
1652 if (ret)
1653 return ret;
1654
1655 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001656
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001657 old_p = get_kprobe(p->addr);
1658 if (old_p) {
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001659 /* Since this may unoptimize old_p, locking text_mutex. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001660 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto out;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001664 cpus_read_lock();
1665 /* Prevent text modification */
1666 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001667 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001668 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001669 cpus_read_unlock();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001670 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001671 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001672
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001673 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001674 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1676
Jessica Yu12310e342018-01-10 00:51:23 +01001677 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1678 ret = arm_kprobe(p);
1679 if (ret) {
1680 hlist_del_rcu(&p->hlist);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001681 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001682 goto out;
1683 }
1684 }
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001685
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001686 /* Try to optimize kprobe */
1687 try_to_optimize_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001689 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001690
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001691 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001692 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return ret;
1695}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001696EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001698/* Check if all probes on the aggrprobe are disabled */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001699static int aggr_kprobe_disabled(struct kprobe *ap)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001700{
1701 struct kprobe *kp;
1702
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001703 lockdep_assert_held(&kprobe_mutex);
1704
1705 list_for_each_entry(kp, &ap->list, list)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001706 if (!kprobe_disabled(kp))
1707 /*
1708 * There is an active probe on the list.
1709 * We can't disable this ap.
1710 */
1711 return 0;
1712
1713 return 1;
1714}
1715
1716/* Disable one kprobe: Make sure called under kprobe_mutex is locked */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001717static struct kprobe *__disable_kprobe(struct kprobe *p)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001718{
1719 struct kprobe *orig_p;
Jessica Yu297f9232018-01-10 00:51:24 +01001720 int ret;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001721
1722 /* Get an original kprobe for return */
1723 orig_p = __get_valid_kprobe(p);
1724 if (unlikely(orig_p == NULL))
Jessica Yu297f9232018-01-10 00:51:24 +01001725 return ERR_PTR(-EINVAL);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001726
1727 if (!kprobe_disabled(p)) {
1728 /* Disable probe if it is a child probe */
1729 if (p != orig_p)
1730 p->flags |= KPROBE_FLAG_DISABLED;
1731
1732 /* Try to disarm and disable this/parent probe */
1733 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Wang Nan69d54b92015-02-13 14:40:26 -08001734 /*
1735 * If kprobes_all_disarmed is set, orig_p
1736 * should have already been disarmed, so
1737 * skip unneed disarming process.
1738 */
Jessica Yu297f9232018-01-10 00:51:24 +01001739 if (!kprobes_all_disarmed) {
1740 ret = disarm_kprobe(orig_p, true);
1741 if (ret) {
1742 p->flags &= ~KPROBE_FLAG_DISABLED;
1743 return ERR_PTR(ret);
1744 }
1745 }
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001746 orig_p->flags |= KPROBE_FLAG_DISABLED;
1747 }
1748 }
1749
1750 return orig_p;
1751}
1752
Masami Hiramatsu98616682008-04-28 02:14:28 -07001753/*
1754 * Unregister a kprobe without a scheduler synchronization.
1755 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001756static int __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001757{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001758 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001759
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001760 /* Disable kprobe. This will disarm it if needed. */
1761 ap = __disable_kprobe(p);
Jessica Yu297f9232018-01-10 00:51:24 +01001762 if (IS_ERR(ap))
1763 return PTR_ERR(ap);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001764
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001765 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001766 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001767 * This probe is an independent(and non-optimized) kprobe
1768 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001769 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001770 goto disarmed;
1771
1772 /* Following process expects this probe is an aggrprobe */
1773 WARN_ON(!kprobe_aggrprobe(ap));
1774
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001775 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1776 /*
1777 * !disarmed could be happen if the probe is under delayed
1778 * unoptimizing.
1779 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001780 goto disarmed;
1781 else {
1782 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001783 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001784 list_for_each_entry(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001785 if ((list_p != p) && (list_p->post_handler))
1786 goto noclean;
1787 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001788 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001789 }
1790noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001791 /*
1792 * Remove from the aggrprobe: this path will do nothing in
1793 * __unregister_kprobe_bottom().
1794 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001795 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001796 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1797 /*
1798 * Try to optimize this probe again, because post
1799 * handler may have been changed.
1800 */
1801 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001802 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001803 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001804
1805disarmed:
1806 hlist_del_rcu(&ap->hlist);
1807 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001808}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001809
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001810static void __unregister_kprobe_bottom(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001811{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001812 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001813
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001814 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001815 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001816 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001817 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001818 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001819 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001820 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001821 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001822 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001823 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001826int register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001827{
1828 int i, ret = 0;
1829
1830 if (num <= 0)
1831 return -EINVAL;
1832 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001833 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001834 if (ret < 0) {
1835 if (i > 0)
1836 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001837 break;
1838 }
1839 }
1840 return ret;
1841}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001842EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001843
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001844void unregister_kprobe(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001845{
1846 unregister_kprobes(&p, 1);
1847}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001848EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001849
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001850void unregister_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001851{
1852 int i;
1853
1854 if (num <= 0)
1855 return;
1856 mutex_lock(&kprobe_mutex);
1857 for (i = 0; i < num; i++)
1858 if (__unregister_kprobe_top(kps[i]) < 0)
1859 kps[i]->addr = NULL;
1860 mutex_unlock(&kprobe_mutex);
1861
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001862 synchronize_rcu();
Masami Hiramatsu98616682008-04-28 02:14:28 -07001863 for (i = 0; i < num; i++)
1864 if (kps[i]->addr)
1865 __unregister_kprobe_bottom(kps[i]);
1866}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001867EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001868
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301869int __weak kprobe_exceptions_notify(struct notifier_block *self,
1870 unsigned long val, void *data)
Naveen N. Raofc62d022017-02-08 01:24:14 +05301871{
1872 return NOTIFY_DONE;
1873}
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301874NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Naveen N. Raofc62d022017-02-08 01:24:14 +05301875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876static struct notifier_block kprobe_exceptions_nb = {
1877 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001878 .priority = 0x7fffffff /* we need to be notified first */
1879};
1880
Michael Ellerman3d7e3382007-07-19 01:48:11 -07001881unsigned long __weak arch_deref_entry_point(void *entry)
1882{
1883 return (unsigned long)entry;
1884}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001886#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -08001887/*
1888 * This kprobe pre_handler is registered with every kretprobe. When probe
1889 * hits it will set up the return probe.
1890 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001891static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Adrian Bunke65cefe2006-02-03 03:03:42 -08001892{
1893 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001894 unsigned long hash, flags = 0;
1895 struct kretprobe_instance *ri;
Adrian Bunke65cefe2006-02-03 03:03:42 -08001896
Masami Hiramatsuf96f5672014-08-04 03:10:16 +00001897 /*
1898 * To avoid deadlocks, prohibit return probing in NMI contexts,
1899 * just skip the probe and increase the (inexact) 'nmissed'
1900 * statistical counter, so that the user is informed that
1901 * something happened:
1902 */
1903 if (unlikely(in_nmi())) {
1904 rp->nmissed++;
1905 return 0;
1906 }
1907
1908 /* TODO: consider to only swap the RA after the last pre_handler fired */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001909 hash = hash_ptr(current, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001910 raw_spin_lock_irqsave(&rp->lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001911 if (!hlist_empty(&rp->free_instances)) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001912 ri = hlist_entry(rp->free_instances.first,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001913 struct kretprobe_instance, hlist);
1914 hlist_del(&ri->hlist);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001915 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001916
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001917 ri->rp = rp;
1918 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001919
Jiang Liu55ca6142012-02-03 15:37:16 -08001920 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
1921 raw_spin_lock_irqsave(&rp->lock, flags);
1922 hlist_add_head(&ri->hlist, &rp->free_instances);
1923 raw_spin_unlock_irqrestore(&rp->lock, flags);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001924 return 0;
Jiang Liu55ca6142012-02-03 15:37:16 -08001925 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001926
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001927 arch_prepare_kretprobe(ri, regs);
1928
1929 /* XXX(hch): why is there no hlist_move_head? */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001930 INIT_HLIST_NODE(&ri->hlist);
1931 kretprobe_table_lock(hash, &flags);
1932 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
1933 kretprobe_table_unlock(hash, &flags);
1934 } else {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001935 rp->nmissed++;
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001936 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001937 }
Adrian Bunke65cefe2006-02-03 03:03:42 -08001938 return 0;
1939}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001940NOKPROBE_SYMBOL(pre_handler_kretprobe);
Adrian Bunke65cefe2006-02-03 03:03:42 -08001941
Naveen N. Rao659b9572017-07-07 22:37:24 +05301942bool __weak arch_kprobe_on_func_entry(unsigned long offset)
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301943{
1944 return !offset;
1945}
1946
Naveen N. Rao659b9572017-07-07 22:37:24 +05301947bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301948{
1949 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
1950
1951 if (IS_ERR(kp_addr))
1952 return false;
1953
1954 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) ||
Naveen N. Rao659b9572017-07-07 22:37:24 +05301955 !arch_kprobe_on_func_entry(offset))
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301956 return false;
1957
1958 return true;
1959}
1960
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001961int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001962{
1963 int ret = 0;
1964 struct kretprobe_instance *inst;
1965 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001966 void *addr;
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301967
Naveen N. Rao659b9572017-07-07 22:37:24 +05301968 if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset))
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05301969 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001970
1971 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001972 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001973 if (IS_ERR(addr))
1974 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001975
1976 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1977 if (kretprobe_blacklist[i].addr == addr)
1978 return -EINVAL;
1979 }
1980 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001981
1982 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07001983 rp->kp.post_handler = NULL;
1984 rp->kp.fault_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001985
1986 /* Pre-allocate memory for max kretprobe instances */
1987 if (rp->maxactive <= 0) {
Thomas Gleixner92616602019-07-26 23:19:41 +02001988#ifdef CONFIG_PREEMPTION
Heiko Carstensc2ef6662009-12-21 13:02:24 +01001989 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07001990#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05301991 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001992#endif
1993 }
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001994 raw_spin_lock_init(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001995 INIT_HLIST_HEAD(&rp->free_instances);
1996 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001997 inst = kmalloc(sizeof(struct kretprobe_instance) +
1998 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001999 if (inst == NULL) {
2000 free_rp_inst(rp);
2001 return -ENOMEM;
2002 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002003 INIT_HLIST_NODE(&inst->hlist);
2004 hlist_add_head(&inst->hlist, &rp->free_instances);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002005 }
2006
2007 rp->nmissed = 0;
2008 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002009 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002010 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002011 free_rp_inst(rp);
2012 return ret;
2013}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002014EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002015
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002016int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002017{
2018 int ret = 0, i;
2019
2020 if (num <= 0)
2021 return -EINVAL;
2022 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002023 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07002024 if (ret < 0) {
2025 if (i > 0)
2026 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002027 break;
2028 }
2029 }
2030 return ret;
2031}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002032EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002033
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002034void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002035{
2036 unregister_kretprobes(&rp, 1);
2037}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002038EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002039
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002040void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002041{
2042 int i;
2043
2044 if (num <= 0)
2045 return;
2046 mutex_lock(&kprobe_mutex);
2047 for (i = 0; i < num; i++)
2048 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2049 rps[i]->kp.addr = NULL;
2050 mutex_unlock(&kprobe_mutex);
2051
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08002052 synchronize_rcu();
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002053 for (i = 0; i < num; i++) {
2054 if (rps[i]->kp.addr) {
2055 __unregister_kprobe_bottom(&rps[i]->kp);
2056 cleanup_rp_inst(rps[i]);
2057 }
2058 }
2059}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002060EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002061
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002062#else /* CONFIG_KRETPROBES */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002063int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002064{
2065 return -ENOSYS;
2066}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002067EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002068
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002069int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002070{
2071 return -ENOSYS;
2072}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002073EXPORT_SYMBOL_GPL(register_kretprobes);
2074
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002075void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002076{
2077}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002078EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002079
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002080void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002081{
2082}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002083EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002084
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002085static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002086{
2087 return 0;
2088}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002089NOKPROBE_SYMBOL(pre_handler_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002090
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002091#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002092
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002093/* Set the kprobe gone and remove its instruction buffer. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002094static void kill_kprobe(struct kprobe *p)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002095{
2096 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002097
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002098 lockdep_assert_held(&kprobe_mutex);
2099
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002100 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002101 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002102 /*
2103 * If this is an aggr_kprobe, we have to list all the
2104 * chained probes and mark them GONE.
2105 */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002106 list_for_each_entry(kp, &p->list, list)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002107 kp->flags |= KPROBE_FLAG_GONE;
2108 p->post_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002109 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002110 }
2111 /*
2112 * Here, we can remove insn_slot safely, because no thread calls
2113 * the original probed function (which will be freed soon) any more.
2114 */
2115 arch_remove_kprobe(p);
2116}
2117
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002118/* Disable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002119int disable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002120{
2121 int ret = 0;
Jessica Yu297f9232018-01-10 00:51:24 +01002122 struct kprobe *p;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002123
2124 mutex_lock(&kprobe_mutex);
2125
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09002126 /* Disable this kprobe */
Jessica Yu297f9232018-01-10 00:51:24 +01002127 p = __disable_kprobe(kp);
2128 if (IS_ERR(p))
2129 ret = PTR_ERR(p);
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002130
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002131 mutex_unlock(&kprobe_mutex);
2132 return ret;
2133}
2134EXPORT_SYMBOL_GPL(disable_kprobe);
2135
2136/* Enable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002137int enable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002138{
2139 int ret = 0;
2140 struct kprobe *p;
2141
2142 mutex_lock(&kprobe_mutex);
2143
2144 /* Check whether specified probe is valid. */
2145 p = __get_valid_kprobe(kp);
2146 if (unlikely(p == NULL)) {
2147 ret = -EINVAL;
2148 goto out;
2149 }
2150
2151 if (kprobe_gone(kp)) {
2152 /* This kprobe has gone, we couldn't enable it. */
2153 ret = -EINVAL;
2154 goto out;
2155 }
2156
2157 if (p != kp)
2158 kp->flags &= ~KPROBE_FLAG_DISABLED;
2159
2160 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2161 p->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01002162 ret = arm_kprobe(p);
2163 if (ret)
2164 p->flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002165 }
2166out:
2167 mutex_unlock(&kprobe_mutex);
2168 return ret;
2169}
2170EXPORT_SYMBOL_GPL(enable_kprobe);
2171
Masami Hiramatsu44585152018-04-28 21:36:33 +09002172/* Caller must NOT call this in usual path. This is only for critical case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002173void dump_kprobe(struct kprobe *kp)
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002174{
Masami Hiramatsu44585152018-04-28 21:36:33 +09002175 pr_err("Dumping kprobe:\n");
2176 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2177 kp->symbol_name, kp->offset, kp->addr);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002178}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002179NOKPROBE_SYMBOL(dump_kprobe);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002180
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002181int kprobe_add_ksym_blacklist(unsigned long entry)
2182{
2183 struct kprobe_blacklist_entry *ent;
2184 unsigned long offset = 0, size = 0;
2185
2186 if (!kernel_text_address(entry) ||
2187 !kallsyms_lookup_size_offset(entry, &size, &offset))
2188 return -EINVAL;
2189
2190 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2191 if (!ent)
2192 return -ENOMEM;
2193 ent->start_addr = entry;
2194 ent->end_addr = entry + size;
2195 INIT_LIST_HEAD(&ent->list);
2196 list_add_tail(&ent->list, &kprobe_blacklist);
2197
2198 return (int)size;
2199}
2200
2201/* Add all symbols in given area into kprobe blacklist */
2202int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2203{
2204 unsigned long entry;
2205 int ret = 0;
2206
2207 for (entry = start; entry < end; entry += ret) {
2208 ret = kprobe_add_ksym_blacklist(entry);
2209 if (ret < 0)
2210 return ret;
2211 if (ret == 0) /* In case of alias symbol */
2212 ret = 1;
2213 }
2214 return 0;
2215}
2216
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002217/* Remove all symbols in given area from kprobe blacklist */
2218static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2219{
2220 struct kprobe_blacklist_entry *ent, *n;
2221
2222 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2223 if (ent->start_addr < start || ent->start_addr >= end)
2224 continue;
2225 list_del(&ent->list);
2226 kfree(ent);
2227 }
2228}
2229
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002230static void kprobe_remove_ksym_blacklist(unsigned long entry)
2231{
2232 kprobe_remove_area_blacklist(entry, entry + 1);
2233}
2234
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002235int __init __weak arch_populate_kprobe_blacklist(void)
2236{
2237 return 0;
2238}
2239
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002240/*
2241 * Lookup and populate the kprobe_blacklist.
2242 *
2243 * Unlike the kretprobe blacklist, we'll need to determine
2244 * the range of addresses that belong to the said functions,
2245 * since a kprobe need not necessarily be at the beginning
2246 * of a function.
2247 */
2248static int __init populate_kprobe_blacklist(unsigned long *start,
2249 unsigned long *end)
2250{
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002251 unsigned long entry;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002252 unsigned long *iter;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002253 int ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002254
2255 for (iter = start; iter < end; iter++) {
Masami Hiramatsud81b4252014-07-17 11:44:11 +00002256 entry = arch_deref_entry_point((void *)*iter);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002257 ret = kprobe_add_ksym_blacklist(entry);
2258 if (ret == -EINVAL)
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002259 continue;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002260 if (ret < 0)
2261 return ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002262 }
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002263
2264 /* Symbols in __kprobes_text are blacklisted */
2265 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2266 (unsigned long)__kprobes_text_end);
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002267 if (ret)
2268 return ret;
2269
2270 /* Symbols in noinstr section are blacklisted */
2271 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2272 (unsigned long)__noinstr_text_end);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002273
2274 return ret ? : arch_populate_kprobe_blacklist();
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002275}
2276
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002277static void add_module_kprobe_blacklist(struct module *mod)
2278{
2279 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002280 int i;
2281
2282 if (mod->kprobe_blacklist) {
2283 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2284 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2285 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002286
2287 start = (unsigned long)mod->kprobes_text_start;
2288 if (start) {
2289 end = start + mod->kprobes_text_size;
2290 kprobe_add_area_blacklist(start, end);
2291 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002292
2293 start = (unsigned long)mod->noinstr_text_start;
2294 if (start) {
2295 end = start + mod->noinstr_text_size;
2296 kprobe_add_area_blacklist(start, end);
2297 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002298}
2299
2300static void remove_module_kprobe_blacklist(struct module *mod)
2301{
2302 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002303 int i;
2304
2305 if (mod->kprobe_blacklist) {
2306 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2307 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2308 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002309
2310 start = (unsigned long)mod->kprobes_text_start;
2311 if (start) {
2312 end = start + mod->kprobes_text_size;
2313 kprobe_remove_area_blacklist(start, end);
2314 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002315
2316 start = (unsigned long)mod->noinstr_text_start;
2317 if (start) {
2318 end = start + mod->noinstr_text_size;
2319 kprobe_remove_area_blacklist(start, end);
2320 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002321}
2322
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002323/* Module notifier call back, checking kprobes on the module */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002324static int kprobes_module_callback(struct notifier_block *nb,
2325 unsigned long val, void *data)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002326{
2327 struct module *mod = data;
2328 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002329 struct kprobe *p;
2330 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002331 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002332
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002333 if (val == MODULE_STATE_COMING) {
2334 mutex_lock(&kprobe_mutex);
2335 add_module_kprobe_blacklist(mod);
2336 mutex_unlock(&kprobe_mutex);
2337 }
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002338 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002339 return NOTIFY_DONE;
2340
2341 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002342 * When MODULE_STATE_GOING was notified, both of module .text and
2343 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2344 * notified, only .init.text section would be freed. We need to
2345 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002346 */
2347 mutex_lock(&kprobe_mutex);
2348 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2349 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002350 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002351 if (within_module_init((unsigned long)p->addr, mod) ||
2352 (checkcore &&
2353 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002354 /*
2355 * The vaddr this probe is installed will soon
2356 * be vfreed buy not synced to disk. Hence,
2357 * disarming the breakpoint isn't needed.
Steven Rostedt (VMware)545a0282017-05-16 14:58:35 -04002358 *
2359 * Note, this will also move any optimized probes
2360 * that are pending to be removed from their
2361 * corresponding lists to the freeing_list and
2362 * will not be touched by the delayed
2363 * kprobe_optimizer work handler.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002364 */
2365 kill_kprobe(p);
2366 }
2367 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002368 if (val == MODULE_STATE_GOING)
2369 remove_module_kprobe_blacklist(mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002370 mutex_unlock(&kprobe_mutex);
2371 return NOTIFY_DONE;
2372}
2373
2374static struct notifier_block kprobe_module_nb = {
2375 .notifier_call = kprobes_module_callback,
2376 .priority = 0
2377};
2378
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002379/* Markers of _kprobe_blacklist section */
2380extern unsigned long __start_kprobe_blacklist[];
2381extern unsigned long __stop_kprobe_blacklist[];
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383static int __init init_kprobes(void)
2384{
2385 int i, err = 0;
2386
2387 /* FIXME allocate the probe table, currently defined statically */
2388 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002389 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002391 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002392 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
Hien Nguyenb94cce92005-06-23 00:09:19 -07002393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002395 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2396 __stop_kprobe_blacklist);
2397 if (err) {
2398 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2399 pr_err("Please take care of using kprobes.\n");
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002400 }
2401
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002402 if (kretprobe_blacklist_size) {
2403 /* lookup the function address from its name */
2404 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05302405 kretprobe_blacklist[i].addr =
Naveen N. Rao290e3072017-04-19 18:21:01 +05302406 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002407 if (!kretprobe_blacklist[i].addr)
2408 printk("kretprobe: lookup failed: %s\n",
2409 kretprobe_blacklist[i].name);
2410 }
2411 }
2412
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002413#if defined(CONFIG_OPTPROBES)
2414#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002415 /* Init kprobe_optinsn_slots */
2416 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2417#endif
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002418 /* By default, kprobes can be optimized */
2419 kprobes_allow_optimization = true;
2420#endif
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002421
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002422 /* By default, kprobes are armed */
2423 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002424
Rusty Lynch67729262005-07-05 18:54:50 -07002425 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002426 if (!err)
2427 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002428 if (!err)
2429 err = register_module_notifier(&kprobe_module_nb);
2430
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002431 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07002432
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01002433 if (!err)
2434 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return err;
2436}
Masami Hiramatsu65fc9652019-06-03 22:04:42 +09002437subsys_initcall(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Srinivasa Ds346fd592007-02-20 13:57:54 -08002439#ifdef CONFIG_DEBUG_FS
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002440static void report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002441 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002442{
2443 char *kprobe_type;
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002444 void *addr = p->addr;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002445
2446 if (p->pre_handler == pre_handler_kretprobe)
2447 kprobe_type = "r";
Srinivasa Ds346fd592007-02-20 13:57:54 -08002448 else
2449 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002450
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002451 if (!kallsyms_show_value())
2452 addr = NULL;
2453
Srinivasa Ds346fd592007-02-20 13:57:54 -08002454 if (sym)
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002455 seq_printf(pi, "%px %s %s+0x%x %s ",
2456 addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002457 (modname ? modname : " "));
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002458 else /* try to use %pS */
2459 seq_printf(pi, "%px %s %pS ",
2460 addr, kprobe_type, p->addr);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002461
2462 if (!pp)
2463 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002464 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002465 (kprobe_gone(p) ? "[GONE]" : ""),
2466 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002467 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2468 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002469}
2470
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002471static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002472{
2473 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2474}
2475
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002476static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002477{
2478 (*pos)++;
2479 if (*pos >= KPROBE_TABLE_SIZE)
2480 return NULL;
2481 return pos;
2482}
2483
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002484static void kprobe_seq_stop(struct seq_file *f, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002485{
2486 /* Nothing to do */
2487}
2488
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002489static int show_kprobe_addr(struct seq_file *pi, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002490{
2491 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002492 struct kprobe *p, *kp;
2493 const char *sym = NULL;
2494 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002495 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002496 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002497
2498 head = &kprobe_table[i];
2499 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002500 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002501 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002502 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002503 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002504 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002505 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002506 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002507 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002508 }
2509 preempt_enable();
2510 return 0;
2511}
2512
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002513static const struct seq_operations kprobes_sops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002514 .start = kprobe_seq_start,
2515 .next = kprobe_seq_next,
2516 .stop = kprobe_seq_stop,
2517 .show = show_kprobe_addr
2518};
2519
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002520DEFINE_SEQ_ATTRIBUTE(kprobes);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002521
Masami Hiramatsu63724742014-04-17 17:18:49 +09002522/* kprobes/blacklist -- shows which functions can not be probed */
2523static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2524{
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002525 mutex_lock(&kprobe_mutex);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002526 return seq_list_start(&kprobe_blacklist, *pos);
2527}
2528
2529static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2530{
2531 return seq_list_next(v, &kprobe_blacklist, pos);
2532}
2533
2534static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2535{
2536 struct kprobe_blacklist_entry *ent =
2537 list_entry(v, struct kprobe_blacklist_entry, list);
2538
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002539 /*
2540 * If /proc/kallsyms is not showing kernel address, we won't
2541 * show them here either.
2542 */
2543 if (!kallsyms_show_value())
2544 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2545 (void *)ent->start_addr);
2546 else
2547 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2548 (void *)ent->end_addr, (void *)ent->start_addr);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002549 return 0;
2550}
2551
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002552static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2553{
2554 mutex_unlock(&kprobe_mutex);
2555}
2556
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002557static const struct seq_operations kprobe_blacklist_sops = {
Masami Hiramatsu63724742014-04-17 17:18:49 +09002558 .start = kprobe_blacklist_seq_start,
2559 .next = kprobe_blacklist_seq_next,
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002560 .stop = kprobe_blacklist_seq_stop,
Masami Hiramatsu63724742014-04-17 17:18:49 +09002561 .show = kprobe_blacklist_seq_show,
2562};
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002563DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002564
Jessica Yu12310e342018-01-10 00:51:23 +01002565static int arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002566{
2567 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002568 struct kprobe *p;
Jessica Yu12310e342018-01-10 00:51:23 +01002569 unsigned int i, total = 0, errors = 0;
2570 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002571
2572 mutex_lock(&kprobe_mutex);
2573
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002574 /* If kprobes are armed, just return */
2575 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002576 goto already_enabled;
2577
Wang Nan977ad482015-02-13 14:40:24 -08002578 /*
2579 * optimize_kprobe() called by arm_kprobe() checks
2580 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2581 * arm_kprobe.
2582 */
2583 kprobes_all_disarmed = false;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002584 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002585 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2586 head = &kprobe_table[i];
Jessica Yu12310e342018-01-10 00:51:23 +01002587 /* Arm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002588 hlist_for_each_entry(p, head, hlist) {
Jessica Yu12310e342018-01-10 00:51:23 +01002589 if (!kprobe_disabled(p)) {
2590 err = arm_kprobe(p);
2591 if (err) {
2592 errors++;
2593 ret = err;
2594 }
2595 total++;
2596 }
2597 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002598 }
2599
Jessica Yu12310e342018-01-10 00:51:23 +01002600 if (errors)
2601 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2602 errors, total);
2603 else
2604 pr_info("Kprobes globally enabled\n");
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002605
2606already_enabled:
2607 mutex_unlock(&kprobe_mutex);
Jessica Yu12310e342018-01-10 00:51:23 +01002608 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002609}
2610
Jessica Yu297f9232018-01-10 00:51:24 +01002611static int disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002612{
2613 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002614 struct kprobe *p;
Jessica Yu297f9232018-01-10 00:51:24 +01002615 unsigned int i, total = 0, errors = 0;
2616 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002617
2618 mutex_lock(&kprobe_mutex);
2619
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002620 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002621 if (kprobes_all_disarmed) {
2622 mutex_unlock(&kprobe_mutex);
Jessica Yu297f9232018-01-10 00:51:24 +01002623 return 0;
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002624 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002625
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002626 kprobes_all_disarmed = true;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002627
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002628 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2629 head = &kprobe_table[i];
Jessica Yu297f9232018-01-10 00:51:24 +01002630 /* Disarm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002631 hlist_for_each_entry(p, head, hlist) {
Jessica Yu297f9232018-01-10 00:51:24 +01002632 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2633 err = disarm_kprobe(p, false);
2634 if (err) {
2635 errors++;
2636 ret = err;
2637 }
2638 total++;
2639 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002640 }
2641 }
Jessica Yu297f9232018-01-10 00:51:24 +01002642
2643 if (errors)
2644 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2645 errors, total);
2646 else
2647 pr_info("Kprobes globally disabled\n");
2648
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002649 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002650
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002651 /* Wait for disarming all kprobes by optimizer */
2652 wait_for_kprobe_optimizer();
Jessica Yu297f9232018-01-10 00:51:24 +01002653
2654 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002655}
2656
2657/*
2658 * XXX: The debugfs bool file interface doesn't allow for callbacks
2659 * when the bool state is switched. We can reuse that facility when
2660 * available
2661 */
2662static ssize_t read_enabled_file_bool(struct file *file,
2663 char __user *user_buf, size_t count, loff_t *ppos)
2664{
2665 char buf[3];
2666
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002667 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002668 buf[0] = '1';
2669 else
2670 buf[0] = '0';
2671 buf[1] = '\n';
2672 buf[2] = 0x00;
2673 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2674}
2675
2676static ssize_t write_enabled_file_bool(struct file *file,
2677 const char __user *user_buf, size_t count, loff_t *ppos)
2678{
2679 char buf[32];
Stephen Boydefeb1562012-01-12 17:17:11 -08002680 size_t buf_size;
Jessica Yu12310e342018-01-10 00:51:23 +01002681 int ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002682
2683 buf_size = min(count, (sizeof(buf)-1));
2684 if (copy_from_user(buf, user_buf, buf_size))
2685 return -EFAULT;
2686
Mathias Krause10fb46d2013-07-03 15:05:39 -07002687 buf[buf_size] = '\0';
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002688 switch (buf[0]) {
2689 case 'y':
2690 case 'Y':
2691 case '1':
Jessica Yu12310e342018-01-10 00:51:23 +01002692 ret = arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002693 break;
2694 case 'n':
2695 case 'N':
2696 case '0':
Jessica Yu297f9232018-01-10 00:51:24 +01002697 ret = disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002698 break;
Mathias Krause10fb46d2013-07-03 15:05:39 -07002699 default:
2700 return -EINVAL;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002701 }
2702
Jessica Yu12310e342018-01-10 00:51:23 +01002703 if (ret)
2704 return ret;
2705
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002706 return count;
2707}
2708
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002709static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002710 .read = read_enabled_file_bool,
2711 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002712 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002713};
2714
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002715static int __init debugfs_kprobe_init(void)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002716{
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002717 struct dentry *dir;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002718 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002719
2720 dir = debugfs_create_dir("kprobes", NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002721
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002722 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002723
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002724 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002725
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002726 debugfs_create_file("blacklist", 0400, dir, NULL,
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002727 &kprobe_blacklist_fops);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002728
Srinivasa Ds346fd592007-02-20 13:57:54 -08002729 return 0;
2730}
2731
2732late_initcall(debugfs_kprobe_init);
2733#endif /* CONFIG_DEBUG_FS */