kprobes.c 59 KB

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