uprobes.c 39 KB

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