kprobes.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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/sysctl.h>
  46. #include <linux/kdebug.h>
  47. #include <linux/memory.h>
  48. #include <linux/ftrace.h>
  49. #include <linux/cpu.h>
  50. #include <asm-generic/sections.h>
  51. #include <asm/cacheflush.h>
  52. #include <asm/errno.h>
  53. #include <asm/uaccess.h>
  54. #define KPROBE_HASH_BITS 6
  55. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  56. /*
  57. * Some oddball architectures like 64bit powerpc have function descriptors
  58. * so this must be overridable.
  59. */
  60. #ifndef kprobe_lookup_name
  61. #define kprobe_lookup_name(name, addr) \
  62. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  63. #endif
  64. static int kprobes_initialized;
  65. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  66. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  67. /* NOTE: change this value only with kprobe_mutex held */
  68. static bool kprobes_all_disarmed;
  69. static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  70. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  71. static struct {
  72. spinlock_t lock ____cacheline_aligned_in_smp;
  73. } kretprobe_table_locks[KPROBE_TABLE_SIZE];
  74. static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
  75. {
  76. return &(kretprobe_table_locks[hash].lock);
  77. }
  78. /*
  79. * Normally, functions that we'd want to prohibit kprobes in, are marked
  80. * __kprobes. But, there are cases where such functions already belong to
  81. * a different section (__sched for preempt_schedule)
  82. *
  83. * For such cases, we now have a blacklist
  84. */
  85. static struct kprobe_blackpoint kprobe_blacklist[] = {
  86. {"preempt_schedule",},
  87. {"native_get_debugreg",},
  88. {"irq_entries_start",},
  89. {"common_interrupt",},
  90. {"mcount",}, /* mcount can be called from everywhere */
  91. {NULL} /* Terminator */
  92. };
  93. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  94. /*
  95. * kprobe->ainsn.insn points to the copy of the instruction to be
  96. * single-stepped. x86_64, POWER4 and above have no-exec support and
  97. * stepping on the instruction on a vmalloced/kmalloced/data page
  98. * is a recipe for disaster
  99. */
  100. struct kprobe_insn_page {
  101. struct list_head list;
  102. kprobe_opcode_t *insns; /* Page of instruction slots */
  103. int nused;
  104. int ngarbage;
  105. char slot_used[];
  106. };
  107. #define KPROBE_INSN_PAGE_SIZE(slots) \
  108. (offsetof(struct kprobe_insn_page, slot_used) + \
  109. (sizeof(char) * (slots)))
  110. struct kprobe_insn_cache {
  111. struct list_head pages; /* list of kprobe_insn_page */
  112. size_t insn_size; /* size of instruction slot */
  113. int nr_garbage;
  114. };
  115. static int slots_per_page(struct kprobe_insn_cache *c)
  116. {
  117. return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
  118. }
  119. enum kprobe_slot_state {
  120. SLOT_CLEAN = 0,
  121. SLOT_DIRTY = 1,
  122. SLOT_USED = 2,
  123. };
  124. static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_slots */
  125. static struct kprobe_insn_cache kprobe_insn_slots = {
  126. .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
  127. .insn_size = MAX_INSN_SIZE,
  128. .nr_garbage = 0,
  129. };
  130. static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c);
  131. /**
  132. * __get_insn_slot() - Find a slot on an executable page for an instruction.
  133. * We allocate an executable page if there's no room on existing ones.
  134. */
  135. static kprobe_opcode_t __kprobes *__get_insn_slot(struct kprobe_insn_cache *c)
  136. {
  137. struct kprobe_insn_page *kip;
  138. retry:
  139. list_for_each_entry(kip, &c->pages, list) {
  140. if (kip->nused < slots_per_page(c)) {
  141. int i;
  142. for (i = 0; i < slots_per_page(c); i++) {
  143. if (kip->slot_used[i] == SLOT_CLEAN) {
  144. kip->slot_used[i] = SLOT_USED;
  145. kip->nused++;
  146. return kip->insns + (i * c->insn_size);
  147. }
  148. }
  149. /* kip->nused is broken. Fix it. */
  150. kip->nused = slots_per_page(c);
  151. WARN_ON(1);
  152. }
  153. }
  154. /* If there are any garbage slots, collect it and try again. */
  155. if (c->nr_garbage && collect_garbage_slots(c) == 0)
  156. goto retry;
  157. /* All out of space. Need to allocate a new page. */
  158. kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
  159. if (!kip)
  160. return NULL;
  161. /*
  162. * Use module_alloc so this page is within +/- 2GB of where the
  163. * kernel image and loaded module images reside. This is required
  164. * so x86_64 can correctly handle the %rip-relative fixups.
  165. */
  166. kip->insns = module_alloc(PAGE_SIZE);
  167. if (!kip->insns) {
  168. kfree(kip);
  169. return NULL;
  170. }
  171. INIT_LIST_HEAD(&kip->list);
  172. memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
  173. kip->slot_used[0] = SLOT_USED;
  174. kip->nused = 1;
  175. kip->ngarbage = 0;
  176. list_add(&kip->list, &c->pages);
  177. return kip->insns;
  178. }
  179. kprobe_opcode_t __kprobes *get_insn_slot(void)
  180. {
  181. kprobe_opcode_t *ret = NULL;
  182. mutex_lock(&kprobe_insn_mutex);
  183. ret = __get_insn_slot(&kprobe_insn_slots);
  184. mutex_unlock(&kprobe_insn_mutex);
  185. return ret;
  186. }
  187. /* Return 1 if all garbages are collected, otherwise 0. */
  188. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  189. {
  190. kip->slot_used[idx] = SLOT_CLEAN;
  191. kip->nused--;
  192. if (kip->nused == 0) {
  193. /*
  194. * Page is no longer in use. Free it unless
  195. * it's the last one. We keep the last one
  196. * so as not to have to set it up again the
  197. * next time somebody inserts a probe.
  198. */
  199. if (!list_is_singular(&kip->list)) {
  200. list_del(&kip->list);
  201. module_free(NULL, kip->insns);
  202. kfree(kip);
  203. }
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c)
  209. {
  210. struct kprobe_insn_page *kip, *next;
  211. /* Ensure no-one is interrupted on the garbages */
  212. synchronize_sched();
  213. list_for_each_entry_safe(kip, next, &c->pages, list) {
  214. int i;
  215. if (kip->ngarbage == 0)
  216. continue;
  217. kip->ngarbage = 0; /* we will collect all garbages */
  218. for (i = 0; i < slots_per_page(c); i++) {
  219. if (kip->slot_used[i] == SLOT_DIRTY &&
  220. collect_one_slot(kip, i))
  221. break;
  222. }
  223. }
  224. c->nr_garbage = 0;
  225. return 0;
  226. }
  227. static void __kprobes __free_insn_slot(struct kprobe_insn_cache *c,
  228. kprobe_opcode_t *slot, int dirty)
  229. {
  230. struct kprobe_insn_page *kip;
  231. list_for_each_entry(kip, &c->pages, list) {
  232. long idx = ((long)slot - (long)kip->insns) /
  233. (c->insn_size * sizeof(kprobe_opcode_t));
  234. if (idx >= 0 && idx < slots_per_page(c)) {
  235. WARN_ON(kip->slot_used[idx] != SLOT_USED);
  236. if (dirty) {
  237. kip->slot_used[idx] = SLOT_DIRTY;
  238. kip->ngarbage++;
  239. if (++c->nr_garbage > slots_per_page(c))
  240. collect_garbage_slots(c);
  241. } else
  242. collect_one_slot(kip, idx);
  243. return;
  244. }
  245. }
  246. /* Could not free this slot. */
  247. WARN_ON(1);
  248. }
  249. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  250. {
  251. mutex_lock(&kprobe_insn_mutex);
  252. __free_insn_slot(&kprobe_insn_slots, slot, dirty);
  253. mutex_unlock(&kprobe_insn_mutex);
  254. }
  255. #ifdef CONFIG_OPTPROBES
  256. /* For optimized_kprobe buffer */
  257. static DEFINE_MUTEX(kprobe_optinsn_mutex); /* Protects kprobe_optinsn_slots */
  258. static struct kprobe_insn_cache kprobe_optinsn_slots = {
  259. .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
  260. /* .insn_size is initialized later */
  261. .nr_garbage = 0,
  262. };
  263. /* Get a slot for optimized_kprobe buffer */
  264. kprobe_opcode_t __kprobes *get_optinsn_slot(void)
  265. {
  266. kprobe_opcode_t *ret = NULL;
  267. mutex_lock(&kprobe_optinsn_mutex);
  268. ret = __get_insn_slot(&kprobe_optinsn_slots);
  269. mutex_unlock(&kprobe_optinsn_mutex);
  270. return ret;
  271. }
  272. void __kprobes free_optinsn_slot(kprobe_opcode_t * slot, int dirty)
  273. {
  274. mutex_lock(&kprobe_optinsn_mutex);
  275. __free_insn_slot(&kprobe_optinsn_slots, slot, dirty);
  276. mutex_unlock(&kprobe_optinsn_mutex);
  277. }
  278. #endif
  279. #endif
  280. /* We have preemption disabled.. so it is safe to use __ versions */
  281. static inline void set_kprobe_instance(struct kprobe *kp)
  282. {
  283. __get_cpu_var(kprobe_instance) = kp;
  284. }
  285. static inline void reset_kprobe_instance(void)
  286. {
  287. __get_cpu_var(kprobe_instance) = NULL;
  288. }
  289. /*
  290. * This routine is called either:
  291. * - under the kprobe_mutex - during kprobe_[un]register()
  292. * OR
  293. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  294. */
  295. struct kprobe __kprobes *get_kprobe(void *addr)
  296. {
  297. struct hlist_head *head;
  298. struct hlist_node *node;
  299. struct kprobe *p;
  300. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  301. hlist_for_each_entry_rcu(p, node, head, hlist) {
  302. if (p->addr == addr)
  303. return p;
  304. }
  305. return NULL;
  306. }
  307. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
  308. /* Return true if the kprobe is an aggregator */
  309. static inline int kprobe_aggrprobe(struct kprobe *p)
  310. {
  311. return p->pre_handler == aggr_pre_handler;
  312. }
  313. /*
  314. * Keep all fields in the kprobe consistent
  315. */
  316. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  317. {
  318. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  319. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  320. }
  321. #ifdef CONFIG_OPTPROBES
  322. /* NOTE: change this value only with kprobe_mutex held */
  323. static bool kprobes_allow_optimization;
  324. /*
  325. * Call all pre_handler on the list, but ignores its return value.
  326. * This must be called from arch-dep optimized caller.
  327. */
  328. void __kprobes opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
  329. {
  330. struct kprobe *kp;
  331. list_for_each_entry_rcu(kp, &p->list, list) {
  332. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  333. set_kprobe_instance(kp);
  334. kp->pre_handler(kp, regs);
  335. }
  336. reset_kprobe_instance();
  337. }
  338. }
  339. /* Return true(!0) if the kprobe is ready for optimization. */
  340. static inline int kprobe_optready(struct kprobe *p)
  341. {
  342. struct optimized_kprobe *op;
  343. if (kprobe_aggrprobe(p)) {
  344. op = container_of(p, struct optimized_kprobe, kp);
  345. return arch_prepared_optinsn(&op->optinsn);
  346. }
  347. return 0;
  348. }
  349. /*
  350. * Return an optimized kprobe whose optimizing code replaces
  351. * instructions including addr (exclude breakpoint).
  352. */
  353. static struct kprobe *__kprobes get_optimized_kprobe(unsigned long addr)
  354. {
  355. int i;
  356. struct kprobe *p = NULL;
  357. struct optimized_kprobe *op;
  358. /* Don't check i == 0, since that is a breakpoint case. */
  359. for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
  360. p = get_kprobe((void *)(addr - i));
  361. if (p && kprobe_optready(p)) {
  362. op = container_of(p, struct optimized_kprobe, kp);
  363. if (arch_within_optimized_kprobe(op, addr))
  364. return p;
  365. }
  366. return NULL;
  367. }
  368. /* Optimization staging list, protected by kprobe_mutex */
  369. static LIST_HEAD(optimizing_list);
  370. static void kprobe_optimizer(struct work_struct *work);
  371. static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
  372. #define OPTIMIZE_DELAY 5
  373. /* Kprobe jump optimizer */
  374. static __kprobes void kprobe_optimizer(struct work_struct *work)
  375. {
  376. struct optimized_kprobe *op, *tmp;
  377. /* Lock modules while optimizing kprobes */
  378. mutex_lock(&module_mutex);
  379. mutex_lock(&kprobe_mutex);
  380. if (kprobes_all_disarmed || !kprobes_allow_optimization)
  381. goto end;
  382. /*
  383. * Wait for quiesence period to ensure all running interrupts
  384. * are done. Because optprobe may modify multiple instructions
  385. * there is a chance that Nth instruction is interrupted. In that
  386. * case, running interrupt can return to 2nd-Nth byte of jump
  387. * instruction. This wait is for avoiding it.
  388. */
  389. synchronize_sched();
  390. /*
  391. * The optimization/unoptimization refers online_cpus via
  392. * stop_machine() and cpu-hotplug modifies online_cpus.
  393. * And same time, text_mutex will be held in cpu-hotplug and here.
  394. * This combination can cause a deadlock (cpu-hotplug try to lock
  395. * text_mutex but stop_machine can not be done because online_cpus
  396. * has been changed)
  397. * To avoid this deadlock, we need to call get_online_cpus()
  398. * for preventing cpu-hotplug outside of text_mutex locking.
  399. */
  400. get_online_cpus();
  401. mutex_lock(&text_mutex);
  402. list_for_each_entry_safe(op, tmp, &optimizing_list, list) {
  403. WARN_ON(kprobe_disabled(&op->kp));
  404. if (arch_optimize_kprobe(op) < 0)
  405. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  406. list_del_init(&op->list);
  407. }
  408. mutex_unlock(&text_mutex);
  409. put_online_cpus();
  410. end:
  411. mutex_unlock(&kprobe_mutex);
  412. mutex_unlock(&module_mutex);
  413. }
  414. /* Optimize kprobe if p is ready to be optimized */
  415. static __kprobes void optimize_kprobe(struct kprobe *p)
  416. {
  417. struct optimized_kprobe *op;
  418. /* Check if the kprobe is disabled or not ready for optimization. */
  419. if (!kprobe_optready(p) || !kprobes_allow_optimization ||
  420. (kprobe_disabled(p) || kprobes_all_disarmed))
  421. return;
  422. /* Both of break_handler and post_handler are not supported. */
  423. if (p->break_handler || p->post_handler)
  424. return;
  425. op = container_of(p, struct optimized_kprobe, kp);
  426. /* Check there is no other kprobes at the optimized instructions */
  427. if (arch_check_optimized_kprobe(op) < 0)
  428. return;
  429. /* Check if it is already optimized. */
  430. if (op->kp.flags & KPROBE_FLAG_OPTIMIZED)
  431. return;
  432. op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
  433. list_add(&op->list, &optimizing_list);
  434. if (!delayed_work_pending(&optimizing_work))
  435. schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
  436. }
  437. /* Unoptimize a kprobe if p is optimized */
  438. static __kprobes void unoptimize_kprobe(struct kprobe *p)
  439. {
  440. struct optimized_kprobe *op;
  441. if ((p->flags & KPROBE_FLAG_OPTIMIZED) && kprobe_aggrprobe(p)) {
  442. op = container_of(p, struct optimized_kprobe, kp);
  443. if (!list_empty(&op->list))
  444. /* Dequeue from the optimization queue */
  445. list_del_init(&op->list);
  446. else
  447. /* Replace jump with break */
  448. arch_unoptimize_kprobe(op);
  449. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  450. }
  451. }
  452. /* Remove optimized instructions */
  453. static void __kprobes kill_optimized_kprobe(struct kprobe *p)
  454. {
  455. struct optimized_kprobe *op;
  456. op = container_of(p, struct optimized_kprobe, kp);
  457. if (!list_empty(&op->list)) {
  458. /* Dequeue from the optimization queue */
  459. list_del_init(&op->list);
  460. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  461. }
  462. /* Don't unoptimize, because the target code will be freed. */
  463. arch_remove_optimized_kprobe(op);
  464. }
  465. /* Try to prepare optimized instructions */
  466. static __kprobes void prepare_optimized_kprobe(struct kprobe *p)
  467. {
  468. struct optimized_kprobe *op;
  469. op = container_of(p, struct optimized_kprobe, kp);
  470. arch_prepare_optimized_kprobe(op);
  471. }
  472. /* Free optimized instructions and optimized_kprobe */
  473. static __kprobes void free_aggr_kprobe(struct kprobe *p)
  474. {
  475. struct optimized_kprobe *op;
  476. op = container_of(p, struct optimized_kprobe, kp);
  477. arch_remove_optimized_kprobe(op);
  478. kfree(op);
  479. }
  480. /* Allocate new optimized_kprobe and try to prepare optimized instructions */
  481. static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  482. {
  483. struct optimized_kprobe *op;
  484. op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
  485. if (!op)
  486. return NULL;
  487. INIT_LIST_HEAD(&op->list);
  488. op->kp.addr = p->addr;
  489. arch_prepare_optimized_kprobe(op);
  490. return &op->kp;
  491. }
  492. static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
  493. /*
  494. * Prepare an optimized_kprobe and optimize it
  495. * NOTE: p must be a normal registered kprobe
  496. */
  497. static __kprobes void try_to_optimize_kprobe(struct kprobe *p)
  498. {
  499. struct kprobe *ap;
  500. struct optimized_kprobe *op;
  501. ap = alloc_aggr_kprobe(p);
  502. if (!ap)
  503. return;
  504. op = container_of(ap, struct optimized_kprobe, kp);
  505. if (!arch_prepared_optinsn(&op->optinsn)) {
  506. /* If failed to setup optimizing, fallback to kprobe */
  507. free_aggr_kprobe(ap);
  508. return;
  509. }
  510. init_aggr_kprobe(ap, p);
  511. optimize_kprobe(ap);
  512. }
  513. #ifdef CONFIG_SYSCTL
  514. static void __kprobes optimize_all_kprobes(void)
  515. {
  516. struct hlist_head *head;
  517. struct hlist_node *node;
  518. struct kprobe *p;
  519. unsigned int i;
  520. /* If optimization is already allowed, just return */
  521. if (kprobes_allow_optimization)
  522. return;
  523. kprobes_allow_optimization = true;
  524. mutex_lock(&text_mutex);
  525. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  526. head = &kprobe_table[i];
  527. hlist_for_each_entry_rcu(p, node, head, hlist)
  528. if (!kprobe_disabled(p))
  529. optimize_kprobe(p);
  530. }
  531. mutex_unlock(&text_mutex);
  532. printk(KERN_INFO "Kprobes globally optimized\n");
  533. }
  534. static void __kprobes unoptimize_all_kprobes(void)
  535. {
  536. struct hlist_head *head;
  537. struct hlist_node *node;
  538. struct kprobe *p;
  539. unsigned int i;
  540. /* If optimization is already prohibited, just return */
  541. if (!kprobes_allow_optimization)
  542. return;
  543. kprobes_allow_optimization = false;
  544. printk(KERN_INFO "Kprobes globally unoptimized\n");
  545. get_online_cpus(); /* For avoiding text_mutex deadlock */
  546. mutex_lock(&text_mutex);
  547. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  548. head = &kprobe_table[i];
  549. hlist_for_each_entry_rcu(p, node, head, hlist) {
  550. if (!kprobe_disabled(p))
  551. unoptimize_kprobe(p);
  552. }
  553. }
  554. mutex_unlock(&text_mutex);
  555. put_online_cpus();
  556. /* Allow all currently running kprobes to complete */
  557. synchronize_sched();
  558. }
  559. int sysctl_kprobes_optimization;
  560. int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
  561. void __user *buffer, size_t *length,
  562. loff_t *ppos)
  563. {
  564. int ret;
  565. mutex_lock(&kprobe_mutex);
  566. sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
  567. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  568. if (sysctl_kprobes_optimization)
  569. optimize_all_kprobes();
  570. else
  571. unoptimize_all_kprobes();
  572. mutex_unlock(&kprobe_mutex);
  573. return ret;
  574. }
  575. #endif /* CONFIG_SYSCTL */
  576. static void __kprobes __arm_kprobe(struct kprobe *p)
  577. {
  578. struct kprobe *old_p;
  579. /* Check collision with other optimized kprobes */
  580. old_p = get_optimized_kprobe((unsigned long)p->addr);
  581. if (unlikely(old_p))
  582. unoptimize_kprobe(old_p); /* Fallback to unoptimized kprobe */
  583. arch_arm_kprobe(p);
  584. optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
  585. }
  586. static void __kprobes __disarm_kprobe(struct kprobe *p)
  587. {
  588. struct kprobe *old_p;
  589. unoptimize_kprobe(p); /* Try to unoptimize */
  590. arch_disarm_kprobe(p);
  591. /* If another kprobe was blocked, optimize it. */
  592. old_p = get_optimized_kprobe((unsigned long)p->addr);
  593. if (unlikely(old_p))
  594. optimize_kprobe(old_p);
  595. }
  596. #else /* !CONFIG_OPTPROBES */
  597. #define optimize_kprobe(p) do {} while (0)
  598. #define unoptimize_kprobe(p) do {} while (0)
  599. #define kill_optimized_kprobe(p) do {} while (0)
  600. #define prepare_optimized_kprobe(p) do {} while (0)
  601. #define try_to_optimize_kprobe(p) do {} while (0)
  602. #define __arm_kprobe(p) arch_arm_kprobe(p)
  603. #define __disarm_kprobe(p) arch_disarm_kprobe(p)
  604. static __kprobes void free_aggr_kprobe(struct kprobe *p)
  605. {
  606. kfree(p);
  607. }
  608. static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  609. {
  610. return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  611. }
  612. #endif /* CONFIG_OPTPROBES */
  613. /* Arm a kprobe with text_mutex */
  614. static void __kprobes arm_kprobe(struct kprobe *kp)
  615. {
  616. /*
  617. * Here, since __arm_kprobe() doesn't use stop_machine(),
  618. * this doesn't cause deadlock on text_mutex. So, we don't
  619. * need get_online_cpus().
  620. */
  621. mutex_lock(&text_mutex);
  622. __arm_kprobe(kp);
  623. mutex_unlock(&text_mutex);
  624. }
  625. /* Disarm a kprobe with text_mutex */
  626. static void __kprobes disarm_kprobe(struct kprobe *kp)
  627. {
  628. get_online_cpus(); /* For avoiding text_mutex deadlock */
  629. mutex_lock(&text_mutex);
  630. __disarm_kprobe(kp);
  631. mutex_unlock(&text_mutex);
  632. put_online_cpus();
  633. }
  634. /*
  635. * Aggregate handlers for multiple kprobes support - these handlers
  636. * take care of invoking the individual kprobe handlers on p->list
  637. */
  638. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  639. {
  640. struct kprobe *kp;
  641. list_for_each_entry_rcu(kp, &p->list, list) {
  642. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  643. set_kprobe_instance(kp);
  644. if (kp->pre_handler(kp, regs))
  645. return 1;
  646. }
  647. reset_kprobe_instance();
  648. }
  649. return 0;
  650. }
  651. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  652. unsigned long flags)
  653. {
  654. struct kprobe *kp;
  655. list_for_each_entry_rcu(kp, &p->list, list) {
  656. if (kp->post_handler && likely(!kprobe_disabled(kp))) {
  657. set_kprobe_instance(kp);
  658. kp->post_handler(kp, regs, flags);
  659. reset_kprobe_instance();
  660. }
  661. }
  662. }
  663. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  664. int trapnr)
  665. {
  666. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  667. /*
  668. * if we faulted "during" the execution of a user specified
  669. * probe handler, invoke just that probe's fault handler
  670. */
  671. if (cur && cur->fault_handler) {
  672. if (cur->fault_handler(cur, regs, trapnr))
  673. return 1;
  674. }
  675. return 0;
  676. }
  677. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  678. {
  679. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  680. int ret = 0;
  681. if (cur && cur->break_handler) {
  682. if (cur->break_handler(cur, regs))
  683. ret = 1;
  684. }
  685. reset_kprobe_instance();
  686. return ret;
  687. }
  688. /* Walks the list and increments nmissed count for multiprobe case */
  689. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  690. {
  691. struct kprobe *kp;
  692. if (!kprobe_aggrprobe(p)) {
  693. p->nmissed++;
  694. } else {
  695. list_for_each_entry_rcu(kp, &p->list, list)
  696. kp->nmissed++;
  697. }
  698. return;
  699. }
  700. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  701. struct hlist_head *head)
  702. {
  703. struct kretprobe *rp = ri->rp;
  704. /* remove rp inst off the rprobe_inst_table */
  705. hlist_del(&ri->hlist);
  706. INIT_HLIST_NODE(&ri->hlist);
  707. if (likely(rp)) {
  708. spin_lock(&rp->lock);
  709. hlist_add_head(&ri->hlist, &rp->free_instances);
  710. spin_unlock(&rp->lock);
  711. } else
  712. /* Unregistering */
  713. hlist_add_head(&ri->hlist, head);
  714. }
  715. void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
  716. struct hlist_head **head, unsigned long *flags)
  717. __acquires(hlist_lock)
  718. {
  719. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  720. spinlock_t *hlist_lock;
  721. *head = &kretprobe_inst_table[hash];
  722. hlist_lock = kretprobe_table_lock_ptr(hash);
  723. spin_lock_irqsave(hlist_lock, *flags);
  724. }
  725. static void __kprobes kretprobe_table_lock(unsigned long hash,
  726. unsigned long *flags)
  727. __acquires(hlist_lock)
  728. {
  729. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  730. spin_lock_irqsave(hlist_lock, *flags);
  731. }
  732. void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
  733. unsigned long *flags)
  734. __releases(hlist_lock)
  735. {
  736. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  737. spinlock_t *hlist_lock;
  738. hlist_lock = kretprobe_table_lock_ptr(hash);
  739. spin_unlock_irqrestore(hlist_lock, *flags);
  740. }
  741. static void __kprobes kretprobe_table_unlock(unsigned long hash,
  742. unsigned long *flags)
  743. __releases(hlist_lock)
  744. {
  745. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  746. spin_unlock_irqrestore(hlist_lock, *flags);
  747. }
  748. /*
  749. * This function is called from finish_task_switch when task tk becomes dead,
  750. * so that we can recycle any function-return probe instances associated
  751. * with this task. These left over instances represent probed functions
  752. * that have been called but will never return.
  753. */
  754. void __kprobes kprobe_flush_task(struct task_struct *tk)
  755. {
  756. struct kretprobe_instance *ri;
  757. struct hlist_head *head, empty_rp;
  758. struct hlist_node *node, *tmp;
  759. unsigned long hash, flags = 0;
  760. if (unlikely(!kprobes_initialized))
  761. /* Early boot. kretprobe_table_locks not yet initialized. */
  762. return;
  763. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  764. head = &kretprobe_inst_table[hash];
  765. kretprobe_table_lock(hash, &flags);
  766. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  767. if (ri->task == tk)
  768. recycle_rp_inst(ri, &empty_rp);
  769. }
  770. kretprobe_table_unlock(hash, &flags);
  771. INIT_HLIST_HEAD(&empty_rp);
  772. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  773. hlist_del(&ri->hlist);
  774. kfree(ri);
  775. }
  776. }
  777. static inline void free_rp_inst(struct kretprobe *rp)
  778. {
  779. struct kretprobe_instance *ri;
  780. struct hlist_node *pos, *next;
  781. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
  782. hlist_del(&ri->hlist);
  783. kfree(ri);
  784. }
  785. }
  786. static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
  787. {
  788. unsigned long flags, hash;
  789. struct kretprobe_instance *ri;
  790. struct hlist_node *pos, *next;
  791. struct hlist_head *head;
  792. /* No race here */
  793. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  794. kretprobe_table_lock(hash, &flags);
  795. head = &kretprobe_inst_table[hash];
  796. hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
  797. if (ri->rp == rp)
  798. ri->rp = NULL;
  799. }
  800. kretprobe_table_unlock(hash, &flags);
  801. }
  802. free_rp_inst(rp);
  803. }
  804. /*
  805. * Add the new probe to ap->list. Fail if this is the
  806. * second jprobe at the address - two jprobes can't coexist
  807. */
  808. static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
  809. {
  810. BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
  811. if (p->break_handler || p->post_handler)
  812. unoptimize_kprobe(ap); /* Fall back to normal kprobe */
  813. if (p->break_handler) {
  814. if (ap->break_handler)
  815. return -EEXIST;
  816. list_add_tail_rcu(&p->list, &ap->list);
  817. ap->break_handler = aggr_break_handler;
  818. } else
  819. list_add_rcu(&p->list, &ap->list);
  820. if (p->post_handler && !ap->post_handler)
  821. ap->post_handler = aggr_post_handler;
  822. if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
  823. ap->flags &= ~KPROBE_FLAG_DISABLED;
  824. if (!kprobes_all_disarmed)
  825. /* Arm the breakpoint again. */
  826. __arm_kprobe(ap);
  827. }
  828. return 0;
  829. }
  830. /*
  831. * Fill in the required fields of the "manager kprobe". Replace the
  832. * earlier kprobe in the hlist with the manager kprobe
  833. */
  834. static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  835. {
  836. /* Copy p's insn slot to ap */
  837. copy_kprobe(p, ap);
  838. flush_insn_slot(ap);
  839. ap->addr = p->addr;
  840. ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
  841. ap->pre_handler = aggr_pre_handler;
  842. ap->fault_handler = aggr_fault_handler;
  843. /* We don't care the kprobe which has gone. */
  844. if (p->post_handler && !kprobe_gone(p))
  845. ap->post_handler = aggr_post_handler;
  846. if (p->break_handler && !kprobe_gone(p))
  847. ap->break_handler = aggr_break_handler;
  848. INIT_LIST_HEAD(&ap->list);
  849. INIT_HLIST_NODE(&ap->hlist);
  850. list_add_rcu(&p->list, &ap->list);
  851. hlist_replace_rcu(&p->hlist, &ap->hlist);
  852. }
  853. /*
  854. * This is the second or subsequent kprobe at the address - handle
  855. * the intricacies
  856. */
  857. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  858. struct kprobe *p)
  859. {
  860. int ret = 0;
  861. struct kprobe *ap = old_p;
  862. if (!kprobe_aggrprobe(old_p)) {
  863. /* If old_p is not an aggr_kprobe, create new aggr_kprobe. */
  864. ap = alloc_aggr_kprobe(old_p);
  865. if (!ap)
  866. return -ENOMEM;
  867. init_aggr_kprobe(ap, old_p);
  868. }
  869. if (kprobe_gone(ap)) {
  870. /*
  871. * Attempting to insert new probe at the same location that
  872. * had a probe in the module vaddr area which already
  873. * freed. So, the instruction slot has already been
  874. * released. We need a new slot for the new probe.
  875. */
  876. ret = arch_prepare_kprobe(ap);
  877. if (ret)
  878. /*
  879. * Even if fail to allocate new slot, don't need to
  880. * free aggr_probe. It will be used next time, or
  881. * freed by unregister_kprobe.
  882. */
  883. return ret;
  884. /* Prepare optimized instructions if possible. */
  885. prepare_optimized_kprobe(ap);
  886. /*
  887. * Clear gone flag to prevent allocating new slot again, and
  888. * set disabled flag because it is not armed yet.
  889. */
  890. ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
  891. | KPROBE_FLAG_DISABLED;
  892. }
  893. /* Copy ap's insn slot to p */
  894. copy_kprobe(ap, p);
  895. return add_new_kprobe(ap, p);
  896. }
  897. /* Try to disable aggr_kprobe, and return 1 if succeeded.*/
  898. static int __kprobes try_to_disable_aggr_kprobe(struct kprobe *p)
  899. {
  900. struct kprobe *kp;
  901. list_for_each_entry_rcu(kp, &p->list, list) {
  902. if (!kprobe_disabled(kp))
  903. /*
  904. * There is an active probe on the list.
  905. * We can't disable aggr_kprobe.
  906. */
  907. return 0;
  908. }
  909. p->flags |= KPROBE_FLAG_DISABLED;
  910. return 1;
  911. }
  912. static int __kprobes in_kprobes_functions(unsigned long addr)
  913. {
  914. struct kprobe_blackpoint *kb;
  915. if (addr >= (unsigned long)__kprobes_text_start &&
  916. addr < (unsigned long)__kprobes_text_end)
  917. return -EINVAL;
  918. /*
  919. * If there exists a kprobe_blacklist, verify and
  920. * fail any probe registration in the prohibited area
  921. */
  922. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  923. if (kb->start_addr) {
  924. if (addr >= kb->start_addr &&
  925. addr < (kb->start_addr + kb->range))
  926. return -EINVAL;
  927. }
  928. }
  929. return 0;
  930. }
  931. /*
  932. * If we have a symbol_name argument, look it up and add the offset field
  933. * to it. This way, we can specify a relative address to a symbol.
  934. */
  935. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  936. {
  937. kprobe_opcode_t *addr = p->addr;
  938. if (p->symbol_name) {
  939. if (addr)
  940. return NULL;
  941. kprobe_lookup_name(p->symbol_name, addr);
  942. }
  943. if (!addr)
  944. return NULL;
  945. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  946. }
  947. /* Check passed kprobe is valid and return kprobe in kprobe_table. */
  948. static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
  949. {
  950. struct kprobe *old_p, *list_p;
  951. old_p = get_kprobe(p->addr);
  952. if (unlikely(!old_p))
  953. return NULL;
  954. if (p != old_p) {
  955. list_for_each_entry_rcu(list_p, &old_p->list, list)
  956. if (list_p == p)
  957. /* kprobe p is a valid probe */
  958. goto valid;
  959. return NULL;
  960. }
  961. valid:
  962. return old_p;
  963. }
  964. /* Return error if the kprobe is being re-registered */
  965. static inline int check_kprobe_rereg(struct kprobe *p)
  966. {
  967. int ret = 0;
  968. struct kprobe *old_p;
  969. mutex_lock(&kprobe_mutex);
  970. old_p = __get_valid_kprobe(p);
  971. if (old_p)
  972. ret = -EINVAL;
  973. mutex_unlock(&kprobe_mutex);
  974. return ret;
  975. }
  976. int __kprobes register_kprobe(struct kprobe *p)
  977. {
  978. int ret = 0;
  979. struct kprobe *old_p;
  980. struct module *probed_mod;
  981. kprobe_opcode_t *addr;
  982. addr = kprobe_addr(p);
  983. if (!addr)
  984. return -EINVAL;
  985. p->addr = addr;
  986. ret = check_kprobe_rereg(p);
  987. if (ret)
  988. return ret;
  989. preempt_disable();
  990. if (!kernel_text_address((unsigned long) p->addr) ||
  991. in_kprobes_functions((unsigned long) p->addr) ||
  992. ftrace_text_reserved(p->addr, p->addr)) {
  993. preempt_enable();
  994. return -EINVAL;
  995. }
  996. /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
  997. p->flags &= KPROBE_FLAG_DISABLED;
  998. /*
  999. * Check if are we probing a module.
  1000. */
  1001. probed_mod = __module_text_address((unsigned long) p->addr);
  1002. if (probed_mod) {
  1003. /*
  1004. * We must hold a refcount of the probed module while updating
  1005. * its code to prohibit unexpected unloading.
  1006. */
  1007. if (unlikely(!try_module_get(probed_mod))) {
  1008. preempt_enable();
  1009. return -EINVAL;
  1010. }
  1011. /*
  1012. * If the module freed .init.text, we couldn't insert
  1013. * kprobes in there.
  1014. */
  1015. if (within_module_init((unsigned long)p->addr, probed_mod) &&
  1016. probed_mod->state != MODULE_STATE_COMING) {
  1017. module_put(probed_mod);
  1018. preempt_enable();
  1019. return -EINVAL;
  1020. }
  1021. }
  1022. preempt_enable();
  1023. p->nmissed = 0;
  1024. INIT_LIST_HEAD(&p->list);
  1025. mutex_lock(&kprobe_mutex);
  1026. get_online_cpus(); /* For avoiding text_mutex deadlock. */
  1027. mutex_lock(&text_mutex);
  1028. old_p = get_kprobe(p->addr);
  1029. if (old_p) {
  1030. /* Since this may unoptimize old_p, locking text_mutex. */
  1031. ret = register_aggr_kprobe(old_p, p);
  1032. goto out;
  1033. }
  1034. ret = arch_prepare_kprobe(p);
  1035. if (ret)
  1036. goto out;
  1037. INIT_HLIST_NODE(&p->hlist);
  1038. hlist_add_head_rcu(&p->hlist,
  1039. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  1040. if (!kprobes_all_disarmed && !kprobe_disabled(p))
  1041. __arm_kprobe(p);
  1042. /* Try to optimize kprobe */
  1043. try_to_optimize_kprobe(p);
  1044. out:
  1045. mutex_unlock(&text_mutex);
  1046. put_online_cpus();
  1047. mutex_unlock(&kprobe_mutex);
  1048. if (probed_mod)
  1049. module_put(probed_mod);
  1050. return ret;
  1051. }
  1052. EXPORT_SYMBOL_GPL(register_kprobe);
  1053. /*
  1054. * Unregister a kprobe without a scheduler synchronization.
  1055. */
  1056. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  1057. {
  1058. struct kprobe *old_p, *list_p;
  1059. old_p = __get_valid_kprobe(p);
  1060. if (old_p == NULL)
  1061. return -EINVAL;
  1062. if (old_p == p ||
  1063. (kprobe_aggrprobe(old_p) &&
  1064. list_is_singular(&old_p->list))) {
  1065. /*
  1066. * Only probe on the hash list. Disarm only if kprobes are
  1067. * enabled and not gone - otherwise, the breakpoint would
  1068. * already have been removed. We save on flushing icache.
  1069. */
  1070. if (!kprobes_all_disarmed && !kprobe_disabled(old_p))
  1071. disarm_kprobe(old_p);
  1072. hlist_del_rcu(&old_p->hlist);
  1073. } else {
  1074. if (p->break_handler && !kprobe_gone(p))
  1075. old_p->break_handler = NULL;
  1076. if (p->post_handler && !kprobe_gone(p)) {
  1077. list_for_each_entry_rcu(list_p, &old_p->list, list) {
  1078. if ((list_p != p) && (list_p->post_handler))
  1079. goto noclean;
  1080. }
  1081. old_p->post_handler = NULL;
  1082. }
  1083. noclean:
  1084. list_del_rcu(&p->list);
  1085. if (!kprobe_disabled(old_p)) {
  1086. try_to_disable_aggr_kprobe(old_p);
  1087. if (!kprobes_all_disarmed) {
  1088. if (kprobe_disabled(old_p))
  1089. disarm_kprobe(old_p);
  1090. else
  1091. /* Try to optimize this probe again */
  1092. optimize_kprobe(old_p);
  1093. }
  1094. }
  1095. }
  1096. return 0;
  1097. }
  1098. static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
  1099. {
  1100. struct kprobe *old_p;
  1101. if (list_empty(&p->list))
  1102. arch_remove_kprobe(p);
  1103. else if (list_is_singular(&p->list)) {
  1104. /* "p" is the last child of an aggr_kprobe */
  1105. old_p = list_entry(p->list.next, struct kprobe, list);
  1106. list_del(&p->list);
  1107. arch_remove_kprobe(old_p);
  1108. free_aggr_kprobe(old_p);
  1109. }
  1110. }
  1111. int __kprobes register_kprobes(struct kprobe **kps, int num)
  1112. {
  1113. int i, ret = 0;
  1114. if (num <= 0)
  1115. return -EINVAL;
  1116. for (i = 0; i < num; i++) {
  1117. ret = register_kprobe(kps[i]);
  1118. if (ret < 0) {
  1119. if (i > 0)
  1120. unregister_kprobes(kps, i);
  1121. break;
  1122. }
  1123. }
  1124. return ret;
  1125. }
  1126. EXPORT_SYMBOL_GPL(register_kprobes);
  1127. void __kprobes unregister_kprobe(struct kprobe *p)
  1128. {
  1129. unregister_kprobes(&p, 1);
  1130. }
  1131. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1132. void __kprobes unregister_kprobes(struct kprobe **kps, int num)
  1133. {
  1134. int i;
  1135. if (num <= 0)
  1136. return;
  1137. mutex_lock(&kprobe_mutex);
  1138. for (i = 0; i < num; i++)
  1139. if (__unregister_kprobe_top(kps[i]) < 0)
  1140. kps[i]->addr = NULL;
  1141. mutex_unlock(&kprobe_mutex);
  1142. synchronize_sched();
  1143. for (i = 0; i < num; i++)
  1144. if (kps[i]->addr)
  1145. __unregister_kprobe_bottom(kps[i]);
  1146. }
  1147. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1148. static struct notifier_block kprobe_exceptions_nb = {
  1149. .notifier_call = kprobe_exceptions_notify,
  1150. .priority = 0x7fffffff /* we need to be notified first */
  1151. };
  1152. unsigned long __weak arch_deref_entry_point(void *entry)
  1153. {
  1154. return (unsigned long)entry;
  1155. }
  1156. int __kprobes register_jprobes(struct jprobe **jps, int num)
  1157. {
  1158. struct jprobe *jp;
  1159. int ret = 0, i;
  1160. if (num <= 0)
  1161. return -EINVAL;
  1162. for (i = 0; i < num; i++) {
  1163. unsigned long addr, offset;
  1164. jp = jps[i];
  1165. addr = arch_deref_entry_point(jp->entry);
  1166. /* Verify probepoint is a function entry point */
  1167. if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
  1168. offset == 0) {
  1169. jp->kp.pre_handler = setjmp_pre_handler;
  1170. jp->kp.break_handler = longjmp_break_handler;
  1171. ret = register_kprobe(&jp->kp);
  1172. } else
  1173. ret = -EINVAL;
  1174. if (ret < 0) {
  1175. if (i > 0)
  1176. unregister_jprobes(jps, i);
  1177. break;
  1178. }
  1179. }
  1180. return ret;
  1181. }
  1182. EXPORT_SYMBOL_GPL(register_jprobes);
  1183. int __kprobes register_jprobe(struct jprobe *jp)
  1184. {
  1185. return register_jprobes(&jp, 1);
  1186. }
  1187. EXPORT_SYMBOL_GPL(register_jprobe);
  1188. void __kprobes unregister_jprobe(struct jprobe *jp)
  1189. {
  1190. unregister_jprobes(&jp, 1);
  1191. }
  1192. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1193. void __kprobes unregister_jprobes(struct jprobe **jps, int num)
  1194. {
  1195. int i;
  1196. if (num <= 0)
  1197. return;
  1198. mutex_lock(&kprobe_mutex);
  1199. for (i = 0; i < num; i++)
  1200. if (__unregister_kprobe_top(&jps[i]->kp) < 0)
  1201. jps[i]->kp.addr = NULL;
  1202. mutex_unlock(&kprobe_mutex);
  1203. synchronize_sched();
  1204. for (i = 0; i < num; i++) {
  1205. if (jps[i]->kp.addr)
  1206. __unregister_kprobe_bottom(&jps[i]->kp);
  1207. }
  1208. }
  1209. EXPORT_SYMBOL_GPL(unregister_jprobes);
  1210. #ifdef CONFIG_KRETPROBES
  1211. /*
  1212. * This kprobe pre_handler is registered with every kretprobe. When probe
  1213. * hits it will set up the return probe.
  1214. */
  1215. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  1216. struct pt_regs *regs)
  1217. {
  1218. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  1219. unsigned long hash, flags = 0;
  1220. struct kretprobe_instance *ri;
  1221. /*TODO: consider to only swap the RA after the last pre_handler fired */
  1222. hash = hash_ptr(current, KPROBE_HASH_BITS);
  1223. spin_lock_irqsave(&rp->lock, flags);
  1224. if (!hlist_empty(&rp->free_instances)) {
  1225. ri = hlist_entry(rp->free_instances.first,
  1226. struct kretprobe_instance, hlist);
  1227. hlist_del(&ri->hlist);
  1228. spin_unlock_irqrestore(&rp->lock, flags);
  1229. ri->rp = rp;
  1230. ri->task = current;
  1231. if (rp->entry_handler && rp->entry_handler(ri, regs))
  1232. return 0;
  1233. arch_prepare_kretprobe(ri, regs);
  1234. /* XXX(hch): why is there no hlist_move_head? */
  1235. INIT_HLIST_NODE(&ri->hlist);
  1236. kretprobe_table_lock(hash, &flags);
  1237. hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
  1238. kretprobe_table_unlock(hash, &flags);
  1239. } else {
  1240. rp->nmissed++;
  1241. spin_unlock_irqrestore(&rp->lock, flags);
  1242. }
  1243. return 0;
  1244. }
  1245. int __kprobes register_kretprobe(struct kretprobe *rp)
  1246. {
  1247. int ret = 0;
  1248. struct kretprobe_instance *inst;
  1249. int i;
  1250. void *addr;
  1251. if (kretprobe_blacklist_size) {
  1252. addr = kprobe_addr(&rp->kp);
  1253. if (!addr)
  1254. return -EINVAL;
  1255. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1256. if (kretprobe_blacklist[i].addr == addr)
  1257. return -EINVAL;
  1258. }
  1259. }
  1260. rp->kp.pre_handler = pre_handler_kretprobe;
  1261. rp->kp.post_handler = NULL;
  1262. rp->kp.fault_handler = NULL;
  1263. rp->kp.break_handler = NULL;
  1264. /* Pre-allocate memory for max kretprobe instances */
  1265. if (rp->maxactive <= 0) {
  1266. #ifdef CONFIG_PREEMPT
  1267. rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
  1268. #else
  1269. rp->maxactive = num_possible_cpus();
  1270. #endif
  1271. }
  1272. spin_lock_init(&rp->lock);
  1273. INIT_HLIST_HEAD(&rp->free_instances);
  1274. for (i = 0; i < rp->maxactive; i++) {
  1275. inst = kmalloc(sizeof(struct kretprobe_instance) +
  1276. rp->data_size, GFP_KERNEL);
  1277. if (inst == NULL) {
  1278. free_rp_inst(rp);
  1279. return -ENOMEM;
  1280. }
  1281. INIT_HLIST_NODE(&inst->hlist);
  1282. hlist_add_head(&inst->hlist, &rp->free_instances);
  1283. }
  1284. rp->nmissed = 0;
  1285. /* Establish function entry probe point */
  1286. ret = register_kprobe(&rp->kp);
  1287. if (ret != 0)
  1288. free_rp_inst(rp);
  1289. return ret;
  1290. }
  1291. EXPORT_SYMBOL_GPL(register_kretprobe);
  1292. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  1293. {
  1294. int ret = 0, i;
  1295. if (num <= 0)
  1296. return -EINVAL;
  1297. for (i = 0; i < num; i++) {
  1298. ret = register_kretprobe(rps[i]);
  1299. if (ret < 0) {
  1300. if (i > 0)
  1301. unregister_kretprobes(rps, i);
  1302. break;
  1303. }
  1304. }
  1305. return ret;
  1306. }
  1307. EXPORT_SYMBOL_GPL(register_kretprobes);
  1308. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  1309. {
  1310. unregister_kretprobes(&rp, 1);
  1311. }
  1312. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1313. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  1314. {
  1315. int i;
  1316. if (num <= 0)
  1317. return;
  1318. mutex_lock(&kprobe_mutex);
  1319. for (i = 0; i < num; i++)
  1320. if (__unregister_kprobe_top(&rps[i]->kp) < 0)
  1321. rps[i]->kp.addr = NULL;
  1322. mutex_unlock(&kprobe_mutex);
  1323. synchronize_sched();
  1324. for (i = 0; i < num; i++) {
  1325. if (rps[i]->kp.addr) {
  1326. __unregister_kprobe_bottom(&rps[i]->kp);
  1327. cleanup_rp_inst(rps[i]);
  1328. }
  1329. }
  1330. }
  1331. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1332. #else /* CONFIG_KRETPROBES */
  1333. int __kprobes register_kretprobe(struct kretprobe *rp)
  1334. {
  1335. return -ENOSYS;
  1336. }
  1337. EXPORT_SYMBOL_GPL(register_kretprobe);
  1338. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  1339. {
  1340. return -ENOSYS;
  1341. }
  1342. EXPORT_SYMBOL_GPL(register_kretprobes);
  1343. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  1344. {
  1345. }
  1346. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1347. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  1348. {
  1349. }
  1350. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1351. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  1352. struct pt_regs *regs)
  1353. {
  1354. return 0;
  1355. }
  1356. #endif /* CONFIG_KRETPROBES */
  1357. /* Set the kprobe gone and remove its instruction buffer. */
  1358. static void __kprobes kill_kprobe(struct kprobe *p)
  1359. {
  1360. struct kprobe *kp;
  1361. p->flags |= KPROBE_FLAG_GONE;
  1362. if (kprobe_aggrprobe(p)) {
  1363. /*
  1364. * If this is an aggr_kprobe, we have to list all the
  1365. * chained probes and mark them GONE.
  1366. */
  1367. list_for_each_entry_rcu(kp, &p->list, list)
  1368. kp->flags |= KPROBE_FLAG_GONE;
  1369. p->post_handler = NULL;
  1370. p->break_handler = NULL;
  1371. kill_optimized_kprobe(p);
  1372. }
  1373. /*
  1374. * Here, we can remove insn_slot safely, because no thread calls
  1375. * the original probed function (which will be freed soon) any more.
  1376. */
  1377. arch_remove_kprobe(p);
  1378. }
  1379. /* Disable one kprobe */
  1380. int __kprobes disable_kprobe(struct kprobe *kp)
  1381. {
  1382. int ret = 0;
  1383. struct kprobe *p;
  1384. mutex_lock(&kprobe_mutex);
  1385. /* Check whether specified probe is valid. */
  1386. p = __get_valid_kprobe(kp);
  1387. if (unlikely(p == NULL)) {
  1388. ret = -EINVAL;
  1389. goto out;
  1390. }
  1391. /* If the probe is already disabled (or gone), just return */
  1392. if (kprobe_disabled(kp))
  1393. goto out;
  1394. kp->flags |= KPROBE_FLAG_DISABLED;
  1395. if (p != kp)
  1396. /* When kp != p, p is always enabled. */
  1397. try_to_disable_aggr_kprobe(p);
  1398. if (!kprobes_all_disarmed && kprobe_disabled(p))
  1399. disarm_kprobe(p);
  1400. out:
  1401. mutex_unlock(&kprobe_mutex);
  1402. return ret;
  1403. }
  1404. EXPORT_SYMBOL_GPL(disable_kprobe);
  1405. /* Enable one kprobe */
  1406. int __kprobes enable_kprobe(struct kprobe *kp)
  1407. {
  1408. int ret = 0;
  1409. struct kprobe *p;
  1410. mutex_lock(&kprobe_mutex);
  1411. /* Check whether specified probe is valid. */
  1412. p = __get_valid_kprobe(kp);
  1413. if (unlikely(p == NULL)) {
  1414. ret = -EINVAL;
  1415. goto out;
  1416. }
  1417. if (kprobe_gone(kp)) {
  1418. /* This kprobe has gone, we couldn't enable it. */
  1419. ret = -EINVAL;
  1420. goto out;
  1421. }
  1422. if (p != kp)
  1423. kp->flags &= ~KPROBE_FLAG_DISABLED;
  1424. if (!kprobes_all_disarmed && kprobe_disabled(p)) {
  1425. p->flags &= ~KPROBE_FLAG_DISABLED;
  1426. arm_kprobe(p);
  1427. }
  1428. out:
  1429. mutex_unlock(&kprobe_mutex);
  1430. return ret;
  1431. }
  1432. EXPORT_SYMBOL_GPL(enable_kprobe);
  1433. void __kprobes dump_kprobe(struct kprobe *kp)
  1434. {
  1435. printk(KERN_WARNING "Dumping kprobe:\n");
  1436. printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
  1437. kp->symbol_name, kp->addr, kp->offset);
  1438. }
  1439. /* Module notifier call back, checking kprobes on the module */
  1440. static int __kprobes kprobes_module_callback(struct notifier_block *nb,
  1441. unsigned long val, void *data)
  1442. {
  1443. struct module *mod = data;
  1444. struct hlist_head *head;
  1445. struct hlist_node *node;
  1446. struct kprobe *p;
  1447. unsigned int i;
  1448. int checkcore = (val == MODULE_STATE_GOING);
  1449. if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
  1450. return NOTIFY_DONE;
  1451. /*
  1452. * When MODULE_STATE_GOING was notified, both of module .text and
  1453. * .init.text sections would be freed. When MODULE_STATE_LIVE was
  1454. * notified, only .init.text section would be freed. We need to
  1455. * disable kprobes which have been inserted in the sections.
  1456. */
  1457. mutex_lock(&kprobe_mutex);
  1458. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1459. head = &kprobe_table[i];
  1460. hlist_for_each_entry_rcu(p, node, head, hlist)
  1461. if (within_module_init((unsigned long)p->addr, mod) ||
  1462. (checkcore &&
  1463. within_module_core((unsigned long)p->addr, mod))) {
  1464. /*
  1465. * The vaddr this probe is installed will soon
  1466. * be vfreed buy not synced to disk. Hence,
  1467. * disarming the breakpoint isn't needed.
  1468. */
  1469. kill_kprobe(p);
  1470. }
  1471. }
  1472. mutex_unlock(&kprobe_mutex);
  1473. return NOTIFY_DONE;
  1474. }
  1475. static struct notifier_block kprobe_module_nb = {
  1476. .notifier_call = kprobes_module_callback,
  1477. .priority = 0
  1478. };
  1479. static int __init init_kprobes(void)
  1480. {
  1481. int i, err = 0;
  1482. unsigned long offset = 0, size = 0;
  1483. char *modname, namebuf[128];
  1484. const char *symbol_name;
  1485. void *addr;
  1486. struct kprobe_blackpoint *kb;
  1487. /* FIXME allocate the probe table, currently defined statically */
  1488. /* initialize all list heads */
  1489. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1490. INIT_HLIST_HEAD(&kprobe_table[i]);
  1491. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  1492. spin_lock_init(&(kretprobe_table_locks[i].lock));
  1493. }
  1494. /*
  1495. * Lookup and populate the kprobe_blacklist.
  1496. *
  1497. * Unlike the kretprobe blacklist, we'll need to determine
  1498. * the range of addresses that belong to the said functions,
  1499. * since a kprobe need not necessarily be at the beginning
  1500. * of a function.
  1501. */
  1502. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  1503. kprobe_lookup_name(kb->name, addr);
  1504. if (!addr)
  1505. continue;
  1506. kb->start_addr = (unsigned long)addr;
  1507. symbol_name = kallsyms_lookup(kb->start_addr,
  1508. &size, &offset, &modname, namebuf);
  1509. if (!symbol_name)
  1510. kb->range = 0;
  1511. else
  1512. kb->range = size;
  1513. }
  1514. if (kretprobe_blacklist_size) {
  1515. /* lookup the function address from its name */
  1516. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1517. kprobe_lookup_name(kretprobe_blacklist[i].name,
  1518. kretprobe_blacklist[i].addr);
  1519. if (!kretprobe_blacklist[i].addr)
  1520. printk("kretprobe: lookup failed: %s\n",
  1521. kretprobe_blacklist[i].name);
  1522. }
  1523. }
  1524. #if defined(CONFIG_OPTPROBES)
  1525. #if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
  1526. /* Init kprobe_optinsn_slots */
  1527. kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
  1528. #endif
  1529. /* By default, kprobes can be optimized */
  1530. kprobes_allow_optimization = true;
  1531. #endif
  1532. /* By default, kprobes are armed */
  1533. kprobes_all_disarmed = false;
  1534. err = arch_init_kprobes();
  1535. if (!err)
  1536. err = register_die_notifier(&kprobe_exceptions_nb);
  1537. if (!err)
  1538. err = register_module_notifier(&kprobe_module_nb);
  1539. kprobes_initialized = (err == 0);
  1540. if (!err)
  1541. init_test_probes();
  1542. return err;
  1543. }
  1544. #ifdef CONFIG_DEBUG_FS
  1545. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  1546. const char *sym, int offset, char *modname, struct kprobe *pp)
  1547. {
  1548. char *kprobe_type;
  1549. if (p->pre_handler == pre_handler_kretprobe)
  1550. kprobe_type = "r";
  1551. else if (p->pre_handler == setjmp_pre_handler)
  1552. kprobe_type = "j";
  1553. else
  1554. kprobe_type = "k";
  1555. if (sym)
  1556. seq_printf(pi, "%p %s %s+0x%x %s ",
  1557. p->addr, kprobe_type, sym, offset,
  1558. (modname ? modname : " "));
  1559. else
  1560. seq_printf(pi, "%p %s %p ",
  1561. p->addr, kprobe_type, p->addr);
  1562. if (!pp)
  1563. pp = p;
  1564. seq_printf(pi, "%s%s%s\n",
  1565. (kprobe_gone(p) ? "[GONE]" : ""),
  1566. ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
  1567. (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""));
  1568. }
  1569. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  1570. {
  1571. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  1572. }
  1573. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  1574. {
  1575. (*pos)++;
  1576. if (*pos >= KPROBE_TABLE_SIZE)
  1577. return NULL;
  1578. return pos;
  1579. }
  1580. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  1581. {
  1582. /* Nothing to do */
  1583. }
  1584. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  1585. {
  1586. struct hlist_head *head;
  1587. struct hlist_node *node;
  1588. struct kprobe *p, *kp;
  1589. const char *sym = NULL;
  1590. unsigned int i = *(loff_t *) v;
  1591. unsigned long offset = 0;
  1592. char *modname, namebuf[128];
  1593. head = &kprobe_table[i];
  1594. preempt_disable();
  1595. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1596. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  1597. &offset, &modname, namebuf);
  1598. if (kprobe_aggrprobe(p)) {
  1599. list_for_each_entry_rcu(kp, &p->list, list)
  1600. report_probe(pi, kp, sym, offset, modname, p);
  1601. } else
  1602. report_probe(pi, p, sym, offset, modname, NULL);
  1603. }
  1604. preempt_enable();
  1605. return 0;
  1606. }
  1607. static const struct seq_operations kprobes_seq_ops = {
  1608. .start = kprobe_seq_start,
  1609. .next = kprobe_seq_next,
  1610. .stop = kprobe_seq_stop,
  1611. .show = show_kprobe_addr
  1612. };
  1613. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  1614. {
  1615. return seq_open(filp, &kprobes_seq_ops);
  1616. }
  1617. static const struct file_operations debugfs_kprobes_operations = {
  1618. .open = kprobes_open,
  1619. .read = seq_read,
  1620. .llseek = seq_lseek,
  1621. .release = seq_release,
  1622. };
  1623. static void __kprobes arm_all_kprobes(void)
  1624. {
  1625. struct hlist_head *head;
  1626. struct hlist_node *node;
  1627. struct kprobe *p;
  1628. unsigned int i;
  1629. mutex_lock(&kprobe_mutex);
  1630. /* If kprobes are armed, just return */
  1631. if (!kprobes_all_disarmed)
  1632. goto already_enabled;
  1633. /* Arming kprobes doesn't optimize kprobe itself */
  1634. mutex_lock(&text_mutex);
  1635. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1636. head = &kprobe_table[i];
  1637. hlist_for_each_entry_rcu(p, node, head, hlist)
  1638. if (!kprobe_disabled(p))
  1639. __arm_kprobe(p);
  1640. }
  1641. mutex_unlock(&text_mutex);
  1642. kprobes_all_disarmed = false;
  1643. printk(KERN_INFO "Kprobes globally enabled\n");
  1644. already_enabled:
  1645. mutex_unlock(&kprobe_mutex);
  1646. return;
  1647. }
  1648. static void __kprobes disarm_all_kprobes(void)
  1649. {
  1650. struct hlist_head *head;
  1651. struct hlist_node *node;
  1652. struct kprobe *p;
  1653. unsigned int i;
  1654. mutex_lock(&kprobe_mutex);
  1655. /* If kprobes are already disarmed, just return */
  1656. if (kprobes_all_disarmed)
  1657. goto already_disabled;
  1658. kprobes_all_disarmed = true;
  1659. printk(KERN_INFO "Kprobes globally disabled\n");
  1660. /*
  1661. * Here we call get_online_cpus() for avoiding text_mutex deadlock,
  1662. * because disarming may also unoptimize kprobes.
  1663. */
  1664. get_online_cpus();
  1665. mutex_lock(&text_mutex);
  1666. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1667. head = &kprobe_table[i];
  1668. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1669. if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
  1670. __disarm_kprobe(p);
  1671. }
  1672. }
  1673. mutex_unlock(&text_mutex);
  1674. put_online_cpus();
  1675. mutex_unlock(&kprobe_mutex);
  1676. /* Allow all currently running kprobes to complete */
  1677. synchronize_sched();
  1678. return;
  1679. already_disabled:
  1680. mutex_unlock(&kprobe_mutex);
  1681. return;
  1682. }
  1683. /*
  1684. * XXX: The debugfs bool file interface doesn't allow for callbacks
  1685. * when the bool state is switched. We can reuse that facility when
  1686. * available
  1687. */
  1688. static ssize_t read_enabled_file_bool(struct file *file,
  1689. char __user *user_buf, size_t count, loff_t *ppos)
  1690. {
  1691. char buf[3];
  1692. if (!kprobes_all_disarmed)
  1693. buf[0] = '1';
  1694. else
  1695. buf[0] = '0';
  1696. buf[1] = '\n';
  1697. buf[2] = 0x00;
  1698. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  1699. }
  1700. static ssize_t write_enabled_file_bool(struct file *file,
  1701. const char __user *user_buf, size_t count, loff_t *ppos)
  1702. {
  1703. char buf[32];
  1704. int buf_size;
  1705. buf_size = min(count, (sizeof(buf)-1));
  1706. if (copy_from_user(buf, user_buf, buf_size))
  1707. return -EFAULT;
  1708. switch (buf[0]) {
  1709. case 'y':
  1710. case 'Y':
  1711. case '1':
  1712. arm_all_kprobes();
  1713. break;
  1714. case 'n':
  1715. case 'N':
  1716. case '0':
  1717. disarm_all_kprobes();
  1718. break;
  1719. }
  1720. return count;
  1721. }
  1722. static const struct file_operations fops_kp = {
  1723. .read = read_enabled_file_bool,
  1724. .write = write_enabled_file_bool,
  1725. };
  1726. static int __kprobes debugfs_kprobe_init(void)
  1727. {
  1728. struct dentry *dir, *file;
  1729. unsigned int value = 1;
  1730. dir = debugfs_create_dir("kprobes", NULL);
  1731. if (!dir)
  1732. return -ENOMEM;
  1733. file = debugfs_create_file("list", 0444, dir, NULL,
  1734. &debugfs_kprobes_operations);
  1735. if (!file) {
  1736. debugfs_remove(dir);
  1737. return -ENOMEM;
  1738. }
  1739. file = debugfs_create_file("enabled", 0600, dir,
  1740. &value, &fops_kp);
  1741. if (!file) {
  1742. debugfs_remove(dir);
  1743. return -ENOMEM;
  1744. }
  1745. return 0;
  1746. }
  1747. late_initcall(debugfs_kprobe_init);
  1748. #endif /* CONFIG_DEBUG_FS */
  1749. module_init(init_kprobes);
  1750. /* defined in arch/.../kernel/kprobes.c */
  1751. EXPORT_SYMBOL_GPL(jprobe_return);