uprobes.c 41 KB

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