uprobes.c 36 KB

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