kprobes.c 61 KB

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