uprobes.c 39 KB

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