kprobes.c 59 KB

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