uprobes.c 40 KB

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