uprobes.c 39 KB

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