blob: 5b5ac76671e7c3e9edaf28ea73de4fdc5d709cf1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
3 * kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation (includes suggestions from
23 * Rusty Russell).
24 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
25 * hlists and exceptions notifier as suggested by Andi Kleen.
26 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
27 * interface to access function arguments.
28 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
29 * exceptions notifier to be first on the priority list.
Hien Nguyenb94cce92005-06-23 00:09:19 -070030 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
31 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
32 * <prasanna@in.ibm.com> added function-return probes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/hash.h>
36#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080037#include <linux/slab.h>
Randy Dunlape3869792007-05-08 00:27:01 -070038#include <linux/stddef.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040039#include <linux/export.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070040#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070041#include <linux/kallsyms.h>
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080042#include <linux/freezer.h>
Srinivasa Ds346fd592007-02-20 13:57:54 -080043#include <linux/seq_file.h>
44#include <linux/debugfs.h>
Masami Hiramatsub2be84d2010-02-25 08:34:15 -050045#include <linux/sysctl.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070046#include <linux/kdebug.h>
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -050047#include <linux/memory.h>
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -050048#include <linux/ftrace.h>
Masami Hiramatsuafd66252010-02-25 08:34:07 -050049#include <linux/cpu.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040050#include <linux/jump_label.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070051
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -070052#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/cacheflush.h>
54#include <asm/errno.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070055#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define KPROBE_HASH_BITS 6
58#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
59
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070060
61/*
62 * Some oddball architectures like 64bit powerpc have function descriptors
63 * so this must be overridable.
64 */
65#ifndef kprobe_lookup_name
66#define kprobe_lookup_name(name, addr) \
67 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
68#endif
69
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070070static int kprobes_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070072static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070074/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070075static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070076
Masami Hiramatsu43948f52010-10-25 22:18:01 +090077/* This protects kprobe_table and optimizing_list */
78static DEFINE_MUTEX(kprobe_mutex);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080079static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070080static struct {
Thomas Gleixnerec4846082009-07-25 16:09:17 +020081 raw_spinlock_t lock ____cacheline_aligned_in_smp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070082} kretprobe_table_locks[KPROBE_TABLE_SIZE];
83
Thomas Gleixnerec4846082009-07-25 16:09:17 +020084static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070085{
86 return &(kretprobe_table_locks[hash].lock);
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070089/*
90 * Normally, functions that we'd want to prohibit kprobes in, are marked
91 * __kprobes. But, there are cases where such functions already belong to
92 * a different section (__sched for preempt_schedule)
93 *
94 * For such cases, we now have a blacklist
95 */
Daniel Guilak544304b2008-07-10 09:38:19 -070096static struct kprobe_blackpoint kprobe_blacklist[] = {
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070097 {"preempt_schedule",},
Masami Hiramatsu65e234e2009-08-27 13:23:32 -040098 {"native_get_debugreg",},
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070099 {NULL} /* Terminator */
100};
101
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800102#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700103/*
104 * kprobe->ainsn.insn points to the copy of the instruction to be
105 * single-stepped. x86_64, POWER4 and above have no-exec support and
106 * stepping on the instruction on a vmalloced/kmalloced/data page
107 * is a recipe for disaster
108 */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700109struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400110 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700111 kprobe_opcode_t *insns; /* Page of instruction slots */
Heiko Carstensaf963972013-09-11 14:24:13 -0700112 struct kprobe_insn_cache *cache;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700113 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800114 int ngarbage;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500115 char slot_used[];
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700116};
117
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500118#define KPROBE_INSN_PAGE_SIZE(slots) \
119 (offsetof(struct kprobe_insn_page, slot_used) + \
120 (sizeof(char) * (slots)))
121
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500122static int slots_per_page(struct kprobe_insn_cache *c)
123{
124 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
125}
126
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800127enum kprobe_slot_state {
128 SLOT_CLEAN = 0,
129 SLOT_DIRTY = 1,
130 SLOT_USED = 2,
131};
132
Heiko Carstensaf963972013-09-11 14:24:13 -0700133static void *alloc_insn_page(void)
134{
135 return module_alloc(PAGE_SIZE);
136}
137
138static void free_insn_page(void *page)
139{
140 module_free(NULL, page);
141}
142
Heiko Carstensc802d642013-09-11 14:24:11 -0700143struct kprobe_insn_cache kprobe_insn_slots = {
144 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700145 .alloc = alloc_insn_page,
146 .free = free_insn_page,
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500147 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
148 .insn_size = MAX_INSN_SIZE,
149 .nr_garbage = 0,
150};
151static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800152
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700153/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800154 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700155 * We allocate an executable page if there's no room on existing ones.
156 */
Heiko Carstensc802d642013-09-11 14:24:11 -0700157kprobe_opcode_t __kprobes *__get_insn_slot(struct kprobe_insn_cache *c)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700158{
159 struct kprobe_insn_page *kip;
Heiko Carstensc802d642013-09-11 14:24:11 -0700160 kprobe_opcode_t *slot = NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700161
Heiko Carstensc802d642013-09-11 14:24:11 -0700162 mutex_lock(&c->mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700163 retry:
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500164 list_for_each_entry(kip, &c->pages, list) {
165 if (kip->nused < slots_per_page(c)) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700166 int i;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500167 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800168 if (kip->slot_used[i] == SLOT_CLEAN) {
169 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700170 kip->nused++;
Heiko Carstensc802d642013-09-11 14:24:11 -0700171 slot = kip->insns + (i * c->insn_size);
172 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700173 }
174 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500175 /* kip->nused is broken. Fix it. */
176 kip->nused = slots_per_page(c);
177 WARN_ON(1);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700178 }
179 }
180
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800181 /* If there are any garbage slots, collect it and try again. */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500182 if (c->nr_garbage && collect_garbage_slots(c) == 0)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800183 goto retry;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500184
185 /* All out of space. Need to allocate a new page. */
186 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700187 if (!kip)
Heiko Carstensc802d642013-09-11 14:24:11 -0700188 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700189
190 /*
191 * Use module_alloc so this page is within +/- 2GB of where the
192 * kernel image and loaded module images reside. This is required
193 * so x86_64 can correctly handle the %rip-relative fixups.
194 */
Heiko Carstensaf963972013-09-11 14:24:13 -0700195 kip->insns = c->alloc();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700196 if (!kip->insns) {
197 kfree(kip);
Heiko Carstensc802d642013-09-11 14:24:11 -0700198 goto out;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700199 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400200 INIT_LIST_HEAD(&kip->list);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500201 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800202 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700203 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800204 kip->ngarbage = 0;
Heiko Carstensaf963972013-09-11 14:24:13 -0700205 kip->cache = c;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500206 list_add(&kip->list, &c->pages);
Heiko Carstensc802d642013-09-11 14:24:11 -0700207 slot = kip->insns;
208out:
209 mutex_unlock(&c->mutex);
210 return slot;
Masami Hiramatsu12941562009-01-06 14:41:50 -0800211}
212
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800213/* Return 1 if all garbages are collected, otherwise 0. */
214static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
215{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800216 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800217 kip->nused--;
218 if (kip->nused == 0) {
219 /*
220 * Page is no longer in use. Free it unless
221 * it's the last one. We keep the last one
222 * so as not to have to set it up again the
223 * next time somebody inserts a probe.
224 */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500225 if (!list_is_singular(&kip->list)) {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400226 list_del(&kip->list);
Heiko Carstensaf963972013-09-11 14:24:13 -0700227 kip->cache->free(kip->insns);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800228 kfree(kip);
229 }
230 return 1;
231 }
232 return 0;
233}
234
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500235static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800236{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400237 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800238
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500239 /* Ensure no-one is interrupted on the garbages */
240 synchronize_sched();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800241
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500242 list_for_each_entry_safe(kip, next, &c->pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800243 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800244 if (kip->ngarbage == 0)
245 continue;
246 kip->ngarbage = 0; /* we will collect all garbages */
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500247 for (i = 0; i < slots_per_page(c); i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800248 if (kip->slot_used[i] == SLOT_DIRTY &&
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800249 collect_one_slot(kip, i))
250 break;
251 }
252 }
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500253 c->nr_garbage = 0;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800254 return 0;
255}
256
Heiko Carstensc802d642013-09-11 14:24:11 -0700257void __kprobes __free_insn_slot(struct kprobe_insn_cache *c,
258 kprobe_opcode_t *slot, int dirty)
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500259{
260 struct kprobe_insn_page *kip;
261
Heiko Carstensc802d642013-09-11 14:24:11 -0700262 mutex_lock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500263 list_for_each_entry(kip, &c->pages, list) {
Masami Hiramatsu83ff56f2010-03-09 10:22:19 -0500264 long idx = ((long)slot - (long)kip->insns) /
265 (c->insn_size * sizeof(kprobe_opcode_t));
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500266 if (idx >= 0 && idx < slots_per_page(c)) {
267 WARN_ON(kip->slot_used[idx] != SLOT_USED);
268 if (dirty) {
269 kip->slot_used[idx] = SLOT_DIRTY;
270 kip->ngarbage++;
271 if (++c->nr_garbage > slots_per_page(c))
272 collect_garbage_slots(c);
273 } else
274 collect_one_slot(kip, idx);
Heiko Carstensc802d642013-09-11 14:24:11 -0700275 goto out;
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500276 }
277 }
278 /* Could not free this slot. */
279 WARN_ON(1);
Heiko Carstensc802d642013-09-11 14:24:11 -0700280out:
281 mutex_unlock(&c->mutex);
Masami Hiramatsu4610ee12010-02-25 08:33:59 -0500282}
283
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500284#ifdef CONFIG_OPTPROBES
285/* For optimized_kprobe buffer */
Heiko Carstensc802d642013-09-11 14:24:11 -0700286struct kprobe_insn_cache kprobe_optinsn_slots = {
287 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
Heiko Carstensaf963972013-09-11 14:24:13 -0700288 .alloc = alloc_insn_page,
289 .free = free_insn_page,
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500290 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
291 /* .insn_size is initialized later */
292 .nr_garbage = 0,
293};
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500294#endif
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800295#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700296
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800297/* We have preemption disabled.. so it is safe to use __ versions */
298static inline void set_kprobe_instance(struct kprobe *kp)
299{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600300 __this_cpu_write(kprobe_instance, kp);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800301}
302
303static inline void reset_kprobe_instance(void)
304{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600305 __this_cpu_write(kprobe_instance, NULL);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800306}
307
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800308/*
309 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800310 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800311 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800312 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800313 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700314struct kprobe __kprobes *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct hlist_head *head;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800317 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800320 hlist_for_each_entry_rcu(p, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (p->addr == addr)
322 return p;
323 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return NULL;
326}
327
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500328static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
329
330/* Return true if the kprobe is an aggregator */
331static inline int kprobe_aggrprobe(struct kprobe *p)
332{
333 return p->pre_handler == aggr_pre_handler;
334}
335
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900336/* Return true(!0) if the kprobe is unused */
337static inline int kprobe_unused(struct kprobe *p)
338{
339 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
340 list_empty(&p->list);
341}
342
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500343/*
344 * Keep all fields in the kprobe consistent
345 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900346static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500347{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900348 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
349 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500350}
351
352#ifdef CONFIG_OPTPROBES
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500353/* NOTE: change this value only with kprobe_mutex held */
354static bool kprobes_allow_optimization;
355
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500356/*
357 * Call all pre_handler on the list, but ignores its return value.
358 * This must be called from arch-dep optimized caller.
359 */
360void __kprobes opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
361{
362 struct kprobe *kp;
363
364 list_for_each_entry_rcu(kp, &p->list, list) {
365 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
366 set_kprobe_instance(kp);
367 kp->pre_handler(kp, regs);
368 }
369 reset_kprobe_instance();
370 }
371}
372
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900373/* Free optimized instructions and optimized_kprobe */
374static __kprobes void free_aggr_kprobe(struct kprobe *p)
375{
376 struct optimized_kprobe *op;
377
378 op = container_of(p, struct optimized_kprobe, kp);
379 arch_remove_optimized_kprobe(op);
380 arch_remove_kprobe(p);
381 kfree(op);
382}
383
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500384/* Return true(!0) if the kprobe is ready for optimization. */
385static inline int kprobe_optready(struct kprobe *p)
386{
387 struct optimized_kprobe *op;
388
389 if (kprobe_aggrprobe(p)) {
390 op = container_of(p, struct optimized_kprobe, kp);
391 return arch_prepared_optinsn(&op->optinsn);
392 }
393
394 return 0;
395}
396
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900397/* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
398static inline int kprobe_disarmed(struct kprobe *p)
399{
400 struct optimized_kprobe *op;
401
402 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
403 if (!kprobe_aggrprobe(p))
404 return kprobe_disabled(p);
405
406 op = container_of(p, struct optimized_kprobe, kp);
407
408 return kprobe_disabled(p) && list_empty(&op->list);
409}
410
411/* Return true(!0) if the probe is queued on (un)optimizing lists */
412static int __kprobes kprobe_queued(struct kprobe *p)
413{
414 struct optimized_kprobe *op;
415
416 if (kprobe_aggrprobe(p)) {
417 op = container_of(p, struct optimized_kprobe, kp);
418 if (!list_empty(&op->list))
419 return 1;
420 }
421 return 0;
422}
423
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500424/*
425 * Return an optimized kprobe whose optimizing code replaces
426 * instructions including addr (exclude breakpoint).
427 */
Namhyung Kim6376b2292010-09-15 10:04:28 +0900428static struct kprobe *__kprobes get_optimized_kprobe(unsigned long addr)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500429{
430 int i;
431 struct kprobe *p = NULL;
432 struct optimized_kprobe *op;
433
434 /* Don't check i == 0, since that is a breakpoint case. */
435 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
436 p = get_kprobe((void *)(addr - i));
437
438 if (p && kprobe_optready(p)) {
439 op = container_of(p, struct optimized_kprobe, kp);
440 if (arch_within_optimized_kprobe(op, addr))
441 return p;
442 }
443
444 return NULL;
445}
446
447/* Optimization staging list, protected by kprobe_mutex */
448static LIST_HEAD(optimizing_list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900449static LIST_HEAD(unoptimizing_list);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900450static LIST_HEAD(freeing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500451
452static void kprobe_optimizer(struct work_struct *work);
453static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
454#define OPTIMIZE_DELAY 5
455
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900456/*
457 * Optimize (replace a breakpoint with a jump) kprobes listed on
458 * optimizing_list.
459 */
460static __kprobes void do_optimize_kprobes(void)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500461{
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900462 /* Optimization never be done when disarmed */
463 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
464 list_empty(&optimizing_list))
465 return;
466
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500467 /*
468 * The optimization/unoptimization refers online_cpus via
469 * stop_machine() and cpu-hotplug modifies online_cpus.
470 * And same time, text_mutex will be held in cpu-hotplug and here.
471 * This combination can cause a deadlock (cpu-hotplug try to lock
472 * text_mutex but stop_machine can not be done because online_cpus
473 * has been changed)
474 * To avoid this deadlock, we need to call get_online_cpus()
475 * for preventing cpu-hotplug outside of text_mutex locking.
476 */
477 get_online_cpus();
478 mutex_lock(&text_mutex);
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900479 arch_optimize_kprobes(&optimizing_list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500480 mutex_unlock(&text_mutex);
481 put_online_cpus();
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900482}
483
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900484/*
485 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
486 * if need) kprobes listed on unoptimizing_list.
487 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900488static __kprobes void do_unoptimize_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900489{
490 struct optimized_kprobe *op, *tmp;
491
492 /* Unoptimization must be done anytime */
493 if (list_empty(&unoptimizing_list))
494 return;
495
496 /* Ditto to do_optimize_kprobes */
497 get_online_cpus();
498 mutex_lock(&text_mutex);
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900499 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900500 /* Loop free_list for disarming */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900501 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900502 /* Disarm probes if marked disabled */
503 if (kprobe_disabled(&op->kp))
504 arch_disarm_kprobe(&op->kp);
505 if (kprobe_unused(&op->kp)) {
506 /*
507 * Remove unused probes from hash list. After waiting
508 * for synchronization, these probes are reclaimed.
509 * (reclaiming is done by do_free_cleaned_kprobes.)
510 */
511 hlist_del_rcu(&op->kp.hlist);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900512 } else
513 list_del_init(&op->list);
514 }
515 mutex_unlock(&text_mutex);
516 put_online_cpus();
517}
518
519/* Reclaim all kprobes on the free_list */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900520static __kprobes void do_free_cleaned_kprobes(void)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900521{
522 struct optimized_kprobe *op, *tmp;
523
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900524 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900525 BUG_ON(!kprobe_unused(&op->kp));
526 list_del_init(&op->list);
527 free_aggr_kprobe(&op->kp);
528 }
529}
530
531/* Start optimizer after OPTIMIZE_DELAY passed */
532static __kprobes void kick_kprobe_optimizer(void)
533{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800534 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900535}
536
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900537/* Kprobe jump optimizer */
538static __kprobes void kprobe_optimizer(struct work_struct *work)
539{
Steven Rostedt72ef3792012-06-05 19:28:14 +0900540 mutex_lock(&kprobe_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900541 /* Lock modules while optimizing kprobes */
542 mutex_lock(&module_mutex);
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900543
544 /*
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900545 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
546 * kprobes before waiting for quiesence period.
547 */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900548 do_unoptimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900549
550 /*
551 * Step 2: Wait for quiesence period to ensure all running interrupts
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900552 * are done. Because optprobe may modify multiple instructions
553 * there is a chance that Nth instruction is interrupted. In that
554 * case, running interrupt can return to 2nd-Nth byte of jump
555 * instruction. This wait is for avoiding it.
556 */
557 synchronize_sched();
558
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900559 /* Step 3: Optimize kprobes after quiesence period */
Masami Hiramatsu61f4e132010-12-03 18:54:03 +0900560 do_optimize_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900561
562 /* Step 4: Free cleaned kprobes after quiesence period */
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900563 do_free_cleaned_kprobes();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900564
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500565 mutex_unlock(&module_mutex);
Steven Rostedt72ef3792012-06-05 19:28:14 +0900566 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900567
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900568 /* Step 5: Kick optimizer again if needed */
Masami Hiramatsuf984ba42010-12-03 18:54:34 +0900569 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +0900570 kick_kprobe_optimizer();
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900571}
572
573/* Wait for completing optimization and unoptimization */
574static __kprobes void wait_for_kprobe_optimizer(void)
575{
Tejun Heoad72b3b2012-12-21 17:57:00 -0800576 mutex_lock(&kprobe_mutex);
577
578 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
579 mutex_unlock(&kprobe_mutex);
580
581 /* this will also make optimizing_work execute immmediately */
582 flush_delayed_work(&optimizing_work);
583 /* @optimizing_work might not have been queued yet, relax */
584 cpu_relax();
585
586 mutex_lock(&kprobe_mutex);
587 }
588
589 mutex_unlock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500590}
591
592/* Optimize kprobe if p is ready to be optimized */
593static __kprobes void optimize_kprobe(struct kprobe *p)
594{
595 struct optimized_kprobe *op;
596
597 /* Check if the kprobe is disabled or not ready for optimization. */
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500598 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500599 (kprobe_disabled(p) || kprobes_all_disarmed))
600 return;
601
602 /* Both of break_handler and post_handler are not supported. */
603 if (p->break_handler || p->post_handler)
604 return;
605
606 op = container_of(p, struct optimized_kprobe, kp);
607
608 /* Check there is no other kprobes at the optimized instructions */
609 if (arch_check_optimized_kprobe(op) < 0)
610 return;
611
612 /* Check if it is already optimized. */
613 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED)
614 return;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500615 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900616
617 if (!list_empty(&op->list))
618 /* This is under unoptimizing. Just dequeue the probe */
619 list_del_init(&op->list);
620 else {
621 list_add(&op->list, &optimizing_list);
622 kick_kprobe_optimizer();
623 }
624}
625
626/* Short cut to direct unoptimizing */
627static __kprobes void force_unoptimize_kprobe(struct optimized_kprobe *op)
628{
629 get_online_cpus();
630 arch_unoptimize_kprobe(op);
631 put_online_cpus();
632 if (kprobe_disabled(&op->kp))
633 arch_disarm_kprobe(&op->kp);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500634}
635
636/* Unoptimize a kprobe if p is optimized */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900637static __kprobes void unoptimize_kprobe(struct kprobe *p, bool force)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500638{
639 struct optimized_kprobe *op;
640
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900641 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
642 return; /* This is not an optprobe nor optimized */
643
644 op = container_of(p, struct optimized_kprobe, kp);
645 if (!kprobe_optimized(p)) {
646 /* Unoptimized or unoptimizing case */
647 if (force && !list_empty(&op->list)) {
648 /*
649 * Only if this is unoptimizing kprobe and forced,
650 * forcibly unoptimize it. (No need to unoptimize
651 * unoptimized kprobe again :)
652 */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500653 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900654 force_unoptimize_kprobe(op);
655 }
656 return;
657 }
658
659 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
660 if (!list_empty(&op->list)) {
661 /* Dequeue from the optimization queue */
662 list_del_init(&op->list);
663 return;
664 }
665 /* Optimized kprobe case */
666 if (force)
667 /* Forcibly update the code: this is a special case */
668 force_unoptimize_kprobe(op);
669 else {
670 list_add(&op->list, &unoptimizing_list);
671 kick_kprobe_optimizer();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500672 }
673}
674
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900675/* Cancel unoptimizing for reusing */
676static void reuse_unused_kprobe(struct kprobe *ap)
677{
678 struct optimized_kprobe *op;
679
680 BUG_ON(!kprobe_unused(ap));
681 /*
682 * Unused kprobe MUST be on the way of delayed unoptimizing (means
683 * there is still a relative jump) and disabled.
684 */
685 op = container_of(ap, struct optimized_kprobe, kp);
686 if (unlikely(list_empty(&op->list)))
687 printk(KERN_WARNING "Warning: found a stray unused "
688 "aggrprobe@%p\n", ap->addr);
689 /* Enable the probe again */
690 ap->flags &= ~KPROBE_FLAG_DISABLED;
691 /* Optimize it again (remove from op->list) */
692 BUG_ON(!kprobe_optready(ap));
693 optimize_kprobe(ap);
694}
695
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500696/* Remove optimized instructions */
697static void __kprobes kill_optimized_kprobe(struct kprobe *p)
698{
699 struct optimized_kprobe *op;
700
701 op = container_of(p, struct optimized_kprobe, kp);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900702 if (!list_empty(&op->list))
703 /* Dequeue from the (un)optimization queue */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500704 list_del_init(&op->list);
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900705 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
Masami Hiramatsu7b959fc2013-05-22 18:34:09 +0900706
707 if (kprobe_unused(p)) {
708 /* Enqueue if it is unused */
709 list_add(&op->list, &freeing_list);
710 /*
711 * Remove unused probes from the hash list. After waiting
712 * for synchronization, this probe is reclaimed.
713 * (reclaiming is done by do_free_cleaned_kprobes().)
714 */
715 hlist_del_rcu(&op->kp.hlist);
716 }
717
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900718 /* Don't touch the code, because it is already freed. */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500719 arch_remove_optimized_kprobe(op);
720}
721
722/* Try to prepare optimized instructions */
723static __kprobes void prepare_optimized_kprobe(struct kprobe *p)
724{
725 struct optimized_kprobe *op;
726
727 op = container_of(p, struct optimized_kprobe, kp);
728 arch_prepare_optimized_kprobe(op);
729}
730
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500731/* Allocate new optimized_kprobe and try to prepare optimized instructions */
732static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
733{
734 struct optimized_kprobe *op;
735
736 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
737 if (!op)
738 return NULL;
739
740 INIT_LIST_HEAD(&op->list);
741 op->kp.addr = p->addr;
742 arch_prepare_optimized_kprobe(op);
743
744 return &op->kp;
745}
746
747static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
748
749/*
750 * Prepare an optimized_kprobe and optimize it
751 * NOTE: p must be a normal registered kprobe
752 */
753static __kprobes void try_to_optimize_kprobe(struct kprobe *p)
754{
755 struct kprobe *ap;
756 struct optimized_kprobe *op;
757
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900758 /* Impossible to optimize ftrace-based kprobe */
759 if (kprobe_ftrace(p))
760 return;
761
Masami Hiramatsu25764282012-06-05 19:28:26 +0900762 /* For preparing optimization, jump_label_text_reserved() is called */
763 jump_label_lock();
764 mutex_lock(&text_mutex);
765
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500766 ap = alloc_aggr_kprobe(p);
767 if (!ap)
Masami Hiramatsu25764282012-06-05 19:28:26 +0900768 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500769
770 op = container_of(ap, struct optimized_kprobe, kp);
771 if (!arch_prepared_optinsn(&op->optinsn)) {
772 /* If failed to setup optimizing, fallback to kprobe */
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900773 arch_remove_optimized_kprobe(op);
774 kfree(op);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900775 goto out;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500776 }
777
778 init_aggr_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +0900779 optimize_kprobe(ap); /* This just kicks optimizer thread */
780
781out:
782 mutex_unlock(&text_mutex);
783 jump_label_unlock();
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500784}
785
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500786#ifdef CONFIG_SYSCTL
787static void __kprobes optimize_all_kprobes(void)
788{
789 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500790 struct kprobe *p;
791 unsigned int i;
792
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900793 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500794 /* If optimization is already allowed, just return */
795 if (kprobes_allow_optimization)
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900796 goto out;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500797
798 kprobes_allow_optimization = true;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500799 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
800 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800801 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500802 if (!kprobe_disabled(p))
803 optimize_kprobe(p);
804 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500805 printk(KERN_INFO "Kprobes globally optimized\n");
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900806out:
807 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500808}
809
810static void __kprobes unoptimize_all_kprobes(void)
811{
812 struct hlist_head *head;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500813 struct kprobe *p;
814 unsigned int i;
815
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900816 mutex_lock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500817 /* If optimization is already prohibited, just return */
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900818 if (!kprobes_allow_optimization) {
819 mutex_unlock(&kprobe_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500820 return;
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900821 }
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500822
823 kprobes_allow_optimization = false;
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500824 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
825 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800826 hlist_for_each_entry_rcu(p, head, hlist) {
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500827 if (!kprobe_disabled(p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900828 unoptimize_kprobe(p, false);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500829 }
830 }
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900831 mutex_unlock(&kprobe_mutex);
832
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900833 /* Wait for unoptimizing completion */
834 wait_for_kprobe_optimizer();
835 printk(KERN_INFO "Kprobes globally unoptimized\n");
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500836}
837
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900838static DEFINE_MUTEX(kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500839int sysctl_kprobes_optimization;
840int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
841 void __user *buffer, size_t *length,
842 loff_t *ppos)
843{
844 int ret;
845
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900846 mutex_lock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500847 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
848 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
849
850 if (sysctl_kprobes_optimization)
851 optimize_all_kprobes();
852 else
853 unoptimize_all_kprobes();
Masami Hiramatsu5c515432013-04-18 18:33:18 +0900854 mutex_unlock(&kprobe_sysctl_mutex);
Masami Hiramatsub2be84d2010-02-25 08:34:15 -0500855
856 return ret;
857}
858#endif /* CONFIG_SYSCTL */
859
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900860/* Put a breakpoint for a probe. Must be called with text_mutex locked */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500861static void __kprobes __arm_kprobe(struct kprobe *p)
862{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900863 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500864
865 /* Check collision with other optimized kprobes */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900866 _p = get_optimized_kprobe((unsigned long)p->addr);
867 if (unlikely(_p))
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900868 /* Fallback to unoptimized kprobe */
869 unoptimize_kprobe(_p, true);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500870
871 arch_arm_kprobe(p);
872 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
873}
874
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900875/* Remove the breakpoint of a probe. Must be called with text_mutex locked */
876static void __kprobes __disarm_kprobe(struct kprobe *p, bool reopt)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500877{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +0900878 struct kprobe *_p;
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500879
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900880 unoptimize_kprobe(p, false); /* Try to unoptimize */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500881
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900882 if (!kprobe_queued(p)) {
883 arch_disarm_kprobe(p);
884 /* If another kprobe was blocked, optimize it. */
885 _p = get_optimized_kprobe((unsigned long)p->addr);
886 if (unlikely(_p) && reopt)
887 optimize_kprobe(_p);
888 }
889 /* TODO: reoptimize others after unoptimized this probe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500890}
891
892#else /* !CONFIG_OPTPROBES */
893
894#define optimize_kprobe(p) do {} while (0)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900895#define unoptimize_kprobe(p, f) do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500896#define kill_optimized_kprobe(p) do {} while (0)
897#define prepare_optimized_kprobe(p) do {} while (0)
898#define try_to_optimize_kprobe(p) do {} while (0)
899#define __arm_kprobe(p) arch_arm_kprobe(p)
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900900#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
901#define kprobe_disarmed(p) kprobe_disabled(p)
902#define wait_for_kprobe_optimizer() do {} while (0)
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500903
Masami Hiramatsu0490cd12010-12-03 18:54:16 +0900904/* There should be no unused kprobes can be reused without optimization */
905static void reuse_unused_kprobe(struct kprobe *ap)
906{
907 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
908 BUG_ON(kprobe_unused(ap));
909}
910
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500911static __kprobes void free_aggr_kprobe(struct kprobe *p)
912{
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900913 arch_remove_kprobe(p);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500914 kfree(p);
915}
916
917static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
918{
919 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
920}
921#endif /* CONFIG_OPTPROBES */
922
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900923#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900924static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
Masami Hiramatsue5253892012-06-05 19:28:38 +0900925 .func = kprobe_ftrace_handler,
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900926 .flags = FTRACE_OPS_FL_SAVE_REGS,
927};
928static int kprobe_ftrace_enabled;
929
930/* Must ensure p->addr is really on ftrace */
931static int __kprobes prepare_kprobe(struct kprobe *p)
932{
933 if (!kprobe_ftrace(p))
934 return arch_prepare_kprobe(p);
935
936 return arch_prepare_kprobe_ftrace(p);
937}
938
939/* Caller must lock kprobe_mutex */
940static void __kprobes arm_kprobe_ftrace(struct kprobe *p)
941{
942 int ret;
943
944 ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
945 (unsigned long)p->addr, 0, 0);
946 WARN(ret < 0, "Failed to arm kprobe-ftrace at %p (%d)\n", p->addr, ret);
947 kprobe_ftrace_enabled++;
948 if (kprobe_ftrace_enabled == 1) {
949 ret = register_ftrace_function(&kprobe_ftrace_ops);
950 WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret);
951 }
952}
953
954/* Caller must lock kprobe_mutex */
955static void __kprobes disarm_kprobe_ftrace(struct kprobe *p)
956{
957 int ret;
958
959 kprobe_ftrace_enabled--;
960 if (kprobe_ftrace_enabled == 0) {
961 ret = unregister_ftrace_function(&kprobe_ftrace_ops);
962 WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret);
963 }
964 ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
965 (unsigned long)p->addr, 1, 0);
966 WARN(ret < 0, "Failed to disarm kprobe-ftrace at %p (%d)\n", p->addr, ret);
967}
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900968#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900969#define prepare_kprobe(p) arch_prepare_kprobe(p)
970#define arm_kprobe_ftrace(p) do {} while (0)
971#define disarm_kprobe_ftrace(p) do {} while (0)
972#endif
973
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400974/* Arm a kprobe with text_mutex */
975static void __kprobes arm_kprobe(struct kprobe *kp)
976{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900977 if (unlikely(kprobe_ftrace(kp))) {
978 arm_kprobe_ftrace(kp);
979 return;
980 }
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500981 /*
982 * Here, since __arm_kprobe() doesn't use stop_machine(),
983 * this doesn't cause deadlock on text_mutex. So, we don't
984 * need get_online_cpus().
985 */
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400986 mutex_lock(&text_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -0500987 __arm_kprobe(kp);
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400988 mutex_unlock(&text_mutex);
989}
990
991/* Disarm a kprobe with text_mutex */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900992static void __kprobes disarm_kprobe(struct kprobe *kp, bool reopt)
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400993{
Masami Hiramatsuae6aa162012-06-05 19:28:32 +0900994 if (unlikely(kprobe_ftrace(kp))) {
995 disarm_kprobe_ftrace(kp);
996 return;
997 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +0900998 /* Ditto */
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400999 mutex_lock(&text_mutex);
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001000 __disarm_kprobe(kp, reopt);
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001001 mutex_unlock(&text_mutex);
1002}
1003
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001004/*
1005 * Aggregate handlers for multiple kprobes support - these handlers
1006 * take care of invoking the individual kprobe handlers on p->list
1007 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001008static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001009{
1010 struct kprobe *kp;
1011
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001012 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001013 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001014 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001015 if (kp->pre_handler(kp, regs))
1016 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001017 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001018 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001019 }
1020 return 0;
1021}
1022
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001023static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1024 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001025{
1026 struct kprobe *kp;
1027
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001028 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001029 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001030 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001031 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001032 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001033 }
1034 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001035}
1036
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001037static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1038 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001039{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001040 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001041
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001042 /*
1043 * if we faulted "during" the execution of a user specified
1044 * probe handler, invoke just that probe's fault handler
1045 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001046 if (cur && cur->fault_handler) {
1047 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001048 return 1;
1049 }
1050 return 0;
1051}
1052
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001053static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001054{
Christoph Lameterb76834b2010-12-06 11:16:25 -06001055 struct kprobe *cur = __this_cpu_read(kprobe_instance);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001056 int ret = 0;
1057
1058 if (cur && cur->break_handler) {
1059 if (cur->break_handler(cur, regs))
1060 ret = 1;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001061 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -08001062 reset_kprobe_instance();
1063 return ret;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001064}
1065
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001066/* Walks the list and increments nmissed count for multiprobe case */
1067void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
1068{
1069 struct kprobe *kp;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001070 if (!kprobe_aggrprobe(p)) {
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -08001071 p->nmissed++;
1072 } else {
1073 list_for_each_entry_rcu(kp, &p->list, list)
1074 kp->nmissed++;
1075 }
1076 return;
1077}
1078
bibo,mao99219a32006-10-02 02:17:35 -07001079void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
1080 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001081{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001082 struct kretprobe *rp = ri->rp;
1083
Hien Nguyenb94cce92005-06-23 00:09:19 -07001084 /* remove rp inst off the rprobe_inst_table */
1085 hlist_del(&ri->hlist);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001086 INIT_HLIST_NODE(&ri->hlist);
1087 if (likely(rp)) {
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001088 raw_spin_lock(&rp->lock);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001089 hlist_add_head(&ri->hlist, &rp->free_instances);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001090 raw_spin_unlock(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001091 } else
1092 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -07001093 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001094}
1095
Masami Hiramatsu017c39b2009-01-06 14:41:51 -08001096void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001097 struct hlist_head **head, unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001098__acquires(hlist_lock)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001099{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001100 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001101 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001102
1103 *head = &kretprobe_inst_table[hash];
1104 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001105 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001106}
1107
Masami Hiramatsu017c39b2009-01-06 14:41:51 -08001108static void __kprobes kretprobe_table_lock(unsigned long hash,
1109 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001110__acquires(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001111{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001112 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1113 raw_spin_lock_irqsave(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001114}
1115
Masami Hiramatsu017c39b2009-01-06 14:41:51 -08001116void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
1117 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001118__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001119{
1120 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001121 raw_spinlock_t *hlist_lock;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001122
1123 hlist_lock = kretprobe_table_lock_ptr(hash);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001124 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001125}
1126
Namhyung Kim6376b2292010-09-15 10:04:28 +09001127static void __kprobes kretprobe_table_unlock(unsigned long hash,
1128 unsigned long *flags)
Namhyung Kim635c17c2010-09-15 10:04:30 +09001129__releases(hlist_lock)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001130{
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001131 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1132 raw_spin_unlock_irqrestore(hlist_lock, *flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001133}
1134
Hien Nguyenb94cce92005-06-23 00:09:19 -07001135/*
bibo maoc6fd91f2006-03-26 01:38:20 -08001136 * This function is called from finish_task_switch when task tk becomes dead,
1137 * so that we can recycle any function-return probe instances associated
1138 * with this task. These left over instances represent probed functions
1139 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -07001140 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001141void __kprobes kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001142{
bibo,mao62c27be2006-10-02 02:17:33 -07001143 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -07001144 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001145 struct hlist_node *tmp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001146 unsigned long hash, flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -07001147
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001148 if (unlikely(!kprobes_initialized))
1149 /* Early boot. kretprobe_table_locks not yet initialized. */
1150 return;
1151
Ananth N Mavinakayanahallid496aab2012-01-20 14:34:04 -08001152 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001153 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1154 head = &kretprobe_inst_table[hash];
1155 kretprobe_table_lock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001156 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -07001157 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -07001158 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -07001159 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001160 kretprobe_table_unlock(hash, &flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001161 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -07001162 hlist_del(&ri->hlist);
1163 kfree(ri);
1164 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001165}
1166
Hien Nguyenb94cce92005-06-23 00:09:19 -07001167static inline void free_rp_inst(struct kretprobe *rp)
1168{
1169 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001170 struct hlist_node *next;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001171
Sasha Levinb67bfe02013-02-27 17:06:00 -08001172 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001173 hlist_del(&ri->hlist);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001174 kfree(ri);
1175 }
1176}
1177
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001178static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
1179{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001180 unsigned long flags, hash;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001181 struct kretprobe_instance *ri;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001182 struct hlist_node *next;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001183 struct hlist_head *head;
1184
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001185 /* No race here */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001186 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1187 kretprobe_table_lock(hash, &flags);
1188 head = &kretprobe_inst_table[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001189 hlist_for_each_entry_safe(ri, next, head, hlist) {
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001190 if (ri->rp == rp)
1191 ri->rp = NULL;
1192 }
1193 kretprobe_table_unlock(hash, &flags);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001194 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001195 free_rp_inst(rp);
1196}
1197
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001198/*
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001199* Add the new probe to ap->list. Fail if this is the
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001200* second jprobe at the address - two jprobes can't coexist
1201*/
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001202static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001203{
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001204 BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001205
1206 if (p->break_handler || p->post_handler)
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001207 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001208
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001209 if (p->break_handler) {
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001210 if (ap->break_handler)
mao, bibo36721652006-06-26 00:25:22 -07001211 return -EEXIST;
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001212 list_add_tail_rcu(&p->list, &ap->list);
1213 ap->break_handler = aggr_break_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001214 } else
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001215 list_add_rcu(&p->list, &ap->list);
1216 if (p->post_handler && !ap->post_handler)
1217 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001218
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001219 return 0;
1220}
1221
1222/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001223 * Fill in the required fields of the "manager kprobe". Replace the
1224 * earlier kprobe in the hlist with the manager kprobe
1225 */
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001226static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001227{
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001228 /* Copy p's insn slot to ap */
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -07001229 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -07001230 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001231 ap->addr = p->addr;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001232 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001233 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001234 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001235 /* We don't care the kprobe which has gone. */
1236 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001237 ap->post_handler = aggr_post_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001238 if (p->break_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -07001239 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001240
1241 INIT_LIST_HEAD(&ap->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001242 INIT_HLIST_NODE(&ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001243
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001244 list_add_rcu(&p->list, &ap->list);
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -08001245 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001246}
1247
1248/*
1249 * This is the second or subsequent kprobe at the address - handle
1250 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001251 */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001252static int __kprobes register_aggr_kprobe(struct kprobe *orig_p,
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001253 struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001254{
1255 int ret = 0;
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001256 struct kprobe *ap = orig_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001257
Masami Hiramatsu25764282012-06-05 19:28:26 +09001258 /* For preparing optimization, jump_label_text_reserved() is called */
1259 jump_label_lock();
1260 /*
1261 * Get online CPUs to avoid text_mutex deadlock.with stop machine,
1262 * which is invoked by unoptimize_kprobe() in add_new_kprobe()
1263 */
1264 get_online_cpus();
1265 mutex_lock(&text_mutex);
1266
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001267 if (!kprobe_aggrprobe(orig_p)) {
1268 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1269 ap = alloc_aggr_kprobe(orig_p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001270 if (!ap) {
1271 ret = -ENOMEM;
1272 goto out;
1273 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001274 init_aggr_kprobe(ap, orig_p);
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001275 } else if (kprobe_unused(ap))
Masami Hiramatsu0490cd12010-12-03 18:54:16 +09001276 /* This probe is going to die. Rescue it */
1277 reuse_unused_kprobe(ap);
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001278
1279 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001280 /*
1281 * Attempting to insert new probe at the same location that
1282 * had a probe in the module vaddr area which already
1283 * freed. So, the instruction slot has already been
1284 * released. We need a new slot for the new probe.
1285 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001286 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001287 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001288 /*
1289 * Even if fail to allocate new slot, don't need to
1290 * free aggr_probe. It will be used next time, or
1291 * freed by unregister_kprobe.
1292 */
Masami Hiramatsu25764282012-06-05 19:28:26 +09001293 goto out;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001294
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001295 /* Prepare optimized instructions if possible. */
1296 prepare_optimized_kprobe(ap);
1297
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001298 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001299 * Clear gone flag to prevent allocating new slot again, and
1300 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001301 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001302 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1303 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001304 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001305
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001306 /* Copy ap's insn slot to p */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -07001307 copy_kprobe(ap, p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001308 ret = add_new_kprobe(ap, p);
1309
1310out:
1311 mutex_unlock(&text_mutex);
1312 put_online_cpus();
1313 jump_label_unlock();
1314
1315 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1316 ap->flags &= ~KPROBE_FLAG_DISABLED;
1317 if (!kprobes_all_disarmed)
1318 /* Arm the breakpoint again. */
1319 arm_kprobe(ap);
1320 }
1321 return ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001322}
1323
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001324bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1325{
1326 /* The __kprobes marked functions and entry code must not be probed */
1327 return addr >= (unsigned long)__kprobes_text_start &&
1328 addr < (unsigned long)__kprobes_text_end;
1329}
1330
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001331static int __kprobes in_kprobes_functions(unsigned long addr)
1332{
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001333 struct kprobe_blackpoint *kb;
1334
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001335 if (arch_within_kprobe_blacklist(addr))
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001336 return -EINVAL;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001337 /*
1338 * If there exists a kprobe_blacklist, verify and
1339 * fail any probe registration in the prohibited area
1340 */
1341 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
1342 if (kb->start_addr) {
1343 if (addr >= kb->start_addr &&
1344 addr < (kb->start_addr + kb->range))
1345 return -EINVAL;
1346 }
1347 }
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001348 return 0;
1349}
1350
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001351/*
1352 * If we have a symbol_name argument, look it up and add the offset field
1353 * to it. This way, we can specify a relative address to a symbol.
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001354 * This returns encoded errors if it fails to look up symbol or invalid
1355 * combination of parameters.
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001356 */
1357static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
1358{
1359 kprobe_opcode_t *addr = p->addr;
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001360
1361 if ((p->symbol_name && p->addr) ||
1362 (!p->symbol_name && !p->addr))
1363 goto invalid;
1364
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001365 if (p->symbol_name) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001366 kprobe_lookup_name(p->symbol_name, addr);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001367 if (!addr)
1368 return ERR_PTR(-ENOENT);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001369 }
1370
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001371 addr = (kprobe_opcode_t *)(((char *)addr) + p->offset);
1372 if (addr)
1373 return addr;
1374
1375invalid:
1376 return ERR_PTR(-EINVAL);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001377}
1378
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301379/* Check passed kprobe is valid and return kprobe in kprobe_table. */
1380static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
1381{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001382 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301383
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001384 ap = get_kprobe(p->addr);
1385 if (unlikely(!ap))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301386 return NULL;
1387
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001388 if (p != ap) {
1389 list_for_each_entry_rcu(list_p, &ap->list, list)
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301390 if (list_p == p)
1391 /* kprobe p is a valid probe */
1392 goto valid;
1393 return NULL;
1394 }
1395valid:
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001396 return ap;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301397}
1398
1399/* Return error if the kprobe is being re-registered */
1400static inline int check_kprobe_rereg(struct kprobe *p)
1401{
1402 int ret = 0;
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301403
1404 mutex_lock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001405 if (__get_valid_kprobe(p))
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301406 ret = -EINVAL;
1407 mutex_unlock(&kprobe_mutex);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001408
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301409 return ret;
1410}
1411
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001412static __kprobes int check_kprobe_address_safe(struct kprobe *p,
1413 struct module **probed_mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 int ret = 0;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001416 unsigned long ftrace_addr;
1417
1418 /*
1419 * If the address is located on a ftrace nop, set the
1420 * breakpoint to the following instruction.
1421 */
1422 ftrace_addr = ftrace_location((unsigned long)p->addr);
1423 if (ftrace_addr) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001424#ifdef CONFIG_KPROBES_ON_FTRACE
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001425 /* Given address is not on the instruction boundary */
1426 if ((unsigned long)p->addr != ftrace_addr)
1427 return -EILSEQ;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001428 p->flags |= KPROBE_FLAG_FTRACE;
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +09001429#else /* !CONFIG_KPROBES_ON_FTRACE */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001430 return -EINVAL;
1431#endif
1432 }
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001433
1434 jump_label_lock();
1435 preempt_disable();
1436
1437 /* Ensure it is not in reserved area nor out of text */
1438 if (!kernel_text_address((unsigned long) p->addr) ||
1439 in_kprobes_functions((unsigned long) p->addr) ||
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001440 jump_label_text_reserved(p->addr, p->addr)) {
1441 ret = -EINVAL;
1442 goto out;
1443 }
1444
1445 /* Check if are we probing a module */
1446 *probed_mod = __module_text_address((unsigned long) p->addr);
1447 if (*probed_mod) {
1448 /*
1449 * We must hold a refcount of the probed module while updating
1450 * its code to prohibit unexpected unloading.
1451 */
1452 if (unlikely(!try_module_get(*probed_mod))) {
1453 ret = -ENOENT;
1454 goto out;
1455 }
1456
1457 /*
1458 * If the module freed .init.text, we couldn't insert
1459 * kprobes in there.
1460 */
1461 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1462 (*probed_mod)->state != MODULE_STATE_COMING) {
1463 module_put(*probed_mod);
1464 *probed_mod = NULL;
1465 ret = -ENOENT;
1466 }
1467 }
1468out:
1469 preempt_enable();
1470 jump_label_unlock();
1471
1472 return ret;
1473}
1474
1475int __kprobes register_kprobe(struct kprobe *p)
1476{
1477 int ret;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001478 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001479 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001480 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001482 /* Adjust probe address from symbol */
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001483 addr = kprobe_addr(p);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001484 if (IS_ERR(addr))
1485 return PTR_ERR(addr);
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001486 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -07001487
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +05301488 ret = check_kprobe_rereg(p);
1489 if (ret)
1490 return ret;
1491
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001492 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1493 p->flags &= KPROBE_FLAG_DISABLED;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001494 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001495 INIT_LIST_HEAD(&p->list);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001496
Masami Hiramatsuf7fa6ef02012-06-05 19:28:20 +09001497 ret = check_kprobe_address_safe(p, &probed_mod);
1498 if (ret)
1499 return ret;
1500
1501 mutex_lock(&kprobe_mutex);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001502
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001503 old_p = get_kprobe(p->addr);
1504 if (old_p) {
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001505 /* Since this may unoptimize old_p, locking text_mutex. */
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001506 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto out;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Masami Hiramatsu25764282012-06-05 19:28:26 +09001510 mutex_lock(&text_mutex); /* Avoiding text modification */
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001511 ret = prepare_kprobe(p);
Masami Hiramatsu25764282012-06-05 19:28:26 +09001512 mutex_unlock(&text_mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -07001513 if (ret)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001514 goto out;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001515
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001516 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -08001517 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1519
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001520 if (!kprobes_all_disarmed && !kprobe_disabled(p))
Masami Hiramatsu25764282012-06-05 19:28:26 +09001521 arm_kprobe(p);
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001522
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001523 /* Try to optimize kprobe */
1524 try_to_optimize_kprobe(p);
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -08001527 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001528
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001529 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001530 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return ret;
1533}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001534EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001536/* Check if all probes on the aggrprobe are disabled */
1537static int __kprobes aggr_kprobe_disabled(struct kprobe *ap)
1538{
1539 struct kprobe *kp;
1540
1541 list_for_each_entry_rcu(kp, &ap->list, list)
1542 if (!kprobe_disabled(kp))
1543 /*
1544 * There is an active probe on the list.
1545 * We can't disable this ap.
1546 */
1547 return 0;
1548
1549 return 1;
1550}
1551
1552/* Disable one kprobe: Make sure called under kprobe_mutex is locked */
1553static struct kprobe *__kprobes __disable_kprobe(struct kprobe *p)
1554{
1555 struct kprobe *orig_p;
1556
1557 /* Get an original kprobe for return */
1558 orig_p = __get_valid_kprobe(p);
1559 if (unlikely(orig_p == NULL))
1560 return NULL;
1561
1562 if (!kprobe_disabled(p)) {
1563 /* Disable probe if it is a child probe */
1564 if (p != orig_p)
1565 p->flags |= KPROBE_FLAG_DISABLED;
1566
1567 /* Try to disarm and disable this/parent probe */
1568 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09001569 disarm_kprobe(orig_p, true);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001570 orig_p->flags |= KPROBE_FLAG_DISABLED;
1571 }
1572 }
1573
1574 return orig_p;
1575}
1576
Masami Hiramatsu98616682008-04-28 02:14:28 -07001577/*
1578 * Unregister a kprobe without a scheduler synchronization.
1579 */
1580static int __kprobes __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -08001581{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001582 struct kprobe *ap, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -07001583
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001584 /* Disable kprobe. This will disarm it if needed. */
1585 ap = __disable_kprobe(p);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001586 if (ap == NULL)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001587 return -EINVAL;
1588
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001589 if (ap == p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001590 /*
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001591 * This probe is an independent(and non-optimized) kprobe
1592 * (not an aggrprobe). Remove from the hash list.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001593 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001594 goto disarmed;
1595
1596 /* Following process expects this probe is an aggrprobe */
1597 WARN_ON(!kprobe_aggrprobe(ap));
1598
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001599 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1600 /*
1601 * !disarmed could be happen if the probe is under delayed
1602 * unoptimizing.
1603 */
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001604 goto disarmed;
1605 else {
1606 /* If disabling probe has special handlers, update aggrprobe */
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001607 if (p->break_handler && !kprobe_gone(p))
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001608 ap->break_handler = NULL;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001609 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001610 list_for_each_entry_rcu(list_p, &ap->list, list) {
Masami Hiramatsu98616682008-04-28 02:14:28 -07001611 if ((list_p != p) && (list_p->post_handler))
1612 goto noclean;
1613 }
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001614 ap->post_handler = NULL;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001615 }
1616noclean:
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001617 /*
1618 * Remove from the aggrprobe: this path will do nothing in
1619 * __unregister_kprobe_bottom().
1620 */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001621 list_del_rcu(&p->list);
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001622 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1623 /*
1624 * Try to optimize this probe again, because post
1625 * handler may have been changed.
1626 */
1627 optimize_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001628 }
Masami Hiramatsu98616682008-04-28 02:14:28 -07001629 return 0;
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001630
1631disarmed:
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001632 BUG_ON(!kprobe_disarmed(ap));
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001633 hlist_del_rcu(&ap->hlist);
1634 return 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -07001635}
Mao, Bibob3e55c72005-12-12 00:37:00 -08001636
Masami Hiramatsu98616682008-04-28 02:14:28 -07001637static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
1638{
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001639 struct kprobe *ap;
Mao, Bibob3e55c72005-12-12 00:37:00 -08001640
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001641 if (list_empty(&p->list))
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001642 /* This is an independent kprobe */
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -08001643 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001644 else if (list_is_singular(&p->list)) {
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001645 /* This is the last child of an aggrprobe */
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001646 ap = list_entry(p->list.next, struct kprobe, list);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001647 list_del(&p->list);
Masami Hiramatsu6d8e40a2010-12-03 18:53:50 +09001648 free_aggr_kprobe(ap);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -08001649 }
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001650 /* Otherwise, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001653int __kprobes register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -07001654{
1655 int i, ret = 0;
1656
1657 if (num <= 0)
1658 return -EINVAL;
1659 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001660 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001661 if (ret < 0) {
1662 if (i > 0)
1663 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001664 break;
1665 }
1666 }
1667 return ret;
1668}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001669EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001670
Masami Hiramatsu98616682008-04-28 02:14:28 -07001671void __kprobes unregister_kprobe(struct kprobe *p)
1672{
1673 unregister_kprobes(&p, 1);
1674}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001675EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001676
Masami Hiramatsu98616682008-04-28 02:14:28 -07001677void __kprobes unregister_kprobes(struct kprobe **kps, int num)
1678{
1679 int i;
1680
1681 if (num <= 0)
1682 return;
1683 mutex_lock(&kprobe_mutex);
1684 for (i = 0; i < num; i++)
1685 if (__unregister_kprobe_top(kps[i]) < 0)
1686 kps[i]->addr = NULL;
1687 mutex_unlock(&kprobe_mutex);
1688
1689 synchronize_sched();
1690 for (i = 0; i < num; i++)
1691 if (kps[i]->addr)
1692 __unregister_kprobe_bottom(kps[i]);
1693}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001694EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static struct notifier_block kprobe_exceptions_nb = {
1697 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -07001698 .priority = 0x7fffffff /* we need to be notified first */
1699};
1700
Michael Ellerman3d7e3382007-07-19 01:48:11 -07001701unsigned long __weak arch_deref_entry_point(void *entry)
1702{
1703 return (unsigned long)entry;
1704}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001706int __kprobes register_jprobes(struct jprobe **jps, int num)
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001707{
1708 struct jprobe *jp;
1709 int ret = 0, i;
1710
1711 if (num <= 0)
1712 return -EINVAL;
1713 for (i = 0; i < num; i++) {
Namhyung Kim05662bd2010-09-15 10:04:27 +09001714 unsigned long addr, offset;
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001715 jp = jps[i];
1716 addr = arch_deref_entry_point(jp->entry);
1717
Namhyung Kim05662bd2010-09-15 10:04:27 +09001718 /* Verify probepoint is a function entry point */
1719 if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
1720 offset == 0) {
1721 jp->kp.pre_handler = setjmp_pre_handler;
1722 jp->kp.break_handler = longjmp_break_handler;
1723 ret = register_kprobe(&jp->kp);
1724 } else
1725 ret = -EINVAL;
Namhyung Kimedbaadb2010-09-15 10:04:26 +09001726
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001727 if (ret < 0) {
1728 if (i > 0)
1729 unregister_jprobes(jps, i);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001730 break;
1731 }
1732 }
1733 return ret;
1734}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001735EXPORT_SYMBOL_GPL(register_jprobes);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001736
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001737int __kprobes register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001739 return register_jprobes(&jp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001741EXPORT_SYMBOL_GPL(register_jprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001743void __kprobes unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001745 unregister_jprobes(&jp, 1);
1746}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001747EXPORT_SYMBOL_GPL(unregister_jprobe);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001748
Masami Hiramatsu26b31c12008-04-28 02:14:29 -07001749void __kprobes unregister_jprobes(struct jprobe **jps, int num)
1750{
1751 int i;
1752
1753 if (num <= 0)
1754 return;
1755 mutex_lock(&kprobe_mutex);
1756 for (i = 0; i < num; i++)
1757 if (__unregister_kprobe_top(&jps[i]->kp) < 0)
1758 jps[i]->kp.addr = NULL;
1759 mutex_unlock(&kprobe_mutex);
1760
1761 synchronize_sched();
1762 for (i = 0; i < num; i++) {
1763 if (jps[i]->kp.addr)
1764 __unregister_kprobe_bottom(&jps[i]->kp);
1765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001767EXPORT_SYMBOL_GPL(unregister_jprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001769#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -08001770/*
1771 * This kprobe pre_handler is registered with every kretprobe. When probe
1772 * hits it will set up the return probe.
1773 */
1774static int __kprobes pre_handler_kretprobe(struct kprobe *p,
1775 struct pt_regs *regs)
1776{
1777 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001778 unsigned long hash, flags = 0;
1779 struct kretprobe_instance *ri;
Adrian Bunke65cefe2006-02-03 03:03:42 -08001780
1781 /*TODO: consider to only swap the RA after the last pre_handler fired */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001782 hash = hash_ptr(current, KPROBE_HASH_BITS);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001783 raw_spin_lock_irqsave(&rp->lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001784 if (!hlist_empty(&rp->free_instances)) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001785 ri = hlist_entry(rp->free_instances.first,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001786 struct kretprobe_instance, hlist);
1787 hlist_del(&ri->hlist);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001788 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001789
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001790 ri->rp = rp;
1791 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001792
Jiang Liu55ca6142012-02-03 15:37:16 -08001793 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
1794 raw_spin_lock_irqsave(&rp->lock, flags);
1795 hlist_add_head(&ri->hlist, &rp->free_instances);
1796 raw_spin_unlock_irqrestore(&rp->lock, flags);
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001797 return 0;
Jiang Liu55ca6142012-02-03 15:37:16 -08001798 }
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001799
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001800 arch_prepare_kretprobe(ri, regs);
1801
1802 /* XXX(hch): why is there no hlist_move_head? */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001803 INIT_HLIST_NODE(&ri->hlist);
1804 kretprobe_table_lock(hash, &flags);
1805 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
1806 kretprobe_table_unlock(hash, &flags);
1807 } else {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -07001808 rp->nmissed++;
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001809 raw_spin_unlock_irqrestore(&rp->lock, flags);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001810 }
Adrian Bunke65cefe2006-02-03 03:03:42 -08001811 return 0;
1812}
1813
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001814int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001815{
1816 int ret = 0;
1817 struct kretprobe_instance *inst;
1818 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001819 void *addr;
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001820
1821 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -08001822 addr = kprobe_addr(&rp->kp);
Masami Hiramatsubc81d482011-06-27 16:26:50 +09001823 if (IS_ERR(addr))
1824 return PTR_ERR(addr);
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001825
1826 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1827 if (kretprobe_blacklist[i].addr == addr)
1828 return -EINVAL;
1829 }
1830 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001831
1832 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07001833 rp->kp.post_handler = NULL;
1834 rp->kp.fault_handler = NULL;
1835 rp->kp.break_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001836
1837 /* Pre-allocate memory for max kretprobe instances */
1838 if (rp->maxactive <= 0) {
1839#ifdef CONFIG_PREEMPT
Heiko Carstensc2ef6662009-12-21 13:02:24 +01001840 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07001841#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05301842 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001843#endif
1844 }
Thomas Gleixnerec4846082009-07-25 16:09:17 +02001845 raw_spin_lock_init(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001846 INIT_HLIST_HEAD(&rp->free_instances);
1847 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001848 inst = kmalloc(sizeof(struct kretprobe_instance) +
1849 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001850 if (inst == NULL) {
1851 free_rp_inst(rp);
1852 return -ENOMEM;
1853 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001854 INIT_HLIST_NODE(&inst->hlist);
1855 hlist_add_head(&inst->hlist, &rp->free_instances);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001856 }
1857
1858 rp->nmissed = 0;
1859 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001860 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001861 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001862 free_rp_inst(rp);
1863 return ret;
1864}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001865EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001866
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001867int __kprobes register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001868{
1869 int ret = 0, i;
1870
1871 if (num <= 0)
1872 return -EINVAL;
1873 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001874 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001875 if (ret < 0) {
1876 if (i > 0)
1877 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001878 break;
1879 }
1880 }
1881 return ret;
1882}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001883EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001884
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001885void __kprobes unregister_kretprobe(struct kretprobe *rp)
1886{
1887 unregister_kretprobes(&rp, 1);
1888}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001889EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001890
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001891void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1892{
1893 int i;
1894
1895 if (num <= 0)
1896 return;
1897 mutex_lock(&kprobe_mutex);
1898 for (i = 0; i < num; i++)
1899 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
1900 rps[i]->kp.addr = NULL;
1901 mutex_unlock(&kprobe_mutex);
1902
1903 synchronize_sched();
1904 for (i = 0; i < num; i++) {
1905 if (rps[i]->kp.addr) {
1906 __unregister_kprobe_bottom(&rps[i]->kp);
1907 cleanup_rp_inst(rps[i]);
1908 }
1909 }
1910}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001911EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001912
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001913#else /* CONFIG_KRETPROBES */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001914int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001915{
1916 return -ENOSYS;
1917}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001918EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001919
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001920int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1921{
1922 return -ENOSYS;
1923}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001924EXPORT_SYMBOL_GPL(register_kretprobes);
1925
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001926void __kprobes unregister_kretprobe(struct kretprobe *rp)
1927{
1928}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001929EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001930
1931void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1932{
1933}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001934EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001935
Srinivasa Ds346fd592007-02-20 13:57:54 -08001936static int __kprobes pre_handler_kretprobe(struct kprobe *p,
1937 struct pt_regs *regs)
1938{
1939 return 0;
1940}
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001941
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001942#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07001943
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001944/* Set the kprobe gone and remove its instruction buffer. */
1945static void __kprobes kill_kprobe(struct kprobe *p)
1946{
1947 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001948
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001949 p->flags |= KPROBE_FLAG_GONE;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001950 if (kprobe_aggrprobe(p)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001951 /*
1952 * If this is an aggr_kprobe, we have to list all the
1953 * chained probes and mark them GONE.
1954 */
1955 list_for_each_entry_rcu(kp, &p->list, list)
1956 kp->flags |= KPROBE_FLAG_GONE;
1957 p->post_handler = NULL;
1958 p->break_handler = NULL;
Masami Hiramatsuafd66252010-02-25 08:34:07 -05001959 kill_optimized_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001960 }
1961 /*
1962 * Here, we can remove insn_slot safely, because no thread calls
1963 * the original probed function (which will be freed soon) any more.
1964 */
1965 arch_remove_kprobe(p);
1966}
1967
Masami Hiramatsuc0614822010-04-27 18:33:12 -04001968/* Disable one kprobe */
1969int __kprobes disable_kprobe(struct kprobe *kp)
1970{
1971 int ret = 0;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04001972
1973 mutex_lock(&kprobe_mutex);
1974
Masami Hiramatsu6f0f1dd2010-12-03 18:53:57 +09001975 /* Disable this kprobe */
1976 if (__disable_kprobe(kp) == NULL)
Masami Hiramatsuc0614822010-04-27 18:33:12 -04001977 ret = -EINVAL;
Masami Hiramatsuc0614822010-04-27 18:33:12 -04001978
Masami Hiramatsuc0614822010-04-27 18:33:12 -04001979 mutex_unlock(&kprobe_mutex);
1980 return ret;
1981}
1982EXPORT_SYMBOL_GPL(disable_kprobe);
1983
1984/* Enable one kprobe */
1985int __kprobes enable_kprobe(struct kprobe *kp)
1986{
1987 int ret = 0;
1988 struct kprobe *p;
1989
1990 mutex_lock(&kprobe_mutex);
1991
1992 /* Check whether specified probe is valid. */
1993 p = __get_valid_kprobe(kp);
1994 if (unlikely(p == NULL)) {
1995 ret = -EINVAL;
1996 goto out;
1997 }
1998
1999 if (kprobe_gone(kp)) {
2000 /* This kprobe has gone, we couldn't enable it. */
2001 ret = -EINVAL;
2002 goto out;
2003 }
2004
2005 if (p != kp)
2006 kp->flags &= ~KPROBE_FLAG_DISABLED;
2007
2008 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2009 p->flags &= ~KPROBE_FLAG_DISABLED;
2010 arm_kprobe(p);
2011 }
2012out:
2013 mutex_unlock(&kprobe_mutex);
2014 return ret;
2015}
2016EXPORT_SYMBOL_GPL(enable_kprobe);
2017
Frederic Weisbecker24851d22009-08-26 23:38:30 +02002018void __kprobes dump_kprobe(struct kprobe *kp)
2019{
2020 printk(KERN_WARNING "Dumping kprobe:\n");
2021 printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
2022 kp->symbol_name, kp->addr, kp->offset);
2023}
2024
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002025/* Module notifier call back, checking kprobes on the module */
2026static int __kprobes kprobes_module_callback(struct notifier_block *nb,
2027 unsigned long val, void *data)
2028{
2029 struct module *mod = data;
2030 struct hlist_head *head;
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002031 struct kprobe *p;
2032 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002033 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002034
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002035 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002036 return NOTIFY_DONE;
2037
2038 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002039 * When MODULE_STATE_GOING was notified, both of module .text and
2040 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2041 * notified, only .init.text section would be freed. We need to
2042 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002043 */
2044 mutex_lock(&kprobe_mutex);
2045 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2046 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002047 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08002048 if (within_module_init((unsigned long)p->addr, mod) ||
2049 (checkcore &&
2050 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002051 /*
2052 * The vaddr this probe is installed will soon
2053 * be vfreed buy not synced to disk. Hence,
2054 * disarming the breakpoint isn't needed.
2055 */
2056 kill_kprobe(p);
2057 }
2058 }
2059 mutex_unlock(&kprobe_mutex);
2060 return NOTIFY_DONE;
2061}
2062
2063static struct notifier_block kprobe_module_nb = {
2064 .notifier_call = kprobes_module_callback,
2065 .priority = 0
2066};
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068static int __init init_kprobes(void)
2069{
2070 int i, err = 0;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002071 unsigned long offset = 0, size = 0;
Joe Marioab767862013-11-12 15:10:23 -08002072 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002073 const char *symbol_name;
2074 void *addr;
2075 struct kprobe_blackpoint *kb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* FIXME allocate the probe table, currently defined statically */
2078 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -07002079 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -07002081 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
Thomas Gleixnerec4846082009-07-25 16:09:17 +02002082 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
Hien Nguyenb94cce92005-06-23 00:09:19 -07002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07002085 /*
2086 * Lookup and populate the kprobe_blacklist.
2087 *
2088 * Unlike the kretprobe blacklist, we'll need to determine
2089 * the range of addresses that belong to the said functions,
2090 * since a kprobe need not necessarily be at the beginning
2091 * of a function.
2092 */
2093 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
2094 kprobe_lookup_name(kb->name, addr);
2095 if (!addr)
2096 continue;
2097
2098 kb->start_addr = (unsigned long)addr;
2099 symbol_name = kallsyms_lookup(kb->start_addr,
2100 &size, &offset, &modname, namebuf);
2101 if (!symbol_name)
2102 kb->range = 0;
2103 else
2104 kb->range = size;
2105 }
2106
Masami Hiramatsuf438d912007-10-16 01:27:49 -07002107 if (kretprobe_blacklist_size) {
2108 /* lookup the function address from its name */
2109 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2110 kprobe_lookup_name(kretprobe_blacklist[i].name,
2111 kretprobe_blacklist[i].addr);
2112 if (!kretprobe_blacklist[i].addr)
2113 printk("kretprobe: lookup failed: %s\n",
2114 kretprobe_blacklist[i].name);
2115 }
2116 }
2117
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002118#if defined(CONFIG_OPTPROBES)
2119#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002120 /* Init kprobe_optinsn_slots */
2121 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2122#endif
Masami Hiramatsub2be84d2010-02-25 08:34:15 -05002123 /* By default, kprobes can be optimized */
2124 kprobes_allow_optimization = true;
2125#endif
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002126
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002127 /* By default, kprobes are armed */
2128 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002129
Rusty Lynch67729262005-07-05 18:54:50 -07002130 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07002131 if (!err)
2132 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08002133 if (!err)
2134 err = register_module_notifier(&kprobe_module_nb);
2135
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07002136 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07002137
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01002138 if (!err)
2139 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return err;
2141}
2142
Srinivasa Ds346fd592007-02-20 13:57:54 -08002143#ifdef CONFIG_DEBUG_FS
2144static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002145 const char *sym, int offset, char *modname, struct kprobe *pp)
Srinivasa Ds346fd592007-02-20 13:57:54 -08002146{
2147 char *kprobe_type;
2148
2149 if (p->pre_handler == pre_handler_kretprobe)
2150 kprobe_type = "r";
2151 else if (p->pre_handler == setjmp_pre_handler)
2152 kprobe_type = "j";
2153 else
2154 kprobe_type = "k";
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002155
Srinivasa Ds346fd592007-02-20 13:57:54 -08002156 if (sym)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002157 seq_printf(pi, "%p %s %s+0x%x %s ",
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002158 p->addr, kprobe_type, sym, offset,
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002159 (modname ? modname : " "));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002160 else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002161 seq_printf(pi, "%p %s %p ",
2162 p->addr, kprobe_type, p->addr);
2163
2164 if (!pp)
2165 pp = p;
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002166 seq_printf(pi, "%s%s%s%s\n",
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002167 (kprobe_gone(p) ? "[GONE]" : ""),
2168 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002169 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2170 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08002171}
2172
2173static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
2174{
2175 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2176}
2177
2178static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
2179{
2180 (*pos)++;
2181 if (*pos >= KPROBE_TABLE_SIZE)
2182 return NULL;
2183 return pos;
2184}
2185
2186static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
2187{
2188 /* Nothing to do */
2189}
2190
2191static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
2192{
2193 struct hlist_head *head;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002194 struct kprobe *p, *kp;
2195 const char *sym = NULL;
2196 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002197 unsigned long offset = 0;
Joe Marioab767862013-11-12 15:10:23 -08002198 char *modname, namebuf[KSYM_NAME_LEN];
Srinivasa Ds346fd592007-02-20 13:57:54 -08002199
2200 head = &kprobe_table[i];
2201 preempt_disable();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002202 hlist_for_each_entry_rcu(p, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002203 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002204 &offset, &modname, namebuf);
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002205 if (kprobe_aggrprobe(p)) {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002206 list_for_each_entry_rcu(kp, &p->list, list)
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002207 report_probe(pi, kp, sym, offset, modname, p);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002208 } else
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002209 report_probe(pi, p, sym, offset, modname, NULL);
Srinivasa Ds346fd592007-02-20 13:57:54 -08002210 }
2211 preempt_enable();
2212 return 0;
2213}
2214
James Morris88e9d342009-09-22 16:43:43 -07002215static const struct seq_operations kprobes_seq_ops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002216 .start = kprobe_seq_start,
2217 .next = kprobe_seq_next,
2218 .stop = kprobe_seq_stop,
2219 .show = show_kprobe_addr
2220};
2221
2222static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
2223{
2224 return seq_open(filp, &kprobes_seq_ops);
2225}
2226
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002227static const struct file_operations debugfs_kprobes_operations = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08002228 .open = kprobes_open,
2229 .read = seq_read,
2230 .llseek = seq_lseek,
2231 .release = seq_release,
2232};
2233
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002234static void __kprobes arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002235{
2236 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002237 struct kprobe *p;
2238 unsigned int i;
2239
2240 mutex_lock(&kprobe_mutex);
2241
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002242 /* If kprobes are armed, just return */
2243 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002244 goto already_enabled;
2245
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002246 /* Arming kprobes doesn't optimize kprobe itself */
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002247 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2248 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002249 hlist_for_each_entry_rcu(p, head, hlist)
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002250 if (!kprobe_disabled(p))
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002251 arm_kprobe(p);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002252 }
2253
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002254 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002255 printk(KERN_INFO "Kprobes globally enabled\n");
2256
2257already_enabled:
2258 mutex_unlock(&kprobe_mutex);
2259 return;
2260}
2261
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002262static void __kprobes disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002263{
2264 struct hlist_head *head;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002265 struct kprobe *p;
2266 unsigned int i;
2267
2268 mutex_lock(&kprobe_mutex);
2269
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002270 /* If kprobes are already disarmed, just return */
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002271 if (kprobes_all_disarmed) {
2272 mutex_unlock(&kprobe_mutex);
2273 return;
2274 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002275
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002276 kprobes_all_disarmed = true;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002277 printk(KERN_INFO "Kprobes globally disabled\n");
Masami Hiramatsuafd66252010-02-25 08:34:07 -05002278
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002279 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2280 head = &kprobe_table[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002281 hlist_for_each_entry_rcu(p, head, hlist) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07002282 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
Masami Hiramatsuae6aa162012-06-05 19:28:32 +09002283 disarm_kprobe(p, false);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002284 }
2285 }
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002286 mutex_unlock(&kprobe_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002287
Masami Hiramatsu6274de42010-12-03 18:54:09 +09002288 /* Wait for disarming all kprobes by optimizer */
2289 wait_for_kprobe_optimizer();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002290}
2291
2292/*
2293 * XXX: The debugfs bool file interface doesn't allow for callbacks
2294 * when the bool state is switched. We can reuse that facility when
2295 * available
2296 */
2297static ssize_t read_enabled_file_bool(struct file *file,
2298 char __user *user_buf, size_t count, loff_t *ppos)
2299{
2300 char buf[3];
2301
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002302 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002303 buf[0] = '1';
2304 else
2305 buf[0] = '0';
2306 buf[1] = '\n';
2307 buf[2] = 0x00;
2308 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2309}
2310
2311static ssize_t write_enabled_file_bool(struct file *file,
2312 const char __user *user_buf, size_t count, loff_t *ppos)
2313{
2314 char buf[32];
Stephen Boydefeb1562012-01-12 17:17:11 -08002315 size_t buf_size;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002316
2317 buf_size = min(count, (sizeof(buf)-1));
2318 if (copy_from_user(buf, user_buf, buf_size))
2319 return -EFAULT;
2320
Mathias Krause10fb46d2013-07-03 15:05:39 -07002321 buf[buf_size] = '\0';
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002322 switch (buf[0]) {
2323 case 'y':
2324 case 'Y':
2325 case '1':
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002326 arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002327 break;
2328 case 'n':
2329 case 'N':
2330 case '0':
Masami Hiramatsue579abe2009-04-06 19:01:01 -07002331 disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002332 break;
Mathias Krause10fb46d2013-07-03 15:05:39 -07002333 default:
2334 return -EINVAL;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002335 }
2336
2337 return count;
2338}
2339
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002340static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002341 .read = read_enabled_file_bool,
2342 .write = write_enabled_file_bool,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002343 .llseek = default_llseek,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002344};
2345
Srinivasa Ds346fd592007-02-20 13:57:54 -08002346static int __kprobes debugfs_kprobe_init(void)
2347{
2348 struct dentry *dir, *file;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002349 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08002350
2351 dir = debugfs_create_dir("kprobes", NULL);
2352 if (!dir)
2353 return -ENOMEM;
2354
Randy Dunlape3869792007-05-08 00:27:01 -07002355 file = debugfs_create_file("list", 0444, dir, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08002356 &debugfs_kprobes_operations);
2357 if (!file) {
2358 debugfs_remove(dir);
2359 return -ENOMEM;
2360 }
2361
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07002362 file = debugfs_create_file("enabled", 0600, dir,
2363 &value, &fops_kp);
2364 if (!file) {
2365 debugfs_remove(dir);
2366 return -ENOMEM;
2367 }
2368
Srinivasa Ds346fd592007-02-20 13:57:54 -08002369 return 0;
2370}
2371
2372late_initcall(debugfs_kprobe_init);
2373#endif /* CONFIG_DEBUG_FS */
2374
2375module_init(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07002377/* defined in arch/.../kernel/kprobes.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378EXPORT_SYMBOL_GPL(jprobe_return);