kprobes.c 62 KB

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