kprobes.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  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.
  30. * 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.
  33. */
  34. #include <linux/kprobes.h>
  35. #include <linux/hash.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/stddef.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleloader.h>
  41. #include <linux/kallsyms.h>
  42. #include <linux/freezer.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/kdebug.h>
  46. #include <asm-generic/sections.h>
  47. #include <asm/cacheflush.h>
  48. #include <asm/errno.h>
  49. #include <asm/uaccess.h>
  50. #define KPROBE_HASH_BITS 6
  51. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  52. /*
  53. * Some oddball architectures like 64bit powerpc have function descriptors
  54. * so this must be overridable.
  55. */
  56. #ifndef kprobe_lookup_name
  57. #define kprobe_lookup_name(name, addr) \
  58. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  59. #endif
  60. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  61. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  62. static atomic_t kprobe_count;
  63. /* NOTE: change this value only with kprobe_mutex held */
  64. static bool kprobe_enabled;
  65. DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  66. DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
  67. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  68. static struct notifier_block kprobe_page_fault_nb = {
  69. .notifier_call = kprobe_exceptions_notify,
  70. .priority = 0x7fffffff /* we need to notified first */
  71. };
  72. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  73. /*
  74. * kprobe->ainsn.insn points to the copy of the instruction to be
  75. * single-stepped. x86_64, POWER4 and above have no-exec support and
  76. * stepping on the instruction on a vmalloced/kmalloced/data page
  77. * is a recipe for disaster
  78. */
  79. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  80. struct kprobe_insn_page {
  81. struct hlist_node hlist;
  82. kprobe_opcode_t *insns; /* Page of instruction slots */
  83. char slot_used[INSNS_PER_PAGE];
  84. int nused;
  85. int ngarbage;
  86. };
  87. enum kprobe_slot_state {
  88. SLOT_CLEAN = 0,
  89. SLOT_DIRTY = 1,
  90. SLOT_USED = 2,
  91. };
  92. static struct hlist_head kprobe_insn_pages;
  93. static int kprobe_garbage_slots;
  94. static int collect_garbage_slots(void);
  95. static int __kprobes check_safety(void)
  96. {
  97. int ret = 0;
  98. #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
  99. ret = freeze_processes();
  100. if (ret == 0) {
  101. struct task_struct *p, *q;
  102. do_each_thread(p, q) {
  103. if (p != current && p->state == TASK_RUNNING &&
  104. p->pid != 0) {
  105. printk("Check failed: %s is running\n",p->comm);
  106. ret = -1;
  107. goto loop_end;
  108. }
  109. } while_each_thread(p, q);
  110. }
  111. loop_end:
  112. thaw_processes();
  113. #else
  114. synchronize_sched();
  115. #endif
  116. return ret;
  117. }
  118. /**
  119. * get_insn_slot() - Find a slot on an executable page for an instruction.
  120. * We allocate an executable page if there's no room on existing ones.
  121. */
  122. kprobe_opcode_t __kprobes *get_insn_slot(void)
  123. {
  124. struct kprobe_insn_page *kip;
  125. struct hlist_node *pos;
  126. retry:
  127. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  128. if (kip->nused < INSNS_PER_PAGE) {
  129. int i;
  130. for (i = 0; i < INSNS_PER_PAGE; i++) {
  131. if (kip->slot_used[i] == SLOT_CLEAN) {
  132. kip->slot_used[i] = SLOT_USED;
  133. kip->nused++;
  134. return kip->insns + (i * MAX_INSN_SIZE);
  135. }
  136. }
  137. /* Surprise! No unused slots. Fix kip->nused. */
  138. kip->nused = INSNS_PER_PAGE;
  139. }
  140. }
  141. /* If there are any garbage slots, collect it and try again. */
  142. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  143. goto retry;
  144. }
  145. /* All out of space. Need to allocate a new page. Use slot 0. */
  146. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  147. if (!kip)
  148. return NULL;
  149. /*
  150. * Use module_alloc so this page is within +/- 2GB of where the
  151. * kernel image and loaded module images reside. This is required
  152. * so x86_64 can correctly handle the %rip-relative fixups.
  153. */
  154. kip->insns = module_alloc(PAGE_SIZE);
  155. if (!kip->insns) {
  156. kfree(kip);
  157. return NULL;
  158. }
  159. INIT_HLIST_NODE(&kip->hlist);
  160. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  161. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  162. kip->slot_used[0] = SLOT_USED;
  163. kip->nused = 1;
  164. kip->ngarbage = 0;
  165. return kip->insns;
  166. }
  167. /* Return 1 if all garbages are collected, otherwise 0. */
  168. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  169. {
  170. kip->slot_used[idx] = SLOT_CLEAN;
  171. kip->nused--;
  172. if (kip->nused == 0) {
  173. /*
  174. * Page is no longer in use. Free it unless
  175. * it's the last one. We keep the last one
  176. * so as not to have to set it up again the
  177. * next time somebody inserts a probe.
  178. */
  179. hlist_del(&kip->hlist);
  180. if (hlist_empty(&kprobe_insn_pages)) {
  181. INIT_HLIST_NODE(&kip->hlist);
  182. hlist_add_head(&kip->hlist,
  183. &kprobe_insn_pages);
  184. } else {
  185. module_free(NULL, kip->insns);
  186. kfree(kip);
  187. }
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. static int __kprobes collect_garbage_slots(void)
  193. {
  194. struct kprobe_insn_page *kip;
  195. struct hlist_node *pos, *next;
  196. /* Ensure no-one is preepmted on the garbages */
  197. if (check_safety() != 0)
  198. return -EAGAIN;
  199. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  200. int i;
  201. if (kip->ngarbage == 0)
  202. continue;
  203. kip->ngarbage = 0; /* we will collect all garbages */
  204. for (i = 0; i < INSNS_PER_PAGE; i++) {
  205. if (kip->slot_used[i] == SLOT_DIRTY &&
  206. collect_one_slot(kip, i))
  207. break;
  208. }
  209. }
  210. kprobe_garbage_slots = 0;
  211. return 0;
  212. }
  213. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  214. {
  215. struct kprobe_insn_page *kip;
  216. struct hlist_node *pos;
  217. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  218. if (kip->insns <= slot &&
  219. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  220. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  221. if (dirty) {
  222. kip->slot_used[i] = SLOT_DIRTY;
  223. kip->ngarbage++;
  224. } else {
  225. collect_one_slot(kip, i);
  226. }
  227. break;
  228. }
  229. }
  230. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  231. collect_garbage_slots();
  232. }
  233. #endif
  234. /* We have preemption disabled.. so it is safe to use __ versions */
  235. static inline void set_kprobe_instance(struct kprobe *kp)
  236. {
  237. __get_cpu_var(kprobe_instance) = kp;
  238. }
  239. static inline void reset_kprobe_instance(void)
  240. {
  241. __get_cpu_var(kprobe_instance) = NULL;
  242. }
  243. /*
  244. * This routine is called either:
  245. * - under the kprobe_mutex - during kprobe_[un]register()
  246. * OR
  247. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  248. */
  249. struct kprobe __kprobes *get_kprobe(void *addr)
  250. {
  251. struct hlist_head *head;
  252. struct hlist_node *node;
  253. struct kprobe *p;
  254. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  255. hlist_for_each_entry_rcu(p, node, head, hlist) {
  256. if (p->addr == addr)
  257. return p;
  258. }
  259. return NULL;
  260. }
  261. /*
  262. * Aggregate handlers for multiple kprobes support - these handlers
  263. * take care of invoking the individual kprobe handlers on p->list
  264. */
  265. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  266. {
  267. struct kprobe *kp;
  268. list_for_each_entry_rcu(kp, &p->list, list) {
  269. if (kp->pre_handler) {
  270. set_kprobe_instance(kp);
  271. if (kp->pre_handler(kp, regs))
  272. return 1;
  273. }
  274. reset_kprobe_instance();
  275. }
  276. return 0;
  277. }
  278. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  279. unsigned long flags)
  280. {
  281. struct kprobe *kp;
  282. list_for_each_entry_rcu(kp, &p->list, list) {
  283. if (kp->post_handler) {
  284. set_kprobe_instance(kp);
  285. kp->post_handler(kp, regs, flags);
  286. reset_kprobe_instance();
  287. }
  288. }
  289. }
  290. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  291. int trapnr)
  292. {
  293. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  294. /*
  295. * if we faulted "during" the execution of a user specified
  296. * probe handler, invoke just that probe's fault handler
  297. */
  298. if (cur && cur->fault_handler) {
  299. if (cur->fault_handler(cur, regs, trapnr))
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  305. {
  306. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  307. int ret = 0;
  308. if (cur && cur->break_handler) {
  309. if (cur->break_handler(cur, regs))
  310. ret = 1;
  311. }
  312. reset_kprobe_instance();
  313. return ret;
  314. }
  315. /* Walks the list and increments nmissed count for multiprobe case */
  316. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  317. {
  318. struct kprobe *kp;
  319. if (p->pre_handler != aggr_pre_handler) {
  320. p->nmissed++;
  321. } else {
  322. list_for_each_entry_rcu(kp, &p->list, list)
  323. kp->nmissed++;
  324. }
  325. return;
  326. }
  327. /* Called with kretprobe_lock held */
  328. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  329. struct hlist_head *head)
  330. {
  331. /* remove rp inst off the rprobe_inst_table */
  332. hlist_del(&ri->hlist);
  333. if (ri->rp) {
  334. /* remove rp inst off the used list */
  335. hlist_del(&ri->uflist);
  336. /* put rp inst back onto the free list */
  337. INIT_HLIST_NODE(&ri->uflist);
  338. hlist_add_head(&ri->uflist, &ri->rp->free_instances);
  339. } else
  340. /* Unregistering */
  341. hlist_add_head(&ri->hlist, head);
  342. }
  343. struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
  344. {
  345. return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  346. }
  347. /*
  348. * This function is called from finish_task_switch when task tk becomes dead,
  349. * so that we can recycle any function-return probe instances associated
  350. * with this task. These left over instances represent probed functions
  351. * that have been called but will never return.
  352. */
  353. void __kprobes kprobe_flush_task(struct task_struct *tk)
  354. {
  355. struct kretprobe_instance *ri;
  356. struct hlist_head *head, empty_rp;
  357. struct hlist_node *node, *tmp;
  358. unsigned long flags = 0;
  359. INIT_HLIST_HEAD(&empty_rp);
  360. spin_lock_irqsave(&kretprobe_lock, flags);
  361. head = kretprobe_inst_table_head(tk);
  362. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  363. if (ri->task == tk)
  364. recycle_rp_inst(ri, &empty_rp);
  365. }
  366. spin_unlock_irqrestore(&kretprobe_lock, flags);
  367. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  368. hlist_del(&ri->hlist);
  369. kfree(ri);
  370. }
  371. }
  372. static inline void free_rp_inst(struct kretprobe *rp)
  373. {
  374. struct kretprobe_instance *ri;
  375. struct hlist_node *pos, *next;
  376. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
  377. hlist_del(&ri->uflist);
  378. kfree(ri);
  379. }
  380. }
  381. /*
  382. * Keep all fields in the kprobe consistent
  383. */
  384. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  385. {
  386. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  387. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  388. }
  389. /*
  390. * Add the new probe to old_p->list. Fail if this is the
  391. * second jprobe at the address - two jprobes can't coexist
  392. */
  393. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  394. {
  395. if (p->break_handler) {
  396. if (old_p->break_handler)
  397. return -EEXIST;
  398. list_add_tail_rcu(&p->list, &old_p->list);
  399. old_p->break_handler = aggr_break_handler;
  400. } else
  401. list_add_rcu(&p->list, &old_p->list);
  402. if (p->post_handler && !old_p->post_handler)
  403. old_p->post_handler = aggr_post_handler;
  404. return 0;
  405. }
  406. /*
  407. * Fill in the required fields of the "manager kprobe". Replace the
  408. * earlier kprobe in the hlist with the manager kprobe
  409. */
  410. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  411. {
  412. copy_kprobe(p, ap);
  413. flush_insn_slot(ap);
  414. ap->addr = p->addr;
  415. ap->pre_handler = aggr_pre_handler;
  416. ap->fault_handler = aggr_fault_handler;
  417. if (p->post_handler)
  418. ap->post_handler = aggr_post_handler;
  419. if (p->break_handler)
  420. ap->break_handler = aggr_break_handler;
  421. INIT_LIST_HEAD(&ap->list);
  422. list_add_rcu(&p->list, &ap->list);
  423. hlist_replace_rcu(&p->hlist, &ap->hlist);
  424. }
  425. /*
  426. * This is the second or subsequent kprobe at the address - handle
  427. * the intricacies
  428. */
  429. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  430. struct kprobe *p)
  431. {
  432. int ret = 0;
  433. struct kprobe *ap;
  434. if (old_p->pre_handler == aggr_pre_handler) {
  435. copy_kprobe(old_p, p);
  436. ret = add_new_kprobe(old_p, p);
  437. } else {
  438. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  439. if (!ap)
  440. return -ENOMEM;
  441. add_aggr_kprobe(ap, old_p);
  442. copy_kprobe(ap, p);
  443. ret = add_new_kprobe(ap, p);
  444. }
  445. return ret;
  446. }
  447. static int __kprobes in_kprobes_functions(unsigned long addr)
  448. {
  449. if (addr >= (unsigned long)__kprobes_text_start &&
  450. addr < (unsigned long)__kprobes_text_end)
  451. return -EINVAL;
  452. return 0;
  453. }
  454. static int __kprobes __register_kprobe(struct kprobe *p,
  455. unsigned long called_from)
  456. {
  457. int ret = 0;
  458. struct kprobe *old_p;
  459. struct module *probed_mod;
  460. /*
  461. * If we have a symbol_name argument look it up,
  462. * and add it to the address. That way the addr
  463. * field can either be global or relative to a symbol.
  464. */
  465. if (p->symbol_name) {
  466. if (p->addr)
  467. return -EINVAL;
  468. kprobe_lookup_name(p->symbol_name, p->addr);
  469. }
  470. if (!p->addr)
  471. return -EINVAL;
  472. p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
  473. if (!kernel_text_address((unsigned long) p->addr) ||
  474. in_kprobes_functions((unsigned long) p->addr))
  475. return -EINVAL;
  476. p->mod_refcounted = 0;
  477. /*
  478. * Check if are we probing a module.
  479. */
  480. probed_mod = module_text_address((unsigned long) p->addr);
  481. if (probed_mod) {
  482. struct module *calling_mod = module_text_address(called_from);
  483. /*
  484. * We must allow modules to probe themself and in this case
  485. * avoid incrementing the module refcount, so as to allow
  486. * unloading of self probing modules.
  487. */
  488. if (calling_mod && calling_mod != probed_mod) {
  489. if (unlikely(!try_module_get(probed_mod)))
  490. return -EINVAL;
  491. p->mod_refcounted = 1;
  492. } else
  493. probed_mod = NULL;
  494. }
  495. p->nmissed = 0;
  496. mutex_lock(&kprobe_mutex);
  497. old_p = get_kprobe(p->addr);
  498. if (old_p) {
  499. ret = register_aggr_kprobe(old_p, p);
  500. if (!ret)
  501. atomic_inc(&kprobe_count);
  502. goto out;
  503. }
  504. ret = arch_prepare_kprobe(p);
  505. if (ret)
  506. goto out;
  507. INIT_HLIST_NODE(&p->hlist);
  508. hlist_add_head_rcu(&p->hlist,
  509. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  510. if (kprobe_enabled) {
  511. if (atomic_add_return(1, &kprobe_count) == \
  512. (ARCH_INACTIVE_KPROBE_COUNT + 1))
  513. register_page_fault_notifier(&kprobe_page_fault_nb);
  514. arch_arm_kprobe(p);
  515. }
  516. out:
  517. mutex_unlock(&kprobe_mutex);
  518. if (ret && probed_mod)
  519. module_put(probed_mod);
  520. return ret;
  521. }
  522. int __kprobes register_kprobe(struct kprobe *p)
  523. {
  524. return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
  525. }
  526. void __kprobes unregister_kprobe(struct kprobe *p)
  527. {
  528. struct module *mod;
  529. struct kprobe *old_p, *list_p;
  530. int cleanup_p;
  531. mutex_lock(&kprobe_mutex);
  532. old_p = get_kprobe(p->addr);
  533. if (unlikely(!old_p)) {
  534. mutex_unlock(&kprobe_mutex);
  535. return;
  536. }
  537. if (p != old_p) {
  538. list_for_each_entry_rcu(list_p, &old_p->list, list)
  539. if (list_p == p)
  540. /* kprobe p is a valid probe */
  541. goto valid_p;
  542. mutex_unlock(&kprobe_mutex);
  543. return;
  544. }
  545. valid_p:
  546. if (old_p == p ||
  547. (old_p->pre_handler == aggr_pre_handler &&
  548. p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
  549. /*
  550. * Only probe on the hash list. Disarm only if kprobes are
  551. * enabled - otherwise, the breakpoint would already have
  552. * been removed. We save on flushing icache.
  553. */
  554. if (kprobe_enabled)
  555. arch_disarm_kprobe(p);
  556. hlist_del_rcu(&old_p->hlist);
  557. cleanup_p = 1;
  558. } else {
  559. list_del_rcu(&p->list);
  560. cleanup_p = 0;
  561. }
  562. mutex_unlock(&kprobe_mutex);
  563. synchronize_sched();
  564. if (p->mod_refcounted) {
  565. mod = module_text_address((unsigned long)p->addr);
  566. if (mod)
  567. module_put(mod);
  568. }
  569. if (cleanup_p) {
  570. if (p != old_p) {
  571. list_del_rcu(&p->list);
  572. kfree(old_p);
  573. }
  574. arch_remove_kprobe(p);
  575. } else {
  576. mutex_lock(&kprobe_mutex);
  577. if (p->break_handler)
  578. old_p->break_handler = NULL;
  579. if (p->post_handler){
  580. list_for_each_entry_rcu(list_p, &old_p->list, list){
  581. if (list_p->post_handler){
  582. cleanup_p = 2;
  583. break;
  584. }
  585. }
  586. if (cleanup_p == 0)
  587. old_p->post_handler = NULL;
  588. }
  589. mutex_unlock(&kprobe_mutex);
  590. }
  591. /* Call unregister_page_fault_notifier()
  592. * if no probes are active
  593. */
  594. mutex_lock(&kprobe_mutex);
  595. if (atomic_add_return(-1, &kprobe_count) == \
  596. ARCH_INACTIVE_KPROBE_COUNT)
  597. unregister_page_fault_notifier(&kprobe_page_fault_nb);
  598. mutex_unlock(&kprobe_mutex);
  599. return;
  600. }
  601. static struct notifier_block kprobe_exceptions_nb = {
  602. .notifier_call = kprobe_exceptions_notify,
  603. .priority = 0x7fffffff /* we need to be notified first */
  604. };
  605. int __kprobes register_jprobe(struct jprobe *jp)
  606. {
  607. /* Todo: Verify probepoint is a function entry point */
  608. jp->kp.pre_handler = setjmp_pre_handler;
  609. jp->kp.break_handler = longjmp_break_handler;
  610. return __register_kprobe(&jp->kp,
  611. (unsigned long)__builtin_return_address(0));
  612. }
  613. void __kprobes unregister_jprobe(struct jprobe *jp)
  614. {
  615. unregister_kprobe(&jp->kp);
  616. }
  617. #ifdef ARCH_SUPPORTS_KRETPROBES
  618. /*
  619. * This kprobe pre_handler is registered with every kretprobe. When probe
  620. * hits it will set up the return probe.
  621. */
  622. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  623. struct pt_regs *regs)
  624. {
  625. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  626. unsigned long flags = 0;
  627. /*TODO: consider to only swap the RA after the last pre_handler fired */
  628. spin_lock_irqsave(&kretprobe_lock, flags);
  629. if (!hlist_empty(&rp->free_instances)) {
  630. struct kretprobe_instance *ri;
  631. ri = hlist_entry(rp->free_instances.first,
  632. struct kretprobe_instance, uflist);
  633. ri->rp = rp;
  634. ri->task = current;
  635. arch_prepare_kretprobe(ri, regs);
  636. /* XXX(hch): why is there no hlist_move_head? */
  637. hlist_del(&ri->uflist);
  638. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  639. hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
  640. } else
  641. rp->nmissed++;
  642. spin_unlock_irqrestore(&kretprobe_lock, flags);
  643. return 0;
  644. }
  645. int __kprobes register_kretprobe(struct kretprobe *rp)
  646. {
  647. int ret = 0;
  648. struct kretprobe_instance *inst;
  649. int i;
  650. rp->kp.pre_handler = pre_handler_kretprobe;
  651. rp->kp.post_handler = NULL;
  652. rp->kp.fault_handler = NULL;
  653. rp->kp.break_handler = NULL;
  654. /* Pre-allocate memory for max kretprobe instances */
  655. if (rp->maxactive <= 0) {
  656. #ifdef CONFIG_PREEMPT
  657. rp->maxactive = max(10, 2 * NR_CPUS);
  658. #else
  659. rp->maxactive = NR_CPUS;
  660. #endif
  661. }
  662. INIT_HLIST_HEAD(&rp->used_instances);
  663. INIT_HLIST_HEAD(&rp->free_instances);
  664. for (i = 0; i < rp->maxactive; i++) {
  665. inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
  666. if (inst == NULL) {
  667. free_rp_inst(rp);
  668. return -ENOMEM;
  669. }
  670. INIT_HLIST_NODE(&inst->uflist);
  671. hlist_add_head(&inst->uflist, &rp->free_instances);
  672. }
  673. rp->nmissed = 0;
  674. /* Establish function entry probe point */
  675. if ((ret = __register_kprobe(&rp->kp,
  676. (unsigned long)__builtin_return_address(0))) != 0)
  677. free_rp_inst(rp);
  678. return ret;
  679. }
  680. #else /* ARCH_SUPPORTS_KRETPROBES */
  681. int __kprobes register_kretprobe(struct kretprobe *rp)
  682. {
  683. return -ENOSYS;
  684. }
  685. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  686. struct pt_regs *regs)
  687. {
  688. return 0;
  689. }
  690. #endif /* ARCH_SUPPORTS_KRETPROBES */
  691. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  692. {
  693. unsigned long flags;
  694. struct kretprobe_instance *ri;
  695. struct hlist_node *pos, *next;
  696. unregister_kprobe(&rp->kp);
  697. /* No race here */
  698. spin_lock_irqsave(&kretprobe_lock, flags);
  699. hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
  700. ri->rp = NULL;
  701. hlist_del(&ri->uflist);
  702. }
  703. spin_unlock_irqrestore(&kretprobe_lock, flags);
  704. free_rp_inst(rp);
  705. }
  706. static int __init init_kprobes(void)
  707. {
  708. int i, err = 0;
  709. /* FIXME allocate the probe table, currently defined statically */
  710. /* initialize all list heads */
  711. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  712. INIT_HLIST_HEAD(&kprobe_table[i]);
  713. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  714. }
  715. atomic_set(&kprobe_count, 0);
  716. /* By default, kprobes are enabled */
  717. kprobe_enabled = true;
  718. err = arch_init_kprobes();
  719. if (!err)
  720. err = register_die_notifier(&kprobe_exceptions_nb);
  721. return err;
  722. }
  723. #ifdef CONFIG_DEBUG_FS
  724. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  725. const char *sym, int offset,char *modname)
  726. {
  727. char *kprobe_type;
  728. if (p->pre_handler == pre_handler_kretprobe)
  729. kprobe_type = "r";
  730. else if (p->pre_handler == setjmp_pre_handler)
  731. kprobe_type = "j";
  732. else
  733. kprobe_type = "k";
  734. if (sym)
  735. seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
  736. sym, offset, (modname ? modname : " "));
  737. else
  738. seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
  739. }
  740. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  741. {
  742. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  743. }
  744. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  745. {
  746. (*pos)++;
  747. if (*pos >= KPROBE_TABLE_SIZE)
  748. return NULL;
  749. return pos;
  750. }
  751. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  752. {
  753. /* Nothing to do */
  754. }
  755. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  756. {
  757. struct hlist_head *head;
  758. struct hlist_node *node;
  759. struct kprobe *p, *kp;
  760. const char *sym = NULL;
  761. unsigned int i = *(loff_t *) v;
  762. unsigned long offset = 0;
  763. char *modname, namebuf[128];
  764. head = &kprobe_table[i];
  765. preempt_disable();
  766. hlist_for_each_entry_rcu(p, node, head, hlist) {
  767. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  768. &offset, &modname, namebuf);
  769. if (p->pre_handler == aggr_pre_handler) {
  770. list_for_each_entry_rcu(kp, &p->list, list)
  771. report_probe(pi, kp, sym, offset, modname);
  772. } else
  773. report_probe(pi, p, sym, offset, modname);
  774. }
  775. preempt_enable();
  776. return 0;
  777. }
  778. static struct seq_operations kprobes_seq_ops = {
  779. .start = kprobe_seq_start,
  780. .next = kprobe_seq_next,
  781. .stop = kprobe_seq_stop,
  782. .show = show_kprobe_addr
  783. };
  784. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  785. {
  786. return seq_open(filp, &kprobes_seq_ops);
  787. }
  788. static struct file_operations debugfs_kprobes_operations = {
  789. .open = kprobes_open,
  790. .read = seq_read,
  791. .llseek = seq_lseek,
  792. .release = seq_release,
  793. };
  794. static void __kprobes enable_all_kprobes(void)
  795. {
  796. struct hlist_head *head;
  797. struct hlist_node *node;
  798. struct kprobe *p;
  799. unsigned int i;
  800. mutex_lock(&kprobe_mutex);
  801. /* If kprobes are already enabled, just return */
  802. if (kprobe_enabled)
  803. goto already_enabled;
  804. /*
  805. * Re-register the page fault notifier only if there are any
  806. * active probes at the time of enabling kprobes globally
  807. */
  808. if (atomic_read(&kprobe_count) > ARCH_INACTIVE_KPROBE_COUNT)
  809. register_page_fault_notifier(&kprobe_page_fault_nb);
  810. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  811. head = &kprobe_table[i];
  812. hlist_for_each_entry_rcu(p, node, head, hlist)
  813. arch_arm_kprobe(p);
  814. }
  815. kprobe_enabled = true;
  816. printk(KERN_INFO "Kprobes globally enabled\n");
  817. already_enabled:
  818. mutex_unlock(&kprobe_mutex);
  819. return;
  820. }
  821. static void __kprobes disable_all_kprobes(void)
  822. {
  823. struct hlist_head *head;
  824. struct hlist_node *node;
  825. struct kprobe *p;
  826. unsigned int i;
  827. mutex_lock(&kprobe_mutex);
  828. /* If kprobes are already disabled, just return */
  829. if (!kprobe_enabled)
  830. goto already_disabled;
  831. kprobe_enabled = false;
  832. printk(KERN_INFO "Kprobes globally disabled\n");
  833. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  834. head = &kprobe_table[i];
  835. hlist_for_each_entry_rcu(p, node, head, hlist) {
  836. if (!arch_trampoline_kprobe(p))
  837. arch_disarm_kprobe(p);
  838. }
  839. }
  840. mutex_unlock(&kprobe_mutex);
  841. /* Allow all currently running kprobes to complete */
  842. synchronize_sched();
  843. mutex_lock(&kprobe_mutex);
  844. /* Unconditionally unregister the page_fault notifier */
  845. unregister_page_fault_notifier(&kprobe_page_fault_nb);
  846. already_disabled:
  847. mutex_unlock(&kprobe_mutex);
  848. return;
  849. }
  850. /*
  851. * XXX: The debugfs bool file interface doesn't allow for callbacks
  852. * when the bool state is switched. We can reuse that facility when
  853. * available
  854. */
  855. static ssize_t read_enabled_file_bool(struct file *file,
  856. char __user *user_buf, size_t count, loff_t *ppos)
  857. {
  858. char buf[3];
  859. if (kprobe_enabled)
  860. buf[0] = '1';
  861. else
  862. buf[0] = '0';
  863. buf[1] = '\n';
  864. buf[2] = 0x00;
  865. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  866. }
  867. static ssize_t write_enabled_file_bool(struct file *file,
  868. const char __user *user_buf, size_t count, loff_t *ppos)
  869. {
  870. char buf[32];
  871. int buf_size;
  872. buf_size = min(count, (sizeof(buf)-1));
  873. if (copy_from_user(buf, user_buf, buf_size))
  874. return -EFAULT;
  875. switch (buf[0]) {
  876. case 'y':
  877. case 'Y':
  878. case '1':
  879. enable_all_kprobes();
  880. break;
  881. case 'n':
  882. case 'N':
  883. case '0':
  884. disable_all_kprobes();
  885. break;
  886. }
  887. return count;
  888. }
  889. static struct file_operations fops_kp = {
  890. .read = read_enabled_file_bool,
  891. .write = write_enabled_file_bool,
  892. };
  893. static int __kprobes debugfs_kprobe_init(void)
  894. {
  895. struct dentry *dir, *file;
  896. unsigned int value = 1;
  897. dir = debugfs_create_dir("kprobes", NULL);
  898. if (!dir)
  899. return -ENOMEM;
  900. file = debugfs_create_file("list", 0444, dir, NULL,
  901. &debugfs_kprobes_operations);
  902. if (!file) {
  903. debugfs_remove(dir);
  904. return -ENOMEM;
  905. }
  906. file = debugfs_create_file("enabled", 0600, dir,
  907. &value, &fops_kp);
  908. if (!file) {
  909. debugfs_remove(dir);
  910. return -ENOMEM;
  911. }
  912. return 0;
  913. }
  914. late_initcall(debugfs_kprobe_init);
  915. #endif /* CONFIG_DEBUG_FS */
  916. module_init(init_kprobes);
  917. EXPORT_SYMBOL_GPL(register_kprobe);
  918. EXPORT_SYMBOL_GPL(unregister_kprobe);
  919. EXPORT_SYMBOL_GPL(register_jprobe);
  920. EXPORT_SYMBOL_GPL(unregister_jprobe);
  921. EXPORT_SYMBOL_GPL(jprobe_return);
  922. EXPORT_SYMBOL_GPL(register_kretprobe);
  923. EXPORT_SYMBOL_GPL(unregister_kretprobe);