kprobes.c 64 KB

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