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