uprobes.c 39 KB

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