kprobes.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  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 int arm_kprobe_ftrace(struct kprobe *p)
  841. {
  842. int ret = 0;
  843. ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
  844. (unsigned long)p->addr, 0, 0);
  845. if (ret) {
  846. pr_debug("Failed to arm kprobe-ftrace at %p (%d)\n", p->addr, ret);
  847. return ret;
  848. }
  849. if (kprobe_ftrace_enabled == 0) {
  850. ret = register_ftrace_function(&kprobe_ftrace_ops);
  851. if (ret) {
  852. pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
  853. goto err_ftrace;
  854. }
  855. }
  856. kprobe_ftrace_enabled++;
  857. return ret;
  858. err_ftrace:
  859. /*
  860. * Note: Since kprobe_ftrace_ops has IPMODIFY set, and ftrace requires a
  861. * non-empty filter_hash for IPMODIFY ops, we're safe from an accidental
  862. * empty filter_hash which would undesirably trace all functions.
  863. */
  864. ftrace_set_filter_ip(&kprobe_ftrace_ops, (unsigned long)p->addr, 1, 0);
  865. return ret;
  866. }
  867. /* Caller must lock kprobe_mutex */
  868. static int disarm_kprobe_ftrace(struct kprobe *p)
  869. {
  870. int ret = 0;
  871. if (kprobe_ftrace_enabled == 1) {
  872. ret = unregister_ftrace_function(&kprobe_ftrace_ops);
  873. if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
  874. return ret;
  875. }
  876. kprobe_ftrace_enabled--;
  877. ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
  878. (unsigned long)p->addr, 1, 0);
  879. WARN(ret < 0, "Failed to disarm kprobe-ftrace at %p (%d)\n", p->addr, ret);
  880. return ret;
  881. }
  882. #else /* !CONFIG_KPROBES_ON_FTRACE */
  883. #define prepare_kprobe(p) arch_prepare_kprobe(p)
  884. #define arm_kprobe_ftrace(p) (-ENODEV)
  885. #define disarm_kprobe_ftrace(p) (-ENODEV)
  886. #endif
  887. /* Arm a kprobe with text_mutex */
  888. static int arm_kprobe(struct kprobe *kp)
  889. {
  890. if (unlikely(kprobe_ftrace(kp)))
  891. return arm_kprobe_ftrace(kp);
  892. cpus_read_lock();
  893. mutex_lock(&text_mutex);
  894. __arm_kprobe(kp);
  895. mutex_unlock(&text_mutex);
  896. cpus_read_unlock();
  897. return 0;
  898. }
  899. /* Disarm a kprobe with text_mutex */
  900. static int disarm_kprobe(struct kprobe *kp, bool reopt)
  901. {
  902. if (unlikely(kprobe_ftrace(kp)))
  903. return disarm_kprobe_ftrace(kp);
  904. cpus_read_lock();
  905. mutex_lock(&text_mutex);
  906. __disarm_kprobe(kp, reopt);
  907. mutex_unlock(&text_mutex);
  908. cpus_read_unlock();
  909. return 0;
  910. }
  911. /*
  912. * Aggregate handlers for multiple kprobes support - these handlers
  913. * take care of invoking the individual kprobe handlers on p->list
  914. */
  915. static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  916. {
  917. struct kprobe *kp;
  918. list_for_each_entry_rcu(kp, &p->list, list) {
  919. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  920. set_kprobe_instance(kp);
  921. if (kp->pre_handler(kp, regs))
  922. return 1;
  923. }
  924. reset_kprobe_instance();
  925. }
  926. return 0;
  927. }
  928. NOKPROBE_SYMBOL(aggr_pre_handler);
  929. static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  930. unsigned long flags)
  931. {
  932. struct kprobe *kp;
  933. list_for_each_entry_rcu(kp, &p->list, list) {
  934. if (kp->post_handler && likely(!kprobe_disabled(kp))) {
  935. set_kprobe_instance(kp);
  936. kp->post_handler(kp, regs, flags);
  937. reset_kprobe_instance();
  938. }
  939. }
  940. }
  941. NOKPROBE_SYMBOL(aggr_post_handler);
  942. static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  943. int trapnr)
  944. {
  945. struct kprobe *cur = __this_cpu_read(kprobe_instance);
  946. /*
  947. * if we faulted "during" the execution of a user specified
  948. * probe handler, invoke just that probe's fault handler
  949. */
  950. if (cur && cur->fault_handler) {
  951. if (cur->fault_handler(cur, regs, trapnr))
  952. return 1;
  953. }
  954. return 0;
  955. }
  956. NOKPROBE_SYMBOL(aggr_fault_handler);
  957. static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  958. {
  959. struct kprobe *cur = __this_cpu_read(kprobe_instance);
  960. int ret = 0;
  961. if (cur && cur->break_handler) {
  962. if (cur->break_handler(cur, regs))
  963. ret = 1;
  964. }
  965. reset_kprobe_instance();
  966. return ret;
  967. }
  968. NOKPROBE_SYMBOL(aggr_break_handler);
  969. /* Walks the list and increments nmissed count for multiprobe case */
  970. void kprobes_inc_nmissed_count(struct kprobe *p)
  971. {
  972. struct kprobe *kp;
  973. if (!kprobe_aggrprobe(p)) {
  974. p->nmissed++;
  975. } else {
  976. list_for_each_entry_rcu(kp, &p->list, list)
  977. kp->nmissed++;
  978. }
  979. return;
  980. }
  981. NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
  982. void recycle_rp_inst(struct kretprobe_instance *ri,
  983. struct hlist_head *head)
  984. {
  985. struct kretprobe *rp = ri->rp;
  986. /* remove rp inst off the rprobe_inst_table */
  987. hlist_del(&ri->hlist);
  988. INIT_HLIST_NODE(&ri->hlist);
  989. if (likely(rp)) {
  990. raw_spin_lock(&rp->lock);
  991. hlist_add_head(&ri->hlist, &rp->free_instances);
  992. raw_spin_unlock(&rp->lock);
  993. } else
  994. /* Unregistering */
  995. hlist_add_head(&ri->hlist, head);
  996. }
  997. NOKPROBE_SYMBOL(recycle_rp_inst);
  998. void kretprobe_hash_lock(struct task_struct *tsk,
  999. struct hlist_head **head, unsigned long *flags)
  1000. __acquires(hlist_lock)
  1001. {
  1002. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  1003. raw_spinlock_t *hlist_lock;
  1004. *head = &kretprobe_inst_table[hash];
  1005. hlist_lock = kretprobe_table_lock_ptr(hash);
  1006. raw_spin_lock_irqsave(hlist_lock, *flags);
  1007. }
  1008. NOKPROBE_SYMBOL(kretprobe_hash_lock);
  1009. static void kretprobe_table_lock(unsigned long hash,
  1010. unsigned long *flags)
  1011. __acquires(hlist_lock)
  1012. {
  1013. raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  1014. raw_spin_lock_irqsave(hlist_lock, *flags);
  1015. }
  1016. NOKPROBE_SYMBOL(kretprobe_table_lock);
  1017. void kretprobe_hash_unlock(struct task_struct *tsk,
  1018. unsigned long *flags)
  1019. __releases(hlist_lock)
  1020. {
  1021. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  1022. raw_spinlock_t *hlist_lock;
  1023. hlist_lock = kretprobe_table_lock_ptr(hash);
  1024. raw_spin_unlock_irqrestore(hlist_lock, *flags);
  1025. }
  1026. NOKPROBE_SYMBOL(kretprobe_hash_unlock);
  1027. static void kretprobe_table_unlock(unsigned long hash,
  1028. unsigned long *flags)
  1029. __releases(hlist_lock)
  1030. {
  1031. raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  1032. raw_spin_unlock_irqrestore(hlist_lock, *flags);
  1033. }
  1034. NOKPROBE_SYMBOL(kretprobe_table_unlock);
  1035. /*
  1036. * This function is called from finish_task_switch when task tk becomes dead,
  1037. * so that we can recycle any function-return probe instances associated
  1038. * with this task. These left over instances represent probed functions
  1039. * that have been called but will never return.
  1040. */
  1041. void kprobe_flush_task(struct task_struct *tk)
  1042. {
  1043. struct kretprobe_instance *ri;
  1044. struct hlist_head *head, empty_rp;
  1045. struct hlist_node *tmp;
  1046. unsigned long hash, flags = 0;
  1047. if (unlikely(!kprobes_initialized))
  1048. /* Early boot. kretprobe_table_locks not yet initialized. */
  1049. return;
  1050. INIT_HLIST_HEAD(&empty_rp);
  1051. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  1052. head = &kretprobe_inst_table[hash];
  1053. kretprobe_table_lock(hash, &flags);
  1054. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  1055. if (ri->task == tk)
  1056. recycle_rp_inst(ri, &empty_rp);
  1057. }
  1058. kretprobe_table_unlock(hash, &flags);
  1059. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  1060. hlist_del(&ri->hlist);
  1061. kfree(ri);
  1062. }
  1063. }
  1064. NOKPROBE_SYMBOL(kprobe_flush_task);
  1065. static inline void free_rp_inst(struct kretprobe *rp)
  1066. {
  1067. struct kretprobe_instance *ri;
  1068. struct hlist_node *next;
  1069. hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
  1070. hlist_del(&ri->hlist);
  1071. kfree(ri);
  1072. }
  1073. }
  1074. static void cleanup_rp_inst(struct kretprobe *rp)
  1075. {
  1076. unsigned long flags, hash;
  1077. struct kretprobe_instance *ri;
  1078. struct hlist_node *next;
  1079. struct hlist_head *head;
  1080. /* No race here */
  1081. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  1082. kretprobe_table_lock(hash, &flags);
  1083. head = &kretprobe_inst_table[hash];
  1084. hlist_for_each_entry_safe(ri, next, head, hlist) {
  1085. if (ri->rp == rp)
  1086. ri->rp = NULL;
  1087. }
  1088. kretprobe_table_unlock(hash, &flags);
  1089. }
  1090. free_rp_inst(rp);
  1091. }
  1092. NOKPROBE_SYMBOL(cleanup_rp_inst);
  1093. /*
  1094. * Add the new probe to ap->list. Fail if this is the
  1095. * second jprobe at the address - two jprobes can't coexist
  1096. */
  1097. static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
  1098. {
  1099. BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
  1100. if (p->break_handler || p->post_handler)
  1101. unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
  1102. if (p->break_handler) {
  1103. if (ap->break_handler)
  1104. return -EEXIST;
  1105. list_add_tail_rcu(&p->list, &ap->list);
  1106. ap->break_handler = aggr_break_handler;
  1107. } else
  1108. list_add_rcu(&p->list, &ap->list);
  1109. if (p->post_handler && !ap->post_handler)
  1110. ap->post_handler = aggr_post_handler;
  1111. return 0;
  1112. }
  1113. /*
  1114. * Fill in the required fields of the "manager kprobe". Replace the
  1115. * earlier kprobe in the hlist with the manager kprobe
  1116. */
  1117. static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  1118. {
  1119. /* Copy p's insn slot to ap */
  1120. copy_kprobe(p, ap);
  1121. flush_insn_slot(ap);
  1122. ap->addr = p->addr;
  1123. ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
  1124. ap->pre_handler = aggr_pre_handler;
  1125. ap->fault_handler = aggr_fault_handler;
  1126. /* We don't care the kprobe which has gone. */
  1127. if (p->post_handler && !kprobe_gone(p))
  1128. ap->post_handler = aggr_post_handler;
  1129. if (p->break_handler && !kprobe_gone(p))
  1130. ap->break_handler = aggr_break_handler;
  1131. INIT_LIST_HEAD(&ap->list);
  1132. INIT_HLIST_NODE(&ap->hlist);
  1133. list_add_rcu(&p->list, &ap->list);
  1134. hlist_replace_rcu(&p->hlist, &ap->hlist);
  1135. }
  1136. /*
  1137. * This is the second or subsequent kprobe at the address - handle
  1138. * the intricacies
  1139. */
  1140. static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
  1141. {
  1142. int ret = 0;
  1143. struct kprobe *ap = orig_p;
  1144. cpus_read_lock();
  1145. /* For preparing optimization, jump_label_text_reserved() is called */
  1146. jump_label_lock();
  1147. mutex_lock(&text_mutex);
  1148. if (!kprobe_aggrprobe(orig_p)) {
  1149. /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
  1150. ap = alloc_aggr_kprobe(orig_p);
  1151. if (!ap) {
  1152. ret = -ENOMEM;
  1153. goto out;
  1154. }
  1155. init_aggr_kprobe(ap, orig_p);
  1156. } else if (kprobe_unused(ap))
  1157. /* This probe is going to die. Rescue it */
  1158. reuse_unused_kprobe(ap);
  1159. if (kprobe_gone(ap)) {
  1160. /*
  1161. * Attempting to insert new probe at the same location that
  1162. * had a probe in the module vaddr area which already
  1163. * freed. So, the instruction slot has already been
  1164. * released. We need a new slot for the new probe.
  1165. */
  1166. ret = arch_prepare_kprobe(ap);
  1167. if (ret)
  1168. /*
  1169. * Even if fail to allocate new slot, don't need to
  1170. * free aggr_probe. It will be used next time, or
  1171. * freed by unregister_kprobe.
  1172. */
  1173. goto out;
  1174. /* Prepare optimized instructions if possible. */
  1175. prepare_optimized_kprobe(ap);
  1176. /*
  1177. * Clear gone flag to prevent allocating new slot again, and
  1178. * set disabled flag because it is not armed yet.
  1179. */
  1180. ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
  1181. | KPROBE_FLAG_DISABLED;
  1182. }
  1183. /* Copy ap's insn slot to p */
  1184. copy_kprobe(ap, p);
  1185. ret = add_new_kprobe(ap, p);
  1186. out:
  1187. mutex_unlock(&text_mutex);
  1188. jump_label_unlock();
  1189. cpus_read_unlock();
  1190. if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
  1191. ap->flags &= ~KPROBE_FLAG_DISABLED;
  1192. if (!kprobes_all_disarmed) {
  1193. /* Arm the breakpoint again. */
  1194. ret = arm_kprobe(ap);
  1195. if (ret) {
  1196. ap->flags |= KPROBE_FLAG_DISABLED;
  1197. list_del_rcu(&p->list);
  1198. synchronize_sched();
  1199. }
  1200. }
  1201. }
  1202. return ret;
  1203. }
  1204. bool __weak arch_within_kprobe_blacklist(unsigned long addr)
  1205. {
  1206. /* The __kprobes marked functions and entry code must not be probed */
  1207. return addr >= (unsigned long)__kprobes_text_start &&
  1208. addr < (unsigned long)__kprobes_text_end;
  1209. }
  1210. bool within_kprobe_blacklist(unsigned long addr)
  1211. {
  1212. struct kprobe_blacklist_entry *ent;
  1213. if (arch_within_kprobe_blacklist(addr))
  1214. return true;
  1215. /*
  1216. * If there exists a kprobe_blacklist, verify and
  1217. * fail any probe registration in the prohibited area
  1218. */
  1219. list_for_each_entry(ent, &kprobe_blacklist, list) {
  1220. if (addr >= ent->start_addr && addr < ent->end_addr)
  1221. return true;
  1222. }
  1223. return false;
  1224. }
  1225. /*
  1226. * If we have a symbol_name argument, look it up and add the offset field
  1227. * to it. This way, we can specify a relative address to a symbol.
  1228. * This returns encoded errors if it fails to look up symbol or invalid
  1229. * combination of parameters.
  1230. */
  1231. static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
  1232. const char *symbol_name, unsigned int offset)
  1233. {
  1234. if ((symbol_name && addr) || (!symbol_name && !addr))
  1235. goto invalid;
  1236. if (symbol_name) {
  1237. addr = kprobe_lookup_name(symbol_name, offset);
  1238. if (!addr)
  1239. return ERR_PTR(-ENOENT);
  1240. }
  1241. addr = (kprobe_opcode_t *)(((char *)addr) + offset);
  1242. if (addr)
  1243. return addr;
  1244. invalid:
  1245. return ERR_PTR(-EINVAL);
  1246. }
  1247. static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
  1248. {
  1249. return _kprobe_addr(p->addr, p->symbol_name, p->offset);
  1250. }
  1251. /* Check passed kprobe is valid and return kprobe in kprobe_table. */
  1252. static struct kprobe *__get_valid_kprobe(struct kprobe *p)
  1253. {
  1254. struct kprobe *ap, *list_p;
  1255. ap = get_kprobe(p->addr);
  1256. if (unlikely(!ap))
  1257. return NULL;
  1258. if (p != ap) {
  1259. list_for_each_entry_rcu(list_p, &ap->list, list)
  1260. if (list_p == p)
  1261. /* kprobe p is a valid probe */
  1262. goto valid;
  1263. return NULL;
  1264. }
  1265. valid:
  1266. return ap;
  1267. }
  1268. /* Return error if the kprobe is being re-registered */
  1269. static inline int check_kprobe_rereg(struct kprobe *p)
  1270. {
  1271. int ret = 0;
  1272. mutex_lock(&kprobe_mutex);
  1273. if (__get_valid_kprobe(p))
  1274. ret = -EINVAL;
  1275. mutex_unlock(&kprobe_mutex);
  1276. return ret;
  1277. }
  1278. int __weak arch_check_ftrace_location(struct kprobe *p)
  1279. {
  1280. unsigned long ftrace_addr;
  1281. ftrace_addr = ftrace_location((unsigned long)p->addr);
  1282. if (ftrace_addr) {
  1283. #ifdef CONFIG_KPROBES_ON_FTRACE
  1284. /* Given address is not on the instruction boundary */
  1285. if ((unsigned long)p->addr != ftrace_addr)
  1286. return -EILSEQ;
  1287. p->flags |= KPROBE_FLAG_FTRACE;
  1288. #else /* !CONFIG_KPROBES_ON_FTRACE */
  1289. return -EINVAL;
  1290. #endif
  1291. }
  1292. return 0;
  1293. }
  1294. static int check_kprobe_address_safe(struct kprobe *p,
  1295. struct module **probed_mod)
  1296. {
  1297. int ret;
  1298. ret = arch_check_ftrace_location(p);
  1299. if (ret)
  1300. return ret;
  1301. jump_label_lock();
  1302. preempt_disable();
  1303. /* Ensure it is not in reserved area nor out of text */
  1304. if (!kernel_text_address((unsigned long) p->addr) ||
  1305. within_kprobe_blacklist((unsigned long) p->addr) ||
  1306. jump_label_text_reserved(p->addr, p->addr)) {
  1307. ret = -EINVAL;
  1308. goto out;
  1309. }
  1310. /* Check if are we probing a module */
  1311. *probed_mod = __module_text_address((unsigned long) p->addr);
  1312. if (*probed_mod) {
  1313. /*
  1314. * We must hold a refcount of the probed module while updating
  1315. * its code to prohibit unexpected unloading.
  1316. */
  1317. if (unlikely(!try_module_get(*probed_mod))) {
  1318. ret = -ENOENT;
  1319. goto out;
  1320. }
  1321. /*
  1322. * If the module freed .init.text, we couldn't insert
  1323. * kprobes in there.
  1324. */
  1325. if (within_module_init((unsigned long)p->addr, *probed_mod) &&
  1326. (*probed_mod)->state != MODULE_STATE_COMING) {
  1327. module_put(*probed_mod);
  1328. *probed_mod = NULL;
  1329. ret = -ENOENT;
  1330. }
  1331. }
  1332. out:
  1333. preempt_enable();
  1334. jump_label_unlock();
  1335. return ret;
  1336. }
  1337. int register_kprobe(struct kprobe *p)
  1338. {
  1339. int ret;
  1340. struct kprobe *old_p;
  1341. struct module *probed_mod;
  1342. kprobe_opcode_t *addr;
  1343. /* Adjust probe address from symbol */
  1344. addr = kprobe_addr(p);
  1345. if (IS_ERR(addr))
  1346. return PTR_ERR(addr);
  1347. p->addr = addr;
  1348. ret = check_kprobe_rereg(p);
  1349. if (ret)
  1350. return ret;
  1351. /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
  1352. p->flags &= KPROBE_FLAG_DISABLED;
  1353. p->nmissed = 0;
  1354. INIT_LIST_HEAD(&p->list);
  1355. ret = check_kprobe_address_safe(p, &probed_mod);
  1356. if (ret)
  1357. return ret;
  1358. mutex_lock(&kprobe_mutex);
  1359. old_p = get_kprobe(p->addr);
  1360. if (old_p) {
  1361. /* Since this may unoptimize old_p, locking text_mutex. */
  1362. ret = register_aggr_kprobe(old_p, p);
  1363. goto out;
  1364. }
  1365. cpus_read_lock();
  1366. /* Prevent text modification */
  1367. mutex_lock(&text_mutex);
  1368. ret = prepare_kprobe(p);
  1369. mutex_unlock(&text_mutex);
  1370. cpus_read_unlock();
  1371. if (ret)
  1372. goto out;
  1373. INIT_HLIST_NODE(&p->hlist);
  1374. hlist_add_head_rcu(&p->hlist,
  1375. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  1376. if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
  1377. ret = arm_kprobe(p);
  1378. if (ret) {
  1379. hlist_del_rcu(&p->hlist);
  1380. synchronize_sched();
  1381. goto out;
  1382. }
  1383. }
  1384. /* Try to optimize kprobe */
  1385. try_to_optimize_kprobe(p);
  1386. out:
  1387. mutex_unlock(&kprobe_mutex);
  1388. if (probed_mod)
  1389. module_put(probed_mod);
  1390. return ret;
  1391. }
  1392. EXPORT_SYMBOL_GPL(register_kprobe);
  1393. /* Check if all probes on the aggrprobe are disabled */
  1394. static int aggr_kprobe_disabled(struct kprobe *ap)
  1395. {
  1396. struct kprobe *kp;
  1397. list_for_each_entry_rcu(kp, &ap->list, list)
  1398. if (!kprobe_disabled(kp))
  1399. /*
  1400. * There is an active probe on the list.
  1401. * We can't disable this ap.
  1402. */
  1403. return 0;
  1404. return 1;
  1405. }
  1406. /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
  1407. static struct kprobe *__disable_kprobe(struct kprobe *p)
  1408. {
  1409. struct kprobe *orig_p;
  1410. int ret;
  1411. /* Get an original kprobe for return */
  1412. orig_p = __get_valid_kprobe(p);
  1413. if (unlikely(orig_p == NULL))
  1414. return ERR_PTR(-EINVAL);
  1415. if (!kprobe_disabled(p)) {
  1416. /* Disable probe if it is a child probe */
  1417. if (p != orig_p)
  1418. p->flags |= KPROBE_FLAG_DISABLED;
  1419. /* Try to disarm and disable this/parent probe */
  1420. if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
  1421. /*
  1422. * If kprobes_all_disarmed is set, orig_p
  1423. * should have already been disarmed, so
  1424. * skip unneed disarming process.
  1425. */
  1426. if (!kprobes_all_disarmed) {
  1427. ret = disarm_kprobe(orig_p, true);
  1428. if (ret) {
  1429. p->flags &= ~KPROBE_FLAG_DISABLED;
  1430. return ERR_PTR(ret);
  1431. }
  1432. }
  1433. orig_p->flags |= KPROBE_FLAG_DISABLED;
  1434. }
  1435. }
  1436. return orig_p;
  1437. }
  1438. /*
  1439. * Unregister a kprobe without a scheduler synchronization.
  1440. */
  1441. static int __unregister_kprobe_top(struct kprobe *p)
  1442. {
  1443. struct kprobe *ap, *list_p;
  1444. /* Disable kprobe. This will disarm it if needed. */
  1445. ap = __disable_kprobe(p);
  1446. if (IS_ERR(ap))
  1447. return PTR_ERR(ap);
  1448. if (ap == p)
  1449. /*
  1450. * This probe is an independent(and non-optimized) kprobe
  1451. * (not an aggrprobe). Remove from the hash list.
  1452. */
  1453. goto disarmed;
  1454. /* Following process expects this probe is an aggrprobe */
  1455. WARN_ON(!kprobe_aggrprobe(ap));
  1456. if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
  1457. /*
  1458. * !disarmed could be happen if the probe is under delayed
  1459. * unoptimizing.
  1460. */
  1461. goto disarmed;
  1462. else {
  1463. /* If disabling probe has special handlers, update aggrprobe */
  1464. if (p->break_handler && !kprobe_gone(p))
  1465. ap->break_handler = NULL;
  1466. if (p->post_handler && !kprobe_gone(p)) {
  1467. list_for_each_entry_rcu(list_p, &ap->list, list) {
  1468. if ((list_p != p) && (list_p->post_handler))
  1469. goto noclean;
  1470. }
  1471. ap->post_handler = NULL;
  1472. }
  1473. noclean:
  1474. /*
  1475. * Remove from the aggrprobe: this path will do nothing in
  1476. * __unregister_kprobe_bottom().
  1477. */
  1478. list_del_rcu(&p->list);
  1479. if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
  1480. /*
  1481. * Try to optimize this probe again, because post
  1482. * handler may have been changed.
  1483. */
  1484. optimize_kprobe(ap);
  1485. }
  1486. return 0;
  1487. disarmed:
  1488. BUG_ON(!kprobe_disarmed(ap));
  1489. hlist_del_rcu(&ap->hlist);
  1490. return 0;
  1491. }
  1492. static void __unregister_kprobe_bottom(struct kprobe *p)
  1493. {
  1494. struct kprobe *ap;
  1495. if (list_empty(&p->list))
  1496. /* This is an independent kprobe */
  1497. arch_remove_kprobe(p);
  1498. else if (list_is_singular(&p->list)) {
  1499. /* This is the last child of an aggrprobe */
  1500. ap = list_entry(p->list.next, struct kprobe, list);
  1501. list_del(&p->list);
  1502. free_aggr_kprobe(ap);
  1503. }
  1504. /* Otherwise, do nothing. */
  1505. }
  1506. int register_kprobes(struct kprobe **kps, int num)
  1507. {
  1508. int i, ret = 0;
  1509. if (num <= 0)
  1510. return -EINVAL;
  1511. for (i = 0; i < num; i++) {
  1512. ret = register_kprobe(kps[i]);
  1513. if (ret < 0) {
  1514. if (i > 0)
  1515. unregister_kprobes(kps, i);
  1516. break;
  1517. }
  1518. }
  1519. return ret;
  1520. }
  1521. EXPORT_SYMBOL_GPL(register_kprobes);
  1522. void unregister_kprobe(struct kprobe *p)
  1523. {
  1524. unregister_kprobes(&p, 1);
  1525. }
  1526. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1527. void unregister_kprobes(struct kprobe **kps, int num)
  1528. {
  1529. int i;
  1530. if (num <= 0)
  1531. return;
  1532. mutex_lock(&kprobe_mutex);
  1533. for (i = 0; i < num; i++)
  1534. if (__unregister_kprobe_top(kps[i]) < 0)
  1535. kps[i]->addr = NULL;
  1536. mutex_unlock(&kprobe_mutex);
  1537. synchronize_sched();
  1538. for (i = 0; i < num; i++)
  1539. if (kps[i]->addr)
  1540. __unregister_kprobe_bottom(kps[i]);
  1541. }
  1542. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1543. int __weak kprobe_exceptions_notify(struct notifier_block *self,
  1544. unsigned long val, void *data)
  1545. {
  1546. return NOTIFY_DONE;
  1547. }
  1548. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  1549. static struct notifier_block kprobe_exceptions_nb = {
  1550. .notifier_call = kprobe_exceptions_notify,
  1551. .priority = 0x7fffffff /* we need to be notified first */
  1552. };
  1553. unsigned long __weak arch_deref_entry_point(void *entry)
  1554. {
  1555. return (unsigned long)entry;
  1556. }
  1557. #if 0
  1558. int register_jprobes(struct jprobe **jps, int num)
  1559. {
  1560. int ret = 0, i;
  1561. if (num <= 0)
  1562. return -EINVAL;
  1563. for (i = 0; i < num; i++) {
  1564. ret = register_jprobe(jps[i]);
  1565. if (ret < 0) {
  1566. if (i > 0)
  1567. unregister_jprobes(jps, i);
  1568. break;
  1569. }
  1570. }
  1571. return ret;
  1572. }
  1573. EXPORT_SYMBOL_GPL(register_jprobes);
  1574. int register_jprobe(struct jprobe *jp)
  1575. {
  1576. unsigned long addr, offset;
  1577. struct kprobe *kp = &jp->kp;
  1578. /*
  1579. * Verify probepoint as well as the jprobe handler are
  1580. * valid function entry points.
  1581. */
  1582. addr = arch_deref_entry_point(jp->entry);
  1583. if (kallsyms_lookup_size_offset(addr, NULL, &offset) && offset == 0 &&
  1584. kprobe_on_func_entry(kp->addr, kp->symbol_name, kp->offset)) {
  1585. kp->pre_handler = setjmp_pre_handler;
  1586. kp->break_handler = longjmp_break_handler;
  1587. return register_kprobe(kp);
  1588. }
  1589. return -EINVAL;
  1590. }
  1591. EXPORT_SYMBOL_GPL(register_jprobe);
  1592. void unregister_jprobe(struct jprobe *jp)
  1593. {
  1594. unregister_jprobes(&jp, 1);
  1595. }
  1596. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1597. void unregister_jprobes(struct jprobe **jps, int num)
  1598. {
  1599. int i;
  1600. if (num <= 0)
  1601. return;
  1602. mutex_lock(&kprobe_mutex);
  1603. for (i = 0; i < num; i++)
  1604. if (__unregister_kprobe_top(&jps[i]->kp) < 0)
  1605. jps[i]->kp.addr = NULL;
  1606. mutex_unlock(&kprobe_mutex);
  1607. synchronize_sched();
  1608. for (i = 0; i < num; i++) {
  1609. if (jps[i]->kp.addr)
  1610. __unregister_kprobe_bottom(&jps[i]->kp);
  1611. }
  1612. }
  1613. EXPORT_SYMBOL_GPL(unregister_jprobes);
  1614. #endif
  1615. #ifdef CONFIG_KRETPROBES
  1616. /*
  1617. * This kprobe pre_handler is registered with every kretprobe. When probe
  1618. * hits it will set up the return probe.
  1619. */
  1620. static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
  1621. {
  1622. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  1623. unsigned long hash, flags = 0;
  1624. struct kretprobe_instance *ri;
  1625. /*
  1626. * To avoid deadlocks, prohibit return probing in NMI contexts,
  1627. * just skip the probe and increase the (inexact) 'nmissed'
  1628. * statistical counter, so that the user is informed that
  1629. * something happened:
  1630. */
  1631. if (unlikely(in_nmi())) {
  1632. rp->nmissed++;
  1633. return 0;
  1634. }
  1635. /* TODO: consider to only swap the RA after the last pre_handler fired */
  1636. hash = hash_ptr(current, KPROBE_HASH_BITS);
  1637. raw_spin_lock_irqsave(&rp->lock, flags);
  1638. if (!hlist_empty(&rp->free_instances)) {
  1639. ri = hlist_entry(rp->free_instances.first,
  1640. struct kretprobe_instance, hlist);
  1641. hlist_del(&ri->hlist);
  1642. raw_spin_unlock_irqrestore(&rp->lock, flags);
  1643. ri->rp = rp;
  1644. ri->task = current;
  1645. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  1646. raw_spin_lock_irqsave(&rp->lock, flags);
  1647. hlist_add_head(&ri->hlist, &rp->free_instances);
  1648. raw_spin_unlock_irqrestore(&rp->lock, flags);
  1649. return 0;
  1650. }
  1651. arch_prepare_kretprobe(ri, regs);
  1652. /* XXX(hch): why is there no hlist_move_head? */
  1653. INIT_HLIST_NODE(&ri->hlist);
  1654. kretprobe_table_lock(hash, &flags);
  1655. hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
  1656. kretprobe_table_unlock(hash, &flags);
  1657. } else {
  1658. rp->nmissed++;
  1659. raw_spin_unlock_irqrestore(&rp->lock, flags);
  1660. }
  1661. return 0;
  1662. }
  1663. NOKPROBE_SYMBOL(pre_handler_kretprobe);
  1664. bool __weak arch_kprobe_on_func_entry(unsigned long offset)
  1665. {
  1666. return !offset;
  1667. }
  1668. bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
  1669. {
  1670. kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
  1671. if (IS_ERR(kp_addr))
  1672. return false;
  1673. if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) ||
  1674. !arch_kprobe_on_func_entry(offset))
  1675. return false;
  1676. return true;
  1677. }
  1678. int register_kretprobe(struct kretprobe *rp)
  1679. {
  1680. int ret = 0;
  1681. struct kretprobe_instance *inst;
  1682. int i;
  1683. void *addr;
  1684. if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset))
  1685. return -EINVAL;
  1686. if (kretprobe_blacklist_size) {
  1687. addr = kprobe_addr(&rp->kp);
  1688. if (IS_ERR(addr))
  1689. return PTR_ERR(addr);
  1690. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1691. if (kretprobe_blacklist[i].addr == addr)
  1692. return -EINVAL;
  1693. }
  1694. }
  1695. rp->kp.pre_handler = pre_handler_kretprobe;
  1696. rp->kp.post_handler = NULL;
  1697. rp->kp.fault_handler = NULL;
  1698. rp->kp.break_handler = NULL;
  1699. /* Pre-allocate memory for max kretprobe instances */
  1700. if (rp->maxactive <= 0) {
  1701. #ifdef CONFIG_PREEMPT
  1702. rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
  1703. #else
  1704. rp->maxactive = num_possible_cpus();
  1705. #endif
  1706. }
  1707. raw_spin_lock_init(&rp->lock);
  1708. INIT_HLIST_HEAD(&rp->free_instances);
  1709. for (i = 0; i < rp->maxactive; i++) {
  1710. inst = kmalloc(sizeof(struct kretprobe_instance) +
  1711. rp->data_size, GFP_KERNEL);
  1712. if (inst == NULL) {
  1713. free_rp_inst(rp);
  1714. return -ENOMEM;
  1715. }
  1716. INIT_HLIST_NODE(&inst->hlist);
  1717. hlist_add_head(&inst->hlist, &rp->free_instances);
  1718. }
  1719. rp->nmissed = 0;
  1720. /* Establish function entry probe point */
  1721. ret = register_kprobe(&rp->kp);
  1722. if (ret != 0)
  1723. free_rp_inst(rp);
  1724. return ret;
  1725. }
  1726. EXPORT_SYMBOL_GPL(register_kretprobe);
  1727. int register_kretprobes(struct kretprobe **rps, int num)
  1728. {
  1729. int ret = 0, i;
  1730. if (num <= 0)
  1731. return -EINVAL;
  1732. for (i = 0; i < num; i++) {
  1733. ret = register_kretprobe(rps[i]);
  1734. if (ret < 0) {
  1735. if (i > 0)
  1736. unregister_kretprobes(rps, i);
  1737. break;
  1738. }
  1739. }
  1740. return ret;
  1741. }
  1742. EXPORT_SYMBOL_GPL(register_kretprobes);
  1743. void unregister_kretprobe(struct kretprobe *rp)
  1744. {
  1745. unregister_kretprobes(&rp, 1);
  1746. }
  1747. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1748. void unregister_kretprobes(struct kretprobe **rps, int num)
  1749. {
  1750. int i;
  1751. if (num <= 0)
  1752. return;
  1753. mutex_lock(&kprobe_mutex);
  1754. for (i = 0; i < num; i++)
  1755. if (__unregister_kprobe_top(&rps[i]->kp) < 0)
  1756. rps[i]->kp.addr = NULL;
  1757. mutex_unlock(&kprobe_mutex);
  1758. synchronize_sched();
  1759. for (i = 0; i < num; i++) {
  1760. if (rps[i]->kp.addr) {
  1761. __unregister_kprobe_bottom(&rps[i]->kp);
  1762. cleanup_rp_inst(rps[i]);
  1763. }
  1764. }
  1765. }
  1766. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1767. #else /* CONFIG_KRETPROBES */
  1768. int register_kretprobe(struct kretprobe *rp)
  1769. {
  1770. return -ENOSYS;
  1771. }
  1772. EXPORT_SYMBOL_GPL(register_kretprobe);
  1773. int register_kretprobes(struct kretprobe **rps, int num)
  1774. {
  1775. return -ENOSYS;
  1776. }
  1777. EXPORT_SYMBOL_GPL(register_kretprobes);
  1778. void unregister_kretprobe(struct kretprobe *rp)
  1779. {
  1780. }
  1781. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1782. void unregister_kretprobes(struct kretprobe **rps, int num)
  1783. {
  1784. }
  1785. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1786. static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
  1787. {
  1788. return 0;
  1789. }
  1790. NOKPROBE_SYMBOL(pre_handler_kretprobe);
  1791. #endif /* CONFIG_KRETPROBES */
  1792. /* Set the kprobe gone and remove its instruction buffer. */
  1793. static void kill_kprobe(struct kprobe *p)
  1794. {
  1795. struct kprobe *kp;
  1796. p->flags |= KPROBE_FLAG_GONE;
  1797. if (kprobe_aggrprobe(p)) {
  1798. /*
  1799. * If this is an aggr_kprobe, we have to list all the
  1800. * chained probes and mark them GONE.
  1801. */
  1802. list_for_each_entry_rcu(kp, &p->list, list)
  1803. kp->flags |= KPROBE_FLAG_GONE;
  1804. p->post_handler = NULL;
  1805. p->break_handler = NULL;
  1806. kill_optimized_kprobe(p);
  1807. }
  1808. /*
  1809. * Here, we can remove insn_slot safely, because no thread calls
  1810. * the original probed function (which will be freed soon) any more.
  1811. */
  1812. arch_remove_kprobe(p);
  1813. }
  1814. /* Disable one kprobe */
  1815. int disable_kprobe(struct kprobe *kp)
  1816. {
  1817. int ret = 0;
  1818. struct kprobe *p;
  1819. mutex_lock(&kprobe_mutex);
  1820. /* Disable this kprobe */
  1821. p = __disable_kprobe(kp);
  1822. if (IS_ERR(p))
  1823. ret = PTR_ERR(p);
  1824. mutex_unlock(&kprobe_mutex);
  1825. return ret;
  1826. }
  1827. EXPORT_SYMBOL_GPL(disable_kprobe);
  1828. /* Enable one kprobe */
  1829. int enable_kprobe(struct kprobe *kp)
  1830. {
  1831. int ret = 0;
  1832. struct kprobe *p;
  1833. mutex_lock(&kprobe_mutex);
  1834. /* Check whether specified probe is valid. */
  1835. p = __get_valid_kprobe(kp);
  1836. if (unlikely(p == NULL)) {
  1837. ret = -EINVAL;
  1838. goto out;
  1839. }
  1840. if (kprobe_gone(kp)) {
  1841. /* This kprobe has gone, we couldn't enable it. */
  1842. ret = -EINVAL;
  1843. goto out;
  1844. }
  1845. if (p != kp)
  1846. kp->flags &= ~KPROBE_FLAG_DISABLED;
  1847. if (!kprobes_all_disarmed && kprobe_disabled(p)) {
  1848. p->flags &= ~KPROBE_FLAG_DISABLED;
  1849. ret = arm_kprobe(p);
  1850. if (ret)
  1851. p->flags |= KPROBE_FLAG_DISABLED;
  1852. }
  1853. out:
  1854. mutex_unlock(&kprobe_mutex);
  1855. return ret;
  1856. }
  1857. EXPORT_SYMBOL_GPL(enable_kprobe);
  1858. void dump_kprobe(struct kprobe *kp)
  1859. {
  1860. printk(KERN_WARNING "Dumping kprobe:\n");
  1861. printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
  1862. kp->symbol_name, kp->addr, kp->offset);
  1863. }
  1864. NOKPROBE_SYMBOL(dump_kprobe);
  1865. /*
  1866. * Lookup and populate the kprobe_blacklist.
  1867. *
  1868. * Unlike the kretprobe blacklist, we'll need to determine
  1869. * the range of addresses that belong to the said functions,
  1870. * since a kprobe need not necessarily be at the beginning
  1871. * of a function.
  1872. */
  1873. static int __init populate_kprobe_blacklist(unsigned long *start,
  1874. unsigned long *end)
  1875. {
  1876. unsigned long *iter;
  1877. struct kprobe_blacklist_entry *ent;
  1878. unsigned long entry, offset = 0, size = 0;
  1879. for (iter = start; iter < end; iter++) {
  1880. entry = arch_deref_entry_point((void *)*iter);
  1881. if (!kernel_text_address(entry) ||
  1882. !kallsyms_lookup_size_offset(entry, &size, &offset)) {
  1883. pr_err("Failed to find blacklist at %p\n",
  1884. (void *)entry);
  1885. continue;
  1886. }
  1887. ent = kmalloc(sizeof(*ent), GFP_KERNEL);
  1888. if (!ent)
  1889. return -ENOMEM;
  1890. ent->start_addr = entry;
  1891. ent->end_addr = entry + size;
  1892. INIT_LIST_HEAD(&ent->list);
  1893. list_add_tail(&ent->list, &kprobe_blacklist);
  1894. }
  1895. return 0;
  1896. }
  1897. /* Module notifier call back, checking kprobes on the module */
  1898. static int kprobes_module_callback(struct notifier_block *nb,
  1899. unsigned long val, void *data)
  1900. {
  1901. struct module *mod = data;
  1902. struct hlist_head *head;
  1903. struct kprobe *p;
  1904. unsigned int i;
  1905. int checkcore = (val == MODULE_STATE_GOING);
  1906. if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
  1907. return NOTIFY_DONE;
  1908. /*
  1909. * When MODULE_STATE_GOING was notified, both of module .text and
  1910. * .init.text sections would be freed. When MODULE_STATE_LIVE was
  1911. * notified, only .init.text section would be freed. We need to
  1912. * disable kprobes which have been inserted in the sections.
  1913. */
  1914. mutex_lock(&kprobe_mutex);
  1915. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1916. head = &kprobe_table[i];
  1917. hlist_for_each_entry_rcu(p, head, hlist)
  1918. if (within_module_init((unsigned long)p->addr, mod) ||
  1919. (checkcore &&
  1920. within_module_core((unsigned long)p->addr, mod))) {
  1921. /*
  1922. * The vaddr this probe is installed will soon
  1923. * be vfreed buy not synced to disk. Hence,
  1924. * disarming the breakpoint isn't needed.
  1925. *
  1926. * Note, this will also move any optimized probes
  1927. * that are pending to be removed from their
  1928. * corresponding lists to the freeing_list and
  1929. * will not be touched by the delayed
  1930. * kprobe_optimizer work handler.
  1931. */
  1932. kill_kprobe(p);
  1933. }
  1934. }
  1935. mutex_unlock(&kprobe_mutex);
  1936. return NOTIFY_DONE;
  1937. }
  1938. static struct notifier_block kprobe_module_nb = {
  1939. .notifier_call = kprobes_module_callback,
  1940. .priority = 0
  1941. };
  1942. /* Markers of _kprobe_blacklist section */
  1943. extern unsigned long __start_kprobe_blacklist[];
  1944. extern unsigned long __stop_kprobe_blacklist[];
  1945. static int __init init_kprobes(void)
  1946. {
  1947. int i, err = 0;
  1948. /* FIXME allocate the probe table, currently defined statically */
  1949. /* initialize all list heads */
  1950. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1951. INIT_HLIST_HEAD(&kprobe_table[i]);
  1952. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  1953. raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
  1954. }
  1955. err = populate_kprobe_blacklist(__start_kprobe_blacklist,
  1956. __stop_kprobe_blacklist);
  1957. if (err) {
  1958. pr_err("kprobes: failed to populate blacklist: %d\n", err);
  1959. pr_err("Please take care of using kprobes.\n");
  1960. }
  1961. if (kretprobe_blacklist_size) {
  1962. /* lookup the function address from its name */
  1963. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1964. kretprobe_blacklist[i].addr =
  1965. kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
  1966. if (!kretprobe_blacklist[i].addr)
  1967. printk("kretprobe: lookup failed: %s\n",
  1968. kretprobe_blacklist[i].name);
  1969. }
  1970. }
  1971. #if defined(CONFIG_OPTPROBES)
  1972. #if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
  1973. /* Init kprobe_optinsn_slots */
  1974. kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
  1975. #endif
  1976. /* By default, kprobes can be optimized */
  1977. kprobes_allow_optimization = true;
  1978. #endif
  1979. /* By default, kprobes are armed */
  1980. kprobes_all_disarmed = false;
  1981. err = arch_init_kprobes();
  1982. if (!err)
  1983. err = register_die_notifier(&kprobe_exceptions_nb);
  1984. if (!err)
  1985. err = register_module_notifier(&kprobe_module_nb);
  1986. kprobes_initialized = (err == 0);
  1987. if (!err)
  1988. init_test_probes();
  1989. return err;
  1990. }
  1991. #ifdef CONFIG_DEBUG_FS
  1992. static void report_probe(struct seq_file *pi, struct kprobe *p,
  1993. const char *sym, int offset, char *modname, struct kprobe *pp)
  1994. {
  1995. char *kprobe_type;
  1996. if (p->pre_handler == pre_handler_kretprobe)
  1997. kprobe_type = "r";
  1998. else if (p->pre_handler == setjmp_pre_handler)
  1999. kprobe_type = "j";
  2000. else
  2001. kprobe_type = "k";
  2002. if (sym)
  2003. seq_printf(pi, "%p %s %s+0x%x %s ",
  2004. p->addr, kprobe_type, sym, offset,
  2005. (modname ? modname : " "));
  2006. else
  2007. seq_printf(pi, "%p %s %p ",
  2008. p->addr, kprobe_type, p->addr);
  2009. if (!pp)
  2010. pp = p;
  2011. seq_printf(pi, "%s%s%s%s\n",
  2012. (kprobe_gone(p) ? "[GONE]" : ""),
  2013. ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
  2014. (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
  2015. (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
  2016. }
  2017. static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  2018. {
  2019. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  2020. }
  2021. static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  2022. {
  2023. (*pos)++;
  2024. if (*pos >= KPROBE_TABLE_SIZE)
  2025. return NULL;
  2026. return pos;
  2027. }
  2028. static void kprobe_seq_stop(struct seq_file *f, void *v)
  2029. {
  2030. /* Nothing to do */
  2031. }
  2032. static int show_kprobe_addr(struct seq_file *pi, void *v)
  2033. {
  2034. struct hlist_head *head;
  2035. struct kprobe *p, *kp;
  2036. const char *sym = NULL;
  2037. unsigned int i = *(loff_t *) v;
  2038. unsigned long offset = 0;
  2039. char *modname, namebuf[KSYM_NAME_LEN];
  2040. head = &kprobe_table[i];
  2041. preempt_disable();
  2042. hlist_for_each_entry_rcu(p, head, hlist) {
  2043. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  2044. &offset, &modname, namebuf);
  2045. if (kprobe_aggrprobe(p)) {
  2046. list_for_each_entry_rcu(kp, &p->list, list)
  2047. report_probe(pi, kp, sym, offset, modname, p);
  2048. } else
  2049. report_probe(pi, p, sym, offset, modname, NULL);
  2050. }
  2051. preempt_enable();
  2052. return 0;
  2053. }
  2054. static const struct seq_operations kprobes_seq_ops = {
  2055. .start = kprobe_seq_start,
  2056. .next = kprobe_seq_next,
  2057. .stop = kprobe_seq_stop,
  2058. .show = show_kprobe_addr
  2059. };
  2060. static int kprobes_open(struct inode *inode, struct file *filp)
  2061. {
  2062. return seq_open(filp, &kprobes_seq_ops);
  2063. }
  2064. static const struct file_operations debugfs_kprobes_operations = {
  2065. .open = kprobes_open,
  2066. .read = seq_read,
  2067. .llseek = seq_lseek,
  2068. .release = seq_release,
  2069. };
  2070. /* kprobes/blacklist -- shows which functions can not be probed */
  2071. static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
  2072. {
  2073. return seq_list_start(&kprobe_blacklist, *pos);
  2074. }
  2075. static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
  2076. {
  2077. return seq_list_next(v, &kprobe_blacklist, pos);
  2078. }
  2079. static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
  2080. {
  2081. struct kprobe_blacklist_entry *ent =
  2082. list_entry(v, struct kprobe_blacklist_entry, list);
  2083. seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
  2084. (void *)ent->end_addr, (void *)ent->start_addr);
  2085. return 0;
  2086. }
  2087. static const struct seq_operations kprobe_blacklist_seq_ops = {
  2088. .start = kprobe_blacklist_seq_start,
  2089. .next = kprobe_blacklist_seq_next,
  2090. .stop = kprobe_seq_stop, /* Reuse void function */
  2091. .show = kprobe_blacklist_seq_show,
  2092. };
  2093. static int kprobe_blacklist_open(struct inode *inode, struct file *filp)
  2094. {
  2095. return seq_open(filp, &kprobe_blacklist_seq_ops);
  2096. }
  2097. static const struct file_operations debugfs_kprobe_blacklist_ops = {
  2098. .open = kprobe_blacklist_open,
  2099. .read = seq_read,
  2100. .llseek = seq_lseek,
  2101. .release = seq_release,
  2102. };
  2103. static int arm_all_kprobes(void)
  2104. {
  2105. struct hlist_head *head;
  2106. struct kprobe *p;
  2107. unsigned int i, total = 0, errors = 0;
  2108. int err, ret = 0;
  2109. mutex_lock(&kprobe_mutex);
  2110. /* If kprobes are armed, just return */
  2111. if (!kprobes_all_disarmed)
  2112. goto already_enabled;
  2113. /*
  2114. * optimize_kprobe() called by arm_kprobe() checks
  2115. * kprobes_all_disarmed, so set kprobes_all_disarmed before
  2116. * arm_kprobe.
  2117. */
  2118. kprobes_all_disarmed = false;
  2119. /* Arming kprobes doesn't optimize kprobe itself */
  2120. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  2121. head = &kprobe_table[i];
  2122. /* Arm all kprobes on a best-effort basis */
  2123. hlist_for_each_entry_rcu(p, head, hlist) {
  2124. if (!kprobe_disabled(p)) {
  2125. err = arm_kprobe(p);
  2126. if (err) {
  2127. errors++;
  2128. ret = err;
  2129. }
  2130. total++;
  2131. }
  2132. }
  2133. }
  2134. if (errors)
  2135. pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
  2136. errors, total);
  2137. else
  2138. pr_info("Kprobes globally enabled\n");
  2139. already_enabled:
  2140. mutex_unlock(&kprobe_mutex);
  2141. return ret;
  2142. }
  2143. static int disarm_all_kprobes(void)
  2144. {
  2145. struct hlist_head *head;
  2146. struct kprobe *p;
  2147. unsigned int i, total = 0, errors = 0;
  2148. int err, ret = 0;
  2149. mutex_lock(&kprobe_mutex);
  2150. /* If kprobes are already disarmed, just return */
  2151. if (kprobes_all_disarmed) {
  2152. mutex_unlock(&kprobe_mutex);
  2153. return 0;
  2154. }
  2155. kprobes_all_disarmed = true;
  2156. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  2157. head = &kprobe_table[i];
  2158. /* Disarm all kprobes on a best-effort basis */
  2159. hlist_for_each_entry_rcu(p, head, hlist) {
  2160. if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
  2161. err = disarm_kprobe(p, false);
  2162. if (err) {
  2163. errors++;
  2164. ret = err;
  2165. }
  2166. total++;
  2167. }
  2168. }
  2169. }
  2170. if (errors)
  2171. pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
  2172. errors, total);
  2173. else
  2174. pr_info("Kprobes globally disabled\n");
  2175. mutex_unlock(&kprobe_mutex);
  2176. /* Wait for disarming all kprobes by optimizer */
  2177. wait_for_kprobe_optimizer();
  2178. return ret;
  2179. }
  2180. /*
  2181. * XXX: The debugfs bool file interface doesn't allow for callbacks
  2182. * when the bool state is switched. We can reuse that facility when
  2183. * available
  2184. */
  2185. static ssize_t read_enabled_file_bool(struct file *file,
  2186. char __user *user_buf, size_t count, loff_t *ppos)
  2187. {
  2188. char buf[3];
  2189. if (!kprobes_all_disarmed)
  2190. buf[0] = '1';
  2191. else
  2192. buf[0] = '0';
  2193. buf[1] = '\n';
  2194. buf[2] = 0x00;
  2195. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  2196. }
  2197. static ssize_t write_enabled_file_bool(struct file *file,
  2198. const char __user *user_buf, size_t count, loff_t *ppos)
  2199. {
  2200. char buf[32];
  2201. size_t buf_size;
  2202. int ret = 0;
  2203. buf_size = min(count, (sizeof(buf)-1));
  2204. if (copy_from_user(buf, user_buf, buf_size))
  2205. return -EFAULT;
  2206. buf[buf_size] = '\0';
  2207. switch (buf[0]) {
  2208. case 'y':
  2209. case 'Y':
  2210. case '1':
  2211. ret = arm_all_kprobes();
  2212. break;
  2213. case 'n':
  2214. case 'N':
  2215. case '0':
  2216. ret = disarm_all_kprobes();
  2217. break;
  2218. default:
  2219. return -EINVAL;
  2220. }
  2221. if (ret)
  2222. return ret;
  2223. return count;
  2224. }
  2225. static const struct file_operations fops_kp = {
  2226. .read = read_enabled_file_bool,
  2227. .write = write_enabled_file_bool,
  2228. .llseek = default_llseek,
  2229. };
  2230. static int __init debugfs_kprobe_init(void)
  2231. {
  2232. struct dentry *dir, *file;
  2233. unsigned int value = 1;
  2234. dir = debugfs_create_dir("kprobes", NULL);
  2235. if (!dir)
  2236. return -ENOMEM;
  2237. file = debugfs_create_file("list", 0444, dir, NULL,
  2238. &debugfs_kprobes_operations);
  2239. if (!file)
  2240. goto error;
  2241. file = debugfs_create_file("enabled", 0600, dir,
  2242. &value, &fops_kp);
  2243. if (!file)
  2244. goto error;
  2245. file = debugfs_create_file("blacklist", 0444, dir, NULL,
  2246. &debugfs_kprobe_blacklist_ops);
  2247. if (!file)
  2248. goto error;
  2249. return 0;
  2250. error:
  2251. debugfs_remove(dir);
  2252. return -ENOMEM;
  2253. }
  2254. late_initcall(debugfs_kprobe_init);
  2255. #endif /* CONFIG_DEBUG_FS */
  2256. module_init(init_kprobes);
  2257. /* defined in arch/.../kernel/kprobes.c */
  2258. EXPORT_SYMBOL_GPL(jprobe_return);