uprobes.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. /*
  2. * User-space Probes (UProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2008-2012
  19. * Authors:
  20. * Srikar Dronamraju
  21. * Jim Keniston
  22. * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/highmem.h>
  26. #include <linux/pagemap.h> /* read_mapping_page */
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/rmap.h> /* anon_vma_prepare */
  30. #include <linux/mmu_notifier.h> /* set_pte_at_notify */
  31. #include <linux/swap.h> /* try_to_free_swap */
  32. #include <linux/ptrace.h> /* user_enable_single_step */
  33. #include <linux/kdebug.h> /* notifier mechanism */
  34. #include "../../mm/internal.h" /* munlock_vma_page */
  35. #include <linux/uprobes.h>
  36. #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
  37. #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
  38. static struct rb_root uprobes_tree = RB_ROOT;
  39. static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
  40. #define UPROBES_HASH_SZ 13
  41. /*
  42. * We need separate register/unregister and mmap/munmap lock hashes because
  43. * of mmap_sem nesting.
  44. *
  45. * uprobe_register() needs to install probes on (potentially) all processes
  46. * and thus needs to acquire multiple mmap_sems (consequtively, not
  47. * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
  48. * for the particular process doing the mmap.
  49. *
  50. * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
  51. * because of lock order against i_mmap_mutex. This means there's a hole in
  52. * the register vma iteration where a mmap() can happen.
  53. *
  54. * Thus uprobe_register() can race with uprobe_mmap() and we can try and
  55. * install a probe where one is already installed.
  56. */
  57. /* serialize (un)register */
  58. static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
  59. #define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  60. /* serialize uprobe->pending_list */
  61. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  62. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  63. /*
  64. * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
  65. * events active at this time. Probably a fine grained per inode count is
  66. * better?
  67. */
  68. static atomic_t uprobe_events = ATOMIC_INIT(0);
  69. struct uprobe {
  70. struct rb_node rb_node; /* node in the rb tree */
  71. atomic_t ref;
  72. struct rw_semaphore consumer_rwsem;
  73. struct list_head pending_list;
  74. struct uprobe_consumer *consumers;
  75. struct inode *inode; /* Also hold a ref to inode */
  76. loff_t offset;
  77. int flags;
  78. struct arch_uprobe arch;
  79. };
  80. /*
  81. * valid_vma: Verify if the specified vma is an executable vma
  82. * Relax restrictions while unregistering: vm_flags might have
  83. * changed after breakpoint was inserted.
  84. * - is_register: indicates if we are in register context.
  85. * - Return 1 if the specified virtual address is in an
  86. * executable vma.
  87. */
  88. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  89. {
  90. if (!vma->vm_file)
  91. return false;
  92. if (!is_register)
  93. return true;
  94. if ((vma->vm_flags & (VM_HUGETLB|VM_READ|VM_WRITE|VM_EXEC|VM_SHARED))
  95. == (VM_READ|VM_EXEC))
  96. return true;
  97. return false;
  98. }
  99. static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
  100. {
  101. return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  102. }
  103. static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
  104. {
  105. return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
  106. }
  107. /**
  108. * __replace_page - replace page in vma by new page.
  109. * based on replace_page in mm/ksm.c
  110. *
  111. * @vma: vma that holds the pte pointing to page
  112. * @addr: address the old @page is mapped at
  113. * @page: the cowed page we are replacing by kpage
  114. * @kpage: the modified page we replace page by
  115. *
  116. * Returns 0 on success, -EFAULT on failure.
  117. */
  118. static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
  119. struct page *page, struct page *kpage)
  120. {
  121. struct mm_struct *mm = vma->vm_mm;
  122. spinlock_t *ptl;
  123. pte_t *ptep;
  124. int err;
  125. /* For try_to_free_swap() and munlock_vma_page() below */
  126. lock_page(page);
  127. err = -EAGAIN;
  128. ptep = page_check_address(page, mm, addr, &ptl, 0);
  129. if (!ptep)
  130. goto unlock;
  131. get_page(kpage);
  132. page_add_new_anon_rmap(kpage, vma, addr);
  133. if (!PageAnon(page)) {
  134. dec_mm_counter(mm, MM_FILEPAGES);
  135. inc_mm_counter(mm, MM_ANONPAGES);
  136. }
  137. flush_cache_page(vma, addr, pte_pfn(*ptep));
  138. ptep_clear_flush(vma, addr, ptep);
  139. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  140. page_remove_rmap(page);
  141. if (!page_mapped(page))
  142. try_to_free_swap(page);
  143. pte_unmap_unlock(ptep, ptl);
  144. if (vma->vm_flags & VM_LOCKED)
  145. munlock_vma_page(page);
  146. put_page(page);
  147. err = 0;
  148. unlock:
  149. unlock_page(page);
  150. return err;
  151. }
  152. /**
  153. * is_swbp_insn - check if instruction is breakpoint instruction.
  154. * @insn: instruction to be checked.
  155. * Default implementation of is_swbp_insn
  156. * Returns true if @insn is a breakpoint instruction.
  157. */
  158. bool __weak is_swbp_insn(uprobe_opcode_t *insn)
  159. {
  160. return *insn == UPROBE_SWBP_INSN;
  161. }
  162. /*
  163. * NOTE:
  164. * Expect the breakpoint instruction to be the smallest size instruction for
  165. * the architecture. If an arch has variable length instruction and the
  166. * breakpoint instruction is not of the smallest length instruction
  167. * supported by that architecture then we need to modify read_opcode /
  168. * write_opcode accordingly. This would never be a problem for archs that
  169. * have fixed length instructions.
  170. */
  171. /*
  172. * write_opcode - write the opcode at a given virtual address.
  173. * @auprobe: arch breakpointing information.
  174. * @mm: the probed process address space.
  175. * @vaddr: the virtual address to store the opcode.
  176. * @opcode: opcode to be written at @vaddr.
  177. *
  178. * Called with mm->mmap_sem held (for read and with a reference to
  179. * mm).
  180. *
  181. * For mm @mm, write the opcode at @vaddr.
  182. * Return 0 (success) or a negative errno.
  183. */
  184. static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
  185. unsigned long vaddr, uprobe_opcode_t opcode)
  186. {
  187. struct page *old_page, *new_page;
  188. void *vaddr_old, *vaddr_new;
  189. struct vm_area_struct *vma;
  190. int ret;
  191. retry:
  192. /* Read the page with vaddr into memory */
  193. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
  194. if (ret <= 0)
  195. return ret;
  196. ret = -ENOMEM;
  197. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  198. if (!new_page)
  199. goto put_old;
  200. __SetPageUptodate(new_page);
  201. /* copy the page now that we've got it stable */
  202. vaddr_old = kmap_atomic(old_page);
  203. vaddr_new = kmap_atomic(new_page);
  204. memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
  205. memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
  206. kunmap_atomic(vaddr_new);
  207. kunmap_atomic(vaddr_old);
  208. ret = anon_vma_prepare(vma);
  209. if (ret)
  210. goto put_new;
  211. ret = __replace_page(vma, vaddr, old_page, new_page);
  212. put_new:
  213. page_cache_release(new_page);
  214. put_old:
  215. put_page(old_page);
  216. if (unlikely(ret == -EAGAIN))
  217. goto retry;
  218. return ret;
  219. }
  220. /**
  221. * read_opcode - read the opcode at a given virtual address.
  222. * @mm: the probed process address space.
  223. * @vaddr: the virtual address to read the opcode.
  224. * @opcode: location to store the read opcode.
  225. *
  226. * Called with mm->mmap_sem held (for read and with a reference to
  227. * mm.
  228. *
  229. * For mm @mm, read the opcode at @vaddr and store it in @opcode.
  230. * Return 0 (success) or a negative errno.
  231. */
  232. static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
  233. {
  234. struct page *page;
  235. void *vaddr_new;
  236. int ret;
  237. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
  238. if (ret <= 0)
  239. return ret;
  240. lock_page(page);
  241. vaddr_new = kmap_atomic(page);
  242. vaddr &= ~PAGE_MASK;
  243. memcpy(opcode, vaddr_new + vaddr, UPROBE_SWBP_INSN_SIZE);
  244. kunmap_atomic(vaddr_new);
  245. unlock_page(page);
  246. put_page(page);
  247. return 0;
  248. }
  249. static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
  250. {
  251. uprobe_opcode_t opcode;
  252. int result;
  253. if (current->mm == mm) {
  254. pagefault_disable();
  255. result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
  256. sizeof(opcode));
  257. pagefault_enable();
  258. if (likely(result == 0))
  259. goto out;
  260. }
  261. result = read_opcode(mm, vaddr, &opcode);
  262. if (result)
  263. return result;
  264. out:
  265. if (is_swbp_insn(&opcode))
  266. return 1;
  267. return 0;
  268. }
  269. /**
  270. * set_swbp - store breakpoint at a given address.
  271. * @auprobe: arch specific probepoint information.
  272. * @mm: the probed process address space.
  273. * @vaddr: the virtual address to insert the opcode.
  274. *
  275. * For mm @mm, store the breakpoint instruction at @vaddr.
  276. * Return 0 (success) or a negative errno.
  277. */
  278. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  279. {
  280. int result;
  281. /*
  282. * See the comment near uprobes_hash().
  283. */
  284. result = is_swbp_at_addr(mm, vaddr);
  285. if (result == 1)
  286. return -EEXIST;
  287. if (result)
  288. return result;
  289. return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
  290. }
  291. /**
  292. * set_orig_insn - Restore the original instruction.
  293. * @mm: the probed process address space.
  294. * @auprobe: arch specific probepoint information.
  295. * @vaddr: the virtual address to insert the opcode.
  296. * @verify: if true, verify existance of breakpoint instruction.
  297. *
  298. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  299. * Return 0 (success) or a negative errno.
  300. */
  301. int __weak
  302. set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, bool verify)
  303. {
  304. if (verify) {
  305. int result;
  306. result = is_swbp_at_addr(mm, vaddr);
  307. if (!result)
  308. return -EINVAL;
  309. if (result != 1)
  310. return result;
  311. }
  312. return write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
  313. }
  314. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  315. {
  316. if (l->inode < r->inode)
  317. return -1;
  318. if (l->inode > r->inode)
  319. return 1;
  320. if (l->offset < r->offset)
  321. return -1;
  322. if (l->offset > r->offset)
  323. return 1;
  324. return 0;
  325. }
  326. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  327. {
  328. struct uprobe u = { .inode = inode, .offset = offset };
  329. struct rb_node *n = uprobes_tree.rb_node;
  330. struct uprobe *uprobe;
  331. int match;
  332. while (n) {
  333. uprobe = rb_entry(n, struct uprobe, rb_node);
  334. match = match_uprobe(&u, uprobe);
  335. if (!match) {
  336. atomic_inc(&uprobe->ref);
  337. return uprobe;
  338. }
  339. if (match < 0)
  340. n = n->rb_left;
  341. else
  342. n = n->rb_right;
  343. }
  344. return NULL;
  345. }
  346. /*
  347. * Find a uprobe corresponding to a given inode:offset
  348. * Acquires uprobes_treelock
  349. */
  350. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  351. {
  352. struct uprobe *uprobe;
  353. unsigned long flags;
  354. spin_lock_irqsave(&uprobes_treelock, flags);
  355. uprobe = __find_uprobe(inode, offset);
  356. spin_unlock_irqrestore(&uprobes_treelock, flags);
  357. return uprobe;
  358. }
  359. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  360. {
  361. struct rb_node **p = &uprobes_tree.rb_node;
  362. struct rb_node *parent = NULL;
  363. struct uprobe *u;
  364. int match;
  365. while (*p) {
  366. parent = *p;
  367. u = rb_entry(parent, struct uprobe, rb_node);
  368. match = match_uprobe(uprobe, u);
  369. if (!match) {
  370. atomic_inc(&u->ref);
  371. return u;
  372. }
  373. if (match < 0)
  374. p = &parent->rb_left;
  375. else
  376. p = &parent->rb_right;
  377. }
  378. u = NULL;
  379. rb_link_node(&uprobe->rb_node, parent, p);
  380. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  381. /* get access + creation ref */
  382. atomic_set(&uprobe->ref, 2);
  383. return u;
  384. }
  385. /*
  386. * Acquire uprobes_treelock.
  387. * Matching uprobe already exists in rbtree;
  388. * increment (access refcount) and return the matching uprobe.
  389. *
  390. * No matching uprobe; insert the uprobe in rb_tree;
  391. * get a double refcount (access + creation) and return NULL.
  392. */
  393. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  394. {
  395. unsigned long flags;
  396. struct uprobe *u;
  397. spin_lock_irqsave(&uprobes_treelock, flags);
  398. u = __insert_uprobe(uprobe);
  399. spin_unlock_irqrestore(&uprobes_treelock, flags);
  400. /* For now assume that the instruction need not be single-stepped */
  401. uprobe->flags |= UPROBE_SKIP_SSTEP;
  402. return u;
  403. }
  404. static void put_uprobe(struct uprobe *uprobe)
  405. {
  406. if (atomic_dec_and_test(&uprobe->ref))
  407. kfree(uprobe);
  408. }
  409. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  410. {
  411. struct uprobe *uprobe, *cur_uprobe;
  412. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  413. if (!uprobe)
  414. return NULL;
  415. uprobe->inode = igrab(inode);
  416. uprobe->offset = offset;
  417. init_rwsem(&uprobe->consumer_rwsem);
  418. /* add to uprobes_tree, sorted on inode:offset */
  419. cur_uprobe = insert_uprobe(uprobe);
  420. /* a uprobe exists for this inode:offset combination */
  421. if (cur_uprobe) {
  422. kfree(uprobe);
  423. uprobe = cur_uprobe;
  424. iput(inode);
  425. } else {
  426. atomic_inc(&uprobe_events);
  427. }
  428. return uprobe;
  429. }
  430. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  431. {
  432. struct uprobe_consumer *uc;
  433. if (!(uprobe->flags & UPROBE_RUN_HANDLER))
  434. return;
  435. down_read(&uprobe->consumer_rwsem);
  436. for (uc = uprobe->consumers; uc; uc = uc->next) {
  437. if (!uc->filter || uc->filter(uc, current))
  438. uc->handler(uc, regs);
  439. }
  440. up_read(&uprobe->consumer_rwsem);
  441. }
  442. /* Returns the previous consumer */
  443. static struct uprobe_consumer *
  444. consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  445. {
  446. down_write(&uprobe->consumer_rwsem);
  447. uc->next = uprobe->consumers;
  448. uprobe->consumers = uc;
  449. up_write(&uprobe->consumer_rwsem);
  450. return uc->next;
  451. }
  452. /*
  453. * For uprobe @uprobe, delete the consumer @uc.
  454. * Return true if the @uc is deleted successfully
  455. * or return false.
  456. */
  457. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  458. {
  459. struct uprobe_consumer **con;
  460. bool ret = false;
  461. down_write(&uprobe->consumer_rwsem);
  462. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  463. if (*con == uc) {
  464. *con = uc->next;
  465. ret = true;
  466. break;
  467. }
  468. }
  469. up_write(&uprobe->consumer_rwsem);
  470. return ret;
  471. }
  472. static int
  473. __copy_insn(struct address_space *mapping, struct file *filp, char *insn,
  474. unsigned long nbytes, loff_t offset)
  475. {
  476. struct page *page;
  477. void *vaddr;
  478. unsigned long off;
  479. pgoff_t idx;
  480. if (!filp)
  481. return -EINVAL;
  482. if (!mapping->a_ops->readpage)
  483. return -EIO;
  484. idx = offset >> PAGE_CACHE_SHIFT;
  485. off = offset & ~PAGE_MASK;
  486. /*
  487. * Ensure that the page that has the original instruction is
  488. * populated and in page-cache.
  489. */
  490. page = read_mapping_page(mapping, idx, filp);
  491. if (IS_ERR(page))
  492. return PTR_ERR(page);
  493. vaddr = kmap_atomic(page);
  494. memcpy(insn, vaddr + off, nbytes);
  495. kunmap_atomic(vaddr);
  496. page_cache_release(page);
  497. return 0;
  498. }
  499. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  500. {
  501. struct address_space *mapping;
  502. unsigned long nbytes;
  503. int bytes;
  504. nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
  505. mapping = uprobe->inode->i_mapping;
  506. /* Instruction at end of binary; copy only available bytes */
  507. if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
  508. bytes = uprobe->inode->i_size - uprobe->offset;
  509. else
  510. bytes = MAX_UINSN_BYTES;
  511. /* Instruction at the page-boundary; copy bytes in second page */
  512. if (nbytes < bytes) {
  513. int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
  514. bytes - nbytes, uprobe->offset + nbytes);
  515. if (err)
  516. return err;
  517. bytes = nbytes;
  518. }
  519. return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
  520. }
  521. /*
  522. * How mm->uprobes_state.count gets updated
  523. * uprobe_mmap() increments the count if
  524. * - it successfully adds a breakpoint.
  525. * - it cannot add a breakpoint, but sees that there is a underlying
  526. * breakpoint (via a is_swbp_at_addr()).
  527. *
  528. * uprobe_munmap() decrements the count if
  529. * - it sees a underlying breakpoint, (via is_swbp_at_addr)
  530. * (Subsequent uprobe_unregister wouldnt find the breakpoint
  531. * unless a uprobe_mmap kicks in, since the old vma would be
  532. * dropped just after uprobe_munmap.)
  533. *
  534. * uprobe_register increments the count if:
  535. * - it successfully adds a breakpoint.
  536. *
  537. * uprobe_unregister decrements the count if:
  538. * - it sees a underlying breakpoint and removes successfully.
  539. * (via is_swbp_at_addr)
  540. * (Subsequent uprobe_munmap wouldnt find the breakpoint
  541. * since there is no underlying breakpoint after the
  542. * breakpoint removal.)
  543. */
  544. static int
  545. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  546. struct vm_area_struct *vma, unsigned long vaddr)
  547. {
  548. int ret;
  549. /*
  550. * If probe is being deleted, unregister thread could be done with
  551. * the vma-rmap-walk through. Adding a probe now can be fatal since
  552. * nobody will be able to cleanup. Also we could be from fork or
  553. * mremap path, where the probe might have already been inserted.
  554. * Hence behave as if probe already existed.
  555. */
  556. if (!uprobe->consumers)
  557. return -EEXIST;
  558. if (!(uprobe->flags & UPROBE_COPY_INSN)) {
  559. ret = copy_insn(uprobe, vma->vm_file);
  560. if (ret)
  561. return ret;
  562. if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
  563. return -ENOTSUPP;
  564. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  565. if (ret)
  566. return ret;
  567. /* write_opcode() assumes we don't cross page boundary */
  568. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  569. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  570. uprobe->flags |= UPROBE_COPY_INSN;
  571. }
  572. /*
  573. * Ideally, should be updating the probe count after the breakpoint
  574. * has been successfully inserted. However a thread could hit the
  575. * breakpoint we just inserted even before the probe count is
  576. * incremented. If this is the first breakpoint placed, breakpoint
  577. * notifier might ignore uprobes and pass the trap to the thread.
  578. * Hence increment before and decrement on failure.
  579. */
  580. atomic_inc(&mm->uprobes_state.count);
  581. ret = set_swbp(&uprobe->arch, mm, vaddr);
  582. if (ret)
  583. atomic_dec(&mm->uprobes_state.count);
  584. return ret;
  585. }
  586. static void
  587. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  588. {
  589. if (!set_orig_insn(&uprobe->arch, mm, vaddr, true))
  590. atomic_dec(&mm->uprobes_state.count);
  591. }
  592. /*
  593. * There could be threads that have already hit the breakpoint. They
  594. * will recheck the current insn and restart if find_uprobe() fails.
  595. * See find_active_uprobe().
  596. */
  597. static void delete_uprobe(struct uprobe *uprobe)
  598. {
  599. unsigned long flags;
  600. spin_lock_irqsave(&uprobes_treelock, flags);
  601. rb_erase(&uprobe->rb_node, &uprobes_tree);
  602. spin_unlock_irqrestore(&uprobes_treelock, flags);
  603. iput(uprobe->inode);
  604. put_uprobe(uprobe);
  605. atomic_dec(&uprobe_events);
  606. }
  607. struct map_info {
  608. struct map_info *next;
  609. struct mm_struct *mm;
  610. unsigned long vaddr;
  611. };
  612. static inline struct map_info *free_map_info(struct map_info *info)
  613. {
  614. struct map_info *next = info->next;
  615. kfree(info);
  616. return next;
  617. }
  618. static struct map_info *
  619. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  620. {
  621. unsigned long pgoff = offset >> PAGE_SHIFT;
  622. struct prio_tree_iter iter;
  623. struct vm_area_struct *vma;
  624. struct map_info *curr = NULL;
  625. struct map_info *prev = NULL;
  626. struct map_info *info;
  627. int more = 0;
  628. again:
  629. mutex_lock(&mapping->i_mmap_mutex);
  630. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  631. if (!valid_vma(vma, is_register))
  632. continue;
  633. if (!prev && !more) {
  634. /*
  635. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  636. * reclaim. This is optimistic, no harm done if it fails.
  637. */
  638. prev = kmalloc(sizeof(struct map_info),
  639. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  640. if (prev)
  641. prev->next = NULL;
  642. }
  643. if (!prev) {
  644. more++;
  645. continue;
  646. }
  647. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  648. continue;
  649. info = prev;
  650. prev = prev->next;
  651. info->next = curr;
  652. curr = info;
  653. info->mm = vma->vm_mm;
  654. info->vaddr = offset_to_vaddr(vma, offset);
  655. }
  656. mutex_unlock(&mapping->i_mmap_mutex);
  657. if (!more)
  658. goto out;
  659. prev = curr;
  660. while (curr) {
  661. mmput(curr->mm);
  662. curr = curr->next;
  663. }
  664. do {
  665. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  666. if (!info) {
  667. curr = ERR_PTR(-ENOMEM);
  668. goto out;
  669. }
  670. info->next = prev;
  671. prev = info;
  672. } while (--more);
  673. goto again;
  674. out:
  675. while (prev)
  676. prev = free_map_info(prev);
  677. return curr;
  678. }
  679. static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
  680. {
  681. struct map_info *info;
  682. int err = 0;
  683. info = build_map_info(uprobe->inode->i_mapping,
  684. uprobe->offset, is_register);
  685. if (IS_ERR(info))
  686. return PTR_ERR(info);
  687. while (info) {
  688. struct mm_struct *mm = info->mm;
  689. struct vm_area_struct *vma;
  690. if (err)
  691. goto free;
  692. down_write(&mm->mmap_sem);
  693. vma = find_vma(mm, info->vaddr);
  694. if (!vma || !valid_vma(vma, is_register) ||
  695. vma->vm_file->f_mapping->host != uprobe->inode)
  696. goto unlock;
  697. if (vma->vm_start > info->vaddr ||
  698. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  699. goto unlock;
  700. if (is_register) {
  701. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  702. /*
  703. * We can race against uprobe_mmap(), see the
  704. * comment near uprobe_hash().
  705. */
  706. if (err == -EEXIST)
  707. err = 0;
  708. } else {
  709. remove_breakpoint(uprobe, mm, info->vaddr);
  710. }
  711. unlock:
  712. up_write(&mm->mmap_sem);
  713. free:
  714. mmput(mm);
  715. info = free_map_info(info);
  716. }
  717. return err;
  718. }
  719. static int __uprobe_register(struct uprobe *uprobe)
  720. {
  721. return register_for_each_vma(uprobe, true);
  722. }
  723. static void __uprobe_unregister(struct uprobe *uprobe)
  724. {
  725. if (!register_for_each_vma(uprobe, false))
  726. delete_uprobe(uprobe);
  727. /* TODO : cant unregister? schedule a worker thread */
  728. }
  729. /*
  730. * uprobe_register - register a probe
  731. * @inode: the file in which the probe has to be placed.
  732. * @offset: offset from the start of the file.
  733. * @uc: information on howto handle the probe..
  734. *
  735. * Apart from the access refcount, uprobe_register() takes a creation
  736. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  737. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  738. * tuple). Creation refcount stops uprobe_unregister from freeing the
  739. * @uprobe even before the register operation is complete. Creation
  740. * refcount is released when the last @uc for the @uprobe
  741. * unregisters.
  742. *
  743. * Return errno if it cannot successully install probes
  744. * else return 0 (success)
  745. */
  746. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  747. {
  748. struct uprobe *uprobe;
  749. int ret;
  750. if (!inode || !uc || uc->next)
  751. return -EINVAL;
  752. if (offset > i_size_read(inode))
  753. return -EINVAL;
  754. ret = 0;
  755. mutex_lock(uprobes_hash(inode));
  756. uprobe = alloc_uprobe(inode, offset);
  757. if (uprobe && !consumer_add(uprobe, uc)) {
  758. ret = __uprobe_register(uprobe);
  759. if (ret) {
  760. uprobe->consumers = NULL;
  761. __uprobe_unregister(uprobe);
  762. } else {
  763. uprobe->flags |= UPROBE_RUN_HANDLER;
  764. }
  765. }
  766. mutex_unlock(uprobes_hash(inode));
  767. put_uprobe(uprobe);
  768. return ret;
  769. }
  770. /*
  771. * uprobe_unregister - unregister a already registered probe.
  772. * @inode: the file in which the probe has to be removed.
  773. * @offset: offset from the start of the file.
  774. * @uc: identify which probe if multiple probes are colocated.
  775. */
  776. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  777. {
  778. struct uprobe *uprobe;
  779. if (!inode || !uc)
  780. return;
  781. uprobe = find_uprobe(inode, offset);
  782. if (!uprobe)
  783. return;
  784. mutex_lock(uprobes_hash(inode));
  785. if (consumer_del(uprobe, uc)) {
  786. if (!uprobe->consumers) {
  787. __uprobe_unregister(uprobe);
  788. uprobe->flags &= ~UPROBE_RUN_HANDLER;
  789. }
  790. }
  791. mutex_unlock(uprobes_hash(inode));
  792. if (uprobe)
  793. put_uprobe(uprobe);
  794. }
  795. static struct rb_node *
  796. find_node_in_range(struct inode *inode, loff_t min, loff_t max)
  797. {
  798. struct rb_node *n = uprobes_tree.rb_node;
  799. while (n) {
  800. struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
  801. if (inode < u->inode) {
  802. n = n->rb_left;
  803. } else if (inode > u->inode) {
  804. n = n->rb_right;
  805. } else {
  806. if (max < u->offset)
  807. n = n->rb_left;
  808. else if (min > u->offset)
  809. n = n->rb_right;
  810. else
  811. break;
  812. }
  813. }
  814. return n;
  815. }
  816. /*
  817. * For a given range in vma, build a list of probes that need to be inserted.
  818. */
  819. static void build_probe_list(struct inode *inode,
  820. struct vm_area_struct *vma,
  821. unsigned long start, unsigned long end,
  822. struct list_head *head)
  823. {
  824. loff_t min, max;
  825. unsigned long flags;
  826. struct rb_node *n, *t;
  827. struct uprobe *u;
  828. INIT_LIST_HEAD(head);
  829. min = vaddr_to_offset(vma, start);
  830. max = min + (end - start) - 1;
  831. spin_lock_irqsave(&uprobes_treelock, flags);
  832. n = find_node_in_range(inode, min, max);
  833. if (n) {
  834. for (t = n; t; t = rb_prev(t)) {
  835. u = rb_entry(t, struct uprobe, rb_node);
  836. if (u->inode != inode || u->offset < min)
  837. break;
  838. list_add(&u->pending_list, head);
  839. atomic_inc(&u->ref);
  840. }
  841. for (t = n; (t = rb_next(t)); ) {
  842. u = rb_entry(t, struct uprobe, rb_node);
  843. if (u->inode != inode || u->offset > max)
  844. break;
  845. list_add(&u->pending_list, head);
  846. atomic_inc(&u->ref);
  847. }
  848. }
  849. spin_unlock_irqrestore(&uprobes_treelock, flags);
  850. }
  851. /*
  852. * Called from mmap_region.
  853. * called with mm->mmap_sem acquired.
  854. *
  855. * Return -ve no if we fail to insert probes and we cannot
  856. * bail-out.
  857. * Return 0 otherwise. i.e:
  858. *
  859. * - successful insertion of probes
  860. * - (or) no possible probes to be inserted.
  861. * - (or) insertion of probes failed but we can bail-out.
  862. */
  863. int uprobe_mmap(struct vm_area_struct *vma)
  864. {
  865. struct list_head tmp_list;
  866. struct uprobe *uprobe, *u;
  867. struct inode *inode;
  868. int ret, count;
  869. if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
  870. return 0;
  871. inode = vma->vm_file->f_mapping->host;
  872. if (!inode)
  873. return 0;
  874. mutex_lock(uprobes_mmap_hash(inode));
  875. build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
  876. ret = 0;
  877. count = 0;
  878. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  879. if (!ret) {
  880. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  881. ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  882. /*
  883. * We can race against uprobe_register(), see the
  884. * comment near uprobe_hash().
  885. */
  886. if (ret == -EEXIST) {
  887. ret = 0;
  888. if (!is_swbp_at_addr(vma->vm_mm, vaddr))
  889. continue;
  890. /*
  891. * Unable to insert a breakpoint, but
  892. * breakpoint lies underneath. Increment the
  893. * probe count.
  894. */
  895. atomic_inc(&vma->vm_mm->uprobes_state.count);
  896. }
  897. if (!ret)
  898. count++;
  899. }
  900. put_uprobe(uprobe);
  901. }
  902. mutex_unlock(uprobes_mmap_hash(inode));
  903. if (ret)
  904. atomic_sub(count, &vma->vm_mm->uprobes_state.count);
  905. return ret;
  906. }
  907. /*
  908. * Called in context of a munmap of a vma.
  909. */
  910. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  911. {
  912. struct list_head tmp_list;
  913. struct uprobe *uprobe, *u;
  914. struct inode *inode;
  915. if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
  916. return;
  917. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  918. return;
  919. if (!atomic_read(&vma->vm_mm->uprobes_state.count))
  920. return;
  921. inode = vma->vm_file->f_mapping->host;
  922. if (!inode)
  923. return;
  924. mutex_lock(uprobes_mmap_hash(inode));
  925. build_probe_list(inode, vma, start, end, &tmp_list);
  926. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  927. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  928. /*
  929. * An unregister could have removed the probe before
  930. * unmap. So check before we decrement the count.
  931. */
  932. if (is_swbp_at_addr(vma->vm_mm, vaddr) == 1)
  933. atomic_dec(&vma->vm_mm->uprobes_state.count);
  934. put_uprobe(uprobe);
  935. }
  936. mutex_unlock(uprobes_mmap_hash(inode));
  937. }
  938. /* Slot allocation for XOL */
  939. static int xol_add_vma(struct xol_area *area)
  940. {
  941. struct mm_struct *mm;
  942. int ret;
  943. area->page = alloc_page(GFP_HIGHUSER);
  944. if (!area->page)
  945. return -ENOMEM;
  946. ret = -EALREADY;
  947. mm = current->mm;
  948. down_write(&mm->mmap_sem);
  949. if (mm->uprobes_state.xol_area)
  950. goto fail;
  951. ret = -ENOMEM;
  952. /* Try to map as high as possible, this is only a hint. */
  953. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
  954. if (area->vaddr & ~PAGE_MASK) {
  955. ret = area->vaddr;
  956. goto fail;
  957. }
  958. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  959. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  960. if (ret)
  961. goto fail;
  962. smp_wmb(); /* pairs with get_xol_area() */
  963. mm->uprobes_state.xol_area = area;
  964. ret = 0;
  965. fail:
  966. up_write(&mm->mmap_sem);
  967. if (ret)
  968. __free_page(area->page);
  969. return ret;
  970. }
  971. static struct xol_area *get_xol_area(struct mm_struct *mm)
  972. {
  973. struct xol_area *area;
  974. area = mm->uprobes_state.xol_area;
  975. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  976. return area;
  977. }
  978. /*
  979. * xol_alloc_area - Allocate process's xol_area.
  980. * This area will be used for storing instructions for execution out of
  981. * line.
  982. *
  983. * Returns the allocated area or NULL.
  984. */
  985. static struct xol_area *xol_alloc_area(void)
  986. {
  987. struct xol_area *area;
  988. area = kzalloc(sizeof(*area), GFP_KERNEL);
  989. if (unlikely(!area))
  990. return NULL;
  991. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  992. if (!area->bitmap)
  993. goto fail;
  994. init_waitqueue_head(&area->wq);
  995. if (!xol_add_vma(area))
  996. return area;
  997. fail:
  998. kfree(area->bitmap);
  999. kfree(area);
  1000. return get_xol_area(current->mm);
  1001. }
  1002. /*
  1003. * uprobe_clear_state - Free the area allocated for slots.
  1004. */
  1005. void uprobe_clear_state(struct mm_struct *mm)
  1006. {
  1007. struct xol_area *area = mm->uprobes_state.xol_area;
  1008. if (!area)
  1009. return;
  1010. put_page(area->page);
  1011. kfree(area->bitmap);
  1012. kfree(area);
  1013. }
  1014. /*
  1015. * uprobe_reset_state - Free the area allocated for slots.
  1016. */
  1017. void uprobe_reset_state(struct mm_struct *mm)
  1018. {
  1019. mm->uprobes_state.xol_area = NULL;
  1020. atomic_set(&mm->uprobes_state.count, 0);
  1021. }
  1022. /*
  1023. * - search for a free slot.
  1024. */
  1025. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1026. {
  1027. unsigned long slot_addr;
  1028. int slot_nr;
  1029. do {
  1030. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1031. if (slot_nr < UINSNS_PER_PAGE) {
  1032. if (!test_and_set_bit(slot_nr, area->bitmap))
  1033. break;
  1034. slot_nr = UINSNS_PER_PAGE;
  1035. continue;
  1036. }
  1037. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1038. } while (slot_nr >= UINSNS_PER_PAGE);
  1039. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1040. atomic_inc(&area->slot_count);
  1041. return slot_addr;
  1042. }
  1043. /*
  1044. * xol_get_insn_slot - If was not allocated a slot, then
  1045. * allocate a slot.
  1046. * Returns the allocated slot address or 0.
  1047. */
  1048. static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
  1049. {
  1050. struct xol_area *area;
  1051. unsigned long offset;
  1052. void *vaddr;
  1053. area = get_xol_area(current->mm);
  1054. if (!area) {
  1055. area = xol_alloc_area();
  1056. if (!area)
  1057. return 0;
  1058. }
  1059. current->utask->xol_vaddr = xol_take_insn_slot(area);
  1060. /*
  1061. * Initialize the slot if xol_vaddr points to valid
  1062. * instruction slot.
  1063. */
  1064. if (unlikely(!current->utask->xol_vaddr))
  1065. return 0;
  1066. current->utask->vaddr = slot_addr;
  1067. offset = current->utask->xol_vaddr & ~PAGE_MASK;
  1068. vaddr = kmap_atomic(area->page);
  1069. memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
  1070. kunmap_atomic(vaddr);
  1071. return current->utask->xol_vaddr;
  1072. }
  1073. /*
  1074. * xol_free_insn_slot - If slot was earlier allocated by
  1075. * @xol_get_insn_slot(), make the slot available for
  1076. * subsequent requests.
  1077. */
  1078. static void xol_free_insn_slot(struct task_struct *tsk)
  1079. {
  1080. struct xol_area *area;
  1081. unsigned long vma_end;
  1082. unsigned long slot_addr;
  1083. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1084. return;
  1085. slot_addr = tsk->utask->xol_vaddr;
  1086. if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
  1087. return;
  1088. area = tsk->mm->uprobes_state.xol_area;
  1089. vma_end = area->vaddr + PAGE_SIZE;
  1090. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1091. unsigned long offset;
  1092. int slot_nr;
  1093. offset = slot_addr - area->vaddr;
  1094. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1095. if (slot_nr >= UINSNS_PER_PAGE)
  1096. return;
  1097. clear_bit(slot_nr, area->bitmap);
  1098. atomic_dec(&area->slot_count);
  1099. if (waitqueue_active(&area->wq))
  1100. wake_up(&area->wq);
  1101. tsk->utask->xol_vaddr = 0;
  1102. }
  1103. }
  1104. /**
  1105. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1106. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1107. * instruction.
  1108. * Return the address of the breakpoint instruction.
  1109. */
  1110. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1111. {
  1112. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1113. }
  1114. /*
  1115. * Called with no locks held.
  1116. * Called in context of a exiting or a exec-ing thread.
  1117. */
  1118. void uprobe_free_utask(struct task_struct *t)
  1119. {
  1120. struct uprobe_task *utask = t->utask;
  1121. if (!utask)
  1122. return;
  1123. if (utask->active_uprobe)
  1124. put_uprobe(utask->active_uprobe);
  1125. xol_free_insn_slot(t);
  1126. kfree(utask);
  1127. t->utask = NULL;
  1128. }
  1129. /*
  1130. * Called in context of a new clone/fork from copy_process.
  1131. */
  1132. void uprobe_copy_process(struct task_struct *t)
  1133. {
  1134. t->utask = NULL;
  1135. }
  1136. /*
  1137. * Allocate a uprobe_task object for the task.
  1138. * Called when the thread hits a breakpoint for the first time.
  1139. *
  1140. * Returns:
  1141. * - pointer to new uprobe_task on success
  1142. * - NULL otherwise
  1143. */
  1144. static struct uprobe_task *add_utask(void)
  1145. {
  1146. struct uprobe_task *utask;
  1147. utask = kzalloc(sizeof *utask, GFP_KERNEL);
  1148. if (unlikely(!utask))
  1149. return NULL;
  1150. current->utask = utask;
  1151. return utask;
  1152. }
  1153. /* Prepare to single-step probed instruction out of line. */
  1154. static int
  1155. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
  1156. {
  1157. if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
  1158. return 0;
  1159. return -EFAULT;
  1160. }
  1161. /*
  1162. * If we are singlestepping, then ensure this thread is not connected to
  1163. * non-fatal signals until completion of singlestep. When xol insn itself
  1164. * triggers the signal, restart the original insn even if the task is
  1165. * already SIGKILL'ed (since coredump should report the correct ip). This
  1166. * is even more important if the task has a handler for SIGSEGV/etc, The
  1167. * _same_ instruction should be repeated again after return from the signal
  1168. * handler, and SSTEP can never finish in this case.
  1169. */
  1170. bool uprobe_deny_signal(void)
  1171. {
  1172. struct task_struct *t = current;
  1173. struct uprobe_task *utask = t->utask;
  1174. if (likely(!utask || !utask->active_uprobe))
  1175. return false;
  1176. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1177. if (signal_pending(t)) {
  1178. spin_lock_irq(&t->sighand->siglock);
  1179. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1180. spin_unlock_irq(&t->sighand->siglock);
  1181. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1182. utask->state = UTASK_SSTEP_TRAPPED;
  1183. set_tsk_thread_flag(t, TIF_UPROBE);
  1184. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1185. }
  1186. }
  1187. return true;
  1188. }
  1189. /*
  1190. * Avoid singlestepping the original instruction if the original instruction
  1191. * is a NOP or can be emulated.
  1192. */
  1193. static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
  1194. {
  1195. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1196. return true;
  1197. uprobe->flags &= ~UPROBE_SKIP_SSTEP;
  1198. return false;
  1199. }
  1200. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1201. {
  1202. struct mm_struct *mm = current->mm;
  1203. struct uprobe *uprobe = NULL;
  1204. struct vm_area_struct *vma;
  1205. down_read(&mm->mmap_sem);
  1206. vma = find_vma(mm, bp_vaddr);
  1207. if (vma && vma->vm_start <= bp_vaddr) {
  1208. if (valid_vma(vma, false)) {
  1209. struct inode *inode = vma->vm_file->f_mapping->host;
  1210. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1211. uprobe = find_uprobe(inode, offset);
  1212. }
  1213. if (!uprobe)
  1214. *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
  1215. } else {
  1216. *is_swbp = -EFAULT;
  1217. }
  1218. up_read(&mm->mmap_sem);
  1219. return uprobe;
  1220. }
  1221. /*
  1222. * Run handler and ask thread to singlestep.
  1223. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1224. */
  1225. static void handle_swbp(struct pt_regs *regs)
  1226. {
  1227. struct uprobe_task *utask;
  1228. struct uprobe *uprobe;
  1229. unsigned long bp_vaddr;
  1230. int uninitialized_var(is_swbp);
  1231. bp_vaddr = uprobe_get_swbp_addr(regs);
  1232. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1233. if (!uprobe) {
  1234. if (is_swbp > 0) {
  1235. /* No matching uprobe; signal SIGTRAP. */
  1236. send_sig(SIGTRAP, current, 0);
  1237. } else {
  1238. /*
  1239. * Either we raced with uprobe_unregister() or we can't
  1240. * access this memory. The latter is only possible if
  1241. * another thread plays with our ->mm. In both cases
  1242. * we can simply restart. If this vma was unmapped we
  1243. * can pretend this insn was not executed yet and get
  1244. * the (correct) SIGSEGV after restart.
  1245. */
  1246. instruction_pointer_set(regs, bp_vaddr);
  1247. }
  1248. return;
  1249. }
  1250. utask = current->utask;
  1251. if (!utask) {
  1252. utask = add_utask();
  1253. /* Cannot allocate; re-execute the instruction. */
  1254. if (!utask)
  1255. goto cleanup_ret;
  1256. }
  1257. utask->active_uprobe = uprobe;
  1258. handler_chain(uprobe, regs);
  1259. if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
  1260. goto cleanup_ret;
  1261. utask->state = UTASK_SSTEP;
  1262. if (!pre_ssout(uprobe, regs, bp_vaddr)) {
  1263. user_enable_single_step(current);
  1264. return;
  1265. }
  1266. cleanup_ret:
  1267. if (utask) {
  1268. utask->active_uprobe = NULL;
  1269. utask->state = UTASK_RUNNING;
  1270. }
  1271. if (uprobe) {
  1272. if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
  1273. /*
  1274. * cannot singlestep; cannot skip instruction;
  1275. * re-execute the instruction.
  1276. */
  1277. instruction_pointer_set(regs, bp_vaddr);
  1278. put_uprobe(uprobe);
  1279. }
  1280. }
  1281. /*
  1282. * Perform required fix-ups and disable singlestep.
  1283. * Allow pending signals to take effect.
  1284. */
  1285. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1286. {
  1287. struct uprobe *uprobe;
  1288. uprobe = utask->active_uprobe;
  1289. if (utask->state == UTASK_SSTEP_ACK)
  1290. arch_uprobe_post_xol(&uprobe->arch, regs);
  1291. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1292. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1293. else
  1294. WARN_ON_ONCE(1);
  1295. put_uprobe(uprobe);
  1296. utask->active_uprobe = NULL;
  1297. utask->state = UTASK_RUNNING;
  1298. user_disable_single_step(current);
  1299. xol_free_insn_slot(current);
  1300. spin_lock_irq(&current->sighand->siglock);
  1301. recalc_sigpending(); /* see uprobe_deny_signal() */
  1302. spin_unlock_irq(&current->sighand->siglock);
  1303. }
  1304. /*
  1305. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag. (and on
  1306. * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
  1307. * allows the thread to return from interrupt.
  1308. *
  1309. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
  1310. * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
  1311. * interrupt.
  1312. *
  1313. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1314. * uprobe_notify_resume().
  1315. */
  1316. void uprobe_notify_resume(struct pt_regs *regs)
  1317. {
  1318. struct uprobe_task *utask;
  1319. utask = current->utask;
  1320. if (!utask || utask->state == UTASK_BP_HIT)
  1321. handle_swbp(regs);
  1322. else
  1323. handle_singlestep(utask, regs);
  1324. }
  1325. /*
  1326. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1327. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1328. */
  1329. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1330. {
  1331. struct uprobe_task *utask;
  1332. if (!current->mm || !atomic_read(&current->mm->uprobes_state.count))
  1333. /* task is currently not uprobed */
  1334. return 0;
  1335. utask = current->utask;
  1336. if (utask)
  1337. utask->state = UTASK_BP_HIT;
  1338. set_thread_flag(TIF_UPROBE);
  1339. return 1;
  1340. }
  1341. /*
  1342. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1343. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1344. */
  1345. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1346. {
  1347. struct uprobe_task *utask = current->utask;
  1348. if (!current->mm || !utask || !utask->active_uprobe)
  1349. /* task is currently not uprobed */
  1350. return 0;
  1351. utask->state = UTASK_SSTEP_ACK;
  1352. set_thread_flag(TIF_UPROBE);
  1353. return 1;
  1354. }
  1355. static struct notifier_block uprobe_exception_nb = {
  1356. .notifier_call = arch_uprobe_exception_notify,
  1357. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1358. };
  1359. static int __init init_uprobes(void)
  1360. {
  1361. int i;
  1362. for (i = 0; i < UPROBES_HASH_SZ; i++) {
  1363. mutex_init(&uprobes_mutex[i]);
  1364. mutex_init(&uprobes_mmap_mutex[i]);
  1365. }
  1366. return register_die_notifier(&uprobe_exception_nb);
  1367. }
  1368. module_init(init_uprobes);
  1369. static void __exit exit_uprobes(void)
  1370. {
  1371. }
  1372. module_exit(exit_uprobes);