kprobes.c 56 KB

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