uprobes.c 39 KB

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