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