blob: 21eccc961bba31cad928b2bf6e9bfc325191e64e [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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) IBM Corporation, 2002, 2004
6 *
7 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
8 * Probes initial implementation (includes suggestions from
9 * Rusty Russell).
10 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
11 * hlists and exceptions notifier as suggested by Andi Kleen.
12 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
13 * interface to access function arguments.
14 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
15 * exceptions notifier to be first on the priority list.
Hien Nguyenb94cce92005-06-23 00:09:19 -070016 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
17 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
18 * <prasanna@in.ibm.com> added function-return probes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +090020
21#define pr_fmt(fmt) "kprobes: " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080026#include <linux/slab.h>
Randy Dunlape3869792007-05-08 00:27:01 -070027#include <linux/stddef.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040028#include <linux/export.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070029#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070030#include <linux/kallsyms.h>
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080031#include <linux/freezer.h>
Srinivasa Ds346fd592007-02-20 13:57:54 -080032#include <linux/seq_file.h>
33#include <linux/debugfs.h>
Masami Hiramatsub2be84d2010-02-25 08:34:15 -050034#include <linux/sysctl.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070035#include <linux/kdebug.h>
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -050036#include <linux/memory.h>
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -050037#include <linux/ftrace.h>
Masami Hiramatsuafd66252010-02-25 08:34:07 -050038#include <linux/cpu.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040039#include <linux/jump_label.h>
Peter Zijlstrafa68bd02021-06-28 13:24:12 +020040#include <linux/static_call.h>
Adrian Hunter69e49082020-05-12 15:19:11 +030041#include <linux/perf_event.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070042
Christoph Hellwigbfd45be2016-10-11 13:52:22 -070043#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/cacheflush.h>
45#include <asm/errno.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define KPROBE_HASH_BITS 6
49#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
50
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070051
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070052static int kprobes_initialized;
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +090053/* kprobe_table can be accessed by
Masami Hiramatsu223a76b2021-09-14 23:39:34 +090054 * - Normal hlist traversal and RCU add/del under 'kprobe_mutex' is held.
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +090055 * Or
56 * - RCU hlist traversal under disabling preempt (breakpoint handlers)
57 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
59
Masami Hiramatsu223a76b2021-09-14 23:39:34 +090060/* NOTE: change this value only with 'kprobe_mutex' held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070061static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070062
Masami Hiramatsu223a76b2021-09-14 23:39:34 +090063/* This protects 'kprobe_table' and 'optimizing_list' */
Masami Hiramatsu43948f52010-10-25 22:18:01 +090064static DEFINE_MUTEX(kprobe_mutex);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +090065static DEFINE_PER_CPU(struct kprobe *, kprobe_instance);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070066
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
Masami Hiramatsu223a76b2021-09-14 23:39:34 +090073/*
74 * Blacklist -- list of 'struct kprobe_blacklist_entry' to store info where
75 * kprobes can not probe.
76 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +090077static LIST_HEAD(kprobe_blacklist);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070078
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080079#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070080/*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +090081 * 'kprobe::ainsn.insn' points to the copy of the instruction to be
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070082 * single-stepped. x86_64, POWER4 and above have no-exec support and
83 * stepping on the instruction on a vmalloced/kmalloced/data page
84 * is a recipe for disaster
85 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070086struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -040087 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070088 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -070089 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070090 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080091 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050092 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070093};
94
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050095#define KPROBE_INSN_PAGE_SIZE(slots) \
96 (offsetof(struct kprobe_insn_page, slot_used) + \
97 (sizeof(char) * (slots)))
98
Masami Hiramatsu4610ee12010-02-25 08:33:59 -050099static int slots_per_page(struct kprobe_insn_cache *c)
100{
101 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
102}
103
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800104enum kprobe_slot_state {
105 SLOT_CLEAN = 0,
106 SLOT_DIRTY = 1,
107 SLOT_USED = 2,
108};
109
Masami Hiramatsu63fef142017-08-18 17:24:00 +0900110void __weak *alloc_insn_page(void)
Heiko Carstensaf963972013-09-11 14:24:13 -0700111{
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900112 /*
113 * Use module_alloc() so this page is within +/- 2GB of where the
114 * kernel image and loaded module images reside. This is required
115 * for most of the architectures.
116 * (e.g. x86-64 needs this to handle the %rip-relative fixups.)
117 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700118 return module_alloc(PAGE_SIZE);
119}
120
Barry Song66ce7512021-06-30 18:56:31 -0700121static void free_insn_page(void *page)
Heiko Carstensaf963972013-09-11 14:24:13 -0700122{
Rusty Russellbe1f2212015-01-20 09:07:05 +1030123 module_memfree(page);
Heiko Carstensaf963972013-09-11 14:24:13 -0700124}
125
Heiko Carstensc802d642013-09-11 14:24:11 -0700126struct kprobe_insn_cache kprobe_insn_slots = {
127 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700128 .alloc = alloc_insn_page,
129 .free = free_insn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300130 .sym = KPROBE_INSN_PAGE_SYM,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500131 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
132 .insn_size = MAX_INSN_SIZE,
133 .nr_garbage = 0,
134};
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900135static int collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800136
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700137/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800138 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700139 * We allocate an executable page if there's no room on existing ones.
140 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900141kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700142{
143 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700144 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700145
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900146 /* Since the slot array is not protected by rcu, we need a mutex */
Heiko Carstensc802d642013-09-11 14:24:11 -0700147 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700148 retry:
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900149 rcu_read_lock();
150 list_for_each_entry_rcu(kip, &c->pages, list) {
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500151 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700152 int i;
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900153
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500154 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800155 if (kip->slot_used[i] == SLOT_CLEAN) {
156 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700157 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700158 slot = kip->insns + (i * c->insn_size);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900159 rcu_read_unlock();
Heiko Carstensc802d642013-09-11 14:24:11 -0700160 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700161 }
162 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500163 /* kip->nused is broken. Fix it. */
164 kip->nused = slots_per_page(c);
165 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700166 }
167 }
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900168 rcu_read_unlock();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700169
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800170 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500171 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800172 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500173
174 /* All out of space. Need to allocate a new page. */
175 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700176 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700177 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700178
Heiko Carstensaf963972013-09-11 14:24:13 -0700179 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700180 if (!kip->insns) {
181 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700182 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700183 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400184 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500185 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800186 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700187 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800188 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700189 kip->cache = c;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900190 list_add_rcu(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700191 slot = kip->insns;
Adrian Hunter69e49082020-05-12 15:19:11 +0300192
193 /* Record the perf ksymbol register event after adding the page */
194 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns,
195 PAGE_SIZE, false, c->sym);
Heiko Carstensc802d642013-09-11 14:24:11 -0700196out:
197 mutex_unlock(&c->mutex);
198 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800199}
200
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900201/* Return true if all garbages are collected, otherwise false. */
202static bool collect_one_slot(struct kprobe_insn_page *kip, int idx)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800203{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800204 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800205 kip->nused--;
206 if (kip->nused == 0) {
207 /*
208 * Page is no longer in use. Free it unless
209 * it's the last one. We keep the last one
210 * so as not to have to set it up again the
211 * next time somebody inserts a probe.
212 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500213 if (!list_is_singular(&kip->list)) {
Adrian Hunter69e49082020-05-12 15:19:11 +0300214 /*
215 * Record perf ksymbol unregister event before removing
216 * the page.
217 */
218 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
219 (unsigned long)kip->insns, PAGE_SIZE, true,
220 kip->cache->sym);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900221 list_del_rcu(&kip->list);
222 synchronize_rcu();
Heiko Carstensaf963972013-09-11 14:24:13 -0700223 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800224 kfree(kip);
225 }
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900226 return true;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800227 }
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900228 return false;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800229}
230
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900231static int collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800232{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400233 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800234
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500235 /* Ensure no-one is interrupted on the garbages */
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -0800236 synchronize_rcu();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800237
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500238 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800239 int i;
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900240
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800241 if (kip->ngarbage == 0)
242 continue;
243 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500244 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900245 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800246 break;
247 }
248 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500249 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800250 return 0;
251}
252
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900253void __free_insn_slot(struct kprobe_insn_cache *c,
254 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500255{
256 struct kprobe_insn_page *kip;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900257 long idx;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500258
Heiko Carstensc802d642013-09-11 14:24:11 -0700259 mutex_lock(&c->mutex);
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900260 rcu_read_lock();
261 list_for_each_entry_rcu(kip, &c->pages, list) {
262 idx = ((long)slot - (long)kip->insns) /
263 (c->insn_size * sizeof(kprobe_opcode_t));
264 if (idx >= 0 && idx < slots_per_page(c))
Heiko Carstensc802d642013-09-11 14:24:11 -0700265 goto out;
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900266 }
267 /* Could not find this slot. */
268 WARN_ON(1);
269 kip = NULL;
270out:
271 rcu_read_unlock();
272 /* Mark and sweep: this may sleep */
273 if (kip) {
274 /* Check double free */
275 WARN_ON(kip->slot_used[idx] != SLOT_USED);
276 if (dirty) {
277 kip->slot_used[idx] = SLOT_DIRTY;
278 kip->ngarbage++;
279 if (++c->nr_garbage > slots_per_page(c))
280 collect_garbage_slots(c);
281 } else {
282 collect_one_slot(kip, idx);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500283 }
284 }
Heiko Carstensc802d642013-09-11 14:24:11 -0700285 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500286}
287
Masami Hiramatsu5b485622017-01-08 23:58:09 +0900288/*
289 * Check given address is on the page of kprobe instruction slots.
290 * This will be used for checking whether the address on a stack
291 * is on a text area or not.
292 */
293bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
294{
295 struct kprobe_insn_page *kip;
296 bool ret = false;
297
298 rcu_read_lock();
299 list_for_each_entry_rcu(kip, &c->pages, list) {
300 if (addr >= (unsigned long)kip->insns &&
301 addr < (unsigned long)kip->insns + PAGE_SIZE) {
302 ret = true;
303 break;
304 }
305 }
306 rcu_read_unlock();
307
308 return ret;
309}
310
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300311int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
312 unsigned long *value, char *type, char *sym)
313{
314 struct kprobe_insn_page *kip;
315 int ret = -ERANGE;
316
317 rcu_read_lock();
318 list_for_each_entry_rcu(kip, &c->pages, list) {
319 if ((*symnum)--)
320 continue;
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900321 strscpy(sym, c->sym, KSYM_NAME_LEN);
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300322 *type = 't';
323 *value = (unsigned long)kip->insns;
324 ret = 0;
325 break;
326 }
327 rcu_read_unlock();
328
329 return ret;
330}
331
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500332#ifdef CONFIG_OPTPROBES
Christophe Leroy7ee3e972021-05-13 09:07:51 +0000333void __weak *alloc_optinsn_page(void)
334{
335 return alloc_insn_page();
336}
337
338void __weak free_optinsn_page(void *page)
339{
340 free_insn_page(page);
341}
342
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500343/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700344struct kprobe_insn_cache kprobe_optinsn_slots = {
345 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Christophe Leroy7ee3e972021-05-13 09:07:51 +0000346 .alloc = alloc_optinsn_page,
347 .free = free_optinsn_page,
Adrian Hunterd002b8b2020-05-28 11:00:58 +0300348 .sym = KPROBE_OPTINSN_PAGE_SYM,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500349 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
350 /* .insn_size is initialized later */
351 .nr_garbage = 0,
352};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500353#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800354#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700355
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800356/* We have preemption disabled.. so it is safe to use __ versions */
357static inline void set_kprobe_instance(struct kprobe *kp)
358{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600359 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800360}
361
362static inline void reset_kprobe_instance(void)
363{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600364 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800365}
366
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800367/*
368 * This routine is called either:
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900369 * - under the 'kprobe_mutex' - during kprobe_[un]register().
370 * OR
371 * - with preemption disabled - from architecture specific code.
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800372 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900373struct kprobe *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800376 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Masami Hiramatsu6743ad42020-05-12 17:02:33 +0900379 hlist_for_each_entry_rcu(p, head, hlist,
380 lockdep_is_held(&kprobe_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (p->addr == addr)
382 return p;
383 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return NULL;
386}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900387NOKPROBE_SYMBOL(get_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900389static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500390
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900391/* Return true if 'p' is an aggregator */
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900392static inline bool kprobe_aggrprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500393{
394 return p->pre_handler == aggr_pre_handler;
395}
396
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900397/* Return true if 'p' is unused */
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900398static inline bool kprobe_unused(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900399{
400 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
401 list_empty(&p->list);
402}
403
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900404/* Keep all fields in the kprobe consistent. */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900405static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500406{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900407 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
408 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500409}
410
411#ifdef CONFIG_OPTPROBES
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900412/* NOTE: This is protected by 'kprobe_mutex'. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500413static bool kprobes_allow_optimization;
414
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500415/*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900416 * Call all 'kprobe::pre_handler' on the list, but ignores its return value.
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500417 * This must be called from arch-dep optimized caller.
418 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900419void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500420{
421 struct kprobe *kp;
422
423 list_for_each_entry_rcu(kp, &p->list, list) {
424 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
425 set_kprobe_instance(kp);
Naveen N. Rao4f3a8712017-10-17 13:48:34 +0530426 kp->pre_handler(kp, regs);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500427 }
428 reset_kprobe_instance();
429 }
430}
Masami Hiramatsu820aede2014-04-17 17:18:21 +0900431NOKPROBE_SYMBOL(opt_pre_handler);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500432
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900433/* Free optimized instructions and optimized_kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900434static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900435{
436 struct optimized_kprobe *op;
437
438 op = container_of(p, struct optimized_kprobe, kp);
439 arch_remove_optimized_kprobe(op);
440 arch_remove_kprobe(p);
441 kfree(op);
442}
443
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900444/* Return true if the kprobe is ready for optimization. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500445static inline int kprobe_optready(struct kprobe *p)
446{
447 struct optimized_kprobe *op;
448
449 if (kprobe_aggrprobe(p)) {
450 op = container_of(p, struct optimized_kprobe, kp);
451 return arch_prepared_optinsn(&op->optinsn);
452 }
453
454 return 0;
455}
456
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900457/* Return true if the kprobe is disarmed. Note: p must be on hash list */
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900458static inline bool kprobe_disarmed(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900459{
460 struct optimized_kprobe *op;
461
462 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
463 if (!kprobe_aggrprobe(p))
464 return kprobe_disabled(p);
465
466 op = container_of(p, struct optimized_kprobe, kp);
467
468 return kprobe_disabled(p) && list_empty(&op->list);
469}
470
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900471/* Return true if the probe is queued on (un)optimizing lists */
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900472static bool kprobe_queued(struct kprobe *p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900473{
474 struct optimized_kprobe *op;
475
476 if (kprobe_aggrprobe(p)) {
477 op = container_of(p, struct optimized_kprobe, kp);
478 if (!list_empty(&op->list))
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900479 return true;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900480 }
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900481 return false;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900482}
483
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500484/*
485 * Return an optimized kprobe whose optimizing code replaces
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900486 * instructions including 'addr' (exclude breakpoint).
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500487 */
Masami Hiramatsuc42421e2021-09-14 23:40:07 +0900488static struct kprobe *get_optimized_kprobe(kprobe_opcode_t *addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500489{
490 int i;
491 struct kprobe *p = NULL;
492 struct optimized_kprobe *op;
493
494 /* Don't check i == 0, since that is a breakpoint case. */
Masami Hiramatsuc42421e2021-09-14 23:40:07 +0900495 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH / sizeof(kprobe_opcode_t); i++)
496 p = get_kprobe(addr - i);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500497
498 if (p && kprobe_optready(p)) {
499 op = container_of(p, struct optimized_kprobe, kp);
500 if (arch_within_optimized_kprobe(op, addr))
501 return p;
502 }
503
504 return NULL;
505}
506
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900507/* Optimization staging list, protected by 'kprobe_mutex' */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500508static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900509static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900510static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500511
512static void kprobe_optimizer(struct work_struct *work);
513static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
514#define OPTIMIZE_DELAY 5
515
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900516/*
517 * Optimize (replace a breakpoint with a jump) kprobes listed on
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900518 * 'optimizing_list'.
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900519 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900520static void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500521{
Andrea Righif1c6ece2019-08-12 20:43:02 +0200522 lockdep_assert_held(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500523 /*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900524 * The optimization/unoptimization refers 'online_cpus' via
525 * stop_machine() and cpu-hotplug modifies the 'online_cpus'.
526 * And same time, 'text_mutex' will be held in cpu-hotplug and here.
527 * This combination can cause a deadlock (cpu-hotplug tries to lock
528 * 'text_mutex' but stop_machine() can not be done because
529 * the 'online_cpus' has been changed)
530 * To avoid this deadlock, caller must have locked cpu-hotplug
531 * for preventing cpu-hotplug outside of 'text_mutex' locking.
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500532 */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200533 lockdep_assert_cpus_held();
534
535 /* Optimization never be done when disarmed */
536 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
537 list_empty(&optimizing_list))
538 return;
539
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900540 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900541}
542
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900543/*
544 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900545 * if need) kprobes listed on 'unoptimizing_list'.
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900546 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900547static void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900548{
549 struct optimized_kprobe *op, *tmp;
550
Andrea Righif1c6ece2019-08-12 20:43:02 +0200551 lockdep_assert_held(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200552 /* See comment in do_optimize_kprobes() */
553 lockdep_assert_cpus_held();
554
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900555 /* Unoptimization must be done anytime */
556 if (list_empty(&unoptimizing_list))
557 return;
558
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900559 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900560 /* Loop on 'freeing_list' for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900561 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900562 /* Switching from detour code to origin */
563 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900564 /* Disarm probes if marked disabled */
565 if (kprobe_disabled(&op->kp))
566 arch_disarm_kprobe(&op->kp);
567 if (kprobe_unused(&op->kp)) {
568 /*
569 * Remove unused probes from hash list. After waiting
570 * for synchronization, these probes are reclaimed.
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900571 * (reclaiming is done by do_free_cleaned_kprobes().)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900572 */
573 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900574 } else
575 list_del_init(&op->list);
576 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900577}
578
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900579/* Reclaim all kprobes on the 'freeing_list' */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900580static void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900581{
582 struct optimized_kprobe *op, *tmp;
583
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900584 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900585 list_del_init(&op->list);
Masami Hiramatsucbdd96f2018-09-11 19:21:09 +0900586 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
587 /*
588 * This must not happen, but if there is a kprobe
589 * still in use, keep it on kprobes hash list.
590 */
591 continue;
592 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900593 free_aggr_kprobe(&op->kp);
594 }
595}
596
597/* Start optimizer after OPTIMIZE_DELAY passed */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900598static void kick_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900599{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800600 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900601}
602
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900603/* Kprobe jump optimizer */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900604static void kprobe_optimizer(struct work_struct *work)
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900605{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900606 mutex_lock(&kprobe_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200607 cpus_read_lock();
Andrea Righif1c6ece2019-08-12 20:43:02 +0200608 mutex_lock(&text_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900609
610 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900611 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
612 * kprobes before waiting for quiesence period.
613 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900614 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900615
616 /*
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900617 * Step 2: Wait for quiesence period to ensure all potentially
618 * preempted tasks to have normally scheduled. Because optprobe
619 * may modify multiple instructions, there is a chance that Nth
620 * instruction is preempted. In that case, such tasks can return
621 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
622 * Note that on non-preemptive kernel, this is transparently converted
623 * to synchronoze_sched() to wait for all interrupts to have completed.
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900624 */
Masami Hiramatsua30b85d2017-10-20 08:43:39 +0900625 synchronize_rcu_tasks();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900626
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900627 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900628 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900629
630 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900631 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900632
Andrea Righif1c6ece2019-08-12 20:43:02 +0200633 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200634 cpus_read_unlock();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900635
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900636 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900637 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900638 kick_kprobe_optimizer();
Masami Hiramatsu1a0aa992020-05-12 17:02:56 +0900639
640 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900641}
642
643/* Wait for completing optimization and unoptimization */
Thomas Gleixner30e7d8942017-05-17 10:19:49 +0200644void wait_for_kprobe_optimizer(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900645{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800646 mutex_lock(&kprobe_mutex);
647
648 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
649 mutex_unlock(&kprobe_mutex);
650
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900651 /* This will also make 'optimizing_work' execute immmediately */
Tejun Heoad72b3b2012-12-21 17:57:00 -0800652 flush_delayed_work(&optimizing_work);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900653 /* 'optimizing_work' might not have been queued yet, relax */
Tejun Heoad72b3b2012-12-21 17:57:00 -0800654 cpu_relax();
655
656 mutex_lock(&kprobe_mutex);
657 }
658
659 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500660}
661
Masami Hiramatsue4add242020-01-07 23:42:24 +0900662static bool optprobe_queued_unopt(struct optimized_kprobe *op)
663{
664 struct optimized_kprobe *_op;
665
666 list_for_each_entry(_op, &unoptimizing_list, list) {
667 if (op == _op)
668 return true;
669 }
670
671 return false;
672}
673
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500674/* Optimize kprobe if p is ready to be optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900675static void optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500676{
677 struct optimized_kprobe *op;
678
679 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500680 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500681 (kprobe_disabled(p) || kprobes_all_disarmed))
682 return;
683
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900684 /* kprobes with 'post_handler' can not be optimized */
Masami Hiramatsu059053a2018-06-20 01:10:27 +0900685 if (p->post_handler)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500686 return;
687
688 op = container_of(p, struct optimized_kprobe, kp);
689
690 /* Check there is no other kprobes at the optimized instructions */
691 if (arch_check_optimized_kprobe(op) < 0)
692 return;
693
694 /* Check if it is already optimized. */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900695 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
696 if (optprobe_queued_unopt(op)) {
697 /* This is under unoptimizing. Just dequeue the probe */
698 list_del_init(&op->list);
699 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500700 return;
Masami Hiramatsue4add242020-01-07 23:42:24 +0900701 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500702 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900703
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900704 /*
705 * On the 'unoptimizing_list' and 'optimizing_list',
706 * 'op' must have OPTIMIZED flag
707 */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900708 if (WARN_ON_ONCE(!list_empty(&op->list)))
709 return;
710
711 list_add(&op->list, &optimizing_list);
712 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900713}
714
715/* Short cut to direct unoptimizing */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900716static void force_unoptimize_kprobe(struct optimized_kprobe *op)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900717{
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200718 lockdep_assert_cpus_held();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900719 arch_unoptimize_kprobe(op);
Masami Hiramatsuf66c0442019-11-27 14:57:04 +0900720 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500721}
722
723/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900724static void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500725{
726 struct optimized_kprobe *op;
727
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900728 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
729 return; /* This is not an optprobe nor optimized */
730
731 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900732 if (!kprobe_optimized(p))
733 return;
734
735 if (!list_empty(&op->list)) {
736 if (optprobe_queued_unopt(op)) {
737 /* Queued in unoptimizing queue */
738 if (force) {
739 /*
740 * Forcibly unoptimize the kprobe here, and queue it
741 * in the freeing list for release afterwards.
742 */
743 force_unoptimize_kprobe(op);
744 list_move(&op->list, &freeing_list);
745 }
746 } else {
747 /* Dequeue from the optimizing queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500748 list_del_init(&op->list);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900749 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900750 }
751 return;
752 }
753
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900754 /* Optimized kprobe case */
Masami Hiramatsue4add242020-01-07 23:42:24 +0900755 if (force) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900756 /* Forcibly update the code: this is a special case */
757 force_unoptimize_kprobe(op);
Masami Hiramatsue4add242020-01-07 23:42:24 +0900758 } else {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900759 list_add(&op->list, &unoptimizing_list);
760 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500761 }
762}
763
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900764/* Cancel unoptimizing for reusing */
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900765static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900766{
767 struct optimized_kprobe *op;
768
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900769 /*
770 * Unused kprobe MUST be on the way of delayed unoptimizing (means
771 * there is still a relative jump) and disabled.
772 */
773 op = container_of(ap, struct optimized_kprobe, kp);
Masami Hiramatsu44585152018-04-28 21:36:33 +0900774 WARN_ON_ONCE(list_empty(&op->list));
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900775 /* Enable the probe again */
776 ap->flags &= ~KPROBE_FLAG_DISABLED;
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900777 /* Optimize it again. (remove from 'op->list') */
Masami Hiramatsu5f843ed2019-04-15 15:01:25 +0900778 if (!kprobe_optready(ap))
779 return -EINVAL;
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900780
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900781 optimize_kprobe(ap);
Masami Hiramatsu819319f2018-09-11 19:20:40 +0900782 return 0;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900783}
784
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500785/* Remove optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900786static void kill_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500787{
788 struct optimized_kprobe *op;
789
790 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900791 if (!list_empty(&op->list))
792 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500793 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900794 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900795
796 if (kprobe_unused(p)) {
797 /* Enqueue if it is unused */
798 list_add(&op->list, &freeing_list);
799 /*
800 * Remove unused probes from the hash list. After waiting
801 * for synchronization, this probe is reclaimed.
802 * (reclaiming is done by do_free_cleaned_kprobes().)
803 */
804 hlist_del_rcu(&op->kp.hlist);
805 }
806
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900807 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500808 arch_remove_optimized_kprobe(op);
809}
810
Masami Hiramatsua4602462017-04-19 18:22:25 +0530811static inline
812void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
813{
814 if (!kprobe_ftrace(p))
815 arch_prepare_optimized_kprobe(op, p);
816}
817
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500818/* Try to prepare optimized instructions */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900819static void prepare_optimized_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500820{
821 struct optimized_kprobe *op;
822
823 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsua4602462017-04-19 18:22:25 +0530824 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500825}
826
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900827/* Allocate new optimized_kprobe and try to prepare optimized instructions. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900828static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500829{
830 struct optimized_kprobe *op;
831
832 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
833 if (!op)
834 return NULL;
835
836 INIT_LIST_HEAD(&op->list);
837 op->kp.addr = p->addr;
Masami Hiramatsua4602462017-04-19 18:22:25 +0530838 __prepare_optimized_kprobe(op, p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500839
840 return &op->kp;
841}
842
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900843static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500844
845/*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900846 * Prepare an optimized_kprobe and optimize it.
847 * NOTE: 'p' must be a normal registered kprobe.
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500848 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900849static void try_to_optimize_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500850{
851 struct kprobe *ap;
852 struct optimized_kprobe *op;
853
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900854 /* Impossible to optimize ftrace-based kprobe. */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900855 if (kprobe_ftrace(p))
856 return;
857
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900858 /* For preparing optimization, jump_label_text_reserved() is called. */
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200859 cpus_read_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +0900860 jump_label_lock();
861 mutex_lock(&text_mutex);
862
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500863 ap = alloc_aggr_kprobe(p);
864 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900865 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500866
867 op = container_of(ap, struct optimized_kprobe, kp);
868 if (!arch_prepared_optinsn(&op->optinsn)) {
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900869 /* If failed to setup optimizing, fallback to kprobe. */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900870 arch_remove_optimized_kprobe(op);
871 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900872 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500873 }
874
875 init_aggr_kprobe(ap, p);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900876 optimize_kprobe(ap); /* This just kicks optimizer thread. */
Masami Hiramatsu25764282012-06-05 19:28:26 +0900877
878out:
879 mutex_unlock(&text_mutex);
880 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200881 cpus_read_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500882}
883
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900884static void optimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500885{
886 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500887 struct kprobe *p;
888 unsigned int i;
889
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900890 mutex_lock(&kprobe_mutex);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900891 /* If optimization is already allowed, just return. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500892 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900893 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500894
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200895 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500896 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500897 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
898 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900899 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500900 if (!kprobe_disabled(p))
901 optimize_kprobe(p);
902 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200903 cpus_read_unlock();
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +0900904 pr_info("kprobe jump-optimization is enabled. All kprobes are optimized if possible.\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900905out:
906 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500907}
908
Masami Hiramatsuc85c9a22021-02-18 23:29:23 +0900909#ifdef CONFIG_SYSCTL
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900910static void unoptimize_all_kprobes(void)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500911{
912 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500913 struct kprobe *p;
914 unsigned int i;
915
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900916 mutex_lock(&kprobe_mutex);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900917 /* If optimization is already prohibited, just return. */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900918 if (!kprobes_allow_optimization) {
919 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500920 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900921 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500922
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200923 cpus_read_lock();
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500924 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500925 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
926 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +0900927 hlist_for_each_entry(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500928 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900929 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500930 }
931 }
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +0200932 cpus_read_unlock();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900933 mutex_unlock(&kprobe_mutex);
934
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900935 /* Wait for unoptimizing completion. */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900936 wait_for_kprobe_optimizer();
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +0900937 pr_info("kprobe jump-optimization is disabled. All kprobes are based on software breakpoint.\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500938}
939
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900940static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500941int sysctl_kprobes_optimization;
942int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200943 void *buffer, size_t *length,
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500944 loff_t *ppos)
945{
946 int ret;
947
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900948 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500949 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
950 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
951
952 if (sysctl_kprobes_optimization)
953 optimize_all_kprobes();
954 else
955 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900956 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500957
958 return ret;
959}
960#endif /* CONFIG_SYSCTL */
961
Masami Hiramatsu57d4e312021-09-14 23:39:55 +0900962/* Put a breakpoint for a probe. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900963static void __arm_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500964{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900965 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500966
Masami Hiramatsu57d4e312021-09-14 23:39:55 +0900967 lockdep_assert_held(&text_mutex);
968
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900969 /* Find the overlapping optimized kprobes. */
Masami Hiramatsuc42421e2021-09-14 23:40:07 +0900970 _p = get_optimized_kprobe(p->addr);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900971 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900972 /* Fallback to unoptimized kprobe */
973 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500974
975 arch_arm_kprobe(p);
976 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
977}
978
Masami Hiramatsu57d4e312021-09-14 23:39:55 +0900979/* Remove the breakpoint of a probe. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +0900980static void __disarm_kprobe(struct kprobe *p, bool reopt)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500981{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900982 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500983
Masami Hiramatsu57d4e312021-09-14 23:39:55 +0900984 lockdep_assert_held(&text_mutex);
985
Wang Nan69d54b92015-02-13 14:40:26 -0800986 /* Try to unoptimize */
987 unoptimize_kprobe(p, kprobes_all_disarmed);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500988
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900989 if (!kprobe_queued(p)) {
990 arch_disarm_kprobe(p);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900991 /* If another kprobe was blocked, re-optimize it. */
Masami Hiramatsuc42421e2021-09-14 23:40:07 +0900992 _p = get_optimized_kprobe(p->addr);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900993 if (unlikely(_p) && reopt)
994 optimize_kprobe(_p);
995 }
Masami Hiramatsu223a76b2021-09-14 23:39:34 +0900996 /*
997 * TODO: Since unoptimization and real disarming will be done by
998 * the worker thread, we can not check whether another probe are
999 * unoptimized because of this probe here. It should be re-optimized
1000 * by the worker thread.
1001 */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001002}
1003
1004#else /* !CONFIG_OPTPROBES */
1005
1006#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001007#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001008#define kill_optimized_kprobe(p) do {} while (0)
1009#define prepare_optimized_kprobe(p) do {} while (0)
1010#define try_to_optimize_kprobe(p) do {} while (0)
1011#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001012#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
1013#define kprobe_disarmed(p) kprobe_disabled(p)
1014#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001015
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001016static int reuse_unused_kprobe(struct kprobe *ap)
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001017{
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001018 /*
1019 * If the optimized kprobe is NOT supported, the aggr kprobe is
1020 * released at the same time that the last aggregated kprobe is
1021 * unregistered.
1022 * Thus there should be no chance to reuse unused kprobe.
1023 */
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09001024 WARN_ON_ONCE(1);
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001025 return -EINVAL;
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001026}
1027
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001028static void free_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001029{
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001030 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001031 kfree(p);
1032}
1033
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001034static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001035{
1036 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1037}
1038#endif /* CONFIG_OPTPROBES */
1039
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001040#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001041static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +09001042 .func = kprobe_ftrace_handler,
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001043 .flags = FTRACE_OPS_FL_SAVE_REGS,
1044};
1045
1046static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1047 .func = kprobe_ftrace_handler,
Masami Hiramatsu1d70be32014-11-21 05:25:23 -05001048 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001049};
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001050
1051static int kprobe_ipmodify_enabled;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001052static int kprobe_ftrace_enabled;
1053
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001054static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1055 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001056{
Jessica Yu12310e342018-01-10 00:51:23 +01001057 int ret = 0;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001058
Masami Hiramatsu57d4e312021-09-14 23:39:55 +09001059 lockdep_assert_held(&kprobe_mutex);
1060
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001061 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09001062 if (WARN_ONCE(ret < 0, "Failed to arm kprobe-ftrace at %pS (error %d)\n", p->addr, ret))
Jessica Yu12310e342018-01-10 00:51:23 +01001063 return ret;
Jessica Yu12310e342018-01-10 00:51:23 +01001064
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001065 if (*cnt == 0) {
1066 ret = register_ftrace_function(ops);
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09001067 if (WARN(ret < 0, "Failed to register kprobe-ftrace (error %d)\n", ret))
Jessica Yu12310e342018-01-10 00:51:23 +01001068 goto err_ftrace;
Jessica Yu12310e342018-01-10 00:51:23 +01001069 }
1070
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001071 (*cnt)++;
Jessica Yu12310e342018-01-10 00:51:23 +01001072 return ret;
1073
1074err_ftrace:
1075 /*
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001076 * At this point, sinec ops is not registered, we should be sefe from
1077 * registering empty filter.
Jessica Yu12310e342018-01-10 00:51:23 +01001078 */
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001079 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Jessica Yu12310e342018-01-10 00:51:23 +01001080 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001081}
1082
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001083static int arm_kprobe_ftrace(struct kprobe *p)
1084{
1085 bool ipmodify = (p->post_handler != NULL);
1086
1087 return __arm_kprobe_ftrace(p,
1088 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1089 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1090}
1091
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001092static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1093 int *cnt)
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001094{
Jessica Yu297f9232018-01-10 00:51:24 +01001095 int ret = 0;
1096
Masami Hiramatsu57d4e312021-09-14 23:39:55 +09001097 lockdep_assert_held(&kprobe_mutex);
1098
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001099 if (*cnt == 1) {
1100 ret = unregister_ftrace_function(ops);
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09001101 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (error %d)\n", ret))
Jessica Yu297f9232018-01-10 00:51:24 +01001102 return ret;
1103 }
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001104
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001105 (*cnt)--;
Jessica Yu297f9232018-01-10 00:51:24 +01001106
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001107 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09001108 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (error %d)\n",
Masami Hiramatsu44585152018-04-28 21:36:33 +09001109 p->addr, ret);
Jessica Yu297f9232018-01-10 00:51:24 +01001110 return ret;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001111}
Masami Hiramatsu0bc11ed2019-07-25 15:24:37 +09001112
1113static int disarm_kprobe_ftrace(struct kprobe *p)
1114{
1115 bool ipmodify = (p->post_handler != NULL);
1116
1117 return __disarm_kprobe_ftrace(p,
1118 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1119 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1120}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001121#else /* !CONFIG_KPROBES_ON_FTRACE */
Muchun Song10de7952020-08-06 01:20:46 +08001122static inline int arm_kprobe_ftrace(struct kprobe *p)
1123{
1124 return -ENODEV;
1125}
1126
1127static inline int disarm_kprobe_ftrace(struct kprobe *p)
1128{
1129 return -ENODEV;
1130}
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001131#endif
1132
Punit Agrawal02afb8d2021-09-14 23:38:57 +09001133static int prepare_kprobe(struct kprobe *p)
1134{
1135 /* Must ensure p->addr is really on ftrace */
1136 if (kprobe_ftrace(p))
1137 return arch_prepare_kprobe_ftrace(p);
1138
1139 return arch_prepare_kprobe(p);
1140}
1141
Jessica Yu12310e342018-01-10 00:51:23 +01001142static int arm_kprobe(struct kprobe *kp)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001143{
Jessica Yu12310e342018-01-10 00:51:23 +01001144 if (unlikely(kprobe_ftrace(kp)))
1145 return arm_kprobe_ftrace(kp);
1146
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001147 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001148 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001149 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001150 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001151 cpus_read_unlock();
Jessica Yu12310e342018-01-10 00:51:23 +01001152
1153 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001154}
1155
Jessica Yu297f9232018-01-10 00:51:24 +01001156static int disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001157{
Jessica Yu297f9232018-01-10 00:51:24 +01001158 if (unlikely(kprobe_ftrace(kp)))
1159 return disarm_kprobe_ftrace(kp);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001160
1161 cpus_read_lock();
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001162 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001163 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001164 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001165 cpus_read_unlock();
Jessica Yu297f9232018-01-10 00:51:24 +01001166
1167 return 0;
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001168}
1169
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001170/*
1171 * Aggregate handlers for multiple kprobes support - these handlers
1172 * take care of invoking the individual kprobe handlers on p->list
1173 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001174static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001175{
1176 struct kprobe *kp;
1177
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001178 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001179 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001180 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001181 if (kp->pre_handler(kp, regs))
1182 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001183 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001184 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001185 }
1186 return 0;
1187}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001188NOKPROBE_SYMBOL(aggr_pre_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001189
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001190static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1191 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001192{
1193 struct kprobe *kp;
1194
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001195 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001196 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001197 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001198 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001199 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001200 }
1201 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001202}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001203NOKPROBE_SYMBOL(aggr_post_handler);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001204
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001205/* Walks the list and increments 'nmissed' if 'p' has child probes. */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001206void kprobes_inc_nmissed_count(struct kprobe *p)
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001207{
1208 struct kprobe *kp;
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001209
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001210 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001211 p->nmissed++;
1212 } else {
1213 list_for_each_entry_rcu(kp, &p->list, list)
1214 kp->nmissed++;
1215 }
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001216}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001217NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001218
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001219static void free_rp_inst_rcu(struct rcu_head *head)
1220{
1221 struct kretprobe_instance *ri = container_of(head, struct kretprobe_instance, rcu);
1222
1223 if (refcount_dec_and_test(&ri->rph->ref))
1224 kfree(ri->rph);
1225 kfree(ri);
1226}
1227NOKPROBE_SYMBOL(free_rp_inst_rcu);
1228
Masami Hiramatsub3388172020-08-29 22:02:47 +09001229static void recycle_rp_inst(struct kretprobe_instance *ri)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001230{
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001231 struct kretprobe *rp = get_kretprobe(ri);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001232
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001233 if (likely(rp))
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001234 freelist_add(&ri->freelist, &rp->freelist);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001235 else
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001236 call_rcu(&ri->rcu, free_rp_inst_rcu);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001237}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001238NOKPROBE_SYMBOL(recycle_rp_inst);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001239
Masami Hiramatsu319f0ce2020-08-29 22:03:02 +09001240static struct kprobe kprobe_busy = {
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001241 .addr = (void *) get_kprobe,
1242};
1243
1244void kprobe_busy_begin(void)
1245{
1246 struct kprobe_ctlblk *kcb;
1247
1248 preempt_disable();
1249 __this_cpu_write(current_kprobe, &kprobe_busy);
1250 kcb = get_kprobe_ctlblk();
1251 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1252}
1253
1254void kprobe_busy_end(void)
1255{
1256 __this_cpu_write(current_kprobe, NULL);
1257 preempt_enable();
1258}
1259
Hien Nguyenb94cce92005-06-23 00:09:19 -07001260/*
Thomas Gleixner670721c2021-09-28 14:24:28 +02001261 * This function is called from delayed_put_task_struct() when a task is
Linus Torvalds79ef0c02021-11-01 20:05:19 -07001262 * dead and cleaned up to recycle any kretprobe instances associated with
1263 * this task. These left over instances represent probed functions that
1264 * have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001265 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001266void kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001267{
bibo,mao62c27be2006-10-02 02:17:33 -07001268 struct kretprobe_instance *ri;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001269 struct llist_node *node;
Rusty Lynch802eae72005-06-27 15:17:08 -07001270
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001271 /* Early boot, not yet initialized. */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001272 if (unlikely(!kprobes_initialized))
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001273 return;
1274
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001275 kprobe_busy_begin();
1276
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001277 node = __llist_del_all(&tk->kretprobe_instances);
1278 while (node) {
1279 ri = container_of(node, struct kretprobe_instance, llist);
1280 node = node->next;
1281
1282 recycle_rp_inst(ri);
bibo,mao62c27be2006-10-02 02:17:33 -07001283 }
Jiri Olsa9b38cc72020-05-12 17:03:18 +09001284
1285 kprobe_busy_end();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001286}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09001287NOKPROBE_SYMBOL(kprobe_flush_task);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001288
Hien Nguyenb94cce92005-06-23 00:09:19 -07001289static inline void free_rp_inst(struct kretprobe *rp)
1290{
1291 struct kretprobe_instance *ri;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001292 struct freelist_node *node;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001293 int count = 0;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001294
Peter Zijlstra6e426e02020-08-29 22:03:56 +09001295 node = rp->freelist.head;
1296 while (node) {
1297 ri = container_of(node, struct kretprobe_instance, freelist);
1298 node = node->next;
1299
Hien Nguyenb94cce92005-06-23 00:09:19 -07001300 kfree(ri);
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001301 count++;
1302 }
1303
1304 if (refcount_sub_and_test(count, &rp->rph->ref)) {
1305 kfree(rp->rph);
1306 rp->rph = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001307 }
1308}
1309
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001310/* Add the new probe to 'ap->list'. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001311static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001312{
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001313 if (p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001314 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001315
Masami Hiramatsu059053a2018-06-20 01:10:27 +09001316 list_add_rcu(&p->list, &ap->list);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001317 if (p->post_handler && !ap->post_handler)
1318 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001319
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001320 return 0;
1321}
1322
1323/*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001324 * Fill in the required fields of the aggregator kprobe. Replace the
1325 * earlier kprobe in the hlist with the aggregator kprobe.
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001326 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001327static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001328{
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001329 /* Copy the insn slot of 'p' to 'ap'. */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001330 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001331 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001332 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001333 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001334 ap->pre_handler = aggr_pre_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001335 /* We don't care the kprobe which has gone. */
1336 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001337 ap->post_handler = aggr_post_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001338
1339 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001340 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001341
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001342 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001343 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001344}
1345
1346/*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001347 * This registers the second or subsequent kprobe at the same address.
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001348 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001349static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001350{
1351 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001352 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001353
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001354 cpus_read_lock();
1355
Masami Hiramatsu25764282012-06-05 19:28:26 +09001356 /* For preparing optimization, jump_label_text_reserved() is called */
1357 jump_label_lock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001358 mutex_lock(&text_mutex);
1359
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001360 if (!kprobe_aggrprobe(orig_p)) {
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001361 /* If 'orig_p' is not an 'aggr_kprobe', create new one. */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001362 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001363 if (!ap) {
1364 ret = -ENOMEM;
1365 goto out;
1366 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001367 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001368 } else if (kprobe_unused(ap)) {
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001369 /* This probe is going to die. Rescue it */
Masami Hiramatsu819319f2018-09-11 19:20:40 +09001370 ret = reuse_unused_kprobe(ap);
1371 if (ret)
1372 goto out;
1373 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001374
1375 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001376 /*
1377 * Attempting to insert new probe at the same location that
1378 * had a probe in the module vaddr area which already
1379 * freed. So, the instruction slot has already been
1380 * released. We need a new slot for the new probe.
1381 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001382 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001383 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001384 /*
1385 * Even if fail to allocate new slot, don't need to
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001386 * free the 'ap'. It will be used next time, or
1387 * freed by unregister_kprobe().
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001388 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001389 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001390
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001391 /* Prepare optimized instructions if possible. */
1392 prepare_optimized_kprobe(ap);
1393
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001394 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001395 * Clear gone flag to prevent allocating new slot again, and
1396 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001397 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001398 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1399 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001400 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001401
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001402 /* Copy the insn slot of 'p' to 'ap'. */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001403 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001404 ret = add_new_kprobe(ap, p);
1405
1406out:
1407 mutex_unlock(&text_mutex);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001408 jump_label_unlock();
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001409 cpus_read_unlock();
Masami Hiramatsu25764282012-06-05 19:28:26 +09001410
1411 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1412 ap->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01001413 if (!kprobes_all_disarmed) {
Masami Hiramatsu25764282012-06-05 19:28:26 +09001414 /* Arm the breakpoint again. */
Jessica Yu12310e342018-01-10 00:51:23 +01001415 ret = arm_kprobe(ap);
1416 if (ret) {
1417 ap->flags |= KPROBE_FLAG_DISABLED;
1418 list_del_rcu(&p->list);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001419 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001420 }
1421 }
Masami Hiramatsu25764282012-06-05 19:28:26 +09001422 }
1423 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001424}
1425
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001426bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1427{
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001428 /* The '__kprobes' functions and entry code must not be probed. */
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001429 return addr >= (unsigned long)__kprobes_text_start &&
1430 addr < (unsigned long)__kprobes_text_end;
1431}
1432
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001433static bool __within_kprobe_blacklist(unsigned long addr)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001434{
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001435 struct kprobe_blacklist_entry *ent;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001436
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001437 if (arch_within_kprobe_blacklist(addr))
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001438 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001439 /*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001440 * If 'kprobe_blacklist' is defined, check the address and
1441 * reject any probe registration in the prohibited area.
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001442 */
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001443 list_for_each_entry(ent, &kprobe_blacklist, list) {
1444 if (addr >= ent->start_addr && addr < ent->end_addr)
1445 return true;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001446 }
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001447 return false;
1448}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001449
Masami Hiramatsu6143c6f2019-02-13 01:13:12 +09001450bool within_kprobe_blacklist(unsigned long addr)
1451{
1452 char symname[KSYM_NAME_LEN], *p;
1453
1454 if (__within_kprobe_blacklist(addr))
1455 return true;
1456
1457 /* Check if the address is on a suffixed-symbol */
1458 if (!lookup_symbol_name(addr, symname)) {
1459 p = strchr(symname, '.');
1460 if (!p)
1461 return false;
1462 *p = '\0';
1463 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1464 if (addr)
1465 return __within_kprobe_blacklist(addr);
1466 }
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001467 return false;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001468}
1469
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001470/*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001471 * If 'symbol_name' is specified, look it up and add the 'offset'
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001472 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001473 * This returns encoded errors if it fails to look up symbol or invalid
1474 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001475 */
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301476static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1477 const char *symbol_name, unsigned int offset)
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001478{
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301479 if ((symbol_name && addr) || (!symbol_name && !addr))
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001480 goto invalid;
1481
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301482 if (symbol_name) {
Linus Torvalds7246f602017-05-05 11:36:44 -07001483 addr = kprobe_lookup_name(symbol_name, offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001484 if (!addr)
1485 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001486 }
1487
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301488 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001489 if (addr)
1490 return addr;
1491
1492invalid:
1493 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001494}
1495
Naveen N. Rao1d585e72017-03-08 13:56:06 +05301496static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1497{
1498 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1499}
1500
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001501/*
1502 * Check the 'p' is valid and return the aggregator kprobe
1503 * at the same address.
1504 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001505static struct kprobe *__get_valid_kprobe(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301506{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001507 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301508
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001509 lockdep_assert_held(&kprobe_mutex);
1510
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001511 ap = get_kprobe(p->addr);
1512 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301513 return NULL;
1514
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001515 if (p != ap) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001516 list_for_each_entry(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301517 if (list_p == p)
1518 /* kprobe p is a valid probe */
1519 goto valid;
1520 return NULL;
1521 }
1522valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001523 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301524}
1525
Masami Hiramatsu33b1d142021-02-03 23:59:27 +09001526/*
1527 * Warn and return error if the kprobe is being re-registered since
1528 * there must be a software bug.
1529 */
1530static inline int warn_kprobe_rereg(struct kprobe *p)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301531{
1532 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301533
1534 mutex_lock(&kprobe_mutex);
Masami Hiramatsu33b1d142021-02-03 23:59:27 +09001535 if (WARN_ON_ONCE(__get_valid_kprobe(p)))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301536 ret = -EINVAL;
1537 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001538
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301539 return ret;
1540}
1541
Punit Agrawal4402dea2021-09-14 23:39:16 +09001542static int check_ftrace_location(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001544 unsigned long ftrace_addr;
1545
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001546 ftrace_addr = ftrace_location((unsigned long)p->addr);
1547 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001548#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001549 /* Given address is not on the instruction boundary */
1550 if ((unsigned long)p->addr != ftrace_addr)
1551 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001552 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001553#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001554 return -EINVAL;
1555#endif
1556 }
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001557 return 0;
1558}
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001559
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001560static int check_kprobe_address_safe(struct kprobe *p,
1561 struct module **probed_mod)
1562{
1563 int ret;
1564
Punit Agrawal4402dea2021-09-14 23:39:16 +09001565 ret = check_ftrace_location(p);
Heiko Carstensf7f242f2014-10-15 12:17:34 +02001566 if (ret)
1567 return ret;
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001568 jump_label_lock();
1569 preempt_disable();
1570
1571 /* Ensure it is not in reserved area nor out of text */
1572 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsu376e2422014-04-17 17:17:05 +09001573 within_kprobe_blacklist((unsigned long) p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001574 jump_label_text_reserved(p->addr, p->addr) ||
Peter Zijlstrafa68bd02021-06-28 13:24:12 +02001575 static_call_text_reserved(p->addr, p->addr) ||
Masami Hiramatsue336b402019-09-03 20:08:21 +09001576 find_bug((unsigned long)p->addr)) {
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001577 ret = -EINVAL;
1578 goto out;
1579 }
1580
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001581 /* Check if 'p' is probing a module. */
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001582 *probed_mod = __module_text_address((unsigned long) p->addr);
1583 if (*probed_mod) {
1584 /*
1585 * We must hold a refcount of the probed module while updating
1586 * its code to prohibit unexpected unloading.
1587 */
1588 if (unlikely(!try_module_get(*probed_mod))) {
1589 ret = -ENOENT;
1590 goto out;
1591 }
1592
1593 /*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001594 * If the module freed '.init.text', we couldn't insert
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001595 * kprobes in there.
1596 */
1597 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1598 (*probed_mod)->state != MODULE_STATE_COMING) {
1599 module_put(*probed_mod);
1600 *probed_mod = NULL;
1601 ret = -ENOENT;
1602 }
1603 }
1604out:
1605 preempt_enable();
1606 jump_label_unlock();
1607
1608 return ret;
1609}
1610
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001611int register_kprobe(struct kprobe *p)
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001612{
1613 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001614 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001615 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001616 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001618 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001619 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001620 if (IS_ERR(addr))
1621 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001622 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001623
Masami Hiramatsu33b1d142021-02-03 23:59:27 +09001624 ret = warn_kprobe_rereg(p);
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301625 if (ret)
1626 return ret;
1627
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001628 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1629 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001630 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001631 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001632
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001633 ret = check_kprobe_address_safe(p, &probed_mod);
1634 if (ret)
1635 return ret;
1636
1637 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001638
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001639 old_p = get_kprobe(p->addr);
1640 if (old_p) {
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001641 /* Since this may unoptimize 'old_p', locking 'text_mutex'. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001642 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 goto out;
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001646 cpus_read_lock();
1647 /* Prevent text modification */
1648 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001649 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001650 mutex_unlock(&text_mutex);
Thomas Gleixner2d1e38f2017-05-24 10:15:36 +02001651 cpus_read_unlock();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001652 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001653 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001654
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001655 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001656 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1658
Jessica Yu12310e342018-01-10 00:51:23 +01001659 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1660 ret = arm_kprobe(p);
1661 if (ret) {
1662 hlist_del_rcu(&p->hlist);
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001663 synchronize_rcu();
Jessica Yu12310e342018-01-10 00:51:23 +01001664 goto out;
1665 }
1666 }
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001667
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001668 /* Try to optimize kprobe */
1669 try_to_optimize_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001671 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001672
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001673 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001674 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return ret;
1677}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001678EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001680/* Check if all probes on the 'ap' are disabled. */
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +09001681static bool aggr_kprobe_disabled(struct kprobe *ap)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001682{
1683 struct kprobe *kp;
1684
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001685 lockdep_assert_held(&kprobe_mutex);
1686
1687 list_for_each_entry(kp, &ap->list, list)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001688 if (!kprobe_disabled(kp))
1689 /*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001690 * Since there is an active probe on the list,
1691 * we can't disable this 'ap'.
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001692 */
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +09001693 return false;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001694
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +09001695 return true;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001696}
1697
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001698static struct kprobe *__disable_kprobe(struct kprobe *p)
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001699{
1700 struct kprobe *orig_p;
Jessica Yu297f9232018-01-10 00:51:24 +01001701 int ret;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001702
Masami Hiramatsu57d4e312021-09-14 23:39:55 +09001703 lockdep_assert_held(&kprobe_mutex);
1704
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001705 /* Get an original kprobe for return */
1706 orig_p = __get_valid_kprobe(p);
1707 if (unlikely(orig_p == NULL))
Jessica Yu297f9232018-01-10 00:51:24 +01001708 return ERR_PTR(-EINVAL);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001709
1710 if (!kprobe_disabled(p)) {
1711 /* Disable probe if it is a child probe */
1712 if (p != orig_p)
1713 p->flags |= KPROBE_FLAG_DISABLED;
1714
1715 /* Try to disarm and disable this/parent probe */
1716 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Wang Nan69d54b92015-02-13 14:40:26 -08001717 /*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09001718 * If 'kprobes_all_disarmed' is set, 'orig_p'
Wang Nan69d54b92015-02-13 14:40:26 -08001719 * should have already been disarmed, so
1720 * skip unneed disarming process.
1721 */
Jessica Yu297f9232018-01-10 00:51:24 +01001722 if (!kprobes_all_disarmed) {
1723 ret = disarm_kprobe(orig_p, true);
1724 if (ret) {
1725 p->flags &= ~KPROBE_FLAG_DISABLED;
1726 return ERR_PTR(ret);
1727 }
1728 }
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001729 orig_p->flags |= KPROBE_FLAG_DISABLED;
1730 }
1731 }
1732
1733 return orig_p;
1734}
1735
Masami Hiramatsu98616682008-04-28 02:14:28 -07001736/*
1737 * Unregister a kprobe without a scheduler synchronization.
1738 */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001739static int __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001740{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001741 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001742
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001743 /* Disable kprobe. This will disarm it if needed. */
1744 ap = __disable_kprobe(p);
Jessica Yu297f9232018-01-10 00:51:24 +01001745 if (IS_ERR(ap))
1746 return PTR_ERR(ap);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001747
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001748 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001749 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001750 * This probe is an independent(and non-optimized) kprobe
1751 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001752 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001753 goto disarmed;
1754
1755 /* Following process expects this probe is an aggrprobe */
1756 WARN_ON(!kprobe_aggrprobe(ap));
1757
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001758 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1759 /*
1760 * !disarmed could be happen if the probe is under delayed
1761 * unoptimizing.
1762 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001763 goto disarmed;
1764 else {
1765 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001766 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09001767 list_for_each_entry(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001768 if ((list_p != p) && (list_p->post_handler))
1769 goto noclean;
1770 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001771 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001772 }
1773noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001774 /*
1775 * Remove from the aggrprobe: this path will do nothing in
1776 * __unregister_kprobe_bottom().
1777 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001778 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001779 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1780 /*
1781 * Try to optimize this probe again, because post
1782 * handler may have been changed.
1783 */
1784 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001785 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001786 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001787
1788disarmed:
1789 hlist_del_rcu(&ap->hlist);
1790 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001791}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001792
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001793static void __unregister_kprobe_bottom(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001794{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001795 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001796
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001797 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001798 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001799 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001800 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001801 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001802 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001803 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001804 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001805 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001806 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001809int register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001810{
1811 int i, ret = 0;
1812
1813 if (num <= 0)
1814 return -EINVAL;
1815 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001816 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001817 if (ret < 0) {
1818 if (i > 0)
1819 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001820 break;
1821 }
1822 }
1823 return ret;
1824}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001825EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001826
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001827void unregister_kprobe(struct kprobe *p)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001828{
1829 unregister_kprobes(&p, 1);
1830}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001831EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001832
Masami Hiramatsu55479f62014-04-17 17:17:54 +09001833void unregister_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001834{
1835 int i;
1836
1837 if (num <= 0)
1838 return;
1839 mutex_lock(&kprobe_mutex);
1840 for (i = 0; i < num; i++)
1841 if (__unregister_kprobe_top(kps[i]) < 0)
1842 kps[i]->addr = NULL;
1843 mutex_unlock(&kprobe_mutex);
1844
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08001845 synchronize_rcu();
Masami Hiramatsu98616682008-04-28 02:14:28 -07001846 for (i = 0; i < num; i++)
1847 if (kps[i]->addr)
1848 __unregister_kprobe_bottom(kps[i]);
1849}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001850EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001851
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301852int __weak kprobe_exceptions_notify(struct notifier_block *self,
1853 unsigned long val, void *data)
Naveen N. Raofc62d022017-02-08 01:24:14 +05301854{
1855 return NOTIFY_DONE;
1856}
Naveen N. Rao5f6bee32017-03-08 22:34:15 +05301857NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Naveen N. Raofc62d022017-02-08 01:24:14 +05301858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859static struct notifier_block kprobe_exceptions_nb = {
1860 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001861 .priority = 0x7fffffff /* we need to be notified first */
1862};
1863
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001864#ifdef CONFIG_KRETPROBES
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001865
Masami Hiramatsu03bac0d2021-09-14 23:41:04 +09001866/* This assumes the 'tsk' is the current task or the is not running. */
1867static kprobe_opcode_t *__kretprobe_find_ret_addr(struct task_struct *tsk,
1868 struct llist_node **cur)
1869{
1870 struct kretprobe_instance *ri = NULL;
1871 struct llist_node *node = *cur;
1872
1873 if (!node)
1874 node = tsk->kretprobe_instances.first;
1875 else
1876 node = node->next;
1877
1878 while (node) {
1879 ri = container_of(node, struct kretprobe_instance, llist);
1880 if (ri->ret_addr != kretprobe_trampoline_addr()) {
1881 *cur = node;
1882 return ri->ret_addr;
1883 }
1884 node = node->next;
1885 }
1886 return NULL;
1887}
1888NOKPROBE_SYMBOL(__kretprobe_find_ret_addr);
1889
1890/**
1891 * kretprobe_find_ret_addr -- Find correct return address modified by kretprobe
1892 * @tsk: Target task
1893 * @fp: A frame pointer
1894 * @cur: a storage of the loop cursor llist_node pointer for next call
1895 *
1896 * Find the correct return address modified by a kretprobe on @tsk in unsigned
1897 * long type. If it finds the return address, this returns that address value,
1898 * or this returns 0.
1899 * The @tsk must be 'current' or a task which is not running. @fp is a hint
1900 * to get the currect return address - which is compared with the
1901 * kretprobe_instance::fp field. The @cur is a loop cursor for searching the
1902 * kretprobe return addresses on the @tsk. The '*@cur' should be NULL at the
1903 * first call, but '@cur' itself must NOT NULL.
1904 */
1905unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
1906 struct llist_node **cur)
1907{
1908 struct kretprobe_instance *ri = NULL;
1909 kprobe_opcode_t *ret;
1910
1911 if (WARN_ON_ONCE(!cur))
1912 return 0;
1913
1914 do {
1915 ret = __kretprobe_find_ret_addr(tsk, cur);
1916 if (!ret)
1917 break;
1918 ri = container_of(*cur, struct kretprobe_instance, llist);
1919 } while (ri->fp != fp);
1920
1921 return (unsigned long)ret;
1922}
1923NOKPROBE_SYMBOL(kretprobe_find_ret_addr);
1924
Masami Hiramatsubf094cf2021-09-14 23:42:51 +09001925void __weak arch_kretprobe_fixup_return(struct pt_regs *regs,
1926 kprobe_opcode_t *correct_ret_addr)
1927{
1928 /*
1929 * Do nothing by default. Please fill this to update the fake return
1930 * address on the stack with the correct one on each arch if possible.
1931 */
1932}
1933
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001934unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001935 void *frame_pointer)
1936{
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001937 kprobe_opcode_t *correct_ret_addr = NULL;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001938 struct kretprobe_instance *ri = NULL;
Masami Hiramatsu03bac0d2021-09-14 23:41:04 +09001939 struct llist_node *first, *node = NULL;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001940 struct kretprobe *rp;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001941
Masami Hiramatsu03bac0d2021-09-14 23:41:04 +09001942 /* Find correct address and all nodes for this frame. */
1943 correct_ret_addr = __kretprobe_find_ret_addr(current, &node);
1944 if (!correct_ret_addr) {
1945 pr_err("kretprobe: Return address not found, not execute handler. Maybe there is a bug in the kernel.\n");
1946 BUG_ON(1);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001947 }
1948
Masami Hiramatsudf91c5b2021-09-14 23:42:12 +09001949 /*
1950 * Set the return address as the instruction pointer, because if the
1951 * user handler calls stack_trace_save_regs() with this 'regs',
1952 * the stack trace will start from the instruction pointer.
1953 */
1954 instruction_pointer_set(regs, (unsigned long)correct_ret_addr);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001955
Masami Hiramatsu03bac0d2021-09-14 23:41:04 +09001956 /* Run the user handler of the nodes. */
1957 first = current->kretprobe_instances.first;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001958 while (first) {
1959 ri = container_of(first, struct kretprobe_instance, llist);
Masami Hiramatsu03bac0d2021-09-14 23:41:04 +09001960
1961 if (WARN_ON_ONCE(ri->fp != frame_pointer))
1962 break;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001963
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001964 rp = get_kretprobe(ri);
1965 if (rp && rp->handler) {
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001966 struct kprobe *prev = kprobe_running();
1967
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001968 __this_cpu_write(current_kprobe, &rp->kp);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001969 ri->ret_addr = correct_ret_addr;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001970 rp->handler(ri, regs);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001971 __this_cpu_write(current_kprobe, prev);
1972 }
Masami Hiramatsu03bac0d2021-09-14 23:41:04 +09001973 if (first == node)
1974 break;
1975
1976 first = first->next;
1977 }
1978
Masami Hiramatsubf094cf2021-09-14 23:42:51 +09001979 arch_kretprobe_fixup_return(regs, correct_ret_addr);
1980
Masami Hiramatsu03bac0d2021-09-14 23:41:04 +09001981 /* Unlink all nodes for this frame. */
1982 first = current->kretprobe_instances.first;
1983 current->kretprobe_instances.first = node->next;
1984 node->next = NULL;
1985
1986 /* Recycle free instances. */
1987 while (first) {
1988 ri = container_of(first, struct kretprobe_instance, llist);
1989 first = first->next;
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001990
Masami Hiramatsub3388172020-08-29 22:02:47 +09001991 recycle_rp_inst(ri);
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001992 }
1993
Masami Hiramatsu66ada2c2020-08-29 22:00:01 +09001994 return (unsigned long)correct_ret_addr;
1995}
1996NOKPROBE_SYMBOL(__kretprobe_trampoline_handler)
1997
Adrian Bunke65cefe2006-02-03 03:03:42 -08001998/*
1999 * This kprobe pre_handler is registered with every kretprobe. When probe
2000 * hits it will set up the return probe.
2001 */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002002static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Adrian Bunke65cefe2006-02-03 03:03:42 -08002003{
2004 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002005 struct kretprobe_instance *ri;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002006 struct freelist_node *fn;
Adrian Bunke65cefe2006-02-03 03:03:42 -08002007
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002008 fn = freelist_try_get(&rp->freelist);
2009 if (!fn) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07002010 rp->nmissed++;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002011 return 0;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002012 }
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002013
2014 ri = container_of(fn, struct kretprobe_instance, freelist);
2015
2016 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
2017 freelist_add(&ri->freelist, &rp->freelist);
2018 return 0;
2019 }
2020
2021 arch_prepare_kretprobe(ri, regs);
2022
2023 __llist_add(&ri->llist, &current->kretprobe_instances);
2024
Adrian Bunke65cefe2006-02-03 03:03:42 -08002025 return 0;
2026}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002027NOKPROBE_SYMBOL(pre_handler_kretprobe);
Adrian Bunke65cefe2006-02-03 03:03:42 -08002028
Naveen N. Rao659b9572017-07-07 22:37:24 +05302029bool __weak arch_kprobe_on_func_entry(unsigned long offset)
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05302030{
2031 return !offset;
2032}
2033
Masami Hiramatsu97c753e2021-01-28 00:37:51 +09002034/**
2035 * kprobe_on_func_entry() -- check whether given address is function entry
2036 * @addr: Target address
2037 * @sym: Target symbol name
2038 * @offset: The offset from the symbol or the address
2039 *
2040 * This checks whether the given @addr+@offset or @sym+@offset is on the
2041 * function entry address or not.
2042 * This returns 0 if it is the function entry, or -EINVAL if it is not.
2043 * And also it returns -ENOENT if it fails the symbol or address lookup.
2044 * Caller must pass @addr or @sym (either one must be NULL), or this
2045 * returns -EINVAL.
2046 */
2047int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
Naveen N. Rao1d585e72017-03-08 13:56:06 +05302048{
2049 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
2050
2051 if (IS_ERR(kp_addr))
Masami Hiramatsu97c753e2021-01-28 00:37:51 +09002052 return PTR_ERR(kp_addr);
Naveen N. Rao1d585e72017-03-08 13:56:06 +05302053
Masami Hiramatsu97c753e2021-01-28 00:37:51 +09002054 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset))
2055 return -ENOENT;
Naveen N. Rao1d585e72017-03-08 13:56:06 +05302056
Masami Hiramatsu97c753e2021-01-28 00:37:51 +09002057 if (!arch_kprobe_on_func_entry(offset))
2058 return -EINVAL;
2059
2060 return 0;
Naveen N. Rao1d585e72017-03-08 13:56:06 +05302061}
2062
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002063int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002064{
Masami Hiramatsu97c753e2021-01-28 00:37:51 +09002065 int ret;
Hien Nguyenb94cce92005-06-23 00:09:19 -07002066 struct kretprobe_instance *inst;
2067 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08002068 void *addr;
Naveen N. Rao90ec5e82017-02-22 19:23:37 +05302069
Masami Hiramatsu97c753e2021-01-28 00:37:51 +09002070 ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset);
2071 if (ret)
2072 return ret;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002073
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002074 /* If only 'rp->kp.addr' is specified, check reregistering kprobes */
Masami Hiramatsu33b1d142021-02-03 23:59:27 +09002075 if (rp->kp.addr && warn_kprobe_rereg(&rp->kp))
Wang ShaoBo0188b872021-01-28 20:44:27 +08002076 return -EINVAL;
2077
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002078 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08002079 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09002080 if (IS_ERR(addr))
2081 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002082
2083 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2084 if (kretprobe_blacklist[i].addr == addr)
2085 return -EINVAL;
2086 }
2087 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07002088
Masami Hiramatsu6bbfa442021-12-01 23:45:50 +09002089 if (rp->data_size > KRETPROBE_MAX_DATA_SIZE)
2090 return -E2BIG;
2091
Hien Nguyenb94cce92005-06-23 00:09:19 -07002092 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07002093 rp->kp.post_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07002094
2095 /* Pre-allocate memory for max kretprobe instances */
2096 if (rp->maxactive <= 0) {
Thomas Gleixner92616602019-07-26 23:19:41 +02002097#ifdef CONFIG_PREEMPTION
Heiko Carstensc2ef6662009-12-21 13:02:24 +01002098 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07002099#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05302100 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07002101#endif
2102 }
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002103 rp->freelist.head = NULL;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002104 rp->rph = kzalloc(sizeof(struct kretprobe_holder), GFP_KERNEL);
2105 if (!rp->rph)
2106 return -ENOMEM;
2107
2108 rp->rph->rp = rp;
Hien Nguyenb94cce92005-06-23 00:09:19 -07002109 for (i = 0; i < rp->maxactive; i++) {
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002110 inst = kzalloc(sizeof(struct kretprobe_instance) +
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08002111 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002112 if (inst == NULL) {
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002113 refcount_set(&rp->rph->ref, i);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002114 free_rp_inst(rp);
2115 return -ENOMEM;
2116 }
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002117 inst->rph = rp->rph;
Peter Zijlstra6e426e02020-08-29 22:03:56 +09002118 freelist_add(&inst->freelist, &rp->freelist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002119 }
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002120 refcount_set(&rp->rph->ref, i);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002121
2122 rp->nmissed = 0;
2123 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002124 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002125 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002126 free_rp_inst(rp);
2127 return ret;
2128}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002129EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002130
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002131int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002132{
2133 int ret = 0, i;
2134
2135 if (num <= 0)
2136 return -EINVAL;
2137 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08002138 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07002139 if (ret < 0) {
2140 if (i > 0)
2141 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002142 break;
2143 }
2144 }
2145 return ret;
2146}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002147EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002148
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002149void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002150{
2151 unregister_kretprobes(&rp, 1);
2152}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002153EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002154
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002155void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002156{
2157 int i;
2158
2159 if (num <= 0)
2160 return;
2161 mutex_lock(&kprobe_mutex);
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002162 for (i = 0; i < num; i++) {
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002163 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2164 rps[i]->kp.addr = NULL;
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002165 rps[i]->rph->rp = NULL;
2166 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002167 mutex_unlock(&kprobe_mutex);
2168
Paul E. McKenneyae8b7ce2018-11-06 19:04:39 -08002169 synchronize_rcu();
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002170 for (i = 0; i < num; i++) {
2171 if (rps[i]->kp.addr) {
2172 __unregister_kprobe_bottom(&rps[i]->kp);
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002173 free_rp_inst(rps[i]);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002174 }
2175 }
2176}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002177EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002178
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002179#else /* CONFIG_KRETPROBES */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002180int register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07002181{
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002182 return -EOPNOTSUPP;
Hien Nguyenb94cce92005-06-23 00:09:19 -07002183}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002184EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002185
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002186int register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002187{
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002188 return -EOPNOTSUPP;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002189}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002190EXPORT_SYMBOL_GPL(register_kretprobes);
2191
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002192void unregister_kretprobe(struct kretprobe *rp)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002193{
2194}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002195EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002196
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002197void unregister_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002198{
2199}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002200EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002201
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002202static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002203{
2204 return 0;
2205}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002206NOKPROBE_SYMBOL(pre_handler_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07002207
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08002208#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002209
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002210/* Set the kprobe gone and remove its instruction buffer. */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002211static void kill_kprobe(struct kprobe *p)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002212{
2213 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002214
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002215 lockdep_assert_held(&kprobe_mutex);
2216
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002217 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002218 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002219 /*
2220 * If this is an aggr_kprobe, we have to list all the
2221 * chained probes and mark them GONE.
2222 */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002223 list_for_each_entry(kp, &p->list, list)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002224 kp->flags |= KPROBE_FLAG_GONE;
2225 p->post_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002226 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002227 }
2228 /*
2229 * Here, we can remove insn_slot safely, because no thread calls
2230 * the original probed function (which will be freed soon) any more.
2231 */
2232 arch_remove_kprobe(p);
Muchun Song0cb2f132020-07-28 14:45:36 +08002233
2234 /*
2235 * The module is going away. We should disarm the kprobe which
Masami Hiramatsubcb53202020-09-01 00:12:07 +09002236 * is using ftrace, because ftrace framework is still available at
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002237 * 'MODULE_STATE_GOING' notification.
Muchun Song0cb2f132020-07-28 14:45:36 +08002238 */
Masami Hiramatsubcb53202020-09-01 00:12:07 +09002239 if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
Muchun Song0cb2f132020-07-28 14:45:36 +08002240 disarm_kprobe_ftrace(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002241}
2242
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002243/* Disable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002244int disable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002245{
2246 int ret = 0;
Jessica Yu297f9232018-01-10 00:51:24 +01002247 struct kprobe *p;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002248
2249 mutex_lock(&kprobe_mutex);
2250
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09002251 /* Disable this kprobe */
Jessica Yu297f9232018-01-10 00:51:24 +01002252 p = __disable_kprobe(kp);
2253 if (IS_ERR(p))
2254 ret = PTR_ERR(p);
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002255
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002256 mutex_unlock(&kprobe_mutex);
2257 return ret;
2258}
2259EXPORT_SYMBOL_GPL(disable_kprobe);
2260
2261/* Enable one kprobe */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002262int enable_kprobe(struct kprobe *kp)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002263{
2264 int ret = 0;
2265 struct kprobe *p;
2266
2267 mutex_lock(&kprobe_mutex);
2268
2269 /* Check whether specified probe is valid. */
2270 p = __get_valid_kprobe(kp);
2271 if (unlikely(p == NULL)) {
2272 ret = -EINVAL;
2273 goto out;
2274 }
2275
2276 if (kprobe_gone(kp)) {
2277 /* This kprobe has gone, we couldn't enable it. */
2278 ret = -EINVAL;
2279 goto out;
2280 }
2281
2282 if (p != kp)
2283 kp->flags &= ~KPROBE_FLAG_DISABLED;
2284
2285 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2286 p->flags &= ~KPROBE_FLAG_DISABLED;
Jessica Yu12310e342018-01-10 00:51:23 +01002287 ret = arm_kprobe(p);
2288 if (ret)
2289 p->flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04002290 }
2291out:
2292 mutex_unlock(&kprobe_mutex);
2293 return ret;
2294}
2295EXPORT_SYMBOL_GPL(enable_kprobe);
2296
Masami Hiramatsu44585152018-04-28 21:36:33 +09002297/* Caller must NOT call this in usual path. This is only for critical case */
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002298void dump_kprobe(struct kprobe *kp)
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002299{
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09002300 pr_err("Dump kprobe:\n.symbol_name = %s, .offset = %x, .addr = %pS\n",
Masami Hiramatsu44585152018-04-28 21:36:33 +09002301 kp->symbol_name, kp->offset, kp->addr);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002302}
Masami Hiramatsu820aede2014-04-17 17:18:21 +09002303NOKPROBE_SYMBOL(dump_kprobe);
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002304
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002305int kprobe_add_ksym_blacklist(unsigned long entry)
2306{
2307 struct kprobe_blacklist_entry *ent;
2308 unsigned long offset = 0, size = 0;
2309
2310 if (!kernel_text_address(entry) ||
2311 !kallsyms_lookup_size_offset(entry, &size, &offset))
2312 return -EINVAL;
2313
2314 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2315 if (!ent)
2316 return -ENOMEM;
2317 ent->start_addr = entry;
2318 ent->end_addr = entry + size;
2319 INIT_LIST_HEAD(&ent->list);
2320 list_add_tail(&ent->list, &kprobe_blacklist);
2321
2322 return (int)size;
2323}
2324
2325/* Add all symbols in given area into kprobe blacklist */
2326int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2327{
2328 unsigned long entry;
2329 int ret = 0;
2330
2331 for (entry = start; entry < end; entry += ret) {
2332 ret = kprobe_add_ksym_blacklist(entry);
2333 if (ret < 0)
2334 return ret;
2335 if (ret == 0) /* In case of alias symbol */
2336 ret = 1;
2337 }
2338 return 0;
2339}
2340
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002341/* Remove all symbols in given area from kprobe blacklist */
2342static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2343{
2344 struct kprobe_blacklist_entry *ent, *n;
2345
2346 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2347 if (ent->start_addr < start || ent->start_addr >= end)
2348 continue;
2349 list_del(&ent->list);
2350 kfree(ent);
2351 }
2352}
2353
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002354static void kprobe_remove_ksym_blacklist(unsigned long entry)
2355{
2356 kprobe_remove_area_blacklist(entry, entry + 1);
2357}
2358
Adrian Hunterd002b8b2020-05-28 11:00:58 +03002359int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2360 char *type, char *sym)
2361{
2362 return -ERANGE;
2363}
2364
2365int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2366 char *sym)
2367{
2368#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2369 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2370 return 0;
2371#ifdef CONFIG_OPTPROBES
2372 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2373 return 0;
2374#endif
2375#endif
2376 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2377 return 0;
2378 return -ERANGE;
2379}
2380
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002381int __init __weak arch_populate_kprobe_blacklist(void)
2382{
2383 return 0;
2384}
2385
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002386/*
2387 * Lookup and populate the kprobe_blacklist.
2388 *
2389 * Unlike the kretprobe blacklist, we'll need to determine
2390 * the range of addresses that belong to the said functions,
2391 * since a kprobe need not necessarily be at the beginning
2392 * of a function.
2393 */
2394static int __init populate_kprobe_blacklist(unsigned long *start,
2395 unsigned long *end)
2396{
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002397 unsigned long entry;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002398 unsigned long *iter;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002399 int ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002400
2401 for (iter = start; iter < end; iter++) {
Masami Hiramatsuf2ec8d92021-09-14 23:40:36 +09002402 entry = (unsigned long)dereference_symbol_descriptor((void *)*iter);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002403 ret = kprobe_add_ksym_blacklist(entry);
2404 if (ret == -EINVAL)
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002405 continue;
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002406 if (ret < 0)
2407 return ret;
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002408 }
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002409
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002410 /* Symbols in '__kprobes_text' are blacklisted */
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002411 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2412 (unsigned long)__kprobes_text_end);
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002413 if (ret)
2414 return ret;
2415
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002416 /* Symbols in 'noinstr' section are blacklisted */
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002417 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2418 (unsigned long)__noinstr_text_end);
Masami Hiramatsufb1a59f2018-12-17 17:20:55 +09002419
2420 return ret ? : arch_populate_kprobe_blacklist();
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002421}
2422
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002423static void add_module_kprobe_blacklist(struct module *mod)
2424{
2425 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002426 int i;
2427
2428 if (mod->kprobe_blacklist) {
2429 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2430 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2431 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002432
2433 start = (unsigned long)mod->kprobes_text_start;
2434 if (start) {
2435 end = start + mod->kprobes_text_size;
2436 kprobe_add_area_blacklist(start, end);
2437 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002438
2439 start = (unsigned long)mod->noinstr_text_start;
2440 if (start) {
2441 end = start + mod->noinstr_text_size;
2442 kprobe_add_area_blacklist(start, end);
2443 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002444}
2445
2446static void remove_module_kprobe_blacklist(struct module *mod)
2447{
2448 unsigned long start, end;
Masami Hiramatsu16db6262020-03-26 23:50:00 +09002449 int i;
2450
2451 if (mod->kprobe_blacklist) {
2452 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2453 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2454 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002455
2456 start = (unsigned long)mod->kprobes_text_start;
2457 if (start) {
2458 end = start + mod->kprobes_text_size;
2459 kprobe_remove_area_blacklist(start, end);
2460 }
Thomas Gleixner66e9b072020-03-10 14:04:34 +01002461
2462 start = (unsigned long)mod->noinstr_text_start;
2463 if (start) {
2464 end = start + mod->noinstr_text_size;
2465 kprobe_remove_area_blacklist(start, end);
2466 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002467}
2468
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002469/* Module notifier call back, checking kprobes on the module */
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002470static int kprobes_module_callback(struct notifier_block *nb,
2471 unsigned long val, void *data)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002472{
2473 struct module *mod = data;
2474 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002475 struct kprobe *p;
2476 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002477 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002478
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002479 if (val == MODULE_STATE_COMING) {
2480 mutex_lock(&kprobe_mutex);
2481 add_module_kprobe_blacklist(mod);
2482 mutex_unlock(&kprobe_mutex);
2483 }
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002484 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002485 return NOTIFY_DONE;
2486
2487 /*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002488 * When 'MODULE_STATE_GOING' was notified, both of module '.text' and
2489 * '.init.text' sections would be freed. When 'MODULE_STATE_LIVE' was
2490 * notified, only '.init.text' section would be freed. We need to
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002491 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002492 */
2493 mutex_lock(&kprobe_mutex);
2494 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2495 head = &kprobe_table[i];
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002496 hlist_for_each_entry(p, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002497 if (within_module_init((unsigned long)p->addr, mod) ||
2498 (checkcore &&
2499 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002500 /*
2501 * The vaddr this probe is installed will soon
2502 * be vfreed buy not synced to disk. Hence,
2503 * disarming the breakpoint isn't needed.
Steven Rostedt (VMware)545a0282017-05-16 14:58:35 -04002504 *
2505 * Note, this will also move any optimized probes
2506 * that are pending to be removed from their
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002507 * corresponding lists to the 'freeing_list' and
Steven Rostedt (VMware)545a0282017-05-16 14:58:35 -04002508 * will not be touched by the delayed
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002509 * kprobe_optimizer() work handler.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002510 */
2511 kill_kprobe(p);
2512 }
2513 }
Masami Hiramatsu1e6769b2020-03-26 23:49:48 +09002514 if (val == MODULE_STATE_GOING)
2515 remove_module_kprobe_blacklist(mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002516 mutex_unlock(&kprobe_mutex);
2517 return NOTIFY_DONE;
2518}
2519
2520static struct notifier_block kprobe_module_nb = {
2521 .notifier_call = kprobes_module_callback,
2522 .priority = 0
2523};
2524
Masami Hiramatsu82d083a2020-09-10 17:55:05 +09002525void kprobe_free_init_mem(void)
2526{
2527 void *start = (void *)(&__init_begin);
2528 void *end = (void *)(&__init_end);
2529 struct hlist_head *head;
2530 struct kprobe *p;
2531 int i;
2532
2533 mutex_lock(&kprobe_mutex);
2534
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002535 /* Kill all kprobes on initmem because the target code has been freed. */
Masami Hiramatsu82d083a2020-09-10 17:55:05 +09002536 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2537 head = &kprobe_table[i];
2538 hlist_for_each_entry(p, head, hlist) {
2539 if (start <= (void *)p->addr && (void *)p->addr < end)
2540 kill_kprobe(p);
2541 }
2542 }
2543
2544 mutex_unlock(&kprobe_mutex);
2545}
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547static int __init init_kprobes(void)
2548{
2549 int i, err = 0;
2550
2551 /* FIXME allocate the probe table, currently defined statically */
2552 /* initialize all list heads */
Peter Zijlstrad741bf42020-08-29 22:03:24 +09002553 for (i = 0; i < KPROBE_TABLE_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 INIT_HLIST_HEAD(&kprobe_table[i]);
2555
Masami Hiramatsu376e2422014-04-17 17:17:05 +09002556 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2557 __stop_kprobe_blacklist);
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002558 if (err)
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09002559 pr_err("Failed to populate blacklist (error %d), kprobes not restricted, be careful using them!\n", err);
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002560
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002561 if (kretprobe_blacklist_size) {
2562 /* lookup the function address from its name */
2563 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
Naveen N. Rao49e0b462017-04-19 18:21:00 +05302564 kretprobe_blacklist[i].addr =
Naveen N. Rao290e3072017-04-19 18:21:01 +05302565 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002566 if (!kretprobe_blacklist[i].addr)
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09002567 pr_err("Failed to lookup symbol '%s' for kretprobe blacklist. Maybe the target function is removed or renamed.\n",
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002568 kretprobe_blacklist[i].name);
2569 }
2570 }
2571
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002572 /* By default, kprobes are armed */
2573 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002574
Masami Hiramatsuc85c9a22021-02-18 23:29:23 +09002575#if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002576 /* Init 'kprobe_optinsn_slots' for allocation */
Masami Hiramatsuc85c9a22021-02-18 23:29:23 +09002577 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2578#endif
2579
Rusty Lynch67729262005-07-05 18:54:50 -07002580 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002581 if (!err)
2582 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002583 if (!err)
2584 err = register_module_notifier(&kprobe_module_nb);
2585
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002586 kprobes_initialized = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return err;
2588}
Masami Hiramatsu36dadef2020-09-10 21:38:39 +09002589early_initcall(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Masami Hiramatsuc85c9a22021-02-18 23:29:23 +09002591#if defined(CONFIG_OPTPROBES)
2592static int __init init_optprobes(void)
2593{
2594 /*
2595 * Enable kprobe optimization - this kicks the optimizer which
2596 * depends on synchronize_rcu_tasks() and ksoftirqd, that is
2597 * not spawned in early initcall. So delay the optimization.
2598 */
2599 optimize_all_kprobes();
2600
2601 return 0;
2602}
2603subsys_initcall(init_optprobes);
2604#endif
2605
Srinivasa Ds346fd592007-02-20 13:57:54 -08002606#ifdef CONFIG_DEBUG_FS
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002607static void report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002608 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002609{
2610 char *kprobe_type;
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002611 void *addr = p->addr;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002612
2613 if (p->pre_handler == pre_handler_kretprobe)
2614 kprobe_type = "r";
Srinivasa Ds346fd592007-02-20 13:57:54 -08002615 else
2616 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002617
Kees Cook60f7bb62020-07-02 15:20:22 -07002618 if (!kallsyms_show_value(pi->file->f_cred))
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002619 addr = NULL;
2620
Srinivasa Ds346fd592007-02-20 13:57:54 -08002621 if (sym)
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002622 seq_printf(pi, "%px %s %s+0x%x %s ",
2623 addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002624 (modname ? modname : " "));
Masami Hiramatsu81365a92018-04-28 21:36:02 +09002625 else /* try to use %pS */
2626 seq_printf(pi, "%px %s %pS ",
2627 addr, kprobe_type, p->addr);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002628
2629 if (!pp)
2630 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002631 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002632 (kprobe_gone(p) ? "[GONE]" : ""),
2633 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002634 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2635 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002636}
2637
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002638static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002639{
2640 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2641}
2642
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002643static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002644{
2645 (*pos)++;
2646 if (*pos >= KPROBE_TABLE_SIZE)
2647 return NULL;
2648 return pos;
2649}
2650
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002651static void kprobe_seq_stop(struct seq_file *f, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002652{
2653 /* Nothing to do */
2654}
2655
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002656static int show_kprobe_addr(struct seq_file *pi, void *v)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002657{
2658 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002659 struct kprobe *p, *kp;
2660 const char *sym = NULL;
2661 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002662 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002663 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002664
2665 head = &kprobe_table[i];
2666 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002667 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002668 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002669 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002670 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002671 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002672 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002673 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002674 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002675 }
2676 preempt_enable();
2677 return 0;
2678}
2679
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002680static const struct seq_operations kprobes_sops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002681 .start = kprobe_seq_start,
2682 .next = kprobe_seq_next,
2683 .stop = kprobe_seq_stop,
2684 .show = show_kprobe_addr
2685};
2686
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002687DEFINE_SEQ_ATTRIBUTE(kprobes);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002688
Masami Hiramatsu63724742014-04-17 17:18:49 +09002689/* kprobes/blacklist -- shows which functions can not be probed */
2690static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2691{
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002692 mutex_lock(&kprobe_mutex);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002693 return seq_list_start(&kprobe_blacklist, *pos);
2694}
2695
2696static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2697{
2698 return seq_list_next(v, &kprobe_blacklist, pos);
2699}
2700
2701static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2702{
2703 struct kprobe_blacklist_entry *ent =
2704 list_entry(v, struct kprobe_blacklist_entry, list);
2705
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002706 /*
Masami Hiramatsu223a76b2021-09-14 23:39:34 +09002707 * If '/proc/kallsyms' is not showing kernel address, we won't
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002708 * show them here either.
2709 */
Kees Cook60f7bb62020-07-02 15:20:22 -07002710 if (!kallsyms_show_value(m->file->f_cred))
Masami Hiramatsuffb9bd62018-04-28 21:35:32 +09002711 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2712 (void *)ent->start_addr);
2713 else
2714 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2715 (void *)ent->end_addr, (void *)ent->start_addr);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002716 return 0;
2717}
2718
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002719static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2720{
2721 mutex_unlock(&kprobe_mutex);
2722}
2723
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002724static const struct seq_operations kprobe_blacklist_sops = {
Masami Hiramatsu63724742014-04-17 17:18:49 +09002725 .start = kprobe_blacklist_seq_start,
2726 .next = kprobe_blacklist_seq_next,
Masami Hiramatsu4fdd8882020-03-26 23:49:36 +09002727 .stop = kprobe_blacklist_seq_stop,
Masami Hiramatsu63724742014-04-17 17:18:49 +09002728 .show = kprobe_blacklist_seq_show,
2729};
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002730DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002731
Jessica Yu12310e342018-01-10 00:51:23 +01002732static int arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002733{
2734 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002735 struct kprobe *p;
Jessica Yu12310e342018-01-10 00:51:23 +01002736 unsigned int i, total = 0, errors = 0;
2737 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002738
2739 mutex_lock(&kprobe_mutex);
2740
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002741 /* If kprobes are armed, just return */
2742 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002743 goto already_enabled;
2744
Wang Nan977ad482015-02-13 14:40:24 -08002745 /*
2746 * optimize_kprobe() called by arm_kprobe() checks
2747 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2748 * arm_kprobe.
2749 */
2750 kprobes_all_disarmed = false;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002751 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002752 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2753 head = &kprobe_table[i];
Jessica Yu12310e342018-01-10 00:51:23 +01002754 /* Arm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002755 hlist_for_each_entry(p, head, hlist) {
Jessica Yu12310e342018-01-10 00:51:23 +01002756 if (!kprobe_disabled(p)) {
2757 err = arm_kprobe(p);
2758 if (err) {
2759 errors++;
2760 ret = err;
2761 }
2762 total++;
2763 }
2764 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002765 }
2766
Jessica Yu12310e342018-01-10 00:51:23 +01002767 if (errors)
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09002768 pr_warn("Kprobes globally enabled, but failed to enable %d out of %d probes. Please check which kprobes are kept disabled via debugfs.\n",
Jessica Yu12310e342018-01-10 00:51:23 +01002769 errors, total);
2770 else
2771 pr_info("Kprobes globally enabled\n");
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002772
2773already_enabled:
2774 mutex_unlock(&kprobe_mutex);
Jessica Yu12310e342018-01-10 00:51:23 +01002775 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002776}
2777
Jessica Yu297f9232018-01-10 00:51:24 +01002778static int disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002779{
2780 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002781 struct kprobe *p;
Jessica Yu297f9232018-01-10 00:51:24 +01002782 unsigned int i, total = 0, errors = 0;
2783 int err, ret = 0;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002784
2785 mutex_lock(&kprobe_mutex);
2786
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002787 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002788 if (kprobes_all_disarmed) {
2789 mutex_unlock(&kprobe_mutex);
Jessica Yu297f9232018-01-10 00:51:24 +01002790 return 0;
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002791 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002792
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002793 kprobes_all_disarmed = true;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002794
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002795 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2796 head = &kprobe_table[i];
Jessica Yu297f9232018-01-10 00:51:24 +01002797 /* Disarm all kprobes on a best-effort basis */
Masami Hiramatsu7e6a71d2020-05-12 17:02:44 +09002798 hlist_for_each_entry(p, head, hlist) {
Jessica Yu297f9232018-01-10 00:51:24 +01002799 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2800 err = disarm_kprobe(p, false);
2801 if (err) {
2802 errors++;
2803 ret = err;
2804 }
2805 total++;
2806 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002807 }
2808 }
Jessica Yu297f9232018-01-10 00:51:24 +01002809
2810 if (errors)
Masami Hiramatsu9c89bb82021-09-14 23:39:25 +09002811 pr_warn("Kprobes globally disabled, but failed to disable %d out of %d probes. Please check which kprobes are kept enabled via debugfs.\n",
Jessica Yu297f9232018-01-10 00:51:24 +01002812 errors, total);
2813 else
2814 pr_info("Kprobes globally disabled\n");
2815
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002816 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002817
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002818 /* Wait for disarming all kprobes by optimizer */
2819 wait_for_kprobe_optimizer();
Jessica Yu297f9232018-01-10 00:51:24 +01002820
2821 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002822}
2823
2824/*
2825 * XXX: The debugfs bool file interface doesn't allow for callbacks
2826 * when the bool state is switched. We can reuse that facility when
2827 * available
2828 */
2829static ssize_t read_enabled_file_bool(struct file *file,
2830 char __user *user_buf, size_t count, loff_t *ppos)
2831{
2832 char buf[3];
2833
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002834 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002835 buf[0] = '1';
2836 else
2837 buf[0] = '0';
2838 buf[1] = '\n';
2839 buf[2] = 0x00;
2840 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2841}
2842
2843static ssize_t write_enabled_file_bool(struct file *file,
2844 const char __user *user_buf, size_t count, loff_t *ppos)
2845{
Punit Agrawal5d6de7d2021-09-14 23:38:46 +09002846 bool enable;
2847 int ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002848
Punit Agrawal5d6de7d2021-09-14 23:38:46 +09002849 ret = kstrtobool_from_user(user_buf, count, &enable);
2850 if (ret)
2851 return ret;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002852
Punit Agrawal5d6de7d2021-09-14 23:38:46 +09002853 ret = enable ? arm_all_kprobes() : disarm_all_kprobes();
Jessica Yu12310e342018-01-10 00:51:23 +01002854 if (ret)
2855 return ret;
2856
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002857 return count;
2858}
2859
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002860static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002861 .read = read_enabled_file_bool,
2862 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002863 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002864};
2865
Masami Hiramatsu55479f62014-04-17 17:17:54 +09002866static int __init debugfs_kprobe_init(void)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002867{
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002868 struct dentry *dir;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002869
2870 dir = debugfs_create_dir("kprobes", NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002871
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002872 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002873
Punit Agrawal8f7262c2021-09-14 23:38:37 +09002874 debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp);
Masami Hiramatsu63724742014-04-17 17:18:49 +09002875
Greg Kroah-Hartman8c0fd1f2019-01-22 16:21:46 +01002876 debugfs_create_file("blacklist", 0400, dir, NULL,
Kefeng Wangeac2cece2020-06-04 16:51:11 -07002877 &kprobe_blacklist_fops);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002878
Srinivasa Ds346fd592007-02-20 13:57:54 -08002879 return 0;
2880}
2881
2882late_initcall(debugfs_kprobe_init);
2883#endif /* CONFIG_DEBUG_FS */