kprobes.c 56 KB

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