blob: 9907a03c29f62e16a1f58dff1c711c185571ea0c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.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>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070045#include <linux/kdebug.h>
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -050046#include <linux/memory.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070047
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -070048#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
50#include <asm/errno.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070051#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define KPROBE_HASH_BITS 6
54#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
55
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070056
57/*
58 * Some oddball architectures like 64bit powerpc have function descriptors
59 * so this must be overridable.
60 */
61#ifndef kprobe_lookup_name
62#define kprobe_lookup_name(name, addr) \
63 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
64#endif
65
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070066static int kprobes_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070068static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070070/* NOTE: change this value only with kprobe_mutex held */
Masami Hiramatsue579abe2009-04-06 19:01:01 -070071static bool kprobes_all_disarmed;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070072
Masami Hiramatsu12941562009-01-06 14:41:50 -080073static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080074static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070075static struct {
Andrew Morton7e036d02008-11-12 13:26:57 -080076 spinlock_t lock ____cacheline_aligned_in_smp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -070077} kretprobe_table_locks[KPROBE_TABLE_SIZE];
78
79static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
80{
81 return &(kretprobe_table_locks[hash].lock);
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070084/*
85 * Normally, functions that we'd want to prohibit kprobes in, are marked
86 * __kprobes. But, there are cases where such functions already belong to
87 * a different section (__sched for preempt_schedule)
88 *
89 * For such cases, we now have a blacklist
90 */
Daniel Guilak544304b2008-07-10 09:38:19 -070091static struct kprobe_blackpoint kprobe_blacklist[] = {
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070092 {"preempt_schedule",},
Masami Hiramatsu65e234e2009-08-27 13:23:32 -040093 {"native_get_debugreg",},
Masami Hiramatsua00e8172009-09-08 12:47:55 -040094 {"irq_entries_start",},
95 {"common_interrupt",},
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070096 {NULL} /* Terminator */
97};
98
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080099#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700100/*
101 * kprobe->ainsn.insn points to the copy of the instruction to be
102 * single-stepped. x86_64, POWER4 and above have no-exec support and
103 * stepping on the instruction on a vmalloced/kmalloced/data page
104 * is a recipe for disaster
105 */
106#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
107
108struct kprobe_insn_page {
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400109 struct list_head list;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700110 kprobe_opcode_t *insns; /* Page of instruction slots */
111 char slot_used[INSNS_PER_PAGE];
112 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800113 int ngarbage;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700114};
115
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800116enum kprobe_slot_state {
117 SLOT_CLEAN = 0,
118 SLOT_DIRTY = 1,
119 SLOT_USED = 2,
120};
121
Masami Hiramatsu12941562009-01-06 14:41:50 -0800122static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_pages */
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400123static LIST_HEAD(kprobe_insn_pages);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800124static int kprobe_garbage_slots;
125static int collect_garbage_slots(void);
126
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700127/**
Masami Hiramatsu12941562009-01-06 14:41:50 -0800128 * __get_insn_slot() - Find a slot on an executable page for an instruction.
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700129 * We allocate an executable page if there's no room on existing ones.
130 */
Masami Hiramatsu12941562009-01-06 14:41:50 -0800131static kprobe_opcode_t __kprobes *__get_insn_slot(void)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700132{
133 struct kprobe_insn_page *kip;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700134
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700135 retry:
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400136 list_for_each_entry(kip, &kprobe_insn_pages, list) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700137 if (kip->nused < INSNS_PER_PAGE) {
138 int i;
139 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800140 if (kip->slot_used[i] == SLOT_CLEAN) {
141 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700142 kip->nused++;
143 return kip->insns + (i * MAX_INSN_SIZE);
144 }
145 }
146 /* Surprise! No unused slots. Fix kip->nused. */
147 kip->nused = INSNS_PER_PAGE;
148 }
149 }
150
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800151 /* If there are any garbage slots, collect it and try again. */
152 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
153 goto retry;
154 }
155 /* All out of space. Need to allocate a new page. Use slot 0. */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700156 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700157 if (!kip)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700158 return NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700159
160 /*
161 * Use module_alloc so this page is within +/- 2GB of where the
162 * kernel image and loaded module images reside. This is required
163 * so x86_64 can correctly handle the %rip-relative fixups.
164 */
165 kip->insns = module_alloc(PAGE_SIZE);
166 if (!kip->insns) {
167 kfree(kip);
168 return NULL;
169 }
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400170 INIT_LIST_HEAD(&kip->list);
171 list_add(&kip->list, &kprobe_insn_pages);
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800172 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
173 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700174 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800175 kip->ngarbage = 0;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700176 return kip->insns;
177}
178
Masami Hiramatsu12941562009-01-06 14:41:50 -0800179kprobe_opcode_t __kprobes *get_insn_slot(void)
180{
181 kprobe_opcode_t *ret;
182 mutex_lock(&kprobe_insn_mutex);
183 ret = __get_insn_slot();
184 mutex_unlock(&kprobe_insn_mutex);
185 return ret;
186}
187
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800188/* Return 1 if all garbages are collected, otherwise 0. */
189static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
190{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800191 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800192 kip->nused--;
193 if (kip->nused == 0) {
194 /*
195 * Page is no longer in use. Free it unless
196 * it's the last one. We keep the last one
197 * so as not to have to set it up again the
198 * next time somebody inserts a probe.
199 */
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400200 if (!list_is_singular(&kprobe_insn_pages)) {
201 list_del(&kip->list);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800202 module_free(NULL, kip->insns);
203 kfree(kip);
204 }
205 return 1;
206 }
207 return 0;
208}
209
210static int __kprobes collect_garbage_slots(void)
211{
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400212 struct kprobe_insn_page *kip, *next;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800213
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500214 /* Ensure no-one is interrupted on the garbages */
215 synchronize_sched();
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800216
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400217 list_for_each_entry_safe(kip, next, &kprobe_insn_pages, list) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800218 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800219 if (kip->ngarbage == 0)
220 continue;
221 kip->ngarbage = 0; /* we will collect all garbages */
222 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800223 if (kip->slot_used[i] == SLOT_DIRTY &&
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800224 collect_one_slot(kip, i))
225 break;
226 }
227 }
228 kprobe_garbage_slots = 0;
229 return 0;
230}
231
232void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700233{
234 struct kprobe_insn_page *kip;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700235
Masami Hiramatsu12941562009-01-06 14:41:50 -0800236 mutex_lock(&kprobe_insn_mutex);
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400237 list_for_each_entry(kip, &kprobe_insn_pages, list) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700238 if (kip->insns <= slot &&
239 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
240 int i = (slot - kip->insns) / MAX_INSN_SIZE;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800241 if (dirty) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800242 kip->slot_used[i] = SLOT_DIRTY;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800243 kip->ngarbage++;
Masami Hiramatsuc5cb5a22009-06-30 17:08:14 -0400244 } else
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800245 collect_one_slot(kip, i);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800246 break;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700247 }
248 }
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700249
250 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800251 collect_garbage_slots();
Masami Hiramatsu12941562009-01-06 14:41:50 -0800252
253 mutex_unlock(&kprobe_insn_mutex);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700254}
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800255#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700256
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800257/* We have preemption disabled.. so it is safe to use __ versions */
258static inline void set_kprobe_instance(struct kprobe *kp)
259{
260 __get_cpu_var(kprobe_instance) = kp;
261}
262
263static inline void reset_kprobe_instance(void)
264{
265 __get_cpu_var(kprobe_instance) = NULL;
266}
267
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800268/*
269 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800270 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800271 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800272 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800273 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700274struct kprobe __kprobes *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct hlist_head *head;
277 struct hlist_node *node;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800278 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800281 hlist_for_each_entry_rcu(p, node, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (p->addr == addr)
283 return p;
284 }
285 return NULL;
286}
287
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400288/* Arm a kprobe with text_mutex */
289static void __kprobes arm_kprobe(struct kprobe *kp)
290{
291 mutex_lock(&text_mutex);
292 arch_arm_kprobe(kp);
293 mutex_unlock(&text_mutex);
294}
295
296/* Disarm a kprobe with text_mutex */
297static void __kprobes disarm_kprobe(struct kprobe *kp)
298{
299 mutex_lock(&text_mutex);
300 arch_disarm_kprobe(kp);
301 mutex_unlock(&text_mutex);
302}
303
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700304/*
305 * Aggregate handlers for multiple kprobes support - these handlers
306 * take care of invoking the individual kprobe handlers on p->list
307 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700308static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700309{
310 struct kprobe *kp;
311
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800312 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700313 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800314 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700315 if (kp->pre_handler(kp, regs))
316 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700317 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800318 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700319 }
320 return 0;
321}
322
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700323static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
324 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700325{
326 struct kprobe *kp;
327
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800328 list_for_each_entry_rcu(kp, &p->list, list) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700329 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800330 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700331 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800332 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700333 }
334 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700335}
336
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700337static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
338 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700339{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800340 struct kprobe *cur = __get_cpu_var(kprobe_instance);
341
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700342 /*
343 * if we faulted "during" the execution of a user specified
344 * probe handler, invoke just that probe's fault handler
345 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800346 if (cur && cur->fault_handler) {
347 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700348 return 1;
349 }
350 return 0;
351}
352
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700353static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700354{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800355 struct kprobe *cur = __get_cpu_var(kprobe_instance);
356 int ret = 0;
357
358 if (cur && cur->break_handler) {
359 if (cur->break_handler(cur, regs))
360 ret = 1;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700361 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800362 reset_kprobe_instance();
363 return ret;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700364}
365
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800366/* Walks the list and increments nmissed count for multiprobe case */
367void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
368{
369 struct kprobe *kp;
370 if (p->pre_handler != aggr_pre_handler) {
371 p->nmissed++;
372 } else {
373 list_for_each_entry_rcu(kp, &p->list, list)
374 kp->nmissed++;
375 }
376 return;
377}
378
bibo,mao99219a32006-10-02 02:17:35 -0700379void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
380 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700381{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700382 struct kretprobe *rp = ri->rp;
383
Hien Nguyenb94cce92005-06-23 00:09:19 -0700384 /* remove rp inst off the rprobe_inst_table */
385 hlist_del(&ri->hlist);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700386 INIT_HLIST_NODE(&ri->hlist);
387 if (likely(rp)) {
388 spin_lock(&rp->lock);
389 hlist_add_head(&ri->hlist, &rp->free_instances);
390 spin_unlock(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700391 } else
392 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -0700393 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700394}
395
Masami Hiramatsu017c39b2009-01-06 14:41:51 -0800396void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700397 struct hlist_head **head, unsigned long *flags)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700398{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700399 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
400 spinlock_t *hlist_lock;
401
402 *head = &kretprobe_inst_table[hash];
403 hlist_lock = kretprobe_table_lock_ptr(hash);
404 spin_lock_irqsave(hlist_lock, *flags);
405}
406
Masami Hiramatsu017c39b2009-01-06 14:41:51 -0800407static void __kprobes kretprobe_table_lock(unsigned long hash,
408 unsigned long *flags)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700409{
410 spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
411 spin_lock_irqsave(hlist_lock, *flags);
412}
413
Masami Hiramatsu017c39b2009-01-06 14:41:51 -0800414void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
415 unsigned long *flags)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700416{
417 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
418 spinlock_t *hlist_lock;
419
420 hlist_lock = kretprobe_table_lock_ptr(hash);
421 spin_unlock_irqrestore(hlist_lock, *flags);
422}
423
Masami Hiramatsu017c39b2009-01-06 14:41:51 -0800424void __kprobes kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700425{
426 spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
427 spin_unlock_irqrestore(hlist_lock, *flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700428}
429
Hien Nguyenb94cce92005-06-23 00:09:19 -0700430/*
bibo maoc6fd91f2006-03-26 01:38:20 -0800431 * This function is called from finish_task_switch when task tk becomes dead,
432 * so that we can recycle any function-return probe instances associated
433 * with this task. These left over instances represent probed functions
434 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -0700435 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700436void __kprobes kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700437{
bibo,mao62c27be2006-10-02 02:17:33 -0700438 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -0700439 struct hlist_head *head, empty_rp;
Rusty Lynch802eae72005-06-27 15:17:08 -0700440 struct hlist_node *node, *tmp;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700441 unsigned long hash, flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -0700442
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700443 if (unlikely(!kprobes_initialized))
444 /* Early boot. kretprobe_table_locks not yet initialized. */
445 return;
446
447 hash = hash_ptr(tk, KPROBE_HASH_BITS);
448 head = &kretprobe_inst_table[hash];
449 kretprobe_table_lock(hash, &flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700450 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
451 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -0700452 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -0700453 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700454 kretprobe_table_unlock(hash, &flags);
455 INIT_HLIST_HEAD(&empty_rp);
bibo,mao99219a32006-10-02 02:17:35 -0700456 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
457 hlist_del(&ri->hlist);
458 kfree(ri);
459 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700460}
461
Hien Nguyenb94cce92005-06-23 00:09:19 -0700462static inline void free_rp_inst(struct kretprobe *rp)
463{
464 struct kretprobe_instance *ri;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700465 struct hlist_node *pos, *next;
466
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700467 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
468 hlist_del(&ri->hlist);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700469 kfree(ri);
470 }
471}
472
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700473static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
474{
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700475 unsigned long flags, hash;
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700476 struct kretprobe_instance *ri;
477 struct hlist_node *pos, *next;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700478 struct hlist_head *head;
479
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700480 /* No race here */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700481 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
482 kretprobe_table_lock(hash, &flags);
483 head = &kretprobe_inst_table[hash];
484 hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
485 if (ri->rp == rp)
486 ri->rp = NULL;
487 }
488 kretprobe_table_unlock(hash, &flags);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700489 }
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700490 free_rp_inst(rp);
491}
492
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700493/*
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700494 * Keep all fields in the kprobe consistent
495 */
496static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
497{
498 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
499 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
500}
501
502/*
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700503* Add the new probe to ap->list. Fail if this is the
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700504* second jprobe at the address - two jprobes can't coexist
505*/
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700506static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700507{
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700508 BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700509 if (p->break_handler) {
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700510 if (ap->break_handler)
mao, bibo36721652006-06-26 00:25:22 -0700511 return -EEXIST;
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700512 list_add_tail_rcu(&p->list, &ap->list);
513 ap->break_handler = aggr_break_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700514 } else
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700515 list_add_rcu(&p->list, &ap->list);
516 if (p->post_handler && !ap->post_handler)
517 ap->post_handler = aggr_post_handler;
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700518
519 if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
520 ap->flags &= ~KPROBE_FLAG_DISABLED;
521 if (!kprobes_all_disarmed)
522 /* Arm the breakpoint again. */
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400523 arm_kprobe(ap);
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700524 }
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700525 return 0;
526}
527
528/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700529 * Fill in the required fields of the "manager kprobe". Replace the
530 * earlier kprobe in the hlist with the manager kprobe
531 */
532static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
533{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700534 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -0700535 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700536 ap->addr = p->addr;
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700537 ap->flags = p->flags;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700538 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700539 ap->fault_handler = aggr_fault_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800540 /* We don't care the kprobe which has gone. */
541 if (p->post_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -0700542 ap->post_handler = aggr_post_handler;
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800543 if (p->break_handler && !kprobe_gone(p))
mao, bibo36721652006-06-26 00:25:22 -0700544 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700545
546 INIT_LIST_HEAD(&ap->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800547 list_add_rcu(&p->list, &ap->list);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700548
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -0800549 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700550}
551
552/*
553 * This is the second or subsequent kprobe at the address - handle
554 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700555 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700556static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
557 struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700558{
559 int ret = 0;
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700560 struct kprobe *ap = old_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700561
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700562 if (old_p->pre_handler != aggr_pre_handler) {
563 /* If old_p is not an aggr_probe, create new aggr_kprobe. */
564 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
565 if (!ap)
566 return -ENOMEM;
567 add_aggr_kprobe(ap, old_p);
568 }
569
570 if (kprobe_gone(ap)) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800571 /*
572 * Attempting to insert new probe at the same location that
573 * had a probe in the module vaddr area which already
574 * freed. So, the instruction slot has already been
575 * released. We need a new slot for the new probe.
576 */
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700577 ret = arch_prepare_kprobe(ap);
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800578 if (ret)
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700579 /*
580 * Even if fail to allocate new slot, don't need to
581 * free aggr_probe. It will be used next time, or
582 * freed by unregister_kprobe.
583 */
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800584 return ret;
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700585
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800586 /*
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700587 * Clear gone flag to prevent allocating new slot again, and
588 * set disabled flag because it is not armed yet.
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800589 */
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700590 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
591 | KPROBE_FLAG_DISABLED;
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800592 }
Masami Hiramatsub918e5e2009-04-06 19:00:58 -0700593
594 copy_kprobe(ap, p);
595 return add_new_kprobe(ap, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700596}
597
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700598/* Try to disable aggr_kprobe, and return 1 if succeeded.*/
599static int __kprobes try_to_disable_aggr_kprobe(struct kprobe *p)
600{
601 struct kprobe *kp;
602
603 list_for_each_entry_rcu(kp, &p->list, list) {
604 if (!kprobe_disabled(kp))
605 /*
606 * There is an active probe on the list.
607 * We can't disable aggr_kprobe.
608 */
609 return 0;
610 }
611 p->flags |= KPROBE_FLAG_DISABLED;
612 return 1;
613}
614
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700615static int __kprobes in_kprobes_functions(unsigned long addr)
616{
Srinivasa Ds3d8d9962008-04-28 02:14:26 -0700617 struct kprobe_blackpoint *kb;
618
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700619 if (addr >= (unsigned long)__kprobes_text_start &&
620 addr < (unsigned long)__kprobes_text_end)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700621 return -EINVAL;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -0700622 /*
623 * If there exists a kprobe_blacklist, verify and
624 * fail any probe registration in the prohibited area
625 */
626 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
627 if (kb->start_addr) {
628 if (addr >= kb->start_addr &&
629 addr < (kb->start_addr + kb->range))
630 return -EINVAL;
631 }
632 }
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700633 return 0;
634}
635
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800636/*
637 * If we have a symbol_name argument, look it up and add the offset field
638 * to it. This way, we can specify a relative address to a symbol.
639 */
640static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
641{
642 kprobe_opcode_t *addr = p->addr;
643 if (p->symbol_name) {
644 if (addr)
645 return NULL;
646 kprobe_lookup_name(p->symbol_name, addr);
647 }
648
649 if (!addr)
650 return NULL;
651 return (kprobe_opcode_t *)(((char *)addr) + p->offset);
652}
653
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +0530654/* Check passed kprobe is valid and return kprobe in kprobe_table. */
655static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
656{
657 struct kprobe *old_p, *list_p;
658
659 old_p = get_kprobe(p->addr);
660 if (unlikely(!old_p))
661 return NULL;
662
663 if (p != old_p) {
664 list_for_each_entry_rcu(list_p, &old_p->list, list)
665 if (list_p == p)
666 /* kprobe p is a valid probe */
667 goto valid;
668 return NULL;
669 }
670valid:
671 return old_p;
672}
673
674/* Return error if the kprobe is being re-registered */
675static inline int check_kprobe_rereg(struct kprobe *p)
676{
677 int ret = 0;
678 struct kprobe *old_p;
679
680 mutex_lock(&kprobe_mutex);
681 old_p = __get_valid_kprobe(p);
682 if (old_p)
683 ret = -EINVAL;
684 mutex_unlock(&kprobe_mutex);
685 return ret;
686}
687
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800688int __kprobes register_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 int ret = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700691 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800692 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800693 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800695 addr = kprobe_addr(p);
696 if (!addr)
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700697 return -EINVAL;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800698 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700699
Ananth N Mavinakayanahalli1f0ab402009-09-15 10:43:07 +0530700 ret = check_kprobe_rereg(p);
701 if (ret)
702 return ret;
703
Masami Hiramatsua189d032008-11-12 13:26:51 -0800704 preempt_disable();
Masami Hiramatsuec30c5f2009-07-28 19:47:23 -0400705 if (!kernel_text_address((unsigned long) p->addr) ||
Masami Hiramatsua189d032008-11-12 13:26:51 -0800706 in_kprobes_functions((unsigned long) p->addr)) {
707 preempt_enable();
Mao, Bibob3e55c72005-12-12 00:37:00 -0800708 return -EINVAL;
Masami Hiramatsua189d032008-11-12 13:26:51 -0800709 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800710
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700711 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
712 p->flags &= KPROBE_FLAG_DISABLED;
713
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700714 /*
715 * Check if are we probing a module.
716 */
Masami Hiramatsua189d032008-11-12 13:26:51 -0800717 probed_mod = __module_text_address((unsigned long) p->addr);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700718 if (probed_mod) {
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700719 /*
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800720 * We must hold a refcount of the probed module while updating
721 * its code to prohibit unexpected unloading.
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800722 */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800723 if (unlikely(!try_module_get(probed_mod))) {
724 preempt_enable();
725 return -EINVAL;
726 }
Masami Hiramatsuf24659d2009-01-06 14:41:55 -0800727 /*
728 * If the module freed .init.text, we couldn't insert
729 * kprobes in there.
730 */
731 if (within_module_init((unsigned long)p->addr, probed_mod) &&
732 probed_mod->state != MODULE_STATE_COMING) {
733 module_put(probed_mod);
734 preempt_enable();
735 return -EINVAL;
736 }
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800737 }
Masami Hiramatsua189d032008-11-12 13:26:51 -0800738 preempt_enable();
Mao, Bibob3e55c72005-12-12 00:37:00 -0800739
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800740 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -0700741 INIT_LIST_HEAD(&p->list);
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800742 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700743 old_p = get_kprobe(p->addr);
744 if (old_p) {
745 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto out;
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -0500749 mutex_lock(&text_mutex);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700750 ret = arch_prepare_kprobe(p);
751 if (ret)
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -0500752 goto out_unlock_text;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800753
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700754 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800755 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
757
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700758 if (!kprobes_all_disarmed && !kprobe_disabled(p))
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700759 arch_arm_kprobe(p);
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700760
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -0500761out_unlock_text:
762 mutex_unlock(&text_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800764 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800765
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800766 if (probed_mod)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800767 module_put(probed_mod);
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return ret;
770}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700771EXPORT_SYMBOL_GPL(register_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Masami Hiramatsu98616682008-04-28 02:14:28 -0700773/*
774 * Unregister a kprobe without a scheduler synchronization.
775 */
776static int __kprobes __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800777{
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800778 struct kprobe *old_p, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700779
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700780 old_p = __get_valid_kprobe(p);
781 if (old_p == NULL)
Masami Hiramatsu98616682008-04-28 02:14:28 -0700782 return -EINVAL;
783
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700784 if (old_p == p ||
785 (old_p->pre_handler == aggr_pre_handler &&
Masami Hiramatsu98616682008-04-28 02:14:28 -0700786 list_is_singular(&old_p->list))) {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700787 /*
788 * Only probe on the hash list. Disarm only if kprobes are
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800789 * enabled and not gone - otherwise, the breakpoint would
790 * already have been removed. We save on flushing icache.
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700791 */
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400792 if (!kprobes_all_disarmed && !kprobe_disabled(old_p))
793 disarm_kprobe(p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800794 hlist_del_rcu(&old_p->hlist);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800795 } else {
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800796 if (p->break_handler && !kprobe_gone(p))
Masami Hiramatsu98616682008-04-28 02:14:28 -0700797 old_p->break_handler = NULL;
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800798 if (p->post_handler && !kprobe_gone(p)) {
Masami Hiramatsu98616682008-04-28 02:14:28 -0700799 list_for_each_entry_rcu(list_p, &old_p->list, list) {
800 if ((list_p != p) && (list_p->post_handler))
801 goto noclean;
802 }
803 old_p->post_handler = NULL;
804 }
805noclean:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800806 list_del_rcu(&p->list);
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700807 if (!kprobe_disabled(old_p)) {
808 try_to_disable_aggr_kprobe(old_p);
809 if (!kprobes_all_disarmed && kprobe_disabled(old_p))
Masami Hiramatsu201517a2009-05-07 16:31:26 -0400810 disarm_kprobe(old_p);
Masami Hiramatsude5bd882009-04-06 19:01:02 -0700811 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800812 }
Masami Hiramatsu98616682008-04-28 02:14:28 -0700813 return 0;
814}
Mao, Bibob3e55c72005-12-12 00:37:00 -0800815
Masami Hiramatsu98616682008-04-28 02:14:28 -0700816static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
817{
Masami Hiramatsu98616682008-04-28 02:14:28 -0700818 struct kprobe *old_p;
Mao, Bibob3e55c72005-12-12 00:37:00 -0800819
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800820 if (list_empty(&p->list))
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800821 arch_remove_kprobe(p);
Masami Hiramatsue8386a02009-01-06 14:41:52 -0800822 else if (list_is_singular(&p->list)) {
823 /* "p" is the last child of an aggr_kprobe */
824 old_p = list_entry(p->list.next, struct kprobe, list);
825 list_del(&p->list);
826 arch_remove_kprobe(old_p);
827 kfree(old_p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800831int __kprobes register_kprobes(struct kprobe **kps, int num)
Masami Hiramatsu98616682008-04-28 02:14:28 -0700832{
833 int i, ret = 0;
834
835 if (num <= 0)
836 return -EINVAL;
837 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800838 ret = register_kprobe(kps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -0700839 if (ret < 0) {
840 if (i > 0)
841 unregister_kprobes(kps, i);
Masami Hiramatsu98616682008-04-28 02:14:28 -0700842 break;
843 }
844 }
845 return ret;
846}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700847EXPORT_SYMBOL_GPL(register_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -0700848
Masami Hiramatsu98616682008-04-28 02:14:28 -0700849void __kprobes unregister_kprobe(struct kprobe *p)
850{
851 unregister_kprobes(&p, 1);
852}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700853EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -0700854
Masami Hiramatsu98616682008-04-28 02:14:28 -0700855void __kprobes unregister_kprobes(struct kprobe **kps, int num)
856{
857 int i;
858
859 if (num <= 0)
860 return;
861 mutex_lock(&kprobe_mutex);
862 for (i = 0; i < num; i++)
863 if (__unregister_kprobe_top(kps[i]) < 0)
864 kps[i]->addr = NULL;
865 mutex_unlock(&kprobe_mutex);
866
867 synchronize_sched();
868 for (i = 0; i < num; i++)
869 if (kps[i]->addr)
870 __unregister_kprobe_bottom(kps[i]);
871}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700872EXPORT_SYMBOL_GPL(unregister_kprobes);
Masami Hiramatsu98616682008-04-28 02:14:28 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static struct notifier_block kprobe_exceptions_nb = {
875 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -0700876 .priority = 0x7fffffff /* we need to be notified first */
877};
878
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700879unsigned long __weak arch_deref_entry_point(void *entry)
880{
881 return (unsigned long)entry;
882}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800884int __kprobes register_jprobes(struct jprobe **jps, int num)
Masami Hiramatsu26b31c12008-04-28 02:14:29 -0700885{
886 struct jprobe *jp;
887 int ret = 0, i;
888
889 if (num <= 0)
890 return -EINVAL;
891 for (i = 0; i < num; i++) {
892 unsigned long addr;
893 jp = jps[i];
894 addr = arch_deref_entry_point(jp->entry);
895
896 if (!kernel_text_address(addr))
897 ret = -EINVAL;
898 else {
899 /* Todo: Verify probepoint is a function entry point */
900 jp->kp.pre_handler = setjmp_pre_handler;
901 jp->kp.break_handler = longjmp_break_handler;
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800902 ret = register_kprobe(&jp->kp);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -0700903 }
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -0700904 if (ret < 0) {
905 if (i > 0)
906 unregister_jprobes(jps, i);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -0700907 break;
908 }
909 }
910 return ret;
911}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700912EXPORT_SYMBOL_GPL(register_jprobes);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -0700913
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700914int __kprobes register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800916 return register_jprobes(&jp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700918EXPORT_SYMBOL_GPL(register_jprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700920void __kprobes unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Masami Hiramatsu26b31c12008-04-28 02:14:29 -0700922 unregister_jprobes(&jp, 1);
923}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700924EXPORT_SYMBOL_GPL(unregister_jprobe);
Masami Hiramatsu26b31c12008-04-28 02:14:29 -0700925
Masami Hiramatsu26b31c12008-04-28 02:14:29 -0700926void __kprobes unregister_jprobes(struct jprobe **jps, int num)
927{
928 int i;
929
930 if (num <= 0)
931 return;
932 mutex_lock(&kprobe_mutex);
933 for (i = 0; i < num; i++)
934 if (__unregister_kprobe_top(&jps[i]->kp) < 0)
935 jps[i]->kp.addr = NULL;
936 mutex_unlock(&kprobe_mutex);
937
938 synchronize_sched();
939 for (i = 0; i < num; i++) {
940 if (jps[i]->kp.addr)
941 __unregister_kprobe_bottom(&jps[i]->kp);
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -0700944EXPORT_SYMBOL_GPL(unregister_jprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -0800946#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -0800947/*
948 * This kprobe pre_handler is registered with every kretprobe. When probe
949 * hits it will set up the return probe.
950 */
951static int __kprobes pre_handler_kretprobe(struct kprobe *p,
952 struct pt_regs *regs)
953{
954 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700955 unsigned long hash, flags = 0;
956 struct kretprobe_instance *ri;
Adrian Bunke65cefe2006-02-03 03:03:42 -0800957
958 /*TODO: consider to only swap the RA after the last pre_handler fired */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700959 hash = hash_ptr(current, KPROBE_HASH_BITS);
960 spin_lock_irqsave(&rp->lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700961 if (!hlist_empty(&rp->free_instances)) {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700962 ri = hlist_entry(rp->free_instances.first,
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700963 struct kretprobe_instance, hlist);
964 hlist_del(&ri->hlist);
965 spin_unlock_irqrestore(&rp->lock, flags);
966
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700967 ri->rp = rp;
968 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800969
Ananth N Mavinakayanahallif02b8622009-03-18 17:06:21 +0530970 if (rp->entry_handler && rp->entry_handler(ri, regs))
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800971 return 0;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800972
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700973 arch_prepare_kretprobe(ri, regs);
974
975 /* XXX(hch): why is there no hlist_move_head? */
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700976 INIT_HLIST_NODE(&ri->hlist);
977 kretprobe_table_lock(hash, &flags);
978 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
979 kretprobe_table_unlock(hash, &flags);
980 } else {
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700981 rp->nmissed++;
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700982 spin_unlock_irqrestore(&rp->lock, flags);
983 }
Adrian Bunke65cefe2006-02-03 03:03:42 -0800984 return 0;
985}
986
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -0800987int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700988{
989 int ret = 0;
990 struct kretprobe_instance *inst;
991 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800992 void *addr;
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700993
994 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800995 addr = kprobe_addr(&rp->kp);
996 if (!addr)
997 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700998
999 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1000 if (kretprobe_blacklist[i].addr == addr)
1001 return -EINVAL;
1002 }
1003 }
Hien Nguyenb94cce92005-06-23 00:09:19 -07001004
1005 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -07001006 rp->kp.post_handler = NULL;
1007 rp->kp.fault_handler = NULL;
1008 rp->kp.break_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -07001009
1010 /* Pre-allocate memory for max kretprobe instances */
1011 if (rp->maxactive <= 0) {
1012#ifdef CONFIG_PREEMPT
Heiko Carstensc2ef6662009-12-21 13:02:24 +01001013 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
Hien Nguyenb94cce92005-06-23 00:09:19 -07001014#else
Ananth N Mavinakayanahalli4dae5602009-10-30 19:23:10 +05301015 rp->maxactive = num_possible_cpus();
Hien Nguyenb94cce92005-06-23 00:09:19 -07001016#endif
1017 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001018 spin_lock_init(&rp->lock);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001019 INIT_HLIST_HEAD(&rp->free_instances);
1020 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -08001021 inst = kmalloc(sizeof(struct kretprobe_instance) +
1022 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001023 if (inst == NULL) {
1024 free_rp_inst(rp);
1025 return -ENOMEM;
1026 }
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001027 INIT_HLIST_NODE(&inst->hlist);
1028 hlist_add_head(&inst->hlist, &rp->free_instances);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001029 }
1030
1031 rp->nmissed = 0;
1032 /* Establish function entry probe point */
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001033 ret = register_kprobe(&rp->kp);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001034 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001035 free_rp_inst(rp);
1036 return ret;
1037}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001038EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001039
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001040int __kprobes register_kretprobes(struct kretprobe **rps, int num)
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001041{
1042 int ret = 0, i;
1043
1044 if (num <= 0)
1045 return -EINVAL;
1046 for (i = 0; i < num; i++) {
Masami Hiramatsu49ad2fd2009-01-06 14:41:53 -08001047 ret = register_kretprobe(rps[i]);
Masami Hiramatsu67dddaa2008-06-12 15:21:35 -07001048 if (ret < 0) {
1049 if (i > 0)
1050 unregister_kretprobes(rps, i);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001051 break;
1052 }
1053 }
1054 return ret;
1055}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001056EXPORT_SYMBOL_GPL(register_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001057
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001058void __kprobes unregister_kretprobe(struct kretprobe *rp)
1059{
1060 unregister_kretprobes(&rp, 1);
1061}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001062EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001063
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001064void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1065{
1066 int i;
1067
1068 if (num <= 0)
1069 return;
1070 mutex_lock(&kprobe_mutex);
1071 for (i = 0; i < num; i++)
1072 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
1073 rps[i]->kp.addr = NULL;
1074 mutex_unlock(&kprobe_mutex);
1075
1076 synchronize_sched();
1077 for (i = 0; i < num; i++) {
1078 if (rps[i]->kp.addr) {
1079 __unregister_kprobe_bottom(&rps[i]->kp);
1080 cleanup_rp_inst(rps[i]);
1081 }
1082 }
1083}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001084EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001085
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001086#else /* CONFIG_KRETPROBES */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -07001087int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -07001088{
1089 return -ENOSYS;
1090}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001091EXPORT_SYMBOL_GPL(register_kretprobe);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001092
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001093int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1094{
1095 return -ENOSYS;
1096}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001097EXPORT_SYMBOL_GPL(register_kretprobes);
1098
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001099void __kprobes unregister_kretprobe(struct kretprobe *rp)
1100{
1101}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001102EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001103
1104void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1105{
1106}
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001107EXPORT_SYMBOL_GPL(unregister_kretprobes);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001108
Srinivasa Ds346fd592007-02-20 13:57:54 -08001109static int __kprobes pre_handler_kretprobe(struct kprobe *p,
1110 struct pt_regs *regs)
1111{
1112 return 0;
1113}
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001114
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -08001115#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -07001116
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001117/* Set the kprobe gone and remove its instruction buffer. */
1118static void __kprobes kill_kprobe(struct kprobe *p)
1119{
1120 struct kprobe *kp;
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001121
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001122 p->flags |= KPROBE_FLAG_GONE;
1123 if (p->pre_handler == aggr_pre_handler) {
1124 /*
1125 * If this is an aggr_kprobe, we have to list all the
1126 * chained probes and mark them GONE.
1127 */
1128 list_for_each_entry_rcu(kp, &p->list, list)
1129 kp->flags |= KPROBE_FLAG_GONE;
1130 p->post_handler = NULL;
1131 p->break_handler = NULL;
1132 }
1133 /*
1134 * Here, we can remove insn_slot safely, because no thread calls
1135 * the original probed function (which will be freed soon) any more.
1136 */
1137 arch_remove_kprobe(p);
1138}
1139
Frederic Weisbecker24851d22009-08-26 23:38:30 +02001140void __kprobes dump_kprobe(struct kprobe *kp)
1141{
1142 printk(KERN_WARNING "Dumping kprobe:\n");
1143 printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
1144 kp->symbol_name, kp->addr, kp->offset);
1145}
1146
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001147/* Module notifier call back, checking kprobes on the module */
1148static int __kprobes kprobes_module_callback(struct notifier_block *nb,
1149 unsigned long val, void *data)
1150{
1151 struct module *mod = data;
1152 struct hlist_head *head;
1153 struct hlist_node *node;
1154 struct kprobe *p;
1155 unsigned int i;
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08001156 int checkcore = (val == MODULE_STATE_GOING);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001157
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08001158 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001159 return NOTIFY_DONE;
1160
1161 /*
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08001162 * When MODULE_STATE_GOING was notified, both of module .text and
1163 * .init.text sections would be freed. When MODULE_STATE_LIVE was
1164 * notified, only .init.text section would be freed. We need to
1165 * disable kprobes which have been inserted in the sections.
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001166 */
1167 mutex_lock(&kprobe_mutex);
1168 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1169 head = &kprobe_table[i];
1170 hlist_for_each_entry_rcu(p, node, head, hlist)
Masami Hiramatsuf24659d2009-01-06 14:41:55 -08001171 if (within_module_init((unsigned long)p->addr, mod) ||
1172 (checkcore &&
1173 within_module_core((unsigned long)p->addr, mod))) {
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001174 /*
1175 * The vaddr this probe is installed will soon
1176 * be vfreed buy not synced to disk. Hence,
1177 * disarming the breakpoint isn't needed.
1178 */
1179 kill_kprobe(p);
1180 }
1181 }
1182 mutex_unlock(&kprobe_mutex);
1183 return NOTIFY_DONE;
1184}
1185
1186static struct notifier_block kprobe_module_nb = {
1187 .notifier_call = kprobes_module_callback,
1188 .priority = 0
1189};
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static int __init init_kprobes(void)
1192{
1193 int i, err = 0;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001194 unsigned long offset = 0, size = 0;
1195 char *modname, namebuf[128];
1196 const char *symbol_name;
1197 void *addr;
1198 struct kprobe_blackpoint *kb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /* FIXME allocate the probe table, currently defined statically */
1201 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -07001202 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -07001204 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001205 spin_lock_init(&(kretprobe_table_locks[i].lock));
Hien Nguyenb94cce92005-06-23 00:09:19 -07001206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Srinivasa Ds3d8d9962008-04-28 02:14:26 -07001208 /*
1209 * Lookup and populate the kprobe_blacklist.
1210 *
1211 * Unlike the kretprobe blacklist, we'll need to determine
1212 * the range of addresses that belong to the said functions,
1213 * since a kprobe need not necessarily be at the beginning
1214 * of a function.
1215 */
1216 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
1217 kprobe_lookup_name(kb->name, addr);
1218 if (!addr)
1219 continue;
1220
1221 kb->start_addr = (unsigned long)addr;
1222 symbol_name = kallsyms_lookup(kb->start_addr,
1223 &size, &offset, &modname, namebuf);
1224 if (!symbol_name)
1225 kb->range = 0;
1226 else
1227 kb->range = size;
1228 }
1229
Masami Hiramatsuf438d912007-10-16 01:27:49 -07001230 if (kretprobe_blacklist_size) {
1231 /* lookup the function address from its name */
1232 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1233 kprobe_lookup_name(kretprobe_blacklist[i].name,
1234 kretprobe_blacklist[i].addr);
1235 if (!kretprobe_blacklist[i].addr)
1236 printk("kretprobe: lookup failed: %s\n",
1237 kretprobe_blacklist[i].name);
1238 }
1239 }
1240
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001241 /* By default, kprobes are armed */
1242 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001243
Rusty Lynch67729262005-07-05 18:54:50 -07001244 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07001245 if (!err)
1246 err = register_die_notifier(&kprobe_exceptions_nb);
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001247 if (!err)
1248 err = register_module_notifier(&kprobe_module_nb);
1249
Srinivasa D Sef53d9c2008-07-25 01:46:04 -07001250 kprobes_initialized = (err == 0);
Rusty Lynch802eae72005-06-27 15:17:08 -07001251
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01001252 if (!err)
1253 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return err;
1255}
1256
Srinivasa Ds346fd592007-02-20 13:57:54 -08001257#ifdef CONFIG_DEBUG_FS
1258static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001259 const char *sym, int offset,char *modname)
Srinivasa Ds346fd592007-02-20 13:57:54 -08001260{
1261 char *kprobe_type;
1262
1263 if (p->pre_handler == pre_handler_kretprobe)
1264 kprobe_type = "r";
1265 else if (p->pre_handler == setjmp_pre_handler)
1266 kprobe_type = "j";
1267 else
1268 kprobe_type = "k";
1269 if (sym)
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001270 seq_printf(pi, "%p %s %s+0x%x %s %s%s\n",
1271 p->addr, kprobe_type, sym, offset,
1272 (modname ? modname : " "),
1273 (kprobe_gone(p) ? "[GONE]" : ""),
1274 ((kprobe_disabled(p) && !kprobe_gone(p)) ?
1275 "[DISABLED]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08001276 else
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001277 seq_printf(pi, "%p %s %p %s%s\n",
1278 p->addr, kprobe_type, p->addr,
1279 (kprobe_gone(p) ? "[GONE]" : ""),
1280 ((kprobe_disabled(p) && !kprobe_gone(p)) ?
1281 "[DISABLED]" : ""));
Srinivasa Ds346fd592007-02-20 13:57:54 -08001282}
1283
1284static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
1285{
1286 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
1287}
1288
1289static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
1290{
1291 (*pos)++;
1292 if (*pos >= KPROBE_TABLE_SIZE)
1293 return NULL;
1294 return pos;
1295}
1296
1297static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
1298{
1299 /* Nothing to do */
1300}
1301
1302static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
1303{
1304 struct hlist_head *head;
1305 struct hlist_node *node;
1306 struct kprobe *p, *kp;
1307 const char *sym = NULL;
1308 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07001309 unsigned long offset = 0;
Srinivasa Ds346fd592007-02-20 13:57:54 -08001310 char *modname, namebuf[128];
1311
1312 head = &kprobe_table[i];
1313 preempt_disable();
1314 hlist_for_each_entry_rcu(p, node, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07001315 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08001316 &offset, &modname, namebuf);
1317 if (p->pre_handler == aggr_pre_handler) {
1318 list_for_each_entry_rcu(kp, &p->list, list)
1319 report_probe(pi, kp, sym, offset, modname);
1320 } else
1321 report_probe(pi, p, sym, offset, modname);
1322 }
1323 preempt_enable();
1324 return 0;
1325}
1326
James Morris88e9d342009-09-22 16:43:43 -07001327static const struct seq_operations kprobes_seq_ops = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08001328 .start = kprobe_seq_start,
1329 .next = kprobe_seq_next,
1330 .stop = kprobe_seq_stop,
1331 .show = show_kprobe_addr
1332};
1333
1334static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
1335{
1336 return seq_open(filp, &kprobes_seq_ops);
1337}
1338
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001339static const struct file_operations debugfs_kprobes_operations = {
Srinivasa Ds346fd592007-02-20 13:57:54 -08001340 .open = kprobes_open,
1341 .read = seq_read,
1342 .llseek = seq_lseek,
1343 .release = seq_release,
1344};
1345
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001346/* Disable one kprobe */
1347int __kprobes disable_kprobe(struct kprobe *kp)
1348{
1349 int ret = 0;
1350 struct kprobe *p;
1351
1352 mutex_lock(&kprobe_mutex);
1353
1354 /* Check whether specified probe is valid. */
1355 p = __get_valid_kprobe(kp);
1356 if (unlikely(p == NULL)) {
1357 ret = -EINVAL;
1358 goto out;
1359 }
1360
1361 /* If the probe is already disabled (or gone), just return */
1362 if (kprobe_disabled(kp))
1363 goto out;
1364
1365 kp->flags |= KPROBE_FLAG_DISABLED;
1366 if (p != kp)
1367 /* When kp != p, p is always enabled. */
1368 try_to_disable_aggr_kprobe(p);
1369
1370 if (!kprobes_all_disarmed && kprobe_disabled(p))
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001371 disarm_kprobe(p);
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001372out:
1373 mutex_unlock(&kprobe_mutex);
1374 return ret;
1375}
1376EXPORT_SYMBOL_GPL(disable_kprobe);
1377
1378/* Enable one kprobe */
1379int __kprobes enable_kprobe(struct kprobe *kp)
1380{
1381 int ret = 0;
1382 struct kprobe *p;
1383
1384 mutex_lock(&kprobe_mutex);
1385
1386 /* Check whether specified probe is valid. */
1387 p = __get_valid_kprobe(kp);
1388 if (unlikely(p == NULL)) {
1389 ret = -EINVAL;
1390 goto out;
1391 }
1392
1393 if (kprobe_gone(kp)) {
1394 /* This kprobe has gone, we couldn't enable it. */
1395 ret = -EINVAL;
1396 goto out;
1397 }
1398
1399 if (!kprobes_all_disarmed && kprobe_disabled(p))
Masami Hiramatsu201517a2009-05-07 16:31:26 -04001400 arm_kprobe(p);
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001401
1402 p->flags &= ~KPROBE_FLAG_DISABLED;
1403 if (p != kp)
1404 kp->flags &= ~KPROBE_FLAG_DISABLED;
1405out:
1406 mutex_unlock(&kprobe_mutex);
1407 return ret;
1408}
1409EXPORT_SYMBOL_GPL(enable_kprobe);
1410
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001411static void __kprobes arm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001412{
1413 struct hlist_head *head;
1414 struct hlist_node *node;
1415 struct kprobe *p;
1416 unsigned int i;
1417
1418 mutex_lock(&kprobe_mutex);
1419
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001420 /* If kprobes are armed, just return */
1421 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001422 goto already_enabled;
1423
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -05001424 mutex_lock(&text_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001425 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1426 head = &kprobe_table[i];
1427 hlist_for_each_entry_rcu(p, node, head, hlist)
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001428 if (!kprobe_disabled(p))
Masami Hiramatsue8386a02009-01-06 14:41:52 -08001429 arch_arm_kprobe(p);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001430 }
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -05001431 mutex_unlock(&text_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001432
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001433 kprobes_all_disarmed = false;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001434 printk(KERN_INFO "Kprobes globally enabled\n");
1435
1436already_enabled:
1437 mutex_unlock(&kprobe_mutex);
1438 return;
1439}
1440
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001441static void __kprobes disarm_all_kprobes(void)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001442{
1443 struct hlist_head *head;
1444 struct hlist_node *node;
1445 struct kprobe *p;
1446 unsigned int i;
1447
1448 mutex_lock(&kprobe_mutex);
1449
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001450 /* If kprobes are already disarmed, just return */
1451 if (kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001452 goto already_disabled;
1453
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001454 kprobes_all_disarmed = true;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001455 printk(KERN_INFO "Kprobes globally disabled\n");
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -05001456 mutex_lock(&text_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001457 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1458 head = &kprobe_table[i];
1459 hlist_for_each_entry_rcu(p, node, head, hlist) {
Masami Hiramatsude5bd882009-04-06 19:01:02 -07001460 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001461 arch_disarm_kprobe(p);
1462 }
1463 }
1464
Mathieu Desnoyers4460fda2009-03-06 10:36:38 -05001465 mutex_unlock(&text_mutex);
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001466 mutex_unlock(&kprobe_mutex);
1467 /* Allow all currently running kprobes to complete */
1468 synchronize_sched();
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001469 return;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001470
1471already_disabled:
1472 mutex_unlock(&kprobe_mutex);
1473 return;
1474}
1475
1476/*
1477 * XXX: The debugfs bool file interface doesn't allow for callbacks
1478 * when the bool state is switched. We can reuse that facility when
1479 * available
1480 */
1481static ssize_t read_enabled_file_bool(struct file *file,
1482 char __user *user_buf, size_t count, loff_t *ppos)
1483{
1484 char buf[3];
1485
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001486 if (!kprobes_all_disarmed)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001487 buf[0] = '1';
1488 else
1489 buf[0] = '0';
1490 buf[1] = '\n';
1491 buf[2] = 0x00;
1492 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1493}
1494
1495static ssize_t write_enabled_file_bool(struct file *file,
1496 const char __user *user_buf, size_t count, loff_t *ppos)
1497{
1498 char buf[32];
1499 int buf_size;
1500
1501 buf_size = min(count, (sizeof(buf)-1));
1502 if (copy_from_user(buf, user_buf, buf_size))
1503 return -EFAULT;
1504
1505 switch (buf[0]) {
1506 case 'y':
1507 case 'Y':
1508 case '1':
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001509 arm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001510 break;
1511 case 'n':
1512 case 'N':
1513 case '0':
Masami Hiramatsue579abe2009-04-06 19:01:01 -07001514 disarm_all_kprobes();
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001515 break;
1516 }
1517
1518 return count;
1519}
1520
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001521static const struct file_operations fops_kp = {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001522 .read = read_enabled_file_bool,
1523 .write = write_enabled_file_bool,
1524};
1525
Srinivasa Ds346fd592007-02-20 13:57:54 -08001526static int __kprobes debugfs_kprobe_init(void)
1527{
1528 struct dentry *dir, *file;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001529 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08001530
1531 dir = debugfs_create_dir("kprobes", NULL);
1532 if (!dir)
1533 return -ENOMEM;
1534
Randy Dunlape3869792007-05-08 00:27:01 -07001535 file = debugfs_create_file("list", 0444, dir, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08001536 &debugfs_kprobes_operations);
1537 if (!file) {
1538 debugfs_remove(dir);
1539 return -ENOMEM;
1540 }
1541
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001542 file = debugfs_create_file("enabled", 0600, dir,
1543 &value, &fops_kp);
1544 if (!file) {
1545 debugfs_remove(dir);
1546 return -ENOMEM;
1547 }
1548
Srinivasa Ds346fd592007-02-20 13:57:54 -08001549 return 0;
1550}
1551
1552late_initcall(debugfs_kprobe_init);
1553#endif /* CONFIG_DEBUG_FS */
1554
1555module_init(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Masami Hiramatsu99081ab2009-04-06 19:00:59 -07001557/* defined in arch/.../kernel/kprobes.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558EXPORT_SYMBOL_GPL(jprobe_return);