uprobes.c 38 KB

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