kprobes.c 65 KB

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