kprobes.c 23 KB

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