uprobes.c 39 KB

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