kprobes.c 63 KB

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