kprobes.c 63 KB

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