xref: /openbmc/linux/kernel/events/uprobes.c (revision 194f8dcb)
1a5f4374aSIngo Molnar /*
2a5f4374aSIngo Molnar  * User-space Probes (UProbes)
3a5f4374aSIngo Molnar  *
4a5f4374aSIngo Molnar  * This program is free software; you can redistribute it and/or modify
5a5f4374aSIngo Molnar  * it under the terms of the GNU General Public License as published by
6a5f4374aSIngo Molnar  * the Free Software Foundation; either version 2 of the License, or
7a5f4374aSIngo Molnar  * (at your option) any later version.
8a5f4374aSIngo Molnar  *
9a5f4374aSIngo Molnar  * This program is distributed in the hope that it will be useful,
10a5f4374aSIngo Molnar  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a5f4374aSIngo Molnar  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12a5f4374aSIngo Molnar  * GNU General Public License for more details.
13a5f4374aSIngo Molnar  *
14a5f4374aSIngo Molnar  * You should have received a copy of the GNU General Public License
15a5f4374aSIngo Molnar  * along with this program; if not, write to the Free Software
16a5f4374aSIngo Molnar  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17a5f4374aSIngo Molnar  *
1835aa621bSIngo Molnar  * Copyright (C) IBM Corporation, 2008-2012
19a5f4374aSIngo Molnar  * Authors:
20a5f4374aSIngo Molnar  *	Srikar Dronamraju
21a5f4374aSIngo Molnar  *	Jim Keniston
2235aa621bSIngo Molnar  * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
23a5f4374aSIngo Molnar  */
24a5f4374aSIngo Molnar 
25a5f4374aSIngo Molnar #include <linux/kernel.h>
26a5f4374aSIngo Molnar #include <linux/highmem.h>
27a5f4374aSIngo Molnar #include <linux/pagemap.h>	/* read_mapping_page */
28a5f4374aSIngo Molnar #include <linux/slab.h>
29a5f4374aSIngo Molnar #include <linux/sched.h>
30a5f4374aSIngo Molnar #include <linux/rmap.h>		/* anon_vma_prepare */
31a5f4374aSIngo Molnar #include <linux/mmu_notifier.h>	/* set_pte_at_notify */
32a5f4374aSIngo Molnar #include <linux/swap.h>		/* try_to_free_swap */
330326f5a9SSrikar Dronamraju #include <linux/ptrace.h>	/* user_enable_single_step */
340326f5a9SSrikar Dronamraju #include <linux/kdebug.h>	/* notifier mechanism */
35194f8dcbSOleg Nesterov #include "../../mm/internal.h"	/* munlock_vma_page */
36a5f4374aSIngo Molnar 
37a5f4374aSIngo Molnar #include <linux/uprobes.h>
38a5f4374aSIngo Molnar 
39d4b3b638SSrikar Dronamraju #define UINSNS_PER_PAGE			(PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
40d4b3b638SSrikar Dronamraju #define MAX_UPROBE_XOL_SLOTS		UINSNS_PER_PAGE
41d4b3b638SSrikar Dronamraju 
42a5f4374aSIngo Molnar static struct rb_root uprobes_tree = RB_ROOT;
43a5f4374aSIngo Molnar 
44a5f4374aSIngo Molnar static DEFINE_SPINLOCK(uprobes_treelock);	/* serialize rbtree access */
45a5f4374aSIngo Molnar 
46a5f4374aSIngo Molnar #define UPROBES_HASH_SZ	13
47a5f4374aSIngo Molnar 
48c5784de2SPeter Zijlstra /*
49c5784de2SPeter Zijlstra  * We need separate register/unregister and mmap/munmap lock hashes because
50c5784de2SPeter Zijlstra  * of mmap_sem nesting.
51c5784de2SPeter Zijlstra  *
52c5784de2SPeter Zijlstra  * uprobe_register() needs to install probes on (potentially) all processes
53c5784de2SPeter Zijlstra  * and thus needs to acquire multiple mmap_sems (consequtively, not
54c5784de2SPeter Zijlstra  * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
55c5784de2SPeter Zijlstra  * for the particular process doing the mmap.
56c5784de2SPeter Zijlstra  *
57c5784de2SPeter Zijlstra  * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
58c5784de2SPeter Zijlstra  * because of lock order against i_mmap_mutex. This means there's a hole in
59c5784de2SPeter Zijlstra  * the register vma iteration where a mmap() can happen.
60c5784de2SPeter Zijlstra  *
61c5784de2SPeter Zijlstra  * Thus uprobe_register() can race with uprobe_mmap() and we can try and
62c5784de2SPeter Zijlstra  * install a probe where one is already installed.
63c5784de2SPeter Zijlstra  */
64c5784de2SPeter Zijlstra 
65a5f4374aSIngo Molnar /* serialize (un)register */
66a5f4374aSIngo Molnar static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
67a5f4374aSIngo Molnar 
68a5f4374aSIngo Molnar #define uprobes_hash(v)		(&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
69a5f4374aSIngo Molnar 
70a5f4374aSIngo Molnar /* serialize uprobe->pending_list */
71a5f4374aSIngo Molnar static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
72a5f4374aSIngo Molnar #define uprobes_mmap_hash(v)	(&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
73a5f4374aSIngo Molnar 
74a5f4374aSIngo Molnar /*
75a5f4374aSIngo Molnar  * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
76a5f4374aSIngo Molnar  * events active at this time.  Probably a fine grained per inode count is
77a5f4374aSIngo Molnar  * better?
78a5f4374aSIngo Molnar  */
79a5f4374aSIngo Molnar static atomic_t uprobe_events = ATOMIC_INIT(0);
80a5f4374aSIngo Molnar 
813ff54efdSSrikar Dronamraju struct uprobe {
823ff54efdSSrikar Dronamraju 	struct rb_node		rb_node;	/* node in the rb tree */
833ff54efdSSrikar Dronamraju 	atomic_t		ref;
843ff54efdSSrikar Dronamraju 	struct rw_semaphore	consumer_rwsem;
853ff54efdSSrikar Dronamraju 	struct list_head	pending_list;
863ff54efdSSrikar Dronamraju 	struct uprobe_consumer	*consumers;
873ff54efdSSrikar Dronamraju 	struct inode		*inode;		/* Also hold a ref to inode */
883ff54efdSSrikar Dronamraju 	loff_t			offset;
893ff54efdSSrikar Dronamraju 	int			flags;
903ff54efdSSrikar Dronamraju 	struct arch_uprobe	arch;
913ff54efdSSrikar Dronamraju };
923ff54efdSSrikar Dronamraju 
93a5f4374aSIngo Molnar /*
94a5f4374aSIngo Molnar  * valid_vma: Verify if the specified vma is an executable vma
95a5f4374aSIngo Molnar  * Relax restrictions while unregistering: vm_flags might have
96a5f4374aSIngo Molnar  * changed after breakpoint was inserted.
97a5f4374aSIngo Molnar  *	- is_register: indicates if we are in register context.
98a5f4374aSIngo Molnar  *	- Return 1 if the specified virtual address is in an
99a5f4374aSIngo Molnar  *	  executable vma.
100a5f4374aSIngo Molnar  */
101a5f4374aSIngo Molnar static bool valid_vma(struct vm_area_struct *vma, bool is_register)
102a5f4374aSIngo Molnar {
103a5f4374aSIngo Molnar 	if (!vma->vm_file)
104a5f4374aSIngo Molnar 		return false;
105a5f4374aSIngo Molnar 
106a5f4374aSIngo Molnar 	if (!is_register)
107a5f4374aSIngo Molnar 		return true;
108a5f4374aSIngo Molnar 
109ea131377SOleg Nesterov 	if ((vma->vm_flags & (VM_HUGETLB|VM_READ|VM_WRITE|VM_EXEC|VM_SHARED))
110ea131377SOleg Nesterov 				== (VM_READ|VM_EXEC))
111a5f4374aSIngo Molnar 		return true;
112a5f4374aSIngo Molnar 
113a5f4374aSIngo Molnar 	return false;
114a5f4374aSIngo Molnar }
115a5f4374aSIngo Molnar 
11657683f72SOleg Nesterov static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
117a5f4374aSIngo Molnar {
11857683f72SOleg Nesterov 	return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
119a5f4374aSIngo Molnar }
120a5f4374aSIngo Molnar 
121cb113b47SOleg Nesterov static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
122cb113b47SOleg Nesterov {
123cb113b47SOleg Nesterov 	return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
124cb113b47SOleg Nesterov }
125cb113b47SOleg Nesterov 
126a5f4374aSIngo Molnar /**
127a5f4374aSIngo Molnar  * __replace_page - replace page in vma by new page.
128a5f4374aSIngo Molnar  * based on replace_page in mm/ksm.c
129a5f4374aSIngo Molnar  *
130a5f4374aSIngo Molnar  * @vma:      vma that holds the pte pointing to page
131c517ee74SOleg Nesterov  * @addr:     address the old @page is mapped at
132a5f4374aSIngo Molnar  * @page:     the cowed page we are replacing by kpage
133a5f4374aSIngo Molnar  * @kpage:    the modified page we replace page by
134a5f4374aSIngo Molnar  *
135a5f4374aSIngo Molnar  * Returns 0 on success, -EFAULT on failure.
136a5f4374aSIngo Molnar  */
137c517ee74SOleg Nesterov static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
138c517ee74SOleg Nesterov 				struct page *page, struct page *kpage)
139a5f4374aSIngo Molnar {
140a5f4374aSIngo Molnar 	struct mm_struct *mm = vma->vm_mm;
1415323ce71SOleg Nesterov 	spinlock_t *ptl;
1425323ce71SOleg Nesterov 	pte_t *ptep;
1439f92448cSOleg Nesterov 	int err;
144a5f4374aSIngo Molnar 
145194f8dcbSOleg Nesterov 	/* For try_to_free_swap() and munlock_vma_page() below */
1469f92448cSOleg Nesterov 	lock_page(page);
1479f92448cSOleg Nesterov 
1489f92448cSOleg Nesterov 	err = -EAGAIN;
1495323ce71SOleg Nesterov 	ptep = page_check_address(page, mm, addr, &ptl, 0);
150a5f4374aSIngo Molnar 	if (!ptep)
1519f92448cSOleg Nesterov 		goto unlock;
152a5f4374aSIngo Molnar 
153a5f4374aSIngo Molnar 	get_page(kpage);
154a5f4374aSIngo Molnar 	page_add_new_anon_rmap(kpage, vma, addr);
155a5f4374aSIngo Molnar 
1567396fa81SSrikar Dronamraju 	if (!PageAnon(page)) {
1577396fa81SSrikar Dronamraju 		dec_mm_counter(mm, MM_FILEPAGES);
1587396fa81SSrikar Dronamraju 		inc_mm_counter(mm, MM_ANONPAGES);
1597396fa81SSrikar Dronamraju 	}
1607396fa81SSrikar Dronamraju 
161a5f4374aSIngo Molnar 	flush_cache_page(vma, addr, pte_pfn(*ptep));
162a5f4374aSIngo Molnar 	ptep_clear_flush(vma, addr, ptep);
163a5f4374aSIngo Molnar 	set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
164a5f4374aSIngo Molnar 
165a5f4374aSIngo Molnar 	page_remove_rmap(page);
166a5f4374aSIngo Molnar 	if (!page_mapped(page))
167a5f4374aSIngo Molnar 		try_to_free_swap(page);
168a5f4374aSIngo Molnar 	pte_unmap_unlock(ptep, ptl);
169a5f4374aSIngo Molnar 
170194f8dcbSOleg Nesterov 	if (vma->vm_flags & VM_LOCKED)
171194f8dcbSOleg Nesterov 		munlock_vma_page(page);
172194f8dcbSOleg Nesterov 	put_page(page);
173194f8dcbSOleg Nesterov 
1749f92448cSOleg Nesterov 	err = 0;
1759f92448cSOleg Nesterov  unlock:
1769f92448cSOleg Nesterov 	unlock_page(page);
1779f92448cSOleg Nesterov 	return err;
178a5f4374aSIngo Molnar }
179a5f4374aSIngo Molnar 
180a5f4374aSIngo Molnar /**
1815cb4ac3aSSrikar Dronamraju  * is_swbp_insn - check if instruction is breakpoint instruction.
182a5f4374aSIngo Molnar  * @insn: instruction to be checked.
1835cb4ac3aSSrikar Dronamraju  * Default implementation of is_swbp_insn
184a5f4374aSIngo Molnar  * Returns true if @insn is a breakpoint instruction.
185a5f4374aSIngo Molnar  */
1865cb4ac3aSSrikar Dronamraju bool __weak is_swbp_insn(uprobe_opcode_t *insn)
187a5f4374aSIngo Molnar {
1885cb4ac3aSSrikar Dronamraju 	return *insn == UPROBE_SWBP_INSN;
189a5f4374aSIngo Molnar }
190a5f4374aSIngo Molnar 
191a5f4374aSIngo Molnar /*
192a5f4374aSIngo Molnar  * NOTE:
193a5f4374aSIngo Molnar  * Expect the breakpoint instruction to be the smallest size instruction for
194a5f4374aSIngo Molnar  * the architecture. If an arch has variable length instruction and the
195a5f4374aSIngo Molnar  * breakpoint instruction is not of the smallest length instruction
196a5f4374aSIngo Molnar  * supported by that architecture then we need to modify read_opcode /
197a5f4374aSIngo Molnar  * write_opcode accordingly. This would never be a problem for archs that
198a5f4374aSIngo Molnar  * have fixed length instructions.
199a5f4374aSIngo Molnar  */
200a5f4374aSIngo Molnar 
201a5f4374aSIngo Molnar /*
202a5f4374aSIngo Molnar  * write_opcode - write the opcode at a given virtual address.
203e3343e6aSSrikar Dronamraju  * @auprobe: arch breakpointing information.
204a5f4374aSIngo Molnar  * @mm: the probed process address space.
205a5f4374aSIngo Molnar  * @vaddr: the virtual address to store the opcode.
206a5f4374aSIngo Molnar  * @opcode: opcode to be written at @vaddr.
207a5f4374aSIngo Molnar  *
208a5f4374aSIngo Molnar  * Called with mm->mmap_sem held (for read and with a reference to
209a5f4374aSIngo Molnar  * mm).
210a5f4374aSIngo Molnar  *
211a5f4374aSIngo Molnar  * For mm @mm, write the opcode at @vaddr.
212a5f4374aSIngo Molnar  * Return 0 (success) or a negative errno.
213a5f4374aSIngo Molnar  */
214e3343e6aSSrikar Dronamraju static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
215a5f4374aSIngo Molnar 			unsigned long vaddr, uprobe_opcode_t opcode)
216a5f4374aSIngo Molnar {
217a5f4374aSIngo Molnar 	struct page *old_page, *new_page;
218a5f4374aSIngo Molnar 	void *vaddr_old, *vaddr_new;
219a5f4374aSIngo Molnar 	struct vm_area_struct *vma;
220a5f4374aSIngo Molnar 	int ret;
221f403072cSOleg Nesterov 
2225323ce71SOleg Nesterov retry:
223a5f4374aSIngo Molnar 	/* Read the page with vaddr into memory */
224a5f4374aSIngo Molnar 	ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
225a5f4374aSIngo Molnar 	if (ret <= 0)
226a5f4374aSIngo Molnar 		return ret;
227a5f4374aSIngo Molnar 
228a5f4374aSIngo Molnar 	ret = -ENOMEM;
229a5f4374aSIngo Molnar 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
230a5f4374aSIngo Molnar 	if (!new_page)
2319f92448cSOleg Nesterov 		goto put_old;
232a5f4374aSIngo Molnar 
233a5f4374aSIngo Molnar 	__SetPageUptodate(new_page);
234a5f4374aSIngo Molnar 
235a5f4374aSIngo Molnar 	/* copy the page now that we've got it stable */
236a5f4374aSIngo Molnar 	vaddr_old = kmap_atomic(old_page);
237a5f4374aSIngo Molnar 	vaddr_new = kmap_atomic(new_page);
238a5f4374aSIngo Molnar 
239a5f4374aSIngo Molnar 	memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
240d9c4a30eSOleg Nesterov 	memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
241a5f4374aSIngo Molnar 
242a5f4374aSIngo Molnar 	kunmap_atomic(vaddr_new);
243a5f4374aSIngo Molnar 	kunmap_atomic(vaddr_old);
244a5f4374aSIngo Molnar 
245a5f4374aSIngo Molnar 	ret = anon_vma_prepare(vma);
246a5f4374aSIngo Molnar 	if (ret)
2479f92448cSOleg Nesterov 		goto put_new;
248a5f4374aSIngo Molnar 
249c517ee74SOleg Nesterov 	ret = __replace_page(vma, vaddr, old_page, new_page);
250a5f4374aSIngo Molnar 
2519f92448cSOleg Nesterov put_new:
252a5f4374aSIngo Molnar 	page_cache_release(new_page);
2539f92448cSOleg Nesterov put_old:
254a5f4374aSIngo Molnar 	put_page(old_page);
255a5f4374aSIngo Molnar 
2565323ce71SOleg Nesterov 	if (unlikely(ret == -EAGAIN))
2575323ce71SOleg Nesterov 		goto retry;
258a5f4374aSIngo Molnar 	return ret;
259a5f4374aSIngo Molnar }
260a5f4374aSIngo Molnar 
261a5f4374aSIngo Molnar /**
262a5f4374aSIngo Molnar  * read_opcode - read the opcode at a given virtual address.
263a5f4374aSIngo Molnar  * @mm: the probed process address space.
264a5f4374aSIngo Molnar  * @vaddr: the virtual address to read the opcode.
265a5f4374aSIngo Molnar  * @opcode: location to store the read opcode.
266a5f4374aSIngo Molnar  *
267a5f4374aSIngo Molnar  * Called with mm->mmap_sem held (for read and with a reference to
268a5f4374aSIngo Molnar  * mm.
269a5f4374aSIngo Molnar  *
270a5f4374aSIngo Molnar  * For mm @mm, read the opcode at @vaddr and store it in @opcode.
271a5f4374aSIngo Molnar  * Return 0 (success) or a negative errno.
272a5f4374aSIngo Molnar  */
273a5f4374aSIngo Molnar static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
274a5f4374aSIngo Molnar {
275a5f4374aSIngo Molnar 	struct page *page;
276a5f4374aSIngo Molnar 	void *vaddr_new;
277a5f4374aSIngo Molnar 	int ret;
278a5f4374aSIngo Molnar 
279a3d7bb47SOleg Nesterov 	ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
280a5f4374aSIngo Molnar 	if (ret <= 0)
281a5f4374aSIngo Molnar 		return ret;
282a5f4374aSIngo Molnar 
283a5f4374aSIngo Molnar 	lock_page(page);
284a5f4374aSIngo Molnar 	vaddr_new = kmap_atomic(page);
285a5f4374aSIngo Molnar 	vaddr &= ~PAGE_MASK;
2865cb4ac3aSSrikar Dronamraju 	memcpy(opcode, vaddr_new + vaddr, UPROBE_SWBP_INSN_SIZE);
287a5f4374aSIngo Molnar 	kunmap_atomic(vaddr_new);
288a5f4374aSIngo Molnar 	unlock_page(page);
289a5f4374aSIngo Molnar 
290a5f4374aSIngo Molnar 	put_page(page);
291a5f4374aSIngo Molnar 
292a5f4374aSIngo Molnar 	return 0;
293a5f4374aSIngo Molnar }
294a5f4374aSIngo Molnar 
2955cb4ac3aSSrikar Dronamraju static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
296a5f4374aSIngo Molnar {
297a5f4374aSIngo Molnar 	uprobe_opcode_t opcode;
298a5f4374aSIngo Molnar 	int result;
299a5f4374aSIngo Molnar 
300c00b2750SOleg Nesterov 	if (current->mm == mm) {
301c00b2750SOleg Nesterov 		pagefault_disable();
302c00b2750SOleg Nesterov 		result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
303c00b2750SOleg Nesterov 								sizeof(opcode));
304c00b2750SOleg Nesterov 		pagefault_enable();
305c00b2750SOleg Nesterov 
306c00b2750SOleg Nesterov 		if (likely(result == 0))
307c00b2750SOleg Nesterov 			goto out;
308c00b2750SOleg Nesterov 	}
309c00b2750SOleg Nesterov 
310a5f4374aSIngo Molnar 	result = read_opcode(mm, vaddr, &opcode);
311a5f4374aSIngo Molnar 	if (result)
312a5f4374aSIngo Molnar 		return result;
313c00b2750SOleg Nesterov out:
3145cb4ac3aSSrikar Dronamraju 	if (is_swbp_insn(&opcode))
315a5f4374aSIngo Molnar 		return 1;
316a5f4374aSIngo Molnar 
317a5f4374aSIngo Molnar 	return 0;
318a5f4374aSIngo Molnar }
319a5f4374aSIngo Molnar 
320a5f4374aSIngo Molnar /**
3215cb4ac3aSSrikar Dronamraju  * set_swbp - store breakpoint at a given address.
322e3343e6aSSrikar Dronamraju  * @auprobe: arch specific probepoint information.
323a5f4374aSIngo Molnar  * @mm: the probed process address space.
324a5f4374aSIngo Molnar  * @vaddr: the virtual address to insert the opcode.
325a5f4374aSIngo Molnar  *
326a5f4374aSIngo Molnar  * For mm @mm, store the breakpoint instruction at @vaddr.
327a5f4374aSIngo Molnar  * Return 0 (success) or a negative errno.
328a5f4374aSIngo Molnar  */
3295cb4ac3aSSrikar Dronamraju int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
330a5f4374aSIngo Molnar {
331a5f4374aSIngo Molnar 	int result;
332c5784de2SPeter Zijlstra 	/*
333c5784de2SPeter Zijlstra 	 * See the comment near uprobes_hash().
334c5784de2SPeter Zijlstra 	 */
3355cb4ac3aSSrikar Dronamraju 	result = is_swbp_at_addr(mm, vaddr);
336a5f4374aSIngo Molnar 	if (result == 1)
337a5f4374aSIngo Molnar 		return -EEXIST;
338a5f4374aSIngo Molnar 
339a5f4374aSIngo Molnar 	if (result)
340a5f4374aSIngo Molnar 		return result;
341a5f4374aSIngo Molnar 
3425cb4ac3aSSrikar Dronamraju 	return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
343a5f4374aSIngo Molnar }
344a5f4374aSIngo Molnar 
345a5f4374aSIngo Molnar /**
346a5f4374aSIngo Molnar  * set_orig_insn - Restore the original instruction.
347a5f4374aSIngo Molnar  * @mm: the probed process address space.
348e3343e6aSSrikar Dronamraju  * @auprobe: arch specific probepoint information.
349a5f4374aSIngo Molnar  * @vaddr: the virtual address to insert the opcode.
350a5f4374aSIngo Molnar  * @verify: if true, verify existance of breakpoint instruction.
351a5f4374aSIngo Molnar  *
352a5f4374aSIngo Molnar  * For mm @mm, restore the original opcode (opcode) at @vaddr.
353a5f4374aSIngo Molnar  * Return 0 (success) or a negative errno.
354a5f4374aSIngo Molnar  */
355a5f4374aSIngo Molnar int __weak
356e3343e6aSSrikar Dronamraju set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, bool verify)
357a5f4374aSIngo Molnar {
358a5f4374aSIngo Molnar 	if (verify) {
359a5f4374aSIngo Molnar 		int result;
360a5f4374aSIngo Molnar 
3615cb4ac3aSSrikar Dronamraju 		result = is_swbp_at_addr(mm, vaddr);
362a5f4374aSIngo Molnar 		if (!result)
363a5f4374aSIngo Molnar 			return -EINVAL;
364a5f4374aSIngo Molnar 
365a5f4374aSIngo Molnar 		if (result != 1)
366a5f4374aSIngo Molnar 			return result;
367a5f4374aSIngo Molnar 	}
368e3343e6aSSrikar Dronamraju 	return write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
369a5f4374aSIngo Molnar }
370a5f4374aSIngo Molnar 
371a5f4374aSIngo Molnar static int match_uprobe(struct uprobe *l, struct uprobe *r)
372a5f4374aSIngo Molnar {
373a5f4374aSIngo Molnar 	if (l->inode < r->inode)
374a5f4374aSIngo Molnar 		return -1;
375a5f4374aSIngo Molnar 
376a5f4374aSIngo Molnar 	if (l->inode > r->inode)
377a5f4374aSIngo Molnar 		return 1;
378a5f4374aSIngo Molnar 
379a5f4374aSIngo Molnar 	if (l->offset < r->offset)
380a5f4374aSIngo Molnar 		return -1;
381a5f4374aSIngo Molnar 
382a5f4374aSIngo Molnar 	if (l->offset > r->offset)
383a5f4374aSIngo Molnar 		return 1;
384a5f4374aSIngo Molnar 
385a5f4374aSIngo Molnar 	return 0;
386a5f4374aSIngo Molnar }
387a5f4374aSIngo Molnar 
388a5f4374aSIngo Molnar static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
389a5f4374aSIngo Molnar {
390a5f4374aSIngo Molnar 	struct uprobe u = { .inode = inode, .offset = offset };
391a5f4374aSIngo Molnar 	struct rb_node *n = uprobes_tree.rb_node;
392a5f4374aSIngo Molnar 	struct uprobe *uprobe;
393a5f4374aSIngo Molnar 	int match;
394a5f4374aSIngo Molnar 
395a5f4374aSIngo Molnar 	while (n) {
396a5f4374aSIngo Molnar 		uprobe = rb_entry(n, struct uprobe, rb_node);
397a5f4374aSIngo Molnar 		match = match_uprobe(&u, uprobe);
398a5f4374aSIngo Molnar 		if (!match) {
399a5f4374aSIngo Molnar 			atomic_inc(&uprobe->ref);
400a5f4374aSIngo Molnar 			return uprobe;
401a5f4374aSIngo Molnar 		}
402a5f4374aSIngo Molnar 
403a5f4374aSIngo Molnar 		if (match < 0)
404a5f4374aSIngo Molnar 			n = n->rb_left;
405a5f4374aSIngo Molnar 		else
406a5f4374aSIngo Molnar 			n = n->rb_right;
407a5f4374aSIngo Molnar 	}
408a5f4374aSIngo Molnar 	return NULL;
409a5f4374aSIngo Molnar }
410a5f4374aSIngo Molnar 
411a5f4374aSIngo Molnar /*
412a5f4374aSIngo Molnar  * Find a uprobe corresponding to a given inode:offset
413a5f4374aSIngo Molnar  * Acquires uprobes_treelock
414a5f4374aSIngo Molnar  */
415a5f4374aSIngo Molnar static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
416a5f4374aSIngo Molnar {
417a5f4374aSIngo Molnar 	struct uprobe *uprobe;
418a5f4374aSIngo Molnar 	unsigned long flags;
419a5f4374aSIngo Molnar 
420a5f4374aSIngo Molnar 	spin_lock_irqsave(&uprobes_treelock, flags);
421a5f4374aSIngo Molnar 	uprobe = __find_uprobe(inode, offset);
422a5f4374aSIngo Molnar 	spin_unlock_irqrestore(&uprobes_treelock, flags);
423a5f4374aSIngo Molnar 
424a5f4374aSIngo Molnar 	return uprobe;
425a5f4374aSIngo Molnar }
426a5f4374aSIngo Molnar 
427a5f4374aSIngo Molnar static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
428a5f4374aSIngo Molnar {
429a5f4374aSIngo Molnar 	struct rb_node **p = &uprobes_tree.rb_node;
430a5f4374aSIngo Molnar 	struct rb_node *parent = NULL;
431a5f4374aSIngo Molnar 	struct uprobe *u;
432a5f4374aSIngo Molnar 	int match;
433a5f4374aSIngo Molnar 
434a5f4374aSIngo Molnar 	while (*p) {
435a5f4374aSIngo Molnar 		parent = *p;
436a5f4374aSIngo Molnar 		u = rb_entry(parent, struct uprobe, rb_node);
437a5f4374aSIngo Molnar 		match = match_uprobe(uprobe, u);
438a5f4374aSIngo Molnar 		if (!match) {
439a5f4374aSIngo Molnar 			atomic_inc(&u->ref);
440a5f4374aSIngo Molnar 			return u;
441a5f4374aSIngo Molnar 		}
442a5f4374aSIngo Molnar 
443a5f4374aSIngo Molnar 		if (match < 0)
444a5f4374aSIngo Molnar 			p = &parent->rb_left;
445a5f4374aSIngo Molnar 		else
446a5f4374aSIngo Molnar 			p = &parent->rb_right;
447a5f4374aSIngo Molnar 
448a5f4374aSIngo Molnar 	}
449a5f4374aSIngo Molnar 
450a5f4374aSIngo Molnar 	u = NULL;
451a5f4374aSIngo Molnar 	rb_link_node(&uprobe->rb_node, parent, p);
452a5f4374aSIngo Molnar 	rb_insert_color(&uprobe->rb_node, &uprobes_tree);
453a5f4374aSIngo Molnar 	/* get access + creation ref */
454a5f4374aSIngo Molnar 	atomic_set(&uprobe->ref, 2);
455a5f4374aSIngo Molnar 
456a5f4374aSIngo Molnar 	return u;
457a5f4374aSIngo Molnar }
458a5f4374aSIngo Molnar 
459a5f4374aSIngo Molnar /*
460a5f4374aSIngo Molnar  * Acquire uprobes_treelock.
461a5f4374aSIngo Molnar  * Matching uprobe already exists in rbtree;
462a5f4374aSIngo Molnar  *	increment (access refcount) and return the matching uprobe.
463a5f4374aSIngo Molnar  *
464a5f4374aSIngo Molnar  * No matching uprobe; insert the uprobe in rb_tree;
465a5f4374aSIngo Molnar  *	get a double refcount (access + creation) and return NULL.
466a5f4374aSIngo Molnar  */
467a5f4374aSIngo Molnar static struct uprobe *insert_uprobe(struct uprobe *uprobe)
468a5f4374aSIngo Molnar {
469a5f4374aSIngo Molnar 	unsigned long flags;
470a5f4374aSIngo Molnar 	struct uprobe *u;
471a5f4374aSIngo Molnar 
472a5f4374aSIngo Molnar 	spin_lock_irqsave(&uprobes_treelock, flags);
473a5f4374aSIngo Molnar 	u = __insert_uprobe(uprobe);
474a5f4374aSIngo Molnar 	spin_unlock_irqrestore(&uprobes_treelock, flags);
475a5f4374aSIngo Molnar 
4760326f5a9SSrikar Dronamraju 	/* For now assume that the instruction need not be single-stepped */
4770326f5a9SSrikar Dronamraju 	uprobe->flags |= UPROBE_SKIP_SSTEP;
4780326f5a9SSrikar Dronamraju 
479a5f4374aSIngo Molnar 	return u;
480a5f4374aSIngo Molnar }
481a5f4374aSIngo Molnar 
482a5f4374aSIngo Molnar static void put_uprobe(struct uprobe *uprobe)
483a5f4374aSIngo Molnar {
484a5f4374aSIngo Molnar 	if (atomic_dec_and_test(&uprobe->ref))
485a5f4374aSIngo Molnar 		kfree(uprobe);
486a5f4374aSIngo Molnar }
487a5f4374aSIngo Molnar 
488a5f4374aSIngo Molnar static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
489a5f4374aSIngo Molnar {
490a5f4374aSIngo Molnar 	struct uprobe *uprobe, *cur_uprobe;
491a5f4374aSIngo Molnar 
492a5f4374aSIngo Molnar 	uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
493a5f4374aSIngo Molnar 	if (!uprobe)
494a5f4374aSIngo Molnar 		return NULL;
495a5f4374aSIngo Molnar 
496a5f4374aSIngo Molnar 	uprobe->inode = igrab(inode);
497a5f4374aSIngo Molnar 	uprobe->offset = offset;
498a5f4374aSIngo Molnar 	init_rwsem(&uprobe->consumer_rwsem);
499a5f4374aSIngo Molnar 
500a5f4374aSIngo Molnar 	/* add to uprobes_tree, sorted on inode:offset */
501a5f4374aSIngo Molnar 	cur_uprobe = insert_uprobe(uprobe);
502a5f4374aSIngo Molnar 
503a5f4374aSIngo Molnar 	/* a uprobe exists for this inode:offset combination */
504a5f4374aSIngo Molnar 	if (cur_uprobe) {
505a5f4374aSIngo Molnar 		kfree(uprobe);
506a5f4374aSIngo Molnar 		uprobe = cur_uprobe;
507a5f4374aSIngo Molnar 		iput(inode);
508a5f4374aSIngo Molnar 	} else {
509a5f4374aSIngo Molnar 		atomic_inc(&uprobe_events);
510a5f4374aSIngo Molnar 	}
511a5f4374aSIngo Molnar 
512a5f4374aSIngo Molnar 	return uprobe;
513a5f4374aSIngo Molnar }
514a5f4374aSIngo Molnar 
5150326f5a9SSrikar Dronamraju static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
5160326f5a9SSrikar Dronamraju {
5170326f5a9SSrikar Dronamraju 	struct uprobe_consumer *uc;
5180326f5a9SSrikar Dronamraju 
5190326f5a9SSrikar Dronamraju 	if (!(uprobe->flags & UPROBE_RUN_HANDLER))
5200326f5a9SSrikar Dronamraju 		return;
5210326f5a9SSrikar Dronamraju 
5220326f5a9SSrikar Dronamraju 	down_read(&uprobe->consumer_rwsem);
5230326f5a9SSrikar Dronamraju 	for (uc = uprobe->consumers; uc; uc = uc->next) {
5240326f5a9SSrikar Dronamraju 		if (!uc->filter || uc->filter(uc, current))
5250326f5a9SSrikar Dronamraju 			uc->handler(uc, regs);
5260326f5a9SSrikar Dronamraju 	}
5270326f5a9SSrikar Dronamraju 	up_read(&uprobe->consumer_rwsem);
5280326f5a9SSrikar Dronamraju }
5290326f5a9SSrikar Dronamraju 
530a5f4374aSIngo Molnar /* Returns the previous consumer */
531a5f4374aSIngo Molnar static struct uprobe_consumer *
532e3343e6aSSrikar Dronamraju consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
533a5f4374aSIngo Molnar {
534a5f4374aSIngo Molnar 	down_write(&uprobe->consumer_rwsem);
535e3343e6aSSrikar Dronamraju 	uc->next = uprobe->consumers;
536e3343e6aSSrikar Dronamraju 	uprobe->consumers = uc;
537a5f4374aSIngo Molnar 	up_write(&uprobe->consumer_rwsem);
538a5f4374aSIngo Molnar 
539e3343e6aSSrikar Dronamraju 	return uc->next;
540a5f4374aSIngo Molnar }
541a5f4374aSIngo Molnar 
542a5f4374aSIngo Molnar /*
543e3343e6aSSrikar Dronamraju  * For uprobe @uprobe, delete the consumer @uc.
544e3343e6aSSrikar Dronamraju  * Return true if the @uc is deleted successfully
545a5f4374aSIngo Molnar  * or return false.
546a5f4374aSIngo Molnar  */
547e3343e6aSSrikar Dronamraju static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
548a5f4374aSIngo Molnar {
549a5f4374aSIngo Molnar 	struct uprobe_consumer **con;
550a5f4374aSIngo Molnar 	bool ret = false;
551a5f4374aSIngo Molnar 
552a5f4374aSIngo Molnar 	down_write(&uprobe->consumer_rwsem);
553a5f4374aSIngo Molnar 	for (con = &uprobe->consumers; *con; con = &(*con)->next) {
554e3343e6aSSrikar Dronamraju 		if (*con == uc) {
555e3343e6aSSrikar Dronamraju 			*con = uc->next;
556a5f4374aSIngo Molnar 			ret = true;
557a5f4374aSIngo Molnar 			break;
558a5f4374aSIngo Molnar 		}
559a5f4374aSIngo Molnar 	}
560a5f4374aSIngo Molnar 	up_write(&uprobe->consumer_rwsem);
561a5f4374aSIngo Molnar 
562a5f4374aSIngo Molnar 	return ret;
563a5f4374aSIngo Molnar }
564a5f4374aSIngo Molnar 
565e3343e6aSSrikar Dronamraju static int
566d436615eSOleg Nesterov __copy_insn(struct address_space *mapping, struct file *filp, char *insn,
567593609a5SOleg Nesterov 			unsigned long nbytes, loff_t offset)
568a5f4374aSIngo Molnar {
569a5f4374aSIngo Molnar 	struct page *page;
570a5f4374aSIngo Molnar 	void *vaddr;
571593609a5SOleg Nesterov 	unsigned long off;
572593609a5SOleg Nesterov 	pgoff_t idx;
573a5f4374aSIngo Molnar 
574a5f4374aSIngo Molnar 	if (!filp)
575a5f4374aSIngo Molnar 		return -EINVAL;
576a5f4374aSIngo Molnar 
577cc359d18SOleg Nesterov 	if (!mapping->a_ops->readpage)
578cc359d18SOleg Nesterov 		return -EIO;
579cc359d18SOleg Nesterov 
580593609a5SOleg Nesterov 	idx = offset >> PAGE_CACHE_SHIFT;
581593609a5SOleg Nesterov 	off = offset & ~PAGE_MASK;
582a5f4374aSIngo Molnar 
583a5f4374aSIngo Molnar 	/*
584a5f4374aSIngo Molnar 	 * Ensure that the page that has the original instruction is
585a5f4374aSIngo Molnar 	 * populated and in page-cache.
586a5f4374aSIngo Molnar 	 */
587a5f4374aSIngo Molnar 	page = read_mapping_page(mapping, idx, filp);
588a5f4374aSIngo Molnar 	if (IS_ERR(page))
589a5f4374aSIngo Molnar 		return PTR_ERR(page);
590a5f4374aSIngo Molnar 
591a5f4374aSIngo Molnar 	vaddr = kmap_atomic(page);
592593609a5SOleg Nesterov 	memcpy(insn, vaddr + off, nbytes);
593a5f4374aSIngo Molnar 	kunmap_atomic(vaddr);
594a5f4374aSIngo Molnar 	page_cache_release(page);
595a5f4374aSIngo Molnar 
596a5f4374aSIngo Molnar 	return 0;
597a5f4374aSIngo Molnar }
598a5f4374aSIngo Molnar 
599d436615eSOleg Nesterov static int copy_insn(struct uprobe *uprobe, struct file *filp)
600a5f4374aSIngo Molnar {
601a5f4374aSIngo Molnar 	struct address_space *mapping;
602a5f4374aSIngo Molnar 	unsigned long nbytes;
603a5f4374aSIngo Molnar 	int bytes;
604a5f4374aSIngo Molnar 
605d436615eSOleg Nesterov 	nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
606a5f4374aSIngo Molnar 	mapping = uprobe->inode->i_mapping;
607a5f4374aSIngo Molnar 
608a5f4374aSIngo Molnar 	/* Instruction at end of binary; copy only available bytes */
609a5f4374aSIngo Molnar 	if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
610a5f4374aSIngo Molnar 		bytes = uprobe->inode->i_size - uprobe->offset;
611a5f4374aSIngo Molnar 	else
612a5f4374aSIngo Molnar 		bytes = MAX_UINSN_BYTES;
613a5f4374aSIngo Molnar 
614a5f4374aSIngo Molnar 	/* Instruction at the page-boundary; copy bytes in second page */
615a5f4374aSIngo Molnar 	if (nbytes < bytes) {
616fc36f595SOleg Nesterov 		int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
617fc36f595SOleg Nesterov 				bytes - nbytes, uprobe->offset + nbytes);
618fc36f595SOleg Nesterov 		if (err)
619fc36f595SOleg Nesterov 			return err;
620a5f4374aSIngo Molnar 		bytes = nbytes;
621a5f4374aSIngo Molnar 	}
622d436615eSOleg Nesterov 	return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
623a5f4374aSIngo Molnar }
624a5f4374aSIngo Molnar 
625682968e0SSrikar Dronamraju /*
626682968e0SSrikar Dronamraju  * How mm->uprobes_state.count gets updated
627682968e0SSrikar Dronamraju  * uprobe_mmap() increments the count if
628682968e0SSrikar Dronamraju  * 	- it successfully adds a breakpoint.
629682968e0SSrikar Dronamraju  * 	- it cannot add a breakpoint, but sees that there is a underlying
630682968e0SSrikar Dronamraju  * 	  breakpoint (via a is_swbp_at_addr()).
631682968e0SSrikar Dronamraju  *
632682968e0SSrikar Dronamraju  * uprobe_munmap() decrements the count if
633682968e0SSrikar Dronamraju  * 	- it sees a underlying breakpoint, (via is_swbp_at_addr)
634682968e0SSrikar Dronamraju  * 	  (Subsequent uprobe_unregister wouldnt find the breakpoint
635682968e0SSrikar Dronamraju  * 	  unless a uprobe_mmap kicks in, since the old vma would be
636682968e0SSrikar Dronamraju  * 	  dropped just after uprobe_munmap.)
637682968e0SSrikar Dronamraju  *
638682968e0SSrikar Dronamraju  * uprobe_register increments the count if:
639682968e0SSrikar Dronamraju  * 	- it successfully adds a breakpoint.
640682968e0SSrikar Dronamraju  *
641682968e0SSrikar Dronamraju  * uprobe_unregister decrements the count if:
642682968e0SSrikar Dronamraju  * 	- it sees a underlying breakpoint and removes successfully.
643682968e0SSrikar Dronamraju  * 	  (via is_swbp_at_addr)
644682968e0SSrikar Dronamraju  * 	  (Subsequent uprobe_munmap wouldnt find the breakpoint
645682968e0SSrikar Dronamraju  * 	  since there is no underlying breakpoint after the
646682968e0SSrikar Dronamraju  * 	  breakpoint removal.)
647682968e0SSrikar Dronamraju  */
648e3343e6aSSrikar Dronamraju static int
649e3343e6aSSrikar Dronamraju install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
650816c03fbSOleg Nesterov 			struct vm_area_struct *vma, unsigned long vaddr)
651a5f4374aSIngo Molnar {
652a5f4374aSIngo Molnar 	int ret;
653a5f4374aSIngo Molnar 
654a5f4374aSIngo Molnar 	/*
655a5f4374aSIngo Molnar 	 * If probe is being deleted, unregister thread could be done with
656a5f4374aSIngo Molnar 	 * the vma-rmap-walk through. Adding a probe now can be fatal since
657a5f4374aSIngo Molnar 	 * nobody will be able to cleanup. Also we could be from fork or
658a5f4374aSIngo Molnar 	 * mremap path, where the probe might have already been inserted.
659a5f4374aSIngo Molnar 	 * Hence behave as if probe already existed.
660a5f4374aSIngo Molnar 	 */
661a5f4374aSIngo Molnar 	if (!uprobe->consumers)
662a5f4374aSIngo Molnar 		return -EEXIST;
663a5f4374aSIngo Molnar 
664900771a4SSrikar Dronamraju 	if (!(uprobe->flags & UPROBE_COPY_INSN)) {
665d436615eSOleg Nesterov 		ret = copy_insn(uprobe, vma->vm_file);
666a5f4374aSIngo Molnar 		if (ret)
667a5f4374aSIngo Molnar 			return ret;
668a5f4374aSIngo Molnar 
6695cb4ac3aSSrikar Dronamraju 		if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
670c1914a09SOleg Nesterov 			return -ENOTSUPP;
671a5f4374aSIngo Molnar 
672816c03fbSOleg Nesterov 		ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
673a5f4374aSIngo Molnar 		if (ret)
674a5f4374aSIngo Molnar 			return ret;
675a5f4374aSIngo Molnar 
676d9c4a30eSOleg Nesterov 		/* write_opcode() assumes we don't cross page boundary */
677d9c4a30eSOleg Nesterov 		BUG_ON((uprobe->offset & ~PAGE_MASK) +
678d9c4a30eSOleg Nesterov 				UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
679d9c4a30eSOleg Nesterov 
680900771a4SSrikar Dronamraju 		uprobe->flags |= UPROBE_COPY_INSN;
681a5f4374aSIngo Molnar 	}
682682968e0SSrikar Dronamraju 
683682968e0SSrikar Dronamraju 	/*
684682968e0SSrikar Dronamraju 	 * Ideally, should be updating the probe count after the breakpoint
685682968e0SSrikar Dronamraju 	 * has been successfully inserted. However a thread could hit the
686682968e0SSrikar Dronamraju 	 * breakpoint we just inserted even before the probe count is
687682968e0SSrikar Dronamraju 	 * incremented. If this is the first breakpoint placed, breakpoint
688682968e0SSrikar Dronamraju 	 * notifier might ignore uprobes and pass the trap to the thread.
689682968e0SSrikar Dronamraju 	 * Hence increment before and decrement on failure.
690682968e0SSrikar Dronamraju 	 */
691682968e0SSrikar Dronamraju 	atomic_inc(&mm->uprobes_state.count);
692816c03fbSOleg Nesterov 	ret = set_swbp(&uprobe->arch, mm, vaddr);
693682968e0SSrikar Dronamraju 	if (ret)
694682968e0SSrikar Dronamraju 		atomic_dec(&mm->uprobes_state.count);
695a5f4374aSIngo Molnar 
696a5f4374aSIngo Molnar 	return ret;
697a5f4374aSIngo Molnar }
698a5f4374aSIngo Molnar 
699e3343e6aSSrikar Dronamraju static void
700816c03fbSOleg Nesterov remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
701a5f4374aSIngo Molnar {
702816c03fbSOleg Nesterov 	if (!set_orig_insn(&uprobe->arch, mm, vaddr, true))
703682968e0SSrikar Dronamraju 		atomic_dec(&mm->uprobes_state.count);
704a5f4374aSIngo Molnar }
705a5f4374aSIngo Molnar 
7060326f5a9SSrikar Dronamraju /*
707778b032dSOleg Nesterov  * There could be threads that have already hit the breakpoint. They
708778b032dSOleg Nesterov  * will recheck the current insn and restart if find_uprobe() fails.
709778b032dSOleg Nesterov  * See find_active_uprobe().
7100326f5a9SSrikar Dronamraju  */
711a5f4374aSIngo Molnar static void delete_uprobe(struct uprobe *uprobe)
712a5f4374aSIngo Molnar {
713a5f4374aSIngo Molnar 	unsigned long flags;
714a5f4374aSIngo Molnar 
715a5f4374aSIngo Molnar 	spin_lock_irqsave(&uprobes_treelock, flags);
716a5f4374aSIngo Molnar 	rb_erase(&uprobe->rb_node, &uprobes_tree);
717a5f4374aSIngo Molnar 	spin_unlock_irqrestore(&uprobes_treelock, flags);
718a5f4374aSIngo Molnar 	iput(uprobe->inode);
719a5f4374aSIngo Molnar 	put_uprobe(uprobe);
720a5f4374aSIngo Molnar 	atomic_dec(&uprobe_events);
721a5f4374aSIngo Molnar }
722a5f4374aSIngo Molnar 
72326872090SOleg Nesterov struct map_info {
72426872090SOleg Nesterov 	struct map_info *next;
72526872090SOleg Nesterov 	struct mm_struct *mm;
726816c03fbSOleg Nesterov 	unsigned long vaddr;
72726872090SOleg Nesterov };
72826872090SOleg Nesterov 
72926872090SOleg Nesterov static inline struct map_info *free_map_info(struct map_info *info)
730a5f4374aSIngo Molnar {
73126872090SOleg Nesterov 	struct map_info *next = info->next;
73226872090SOleg Nesterov 	kfree(info);
73326872090SOleg Nesterov 	return next;
73426872090SOleg Nesterov }
73526872090SOleg Nesterov 
73626872090SOleg Nesterov static struct map_info *
73726872090SOleg Nesterov build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
73826872090SOleg Nesterov {
73926872090SOleg Nesterov 	unsigned long pgoff = offset >> PAGE_SHIFT;
740a5f4374aSIngo Molnar 	struct prio_tree_iter iter;
741a5f4374aSIngo Molnar 	struct vm_area_struct *vma;
74226872090SOleg Nesterov 	struct map_info *curr = NULL;
74326872090SOleg Nesterov 	struct map_info *prev = NULL;
74426872090SOleg Nesterov 	struct map_info *info;
74526872090SOleg Nesterov 	int more = 0;
746a5f4374aSIngo Molnar 
74726872090SOleg Nesterov  again:
74826872090SOleg Nesterov 	mutex_lock(&mapping->i_mmap_mutex);
749a5f4374aSIngo Molnar 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
750a5f4374aSIngo Molnar 		if (!valid_vma(vma, is_register))
751a5f4374aSIngo Molnar 			continue;
752a5f4374aSIngo Molnar 
7537a5bfb66SOleg Nesterov 		if (!prev && !more) {
7547a5bfb66SOleg Nesterov 			/*
7557a5bfb66SOleg Nesterov 			 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
7567a5bfb66SOleg Nesterov 			 * reclaim. This is optimistic, no harm done if it fails.
7577a5bfb66SOleg Nesterov 			 */
7587a5bfb66SOleg Nesterov 			prev = kmalloc(sizeof(struct map_info),
7597a5bfb66SOleg Nesterov 					GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
7607a5bfb66SOleg Nesterov 			if (prev)
7617a5bfb66SOleg Nesterov 				prev->next = NULL;
7627a5bfb66SOleg Nesterov 		}
76326872090SOleg Nesterov 		if (!prev) {
76426872090SOleg Nesterov 			more++;
76526872090SOleg Nesterov 			continue;
766a5f4374aSIngo Molnar 		}
767a5f4374aSIngo Molnar 
76826872090SOleg Nesterov 		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
76926872090SOleg Nesterov 			continue;
770a5f4374aSIngo Molnar 
77126872090SOleg Nesterov 		info = prev;
77226872090SOleg Nesterov 		prev = prev->next;
77326872090SOleg Nesterov 		info->next = curr;
77426872090SOleg Nesterov 		curr = info;
77526872090SOleg Nesterov 
77626872090SOleg Nesterov 		info->mm = vma->vm_mm;
77757683f72SOleg Nesterov 		info->vaddr = offset_to_vaddr(vma, offset);
778a5f4374aSIngo Molnar 	}
779a5f4374aSIngo Molnar 	mutex_unlock(&mapping->i_mmap_mutex);
780a5f4374aSIngo Molnar 
78126872090SOleg Nesterov 	if (!more)
78226872090SOleg Nesterov 		goto out;
783a5f4374aSIngo Molnar 
78426872090SOleg Nesterov 	prev = curr;
78526872090SOleg Nesterov 	while (curr) {
78626872090SOleg Nesterov 		mmput(curr->mm);
78726872090SOleg Nesterov 		curr = curr->next;
78826872090SOleg Nesterov 	}
78926872090SOleg Nesterov 
79026872090SOleg Nesterov 	do {
79126872090SOleg Nesterov 		info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
79226872090SOleg Nesterov 		if (!info) {
79326872090SOleg Nesterov 			curr = ERR_PTR(-ENOMEM);
79426872090SOleg Nesterov 			goto out;
79526872090SOleg Nesterov 		}
79626872090SOleg Nesterov 		info->next = prev;
79726872090SOleg Nesterov 		prev = info;
79826872090SOleg Nesterov 	} while (--more);
79926872090SOleg Nesterov 
80026872090SOleg Nesterov 	goto again;
80126872090SOleg Nesterov  out:
80226872090SOleg Nesterov 	while (prev)
80326872090SOleg Nesterov 		prev = free_map_info(prev);
80426872090SOleg Nesterov 	return curr;
805a5f4374aSIngo Molnar }
806a5f4374aSIngo Molnar 
807a5f4374aSIngo Molnar static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
808a5f4374aSIngo Molnar {
80926872090SOleg Nesterov 	struct map_info *info;
81026872090SOleg Nesterov 	int err = 0;
81126872090SOleg Nesterov 
81226872090SOleg Nesterov 	info = build_map_info(uprobe->inode->i_mapping,
81326872090SOleg Nesterov 					uprobe->offset, is_register);
81426872090SOleg Nesterov 	if (IS_ERR(info))
81526872090SOleg Nesterov 		return PTR_ERR(info);
81626872090SOleg Nesterov 
81726872090SOleg Nesterov 	while (info) {
81826872090SOleg Nesterov 		struct mm_struct *mm = info->mm;
819a5f4374aSIngo Molnar 		struct vm_area_struct *vma;
820a5f4374aSIngo Molnar 
82126872090SOleg Nesterov 		if (err)
82226872090SOleg Nesterov 			goto free;
823a5f4374aSIngo Molnar 
82477fc4af1SOleg Nesterov 		down_write(&mm->mmap_sem);
825f4d6dfe5SOleg Nesterov 		vma = find_vma(mm, info->vaddr);
826f4d6dfe5SOleg Nesterov 		if (!vma || !valid_vma(vma, is_register) ||
827f4d6dfe5SOleg Nesterov 		    vma->vm_file->f_mapping->host != uprobe->inode)
82826872090SOleg Nesterov 			goto unlock;
82926872090SOleg Nesterov 
830f4d6dfe5SOleg Nesterov 		if (vma->vm_start > info->vaddr ||
831f4d6dfe5SOleg Nesterov 		    vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
83226872090SOleg Nesterov 			goto unlock;
833a5f4374aSIngo Molnar 
834a5f4374aSIngo Molnar 		if (is_register) {
83526872090SOleg Nesterov 			err = install_breakpoint(uprobe, mm, vma, info->vaddr);
836c5784de2SPeter Zijlstra 			/*
837c5784de2SPeter Zijlstra 			 * We can race against uprobe_mmap(), see the
838c5784de2SPeter Zijlstra 			 * comment near uprobe_hash().
839c5784de2SPeter Zijlstra 			 */
84026872090SOleg Nesterov 			if (err == -EEXIST)
84126872090SOleg Nesterov 				err = 0;
84226872090SOleg Nesterov 		} else {
84326872090SOleg Nesterov 			remove_breakpoint(uprobe, mm, info->vaddr);
844a5f4374aSIngo Molnar 		}
84526872090SOleg Nesterov  unlock:
84626872090SOleg Nesterov 		up_write(&mm->mmap_sem);
84726872090SOleg Nesterov  free:
84826872090SOleg Nesterov 		mmput(mm);
84926872090SOleg Nesterov 		info = free_map_info(info);
850a5f4374aSIngo Molnar 	}
851a5f4374aSIngo Molnar 
85226872090SOleg Nesterov 	return err;
853a5f4374aSIngo Molnar }
854a5f4374aSIngo Molnar 
855a5f4374aSIngo Molnar static int __uprobe_register(struct uprobe *uprobe)
856a5f4374aSIngo Molnar {
857a5f4374aSIngo Molnar 	return register_for_each_vma(uprobe, true);
858a5f4374aSIngo Molnar }
859a5f4374aSIngo Molnar 
860a5f4374aSIngo Molnar static void __uprobe_unregister(struct uprobe *uprobe)
861a5f4374aSIngo Molnar {
862a5f4374aSIngo Molnar 	if (!register_for_each_vma(uprobe, false))
863a5f4374aSIngo Molnar 		delete_uprobe(uprobe);
864a5f4374aSIngo Molnar 
865a5f4374aSIngo Molnar 	/* TODO : cant unregister? schedule a worker thread */
866a5f4374aSIngo Molnar }
867a5f4374aSIngo Molnar 
868a5f4374aSIngo Molnar /*
869a5f4374aSIngo Molnar  * uprobe_register - register a probe
870a5f4374aSIngo Molnar  * @inode: the file in which the probe has to be placed.
871a5f4374aSIngo Molnar  * @offset: offset from the start of the file.
872e3343e6aSSrikar Dronamraju  * @uc: information on howto handle the probe..
873a5f4374aSIngo Molnar  *
874a5f4374aSIngo Molnar  * Apart from the access refcount, uprobe_register() takes a creation
875a5f4374aSIngo Molnar  * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
876a5f4374aSIngo Molnar  * inserted into the rbtree (i.e first consumer for a @inode:@offset
877a5f4374aSIngo Molnar  * tuple).  Creation refcount stops uprobe_unregister from freeing the
878a5f4374aSIngo Molnar  * @uprobe even before the register operation is complete. Creation
879e3343e6aSSrikar Dronamraju  * refcount is released when the last @uc for the @uprobe
880a5f4374aSIngo Molnar  * unregisters.
881a5f4374aSIngo Molnar  *
882a5f4374aSIngo Molnar  * Return errno if it cannot successully install probes
883a5f4374aSIngo Molnar  * else return 0 (success)
884a5f4374aSIngo Molnar  */
885e3343e6aSSrikar Dronamraju int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
886a5f4374aSIngo Molnar {
887a5f4374aSIngo Molnar 	struct uprobe *uprobe;
888a5f4374aSIngo Molnar 	int ret;
889a5f4374aSIngo Molnar 
890e3343e6aSSrikar Dronamraju 	if (!inode || !uc || uc->next)
891a5f4374aSIngo Molnar 		return -EINVAL;
892a5f4374aSIngo Molnar 
893a5f4374aSIngo Molnar 	if (offset > i_size_read(inode))
894a5f4374aSIngo Molnar 		return -EINVAL;
895a5f4374aSIngo Molnar 
896a5f4374aSIngo Molnar 	ret = 0;
897a5f4374aSIngo Molnar 	mutex_lock(uprobes_hash(inode));
898a5f4374aSIngo Molnar 	uprobe = alloc_uprobe(inode, offset);
899a5f4374aSIngo Molnar 
900e3343e6aSSrikar Dronamraju 	if (uprobe && !consumer_add(uprobe, uc)) {
901a5f4374aSIngo Molnar 		ret = __uprobe_register(uprobe);
902a5f4374aSIngo Molnar 		if (ret) {
903a5f4374aSIngo Molnar 			uprobe->consumers = NULL;
904a5f4374aSIngo Molnar 			__uprobe_unregister(uprobe);
905a5f4374aSIngo Molnar 		} else {
906900771a4SSrikar Dronamraju 			uprobe->flags |= UPROBE_RUN_HANDLER;
907a5f4374aSIngo Molnar 		}
908a5f4374aSIngo Molnar 	}
909a5f4374aSIngo Molnar 
910a5f4374aSIngo Molnar 	mutex_unlock(uprobes_hash(inode));
911a5f4374aSIngo Molnar 	put_uprobe(uprobe);
912a5f4374aSIngo Molnar 
913a5f4374aSIngo Molnar 	return ret;
914a5f4374aSIngo Molnar }
915a5f4374aSIngo Molnar 
916a5f4374aSIngo Molnar /*
917a5f4374aSIngo Molnar  * uprobe_unregister - unregister a already registered probe.
918a5f4374aSIngo Molnar  * @inode: the file in which the probe has to be removed.
919a5f4374aSIngo Molnar  * @offset: offset from the start of the file.
920e3343e6aSSrikar Dronamraju  * @uc: identify which probe if multiple probes are colocated.
921a5f4374aSIngo Molnar  */
922e3343e6aSSrikar Dronamraju void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
923a5f4374aSIngo Molnar {
924a5f4374aSIngo Molnar 	struct uprobe *uprobe;
925a5f4374aSIngo Molnar 
926e3343e6aSSrikar Dronamraju 	if (!inode || !uc)
927a5f4374aSIngo Molnar 		return;
928a5f4374aSIngo Molnar 
929a5f4374aSIngo Molnar 	uprobe = find_uprobe(inode, offset);
930a5f4374aSIngo Molnar 	if (!uprobe)
931a5f4374aSIngo Molnar 		return;
932a5f4374aSIngo Molnar 
933a5f4374aSIngo Molnar 	mutex_lock(uprobes_hash(inode));
934a5f4374aSIngo Molnar 
935e3343e6aSSrikar Dronamraju 	if (consumer_del(uprobe, uc)) {
936a5f4374aSIngo Molnar 		if (!uprobe->consumers) {
937a5f4374aSIngo Molnar 			__uprobe_unregister(uprobe);
938900771a4SSrikar Dronamraju 			uprobe->flags &= ~UPROBE_RUN_HANDLER;
939a5f4374aSIngo Molnar 		}
940a5f4374aSIngo Molnar 	}
941a5f4374aSIngo Molnar 
942a5f4374aSIngo Molnar 	mutex_unlock(uprobes_hash(inode));
943a5f4374aSIngo Molnar 	if (uprobe)
944a5f4374aSIngo Molnar 		put_uprobe(uprobe);
945a5f4374aSIngo Molnar }
946a5f4374aSIngo Molnar 
947891c3970SOleg Nesterov static struct rb_node *
948891c3970SOleg Nesterov find_node_in_range(struct inode *inode, loff_t min, loff_t max)
949a5f4374aSIngo Molnar {
950a5f4374aSIngo Molnar 	struct rb_node *n = uprobes_tree.rb_node;
951a5f4374aSIngo Molnar 
952a5f4374aSIngo Molnar 	while (n) {
953891c3970SOleg Nesterov 		struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
954a5f4374aSIngo Molnar 
955891c3970SOleg Nesterov 		if (inode < u->inode) {
956a5f4374aSIngo Molnar 			n = n->rb_left;
957891c3970SOleg Nesterov 		} else if (inode > u->inode) {
958a5f4374aSIngo Molnar 			n = n->rb_right;
959891c3970SOleg Nesterov 		} else {
960891c3970SOleg Nesterov 			if (max < u->offset)
961891c3970SOleg Nesterov 				n = n->rb_left;
962891c3970SOleg Nesterov 			else if (min > u->offset)
963891c3970SOleg Nesterov 				n = n->rb_right;
964891c3970SOleg Nesterov 			else
965891c3970SOleg Nesterov 				break;
966891c3970SOleg Nesterov 		}
967a5f4374aSIngo Molnar 	}
968a5f4374aSIngo Molnar 
969891c3970SOleg Nesterov 	return n;
970a5f4374aSIngo Molnar }
971a5f4374aSIngo Molnar 
972a5f4374aSIngo Molnar /*
973891c3970SOleg Nesterov  * For a given range in vma, build a list of probes that need to be inserted.
974a5f4374aSIngo Molnar  */
975891c3970SOleg Nesterov static void build_probe_list(struct inode *inode,
976891c3970SOleg Nesterov 				struct vm_area_struct *vma,
977891c3970SOleg Nesterov 				unsigned long start, unsigned long end,
978891c3970SOleg Nesterov 				struct list_head *head)
979a5f4374aSIngo Molnar {
980891c3970SOleg Nesterov 	loff_t min, max;
981a5f4374aSIngo Molnar 	unsigned long flags;
982891c3970SOleg Nesterov 	struct rb_node *n, *t;
983891c3970SOleg Nesterov 	struct uprobe *u;
984891c3970SOleg Nesterov 
985891c3970SOleg Nesterov 	INIT_LIST_HEAD(head);
986cb113b47SOleg Nesterov 	min = vaddr_to_offset(vma, start);
987891c3970SOleg Nesterov 	max = min + (end - start) - 1;
988a5f4374aSIngo Molnar 
989a5f4374aSIngo Molnar 	spin_lock_irqsave(&uprobes_treelock, flags);
990891c3970SOleg Nesterov 	n = find_node_in_range(inode, min, max);
991891c3970SOleg Nesterov 	if (n) {
992891c3970SOleg Nesterov 		for (t = n; t; t = rb_prev(t)) {
993891c3970SOleg Nesterov 			u = rb_entry(t, struct uprobe, rb_node);
994891c3970SOleg Nesterov 			if (u->inode != inode || u->offset < min)
995a5f4374aSIngo Molnar 				break;
996891c3970SOleg Nesterov 			list_add(&u->pending_list, head);
997891c3970SOleg Nesterov 			atomic_inc(&u->ref);
998a5f4374aSIngo Molnar 		}
999891c3970SOleg Nesterov 		for (t = n; (t = rb_next(t)); ) {
1000891c3970SOleg Nesterov 			u = rb_entry(t, struct uprobe, rb_node);
1001891c3970SOleg Nesterov 			if (u->inode != inode || u->offset > max)
1002891c3970SOleg Nesterov 				break;
1003891c3970SOleg Nesterov 			list_add(&u->pending_list, head);
1004891c3970SOleg Nesterov 			atomic_inc(&u->ref);
1005891c3970SOleg Nesterov 		}
1006891c3970SOleg Nesterov 	}
1007a5f4374aSIngo Molnar 	spin_unlock_irqrestore(&uprobes_treelock, flags);
1008a5f4374aSIngo Molnar }
1009a5f4374aSIngo Molnar 
1010a5f4374aSIngo Molnar /*
1011a5f4374aSIngo Molnar  * Called from mmap_region.
1012a5f4374aSIngo Molnar  * called with mm->mmap_sem acquired.
1013a5f4374aSIngo Molnar  *
1014a5f4374aSIngo Molnar  * Return -ve no if we fail to insert probes and we cannot
1015a5f4374aSIngo Molnar  * bail-out.
1016a5f4374aSIngo Molnar  * Return 0 otherwise. i.e:
1017a5f4374aSIngo Molnar  *
1018a5f4374aSIngo Molnar  *	- successful insertion of probes
1019a5f4374aSIngo Molnar  *	- (or) no possible probes to be inserted.
1020a5f4374aSIngo Molnar  *	- (or) insertion of probes failed but we can bail-out.
1021a5f4374aSIngo Molnar  */
1022a5f4374aSIngo Molnar int uprobe_mmap(struct vm_area_struct *vma)
1023a5f4374aSIngo Molnar {
1024a5f4374aSIngo Molnar 	struct list_head tmp_list;
1025665605a2SOleg Nesterov 	struct uprobe *uprobe, *u;
1026a5f4374aSIngo Molnar 	struct inode *inode;
1027682968e0SSrikar Dronamraju 	int ret, count;
1028a5f4374aSIngo Molnar 
1029a5f4374aSIngo Molnar 	if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
1030a5f4374aSIngo Molnar 		return 0;
1031a5f4374aSIngo Molnar 
1032a5f4374aSIngo Molnar 	inode = vma->vm_file->f_mapping->host;
1033a5f4374aSIngo Molnar 	if (!inode)
1034a5f4374aSIngo Molnar 		return 0;
1035a5f4374aSIngo Molnar 
1036a5f4374aSIngo Molnar 	mutex_lock(uprobes_mmap_hash(inode));
1037891c3970SOleg Nesterov 	build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
1038a5f4374aSIngo Molnar 
1039a5f4374aSIngo Molnar 	ret = 0;
1040682968e0SSrikar Dronamraju 	count = 0;
1041a5f4374aSIngo Molnar 
1042665605a2SOleg Nesterov 	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1043a5f4374aSIngo Molnar 		if (!ret) {
104457683f72SOleg Nesterov 			unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
1045682968e0SSrikar Dronamraju 
1046682968e0SSrikar Dronamraju 			ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
1047c5784de2SPeter Zijlstra 			/*
1048c5784de2SPeter Zijlstra 			 * We can race against uprobe_register(), see the
1049c5784de2SPeter Zijlstra 			 * comment near uprobe_hash().
1050c5784de2SPeter Zijlstra 			 */
1051682968e0SSrikar Dronamraju 			if (ret == -EEXIST) {
1052682968e0SSrikar Dronamraju 				ret = 0;
1053682968e0SSrikar Dronamraju 
1054682968e0SSrikar Dronamraju 				if (!is_swbp_at_addr(vma->vm_mm, vaddr))
1055682968e0SSrikar Dronamraju 					continue;
1056682968e0SSrikar Dronamraju 
1057682968e0SSrikar Dronamraju 				/*
1058682968e0SSrikar Dronamraju 				 * Unable to insert a breakpoint, but
1059682968e0SSrikar Dronamraju 				 * breakpoint lies underneath. Increment the
1060682968e0SSrikar Dronamraju 				 * probe count.
1061682968e0SSrikar Dronamraju 				 */
1062682968e0SSrikar Dronamraju 				atomic_inc(&vma->vm_mm->uprobes_state.count);
1063682968e0SSrikar Dronamraju 			}
1064682968e0SSrikar Dronamraju 
1065682968e0SSrikar Dronamraju 			if (!ret)
1066682968e0SSrikar Dronamraju 				count++;
1067a5f4374aSIngo Molnar 		}
1068a5f4374aSIngo Molnar 		put_uprobe(uprobe);
1069a5f4374aSIngo Molnar 	}
1070a5f4374aSIngo Molnar 
1071a5f4374aSIngo Molnar 	mutex_unlock(uprobes_mmap_hash(inode));
1072a5f4374aSIngo Molnar 
1073682968e0SSrikar Dronamraju 	if (ret)
1074682968e0SSrikar Dronamraju 		atomic_sub(count, &vma->vm_mm->uprobes_state.count);
1075682968e0SSrikar Dronamraju 
1076a5f4374aSIngo Molnar 	return ret;
1077a5f4374aSIngo Molnar }
1078a5f4374aSIngo Molnar 
1079682968e0SSrikar Dronamraju /*
1080682968e0SSrikar Dronamraju  * Called in context of a munmap of a vma.
1081682968e0SSrikar Dronamraju  */
1082cbc91f71SSrikar Dronamraju void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1083682968e0SSrikar Dronamraju {
1084682968e0SSrikar Dronamraju 	struct list_head tmp_list;
1085665605a2SOleg Nesterov 	struct uprobe *uprobe, *u;
1086682968e0SSrikar Dronamraju 	struct inode *inode;
1087682968e0SSrikar Dronamraju 
1088682968e0SSrikar Dronamraju 	if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1089682968e0SSrikar Dronamraju 		return;
1090682968e0SSrikar Dronamraju 
10912fd611a9SOleg Nesterov 	if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
10922fd611a9SOleg Nesterov 		return;
10932fd611a9SOleg Nesterov 
1094682968e0SSrikar Dronamraju 	if (!atomic_read(&vma->vm_mm->uprobes_state.count))
1095682968e0SSrikar Dronamraju 		return;
1096682968e0SSrikar Dronamraju 
1097682968e0SSrikar Dronamraju 	inode = vma->vm_file->f_mapping->host;
1098682968e0SSrikar Dronamraju 	if (!inode)
1099682968e0SSrikar Dronamraju 		return;
1100682968e0SSrikar Dronamraju 
1101682968e0SSrikar Dronamraju 	mutex_lock(uprobes_mmap_hash(inode));
1102891c3970SOleg Nesterov 	build_probe_list(inode, vma, start, end, &tmp_list);
1103682968e0SSrikar Dronamraju 
1104665605a2SOleg Nesterov 	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
110557683f72SOleg Nesterov 		unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
1106682968e0SSrikar Dronamraju 		/*
1107682968e0SSrikar Dronamraju 		 * An unregister could have removed the probe before
1108682968e0SSrikar Dronamraju 		 * unmap. So check before we decrement the count.
1109682968e0SSrikar Dronamraju 		 */
1110682968e0SSrikar Dronamraju 		if (is_swbp_at_addr(vma->vm_mm, vaddr) == 1)
1111682968e0SSrikar Dronamraju 			atomic_dec(&vma->vm_mm->uprobes_state.count);
1112682968e0SSrikar Dronamraju 		put_uprobe(uprobe);
1113682968e0SSrikar Dronamraju 	}
1114682968e0SSrikar Dronamraju 	mutex_unlock(uprobes_mmap_hash(inode));
1115682968e0SSrikar Dronamraju }
1116682968e0SSrikar Dronamraju 
1117d4b3b638SSrikar Dronamraju /* Slot allocation for XOL */
1118d4b3b638SSrikar Dronamraju static int xol_add_vma(struct xol_area *area)
1119d4b3b638SSrikar Dronamraju {
1120d4b3b638SSrikar Dronamraju 	struct mm_struct *mm;
1121d4b3b638SSrikar Dronamraju 	int ret;
1122d4b3b638SSrikar Dronamraju 
1123d4b3b638SSrikar Dronamraju 	area->page = alloc_page(GFP_HIGHUSER);
1124d4b3b638SSrikar Dronamraju 	if (!area->page)
1125d4b3b638SSrikar Dronamraju 		return -ENOMEM;
1126d4b3b638SSrikar Dronamraju 
1127d4b3b638SSrikar Dronamraju 	ret = -EALREADY;
1128d4b3b638SSrikar Dronamraju 	mm = current->mm;
1129d4b3b638SSrikar Dronamraju 
1130d4b3b638SSrikar Dronamraju 	down_write(&mm->mmap_sem);
1131d4b3b638SSrikar Dronamraju 	if (mm->uprobes_state.xol_area)
1132d4b3b638SSrikar Dronamraju 		goto fail;
1133d4b3b638SSrikar Dronamraju 
1134d4b3b638SSrikar Dronamraju 	ret = -ENOMEM;
1135d4b3b638SSrikar Dronamraju 
1136d4b3b638SSrikar Dronamraju 	/* Try to map as high as possible, this is only a hint. */
1137d4b3b638SSrikar Dronamraju 	area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1138d4b3b638SSrikar Dronamraju 	if (area->vaddr & ~PAGE_MASK) {
1139d4b3b638SSrikar Dronamraju 		ret = area->vaddr;
1140d4b3b638SSrikar Dronamraju 		goto fail;
1141d4b3b638SSrikar Dronamraju 	}
1142d4b3b638SSrikar Dronamraju 
1143d4b3b638SSrikar Dronamraju 	ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1144d4b3b638SSrikar Dronamraju 				VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1145d4b3b638SSrikar Dronamraju 	if (ret)
1146d4b3b638SSrikar Dronamraju 		goto fail;
1147d4b3b638SSrikar Dronamraju 
1148d4b3b638SSrikar Dronamraju 	smp_wmb();	/* pairs with get_xol_area() */
1149d4b3b638SSrikar Dronamraju 	mm->uprobes_state.xol_area = area;
1150d4b3b638SSrikar Dronamraju 	ret = 0;
1151d4b3b638SSrikar Dronamraju 
1152d4b3b638SSrikar Dronamraju fail:
1153d4b3b638SSrikar Dronamraju 	up_write(&mm->mmap_sem);
1154d4b3b638SSrikar Dronamraju 	if (ret)
1155d4b3b638SSrikar Dronamraju 		__free_page(area->page);
1156d4b3b638SSrikar Dronamraju 
1157d4b3b638SSrikar Dronamraju 	return ret;
1158d4b3b638SSrikar Dronamraju }
1159d4b3b638SSrikar Dronamraju 
1160d4b3b638SSrikar Dronamraju static struct xol_area *get_xol_area(struct mm_struct *mm)
1161d4b3b638SSrikar Dronamraju {
1162d4b3b638SSrikar Dronamraju 	struct xol_area *area;
1163d4b3b638SSrikar Dronamraju 
1164d4b3b638SSrikar Dronamraju 	area = mm->uprobes_state.xol_area;
1165d4b3b638SSrikar Dronamraju 	smp_read_barrier_depends();	/* pairs with wmb in xol_add_vma() */
1166d4b3b638SSrikar Dronamraju 
1167d4b3b638SSrikar Dronamraju 	return area;
1168d4b3b638SSrikar Dronamraju }
1169d4b3b638SSrikar Dronamraju 
1170d4b3b638SSrikar Dronamraju /*
1171d4b3b638SSrikar Dronamraju  * xol_alloc_area - Allocate process's xol_area.
1172d4b3b638SSrikar Dronamraju  * This area will be used for storing instructions for execution out of
1173d4b3b638SSrikar Dronamraju  * line.
1174d4b3b638SSrikar Dronamraju  *
1175d4b3b638SSrikar Dronamraju  * Returns the allocated area or NULL.
1176d4b3b638SSrikar Dronamraju  */
1177d4b3b638SSrikar Dronamraju static struct xol_area *xol_alloc_area(void)
1178d4b3b638SSrikar Dronamraju {
1179d4b3b638SSrikar Dronamraju 	struct xol_area *area;
1180d4b3b638SSrikar Dronamraju 
1181d4b3b638SSrikar Dronamraju 	area = kzalloc(sizeof(*area), GFP_KERNEL);
1182d4b3b638SSrikar Dronamraju 	if (unlikely(!area))
1183d4b3b638SSrikar Dronamraju 		return NULL;
1184d4b3b638SSrikar Dronamraju 
1185d4b3b638SSrikar Dronamraju 	area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1186d4b3b638SSrikar Dronamraju 
1187d4b3b638SSrikar Dronamraju 	if (!area->bitmap)
1188d4b3b638SSrikar Dronamraju 		goto fail;
1189d4b3b638SSrikar Dronamraju 
1190d4b3b638SSrikar Dronamraju 	init_waitqueue_head(&area->wq);
1191d4b3b638SSrikar Dronamraju 	if (!xol_add_vma(area))
1192d4b3b638SSrikar Dronamraju 		return area;
1193d4b3b638SSrikar Dronamraju 
1194d4b3b638SSrikar Dronamraju fail:
1195d4b3b638SSrikar Dronamraju 	kfree(area->bitmap);
1196d4b3b638SSrikar Dronamraju 	kfree(area);
1197d4b3b638SSrikar Dronamraju 
1198d4b3b638SSrikar Dronamraju 	return get_xol_area(current->mm);
1199d4b3b638SSrikar Dronamraju }
1200d4b3b638SSrikar Dronamraju 
1201d4b3b638SSrikar Dronamraju /*
1202d4b3b638SSrikar Dronamraju  * uprobe_clear_state - Free the area allocated for slots.
1203d4b3b638SSrikar Dronamraju  */
1204d4b3b638SSrikar Dronamraju void uprobe_clear_state(struct mm_struct *mm)
1205d4b3b638SSrikar Dronamraju {
1206d4b3b638SSrikar Dronamraju 	struct xol_area *area = mm->uprobes_state.xol_area;
1207d4b3b638SSrikar Dronamraju 
1208d4b3b638SSrikar Dronamraju 	if (!area)
1209d4b3b638SSrikar Dronamraju 		return;
1210d4b3b638SSrikar Dronamraju 
1211d4b3b638SSrikar Dronamraju 	put_page(area->page);
1212d4b3b638SSrikar Dronamraju 	kfree(area->bitmap);
1213d4b3b638SSrikar Dronamraju 	kfree(area);
1214d4b3b638SSrikar Dronamraju }
1215d4b3b638SSrikar Dronamraju 
1216d4b3b638SSrikar Dronamraju /*
1217d4b3b638SSrikar Dronamraju  * uprobe_reset_state - Free the area allocated for slots.
1218d4b3b638SSrikar Dronamraju  */
1219d4b3b638SSrikar Dronamraju void uprobe_reset_state(struct mm_struct *mm)
1220d4b3b638SSrikar Dronamraju {
1221d4b3b638SSrikar Dronamraju 	mm->uprobes_state.xol_area = NULL;
1222682968e0SSrikar Dronamraju 	atomic_set(&mm->uprobes_state.count, 0);
1223d4b3b638SSrikar Dronamraju }
1224d4b3b638SSrikar Dronamraju 
1225d4b3b638SSrikar Dronamraju /*
1226d4b3b638SSrikar Dronamraju  *  - search for a free slot.
1227d4b3b638SSrikar Dronamraju  */
1228d4b3b638SSrikar Dronamraju static unsigned long xol_take_insn_slot(struct xol_area *area)
1229d4b3b638SSrikar Dronamraju {
1230d4b3b638SSrikar Dronamraju 	unsigned long slot_addr;
1231d4b3b638SSrikar Dronamraju 	int slot_nr;
1232d4b3b638SSrikar Dronamraju 
1233d4b3b638SSrikar Dronamraju 	do {
1234d4b3b638SSrikar Dronamraju 		slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1235d4b3b638SSrikar Dronamraju 		if (slot_nr < UINSNS_PER_PAGE) {
1236d4b3b638SSrikar Dronamraju 			if (!test_and_set_bit(slot_nr, area->bitmap))
1237d4b3b638SSrikar Dronamraju 				break;
1238d4b3b638SSrikar Dronamraju 
1239d4b3b638SSrikar Dronamraju 			slot_nr = UINSNS_PER_PAGE;
1240d4b3b638SSrikar Dronamraju 			continue;
1241d4b3b638SSrikar Dronamraju 		}
1242d4b3b638SSrikar Dronamraju 		wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1243d4b3b638SSrikar Dronamraju 	} while (slot_nr >= UINSNS_PER_PAGE);
1244d4b3b638SSrikar Dronamraju 
1245d4b3b638SSrikar Dronamraju 	slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1246d4b3b638SSrikar Dronamraju 	atomic_inc(&area->slot_count);
1247d4b3b638SSrikar Dronamraju 
1248d4b3b638SSrikar Dronamraju 	return slot_addr;
1249d4b3b638SSrikar Dronamraju }
1250d4b3b638SSrikar Dronamraju 
1251d4b3b638SSrikar Dronamraju /*
1252d4b3b638SSrikar Dronamraju  * xol_get_insn_slot - If was not allocated a slot, then
1253d4b3b638SSrikar Dronamraju  * allocate a slot.
1254d4b3b638SSrikar Dronamraju  * Returns the allocated slot address or 0.
1255d4b3b638SSrikar Dronamraju  */
1256d4b3b638SSrikar Dronamraju static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1257d4b3b638SSrikar Dronamraju {
1258d4b3b638SSrikar Dronamraju 	struct xol_area *area;
1259d4b3b638SSrikar Dronamraju 	unsigned long offset;
1260d4b3b638SSrikar Dronamraju 	void *vaddr;
1261d4b3b638SSrikar Dronamraju 
1262d4b3b638SSrikar Dronamraju 	area = get_xol_area(current->mm);
1263d4b3b638SSrikar Dronamraju 	if (!area) {
1264d4b3b638SSrikar Dronamraju 		area = xol_alloc_area();
1265d4b3b638SSrikar Dronamraju 		if (!area)
1266d4b3b638SSrikar Dronamraju 			return 0;
1267d4b3b638SSrikar Dronamraju 	}
1268d4b3b638SSrikar Dronamraju 	current->utask->xol_vaddr = xol_take_insn_slot(area);
1269d4b3b638SSrikar Dronamraju 
1270d4b3b638SSrikar Dronamraju 	/*
1271d4b3b638SSrikar Dronamraju 	 * Initialize the slot if xol_vaddr points to valid
1272d4b3b638SSrikar Dronamraju 	 * instruction slot.
1273d4b3b638SSrikar Dronamraju 	 */
1274d4b3b638SSrikar Dronamraju 	if (unlikely(!current->utask->xol_vaddr))
1275d4b3b638SSrikar Dronamraju 		return 0;
1276d4b3b638SSrikar Dronamraju 
1277d4b3b638SSrikar Dronamraju 	current->utask->vaddr = slot_addr;
1278d4b3b638SSrikar Dronamraju 	offset = current->utask->xol_vaddr & ~PAGE_MASK;
1279d4b3b638SSrikar Dronamraju 	vaddr = kmap_atomic(area->page);
1280d4b3b638SSrikar Dronamraju 	memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1281d4b3b638SSrikar Dronamraju 	kunmap_atomic(vaddr);
1282d4b3b638SSrikar Dronamraju 
1283d4b3b638SSrikar Dronamraju 	return current->utask->xol_vaddr;
1284d4b3b638SSrikar Dronamraju }
1285d4b3b638SSrikar Dronamraju 
1286d4b3b638SSrikar Dronamraju /*
1287d4b3b638SSrikar Dronamraju  * xol_free_insn_slot - If slot was earlier allocated by
1288d4b3b638SSrikar Dronamraju  * @xol_get_insn_slot(), make the slot available for
1289d4b3b638SSrikar Dronamraju  * subsequent requests.
1290d4b3b638SSrikar Dronamraju  */
1291d4b3b638SSrikar Dronamraju static void xol_free_insn_slot(struct task_struct *tsk)
1292d4b3b638SSrikar Dronamraju {
1293d4b3b638SSrikar Dronamraju 	struct xol_area *area;
1294d4b3b638SSrikar Dronamraju 	unsigned long vma_end;
1295d4b3b638SSrikar Dronamraju 	unsigned long slot_addr;
1296d4b3b638SSrikar Dronamraju 
1297d4b3b638SSrikar Dronamraju 	if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1298d4b3b638SSrikar Dronamraju 		return;
1299d4b3b638SSrikar Dronamraju 
1300d4b3b638SSrikar Dronamraju 	slot_addr = tsk->utask->xol_vaddr;
1301d4b3b638SSrikar Dronamraju 
1302d4b3b638SSrikar Dronamraju 	if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1303d4b3b638SSrikar Dronamraju 		return;
1304d4b3b638SSrikar Dronamraju 
1305d4b3b638SSrikar Dronamraju 	area = tsk->mm->uprobes_state.xol_area;
1306d4b3b638SSrikar Dronamraju 	vma_end = area->vaddr + PAGE_SIZE;
1307d4b3b638SSrikar Dronamraju 	if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1308d4b3b638SSrikar Dronamraju 		unsigned long offset;
1309d4b3b638SSrikar Dronamraju 		int slot_nr;
1310d4b3b638SSrikar Dronamraju 
1311d4b3b638SSrikar Dronamraju 		offset = slot_addr - area->vaddr;
1312d4b3b638SSrikar Dronamraju 		slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1313d4b3b638SSrikar Dronamraju 		if (slot_nr >= UINSNS_PER_PAGE)
1314d4b3b638SSrikar Dronamraju 			return;
1315d4b3b638SSrikar Dronamraju 
1316d4b3b638SSrikar Dronamraju 		clear_bit(slot_nr, area->bitmap);
1317d4b3b638SSrikar Dronamraju 		atomic_dec(&area->slot_count);
1318d4b3b638SSrikar Dronamraju 		if (waitqueue_active(&area->wq))
1319d4b3b638SSrikar Dronamraju 			wake_up(&area->wq);
1320d4b3b638SSrikar Dronamraju 
1321d4b3b638SSrikar Dronamraju 		tsk->utask->xol_vaddr = 0;
1322d4b3b638SSrikar Dronamraju 	}
1323d4b3b638SSrikar Dronamraju }
1324d4b3b638SSrikar Dronamraju 
13250326f5a9SSrikar Dronamraju /**
13260326f5a9SSrikar Dronamraju  * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
13270326f5a9SSrikar Dronamraju  * @regs: Reflects the saved state of the task after it has hit a breakpoint
13280326f5a9SSrikar Dronamraju  * instruction.
13290326f5a9SSrikar Dronamraju  * Return the address of the breakpoint instruction.
13300326f5a9SSrikar Dronamraju  */
13310326f5a9SSrikar Dronamraju unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
13320326f5a9SSrikar Dronamraju {
13330326f5a9SSrikar Dronamraju 	return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
13340326f5a9SSrikar Dronamraju }
13350326f5a9SSrikar Dronamraju 
13360326f5a9SSrikar Dronamraju /*
13370326f5a9SSrikar Dronamraju  * Called with no locks held.
13380326f5a9SSrikar Dronamraju  * Called in context of a exiting or a exec-ing thread.
13390326f5a9SSrikar Dronamraju  */
13400326f5a9SSrikar Dronamraju void uprobe_free_utask(struct task_struct *t)
13410326f5a9SSrikar Dronamraju {
13420326f5a9SSrikar Dronamraju 	struct uprobe_task *utask = t->utask;
13430326f5a9SSrikar Dronamraju 
13440326f5a9SSrikar Dronamraju 	if (!utask)
13450326f5a9SSrikar Dronamraju 		return;
13460326f5a9SSrikar Dronamraju 
13470326f5a9SSrikar Dronamraju 	if (utask->active_uprobe)
13480326f5a9SSrikar Dronamraju 		put_uprobe(utask->active_uprobe);
13490326f5a9SSrikar Dronamraju 
1350d4b3b638SSrikar Dronamraju 	xol_free_insn_slot(t);
13510326f5a9SSrikar Dronamraju 	kfree(utask);
13520326f5a9SSrikar Dronamraju 	t->utask = NULL;
13530326f5a9SSrikar Dronamraju }
13540326f5a9SSrikar Dronamraju 
13550326f5a9SSrikar Dronamraju /*
13560326f5a9SSrikar Dronamraju  * Called in context of a new clone/fork from copy_process.
13570326f5a9SSrikar Dronamraju  */
13580326f5a9SSrikar Dronamraju void uprobe_copy_process(struct task_struct *t)
13590326f5a9SSrikar Dronamraju {
13600326f5a9SSrikar Dronamraju 	t->utask = NULL;
13610326f5a9SSrikar Dronamraju }
13620326f5a9SSrikar Dronamraju 
13630326f5a9SSrikar Dronamraju /*
13640326f5a9SSrikar Dronamraju  * Allocate a uprobe_task object for the task.
13650326f5a9SSrikar Dronamraju  * Called when the thread hits a breakpoint for the first time.
13660326f5a9SSrikar Dronamraju  *
13670326f5a9SSrikar Dronamraju  * Returns:
13680326f5a9SSrikar Dronamraju  * - pointer to new uprobe_task on success
13690326f5a9SSrikar Dronamraju  * - NULL otherwise
13700326f5a9SSrikar Dronamraju  */
13710326f5a9SSrikar Dronamraju static struct uprobe_task *add_utask(void)
13720326f5a9SSrikar Dronamraju {
13730326f5a9SSrikar Dronamraju 	struct uprobe_task *utask;
13740326f5a9SSrikar Dronamraju 
13750326f5a9SSrikar Dronamraju 	utask = kzalloc(sizeof *utask, GFP_KERNEL);
13760326f5a9SSrikar Dronamraju 	if (unlikely(!utask))
13770326f5a9SSrikar Dronamraju 		return NULL;
13780326f5a9SSrikar Dronamraju 
13790326f5a9SSrikar Dronamraju 	current->utask = utask;
13800326f5a9SSrikar Dronamraju 	return utask;
13810326f5a9SSrikar Dronamraju }
13820326f5a9SSrikar Dronamraju 
13830326f5a9SSrikar Dronamraju /* Prepare to single-step probed instruction out of line. */
13840326f5a9SSrikar Dronamraju static int
13850326f5a9SSrikar Dronamraju pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
13860326f5a9SSrikar Dronamraju {
1387d4b3b638SSrikar Dronamraju 	if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1388d4b3b638SSrikar Dronamraju 		return 0;
1389d4b3b638SSrikar Dronamraju 
13900326f5a9SSrikar Dronamraju 	return -EFAULT;
13910326f5a9SSrikar Dronamraju }
13920326f5a9SSrikar Dronamraju 
13930326f5a9SSrikar Dronamraju /*
13940326f5a9SSrikar Dronamraju  * If we are singlestepping, then ensure this thread is not connected to
13950326f5a9SSrikar Dronamraju  * non-fatal signals until completion of singlestep.  When xol insn itself
13960326f5a9SSrikar Dronamraju  * triggers the signal,  restart the original insn even if the task is
13970326f5a9SSrikar Dronamraju  * already SIGKILL'ed (since coredump should report the correct ip).  This
13980326f5a9SSrikar Dronamraju  * is even more important if the task has a handler for SIGSEGV/etc, The
13990326f5a9SSrikar Dronamraju  * _same_ instruction should be repeated again after return from the signal
14000326f5a9SSrikar Dronamraju  * handler, and SSTEP can never finish in this case.
14010326f5a9SSrikar Dronamraju  */
14020326f5a9SSrikar Dronamraju bool uprobe_deny_signal(void)
14030326f5a9SSrikar Dronamraju {
14040326f5a9SSrikar Dronamraju 	struct task_struct *t = current;
14050326f5a9SSrikar Dronamraju 	struct uprobe_task *utask = t->utask;
14060326f5a9SSrikar Dronamraju 
14070326f5a9SSrikar Dronamraju 	if (likely(!utask || !utask->active_uprobe))
14080326f5a9SSrikar Dronamraju 		return false;
14090326f5a9SSrikar Dronamraju 
14100326f5a9SSrikar Dronamraju 	WARN_ON_ONCE(utask->state != UTASK_SSTEP);
14110326f5a9SSrikar Dronamraju 
14120326f5a9SSrikar Dronamraju 	if (signal_pending(t)) {
14130326f5a9SSrikar Dronamraju 		spin_lock_irq(&t->sighand->siglock);
14140326f5a9SSrikar Dronamraju 		clear_tsk_thread_flag(t, TIF_SIGPENDING);
14150326f5a9SSrikar Dronamraju 		spin_unlock_irq(&t->sighand->siglock);
14160326f5a9SSrikar Dronamraju 
14170326f5a9SSrikar Dronamraju 		if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
14180326f5a9SSrikar Dronamraju 			utask->state = UTASK_SSTEP_TRAPPED;
14190326f5a9SSrikar Dronamraju 			set_tsk_thread_flag(t, TIF_UPROBE);
14200326f5a9SSrikar Dronamraju 			set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
14210326f5a9SSrikar Dronamraju 		}
14220326f5a9SSrikar Dronamraju 	}
14230326f5a9SSrikar Dronamraju 
14240326f5a9SSrikar Dronamraju 	return true;
14250326f5a9SSrikar Dronamraju }
14260326f5a9SSrikar Dronamraju 
14270326f5a9SSrikar Dronamraju /*
14280326f5a9SSrikar Dronamraju  * Avoid singlestepping the original instruction if the original instruction
14290326f5a9SSrikar Dronamraju  * is a NOP or can be emulated.
14300326f5a9SSrikar Dronamraju  */
14310326f5a9SSrikar Dronamraju static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
14320326f5a9SSrikar Dronamraju {
14330326f5a9SSrikar Dronamraju 	if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
14340326f5a9SSrikar Dronamraju 		return true;
14350326f5a9SSrikar Dronamraju 
14360326f5a9SSrikar Dronamraju 	uprobe->flags &= ~UPROBE_SKIP_SSTEP;
14370326f5a9SSrikar Dronamraju 	return false;
14380326f5a9SSrikar Dronamraju }
14390326f5a9SSrikar Dronamraju 
1440d790d346SOleg Nesterov static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
14410326f5a9SSrikar Dronamraju {
14423a9ea052SOleg Nesterov 	struct mm_struct *mm = current->mm;
14433a9ea052SOleg Nesterov 	struct uprobe *uprobe = NULL;
14440326f5a9SSrikar Dronamraju 	struct vm_area_struct *vma;
14450326f5a9SSrikar Dronamraju 
14460326f5a9SSrikar Dronamraju 	down_read(&mm->mmap_sem);
14470326f5a9SSrikar Dronamraju 	vma = find_vma(mm, bp_vaddr);
14483a9ea052SOleg Nesterov 	if (vma && vma->vm_start <= bp_vaddr) {
14493a9ea052SOleg Nesterov 		if (valid_vma(vma, false)) {
1450cb113b47SOleg Nesterov 			struct inode *inode = vma->vm_file->f_mapping->host;
1451cb113b47SOleg Nesterov 			loff_t offset = vaddr_to_offset(vma, bp_vaddr);
14520326f5a9SSrikar Dronamraju 
14530326f5a9SSrikar Dronamraju 			uprobe = find_uprobe(inode, offset);
14540326f5a9SSrikar Dronamraju 		}
1455d790d346SOleg Nesterov 
1456d790d346SOleg Nesterov 		if (!uprobe)
1457d790d346SOleg Nesterov 			*is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1458d790d346SOleg Nesterov 	} else {
1459d790d346SOleg Nesterov 		*is_swbp = -EFAULT;
14603a9ea052SOleg Nesterov 	}
14610326f5a9SSrikar Dronamraju 	up_read(&mm->mmap_sem);
14620326f5a9SSrikar Dronamraju 
14633a9ea052SOleg Nesterov 	return uprobe;
14643a9ea052SOleg Nesterov }
14653a9ea052SOleg Nesterov 
14663a9ea052SOleg Nesterov /*
14673a9ea052SOleg Nesterov  * Run handler and ask thread to singlestep.
14683a9ea052SOleg Nesterov  * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
14693a9ea052SOleg Nesterov  */
14703a9ea052SOleg Nesterov static void handle_swbp(struct pt_regs *regs)
14713a9ea052SOleg Nesterov {
14723a9ea052SOleg Nesterov 	struct uprobe_task *utask;
14733a9ea052SOleg Nesterov 	struct uprobe *uprobe;
14743a9ea052SOleg Nesterov 	unsigned long bp_vaddr;
147556bb4cf6SOleg Nesterov 	int uninitialized_var(is_swbp);
14763a9ea052SOleg Nesterov 
14773a9ea052SOleg Nesterov 	bp_vaddr = uprobe_get_swbp_addr(regs);
1478d790d346SOleg Nesterov 	uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
14793a9ea052SOleg Nesterov 
14800326f5a9SSrikar Dronamraju 	if (!uprobe) {
148156bb4cf6SOleg Nesterov 		if (is_swbp > 0) {
14820326f5a9SSrikar Dronamraju 			/* No matching uprobe; signal SIGTRAP. */
14830326f5a9SSrikar Dronamraju 			send_sig(SIGTRAP, current, 0);
148456bb4cf6SOleg Nesterov 		} else {
148556bb4cf6SOleg Nesterov 			/*
148656bb4cf6SOleg Nesterov 			 * Either we raced with uprobe_unregister() or we can't
148756bb4cf6SOleg Nesterov 			 * access this memory. The latter is only possible if
148856bb4cf6SOleg Nesterov 			 * another thread plays with our ->mm. In both cases
148956bb4cf6SOleg Nesterov 			 * we can simply restart. If this vma was unmapped we
149056bb4cf6SOleg Nesterov 			 * can pretend this insn was not executed yet and get
149156bb4cf6SOleg Nesterov 			 * the (correct) SIGSEGV after restart.
149256bb4cf6SOleg Nesterov 			 */
149356bb4cf6SOleg Nesterov 			instruction_pointer_set(regs, bp_vaddr);
149456bb4cf6SOleg Nesterov 		}
14950326f5a9SSrikar Dronamraju 		return;
14960326f5a9SSrikar Dronamraju 	}
14970326f5a9SSrikar Dronamraju 
14980326f5a9SSrikar Dronamraju 	utask = current->utask;
14990326f5a9SSrikar Dronamraju 	if (!utask) {
15000326f5a9SSrikar Dronamraju 		utask = add_utask();
15010326f5a9SSrikar Dronamraju 		/* Cannot allocate; re-execute the instruction. */
15020326f5a9SSrikar Dronamraju 		if (!utask)
15030326f5a9SSrikar Dronamraju 			goto cleanup_ret;
15040326f5a9SSrikar Dronamraju 	}
15050326f5a9SSrikar Dronamraju 	utask->active_uprobe = uprobe;
15060326f5a9SSrikar Dronamraju 	handler_chain(uprobe, regs);
15070326f5a9SSrikar Dronamraju 	if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
15080326f5a9SSrikar Dronamraju 		goto cleanup_ret;
15090326f5a9SSrikar Dronamraju 
15100326f5a9SSrikar Dronamraju 	utask->state = UTASK_SSTEP;
15110326f5a9SSrikar Dronamraju 	if (!pre_ssout(uprobe, regs, bp_vaddr)) {
15120326f5a9SSrikar Dronamraju 		user_enable_single_step(current);
15130326f5a9SSrikar Dronamraju 		return;
15140326f5a9SSrikar Dronamraju 	}
15150326f5a9SSrikar Dronamraju 
15160326f5a9SSrikar Dronamraju cleanup_ret:
15170326f5a9SSrikar Dronamraju 	if (utask) {
15180326f5a9SSrikar Dronamraju 		utask->active_uprobe = NULL;
15190326f5a9SSrikar Dronamraju 		utask->state = UTASK_RUNNING;
15200326f5a9SSrikar Dronamraju 	}
15210326f5a9SSrikar Dronamraju 	if (uprobe) {
15220326f5a9SSrikar Dronamraju 		if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
15230326f5a9SSrikar Dronamraju 
15240326f5a9SSrikar Dronamraju 			/*
15250326f5a9SSrikar Dronamraju 			 * cannot singlestep; cannot skip instruction;
15260326f5a9SSrikar Dronamraju 			 * re-execute the instruction.
15270326f5a9SSrikar Dronamraju 			 */
15280326f5a9SSrikar Dronamraju 			instruction_pointer_set(regs, bp_vaddr);
15290326f5a9SSrikar Dronamraju 
15300326f5a9SSrikar Dronamraju 		put_uprobe(uprobe);
15310326f5a9SSrikar Dronamraju 	}
15320326f5a9SSrikar Dronamraju }
15330326f5a9SSrikar Dronamraju 
15340326f5a9SSrikar Dronamraju /*
15350326f5a9SSrikar Dronamraju  * Perform required fix-ups and disable singlestep.
15360326f5a9SSrikar Dronamraju  * Allow pending signals to take effect.
15370326f5a9SSrikar Dronamraju  */
15380326f5a9SSrikar Dronamraju static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
15390326f5a9SSrikar Dronamraju {
15400326f5a9SSrikar Dronamraju 	struct uprobe *uprobe;
15410326f5a9SSrikar Dronamraju 
15420326f5a9SSrikar Dronamraju 	uprobe = utask->active_uprobe;
15430326f5a9SSrikar Dronamraju 	if (utask->state == UTASK_SSTEP_ACK)
15440326f5a9SSrikar Dronamraju 		arch_uprobe_post_xol(&uprobe->arch, regs);
15450326f5a9SSrikar Dronamraju 	else if (utask->state == UTASK_SSTEP_TRAPPED)
15460326f5a9SSrikar Dronamraju 		arch_uprobe_abort_xol(&uprobe->arch, regs);
15470326f5a9SSrikar Dronamraju 	else
15480326f5a9SSrikar Dronamraju 		WARN_ON_ONCE(1);
15490326f5a9SSrikar Dronamraju 
15500326f5a9SSrikar Dronamraju 	put_uprobe(uprobe);
15510326f5a9SSrikar Dronamraju 	utask->active_uprobe = NULL;
15520326f5a9SSrikar Dronamraju 	utask->state = UTASK_RUNNING;
15530326f5a9SSrikar Dronamraju 	user_disable_single_step(current);
1554d4b3b638SSrikar Dronamraju 	xol_free_insn_slot(current);
15550326f5a9SSrikar Dronamraju 
15560326f5a9SSrikar Dronamraju 	spin_lock_irq(&current->sighand->siglock);
15570326f5a9SSrikar Dronamraju 	recalc_sigpending(); /* see uprobe_deny_signal() */
15580326f5a9SSrikar Dronamraju 	spin_unlock_irq(&current->sighand->siglock);
15590326f5a9SSrikar Dronamraju }
15600326f5a9SSrikar Dronamraju 
15610326f5a9SSrikar Dronamraju /*
15620326f5a9SSrikar Dronamraju  * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag.  (and on
15630326f5a9SSrikar Dronamraju  * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
15640326f5a9SSrikar Dronamraju  * allows the thread to return from interrupt.
15650326f5a9SSrikar Dronamraju  *
15660326f5a9SSrikar Dronamraju  * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
15670326f5a9SSrikar Dronamraju  * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
15680326f5a9SSrikar Dronamraju  * interrupt.
15690326f5a9SSrikar Dronamraju  *
15700326f5a9SSrikar Dronamraju  * While returning to userspace, thread notices the TIF_UPROBE flag and calls
15710326f5a9SSrikar Dronamraju  * uprobe_notify_resume().
15720326f5a9SSrikar Dronamraju  */
15730326f5a9SSrikar Dronamraju void uprobe_notify_resume(struct pt_regs *regs)
15740326f5a9SSrikar Dronamraju {
15750326f5a9SSrikar Dronamraju 	struct uprobe_task *utask;
15760326f5a9SSrikar Dronamraju 
15770326f5a9SSrikar Dronamraju 	utask = current->utask;
15780326f5a9SSrikar Dronamraju 	if (!utask || utask->state == UTASK_BP_HIT)
15790326f5a9SSrikar Dronamraju 		handle_swbp(regs);
15800326f5a9SSrikar Dronamraju 	else
15810326f5a9SSrikar Dronamraju 		handle_singlestep(utask, regs);
15820326f5a9SSrikar Dronamraju }
15830326f5a9SSrikar Dronamraju 
15840326f5a9SSrikar Dronamraju /*
15850326f5a9SSrikar Dronamraju  * uprobe_pre_sstep_notifier gets called from interrupt context as part of
15860326f5a9SSrikar Dronamraju  * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
15870326f5a9SSrikar Dronamraju  */
15880326f5a9SSrikar Dronamraju int uprobe_pre_sstep_notifier(struct pt_regs *regs)
15890326f5a9SSrikar Dronamraju {
15900326f5a9SSrikar Dronamraju 	struct uprobe_task *utask;
15910326f5a9SSrikar Dronamraju 
1592682968e0SSrikar Dronamraju 	if (!current->mm || !atomic_read(&current->mm->uprobes_state.count))
1593682968e0SSrikar Dronamraju 		/* task is currently not uprobed */
15940326f5a9SSrikar Dronamraju 		return 0;
15950326f5a9SSrikar Dronamraju 
15960326f5a9SSrikar Dronamraju 	utask = current->utask;
15970326f5a9SSrikar Dronamraju 	if (utask)
15980326f5a9SSrikar Dronamraju 		utask->state = UTASK_BP_HIT;
15990326f5a9SSrikar Dronamraju 
16000326f5a9SSrikar Dronamraju 	set_thread_flag(TIF_UPROBE);
16010326f5a9SSrikar Dronamraju 
16020326f5a9SSrikar Dronamraju 	return 1;
16030326f5a9SSrikar Dronamraju }
16040326f5a9SSrikar Dronamraju 
16050326f5a9SSrikar Dronamraju /*
16060326f5a9SSrikar Dronamraju  * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
16070326f5a9SSrikar Dronamraju  * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
16080326f5a9SSrikar Dronamraju  */
16090326f5a9SSrikar Dronamraju int uprobe_post_sstep_notifier(struct pt_regs *regs)
16100326f5a9SSrikar Dronamraju {
16110326f5a9SSrikar Dronamraju 	struct uprobe_task *utask = current->utask;
16120326f5a9SSrikar Dronamraju 
16130326f5a9SSrikar Dronamraju 	if (!current->mm || !utask || !utask->active_uprobe)
16140326f5a9SSrikar Dronamraju 		/* task is currently not uprobed */
16150326f5a9SSrikar Dronamraju 		return 0;
16160326f5a9SSrikar Dronamraju 
16170326f5a9SSrikar Dronamraju 	utask->state = UTASK_SSTEP_ACK;
16180326f5a9SSrikar Dronamraju 	set_thread_flag(TIF_UPROBE);
16190326f5a9SSrikar Dronamraju 	return 1;
16200326f5a9SSrikar Dronamraju }
16210326f5a9SSrikar Dronamraju 
16220326f5a9SSrikar Dronamraju static struct notifier_block uprobe_exception_nb = {
16230326f5a9SSrikar Dronamraju 	.notifier_call		= arch_uprobe_exception_notify,
16240326f5a9SSrikar Dronamraju 	.priority		= INT_MAX-1,	/* notified after kprobes, kgdb */
16250326f5a9SSrikar Dronamraju };
16260326f5a9SSrikar Dronamraju 
1627a5f4374aSIngo Molnar static int __init init_uprobes(void)
1628a5f4374aSIngo Molnar {
1629a5f4374aSIngo Molnar 	int i;
1630a5f4374aSIngo Molnar 
1631a5f4374aSIngo Molnar 	for (i = 0; i < UPROBES_HASH_SZ; i++) {
1632a5f4374aSIngo Molnar 		mutex_init(&uprobes_mutex[i]);
1633a5f4374aSIngo Molnar 		mutex_init(&uprobes_mmap_mutex[i]);
1634a5f4374aSIngo Molnar 	}
16350326f5a9SSrikar Dronamraju 
16360326f5a9SSrikar Dronamraju 	return register_die_notifier(&uprobe_exception_nb);
1637a5f4374aSIngo Molnar }
16380326f5a9SSrikar Dronamraju module_init(init_uprobes);
1639a5f4374aSIngo Molnar 
1640a5f4374aSIngo Molnar static void __exit exit_uprobes(void)
1641a5f4374aSIngo Molnar {
1642a5f4374aSIngo Molnar }
1643a5f4374aSIngo Molnar module_exit(exit_uprobes);
1644