• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * User-space Probes (UProbes)
4  *
5  * Copyright (C) IBM Corporation, 2008-2012
6  * Authors:
7  *	Srikar Dronamraju
8  *	Jim Keniston
9  * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/highmem.h>
14 #include <linux/pagemap.h>	/* read_mapping_page */
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/sched/mm.h>
18 #include <linux/sched/coredump.h>
19 #include <linux/export.h>
20 #include <linux/rmap.h>		/* anon_vma_prepare */
21 #include <linux/mmu_notifier.h>
22 #include <linux/swap.h>		/* folio_free_swap */
23 #include <linux/ptrace.h>	/* user_enable_single_step */
24 #include <linux/kdebug.h>	/* notifier mechanism */
25 #include <linux/percpu-rwsem.h>
26 #include <linux/task_work.h>
27 #include <linux/shmem_fs.h>
28 #include <linux/khugepaged.h>
29 
30 #include <linux/uprobes.h>
31 
32 #undef CREATE_TRACE_POINTS
33 #include <trace/hooks/mm.h>
34 
35 #define UINSNS_PER_PAGE			(PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
36 #define MAX_UPROBE_XOL_SLOTS		UINSNS_PER_PAGE
37 
38 static struct rb_root uprobes_tree = RB_ROOT;
39 /*
40  * allows us to skip the uprobe_mmap if there are no uprobe events active
41  * at this time.  Probably a fine grained per inode count is better?
42  */
43 #define no_uprobe_events()	RB_EMPTY_ROOT(&uprobes_tree)
44 
45 static DEFINE_RWLOCK(uprobes_treelock);	/* serialize rbtree access */
46 static seqcount_rwlock_t uprobes_seqcount = SEQCNT_RWLOCK_ZERO(uprobes_seqcount, &uprobes_treelock);
47 
48 DEFINE_STATIC_SRCU(uprobes_srcu);
49 
50 #define UPROBES_HASH_SZ	13
51 /* serialize uprobe->pending_list */
52 static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
53 #define uprobes_mmap_hash(v)	(&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
54 
55 DEFINE_STATIC_PERCPU_RWSEM(dup_mmap_sem);
56 
57 /* Have a copy of original instruction */
58 #define UPROBE_COPY_INSN	0
59 
60 struct uprobe {
61 	struct rb_node		rb_node;	/* node in the rb tree */
62 	refcount_t		ref;
63 	struct rw_semaphore	register_rwsem;
64 	struct rw_semaphore	consumer_rwsem;
65 	struct list_head	pending_list;
66 	struct list_head	consumers;
67 	struct inode		*inode;		/* Also hold a ref to inode */
68 	struct rcu_head		rcu;
69 	loff_t			offset;
70 	loff_t			ref_ctr_offset;
71 	unsigned long		flags;
72 
73 	/*
74 	 * The generic code assumes that it has two members of unknown type
75 	 * owned by the arch-specific code:
76 	 *
77 	 * 	insn -	copy_insn() saves the original instruction here for
78 	 *		arch_uprobe_analyze_insn().
79 	 *
80 	 *	ixol -	potentially modified instruction to execute out of
81 	 *		line, copied to xol_area by xol_get_insn_slot().
82 	 */
83 	struct arch_uprobe	arch;
84 };
85 
86 struct delayed_uprobe {
87 	struct list_head list;
88 	struct uprobe *uprobe;
89 	struct mm_struct *mm;
90 };
91 
92 static DEFINE_MUTEX(delayed_uprobe_lock);
93 static LIST_HEAD(delayed_uprobe_list);
94 
95 /*
96  * Execute out of line area: anonymous executable mapping installed
97  * by the probed task to execute the copy of the original instruction
98  * mangled by set_swbp().
99  *
100  * On a breakpoint hit, thread contests for a slot.  It frees the
101  * slot after singlestep. Currently a fixed number of slots are
102  * allocated.
103  */
104 struct xol_area {
105 	wait_queue_head_t 		wq;		/* if all slots are busy */
106 	atomic_t 			slot_count;	/* number of in-use slots */
107 	unsigned long 			*bitmap;	/* 0 = free slot */
108 
109 	struct page			*page;
110 	/*
111 	 * We keep the vma's vm_start rather than a pointer to the vma
112 	 * itself.  The probed process or a naughty kernel module could make
113 	 * the vma go away, and we must handle that reasonably gracefully.
114 	 */
115 	unsigned long 			vaddr;		/* Page(s) of instruction slots */
116 };
117 
uprobe_warn(struct task_struct * t,const char * msg)118 static void uprobe_warn(struct task_struct *t, const char *msg)
119 {
120 	pr_warn("uprobe: %s:%d failed to %s\n", current->comm, current->pid, msg);
121 }
122 
123 /*
124  * valid_vma: Verify if the specified vma is an executable vma
125  * Relax restrictions while unregistering: vm_flags might have
126  * changed after breakpoint was inserted.
127  *	- is_register: indicates if we are in register context.
128  *	- Return 1 if the specified virtual address is in an
129  *	  executable vma.
130  */
valid_vma(struct vm_area_struct * vma,bool is_register)131 static bool valid_vma(struct vm_area_struct *vma, bool is_register)
132 {
133 	vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE;
134 
135 	if (is_register)
136 		flags |= VM_WRITE;
137 
138 	return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
139 }
140 
offset_to_vaddr(struct vm_area_struct * vma,loff_t offset)141 static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
142 {
143 	return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
144 }
145 
vaddr_to_offset(struct vm_area_struct * vma,unsigned long vaddr)146 static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
147 {
148 	return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
149 }
150 
151 /**
152  * __replace_page - replace page in vma by new page.
153  * based on replace_page in mm/ksm.c
154  *
155  * @vma:      vma that holds the pte pointing to page
156  * @addr:     address the old @page is mapped at
157  * @old_page: the page we are replacing by new_page
158  * @new_page: the modified page we replace page by
159  *
160  * If @new_page is NULL, only unmap @old_page.
161  *
162  * Returns 0 on success, negative error code otherwise.
163  */
__replace_page(struct vm_area_struct * vma,unsigned long addr,struct page * old_page,struct page * new_page)164 static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
165 				struct page *old_page, struct page *new_page)
166 {
167 	struct folio *old_folio = page_folio(old_page);
168 	struct folio *new_folio;
169 	struct mm_struct *mm = vma->vm_mm;
170 	DEFINE_FOLIO_VMA_WALK(pvmw, old_folio, vma, addr, 0);
171 	int err;
172 	struct mmu_notifier_range range;
173 	pte_t pte;
174 
175 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, addr,
176 				addr + PAGE_SIZE);
177 
178 	if (new_page) {
179 		new_folio = page_folio(new_page);
180 		err = mem_cgroup_charge(new_folio, vma->vm_mm, GFP_KERNEL);
181 		if (err)
182 			return err;
183 	}
184 
185 	/* For folio_free_swap() below */
186 	folio_lock(old_folio);
187 
188 	mmu_notifier_invalidate_range_start(&range);
189 	err = -EAGAIN;
190 	if (!page_vma_mapped_walk(&pvmw))
191 		goto unlock;
192 	VM_BUG_ON_PAGE(addr != pvmw.address, old_page);
193 	pte = ptep_get(pvmw.pte);
194 
195 	/*
196 	 * Handle PFN swap PTES, such as device-exclusive ones, that actually
197 	 * map pages: simply trigger GUP again to fix it up.
198 	 */
199 	if (unlikely(!pte_present(pte))) {
200 		page_vma_mapped_walk_done(&pvmw);
201 		goto unlock;
202 	}
203 
204 	if (new_page) {
205 		folio_get(new_folio);
206 		folio_add_new_anon_rmap(new_folio, vma, addr, RMAP_EXCLUSIVE);
207 		folio_add_lru_vma(new_folio, vma);
208 		trace_android_vh_uprobes_replace_page(new_folio, old_folio);
209 	} else
210 		/* no new page, just dec_mm_counter for old_page */
211 		dec_mm_counter(mm, MM_ANONPAGES);
212 
213 	if (!folio_test_anon(old_folio)) {
214 		dec_mm_counter(mm, mm_counter_file(old_folio));
215 		inc_mm_counter(mm, MM_ANONPAGES);
216 	}
217 
218 	flush_cache_page(vma, addr, pte_pfn(pte));
219 	ptep_clear_flush(vma, addr, pvmw.pte);
220 	if (new_page)
221 		set_pte_at(mm, addr, pvmw.pte,
222 			   mk_pte(new_page, vma->vm_page_prot));
223 
224 	folio_remove_rmap_pte(old_folio, old_page, vma);
225 	if (!folio_mapped(old_folio))
226 		folio_free_swap(old_folio);
227 	page_vma_mapped_walk_done(&pvmw);
228 	folio_put(old_folio);
229 
230 	err = 0;
231  unlock:
232 	mmu_notifier_invalidate_range_end(&range);
233 	folio_unlock(old_folio);
234 	return err;
235 }
236 
237 /**
238  * is_swbp_insn - check if instruction is breakpoint instruction.
239  * @insn: instruction to be checked.
240  * Default implementation of is_swbp_insn
241  * Returns true if @insn is a breakpoint instruction.
242  */
is_swbp_insn(uprobe_opcode_t * insn)243 bool __weak is_swbp_insn(uprobe_opcode_t *insn)
244 {
245 	return *insn == UPROBE_SWBP_INSN;
246 }
247 
248 /**
249  * is_trap_insn - check if instruction is breakpoint instruction.
250  * @insn: instruction to be checked.
251  * Default implementation of is_trap_insn
252  * Returns true if @insn is a breakpoint instruction.
253  *
254  * This function is needed for the case where an architecture has multiple
255  * trap instructions (like powerpc).
256  */
is_trap_insn(uprobe_opcode_t * insn)257 bool __weak is_trap_insn(uprobe_opcode_t *insn)
258 {
259 	return is_swbp_insn(insn);
260 }
261 
copy_from_page(struct page * page,unsigned long vaddr,void * dst,int len)262 static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
263 {
264 	void *kaddr = kmap_atomic(page);
265 	memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
266 	kunmap_atomic(kaddr);
267 }
268 
copy_to_page(struct page * page,unsigned long vaddr,const void * src,int len)269 static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len)
270 {
271 	void *kaddr = kmap_atomic(page);
272 	memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
273 	kunmap_atomic(kaddr);
274 }
275 
verify_opcode(struct page * page,unsigned long vaddr,uprobe_opcode_t * new_opcode)276 static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
277 {
278 	uprobe_opcode_t old_opcode;
279 	bool is_swbp;
280 
281 	/*
282 	 * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
283 	 * We do not check if it is any other 'trap variant' which could
284 	 * be conditional trap instruction such as the one powerpc supports.
285 	 *
286 	 * The logic is that we do not care if the underlying instruction
287 	 * is a trap variant; uprobes always wins over any other (gdb)
288 	 * breakpoint.
289 	 */
290 	copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
291 	is_swbp = is_swbp_insn(&old_opcode);
292 
293 	if (is_swbp_insn(new_opcode)) {
294 		if (is_swbp)		/* register: already installed? */
295 			return 0;
296 	} else {
297 		if (!is_swbp)		/* unregister: was it changed by us? */
298 			return 0;
299 	}
300 
301 	return 1;
302 }
303 
304 static struct delayed_uprobe *
delayed_uprobe_check(struct uprobe * uprobe,struct mm_struct * mm)305 delayed_uprobe_check(struct uprobe *uprobe, struct mm_struct *mm)
306 {
307 	struct delayed_uprobe *du;
308 
309 	list_for_each_entry(du, &delayed_uprobe_list, list)
310 		if (du->uprobe == uprobe && du->mm == mm)
311 			return du;
312 	return NULL;
313 }
314 
delayed_uprobe_add(struct uprobe * uprobe,struct mm_struct * mm)315 static int delayed_uprobe_add(struct uprobe *uprobe, struct mm_struct *mm)
316 {
317 	struct delayed_uprobe *du;
318 
319 	if (delayed_uprobe_check(uprobe, mm))
320 		return 0;
321 
322 	du  = kzalloc(sizeof(*du), GFP_KERNEL);
323 	if (!du)
324 		return -ENOMEM;
325 
326 	du->uprobe = uprobe;
327 	du->mm = mm;
328 	list_add(&du->list, &delayed_uprobe_list);
329 	return 0;
330 }
331 
delayed_uprobe_delete(struct delayed_uprobe * du)332 static void delayed_uprobe_delete(struct delayed_uprobe *du)
333 {
334 	if (WARN_ON(!du))
335 		return;
336 	list_del(&du->list);
337 	kfree(du);
338 }
339 
delayed_uprobe_remove(struct uprobe * uprobe,struct mm_struct * mm)340 static void delayed_uprobe_remove(struct uprobe *uprobe, struct mm_struct *mm)
341 {
342 	struct list_head *pos, *q;
343 	struct delayed_uprobe *du;
344 
345 	if (!uprobe && !mm)
346 		return;
347 
348 	list_for_each_safe(pos, q, &delayed_uprobe_list) {
349 		du = list_entry(pos, struct delayed_uprobe, list);
350 
351 		if (uprobe && du->uprobe != uprobe)
352 			continue;
353 		if (mm && du->mm != mm)
354 			continue;
355 
356 		delayed_uprobe_delete(du);
357 	}
358 }
359 
valid_ref_ctr_vma(struct uprobe * uprobe,struct vm_area_struct * vma)360 static bool valid_ref_ctr_vma(struct uprobe *uprobe,
361 			      struct vm_area_struct *vma)
362 {
363 	unsigned long vaddr = offset_to_vaddr(vma, uprobe->ref_ctr_offset);
364 
365 	return uprobe->ref_ctr_offset &&
366 		vma->vm_file &&
367 		file_inode(vma->vm_file) == uprobe->inode &&
368 		(vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE &&
369 		vma->vm_start <= vaddr &&
370 		vma->vm_end > vaddr;
371 }
372 
373 static struct vm_area_struct *
find_ref_ctr_vma(struct uprobe * uprobe,struct mm_struct * mm)374 find_ref_ctr_vma(struct uprobe *uprobe, struct mm_struct *mm)
375 {
376 	VMA_ITERATOR(vmi, mm, 0);
377 	struct vm_area_struct *tmp;
378 
379 	for_each_vma(vmi, tmp)
380 		if (valid_ref_ctr_vma(uprobe, tmp))
381 			return tmp;
382 
383 	return NULL;
384 }
385 
386 static int
__update_ref_ctr(struct mm_struct * mm,unsigned long vaddr,short d)387 __update_ref_ctr(struct mm_struct *mm, unsigned long vaddr, short d)
388 {
389 	void *kaddr;
390 	struct page *page;
391 	int ret;
392 	short *ptr;
393 
394 	if (!vaddr || !d)
395 		return -EINVAL;
396 
397 	ret = get_user_pages_remote(mm, vaddr, 1,
398 				    FOLL_WRITE, &page, NULL);
399 	if (unlikely(ret <= 0)) {
400 		/*
401 		 * We are asking for 1 page. If get_user_pages_remote() fails,
402 		 * it may return 0, in that case we have to return error.
403 		 */
404 		return ret == 0 ? -EBUSY : ret;
405 	}
406 
407 	kaddr = kmap_atomic(page);
408 	ptr = kaddr + (vaddr & ~PAGE_MASK);
409 
410 	if (unlikely(*ptr + d < 0)) {
411 		pr_warn("ref_ctr going negative. vaddr: 0x%lx, "
412 			"curr val: %d, delta: %d\n", vaddr, *ptr, d);
413 		ret = -EINVAL;
414 		goto out;
415 	}
416 
417 	*ptr += d;
418 	ret = 0;
419 out:
420 	kunmap_atomic(kaddr);
421 	put_page(page);
422 	return ret;
423 }
424 
update_ref_ctr_warn(struct uprobe * uprobe,struct mm_struct * mm,short d)425 static void update_ref_ctr_warn(struct uprobe *uprobe,
426 				struct mm_struct *mm, short d)
427 {
428 	pr_warn("ref_ctr %s failed for inode: 0x%lx offset: "
429 		"0x%llx ref_ctr_offset: 0x%llx of mm: 0x%pK\n",
430 		d > 0 ? "increment" : "decrement", uprobe->inode->i_ino,
431 		(unsigned long long) uprobe->offset,
432 		(unsigned long long) uprobe->ref_ctr_offset, mm);
433 }
434 
update_ref_ctr(struct uprobe * uprobe,struct mm_struct * mm,short d)435 static int update_ref_ctr(struct uprobe *uprobe, struct mm_struct *mm,
436 			  short d)
437 {
438 	struct vm_area_struct *rc_vma;
439 	unsigned long rc_vaddr;
440 	int ret = 0;
441 
442 	rc_vma = find_ref_ctr_vma(uprobe, mm);
443 
444 	if (rc_vma) {
445 		rc_vaddr = offset_to_vaddr(rc_vma, uprobe->ref_ctr_offset);
446 		ret = __update_ref_ctr(mm, rc_vaddr, d);
447 		if (ret)
448 			update_ref_ctr_warn(uprobe, mm, d);
449 
450 		if (d > 0)
451 			return ret;
452 	}
453 
454 	mutex_lock(&delayed_uprobe_lock);
455 	if (d > 0)
456 		ret = delayed_uprobe_add(uprobe, mm);
457 	else
458 		delayed_uprobe_remove(uprobe, mm);
459 	mutex_unlock(&delayed_uprobe_lock);
460 
461 	return ret;
462 }
463 
464 /*
465  * NOTE:
466  * Expect the breakpoint instruction to be the smallest size instruction for
467  * the architecture. If an arch has variable length instruction and the
468  * breakpoint instruction is not of the smallest length instruction
469  * supported by that architecture then we need to modify is_trap_at_addr and
470  * uprobe_write_opcode accordingly. This would never be a problem for archs
471  * that have fixed length instructions.
472  *
473  * uprobe_write_opcode - write the opcode at a given virtual address.
474  * @auprobe: arch specific probepoint information.
475  * @mm: the probed process address space.
476  * @vaddr: the virtual address to store the opcode.
477  * @opcode: opcode to be written at @vaddr.
478  *
479  * Called with mm->mmap_lock held for read or write.
480  * Return 0 (success) or a negative errno.
481  */
uprobe_write_opcode(struct arch_uprobe * auprobe,struct mm_struct * mm,unsigned long vaddr,uprobe_opcode_t opcode)482 int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
483 			unsigned long vaddr, uprobe_opcode_t opcode)
484 {
485 	struct uprobe *uprobe;
486 	struct page *old_page, *new_page;
487 	struct vm_area_struct *vma;
488 	int ret, is_register, ref_ctr_updated = 0;
489 	bool orig_page_huge = false;
490 	unsigned int gup_flags = FOLL_FORCE;
491 
492 	is_register = is_swbp_insn(&opcode);
493 	uprobe = container_of(auprobe, struct uprobe, arch);
494 
495 retry:
496 	if (is_register)
497 		gup_flags |= FOLL_SPLIT_PMD;
498 	/* Read the page with vaddr into memory */
499 	old_page = get_user_page_vma_remote(mm, vaddr, gup_flags, &vma);
500 	if (IS_ERR(old_page))
501 		return PTR_ERR(old_page);
502 
503 	ret = verify_opcode(old_page, vaddr, &opcode);
504 	if (ret <= 0)
505 		goto put_old;
506 
507 	if (is_zero_page(old_page)) {
508 		ret = -EINVAL;
509 		goto put_old;
510 	}
511 
512 	if (WARN(!is_register && PageCompound(old_page),
513 		 "uprobe unregister should never work on compound page\n")) {
514 		ret = -EINVAL;
515 		goto put_old;
516 	}
517 
518 	/* We are going to replace instruction, update ref_ctr. */
519 	if (!ref_ctr_updated && uprobe->ref_ctr_offset) {
520 		ret = update_ref_ctr(uprobe, mm, is_register ? 1 : -1);
521 		if (ret)
522 			goto put_old;
523 
524 		ref_ctr_updated = 1;
525 	}
526 
527 	ret = 0;
528 	if (!is_register && !PageAnon(old_page))
529 		goto put_old;
530 
531 	ret = anon_vma_prepare(vma);
532 	if (ret)
533 		goto put_old;
534 
535 	ret = -ENOMEM;
536 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
537 	if (!new_page)
538 		goto put_old;
539 
540 	__SetPageUptodate(new_page);
541 	copy_highpage(new_page, old_page);
542 	copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
543 
544 	if (!is_register) {
545 		struct page *orig_page;
546 		pgoff_t index;
547 
548 		VM_BUG_ON_PAGE(!PageAnon(old_page), old_page);
549 
550 		index = vaddr_to_offset(vma, vaddr & PAGE_MASK) >> PAGE_SHIFT;
551 		orig_page = find_get_page(vma->vm_file->f_inode->i_mapping,
552 					  index);
553 
554 		if (orig_page) {
555 			if (PageUptodate(orig_page) &&
556 			    pages_identical(new_page, orig_page)) {
557 				/* let go new_page */
558 				put_page(new_page);
559 				new_page = NULL;
560 
561 				if (PageCompound(orig_page))
562 					orig_page_huge = true;
563 			}
564 			put_page(orig_page);
565 		}
566 	}
567 
568 	ret = __replace_page(vma, vaddr & PAGE_MASK, old_page, new_page);
569 	if (new_page)
570 		put_page(new_page);
571 put_old:
572 	put_page(old_page);
573 
574 	if (unlikely(ret == -EAGAIN))
575 		goto retry;
576 
577 	/* Revert back reference counter if instruction update failed. */
578 	if (ret && is_register && ref_ctr_updated)
579 		update_ref_ctr(uprobe, mm, -1);
580 
581 	/* try collapse pmd for compound page */
582 	if (!ret && orig_page_huge)
583 		collapse_pte_mapped_thp(mm, vaddr, false);
584 
585 	return ret;
586 }
587 
588 /**
589  * set_swbp - store breakpoint at a given address.
590  * @auprobe: arch specific probepoint information.
591  * @mm: the probed process address space.
592  * @vaddr: the virtual address to insert the opcode.
593  *
594  * For mm @mm, store the breakpoint instruction at @vaddr.
595  * Return 0 (success) or a negative errno.
596  */
set_swbp(struct arch_uprobe * auprobe,struct mm_struct * mm,unsigned long vaddr)597 int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
598 {
599 	return uprobe_write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
600 }
601 
602 /**
603  * set_orig_insn - Restore the original instruction.
604  * @mm: the probed process address space.
605  * @auprobe: arch specific probepoint information.
606  * @vaddr: the virtual address to insert the opcode.
607  *
608  * For mm @mm, restore the original opcode (opcode) at @vaddr.
609  * Return 0 (success) or a negative errno.
610  */
611 int __weak
set_orig_insn(struct arch_uprobe * auprobe,struct mm_struct * mm,unsigned long vaddr)612 set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
613 {
614 	return uprobe_write_opcode(auprobe, mm, vaddr,
615 			*(uprobe_opcode_t *)&auprobe->insn);
616 }
617 
618 /* uprobe should have guaranteed positive refcount */
get_uprobe(struct uprobe * uprobe)619 static struct uprobe *get_uprobe(struct uprobe *uprobe)
620 {
621 	refcount_inc(&uprobe->ref);
622 	return uprobe;
623 }
624 
625 /*
626  * uprobe should have guaranteed lifetime, which can be either of:
627  *   - caller already has refcount taken (and wants an extra one);
628  *   - uprobe is RCU protected and won't be freed until after grace period;
629  *   - we are holding uprobes_treelock (for read or write, doesn't matter).
630  */
try_get_uprobe(struct uprobe * uprobe)631 static struct uprobe *try_get_uprobe(struct uprobe *uprobe)
632 {
633 	if (refcount_inc_not_zero(&uprobe->ref))
634 		return uprobe;
635 	return NULL;
636 }
637 
uprobe_is_active(struct uprobe * uprobe)638 static inline bool uprobe_is_active(struct uprobe *uprobe)
639 {
640 	return !RB_EMPTY_NODE(&uprobe->rb_node);
641 }
642 
uprobe_free_rcu(struct rcu_head * rcu)643 static void uprobe_free_rcu(struct rcu_head *rcu)
644 {
645 	struct uprobe *uprobe = container_of(rcu, struct uprobe, rcu);
646 
647 	kfree(uprobe);
648 }
649 
put_uprobe(struct uprobe * uprobe)650 static void put_uprobe(struct uprobe *uprobe)
651 {
652 	if (!refcount_dec_and_test(&uprobe->ref))
653 		return;
654 
655 	write_lock(&uprobes_treelock);
656 
657 	if (uprobe_is_active(uprobe)) {
658 		write_seqcount_begin(&uprobes_seqcount);
659 		rb_erase(&uprobe->rb_node, &uprobes_tree);
660 		write_seqcount_end(&uprobes_seqcount);
661 	}
662 
663 	write_unlock(&uprobes_treelock);
664 
665 	/*
666 	 * If application munmap(exec_vma) before uprobe_unregister()
667 	 * gets called, we don't get a chance to remove uprobe from
668 	 * delayed_uprobe_list from remove_breakpoint(). Do it here.
669 	 */
670 	mutex_lock(&delayed_uprobe_lock);
671 	delayed_uprobe_remove(uprobe, NULL);
672 	mutex_unlock(&delayed_uprobe_lock);
673 
674 	call_srcu(&uprobes_srcu, &uprobe->rcu, uprobe_free_rcu);
675 }
676 
677 static __always_inline
uprobe_cmp(const struct inode * l_inode,const loff_t l_offset,const struct uprobe * r)678 int uprobe_cmp(const struct inode *l_inode, const loff_t l_offset,
679 	       const struct uprobe *r)
680 {
681 	if (l_inode < r->inode)
682 		return -1;
683 
684 	if (l_inode > r->inode)
685 		return 1;
686 
687 	if (l_offset < r->offset)
688 		return -1;
689 
690 	if (l_offset > r->offset)
691 		return 1;
692 
693 	return 0;
694 }
695 
696 #define __node_2_uprobe(node) \
697 	rb_entry((node), struct uprobe, rb_node)
698 
699 struct __uprobe_key {
700 	struct inode *inode;
701 	loff_t offset;
702 };
703 
__uprobe_cmp_key(const void * key,const struct rb_node * b)704 static inline int __uprobe_cmp_key(const void *key, const struct rb_node *b)
705 {
706 	const struct __uprobe_key *a = key;
707 	return uprobe_cmp(a->inode, a->offset, __node_2_uprobe(b));
708 }
709 
__uprobe_cmp(struct rb_node * a,const struct rb_node * b)710 static inline int __uprobe_cmp(struct rb_node *a, const struct rb_node *b)
711 {
712 	struct uprobe *u = __node_2_uprobe(a);
713 	return uprobe_cmp(u->inode, u->offset, __node_2_uprobe(b));
714 }
715 
716 /*
717  * Assumes being inside RCU protected region.
718  * No refcount is taken on returned uprobe.
719  */
find_uprobe_rcu(struct inode * inode,loff_t offset)720 static struct uprobe *find_uprobe_rcu(struct inode *inode, loff_t offset)
721 {
722 	struct __uprobe_key key = {
723 		.inode = inode,
724 		.offset = offset,
725 	};
726 	struct rb_node *node;
727 	unsigned int seq;
728 
729 	lockdep_assert(srcu_read_lock_held(&uprobes_srcu));
730 
731 	do {
732 		seq = read_seqcount_begin(&uprobes_seqcount);
733 		node = rb_find_rcu(&key, &uprobes_tree, __uprobe_cmp_key);
734 		/*
735 		 * Lockless RB-tree lookups can result only in false negatives.
736 		 * If the element is found, it is correct and can be returned
737 		 * under RCU protection. If we find nothing, we need to
738 		 * validate that seqcount didn't change. If it did, we have to
739 		 * try again as we might have missed the element (false
740 		 * negative). If seqcount is unchanged, search truly failed.
741 		 */
742 		if (node)
743 			return __node_2_uprobe(node);
744 	} while (read_seqcount_retry(&uprobes_seqcount, seq));
745 
746 	return NULL;
747 }
748 
749 /*
750  * Attempt to insert a new uprobe into uprobes_tree.
751  *
752  * If uprobe already exists (for given inode+offset), we just increment
753  * refcount of previously existing uprobe.
754  *
755  * If not, a provided new instance of uprobe is inserted into the tree (with
756  * assumed initial refcount == 1).
757  *
758  * In any case, we return a uprobe instance that ends up being in uprobes_tree.
759  * Caller has to clean up new uprobe instance, if it ended up not being
760  * inserted into the tree.
761  *
762  * We assume that uprobes_treelock is held for writing.
763  */
__insert_uprobe(struct uprobe * uprobe)764 static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
765 {
766 	struct rb_node *node;
767 again:
768 	node = rb_find_add_rcu(&uprobe->rb_node, &uprobes_tree, __uprobe_cmp);
769 	if (node) {
770 		struct uprobe *u = __node_2_uprobe(node);
771 
772 		if (!try_get_uprobe(u)) {
773 			rb_erase(node, &uprobes_tree);
774 			RB_CLEAR_NODE(&u->rb_node);
775 			goto again;
776 		}
777 
778 		return u;
779 	}
780 
781 	return uprobe;
782 }
783 
784 /*
785  * Acquire uprobes_treelock and insert uprobe into uprobes_tree
786  * (or reuse existing one, see __insert_uprobe() comments above).
787  */
insert_uprobe(struct uprobe * uprobe)788 static struct uprobe *insert_uprobe(struct uprobe *uprobe)
789 {
790 	struct uprobe *u;
791 
792 	write_lock(&uprobes_treelock);
793 	write_seqcount_begin(&uprobes_seqcount);
794 	u = __insert_uprobe(uprobe);
795 	write_seqcount_end(&uprobes_seqcount);
796 	write_unlock(&uprobes_treelock);
797 
798 	return u;
799 }
800 
801 static void
ref_ctr_mismatch_warn(struct uprobe * cur_uprobe,struct uprobe * uprobe)802 ref_ctr_mismatch_warn(struct uprobe *cur_uprobe, struct uprobe *uprobe)
803 {
804 	pr_warn("ref_ctr_offset mismatch. inode: 0x%lx offset: 0x%llx "
805 		"ref_ctr_offset(old): 0x%llx ref_ctr_offset(new): 0x%llx\n",
806 		uprobe->inode->i_ino, (unsigned long long) uprobe->offset,
807 		(unsigned long long) cur_uprobe->ref_ctr_offset,
808 		(unsigned long long) uprobe->ref_ctr_offset);
809 }
810 
alloc_uprobe(struct inode * inode,loff_t offset,loff_t ref_ctr_offset)811 static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset,
812 				   loff_t ref_ctr_offset)
813 {
814 	struct uprobe *uprobe, *cur_uprobe;
815 
816 	uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
817 	if (!uprobe)
818 		return ERR_PTR(-ENOMEM);
819 
820 	uprobe->inode = inode;
821 	uprobe->offset = offset;
822 	uprobe->ref_ctr_offset = ref_ctr_offset;
823 	INIT_LIST_HEAD(&uprobe->consumers);
824 	init_rwsem(&uprobe->register_rwsem);
825 	init_rwsem(&uprobe->consumer_rwsem);
826 	RB_CLEAR_NODE(&uprobe->rb_node);
827 	refcount_set(&uprobe->ref, 1);
828 
829 	/* add to uprobes_tree, sorted on inode:offset */
830 	cur_uprobe = insert_uprobe(uprobe);
831 	/* a uprobe exists for this inode:offset combination */
832 	if (cur_uprobe != uprobe) {
833 		if (cur_uprobe->ref_ctr_offset != uprobe->ref_ctr_offset) {
834 			ref_ctr_mismatch_warn(cur_uprobe, uprobe);
835 			put_uprobe(cur_uprobe);
836 			kfree(uprobe);
837 			return ERR_PTR(-EINVAL);
838 		}
839 		kfree(uprobe);
840 		uprobe = cur_uprobe;
841 	}
842 
843 	return uprobe;
844 }
845 
consumer_add(struct uprobe * uprobe,struct uprobe_consumer * uc)846 static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
847 {
848 	down_write(&uprobe->consumer_rwsem);
849 	list_add_rcu(&uc->cons_node, &uprobe->consumers);
850 	up_write(&uprobe->consumer_rwsem);
851 }
852 
853 /*
854  * For uprobe @uprobe, delete the consumer @uc.
855  * Should never be called with consumer that's not part of @uprobe->consumers.
856  */
consumer_del(struct uprobe * uprobe,struct uprobe_consumer * uc)857 static void consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
858 {
859 	down_write(&uprobe->consumer_rwsem);
860 	list_del_rcu(&uc->cons_node);
861 	up_write(&uprobe->consumer_rwsem);
862 }
863 
__copy_insn(struct address_space * mapping,struct file * filp,void * insn,int nbytes,loff_t offset)864 static int __copy_insn(struct address_space *mapping, struct file *filp,
865 			void *insn, int nbytes, loff_t offset)
866 {
867 	struct page *page;
868 	/*
869 	 * Ensure that the page that has the original instruction is populated
870 	 * and in page-cache. If ->read_folio == NULL it must be shmem_mapping(),
871 	 * see uprobe_register().
872 	 */
873 	if (mapping->a_ops->read_folio)
874 		page = read_mapping_page(mapping, offset >> PAGE_SHIFT, filp);
875 	else
876 		page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
877 	if (IS_ERR(page))
878 		return PTR_ERR(page);
879 
880 	copy_from_page(page, offset, insn, nbytes);
881 	put_page(page);
882 
883 	return 0;
884 }
885 
copy_insn(struct uprobe * uprobe,struct file * filp)886 static int copy_insn(struct uprobe *uprobe, struct file *filp)
887 {
888 	struct address_space *mapping = uprobe->inode->i_mapping;
889 	loff_t offs = uprobe->offset;
890 	void *insn = &uprobe->arch.insn;
891 	int size = sizeof(uprobe->arch.insn);
892 	int len, err = -EIO;
893 
894 	/* Copy only available bytes, -EIO if nothing was read */
895 	do {
896 		if (offs >= i_size_read(uprobe->inode))
897 			break;
898 
899 		len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK));
900 		err = __copy_insn(mapping, filp, insn, len, offs);
901 		if (err)
902 			break;
903 
904 		insn += len;
905 		offs += len;
906 		size -= len;
907 	} while (size);
908 
909 	return err;
910 }
911 
prepare_uprobe(struct uprobe * uprobe,struct file * file,struct mm_struct * mm,unsigned long vaddr)912 static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
913 				struct mm_struct *mm, unsigned long vaddr)
914 {
915 	int ret = 0;
916 
917 	if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
918 		return ret;
919 
920 	/* TODO: move this into _register, until then we abuse this sem. */
921 	down_write(&uprobe->consumer_rwsem);
922 	if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
923 		goto out;
924 
925 	ret = copy_insn(uprobe, file);
926 	if (ret)
927 		goto out;
928 
929 	ret = -ENOTSUPP;
930 	if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn))
931 		goto out;
932 
933 	ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
934 	if (ret)
935 		goto out;
936 
937 	smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
938 	set_bit(UPROBE_COPY_INSN, &uprobe->flags);
939 
940  out:
941 	up_write(&uprobe->consumer_rwsem);
942 
943 	return ret;
944 }
945 
consumer_filter(struct uprobe_consumer * uc,struct mm_struct * mm)946 static inline bool consumer_filter(struct uprobe_consumer *uc, struct mm_struct *mm)
947 {
948 	return !uc->filter || uc->filter(uc, mm);
949 }
950 
filter_chain(struct uprobe * uprobe,struct mm_struct * mm)951 static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
952 {
953 	struct uprobe_consumer *uc;
954 	bool ret = false;
955 
956 	down_read(&uprobe->consumer_rwsem);
957 	list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node,
958 				 srcu_read_lock_held(&uprobes_srcu)) {
959 		ret = consumer_filter(uc, mm);
960 		if (ret)
961 			break;
962 	}
963 	up_read(&uprobe->consumer_rwsem);
964 
965 	return ret;
966 }
967 
968 static int
install_breakpoint(struct uprobe * uprobe,struct mm_struct * mm,struct vm_area_struct * vma,unsigned long vaddr)969 install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
970 			struct vm_area_struct *vma, unsigned long vaddr)
971 {
972 	bool first_uprobe;
973 	int ret;
974 
975 	ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
976 	if (ret)
977 		return ret;
978 
979 	/*
980 	 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
981 	 * the task can hit this breakpoint right after __replace_page().
982 	 */
983 	first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
984 	if (first_uprobe)
985 		set_bit(MMF_HAS_UPROBES, &mm->flags);
986 
987 	ret = set_swbp(&uprobe->arch, mm, vaddr);
988 	if (!ret)
989 		clear_bit(MMF_RECALC_UPROBES, &mm->flags);
990 	else if (first_uprobe)
991 		clear_bit(MMF_HAS_UPROBES, &mm->flags);
992 
993 	return ret;
994 }
995 
996 static int
remove_breakpoint(struct uprobe * uprobe,struct mm_struct * mm,unsigned long vaddr)997 remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
998 {
999 	set_bit(MMF_RECALC_UPROBES, &mm->flags);
1000 	return set_orig_insn(&uprobe->arch, mm, vaddr);
1001 }
1002 
1003 struct map_info {
1004 	struct map_info *next;
1005 	struct mm_struct *mm;
1006 	unsigned long vaddr;
1007 };
1008 
free_map_info(struct map_info * info)1009 static inline struct map_info *free_map_info(struct map_info *info)
1010 {
1011 	struct map_info *next = info->next;
1012 	kfree(info);
1013 	return next;
1014 }
1015 
1016 static struct map_info *
build_map_info(struct address_space * mapping,loff_t offset,bool is_register)1017 build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
1018 {
1019 	unsigned long pgoff = offset >> PAGE_SHIFT;
1020 	struct vm_area_struct *vma;
1021 	struct map_info *curr = NULL;
1022 	struct map_info *prev = NULL;
1023 	struct map_info *info;
1024 	int more = 0;
1025 
1026  again:
1027 	i_mmap_lock_read(mapping);
1028 	vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
1029 		if (!valid_vma(vma, is_register))
1030 			continue;
1031 
1032 		if (!prev && !more) {
1033 			/*
1034 			 * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through
1035 			 * reclaim. This is optimistic, no harm done if it fails.
1036 			 */
1037 			prev = kmalloc(sizeof(struct map_info),
1038 					GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
1039 			if (prev)
1040 				prev->next = NULL;
1041 		}
1042 		if (!prev) {
1043 			more++;
1044 			continue;
1045 		}
1046 
1047 		if (!mmget_not_zero(vma->vm_mm))
1048 			continue;
1049 
1050 		info = prev;
1051 		prev = prev->next;
1052 		info->next = curr;
1053 		curr = info;
1054 
1055 		info->mm = vma->vm_mm;
1056 		info->vaddr = offset_to_vaddr(vma, offset);
1057 	}
1058 	i_mmap_unlock_read(mapping);
1059 
1060 	if (!more)
1061 		goto out;
1062 
1063 	prev = curr;
1064 	while (curr) {
1065 		mmput(curr->mm);
1066 		curr = curr->next;
1067 	}
1068 
1069 	do {
1070 		info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
1071 		if (!info) {
1072 			curr = ERR_PTR(-ENOMEM);
1073 			goto out;
1074 		}
1075 		info->next = prev;
1076 		prev = info;
1077 	} while (--more);
1078 
1079 	goto again;
1080  out:
1081 	while (prev)
1082 		prev = free_map_info(prev);
1083 	return curr;
1084 }
1085 
1086 static int
register_for_each_vma(struct uprobe * uprobe,struct uprobe_consumer * new)1087 register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
1088 {
1089 	bool is_register = !!new;
1090 	struct map_info *info;
1091 	int err = 0;
1092 
1093 	percpu_down_write(&dup_mmap_sem);
1094 	info = build_map_info(uprobe->inode->i_mapping,
1095 					uprobe->offset, is_register);
1096 	if (IS_ERR(info)) {
1097 		err = PTR_ERR(info);
1098 		goto out;
1099 	}
1100 
1101 	while (info) {
1102 		struct mm_struct *mm = info->mm;
1103 		struct vm_area_struct *vma;
1104 
1105 		if (err && is_register)
1106 			goto free;
1107 		/*
1108 		 * We take mmap_lock for writing to avoid the race with
1109 		 * find_active_uprobe_rcu() which takes mmap_lock for reading.
1110 		 * Thus this install_breakpoint() can not make
1111 		 * is_trap_at_addr() true right after find_uprobe_rcu()
1112 		 * returns NULL in find_active_uprobe_rcu().
1113 		 */
1114 		mmap_write_lock(mm);
1115 		vma = find_vma(mm, info->vaddr);
1116 		if (!vma || !valid_vma(vma, is_register) ||
1117 		    file_inode(vma->vm_file) != uprobe->inode)
1118 			goto unlock;
1119 
1120 		if (vma->vm_start > info->vaddr ||
1121 		    vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
1122 			goto unlock;
1123 
1124 		if (is_register) {
1125 			/* consult only the "caller", new consumer. */
1126 			if (consumer_filter(new, mm))
1127 				err = install_breakpoint(uprobe, mm, vma, info->vaddr);
1128 		} else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
1129 			if (!filter_chain(uprobe, mm))
1130 				err |= remove_breakpoint(uprobe, mm, info->vaddr);
1131 		}
1132 
1133  unlock:
1134 		mmap_write_unlock(mm);
1135  free:
1136 		mmput(mm);
1137 		info = free_map_info(info);
1138 	}
1139  out:
1140 	percpu_up_write(&dup_mmap_sem);
1141 	return err;
1142 }
1143 
1144 /**
1145  * uprobe_unregister_nosync - unregister an already registered probe.
1146  * @uprobe: uprobe to remove
1147  * @uc: identify which probe if multiple probes are colocated.
1148  */
uprobe_unregister_nosync(struct uprobe * uprobe,struct uprobe_consumer * uc)1149 void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc)
1150 {
1151 	int err;
1152 
1153 	down_write(&uprobe->register_rwsem);
1154 	consumer_del(uprobe, uc);
1155 	err = register_for_each_vma(uprobe, NULL);
1156 	up_write(&uprobe->register_rwsem);
1157 
1158 	/* TODO : cant unregister? schedule a worker thread */
1159 	if (unlikely(err)) {
1160 		uprobe_warn(current, "unregister, leaking uprobe");
1161 		return;
1162 	}
1163 
1164 	put_uprobe(uprobe);
1165 }
1166 EXPORT_SYMBOL_GPL(uprobe_unregister_nosync);
1167 
uprobe_unregister_sync(void)1168 void uprobe_unregister_sync(void)
1169 {
1170 	/*
1171 	 * Now that handler_chain() and handle_uretprobe_chain() iterate over
1172 	 * uprobe->consumers list under RCU protection without holding
1173 	 * uprobe->register_rwsem, we need to wait for RCU grace period to
1174 	 * make sure that we can't call into just unregistered
1175 	 * uprobe_consumer's callbacks anymore. If we don't do that, fast and
1176 	 * unlucky enough caller can free consumer's memory and cause
1177 	 * handler_chain() or handle_uretprobe_chain() to do an use-after-free.
1178 	 */
1179 	synchronize_srcu(&uprobes_srcu);
1180 }
1181 EXPORT_SYMBOL_GPL(uprobe_unregister_sync);
1182 
1183 /**
1184  * uprobe_register - register a probe
1185  * @inode: the file in which the probe has to be placed.
1186  * @offset: offset from the start of the file.
1187  * @ref_ctr_offset: offset of SDT marker / reference counter
1188  * @uc: information on howto handle the probe..
1189  *
1190  * Apart from the access refcount, uprobe_register() takes a creation
1191  * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
1192  * inserted into the rbtree (i.e first consumer for a @inode:@offset
1193  * tuple).  Creation refcount stops uprobe_unregister from freeing the
1194  * @uprobe even before the register operation is complete. Creation
1195  * refcount is released when the last @uc for the @uprobe
1196  * unregisters. Caller of uprobe_register() is required to keep @inode
1197  * (and the containing mount) referenced.
1198  *
1199  * Return: pointer to the new uprobe on success or an ERR_PTR on failure.
1200  */
uprobe_register(struct inode * inode,loff_t offset,loff_t ref_ctr_offset,struct uprobe_consumer * uc)1201 struct uprobe *uprobe_register(struct inode *inode,
1202 				loff_t offset, loff_t ref_ctr_offset,
1203 				struct uprobe_consumer *uc)
1204 {
1205 	struct uprobe *uprobe;
1206 	int ret;
1207 
1208 	/* Uprobe must have at least one set consumer */
1209 	if (!uc->handler && !uc->ret_handler)
1210 		return ERR_PTR(-EINVAL);
1211 
1212 	/* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */
1213 	if (!inode->i_mapping->a_ops->read_folio &&
1214 	    !shmem_mapping(inode->i_mapping))
1215 		return ERR_PTR(-EIO);
1216 	/* Racy, just to catch the obvious mistakes */
1217 	if (offset > i_size_read(inode))
1218 		return ERR_PTR(-EINVAL);
1219 
1220 	/*
1221 	 * This ensures that copy_from_page(), copy_to_page() and
1222 	 * __update_ref_ctr() can't cross page boundary.
1223 	 */
1224 	if (!IS_ALIGNED(offset, UPROBE_SWBP_INSN_SIZE))
1225 		return ERR_PTR(-EINVAL);
1226 	if (!IS_ALIGNED(ref_ctr_offset, sizeof(short)))
1227 		return ERR_PTR(-EINVAL);
1228 
1229 	uprobe = alloc_uprobe(inode, offset, ref_ctr_offset);
1230 	if (IS_ERR(uprobe))
1231 		return uprobe;
1232 
1233 	down_write(&uprobe->register_rwsem);
1234 	consumer_add(uprobe, uc);
1235 	ret = register_for_each_vma(uprobe, uc);
1236 	up_write(&uprobe->register_rwsem);
1237 
1238 	if (ret) {
1239 		uprobe_unregister_nosync(uprobe, uc);
1240 		/*
1241 		 * Registration might have partially succeeded, so we can have
1242 		 * this consumer being called right at this time. We need to
1243 		 * sync here. It's ok, it's unlikely slow path.
1244 		 */
1245 		uprobe_unregister_sync();
1246 		return ERR_PTR(ret);
1247 	}
1248 
1249 	return uprobe;
1250 }
1251 EXPORT_SYMBOL_GPL(uprobe_register);
1252 
1253 /**
1254  * uprobe_apply - add or remove the breakpoints according to @uc->filter
1255  * @uprobe: uprobe which "owns" the breakpoint
1256  * @uc: consumer which wants to add more or remove some breakpoints
1257  * @add: add or remove the breakpoints
1258  * Return: 0 on success or negative error code.
1259  */
uprobe_apply(struct uprobe * uprobe,struct uprobe_consumer * uc,bool add)1260 int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool add)
1261 {
1262 	struct uprobe_consumer *con;
1263 	int ret = -ENOENT, srcu_idx;
1264 
1265 	down_write(&uprobe->register_rwsem);
1266 
1267 	srcu_idx = srcu_read_lock(&uprobes_srcu);
1268 	list_for_each_entry_srcu(con, &uprobe->consumers, cons_node,
1269 				 srcu_read_lock_held(&uprobes_srcu)) {
1270 		if (con == uc) {
1271 			ret = register_for_each_vma(uprobe, add ? uc : NULL);
1272 			break;
1273 		}
1274 	}
1275 	srcu_read_unlock(&uprobes_srcu, srcu_idx);
1276 
1277 	up_write(&uprobe->register_rwsem);
1278 
1279 	return ret;
1280 }
1281 
unapply_uprobe(struct uprobe * uprobe,struct mm_struct * mm)1282 static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
1283 {
1284 	VMA_ITERATOR(vmi, mm, 0);
1285 	struct vm_area_struct *vma;
1286 	int err = 0;
1287 
1288 	mmap_read_lock(mm);
1289 	for_each_vma(vmi, vma) {
1290 		unsigned long vaddr;
1291 		loff_t offset;
1292 
1293 		if (!valid_vma(vma, false) ||
1294 		    file_inode(vma->vm_file) != uprobe->inode)
1295 			continue;
1296 
1297 		offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1298 		if (uprobe->offset <  offset ||
1299 		    uprobe->offset >= offset + vma->vm_end - vma->vm_start)
1300 			continue;
1301 
1302 		vaddr = offset_to_vaddr(vma, uprobe->offset);
1303 		err |= remove_breakpoint(uprobe, mm, vaddr);
1304 	}
1305 	mmap_read_unlock(mm);
1306 
1307 	return err;
1308 }
1309 
1310 static struct rb_node *
find_node_in_range(struct inode * inode,loff_t min,loff_t max)1311 find_node_in_range(struct inode *inode, loff_t min, loff_t max)
1312 {
1313 	struct rb_node *n = uprobes_tree.rb_node;
1314 
1315 	while (n) {
1316 		struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
1317 
1318 		if (inode < u->inode) {
1319 			n = n->rb_left;
1320 		} else if (inode > u->inode) {
1321 			n = n->rb_right;
1322 		} else {
1323 			if (max < u->offset)
1324 				n = n->rb_left;
1325 			else if (min > u->offset)
1326 				n = n->rb_right;
1327 			else
1328 				break;
1329 		}
1330 	}
1331 
1332 	return n;
1333 }
1334 
1335 /*
1336  * For a given range in vma, build a list of probes that need to be inserted.
1337  */
build_probe_list(struct inode * inode,struct vm_area_struct * vma,unsigned long start,unsigned long end,struct list_head * head)1338 static void build_probe_list(struct inode *inode,
1339 				struct vm_area_struct *vma,
1340 				unsigned long start, unsigned long end,
1341 				struct list_head *head)
1342 {
1343 	loff_t min, max;
1344 	struct rb_node *n, *t;
1345 	struct uprobe *u;
1346 
1347 	INIT_LIST_HEAD(head);
1348 	min = vaddr_to_offset(vma, start);
1349 	max = min + (end - start) - 1;
1350 
1351 	read_lock(&uprobes_treelock);
1352 	n = find_node_in_range(inode, min, max);
1353 	if (n) {
1354 		for (t = n; t; t = rb_prev(t)) {
1355 			u = rb_entry(t, struct uprobe, rb_node);
1356 			if (u->inode != inode || u->offset < min)
1357 				break;
1358 			/* if uprobe went away, it's safe to ignore it */
1359 			if (try_get_uprobe(u))
1360 				list_add(&u->pending_list, head);
1361 		}
1362 		for (t = n; (t = rb_next(t)); ) {
1363 			u = rb_entry(t, struct uprobe, rb_node);
1364 			if (u->inode != inode || u->offset > max)
1365 				break;
1366 			/* if uprobe went away, it's safe to ignore it */
1367 			if (try_get_uprobe(u))
1368 				list_add(&u->pending_list, head);
1369 		}
1370 	}
1371 	read_unlock(&uprobes_treelock);
1372 }
1373 
1374 /* @vma contains reference counter, not the probed instruction. */
delayed_ref_ctr_inc(struct vm_area_struct * vma)1375 static int delayed_ref_ctr_inc(struct vm_area_struct *vma)
1376 {
1377 	struct list_head *pos, *q;
1378 	struct delayed_uprobe *du;
1379 	unsigned long vaddr;
1380 	int ret = 0, err = 0;
1381 
1382 	mutex_lock(&delayed_uprobe_lock);
1383 	list_for_each_safe(pos, q, &delayed_uprobe_list) {
1384 		du = list_entry(pos, struct delayed_uprobe, list);
1385 
1386 		if (du->mm != vma->vm_mm ||
1387 		    !valid_ref_ctr_vma(du->uprobe, vma))
1388 			continue;
1389 
1390 		vaddr = offset_to_vaddr(vma, du->uprobe->ref_ctr_offset);
1391 		ret = __update_ref_ctr(vma->vm_mm, vaddr, 1);
1392 		if (ret) {
1393 			update_ref_ctr_warn(du->uprobe, vma->vm_mm, 1);
1394 			if (!err)
1395 				err = ret;
1396 		}
1397 		delayed_uprobe_delete(du);
1398 	}
1399 	mutex_unlock(&delayed_uprobe_lock);
1400 	return err;
1401 }
1402 
1403 /*
1404  * Called from mmap_region/vma_merge with mm->mmap_lock acquired.
1405  *
1406  * Currently we ignore all errors and always return 0, the callers
1407  * can't handle the failure anyway.
1408  */
uprobe_mmap(struct vm_area_struct * vma)1409 int uprobe_mmap(struct vm_area_struct *vma)
1410 {
1411 	struct list_head tmp_list;
1412 	struct uprobe *uprobe, *u;
1413 	struct inode *inode;
1414 
1415 	if (no_uprobe_events())
1416 		return 0;
1417 
1418 	if (vma->vm_file &&
1419 	    (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE &&
1420 	    test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags))
1421 		delayed_ref_ctr_inc(vma);
1422 
1423 	if (!valid_vma(vma, true))
1424 		return 0;
1425 
1426 	inode = file_inode(vma->vm_file);
1427 	if (!inode)
1428 		return 0;
1429 
1430 	mutex_lock(uprobes_mmap_hash(inode));
1431 	build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
1432 	/*
1433 	 * We can race with uprobe_unregister(), this uprobe can be already
1434 	 * removed. But in this case filter_chain() must return false, all
1435 	 * consumers have gone away.
1436 	 */
1437 	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1438 		if (!fatal_signal_pending(current) &&
1439 		    filter_chain(uprobe, vma->vm_mm)) {
1440 			unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
1441 			install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
1442 		}
1443 		put_uprobe(uprobe);
1444 	}
1445 	mutex_unlock(uprobes_mmap_hash(inode));
1446 
1447 	return 0;
1448 }
1449 
1450 static bool
vma_has_uprobes(struct vm_area_struct * vma,unsigned long start,unsigned long end)1451 vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1452 {
1453 	loff_t min, max;
1454 	struct inode *inode;
1455 	struct rb_node *n;
1456 
1457 	inode = file_inode(vma->vm_file);
1458 
1459 	min = vaddr_to_offset(vma, start);
1460 	max = min + (end - start) - 1;
1461 
1462 	read_lock(&uprobes_treelock);
1463 	n = find_node_in_range(inode, min, max);
1464 	read_unlock(&uprobes_treelock);
1465 
1466 	return !!n;
1467 }
1468 
1469 /*
1470  * Called in context of a munmap of a vma.
1471  */
uprobe_munmap(struct vm_area_struct * vma,unsigned long start,unsigned long end)1472 void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1473 {
1474 	if (no_uprobe_events() || !valid_vma(vma, false))
1475 		return;
1476 
1477 	if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1478 		return;
1479 
1480 	if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1481 	     test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
1482 		return;
1483 
1484 	if (vma_has_uprobes(vma, start, end))
1485 		set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
1486 }
1487 
xol_fault(const struct vm_special_mapping * sm,struct vm_area_struct * vma,struct vm_fault * vmf)1488 static vm_fault_t xol_fault(const struct vm_special_mapping *sm,
1489 			    struct vm_area_struct *vma, struct vm_fault *vmf)
1490 {
1491 	struct xol_area *area = vma->vm_mm->uprobes_state.xol_area;
1492 
1493 	vmf->page = area->page;
1494 	get_page(vmf->page);
1495 	return 0;
1496 }
1497 
1498 static const struct vm_special_mapping xol_mapping = {
1499 	.name = "[uprobes]",
1500 	.fault = xol_fault,
1501 };
1502 
1503 /* Slot allocation for XOL */
xol_add_vma(struct mm_struct * mm,struct xol_area * area)1504 static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
1505 {
1506 	struct vm_area_struct *vma;
1507 	int ret;
1508 
1509 	if (mmap_write_lock_killable(mm))
1510 		return -EINTR;
1511 
1512 	if (mm->uprobes_state.xol_area) {
1513 		ret = -EALREADY;
1514 		goto fail;
1515 	}
1516 
1517 	if (!area->vaddr) {
1518 		/* Try to map as high as possible, this is only a hint. */
1519 		area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
1520 						PAGE_SIZE, 0, 0);
1521 		if (IS_ERR_VALUE(area->vaddr)) {
1522 			ret = area->vaddr;
1523 			goto fail;
1524 		}
1525 	}
1526 
1527 	vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1528 				VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
1529 				&xol_mapping);
1530 	if (IS_ERR(vma)) {
1531 		ret = PTR_ERR(vma);
1532 		goto fail;
1533 	}
1534 
1535 	ret = 0;
1536 	/* pairs with get_xol_area() */
1537 	smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */
1538  fail:
1539 	mmap_write_unlock(mm);
1540 
1541 	return ret;
1542 }
1543 
arch_uprobe_trampoline(unsigned long * psize)1544 void * __weak arch_uprobe_trampoline(unsigned long *psize)
1545 {
1546 	static uprobe_opcode_t insn = UPROBE_SWBP_INSN;
1547 
1548 	*psize = UPROBE_SWBP_INSN_SIZE;
1549 	return &insn;
1550 }
1551 
__create_xol_area(unsigned long vaddr)1552 static struct xol_area *__create_xol_area(unsigned long vaddr)
1553 {
1554 	struct mm_struct *mm = current->mm;
1555 	unsigned long insns_size;
1556 	struct xol_area *area;
1557 	void *insns;
1558 
1559 	area = kzalloc(sizeof(*area), GFP_KERNEL);
1560 	if (unlikely(!area))
1561 		goto out;
1562 
1563 	area->bitmap = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long),
1564 			       GFP_KERNEL);
1565 	if (!area->bitmap)
1566 		goto free_area;
1567 
1568 	area->page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
1569 	if (!area->page)
1570 		goto free_bitmap;
1571 
1572 	area->vaddr = vaddr;
1573 	init_waitqueue_head(&area->wq);
1574 	/* Reserve the 1st slot for get_trampoline_vaddr() */
1575 	set_bit(0, area->bitmap);
1576 	atomic_set(&area->slot_count, 1);
1577 	insns = arch_uprobe_trampoline(&insns_size);
1578 	arch_uprobe_copy_ixol(area->page, 0, insns, insns_size);
1579 
1580 	if (!xol_add_vma(mm, area))
1581 		return area;
1582 
1583 	__free_page(area->page);
1584  free_bitmap:
1585 	kfree(area->bitmap);
1586  free_area:
1587 	kfree(area);
1588  out:
1589 	return NULL;
1590 }
1591 
1592 /*
1593  * get_xol_area - Allocate process's xol_area if necessary.
1594  * This area will be used for storing instructions for execution out of line.
1595  *
1596  * Returns the allocated area or NULL.
1597  */
get_xol_area(void)1598 static struct xol_area *get_xol_area(void)
1599 {
1600 	struct mm_struct *mm = current->mm;
1601 	struct xol_area *area;
1602 
1603 	if (!mm->uprobes_state.xol_area)
1604 		__create_xol_area(0);
1605 
1606 	/* Pairs with xol_add_vma() smp_store_release() */
1607 	area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */
1608 	return area;
1609 }
1610 
1611 /*
1612  * uprobe_clear_state - Free the area allocated for slots.
1613  */
uprobe_clear_state(struct mm_struct * mm)1614 void uprobe_clear_state(struct mm_struct *mm)
1615 {
1616 	struct xol_area *area = mm->uprobes_state.xol_area;
1617 
1618 	mutex_lock(&delayed_uprobe_lock);
1619 	delayed_uprobe_remove(NULL, mm);
1620 	mutex_unlock(&delayed_uprobe_lock);
1621 
1622 	if (!area)
1623 		return;
1624 
1625 	put_page(area->page);
1626 	kfree(area->bitmap);
1627 	kfree(area);
1628 }
1629 
uprobe_start_dup_mmap(void)1630 void uprobe_start_dup_mmap(void)
1631 {
1632 	percpu_down_read(&dup_mmap_sem);
1633 }
1634 
uprobe_end_dup_mmap(void)1635 void uprobe_end_dup_mmap(void)
1636 {
1637 	percpu_up_read(&dup_mmap_sem);
1638 }
1639 
uprobe_dup_mmap(struct mm_struct * oldmm,struct mm_struct * newmm)1640 void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1641 {
1642 	if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
1643 		set_bit(MMF_HAS_UPROBES, &newmm->flags);
1644 		/* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1645 		set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1646 	}
1647 }
1648 
1649 /*
1650  *  - search for a free slot.
1651  */
xol_take_insn_slot(struct xol_area * area)1652 static unsigned long xol_take_insn_slot(struct xol_area *area)
1653 {
1654 	unsigned long slot_addr;
1655 	int slot_nr;
1656 
1657 	do {
1658 		slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1659 		if (slot_nr < UINSNS_PER_PAGE) {
1660 			if (!test_and_set_bit(slot_nr, area->bitmap))
1661 				break;
1662 
1663 			slot_nr = UINSNS_PER_PAGE;
1664 			continue;
1665 		}
1666 		wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1667 	} while (slot_nr >= UINSNS_PER_PAGE);
1668 
1669 	slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1670 	atomic_inc(&area->slot_count);
1671 
1672 	return slot_addr;
1673 }
1674 
1675 /*
1676  * xol_get_insn_slot - allocate a slot for xol.
1677  * Returns the allocated slot address or 0.
1678  */
xol_get_insn_slot(struct uprobe * uprobe)1679 static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
1680 {
1681 	struct xol_area *area;
1682 	unsigned long xol_vaddr;
1683 
1684 	area = get_xol_area();
1685 	if (!area)
1686 		return 0;
1687 
1688 	xol_vaddr = xol_take_insn_slot(area);
1689 	if (unlikely(!xol_vaddr))
1690 		return 0;
1691 
1692 	arch_uprobe_copy_ixol(area->page, xol_vaddr,
1693 			      &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
1694 
1695 	return xol_vaddr;
1696 }
1697 
1698 /*
1699  * xol_free_insn_slot - If slot was earlier allocated by
1700  * @xol_get_insn_slot(), make the slot available for
1701  * subsequent requests.
1702  */
xol_free_insn_slot(struct task_struct * tsk)1703 static void xol_free_insn_slot(struct task_struct *tsk)
1704 {
1705 	struct xol_area *area;
1706 	unsigned long vma_end;
1707 	unsigned long slot_addr;
1708 
1709 	if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1710 		return;
1711 
1712 	slot_addr = tsk->utask->xol_vaddr;
1713 	if (unlikely(!slot_addr))
1714 		return;
1715 
1716 	area = tsk->mm->uprobes_state.xol_area;
1717 	vma_end = area->vaddr + PAGE_SIZE;
1718 	if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1719 		unsigned long offset;
1720 		int slot_nr;
1721 
1722 		offset = slot_addr - area->vaddr;
1723 		slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1724 		if (slot_nr >= UINSNS_PER_PAGE)
1725 			return;
1726 
1727 		clear_bit(slot_nr, area->bitmap);
1728 		atomic_dec(&area->slot_count);
1729 		smp_mb__after_atomic(); /* pairs with prepare_to_wait() */
1730 		if (waitqueue_active(&area->wq))
1731 			wake_up(&area->wq);
1732 
1733 		tsk->utask->xol_vaddr = 0;
1734 	}
1735 }
1736 
arch_uprobe_copy_ixol(struct page * page,unsigned long vaddr,void * src,unsigned long len)1737 void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
1738 				  void *src, unsigned long len)
1739 {
1740 	/* Initialize the slot */
1741 	copy_to_page(page, vaddr, src, len);
1742 
1743 	/*
1744 	 * We probably need flush_icache_user_page() but it needs vma.
1745 	 * This should work on most of architectures by default. If
1746 	 * architecture needs to do something different it can define
1747 	 * its own version of the function.
1748 	 */
1749 	flush_dcache_page(page);
1750 }
1751 
1752 /**
1753  * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1754  * @regs: Reflects the saved state of the task after it has hit a breakpoint
1755  * instruction.
1756  * Return the address of the breakpoint instruction.
1757  */
uprobe_get_swbp_addr(struct pt_regs * regs)1758 unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1759 {
1760 	return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1761 }
1762 
uprobe_get_trap_addr(struct pt_regs * regs)1763 unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
1764 {
1765 	struct uprobe_task *utask = current->utask;
1766 
1767 	if (unlikely(utask && utask->active_uprobe))
1768 		return utask->vaddr;
1769 
1770 	return instruction_pointer(regs);
1771 }
1772 
free_ret_instance(struct return_instance * ri)1773 static struct return_instance *free_ret_instance(struct return_instance *ri)
1774 {
1775 	struct return_instance *next = ri->next;
1776 	put_uprobe(ri->uprobe);
1777 	kfree(ri);
1778 	return next;
1779 }
1780 
1781 /*
1782  * Called with no locks held.
1783  * Called in context of an exiting or an exec-ing thread.
1784  */
uprobe_free_utask(struct task_struct * t)1785 void uprobe_free_utask(struct task_struct *t)
1786 {
1787 	struct uprobe_task *utask = t->utask;
1788 	struct return_instance *ri;
1789 
1790 	if (!utask)
1791 		return;
1792 
1793 	t->utask = NULL;
1794 	if (utask->active_uprobe)
1795 		put_uprobe(utask->active_uprobe);
1796 
1797 	ri = utask->return_instances;
1798 	while (ri)
1799 		ri = free_ret_instance(ri);
1800 
1801 	xol_free_insn_slot(t);
1802 	kfree(utask);
1803 }
1804 
1805 /*
1806  * Allocate a uprobe_task object for the task if necessary.
1807  * Called when the thread hits a breakpoint.
1808  *
1809  * Returns:
1810  * - pointer to new uprobe_task on success
1811  * - NULL otherwise
1812  */
get_utask(void)1813 static struct uprobe_task *get_utask(void)
1814 {
1815 	if (!current->utask)
1816 		current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
1817 	return current->utask;
1818 }
1819 
dup_utask(struct task_struct * t,struct uprobe_task * o_utask)1820 static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
1821 {
1822 	struct uprobe_task *n_utask;
1823 	struct return_instance **p, *o, *n;
1824 
1825 	n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
1826 	if (!n_utask)
1827 		return -ENOMEM;
1828 	t->utask = n_utask;
1829 
1830 	p = &n_utask->return_instances;
1831 	for (o = o_utask->return_instances; o; o = o->next) {
1832 		n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
1833 		if (!n)
1834 			return -ENOMEM;
1835 
1836 		*n = *o;
1837 		/*
1838 		 * uprobe's refcnt has to be positive at this point, kept by
1839 		 * utask->return_instances items; return_instances can't be
1840 		 * removed right now, as task is blocked due to duping; so
1841 		 * get_uprobe() is safe to use here.
1842 		 */
1843 		get_uprobe(n->uprobe);
1844 		n->next = NULL;
1845 
1846 		*p = n;
1847 		p = &n->next;
1848 		n_utask->depth++;
1849 	}
1850 
1851 	return 0;
1852 }
1853 
dup_xol_work(struct callback_head * work)1854 static void dup_xol_work(struct callback_head *work)
1855 {
1856 	if (current->flags & PF_EXITING)
1857 		return;
1858 
1859 	if (!__create_xol_area(current->utask->dup_xol_addr) &&
1860 			!fatal_signal_pending(current))
1861 		uprobe_warn(current, "dup xol area");
1862 }
1863 
1864 /*
1865  * Called in context of a new clone/fork from copy_process.
1866  */
uprobe_copy_process(struct task_struct * t,unsigned long flags)1867 void uprobe_copy_process(struct task_struct *t, unsigned long flags)
1868 {
1869 	struct uprobe_task *utask = current->utask;
1870 	struct mm_struct *mm = current->mm;
1871 	struct xol_area *area;
1872 
1873 	t->utask = NULL;
1874 
1875 	if (!utask || !utask->return_instances)
1876 		return;
1877 
1878 	if (mm == t->mm && !(flags & CLONE_VFORK))
1879 		return;
1880 
1881 	if (dup_utask(t, utask))
1882 		return uprobe_warn(t, "dup ret instances");
1883 
1884 	/* The task can fork() after dup_xol_work() fails */
1885 	area = mm->uprobes_state.xol_area;
1886 	if (!area)
1887 		return uprobe_warn(t, "dup xol area");
1888 
1889 	if (mm == t->mm)
1890 		return;
1891 
1892 	t->utask->dup_xol_addr = area->vaddr;
1893 	init_task_work(&t->utask->dup_xol_work, dup_xol_work);
1894 	task_work_add(t, &t->utask->dup_xol_work, TWA_RESUME);
1895 }
1896 
1897 /*
1898  * Current area->vaddr notion assume the trampoline address is always
1899  * equal area->vaddr.
1900  *
1901  * Returns -1 in case the xol_area is not allocated.
1902  */
uprobe_get_trampoline_vaddr(void)1903 unsigned long uprobe_get_trampoline_vaddr(void)
1904 {
1905 	unsigned long trampoline_vaddr = UPROBE_NO_TRAMPOLINE_VADDR;
1906 	struct xol_area *area;
1907 
1908 	/* Pairs with xol_add_vma() smp_store_release() */
1909 	area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */
1910 	if (area)
1911 		trampoline_vaddr = area->vaddr;
1912 
1913 	return trampoline_vaddr;
1914 }
1915 
cleanup_return_instances(struct uprobe_task * utask,bool chained,struct pt_regs * regs)1916 static void cleanup_return_instances(struct uprobe_task *utask, bool chained,
1917 					struct pt_regs *regs)
1918 {
1919 	struct return_instance *ri = utask->return_instances;
1920 	enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL;
1921 
1922 	while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) {
1923 		ri = free_ret_instance(ri);
1924 		utask->depth--;
1925 	}
1926 	utask->return_instances = ri;
1927 }
1928 
prepare_uretprobe(struct uprobe * uprobe,struct pt_regs * regs)1929 static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
1930 {
1931 	struct return_instance *ri;
1932 	struct uprobe_task *utask;
1933 	unsigned long orig_ret_vaddr, trampoline_vaddr;
1934 	bool chained;
1935 
1936 	if (!get_xol_area())
1937 		return;
1938 
1939 	utask = get_utask();
1940 	if (!utask)
1941 		return;
1942 
1943 	if (utask->depth >= MAX_URETPROBE_DEPTH) {
1944 		printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
1945 				" nestedness limit pid/tgid=%d/%d\n",
1946 				current->pid, current->tgid);
1947 		return;
1948 	}
1949 
1950 	/* we need to bump refcount to store uprobe in utask */
1951 	if (!try_get_uprobe(uprobe))
1952 		return;
1953 
1954 	ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
1955 	if (!ri)
1956 		goto fail;
1957 
1958 	trampoline_vaddr = uprobe_get_trampoline_vaddr();
1959 	orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
1960 	if (orig_ret_vaddr == -1)
1961 		goto fail;
1962 
1963 	/* drop the entries invalidated by longjmp() */
1964 	chained = (orig_ret_vaddr == trampoline_vaddr);
1965 	cleanup_return_instances(utask, chained, regs);
1966 
1967 	/*
1968 	 * We don't want to keep trampoline address in stack, rather keep the
1969 	 * original return address of first caller thru all the consequent
1970 	 * instances. This also makes breakpoint unwrapping easier.
1971 	 */
1972 	if (chained) {
1973 		if (!utask->return_instances) {
1974 			/*
1975 			 * This situation is not possible. Likely we have an
1976 			 * attack from user-space.
1977 			 */
1978 			uprobe_warn(current, "handle tail call");
1979 			goto fail;
1980 		}
1981 		orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
1982 	}
1983 	ri->uprobe = uprobe;
1984 	ri->func = instruction_pointer(regs);
1985 	ri->stack = user_stack_pointer(regs);
1986 	ri->orig_ret_vaddr = orig_ret_vaddr;
1987 	ri->chained = chained;
1988 
1989 	utask->depth++;
1990 	ri->next = utask->return_instances;
1991 	utask->return_instances = ri;
1992 
1993 	return;
1994 fail:
1995 	kfree(ri);
1996 	put_uprobe(uprobe);
1997 }
1998 
1999 /* Prepare to single-step probed instruction out of line. */
2000 static int
pre_ssout(struct uprobe * uprobe,struct pt_regs * regs,unsigned long bp_vaddr)2001 pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
2002 {
2003 	struct uprobe_task *utask;
2004 	unsigned long xol_vaddr;
2005 	int err;
2006 
2007 	utask = get_utask();
2008 	if (!utask)
2009 		return -ENOMEM;
2010 
2011 	if (!try_get_uprobe(uprobe))
2012 		return -EINVAL;
2013 
2014 	xol_vaddr = xol_get_insn_slot(uprobe);
2015 	if (!xol_vaddr) {
2016 		err = -ENOMEM;
2017 		goto err_out;
2018 	}
2019 
2020 	utask->xol_vaddr = xol_vaddr;
2021 	utask->vaddr = bp_vaddr;
2022 
2023 	err = arch_uprobe_pre_xol(&uprobe->arch, regs);
2024 	if (unlikely(err)) {
2025 		xol_free_insn_slot(current);
2026 		goto err_out;
2027 	}
2028 
2029 	utask->active_uprobe = uprobe;
2030 	utask->state = UTASK_SSTEP;
2031 	return 0;
2032 err_out:
2033 	put_uprobe(uprobe);
2034 	return err;
2035 }
2036 
2037 /*
2038  * If we are singlestepping, then ensure this thread is not connected to
2039  * non-fatal signals until completion of singlestep.  When xol insn itself
2040  * triggers the signal,  restart the original insn even if the task is
2041  * already SIGKILL'ed (since coredump should report the correct ip).  This
2042  * is even more important if the task has a handler for SIGSEGV/etc, The
2043  * _same_ instruction should be repeated again after return from the signal
2044  * handler, and SSTEP can never finish in this case.
2045  */
uprobe_deny_signal(void)2046 bool uprobe_deny_signal(void)
2047 {
2048 	struct task_struct *t = current;
2049 	struct uprobe_task *utask = t->utask;
2050 
2051 	if (likely(!utask || !utask->active_uprobe))
2052 		return false;
2053 
2054 	WARN_ON_ONCE(utask->state != UTASK_SSTEP);
2055 
2056 	if (task_sigpending(t)) {
2057 		spin_lock_irq(&t->sighand->siglock);
2058 		clear_tsk_thread_flag(t, TIF_SIGPENDING);
2059 		spin_unlock_irq(&t->sighand->siglock);
2060 
2061 		if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
2062 			utask->state = UTASK_SSTEP_TRAPPED;
2063 			set_tsk_thread_flag(t, TIF_UPROBE);
2064 		}
2065 	}
2066 
2067 	return true;
2068 }
2069 
mmf_recalc_uprobes(struct mm_struct * mm)2070 static void mmf_recalc_uprobes(struct mm_struct *mm)
2071 {
2072 	VMA_ITERATOR(vmi, mm, 0);
2073 	struct vm_area_struct *vma;
2074 
2075 	for_each_vma(vmi, vma) {
2076 		if (!valid_vma(vma, false))
2077 			continue;
2078 		/*
2079 		 * This is not strictly accurate, we can race with
2080 		 * uprobe_unregister() and see the already removed
2081 		 * uprobe if delete_uprobe() was not yet called.
2082 		 * Or this uprobe can be filtered out.
2083 		 */
2084 		if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
2085 			return;
2086 	}
2087 
2088 	clear_bit(MMF_HAS_UPROBES, &mm->flags);
2089 }
2090 
is_trap_at_addr(struct mm_struct * mm,unsigned long vaddr)2091 static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
2092 {
2093 	struct page *page;
2094 	uprobe_opcode_t opcode;
2095 	int result;
2096 
2097 	if (WARN_ON_ONCE(!IS_ALIGNED(vaddr, UPROBE_SWBP_INSN_SIZE)))
2098 		return -EINVAL;
2099 
2100 	pagefault_disable();
2101 	result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);
2102 	pagefault_enable();
2103 
2104 	if (likely(result == 0))
2105 		goto out;
2106 
2107 	result = get_user_pages(vaddr, 1, FOLL_FORCE, &page);
2108 	if (result < 0)
2109 		return result;
2110 
2111 	copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
2112 	put_page(page);
2113  out:
2114 	/* This needs to return true for any variant of the trap insn */
2115 	return is_trap_insn(&opcode);
2116 }
2117 
2118 /* assumes being inside RCU protected region */
find_active_uprobe_rcu(unsigned long bp_vaddr,int * is_swbp)2119 static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swbp)
2120 {
2121 	struct mm_struct *mm = current->mm;
2122 	struct uprobe *uprobe = NULL;
2123 	struct vm_area_struct *vma;
2124 
2125 	mmap_read_lock(mm);
2126 	vma = vma_lookup(mm, bp_vaddr);
2127 	if (vma) {
2128 		if (valid_vma(vma, false)) {
2129 			struct inode *inode = file_inode(vma->vm_file);
2130 			loff_t offset = vaddr_to_offset(vma, bp_vaddr);
2131 
2132 			uprobe = find_uprobe_rcu(inode, offset);
2133 		}
2134 
2135 		if (!uprobe)
2136 			*is_swbp = is_trap_at_addr(mm, bp_vaddr);
2137 	} else {
2138 		*is_swbp = -EFAULT;
2139 	}
2140 
2141 	if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
2142 		mmf_recalc_uprobes(mm);
2143 	mmap_read_unlock(mm);
2144 
2145 	return uprobe;
2146 }
2147 
handler_chain(struct uprobe * uprobe,struct pt_regs * regs)2148 static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
2149 {
2150 	struct uprobe_consumer *uc;
2151 	int remove = UPROBE_HANDLER_REMOVE;
2152 	bool need_prep = false; /* prepare return uprobe, when needed */
2153 	bool has_consumers = false;
2154 
2155 	current->utask->auprobe = &uprobe->arch;
2156 
2157 	list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node,
2158 				 srcu_read_lock_held(&uprobes_srcu)) {
2159 		int rc = 0;
2160 
2161 		if (uc->handler) {
2162 			rc = uc->handler(uc, regs);
2163 			WARN(rc & ~UPROBE_HANDLER_MASK,
2164 				"bad rc=0x%x from %ps()\n", rc, uc->handler);
2165 		}
2166 
2167 		if (uc->ret_handler)
2168 			need_prep = true;
2169 
2170 		remove &= rc;
2171 		has_consumers = true;
2172 	}
2173 	current->utask->auprobe = NULL;
2174 
2175 	if (need_prep && !remove)
2176 		prepare_uretprobe(uprobe, regs); /* put bp at return */
2177 
2178 	if (remove && has_consumers) {
2179 		down_read(&uprobe->register_rwsem);
2180 
2181 		/* re-check that removal is still required, this time under lock */
2182 		if (!filter_chain(uprobe, current->mm)) {
2183 			WARN_ON(!uprobe_is_active(uprobe));
2184 			unapply_uprobe(uprobe, current->mm);
2185 		}
2186 
2187 		up_read(&uprobe->register_rwsem);
2188 	}
2189 }
2190 
2191 static void
handle_uretprobe_chain(struct return_instance * ri,struct pt_regs * regs)2192 handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
2193 {
2194 	struct uprobe *uprobe = ri->uprobe;
2195 	struct uprobe_consumer *uc;
2196 	int srcu_idx;
2197 
2198 	srcu_idx = srcu_read_lock(&uprobes_srcu);
2199 	list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node,
2200 				 srcu_read_lock_held(&uprobes_srcu)) {
2201 		if (uc->ret_handler)
2202 			uc->ret_handler(uc, ri->func, regs);
2203 	}
2204 	srcu_read_unlock(&uprobes_srcu, srcu_idx);
2205 }
2206 
find_next_ret_chain(struct return_instance * ri)2207 static struct return_instance *find_next_ret_chain(struct return_instance *ri)
2208 {
2209 	bool chained;
2210 
2211 	do {
2212 		chained = ri->chained;
2213 		ri = ri->next;	/* can't be NULL if chained */
2214 	} while (chained);
2215 
2216 	return ri;
2217 }
2218 
uprobe_handle_trampoline(struct pt_regs * regs)2219 void uprobe_handle_trampoline(struct pt_regs *regs)
2220 {
2221 	struct uprobe_task *utask;
2222 	struct return_instance *ri, *next;
2223 	bool valid;
2224 
2225 	utask = current->utask;
2226 	if (!utask)
2227 		goto sigill;
2228 
2229 	ri = utask->return_instances;
2230 	if (!ri)
2231 		goto sigill;
2232 
2233 	do {
2234 		/*
2235 		 * We should throw out the frames invalidated by longjmp().
2236 		 * If this chain is valid, then the next one should be alive
2237 		 * or NULL; the latter case means that nobody but ri->func
2238 		 * could hit this trampoline on return. TODO: sigaltstack().
2239 		 */
2240 		next = find_next_ret_chain(ri);
2241 		valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs);
2242 
2243 		instruction_pointer_set(regs, ri->orig_ret_vaddr);
2244 		do {
2245 			/* pop current instance from the stack of pending return instances,
2246 			 * as it's not pending anymore: we just fixed up original
2247 			 * instruction pointer in regs and are about to call handlers;
2248 			 * this allows fixup_uretprobe_trampoline_entries() to properly fix up
2249 			 * captured stack traces from uretprobe handlers, in which pending
2250 			 * trampoline addresses on the stack are replaced with correct
2251 			 * original return addresses
2252 			 */
2253 			utask->return_instances = ri->next;
2254 			if (valid)
2255 				handle_uretprobe_chain(ri, regs);
2256 			ri = free_ret_instance(ri);
2257 			utask->depth--;
2258 		} while (ri != next);
2259 	} while (!valid);
2260 
2261 	utask->return_instances = ri;
2262 	return;
2263 
2264  sigill:
2265 	uprobe_warn(current, "handle uretprobe, sending SIGILL.");
2266 	force_sig(SIGILL);
2267 
2268 }
2269 
arch_uprobe_ignore(struct arch_uprobe * aup,struct pt_regs * regs)2270 bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs)
2271 {
2272 	return false;
2273 }
2274 
arch_uretprobe_is_alive(struct return_instance * ret,enum rp_check ctx,struct pt_regs * regs)2275 bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
2276 					struct pt_regs *regs)
2277 {
2278 	return true;
2279 }
2280 
2281 /*
2282  * Run handler and ask thread to singlestep.
2283  * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
2284  */
handle_swbp(struct pt_regs * regs)2285 static void handle_swbp(struct pt_regs *regs)
2286 {
2287 	struct uprobe *uprobe;
2288 	unsigned long bp_vaddr;
2289 	int is_swbp, srcu_idx;
2290 
2291 	bp_vaddr = uprobe_get_swbp_addr(regs);
2292 	if (bp_vaddr == uprobe_get_trampoline_vaddr())
2293 		return uprobe_handle_trampoline(regs);
2294 
2295 	srcu_idx = srcu_read_lock(&uprobes_srcu);
2296 
2297 	uprobe = find_active_uprobe_rcu(bp_vaddr, &is_swbp);
2298 	if (!uprobe) {
2299 		if (is_swbp > 0) {
2300 			/* No matching uprobe; signal SIGTRAP. */
2301 			force_sig(SIGTRAP);
2302 		} else {
2303 			/*
2304 			 * Either we raced with uprobe_unregister() or we can't
2305 			 * access this memory. The latter is only possible if
2306 			 * another thread plays with our ->mm. In both cases
2307 			 * we can simply restart. If this vma was unmapped we
2308 			 * can pretend this insn was not executed yet and get
2309 			 * the (correct) SIGSEGV after restart.
2310 			 */
2311 			instruction_pointer_set(regs, bp_vaddr);
2312 		}
2313 		goto out;
2314 	}
2315 
2316 	/* change it in advance for ->handler() and restart */
2317 	instruction_pointer_set(regs, bp_vaddr);
2318 
2319 	/*
2320 	 * TODO: move copy_insn/etc into _register and remove this hack.
2321 	 * After we hit the bp, _unregister + _register can install the
2322 	 * new and not-yet-analyzed uprobe at the same address, restart.
2323 	 */
2324 	if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
2325 		goto out;
2326 
2327 	/*
2328 	 * Pairs with the smp_wmb() in prepare_uprobe().
2329 	 *
2330 	 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
2331 	 * we must also see the stores to &uprobe->arch performed by the
2332 	 * prepare_uprobe() call.
2333 	 */
2334 	smp_rmb();
2335 
2336 	/* Tracing handlers use ->utask to communicate with fetch methods */
2337 	if (!get_utask())
2338 		goto out;
2339 
2340 	if (arch_uprobe_ignore(&uprobe->arch, regs))
2341 		goto out;
2342 
2343 	handler_chain(uprobe, regs);
2344 
2345 	if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
2346 		goto out;
2347 
2348 	if (pre_ssout(uprobe, regs, bp_vaddr))
2349 		goto out;
2350 
2351 out:
2352 	/* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */
2353 	srcu_read_unlock(&uprobes_srcu, srcu_idx);
2354 }
2355 
2356 /*
2357  * Perform required fix-ups and disable singlestep.
2358  * Allow pending signals to take effect.
2359  */
handle_singlestep(struct uprobe_task * utask,struct pt_regs * regs)2360 static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
2361 {
2362 	struct uprobe *uprobe;
2363 	int err = 0;
2364 
2365 	uprobe = utask->active_uprobe;
2366 	if (utask->state == UTASK_SSTEP_ACK)
2367 		err = arch_uprobe_post_xol(&uprobe->arch, regs);
2368 	else if (utask->state == UTASK_SSTEP_TRAPPED)
2369 		arch_uprobe_abort_xol(&uprobe->arch, regs);
2370 	else
2371 		WARN_ON_ONCE(1);
2372 
2373 	put_uprobe(uprobe);
2374 	utask->active_uprobe = NULL;
2375 	utask->state = UTASK_RUNNING;
2376 	xol_free_insn_slot(current);
2377 
2378 	spin_lock_irq(&current->sighand->siglock);
2379 	recalc_sigpending(); /* see uprobe_deny_signal() */
2380 	spin_unlock_irq(&current->sighand->siglock);
2381 
2382 	if (unlikely(err)) {
2383 		uprobe_warn(current, "execute the probed insn, sending SIGILL.");
2384 		force_sig(SIGILL);
2385 	}
2386 }
2387 
2388 /*
2389  * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
2390  * allows the thread to return from interrupt. After that handle_swbp()
2391  * sets utask->active_uprobe.
2392  *
2393  * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
2394  * and allows the thread to return from interrupt.
2395  *
2396  * While returning to userspace, thread notices the TIF_UPROBE flag and calls
2397  * uprobe_notify_resume().
2398  */
uprobe_notify_resume(struct pt_regs * regs)2399 void uprobe_notify_resume(struct pt_regs *regs)
2400 {
2401 	struct uprobe_task *utask;
2402 
2403 	clear_thread_flag(TIF_UPROBE);
2404 
2405 	utask = current->utask;
2406 	if (utask && utask->active_uprobe)
2407 		handle_singlestep(utask, regs);
2408 	else
2409 		handle_swbp(regs);
2410 }
2411 
2412 /*
2413  * uprobe_pre_sstep_notifier gets called from interrupt context as part of
2414  * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
2415  */
uprobe_pre_sstep_notifier(struct pt_regs * regs)2416 int uprobe_pre_sstep_notifier(struct pt_regs *regs)
2417 {
2418 	if (!current->mm)
2419 		return 0;
2420 
2421 	if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
2422 	    (!current->utask || !current->utask->return_instances))
2423 		return 0;
2424 
2425 	set_thread_flag(TIF_UPROBE);
2426 	return 1;
2427 }
2428 
2429 /*
2430  * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
2431  * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
2432  */
uprobe_post_sstep_notifier(struct pt_regs * regs)2433 int uprobe_post_sstep_notifier(struct pt_regs *regs)
2434 {
2435 	struct uprobe_task *utask = current->utask;
2436 
2437 	if (!current->mm || !utask || !utask->active_uprobe)
2438 		/* task is currently not uprobed */
2439 		return 0;
2440 
2441 	utask->state = UTASK_SSTEP_ACK;
2442 	set_thread_flag(TIF_UPROBE);
2443 	return 1;
2444 }
2445 
2446 static struct notifier_block uprobe_exception_nb = {
2447 	.notifier_call		= arch_uprobe_exception_notify,
2448 	.priority		= INT_MAX-1,	/* notified after kprobes, kgdb */
2449 };
2450 
uprobes_init(void)2451 void __init uprobes_init(void)
2452 {
2453 	int i;
2454 
2455 	for (i = 0; i < UPROBES_HASH_SZ; i++)
2456 		mutex_init(&uprobes_mmap_mutex[i]);
2457 
2458 	BUG_ON(register_die_notifier(&uprobe_exception_nb));
2459 }
2460