kprobes.c 73 KB

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