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