kprobes.c 59 KB

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