kprobes.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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 <linux/memory.h>
  47. #include <asm-generic/sections.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/errno.h>
  50. #include <asm/uaccess.h>
  51. #define KPROBE_HASH_BITS 6
  52. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  53. /*
  54. * Some oddball architectures like 64bit powerpc have function descriptors
  55. * so this must be overridable.
  56. */
  57. #ifndef kprobe_lookup_name
  58. #define kprobe_lookup_name(name, addr) \
  59. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  60. #endif
  61. static int kprobes_initialized;
  62. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  63. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  64. /* NOTE: change this value only with kprobe_mutex held */
  65. static bool kprobe_enabled;
  66. static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  67. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  68. static struct {
  69. spinlock_t lock ____cacheline_aligned_in_smp;
  70. } kretprobe_table_locks[KPROBE_TABLE_SIZE];
  71. static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
  72. {
  73. return &(kretprobe_table_locks[hash].lock);
  74. }
  75. /*
  76. * Normally, functions that we'd want to prohibit kprobes in, are marked
  77. * __kprobes. But, there are cases where such functions already belong to
  78. * a different section (__sched for preempt_schedule)
  79. *
  80. * For such cases, we now have a blacklist
  81. */
  82. static struct kprobe_blackpoint kprobe_blacklist[] = {
  83. {"preempt_schedule",},
  84. {NULL} /* Terminator */
  85. };
  86. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  87. /*
  88. * kprobe->ainsn.insn points to the copy of the instruction to be
  89. * single-stepped. x86_64, POWER4 and above have no-exec support and
  90. * stepping on the instruction on a vmalloced/kmalloced/data page
  91. * is a recipe for disaster
  92. */
  93. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  94. struct kprobe_insn_page {
  95. struct hlist_node hlist;
  96. kprobe_opcode_t *insns; /* Page of instruction slots */
  97. char slot_used[INSNS_PER_PAGE];
  98. int nused;
  99. int ngarbage;
  100. };
  101. enum kprobe_slot_state {
  102. SLOT_CLEAN = 0,
  103. SLOT_DIRTY = 1,
  104. SLOT_USED = 2,
  105. };
  106. static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_pages */
  107. static struct hlist_head kprobe_insn_pages;
  108. static int kprobe_garbage_slots;
  109. static int collect_garbage_slots(void);
  110. static int __kprobes check_safety(void)
  111. {
  112. int ret = 0;
  113. #if defined(CONFIG_PREEMPT) && defined(CONFIG_FREEZER)
  114. ret = freeze_processes();
  115. if (ret == 0) {
  116. struct task_struct *p, *q;
  117. do_each_thread(p, q) {
  118. if (p != current && p->state == TASK_RUNNING &&
  119. p->pid != 0) {
  120. printk("Check failed: %s is running\n",p->comm);
  121. ret = -1;
  122. goto loop_end;
  123. }
  124. } while_each_thread(p, q);
  125. }
  126. loop_end:
  127. thaw_processes();
  128. #else
  129. synchronize_sched();
  130. #endif
  131. return ret;
  132. }
  133. /**
  134. * __get_insn_slot() - Find a slot on an executable page for an instruction.
  135. * We allocate an executable page if there's no room on existing ones.
  136. */
  137. static kprobe_opcode_t __kprobes *__get_insn_slot(void)
  138. {
  139. struct kprobe_insn_page *kip;
  140. struct hlist_node *pos;
  141. retry:
  142. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  143. if (kip->nused < INSNS_PER_PAGE) {
  144. int i;
  145. for (i = 0; i < INSNS_PER_PAGE; i++) {
  146. if (kip->slot_used[i] == SLOT_CLEAN) {
  147. kip->slot_used[i] = SLOT_USED;
  148. kip->nused++;
  149. return kip->insns + (i * MAX_INSN_SIZE);
  150. }
  151. }
  152. /* Surprise! No unused slots. Fix kip->nused. */
  153. kip->nused = INSNS_PER_PAGE;
  154. }
  155. }
  156. /* If there are any garbage slots, collect it and try again. */
  157. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  158. goto retry;
  159. }
  160. /* All out of space. Need to allocate a new page. Use slot 0. */
  161. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  162. if (!kip)
  163. return NULL;
  164. /*
  165. * Use module_alloc so this page is within +/- 2GB of where the
  166. * kernel image and loaded module images reside. This is required
  167. * so x86_64 can correctly handle the %rip-relative fixups.
  168. */
  169. kip->insns = module_alloc(PAGE_SIZE);
  170. if (!kip->insns) {
  171. kfree(kip);
  172. return NULL;
  173. }
  174. INIT_HLIST_NODE(&kip->hlist);
  175. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  176. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  177. kip->slot_used[0] = SLOT_USED;
  178. kip->nused = 1;
  179. kip->ngarbage = 0;
  180. return kip->insns;
  181. }
  182. kprobe_opcode_t __kprobes *get_insn_slot(void)
  183. {
  184. kprobe_opcode_t *ret;
  185. mutex_lock(&kprobe_insn_mutex);
  186. ret = __get_insn_slot();
  187. mutex_unlock(&kprobe_insn_mutex);
  188. return ret;
  189. }
  190. /* Return 1 if all garbages are collected, otherwise 0. */
  191. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  192. {
  193. kip->slot_used[idx] = SLOT_CLEAN;
  194. kip->nused--;
  195. if (kip->nused == 0) {
  196. /*
  197. * Page is no longer in use. Free it unless
  198. * it's the last one. We keep the last one
  199. * so as not to have to set it up again the
  200. * next time somebody inserts a probe.
  201. */
  202. hlist_del(&kip->hlist);
  203. if (hlist_empty(&kprobe_insn_pages)) {
  204. INIT_HLIST_NODE(&kip->hlist);
  205. hlist_add_head(&kip->hlist,
  206. &kprobe_insn_pages);
  207. } else {
  208. module_free(NULL, kip->insns);
  209. kfree(kip);
  210. }
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. static int __kprobes collect_garbage_slots(void)
  216. {
  217. struct kprobe_insn_page *kip;
  218. struct hlist_node *pos, *next;
  219. int safety;
  220. /* Ensure no-one is preepmted on the garbages */
  221. mutex_unlock(&kprobe_insn_mutex);
  222. safety = check_safety();
  223. mutex_lock(&kprobe_insn_mutex);
  224. if (safety != 0)
  225. return -EAGAIN;
  226. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  227. int i;
  228. if (kip->ngarbage == 0)
  229. continue;
  230. kip->ngarbage = 0; /* we will collect all garbages */
  231. for (i = 0; i < INSNS_PER_PAGE; i++) {
  232. if (kip->slot_used[i] == SLOT_DIRTY &&
  233. collect_one_slot(kip, i))
  234. break;
  235. }
  236. }
  237. kprobe_garbage_slots = 0;
  238. return 0;
  239. }
  240. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  241. {
  242. struct kprobe_insn_page *kip;
  243. struct hlist_node *pos;
  244. mutex_lock(&kprobe_insn_mutex);
  245. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  246. if (kip->insns <= slot &&
  247. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  248. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  249. if (dirty) {
  250. kip->slot_used[i] = SLOT_DIRTY;
  251. kip->ngarbage++;
  252. } else {
  253. collect_one_slot(kip, i);
  254. }
  255. break;
  256. }
  257. }
  258. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  259. collect_garbage_slots();
  260. mutex_unlock(&kprobe_insn_mutex);
  261. }
  262. #endif
  263. /* We have preemption disabled.. so it is safe to use __ versions */
  264. static inline void set_kprobe_instance(struct kprobe *kp)
  265. {
  266. __get_cpu_var(kprobe_instance) = kp;
  267. }
  268. static inline void reset_kprobe_instance(void)
  269. {
  270. __get_cpu_var(kprobe_instance) = NULL;
  271. }
  272. /*
  273. * This routine is called either:
  274. * - under the kprobe_mutex - during kprobe_[un]register()
  275. * OR
  276. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  277. */
  278. struct kprobe __kprobes *get_kprobe(void *addr)
  279. {
  280. struct hlist_head *head;
  281. struct hlist_node *node;
  282. struct kprobe *p;
  283. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  284. hlist_for_each_entry_rcu(p, node, head, hlist) {
  285. if (p->addr == addr)
  286. return p;
  287. }
  288. return NULL;
  289. }
  290. /*
  291. * Aggregate handlers for multiple kprobes support - these handlers
  292. * take care of invoking the individual kprobe handlers on p->list
  293. */
  294. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  295. {
  296. struct kprobe *kp;
  297. list_for_each_entry_rcu(kp, &p->list, list) {
  298. if (kp->pre_handler && !kprobe_gone(kp)) {
  299. set_kprobe_instance(kp);
  300. if (kp->pre_handler(kp, regs))
  301. return 1;
  302. }
  303. reset_kprobe_instance();
  304. }
  305. return 0;
  306. }
  307. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  308. unsigned long flags)
  309. {
  310. struct kprobe *kp;
  311. list_for_each_entry_rcu(kp, &p->list, list) {
  312. if (kp->post_handler && !kprobe_gone(kp)) {
  313. set_kprobe_instance(kp);
  314. kp->post_handler(kp, regs, flags);
  315. reset_kprobe_instance();
  316. }
  317. }
  318. }
  319. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  320. int trapnr)
  321. {
  322. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  323. /*
  324. * if we faulted "during" the execution of a user specified
  325. * probe handler, invoke just that probe's fault handler
  326. */
  327. if (cur && cur->fault_handler) {
  328. if (cur->fault_handler(cur, regs, trapnr))
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  334. {
  335. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  336. int ret = 0;
  337. if (cur && cur->break_handler) {
  338. if (cur->break_handler(cur, regs))
  339. ret = 1;
  340. }
  341. reset_kprobe_instance();
  342. return ret;
  343. }
  344. /* Walks the list and increments nmissed count for multiprobe case */
  345. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  346. {
  347. struct kprobe *kp;
  348. if (p->pre_handler != aggr_pre_handler) {
  349. p->nmissed++;
  350. } else {
  351. list_for_each_entry_rcu(kp, &p->list, list)
  352. kp->nmissed++;
  353. }
  354. return;
  355. }
  356. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  357. struct hlist_head *head)
  358. {
  359. struct kretprobe *rp = ri->rp;
  360. /* remove rp inst off the rprobe_inst_table */
  361. hlist_del(&ri->hlist);
  362. INIT_HLIST_NODE(&ri->hlist);
  363. if (likely(rp)) {
  364. spin_lock(&rp->lock);
  365. hlist_add_head(&ri->hlist, &rp->free_instances);
  366. spin_unlock(&rp->lock);
  367. } else
  368. /* Unregistering */
  369. hlist_add_head(&ri->hlist, head);
  370. }
  371. void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
  372. struct hlist_head **head, unsigned long *flags)
  373. {
  374. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  375. spinlock_t *hlist_lock;
  376. *head = &kretprobe_inst_table[hash];
  377. hlist_lock = kretprobe_table_lock_ptr(hash);
  378. spin_lock_irqsave(hlist_lock, *flags);
  379. }
  380. static void __kprobes kretprobe_table_lock(unsigned long hash,
  381. unsigned long *flags)
  382. {
  383. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  384. spin_lock_irqsave(hlist_lock, *flags);
  385. }
  386. void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
  387. unsigned long *flags)
  388. {
  389. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  390. spinlock_t *hlist_lock;
  391. hlist_lock = kretprobe_table_lock_ptr(hash);
  392. spin_unlock_irqrestore(hlist_lock, *flags);
  393. }
  394. void __kprobes kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
  395. {
  396. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  397. spin_unlock_irqrestore(hlist_lock, *flags);
  398. }
  399. /*
  400. * This function is called from finish_task_switch when task tk becomes dead,
  401. * so that we can recycle any function-return probe instances associated
  402. * with this task. These left over instances represent probed functions
  403. * that have been called but will never return.
  404. */
  405. void __kprobes kprobe_flush_task(struct task_struct *tk)
  406. {
  407. struct kretprobe_instance *ri;
  408. struct hlist_head *head, empty_rp;
  409. struct hlist_node *node, *tmp;
  410. unsigned long hash, flags = 0;
  411. if (unlikely(!kprobes_initialized))
  412. /* Early boot. kretprobe_table_locks not yet initialized. */
  413. return;
  414. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  415. head = &kretprobe_inst_table[hash];
  416. kretprobe_table_lock(hash, &flags);
  417. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  418. if (ri->task == tk)
  419. recycle_rp_inst(ri, &empty_rp);
  420. }
  421. kretprobe_table_unlock(hash, &flags);
  422. INIT_HLIST_HEAD(&empty_rp);
  423. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  424. hlist_del(&ri->hlist);
  425. kfree(ri);
  426. }
  427. }
  428. static inline void free_rp_inst(struct kretprobe *rp)
  429. {
  430. struct kretprobe_instance *ri;
  431. struct hlist_node *pos, *next;
  432. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
  433. hlist_del(&ri->hlist);
  434. kfree(ri);
  435. }
  436. }
  437. static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
  438. {
  439. unsigned long flags, hash;
  440. struct kretprobe_instance *ri;
  441. struct hlist_node *pos, *next;
  442. struct hlist_head *head;
  443. /* No race here */
  444. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  445. kretprobe_table_lock(hash, &flags);
  446. head = &kretprobe_inst_table[hash];
  447. hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
  448. if (ri->rp == rp)
  449. ri->rp = NULL;
  450. }
  451. kretprobe_table_unlock(hash, &flags);
  452. }
  453. free_rp_inst(rp);
  454. }
  455. /*
  456. * Keep all fields in the kprobe consistent
  457. */
  458. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  459. {
  460. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  461. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  462. }
  463. /*
  464. * Add the new probe to old_p->list. Fail if this is the
  465. * second jprobe at the address - two jprobes can't coexist
  466. */
  467. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  468. {
  469. if (p->break_handler) {
  470. if (old_p->break_handler)
  471. return -EEXIST;
  472. list_add_tail_rcu(&p->list, &old_p->list);
  473. old_p->break_handler = aggr_break_handler;
  474. } else
  475. list_add_rcu(&p->list, &old_p->list);
  476. if (p->post_handler && !old_p->post_handler)
  477. old_p->post_handler = aggr_post_handler;
  478. return 0;
  479. }
  480. /*
  481. * Fill in the required fields of the "manager kprobe". Replace the
  482. * earlier kprobe in the hlist with the manager kprobe
  483. */
  484. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  485. {
  486. copy_kprobe(p, ap);
  487. flush_insn_slot(ap);
  488. ap->addr = p->addr;
  489. ap->pre_handler = aggr_pre_handler;
  490. ap->fault_handler = aggr_fault_handler;
  491. /* We don't care the kprobe which has gone. */
  492. if (p->post_handler && !kprobe_gone(p))
  493. ap->post_handler = aggr_post_handler;
  494. if (p->break_handler && !kprobe_gone(p))
  495. ap->break_handler = aggr_break_handler;
  496. INIT_LIST_HEAD(&ap->list);
  497. list_add_rcu(&p->list, &ap->list);
  498. hlist_replace_rcu(&p->hlist, &ap->hlist);
  499. }
  500. /*
  501. * This is the second or subsequent kprobe at the address - handle
  502. * the intricacies
  503. */
  504. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  505. struct kprobe *p)
  506. {
  507. int ret = 0;
  508. struct kprobe *ap;
  509. if (kprobe_gone(old_p)) {
  510. /*
  511. * Attempting to insert new probe at the same location that
  512. * had a probe in the module vaddr area which already
  513. * freed. So, the instruction slot has already been
  514. * released. We need a new slot for the new probe.
  515. */
  516. ret = arch_prepare_kprobe(old_p);
  517. if (ret)
  518. return ret;
  519. }
  520. if (old_p->pre_handler == aggr_pre_handler) {
  521. copy_kprobe(old_p, p);
  522. ret = add_new_kprobe(old_p, p);
  523. ap = old_p;
  524. } else {
  525. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  526. if (!ap) {
  527. if (kprobe_gone(old_p))
  528. arch_remove_kprobe(old_p);
  529. return -ENOMEM;
  530. }
  531. add_aggr_kprobe(ap, old_p);
  532. copy_kprobe(ap, p);
  533. ret = add_new_kprobe(ap, p);
  534. }
  535. if (kprobe_gone(old_p)) {
  536. /*
  537. * If the old_p has gone, its breakpoint has been disarmed.
  538. * We have to arm it again after preparing real kprobes.
  539. */
  540. ap->flags &= ~KPROBE_FLAG_GONE;
  541. if (kprobe_enabled)
  542. arch_arm_kprobe(ap);
  543. }
  544. return ret;
  545. }
  546. static int __kprobes in_kprobes_functions(unsigned long addr)
  547. {
  548. struct kprobe_blackpoint *kb;
  549. if (addr >= (unsigned long)__kprobes_text_start &&
  550. addr < (unsigned long)__kprobes_text_end)
  551. return -EINVAL;
  552. /*
  553. * If there exists a kprobe_blacklist, verify and
  554. * fail any probe registration in the prohibited area
  555. */
  556. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  557. if (kb->start_addr) {
  558. if (addr >= kb->start_addr &&
  559. addr < (kb->start_addr + kb->range))
  560. return -EINVAL;
  561. }
  562. }
  563. return 0;
  564. }
  565. /*
  566. * If we have a symbol_name argument, look it up and add the offset field
  567. * to it. This way, we can specify a relative address to a symbol.
  568. */
  569. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  570. {
  571. kprobe_opcode_t *addr = p->addr;
  572. if (p->symbol_name) {
  573. if (addr)
  574. return NULL;
  575. kprobe_lookup_name(p->symbol_name, addr);
  576. }
  577. if (!addr)
  578. return NULL;
  579. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  580. }
  581. int __kprobes register_kprobe(struct kprobe *p)
  582. {
  583. int ret = 0;
  584. struct kprobe *old_p;
  585. struct module *probed_mod;
  586. kprobe_opcode_t *addr;
  587. addr = kprobe_addr(p);
  588. if (!addr)
  589. return -EINVAL;
  590. p->addr = addr;
  591. preempt_disable();
  592. if (!__kernel_text_address((unsigned long) p->addr) ||
  593. in_kprobes_functions((unsigned long) p->addr)) {
  594. preempt_enable();
  595. return -EINVAL;
  596. }
  597. p->flags = 0;
  598. /*
  599. * Check if are we probing a module.
  600. */
  601. probed_mod = __module_text_address((unsigned long) p->addr);
  602. if (probed_mod) {
  603. /*
  604. * We must hold a refcount of the probed module while updating
  605. * its code to prohibit unexpected unloading.
  606. */
  607. if (unlikely(!try_module_get(probed_mod))) {
  608. preempt_enable();
  609. return -EINVAL;
  610. }
  611. /*
  612. * If the module freed .init.text, we couldn't insert
  613. * kprobes in there.
  614. */
  615. if (within_module_init((unsigned long)p->addr, probed_mod) &&
  616. probed_mod->state != MODULE_STATE_COMING) {
  617. module_put(probed_mod);
  618. preempt_enable();
  619. return -EINVAL;
  620. }
  621. }
  622. preempt_enable();
  623. p->nmissed = 0;
  624. INIT_LIST_HEAD(&p->list);
  625. mutex_lock(&kprobe_mutex);
  626. old_p = get_kprobe(p->addr);
  627. if (old_p) {
  628. ret = register_aggr_kprobe(old_p, p);
  629. goto out;
  630. }
  631. mutex_lock(&text_mutex);
  632. ret = arch_prepare_kprobe(p);
  633. if (ret)
  634. goto out_unlock_text;
  635. INIT_HLIST_NODE(&p->hlist);
  636. hlist_add_head_rcu(&p->hlist,
  637. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  638. if (kprobe_enabled)
  639. arch_arm_kprobe(p);
  640. out_unlock_text:
  641. mutex_unlock(&text_mutex);
  642. out:
  643. mutex_unlock(&kprobe_mutex);
  644. if (probed_mod)
  645. module_put(probed_mod);
  646. return ret;
  647. }
  648. /*
  649. * Unregister a kprobe without a scheduler synchronization.
  650. */
  651. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  652. {
  653. struct kprobe *old_p, *list_p;
  654. old_p = get_kprobe(p->addr);
  655. if (unlikely(!old_p))
  656. return -EINVAL;
  657. if (p != old_p) {
  658. list_for_each_entry_rcu(list_p, &old_p->list, list)
  659. if (list_p == p)
  660. /* kprobe p is a valid probe */
  661. goto valid_p;
  662. return -EINVAL;
  663. }
  664. valid_p:
  665. if (old_p == p ||
  666. (old_p->pre_handler == aggr_pre_handler &&
  667. list_is_singular(&old_p->list))) {
  668. /*
  669. * Only probe on the hash list. Disarm only if kprobes are
  670. * enabled and not gone - otherwise, the breakpoint would
  671. * already have been removed. We save on flushing icache.
  672. */
  673. if (kprobe_enabled && !kprobe_gone(old_p)) {
  674. mutex_lock(&text_mutex);
  675. arch_disarm_kprobe(p);
  676. mutex_unlock(&text_mutex);
  677. }
  678. hlist_del_rcu(&old_p->hlist);
  679. } else {
  680. if (p->break_handler && !kprobe_gone(p))
  681. old_p->break_handler = NULL;
  682. if (p->post_handler && !kprobe_gone(p)) {
  683. list_for_each_entry_rcu(list_p, &old_p->list, list) {
  684. if ((list_p != p) && (list_p->post_handler))
  685. goto noclean;
  686. }
  687. old_p->post_handler = NULL;
  688. }
  689. noclean:
  690. list_del_rcu(&p->list);
  691. }
  692. return 0;
  693. }
  694. static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
  695. {
  696. struct kprobe *old_p;
  697. if (list_empty(&p->list))
  698. arch_remove_kprobe(p);
  699. else if (list_is_singular(&p->list)) {
  700. /* "p" is the last child of an aggr_kprobe */
  701. old_p = list_entry(p->list.next, struct kprobe, list);
  702. list_del(&p->list);
  703. arch_remove_kprobe(old_p);
  704. kfree(old_p);
  705. }
  706. }
  707. int __kprobes register_kprobes(struct kprobe **kps, int num)
  708. {
  709. int i, ret = 0;
  710. if (num <= 0)
  711. return -EINVAL;
  712. for (i = 0; i < num; i++) {
  713. ret = register_kprobe(kps[i]);
  714. if (ret < 0) {
  715. if (i > 0)
  716. unregister_kprobes(kps, i);
  717. break;
  718. }
  719. }
  720. return ret;
  721. }
  722. void __kprobes unregister_kprobe(struct kprobe *p)
  723. {
  724. unregister_kprobes(&p, 1);
  725. }
  726. void __kprobes unregister_kprobes(struct kprobe **kps, int num)
  727. {
  728. int i;
  729. if (num <= 0)
  730. return;
  731. mutex_lock(&kprobe_mutex);
  732. for (i = 0; i < num; i++)
  733. if (__unregister_kprobe_top(kps[i]) < 0)
  734. kps[i]->addr = NULL;
  735. mutex_unlock(&kprobe_mutex);
  736. synchronize_sched();
  737. for (i = 0; i < num; i++)
  738. if (kps[i]->addr)
  739. __unregister_kprobe_bottom(kps[i]);
  740. }
  741. static struct notifier_block kprobe_exceptions_nb = {
  742. .notifier_call = kprobe_exceptions_notify,
  743. .priority = 0x7fffffff /* we need to be notified first */
  744. };
  745. unsigned long __weak arch_deref_entry_point(void *entry)
  746. {
  747. return (unsigned long)entry;
  748. }
  749. int __kprobes register_jprobes(struct jprobe **jps, int num)
  750. {
  751. struct jprobe *jp;
  752. int ret = 0, i;
  753. if (num <= 0)
  754. return -EINVAL;
  755. for (i = 0; i < num; i++) {
  756. unsigned long addr;
  757. jp = jps[i];
  758. addr = arch_deref_entry_point(jp->entry);
  759. if (!kernel_text_address(addr))
  760. ret = -EINVAL;
  761. else {
  762. /* Todo: Verify probepoint is a function entry point */
  763. jp->kp.pre_handler = setjmp_pre_handler;
  764. jp->kp.break_handler = longjmp_break_handler;
  765. ret = register_kprobe(&jp->kp);
  766. }
  767. if (ret < 0) {
  768. if (i > 0)
  769. unregister_jprobes(jps, i);
  770. break;
  771. }
  772. }
  773. return ret;
  774. }
  775. int __kprobes register_jprobe(struct jprobe *jp)
  776. {
  777. return register_jprobes(&jp, 1);
  778. }
  779. void __kprobes unregister_jprobe(struct jprobe *jp)
  780. {
  781. unregister_jprobes(&jp, 1);
  782. }
  783. void __kprobes unregister_jprobes(struct jprobe **jps, int num)
  784. {
  785. int i;
  786. if (num <= 0)
  787. return;
  788. mutex_lock(&kprobe_mutex);
  789. for (i = 0; i < num; i++)
  790. if (__unregister_kprobe_top(&jps[i]->kp) < 0)
  791. jps[i]->kp.addr = NULL;
  792. mutex_unlock(&kprobe_mutex);
  793. synchronize_sched();
  794. for (i = 0; i < num; i++) {
  795. if (jps[i]->kp.addr)
  796. __unregister_kprobe_bottom(&jps[i]->kp);
  797. }
  798. }
  799. #ifdef CONFIG_KRETPROBES
  800. /*
  801. * This kprobe pre_handler is registered with every kretprobe. When probe
  802. * hits it will set up the return probe.
  803. */
  804. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  805. struct pt_regs *regs)
  806. {
  807. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  808. unsigned long hash, flags = 0;
  809. struct kretprobe_instance *ri;
  810. /*TODO: consider to only swap the RA after the last pre_handler fired */
  811. hash = hash_ptr(current, KPROBE_HASH_BITS);
  812. spin_lock_irqsave(&rp->lock, flags);
  813. if (!hlist_empty(&rp->free_instances)) {
  814. ri = hlist_entry(rp->free_instances.first,
  815. struct kretprobe_instance, hlist);
  816. hlist_del(&ri->hlist);
  817. spin_unlock_irqrestore(&rp->lock, flags);
  818. ri->rp = rp;
  819. ri->task = current;
  820. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  821. spin_unlock_irqrestore(&rp->lock, flags);
  822. return 0;
  823. }
  824. arch_prepare_kretprobe(ri, regs);
  825. /* XXX(hch): why is there no hlist_move_head? */
  826. INIT_HLIST_NODE(&ri->hlist);
  827. kretprobe_table_lock(hash, &flags);
  828. hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
  829. kretprobe_table_unlock(hash, &flags);
  830. } else {
  831. rp->nmissed++;
  832. spin_unlock_irqrestore(&rp->lock, flags);
  833. }
  834. return 0;
  835. }
  836. int __kprobes register_kretprobe(struct kretprobe *rp)
  837. {
  838. int ret = 0;
  839. struct kretprobe_instance *inst;
  840. int i;
  841. void *addr;
  842. if (kretprobe_blacklist_size) {
  843. addr = kprobe_addr(&rp->kp);
  844. if (!addr)
  845. return -EINVAL;
  846. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  847. if (kretprobe_blacklist[i].addr == addr)
  848. return -EINVAL;
  849. }
  850. }
  851. rp->kp.pre_handler = pre_handler_kretprobe;
  852. rp->kp.post_handler = NULL;
  853. rp->kp.fault_handler = NULL;
  854. rp->kp.break_handler = NULL;
  855. /* Pre-allocate memory for max kretprobe instances */
  856. if (rp->maxactive <= 0) {
  857. #ifdef CONFIG_PREEMPT
  858. rp->maxactive = max(10, 2 * NR_CPUS);
  859. #else
  860. rp->maxactive = NR_CPUS;
  861. #endif
  862. }
  863. spin_lock_init(&rp->lock);
  864. INIT_HLIST_HEAD(&rp->free_instances);
  865. for (i = 0; i < rp->maxactive; i++) {
  866. inst = kmalloc(sizeof(struct kretprobe_instance) +
  867. rp->data_size, GFP_KERNEL);
  868. if (inst == NULL) {
  869. free_rp_inst(rp);
  870. return -ENOMEM;
  871. }
  872. INIT_HLIST_NODE(&inst->hlist);
  873. hlist_add_head(&inst->hlist, &rp->free_instances);
  874. }
  875. rp->nmissed = 0;
  876. /* Establish function entry probe point */
  877. ret = register_kprobe(&rp->kp);
  878. if (ret != 0)
  879. free_rp_inst(rp);
  880. return ret;
  881. }
  882. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  883. {
  884. int ret = 0, i;
  885. if (num <= 0)
  886. return -EINVAL;
  887. for (i = 0; i < num; i++) {
  888. ret = register_kretprobe(rps[i]);
  889. if (ret < 0) {
  890. if (i > 0)
  891. unregister_kretprobes(rps, i);
  892. break;
  893. }
  894. }
  895. return ret;
  896. }
  897. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  898. {
  899. unregister_kretprobes(&rp, 1);
  900. }
  901. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  902. {
  903. int i;
  904. if (num <= 0)
  905. return;
  906. mutex_lock(&kprobe_mutex);
  907. for (i = 0; i < num; i++)
  908. if (__unregister_kprobe_top(&rps[i]->kp) < 0)
  909. rps[i]->kp.addr = NULL;
  910. mutex_unlock(&kprobe_mutex);
  911. synchronize_sched();
  912. for (i = 0; i < num; i++) {
  913. if (rps[i]->kp.addr) {
  914. __unregister_kprobe_bottom(&rps[i]->kp);
  915. cleanup_rp_inst(rps[i]);
  916. }
  917. }
  918. }
  919. #else /* CONFIG_KRETPROBES */
  920. int __kprobes register_kretprobe(struct kretprobe *rp)
  921. {
  922. return -ENOSYS;
  923. }
  924. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  925. {
  926. return -ENOSYS;
  927. }
  928. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  929. {
  930. }
  931. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  932. {
  933. }
  934. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  935. struct pt_regs *regs)
  936. {
  937. return 0;
  938. }
  939. #endif /* CONFIG_KRETPROBES */
  940. /* Set the kprobe gone and remove its instruction buffer. */
  941. static void __kprobes kill_kprobe(struct kprobe *p)
  942. {
  943. struct kprobe *kp;
  944. p->flags |= KPROBE_FLAG_GONE;
  945. if (p->pre_handler == aggr_pre_handler) {
  946. /*
  947. * If this is an aggr_kprobe, we have to list all the
  948. * chained probes and mark them GONE.
  949. */
  950. list_for_each_entry_rcu(kp, &p->list, list)
  951. kp->flags |= KPROBE_FLAG_GONE;
  952. p->post_handler = NULL;
  953. p->break_handler = NULL;
  954. }
  955. /*
  956. * Here, we can remove insn_slot safely, because no thread calls
  957. * the original probed function (which will be freed soon) any more.
  958. */
  959. arch_remove_kprobe(p);
  960. }
  961. /* Module notifier call back, checking kprobes on the module */
  962. static int __kprobes kprobes_module_callback(struct notifier_block *nb,
  963. unsigned long val, void *data)
  964. {
  965. struct module *mod = data;
  966. struct hlist_head *head;
  967. struct hlist_node *node;
  968. struct kprobe *p;
  969. unsigned int i;
  970. int checkcore = (val == MODULE_STATE_GOING);
  971. if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
  972. return NOTIFY_DONE;
  973. /*
  974. * When MODULE_STATE_GOING was notified, both of module .text and
  975. * .init.text sections would be freed. When MODULE_STATE_LIVE was
  976. * notified, only .init.text section would be freed. We need to
  977. * disable kprobes which have been inserted in the sections.
  978. */
  979. mutex_lock(&kprobe_mutex);
  980. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  981. head = &kprobe_table[i];
  982. hlist_for_each_entry_rcu(p, node, head, hlist)
  983. if (within_module_init((unsigned long)p->addr, mod) ||
  984. (checkcore &&
  985. within_module_core((unsigned long)p->addr, mod))) {
  986. /*
  987. * The vaddr this probe is installed will soon
  988. * be vfreed buy not synced to disk. Hence,
  989. * disarming the breakpoint isn't needed.
  990. */
  991. kill_kprobe(p);
  992. }
  993. }
  994. mutex_unlock(&kprobe_mutex);
  995. return NOTIFY_DONE;
  996. }
  997. static struct notifier_block kprobe_module_nb = {
  998. .notifier_call = kprobes_module_callback,
  999. .priority = 0
  1000. };
  1001. static int __init init_kprobes(void)
  1002. {
  1003. int i, err = 0;
  1004. unsigned long offset = 0, size = 0;
  1005. char *modname, namebuf[128];
  1006. const char *symbol_name;
  1007. void *addr;
  1008. struct kprobe_blackpoint *kb;
  1009. /* FIXME allocate the probe table, currently defined statically */
  1010. /* initialize all list heads */
  1011. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1012. INIT_HLIST_HEAD(&kprobe_table[i]);
  1013. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  1014. spin_lock_init(&(kretprobe_table_locks[i].lock));
  1015. }
  1016. /*
  1017. * Lookup and populate the kprobe_blacklist.
  1018. *
  1019. * Unlike the kretprobe blacklist, we'll need to determine
  1020. * the range of addresses that belong to the said functions,
  1021. * since a kprobe need not necessarily be at the beginning
  1022. * of a function.
  1023. */
  1024. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  1025. kprobe_lookup_name(kb->name, addr);
  1026. if (!addr)
  1027. continue;
  1028. kb->start_addr = (unsigned long)addr;
  1029. symbol_name = kallsyms_lookup(kb->start_addr,
  1030. &size, &offset, &modname, namebuf);
  1031. if (!symbol_name)
  1032. kb->range = 0;
  1033. else
  1034. kb->range = size;
  1035. }
  1036. if (kretprobe_blacklist_size) {
  1037. /* lookup the function address from its name */
  1038. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1039. kprobe_lookup_name(kretprobe_blacklist[i].name,
  1040. kretprobe_blacklist[i].addr);
  1041. if (!kretprobe_blacklist[i].addr)
  1042. printk("kretprobe: lookup failed: %s\n",
  1043. kretprobe_blacklist[i].name);
  1044. }
  1045. }
  1046. /* By default, kprobes are enabled */
  1047. kprobe_enabled = true;
  1048. err = arch_init_kprobes();
  1049. if (!err)
  1050. err = register_die_notifier(&kprobe_exceptions_nb);
  1051. if (!err)
  1052. err = register_module_notifier(&kprobe_module_nb);
  1053. kprobes_initialized = (err == 0);
  1054. if (!err)
  1055. init_test_probes();
  1056. return err;
  1057. }
  1058. #ifdef CONFIG_DEBUG_FS
  1059. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  1060. const char *sym, int offset,char *modname)
  1061. {
  1062. char *kprobe_type;
  1063. if (p->pre_handler == pre_handler_kretprobe)
  1064. kprobe_type = "r";
  1065. else if (p->pre_handler == setjmp_pre_handler)
  1066. kprobe_type = "j";
  1067. else
  1068. kprobe_type = "k";
  1069. if (sym)
  1070. seq_printf(pi, "%p %s %s+0x%x %s %s\n", p->addr, kprobe_type,
  1071. sym, offset, (modname ? modname : " "),
  1072. (kprobe_gone(p) ? "[GONE]" : ""));
  1073. else
  1074. seq_printf(pi, "%p %s %p %s\n", p->addr, kprobe_type, p->addr,
  1075. (kprobe_gone(p) ? "[GONE]" : ""));
  1076. }
  1077. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  1078. {
  1079. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  1080. }
  1081. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  1082. {
  1083. (*pos)++;
  1084. if (*pos >= KPROBE_TABLE_SIZE)
  1085. return NULL;
  1086. return pos;
  1087. }
  1088. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  1089. {
  1090. /* Nothing to do */
  1091. }
  1092. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  1093. {
  1094. struct hlist_head *head;
  1095. struct hlist_node *node;
  1096. struct kprobe *p, *kp;
  1097. const char *sym = NULL;
  1098. unsigned int i = *(loff_t *) v;
  1099. unsigned long offset = 0;
  1100. char *modname, namebuf[128];
  1101. head = &kprobe_table[i];
  1102. preempt_disable();
  1103. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1104. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  1105. &offset, &modname, namebuf);
  1106. if (p->pre_handler == aggr_pre_handler) {
  1107. list_for_each_entry_rcu(kp, &p->list, list)
  1108. report_probe(pi, kp, sym, offset, modname);
  1109. } else
  1110. report_probe(pi, p, sym, offset, modname);
  1111. }
  1112. preempt_enable();
  1113. return 0;
  1114. }
  1115. static struct seq_operations kprobes_seq_ops = {
  1116. .start = kprobe_seq_start,
  1117. .next = kprobe_seq_next,
  1118. .stop = kprobe_seq_stop,
  1119. .show = show_kprobe_addr
  1120. };
  1121. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  1122. {
  1123. return seq_open(filp, &kprobes_seq_ops);
  1124. }
  1125. static struct file_operations debugfs_kprobes_operations = {
  1126. .open = kprobes_open,
  1127. .read = seq_read,
  1128. .llseek = seq_lseek,
  1129. .release = seq_release,
  1130. };
  1131. static void __kprobes enable_all_kprobes(void)
  1132. {
  1133. struct hlist_head *head;
  1134. struct hlist_node *node;
  1135. struct kprobe *p;
  1136. unsigned int i;
  1137. mutex_lock(&kprobe_mutex);
  1138. /* If kprobes are already enabled, just return */
  1139. if (kprobe_enabled)
  1140. goto already_enabled;
  1141. mutex_lock(&text_mutex);
  1142. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1143. head = &kprobe_table[i];
  1144. hlist_for_each_entry_rcu(p, node, head, hlist)
  1145. if (!kprobe_gone(p))
  1146. arch_arm_kprobe(p);
  1147. }
  1148. mutex_unlock(&text_mutex);
  1149. kprobe_enabled = true;
  1150. printk(KERN_INFO "Kprobes globally enabled\n");
  1151. already_enabled:
  1152. mutex_unlock(&kprobe_mutex);
  1153. return;
  1154. }
  1155. static void __kprobes disable_all_kprobes(void)
  1156. {
  1157. struct hlist_head *head;
  1158. struct hlist_node *node;
  1159. struct kprobe *p;
  1160. unsigned int i;
  1161. mutex_lock(&kprobe_mutex);
  1162. /* If kprobes are already disabled, just return */
  1163. if (!kprobe_enabled)
  1164. goto already_disabled;
  1165. kprobe_enabled = false;
  1166. printk(KERN_INFO "Kprobes globally disabled\n");
  1167. mutex_lock(&text_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) {
  1171. if (!arch_trampoline_kprobe(p) && !kprobe_gone(p))
  1172. arch_disarm_kprobe(p);
  1173. }
  1174. }
  1175. mutex_unlock(&text_mutex);
  1176. mutex_unlock(&kprobe_mutex);
  1177. /* Allow all currently running kprobes to complete */
  1178. synchronize_sched();
  1179. return;
  1180. already_disabled:
  1181. mutex_unlock(&kprobe_mutex);
  1182. return;
  1183. }
  1184. /*
  1185. * XXX: The debugfs bool file interface doesn't allow for callbacks
  1186. * when the bool state is switched. We can reuse that facility when
  1187. * available
  1188. */
  1189. static ssize_t read_enabled_file_bool(struct file *file,
  1190. char __user *user_buf, size_t count, loff_t *ppos)
  1191. {
  1192. char buf[3];
  1193. if (kprobe_enabled)
  1194. buf[0] = '1';
  1195. else
  1196. buf[0] = '0';
  1197. buf[1] = '\n';
  1198. buf[2] = 0x00;
  1199. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  1200. }
  1201. static ssize_t write_enabled_file_bool(struct file *file,
  1202. const char __user *user_buf, size_t count, loff_t *ppos)
  1203. {
  1204. char buf[32];
  1205. int buf_size;
  1206. buf_size = min(count, (sizeof(buf)-1));
  1207. if (copy_from_user(buf, user_buf, buf_size))
  1208. return -EFAULT;
  1209. switch (buf[0]) {
  1210. case 'y':
  1211. case 'Y':
  1212. case '1':
  1213. enable_all_kprobes();
  1214. break;
  1215. case 'n':
  1216. case 'N':
  1217. case '0':
  1218. disable_all_kprobes();
  1219. break;
  1220. }
  1221. return count;
  1222. }
  1223. static struct file_operations fops_kp = {
  1224. .read = read_enabled_file_bool,
  1225. .write = write_enabled_file_bool,
  1226. };
  1227. static int __kprobes debugfs_kprobe_init(void)
  1228. {
  1229. struct dentry *dir, *file;
  1230. unsigned int value = 1;
  1231. dir = debugfs_create_dir("kprobes", NULL);
  1232. if (!dir)
  1233. return -ENOMEM;
  1234. file = debugfs_create_file("list", 0444, dir, NULL,
  1235. &debugfs_kprobes_operations);
  1236. if (!file) {
  1237. debugfs_remove(dir);
  1238. return -ENOMEM;
  1239. }
  1240. file = debugfs_create_file("enabled", 0600, dir,
  1241. &value, &fops_kp);
  1242. if (!file) {
  1243. debugfs_remove(dir);
  1244. return -ENOMEM;
  1245. }
  1246. return 0;
  1247. }
  1248. late_initcall(debugfs_kprobe_init);
  1249. #endif /* CONFIG_DEBUG_FS */
  1250. module_init(init_kprobes);
  1251. EXPORT_SYMBOL_GPL(register_kprobe);
  1252. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1253. EXPORT_SYMBOL_GPL(register_kprobes);
  1254. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1255. EXPORT_SYMBOL_GPL(register_jprobe);
  1256. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1257. EXPORT_SYMBOL_GPL(register_jprobes);
  1258. EXPORT_SYMBOL_GPL(unregister_jprobes);
  1259. EXPORT_SYMBOL_GPL(jprobe_return);
  1260. EXPORT_SYMBOL_GPL(register_kretprobe);
  1261. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1262. EXPORT_SYMBOL_GPL(register_kretprobes);
  1263. EXPORT_SYMBOL_GPL(unregister_kretprobes);