kprobes.c 56 KB

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