kprobes.c 60 KB

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