uprobes.c 39 KB

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