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) |
| 4 | * kernel/kprobes.c |
| 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) IBM Corporation, 2002, 2004 |
| 7 | * |
| 8 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel |
| 9 | * Probes initial implementation (includes suggestions from |
| 10 | * Rusty Russell). |
| 11 | * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with |
| 12 | * hlists and exceptions notifier as suggested by Andi Kleen. |
| 13 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes |
| 14 | * interface to access function arguments. |
| 15 | * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes |
| 16 | * exceptions notifier to be first on the priority list. |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 17 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
| 18 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
| 19 | * <prasanna@in.ibm.com> added function-return probes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
| 21 | #include <linux/kprobes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/hash.h> |
| 23 | #include <linux/init.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 24 | #include <linux/slab.h> |
Randy Dunlap | e386979 | 2007-05-08 00:27:01 -0700 | [diff] [blame] | 25 | #include <linux/stddef.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 27 | #include <linux/moduleloader.h> |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 28 | #include <linux/kallsyms.h> |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 29 | #include <linux/freezer.h> |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 30 | #include <linux/seq_file.h> |
| 31 | #include <linux/debugfs.h> |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 32 | #include <linux/sysctl.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 33 | #include <linux/kdebug.h> |
Mathieu Desnoyers | 4460fda | 2009-03-06 10:36:38 -0500 | [diff] [blame] | 34 | #include <linux/memory.h> |
Masami Hiramatsu | 4554dbc | 2010-02-02 16:49:18 -0500 | [diff] [blame] | 35 | #include <linux/ftrace.h> |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 36 | #include <linux/cpu.h> |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 37 | #include <linux/jump_label.h> |
Peter Zijlstra | fa68bd0 | 2021-06-28 13:24:12 +0200 | [diff] [blame^] | 38 | #include <linux/static_call.h> |
Adrian Hunter | 69e4908 | 2020-05-12 15:19:11 +0300 | [diff] [blame] | 39 | #include <linux/perf_event.h> |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 40 | |
Christoph Hellwig | bfd45be | 2016-10-11 13:52:22 -0700 | [diff] [blame] | 41 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/cacheflush.h> |
| 43 | #include <asm/errno.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 44 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #define KPROBE_HASH_BITS 6 |
| 47 | #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) |
| 48 | |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 49 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 50 | static int kprobes_initialized; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 51 | /* kprobe_table can be accessed by |
| 52 | * - Normal hlist traversal and RCU add/del under kprobe_mutex is held. |
| 53 | * Or |
| 54 | * - RCU hlist traversal under disabling preempt (breakpoint handlers) |
| 55 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; |
| 57 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 58 | /* NOTE: change this value only with kprobe_mutex held */ |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 59 | static bool kprobes_all_disarmed; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 60 | |
Masami Hiramatsu | 43948f5 | 2010-10-25 22:18:01 +0900 | [diff] [blame] | 61 | /* This protects kprobe_table and optimizing_list */ |
| 62 | static DEFINE_MUTEX(kprobe_mutex); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 63 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 64 | |
Naveen N. Rao | 290e307 | 2017-04-19 18:21:01 +0530 | [diff] [blame] | 65 | kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, |
| 66 | unsigned int __unused) |
Naveen N. Rao | 49e0b46 | 2017-04-19 18:21:00 +0530 | [diff] [blame] | 67 | { |
| 68 | return ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); |
| 69 | } |
| 70 | |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 71 | /* Blacklist -- list of struct kprobe_blacklist_entry */ |
| 72 | static LIST_HEAD(kprobe_blacklist); |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 73 | |
Anil S Keshavamurthy | 2d14e39 | 2006-01-09 20:52:41 -0800 | [diff] [blame] | 74 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 75 | /* |
| 76 | * kprobe->ainsn.insn points to the copy of the instruction to be |
| 77 | * single-stepped. x86_64, POWER4 and above have no-exec support and |
| 78 | * stepping on the instruction on a vmalloced/kmalloced/data page |
| 79 | * is a recipe for disaster |
| 80 | */ |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 81 | struct kprobe_insn_page { |
Masami Hiramatsu | c5cb5a2 | 2009-06-30 17:08:14 -0400 | [diff] [blame] | 82 | struct list_head list; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 83 | kprobe_opcode_t *insns; /* Page of instruction slots */ |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 84 | struct kprobe_insn_cache *cache; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 85 | int nused; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 86 | int ngarbage; |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 87 | char slot_used[]; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 90 | #define KPROBE_INSN_PAGE_SIZE(slots) \ |
| 91 | (offsetof(struct kprobe_insn_page, slot_used) + \ |
| 92 | (sizeof(char) * (slots))) |
| 93 | |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 94 | static int slots_per_page(struct kprobe_insn_cache *c) |
| 95 | { |
| 96 | return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t)); |
| 97 | } |
| 98 | |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 99 | enum kprobe_slot_state { |
| 100 | SLOT_CLEAN = 0, |
| 101 | SLOT_DIRTY = 1, |
| 102 | SLOT_USED = 2, |
| 103 | }; |
| 104 | |
Masami Hiramatsu | 63fef14 | 2017-08-18 17:24:00 +0900 | [diff] [blame] | 105 | void __weak *alloc_insn_page(void) |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 106 | { |
| 107 | return module_alloc(PAGE_SIZE); |
| 108 | } |
| 109 | |
Masami Hiramatsu | c93f5cf | 2017-05-25 19:38:17 +0900 | [diff] [blame] | 110 | void __weak free_insn_page(void *page) |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 111 | { |
Rusty Russell | be1f221 | 2015-01-20 09:07:05 +1030 | [diff] [blame] | 112 | module_memfree(page); |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 115 | struct kprobe_insn_cache kprobe_insn_slots = { |
| 116 | .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex), |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 117 | .alloc = alloc_insn_page, |
| 118 | .free = free_insn_page, |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 119 | .sym = KPROBE_INSN_PAGE_SYM, |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 120 | .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages), |
| 121 | .insn_size = MAX_INSN_SIZE, |
| 122 | .nr_garbage = 0, |
| 123 | }; |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 124 | static int collect_garbage_slots(struct kprobe_insn_cache *c); |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 125 | |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 126 | /** |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 127 | * __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] | 128 | * We allocate an executable page if there's no room on existing ones. |
| 129 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 130 | kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c) |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 131 | { |
| 132 | struct kprobe_insn_page *kip; |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 133 | kprobe_opcode_t *slot = NULL; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 134 | |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 135 | /* 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] | 136 | mutex_lock(&c->mutex); |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 137 | retry: |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 138 | rcu_read_lock(); |
| 139 | list_for_each_entry_rcu(kip, &c->pages, list) { |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 140 | if (kip->nused < slots_per_page(c)) { |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 141 | int i; |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 142 | for (i = 0; i < slots_per_page(c); i++) { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 143 | if (kip->slot_used[i] == SLOT_CLEAN) { |
| 144 | kip->slot_used[i] = SLOT_USED; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 145 | kip->nused++; |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 146 | slot = kip->insns + (i * c->insn_size); |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 147 | rcu_read_unlock(); |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 148 | goto out; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 151 | /* kip->nused is broken. Fix it. */ |
| 152 | kip->nused = slots_per_page(c); |
| 153 | WARN_ON(1); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 156 | rcu_read_unlock(); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 157 | |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 158 | /* If there are any garbage slots, collect it and try again. */ |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 159 | if (c->nr_garbage && collect_garbage_slots(c) == 0) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 160 | goto retry; |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 161 | |
| 162 | /* All out of space. Need to allocate a new page. */ |
| 163 | kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL); |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 164 | if (!kip) |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 165 | goto out; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * Use module_alloc so this page is within +/- 2GB of where the |
| 169 | * kernel image and loaded module images reside. This is required |
| 170 | * so x86_64 can correctly handle the %rip-relative fixups. |
| 171 | */ |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 172 | kip->insns = c->alloc(); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 173 | if (!kip->insns) { |
| 174 | kfree(kip); |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 175 | goto out; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 176 | } |
Masami Hiramatsu | c5cb5a2 | 2009-06-30 17:08:14 -0400 | [diff] [blame] | 177 | INIT_LIST_HEAD(&kip->list); |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 178 | memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c)); |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 179 | kip->slot_used[0] = SLOT_USED; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 180 | kip->nused = 1; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 181 | kip->ngarbage = 0; |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 182 | kip->cache = c; |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 183 | list_add_rcu(&kip->list, &c->pages); |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 184 | slot = kip->insns; |
Adrian Hunter | 69e4908 | 2020-05-12 15:19:11 +0300 | [diff] [blame] | 185 | |
| 186 | /* Record the perf ksymbol register event after adding the page */ |
| 187 | perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns, |
| 188 | PAGE_SIZE, false, c->sym); |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 189 | out: |
| 190 | mutex_unlock(&c->mutex); |
| 191 | return slot; |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 194 | /* Return 1 if all garbages are collected, otherwise 0. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 195 | static int collect_one_slot(struct kprobe_insn_page *kip, int idx) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 196 | { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 197 | kip->slot_used[idx] = SLOT_CLEAN; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 198 | kip->nused--; |
| 199 | if (kip->nused == 0) { |
| 200 | /* |
| 201 | * Page is no longer in use. Free it unless |
| 202 | * it's the last one. We keep the last one |
| 203 | * so as not to have to set it up again the |
| 204 | * next time somebody inserts a probe. |
| 205 | */ |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 206 | if (!list_is_singular(&kip->list)) { |
Adrian Hunter | 69e4908 | 2020-05-12 15:19:11 +0300 | [diff] [blame] | 207 | /* |
| 208 | * Record perf ksymbol unregister event before removing |
| 209 | * the page. |
| 210 | */ |
| 211 | perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, |
| 212 | (unsigned long)kip->insns, PAGE_SIZE, true, |
| 213 | kip->cache->sym); |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 214 | list_del_rcu(&kip->list); |
| 215 | synchronize_rcu(); |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 216 | kip->cache->free(kip->insns); |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 217 | kfree(kip); |
| 218 | } |
| 219 | return 1; |
| 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 224 | static int collect_garbage_slots(struct kprobe_insn_cache *c) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 225 | { |
Masami Hiramatsu | c5cb5a2 | 2009-06-30 17:08:14 -0400 | [diff] [blame] | 226 | struct kprobe_insn_page *kip, *next; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 227 | |
Masami Hiramatsu | 615d0eb | 2010-02-02 16:49:04 -0500 | [diff] [blame] | 228 | /* Ensure no-one is interrupted on the garbages */ |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 229 | synchronize_rcu(); |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 230 | |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 231 | list_for_each_entry_safe(kip, next, &c->pages, list) { |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 232 | int i; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 233 | if (kip->ngarbage == 0) |
| 234 | continue; |
| 235 | kip->ngarbage = 0; /* we will collect all garbages */ |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 236 | for (i = 0; i < slots_per_page(c); i++) { |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 237 | if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i)) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | } |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 241 | c->nr_garbage = 0; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 245 | void __free_insn_slot(struct kprobe_insn_cache *c, |
| 246 | kprobe_opcode_t *slot, int dirty) |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 247 | { |
| 248 | struct kprobe_insn_page *kip; |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 249 | long idx; |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 250 | |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 251 | mutex_lock(&c->mutex); |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 252 | rcu_read_lock(); |
| 253 | list_for_each_entry_rcu(kip, &c->pages, list) { |
| 254 | idx = ((long)slot - (long)kip->insns) / |
| 255 | (c->insn_size * sizeof(kprobe_opcode_t)); |
| 256 | if (idx >= 0 && idx < slots_per_page(c)) |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 257 | goto out; |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 258 | } |
| 259 | /* Could not find this slot. */ |
| 260 | WARN_ON(1); |
| 261 | kip = NULL; |
| 262 | out: |
| 263 | rcu_read_unlock(); |
| 264 | /* Mark and sweep: this may sleep */ |
| 265 | if (kip) { |
| 266 | /* Check double free */ |
| 267 | WARN_ON(kip->slot_used[idx] != SLOT_USED); |
| 268 | if (dirty) { |
| 269 | kip->slot_used[idx] = SLOT_DIRTY; |
| 270 | kip->ngarbage++; |
| 271 | if (++c->nr_garbage > slots_per_page(c)) |
| 272 | collect_garbage_slots(c); |
| 273 | } else { |
| 274 | collect_one_slot(kip, idx); |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 275 | } |
| 276 | } |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 277 | mutex_unlock(&c->mutex); |
Masami Hiramatsu | 4610ee1 | 2010-02-25 08:33:59 -0500 | [diff] [blame] | 278 | } |
| 279 | |
Masami Hiramatsu | 5b48562 | 2017-01-08 23:58:09 +0900 | [diff] [blame] | 280 | /* |
| 281 | * Check given address is on the page of kprobe instruction slots. |
| 282 | * This will be used for checking whether the address on a stack |
| 283 | * is on a text area or not. |
| 284 | */ |
| 285 | bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr) |
| 286 | { |
| 287 | struct kprobe_insn_page *kip; |
| 288 | bool ret = false; |
| 289 | |
| 290 | rcu_read_lock(); |
| 291 | list_for_each_entry_rcu(kip, &c->pages, list) { |
| 292 | if (addr >= (unsigned long)kip->insns && |
| 293 | addr < (unsigned long)kip->insns + PAGE_SIZE) { |
| 294 | ret = true; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | rcu_read_unlock(); |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 303 | int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum, |
| 304 | unsigned long *value, char *type, char *sym) |
| 305 | { |
| 306 | struct kprobe_insn_page *kip; |
| 307 | int ret = -ERANGE; |
| 308 | |
| 309 | rcu_read_lock(); |
| 310 | list_for_each_entry_rcu(kip, &c->pages, list) { |
| 311 | if ((*symnum)--) |
| 312 | continue; |
| 313 | strlcpy(sym, c->sym, KSYM_NAME_LEN); |
| 314 | *type = 't'; |
| 315 | *value = (unsigned long)kip->insns; |
| 316 | ret = 0; |
| 317 | break; |
| 318 | } |
| 319 | rcu_read_unlock(); |
| 320 | |
| 321 | return ret; |
| 322 | } |
| 323 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 324 | #ifdef CONFIG_OPTPROBES |
| 325 | /* For optimized_kprobe buffer */ |
Heiko Carstens | c802d64 | 2013-09-11 14:24:11 -0700 | [diff] [blame] | 326 | struct kprobe_insn_cache kprobe_optinsn_slots = { |
| 327 | .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex), |
Heiko Carstens | af96397 | 2013-09-11 14:24:13 -0700 | [diff] [blame] | 328 | .alloc = alloc_insn_page, |
| 329 | .free = free_insn_page, |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 330 | .sym = KPROBE_OPTINSN_PAGE_SYM, |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 331 | .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages), |
| 332 | /* .insn_size is initialized later */ |
| 333 | .nr_garbage = 0, |
| 334 | }; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 335 | #endif |
Anil S Keshavamurthy | 2d14e39 | 2006-01-09 20:52:41 -0800 | [diff] [blame] | 336 | #endif |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 337 | |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 338 | /* We have preemption disabled.. so it is safe to use __ versions */ |
| 339 | static inline void set_kprobe_instance(struct kprobe *kp) |
| 340 | { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 341 | __this_cpu_write(kprobe_instance, kp); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static inline void reset_kprobe_instance(void) |
| 345 | { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 346 | __this_cpu_write(kprobe_instance, NULL); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 349 | /* |
| 350 | * This routine is called either: |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 351 | * - under the kprobe_mutex - during kprobe_[un]register() |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 352 | * OR |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 353 | * - with preemption disabled - from arch/xxx/kernel/kprobes.c |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 354 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 355 | struct kprobe *get_kprobe(void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | struct hlist_head *head; |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 358 | struct kprobe *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; |
Masami Hiramatsu | 6743ad4 | 2020-05-12 17:02:33 +0900 | [diff] [blame] | 361 | hlist_for_each_entry_rcu(p, head, hlist, |
| 362 | lockdep_is_held(&kprobe_mutex)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (p->addr == addr) |
| 364 | return p; |
| 365 | } |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 369 | NOKPROBE_SYMBOL(get_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 371 | static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 372 | |
| 373 | /* Return true if the kprobe is an aggregator */ |
| 374 | static inline int kprobe_aggrprobe(struct kprobe *p) |
| 375 | { |
| 376 | return p->pre_handler == aggr_pre_handler; |
| 377 | } |
| 378 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 379 | /* Return true(!0) if the kprobe is unused */ |
| 380 | static inline int kprobe_unused(struct kprobe *p) |
| 381 | { |
| 382 | return kprobe_aggrprobe(p) && kprobe_disabled(p) && |
| 383 | list_empty(&p->list); |
| 384 | } |
| 385 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 386 | /* |
| 387 | * Keep all fields in the kprobe consistent |
| 388 | */ |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 389 | static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 390 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 391 | memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t)); |
| 392 | memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn)); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | #ifdef CONFIG_OPTPROBES |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 396 | /* NOTE: change this value only with kprobe_mutex held */ |
| 397 | static bool kprobes_allow_optimization; |
| 398 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 399 | /* |
| 400 | * Call all pre_handler on the list, but ignores its return value. |
| 401 | * This must be called from arch-dep optimized caller. |
| 402 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 403 | void opt_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 404 | { |
| 405 | struct kprobe *kp; |
| 406 | |
| 407 | list_for_each_entry_rcu(kp, &p->list, list) { |
| 408 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { |
| 409 | set_kprobe_instance(kp); |
Naveen N. Rao | 4f3a871 | 2017-10-17 13:48:34 +0530 | [diff] [blame] | 410 | kp->pre_handler(kp, regs); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 411 | } |
| 412 | reset_kprobe_instance(); |
| 413 | } |
| 414 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 415 | NOKPROBE_SYMBOL(opt_pre_handler); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 416 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 417 | /* Free optimized instructions and optimized_kprobe */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 418 | static void free_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 419 | { |
| 420 | struct optimized_kprobe *op; |
| 421 | |
| 422 | op = container_of(p, struct optimized_kprobe, kp); |
| 423 | arch_remove_optimized_kprobe(op); |
| 424 | arch_remove_kprobe(p); |
| 425 | kfree(op); |
| 426 | } |
| 427 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 428 | /* Return true(!0) if the kprobe is ready for optimization. */ |
| 429 | static inline int kprobe_optready(struct kprobe *p) |
| 430 | { |
| 431 | struct optimized_kprobe *op; |
| 432 | |
| 433 | if (kprobe_aggrprobe(p)) { |
| 434 | op = container_of(p, struct optimized_kprobe, kp); |
| 435 | return arch_prepared_optinsn(&op->optinsn); |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 441 | /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */ |
| 442 | static inline int kprobe_disarmed(struct kprobe *p) |
| 443 | { |
| 444 | struct optimized_kprobe *op; |
| 445 | |
| 446 | /* If kprobe is not aggr/opt probe, just return kprobe is disabled */ |
| 447 | if (!kprobe_aggrprobe(p)) |
| 448 | return kprobe_disabled(p); |
| 449 | |
| 450 | op = container_of(p, struct optimized_kprobe, kp); |
| 451 | |
| 452 | return kprobe_disabled(p) && list_empty(&op->list); |
| 453 | } |
| 454 | |
| 455 | /* Return true(!0) if the probe is queued on (un)optimizing lists */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 456 | static int kprobe_queued(struct kprobe *p) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 457 | { |
| 458 | struct optimized_kprobe *op; |
| 459 | |
| 460 | if (kprobe_aggrprobe(p)) { |
| 461 | op = container_of(p, struct optimized_kprobe, kp); |
| 462 | if (!list_empty(&op->list)) |
| 463 | return 1; |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 468 | /* |
| 469 | * Return an optimized kprobe whose optimizing code replaces |
| 470 | * instructions including addr (exclude breakpoint). |
| 471 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 472 | static struct kprobe *get_optimized_kprobe(unsigned long addr) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 473 | { |
| 474 | int i; |
| 475 | struct kprobe *p = NULL; |
| 476 | struct optimized_kprobe *op; |
| 477 | |
| 478 | /* Don't check i == 0, since that is a breakpoint case. */ |
| 479 | for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++) |
| 480 | p = get_kprobe((void *)(addr - i)); |
| 481 | |
| 482 | if (p && kprobe_optready(p)) { |
| 483 | op = container_of(p, struct optimized_kprobe, kp); |
| 484 | if (arch_within_optimized_kprobe(op, addr)) |
| 485 | return p; |
| 486 | } |
| 487 | |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | /* Optimization staging list, protected by kprobe_mutex */ |
| 492 | static LIST_HEAD(optimizing_list); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 493 | static LIST_HEAD(unoptimizing_list); |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 494 | static LIST_HEAD(freeing_list); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 495 | |
| 496 | static void kprobe_optimizer(struct work_struct *work); |
| 497 | static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer); |
| 498 | #define OPTIMIZE_DELAY 5 |
| 499 | |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 500 | /* |
| 501 | * Optimize (replace a breakpoint with a jump) kprobes listed on |
| 502 | * optimizing_list. |
| 503 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 504 | static void do_optimize_kprobes(void) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 505 | { |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 506 | lockdep_assert_held(&text_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 507 | /* |
| 508 | * The optimization/unoptimization refers online_cpus via |
| 509 | * stop_machine() and cpu-hotplug modifies online_cpus. |
| 510 | * And same time, text_mutex will be held in cpu-hotplug and here. |
| 511 | * This combination can cause a deadlock (cpu-hotplug try to lock |
| 512 | * text_mutex but stop_machine can not be done because online_cpus |
| 513 | * has been changed) |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 514 | * To avoid this deadlock, caller must have locked cpu hotplug |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 515 | * for preventing cpu-hotplug outside of text_mutex locking. |
| 516 | */ |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 517 | lockdep_assert_cpus_held(); |
| 518 | |
| 519 | /* Optimization never be done when disarmed */ |
| 520 | if (kprobes_all_disarmed || !kprobes_allow_optimization || |
| 521 | list_empty(&optimizing_list)) |
| 522 | return; |
| 523 | |
Masami Hiramatsu | cd7ebe2 | 2010-12-03 18:54:28 +0900 | [diff] [blame] | 524 | arch_optimize_kprobes(&optimizing_list); |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 525 | } |
| 526 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 527 | /* |
| 528 | * Unoptimize (replace a jump with a breakpoint and remove the breakpoint |
| 529 | * if need) kprobes listed on unoptimizing_list. |
| 530 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 531 | static void do_unoptimize_kprobes(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 532 | { |
| 533 | struct optimized_kprobe *op, *tmp; |
| 534 | |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 535 | lockdep_assert_held(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 536 | /* See comment in do_optimize_kprobes() */ |
| 537 | lockdep_assert_cpus_held(); |
| 538 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 539 | /* Unoptimization must be done anytime */ |
| 540 | if (list_empty(&unoptimizing_list)) |
| 541 | return; |
| 542 | |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 543 | arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list); |
Masami Hiramatsu | f984ba4 | 2010-12-03 18:54:34 +0900 | [diff] [blame] | 544 | /* Loop free_list for disarming */ |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 545 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
Masami Hiramatsu | f66c044 | 2019-11-27 14:57:04 +0900 | [diff] [blame] | 546 | /* Switching from detour code to origin */ |
| 547 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 548 | /* Disarm probes if marked disabled */ |
| 549 | if (kprobe_disabled(&op->kp)) |
| 550 | arch_disarm_kprobe(&op->kp); |
| 551 | if (kprobe_unused(&op->kp)) { |
| 552 | /* |
| 553 | * Remove unused probes from hash list. After waiting |
| 554 | * for synchronization, these probes are reclaimed. |
| 555 | * (reclaiming is done by do_free_cleaned_kprobes.) |
| 556 | */ |
| 557 | hlist_del_rcu(&op->kp.hlist); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 558 | } else |
| 559 | list_del_init(&op->list); |
| 560 | } |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* Reclaim all kprobes on the free_list */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 564 | static void do_free_cleaned_kprobes(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 565 | { |
| 566 | struct optimized_kprobe *op, *tmp; |
| 567 | |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 568 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 569 | list_del_init(&op->list); |
Masami Hiramatsu | cbdd96f | 2018-09-11 19:21:09 +0900 | [diff] [blame] | 570 | if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) { |
| 571 | /* |
| 572 | * This must not happen, but if there is a kprobe |
| 573 | * still in use, keep it on kprobes hash list. |
| 574 | */ |
| 575 | continue; |
| 576 | } |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 577 | free_aggr_kprobe(&op->kp); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /* Start optimizer after OPTIMIZE_DELAY passed */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 582 | static void kick_kprobe_optimizer(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 583 | { |
Tejun Heo | ad72b3b | 2012-12-21 17:57:00 -0800 | [diff] [blame] | 584 | schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 585 | } |
| 586 | |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 587 | /* Kprobe jump optimizer */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 588 | static void kprobe_optimizer(struct work_struct *work) |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 589 | { |
Steven Rostedt | 72ef379 | 2012-06-05 19:28:14 +0900 | [diff] [blame] | 590 | mutex_lock(&kprobe_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 591 | cpus_read_lock(); |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 592 | mutex_lock(&text_mutex); |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 593 | |
| 594 | /* |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 595 | * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed) |
| 596 | * kprobes before waiting for quiesence period. |
| 597 | */ |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 598 | do_unoptimize_kprobes(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 599 | |
| 600 | /* |
Masami Hiramatsu | a30b85d | 2017-10-20 08:43:39 +0900 | [diff] [blame] | 601 | * Step 2: Wait for quiesence period to ensure all potentially |
| 602 | * preempted tasks to have normally scheduled. Because optprobe |
| 603 | * may modify multiple instructions, there is a chance that Nth |
| 604 | * instruction is preempted. In that case, such tasks can return |
| 605 | * to 2nd-Nth byte of jump instruction. This wait is for avoiding it. |
| 606 | * Note that on non-preemptive kernel, this is transparently converted |
| 607 | * to synchronoze_sched() to wait for all interrupts to have completed. |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 608 | */ |
Masami Hiramatsu | a30b85d | 2017-10-20 08:43:39 +0900 | [diff] [blame] | 609 | synchronize_rcu_tasks(); |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 610 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 611 | /* Step 3: Optimize kprobes after quiesence period */ |
Masami Hiramatsu | 61f4e13 | 2010-12-03 18:54:03 +0900 | [diff] [blame] | 612 | do_optimize_kprobes(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 613 | |
| 614 | /* Step 4: Free cleaned kprobes after quiesence period */ |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 615 | do_free_cleaned_kprobes(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 616 | |
Andrea Righi | f1c6ece | 2019-08-12 20:43:02 +0200 | [diff] [blame] | 617 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 618 | cpus_read_unlock(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 619 | |
Masami Hiramatsu | cd7ebe2 | 2010-12-03 18:54:28 +0900 | [diff] [blame] | 620 | /* Step 5: Kick optimizer again if needed */ |
Masami Hiramatsu | f984ba4 | 2010-12-03 18:54:34 +0900 | [diff] [blame] | 621 | if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) |
Masami Hiramatsu | cd7ebe2 | 2010-12-03 18:54:28 +0900 | [diff] [blame] | 622 | kick_kprobe_optimizer(); |
Masami Hiramatsu | 1a0aa99 | 2020-05-12 17:02:56 +0900 | [diff] [blame] | 623 | |
| 624 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* Wait for completing optimization and unoptimization */ |
Thomas Gleixner | 30e7d894 | 2017-05-17 10:19:49 +0200 | [diff] [blame] | 628 | void wait_for_kprobe_optimizer(void) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 629 | { |
Tejun Heo | ad72b3b | 2012-12-21 17:57:00 -0800 | [diff] [blame] | 630 | mutex_lock(&kprobe_mutex); |
| 631 | |
| 632 | while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) { |
| 633 | mutex_unlock(&kprobe_mutex); |
| 634 | |
| 635 | /* this will also make optimizing_work execute immmediately */ |
| 636 | flush_delayed_work(&optimizing_work); |
| 637 | /* @optimizing_work might not have been queued yet, relax */ |
| 638 | cpu_relax(); |
| 639 | |
| 640 | mutex_lock(&kprobe_mutex); |
| 641 | } |
| 642 | |
| 643 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 644 | } |
| 645 | |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 646 | static bool optprobe_queued_unopt(struct optimized_kprobe *op) |
| 647 | { |
| 648 | struct optimized_kprobe *_op; |
| 649 | |
| 650 | list_for_each_entry(_op, &unoptimizing_list, list) { |
| 651 | if (op == _op) |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | return false; |
| 656 | } |
| 657 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 658 | /* Optimize kprobe if p is ready to be optimized */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 659 | static void optimize_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 660 | { |
| 661 | struct optimized_kprobe *op; |
| 662 | |
| 663 | /* Check if the kprobe is disabled or not ready for optimization. */ |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 664 | if (!kprobe_optready(p) || !kprobes_allow_optimization || |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 665 | (kprobe_disabled(p) || kprobes_all_disarmed)) |
| 666 | return; |
| 667 | |
Masami Hiramatsu | 059053a | 2018-06-20 01:10:27 +0900 | [diff] [blame] | 668 | /* kprobes with post_handler can not be optimized */ |
| 669 | if (p->post_handler) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 670 | return; |
| 671 | |
| 672 | op = container_of(p, struct optimized_kprobe, kp); |
| 673 | |
| 674 | /* Check there is no other kprobes at the optimized instructions */ |
| 675 | if (arch_check_optimized_kprobe(op) < 0) |
| 676 | return; |
| 677 | |
| 678 | /* Check if it is already optimized. */ |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 679 | if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) { |
| 680 | if (optprobe_queued_unopt(op)) { |
| 681 | /* This is under unoptimizing. Just dequeue the probe */ |
| 682 | list_del_init(&op->list); |
| 683 | } |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 684 | return; |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 685 | } |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 686 | op->kp.flags |= KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 687 | |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 688 | /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */ |
| 689 | if (WARN_ON_ONCE(!list_empty(&op->list))) |
| 690 | return; |
| 691 | |
| 692 | list_add(&op->list, &optimizing_list); |
| 693 | kick_kprobe_optimizer(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* Short cut to direct unoptimizing */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 697 | static void force_unoptimize_kprobe(struct optimized_kprobe *op) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 698 | { |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 699 | lockdep_assert_cpus_held(); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 700 | arch_unoptimize_kprobe(op); |
Masami Hiramatsu | f66c044 | 2019-11-27 14:57:04 +0900 | [diff] [blame] | 701 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* Unoptimize a kprobe if p is optimized */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 705 | static void unoptimize_kprobe(struct kprobe *p, bool force) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 706 | { |
| 707 | struct optimized_kprobe *op; |
| 708 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 709 | if (!kprobe_aggrprobe(p) || kprobe_disarmed(p)) |
| 710 | return; /* This is not an optprobe nor optimized */ |
| 711 | |
| 712 | op = container_of(p, struct optimized_kprobe, kp); |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 713 | if (!kprobe_optimized(p)) |
| 714 | return; |
| 715 | |
| 716 | if (!list_empty(&op->list)) { |
| 717 | if (optprobe_queued_unopt(op)) { |
| 718 | /* Queued in unoptimizing queue */ |
| 719 | if (force) { |
| 720 | /* |
| 721 | * Forcibly unoptimize the kprobe here, and queue it |
| 722 | * in the freeing list for release afterwards. |
| 723 | */ |
| 724 | force_unoptimize_kprobe(op); |
| 725 | list_move(&op->list, &freeing_list); |
| 726 | } |
| 727 | } else { |
| 728 | /* Dequeue from the optimizing queue */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 729 | list_del_init(&op->list); |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 730 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 731 | } |
| 732 | return; |
| 733 | } |
| 734 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 735 | /* Optimized kprobe case */ |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 736 | if (force) { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 737 | /* Forcibly update the code: this is a special case */ |
| 738 | force_unoptimize_kprobe(op); |
Masami Hiramatsu | e4add24 | 2020-01-07 23:42:24 +0900 | [diff] [blame] | 739 | } else { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 740 | list_add(&op->list, &unoptimizing_list); |
| 741 | kick_kprobe_optimizer(); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 745 | /* Cancel unoptimizing for reusing */ |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 746 | static int reuse_unused_kprobe(struct kprobe *ap) |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 747 | { |
| 748 | struct optimized_kprobe *op; |
| 749 | |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 750 | /* |
| 751 | * Unused kprobe MUST be on the way of delayed unoptimizing (means |
| 752 | * there is still a relative jump) and disabled. |
| 753 | */ |
| 754 | op = container_of(ap, struct optimized_kprobe, kp); |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 755 | WARN_ON_ONCE(list_empty(&op->list)); |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 756 | /* Enable the probe again */ |
| 757 | ap->flags &= ~KPROBE_FLAG_DISABLED; |
| 758 | /* Optimize it again (remove from op->list) */ |
Masami Hiramatsu | 5f843ed | 2019-04-15 15:01:25 +0900 | [diff] [blame] | 759 | if (!kprobe_optready(ap)) |
| 760 | return -EINVAL; |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 761 | |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 762 | optimize_kprobe(ap); |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 763 | return 0; |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 764 | } |
| 765 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 766 | /* Remove optimized instructions */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 767 | static void kill_optimized_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 768 | { |
| 769 | struct optimized_kprobe *op; |
| 770 | |
| 771 | op = container_of(p, struct optimized_kprobe, kp); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 772 | if (!list_empty(&op->list)) |
| 773 | /* Dequeue from the (un)optimization queue */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 774 | list_del_init(&op->list); |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 775 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Masami Hiramatsu | 7b959fc | 2013-05-22 18:34:09 +0900 | [diff] [blame] | 776 | |
| 777 | if (kprobe_unused(p)) { |
| 778 | /* Enqueue if it is unused */ |
| 779 | list_add(&op->list, &freeing_list); |
| 780 | /* |
| 781 | * Remove unused probes from the hash list. After waiting |
| 782 | * for synchronization, this probe is reclaimed. |
| 783 | * (reclaiming is done by do_free_cleaned_kprobes().) |
| 784 | */ |
| 785 | hlist_del_rcu(&op->kp.hlist); |
| 786 | } |
| 787 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 788 | /* Don't touch the code, because it is already freed. */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 789 | arch_remove_optimized_kprobe(op); |
| 790 | } |
| 791 | |
Masami Hiramatsu | a460246 | 2017-04-19 18:22:25 +0530 | [diff] [blame] | 792 | static inline |
| 793 | void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p) |
| 794 | { |
| 795 | if (!kprobe_ftrace(p)) |
| 796 | arch_prepare_optimized_kprobe(op, p); |
| 797 | } |
| 798 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 799 | /* Try to prepare optimized instructions */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 800 | static void prepare_optimized_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 801 | { |
| 802 | struct optimized_kprobe *op; |
| 803 | |
| 804 | op = container_of(p, struct optimized_kprobe, kp); |
Masami Hiramatsu | a460246 | 2017-04-19 18:22:25 +0530 | [diff] [blame] | 805 | __prepare_optimized_kprobe(op, p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 806 | } |
| 807 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 808 | /* Allocate new optimized_kprobe and try to prepare optimized instructions */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 809 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 810 | { |
| 811 | struct optimized_kprobe *op; |
| 812 | |
| 813 | op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL); |
| 814 | if (!op) |
| 815 | return NULL; |
| 816 | |
| 817 | INIT_LIST_HEAD(&op->list); |
| 818 | op->kp.addr = p->addr; |
Masami Hiramatsu | a460246 | 2017-04-19 18:22:25 +0530 | [diff] [blame] | 819 | __prepare_optimized_kprobe(op, p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 820 | |
| 821 | return &op->kp; |
| 822 | } |
| 823 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 824 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 825 | |
| 826 | /* |
| 827 | * Prepare an optimized_kprobe and optimize it |
| 828 | * NOTE: p must be a normal registered kprobe |
| 829 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 830 | static void try_to_optimize_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 831 | { |
| 832 | struct kprobe *ap; |
| 833 | struct optimized_kprobe *op; |
| 834 | |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 835 | /* Impossible to optimize ftrace-based kprobe */ |
| 836 | if (kprobe_ftrace(p)) |
| 837 | return; |
| 838 | |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 839 | /* For preparing optimization, jump_label_text_reserved() is called */ |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 840 | cpus_read_lock(); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 841 | jump_label_lock(); |
| 842 | mutex_lock(&text_mutex); |
| 843 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 844 | ap = alloc_aggr_kprobe(p); |
| 845 | if (!ap) |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 846 | goto out; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 847 | |
| 848 | op = container_of(ap, struct optimized_kprobe, kp); |
| 849 | if (!arch_prepared_optinsn(&op->optinsn)) { |
| 850 | /* If failed to setup optimizing, fallback to kprobe */ |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 851 | arch_remove_optimized_kprobe(op); |
| 852 | kfree(op); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 853 | goto out; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | init_aggr_kprobe(ap, p); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 857 | optimize_kprobe(ap); /* This just kicks optimizer thread */ |
| 858 | |
| 859 | out: |
| 860 | mutex_unlock(&text_mutex); |
| 861 | jump_label_unlock(); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 862 | cpus_read_unlock(); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 863 | } |
| 864 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 865 | static void optimize_all_kprobes(void) |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 866 | { |
| 867 | struct hlist_head *head; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 868 | struct kprobe *p; |
| 869 | unsigned int i; |
| 870 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 871 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 872 | /* If optimization is already allowed, just return */ |
| 873 | if (kprobes_allow_optimization) |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 874 | goto out; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 875 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 876 | cpus_read_lock(); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 877 | kprobes_allow_optimization = true; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 878 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 879 | head = &kprobe_table[i]; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 880 | hlist_for_each_entry(p, head, hlist) |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 881 | if (!kprobe_disabled(p)) |
| 882 | optimize_kprobe(p); |
| 883 | } |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 884 | cpus_read_unlock(); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 885 | printk(KERN_INFO "Kprobes globally optimized\n"); |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 886 | out: |
| 887 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 888 | } |
| 889 | |
Masami Hiramatsu | c85c9a2 | 2021-02-18 23:29:23 +0900 | [diff] [blame] | 890 | #ifdef CONFIG_SYSCTL |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 891 | static void unoptimize_all_kprobes(void) |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 892 | { |
| 893 | struct hlist_head *head; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 894 | struct kprobe *p; |
| 895 | unsigned int i; |
| 896 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 897 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 898 | /* If optimization is already prohibited, just return */ |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 899 | if (!kprobes_allow_optimization) { |
| 900 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 901 | return; |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 902 | } |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 903 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 904 | cpus_read_lock(); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 905 | kprobes_allow_optimization = false; |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 906 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 907 | head = &kprobe_table[i]; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 908 | hlist_for_each_entry(p, head, hlist) { |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 909 | if (!kprobe_disabled(p)) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 910 | unoptimize_kprobe(p, false); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 911 | } |
| 912 | } |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 913 | cpus_read_unlock(); |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 914 | mutex_unlock(&kprobe_mutex); |
| 915 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 916 | /* Wait for unoptimizing completion */ |
| 917 | wait_for_kprobe_optimizer(); |
| 918 | printk(KERN_INFO "Kprobes globally unoptimized\n"); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 919 | } |
| 920 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 921 | static DEFINE_MUTEX(kprobe_sysctl_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 922 | int sysctl_kprobes_optimization; |
| 923 | int proc_kprobes_optimization_handler(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 924 | void *buffer, size_t *length, |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 925 | loff_t *ppos) |
| 926 | { |
| 927 | int ret; |
| 928 | |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 929 | mutex_lock(&kprobe_sysctl_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 930 | sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0; |
| 931 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 932 | |
| 933 | if (sysctl_kprobes_optimization) |
| 934 | optimize_all_kprobes(); |
| 935 | else |
| 936 | unoptimize_all_kprobes(); |
Masami Hiramatsu | 5c51543 | 2013-04-18 18:33:18 +0900 | [diff] [blame] | 937 | mutex_unlock(&kprobe_sysctl_mutex); |
Masami Hiramatsu | b2be84d | 2010-02-25 08:34:15 -0500 | [diff] [blame] | 938 | |
| 939 | return ret; |
| 940 | } |
| 941 | #endif /* CONFIG_SYSCTL */ |
| 942 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 943 | /* Put a breakpoint for a probe. Must be called with text_mutex locked */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 944 | static void __arm_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 945 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 946 | struct kprobe *_p; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 947 | |
| 948 | /* Check collision with other optimized kprobes */ |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 949 | _p = get_optimized_kprobe((unsigned long)p->addr); |
| 950 | if (unlikely(_p)) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 951 | /* Fallback to unoptimized kprobe */ |
| 952 | unoptimize_kprobe(_p, true); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 953 | |
| 954 | arch_arm_kprobe(p); |
| 955 | optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */ |
| 956 | } |
| 957 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 958 | /* Remove the breakpoint of a probe. Must be called with text_mutex locked */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 959 | static void __disarm_kprobe(struct kprobe *p, bool reopt) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 960 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 961 | struct kprobe *_p; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 962 | |
Wang Nan | 69d54b9 | 2015-02-13 14:40:26 -0800 | [diff] [blame] | 963 | /* Try to unoptimize */ |
| 964 | unoptimize_kprobe(p, kprobes_all_disarmed); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 965 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 966 | if (!kprobe_queued(p)) { |
| 967 | arch_disarm_kprobe(p); |
| 968 | /* If another kprobe was blocked, optimize it. */ |
| 969 | _p = get_optimized_kprobe((unsigned long)p->addr); |
| 970 | if (unlikely(_p) && reopt) |
| 971 | optimize_kprobe(_p); |
| 972 | } |
| 973 | /* TODO: reoptimize others after unoptimized this probe */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | #else /* !CONFIG_OPTPROBES */ |
| 977 | |
| 978 | #define optimize_kprobe(p) do {} while (0) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 979 | #define unoptimize_kprobe(p, f) do {} while (0) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 980 | #define kill_optimized_kprobe(p) do {} while (0) |
| 981 | #define prepare_optimized_kprobe(p) do {} while (0) |
| 982 | #define try_to_optimize_kprobe(p) do {} while (0) |
| 983 | #define __arm_kprobe(p) arch_arm_kprobe(p) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 984 | #define __disarm_kprobe(p, o) arch_disarm_kprobe(p) |
| 985 | #define kprobe_disarmed(p) kprobe_disabled(p) |
| 986 | #define wait_for_kprobe_optimizer() do {} while (0) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 987 | |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 988 | static int reuse_unused_kprobe(struct kprobe *ap) |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 989 | { |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 990 | /* |
| 991 | * If the optimized kprobe is NOT supported, the aggr kprobe is |
| 992 | * released at the same time that the last aggregated kprobe is |
| 993 | * unregistered. |
| 994 | * Thus there should be no chance to reuse unused kprobe. |
| 995 | */ |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 996 | printk(KERN_ERR "Error: There should be no unused kprobe here.\n"); |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 997 | return -EINVAL; |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 998 | } |
| 999 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1000 | static void free_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1001 | { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1002 | arch_remove_kprobe(p); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1003 | kfree(p); |
| 1004 | } |
| 1005 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1006 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1007 | { |
| 1008 | return kzalloc(sizeof(struct kprobe), GFP_KERNEL); |
| 1009 | } |
| 1010 | #endif /* CONFIG_OPTPROBES */ |
| 1011 | |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1012 | #ifdef CONFIG_KPROBES_ON_FTRACE |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1013 | static struct ftrace_ops kprobe_ftrace_ops __read_mostly = { |
Masami Hiramatsu | e525389 | 2012-06-05 19:28:38 +0900 | [diff] [blame] | 1014 | .func = kprobe_ftrace_handler, |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1015 | .flags = FTRACE_OPS_FL_SAVE_REGS, |
| 1016 | }; |
| 1017 | |
| 1018 | static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = { |
| 1019 | .func = kprobe_ftrace_handler, |
Masami Hiramatsu | 1d70be3 | 2014-11-21 05:25:23 -0500 | [diff] [blame] | 1020 | .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY, |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1021 | }; |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1022 | |
| 1023 | static int kprobe_ipmodify_enabled; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1024 | static int kprobe_ftrace_enabled; |
| 1025 | |
| 1026 | /* Must ensure p->addr is really on ftrace */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1027 | static int prepare_kprobe(struct kprobe *p) |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1028 | { |
| 1029 | if (!kprobe_ftrace(p)) |
| 1030 | return arch_prepare_kprobe(p); |
| 1031 | |
| 1032 | return arch_prepare_kprobe_ftrace(p); |
| 1033 | } |
| 1034 | |
| 1035 | /* Caller must lock kprobe_mutex */ |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1036 | static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
| 1037 | int *cnt) |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1038 | { |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1039 | int ret = 0; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1040 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1041 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1042 | if (ret) { |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 1043 | pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n", |
| 1044 | p->addr, ret); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1045 | return ret; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1046 | } |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1047 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1048 | if (*cnt == 0) { |
| 1049 | ret = register_ftrace_function(ops); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1050 | if (ret) { |
| 1051 | pr_debug("Failed to init kprobe-ftrace (%d)\n", ret); |
| 1052 | goto err_ftrace; |
| 1053 | } |
| 1054 | } |
| 1055 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1056 | (*cnt)++; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1057 | return ret; |
| 1058 | |
| 1059 | err_ftrace: |
| 1060 | /* |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1061 | * At this point, sinec ops is not registered, we should be sefe from |
| 1062 | * registering empty filter. |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1063 | */ |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1064 | ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1065 | return ret; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1066 | } |
| 1067 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1068 | static int arm_kprobe_ftrace(struct kprobe *p) |
| 1069 | { |
| 1070 | bool ipmodify = (p->post_handler != NULL); |
| 1071 | |
| 1072 | return __arm_kprobe_ftrace(p, |
| 1073 | ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, |
| 1074 | ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); |
| 1075 | } |
| 1076 | |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1077 | /* Caller must lock kprobe_mutex */ |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1078 | static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
| 1079 | int *cnt) |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1080 | { |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1081 | int ret = 0; |
| 1082 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1083 | if (*cnt == 1) { |
| 1084 | ret = unregister_ftrace_function(ops); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1085 | if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret)) |
| 1086 | return ret; |
| 1087 | } |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1088 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1089 | (*cnt)--; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1090 | |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1091 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 1092 | WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n", |
| 1093 | p->addr, ret); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1094 | return ret; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1095 | } |
Masami Hiramatsu | 0bc11ed | 2019-07-25 15:24:37 +0900 | [diff] [blame] | 1096 | |
| 1097 | static int disarm_kprobe_ftrace(struct kprobe *p) |
| 1098 | { |
| 1099 | bool ipmodify = (p->post_handler != NULL); |
| 1100 | |
| 1101 | return __disarm_kprobe_ftrace(p, |
| 1102 | ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, |
| 1103 | ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); |
| 1104 | } |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1105 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
Muchun Song | 10de795 | 2020-08-06 01:20:46 +0800 | [diff] [blame] | 1106 | static inline int prepare_kprobe(struct kprobe *p) |
| 1107 | { |
| 1108 | return arch_prepare_kprobe(p); |
| 1109 | } |
| 1110 | |
| 1111 | static inline int arm_kprobe_ftrace(struct kprobe *p) |
| 1112 | { |
| 1113 | return -ENODEV; |
| 1114 | } |
| 1115 | |
| 1116 | static inline int disarm_kprobe_ftrace(struct kprobe *p) |
| 1117 | { |
| 1118 | return -ENODEV; |
| 1119 | } |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1120 | #endif |
| 1121 | |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1122 | /* Arm a kprobe with text_mutex */ |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1123 | static int arm_kprobe(struct kprobe *kp) |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1124 | { |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1125 | if (unlikely(kprobe_ftrace(kp))) |
| 1126 | return arm_kprobe_ftrace(kp); |
| 1127 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1128 | cpus_read_lock(); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1129 | mutex_lock(&text_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1130 | __arm_kprobe(kp); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1131 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1132 | cpus_read_unlock(); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1133 | |
| 1134 | return 0; |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | /* Disarm a kprobe with text_mutex */ |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1138 | static int disarm_kprobe(struct kprobe *kp, bool reopt) |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1139 | { |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1140 | if (unlikely(kprobe_ftrace(kp))) |
| 1141 | return disarm_kprobe_ftrace(kp); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1142 | |
| 1143 | cpus_read_lock(); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1144 | mutex_lock(&text_mutex); |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1145 | __disarm_kprobe(kp, reopt); |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1146 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1147 | cpus_read_unlock(); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1148 | |
| 1149 | return 0; |
Masami Hiramatsu | 201517a | 2009-05-07 16:31:26 -0400 | [diff] [blame] | 1150 | } |
| 1151 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1152 | /* |
| 1153 | * Aggregate handlers for multiple kprobes support - these handlers |
| 1154 | * take care of invoking the individual kprobe handlers on p->list |
| 1155 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1156 | 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] | 1157 | { |
| 1158 | struct kprobe *kp; |
| 1159 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1160 | list_for_each_entry_rcu(kp, &p->list, list) { |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1161 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1162 | set_kprobe_instance(kp); |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1163 | if (kp->pre_handler(kp, regs)) |
| 1164 | return 1; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1165 | } |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1166 | reset_kprobe_instance(); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1167 | } |
| 1168 | return 0; |
| 1169 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1170 | NOKPROBE_SYMBOL(aggr_pre_handler); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1171 | |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1172 | static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, |
| 1173 | unsigned long flags) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1174 | { |
| 1175 | struct kprobe *kp; |
| 1176 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1177 | list_for_each_entry_rcu(kp, &p->list, list) { |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1178 | if (kp->post_handler && likely(!kprobe_disabled(kp))) { |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1179 | set_kprobe_instance(kp); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1180 | kp->post_handler(kp, regs, flags); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1181 | reset_kprobe_instance(); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1182 | } |
| 1183 | } |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1184 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1185 | NOKPROBE_SYMBOL(aggr_post_handler); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1186 | |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1187 | /* Walks the list and increments nmissed count for multiprobe case */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1188 | void kprobes_inc_nmissed_count(struct kprobe *p) |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1189 | { |
| 1190 | struct kprobe *kp; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1191 | if (!kprobe_aggrprobe(p)) { |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1192 | p->nmissed++; |
| 1193 | } else { |
| 1194 | list_for_each_entry_rcu(kp, &p->list, list) |
| 1195 | kp->nmissed++; |
| 1196 | } |
| 1197 | return; |
| 1198 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1199 | NOKPROBE_SYMBOL(kprobes_inc_nmissed_count); |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 1200 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1201 | static void free_rp_inst_rcu(struct rcu_head *head) |
| 1202 | { |
| 1203 | struct kretprobe_instance *ri = container_of(head, struct kretprobe_instance, rcu); |
| 1204 | |
| 1205 | if (refcount_dec_and_test(&ri->rph->ref)) |
| 1206 | kfree(ri->rph); |
| 1207 | kfree(ri); |
| 1208 | } |
| 1209 | NOKPROBE_SYMBOL(free_rp_inst_rcu); |
| 1210 | |
Masami Hiramatsu | b338817 | 2020-08-29 22:02:47 +0900 | [diff] [blame] | 1211 | static void recycle_rp_inst(struct kretprobe_instance *ri) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1212 | { |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1213 | struct kretprobe *rp = get_kretprobe(ri); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1214 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1215 | if (likely(rp)) { |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1216 | freelist_add(&ri->freelist, &rp->freelist); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1217 | } else |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1218 | call_rcu(&ri->rcu, free_rp_inst_rcu); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1219 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1220 | NOKPROBE_SYMBOL(recycle_rp_inst); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1221 | |
Masami Hiramatsu | 319f0ce | 2020-08-29 22:03:02 +0900 | [diff] [blame] | 1222 | static struct kprobe kprobe_busy = { |
Jiri Olsa | 9b38cc7 | 2020-05-12 17:03:18 +0900 | [diff] [blame] | 1223 | .addr = (void *) get_kprobe, |
| 1224 | }; |
| 1225 | |
| 1226 | void kprobe_busy_begin(void) |
| 1227 | { |
| 1228 | struct kprobe_ctlblk *kcb; |
| 1229 | |
| 1230 | preempt_disable(); |
| 1231 | __this_cpu_write(current_kprobe, &kprobe_busy); |
| 1232 | kcb = get_kprobe_ctlblk(); |
| 1233 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
| 1234 | } |
| 1235 | |
| 1236 | void kprobe_busy_end(void) |
| 1237 | { |
| 1238 | __this_cpu_write(current_kprobe, NULL); |
| 1239 | preempt_enable(); |
| 1240 | } |
| 1241 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1242 | /* |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 1243 | * This function is called from finish_task_switch when task tk becomes dead, |
| 1244 | * so that we can recycle any function-return probe instances associated |
| 1245 | * with this task. These left over instances represent probed functions |
| 1246 | * that have been called but will never return. |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1247 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1248 | void kprobe_flush_task(struct task_struct *tk) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1249 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 1250 | struct kretprobe_instance *ri; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1251 | struct llist_node *node; |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 1252 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1253 | /* Early boot, not yet initialized. */ |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1254 | if (unlikely(!kprobes_initialized)) |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1255 | return; |
| 1256 | |
Jiri Olsa | 9b38cc7 | 2020-05-12 17:03:18 +0900 | [diff] [blame] | 1257 | kprobe_busy_begin(); |
| 1258 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1259 | node = __llist_del_all(&tk->kretprobe_instances); |
| 1260 | while (node) { |
| 1261 | ri = container_of(node, struct kretprobe_instance, llist); |
| 1262 | node = node->next; |
| 1263 | |
| 1264 | recycle_rp_inst(ri); |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 1265 | } |
Jiri Olsa | 9b38cc7 | 2020-05-12 17:03:18 +0900 | [diff] [blame] | 1266 | |
| 1267 | kprobe_busy_end(); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1268 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1269 | NOKPROBE_SYMBOL(kprobe_flush_task); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1270 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1271 | static inline void free_rp_inst(struct kretprobe *rp) |
| 1272 | { |
| 1273 | struct kretprobe_instance *ri; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1274 | struct freelist_node *node; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1275 | int count = 0; |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 1276 | |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1277 | node = rp->freelist.head; |
| 1278 | while (node) { |
| 1279 | ri = container_of(node, struct kretprobe_instance, freelist); |
| 1280 | node = node->next; |
| 1281 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1282 | kfree(ri); |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1283 | count++; |
| 1284 | } |
| 1285 | |
| 1286 | if (refcount_sub_and_test(count, &rp->rph->ref)) { |
| 1287 | kfree(rp->rph); |
| 1288 | rp->rph = NULL; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
Masami Hiramatsu | 059053a | 2018-06-20 01:10:27 +0900 | [diff] [blame] | 1292 | /* Add the new probe to ap->list */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1293 | static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1294 | { |
Masami Hiramatsu | 059053a | 2018-06-20 01:10:27 +0900 | [diff] [blame] | 1295 | if (p->post_handler) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1296 | unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1297 | |
Masami Hiramatsu | 059053a | 2018-06-20 01:10:27 +0900 | [diff] [blame] | 1298 | list_add_rcu(&p->list, &ap->list); |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1299 | if (p->post_handler && !ap->post_handler) |
| 1300 | ap->post_handler = aggr_post_handler; |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1301 | |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | /* |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1306 | * Fill in the required fields of the "manager kprobe". Replace the |
| 1307 | * earlier kprobe in the hlist with the manager kprobe |
| 1308 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1309 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1310 | { |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1311 | /* Copy p's insn slot to ap */ |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 1312 | copy_kprobe(p, ap); |
bibo, mao | a9ad965 | 2006-07-30 03:03:26 -0700 | [diff] [blame] | 1313 | flush_insn_slot(ap); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1314 | ap->addr = p->addr; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1315 | ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1316 | ap->pre_handler = aggr_pre_handler; |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1317 | /* We don't care the kprobe which has gone. */ |
| 1318 | if (p->post_handler && !kprobe_gone(p)) |
mao, bibo | 3672165 | 2006-06-26 00:25:22 -0700 | [diff] [blame] | 1319 | ap->post_handler = aggr_post_handler; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1320 | |
| 1321 | INIT_LIST_HEAD(&ap->list); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1322 | INIT_HLIST_NODE(&ap->hlist); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1323 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1324 | list_add_rcu(&p->list, &ap->list); |
Keshavamurthy Anil S | adad0f3 | 2005-12-12 00:37:12 -0800 | [diff] [blame] | 1325 | hlist_replace_rcu(&p->hlist, &ap->hlist); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * This is the second or subsequent kprobe at the address - handle |
| 1330 | * the intricacies |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1331 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1332 | 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] | 1333 | { |
| 1334 | int ret = 0; |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1335 | struct kprobe *ap = orig_p; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1336 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1337 | cpus_read_lock(); |
| 1338 | |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1339 | /* For preparing optimization, jump_label_text_reserved() is called */ |
| 1340 | jump_label_lock(); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1341 | mutex_lock(&text_mutex); |
| 1342 | |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1343 | if (!kprobe_aggrprobe(orig_p)) { |
| 1344 | /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */ |
| 1345 | ap = alloc_aggr_kprobe(orig_p); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1346 | if (!ap) { |
| 1347 | ret = -ENOMEM; |
| 1348 | goto out; |
| 1349 | } |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1350 | init_aggr_kprobe(ap, orig_p); |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 1351 | } else if (kprobe_unused(ap)) { |
Masami Hiramatsu | 0490cd1 | 2010-12-03 18:54:16 +0900 | [diff] [blame] | 1352 | /* This probe is going to die. Rescue it */ |
Masami Hiramatsu | 819319f | 2018-09-11 19:20:40 +0900 | [diff] [blame] | 1353 | ret = reuse_unused_kprobe(ap); |
| 1354 | if (ret) |
| 1355 | goto out; |
| 1356 | } |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1357 | |
| 1358 | if (kprobe_gone(ap)) { |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1359 | /* |
| 1360 | * Attempting to insert new probe at the same location that |
| 1361 | * had a probe in the module vaddr area which already |
| 1362 | * freed. So, the instruction slot has already been |
| 1363 | * released. We need a new slot for the new probe. |
| 1364 | */ |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1365 | ret = arch_prepare_kprobe(ap); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1366 | if (ret) |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1367 | /* |
| 1368 | * Even if fail to allocate new slot, don't need to |
| 1369 | * free aggr_probe. It will be used next time, or |
| 1370 | * freed by unregister_kprobe. |
| 1371 | */ |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1372 | goto out; |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1373 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1374 | /* Prepare optimized instructions if possible. */ |
| 1375 | prepare_optimized_kprobe(ap); |
| 1376 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1377 | /* |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1378 | * Clear gone flag to prevent allocating new slot again, and |
| 1379 | * set disabled flag because it is not armed yet. |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1380 | */ |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1381 | ap->flags = (ap->flags & ~KPROBE_FLAG_GONE) |
| 1382 | | KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1383 | } |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1384 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1385 | /* Copy ap's insn slot to p */ |
Masami Hiramatsu | b918e5e | 2009-04-06 19:00:58 -0700 | [diff] [blame] | 1386 | copy_kprobe(ap, p); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1387 | ret = add_new_kprobe(ap, p); |
| 1388 | |
| 1389 | out: |
| 1390 | mutex_unlock(&text_mutex); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1391 | jump_label_unlock(); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1392 | cpus_read_unlock(); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1393 | |
| 1394 | if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) { |
| 1395 | ap->flags &= ~KPROBE_FLAG_DISABLED; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1396 | if (!kprobes_all_disarmed) { |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1397 | /* Arm the breakpoint again. */ |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1398 | ret = arm_kprobe(ap); |
| 1399 | if (ret) { |
| 1400 | ap->flags |= KPROBE_FLAG_DISABLED; |
| 1401 | list_del_rcu(&p->list); |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 1402 | synchronize_rcu(); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1403 | } |
| 1404 | } |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1405 | } |
| 1406 | return ret; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
Masami Hiramatsu | be8f274 | 2014-04-17 17:16:58 +0900 | [diff] [blame] | 1409 | bool __weak arch_within_kprobe_blacklist(unsigned long addr) |
| 1410 | { |
| 1411 | /* The __kprobes marked functions and entry code must not be probed */ |
| 1412 | return addr >= (unsigned long)__kprobes_text_start && |
| 1413 | addr < (unsigned long)__kprobes_text_end; |
| 1414 | } |
| 1415 | |
Masami Hiramatsu | 6143c6f | 2019-02-13 01:13:12 +0900 | [diff] [blame] | 1416 | static bool __within_kprobe_blacklist(unsigned long addr) |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 1417 | { |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1418 | struct kprobe_blacklist_entry *ent; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1419 | |
Masami Hiramatsu | be8f274 | 2014-04-17 17:16:58 +0900 | [diff] [blame] | 1420 | if (arch_within_kprobe_blacklist(addr)) |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1421 | return true; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1422 | /* |
| 1423 | * If there exists a kprobe_blacklist, verify and |
| 1424 | * fail any probe registration in the prohibited area |
| 1425 | */ |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1426 | list_for_each_entry(ent, &kprobe_blacklist, list) { |
| 1427 | if (addr >= ent->start_addr && addr < ent->end_addr) |
| 1428 | return true; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1429 | } |
Masami Hiramatsu | 6143c6f | 2019-02-13 01:13:12 +0900 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1432 | |
Masami Hiramatsu | 6143c6f | 2019-02-13 01:13:12 +0900 | [diff] [blame] | 1433 | bool within_kprobe_blacklist(unsigned long addr) |
| 1434 | { |
| 1435 | char symname[KSYM_NAME_LEN], *p; |
| 1436 | |
| 1437 | if (__within_kprobe_blacklist(addr)) |
| 1438 | return true; |
| 1439 | |
| 1440 | /* Check if the address is on a suffixed-symbol */ |
| 1441 | if (!lookup_symbol_name(addr, symname)) { |
| 1442 | p = strchr(symname, '.'); |
| 1443 | if (!p) |
| 1444 | return false; |
| 1445 | *p = '\0'; |
| 1446 | addr = (unsigned long)kprobe_lookup_name(symname, 0); |
| 1447 | if (addr) |
| 1448 | return __within_kprobe_blacklist(addr); |
| 1449 | } |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1450 | return false; |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1453 | /* |
| 1454 | * If we have a symbol_name argument, look it up and add the offset field |
| 1455 | * 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] | 1456 | * This returns encoded errors if it fails to look up symbol or invalid |
| 1457 | * combination of parameters. |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1458 | */ |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1459 | static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, |
| 1460 | const char *symbol_name, unsigned int offset) |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1461 | { |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1462 | if ((symbol_name && addr) || (!symbol_name && !addr)) |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1463 | goto invalid; |
| 1464 | |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1465 | if (symbol_name) { |
Linus Torvalds | 7246f60 | 2017-05-05 11:36:44 -0700 | [diff] [blame] | 1466 | addr = kprobe_lookup_name(symbol_name, offset); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1467 | if (!addr) |
| 1468 | return ERR_PTR(-ENOENT); |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1471 | addr = (kprobe_opcode_t *)(((char *)addr) + offset); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1472 | if (addr) |
| 1473 | return addr; |
| 1474 | |
| 1475 | invalid: |
| 1476 | return ERR_PTR(-EINVAL); |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1479 | static kprobe_opcode_t *kprobe_addr(struct kprobe *p) |
| 1480 | { |
| 1481 | return _kprobe_addr(p->addr, p->symbol_name, p->offset); |
| 1482 | } |
| 1483 | |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1484 | /* Check passed kprobe is valid and return kprobe in kprobe_table. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1485 | static struct kprobe *__get_valid_kprobe(struct kprobe *p) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1486 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1487 | struct kprobe *ap, *list_p; |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1488 | |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1489 | lockdep_assert_held(&kprobe_mutex); |
| 1490 | |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1491 | ap = get_kprobe(p->addr); |
| 1492 | if (unlikely(!ap)) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1493 | return NULL; |
| 1494 | |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1495 | if (p != ap) { |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1496 | list_for_each_entry(list_p, &ap->list, list) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1497 | if (list_p == p) |
| 1498 | /* kprobe p is a valid probe */ |
| 1499 | goto valid; |
| 1500 | return NULL; |
| 1501 | } |
| 1502 | valid: |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1503 | return ap; |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1504 | } |
| 1505 | |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 1506 | /* |
| 1507 | * Warn and return error if the kprobe is being re-registered since |
| 1508 | * there must be a software bug. |
| 1509 | */ |
| 1510 | static inline int warn_kprobe_rereg(struct kprobe *p) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1511 | { |
| 1512 | int ret = 0; |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1513 | |
| 1514 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 1515 | if (WARN_ON_ONCE(__get_valid_kprobe(p))) |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1516 | ret = -EINVAL; |
| 1517 | mutex_unlock(&kprobe_mutex); |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1518 | |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1519 | return ret; |
| 1520 | } |
| 1521 | |
Heiko Carstens | f7f242f | 2014-10-15 12:17:34 +0200 | [diff] [blame] | 1522 | int __weak arch_check_ftrace_location(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | { |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1524 | unsigned long ftrace_addr; |
| 1525 | |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1526 | ftrace_addr = ftrace_location((unsigned long)p->addr); |
| 1527 | if (ftrace_addr) { |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1528 | #ifdef CONFIG_KPROBES_ON_FTRACE |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1529 | /* Given address is not on the instruction boundary */ |
| 1530 | if ((unsigned long)p->addr != ftrace_addr) |
| 1531 | return -EILSEQ; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1532 | p->flags |= KPROBE_FLAG_FTRACE; |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 1533 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1534 | return -EINVAL; |
| 1535 | #endif |
| 1536 | } |
Heiko Carstens | f7f242f | 2014-10-15 12:17:34 +0200 | [diff] [blame] | 1537 | return 0; |
| 1538 | } |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1539 | |
Heiko Carstens | f7f242f | 2014-10-15 12:17:34 +0200 | [diff] [blame] | 1540 | static int check_kprobe_address_safe(struct kprobe *p, |
| 1541 | struct module **probed_mod) |
| 1542 | { |
| 1543 | int ret; |
| 1544 | |
| 1545 | ret = arch_check_ftrace_location(p); |
| 1546 | if (ret) |
| 1547 | return ret; |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1548 | jump_label_lock(); |
| 1549 | preempt_disable(); |
| 1550 | |
| 1551 | /* Ensure it is not in reserved area nor out of text */ |
| 1552 | if (!kernel_text_address((unsigned long) p->addr) || |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 1553 | within_kprobe_blacklist((unsigned long) p->addr) || |
Masami Hiramatsu | e336b40 | 2019-09-03 20:08:21 +0900 | [diff] [blame] | 1554 | jump_label_text_reserved(p->addr, p->addr) || |
Peter Zijlstra | fa68bd0 | 2021-06-28 13:24:12 +0200 | [diff] [blame^] | 1555 | static_call_text_reserved(p->addr, p->addr) || |
Masami Hiramatsu | e336b40 | 2019-09-03 20:08:21 +0900 | [diff] [blame] | 1556 | find_bug((unsigned long)p->addr)) { |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1557 | ret = -EINVAL; |
| 1558 | goto out; |
| 1559 | } |
| 1560 | |
| 1561 | /* Check if are we probing a module */ |
| 1562 | *probed_mod = __module_text_address((unsigned long) p->addr); |
| 1563 | if (*probed_mod) { |
| 1564 | /* |
| 1565 | * We must hold a refcount of the probed module while updating |
| 1566 | * its code to prohibit unexpected unloading. |
| 1567 | */ |
| 1568 | if (unlikely(!try_module_get(*probed_mod))) { |
| 1569 | ret = -ENOENT; |
| 1570 | goto out; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * If the module freed .init.text, we couldn't insert |
| 1575 | * kprobes in there. |
| 1576 | */ |
| 1577 | if (within_module_init((unsigned long)p->addr, *probed_mod) && |
| 1578 | (*probed_mod)->state != MODULE_STATE_COMING) { |
| 1579 | module_put(*probed_mod); |
| 1580 | *probed_mod = NULL; |
| 1581 | ret = -ENOENT; |
| 1582 | } |
| 1583 | } |
| 1584 | out: |
| 1585 | preempt_enable(); |
| 1586 | jump_label_unlock(); |
| 1587 | |
| 1588 | return ret; |
| 1589 | } |
| 1590 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1591 | int register_kprobe(struct kprobe *p) |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1592 | { |
| 1593 | int ret; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1594 | struct kprobe *old_p; |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 1595 | struct module *probed_mod; |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1596 | kprobe_opcode_t *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1598 | /* Adjust probe address from symbol */ |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1599 | addr = kprobe_addr(p); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1600 | if (IS_ERR(addr)) |
| 1601 | return PTR_ERR(addr); |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1602 | p->addr = addr; |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 1603 | |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 1604 | ret = warn_kprobe_rereg(p); |
Ananth N Mavinakayanahalli | 1f0ab40 | 2009-09-15 10:43:07 +0530 | [diff] [blame] | 1605 | if (ret) |
| 1606 | return ret; |
| 1607 | |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 1608 | /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ |
| 1609 | p->flags &= KPROBE_FLAG_DISABLED; |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1610 | p->nmissed = 0; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1611 | INIT_LIST_HEAD(&p->list); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1612 | |
Masami Hiramatsu | f7fa6ef0 | 2012-06-05 19:28:20 +0900 | [diff] [blame] | 1613 | ret = check_kprobe_address_safe(p, &probed_mod); |
| 1614 | if (ret) |
| 1615 | return ret; |
| 1616 | |
| 1617 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1618 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1619 | old_p = get_kprobe(p->addr); |
| 1620 | if (old_p) { |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1621 | /* Since this may unoptimize old_p, locking text_mutex. */ |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1622 | ret = register_aggr_kprobe(old_p, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | goto out; |
| 1624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1626 | cpus_read_lock(); |
| 1627 | /* Prevent text modification */ |
| 1628 | mutex_lock(&text_mutex); |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 1629 | ret = prepare_kprobe(p); |
Masami Hiramatsu | 2576428 | 2012-06-05 19:28:26 +0900 | [diff] [blame] | 1630 | mutex_unlock(&text_mutex); |
Thomas Gleixner | 2d1e38f | 2017-05-24 10:15:36 +0200 | [diff] [blame] | 1631 | cpus_read_unlock(); |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 1632 | if (ret) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1633 | goto out; |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1634 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1635 | INIT_HLIST_NODE(&p->hlist); |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 1636 | hlist_add_head_rcu(&p->hlist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
| 1638 | |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1639 | if (!kprobes_all_disarmed && !kprobe_disabled(p)) { |
| 1640 | ret = arm_kprobe(p); |
| 1641 | if (ret) { |
| 1642 | hlist_del_rcu(&p->hlist); |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 1643 | synchronize_rcu(); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 1644 | goto out; |
| 1645 | } |
| 1646 | } |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 1647 | |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 1648 | /* Try to optimize kprobe */ |
| 1649 | try_to_optimize_kprobe(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | out: |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 1651 | mutex_unlock(&kprobe_mutex); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1652 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1653 | if (probed_mod) |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 1654 | module_put(probed_mod); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | return ret; |
| 1657 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1658 | EXPORT_SYMBOL_GPL(register_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1660 | /* Check if all probes on the aggrprobe are disabled */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1661 | static int aggr_kprobe_disabled(struct kprobe *ap) |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1662 | { |
| 1663 | struct kprobe *kp; |
| 1664 | |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1665 | lockdep_assert_held(&kprobe_mutex); |
| 1666 | |
| 1667 | list_for_each_entry(kp, &ap->list, list) |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1668 | if (!kprobe_disabled(kp)) |
| 1669 | /* |
| 1670 | * There is an active probe on the list. |
| 1671 | * We can't disable this ap. |
| 1672 | */ |
| 1673 | return 0; |
| 1674 | |
| 1675 | return 1; |
| 1676 | } |
| 1677 | |
| 1678 | /* Disable one kprobe: Make sure called under kprobe_mutex is locked */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1679 | static struct kprobe *__disable_kprobe(struct kprobe *p) |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1680 | { |
| 1681 | struct kprobe *orig_p; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1682 | int ret; |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1683 | |
| 1684 | /* Get an original kprobe for return */ |
| 1685 | orig_p = __get_valid_kprobe(p); |
| 1686 | if (unlikely(orig_p == NULL)) |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1687 | return ERR_PTR(-EINVAL); |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1688 | |
| 1689 | if (!kprobe_disabled(p)) { |
| 1690 | /* Disable probe if it is a child probe */ |
| 1691 | if (p != orig_p) |
| 1692 | p->flags |= KPROBE_FLAG_DISABLED; |
| 1693 | |
| 1694 | /* Try to disarm and disable this/parent probe */ |
| 1695 | if (p == orig_p || aggr_kprobe_disabled(orig_p)) { |
Wang Nan | 69d54b9 | 2015-02-13 14:40:26 -0800 | [diff] [blame] | 1696 | /* |
| 1697 | * If kprobes_all_disarmed is set, orig_p |
| 1698 | * should have already been disarmed, so |
| 1699 | * skip unneed disarming process. |
| 1700 | */ |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1701 | if (!kprobes_all_disarmed) { |
| 1702 | ret = disarm_kprobe(orig_p, true); |
| 1703 | if (ret) { |
| 1704 | p->flags &= ~KPROBE_FLAG_DISABLED; |
| 1705 | return ERR_PTR(ret); |
| 1706 | } |
| 1707 | } |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1708 | orig_p->flags |= KPROBE_FLAG_DISABLED; |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | return orig_p; |
| 1713 | } |
| 1714 | |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1715 | /* |
| 1716 | * Unregister a kprobe without a scheduler synchronization. |
| 1717 | */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1718 | static int __unregister_kprobe_top(struct kprobe *p) |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 1719 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1720 | struct kprobe *ap, *list_p; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 1721 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1722 | /* Disable kprobe. This will disarm it if needed. */ |
| 1723 | ap = __disable_kprobe(p); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 1724 | if (IS_ERR(ap)) |
| 1725 | return PTR_ERR(ap); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1726 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1727 | if (ap == p) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1728 | /* |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1729 | * This probe is an independent(and non-optimized) kprobe |
| 1730 | * (not an aggrprobe). Remove from the hash list. |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1731 | */ |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1732 | goto disarmed; |
| 1733 | |
| 1734 | /* Following process expects this probe is an aggrprobe */ |
| 1735 | WARN_ON(!kprobe_aggrprobe(ap)); |
| 1736 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1737 | if (list_is_singular(&ap->list) && kprobe_disarmed(ap)) |
| 1738 | /* |
| 1739 | * !disarmed could be happen if the probe is under delayed |
| 1740 | * unoptimizing. |
| 1741 | */ |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1742 | goto disarmed; |
| 1743 | else { |
| 1744 | /* If disabling probe has special handlers, update aggrprobe */ |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1745 | if (p->post_handler && !kprobe_gone(p)) { |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 1746 | list_for_each_entry(list_p, &ap->list, list) { |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1747 | if ((list_p != p) && (list_p->post_handler)) |
| 1748 | goto noclean; |
| 1749 | } |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1750 | ap->post_handler = NULL; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1751 | } |
| 1752 | noclean: |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1753 | /* |
| 1754 | * Remove from the aggrprobe: this path will do nothing in |
| 1755 | * __unregister_kprobe_bottom(). |
| 1756 | */ |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1757 | list_del_rcu(&p->list); |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1758 | if (!kprobe_disabled(ap) && !kprobes_all_disarmed) |
| 1759 | /* |
| 1760 | * Try to optimize this probe again, because post |
| 1761 | * handler may have been changed. |
| 1762 | */ |
| 1763 | optimize_kprobe(ap); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1764 | } |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1765 | return 0; |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 1766 | |
| 1767 | disarmed: |
| 1768 | hlist_del_rcu(&ap->hlist); |
| 1769 | return 0; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1770 | } |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 1771 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1772 | static void __unregister_kprobe_bottom(struct kprobe *p) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1773 | { |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1774 | struct kprobe *ap; |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 1775 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1776 | if (list_empty(&p->list)) |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1777 | /* This is an independent kprobe */ |
Ananth N Mavinakayanahalli | 0498b63 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 1778 | arch_remove_kprobe(p); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1779 | else if (list_is_singular(&p->list)) { |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1780 | /* This is the last child of an aggrprobe */ |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1781 | ap = list_entry(p->list.next, struct kprobe, list); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 1782 | list_del(&p->list); |
Masami Hiramatsu | 6d8e40a | 2010-12-03 18:53:50 +0900 | [diff] [blame] | 1783 | free_aggr_kprobe(ap); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 1784 | } |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 1785 | /* Otherwise, do nothing. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1788 | int register_kprobes(struct kprobe **kps, int num) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1789 | { |
| 1790 | int i, ret = 0; |
| 1791 | |
| 1792 | if (num <= 0) |
| 1793 | return -EINVAL; |
| 1794 | for (i = 0; i < num; i++) { |
Masami Hiramatsu | 49ad2fd | 2009-01-06 14:41:53 -0800 | [diff] [blame] | 1795 | ret = register_kprobe(kps[i]); |
Masami Hiramatsu | 67dddaa | 2008-06-12 15:21:35 -0700 | [diff] [blame] | 1796 | if (ret < 0) { |
| 1797 | if (i > 0) |
| 1798 | unregister_kprobes(kps, i); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1799 | break; |
| 1800 | } |
| 1801 | } |
| 1802 | return ret; |
| 1803 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1804 | EXPORT_SYMBOL_GPL(register_kprobes); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1805 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1806 | void unregister_kprobe(struct kprobe *p) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1807 | { |
| 1808 | unregister_kprobes(&p, 1); |
| 1809 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1810 | EXPORT_SYMBOL_GPL(unregister_kprobe); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1811 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1812 | void unregister_kprobes(struct kprobe **kps, int num) |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1813 | { |
| 1814 | int i; |
| 1815 | |
| 1816 | if (num <= 0) |
| 1817 | return; |
| 1818 | mutex_lock(&kprobe_mutex); |
| 1819 | for (i = 0; i < num; i++) |
| 1820 | if (__unregister_kprobe_top(kps[i]) < 0) |
| 1821 | kps[i]->addr = NULL; |
| 1822 | mutex_unlock(&kprobe_mutex); |
| 1823 | |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 1824 | synchronize_rcu(); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1825 | for (i = 0; i < num; i++) |
| 1826 | if (kps[i]->addr) |
| 1827 | __unregister_kprobe_bottom(kps[i]); |
| 1828 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 1829 | EXPORT_SYMBOL_GPL(unregister_kprobes); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1830 | |
Naveen N. Rao | 5f6bee3 | 2017-03-08 22:34:15 +0530 | [diff] [blame] | 1831 | int __weak kprobe_exceptions_notify(struct notifier_block *self, |
| 1832 | unsigned long val, void *data) |
Naveen N. Rao | fc62d02 | 2017-02-08 01:24:14 +0530 | [diff] [blame] | 1833 | { |
| 1834 | return NOTIFY_DONE; |
| 1835 | } |
Naveen N. Rao | 5f6bee3 | 2017-03-08 22:34:15 +0530 | [diff] [blame] | 1836 | NOKPROBE_SYMBOL(kprobe_exceptions_notify); |
Naveen N. Rao | fc62d02 | 2017-02-08 01:24:14 +0530 | [diff] [blame] | 1837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | static struct notifier_block kprobe_exceptions_nb = { |
| 1839 | .notifier_call = kprobe_exceptions_notify, |
Anil S Keshavamurthy | 3d5631e | 2006-06-26 00:25:28 -0700 | [diff] [blame] | 1840 | .priority = 0x7fffffff /* we need to be notified first */ |
| 1841 | }; |
| 1842 | |
Michael Ellerman | 3d7e338 | 2007-07-19 01:48:11 -0700 | [diff] [blame] | 1843 | unsigned long __weak arch_deref_entry_point(void *entry) |
| 1844 | { |
| 1845 | return (unsigned long)entry; |
| 1846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 1848 | #ifdef CONFIG_KRETPROBES |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1849 | |
| 1850 | unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, |
| 1851 | void *trampoline_address, |
| 1852 | void *frame_pointer) |
| 1853 | { |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1854 | kprobe_opcode_t *correct_ret_addr = NULL; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1855 | struct kretprobe_instance *ri = NULL; |
| 1856 | struct llist_node *first, *node; |
| 1857 | struct kretprobe *rp; |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1858 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1859 | /* Find all nodes for this frame. */ |
| 1860 | first = node = current->kretprobe_instances.first; |
| 1861 | while (node) { |
| 1862 | ri = container_of(node, struct kretprobe_instance, llist); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1863 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1864 | BUG_ON(ri->fp != frame_pointer); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1865 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1866 | if (ri->ret_addr != trampoline_address) { |
| 1867 | correct_ret_addr = ri->ret_addr; |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1868 | /* |
| 1869 | * This is the real return address. Any other |
| 1870 | * instances associated with this task are for |
| 1871 | * other calls deeper on the call stack |
| 1872 | */ |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1873 | goto found; |
| 1874 | } |
| 1875 | |
| 1876 | node = node->next; |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1877 | } |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1878 | pr_err("Oops! Kretprobe fails to find correct return address.\n"); |
| 1879 | BUG_ON(1); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1880 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1881 | found: |
| 1882 | /* Unlink all nodes for this frame. */ |
| 1883 | current->kretprobe_instances.first = node->next; |
| 1884 | node->next = NULL; |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1885 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1886 | /* Run them.. */ |
| 1887 | while (first) { |
| 1888 | ri = container_of(first, struct kretprobe_instance, llist); |
| 1889 | first = first->next; |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1890 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1891 | rp = get_kretprobe(ri); |
| 1892 | if (rp && rp->handler) { |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1893 | struct kprobe *prev = kprobe_running(); |
| 1894 | |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1895 | __this_cpu_write(current_kprobe, &rp->kp); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1896 | ri->ret_addr = correct_ret_addr; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 1897 | rp->handler(ri, regs); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1898 | __this_cpu_write(current_kprobe, prev); |
| 1899 | } |
| 1900 | |
Masami Hiramatsu | b338817 | 2020-08-29 22:02:47 +0900 | [diff] [blame] | 1901 | recycle_rp_inst(ri); |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1902 | } |
| 1903 | |
Masami Hiramatsu | 66ada2c | 2020-08-29 22:00:01 +0900 | [diff] [blame] | 1904 | return (unsigned long)correct_ret_addr; |
| 1905 | } |
| 1906 | NOKPROBE_SYMBOL(__kretprobe_trampoline_handler) |
| 1907 | |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 1908 | /* |
| 1909 | * This kprobe pre_handler is registered with every kretprobe. When probe |
| 1910 | * hits it will set up the return probe. |
| 1911 | */ |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1912 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 1913 | { |
| 1914 | struct kretprobe *rp = container_of(p, struct kretprobe, kp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1915 | struct kretprobe_instance *ri; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1916 | struct freelist_node *fn; |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 1917 | |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1918 | fn = freelist_try_get(&rp->freelist); |
| 1919 | if (!fn) { |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 1920 | rp->nmissed++; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1921 | return 0; |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 1922 | } |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 1923 | |
| 1924 | ri = container_of(fn, struct kretprobe_instance, freelist); |
| 1925 | |
| 1926 | if (rp->entry_handler && rp->entry_handler(ri, regs)) { |
| 1927 | freelist_add(&ri->freelist, &rp->freelist); |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | arch_prepare_kretprobe(ri, regs); |
| 1932 | |
| 1933 | __llist_add(&ri->llist, ¤t->kretprobe_instances); |
| 1934 | |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 1935 | return 0; |
| 1936 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 1937 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 1938 | |
Naveen N. Rao | 659b957 | 2017-07-07 22:37:24 +0530 | [diff] [blame] | 1939 | bool __weak arch_kprobe_on_func_entry(unsigned long offset) |
Naveen N. Rao | 90ec5e8 | 2017-02-22 19:23:37 +0530 | [diff] [blame] | 1940 | { |
| 1941 | return !offset; |
| 1942 | } |
| 1943 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 1944 | /** |
| 1945 | * kprobe_on_func_entry() -- check whether given address is function entry |
| 1946 | * @addr: Target address |
| 1947 | * @sym: Target symbol name |
| 1948 | * @offset: The offset from the symbol or the address |
| 1949 | * |
| 1950 | * This checks whether the given @addr+@offset or @sym+@offset is on the |
| 1951 | * function entry address or not. |
| 1952 | * This returns 0 if it is the function entry, or -EINVAL if it is not. |
| 1953 | * And also it returns -ENOENT if it fails the symbol or address lookup. |
| 1954 | * Caller must pass @addr or @sym (either one must be NULL), or this |
| 1955 | * returns -EINVAL. |
| 1956 | */ |
| 1957 | 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] | 1958 | { |
| 1959 | kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset); |
| 1960 | |
| 1961 | if (IS_ERR(kp_addr)) |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 1962 | return PTR_ERR(kp_addr); |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1963 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 1964 | if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset)) |
| 1965 | return -ENOENT; |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1966 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 1967 | if (!arch_kprobe_on_func_entry(offset)) |
| 1968 | return -EINVAL; |
| 1969 | |
| 1970 | return 0; |
Naveen N. Rao | 1d585e7 | 2017-03-08 13:56:06 +0530 | [diff] [blame] | 1971 | } |
| 1972 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 1973 | int register_kretprobe(struct kretprobe *rp) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1974 | { |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 1975 | int ret; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1976 | struct kretprobe_instance *inst; |
| 1977 | int i; |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1978 | void *addr; |
Naveen N. Rao | 90ec5e8 | 2017-02-22 19:23:37 +0530 | [diff] [blame] | 1979 | |
Masami Hiramatsu | 97c753e | 2021-01-28 00:37:51 +0900 | [diff] [blame] | 1980 | ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset); |
| 1981 | if (ret) |
| 1982 | return ret; |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 1983 | |
Wang ShaoBo | 0188b87 | 2021-01-28 20:44:27 +0800 | [diff] [blame] | 1984 | /* If only rp->kp.addr is specified, check reregistering kprobes */ |
Masami Hiramatsu | 33b1d14 | 2021-02-03 23:59:27 +0900 | [diff] [blame] | 1985 | if (rp->kp.addr && warn_kprobe_rereg(&rp->kp)) |
Wang ShaoBo | 0188b87 | 2021-01-28 20:44:27 +0800 | [diff] [blame] | 1986 | return -EINVAL; |
| 1987 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 1988 | if (kretprobe_blacklist_size) { |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 1989 | addr = kprobe_addr(&rp->kp); |
Masami Hiramatsu | bc81d48 | 2011-06-27 16:26:50 +0900 | [diff] [blame] | 1990 | if (IS_ERR(addr)) |
| 1991 | return PTR_ERR(addr); |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 1992 | |
| 1993 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
| 1994 | if (kretprobe_blacklist[i].addr == addr) |
| 1995 | return -EINVAL; |
| 1996 | } |
| 1997 | } |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1998 | |
| 1999 | rp->kp.pre_handler = pre_handler_kretprobe; |
Ananth N Mavinakayanahalli | 7522a84 | 2006-04-20 02:43:11 -0700 | [diff] [blame] | 2000 | rp->kp.post_handler = NULL; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2001 | |
| 2002 | /* Pre-allocate memory for max kretprobe instances */ |
| 2003 | if (rp->maxactive <= 0) { |
Thomas Gleixner | 9261660 | 2019-07-26 23:19:41 +0200 | [diff] [blame] | 2004 | #ifdef CONFIG_PREEMPTION |
Heiko Carstens | c2ef666 | 2009-12-21 13:02:24 +0100 | [diff] [blame] | 2005 | rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus()); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2006 | #else |
Ananth N Mavinakayanahalli | 4dae560 | 2009-10-30 19:23:10 +0530 | [diff] [blame] | 2007 | rp->maxactive = num_possible_cpus(); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2008 | #endif |
| 2009 | } |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2010 | rp->freelist.head = NULL; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2011 | rp->rph = kzalloc(sizeof(struct kretprobe_holder), GFP_KERNEL); |
| 2012 | if (!rp->rph) |
| 2013 | return -ENOMEM; |
| 2014 | |
| 2015 | rp->rph->rp = rp; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2016 | for (i = 0; i < rp->maxactive; i++) { |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2017 | inst = kzalloc(sizeof(struct kretprobe_instance) + |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 2018 | rp->data_size, GFP_KERNEL); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2019 | if (inst == NULL) { |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2020 | refcount_set(&rp->rph->ref, i); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2021 | free_rp_inst(rp); |
| 2022 | return -ENOMEM; |
| 2023 | } |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2024 | inst->rph = rp->rph; |
Peter Zijlstra | 6e426e0 | 2020-08-29 22:03:56 +0900 | [diff] [blame] | 2025 | freelist_add(&inst->freelist, &rp->freelist); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2026 | } |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2027 | refcount_set(&rp->rph->ref, i); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2028 | |
| 2029 | rp->nmissed = 0; |
| 2030 | /* Establish function entry probe point */ |
Masami Hiramatsu | 49ad2fd | 2009-01-06 14:41:53 -0800 | [diff] [blame] | 2031 | ret = register_kprobe(&rp->kp); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2032 | if (ret != 0) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2033 | free_rp_inst(rp); |
| 2034 | return ret; |
| 2035 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2036 | EXPORT_SYMBOL_GPL(register_kretprobe); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2037 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2038 | int register_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2039 | { |
| 2040 | int ret = 0, i; |
| 2041 | |
| 2042 | if (num <= 0) |
| 2043 | return -EINVAL; |
| 2044 | for (i = 0; i < num; i++) { |
Masami Hiramatsu | 49ad2fd | 2009-01-06 14:41:53 -0800 | [diff] [blame] | 2045 | ret = register_kretprobe(rps[i]); |
Masami Hiramatsu | 67dddaa | 2008-06-12 15:21:35 -0700 | [diff] [blame] | 2046 | if (ret < 0) { |
| 2047 | if (i > 0) |
| 2048 | unregister_kretprobes(rps, i); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2049 | break; |
| 2050 | } |
| 2051 | } |
| 2052 | return ret; |
| 2053 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2054 | EXPORT_SYMBOL_GPL(register_kretprobes); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2055 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2056 | void unregister_kretprobe(struct kretprobe *rp) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2057 | { |
| 2058 | unregister_kretprobes(&rp, 1); |
| 2059 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2060 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2061 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2062 | void unregister_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2063 | { |
| 2064 | int i; |
| 2065 | |
| 2066 | if (num <= 0) |
| 2067 | return; |
| 2068 | mutex_lock(&kprobe_mutex); |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2069 | for (i = 0; i < num; i++) { |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2070 | if (__unregister_kprobe_top(&rps[i]->kp) < 0) |
| 2071 | rps[i]->kp.addr = NULL; |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2072 | rps[i]->rph->rp = NULL; |
| 2073 | } |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2074 | mutex_unlock(&kprobe_mutex); |
| 2075 | |
Paul E. McKenney | ae8b7ce | 2018-11-06 19:04:39 -0800 | [diff] [blame] | 2076 | synchronize_rcu(); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2077 | for (i = 0; i < num; i++) { |
| 2078 | if (rps[i]->kp.addr) { |
| 2079 | __unregister_kprobe_bottom(&rps[i]->kp); |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2080 | free_rp_inst(rps[i]); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2084 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2085 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 2086 | #else /* CONFIG_KRETPROBES */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2087 | int register_kretprobe(struct kretprobe *rp) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2088 | { |
| 2089 | return -ENOSYS; |
| 2090 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2091 | EXPORT_SYMBOL_GPL(register_kretprobe); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2092 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2093 | int register_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2094 | { |
| 2095 | return -ENOSYS; |
| 2096 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2097 | EXPORT_SYMBOL_GPL(register_kretprobes); |
| 2098 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2099 | void unregister_kretprobe(struct kretprobe *rp) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2100 | { |
| 2101 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2102 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2103 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2104 | void unregister_kretprobes(struct kretprobe **rps, int num) |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2105 | { |
| 2106 | } |
Masami Hiramatsu | 99081ab | 2009-04-06 19:00:59 -0700 | [diff] [blame] | 2107 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2108 | |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2109 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2110 | { |
| 2111 | return 0; |
| 2112 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2113 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 2114 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 2115 | #endif /* CONFIG_KRETPROBES */ |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 2116 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2117 | /* Set the kprobe gone and remove its instruction buffer. */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2118 | static void kill_kprobe(struct kprobe *p) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2119 | { |
| 2120 | struct kprobe *kp; |
Masami Hiramatsu | de5bd88 | 2009-04-06 19:01:02 -0700 | [diff] [blame] | 2121 | |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2122 | lockdep_assert_held(&kprobe_mutex); |
| 2123 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2124 | p->flags |= KPROBE_FLAG_GONE; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2125 | if (kprobe_aggrprobe(p)) { |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2126 | /* |
| 2127 | * If this is an aggr_kprobe, we have to list all the |
| 2128 | * chained probes and mark them GONE. |
| 2129 | */ |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2130 | list_for_each_entry(kp, &p->list, list) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2131 | kp->flags |= KPROBE_FLAG_GONE; |
| 2132 | p->post_handler = NULL; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2133 | kill_optimized_kprobe(p); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2134 | } |
| 2135 | /* |
| 2136 | * Here, we can remove insn_slot safely, because no thread calls |
| 2137 | * the original probed function (which will be freed soon) any more. |
| 2138 | */ |
| 2139 | arch_remove_kprobe(p); |
Muchun Song | 0cb2f13 | 2020-07-28 14:45:36 +0800 | [diff] [blame] | 2140 | |
| 2141 | /* |
| 2142 | * The module is going away. We should disarm the kprobe which |
Masami Hiramatsu | bcb5320 | 2020-09-01 00:12:07 +0900 | [diff] [blame] | 2143 | * is using ftrace, because ftrace framework is still available at |
| 2144 | * MODULE_STATE_GOING notification. |
Muchun Song | 0cb2f13 | 2020-07-28 14:45:36 +0800 | [diff] [blame] | 2145 | */ |
Masami Hiramatsu | bcb5320 | 2020-09-01 00:12:07 +0900 | [diff] [blame] | 2146 | if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed) |
Muchun Song | 0cb2f13 | 2020-07-28 14:45:36 +0800 | [diff] [blame] | 2147 | disarm_kprobe_ftrace(p); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2148 | } |
| 2149 | |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2150 | /* Disable one kprobe */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2151 | int disable_kprobe(struct kprobe *kp) |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2152 | { |
| 2153 | int ret = 0; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2154 | struct kprobe *p; |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2155 | |
| 2156 | mutex_lock(&kprobe_mutex); |
| 2157 | |
Masami Hiramatsu | 6f0f1dd | 2010-12-03 18:53:57 +0900 | [diff] [blame] | 2158 | /* Disable this kprobe */ |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2159 | p = __disable_kprobe(kp); |
| 2160 | if (IS_ERR(p)) |
| 2161 | ret = PTR_ERR(p); |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2162 | |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2163 | mutex_unlock(&kprobe_mutex); |
| 2164 | return ret; |
| 2165 | } |
| 2166 | EXPORT_SYMBOL_GPL(disable_kprobe); |
| 2167 | |
| 2168 | /* Enable one kprobe */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2169 | int enable_kprobe(struct kprobe *kp) |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2170 | { |
| 2171 | int ret = 0; |
| 2172 | struct kprobe *p; |
| 2173 | |
| 2174 | mutex_lock(&kprobe_mutex); |
| 2175 | |
| 2176 | /* Check whether specified probe is valid. */ |
| 2177 | p = __get_valid_kprobe(kp); |
| 2178 | if (unlikely(p == NULL)) { |
| 2179 | ret = -EINVAL; |
| 2180 | goto out; |
| 2181 | } |
| 2182 | |
| 2183 | if (kprobe_gone(kp)) { |
| 2184 | /* This kprobe has gone, we couldn't enable it. */ |
| 2185 | ret = -EINVAL; |
| 2186 | goto out; |
| 2187 | } |
| 2188 | |
| 2189 | if (p != kp) |
| 2190 | kp->flags &= ~KPROBE_FLAG_DISABLED; |
| 2191 | |
| 2192 | if (!kprobes_all_disarmed && kprobe_disabled(p)) { |
| 2193 | p->flags &= ~KPROBE_FLAG_DISABLED; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2194 | ret = arm_kprobe(p); |
| 2195 | if (ret) |
| 2196 | p->flags |= KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | c061482 | 2010-04-27 18:33:12 -0400 | [diff] [blame] | 2197 | } |
| 2198 | out: |
| 2199 | mutex_unlock(&kprobe_mutex); |
| 2200 | return ret; |
| 2201 | } |
| 2202 | EXPORT_SYMBOL_GPL(enable_kprobe); |
| 2203 | |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 2204 | /* 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] | 2205 | void dump_kprobe(struct kprobe *kp) |
Frederic Weisbecker | 24851d2 | 2009-08-26 23:38:30 +0200 | [diff] [blame] | 2206 | { |
Masami Hiramatsu | 4458515 | 2018-04-28 21:36:33 +0900 | [diff] [blame] | 2207 | pr_err("Dumping kprobe:\n"); |
| 2208 | pr_err("Name: %s\nOffset: %x\nAddress: %pS\n", |
| 2209 | kp->symbol_name, kp->offset, kp->addr); |
Frederic Weisbecker | 24851d2 | 2009-08-26 23:38:30 +0200 | [diff] [blame] | 2210 | } |
Masami Hiramatsu | 820aede | 2014-04-17 17:18:21 +0900 | [diff] [blame] | 2211 | NOKPROBE_SYMBOL(dump_kprobe); |
Frederic Weisbecker | 24851d2 | 2009-08-26 23:38:30 +0200 | [diff] [blame] | 2212 | |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2213 | int kprobe_add_ksym_blacklist(unsigned long entry) |
| 2214 | { |
| 2215 | struct kprobe_blacklist_entry *ent; |
| 2216 | unsigned long offset = 0, size = 0; |
| 2217 | |
| 2218 | if (!kernel_text_address(entry) || |
| 2219 | !kallsyms_lookup_size_offset(entry, &size, &offset)) |
| 2220 | return -EINVAL; |
| 2221 | |
| 2222 | ent = kmalloc(sizeof(*ent), GFP_KERNEL); |
| 2223 | if (!ent) |
| 2224 | return -ENOMEM; |
| 2225 | ent->start_addr = entry; |
| 2226 | ent->end_addr = entry + size; |
| 2227 | INIT_LIST_HEAD(&ent->list); |
| 2228 | list_add_tail(&ent->list, &kprobe_blacklist); |
| 2229 | |
| 2230 | return (int)size; |
| 2231 | } |
| 2232 | |
| 2233 | /* Add all symbols in given area into kprobe blacklist */ |
| 2234 | int kprobe_add_area_blacklist(unsigned long start, unsigned long end) |
| 2235 | { |
| 2236 | unsigned long entry; |
| 2237 | int ret = 0; |
| 2238 | |
| 2239 | for (entry = start; entry < end; entry += ret) { |
| 2240 | ret = kprobe_add_ksym_blacklist(entry); |
| 2241 | if (ret < 0) |
| 2242 | return ret; |
| 2243 | if (ret == 0) /* In case of alias symbol */ |
| 2244 | ret = 1; |
| 2245 | } |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2249 | /* Remove all symbols in given area from kprobe blacklist */ |
| 2250 | static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end) |
| 2251 | { |
| 2252 | struct kprobe_blacklist_entry *ent, *n; |
| 2253 | |
| 2254 | list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) { |
| 2255 | if (ent->start_addr < start || ent->start_addr >= end) |
| 2256 | continue; |
| 2257 | list_del(&ent->list); |
| 2258 | kfree(ent); |
| 2259 | } |
| 2260 | } |
| 2261 | |
Masami Hiramatsu | 16db626 | 2020-03-26 23:50:00 +0900 | [diff] [blame] | 2262 | static void kprobe_remove_ksym_blacklist(unsigned long entry) |
| 2263 | { |
| 2264 | kprobe_remove_area_blacklist(entry, entry + 1); |
| 2265 | } |
| 2266 | |
Adrian Hunter | d002b8b | 2020-05-28 11:00:58 +0300 | [diff] [blame] | 2267 | int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value, |
| 2268 | char *type, char *sym) |
| 2269 | { |
| 2270 | return -ERANGE; |
| 2271 | } |
| 2272 | |
| 2273 | int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
| 2274 | char *sym) |
| 2275 | { |
| 2276 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
| 2277 | if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym)) |
| 2278 | return 0; |
| 2279 | #ifdef CONFIG_OPTPROBES |
| 2280 | if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym)) |
| 2281 | return 0; |
| 2282 | #endif |
| 2283 | #endif |
| 2284 | if (!arch_kprobe_get_kallsym(&symnum, value, type, sym)) |
| 2285 | return 0; |
| 2286 | return -ERANGE; |
| 2287 | } |
| 2288 | |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2289 | int __init __weak arch_populate_kprobe_blacklist(void) |
| 2290 | { |
| 2291 | return 0; |
| 2292 | } |
| 2293 | |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2294 | /* |
| 2295 | * Lookup and populate the kprobe_blacklist. |
| 2296 | * |
| 2297 | * Unlike the kretprobe blacklist, we'll need to determine |
| 2298 | * the range of addresses that belong to the said functions, |
| 2299 | * since a kprobe need not necessarily be at the beginning |
| 2300 | * of a function. |
| 2301 | */ |
| 2302 | static int __init populate_kprobe_blacklist(unsigned long *start, |
| 2303 | unsigned long *end) |
| 2304 | { |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2305 | unsigned long entry; |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2306 | unsigned long *iter; |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2307 | int ret; |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2308 | |
| 2309 | for (iter = start; iter < end; iter++) { |
Masami Hiramatsu | d81b425 | 2014-07-17 11:44:11 +0000 | [diff] [blame] | 2310 | entry = arch_deref_entry_point((void *)*iter); |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2311 | ret = kprobe_add_ksym_blacklist(entry); |
| 2312 | if (ret == -EINVAL) |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2313 | continue; |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2314 | if (ret < 0) |
| 2315 | return ret; |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2316 | } |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2317 | |
| 2318 | /* Symbols in __kprobes_text are blacklisted */ |
| 2319 | ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start, |
| 2320 | (unsigned long)__kprobes_text_end); |
Thomas Gleixner | 66e9b07 | 2020-03-10 14:04:34 +0100 | [diff] [blame] | 2321 | if (ret) |
| 2322 | return ret; |
| 2323 | |
| 2324 | /* Symbols in noinstr section are blacklisted */ |
| 2325 | ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start, |
| 2326 | (unsigned long)__noinstr_text_end); |
Masami Hiramatsu | fb1a59f | 2018-12-17 17:20:55 +0900 | [diff] [blame] | 2327 | |
| 2328 | return ret ? : arch_populate_kprobe_blacklist(); |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2329 | } |
| 2330 | |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2331 | static void add_module_kprobe_blacklist(struct module *mod) |
| 2332 | { |
| 2333 | unsigned long start, end; |
Masami Hiramatsu | 16db626 | 2020-03-26 23:50:00 +0900 | [diff] [blame] | 2334 | int i; |
| 2335 | |
| 2336 | if (mod->kprobe_blacklist) { |
| 2337 | for (i = 0; i < mod->num_kprobe_blacklist; i++) |
| 2338 | kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]); |
| 2339 | } |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2340 | |
| 2341 | start = (unsigned long)mod->kprobes_text_start; |
| 2342 | if (start) { |
| 2343 | end = start + mod->kprobes_text_size; |
| 2344 | kprobe_add_area_blacklist(start, end); |
| 2345 | } |
Thomas Gleixner | 66e9b07 | 2020-03-10 14:04:34 +0100 | [diff] [blame] | 2346 | |
| 2347 | start = (unsigned long)mod->noinstr_text_start; |
| 2348 | if (start) { |
| 2349 | end = start + mod->noinstr_text_size; |
| 2350 | kprobe_add_area_blacklist(start, end); |
| 2351 | } |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | static void remove_module_kprobe_blacklist(struct module *mod) |
| 2355 | { |
| 2356 | unsigned long start, end; |
Masami Hiramatsu | 16db626 | 2020-03-26 23:50:00 +0900 | [diff] [blame] | 2357 | int i; |
| 2358 | |
| 2359 | if (mod->kprobe_blacklist) { |
| 2360 | for (i = 0; i < mod->num_kprobe_blacklist; i++) |
| 2361 | kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]); |
| 2362 | } |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2363 | |
| 2364 | start = (unsigned long)mod->kprobes_text_start; |
| 2365 | if (start) { |
| 2366 | end = start + mod->kprobes_text_size; |
| 2367 | kprobe_remove_area_blacklist(start, end); |
| 2368 | } |
Thomas Gleixner | 66e9b07 | 2020-03-10 14:04:34 +0100 | [diff] [blame] | 2369 | |
| 2370 | start = (unsigned long)mod->noinstr_text_start; |
| 2371 | if (start) { |
| 2372 | end = start + mod->noinstr_text_size; |
| 2373 | kprobe_remove_area_blacklist(start, end); |
| 2374 | } |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2375 | } |
| 2376 | |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2377 | /* Module notifier call back, checking kprobes on the module */ |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2378 | static int kprobes_module_callback(struct notifier_block *nb, |
| 2379 | unsigned long val, void *data) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2380 | { |
| 2381 | struct module *mod = data; |
| 2382 | struct hlist_head *head; |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2383 | struct kprobe *p; |
| 2384 | unsigned int i; |
Masami Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2385 | int checkcore = (val == MODULE_STATE_GOING); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2386 | |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2387 | if (val == MODULE_STATE_COMING) { |
| 2388 | mutex_lock(&kprobe_mutex); |
| 2389 | add_module_kprobe_blacklist(mod); |
| 2390 | mutex_unlock(&kprobe_mutex); |
| 2391 | } |
Masami Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2392 | if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE) |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2393 | return NOTIFY_DONE; |
| 2394 | |
| 2395 | /* |
Masami Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2396 | * When MODULE_STATE_GOING was notified, both of module .text and |
| 2397 | * .init.text sections would be freed. When MODULE_STATE_LIVE was |
| 2398 | * notified, only .init.text section would be freed. We need to |
| 2399 | * disable kprobes which have been inserted in the sections. |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2400 | */ |
| 2401 | mutex_lock(&kprobe_mutex); |
| 2402 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2403 | head = &kprobe_table[i]; |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2404 | hlist_for_each_entry(p, head, hlist) |
Masami Hiramatsu | f24659d | 2009-01-06 14:41:55 -0800 | [diff] [blame] | 2405 | if (within_module_init((unsigned long)p->addr, mod) || |
| 2406 | (checkcore && |
| 2407 | within_module_core((unsigned long)p->addr, mod))) { |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2408 | /* |
| 2409 | * The vaddr this probe is installed will soon |
| 2410 | * be vfreed buy not synced to disk. Hence, |
| 2411 | * disarming the breakpoint isn't needed. |
Steven Rostedt (VMware) | 545a028 | 2017-05-16 14:58:35 -0400 | [diff] [blame] | 2412 | * |
| 2413 | * Note, this will also move any optimized probes |
| 2414 | * that are pending to be removed from their |
| 2415 | * corresponding lists to the freeing_list and |
| 2416 | * will not be touched by the delayed |
| 2417 | * kprobe_optimizer work handler. |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2418 | */ |
| 2419 | kill_kprobe(p); |
| 2420 | } |
| 2421 | } |
Masami Hiramatsu | 1e6769b | 2020-03-26 23:49:48 +0900 | [diff] [blame] | 2422 | if (val == MODULE_STATE_GOING) |
| 2423 | remove_module_kprobe_blacklist(mod); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2424 | mutex_unlock(&kprobe_mutex); |
| 2425 | return NOTIFY_DONE; |
| 2426 | } |
| 2427 | |
| 2428 | static struct notifier_block kprobe_module_nb = { |
| 2429 | .notifier_call = kprobes_module_callback, |
| 2430 | .priority = 0 |
| 2431 | }; |
| 2432 | |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2433 | /* Markers of _kprobe_blacklist section */ |
| 2434 | extern unsigned long __start_kprobe_blacklist[]; |
| 2435 | extern unsigned long __stop_kprobe_blacklist[]; |
| 2436 | |
Masami Hiramatsu | 82d083a | 2020-09-10 17:55:05 +0900 | [diff] [blame] | 2437 | void kprobe_free_init_mem(void) |
| 2438 | { |
| 2439 | void *start = (void *)(&__init_begin); |
| 2440 | void *end = (void *)(&__init_end); |
| 2441 | struct hlist_head *head; |
| 2442 | struct kprobe *p; |
| 2443 | int i; |
| 2444 | |
| 2445 | mutex_lock(&kprobe_mutex); |
| 2446 | |
| 2447 | /* Kill all kprobes on initmem */ |
| 2448 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2449 | head = &kprobe_table[i]; |
| 2450 | hlist_for_each_entry(p, head, hlist) { |
| 2451 | if (start <= (void *)p->addr && (void *)p->addr < end) |
| 2452 | kill_kprobe(p); |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | mutex_unlock(&kprobe_mutex); |
| 2457 | } |
| 2458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | static int __init init_kprobes(void) |
| 2460 | { |
| 2461 | int i, err = 0; |
| 2462 | |
| 2463 | /* FIXME allocate the probe table, currently defined statically */ |
| 2464 | /* initialize all list heads */ |
Peter Zijlstra | d741bf4 | 2020-08-29 22:03:24 +0900 | [diff] [blame] | 2465 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | INIT_HLIST_HEAD(&kprobe_table[i]); |
| 2467 | |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 2468 | err = populate_kprobe_blacklist(__start_kprobe_blacklist, |
| 2469 | __stop_kprobe_blacklist); |
| 2470 | if (err) { |
| 2471 | pr_err("kprobes: failed to populate blacklist: %d\n", err); |
| 2472 | pr_err("Please take care of using kprobes.\n"); |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 2473 | } |
| 2474 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2475 | if (kretprobe_blacklist_size) { |
| 2476 | /* lookup the function address from its name */ |
| 2477 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
Naveen N. Rao | 49e0b46 | 2017-04-19 18:21:00 +0530 | [diff] [blame] | 2478 | kretprobe_blacklist[i].addr = |
Naveen N. Rao | 290e307 | 2017-04-19 18:21:01 +0530 | [diff] [blame] | 2479 | kprobe_lookup_name(kretprobe_blacklist[i].name, 0); |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 2480 | if (!kretprobe_blacklist[i].addr) |
| 2481 | printk("kretprobe: lookup failed: %s\n", |
| 2482 | kretprobe_blacklist[i].name); |
| 2483 | } |
| 2484 | } |
| 2485 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2486 | /* By default, kprobes are armed */ |
| 2487 | kprobes_all_disarmed = false; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2488 | |
Masami Hiramatsu | c85c9a2 | 2021-02-18 23:29:23 +0900 | [diff] [blame] | 2489 | #if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT) |
| 2490 | /* Init kprobe_optinsn_slots for allocation */ |
| 2491 | kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE; |
| 2492 | #endif |
| 2493 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 2494 | err = arch_init_kprobes(); |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 2495 | if (!err) |
| 2496 | err = register_die_notifier(&kprobe_exceptions_nb); |
Masami Hiramatsu | e8386a0 | 2009-01-06 14:41:52 -0800 | [diff] [blame] | 2497 | if (!err) |
| 2498 | err = register_module_notifier(&kprobe_module_nb); |
| 2499 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 2500 | kprobes_initialized = (err == 0); |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 2501 | |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 2502 | if (!err) |
| 2503 | init_test_probes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | return err; |
| 2505 | } |
Masami Hiramatsu | 36dadef | 2020-09-10 21:38:39 +0900 | [diff] [blame] | 2506 | early_initcall(init_kprobes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | |
Masami Hiramatsu | c85c9a2 | 2021-02-18 23:29:23 +0900 | [diff] [blame] | 2508 | #if defined(CONFIG_OPTPROBES) |
| 2509 | static int __init init_optprobes(void) |
| 2510 | { |
| 2511 | /* |
| 2512 | * Enable kprobe optimization - this kicks the optimizer which |
| 2513 | * depends on synchronize_rcu_tasks() and ksoftirqd, that is |
| 2514 | * not spawned in early initcall. So delay the optimization. |
| 2515 | */ |
| 2516 | optimize_all_kprobes(); |
| 2517 | |
| 2518 | return 0; |
| 2519 | } |
| 2520 | subsys_initcall(init_optprobes); |
| 2521 | #endif |
| 2522 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2523 | #ifdef CONFIG_DEBUG_FS |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2524 | static void report_probe(struct seq_file *pi, struct kprobe *p, |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2525 | const char *sym, int offset, char *modname, struct kprobe *pp) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2526 | { |
| 2527 | char *kprobe_type; |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2528 | void *addr = p->addr; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2529 | |
| 2530 | if (p->pre_handler == pre_handler_kretprobe) |
| 2531 | kprobe_type = "r"; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2532 | else |
| 2533 | kprobe_type = "k"; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2534 | |
Kees Cook | 60f7bb6 | 2020-07-02 15:20:22 -0700 | [diff] [blame] | 2535 | if (!kallsyms_show_value(pi->file->f_cred)) |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2536 | addr = NULL; |
| 2537 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2538 | if (sym) |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2539 | seq_printf(pi, "%px %s %s+0x%x %s ", |
| 2540 | addr, kprobe_type, sym, offset, |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2541 | (modname ? modname : " ")); |
Masami Hiramatsu | 81365a9 | 2018-04-28 21:36:02 +0900 | [diff] [blame] | 2542 | else /* try to use %pS */ |
| 2543 | seq_printf(pi, "%px %s %pS ", |
| 2544 | addr, kprobe_type, p->addr); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2545 | |
| 2546 | if (!pp) |
| 2547 | pp = p; |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 2548 | seq_printf(pi, "%s%s%s%s\n", |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2549 | (kprobe_gone(p) ? "[GONE]" : ""), |
| 2550 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""), |
Masami Hiramatsu | ae6aa16 | 2012-06-05 19:28:32 +0900 | [diff] [blame] | 2551 | (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""), |
| 2552 | (kprobe_ftrace(pp) ? "[FTRACE]" : "")); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2553 | } |
| 2554 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2555 | static void *kprobe_seq_start(struct seq_file *f, loff_t *pos) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2556 | { |
| 2557 | return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL; |
| 2558 | } |
| 2559 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2560 | 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] | 2561 | { |
| 2562 | (*pos)++; |
| 2563 | if (*pos >= KPROBE_TABLE_SIZE) |
| 2564 | return NULL; |
| 2565 | return pos; |
| 2566 | } |
| 2567 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2568 | static void kprobe_seq_stop(struct seq_file *f, void *v) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2569 | { |
| 2570 | /* Nothing to do */ |
| 2571 | } |
| 2572 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2573 | static int show_kprobe_addr(struct seq_file *pi, void *v) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2574 | { |
| 2575 | struct hlist_head *head; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2576 | struct kprobe *p, *kp; |
| 2577 | const char *sym = NULL; |
| 2578 | unsigned int i = *(loff_t *) v; |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 2579 | unsigned long offset = 0; |
Joe Mario | ab76786 | 2013-11-12 15:10:23 -0800 | [diff] [blame] | 2580 | char *modname, namebuf[KSYM_NAME_LEN]; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2581 | |
| 2582 | head = &kprobe_table[i]; |
| 2583 | preempt_disable(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2584 | hlist_for_each_entry_rcu(p, head, hlist) { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 2585 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2586 | &offset, &modname, namebuf); |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2587 | if (kprobe_aggrprobe(p)) { |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2588 | list_for_each_entry_rcu(kp, &p->list, list) |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2589 | report_probe(pi, kp, sym, offset, modname, p); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2590 | } else |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2591 | report_probe(pi, p, sym, offset, modname, NULL); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2592 | } |
| 2593 | preempt_enable(); |
| 2594 | return 0; |
| 2595 | } |
| 2596 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2597 | static const struct seq_operations kprobes_sops = { |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2598 | .start = kprobe_seq_start, |
| 2599 | .next = kprobe_seq_next, |
| 2600 | .stop = kprobe_seq_stop, |
| 2601 | .show = show_kprobe_addr |
| 2602 | }; |
| 2603 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2604 | DEFINE_SEQ_ATTRIBUTE(kprobes); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2605 | |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2606 | /* kprobes/blacklist -- shows which functions can not be probed */ |
| 2607 | static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos) |
| 2608 | { |
Masami Hiramatsu | 4fdd888 | 2020-03-26 23:49:36 +0900 | [diff] [blame] | 2609 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2610 | return seq_list_start(&kprobe_blacklist, *pos); |
| 2611 | } |
| 2612 | |
| 2613 | static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 2614 | { |
| 2615 | return seq_list_next(v, &kprobe_blacklist, pos); |
| 2616 | } |
| 2617 | |
| 2618 | static int kprobe_blacklist_seq_show(struct seq_file *m, void *v) |
| 2619 | { |
| 2620 | struct kprobe_blacklist_entry *ent = |
| 2621 | list_entry(v, struct kprobe_blacklist_entry, list); |
| 2622 | |
Masami Hiramatsu | ffb9bd6 | 2018-04-28 21:35:32 +0900 | [diff] [blame] | 2623 | /* |
| 2624 | * If /proc/kallsyms is not showing kernel address, we won't |
| 2625 | * show them here either. |
| 2626 | */ |
Kees Cook | 60f7bb6 | 2020-07-02 15:20:22 -0700 | [diff] [blame] | 2627 | if (!kallsyms_show_value(m->file->f_cred)) |
Masami Hiramatsu | ffb9bd6 | 2018-04-28 21:35:32 +0900 | [diff] [blame] | 2628 | seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL, |
| 2629 | (void *)ent->start_addr); |
| 2630 | else |
| 2631 | seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr, |
| 2632 | (void *)ent->end_addr, (void *)ent->start_addr); |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2633 | return 0; |
| 2634 | } |
| 2635 | |
Masami Hiramatsu | 4fdd888 | 2020-03-26 23:49:36 +0900 | [diff] [blame] | 2636 | static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v) |
| 2637 | { |
| 2638 | mutex_unlock(&kprobe_mutex); |
| 2639 | } |
| 2640 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2641 | static const struct seq_operations kprobe_blacklist_sops = { |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2642 | .start = kprobe_blacklist_seq_start, |
| 2643 | .next = kprobe_blacklist_seq_next, |
Masami Hiramatsu | 4fdd888 | 2020-03-26 23:49:36 +0900 | [diff] [blame] | 2644 | .stop = kprobe_blacklist_seq_stop, |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2645 | .show = kprobe_blacklist_seq_show, |
| 2646 | }; |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2647 | DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist); |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2648 | |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2649 | static int arm_all_kprobes(void) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2650 | { |
| 2651 | struct hlist_head *head; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2652 | struct kprobe *p; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2653 | unsigned int i, total = 0, errors = 0; |
| 2654 | int err, ret = 0; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2655 | |
| 2656 | mutex_lock(&kprobe_mutex); |
| 2657 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2658 | /* If kprobes are armed, just return */ |
| 2659 | if (!kprobes_all_disarmed) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2660 | goto already_enabled; |
| 2661 | |
Wang Nan | 977ad48 | 2015-02-13 14:40:24 -0800 | [diff] [blame] | 2662 | /* |
| 2663 | * optimize_kprobe() called by arm_kprobe() checks |
| 2664 | * kprobes_all_disarmed, so set kprobes_all_disarmed before |
| 2665 | * arm_kprobe. |
| 2666 | */ |
| 2667 | kprobes_all_disarmed = false; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2668 | /* Arming kprobes doesn't optimize kprobe itself */ |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2669 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2670 | head = &kprobe_table[i]; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2671 | /* Arm all kprobes on a best-effort basis */ |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2672 | hlist_for_each_entry(p, head, hlist) { |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2673 | if (!kprobe_disabled(p)) { |
| 2674 | err = arm_kprobe(p); |
| 2675 | if (err) { |
| 2676 | errors++; |
| 2677 | ret = err; |
| 2678 | } |
| 2679 | total++; |
| 2680 | } |
| 2681 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2682 | } |
| 2683 | |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2684 | if (errors) |
| 2685 | pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n", |
| 2686 | errors, total); |
| 2687 | else |
| 2688 | pr_info("Kprobes globally enabled\n"); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2689 | |
| 2690 | already_enabled: |
| 2691 | mutex_unlock(&kprobe_mutex); |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2692 | return ret; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2693 | } |
| 2694 | |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2695 | static int disarm_all_kprobes(void) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2696 | { |
| 2697 | struct hlist_head *head; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2698 | struct kprobe *p; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2699 | unsigned int i, total = 0, errors = 0; |
| 2700 | int err, ret = 0; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2701 | |
| 2702 | mutex_lock(&kprobe_mutex); |
| 2703 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2704 | /* If kprobes are already disarmed, just return */ |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 2705 | if (kprobes_all_disarmed) { |
| 2706 | mutex_unlock(&kprobe_mutex); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2707 | return 0; |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 2708 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2709 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2710 | kprobes_all_disarmed = true; |
Masami Hiramatsu | afd6625 | 2010-02-25 08:34:07 -0500 | [diff] [blame] | 2711 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2712 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2713 | head = &kprobe_table[i]; |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2714 | /* Disarm all kprobes on a best-effort basis */ |
Masami Hiramatsu | 7e6a71d | 2020-05-12 17:02:44 +0900 | [diff] [blame] | 2715 | hlist_for_each_entry(p, head, hlist) { |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2716 | if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) { |
| 2717 | err = disarm_kprobe(p, false); |
| 2718 | if (err) { |
| 2719 | errors++; |
| 2720 | ret = err; |
| 2721 | } |
| 2722 | total++; |
| 2723 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2724 | } |
| 2725 | } |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2726 | |
| 2727 | if (errors) |
| 2728 | pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n", |
| 2729 | errors, total); |
| 2730 | else |
| 2731 | pr_info("Kprobes globally disabled\n"); |
| 2732 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2733 | mutex_unlock(&kprobe_mutex); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2734 | |
Masami Hiramatsu | 6274de4 | 2010-12-03 18:54:09 +0900 | [diff] [blame] | 2735 | /* Wait for disarming all kprobes by optimizer */ |
| 2736 | wait_for_kprobe_optimizer(); |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2737 | |
| 2738 | return ret; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | /* |
| 2742 | * XXX: The debugfs bool file interface doesn't allow for callbacks |
| 2743 | * when the bool state is switched. We can reuse that facility when |
| 2744 | * available |
| 2745 | */ |
| 2746 | static ssize_t read_enabled_file_bool(struct file *file, |
| 2747 | char __user *user_buf, size_t count, loff_t *ppos) |
| 2748 | { |
| 2749 | char buf[3]; |
| 2750 | |
Masami Hiramatsu | e579abe | 2009-04-06 19:01:01 -0700 | [diff] [blame] | 2751 | if (!kprobes_all_disarmed) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2752 | buf[0] = '1'; |
| 2753 | else |
| 2754 | buf[0] = '0'; |
| 2755 | buf[1] = '\n'; |
| 2756 | buf[2] = 0x00; |
| 2757 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 2758 | } |
| 2759 | |
| 2760 | static ssize_t write_enabled_file_bool(struct file *file, |
| 2761 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 2762 | { |
| 2763 | char buf[32]; |
Stephen Boyd | efeb156 | 2012-01-12 17:17:11 -0800 | [diff] [blame] | 2764 | size_t buf_size; |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2765 | int ret = 0; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2766 | |
| 2767 | buf_size = min(count, (sizeof(buf)-1)); |
| 2768 | if (copy_from_user(buf, user_buf, buf_size)) |
| 2769 | return -EFAULT; |
| 2770 | |
Mathias Krause | 10fb46d | 2013-07-03 15:05:39 -0700 | [diff] [blame] | 2771 | buf[buf_size] = '\0'; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2772 | switch (buf[0]) { |
| 2773 | case 'y': |
| 2774 | case 'Y': |
| 2775 | case '1': |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2776 | ret = arm_all_kprobes(); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2777 | break; |
| 2778 | case 'n': |
| 2779 | case 'N': |
| 2780 | case '0': |
Jessica Yu | 297f923 | 2018-01-10 00:51:24 +0100 | [diff] [blame] | 2781 | ret = disarm_all_kprobes(); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2782 | break; |
Mathias Krause | 10fb46d | 2013-07-03 15:05:39 -0700 | [diff] [blame] | 2783 | default: |
| 2784 | return -EINVAL; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2785 | } |
| 2786 | |
Jessica Yu | 12310e34 | 2018-01-10 00:51:23 +0100 | [diff] [blame] | 2787 | if (ret) |
| 2788 | return ret; |
| 2789 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2790 | return count; |
| 2791 | } |
| 2792 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2793 | static const struct file_operations fops_kp = { |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2794 | .read = read_enabled_file_bool, |
| 2795 | .write = write_enabled_file_bool, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2796 | .llseek = default_llseek, |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2797 | }; |
| 2798 | |
Masami Hiramatsu | 55479f6 | 2014-04-17 17:17:54 +0900 | [diff] [blame] | 2799 | static int __init debugfs_kprobe_init(void) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2800 | { |
Greg Kroah-Hartman | 8c0fd1f | 2019-01-22 16:21:46 +0100 | [diff] [blame] | 2801 | struct dentry *dir; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2802 | unsigned int value = 1; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2803 | |
| 2804 | dir = debugfs_create_dir("kprobes", NULL); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2805 | |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2806 | debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops); |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2807 | |
Greg Kroah-Hartman | 8c0fd1f | 2019-01-22 16:21:46 +0100 | [diff] [blame] | 2808 | debugfs_create_file("enabled", 0600, dir, &value, &fops_kp); |
Masami Hiramatsu | 6372474 | 2014-04-17 17:18:49 +0900 | [diff] [blame] | 2809 | |
Greg Kroah-Hartman | 8c0fd1f | 2019-01-22 16:21:46 +0100 | [diff] [blame] | 2810 | debugfs_create_file("blacklist", 0400, dir, NULL, |
Kefeng Wang | eac2cece | 2020-06-04 16:51:11 -0700 | [diff] [blame] | 2811 | &kprobe_blacklist_fops); |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 2812 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 2813 | return 0; |
| 2814 | } |
| 2815 | |
| 2816 | late_initcall(debugfs_kprobe_init); |
| 2817 | #endif /* CONFIG_DEBUG_FS */ |