Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Kernel Probes (KProbes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * 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 Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 16 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | */ |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 20 | |
| 21 | #define pr_fmt(fmt) "kprobes: " fmt |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/kprobes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/hash.h> |
| 25 | #include <linux/init.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 26 | #include <linux/slab.h> |
Randy Dunlap | e386979 | 2007-05-08 00:27:01 -0700 | [diff] [blame] | 27 | #include <linux/stddef.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 28 | #include <linux/export.h> |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 29 | #include <linux/moduleloader.h> |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 30 | #include <linux/kallsyms.h> |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 31 | #include <linux/freezer.h> |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/debugfs.h> |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 34 | #include <linux/sysctl.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 35 | #include <linux/kdebug.h> |
Mathieu Desnoyers | 4460fda | 2009-03-06 10:36:38 -0500 | [diff] [blame] | 36 | #include <linux/memory.h> |
Masami Hiramatsu | 4554dbc | 2010-02-02 16:49:18 -0500 | [diff] [blame] | 37 | #include <linux/ftrace.h> |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 38 | #include <linux/cpu.h> |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 39 | #include <linux/jump_label.h> |
Peter Zijlstra | fa68bd0 | 2021-06-28 13:24:12 +0200 | [diff] [blame] | 40 | #include <linux/static_call.h> |
Adrian Hunter | 69e4908 | 2020-05-12 15:19:11 +0300 | [diff] [blame] | 41 | #include <linux/perf_event.h> |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 42 | |
Christoph Hellwig | bfd45be | 2016-10-11 13:52:22 -0700 | [diff] [blame] | 43 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/cacheflush.h> |
| 45 | #include <asm/errno.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 46 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #define KPROBE_HASH_BITS 6 |
| 49 | #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) |
| 50 | |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 51 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 52 | static int kprobes_initialized; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 53 | /* kprobe_table can be accessed by |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 54 | * - Normal hlist traversal and RCU add/del under 'kprobe_mutex' is held. |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 55 | * Or |
| 56 | * - RCU hlist traversal under disabling preempt (breakpoint handlers) |
| 57 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; |
| 59 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 60 | /* NOTE: change this value only with 'kprobe_mutex' held */ |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 61 | static bool kprobes_all_disarmed; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 62 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 63 | /* This protects 'kprobe_table' and 'optimizing_list' */ |
Masami Hiramatsu | 43948f5 | 2010-10-25 22:18:01 +0900 | [diff] [blame] | 64 | static DEFINE_MUTEX(kprobe_mutex); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 65 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 66 | |
Naveen N. Rao | 290e307 | 2017-04-19 18:21:01 +0530 | [diff] [blame] | 67 | kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, |
| 68 | unsigned int __unused) |
Naveen N. Rao | 49e0b46 | 2017-04-19 18:21:00 +0530 | [diff] [blame] | 69 | { |
| 70 | return ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); |
| 71 | } |
| 72 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 73 | /* |
| 74 | * Blacklist -- list of 'struct kprobe_blacklist_entry' to store info where |
| 75 | * kprobes can not probe. |
| 76 | */ |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 77 | static LIST_HEAD(kprobe_blacklist); |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 78 | |
Anil S Keshavamurthy | 2d14e39 | 2006-01-09 20:52:41 -0800 | [diff] [blame] | 79 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 80 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 81 | * 'kprobe::ainsn.insn' points to the copy of the instruction to be |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 82 | * 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 Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 86 | struct kprobe_insn_page { |
Masami Hiramatsu | c5cb5a2 | 2009-06-30 17:08:14 -0400 | [diff] [blame] | 87 | struct list_head list; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 88 | kprobe_opcode_t *insns; /* Page of instruction slots */ |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 89 | struct kprobe_insn_cache *cache; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 90 | int nused; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 91 | int ngarbage; |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 92 | char slot_used[]; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 95 | #define KPROBE_INSN_PAGE_SIZE(slots) \ |
| 96 | (offsetof(struct kprobe_insn_page, slot_used) + \ |
| 97 | (sizeof(char) * (slots))) |
| 98 | |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 99 | static int slots_per_page(struct kprobe_insn_cache *c) |
| 100 | { |
| 101 | return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t)); |
| 102 | } |
| 103 | |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 104 | enum kprobe_slot_state { |
| 105 | SLOT_CLEAN = 0, |
| 106 | SLOT_DIRTY = 1, |
| 107 | SLOT_USED = 2, |
| 108 | }; |
| 109 | |
Masami Hiramatsu | 63fef14 | 2017-08-18 17:24:00 +0900 | [diff] [blame] | 110 | void __weak *alloc_insn_page(void) |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 111 | { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 112 | /* |
| 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 Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 118 | return module_alloc(PAGE_SIZE); |
| 119 | } |
| 120 | |
Barry Song | 66ce751 | 2021-06-30 18:56:31 -0700 | [diff] [blame] | 121 | static void free_insn_page(void *page) |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 122 | { |
Rusty Russell | be1f221 | 2015-01-20 09:07:05 +1030 | [diff] [blame] | 123 | module_memfree(page); |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 126 | struct kprobe_insn_cache kprobe_insn_slots = { |
| 127 | .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex), |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 128 | .alloc = alloc_insn_page, |
| 129 | .free = free_insn_page, |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 130 | .sym = KPROBE_INSN_PAGE_SYM, |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 131 | .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages), |
| 132 | .insn_size = MAX_INSN_SIZE, |
| 133 | .nr_garbage = 0, |
| 134 | }; |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 135 | static int collect_garbage_slots(struct kprobe_insn_cache *c); |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 136 | |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 137 | /** |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 138 | * __get_insn_slot() - Find a slot on an executable page for an instruction. |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 139 | * We allocate an executable page if there's no room on existing ones. |
| 140 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 141 | kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c) |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 142 | { |
| 143 | struct kprobe_insn_page *kip; |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 144 | kprobe_opcode_t *slot = NULL; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 145 | |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 146 | /* Since the slot array is not protected by rcu, we need a mutex */ |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 147 | mutex_lock(&c->mutex); |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 148 | retry: |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 149 | rcu_read_lock(); |
| 150 | list_for_each_entry_rcu(kip, &c->pages, list) { |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 151 | if (kip->nused < slots_per_page(c)) { |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 152 | int i; |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 153 | |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 154 | for (i = 0; i < slots_per_page(c); i++) { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 155 | if (kip->slot_used[i] == SLOT_CLEAN) { |
| 156 | kip->slot_used[i] = SLOT_USED; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 157 | kip->nused++; |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 158 | slot = kip->insns + (i * c->insn_size); |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 159 | rcu_read_unlock(); |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 160 | goto out; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 163 | /* kip->nused is broken. Fix it. */ |
| 164 | kip->nused = slots_per_page(c); |
| 165 | WARN_ON(1); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 168 | rcu_read_unlock(); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 169 | |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 170 | /* If there are any garbage slots, collect it and try again. */ |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 171 | if (c->nr_garbage && collect_garbage_slots(c) == 0) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 172 | goto retry; |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 173 | |
| 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 Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 176 | if (!kip) |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 177 | goto out; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 178 | |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 179 | kip->insns = c->alloc(); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 180 | if (!kip->insns) { |
| 181 | kfree(kip); |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 182 | goto out; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 183 | } |
Masami Hiramatsu | c5cb5a2 | 2009-06-30 17:08:14 -0400 | [diff] [blame] | 184 | INIT_LIST_HEAD(&kip->list); |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 185 | memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c)); |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 186 | kip->slot_used[0] = SLOT_USED; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 187 | kip->nused = 1; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 188 | kip->ngarbage = 0; |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 189 | kip->cache = c; |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 190 | list_add_rcu(&kip->list, &c->pages); |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 191 | slot = kip->insns; |
Adrian Hunter | 69e4908 | 2020-05-12 15:19:11 +0300 | [diff] [blame] | 192 | |
| 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 Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 196 | out: |
| 197 | mutex_unlock(&c->mutex); |
| 198 | return slot; |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 201 | /* Return true if all garbages are collected, otherwise false. */ |
| 202 | static bool collect_one_slot(struct kprobe_insn_page *kip, int idx) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 203 | { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 204 | kip->slot_used[idx] = SLOT_CLEAN; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 205 | 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 Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 213 | if (!list_is_singular(&kip->list)) { |
Adrian Hunter | 69e4908 | 2020-05-12 15:19:11 +0300 | [diff] [blame] | 214 | /* |
| 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 Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 221 | list_del_rcu(&kip->list); |
| 222 | synchronize_rcu(); |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 223 | kip->cache->free(kip->insns); |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 224 | kfree(kip); |
| 225 | } |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 226 | return true; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 227 | } |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 228 | return false; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 231 | static int collect_garbage_slots(struct kprobe_insn_cache *c) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 232 | { |
Masami Hiramatsu | c5cb5a2 | 2009-06-30 17:08:14 -0400 | [diff] [blame] | 233 | struct kprobe_insn_page *kip, *next; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 234 | |
Masami Hiramatsu | 615d0eb | 2010-02-02 16:49:04 -0500 | [diff] [blame] | 235 | /* Ensure no-one is interrupted on the garbages */ |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 236 | synchronize_rcu(); |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 237 | |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 238 | list_for_each_entry_safe(kip, next, &c->pages, list) { |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 239 | int i; |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 240 | |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 241 | if (kip->ngarbage == 0) |
| 242 | continue; |
| 243 | kip->ngarbage = 0; /* we will collect all garbages */ |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 244 | for (i = 0; i < slots_per_page(c); i++) { |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 245 | if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i)) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | } |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 249 | c->nr_garbage = 0; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 253 | void __free_insn_slot(struct kprobe_insn_cache *c, |
| 254 | kprobe_opcode_t *slot, int dirty) |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 255 | { |
| 256 | struct kprobe_insn_page *kip; |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 257 | long idx; |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 258 | |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 259 | mutex_lock(&c->mutex); |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 260 | 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 Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 265 | goto out; |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 266 | } |
| 267 | /* Could not find this slot. */ |
| 268 | WARN_ON(1); |
| 269 | kip = NULL; |
| 270 | out: |
| 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 Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 285 | mutex_unlock(&c->mutex); |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 288 | /* |
| 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 | */ |
| 293 | bool __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 Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 311 | int 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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 321 | strscpy(sym, c->sym, KSYM_NAME_LEN); |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 322 | *type = 't'; |
| 323 | *value = (unsigned long)kip->insns; |
| 324 | ret = 0; |
| 325 | break; |
| 326 | } |
| 327 | rcu_read_unlock(); |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 332 | #ifdef CONFIG_OPTPROBES |
Christophe Leroy | 7ee3e97 | 2021-05-13 09:07:51 +0000 | [diff] [blame] | 333 | void __weak *alloc_optinsn_page(void) |
| 334 | { |
| 335 | return alloc_insn_page(); |
| 336 | } |
| 337 | |
| 338 | void __weak free_optinsn_page(void *page) |
| 339 | { |
| 340 | free_insn_page(page); |
| 341 | } |
| 342 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 343 | /* For optimized_kprobe buffer */ |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 344 | struct kprobe_insn_cache kprobe_optinsn_slots = { |
| 345 | .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex), |
Christophe Leroy | 7ee3e97 | 2021-05-13 09:07:51 +0000 | [diff] [blame] | 346 | .alloc = alloc_optinsn_page, |
| 347 | .free = free_optinsn_page, |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 348 | .sym = KPROBE_OPTINSN_PAGE_SYM, |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 349 | .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages), |
| 350 | /* .insn_size is initialized later */ |
| 351 | .nr_garbage = 0, |
| 352 | }; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 353 | #endif |
Anil S Keshavamurthy | 2d14e39 | 2006-01-09 20:52:41 -0800 | [diff] [blame] | 354 | #endif |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 355 | |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 356 | /* We have preemption disabled.. so it is safe to use __ versions */ |
| 357 | static inline void set_kprobe_instance(struct kprobe *kp) |
| 358 | { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 359 | __this_cpu_write(kprobe_instance, kp); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static inline void reset_kprobe_instance(void) |
| 363 | { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 364 | __this_cpu_write(kprobe_instance, NULL); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 367 | /* |
| 368 | * This routine is called either: |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 369 | * - under the 'kprobe_mutex' - during kprobe_[un]register(). |
| 370 | * OR |
| 371 | * - with preemption disabled - from architecture specific code. |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 372 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 373 | struct kprobe *get_kprobe(void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | struct hlist_head *head; |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 376 | struct kprobe *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; |
Masami Hiramatsu | 6743ad4 | 2020-05-12 17:02:33 +0900 | [diff] [blame] | 379 | hlist_for_each_entry_rcu(p, head, hlist, |
| 380 | lockdep_is_held(&kprobe_mutex)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (p->addr == addr) |
| 382 | return p; |
| 383 | } |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return NULL; |
| 386 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 387 | NOKPROBE_SYMBOL(get_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 389 | static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 390 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 391 | /* Return true if 'p' is an aggregator */ |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 392 | static inline bool kprobe_aggrprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 393 | { |
| 394 | return p->pre_handler == aggr_pre_handler; |
| 395 | } |
| 396 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 397 | /* Return true if 'p' is unused */ |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 398 | static inline bool kprobe_unused(struct kprobe *p) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 399 | { |
| 400 | return kprobe_aggrprobe(p) && kprobe_disabled(p) && |
| 401 | list_empty(&p->list); |
| 402 | } |
| 403 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 404 | /* Keep all fields in the kprobe consistent. */ |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 405 | static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 406 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 407 | memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t)); |
| 408 | memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn)); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | #ifdef CONFIG_OPTPROBES |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 412 | /* NOTE: This is protected by 'kprobe_mutex'. */ |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 413 | static bool kprobes_allow_optimization; |
| 414 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 415 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 416 | * Call all 'kprobe::pre_handler' on the list, but ignores its return value. |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 417 | * This must be called from arch-dep optimized caller. |
| 418 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 419 | void opt_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 420 | { |
| 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. Rao | 4f3a871 | 2017-10-17 13:48:34 +0530 | [diff] [blame] | 426 | kp->pre_handler(kp, regs); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 427 | } |
| 428 | reset_kprobe_instance(); |
| 429 | } |
| 430 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 431 | NOKPROBE_SYMBOL(opt_pre_handler); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 432 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 433 | /* Free optimized instructions and optimized_kprobe */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 434 | static void free_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 435 | { |
| 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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 444 | /* Return true if the kprobe is ready for optimization. */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 445 | static 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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 457 | /* Return true if the kprobe is disarmed. Note: p must be on hash list */ |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 458 | static inline bool kprobe_disarmed(struct kprobe *p) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 459 | { |
| 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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 471 | /* Return true if the probe is queued on (un)optimizing lists */ |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 472 | static bool kprobe_queued(struct kprobe *p) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 473 | { |
| 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 Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 479 | return true; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 480 | } |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 481 | return false; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 482 | } |
| 483 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 484 | /* |
| 485 | * Return an optimized kprobe whose optimizing code replaces |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 486 | * instructions including 'addr' (exclude breakpoint). |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 487 | */ |
Masami Hiramatsu | c42421e | 2021-09-14 23:40:07 +0900 | [diff] [blame] | 488 | static struct kprobe *get_optimized_kprobe(kprobe_opcode_t *addr) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 489 | { |
| 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 Hiramatsu | c42421e | 2021-09-14 23:40:07 +0900 | [diff] [blame] | 495 | for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH / sizeof(kprobe_opcode_t); i++) |
| 496 | p = get_kprobe(addr - i); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 497 | |
| 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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 507 | /* Optimization staging list, protected by 'kprobe_mutex' */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 508 | static LIST_HEAD(optimizing_list); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 509 | static LIST_HEAD(unoptimizing_list); |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 510 | static LIST_HEAD(freeing_list); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 511 | |
| 512 | static void kprobe_optimizer(struct work_struct *work); |
| 513 | static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer); |
| 514 | #define OPTIMIZE_DELAY 5 |
| 515 | |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 516 | /* |
| 517 | * Optimize (replace a breakpoint with a jump) kprobes listed on |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 518 | * 'optimizing_list'. |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 519 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 520 | static void do_optimize_kprobes(void) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 521 | { |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 522 | lockdep_assert_held(&text_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 523 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 524 | * 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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 532 | */ |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 533 | 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 Hiramatsu | cd7ebe2 | 2010-12-03 18:54:28 +0900 | [diff] [blame] | 540 | arch_optimize_kprobes(&optimizing_list); |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 541 | } |
| 542 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 543 | /* |
| 544 | * Unoptimize (replace a jump with a breakpoint and remove the breakpoint |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 545 | * if need) kprobes listed on 'unoptimizing_list'. |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 546 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 547 | static void do_unoptimize_kprobes(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 548 | { |
| 549 | struct optimized_kprobe *op, *tmp; |
| 550 | |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 551 | lockdep_assert_held(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 552 | /* See comment in do_optimize_kprobes() */ |
| 553 | lockdep_assert_cpus_held(); |
| 554 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 555 | /* Unoptimization must be done anytime */ |
| 556 | if (list_empty(&unoptimizing_list)) |
| 557 | return; |
| 558 | |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 559 | arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 560 | /* Loop on 'freeing_list' for disarming */ |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 561 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
Masami Hiramatsu | f66c044 | 2019-11-27 14:57:04 +0900 | [diff] [blame] | 562 | /* Switching from detour code to origin */ |
| 563 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 564 | /* 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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 571 | * (reclaiming is done by do_free_cleaned_kprobes().) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 572 | */ |
| 573 | hlist_del_rcu(&op->kp.hlist); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 574 | } else |
| 575 | list_del_init(&op->list); |
| 576 | } |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 577 | } |
| 578 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 579 | /* Reclaim all kprobes on the 'freeing_list' */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 580 | static void do_free_cleaned_kprobes(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 581 | { |
| 582 | struct optimized_kprobe *op, *tmp; |
| 583 | |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 584 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 585 | list_del_init(&op->list); |
Masami Hiramatsu | cbdd96f | 2018-09-11 19:21:09 +0900 | [diff] [blame] | 586 | 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 Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 593 | free_aggr_kprobe(&op->kp); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /* Start optimizer after OPTIMIZE_DELAY passed */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 598 | static void kick_kprobe_optimizer(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 599 | { |
Tejun Heo | ad72b3b | 2012-12-21 17:57:00 -0800 | [diff] [blame] | 600 | schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 601 | } |
| 602 | |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 603 | /* Kprobe jump optimizer */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 604 | static void kprobe_optimizer(struct work_struct *work) |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 605 | { |
Steven Rostedt | 72ef379 | 2012-06-05 19:28:14 +0900 | [diff] [blame] | 606 | mutex_lock(&kprobe_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 607 | cpus_read_lock(); |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 608 | mutex_lock(&text_mutex); |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 609 | |
| 610 | /* |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 611 | * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed) |
| 612 | * kprobes before waiting for quiesence period. |
| 613 | */ |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 614 | do_unoptimize_kprobes(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 615 | |
| 616 | /* |
Masami Hiramatsu | a30b85d | 2017-10-20 08:43:39 +0900 | [diff] [blame] | 617 | * 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 Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 624 | */ |
Masami Hiramatsu | a30b85d | 2017-10-20 08:43:39 +0900 | [diff] [blame] | 625 | synchronize_rcu_tasks(); |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 626 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 627 | /* Step 3: Optimize kprobes after quiesence period */ |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 628 | do_optimize_kprobes(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 629 | |
| 630 | /* Step 4: Free cleaned kprobes after quiesence period */ |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 631 | do_free_cleaned_kprobes(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 632 | |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 633 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 634 | cpus_read_unlock(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 635 | |
Masami Hiramatsu | cd7ebe2 | 2010-12-03 18:54:28 +0900 | [diff] [blame] | 636 | /* Step 5: Kick optimizer again if needed */ |
Masami Hiramatsu | f984ba4 | 2010-12-03 18:54:34 +0900 | [diff] [blame] | 637 | if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) |
Masami Hiramatsu | cd7ebe2 | 2010-12-03 18:54:28 +0900 | [diff] [blame] | 638 | kick_kprobe_optimizer(); |
Masami Hiramatsu | 1a0aa99 | 2020-05-12 17:02:56 +0900 | [diff] [blame] | 639 | |
| 640 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Wait for completing optimization and unoptimization */ |
Thomas Gleixner | 30e7d894 | 2017-05-17 10:19:49 +0200 | [diff] [blame] | 644 | void wait_for_kprobe_optimizer(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 645 | { |
Tejun Heo | ad72b3b | 2012-12-21 17:57:00 -0800 | [diff] [blame] | 646 | mutex_lock(&kprobe_mutex); |
| 647 | |
| 648 | while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) { |
| 649 | mutex_unlock(&kprobe_mutex); |
| 650 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 651 | /* This will also make 'optimizing_work' execute immmediately */ |
Tejun Heo | ad72b3b | 2012-12-21 17:57:00 -0800 | [diff] [blame] | 652 | flush_delayed_work(&optimizing_work); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 653 | /* 'optimizing_work' might not have been queued yet, relax */ |
Tejun Heo | ad72b3b | 2012-12-21 17:57:00 -0800 | [diff] [blame] | 654 | cpu_relax(); |
| 655 | |
| 656 | mutex_lock(&kprobe_mutex); |
| 657 | } |
| 658 | |
| 659 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 662 | static 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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 674 | /* Optimize kprobe if p is ready to be optimized */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 675 | static void optimize_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 676 | { |
| 677 | struct optimized_kprobe *op; |
| 678 | |
| 679 | /* Check if the kprobe is disabled or not ready for optimization. */ |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 680 | if (!kprobe_optready(p) || !kprobes_allow_optimization || |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 681 | (kprobe_disabled(p) || kprobes_all_disarmed)) |
| 682 | return; |
| 683 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 684 | /* kprobes with 'post_handler' can not be optimized */ |
Masami Hiramatsu | 059053a | 2018-06-20 01:10:27 +0900 | [diff] [blame] | 685 | if (p->post_handler) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 686 | 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 Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 695 | 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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 700 | return; |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 701 | } |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 702 | op->kp.flags |= KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 703 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 704 | /* |
| 705 | * On the 'unoptimizing_list' and 'optimizing_list', |
| 706 | * 'op' must have OPTIMIZED flag |
| 707 | */ |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 708 | if (WARN_ON_ONCE(!list_empty(&op->list))) |
| 709 | return; |
| 710 | |
| 711 | list_add(&op->list, &optimizing_list); |
| 712 | kick_kprobe_optimizer(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* Short cut to direct unoptimizing */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 716 | static void force_unoptimize_kprobe(struct optimized_kprobe *op) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 717 | { |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 718 | lockdep_assert_cpus_held(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 719 | arch_unoptimize_kprobe(op); |
Masami Hiramatsu | f66c044 | 2019-11-27 14:57:04 +0900 | [diff] [blame] | 720 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* Unoptimize a kprobe if p is optimized */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 724 | static void unoptimize_kprobe(struct kprobe *p, bool force) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 725 | { |
| 726 | struct optimized_kprobe *op; |
| 727 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 728 | 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 Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 732 | 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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 748 | list_del_init(&op->list); |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 749 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 750 | } |
| 751 | return; |
| 752 | } |
| 753 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 754 | /* Optimized kprobe case */ |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 755 | if (force) { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 756 | /* Forcibly update the code: this is a special case */ |
| 757 | force_unoptimize_kprobe(op); |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 758 | } else { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 759 | list_add(&op->list, &unoptimizing_list); |
| 760 | kick_kprobe_optimizer(); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 764 | /* Cancel unoptimizing for reusing */ |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 765 | static int reuse_unused_kprobe(struct kprobe *ap) |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 766 | { |
| 767 | struct optimized_kprobe *op; |
| 768 | |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 769 | /* |
| 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 Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 774 | WARN_ON_ONCE(list_empty(&op->list)); |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 775 | /* Enable the probe again */ |
| 776 | ap->flags &= ~KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 777 | /* Optimize it again. (remove from 'op->list') */ |
Masami Hiramatsu | 5f843ed | 2019-04-15 15:01:25 +0900 | [diff] [blame] | 778 | if (!kprobe_optready(ap)) |
| 779 | return -EINVAL; |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 780 | |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 781 | optimize_kprobe(ap); |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 782 | return 0; |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 783 | } |
| 784 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 785 | /* Remove optimized instructions */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 786 | static void kill_optimized_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 787 | { |
| 788 | struct optimized_kprobe *op; |
| 789 | |
| 790 | op = container_of(p, struct optimized_kprobe, kp); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 791 | if (!list_empty(&op->list)) |
| 792 | /* Dequeue from the (un)optimization queue */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 793 | list_del_init(&op->list); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 794 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 795 | |
| 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 Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 807 | /* Don't touch the code, because it is already freed. */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 808 | arch_remove_optimized_kprobe(op); |
| 809 | } |
| 810 | |
Masami Hiramatsu | a460246 | 2017-04-19 18:22:25 +0530 | [diff] [blame] | 811 | static inline |
| 812 | void __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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 818 | /* Try to prepare optimized instructions */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 819 | static void prepare_optimized_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 820 | { |
| 821 | struct optimized_kprobe *op; |
| 822 | |
| 823 | op = container_of(p, struct optimized_kprobe, kp); |
Masami Hiramatsu | a460246 | 2017-04-19 18:22:25 +0530 | [diff] [blame] | 824 | __prepare_optimized_kprobe(op, p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 825 | } |
| 826 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 827 | /* Allocate new optimized_kprobe and try to prepare optimized instructions. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 828 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 829 | { |
| 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 Hiramatsu | a460246 | 2017-04-19 18:22:25 +0530 | [diff] [blame] | 838 | __prepare_optimized_kprobe(op, p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 839 | |
| 840 | return &op->kp; |
| 841 | } |
| 842 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 843 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 844 | |
| 845 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 846 | * Prepare an optimized_kprobe and optimize it. |
| 847 | * NOTE: 'p' must be a normal registered kprobe. |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 848 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 849 | static void try_to_optimize_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 850 | { |
| 851 | struct kprobe *ap; |
| 852 | struct optimized_kprobe *op; |
| 853 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 854 | /* Impossible to optimize ftrace-based kprobe. */ |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 855 | if (kprobe_ftrace(p)) |
| 856 | return; |
| 857 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 858 | /* For preparing optimization, jump_label_text_reserved() is called. */ |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 859 | cpus_read_lock(); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 860 | jump_label_lock(); |
| 861 | mutex_lock(&text_mutex); |
| 862 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 863 | ap = alloc_aggr_kprobe(p); |
| 864 | if (!ap) |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 865 | goto out; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 866 | |
| 867 | op = container_of(ap, struct optimized_kprobe, kp); |
| 868 | if (!arch_prepared_optinsn(&op->optinsn)) { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 869 | /* If failed to setup optimizing, fallback to kprobe. */ |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 870 | arch_remove_optimized_kprobe(op); |
| 871 | kfree(op); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 872 | goto out; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | init_aggr_kprobe(ap, p); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 876 | optimize_kprobe(ap); /* This just kicks optimizer thread. */ |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 877 | |
| 878 | out: |
| 879 | mutex_unlock(&text_mutex); |
| 880 | jump_label_unlock(); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 881 | cpus_read_unlock(); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 882 | } |
| 883 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 884 | static void optimize_all_kprobes(void) |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 885 | { |
| 886 | struct hlist_head *head; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 887 | struct kprobe *p; |
| 888 | unsigned int i; |
| 889 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 890 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 891 | /* If optimization is already allowed, just return. */ |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 892 | if (kprobes_allow_optimization) |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 893 | goto out; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 894 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 895 | cpus_read_lock(); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 896 | kprobes_allow_optimization = true; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 897 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 898 | head = &kprobe_table[i]; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 899 | hlist_for_each_entry(p, head, hlist) |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 900 | if (!kprobe_disabled(p)) |
| 901 | optimize_kprobe(p); |
| 902 | } |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 903 | cpus_read_unlock(); |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 904 | pr_info("kprobe jump-optimization is enabled. All kprobes are optimized if possible.\n"); |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 905 | out: |
| 906 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 907 | } |
| 908 | |
Masami Hiramatsu | c85c9a2 | 2021-02-18 23:29:23 +0900 | [diff] [blame] | 909 | #ifdef CONFIG_SYSCTL |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 910 | static void unoptimize_all_kprobes(void) |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 911 | { |
| 912 | struct hlist_head *head; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 913 | struct kprobe *p; |
| 914 | unsigned int i; |
| 915 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 916 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 917 | /* If optimization is already prohibited, just return. */ |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 918 | if (!kprobes_allow_optimization) { |
| 919 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 920 | return; |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 921 | } |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 922 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 923 | cpus_read_lock(); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 924 | kprobes_allow_optimization = false; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 925 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 926 | head = &kprobe_table[i]; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 927 | hlist_for_each_entry(p, head, hlist) { |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 928 | if (!kprobe_disabled(p)) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 929 | unoptimize_kprobe(p, false); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 930 | } |
| 931 | } |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 932 | cpus_read_unlock(); |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 933 | mutex_unlock(&kprobe_mutex); |
| 934 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 935 | /* Wait for unoptimizing completion. */ |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 936 | wait_for_kprobe_optimizer(); |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 937 | pr_info("kprobe jump-optimization is disabled. All kprobes are based on software breakpoint.\n"); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 938 | } |
| 939 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 940 | static DEFINE_MUTEX(kprobe_sysctl_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 941 | int sysctl_kprobes_optimization; |
| 942 | int proc_kprobes_optimization_handler(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 943 | void *buffer, size_t *length, |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 944 | loff_t *ppos) |
| 945 | { |
| 946 | int ret; |
| 947 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 948 | mutex_lock(&kprobe_sysctl_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 949 | 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 Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 956 | mutex_unlock(&kprobe_sysctl_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 957 | |
| 958 | return ret; |
| 959 | } |
| 960 | #endif /* CONFIG_SYSCTL */ |
| 961 | |
Masami Hiramatsu | 57d4e31 | 2021-09-14 23:39:55 +0900 | [diff] [blame] | 962 | /* Put a breakpoint for a probe. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 963 | static void __arm_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 964 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 965 | struct kprobe *_p; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 966 | |
Masami Hiramatsu | 57d4e31 | 2021-09-14 23:39:55 +0900 | [diff] [blame] | 967 | lockdep_assert_held(&text_mutex); |
| 968 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 969 | /* Find the overlapping optimized kprobes. */ |
Masami Hiramatsu | c42421e | 2021-09-14 23:40:07 +0900 | [diff] [blame] | 970 | _p = get_optimized_kprobe(p->addr); |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 971 | if (unlikely(_p)) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 972 | /* Fallback to unoptimized kprobe */ |
| 973 | unoptimize_kprobe(_p, true); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 974 | |
| 975 | arch_arm_kprobe(p); |
| 976 | optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */ |
| 977 | } |
| 978 | |
Masami Hiramatsu | 57d4e31 | 2021-09-14 23:39:55 +0900 | [diff] [blame] | 979 | /* Remove the breakpoint of a probe. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 980 | static void __disarm_kprobe(struct kprobe *p, bool reopt) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 981 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 982 | struct kprobe *_p; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 983 | |
Masami Hiramatsu | 57d4e31 | 2021-09-14 23:39:55 +0900 | [diff] [blame] | 984 | lockdep_assert_held(&text_mutex); |
| 985 | |
Wang Nan | 69d54b9 | 2015-02-13 14:40:26 -0800 | [diff] [blame] | 986 | /* Try to unoptimize */ |
| 987 | unoptimize_kprobe(p, kprobes_all_disarmed); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 988 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 989 | if (!kprobe_queued(p)) { |
| 990 | arch_disarm_kprobe(p); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 991 | /* If another kprobe was blocked, re-optimize it. */ |
Masami Hiramatsu | c42421e | 2021-09-14 23:40:07 +0900 | [diff] [blame] | 992 | _p = get_optimized_kprobe(p->addr); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 993 | if (unlikely(_p) && reopt) |
| 994 | optimize_kprobe(_p); |
| 995 | } |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 996 | /* |
| 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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | #else /* !CONFIG_OPTPROBES */ |
| 1005 | |
| 1006 | #define optimize_kprobe(p) do {} while (0) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1007 | #define unoptimize_kprobe(p, f) do {} while (0) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1008 | #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 Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1012 | #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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1015 | |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 1016 | static int reuse_unused_kprobe(struct kprobe *ap) |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 1017 | { |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 1018 | /* |
| 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 Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 1024 | WARN_ON_ONCE(1); |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 1025 | return -EINVAL; |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 1026 | } |
| 1027 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1028 | static void free_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1029 | { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1030 | arch_remove_kprobe(p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1031 | kfree(p); |
| 1032 | } |
| 1033 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1034 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1035 | { |
| 1036 | return kzalloc(sizeof(struct kprobe), GFP_KERNEL); |
| 1037 | } |
| 1038 | #endif /* CONFIG_OPTPROBES */ |
| 1039 | |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1040 | #ifdef CONFIG_KPROBES_ON_FTRACE |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1041 | static struct ftrace_ops kprobe_ftrace_ops __read_mostly = { |
Masami Hiramatsu | e525389 | 2012-06-05 19:28:38 +0900 | [diff] [blame] | 1042 | .func = kprobe_ftrace_handler, |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1043 | .flags = FTRACE_OPS_FL_SAVE_REGS, |
| 1044 | }; |
| 1045 | |
| 1046 | static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = { |
| 1047 | .func = kprobe_ftrace_handler, |
Masami Hiramatsu | 1d70be3 | 2014-11-21 05:25:23 -0500 | [diff] [blame] | 1048 | .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY, |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1049 | }; |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1050 | |
| 1051 | static int kprobe_ipmodify_enabled; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1052 | static int kprobe_ftrace_enabled; |
| 1053 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1054 | static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
| 1055 | int *cnt) |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1056 | { |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1057 | int ret = 0; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1058 | |
Masami Hiramatsu | 57d4e31 | 2021-09-14 23:39:55 +0900 | [diff] [blame] | 1059 | lockdep_assert_held(&kprobe_mutex); |
| 1060 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1061 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0); |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 1062 | if (WARN_ONCE(ret < 0, "Failed to arm kprobe-ftrace at %pS (error %d)\n", p->addr, ret)) |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1063 | return ret; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1064 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1065 | if (*cnt == 0) { |
| 1066 | ret = register_ftrace_function(ops); |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 1067 | if (WARN(ret < 0, "Failed to register kprobe-ftrace (error %d)\n", ret)) |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1068 | goto err_ftrace; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1071 | (*cnt)++; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1072 | return ret; |
| 1073 | |
| 1074 | err_ftrace: |
| 1075 | /* |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1076 | * At this point, sinec ops is not registered, we should be sefe from |
| 1077 | * registering empty filter. |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1078 | */ |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1079 | ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1080 | return ret; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1081 | } |
| 1082 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1083 | static 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 Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1092 | static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
| 1093 | int *cnt) |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1094 | { |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1095 | int ret = 0; |
| 1096 | |
Masami Hiramatsu | 57d4e31 | 2021-09-14 23:39:55 +0900 | [diff] [blame] | 1097 | lockdep_assert_held(&kprobe_mutex); |
| 1098 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1099 | if (*cnt == 1) { |
| 1100 | ret = unregister_ftrace_function(ops); |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 1101 | if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (error %d)\n", ret)) |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1102 | return ret; |
| 1103 | } |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1104 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1105 | (*cnt)--; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1106 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1107 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 1108 | WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (error %d)\n", |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 1109 | p->addr, ret); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1110 | return ret; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1111 | } |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1112 | |
| 1113 | static 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 Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1121 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
Muchun Song | 10de795 | 2020-08-06 01:20:46 +0800 | [diff] [blame] | 1122 | static inline int arm_kprobe_ftrace(struct kprobe *p) |
| 1123 | { |
| 1124 | return -ENODEV; |
| 1125 | } |
| 1126 | |
| 1127 | static inline int disarm_kprobe_ftrace(struct kprobe *p) |
| 1128 | { |
| 1129 | return -ENODEV; |
| 1130 | } |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1131 | #endif |
| 1132 | |
Punit Agrawal | 02afb8d | 2021-09-14 23:38:57 +0900 | [diff] [blame] | 1133 | static 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 Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1142 | static int arm_kprobe(struct kprobe *kp) |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1143 | { |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1144 | if (unlikely(kprobe_ftrace(kp))) |
| 1145 | return arm_kprobe_ftrace(kp); |
| 1146 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1147 | cpus_read_lock(); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1148 | mutex_lock(&text_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1149 | __arm_kprobe(kp); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1150 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1151 | cpus_read_unlock(); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1152 | |
| 1153 | return 0; |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1154 | } |
| 1155 | |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1156 | static int disarm_kprobe(struct kprobe *kp, bool reopt) |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1157 | { |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1158 | if (unlikely(kprobe_ftrace(kp))) |
| 1159 | return disarm_kprobe_ftrace(kp); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1160 | |
| 1161 | cpus_read_lock(); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1162 | mutex_lock(&text_mutex); |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1163 | __disarm_kprobe(kp, reopt); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1164 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1165 | cpus_read_unlock(); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1166 | |
| 1167 | return 0; |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1168 | } |
| 1169 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1170 | /* |
| 1171 | * Aggregate handlers for multiple kprobes support - these handlers |
| 1172 | * take care of invoking the individual kprobe handlers on p->list |
| 1173 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1174 | static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1175 | { |
| 1176 | struct kprobe *kp; |
| 1177 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1178 | list_for_each_entry_rcu(kp, &p->list, list) { |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1179 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1180 | set_kprobe_instance(kp); |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1181 | if (kp->pre_handler(kp, regs)) |
| 1182 | return 1; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1183 | } |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1184 | reset_kprobe_instance(); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1185 | } |
| 1186 | return 0; |
| 1187 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1188 | NOKPROBE_SYMBOL(aggr_pre_handler); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1189 | |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1190 | static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, |
| 1191 | unsigned long flags) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1192 | { |
| 1193 | struct kprobe *kp; |
| 1194 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1195 | list_for_each_entry_rcu(kp, &p->list, list) { |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1196 | if (kp->post_handler && likely(!kprobe_disabled(kp))) { |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1197 | set_kprobe_instance(kp); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1198 | kp->post_handler(kp, regs, flags); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1199 | reset_kprobe_instance(); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1200 | } |
| 1201 | } |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1202 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1203 | NOKPROBE_SYMBOL(aggr_post_handler); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1204 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1205 | /* Walks the list and increments 'nmissed' if 'p' has child probes. */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1206 | void kprobes_inc_nmissed_count(struct kprobe *p) |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1207 | { |
| 1208 | struct kprobe *kp; |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1209 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1210 | if (!kprobe_aggrprobe(p)) { |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1211 | p->nmissed++; |
| 1212 | } else { |
| 1213 | list_for_each_entry_rcu(kp, &p->list, list) |
| 1214 | kp->nmissed++; |
| 1215 | } |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1216 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1217 | NOKPROBE_SYMBOL(kprobes_inc_nmissed_count); |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1218 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1219 | static 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 | } |
| 1227 | NOKPROBE_SYMBOL(free_rp_inst_rcu); |
| 1228 | |
Masami Hiramatsu | b338817 | 2020-08-29 22:02:47 +0900 | [diff] [blame] | 1229 | static void recycle_rp_inst(struct kretprobe_instance *ri) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1230 | { |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1231 | struct kretprobe *rp = get_kretprobe(ri); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1232 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1233 | if (likely(rp)) |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1234 | freelist_add(&ri->freelist, &rp->freelist); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1235 | else |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1236 | call_rcu(&ri->rcu, free_rp_inst_rcu); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1237 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1238 | NOKPROBE_SYMBOL(recycle_rp_inst); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1239 | |
Masami Hiramatsu | 319f0ce | 2020-08-29 22:03:02 +0900 | [diff] [blame] | 1240 | static struct kprobe kprobe_busy = { |
Jiri Olsa | 9b38cc7 | 2020-05-12 17:03:18 +0900 | [diff] [blame] | 1241 | .addr = (void *) get_kprobe, |
| 1242 | }; |
| 1243 | |
| 1244 | void 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 | |
| 1254 | void kprobe_busy_end(void) |
| 1255 | { |
| 1256 | __this_cpu_write(current_kprobe, NULL); |
| 1257 | preempt_enable(); |
| 1258 | } |
| 1259 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1260 | /* |
Thomas Gleixner | 670721c | 2021-09-28 14:24:28 +0200 | [diff] [blame] | 1261 | * This function is called from delayed_put_task_struct() when a task is |
Linus Torvalds | 79ef0c0 | 2021-11-01 20:05:19 -0700 | [diff] [blame] | 1262 | * 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 Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1265 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1266 | void kprobe_flush_task(struct task_struct *tk) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1267 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 1268 | struct kretprobe_instance *ri; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1269 | struct llist_node *node; |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 1270 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1271 | /* Early boot, not yet initialized. */ |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1272 | if (unlikely(!kprobes_initialized)) |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1273 | return; |
| 1274 | |
Jiri Olsa | 9b38cc7 | 2020-05-12 17:03:18 +0900 | [diff] [blame] | 1275 | kprobe_busy_begin(); |
| 1276 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1277 | 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,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 1283 | } |
Jiri Olsa | 9b38cc7 | 2020-05-12 17:03:18 +0900 | [diff] [blame] | 1284 | |
| 1285 | kprobe_busy_end(); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1286 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1287 | NOKPROBE_SYMBOL(kprobe_flush_task); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1288 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1289 | static inline void free_rp_inst(struct kretprobe *rp) |
| 1290 | { |
| 1291 | struct kretprobe_instance *ri; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1292 | struct freelist_node *node; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1293 | int count = 0; |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 1294 | |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1295 | node = rp->freelist.head; |
| 1296 | while (node) { |
| 1297 | ri = container_of(node, struct kretprobe_instance, freelist); |
| 1298 | node = node->next; |
| 1299 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1300 | kfree(ri); |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1301 | count++; |
| 1302 | } |
| 1303 | |
| 1304 | if (refcount_sub_and_test(count, &rp->rph->ref)) { |
| 1305 | kfree(rp->rph); |
| 1306 | rp->rph = NULL; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1310 | /* Add the new probe to 'ap->list'. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1311 | static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1312 | { |
Masami Hiramatsu | 059053a | 2018-06-20 01:10:27 +0900 | [diff] [blame] | 1313 | if (p->post_handler) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1314 | unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1315 | |
Masami Hiramatsu | 059053a | 2018-06-20 01:10:27 +0900 | [diff] [blame] | 1316 | list_add_rcu(&p->list, &ap->list); |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1317 | if (p->post_handler && !ap->post_handler) |
| 1318 | ap->post_handler = aggr_post_handler; |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1319 | |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1324 | * Fill in the required fields of the aggregator kprobe. Replace the |
| 1325 | * earlier kprobe in the hlist with the aggregator kprobe. |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1326 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1327 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1328 | { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1329 | /* Copy the insn slot of 'p' to 'ap'. */ |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1330 | copy_kprobe(p, ap); |
bibo, mao | a9ad965 | 2006-07-30 03:03:26 -0700 | [diff] [blame] | 1331 | flush_insn_slot(ap); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1332 | ap->addr = p->addr; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1333 | ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1334 | ap->pre_handler = aggr_pre_handler; |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1335 | /* We don't care the kprobe which has gone. */ |
| 1336 | if (p->post_handler && !kprobe_gone(p)) |
mao, bibo | 3672165 | 2006-06-26 00:25:22 -0700 | [diff] [blame] | 1337 | ap->post_handler = aggr_post_handler; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1338 | |
| 1339 | INIT_LIST_HEAD(&ap->list); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1340 | INIT_HLIST_NODE(&ap->hlist); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1341 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1342 | list_add_rcu(&p->list, &ap->list); |
Keshavamurthy Anil S | adad0f3 | 2005-12-12 00:37:12 -0800 | [diff] [blame] | 1343 | hlist_replace_rcu(&p->hlist, &ap->hlist); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1347 | * This registers the second or subsequent kprobe at the same address. |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1348 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1349 | static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1350 | { |
| 1351 | int ret = 0; |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1352 | struct kprobe *ap = orig_p; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1353 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1354 | cpus_read_lock(); |
| 1355 | |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1356 | /* For preparing optimization, jump_label_text_reserved() is called */ |
| 1357 | jump_label_lock(); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1358 | mutex_lock(&text_mutex); |
| 1359 | |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1360 | if (!kprobe_aggrprobe(orig_p)) { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1361 | /* If 'orig_p' is not an 'aggr_kprobe', create new one. */ |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1362 | ap = alloc_aggr_kprobe(orig_p); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1363 | if (!ap) { |
| 1364 | ret = -ENOMEM; |
| 1365 | goto out; |
| 1366 | } |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1367 | init_aggr_kprobe(ap, orig_p); |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 1368 | } else if (kprobe_unused(ap)) { |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 1369 | /* This probe is going to die. Rescue it */ |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 1370 | ret = reuse_unused_kprobe(ap); |
| 1371 | if (ret) |
| 1372 | goto out; |
| 1373 | } |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1374 | |
| 1375 | if (kprobe_gone(ap)) { |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1376 | /* |
| 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 Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1382 | ret = arch_prepare_kprobe(ap); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1383 | if (ret) |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1384 | /* |
| 1385 | * Even if fail to allocate new slot, don't need to |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1386 | * free the 'ap'. It will be used next time, or |
| 1387 | * freed by unregister_kprobe(). |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1388 | */ |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1389 | goto out; |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1390 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1391 | /* Prepare optimized instructions if possible. */ |
| 1392 | prepare_optimized_kprobe(ap); |
| 1393 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1394 | /* |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1395 | * Clear gone flag to prevent allocating new slot again, and |
| 1396 | * set disabled flag because it is not armed yet. |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1397 | */ |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1398 | ap->flags = (ap->flags & ~KPROBE_FLAG_GONE) |
| 1399 | | KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1400 | } |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1401 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1402 | /* Copy the insn slot of 'p' to 'ap'. */ |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1403 | copy_kprobe(ap, p); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1404 | ret = add_new_kprobe(ap, p); |
| 1405 | |
| 1406 | out: |
| 1407 | mutex_unlock(&text_mutex); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1408 | jump_label_unlock(); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1409 | cpus_read_unlock(); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1410 | |
| 1411 | if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) { |
| 1412 | ap->flags &= ~KPROBE_FLAG_DISABLED; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1413 | if (!kprobes_all_disarmed) { |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1414 | /* Arm the breakpoint again. */ |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1415 | ret = arm_kprobe(ap); |
| 1416 | if (ret) { |
| 1417 | ap->flags |= KPROBE_FLAG_DISABLED; |
| 1418 | list_del_rcu(&p->list); |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 1419 | synchronize_rcu(); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1420 | } |
| 1421 | } |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1422 | } |
| 1423 | return ret; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Masami Hiramatsu | be8f274 | 2014-04-17 17:16:58 +0900 | [diff] [blame] | 1426 | bool __weak arch_within_kprobe_blacklist(unsigned long addr) |
| 1427 | { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1428 | /* The '__kprobes' functions and entry code must not be probed. */ |
Masami Hiramatsu | be8f274 | 2014-04-17 17:16:58 +0900 | [diff] [blame] | 1429 | return addr >= (unsigned long)__kprobes_text_start && |
| 1430 | addr < (unsigned long)__kprobes_text_end; |
| 1431 | } |
| 1432 | |
Masami Hiramatsu | 6143c6f | 2019-02-13 01:13:12 +0900 | [diff] [blame] | 1433 | static bool __within_kprobe_blacklist(unsigned long addr) |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 1434 | { |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1435 | struct kprobe_blacklist_entry *ent; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1436 | |
Masami Hiramatsu | be8f274 | 2014-04-17 17:16:58 +0900 | [diff] [blame] | 1437 | if (arch_within_kprobe_blacklist(addr)) |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1438 | return true; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1439 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1440 | * If 'kprobe_blacklist' is defined, check the address and |
| 1441 | * reject any probe registration in the prohibited area. |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1442 | */ |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1443 | list_for_each_entry(ent, &kprobe_blacklist, list) { |
| 1444 | if (addr >= ent->start_addr && addr < ent->end_addr) |
| 1445 | return true; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1446 | } |
Masami Hiramatsu | 6143c6f | 2019-02-13 01:13:12 +0900 | [diff] [blame] | 1447 | return false; |
| 1448 | } |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1449 | |
Masami Hiramatsu | 6143c6f | 2019-02-13 01:13:12 +0900 | [diff] [blame] | 1450 | bool 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 Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1467 | return false; |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1470 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1471 | * If 'symbol_name' is specified, look it up and add the 'offset' |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1472 | * to it. This way, we can specify a relative address to a symbol. |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1473 | * This returns encoded errors if it fails to look up symbol or invalid |
| 1474 | * combination of parameters. |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1475 | */ |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1476 | static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, |
| 1477 | const char *symbol_name, unsigned int offset) |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1478 | { |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1479 | if ((symbol_name && addr) || (!symbol_name && !addr)) |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1480 | goto invalid; |
| 1481 | |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1482 | if (symbol_name) { |
Linus Torvalds | 7246f60 | 2017-05-05 11:36:44 -0700 | [diff] [blame] | 1483 | addr = kprobe_lookup_name(symbol_name, offset); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1484 | if (!addr) |
| 1485 | return ERR_PTR(-ENOENT); |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1488 | addr = (kprobe_opcode_t *)(((char *)addr) + offset); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1489 | if (addr) |
| 1490 | return addr; |
| 1491 | |
| 1492 | invalid: |
| 1493 | return ERR_PTR(-EINVAL); |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1496 | static kprobe_opcode_t *kprobe_addr(struct kprobe *p) |
| 1497 | { |
| 1498 | return _kprobe_addr(p->addr, p->symbol_name, p->offset); |
| 1499 | } |
| 1500 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1501 | /* |
| 1502 | * Check the 'p' is valid and return the aggregator kprobe |
| 1503 | * at the same address. |
| 1504 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1505 | static struct kprobe *__get_valid_kprobe(struct kprobe *p) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1506 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1507 | struct kprobe *ap, *list_p; |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1508 | |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1509 | lockdep_assert_held(&kprobe_mutex); |
| 1510 | |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1511 | ap = get_kprobe(p->addr); |
| 1512 | if (unlikely(!ap)) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1513 | return NULL; |
| 1514 | |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1515 | if (p != ap) { |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1516 | list_for_each_entry(list_p, &ap->list, list) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1517 | if (list_p == p) |
| 1518 | /* kprobe p is a valid probe */ |
| 1519 | goto valid; |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | valid: |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1523 | return ap; |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1524 | } |
| 1525 | |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 1526 | /* |
| 1527 | * Warn and return error if the kprobe is being re-registered since |
| 1528 | * there must be a software bug. |
| 1529 | */ |
| 1530 | static inline int warn_kprobe_rereg(struct kprobe *p) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1531 | { |
| 1532 | int ret = 0; |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1533 | |
| 1534 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 1535 | if (WARN_ON_ONCE(__get_valid_kprobe(p))) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1536 | ret = -EINVAL; |
| 1537 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1538 | |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1539 | return ret; |
| 1540 | } |
| 1541 | |
Punit Agrawal | 4402dea | 2021-09-14 23:39:16 +0900 | [diff] [blame] | 1542 | static int check_ftrace_location(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | { |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1544 | unsigned long ftrace_addr; |
| 1545 | |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1546 | ftrace_addr = ftrace_location((unsigned long)p->addr); |
| 1547 | if (ftrace_addr) { |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1548 | #ifdef CONFIG_KPROBES_ON_FTRACE |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1549 | /* Given address is not on the instruction boundary */ |
| 1550 | if ((unsigned long)p->addr != ftrace_addr) |
| 1551 | return -EILSEQ; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1552 | p->flags |= KPROBE_FLAG_FTRACE; |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1553 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1554 | return -EINVAL; |
| 1555 | #endif |
| 1556 | } |
Heiko Carstens | f7f242f | 2014-10-15 12:17:34 +0200 | [diff] [blame] | 1557 | return 0; |
| 1558 | } |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1559 | |
Heiko Carstens | f7f242f | 2014-10-15 12:17:34 +0200 | [diff] [blame] | 1560 | static int check_kprobe_address_safe(struct kprobe *p, |
| 1561 | struct module **probed_mod) |
| 1562 | { |
| 1563 | int ret; |
| 1564 | |
Punit Agrawal | 4402dea | 2021-09-14 23:39:16 +0900 | [diff] [blame] | 1565 | ret = check_ftrace_location(p); |
Heiko Carstens | f7f242f | 2014-10-15 12:17:34 +0200 | [diff] [blame] | 1566 | if (ret) |
| 1567 | return ret; |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1568 | 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 Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1573 | within_kprobe_blacklist((unsigned long) p->addr) || |
Masami Hiramatsu | e336b40 | 2019-09-03 20:08:21 +0900 | [diff] [blame] | 1574 | jump_label_text_reserved(p->addr, p->addr) || |
Peter Zijlstra | fa68bd0 | 2021-06-28 13:24:12 +0200 | [diff] [blame] | 1575 | static_call_text_reserved(p->addr, p->addr) || |
Masami Hiramatsu | e336b40 | 2019-09-03 20:08:21 +0900 | [diff] [blame] | 1576 | find_bug((unsigned long)p->addr)) { |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1577 | ret = -EINVAL; |
| 1578 | goto out; |
| 1579 | } |
| 1580 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1581 | /* Check if 'p' is probing a module. */ |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1582 | *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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1594 | * If the module freed '.init.text', we couldn't insert |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1595 | * 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 | } |
| 1604 | out: |
| 1605 | preempt_enable(); |
| 1606 | jump_label_unlock(); |
| 1607 | |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1611 | int register_kprobe(struct kprobe *p) |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1612 | { |
| 1613 | int ret; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1614 | struct kprobe *old_p; |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 1615 | struct module *probed_mod; |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1616 | kprobe_opcode_t *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1618 | /* Adjust probe address from symbol */ |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1619 | addr = kprobe_addr(p); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1620 | if (IS_ERR(addr)) |
| 1621 | return PTR_ERR(addr); |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1622 | p->addr = addr; |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 1623 | |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 1624 | ret = warn_kprobe_rereg(p); |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1625 | if (ret) |
| 1626 | return ret; |
| 1627 | |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1628 | /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ |
| 1629 | p->flags &= KPROBE_FLAG_DISABLED; |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1630 | p->nmissed = 0; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1631 | INIT_LIST_HEAD(&p->list); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1632 | |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1633 | ret = check_kprobe_address_safe(p, &probed_mod); |
| 1634 | if (ret) |
| 1635 | return ret; |
| 1636 | |
| 1637 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1638 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1639 | old_p = get_kprobe(p->addr); |
| 1640 | if (old_p) { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1641 | /* Since this may unoptimize 'old_p', locking 'text_mutex'. */ |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1642 | ret = register_aggr_kprobe(old_p, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | goto out; |
| 1644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1646 | cpus_read_lock(); |
| 1647 | /* Prevent text modification */ |
| 1648 | mutex_lock(&text_mutex); |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1649 | ret = prepare_kprobe(p); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1650 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1651 | cpus_read_unlock(); |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 1652 | if (ret) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1653 | goto out; |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1654 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1655 | INIT_HLIST_NODE(&p->hlist); |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1656 | hlist_add_head_rcu(&p->hlist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
| 1658 | |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1659 | if (!kprobes_all_disarmed && !kprobe_disabled(p)) { |
| 1660 | ret = arm_kprobe(p); |
| 1661 | if (ret) { |
| 1662 | hlist_del_rcu(&p->hlist); |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 1663 | synchronize_rcu(); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1664 | goto out; |
| 1665 | } |
| 1666 | } |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 1667 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1668 | /* Try to optimize kprobe */ |
| 1669 | try_to_optimize_kprobe(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | out: |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 1671 | mutex_unlock(&kprobe_mutex); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1672 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1673 | if (probed_mod) |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 1674 | module_put(probed_mod); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | return ret; |
| 1677 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1678 | EXPORT_SYMBOL_GPL(register_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1680 | /* Check if all probes on the 'ap' are disabled. */ |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 1681 | static bool aggr_kprobe_disabled(struct kprobe *ap) |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1682 | { |
| 1683 | struct kprobe *kp; |
| 1684 | |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1685 | lockdep_assert_held(&kprobe_mutex); |
| 1686 | |
| 1687 | list_for_each_entry(kp, &ap->list, list) |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1688 | if (!kprobe_disabled(kp)) |
| 1689 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1690 | * Since there is an active probe on the list, |
| 1691 | * we can't disable this 'ap'. |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1692 | */ |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 1693 | return false; |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1694 | |
Masami Hiramatsu | 29e8077a | 2021-09-14 23:40:16 +0900 | [diff] [blame] | 1695 | return true; |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1696 | } |
| 1697 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1698 | static struct kprobe *__disable_kprobe(struct kprobe *p) |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1699 | { |
| 1700 | struct kprobe *orig_p; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1701 | int ret; |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1702 | |
Masami Hiramatsu | 57d4e31 | 2021-09-14 23:39:55 +0900 | [diff] [blame] | 1703 | lockdep_assert_held(&kprobe_mutex); |
| 1704 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1705 | /* Get an original kprobe for return */ |
| 1706 | orig_p = __get_valid_kprobe(p); |
| 1707 | if (unlikely(orig_p == NULL)) |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1708 | return ERR_PTR(-EINVAL); |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1709 | |
| 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 Nan | 69d54b9 | 2015-02-13 14:40:26 -0800 | [diff] [blame] | 1717 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 1718 | * If 'kprobes_all_disarmed' is set, 'orig_p' |
Wang Nan | 69d54b9 | 2015-02-13 14:40:26 -0800 | [diff] [blame] | 1719 | * should have already been disarmed, so |
| 1720 | * skip unneed disarming process. |
| 1721 | */ |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1722 | 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 Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1729 | orig_p->flags |= KPROBE_FLAG_DISABLED; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | return orig_p; |
| 1734 | } |
| 1735 | |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1736 | /* |
| 1737 | * Unregister a kprobe without a scheduler synchronization. |
| 1738 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1739 | static int __unregister_kprobe_top(struct kprobe *p) |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 1740 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1741 | struct kprobe *ap, *list_p; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1742 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1743 | /* Disable kprobe. This will disarm it if needed. */ |
| 1744 | ap = __disable_kprobe(p); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1745 | if (IS_ERR(ap)) |
| 1746 | return PTR_ERR(ap); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1747 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1748 | if (ap == p) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1749 | /* |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1750 | * This probe is an independent(and non-optimized) kprobe |
| 1751 | * (not an aggrprobe). Remove from the hash list. |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1752 | */ |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1753 | goto disarmed; |
| 1754 | |
| 1755 | /* Following process expects this probe is an aggrprobe */ |
| 1756 | WARN_ON(!kprobe_aggrprobe(ap)); |
| 1757 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1758 | 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 Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1763 | goto disarmed; |
| 1764 | else { |
| 1765 | /* If disabling probe has special handlers, update aggrprobe */ |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1766 | if (p->post_handler && !kprobe_gone(p)) { |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1767 | list_for_each_entry(list_p, &ap->list, list) { |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1768 | if ((list_p != p) && (list_p->post_handler)) |
| 1769 | goto noclean; |
| 1770 | } |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1771 | ap->post_handler = NULL; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1772 | } |
| 1773 | noclean: |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1774 | /* |
| 1775 | * Remove from the aggrprobe: this path will do nothing in |
| 1776 | * __unregister_kprobe_bottom(). |
| 1777 | */ |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1778 | list_del_rcu(&p->list); |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1779 | 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 Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1785 | } |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1786 | return 0; |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1787 | |
| 1788 | disarmed: |
| 1789 | hlist_del_rcu(&ap->hlist); |
| 1790 | return 0; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1791 | } |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 1792 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1793 | static void __unregister_kprobe_bottom(struct kprobe *p) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1794 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1795 | struct kprobe *ap; |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 1796 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1797 | if (list_empty(&p->list)) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1798 | /* This is an independent kprobe */ |
Ananth N Mavinakayanahalli | 0498b63 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 1799 | arch_remove_kprobe(p); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1800 | else if (list_is_singular(&p->list)) { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1801 | /* This is the last child of an aggrprobe */ |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1802 | ap = list_entry(p->list.next, struct kprobe, list); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1803 | list_del(&p->list); |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1804 | free_aggr_kprobe(ap); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1805 | } |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1806 | /* Otherwise, do nothing. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1809 | int register_kprobes(struct kprobe **kps, int num) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1810 | { |
| 1811 | int i, ret = 0; |
| 1812 | |
| 1813 | if (num <= 0) |
| 1814 | return -EINVAL; |
| 1815 | for (i = 0; i < num; i++) { |
Masami Hiramatsu | 49ad2fd | 2009-01-06 14:41:53 -0800 | [diff] [blame] | 1816 | ret = register_kprobe(kps[i]); |
Masami Hiramatsu | 67dddaa | 2008-06-12 15:21:35 -0700 | [diff] [blame] | 1817 | if (ret < 0) { |
| 1818 | if (i > 0) |
| 1819 | unregister_kprobes(kps, i); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1820 | break; |
| 1821 | } |
| 1822 | } |
| 1823 | return ret; |
| 1824 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1825 | EXPORT_SYMBOL_GPL(register_kprobes); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1826 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1827 | void unregister_kprobe(struct kprobe *p) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1828 | { |
| 1829 | unregister_kprobes(&p, 1); |
| 1830 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1831 | EXPORT_SYMBOL_GPL(unregister_kprobe); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1832 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1833 | void unregister_kprobes(struct kprobe **kps, int num) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1834 | { |
| 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. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 1845 | synchronize_rcu(); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1846 | for (i = 0; i < num; i++) |
| 1847 | if (kps[i]->addr) |
| 1848 | __unregister_kprobe_bottom(kps[i]); |
| 1849 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1850 | EXPORT_SYMBOL_GPL(unregister_kprobes); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1851 | |
Naveen N. Rao | 5f6bee3 | 2017-03-08 22:34:15 +0530 | [diff] [blame] | 1852 | int __weak kprobe_exceptions_notify(struct notifier_block *self, |
| 1853 | unsigned long val, void *data) |
Naveen N. Rao | fc62d02 | 2017-02-08 01:24:14 +0530 | [diff] [blame] | 1854 | { |
| 1855 | return NOTIFY_DONE; |
| 1856 | } |
Naveen N. Rao | 5f6bee3 | 2017-03-08 22:34:15 +0530 | [diff] [blame] | 1857 | NOKPROBE_SYMBOL(kprobe_exceptions_notify); |
Naveen N. Rao | fc62d02 | 2017-02-08 01:24:14 +0530 | [diff] [blame] | 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | static struct notifier_block kprobe_exceptions_nb = { |
| 1860 | .notifier_call = kprobe_exceptions_notify, |
Anil S Keshavamurthy | 3d5631e | 2006-06-26 00:25:28 -0700 | [diff] [blame] | 1861 | .priority = 0x7fffffff /* we need to be notified first */ |
| 1862 | }; |
| 1863 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 1864 | #ifdef CONFIG_KRETPROBES |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1865 | |
Masami Hiramatsu | 03bac0d | 2021-09-14 23:41:04 +0900 | [diff] [blame] | 1866 | /* This assumes the 'tsk' is the current task or the is not running. */ |
| 1867 | static 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 | } |
| 1888 | NOKPROBE_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 | */ |
| 1905 | unsigned 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 | } |
| 1923 | NOKPROBE_SYMBOL(kretprobe_find_ret_addr); |
| 1924 | |
Masami Hiramatsu | bf094cf | 2021-09-14 23:42:51 +0900 | [diff] [blame] | 1925 | void __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 Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1934 | unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1935 | void *frame_pointer) |
| 1936 | { |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1937 | kprobe_opcode_t *correct_ret_addr = NULL; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1938 | struct kretprobe_instance *ri = NULL; |
Masami Hiramatsu | 03bac0d | 2021-09-14 23:41:04 +0900 | [diff] [blame] | 1939 | struct llist_node *first, *node = NULL; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1940 | struct kretprobe *rp; |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1941 | |
Masami Hiramatsu | 03bac0d | 2021-09-14 23:41:04 +0900 | [diff] [blame] | 1942 | /* 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 Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1947 | } |
| 1948 | |
Masami Hiramatsu | df91c5b | 2021-09-14 23:42:12 +0900 | [diff] [blame] | 1949 | /* |
| 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 Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1955 | |
Masami Hiramatsu | 03bac0d | 2021-09-14 23:41:04 +0900 | [diff] [blame] | 1956 | /* Run the user handler of the nodes. */ |
| 1957 | first = current->kretprobe_instances.first; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1958 | while (first) { |
| 1959 | ri = container_of(first, struct kretprobe_instance, llist); |
Masami Hiramatsu | 03bac0d | 2021-09-14 23:41:04 +0900 | [diff] [blame] | 1960 | |
| 1961 | if (WARN_ON_ONCE(ri->fp != frame_pointer)) |
| 1962 | break; |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1963 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1964 | rp = get_kretprobe(ri); |
| 1965 | if (rp && rp->handler) { |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1966 | struct kprobe *prev = kprobe_running(); |
| 1967 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1968 | __this_cpu_write(current_kprobe, &rp->kp); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1969 | ri->ret_addr = correct_ret_addr; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1970 | rp->handler(ri, regs); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1971 | __this_cpu_write(current_kprobe, prev); |
| 1972 | } |
Masami Hiramatsu | 03bac0d | 2021-09-14 23:41:04 +0900 | [diff] [blame] | 1973 | if (first == node) |
| 1974 | break; |
| 1975 | |
| 1976 | first = first->next; |
| 1977 | } |
| 1978 | |
Masami Hiramatsu | bf094cf | 2021-09-14 23:42:51 +0900 | [diff] [blame] | 1979 | arch_kretprobe_fixup_return(regs, correct_ret_addr); |
| 1980 | |
Masami Hiramatsu | 03bac0d | 2021-09-14 23:41:04 +0900 | [diff] [blame] | 1981 | /* 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 Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1990 | |
Masami Hiramatsu | b338817 | 2020-08-29 22:02:47 +0900 | [diff] [blame] | 1991 | recycle_rp_inst(ri); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1992 | } |
| 1993 | |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1994 | return (unsigned long)correct_ret_addr; |
| 1995 | } |
| 1996 | NOKPROBE_SYMBOL(__kretprobe_trampoline_handler) |
| 1997 | |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 1998 | /* |
| 1999 | * This kprobe pre_handler is registered with every kretprobe. When probe |
| 2000 | * hits it will set up the return probe. |
| 2001 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2002 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 2003 | { |
| 2004 | struct kretprobe *rp = container_of(p, struct kretprobe, kp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 2005 | struct kretprobe_instance *ri; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2006 | struct freelist_node *fn; |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 2007 | |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2008 | fn = freelist_try_get(&rp->freelist); |
| 2009 | if (!fn) { |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 2010 | rp->nmissed++; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2011 | return 0; |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 2012 | } |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2013 | |
| 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, ¤t->kretprobe_instances); |
| 2024 | |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 2025 | return 0; |
| 2026 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2027 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 2028 | |
Naveen N. Rao | 659b957 | 2017-07-07 22:37:24 +0530 | [diff] [blame] | 2029 | bool __weak arch_kprobe_on_func_entry(unsigned long offset) |
Naveen N. Rao | 90ec5e8 | 2017-02-22 19:23:37 +0530 | [diff] [blame] | 2030 | { |
| 2031 | return !offset; |
| 2032 | } |
| 2033 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 2034 | /** |
| 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 | */ |
| 2047 | int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset) |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 2048 | { |
| 2049 | kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset); |
| 2050 | |
| 2051 | if (IS_ERR(kp_addr)) |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 2052 | return PTR_ERR(kp_addr); |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 2053 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 2054 | if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset)) |
| 2055 | return -ENOENT; |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 2056 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 2057 | if (!arch_kprobe_on_func_entry(offset)) |
| 2058 | return -EINVAL; |
| 2059 | |
| 2060 | return 0; |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 2061 | } |
| 2062 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2063 | int register_kretprobe(struct kretprobe *rp) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2064 | { |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 2065 | int ret; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2066 | struct kretprobe_instance *inst; |
| 2067 | int i; |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 2068 | void *addr; |
Naveen N. Rao | 90ec5e8 | 2017-02-22 19:23:37 +0530 | [diff] [blame] | 2069 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 2070 | ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset); |
| 2071 | if (ret) |
| 2072 | return ret; |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2073 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2074 | /* If only 'rp->kp.addr' is specified, check reregistering kprobes */ |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 2075 | if (rp->kp.addr && warn_kprobe_rereg(&rp->kp)) |
Wang ShaoBo | 0188b87 | 2021-01-28 20:44:27 +0800 | [diff] [blame] | 2076 | return -EINVAL; |
| 2077 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2078 | if (kretprobe_blacklist_size) { |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 2079 | addr = kprobe_addr(&rp->kp); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 2080 | if (IS_ERR(addr)) |
| 2081 | return PTR_ERR(addr); |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2082 | |
| 2083 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
| 2084 | if (kretprobe_blacklist[i].addr == addr) |
| 2085 | return -EINVAL; |
| 2086 | } |
| 2087 | } |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2088 | |
Masami Hiramatsu | 6bbfa44 | 2021-12-01 23:45:50 +0900 | [diff] [blame] | 2089 | if (rp->data_size > KRETPROBE_MAX_DATA_SIZE) |
| 2090 | return -E2BIG; |
| 2091 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2092 | rp->kp.pre_handler = pre_handler_kretprobe; |
Ananth N Mavinakayanahalli | 7522a84 | 2006-04-20 02:43:11 -0700 | [diff] [blame] | 2093 | rp->kp.post_handler = NULL; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2094 | |
| 2095 | /* Pre-allocate memory for max kretprobe instances */ |
| 2096 | if (rp->maxactive <= 0) { |
Thomas Gleixner | 9261660 | 2019-07-26 23:19:41 +0200 | [diff] [blame] | 2097 | #ifdef CONFIG_PREEMPTION |
Heiko Carstens | c2ef666 | 2009-12-21 13:02:24 +0100 | [diff] [blame] | 2098 | rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus()); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2099 | #else |
Ananth N Mavinakayanahalli | 4dae560 | 2009-10-30 19:23:10 +0530 | [diff] [blame] | 2100 | rp->maxactive = num_possible_cpus(); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2101 | #endif |
| 2102 | } |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2103 | rp->freelist.head = NULL; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2104 | rp->rph = kzalloc(sizeof(struct kretprobe_holder), GFP_KERNEL); |
| 2105 | if (!rp->rph) |
| 2106 | return -ENOMEM; |
| 2107 | |
| 2108 | rp->rph->rp = rp; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2109 | for (i = 0; i < rp->maxactive; i++) { |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2110 | inst = kzalloc(sizeof(struct kretprobe_instance) + |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 2111 | rp->data_size, GFP_KERNEL); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2112 | if (inst == NULL) { |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2113 | refcount_set(&rp->rph->ref, i); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2114 | free_rp_inst(rp); |
| 2115 | return -ENOMEM; |
| 2116 | } |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2117 | inst->rph = rp->rph; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2118 | freelist_add(&inst->freelist, &rp->freelist); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2119 | } |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2120 | refcount_set(&rp->rph->ref, i); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2121 | |
| 2122 | rp->nmissed = 0; |
| 2123 | /* Establish function entry probe point */ |
Masami Hiramatsu | 49ad2fd | 2009-01-06 14:41:53 -0800 | [diff] [blame] | 2124 | ret = register_kprobe(&rp->kp); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2125 | if (ret != 0) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2126 | free_rp_inst(rp); |
| 2127 | return ret; |
| 2128 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2129 | EXPORT_SYMBOL_GPL(register_kretprobe); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2130 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2131 | int register_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2132 | { |
| 2133 | int ret = 0, i; |
| 2134 | |
| 2135 | if (num <= 0) |
| 2136 | return -EINVAL; |
| 2137 | for (i = 0; i < num; i++) { |
Masami Hiramatsu | 49ad2fd | 2009-01-06 14:41:53 -0800 | [diff] [blame] | 2138 | ret = register_kretprobe(rps[i]); |
Masami Hiramatsu | 67dddaa | 2008-06-12 15:21:35 -0700 | [diff] [blame] | 2139 | if (ret < 0) { |
| 2140 | if (i > 0) |
| 2141 | unregister_kretprobes(rps, i); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2142 | break; |
| 2143 | } |
| 2144 | } |
| 2145 | return ret; |
| 2146 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2147 | EXPORT_SYMBOL_GPL(register_kretprobes); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2148 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2149 | void unregister_kretprobe(struct kretprobe *rp) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2150 | { |
| 2151 | unregister_kretprobes(&rp, 1); |
| 2152 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2153 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2154 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2155 | void unregister_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2156 | { |
| 2157 | int i; |
| 2158 | |
| 2159 | if (num <= 0) |
| 2160 | return; |
| 2161 | mutex_lock(&kprobe_mutex); |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2162 | for (i = 0; i < num; i++) { |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2163 | if (__unregister_kprobe_top(&rps[i]->kp) < 0) |
| 2164 | rps[i]->kp.addr = NULL; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2165 | rps[i]->rph->rp = NULL; |
| 2166 | } |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2167 | mutex_unlock(&kprobe_mutex); |
| 2168 | |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 2169 | synchronize_rcu(); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2170 | for (i = 0; i < num; i++) { |
| 2171 | if (rps[i]->kp.addr) { |
| 2172 | __unregister_kprobe_bottom(&rps[i]->kp); |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2173 | free_rp_inst(rps[i]); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2177 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2178 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 2179 | #else /* CONFIG_KRETPROBES */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2180 | int register_kretprobe(struct kretprobe *rp) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2181 | { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2182 | return -EOPNOTSUPP; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2183 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2184 | EXPORT_SYMBOL_GPL(register_kretprobe); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2185 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2186 | int register_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2187 | { |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2188 | return -EOPNOTSUPP; |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2189 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2190 | EXPORT_SYMBOL_GPL(register_kretprobes); |
| 2191 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2192 | void unregister_kretprobe(struct kretprobe *rp) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2193 | { |
| 2194 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2195 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2196 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2197 | void unregister_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2198 | { |
| 2199 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2200 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2201 | |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2202 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2203 | { |
| 2204 | return 0; |
| 2205 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2206 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2207 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 2208 | #endif /* CONFIG_KRETPROBES */ |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2209 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2210 | /* Set the kprobe gone and remove its instruction buffer. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2211 | static void kill_kprobe(struct kprobe *p) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2212 | { |
| 2213 | struct kprobe *kp; |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 2214 | |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2215 | lockdep_assert_held(&kprobe_mutex); |
| 2216 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2217 | p->flags |= KPROBE_FLAG_GONE; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2218 | if (kprobe_aggrprobe(p)) { |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2219 | /* |
| 2220 | * If this is an aggr_kprobe, we have to list all the |
| 2221 | * chained probes and mark them GONE. |
| 2222 | */ |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2223 | list_for_each_entry(kp, &p->list, list) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2224 | kp->flags |= KPROBE_FLAG_GONE; |
| 2225 | p->post_handler = NULL; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2226 | kill_optimized_kprobe(p); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2227 | } |
| 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 Song | 0cb2f13 | 2020-07-28 14:45:36 +0800 | [diff] [blame] | 2233 | |
| 2234 | /* |
| 2235 | * The module is going away. We should disarm the kprobe which |
Masami Hiramatsu | bcb5320 | 2020-09-01 00:12:07 +0900 | [diff] [blame] | 2236 | * is using ftrace, because ftrace framework is still available at |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2237 | * 'MODULE_STATE_GOING' notification. |
Muchun Song | 0cb2f13 | 2020-07-28 14:45:36 +0800 | [diff] [blame] | 2238 | */ |
Masami Hiramatsu | bcb5320 | 2020-09-01 00:12:07 +0900 | [diff] [blame] | 2239 | if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed) |
Muchun Song | 0cb2f13 | 2020-07-28 14:45:36 +0800 | [diff] [blame] | 2240 | disarm_kprobe_ftrace(p); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2241 | } |
| 2242 | |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2243 | /* Disable one kprobe */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2244 | int disable_kprobe(struct kprobe *kp) |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2245 | { |
| 2246 | int ret = 0; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2247 | struct kprobe *p; |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2248 | |
| 2249 | mutex_lock(&kprobe_mutex); |
| 2250 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 2251 | /* Disable this kprobe */ |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2252 | p = __disable_kprobe(kp); |
| 2253 | if (IS_ERR(p)) |
| 2254 | ret = PTR_ERR(p); |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2255 | |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2256 | mutex_unlock(&kprobe_mutex); |
| 2257 | return ret; |
| 2258 | } |
| 2259 | EXPORT_SYMBOL_GPL(disable_kprobe); |
| 2260 | |
| 2261 | /* Enable one kprobe */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2262 | int enable_kprobe(struct kprobe *kp) |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2263 | { |
| 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 Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2287 | ret = arm_kprobe(p); |
| 2288 | if (ret) |
| 2289 | p->flags |= KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2290 | } |
| 2291 | out: |
| 2292 | mutex_unlock(&kprobe_mutex); |
| 2293 | return ret; |
| 2294 | } |
| 2295 | EXPORT_SYMBOL_GPL(enable_kprobe); |
| 2296 | |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 2297 | /* Caller must NOT call this in usual path. This is only for critical case */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2298 | void dump_kprobe(struct kprobe *kp) |
Frederic Weisbecker | 24851d2 | 2009-08-26 23:38:30 +0200 | [diff] [blame] | 2299 | { |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 2300 | pr_err("Dump kprobe:\n.symbol_name = %s, .offset = %x, .addr = %pS\n", |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 2301 | kp->symbol_name, kp->offset, kp->addr); |
Frederic Weisbecker | 24851d2 | 2009-08-26 23:38:30 +0200 | [diff] [blame] | 2302 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2303 | NOKPROBE_SYMBOL(dump_kprobe); |
Frederic Weisbecker | 24851d2 | 2009-08-26 23:38:30 +0200 | [diff] [blame] | 2304 | |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2305 | int 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 */ |
| 2326 | int 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 Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2341 | /* Remove all symbols in given area from kprobe blacklist */ |
| 2342 | static 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 Hiramatsu | 16db626 | 2020-03-26 23:50:00 +0900 | [diff] [blame] | 2354 | static void kprobe_remove_ksym_blacklist(unsigned long entry) |
| 2355 | { |
| 2356 | kprobe_remove_area_blacklist(entry, entry + 1); |
| 2357 | } |
| 2358 | |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 2359 | int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value, |
| 2360 | char *type, char *sym) |
| 2361 | { |
| 2362 | return -ERANGE; |
| 2363 | } |
| 2364 | |
| 2365 | int 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 Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2381 | int __init __weak arch_populate_kprobe_blacklist(void) |
| 2382 | { |
| 2383 | return 0; |
| 2384 | } |
| 2385 | |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2386 | /* |
| 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 | */ |
| 2394 | static int __init populate_kprobe_blacklist(unsigned long *start, |
| 2395 | unsigned long *end) |
| 2396 | { |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2397 | unsigned long entry; |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2398 | unsigned long *iter; |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2399 | int ret; |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2400 | |
| 2401 | for (iter = start; iter < end; iter++) { |
Masami Hiramatsu | f2ec8d9 | 2021-09-14 23:40:36 +0900 | [diff] [blame] | 2402 | entry = (unsigned long)dereference_symbol_descriptor((void *)*iter); |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2403 | ret = kprobe_add_ksym_blacklist(entry); |
| 2404 | if (ret == -EINVAL) |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2405 | continue; |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2406 | if (ret < 0) |
| 2407 | return ret; |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2408 | } |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2409 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2410 | /* Symbols in '__kprobes_text' are blacklisted */ |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2411 | ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start, |
| 2412 | (unsigned long)__kprobes_text_end); |
Thomas Gleixner | 66e9b07 | 2020-03-10 14:04:34 +0100 | [diff] [blame] | 2413 | if (ret) |
| 2414 | return ret; |
| 2415 | |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2416 | /* Symbols in 'noinstr' section are blacklisted */ |
Thomas Gleixner | 66e9b07 | 2020-03-10 14:04:34 +0100 | [diff] [blame] | 2417 | ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start, |
| 2418 | (unsigned long)__noinstr_text_end); |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2419 | |
| 2420 | return ret ? : arch_populate_kprobe_blacklist(); |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2421 | } |
| 2422 | |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2423 | static void add_module_kprobe_blacklist(struct module *mod) |
| 2424 | { |
| 2425 | unsigned long start, end; |
Masami Hiramatsu | 16db626 | 2020-03-26 23:50:00 +0900 | [diff] [blame] | 2426 | 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 Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2432 | |
| 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 Gleixner | 66e9b07 | 2020-03-10 14:04:34 +0100 | [diff] [blame] | 2438 | |
| 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 Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | static void remove_module_kprobe_blacklist(struct module *mod) |
| 2447 | { |
| 2448 | unsigned long start, end; |
Masami Hiramatsu | 16db626 | 2020-03-26 23:50:00 +0900 | [diff] [blame] | 2449 | 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 Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2455 | |
| 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 Gleixner | 66e9b07 | 2020-03-10 14:04:34 +0100 | [diff] [blame] | 2461 | |
| 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 Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2467 | } |
| 2468 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2469 | /* Module notifier call back, checking kprobes on the module */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2470 | static int kprobes_module_callback(struct notifier_block *nb, |
| 2471 | unsigned long val, void *data) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2472 | { |
| 2473 | struct module *mod = data; |
| 2474 | struct hlist_head *head; |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2475 | struct kprobe *p; |
| 2476 | unsigned int i; |
Masami Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2477 | int checkcore = (val == MODULE_STATE_GOING); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2478 | |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2479 | if (val == MODULE_STATE_COMING) { |
| 2480 | mutex_lock(&kprobe_mutex); |
| 2481 | add_module_kprobe_blacklist(mod); |
| 2482 | mutex_unlock(&kprobe_mutex); |
| 2483 | } |
Masami Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2484 | if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2485 | return NOTIFY_DONE; |
| 2486 | |
| 2487 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2488 | * 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 Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2491 | * disable kprobes which have been inserted in the sections. |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2492 | */ |
| 2493 | mutex_lock(&kprobe_mutex); |
| 2494 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2495 | head = &kprobe_table[i]; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2496 | hlist_for_each_entry(p, head, hlist) |
Masami Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2497 | if (within_module_init((unsigned long)p->addr, mod) || |
| 2498 | (checkcore && |
| 2499 | within_module_core((unsigned long)p->addr, mod))) { |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2500 | /* |
| 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) | 545a028 | 2017-05-16 14:58:35 -0400 | [diff] [blame] | 2504 | * |
| 2505 | * Note, this will also move any optimized probes |
| 2506 | * that are pending to be removed from their |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2507 | * corresponding lists to the 'freeing_list' and |
Steven Rostedt (VMware) | 545a028 | 2017-05-16 14:58:35 -0400 | [diff] [blame] | 2508 | * will not be touched by the delayed |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2509 | * kprobe_optimizer() work handler. |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2510 | */ |
| 2511 | kill_kprobe(p); |
| 2512 | } |
| 2513 | } |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2514 | if (val == MODULE_STATE_GOING) |
| 2515 | remove_module_kprobe_blacklist(mod); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2516 | mutex_unlock(&kprobe_mutex); |
| 2517 | return NOTIFY_DONE; |
| 2518 | } |
| 2519 | |
| 2520 | static struct notifier_block kprobe_module_nb = { |
| 2521 | .notifier_call = kprobes_module_callback, |
| 2522 | .priority = 0 |
| 2523 | }; |
| 2524 | |
Masami Hiramatsu | 82d083a | 2020-09-10 17:55:05 +0900 | [diff] [blame] | 2525 | void 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 Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2535 | /* Kill all kprobes on initmem because the target code has been freed. */ |
Masami Hiramatsu | 82d083a | 2020-09-10 17:55:05 +0900 | [diff] [blame] | 2536 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | static 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 Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2553 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | INIT_HLIST_HEAD(&kprobe_table[i]); |
| 2555 | |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2556 | err = populate_kprobe_blacklist(__start_kprobe_blacklist, |
| 2557 | __stop_kprobe_blacklist); |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2558 | if (err) |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 2559 | pr_err("Failed to populate blacklist (error %d), kprobes not restricted, be careful using them!\n", err); |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 2560 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2561 | if (kretprobe_blacklist_size) { |
| 2562 | /* lookup the function address from its name */ |
| 2563 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
Naveen N. Rao | 49e0b46 | 2017-04-19 18:21:00 +0530 | [diff] [blame] | 2564 | kretprobe_blacklist[i].addr = |
Naveen N. Rao | 290e307 | 2017-04-19 18:21:01 +0530 | [diff] [blame] | 2565 | kprobe_lookup_name(kretprobe_blacklist[i].name, 0); |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2566 | if (!kretprobe_blacklist[i].addr) |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 2567 | pr_err("Failed to lookup symbol '%s' for kretprobe blacklist. Maybe the target function is removed or renamed.\n", |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2568 | kretprobe_blacklist[i].name); |
| 2569 | } |
| 2570 | } |
| 2571 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2572 | /* By default, kprobes are armed */ |
| 2573 | kprobes_all_disarmed = false; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2574 | |
Masami Hiramatsu | c85c9a2 | 2021-02-18 23:29:23 +0900 | [diff] [blame] | 2575 | #if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT) |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2576 | /* Init 'kprobe_optinsn_slots' for allocation */ |
Masami Hiramatsu | c85c9a2 | 2021-02-18 23:29:23 +0900 | [diff] [blame] | 2577 | kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE; |
| 2578 | #endif |
| 2579 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 2580 | err = arch_init_kprobes(); |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 2581 | if (!err) |
| 2582 | err = register_die_notifier(&kprobe_exceptions_nb); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2583 | if (!err) |
| 2584 | err = register_module_notifier(&kprobe_module_nb); |
| 2585 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 2586 | kprobes_initialized = (err == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | return err; |
| 2588 | } |
Masami Hiramatsu | 36dadef | 2020-09-10 21:38:39 +0900 | [diff] [blame] | 2589 | early_initcall(init_kprobes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | |
Masami Hiramatsu | c85c9a2 | 2021-02-18 23:29:23 +0900 | [diff] [blame] | 2591 | #if defined(CONFIG_OPTPROBES) |
| 2592 | static 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 | } |
| 2603 | subsys_initcall(init_optprobes); |
| 2604 | #endif |
| 2605 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2606 | #ifdef CONFIG_DEBUG_FS |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2607 | static void report_probe(struct seq_file *pi, struct kprobe *p, |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2608 | const char *sym, int offset, char *modname, struct kprobe *pp) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2609 | { |
| 2610 | char *kprobe_type; |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2611 | void *addr = p->addr; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2612 | |
| 2613 | if (p->pre_handler == pre_handler_kretprobe) |
| 2614 | kprobe_type = "r"; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2615 | else |
| 2616 | kprobe_type = "k"; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2617 | |
Kees Cook | 60f7bb6 | 2020-07-02 15:20:22 -0700 | [diff] [blame] | 2618 | if (!kallsyms_show_value(pi->file->f_cred)) |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2619 | addr = NULL; |
| 2620 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2621 | if (sym) |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2622 | seq_printf(pi, "%px %s %s+0x%x %s ", |
| 2623 | addr, kprobe_type, sym, offset, |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2624 | (modname ? modname : " ")); |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2625 | else /* try to use %pS */ |
| 2626 | seq_printf(pi, "%px %s %pS ", |
| 2627 | addr, kprobe_type, p->addr); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2628 | |
| 2629 | if (!pp) |
| 2630 | pp = p; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 2631 | seq_printf(pi, "%s%s%s%s\n", |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2632 | (kprobe_gone(p) ? "[GONE]" : ""), |
| 2633 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""), |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 2634 | (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""), |
| 2635 | (kprobe_ftrace(pp) ? "[FTRACE]" : "")); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2636 | } |
| 2637 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2638 | static void *kprobe_seq_start(struct seq_file *f, loff_t *pos) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2639 | { |
| 2640 | return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL; |
| 2641 | } |
| 2642 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2643 | static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2644 | { |
| 2645 | (*pos)++; |
| 2646 | if (*pos >= KPROBE_TABLE_SIZE) |
| 2647 | return NULL; |
| 2648 | return pos; |
| 2649 | } |
| 2650 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2651 | static void kprobe_seq_stop(struct seq_file *f, void *v) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2652 | { |
| 2653 | /* Nothing to do */ |
| 2654 | } |
| 2655 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2656 | static int show_kprobe_addr(struct seq_file *pi, void *v) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2657 | { |
| 2658 | struct hlist_head *head; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2659 | struct kprobe *p, *kp; |
| 2660 | const char *sym = NULL; |
| 2661 | unsigned int i = *(loff_t *) v; |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 2662 | unsigned long offset = 0; |
Joe Mario | ab76786 | 2013-11-12 15:10:23 -0800 | [diff] [blame] | 2663 | char *modname, namebuf[KSYM_NAME_LEN]; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2664 | |
| 2665 | head = &kprobe_table[i]; |
| 2666 | preempt_disable(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2667 | hlist_for_each_entry_rcu(p, head, hlist) { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 2668 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2669 | &offset, &modname, namebuf); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2670 | if (kprobe_aggrprobe(p)) { |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2671 | list_for_each_entry_rcu(kp, &p->list, list) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2672 | report_probe(pi, kp, sym, offset, modname, p); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2673 | } else |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2674 | report_probe(pi, p, sym, offset, modname, NULL); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2675 | } |
| 2676 | preempt_enable(); |
| 2677 | return 0; |
| 2678 | } |
| 2679 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2680 | static const struct seq_operations kprobes_sops = { |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2681 | .start = kprobe_seq_start, |
| 2682 | .next = kprobe_seq_next, |
| 2683 | .stop = kprobe_seq_stop, |
| 2684 | .show = show_kprobe_addr |
| 2685 | }; |
| 2686 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2687 | DEFINE_SEQ_ATTRIBUTE(kprobes); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2688 | |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2689 | /* kprobes/blacklist -- shows which functions can not be probed */ |
| 2690 | static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos) |
| 2691 | { |
Masami Hiramatsu | 4fdd888 | 2020-03-26 23:49:36 +0900 | [diff] [blame] | 2692 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2693 | return seq_list_start(&kprobe_blacklist, *pos); |
| 2694 | } |
| 2695 | |
| 2696 | static 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 | |
| 2701 | static 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 Hiramatsu | ffb9bd6 | 2018-04-28 21:35:32 +0900 | [diff] [blame] | 2706 | /* |
Masami Hiramatsu | 223a76b | 2021-09-14 23:39:34 +0900 | [diff] [blame] | 2707 | * If '/proc/kallsyms' is not showing kernel address, we won't |
Masami Hiramatsu | ffb9bd6 | 2018-04-28 21:35:32 +0900 | [diff] [blame] | 2708 | * show them here either. |
| 2709 | */ |
Kees Cook | 60f7bb6 | 2020-07-02 15:20:22 -0700 | [diff] [blame] | 2710 | if (!kallsyms_show_value(m->file->f_cred)) |
Masami Hiramatsu | ffb9bd6 | 2018-04-28 21:35:32 +0900 | [diff] [blame] | 2711 | 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 Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2716 | return 0; |
| 2717 | } |
| 2718 | |
Masami Hiramatsu | 4fdd888 | 2020-03-26 23:49:36 +0900 | [diff] [blame] | 2719 | static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v) |
| 2720 | { |
| 2721 | mutex_unlock(&kprobe_mutex); |
| 2722 | } |
| 2723 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2724 | static const struct seq_operations kprobe_blacklist_sops = { |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2725 | .start = kprobe_blacklist_seq_start, |
| 2726 | .next = kprobe_blacklist_seq_next, |
Masami Hiramatsu | 4fdd888 | 2020-03-26 23:49:36 +0900 | [diff] [blame] | 2727 | .stop = kprobe_blacklist_seq_stop, |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2728 | .show = kprobe_blacklist_seq_show, |
| 2729 | }; |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2730 | DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist); |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2731 | |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2732 | static int arm_all_kprobes(void) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2733 | { |
| 2734 | struct hlist_head *head; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2735 | struct kprobe *p; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2736 | unsigned int i, total = 0, errors = 0; |
| 2737 | int err, ret = 0; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2738 | |
| 2739 | mutex_lock(&kprobe_mutex); |
| 2740 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2741 | /* If kprobes are armed, just return */ |
| 2742 | if (!kprobes_all_disarmed) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2743 | goto already_enabled; |
| 2744 | |
Wang Nan | 977ad48 | 2015-02-13 14:40:24 -0800 | [diff] [blame] | 2745 | /* |
| 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 Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2751 | /* Arming kprobes doesn't optimize kprobe itself */ |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2752 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2753 | head = &kprobe_table[i]; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2754 | /* Arm all kprobes on a best-effort basis */ |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2755 | hlist_for_each_entry(p, head, hlist) { |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2756 | if (!kprobe_disabled(p)) { |
| 2757 | err = arm_kprobe(p); |
| 2758 | if (err) { |
| 2759 | errors++; |
| 2760 | ret = err; |
| 2761 | } |
| 2762 | total++; |
| 2763 | } |
| 2764 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2767 | if (errors) |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 2768 | 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 Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2769 | errors, total); |
| 2770 | else |
| 2771 | pr_info("Kprobes globally enabled\n"); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2772 | |
| 2773 | already_enabled: |
| 2774 | mutex_unlock(&kprobe_mutex); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2775 | return ret; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2778 | static int disarm_all_kprobes(void) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2779 | { |
| 2780 | struct hlist_head *head; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2781 | struct kprobe *p; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2782 | unsigned int i, total = 0, errors = 0; |
| 2783 | int err, ret = 0; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2784 | |
| 2785 | mutex_lock(&kprobe_mutex); |
| 2786 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2787 | /* If kprobes are already disarmed, just return */ |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 2788 | if (kprobes_all_disarmed) { |
| 2789 | mutex_unlock(&kprobe_mutex); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2790 | return 0; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 2791 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2792 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2793 | kprobes_all_disarmed = true; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2794 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2795 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2796 | head = &kprobe_table[i]; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2797 | /* Disarm all kprobes on a best-effort basis */ |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2798 | hlist_for_each_entry(p, head, hlist) { |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2799 | 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 Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2807 | } |
| 2808 | } |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2809 | |
| 2810 | if (errors) |
Masami Hiramatsu | 9c89bb8 | 2021-09-14 23:39:25 +0900 | [diff] [blame] | 2811 | 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 Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2812 | errors, total); |
| 2813 | else |
| 2814 | pr_info("Kprobes globally disabled\n"); |
| 2815 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2816 | mutex_unlock(&kprobe_mutex); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2817 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 2818 | /* Wait for disarming all kprobes by optimizer */ |
| 2819 | wait_for_kprobe_optimizer(); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2820 | |
| 2821 | return ret; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2822 | } |
| 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 | */ |
| 2829 | static 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 Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2834 | if (!kprobes_all_disarmed) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2835 | 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 | |
| 2843 | static ssize_t write_enabled_file_bool(struct file *file, |
| 2844 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 2845 | { |
Punit Agrawal | 5d6de7d | 2021-09-14 23:38:46 +0900 | [diff] [blame] | 2846 | bool enable; |
| 2847 | int ret; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2848 | |
Punit Agrawal | 5d6de7d | 2021-09-14 23:38:46 +0900 | [diff] [blame] | 2849 | ret = kstrtobool_from_user(user_buf, count, &enable); |
| 2850 | if (ret) |
| 2851 | return ret; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2852 | |
Punit Agrawal | 5d6de7d | 2021-09-14 23:38:46 +0900 | [diff] [blame] | 2853 | ret = enable ? arm_all_kprobes() : disarm_all_kprobes(); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2854 | if (ret) |
| 2855 | return ret; |
| 2856 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2857 | return count; |
| 2858 | } |
| 2859 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2860 | static const struct file_operations fops_kp = { |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2861 | .read = read_enabled_file_bool, |
| 2862 | .write = write_enabled_file_bool, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2863 | .llseek = default_llseek, |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2864 | }; |
| 2865 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2866 | static int __init debugfs_kprobe_init(void) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2867 | { |
Greg Kroah-Hartman | 8c0fd1f | 2019-01-22 16:21:46 +0100 | [diff] [blame] | 2868 | struct dentry *dir; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2869 | |
| 2870 | dir = debugfs_create_dir("kprobes", NULL); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2871 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2872 | debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2873 | |
Punit Agrawal | 8f7262c | 2021-09-14 23:38:37 +0900 | [diff] [blame] | 2874 | debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp); |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2875 | |
Greg Kroah-Hartman | 8c0fd1f | 2019-01-22 16:21:46 +0100 | [diff] [blame] | 2876 | debugfs_create_file("blacklist", 0400, dir, NULL, |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2877 | &kprobe_blacklist_fops); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2878 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2879 | return 0; |
| 2880 | } |
| 2881 | |
| 2882 | late_initcall(debugfs_kprobe_init); |
| 2883 | #endif /* CONFIG_DEBUG_FS */ |