uprobes.c 39 KB

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