kprobes.c 59 KB

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