kprobes.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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. __get_cpu_var(kprobe_instance) = kp;
  286. }
  287. static inline void reset_kprobe_instance(void)
  288. {
  289. __get_cpu_var(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. /*
  316. * Keep all fields in the kprobe consistent
  317. */
  318. static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
  319. {
  320. memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
  321. memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
  322. }
  323. #ifdef CONFIG_OPTPROBES
  324. /* NOTE: change this value only with kprobe_mutex held */
  325. static bool kprobes_allow_optimization;
  326. /*
  327. * Call all pre_handler on the list, but ignores its return value.
  328. * This must be called from arch-dep optimized caller.
  329. */
  330. void __kprobes opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
  331. {
  332. struct kprobe *kp;
  333. list_for_each_entry_rcu(kp, &p->list, list) {
  334. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  335. set_kprobe_instance(kp);
  336. kp->pre_handler(kp, regs);
  337. }
  338. reset_kprobe_instance();
  339. }
  340. }
  341. /* Return true(!0) if the kprobe is ready for optimization. */
  342. static inline int kprobe_optready(struct kprobe *p)
  343. {
  344. struct optimized_kprobe *op;
  345. if (kprobe_aggrprobe(p)) {
  346. op = container_of(p, struct optimized_kprobe, kp);
  347. return arch_prepared_optinsn(&op->optinsn);
  348. }
  349. return 0;
  350. }
  351. /*
  352. * Return an optimized kprobe whose optimizing code replaces
  353. * instructions including addr (exclude breakpoint).
  354. */
  355. static struct kprobe *__kprobes get_optimized_kprobe(unsigned long addr)
  356. {
  357. int i;
  358. struct kprobe *p = NULL;
  359. struct optimized_kprobe *op;
  360. /* Don't check i == 0, since that is a breakpoint case. */
  361. for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
  362. p = get_kprobe((void *)(addr - i));
  363. if (p && kprobe_optready(p)) {
  364. op = container_of(p, struct optimized_kprobe, kp);
  365. if (arch_within_optimized_kprobe(op, addr))
  366. return p;
  367. }
  368. return NULL;
  369. }
  370. /* Optimization staging list, protected by kprobe_mutex */
  371. static LIST_HEAD(optimizing_list);
  372. static void kprobe_optimizer(struct work_struct *work);
  373. static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
  374. #define OPTIMIZE_DELAY 5
  375. /*
  376. * Optimize (replace a breakpoint with a jump) kprobes listed on
  377. * optimizing_list.
  378. */
  379. static __kprobes void do_optimize_kprobes(void)
  380. {
  381. struct optimized_kprobe *op, *tmp;
  382. /*
  383. * The optimization/unoptimization refers online_cpus via
  384. * stop_machine() and cpu-hotplug modifies online_cpus.
  385. * And same time, text_mutex will be held in cpu-hotplug and here.
  386. * This combination can cause a deadlock (cpu-hotplug try to lock
  387. * text_mutex but stop_machine can not be done because online_cpus
  388. * has been changed)
  389. * To avoid this deadlock, we need to call get_online_cpus()
  390. * for preventing cpu-hotplug outside of text_mutex locking.
  391. */
  392. get_online_cpus();
  393. mutex_lock(&text_mutex);
  394. list_for_each_entry_safe(op, tmp, &optimizing_list, list) {
  395. WARN_ON(kprobe_disabled(&op->kp));
  396. if (arch_optimize_kprobe(op) < 0)
  397. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  398. list_del_init(&op->list);
  399. }
  400. mutex_unlock(&text_mutex);
  401. put_online_cpus();
  402. }
  403. /* Kprobe jump optimizer */
  404. static __kprobes void kprobe_optimizer(struct work_struct *work)
  405. {
  406. /* Lock modules while optimizing kprobes */
  407. mutex_lock(&module_mutex);
  408. mutex_lock(&kprobe_mutex);
  409. if (kprobes_all_disarmed || !kprobes_allow_optimization)
  410. goto end;
  411. /*
  412. * Wait for quiesence period to ensure all running interrupts
  413. * are done. Because optprobe may modify multiple instructions
  414. * there is a chance that Nth instruction is interrupted. In that
  415. * case, running interrupt can return to 2nd-Nth byte of jump
  416. * instruction. This wait is for avoiding it.
  417. */
  418. synchronize_sched();
  419. do_optimize_kprobes();
  420. end:
  421. mutex_unlock(&kprobe_mutex);
  422. mutex_unlock(&module_mutex);
  423. }
  424. /* Optimize kprobe if p is ready to be optimized */
  425. static __kprobes void optimize_kprobe(struct kprobe *p)
  426. {
  427. struct optimized_kprobe *op;
  428. /* Check if the kprobe is disabled or not ready for optimization. */
  429. if (!kprobe_optready(p) || !kprobes_allow_optimization ||
  430. (kprobe_disabled(p) || kprobes_all_disarmed))
  431. return;
  432. /* Both of break_handler and post_handler are not supported. */
  433. if (p->break_handler || p->post_handler)
  434. return;
  435. op = container_of(p, struct optimized_kprobe, kp);
  436. /* Check there is no other kprobes at the optimized instructions */
  437. if (arch_check_optimized_kprobe(op) < 0)
  438. return;
  439. /* Check if it is already optimized. */
  440. if (op->kp.flags & KPROBE_FLAG_OPTIMIZED)
  441. return;
  442. op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
  443. list_add(&op->list, &optimizing_list);
  444. if (!delayed_work_pending(&optimizing_work))
  445. schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
  446. }
  447. /* Unoptimize a kprobe if p is optimized */
  448. static __kprobes void unoptimize_kprobe(struct kprobe *p)
  449. {
  450. struct optimized_kprobe *op;
  451. if ((p->flags & KPROBE_FLAG_OPTIMIZED) && kprobe_aggrprobe(p)) {
  452. op = container_of(p, struct optimized_kprobe, kp);
  453. if (!list_empty(&op->list))
  454. /* Dequeue from the optimization queue */
  455. list_del_init(&op->list);
  456. else
  457. /* Replace jump with break */
  458. arch_unoptimize_kprobe(op);
  459. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  460. }
  461. }
  462. /* Remove optimized instructions */
  463. static void __kprobes kill_optimized_kprobe(struct kprobe *p)
  464. {
  465. struct optimized_kprobe *op;
  466. op = container_of(p, struct optimized_kprobe, kp);
  467. if (!list_empty(&op->list)) {
  468. /* Dequeue from the optimization queue */
  469. list_del_init(&op->list);
  470. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  471. }
  472. /* Don't unoptimize, because the target code will be freed. */
  473. arch_remove_optimized_kprobe(op);
  474. }
  475. /* Try to prepare optimized instructions */
  476. static __kprobes void prepare_optimized_kprobe(struct kprobe *p)
  477. {
  478. struct optimized_kprobe *op;
  479. op = container_of(p, struct optimized_kprobe, kp);
  480. arch_prepare_optimized_kprobe(op);
  481. }
  482. /* Free optimized instructions and optimized_kprobe */
  483. static __kprobes void free_aggr_kprobe(struct kprobe *p)
  484. {
  485. struct optimized_kprobe *op;
  486. op = container_of(p, struct optimized_kprobe, kp);
  487. arch_remove_optimized_kprobe(op);
  488. kfree(op);
  489. }
  490. /* Allocate new optimized_kprobe and try to prepare optimized instructions */
  491. static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  492. {
  493. struct optimized_kprobe *op;
  494. op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
  495. if (!op)
  496. return NULL;
  497. INIT_LIST_HEAD(&op->list);
  498. op->kp.addr = p->addr;
  499. arch_prepare_optimized_kprobe(op);
  500. return &op->kp;
  501. }
  502. static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
  503. /*
  504. * Prepare an optimized_kprobe and optimize it
  505. * NOTE: p must be a normal registered kprobe
  506. */
  507. static __kprobes void try_to_optimize_kprobe(struct kprobe *p)
  508. {
  509. struct kprobe *ap;
  510. struct optimized_kprobe *op;
  511. ap = alloc_aggr_kprobe(p);
  512. if (!ap)
  513. return;
  514. op = container_of(ap, struct optimized_kprobe, kp);
  515. if (!arch_prepared_optinsn(&op->optinsn)) {
  516. /* If failed to setup optimizing, fallback to kprobe */
  517. free_aggr_kprobe(ap);
  518. return;
  519. }
  520. init_aggr_kprobe(ap, p);
  521. optimize_kprobe(ap);
  522. }
  523. #ifdef CONFIG_SYSCTL
  524. /* This should be called with kprobe_mutex locked */
  525. static void __kprobes optimize_all_kprobes(void)
  526. {
  527. struct hlist_head *head;
  528. struct hlist_node *node;
  529. struct kprobe *p;
  530. unsigned int i;
  531. /* If optimization is already allowed, just return */
  532. if (kprobes_allow_optimization)
  533. return;
  534. kprobes_allow_optimization = true;
  535. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  536. head = &kprobe_table[i];
  537. hlist_for_each_entry_rcu(p, node, head, hlist)
  538. if (!kprobe_disabled(p))
  539. optimize_kprobe(p);
  540. }
  541. printk(KERN_INFO "Kprobes globally optimized\n");
  542. }
  543. /* This should be called with kprobe_mutex locked */
  544. static void __kprobes unoptimize_all_kprobes(void)
  545. {
  546. struct hlist_head *head;
  547. struct hlist_node *node;
  548. struct kprobe *p;
  549. unsigned int i;
  550. /* If optimization is already prohibited, just return */
  551. if (!kprobes_allow_optimization)
  552. return;
  553. kprobes_allow_optimization = false;
  554. printk(KERN_INFO "Kprobes globally unoptimized\n");
  555. get_online_cpus(); /* For avoiding text_mutex deadlock */
  556. mutex_lock(&text_mutex);
  557. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  558. head = &kprobe_table[i];
  559. hlist_for_each_entry_rcu(p, node, head, hlist) {
  560. if (!kprobe_disabled(p))
  561. unoptimize_kprobe(p);
  562. }
  563. }
  564. mutex_unlock(&text_mutex);
  565. put_online_cpus();
  566. /* Allow all currently running kprobes to complete */
  567. synchronize_sched();
  568. }
  569. int sysctl_kprobes_optimization;
  570. int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
  571. void __user *buffer, size_t *length,
  572. loff_t *ppos)
  573. {
  574. int ret;
  575. mutex_lock(&kprobe_mutex);
  576. sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
  577. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  578. if (sysctl_kprobes_optimization)
  579. optimize_all_kprobes();
  580. else
  581. unoptimize_all_kprobes();
  582. mutex_unlock(&kprobe_mutex);
  583. return ret;
  584. }
  585. #endif /* CONFIG_SYSCTL */
  586. static void __kprobes __arm_kprobe(struct kprobe *p)
  587. {
  588. struct kprobe *_p;
  589. /* Check collision with other optimized kprobes */
  590. _p = get_optimized_kprobe((unsigned long)p->addr);
  591. if (unlikely(_p))
  592. unoptimize_kprobe(_p); /* Fallback to unoptimized kprobe */
  593. arch_arm_kprobe(p);
  594. optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
  595. }
  596. static void __kprobes __disarm_kprobe(struct kprobe *p)
  597. {
  598. struct kprobe *_p;
  599. unoptimize_kprobe(p); /* Try to unoptimize */
  600. arch_disarm_kprobe(p);
  601. /* If another kprobe was blocked, optimize it. */
  602. _p = get_optimized_kprobe((unsigned long)p->addr);
  603. if (unlikely(_p))
  604. optimize_kprobe(_p);
  605. }
  606. #else /* !CONFIG_OPTPROBES */
  607. #define optimize_kprobe(p) do {} while (0)
  608. #define unoptimize_kprobe(p) do {} while (0)
  609. #define kill_optimized_kprobe(p) do {} while (0)
  610. #define prepare_optimized_kprobe(p) do {} while (0)
  611. #define try_to_optimize_kprobe(p) do {} while (0)
  612. #define __arm_kprobe(p) arch_arm_kprobe(p)
  613. #define __disarm_kprobe(p) arch_disarm_kprobe(p)
  614. static __kprobes void free_aggr_kprobe(struct kprobe *p)
  615. {
  616. kfree(p);
  617. }
  618. static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  619. {
  620. return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  621. }
  622. #endif /* CONFIG_OPTPROBES */
  623. /* Arm a kprobe with text_mutex */
  624. static void __kprobes arm_kprobe(struct kprobe *kp)
  625. {
  626. /*
  627. * Here, since __arm_kprobe() doesn't use stop_machine(),
  628. * this doesn't cause deadlock on text_mutex. So, we don't
  629. * need get_online_cpus().
  630. */
  631. mutex_lock(&text_mutex);
  632. __arm_kprobe(kp);
  633. mutex_unlock(&text_mutex);
  634. }
  635. /* Disarm a kprobe with text_mutex */
  636. static void __kprobes disarm_kprobe(struct kprobe *kp)
  637. {
  638. get_online_cpus(); /* For avoiding text_mutex deadlock */
  639. mutex_lock(&text_mutex);
  640. __disarm_kprobe(kp);
  641. mutex_unlock(&text_mutex);
  642. put_online_cpus();
  643. }
  644. /*
  645. * Aggregate handlers for multiple kprobes support - these handlers
  646. * take care of invoking the individual kprobe handlers on p->list
  647. */
  648. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  649. {
  650. struct kprobe *kp;
  651. list_for_each_entry_rcu(kp, &p->list, list) {
  652. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  653. set_kprobe_instance(kp);
  654. if (kp->pre_handler(kp, regs))
  655. return 1;
  656. }
  657. reset_kprobe_instance();
  658. }
  659. return 0;
  660. }
  661. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  662. unsigned long flags)
  663. {
  664. struct kprobe *kp;
  665. list_for_each_entry_rcu(kp, &p->list, list) {
  666. if (kp->post_handler && likely(!kprobe_disabled(kp))) {
  667. set_kprobe_instance(kp);
  668. kp->post_handler(kp, regs, flags);
  669. reset_kprobe_instance();
  670. }
  671. }
  672. }
  673. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  674. int trapnr)
  675. {
  676. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  677. /*
  678. * if we faulted "during" the execution of a user specified
  679. * probe handler, invoke just that probe's fault handler
  680. */
  681. if (cur && cur->fault_handler) {
  682. if (cur->fault_handler(cur, regs, trapnr))
  683. return 1;
  684. }
  685. return 0;
  686. }
  687. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  688. {
  689. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  690. int ret = 0;
  691. if (cur && cur->break_handler) {
  692. if (cur->break_handler(cur, regs))
  693. ret = 1;
  694. }
  695. reset_kprobe_instance();
  696. return ret;
  697. }
  698. /* Walks the list and increments nmissed count for multiprobe case */
  699. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  700. {
  701. struct kprobe *kp;
  702. if (!kprobe_aggrprobe(p)) {
  703. p->nmissed++;
  704. } else {
  705. list_for_each_entry_rcu(kp, &p->list, list)
  706. kp->nmissed++;
  707. }
  708. return;
  709. }
  710. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  711. struct hlist_head *head)
  712. {
  713. struct kretprobe *rp = ri->rp;
  714. /* remove rp inst off the rprobe_inst_table */
  715. hlist_del(&ri->hlist);
  716. INIT_HLIST_NODE(&ri->hlist);
  717. if (likely(rp)) {
  718. spin_lock(&rp->lock);
  719. hlist_add_head(&ri->hlist, &rp->free_instances);
  720. spin_unlock(&rp->lock);
  721. } else
  722. /* Unregistering */
  723. hlist_add_head(&ri->hlist, head);
  724. }
  725. void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
  726. struct hlist_head **head, unsigned long *flags)
  727. __acquires(hlist_lock)
  728. {
  729. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  730. spinlock_t *hlist_lock;
  731. *head = &kretprobe_inst_table[hash];
  732. hlist_lock = kretprobe_table_lock_ptr(hash);
  733. spin_lock_irqsave(hlist_lock, *flags);
  734. }
  735. static void __kprobes kretprobe_table_lock(unsigned long hash,
  736. unsigned long *flags)
  737. __acquires(hlist_lock)
  738. {
  739. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  740. spin_lock_irqsave(hlist_lock, *flags);
  741. }
  742. void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
  743. unsigned long *flags)
  744. __releases(hlist_lock)
  745. {
  746. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  747. spinlock_t *hlist_lock;
  748. hlist_lock = kretprobe_table_lock_ptr(hash);
  749. spin_unlock_irqrestore(hlist_lock, *flags);
  750. }
  751. static void __kprobes kretprobe_table_unlock(unsigned long hash,
  752. unsigned long *flags)
  753. __releases(hlist_lock)
  754. {
  755. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  756. spin_unlock_irqrestore(hlist_lock, *flags);
  757. }
  758. /*
  759. * This function is called from finish_task_switch when task tk becomes dead,
  760. * so that we can recycle any function-return probe instances associated
  761. * with this task. These left over instances represent probed functions
  762. * that have been called but will never return.
  763. */
  764. void __kprobes kprobe_flush_task(struct task_struct *tk)
  765. {
  766. struct kretprobe_instance *ri;
  767. struct hlist_head *head, empty_rp;
  768. struct hlist_node *node, *tmp;
  769. unsigned long hash, flags = 0;
  770. if (unlikely(!kprobes_initialized))
  771. /* Early boot. kretprobe_table_locks not yet initialized. */
  772. return;
  773. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  774. head = &kretprobe_inst_table[hash];
  775. kretprobe_table_lock(hash, &flags);
  776. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  777. if (ri->task == tk)
  778. recycle_rp_inst(ri, &empty_rp);
  779. }
  780. kretprobe_table_unlock(hash, &flags);
  781. INIT_HLIST_HEAD(&empty_rp);
  782. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  783. hlist_del(&ri->hlist);
  784. kfree(ri);
  785. }
  786. }
  787. static inline void free_rp_inst(struct kretprobe *rp)
  788. {
  789. struct kretprobe_instance *ri;
  790. struct hlist_node *pos, *next;
  791. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
  792. hlist_del(&ri->hlist);
  793. kfree(ri);
  794. }
  795. }
  796. static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
  797. {
  798. unsigned long flags, hash;
  799. struct kretprobe_instance *ri;
  800. struct hlist_node *pos, *next;
  801. struct hlist_head *head;
  802. /* No race here */
  803. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  804. kretprobe_table_lock(hash, &flags);
  805. head = &kretprobe_inst_table[hash];
  806. hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
  807. if (ri->rp == rp)
  808. ri->rp = NULL;
  809. }
  810. kretprobe_table_unlock(hash, &flags);
  811. }
  812. free_rp_inst(rp);
  813. }
  814. /*
  815. * Add the new probe to ap->list. Fail if this is the
  816. * second jprobe at the address - two jprobes can't coexist
  817. */
  818. static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
  819. {
  820. BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
  821. if (p->break_handler || p->post_handler)
  822. unoptimize_kprobe(ap); /* Fall back to normal kprobe */
  823. if (p->break_handler) {
  824. if (ap->break_handler)
  825. return -EEXIST;
  826. list_add_tail_rcu(&p->list, &ap->list);
  827. ap->break_handler = aggr_break_handler;
  828. } else
  829. list_add_rcu(&p->list, &ap->list);
  830. if (p->post_handler && !ap->post_handler)
  831. ap->post_handler = aggr_post_handler;
  832. if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
  833. ap->flags &= ~KPROBE_FLAG_DISABLED;
  834. if (!kprobes_all_disarmed)
  835. /* Arm the breakpoint again. */
  836. __arm_kprobe(ap);
  837. }
  838. return 0;
  839. }
  840. /*
  841. * Fill in the required fields of the "manager kprobe". Replace the
  842. * earlier kprobe in the hlist with the manager kprobe
  843. */
  844. static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  845. {
  846. /* Copy p's insn slot to ap */
  847. copy_kprobe(p, ap);
  848. flush_insn_slot(ap);
  849. ap->addr = p->addr;
  850. ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
  851. ap->pre_handler = aggr_pre_handler;
  852. ap->fault_handler = aggr_fault_handler;
  853. /* We don't care the kprobe which has gone. */
  854. if (p->post_handler && !kprobe_gone(p))
  855. ap->post_handler = aggr_post_handler;
  856. if (p->break_handler && !kprobe_gone(p))
  857. ap->break_handler = aggr_break_handler;
  858. INIT_LIST_HEAD(&ap->list);
  859. INIT_HLIST_NODE(&ap->hlist);
  860. list_add_rcu(&p->list, &ap->list);
  861. hlist_replace_rcu(&p->hlist, &ap->hlist);
  862. }
  863. /*
  864. * This is the second or subsequent kprobe at the address - handle
  865. * the intricacies
  866. */
  867. static int __kprobes register_aggr_kprobe(struct kprobe *orig_p,
  868. struct kprobe *p)
  869. {
  870. int ret = 0;
  871. struct kprobe *ap = orig_p;
  872. if (!kprobe_aggrprobe(orig_p)) {
  873. /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
  874. ap = alloc_aggr_kprobe(orig_p);
  875. if (!ap)
  876. return -ENOMEM;
  877. init_aggr_kprobe(ap, orig_p);
  878. }
  879. if (kprobe_gone(ap)) {
  880. /*
  881. * Attempting to insert new probe at the same location that
  882. * had a probe in the module vaddr area which already
  883. * freed. So, the instruction slot has already been
  884. * released. We need a new slot for the new probe.
  885. */
  886. ret = arch_prepare_kprobe(ap);
  887. if (ret)
  888. /*
  889. * Even if fail to allocate new slot, don't need to
  890. * free aggr_probe. It will be used next time, or
  891. * freed by unregister_kprobe.
  892. */
  893. return ret;
  894. /* Prepare optimized instructions if possible. */
  895. prepare_optimized_kprobe(ap);
  896. /*
  897. * Clear gone flag to prevent allocating new slot again, and
  898. * set disabled flag because it is not armed yet.
  899. */
  900. ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
  901. | KPROBE_FLAG_DISABLED;
  902. }
  903. /* Copy ap's insn slot to p */
  904. copy_kprobe(ap, p);
  905. return add_new_kprobe(ap, p);
  906. }
  907. static int __kprobes in_kprobes_functions(unsigned long addr)
  908. {
  909. struct kprobe_blackpoint *kb;
  910. if (addr >= (unsigned long)__kprobes_text_start &&
  911. addr < (unsigned long)__kprobes_text_end)
  912. return -EINVAL;
  913. /*
  914. * If there exists a kprobe_blacklist, verify and
  915. * fail any probe registration in the prohibited area
  916. */
  917. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  918. if (kb->start_addr) {
  919. if (addr >= kb->start_addr &&
  920. addr < (kb->start_addr + kb->range))
  921. return -EINVAL;
  922. }
  923. }
  924. return 0;
  925. }
  926. /*
  927. * If we have a symbol_name argument, look it up and add the offset field
  928. * to it. This way, we can specify a relative address to a symbol.
  929. */
  930. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  931. {
  932. kprobe_opcode_t *addr = p->addr;
  933. if (p->symbol_name) {
  934. if (addr)
  935. return NULL;
  936. kprobe_lookup_name(p->symbol_name, addr);
  937. }
  938. if (!addr)
  939. return NULL;
  940. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  941. }
  942. /* Check passed kprobe is valid and return kprobe in kprobe_table. */
  943. static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
  944. {
  945. struct kprobe *ap, *list_p;
  946. ap = get_kprobe(p->addr);
  947. if (unlikely(!ap))
  948. return NULL;
  949. if (p != ap) {
  950. list_for_each_entry_rcu(list_p, &ap->list, list)
  951. if (list_p == p)
  952. /* kprobe p is a valid probe */
  953. goto valid;
  954. return NULL;
  955. }
  956. valid:
  957. return ap;
  958. }
  959. /* Return error if the kprobe is being re-registered */
  960. static inline int check_kprobe_rereg(struct kprobe *p)
  961. {
  962. int ret = 0;
  963. mutex_lock(&kprobe_mutex);
  964. if (__get_valid_kprobe(p))
  965. ret = -EINVAL;
  966. mutex_unlock(&kprobe_mutex);
  967. return ret;
  968. }
  969. int __kprobes register_kprobe(struct kprobe *p)
  970. {
  971. int ret = 0;
  972. struct kprobe *old_p;
  973. struct module *probed_mod;
  974. kprobe_opcode_t *addr;
  975. addr = kprobe_addr(p);
  976. if (!addr)
  977. return -EINVAL;
  978. p->addr = addr;
  979. ret = check_kprobe_rereg(p);
  980. if (ret)
  981. return ret;
  982. jump_label_lock();
  983. preempt_disable();
  984. if (!kernel_text_address((unsigned long) p->addr) ||
  985. in_kprobes_functions((unsigned long) p->addr) ||
  986. ftrace_text_reserved(p->addr, p->addr) ||
  987. jump_label_text_reserved(p->addr, p->addr))
  988. goto fail_with_jump_label;
  989. /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
  990. p->flags &= KPROBE_FLAG_DISABLED;
  991. /*
  992. * Check if are we probing a module.
  993. */
  994. probed_mod = __module_text_address((unsigned long) p->addr);
  995. if (probed_mod) {
  996. /*
  997. * We must hold a refcount of the probed module while updating
  998. * its code to prohibit unexpected unloading.
  999. */
  1000. if (unlikely(!try_module_get(probed_mod)))
  1001. goto fail_with_jump_label;
  1002. /*
  1003. * If the module freed .init.text, we couldn't insert
  1004. * kprobes in there.
  1005. */
  1006. if (within_module_init((unsigned long)p->addr, probed_mod) &&
  1007. probed_mod->state != MODULE_STATE_COMING) {
  1008. module_put(probed_mod);
  1009. goto fail_with_jump_label;
  1010. }
  1011. }
  1012. preempt_enable();
  1013. jump_label_unlock();
  1014. p->nmissed = 0;
  1015. INIT_LIST_HEAD(&p->list);
  1016. mutex_lock(&kprobe_mutex);
  1017. jump_label_lock(); /* needed to call jump_label_text_reserved() */
  1018. get_online_cpus(); /* For avoiding text_mutex deadlock. */
  1019. mutex_lock(&text_mutex);
  1020. old_p = get_kprobe(p->addr);
  1021. if (old_p) {
  1022. /* Since this may unoptimize old_p, locking text_mutex. */
  1023. ret = register_aggr_kprobe(old_p, p);
  1024. goto out;
  1025. }
  1026. ret = arch_prepare_kprobe(p);
  1027. if (ret)
  1028. goto out;
  1029. INIT_HLIST_NODE(&p->hlist);
  1030. hlist_add_head_rcu(&p->hlist,
  1031. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  1032. if (!kprobes_all_disarmed && !kprobe_disabled(p))
  1033. __arm_kprobe(p);
  1034. /* Try to optimize kprobe */
  1035. try_to_optimize_kprobe(p);
  1036. out:
  1037. mutex_unlock(&text_mutex);
  1038. put_online_cpus();
  1039. jump_label_unlock();
  1040. mutex_unlock(&kprobe_mutex);
  1041. if (probed_mod)
  1042. module_put(probed_mod);
  1043. return ret;
  1044. fail_with_jump_label:
  1045. preempt_enable();
  1046. jump_label_unlock();
  1047. return -EINVAL;
  1048. }
  1049. EXPORT_SYMBOL_GPL(register_kprobe);
  1050. /* Check if all probes on the aggrprobe are disabled */
  1051. static int __kprobes aggr_kprobe_disabled(struct kprobe *ap)
  1052. {
  1053. struct kprobe *kp;
  1054. list_for_each_entry_rcu(kp, &ap->list, list)
  1055. if (!kprobe_disabled(kp))
  1056. /*
  1057. * There is an active probe on the list.
  1058. * We can't disable this ap.
  1059. */
  1060. return 0;
  1061. return 1;
  1062. }
  1063. /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
  1064. static struct kprobe *__kprobes __disable_kprobe(struct kprobe *p)
  1065. {
  1066. struct kprobe *orig_p;
  1067. /* Get an original kprobe for return */
  1068. orig_p = __get_valid_kprobe(p);
  1069. if (unlikely(orig_p == NULL))
  1070. return NULL;
  1071. if (!kprobe_disabled(p)) {
  1072. /* Disable probe if it is a child probe */
  1073. if (p != orig_p)
  1074. p->flags |= KPROBE_FLAG_DISABLED;
  1075. /* Try to disarm and disable this/parent probe */
  1076. if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
  1077. disarm_kprobe(orig_p);
  1078. orig_p->flags |= KPROBE_FLAG_DISABLED;
  1079. }
  1080. }
  1081. return orig_p;
  1082. }
  1083. /*
  1084. * Unregister a kprobe without a scheduler synchronization.
  1085. */
  1086. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  1087. {
  1088. struct kprobe *ap, *list_p;
  1089. /* Disable kprobe. This will disarm it if needed. */
  1090. ap = __disable_kprobe(p);
  1091. if (ap == NULL)
  1092. return -EINVAL;
  1093. if (ap == p)
  1094. /*
  1095. * This probe is an independent(and non-optimized) kprobe
  1096. * (not an aggrprobe). Remove from the hash list.
  1097. */
  1098. goto disarmed;
  1099. /* Following process expects this probe is an aggrprobe */
  1100. WARN_ON(!kprobe_aggrprobe(ap));
  1101. if (list_is_singular(&ap->list))
  1102. /* This probe is the last child of aggrprobe */
  1103. goto disarmed;
  1104. else {
  1105. /* If disabling probe has special handlers, update aggrprobe */
  1106. if (p->break_handler && !kprobe_gone(p))
  1107. ap->break_handler = NULL;
  1108. if (p->post_handler && !kprobe_gone(p)) {
  1109. list_for_each_entry_rcu(list_p, &ap->list, list) {
  1110. if ((list_p != p) && (list_p->post_handler))
  1111. goto noclean;
  1112. }
  1113. ap->post_handler = NULL;
  1114. }
  1115. noclean:
  1116. /*
  1117. * Remove from the aggrprobe: this path will do nothing in
  1118. * __unregister_kprobe_bottom().
  1119. */
  1120. list_del_rcu(&p->list);
  1121. if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
  1122. /*
  1123. * Try to optimize this probe again, because post
  1124. * handler may have been changed.
  1125. */
  1126. optimize_kprobe(ap);
  1127. }
  1128. return 0;
  1129. disarmed:
  1130. hlist_del_rcu(&ap->hlist);
  1131. return 0;
  1132. }
  1133. static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
  1134. {
  1135. struct kprobe *ap;
  1136. if (list_empty(&p->list))
  1137. arch_remove_kprobe(p);
  1138. else if (list_is_singular(&p->list)) {
  1139. /* "p" is the last child of an aggr_kprobe */
  1140. ap = list_entry(p->list.next, struct kprobe, list);
  1141. list_del(&p->list);
  1142. arch_remove_kprobe(ap);
  1143. free_aggr_kprobe(ap);
  1144. }
  1145. }
  1146. int __kprobes register_kprobes(struct kprobe **kps, int num)
  1147. {
  1148. int i, ret = 0;
  1149. if (num <= 0)
  1150. return -EINVAL;
  1151. for (i = 0; i < num; i++) {
  1152. ret = register_kprobe(kps[i]);
  1153. if (ret < 0) {
  1154. if (i > 0)
  1155. unregister_kprobes(kps, i);
  1156. break;
  1157. }
  1158. }
  1159. return ret;
  1160. }
  1161. EXPORT_SYMBOL_GPL(register_kprobes);
  1162. void __kprobes unregister_kprobe(struct kprobe *p)
  1163. {
  1164. unregister_kprobes(&p, 1);
  1165. }
  1166. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1167. void __kprobes unregister_kprobes(struct kprobe **kps, int num)
  1168. {
  1169. int i;
  1170. if (num <= 0)
  1171. return;
  1172. mutex_lock(&kprobe_mutex);
  1173. for (i = 0; i < num; i++)
  1174. if (__unregister_kprobe_top(kps[i]) < 0)
  1175. kps[i]->addr = NULL;
  1176. mutex_unlock(&kprobe_mutex);
  1177. synchronize_sched();
  1178. for (i = 0; i < num; i++)
  1179. if (kps[i]->addr)
  1180. __unregister_kprobe_bottom(kps[i]);
  1181. }
  1182. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1183. static struct notifier_block kprobe_exceptions_nb = {
  1184. .notifier_call = kprobe_exceptions_notify,
  1185. .priority = 0x7fffffff /* we need to be notified first */
  1186. };
  1187. unsigned long __weak arch_deref_entry_point(void *entry)
  1188. {
  1189. return (unsigned long)entry;
  1190. }
  1191. int __kprobes register_jprobes(struct jprobe **jps, int num)
  1192. {
  1193. struct jprobe *jp;
  1194. int ret = 0, i;
  1195. if (num <= 0)
  1196. return -EINVAL;
  1197. for (i = 0; i < num; i++) {
  1198. unsigned long addr, offset;
  1199. jp = jps[i];
  1200. addr = arch_deref_entry_point(jp->entry);
  1201. /* Verify probepoint is a function entry point */
  1202. if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
  1203. offset == 0) {
  1204. jp->kp.pre_handler = setjmp_pre_handler;
  1205. jp->kp.break_handler = longjmp_break_handler;
  1206. ret = register_kprobe(&jp->kp);
  1207. } else
  1208. ret = -EINVAL;
  1209. if (ret < 0) {
  1210. if (i > 0)
  1211. unregister_jprobes(jps, i);
  1212. break;
  1213. }
  1214. }
  1215. return ret;
  1216. }
  1217. EXPORT_SYMBOL_GPL(register_jprobes);
  1218. int __kprobes register_jprobe(struct jprobe *jp)
  1219. {
  1220. return register_jprobes(&jp, 1);
  1221. }
  1222. EXPORT_SYMBOL_GPL(register_jprobe);
  1223. void __kprobes unregister_jprobe(struct jprobe *jp)
  1224. {
  1225. unregister_jprobes(&jp, 1);
  1226. }
  1227. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1228. void __kprobes unregister_jprobes(struct jprobe **jps, int num)
  1229. {
  1230. int i;
  1231. if (num <= 0)
  1232. return;
  1233. mutex_lock(&kprobe_mutex);
  1234. for (i = 0; i < num; i++)
  1235. if (__unregister_kprobe_top(&jps[i]->kp) < 0)
  1236. jps[i]->kp.addr = NULL;
  1237. mutex_unlock(&kprobe_mutex);
  1238. synchronize_sched();
  1239. for (i = 0; i < num; i++) {
  1240. if (jps[i]->kp.addr)
  1241. __unregister_kprobe_bottom(&jps[i]->kp);
  1242. }
  1243. }
  1244. EXPORT_SYMBOL_GPL(unregister_jprobes);
  1245. #ifdef CONFIG_KRETPROBES
  1246. /*
  1247. * This kprobe pre_handler is registered with every kretprobe. When probe
  1248. * hits it will set up the return probe.
  1249. */
  1250. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  1251. struct pt_regs *regs)
  1252. {
  1253. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  1254. unsigned long hash, flags = 0;
  1255. struct kretprobe_instance *ri;
  1256. /*TODO: consider to only swap the RA after the last pre_handler fired */
  1257. hash = hash_ptr(current, KPROBE_HASH_BITS);
  1258. spin_lock_irqsave(&rp->lock, flags);
  1259. if (!hlist_empty(&rp->free_instances)) {
  1260. ri = hlist_entry(rp->free_instances.first,
  1261. struct kretprobe_instance, hlist);
  1262. hlist_del(&ri->hlist);
  1263. spin_unlock_irqrestore(&rp->lock, flags);
  1264. ri->rp = rp;
  1265. ri->task = current;
  1266. if (rp->entry_handler && rp->entry_handler(ri, regs))
  1267. return 0;
  1268. arch_prepare_kretprobe(ri, regs);
  1269. /* XXX(hch): why is there no hlist_move_head? */
  1270. INIT_HLIST_NODE(&ri->hlist);
  1271. kretprobe_table_lock(hash, &flags);
  1272. hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
  1273. kretprobe_table_unlock(hash, &flags);
  1274. } else {
  1275. rp->nmissed++;
  1276. spin_unlock_irqrestore(&rp->lock, flags);
  1277. }
  1278. return 0;
  1279. }
  1280. int __kprobes register_kretprobe(struct kretprobe *rp)
  1281. {
  1282. int ret = 0;
  1283. struct kretprobe_instance *inst;
  1284. int i;
  1285. void *addr;
  1286. if (kretprobe_blacklist_size) {
  1287. addr = kprobe_addr(&rp->kp);
  1288. if (!addr)
  1289. return -EINVAL;
  1290. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1291. if (kretprobe_blacklist[i].addr == addr)
  1292. return -EINVAL;
  1293. }
  1294. }
  1295. rp->kp.pre_handler = pre_handler_kretprobe;
  1296. rp->kp.post_handler = NULL;
  1297. rp->kp.fault_handler = NULL;
  1298. rp->kp.break_handler = NULL;
  1299. /* Pre-allocate memory for max kretprobe instances */
  1300. if (rp->maxactive <= 0) {
  1301. #ifdef CONFIG_PREEMPT
  1302. rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
  1303. #else
  1304. rp->maxactive = num_possible_cpus();
  1305. #endif
  1306. }
  1307. spin_lock_init(&rp->lock);
  1308. INIT_HLIST_HEAD(&rp->free_instances);
  1309. for (i = 0; i < rp->maxactive; i++) {
  1310. inst = kmalloc(sizeof(struct kretprobe_instance) +
  1311. rp->data_size, GFP_KERNEL);
  1312. if (inst == NULL) {
  1313. free_rp_inst(rp);
  1314. return -ENOMEM;
  1315. }
  1316. INIT_HLIST_NODE(&inst->hlist);
  1317. hlist_add_head(&inst->hlist, &rp->free_instances);
  1318. }
  1319. rp->nmissed = 0;
  1320. /* Establish function entry probe point */
  1321. ret = register_kprobe(&rp->kp);
  1322. if (ret != 0)
  1323. free_rp_inst(rp);
  1324. return ret;
  1325. }
  1326. EXPORT_SYMBOL_GPL(register_kretprobe);
  1327. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  1328. {
  1329. int ret = 0, i;
  1330. if (num <= 0)
  1331. return -EINVAL;
  1332. for (i = 0; i < num; i++) {
  1333. ret = register_kretprobe(rps[i]);
  1334. if (ret < 0) {
  1335. if (i > 0)
  1336. unregister_kretprobes(rps, i);
  1337. break;
  1338. }
  1339. }
  1340. return ret;
  1341. }
  1342. EXPORT_SYMBOL_GPL(register_kretprobes);
  1343. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  1344. {
  1345. unregister_kretprobes(&rp, 1);
  1346. }
  1347. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1348. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  1349. {
  1350. int i;
  1351. if (num <= 0)
  1352. return;
  1353. mutex_lock(&kprobe_mutex);
  1354. for (i = 0; i < num; i++)
  1355. if (__unregister_kprobe_top(&rps[i]->kp) < 0)
  1356. rps[i]->kp.addr = NULL;
  1357. mutex_unlock(&kprobe_mutex);
  1358. synchronize_sched();
  1359. for (i = 0; i < num; i++) {
  1360. if (rps[i]->kp.addr) {
  1361. __unregister_kprobe_bottom(&rps[i]->kp);
  1362. cleanup_rp_inst(rps[i]);
  1363. }
  1364. }
  1365. }
  1366. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1367. #else /* CONFIG_KRETPROBES */
  1368. int __kprobes register_kretprobe(struct kretprobe *rp)
  1369. {
  1370. return -ENOSYS;
  1371. }
  1372. EXPORT_SYMBOL_GPL(register_kretprobe);
  1373. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  1374. {
  1375. return -ENOSYS;
  1376. }
  1377. EXPORT_SYMBOL_GPL(register_kretprobes);
  1378. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  1379. {
  1380. }
  1381. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1382. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  1383. {
  1384. }
  1385. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1386. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  1387. struct pt_regs *regs)
  1388. {
  1389. return 0;
  1390. }
  1391. #endif /* CONFIG_KRETPROBES */
  1392. /* Set the kprobe gone and remove its instruction buffer. */
  1393. static void __kprobes kill_kprobe(struct kprobe *p)
  1394. {
  1395. struct kprobe *kp;
  1396. p->flags |= KPROBE_FLAG_GONE;
  1397. if (kprobe_aggrprobe(p)) {
  1398. /*
  1399. * If this is an aggr_kprobe, we have to list all the
  1400. * chained probes and mark them GONE.
  1401. */
  1402. list_for_each_entry_rcu(kp, &p->list, list)
  1403. kp->flags |= KPROBE_FLAG_GONE;
  1404. p->post_handler = NULL;
  1405. p->break_handler = NULL;
  1406. kill_optimized_kprobe(p);
  1407. }
  1408. /*
  1409. * Here, we can remove insn_slot safely, because no thread calls
  1410. * the original probed function (which will be freed soon) any more.
  1411. */
  1412. arch_remove_kprobe(p);
  1413. }
  1414. /* Disable one kprobe */
  1415. int __kprobes disable_kprobe(struct kprobe *kp)
  1416. {
  1417. int ret = 0;
  1418. mutex_lock(&kprobe_mutex);
  1419. /* Disable this kprobe */
  1420. if (__disable_kprobe(kp) == NULL)
  1421. ret = -EINVAL;
  1422. mutex_unlock(&kprobe_mutex);
  1423. return ret;
  1424. }
  1425. EXPORT_SYMBOL_GPL(disable_kprobe);
  1426. /* Enable one kprobe */
  1427. int __kprobes enable_kprobe(struct kprobe *kp)
  1428. {
  1429. int ret = 0;
  1430. struct kprobe *p;
  1431. mutex_lock(&kprobe_mutex);
  1432. /* Check whether specified probe is valid. */
  1433. p = __get_valid_kprobe(kp);
  1434. if (unlikely(p == NULL)) {
  1435. ret = -EINVAL;
  1436. goto out;
  1437. }
  1438. if (kprobe_gone(kp)) {
  1439. /* This kprobe has gone, we couldn't enable it. */
  1440. ret = -EINVAL;
  1441. goto out;
  1442. }
  1443. if (p != kp)
  1444. kp->flags &= ~KPROBE_FLAG_DISABLED;
  1445. if (!kprobes_all_disarmed && kprobe_disabled(p)) {
  1446. p->flags &= ~KPROBE_FLAG_DISABLED;
  1447. arm_kprobe(p);
  1448. }
  1449. out:
  1450. mutex_unlock(&kprobe_mutex);
  1451. return ret;
  1452. }
  1453. EXPORT_SYMBOL_GPL(enable_kprobe);
  1454. void __kprobes dump_kprobe(struct kprobe *kp)
  1455. {
  1456. printk(KERN_WARNING "Dumping kprobe:\n");
  1457. printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
  1458. kp->symbol_name, kp->addr, kp->offset);
  1459. }
  1460. /* Module notifier call back, checking kprobes on the module */
  1461. static int __kprobes kprobes_module_callback(struct notifier_block *nb,
  1462. unsigned long val, void *data)
  1463. {
  1464. struct module *mod = data;
  1465. struct hlist_head *head;
  1466. struct hlist_node *node;
  1467. struct kprobe *p;
  1468. unsigned int i;
  1469. int checkcore = (val == MODULE_STATE_GOING);
  1470. if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
  1471. return NOTIFY_DONE;
  1472. /*
  1473. * When MODULE_STATE_GOING was notified, both of module .text and
  1474. * .init.text sections would be freed. When MODULE_STATE_LIVE was
  1475. * notified, only .init.text section would be freed. We need to
  1476. * disable kprobes which have been inserted in the sections.
  1477. */
  1478. mutex_lock(&kprobe_mutex);
  1479. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1480. head = &kprobe_table[i];
  1481. hlist_for_each_entry_rcu(p, node, head, hlist)
  1482. if (within_module_init((unsigned long)p->addr, mod) ||
  1483. (checkcore &&
  1484. within_module_core((unsigned long)p->addr, mod))) {
  1485. /*
  1486. * The vaddr this probe is installed will soon
  1487. * be vfreed buy not synced to disk. Hence,
  1488. * disarming the breakpoint isn't needed.
  1489. */
  1490. kill_kprobe(p);
  1491. }
  1492. }
  1493. mutex_unlock(&kprobe_mutex);
  1494. return NOTIFY_DONE;
  1495. }
  1496. static struct notifier_block kprobe_module_nb = {
  1497. .notifier_call = kprobes_module_callback,
  1498. .priority = 0
  1499. };
  1500. static int __init init_kprobes(void)
  1501. {
  1502. int i, err = 0;
  1503. unsigned long offset = 0, size = 0;
  1504. char *modname, namebuf[128];
  1505. const char *symbol_name;
  1506. void *addr;
  1507. struct kprobe_blackpoint *kb;
  1508. /* FIXME allocate the probe table, currently defined statically */
  1509. /* initialize all list heads */
  1510. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1511. INIT_HLIST_HEAD(&kprobe_table[i]);
  1512. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  1513. spin_lock_init(&(kretprobe_table_locks[i].lock));
  1514. }
  1515. /*
  1516. * Lookup and populate the kprobe_blacklist.
  1517. *
  1518. * Unlike the kretprobe blacklist, we'll need to determine
  1519. * the range of addresses that belong to the said functions,
  1520. * since a kprobe need not necessarily be at the beginning
  1521. * of a function.
  1522. */
  1523. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  1524. kprobe_lookup_name(kb->name, addr);
  1525. if (!addr)
  1526. continue;
  1527. kb->start_addr = (unsigned long)addr;
  1528. symbol_name = kallsyms_lookup(kb->start_addr,
  1529. &size, &offset, &modname, namebuf);
  1530. if (!symbol_name)
  1531. kb->range = 0;
  1532. else
  1533. kb->range = size;
  1534. }
  1535. if (kretprobe_blacklist_size) {
  1536. /* lookup the function address from its name */
  1537. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1538. kprobe_lookup_name(kretprobe_blacklist[i].name,
  1539. kretprobe_blacklist[i].addr);
  1540. if (!kretprobe_blacklist[i].addr)
  1541. printk("kretprobe: lookup failed: %s\n",
  1542. kretprobe_blacklist[i].name);
  1543. }
  1544. }
  1545. #if defined(CONFIG_OPTPROBES)
  1546. #if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
  1547. /* Init kprobe_optinsn_slots */
  1548. kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
  1549. #endif
  1550. /* By default, kprobes can be optimized */
  1551. kprobes_allow_optimization = true;
  1552. #endif
  1553. /* By default, kprobes are armed */
  1554. kprobes_all_disarmed = false;
  1555. err = arch_init_kprobes();
  1556. if (!err)
  1557. err = register_die_notifier(&kprobe_exceptions_nb);
  1558. if (!err)
  1559. err = register_module_notifier(&kprobe_module_nb);
  1560. kprobes_initialized = (err == 0);
  1561. if (!err)
  1562. init_test_probes();
  1563. return err;
  1564. }
  1565. #ifdef CONFIG_DEBUG_FS
  1566. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  1567. const char *sym, int offset, char *modname, struct kprobe *pp)
  1568. {
  1569. char *kprobe_type;
  1570. if (p->pre_handler == pre_handler_kretprobe)
  1571. kprobe_type = "r";
  1572. else if (p->pre_handler == setjmp_pre_handler)
  1573. kprobe_type = "j";
  1574. else
  1575. kprobe_type = "k";
  1576. if (sym)
  1577. seq_printf(pi, "%p %s %s+0x%x %s ",
  1578. p->addr, kprobe_type, sym, offset,
  1579. (modname ? modname : " "));
  1580. else
  1581. seq_printf(pi, "%p %s %p ",
  1582. p->addr, kprobe_type, p->addr);
  1583. if (!pp)
  1584. pp = p;
  1585. seq_printf(pi, "%s%s%s\n",
  1586. (kprobe_gone(p) ? "[GONE]" : ""),
  1587. ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
  1588. (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""));
  1589. }
  1590. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  1591. {
  1592. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  1593. }
  1594. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  1595. {
  1596. (*pos)++;
  1597. if (*pos >= KPROBE_TABLE_SIZE)
  1598. return NULL;
  1599. return pos;
  1600. }
  1601. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  1602. {
  1603. /* Nothing to do */
  1604. }
  1605. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  1606. {
  1607. struct hlist_head *head;
  1608. struct hlist_node *node;
  1609. struct kprobe *p, *kp;
  1610. const char *sym = NULL;
  1611. unsigned int i = *(loff_t *) v;
  1612. unsigned long offset = 0;
  1613. char *modname, namebuf[128];
  1614. head = &kprobe_table[i];
  1615. preempt_disable();
  1616. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1617. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  1618. &offset, &modname, namebuf);
  1619. if (kprobe_aggrprobe(p)) {
  1620. list_for_each_entry_rcu(kp, &p->list, list)
  1621. report_probe(pi, kp, sym, offset, modname, p);
  1622. } else
  1623. report_probe(pi, p, sym, offset, modname, NULL);
  1624. }
  1625. preempt_enable();
  1626. return 0;
  1627. }
  1628. static const struct seq_operations kprobes_seq_ops = {
  1629. .start = kprobe_seq_start,
  1630. .next = kprobe_seq_next,
  1631. .stop = kprobe_seq_stop,
  1632. .show = show_kprobe_addr
  1633. };
  1634. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  1635. {
  1636. return seq_open(filp, &kprobes_seq_ops);
  1637. }
  1638. static const struct file_operations debugfs_kprobes_operations = {
  1639. .open = kprobes_open,
  1640. .read = seq_read,
  1641. .llseek = seq_lseek,
  1642. .release = seq_release,
  1643. };
  1644. static void __kprobes arm_all_kprobes(void)
  1645. {
  1646. struct hlist_head *head;
  1647. struct hlist_node *node;
  1648. struct kprobe *p;
  1649. unsigned int i;
  1650. mutex_lock(&kprobe_mutex);
  1651. /* If kprobes are armed, just return */
  1652. if (!kprobes_all_disarmed)
  1653. goto already_enabled;
  1654. /* Arming kprobes doesn't optimize kprobe itself */
  1655. mutex_lock(&text_mutex);
  1656. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1657. head = &kprobe_table[i];
  1658. hlist_for_each_entry_rcu(p, node, head, hlist)
  1659. if (!kprobe_disabled(p))
  1660. __arm_kprobe(p);
  1661. }
  1662. mutex_unlock(&text_mutex);
  1663. kprobes_all_disarmed = false;
  1664. printk(KERN_INFO "Kprobes globally enabled\n");
  1665. already_enabled:
  1666. mutex_unlock(&kprobe_mutex);
  1667. return;
  1668. }
  1669. static void __kprobes disarm_all_kprobes(void)
  1670. {
  1671. struct hlist_head *head;
  1672. struct hlist_node *node;
  1673. struct kprobe *p;
  1674. unsigned int i;
  1675. mutex_lock(&kprobe_mutex);
  1676. /* If kprobes are already disarmed, just return */
  1677. if (kprobes_all_disarmed)
  1678. goto already_disabled;
  1679. kprobes_all_disarmed = true;
  1680. printk(KERN_INFO "Kprobes globally disabled\n");
  1681. /*
  1682. * Here we call get_online_cpus() for avoiding text_mutex deadlock,
  1683. * because disarming may also unoptimize kprobes.
  1684. */
  1685. get_online_cpus();
  1686. mutex_lock(&text_mutex);
  1687. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1688. head = &kprobe_table[i];
  1689. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1690. if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
  1691. __disarm_kprobe(p);
  1692. }
  1693. }
  1694. mutex_unlock(&text_mutex);
  1695. put_online_cpus();
  1696. mutex_unlock(&kprobe_mutex);
  1697. /* Allow all currently running kprobes to complete */
  1698. synchronize_sched();
  1699. return;
  1700. already_disabled:
  1701. mutex_unlock(&kprobe_mutex);
  1702. return;
  1703. }
  1704. /*
  1705. * XXX: The debugfs bool file interface doesn't allow for callbacks
  1706. * when the bool state is switched. We can reuse that facility when
  1707. * available
  1708. */
  1709. static ssize_t read_enabled_file_bool(struct file *file,
  1710. char __user *user_buf, size_t count, loff_t *ppos)
  1711. {
  1712. char buf[3];
  1713. if (!kprobes_all_disarmed)
  1714. buf[0] = '1';
  1715. else
  1716. buf[0] = '0';
  1717. buf[1] = '\n';
  1718. buf[2] = 0x00;
  1719. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  1720. }
  1721. static ssize_t write_enabled_file_bool(struct file *file,
  1722. const char __user *user_buf, size_t count, loff_t *ppos)
  1723. {
  1724. char buf[32];
  1725. int buf_size;
  1726. buf_size = min(count, (sizeof(buf)-1));
  1727. if (copy_from_user(buf, user_buf, buf_size))
  1728. return -EFAULT;
  1729. switch (buf[0]) {
  1730. case 'y':
  1731. case 'Y':
  1732. case '1':
  1733. arm_all_kprobes();
  1734. break;
  1735. case 'n':
  1736. case 'N':
  1737. case '0':
  1738. disarm_all_kprobes();
  1739. break;
  1740. }
  1741. return count;
  1742. }
  1743. static const struct file_operations fops_kp = {
  1744. .read = read_enabled_file_bool,
  1745. .write = write_enabled_file_bool,
  1746. .llseek = default_llseek,
  1747. };
  1748. static int __kprobes debugfs_kprobe_init(void)
  1749. {
  1750. struct dentry *dir, *file;
  1751. unsigned int value = 1;
  1752. dir = debugfs_create_dir("kprobes", NULL);
  1753. if (!dir)
  1754. return -ENOMEM;
  1755. file = debugfs_create_file("list", 0444, dir, NULL,
  1756. &debugfs_kprobes_operations);
  1757. if (!file) {
  1758. debugfs_remove(dir);
  1759. return -ENOMEM;
  1760. }
  1761. file = debugfs_create_file("enabled", 0600, dir,
  1762. &value, &fops_kp);
  1763. if (!file) {
  1764. debugfs_remove(dir);
  1765. return -ENOMEM;
  1766. }
  1767. return 0;
  1768. }
  1769. late_initcall(debugfs_kprobe_init);
  1770. #endif /* CONFIG_DEBUG_FS */
  1771. module_init(init_kprobes);
  1772. /* defined in arch/.../kernel/kprobes.c */
  1773. EXPORT_SYMBOL_GPL(jprobe_return);