kprobes.c 61 KB

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