kprobes.c 62 KB

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