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. __this_cpu_write(kprobe_instance, kp);
  286. }
  287. static inline void reset_kprobe_instance(void)
  288. {
  289. __this_cpu_write(kprobe_instance, NULL);
  290. }
  291. /*
  292. * This routine is called either:
  293. * - under the kprobe_mutex - during kprobe_[un]register()
  294. * OR
  295. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  296. */
  297. struct kprobe __kprobes *get_kprobe(void *addr)
  298. {
  299. struct hlist_head *head;
  300. struct hlist_node *node;
  301. struct kprobe *p;
  302. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  303. hlist_for_each_entry_rcu(p, node, head, hlist) {
  304. if (p->addr == addr)
  305. return p;
  306. }
  307. return NULL;
  308. }
  309. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
  310. /* Return true if the kprobe is an aggregator */
  311. static inline int kprobe_aggrprobe(struct kprobe *p)
  312. {
  313. return p->pre_handler == aggr_pre_handler;
  314. }
  315. /* Return true(!0) if the kprobe is unused */
  316. static inline int kprobe_unused(struct kprobe *p)
  317. {
  318. return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
  319. list_empty(&p->list);
  320. }
  321. /*
  322. * Keep all fields in the kprobe consistent
  323. */
  324. static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
  325. {
  326. memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
  327. memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
  328. }
  329. #ifdef CONFIG_OPTPROBES
  330. /* NOTE: change this value only with kprobe_mutex held */
  331. static bool kprobes_allow_optimization;
  332. /*
  333. * Call all pre_handler on the list, but ignores its return value.
  334. * This must be called from arch-dep optimized caller.
  335. */
  336. void __kprobes opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
  337. {
  338. struct kprobe *kp;
  339. list_for_each_entry_rcu(kp, &p->list, list) {
  340. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  341. set_kprobe_instance(kp);
  342. kp->pre_handler(kp, regs);
  343. }
  344. reset_kprobe_instance();
  345. }
  346. }
  347. /* Free optimized instructions and optimized_kprobe */
  348. static __kprobes void free_aggr_kprobe(struct kprobe *p)
  349. {
  350. struct optimized_kprobe *op;
  351. op = container_of(p, struct optimized_kprobe, kp);
  352. arch_remove_optimized_kprobe(op);
  353. arch_remove_kprobe(p);
  354. kfree(op);
  355. }
  356. /* Return true(!0) if the kprobe is ready for optimization. */
  357. static inline int kprobe_optready(struct kprobe *p)
  358. {
  359. struct optimized_kprobe *op;
  360. if (kprobe_aggrprobe(p)) {
  361. op = container_of(p, struct optimized_kprobe, kp);
  362. return arch_prepared_optinsn(&op->optinsn);
  363. }
  364. return 0;
  365. }
  366. /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
  367. static inline int kprobe_disarmed(struct kprobe *p)
  368. {
  369. struct optimized_kprobe *op;
  370. /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
  371. if (!kprobe_aggrprobe(p))
  372. return kprobe_disabled(p);
  373. op = container_of(p, struct optimized_kprobe, kp);
  374. return kprobe_disabled(p) && list_empty(&op->list);
  375. }
  376. /* Return true(!0) if the probe is queued on (un)optimizing lists */
  377. static int __kprobes kprobe_queued(struct kprobe *p)
  378. {
  379. struct optimized_kprobe *op;
  380. if (kprobe_aggrprobe(p)) {
  381. op = container_of(p, struct optimized_kprobe, kp);
  382. if (!list_empty(&op->list))
  383. return 1;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * Return an optimized kprobe whose optimizing code replaces
  389. * instructions including addr (exclude breakpoint).
  390. */
  391. static struct kprobe *__kprobes get_optimized_kprobe(unsigned long addr)
  392. {
  393. int i;
  394. struct kprobe *p = NULL;
  395. struct optimized_kprobe *op;
  396. /* Don't check i == 0, since that is a breakpoint case. */
  397. for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
  398. p = get_kprobe((void *)(addr - i));
  399. if (p && kprobe_optready(p)) {
  400. op = container_of(p, struct optimized_kprobe, kp);
  401. if (arch_within_optimized_kprobe(op, addr))
  402. return p;
  403. }
  404. return NULL;
  405. }
  406. /* Optimization staging list, protected by kprobe_mutex */
  407. static LIST_HEAD(optimizing_list);
  408. static LIST_HEAD(unoptimizing_list);
  409. static void kprobe_optimizer(struct work_struct *work);
  410. static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
  411. static DECLARE_COMPLETION(optimizer_comp);
  412. #define OPTIMIZE_DELAY 5
  413. /*
  414. * Optimize (replace a breakpoint with a jump) kprobes listed on
  415. * optimizing_list.
  416. */
  417. static __kprobes void do_optimize_kprobes(void)
  418. {
  419. /* Optimization never be done when disarmed */
  420. if (kprobes_all_disarmed || !kprobes_allow_optimization ||
  421. list_empty(&optimizing_list))
  422. return;
  423. /*
  424. * The optimization/unoptimization refers online_cpus via
  425. * stop_machine() and cpu-hotplug modifies online_cpus.
  426. * And same time, text_mutex will be held in cpu-hotplug and here.
  427. * This combination can cause a deadlock (cpu-hotplug try to lock
  428. * text_mutex but stop_machine can not be done because online_cpus
  429. * has been changed)
  430. * To avoid this deadlock, we need to call get_online_cpus()
  431. * for preventing cpu-hotplug outside of text_mutex locking.
  432. */
  433. get_online_cpus();
  434. mutex_lock(&text_mutex);
  435. arch_optimize_kprobes(&optimizing_list);
  436. mutex_unlock(&text_mutex);
  437. put_online_cpus();
  438. }
  439. /*
  440. * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
  441. * if need) kprobes listed on unoptimizing_list.
  442. */
  443. static __kprobes void do_unoptimize_kprobes(struct list_head *free_list)
  444. {
  445. struct optimized_kprobe *op, *tmp;
  446. /* Unoptimization must be done anytime */
  447. if (list_empty(&unoptimizing_list))
  448. return;
  449. /* Ditto to do_optimize_kprobes */
  450. get_online_cpus();
  451. mutex_lock(&text_mutex);
  452. arch_unoptimize_kprobes(&unoptimizing_list, free_list);
  453. /* Loop free_list for disarming */
  454. list_for_each_entry_safe(op, tmp, free_list, list) {
  455. /* Disarm probes if marked disabled */
  456. if (kprobe_disabled(&op->kp))
  457. arch_disarm_kprobe(&op->kp);
  458. if (kprobe_unused(&op->kp)) {
  459. /*
  460. * Remove unused probes from hash list. After waiting
  461. * for synchronization, these probes are reclaimed.
  462. * (reclaiming is done by do_free_cleaned_kprobes.)
  463. */
  464. hlist_del_rcu(&op->kp.hlist);
  465. } else
  466. list_del_init(&op->list);
  467. }
  468. mutex_unlock(&text_mutex);
  469. put_online_cpus();
  470. }
  471. /* Reclaim all kprobes on the free_list */
  472. static __kprobes void do_free_cleaned_kprobes(struct list_head *free_list)
  473. {
  474. struct optimized_kprobe *op, *tmp;
  475. list_for_each_entry_safe(op, tmp, free_list, list) {
  476. BUG_ON(!kprobe_unused(&op->kp));
  477. list_del_init(&op->list);
  478. free_aggr_kprobe(&op->kp);
  479. }
  480. }
  481. /* Start optimizer after OPTIMIZE_DELAY passed */
  482. static __kprobes void kick_kprobe_optimizer(void)
  483. {
  484. if (!delayed_work_pending(&optimizing_work))
  485. schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
  486. }
  487. /* Kprobe jump optimizer */
  488. static __kprobes void kprobe_optimizer(struct work_struct *work)
  489. {
  490. LIST_HEAD(free_list);
  491. /* Lock modules while optimizing kprobes */
  492. mutex_lock(&module_mutex);
  493. mutex_lock(&kprobe_mutex);
  494. /*
  495. * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
  496. * kprobes before waiting for quiesence period.
  497. */
  498. do_unoptimize_kprobes(&free_list);
  499. /*
  500. * Step 2: Wait for quiesence period to ensure all running interrupts
  501. * are done. Because optprobe may modify multiple instructions
  502. * there is a chance that Nth instruction is interrupted. In that
  503. * case, running interrupt can return to 2nd-Nth byte of jump
  504. * instruction. This wait is for avoiding it.
  505. */
  506. synchronize_sched();
  507. /* Step 3: Optimize kprobes after quiesence period */
  508. do_optimize_kprobes();
  509. /* Step 4: Free cleaned kprobes after quiesence period */
  510. do_free_cleaned_kprobes(&free_list);
  511. mutex_unlock(&kprobe_mutex);
  512. mutex_unlock(&module_mutex);
  513. /* Step 5: Kick optimizer again if needed */
  514. if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
  515. kick_kprobe_optimizer();
  516. else
  517. /* Wake up all waiters */
  518. complete_all(&optimizer_comp);
  519. }
  520. /* Wait for completing optimization and unoptimization */
  521. static __kprobes void wait_for_kprobe_optimizer(void)
  522. {
  523. if (delayed_work_pending(&optimizing_work))
  524. wait_for_completion(&optimizer_comp);
  525. }
  526. /* Optimize kprobe if p is ready to be optimized */
  527. static __kprobes void optimize_kprobe(struct kprobe *p)
  528. {
  529. struct optimized_kprobe *op;
  530. /* Check if the kprobe is disabled or not ready for optimization. */
  531. if (!kprobe_optready(p) || !kprobes_allow_optimization ||
  532. (kprobe_disabled(p) || kprobes_all_disarmed))
  533. return;
  534. /* Both of break_handler and post_handler are not supported. */
  535. if (p->break_handler || p->post_handler)
  536. return;
  537. op = container_of(p, struct optimized_kprobe, kp);
  538. /* Check there is no other kprobes at the optimized instructions */
  539. if (arch_check_optimized_kprobe(op) < 0)
  540. return;
  541. /* Check if it is already optimized. */
  542. if (op->kp.flags & KPROBE_FLAG_OPTIMIZED)
  543. return;
  544. op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
  545. if (!list_empty(&op->list))
  546. /* This is under unoptimizing. Just dequeue the probe */
  547. list_del_init(&op->list);
  548. else {
  549. list_add(&op->list, &optimizing_list);
  550. kick_kprobe_optimizer();
  551. }
  552. }
  553. /* Short cut to direct unoptimizing */
  554. static __kprobes void force_unoptimize_kprobe(struct optimized_kprobe *op)
  555. {
  556. get_online_cpus();
  557. arch_unoptimize_kprobe(op);
  558. put_online_cpus();
  559. if (kprobe_disabled(&op->kp))
  560. arch_disarm_kprobe(&op->kp);
  561. }
  562. /* Unoptimize a kprobe if p is optimized */
  563. static __kprobes void unoptimize_kprobe(struct kprobe *p, bool force)
  564. {
  565. struct optimized_kprobe *op;
  566. if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
  567. return; /* This is not an optprobe nor optimized */
  568. op = container_of(p, struct optimized_kprobe, kp);
  569. if (!kprobe_optimized(p)) {
  570. /* Unoptimized or unoptimizing case */
  571. if (force && !list_empty(&op->list)) {
  572. /*
  573. * Only if this is unoptimizing kprobe and forced,
  574. * forcibly unoptimize it. (No need to unoptimize
  575. * unoptimized kprobe again :)
  576. */
  577. list_del_init(&op->list);
  578. force_unoptimize_kprobe(op);
  579. }
  580. return;
  581. }
  582. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  583. if (!list_empty(&op->list)) {
  584. /* Dequeue from the optimization queue */
  585. list_del_init(&op->list);
  586. return;
  587. }
  588. /* Optimized kprobe case */
  589. if (force)
  590. /* Forcibly update the code: this is a special case */
  591. force_unoptimize_kprobe(op);
  592. else {
  593. list_add(&op->list, &unoptimizing_list);
  594. kick_kprobe_optimizer();
  595. }
  596. }
  597. /* Cancel unoptimizing for reusing */
  598. static void reuse_unused_kprobe(struct kprobe *ap)
  599. {
  600. struct optimized_kprobe *op;
  601. BUG_ON(!kprobe_unused(ap));
  602. /*
  603. * Unused kprobe MUST be on the way of delayed unoptimizing (means
  604. * there is still a relative jump) and disabled.
  605. */
  606. op = container_of(ap, struct optimized_kprobe, kp);
  607. if (unlikely(list_empty(&op->list)))
  608. printk(KERN_WARNING "Warning: found a stray unused "
  609. "aggrprobe@%p\n", ap->addr);
  610. /* Enable the probe again */
  611. ap->flags &= ~KPROBE_FLAG_DISABLED;
  612. /* Optimize it again (remove from op->list) */
  613. BUG_ON(!kprobe_optready(ap));
  614. optimize_kprobe(ap);
  615. }
  616. /* Remove optimized instructions */
  617. static void __kprobes kill_optimized_kprobe(struct kprobe *p)
  618. {
  619. struct optimized_kprobe *op;
  620. op = container_of(p, struct optimized_kprobe, kp);
  621. if (!list_empty(&op->list))
  622. /* Dequeue from the (un)optimization queue */
  623. list_del_init(&op->list);
  624. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  625. /* Don't touch the code, because it is already freed. */
  626. arch_remove_optimized_kprobe(op);
  627. }
  628. /* Try to prepare optimized instructions */
  629. static __kprobes void prepare_optimized_kprobe(struct kprobe *p)
  630. {
  631. struct optimized_kprobe *op;
  632. op = container_of(p, struct optimized_kprobe, kp);
  633. arch_prepare_optimized_kprobe(op);
  634. }
  635. /* Allocate new optimized_kprobe and try to prepare optimized instructions */
  636. static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  637. {
  638. struct optimized_kprobe *op;
  639. op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
  640. if (!op)
  641. return NULL;
  642. INIT_LIST_HEAD(&op->list);
  643. op->kp.addr = p->addr;
  644. arch_prepare_optimized_kprobe(op);
  645. return &op->kp;
  646. }
  647. static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
  648. /*
  649. * Prepare an optimized_kprobe and optimize it
  650. * NOTE: p must be a normal registered kprobe
  651. */
  652. static __kprobes void try_to_optimize_kprobe(struct kprobe *p)
  653. {
  654. struct kprobe *ap;
  655. struct optimized_kprobe *op;
  656. ap = alloc_aggr_kprobe(p);
  657. if (!ap)
  658. return;
  659. op = container_of(ap, struct optimized_kprobe, kp);
  660. if (!arch_prepared_optinsn(&op->optinsn)) {
  661. /* If failed to setup optimizing, fallback to kprobe */
  662. arch_remove_optimized_kprobe(op);
  663. kfree(op);
  664. return;
  665. }
  666. init_aggr_kprobe(ap, p);
  667. optimize_kprobe(ap);
  668. }
  669. #ifdef CONFIG_SYSCTL
  670. /* This should be called with kprobe_mutex locked */
  671. static void __kprobes optimize_all_kprobes(void)
  672. {
  673. struct hlist_head *head;
  674. struct hlist_node *node;
  675. struct kprobe *p;
  676. unsigned int i;
  677. /* If optimization is already allowed, just return */
  678. if (kprobes_allow_optimization)
  679. return;
  680. kprobes_allow_optimization = true;
  681. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  682. head = &kprobe_table[i];
  683. hlist_for_each_entry_rcu(p, node, head, hlist)
  684. if (!kprobe_disabled(p))
  685. optimize_kprobe(p);
  686. }
  687. printk(KERN_INFO "Kprobes globally optimized\n");
  688. }
  689. /* This should be called with kprobe_mutex locked */
  690. static void __kprobes unoptimize_all_kprobes(void)
  691. {
  692. struct hlist_head *head;
  693. struct hlist_node *node;
  694. struct kprobe *p;
  695. unsigned int i;
  696. /* If optimization is already prohibited, just return */
  697. if (!kprobes_allow_optimization)
  698. return;
  699. kprobes_allow_optimization = false;
  700. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  701. head = &kprobe_table[i];
  702. hlist_for_each_entry_rcu(p, node, head, hlist) {
  703. if (!kprobe_disabled(p))
  704. unoptimize_kprobe(p, false);
  705. }
  706. }
  707. /* Wait for unoptimizing completion */
  708. wait_for_kprobe_optimizer();
  709. printk(KERN_INFO "Kprobes globally unoptimized\n");
  710. }
  711. int sysctl_kprobes_optimization;
  712. int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
  713. void __user *buffer, size_t *length,
  714. loff_t *ppos)
  715. {
  716. int ret;
  717. mutex_lock(&kprobe_mutex);
  718. sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
  719. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  720. if (sysctl_kprobes_optimization)
  721. optimize_all_kprobes();
  722. else
  723. unoptimize_all_kprobes();
  724. mutex_unlock(&kprobe_mutex);
  725. return ret;
  726. }
  727. #endif /* CONFIG_SYSCTL */
  728. /* Put a breakpoint for a probe. Must be called with text_mutex locked */
  729. static void __kprobes __arm_kprobe(struct kprobe *p)
  730. {
  731. struct kprobe *_p;
  732. /* Check collision with other optimized kprobes */
  733. _p = get_optimized_kprobe((unsigned long)p->addr);
  734. if (unlikely(_p))
  735. /* Fallback to unoptimized kprobe */
  736. unoptimize_kprobe(_p, true);
  737. arch_arm_kprobe(p);
  738. optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
  739. }
  740. /* Remove the breakpoint of a probe. Must be called with text_mutex locked */
  741. static void __kprobes __disarm_kprobe(struct kprobe *p, bool reopt)
  742. {
  743. struct kprobe *_p;
  744. unoptimize_kprobe(p, false); /* Try to unoptimize */
  745. if (!kprobe_queued(p)) {
  746. arch_disarm_kprobe(p);
  747. /* If another kprobe was blocked, optimize it. */
  748. _p = get_optimized_kprobe((unsigned long)p->addr);
  749. if (unlikely(_p) && reopt)
  750. optimize_kprobe(_p);
  751. }
  752. /* TODO: reoptimize others after unoptimized this probe */
  753. }
  754. #else /* !CONFIG_OPTPROBES */
  755. #define optimize_kprobe(p) do {} while (0)
  756. #define unoptimize_kprobe(p, f) do {} while (0)
  757. #define kill_optimized_kprobe(p) do {} while (0)
  758. #define prepare_optimized_kprobe(p) do {} while (0)
  759. #define try_to_optimize_kprobe(p) do {} while (0)
  760. #define __arm_kprobe(p) arch_arm_kprobe(p)
  761. #define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
  762. #define kprobe_disarmed(p) kprobe_disabled(p)
  763. #define wait_for_kprobe_optimizer() do {} while (0)
  764. /* There should be no unused kprobes can be reused without optimization */
  765. static void reuse_unused_kprobe(struct kprobe *ap)
  766. {
  767. printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
  768. BUG_ON(kprobe_unused(ap));
  769. }
  770. static __kprobes void free_aggr_kprobe(struct kprobe *p)
  771. {
  772. arch_remove_kprobe(p);
  773. kfree(p);
  774. }
  775. static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  776. {
  777. return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  778. }
  779. #endif /* CONFIG_OPTPROBES */
  780. /* Arm a kprobe with text_mutex */
  781. static void __kprobes arm_kprobe(struct kprobe *kp)
  782. {
  783. /*
  784. * Here, since __arm_kprobe() doesn't use stop_machine(),
  785. * this doesn't cause deadlock on text_mutex. So, we don't
  786. * need get_online_cpus().
  787. */
  788. mutex_lock(&text_mutex);
  789. __arm_kprobe(kp);
  790. mutex_unlock(&text_mutex);
  791. }
  792. /* Disarm a kprobe with text_mutex */
  793. static void __kprobes disarm_kprobe(struct kprobe *kp)
  794. {
  795. /* Ditto */
  796. mutex_lock(&text_mutex);
  797. __disarm_kprobe(kp, true);
  798. mutex_unlock(&text_mutex);
  799. }
  800. /*
  801. * Aggregate handlers for multiple kprobes support - these handlers
  802. * take care of invoking the individual kprobe handlers on p->list
  803. */
  804. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  805. {
  806. struct kprobe *kp;
  807. list_for_each_entry_rcu(kp, &p->list, list) {
  808. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  809. set_kprobe_instance(kp);
  810. if (kp->pre_handler(kp, regs))
  811. return 1;
  812. }
  813. reset_kprobe_instance();
  814. }
  815. return 0;
  816. }
  817. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  818. unsigned long flags)
  819. {
  820. struct kprobe *kp;
  821. list_for_each_entry_rcu(kp, &p->list, list) {
  822. if (kp->post_handler && likely(!kprobe_disabled(kp))) {
  823. set_kprobe_instance(kp);
  824. kp->post_handler(kp, regs, flags);
  825. reset_kprobe_instance();
  826. }
  827. }
  828. }
  829. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  830. int trapnr)
  831. {
  832. struct kprobe *cur = __this_cpu_read(kprobe_instance);
  833. /*
  834. * if we faulted "during" the execution of a user specified
  835. * probe handler, invoke just that probe's fault handler
  836. */
  837. if (cur && cur->fault_handler) {
  838. if (cur->fault_handler(cur, regs, trapnr))
  839. return 1;
  840. }
  841. return 0;
  842. }
  843. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  844. {
  845. struct kprobe *cur = __this_cpu_read(kprobe_instance);
  846. int ret = 0;
  847. if (cur && cur->break_handler) {
  848. if (cur->break_handler(cur, regs))
  849. ret = 1;
  850. }
  851. reset_kprobe_instance();
  852. return ret;
  853. }
  854. /* Walks the list and increments nmissed count for multiprobe case */
  855. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  856. {
  857. struct kprobe *kp;
  858. if (!kprobe_aggrprobe(p)) {
  859. p->nmissed++;
  860. } else {
  861. list_for_each_entry_rcu(kp, &p->list, list)
  862. kp->nmissed++;
  863. }
  864. return;
  865. }
  866. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  867. struct hlist_head *head)
  868. {
  869. struct kretprobe *rp = ri->rp;
  870. /* remove rp inst off the rprobe_inst_table */
  871. hlist_del(&ri->hlist);
  872. INIT_HLIST_NODE(&ri->hlist);
  873. if (likely(rp)) {
  874. spin_lock(&rp->lock);
  875. hlist_add_head(&ri->hlist, &rp->free_instances);
  876. spin_unlock(&rp->lock);
  877. } else
  878. /* Unregistering */
  879. hlist_add_head(&ri->hlist, head);
  880. }
  881. void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
  882. struct hlist_head **head, unsigned long *flags)
  883. __acquires(hlist_lock)
  884. {
  885. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  886. spinlock_t *hlist_lock;
  887. *head = &kretprobe_inst_table[hash];
  888. hlist_lock = kretprobe_table_lock_ptr(hash);
  889. spin_lock_irqsave(hlist_lock, *flags);
  890. }
  891. static void __kprobes kretprobe_table_lock(unsigned long hash,
  892. unsigned long *flags)
  893. __acquires(hlist_lock)
  894. {
  895. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  896. spin_lock_irqsave(hlist_lock, *flags);
  897. }
  898. void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
  899. unsigned long *flags)
  900. __releases(hlist_lock)
  901. {
  902. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  903. spinlock_t *hlist_lock;
  904. hlist_lock = kretprobe_table_lock_ptr(hash);
  905. spin_unlock_irqrestore(hlist_lock, *flags);
  906. }
  907. static void __kprobes kretprobe_table_unlock(unsigned long hash,
  908. unsigned long *flags)
  909. __releases(hlist_lock)
  910. {
  911. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  912. spin_unlock_irqrestore(hlist_lock, *flags);
  913. }
  914. /*
  915. * This function is called from finish_task_switch when task tk becomes dead,
  916. * so that we can recycle any function-return probe instances associated
  917. * with this task. These left over instances represent probed functions
  918. * that have been called but will never return.
  919. */
  920. void __kprobes kprobe_flush_task(struct task_struct *tk)
  921. {
  922. struct kretprobe_instance *ri;
  923. struct hlist_head *head, empty_rp;
  924. struct hlist_node *node, *tmp;
  925. unsigned long hash, flags = 0;
  926. if (unlikely(!kprobes_initialized))
  927. /* Early boot. kretprobe_table_locks not yet initialized. */
  928. return;
  929. INIT_HLIST_HEAD(&empty_rp);
  930. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  931. head = &kretprobe_inst_table[hash];
  932. kretprobe_table_lock(hash, &flags);
  933. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  934. if (ri->task == tk)
  935. recycle_rp_inst(ri, &empty_rp);
  936. }
  937. kretprobe_table_unlock(hash, &flags);
  938. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  939. hlist_del(&ri->hlist);
  940. kfree(ri);
  941. }
  942. }
  943. static inline void free_rp_inst(struct kretprobe *rp)
  944. {
  945. struct kretprobe_instance *ri;
  946. struct hlist_node *pos, *next;
  947. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
  948. hlist_del(&ri->hlist);
  949. kfree(ri);
  950. }
  951. }
  952. static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
  953. {
  954. unsigned long flags, hash;
  955. struct kretprobe_instance *ri;
  956. struct hlist_node *pos, *next;
  957. struct hlist_head *head;
  958. /* No race here */
  959. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  960. kretprobe_table_lock(hash, &flags);
  961. head = &kretprobe_inst_table[hash];
  962. hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
  963. if (ri->rp == rp)
  964. ri->rp = NULL;
  965. }
  966. kretprobe_table_unlock(hash, &flags);
  967. }
  968. free_rp_inst(rp);
  969. }
  970. /*
  971. * Add the new probe to ap->list. Fail if this is the
  972. * second jprobe at the address - two jprobes can't coexist
  973. */
  974. static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
  975. {
  976. BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
  977. if (p->break_handler || p->post_handler)
  978. unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
  979. if (p->break_handler) {
  980. if (ap->break_handler)
  981. return -EEXIST;
  982. list_add_tail_rcu(&p->list, &ap->list);
  983. ap->break_handler = aggr_break_handler;
  984. } else
  985. list_add_rcu(&p->list, &ap->list);
  986. if (p->post_handler && !ap->post_handler)
  987. ap->post_handler = aggr_post_handler;
  988. if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
  989. ap->flags &= ~KPROBE_FLAG_DISABLED;
  990. if (!kprobes_all_disarmed)
  991. /* Arm the breakpoint again. */
  992. __arm_kprobe(ap);
  993. }
  994. return 0;
  995. }
  996. /*
  997. * Fill in the required fields of the "manager kprobe". Replace the
  998. * earlier kprobe in the hlist with the manager kprobe
  999. */
  1000. static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  1001. {
  1002. /* Copy p's insn slot to ap */
  1003. copy_kprobe(p, ap);
  1004. flush_insn_slot(ap);
  1005. ap->addr = p->addr;
  1006. ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
  1007. ap->pre_handler = aggr_pre_handler;
  1008. ap->fault_handler = aggr_fault_handler;
  1009. /* We don't care the kprobe which has gone. */
  1010. if (p->post_handler && !kprobe_gone(p))
  1011. ap->post_handler = aggr_post_handler;
  1012. if (p->break_handler && !kprobe_gone(p))
  1013. ap->break_handler = aggr_break_handler;
  1014. INIT_LIST_HEAD(&ap->list);
  1015. INIT_HLIST_NODE(&ap->hlist);
  1016. list_add_rcu(&p->list, &ap->list);
  1017. hlist_replace_rcu(&p->hlist, &ap->hlist);
  1018. }
  1019. /*
  1020. * This is the second or subsequent kprobe at the address - handle
  1021. * the intricacies
  1022. */
  1023. static int __kprobes register_aggr_kprobe(struct kprobe *orig_p,
  1024. struct kprobe *p)
  1025. {
  1026. int ret = 0;
  1027. struct kprobe *ap = orig_p;
  1028. if (!kprobe_aggrprobe(orig_p)) {
  1029. /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
  1030. ap = alloc_aggr_kprobe(orig_p);
  1031. if (!ap)
  1032. return -ENOMEM;
  1033. init_aggr_kprobe(ap, orig_p);
  1034. } else if (kprobe_unused(ap))
  1035. /* This probe is going to die. Rescue it */
  1036. reuse_unused_kprobe(ap);
  1037. if (kprobe_gone(ap)) {
  1038. /*
  1039. * Attempting to insert new probe at the same location that
  1040. * had a probe in the module vaddr area which already
  1041. * freed. So, the instruction slot has already been
  1042. * released. We need a new slot for the new probe.
  1043. */
  1044. ret = arch_prepare_kprobe(ap);
  1045. if (ret)
  1046. /*
  1047. * Even if fail to allocate new slot, don't need to
  1048. * free aggr_probe. It will be used next time, or
  1049. * freed by unregister_kprobe.
  1050. */
  1051. return ret;
  1052. /* Prepare optimized instructions if possible. */
  1053. prepare_optimized_kprobe(ap);
  1054. /*
  1055. * Clear gone flag to prevent allocating new slot again, and
  1056. * set disabled flag because it is not armed yet.
  1057. */
  1058. ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
  1059. | KPROBE_FLAG_DISABLED;
  1060. }
  1061. /* Copy ap's insn slot to p */
  1062. copy_kprobe(ap, p);
  1063. return add_new_kprobe(ap, p);
  1064. }
  1065. static int __kprobes in_kprobes_functions(unsigned long addr)
  1066. {
  1067. struct kprobe_blackpoint *kb;
  1068. if (addr >= (unsigned long)__kprobes_text_start &&
  1069. addr < (unsigned long)__kprobes_text_end)
  1070. return -EINVAL;
  1071. /*
  1072. * If there exists a kprobe_blacklist, verify and
  1073. * fail any probe registration in the prohibited area
  1074. */
  1075. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  1076. if (kb->start_addr) {
  1077. if (addr >= kb->start_addr &&
  1078. addr < (kb->start_addr + kb->range))
  1079. return -EINVAL;
  1080. }
  1081. }
  1082. return 0;
  1083. }
  1084. /*
  1085. * If we have a symbol_name argument, look it up and add the offset field
  1086. * to it. This way, we can specify a relative address to a symbol.
  1087. */
  1088. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  1089. {
  1090. kprobe_opcode_t *addr = p->addr;
  1091. if (p->symbol_name) {
  1092. if (addr)
  1093. return NULL;
  1094. kprobe_lookup_name(p->symbol_name, addr);
  1095. }
  1096. if (!addr)
  1097. return NULL;
  1098. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  1099. }
  1100. /* Check passed kprobe is valid and return kprobe in kprobe_table. */
  1101. static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
  1102. {
  1103. struct kprobe *ap, *list_p;
  1104. ap = get_kprobe(p->addr);
  1105. if (unlikely(!ap))
  1106. return NULL;
  1107. if (p != ap) {
  1108. list_for_each_entry_rcu(list_p, &ap->list, list)
  1109. if (list_p == p)
  1110. /* kprobe p is a valid probe */
  1111. goto valid;
  1112. return NULL;
  1113. }
  1114. valid:
  1115. return ap;
  1116. }
  1117. /* Return error if the kprobe is being re-registered */
  1118. static inline int check_kprobe_rereg(struct kprobe *p)
  1119. {
  1120. int ret = 0;
  1121. mutex_lock(&kprobe_mutex);
  1122. if (__get_valid_kprobe(p))
  1123. ret = -EINVAL;
  1124. mutex_unlock(&kprobe_mutex);
  1125. return ret;
  1126. }
  1127. int __kprobes register_kprobe(struct kprobe *p)
  1128. {
  1129. int ret = 0;
  1130. struct kprobe *old_p;
  1131. struct module *probed_mod;
  1132. kprobe_opcode_t *addr;
  1133. addr = kprobe_addr(p);
  1134. if (!addr)
  1135. return -EINVAL;
  1136. p->addr = addr;
  1137. ret = check_kprobe_rereg(p);
  1138. if (ret)
  1139. return ret;
  1140. jump_label_lock();
  1141. preempt_disable();
  1142. if (!kernel_text_address((unsigned long) p->addr) ||
  1143. in_kprobes_functions((unsigned long) p->addr) ||
  1144. ftrace_text_reserved(p->addr, p->addr) ||
  1145. jump_label_text_reserved(p->addr, p->addr))
  1146. goto fail_with_jump_label;
  1147. /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
  1148. p->flags &= KPROBE_FLAG_DISABLED;
  1149. /*
  1150. * Check if are we probing a module.
  1151. */
  1152. probed_mod = __module_text_address((unsigned long) p->addr);
  1153. if (probed_mod) {
  1154. /*
  1155. * We must hold a refcount of the probed module while updating
  1156. * its code to prohibit unexpected unloading.
  1157. */
  1158. if (unlikely(!try_module_get(probed_mod)))
  1159. goto fail_with_jump_label;
  1160. /*
  1161. * If the module freed .init.text, we couldn't insert
  1162. * kprobes in there.
  1163. */
  1164. if (within_module_init((unsigned long)p->addr, probed_mod) &&
  1165. probed_mod->state != MODULE_STATE_COMING) {
  1166. module_put(probed_mod);
  1167. goto fail_with_jump_label;
  1168. }
  1169. }
  1170. preempt_enable();
  1171. jump_label_unlock();
  1172. p->nmissed = 0;
  1173. INIT_LIST_HEAD(&p->list);
  1174. mutex_lock(&kprobe_mutex);
  1175. jump_label_lock(); /* needed to call jump_label_text_reserved() */
  1176. get_online_cpus(); /* For avoiding text_mutex deadlock. */
  1177. mutex_lock(&text_mutex);
  1178. old_p = get_kprobe(p->addr);
  1179. if (old_p) {
  1180. /* Since this may unoptimize old_p, locking text_mutex. */
  1181. ret = register_aggr_kprobe(old_p, p);
  1182. goto out;
  1183. }
  1184. ret = arch_prepare_kprobe(p);
  1185. if (ret)
  1186. goto out;
  1187. INIT_HLIST_NODE(&p->hlist);
  1188. hlist_add_head_rcu(&p->hlist,
  1189. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  1190. if (!kprobes_all_disarmed && !kprobe_disabled(p))
  1191. __arm_kprobe(p);
  1192. /* Try to optimize kprobe */
  1193. try_to_optimize_kprobe(p);
  1194. out:
  1195. mutex_unlock(&text_mutex);
  1196. put_online_cpus();
  1197. jump_label_unlock();
  1198. mutex_unlock(&kprobe_mutex);
  1199. if (probed_mod)
  1200. module_put(probed_mod);
  1201. return ret;
  1202. fail_with_jump_label:
  1203. preempt_enable();
  1204. jump_label_unlock();
  1205. return -EINVAL;
  1206. }
  1207. EXPORT_SYMBOL_GPL(register_kprobe);
  1208. /* Check if all probes on the aggrprobe are disabled */
  1209. static int __kprobes aggr_kprobe_disabled(struct kprobe *ap)
  1210. {
  1211. struct kprobe *kp;
  1212. list_for_each_entry_rcu(kp, &ap->list, list)
  1213. if (!kprobe_disabled(kp))
  1214. /*
  1215. * There is an active probe on the list.
  1216. * We can't disable this ap.
  1217. */
  1218. return 0;
  1219. return 1;
  1220. }
  1221. /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
  1222. static struct kprobe *__kprobes __disable_kprobe(struct kprobe *p)
  1223. {
  1224. struct kprobe *orig_p;
  1225. /* Get an original kprobe for return */
  1226. orig_p = __get_valid_kprobe(p);
  1227. if (unlikely(orig_p == NULL))
  1228. return NULL;
  1229. if (!kprobe_disabled(p)) {
  1230. /* Disable probe if it is a child probe */
  1231. if (p != orig_p)
  1232. p->flags |= KPROBE_FLAG_DISABLED;
  1233. /* Try to disarm and disable this/parent probe */
  1234. if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
  1235. disarm_kprobe(orig_p);
  1236. orig_p->flags |= KPROBE_FLAG_DISABLED;
  1237. }
  1238. }
  1239. return orig_p;
  1240. }
  1241. /*
  1242. * Unregister a kprobe without a scheduler synchronization.
  1243. */
  1244. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  1245. {
  1246. struct kprobe *ap, *list_p;
  1247. /* Disable kprobe. This will disarm it if needed. */
  1248. ap = __disable_kprobe(p);
  1249. if (ap == NULL)
  1250. return -EINVAL;
  1251. if (ap == p)
  1252. /*
  1253. * This probe is an independent(and non-optimized) kprobe
  1254. * (not an aggrprobe). Remove from the hash list.
  1255. */
  1256. goto disarmed;
  1257. /* Following process expects this probe is an aggrprobe */
  1258. WARN_ON(!kprobe_aggrprobe(ap));
  1259. if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
  1260. /*
  1261. * !disarmed could be happen if the probe is under delayed
  1262. * unoptimizing.
  1263. */
  1264. goto disarmed;
  1265. else {
  1266. /* If disabling probe has special handlers, update aggrprobe */
  1267. if (p->break_handler && !kprobe_gone(p))
  1268. ap->break_handler = NULL;
  1269. if (p->post_handler && !kprobe_gone(p)) {
  1270. list_for_each_entry_rcu(list_p, &ap->list, list) {
  1271. if ((list_p != p) && (list_p->post_handler))
  1272. goto noclean;
  1273. }
  1274. ap->post_handler = NULL;
  1275. }
  1276. noclean:
  1277. /*
  1278. * Remove from the aggrprobe: this path will do nothing in
  1279. * __unregister_kprobe_bottom().
  1280. */
  1281. list_del_rcu(&p->list);
  1282. if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
  1283. /*
  1284. * Try to optimize this probe again, because post
  1285. * handler may have been changed.
  1286. */
  1287. optimize_kprobe(ap);
  1288. }
  1289. return 0;
  1290. disarmed:
  1291. BUG_ON(!kprobe_disarmed(ap));
  1292. hlist_del_rcu(&ap->hlist);
  1293. return 0;
  1294. }
  1295. static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
  1296. {
  1297. struct kprobe *ap;
  1298. if (list_empty(&p->list))
  1299. /* This is an independent kprobe */
  1300. arch_remove_kprobe(p);
  1301. else if (list_is_singular(&p->list)) {
  1302. /* This is the last child of an aggrprobe */
  1303. ap = list_entry(p->list.next, struct kprobe, list);
  1304. list_del(&p->list);
  1305. free_aggr_kprobe(ap);
  1306. }
  1307. /* Otherwise, do nothing. */
  1308. }
  1309. int __kprobes register_kprobes(struct kprobe **kps, int num)
  1310. {
  1311. int i, ret = 0;
  1312. if (num <= 0)
  1313. return -EINVAL;
  1314. for (i = 0; i < num; i++) {
  1315. ret = register_kprobe(kps[i]);
  1316. if (ret < 0) {
  1317. if (i > 0)
  1318. unregister_kprobes(kps, i);
  1319. break;
  1320. }
  1321. }
  1322. return ret;
  1323. }
  1324. EXPORT_SYMBOL_GPL(register_kprobes);
  1325. void __kprobes unregister_kprobe(struct kprobe *p)
  1326. {
  1327. unregister_kprobes(&p, 1);
  1328. }
  1329. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1330. void __kprobes unregister_kprobes(struct kprobe **kps, int num)
  1331. {
  1332. int i;
  1333. if (num <= 0)
  1334. return;
  1335. mutex_lock(&kprobe_mutex);
  1336. for (i = 0; i < num; i++)
  1337. if (__unregister_kprobe_top(kps[i]) < 0)
  1338. kps[i]->addr = NULL;
  1339. mutex_unlock(&kprobe_mutex);
  1340. synchronize_sched();
  1341. for (i = 0; i < num; i++)
  1342. if (kps[i]->addr)
  1343. __unregister_kprobe_bottom(kps[i]);
  1344. }
  1345. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1346. static struct notifier_block kprobe_exceptions_nb = {
  1347. .notifier_call = kprobe_exceptions_notify,
  1348. .priority = 0x7fffffff /* we need to be notified first */
  1349. };
  1350. unsigned long __weak arch_deref_entry_point(void *entry)
  1351. {
  1352. return (unsigned long)entry;
  1353. }
  1354. int __kprobes register_jprobes(struct jprobe **jps, int num)
  1355. {
  1356. struct jprobe *jp;
  1357. int ret = 0, i;
  1358. if (num <= 0)
  1359. return -EINVAL;
  1360. for (i = 0; i < num; i++) {
  1361. unsigned long addr, offset;
  1362. jp = jps[i];
  1363. addr = arch_deref_entry_point(jp->entry);
  1364. /* Verify probepoint is a function entry point */
  1365. if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
  1366. offset == 0) {
  1367. jp->kp.pre_handler = setjmp_pre_handler;
  1368. jp->kp.break_handler = longjmp_break_handler;
  1369. ret = register_kprobe(&jp->kp);
  1370. } else
  1371. ret = -EINVAL;
  1372. if (ret < 0) {
  1373. if (i > 0)
  1374. unregister_jprobes(jps, i);
  1375. break;
  1376. }
  1377. }
  1378. return ret;
  1379. }
  1380. EXPORT_SYMBOL_GPL(register_jprobes);
  1381. int __kprobes register_jprobe(struct jprobe *jp)
  1382. {
  1383. return register_jprobes(&jp, 1);
  1384. }
  1385. EXPORT_SYMBOL_GPL(register_jprobe);
  1386. void __kprobes unregister_jprobe(struct jprobe *jp)
  1387. {
  1388. unregister_jprobes(&jp, 1);
  1389. }
  1390. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1391. void __kprobes unregister_jprobes(struct jprobe **jps, int num)
  1392. {
  1393. int i;
  1394. if (num <= 0)
  1395. return;
  1396. mutex_lock(&kprobe_mutex);
  1397. for (i = 0; i < num; i++)
  1398. if (__unregister_kprobe_top(&jps[i]->kp) < 0)
  1399. jps[i]->kp.addr = NULL;
  1400. mutex_unlock(&kprobe_mutex);
  1401. synchronize_sched();
  1402. for (i = 0; i < num; i++) {
  1403. if (jps[i]->kp.addr)
  1404. __unregister_kprobe_bottom(&jps[i]->kp);
  1405. }
  1406. }
  1407. EXPORT_SYMBOL_GPL(unregister_jprobes);
  1408. #ifdef CONFIG_KRETPROBES
  1409. /*
  1410. * This kprobe pre_handler is registered with every kretprobe. When probe
  1411. * hits it will set up the return probe.
  1412. */
  1413. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  1414. struct pt_regs *regs)
  1415. {
  1416. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  1417. unsigned long hash, flags = 0;
  1418. struct kretprobe_instance *ri;
  1419. /*TODO: consider to only swap the RA after the last pre_handler fired */
  1420. hash = hash_ptr(current, KPROBE_HASH_BITS);
  1421. spin_lock_irqsave(&rp->lock, flags);
  1422. if (!hlist_empty(&rp->free_instances)) {
  1423. ri = hlist_entry(rp->free_instances.first,
  1424. struct kretprobe_instance, hlist);
  1425. hlist_del(&ri->hlist);
  1426. spin_unlock_irqrestore(&rp->lock, flags);
  1427. ri->rp = rp;
  1428. ri->task = current;
  1429. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  1430. spin_lock_irqsave(&rp->lock, flags);
  1431. hlist_add_head(&ri->hlist, &rp->free_instances);
  1432. spin_unlock_irqrestore(&rp->lock, flags);
  1433. return 0;
  1434. }
  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);