kprobes.c 62 KB

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