kprobes.c 65 KB

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