blob: c5cde87329c7cdac7b968c7672aad5c0d245ceca [file] [log] [blame]
Thomas Gleixner720e5962019-01-16 12:11:01 +01001// SPDX-License-Identifier: GPL-2.0+
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01003 * User-space Probes (UProbes)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05304 *
Ingo Molnar35aa6212012-02-22 11:37:29 +01005 * Copyright (C) IBM Corporation, 2008-2012
Srikar Dronamraju2b144492012-02-09 14:56:42 +05306 * Authors:
7 * Srikar Dronamraju
8 * Jim Keniston
Peter Zijlstra90eec102015-11-16 11:08:45 +01009 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
Srikar Dronamraju2b144492012-02-09 14:56:42 +053010 */
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>
Ingo Molnar6e84f312017-02-08 18:51:29 +010017#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010018#include <linux/sched/coredump.h>
Josh Stonee8440c12013-01-13 19:03:34 +010019#include <linux/export.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053020#include <linux/rmap.h> /* anon_vma_prepare */
21#include <linux/mmu_notifier.h> /* set_pte_at_notify */
22#include <linux/swap.h> /* try_to_free_swap */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053023#include <linux/ptrace.h> /* user_enable_single_step */
24#include <linux/kdebug.h> /* notifier mechanism */
Oleg Nesterov194f8dc2012-07-29 20:22:49 +020025#include "../../mm/internal.h" /* munlock_vma_page */
Oleg Nesterov32cdba12012-11-14 19:03:42 +010026#include <linux/percpu-rwsem.h>
Oleg Nesterovaa59c532013-10-13 21:18:44 +020027#include <linux/task_work.h>
Oleg Nesterov40814f62014-05-19 20:41:36 +020028#include <linux/shmem_fs.h>
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010029
Srikar Dronamraju2b144492012-02-09 14:56:42 +053030#include <linux/uprobes.h>
31
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053032#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
33#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
34
Srikar Dronamraju2b144492012-02-09 14:56:42 +053035static struct rb_root uprobes_tree = RB_ROOT;
Oleg Nesterov441f1eb72012-11-25 19:54:29 +010036/*
37 * allows us to skip the uprobe_mmap if there are no uprobe events active
38 * at this time. Probably a fine grained per inode count is better?
39 */
40#define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010041
Srikar Dronamraju2b144492012-02-09 14:56:42 +053042static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
43
44#define UPROBES_HASH_SZ 13
Srikar Dronamraju2b144492012-02-09 14:56:42 +053045/* serialize uprobe->pending_list */
46static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010047#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053048
Oleg Nesterov32cdba12012-11-14 19:03:42 +010049static struct percpu_rw_semaphore dup_mmap_sem;
50
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020051/* Have a copy of original instruction */
Oleg Nesterov71434f22012-09-30 21:12:44 +020052#define UPROBE_COPY_INSN 0
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020053
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053054struct uprobe {
55 struct rb_node rb_node; /* node in the rb tree */
Elena Reshetovace59b8e2019-01-16 13:20:27 +020056 refcount_t ref;
Oleg Nesterove591c8d2012-11-24 17:29:40 +010057 struct rw_semaphore register_rwsem;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053058 struct rw_semaphore consumer_rwsem;
59 struct list_head pending_list;
60 struct uprobe_consumer *consumers;
61 struct inode *inode; /* Also hold a ref to inode */
62 loff_t offset;
Ravi Bangoria1cc33162018-08-20 10:12:47 +053063 loff_t ref_ctr_offset;
Oleg Nesterov71434f22012-09-30 21:12:44 +020064 unsigned long flags;
Oleg Nesterovad439352013-11-19 17:20:21 +010065
66 /*
67 * The generic code assumes that it has two members of unknown type
68 * owned by the arch-specific code:
69 *
70 * insn - copy_insn() saves the original instruction here for
71 * arch_uprobe_analyze_insn().
72 *
73 * ixol - potentially modified instruction to execute out of
74 * line, copied to xol_area by xol_get_insn_slot().
75 */
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053076 struct arch_uprobe arch;
77};
78
Ravi Bangoria1cc33162018-08-20 10:12:47 +053079struct delayed_uprobe {
80 struct list_head list;
81 struct uprobe *uprobe;
82 struct mm_struct *mm;
83};
84
85static DEFINE_MUTEX(delayed_uprobe_lock);
86static LIST_HEAD(delayed_uprobe_list);
87
Srikar Dronamraju2b144492012-02-09 14:56:42 +053088/*
Oleg Nesterovad439352013-11-19 17:20:21 +010089 * Execute out of line area: anonymous executable mapping installed
90 * by the probed task to execute the copy of the original instruction
91 * mangled by set_swbp().
92 *
Oleg Nesterovc912dae2013-11-09 19:49:39 +010093 * On a breakpoint hit, thread contests for a slot. It frees the
94 * slot after singlestep. Currently a fixed number of slots are
95 * allocated.
96 */
97struct xol_area {
Oleg Nesterov704bde32015-07-21 15:40:33 +020098 wait_queue_head_t wq; /* if all slots are busy */
99 atomic_t slot_count; /* number of in-use slots */
100 unsigned long *bitmap; /* 0 = free slot */
Oleg Nesterovc912dae2013-11-09 19:49:39 +0100101
Oleg Nesterov704bde32015-07-21 15:40:33 +0200102 struct vm_special_mapping xol_mapping;
103 struct page *pages[2];
Oleg Nesterovc912dae2013-11-09 19:49:39 +0100104 /*
105 * We keep the vma's vm_start rather than a pointer to the vma
106 * itself. The probed process or a naughty kernel module could make
107 * the vma go away, and we must handle that reasonably gracefully.
108 */
Oleg Nesterov704bde32015-07-21 15:40:33 +0200109 unsigned long vaddr; /* Page(s) of instruction slots */
Oleg Nesterovc912dae2013-11-09 19:49:39 +0100110};
111
112/*
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530113 * valid_vma: Verify if the specified vma is an executable vma
114 * Relax restrictions while unregistering: vm_flags might have
115 * changed after breakpoint was inserted.
116 * - is_register: indicates if we are in register context.
117 * - Return 1 if the specified virtual address is in an
118 * executable vma.
119 */
120static bool valid_vma(struct vm_area_struct *vma, bool is_register)
121{
Oleg Nesterov13f59c52014-04-28 20:15:43 +0200122 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530123
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200124 if (is_register)
125 flags |= VM_WRITE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530126
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200127 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530128}
129
Oleg Nesterov57683f72012-07-29 20:22:47 +0200130static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530131{
Oleg Nesterov57683f72012-07-29 20:22:47 +0200132 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530133}
134
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200135static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
136{
137 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
138}
139
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530140/**
141 * __replace_page - replace page in vma by new page.
142 * based on replace_page in mm/ksm.c
143 *
144 * @vma: vma that holds the pte pointing to page
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200145 * @addr: address the old @page is mapped at
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530146 * @page: the cowed page we are replacing by kpage
147 * @kpage: the modified page we replace page by
148 *
149 * Returns 0 on success, -EFAULT on failure.
150 */
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200151static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200152 struct page *old_page, struct page *new_page)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530153{
154 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov14fa2da2017-02-24 14:58:07 -0800155 struct page_vma_mapped_walk pvmw = {
156 .page = old_page,
157 .vma = vma,
158 .address = addr,
159 };
Oleg Nesterov9f924482012-07-29 20:22:20 +0200160 int err;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800161 struct mmu_notifier_range range;
Johannes Weiner00501b52014-08-08 14:19:20 -0700162 struct mem_cgroup *memcg;
163
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800164 mmu_notifier_range_init(&range, mm, addr, addr + PAGE_SIZE);
165
Kirill A. Shutemov14fa2da2017-02-24 14:58:07 -0800166 VM_BUG_ON_PAGE(PageTransHuge(old_page), old_page);
167
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200168 err = mem_cgroup_try_charge(new_page, vma->vm_mm, GFP_KERNEL, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800169 false);
Johannes Weiner00501b52014-08-08 14:19:20 -0700170 if (err)
171 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530172
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200173 /* For try_to_free_swap() and munlock_vma_page() below */
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200174 lock_page(old_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200175
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800176 mmu_notifier_invalidate_range_start(&range);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200177 err = -EAGAIN;
Kirill A. Shutemov14fa2da2017-02-24 14:58:07 -0800178 if (!page_vma_mapped_walk(&pvmw)) {
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200179 mem_cgroup_cancel_charge(new_page, memcg, false);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200180 goto unlock;
Oleg Nesterov6c4687c2016-08-17 17:36:29 +0200181 }
Kirill A. Shutemov14fa2da2017-02-24 14:58:07 -0800182 VM_BUG_ON_PAGE(addr != pvmw.address, old_page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530183
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200184 get_page(new_page);
185 page_add_new_anon_rmap(new_page, vma, addr, false);
186 mem_cgroup_commit_charge(new_page, memcg, false, false);
187 lru_cache_add_active_or_unevictable(new_page, vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530188
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200189 if (!PageAnon(old_page)) {
190 dec_mm_counter(mm, mm_counter_file(old_page));
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530191 inc_mm_counter(mm, MM_ANONPAGES);
192 }
193
Kirill A. Shutemov14fa2da2017-02-24 14:58:07 -0800194 flush_cache_page(vma, addr, pte_pfn(*pvmw.pte));
195 ptep_clear_flush_notify(vma, addr, pvmw.pte);
196 set_pte_at_notify(mm, addr, pvmw.pte,
197 mk_pte(new_page, vma->vm_page_prot));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530198
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200199 page_remove_rmap(old_page, false);
200 if (!page_mapped(old_page))
201 try_to_free_swap(old_page);
Kirill A. Shutemov14fa2da2017-02-24 14:58:07 -0800202 page_vma_mapped_walk_done(&pvmw);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530203
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200204 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200205 munlock_vma_page(old_page);
206 put_page(old_page);
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200207
Oleg Nesterov9f924482012-07-29 20:22:20 +0200208 err = 0;
209 unlock:
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800210 mmu_notifier_invalidate_range_end(&range);
Oleg Nesterovbdfaa2e2016-08-17 17:37:04 +0200211 unlock_page(old_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200212 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530213}
214
215/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530216 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530217 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530218 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530219 * Returns true if @insn is a breakpoint instruction.
220 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530221bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530222{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530223 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530224}
225
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530226/**
227 * is_trap_insn - check if instruction is breakpoint instruction.
228 * @insn: instruction to be checked.
229 * Default implementation of is_trap_insn
230 * Returns true if @insn is a breakpoint instruction.
231 *
232 * This function is needed for the case where an architecture has multiple
233 * trap instructions (like powerpc).
234 */
235bool __weak is_trap_insn(uprobe_opcode_t *insn)
236{
237 return is_swbp_insn(insn);
238}
239
Oleg Nesterovab0d8052013-03-24 18:24:37 +0100240static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200241{
242 void *kaddr = kmap_atomic(page);
Oleg Nesterovab0d8052013-03-24 18:24:37 +0100243 memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200244 kunmap_atomic(kaddr);
245}
246
Oleg Nesterov5669cce2013-03-24 18:58:04 +0100247static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len)
248{
249 void *kaddr = kmap_atomic(page);
250 memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
251 kunmap_atomic(kaddr);
252}
253
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200254static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
255{
256 uprobe_opcode_t old_opcode;
257 bool is_swbp;
258
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530259 /*
260 * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
261 * We do not check if it is any other 'trap variant' which could
262 * be conditional trap instruction such as the one powerpc supports.
263 *
264 * The logic is that we do not care if the underlying instruction
265 * is a trap variant; uprobes always wins over any other (gdb)
266 * breakpoint.
267 */
Oleg Nesterovab0d8052013-03-24 18:24:37 +0100268 copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200269 is_swbp = is_swbp_insn(&old_opcode);
270
271 if (is_swbp_insn(new_opcode)) {
272 if (is_swbp) /* register: already installed? */
273 return 0;
274 } else {
275 if (!is_swbp) /* unregister: was it changed by us? */
Oleg Nesterov076a3652012-09-30 18:54:53 +0200276 return 0;
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200277 }
278
279 return 1;
280}
281
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530282static struct delayed_uprobe *
283delayed_uprobe_check(struct uprobe *uprobe, struct mm_struct *mm)
284{
285 struct delayed_uprobe *du;
286
287 list_for_each_entry(du, &delayed_uprobe_list, list)
288 if (du->uprobe == uprobe && du->mm == mm)
289 return du;
290 return NULL;
291}
292
293static int delayed_uprobe_add(struct uprobe *uprobe, struct mm_struct *mm)
294{
295 struct delayed_uprobe *du;
296
297 if (delayed_uprobe_check(uprobe, mm))
298 return 0;
299
300 du = kzalloc(sizeof(*du), GFP_KERNEL);
301 if (!du)
302 return -ENOMEM;
303
304 du->uprobe = uprobe;
305 du->mm = mm;
306 list_add(&du->list, &delayed_uprobe_list);
307 return 0;
308}
309
310static void delayed_uprobe_delete(struct delayed_uprobe *du)
311{
312 if (WARN_ON(!du))
313 return;
314 list_del(&du->list);
315 kfree(du);
316}
317
318static void delayed_uprobe_remove(struct uprobe *uprobe, struct mm_struct *mm)
319{
320 struct list_head *pos, *q;
321 struct delayed_uprobe *du;
322
323 if (!uprobe && !mm)
324 return;
325
326 list_for_each_safe(pos, q, &delayed_uprobe_list) {
327 du = list_entry(pos, struct delayed_uprobe, list);
328
329 if (uprobe && du->uprobe != uprobe)
330 continue;
331 if (mm && du->mm != mm)
332 continue;
333
334 delayed_uprobe_delete(du);
335 }
336}
337
338static bool valid_ref_ctr_vma(struct uprobe *uprobe,
339 struct vm_area_struct *vma)
340{
341 unsigned long vaddr = offset_to_vaddr(vma, uprobe->ref_ctr_offset);
342
343 return uprobe->ref_ctr_offset &&
344 vma->vm_file &&
345 file_inode(vma->vm_file) == uprobe->inode &&
346 (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE &&
347 vma->vm_start <= vaddr &&
348 vma->vm_end > vaddr;
349}
350
351static struct vm_area_struct *
352find_ref_ctr_vma(struct uprobe *uprobe, struct mm_struct *mm)
353{
354 struct vm_area_struct *tmp;
355
356 for (tmp = mm->mmap; tmp; tmp = tmp->vm_next)
357 if (valid_ref_ctr_vma(uprobe, tmp))
358 return tmp;
359
360 return NULL;
361}
362
363static int
364__update_ref_ctr(struct mm_struct *mm, unsigned long vaddr, short d)
365{
366 void *kaddr;
367 struct page *page;
368 struct vm_area_struct *vma;
369 int ret;
370 short *ptr;
371
372 if (!vaddr || !d)
373 return -EINVAL;
374
375 ret = get_user_pages_remote(NULL, mm, vaddr, 1,
376 FOLL_WRITE, &page, &vma, NULL);
377 if (unlikely(ret <= 0)) {
378 /*
379 * We are asking for 1 page. If get_user_pages_remote() fails,
380 * it may return 0, in that case we have to return error.
381 */
382 return ret == 0 ? -EBUSY : ret;
383 }
384
385 kaddr = kmap_atomic(page);
386 ptr = kaddr + (vaddr & ~PAGE_MASK);
387
388 if (unlikely(*ptr + d < 0)) {
389 pr_warn("ref_ctr going negative. vaddr: 0x%lx, "
390 "curr val: %d, delta: %d\n", vaddr, *ptr, d);
391 ret = -EINVAL;
392 goto out;
393 }
394
395 *ptr += d;
396 ret = 0;
397out:
398 kunmap_atomic(kaddr);
399 put_page(page);
400 return ret;
401}
402
403static void update_ref_ctr_warn(struct uprobe *uprobe,
404 struct mm_struct *mm, short d)
405{
406 pr_warn("ref_ctr %s failed for inode: 0x%lx offset: "
407 "0x%llx ref_ctr_offset: 0x%llx of mm: 0x%pK\n",
408 d > 0 ? "increment" : "decrement", uprobe->inode->i_ino,
409 (unsigned long long) uprobe->offset,
410 (unsigned long long) uprobe->ref_ctr_offset, mm);
411}
412
413static int update_ref_ctr(struct uprobe *uprobe, struct mm_struct *mm,
414 short d)
415{
416 struct vm_area_struct *rc_vma;
417 unsigned long rc_vaddr;
418 int ret = 0;
419
420 rc_vma = find_ref_ctr_vma(uprobe, mm);
421
422 if (rc_vma) {
423 rc_vaddr = offset_to_vaddr(rc_vma, uprobe->ref_ctr_offset);
424 ret = __update_ref_ctr(mm, rc_vaddr, d);
425 if (ret)
426 update_ref_ctr_warn(uprobe, mm, d);
427
428 if (d > 0)
429 return ret;
430 }
431
432 mutex_lock(&delayed_uprobe_lock);
433 if (d > 0)
434 ret = delayed_uprobe_add(uprobe, mm);
435 else
436 delayed_uprobe_remove(uprobe, mm);
437 mutex_unlock(&delayed_uprobe_lock);
438
439 return ret;
440}
441
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530442/*
443 * NOTE:
444 * Expect the breakpoint instruction to be the smallest size instruction for
445 * the architecture. If an arch has variable length instruction and the
446 * breakpoint instruction is not of the smallest length instruction
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530447 * supported by that architecture then we need to modify is_trap_at_addr and
Oleg Nesterovf72d41f2013-11-05 19:50:39 +0100448 * uprobe_write_opcode accordingly. This would never be a problem for archs
449 * that have fixed length instructions.
Oleg Nesterov29dedee2014-05-05 16:38:18 +0200450 *
Oleg Nesterovf72d41f2013-11-05 19:50:39 +0100451 * uprobe_write_opcode - write the opcode at a given virtual address.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530452 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530453 * @vaddr: the virtual address to store the opcode.
454 * @opcode: opcode to be written at @vaddr.
455 *
Oleg Nesterov29dedee2014-05-05 16:38:18 +0200456 * Called with mm->mmap_sem held for write.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530457 * Return 0 (success) or a negative errno.
458 */
Ravi Bangoria6d437432018-08-09 09:48:52 +0530459int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
460 unsigned long vaddr, uprobe_opcode_t opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530461{
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530462 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530463 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530464 struct vm_area_struct *vma;
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530465 int ret, is_register, ref_ctr_updated = 0;
466
467 is_register = is_swbp_insn(&opcode);
468 uprobe = container_of(auprobe, struct uprobe, arch);
Oleg Nesterovf4030722012-07-29 20:22:12 +0200469
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200470retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530471 /* Read the page with vaddr into memory */
Kirill A. Shutemovc8394812017-02-24 14:57:42 -0800472 ret = get_user_pages_remote(NULL, mm, vaddr, 1,
473 FOLL_FORCE | FOLL_SPLIT, &old_page, &vma, NULL);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530474 if (ret <= 0)
475 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100476
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200477 ret = verify_opcode(old_page, vaddr, &opcode);
478 if (ret <= 0)
479 goto put_old;
480
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530481 /* We are going to replace instruction, update ref_ctr. */
482 if (!ref_ctr_updated && uprobe->ref_ctr_offset) {
483 ret = update_ref_ctr(uprobe, mm, is_register ? 1 : -1);
484 if (ret)
485 goto put_old;
486
487 ref_ctr_updated = 1;
488 }
489
Oleg Nesterov29dedee2014-05-05 16:38:18 +0200490 ret = anon_vma_prepare(vma);
491 if (ret)
492 goto put_old;
493
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530494 ret = -ENOMEM;
495 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
496 if (!new_page)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200497 goto put_old;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530498
Oleg Nesterov29dedee2014-05-05 16:38:18 +0200499 __SetPageUptodate(new_page);
Oleg Nesterov3f471072013-03-24 19:04:36 +0100500 copy_highpage(new_page, old_page);
501 copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530502
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200503 ret = __replace_page(vma, vaddr, old_page, new_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300504 put_page(new_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200505put_old:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100506 put_page(old_page);
507
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200508 if (unlikely(ret == -EAGAIN))
509 goto retry;
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530510
511 /* Revert back reference counter if instruction update failed. */
512 if (ret && is_register && ref_ctr_updated)
513 update_ref_ctr(uprobe, mm, -1);
514
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530515 return ret;
516}
517
518/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530519 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530520 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530521 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530522 * @vaddr: the virtual address to insert the opcode.
523 *
524 * For mm @mm, store the breakpoint instruction at @vaddr.
525 * Return 0 (success) or a negative errno.
526 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530527int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530528{
Ravi Bangoria6d437432018-08-09 09:48:52 +0530529 return uprobe_write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530530}
531
532/**
533 * set_orig_insn - Restore the original instruction.
534 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530535 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530536 * @vaddr: the virtual address to insert the opcode.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530537 *
538 * For mm @mm, restore the original opcode (opcode) at @vaddr.
539 * Return 0 (success) or a negative errno.
540 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100541int __weak
Oleg Nesterovded86e72012-08-08 18:07:03 +0200542set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530543{
Ravi Bangoria6d437432018-08-09 09:48:52 +0530544 return uprobe_write_opcode(auprobe, mm, vaddr,
545 *(uprobe_opcode_t *)&auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530546}
547
Oleg Nesterovf2317222015-07-21 15:40:03 +0200548static struct uprobe *get_uprobe(struct uprobe *uprobe)
549{
Elena Reshetovace59b8e2019-01-16 13:20:27 +0200550 refcount_inc(&uprobe->ref);
Oleg Nesterovf2317222015-07-21 15:40:03 +0200551 return uprobe;
552}
553
554static void put_uprobe(struct uprobe *uprobe)
555{
Elena Reshetovace59b8e2019-01-16 13:20:27 +0200556 if (refcount_dec_and_test(&uprobe->ref)) {
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530557 /*
558 * If application munmap(exec_vma) before uprobe_unregister()
559 * gets called, we don't get a chance to remove uprobe from
560 * delayed_uprobe_list from remove_breakpoint(). Do it here.
561 */
Ravi Bangoria1aed58e2018-12-05 09:04:23 +0530562 mutex_lock(&delayed_uprobe_lock);
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530563 delayed_uprobe_remove(uprobe, NULL);
Ravi Bangoria1aed58e2018-12-05 09:04:23 +0530564 mutex_unlock(&delayed_uprobe_lock);
Oleg Nesterovf2317222015-07-21 15:40:03 +0200565 kfree(uprobe);
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530566 }
Oleg Nesterovf2317222015-07-21 15:40:03 +0200567}
568
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530569static int match_uprobe(struct uprobe *l, struct uprobe *r)
570{
571 if (l->inode < r->inode)
572 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100573
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530574 if (l->inode > r->inode)
575 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530576
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100577 if (l->offset < r->offset)
578 return -1;
579
580 if (l->offset > r->offset)
581 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530582
583 return 0;
584}
585
586static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
587{
588 struct uprobe u = { .inode = inode, .offset = offset };
589 struct rb_node *n = uprobes_tree.rb_node;
590 struct uprobe *uprobe;
591 int match;
592
593 while (n) {
594 uprobe = rb_entry(n, struct uprobe, rb_node);
595 match = match_uprobe(&u, uprobe);
Oleg Nesterovf2317222015-07-21 15:40:03 +0200596 if (!match)
597 return get_uprobe(uprobe);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100598
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530599 if (match < 0)
600 n = n->rb_left;
601 else
602 n = n->rb_right;
603 }
604 return NULL;
605}
606
607/*
608 * Find a uprobe corresponding to a given inode:offset
609 * Acquires uprobes_treelock
610 */
611static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
612{
613 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530614
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200615 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530616 uprobe = __find_uprobe(inode, offset);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200617 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100618
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530619 return uprobe;
620}
621
622static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
623{
624 struct rb_node **p = &uprobes_tree.rb_node;
625 struct rb_node *parent = NULL;
626 struct uprobe *u;
627 int match;
628
629 while (*p) {
630 parent = *p;
631 u = rb_entry(parent, struct uprobe, rb_node);
632 match = match_uprobe(uprobe, u);
Oleg Nesterovf2317222015-07-21 15:40:03 +0200633 if (!match)
634 return get_uprobe(u);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530635
636 if (match < 0)
637 p = &parent->rb_left;
638 else
639 p = &parent->rb_right;
640
641 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100642
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530643 u = NULL;
644 rb_link_node(&uprobe->rb_node, parent, p);
645 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
646 /* get access + creation ref */
Elena Reshetovace59b8e2019-01-16 13:20:27 +0200647 refcount_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100648
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530649 return u;
650}
651
652/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100653 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530654 * Matching uprobe already exists in rbtree;
655 * increment (access refcount) and return the matching uprobe.
656 *
657 * No matching uprobe; insert the uprobe in rb_tree;
658 * get a double refcount (access + creation) and return NULL.
659 */
660static struct uprobe *insert_uprobe(struct uprobe *uprobe)
661{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530662 struct uprobe *u;
663
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200664 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530665 u = __insert_uprobe(uprobe);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200666 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100667
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530668 return u;
669}
670
Ravi Bangoria22bad382018-08-20 10:12:48 +0530671static void
672ref_ctr_mismatch_warn(struct uprobe *cur_uprobe, struct uprobe *uprobe)
673{
674 pr_warn("ref_ctr_offset mismatch. inode: 0x%lx offset: 0x%llx "
675 "ref_ctr_offset(old): 0x%llx ref_ctr_offset(new): 0x%llx\n",
676 uprobe->inode->i_ino, (unsigned long long) uprobe->offset,
677 (unsigned long long) cur_uprobe->ref_ctr_offset,
678 (unsigned long long) uprobe->ref_ctr_offset);
679}
680
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530681static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset,
682 loff_t ref_ctr_offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530683{
684 struct uprobe *uprobe, *cur_uprobe;
685
686 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
687 if (!uprobe)
688 return NULL;
689
Song Liu61f94202018-04-23 10:21:35 -0700690 uprobe->inode = inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530691 uprobe->offset = offset;
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530692 uprobe->ref_ctr_offset = ref_ctr_offset;
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100693 init_rwsem(&uprobe->register_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530694 init_rwsem(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530695
696 /* add to uprobes_tree, sorted on inode:offset */
697 cur_uprobe = insert_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530698 /* a uprobe exists for this inode:offset combination */
699 if (cur_uprobe) {
Ravi Bangoria22bad382018-08-20 10:12:48 +0530700 if (cur_uprobe->ref_ctr_offset != uprobe->ref_ctr_offset) {
701 ref_ctr_mismatch_warn(cur_uprobe, uprobe);
702 put_uprobe(cur_uprobe);
703 kfree(uprobe);
704 return ERR_PTR(-EINVAL);
705 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530706 kfree(uprobe);
707 uprobe = cur_uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100708 }
709
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530710 return uprobe;
711}
712
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100713static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530714{
715 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530716 uc->next = uprobe->consumers;
717 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530718 up_write(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530719}
720
721/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530722 * For uprobe @uprobe, delete the consumer @uc.
723 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530724 * or return false.
725 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530726static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530727{
728 struct uprobe_consumer **con;
729 bool ret = false;
730
731 down_write(&uprobe->consumer_rwsem);
732 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530733 if (*con == uc) {
734 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530735 ret = true;
736 break;
737 }
738 }
739 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100740
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530741 return ret;
742}
743
Oleg Nesterov2ded0982013-11-07 19:41:57 +0100744static int __copy_insn(struct address_space *mapping, struct file *filp,
745 void *insn, int nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530746{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530747 struct page *page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530748 /*
Oleg Nesterov40814f62014-05-19 20:41:36 +0200749 * Ensure that the page that has the original instruction is populated
750 * and in page-cache. If ->readpage == NULL it must be shmem_mapping(),
751 * see uprobe_register().
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530752 */
Oleg Nesterov40814f62014-05-19 20:41:36 +0200753 if (mapping->a_ops->readpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300754 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, filp);
Oleg Nesterov40814f62014-05-19 20:41:36 +0200755 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300756 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530757 if (IS_ERR(page))
758 return PTR_ERR(page);
759
Oleg Nesterov2edb7b52013-03-24 18:37:48 +0100760 copy_from_page(page, offset, insn, nbytes);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300761 put_page(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100762
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530763 return 0;
764}
765
Oleg Nesterovd4366152012-06-15 17:43:42 +0200766static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530767{
Oleg Nesterov2ded0982013-11-07 19:41:57 +0100768 struct address_space *mapping = uprobe->inode->i_mapping;
769 loff_t offs = uprobe->offset;
Oleg Nesterov803200e2013-11-09 17:58:54 +0100770 void *insn = &uprobe->arch.insn;
771 int size = sizeof(uprobe->arch.insn);
Oleg Nesterov2ded0982013-11-07 19:41:57 +0100772 int len, err = -EIO;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530773
Oleg Nesterov2ded0982013-11-07 19:41:57 +0100774 /* Copy only available bytes, -EIO if nothing was read */
775 do {
776 if (offs >= i_size_read(uprobe->inode))
777 break;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530778
Oleg Nesterov2ded0982013-11-07 19:41:57 +0100779 len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK));
780 err = __copy_insn(mapping, filp, insn, len, offs);
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200781 if (err)
Oleg Nesterov2ded0982013-11-07 19:41:57 +0100782 break;
783
784 insn += len;
785 offs += len;
786 size -= len;
787 } while (size);
788
789 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530790}
791
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200792static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
793 struct mm_struct *mm, unsigned long vaddr)
794{
795 int ret = 0;
796
Oleg Nesterov71434f22012-09-30 21:12:44 +0200797 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200798 return ret;
799
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100800 /* TODO: move this into _register, until then we abuse this sem. */
801 down_write(&uprobe->consumer_rwsem);
Oleg Nesterov71434f22012-09-30 21:12:44 +0200802 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterov4710f05f2012-09-30 20:31:41 +0200803 goto out;
804
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200805 ret = copy_insn(uprobe, file);
806 if (ret)
807 goto out;
808
809 ret = -ENOTSUPP;
Oleg Nesterov803200e2013-11-09 17:58:54 +0100810 if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200811 goto out;
812
813 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
814 if (ret)
815 goto out;
816
Oleg Nesterovf72d41f2013-11-05 19:50:39 +0100817 /* uprobe_write_opcode() assumes we don't cross page boundary */
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200818 BUG_ON((uprobe->offset & ~PAGE_MASK) +
819 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
820
Andrea Parri09d3f012018-11-22 17:10:31 +0100821 smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
Oleg Nesterov71434f22012-09-30 21:12:44 +0200822 set_bit(UPROBE_COPY_INSN, &uprobe->flags);
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200823
824 out:
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100825 up_write(&uprobe->consumer_rwsem);
Oleg Nesterov4710f05f2012-09-30 20:31:41 +0200826
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200827 return ret;
828}
829
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100830static inline bool consumer_filter(struct uprobe_consumer *uc,
831 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100832{
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100833 return !uc->filter || uc->filter(uc, ctx, mm);
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100834}
835
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100836static bool filter_chain(struct uprobe *uprobe,
837 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100838{
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100839 struct uprobe_consumer *uc;
840 bool ret = false;
841
842 down_read(&uprobe->consumer_rwsem);
843 for (uc = uprobe->consumers; uc; uc = uc->next) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100844 ret = consumer_filter(uc, ctx, mm);
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100845 if (ret)
846 break;
847 }
848 up_read(&uprobe->consumer_rwsem);
849
850 return ret;
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100851}
852
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530853static int
854install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200855 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530856{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200857 bool first_uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530858 int ret;
859
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200860 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
861 if (ret)
862 return ret;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530863
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200864 /*
865 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
866 * the task can hit this breakpoint right after __replace_page().
867 */
868 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
869 if (first_uprobe)
870 set_bit(MMF_HAS_UPROBES, &mm->flags);
871
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200872 ret = set_swbp(&uprobe->arch, mm, vaddr);
Oleg Nesterov9f68f6722012-08-19 16:15:09 +0200873 if (!ret)
874 clear_bit(MMF_RECALC_UPROBES, &mm->flags);
875 else if (first_uprobe)
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200876 clear_bit(MMF_HAS_UPROBES, &mm->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530877
878 return ret;
879}
880
Oleg Nesterov076a3652012-09-30 18:54:53 +0200881static int
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200882remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530883{
Oleg Nesterov9f68f6722012-08-19 16:15:09 +0200884 set_bit(MMF_RECALC_UPROBES, &mm->flags);
Oleg Nesterov076a3652012-09-30 18:54:53 +0200885 return set_orig_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530886}
887
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100888static inline bool uprobe_is_active(struct uprobe *uprobe)
889{
890 return !RB_EMPTY_NODE(&uprobe->rb_node);
891}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530892/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200893 * There could be threads that have already hit the breakpoint. They
894 * will recheck the current insn and restart if find_uprobe() fails.
895 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530896 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530897static void delete_uprobe(struct uprobe *uprobe)
898{
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100899 if (WARN_ON(!uprobe_is_active(uprobe)))
900 return;
901
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200902 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530903 rb_erase(&uprobe->rb_node, &uprobes_tree);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200904 spin_unlock(&uprobes_treelock);
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100905 RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530906 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530907}
908
Oleg Nesterov26872092012-06-15 17:43:33 +0200909struct map_info {
910 struct map_info *next;
911 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200912 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200913};
914
915static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530916{
Oleg Nesterov26872092012-06-15 17:43:33 +0200917 struct map_info *next = info->next;
918 kfree(info);
919 return next;
920}
921
922static struct map_info *
923build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
924{
925 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530926 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200927 struct map_info *curr = NULL;
928 struct map_info *prev = NULL;
929 struct map_info *info;
930 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100931
Oleg Nesterov26872092012-06-15 17:43:33 +0200932 again:
Davidlohr Bueso4a23717a2014-12-12 16:54:30 -0800933 i_mmap_lock_read(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700934 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530935 if (!valid_vma(vma, is_register))
936 continue;
937
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200938 if (!prev && !more) {
939 /*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800940 * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200941 * reclaim. This is optimistic, no harm done if it fails.
942 */
943 prev = kmalloc(sizeof(struct map_info),
944 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
945 if (prev)
946 prev->next = NULL;
947 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200948 if (!prev) {
949 more++;
950 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530951 }
952
Vegard Nossum388f7932017-02-27 14:30:13 -0800953 if (!mmget_not_zero(vma->vm_mm))
Oleg Nesterov26872092012-06-15 17:43:33 +0200954 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100955
Oleg Nesterov26872092012-06-15 17:43:33 +0200956 info = prev;
957 prev = prev->next;
958 info->next = curr;
959 curr = info;
960
961 info->mm = vma->vm_mm;
Oleg Nesterov57683f72012-07-29 20:22:47 +0200962 info->vaddr = offset_to_vaddr(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530963 }
Davidlohr Bueso4a23717a2014-12-12 16:54:30 -0800964 i_mmap_unlock_read(mapping);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530965
Oleg Nesterov26872092012-06-15 17:43:33 +0200966 if (!more)
967 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100968
Oleg Nesterov26872092012-06-15 17:43:33 +0200969 prev = curr;
970 while (curr) {
971 mmput(curr->mm);
972 curr = curr->next;
973 }
974
975 do {
976 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
977 if (!info) {
978 curr = ERR_PTR(-ENOMEM);
979 goto out;
980 }
981 info->next = prev;
982 prev = info;
983 } while (--more);
984
985 goto again;
986 out:
987 while (prev)
988 prev = free_map_info(prev);
989 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530990}
991
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100992static int
993register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530994{
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100995 bool is_register = !!new;
Oleg Nesterov26872092012-06-15 17:43:33 +0200996 struct map_info *info;
997 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530998
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100999 percpu_down_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +02001000 info = build_map_info(uprobe->inode->i_mapping,
1001 uprobe->offset, is_register);
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001002 if (IS_ERR(info)) {
1003 err = PTR_ERR(info);
1004 goto out;
1005 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001006
Oleg Nesterov26872092012-06-15 17:43:33 +02001007 while (info) {
1008 struct mm_struct *mm = info->mm;
1009 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001010
Oleg Nesterov076a3652012-09-30 18:54:53 +02001011 if (err && is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +02001012 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001013
Oleg Nesterov77fc4af2012-05-29 21:29:28 +02001014 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +02001015 vma = find_vma(mm, info->vaddr);
1016 if (!vma || !valid_vma(vma, is_register) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +01001017 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +02001018 goto unlock;
1019
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +02001020 if (vma->vm_start > info->vaddr ||
1021 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +02001022 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301023
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001024 if (is_register) {
1025 /* consult only the "caller", new consumer. */
Oleg Nesterovbdf86472013-02-03 19:21:12 +01001026 if (consumer_filter(new,
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +01001027 UPROBE_FILTER_REGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001028 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
1029 } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +01001030 if (!filter_chain(uprobe,
1031 UPROBE_FILTER_UNREGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001032 err |= remove_breakpoint(uprobe, mm, info->vaddr);
1033 }
Oleg Nesterov78f74112012-08-08 17:35:08 +02001034
Oleg Nesterov26872092012-06-15 17:43:33 +02001035 unlock:
1036 up_write(&mm->mmap_sem);
1037 free:
1038 mmput(mm);
1039 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301040 }
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001041 out:
1042 percpu_up_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +02001043 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301044}
1045
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301046static void
1047__uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301048{
Oleg Nesterov04aab9b2012-11-23 19:43:50 +01001049 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301050
Oleg Nesterov06d07132014-06-27 19:01:40 +02001051 if (WARN_ON(!consumer_del(uprobe, uc)))
Oleg Nesterov04aab9b2012-11-23 19:43:50 +01001052 return;
1053
Oleg Nesterovbdf86472013-02-03 19:21:12 +01001054 err = register_for_each_vma(uprobe, NULL);
Oleg Nesterovbb929282012-11-24 18:27:08 +01001055 /* TODO : cant unregister? schedule a worker thread */
1056 if (!uprobe->consumers && !err)
1057 delete_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301058}
1059
1060/*
Linus Torvalds7140ad32018-08-20 18:32:00 -07001061 * uprobe_unregister - unregister an already registered probe.
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301062 * @inode: the file in which the probe has to be removed.
1063 * @offset: offset from the start of the file.
1064 * @uc: identify which probe if multiple probes are colocated.
1065 */
1066void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
1067{
1068 struct uprobe *uprobe;
1069
1070 uprobe = find_uprobe(inode, offset);
1071 if (WARN_ON(!uprobe))
1072 return;
1073
1074 down_write(&uprobe->register_rwsem);
1075 __uprobe_unregister(uprobe, uc);
1076 up_write(&uprobe->register_rwsem);
1077 put_uprobe(uprobe);
1078}
1079EXPORT_SYMBOL_GPL(uprobe_unregister);
1080
1081/*
1082 * __uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301083 * @inode: the file in which the probe has to be placed.
1084 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +05301085 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301086 *
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301087 * Apart from the access refcount, __uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301088 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
1089 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001090 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301091 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +05301092 * refcount is released when the last @uc for the @uprobe
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301093 * unregisters. Caller of __uprobe_register() is required to keep @inode
Song Liu61f94202018-04-23 10:21:35 -07001094 * (and the containing mount) referenced.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301095 *
1096 * Return errno if it cannot successully install probes
1097 * else return 0 (success)
1098 */
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301099static int __uprobe_register(struct inode *inode, loff_t offset,
Ravi Bangoria1cc33162018-08-20 10:12:47 +05301100 loff_t ref_ctr_offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301101{
1102 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001103 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301104
Anton Arapovea024872013-04-03 18:00:31 +02001105 /* Uprobe must have at least one set consumer */
1106 if (!uc->handler && !uc->ret_handler)
1107 return -EINVAL;
1108
Oleg Nesterov40814f62014-05-19 20:41:36 +02001109 /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */
1110 if (!inode->i_mapping->a_ops->readpage && !shmem_mapping(inode->i_mapping))
Oleg Nesterov41ccba02014-05-19 20:40:54 +02001111 return -EIO;
Oleg Nesterovf0744af2012-11-21 18:01:43 +01001112 /* Racy, just to catch the obvious mistakes */
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301113 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001114 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301115
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001116 retry:
Ravi Bangoria1cc33162018-08-20 10:12:47 +05301117 uprobe = alloc_uprobe(inode, offset, ref_ctr_offset);
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001118 if (!uprobe)
1119 return -ENOMEM;
Ravi Bangoria22bad382018-08-20 10:12:48 +05301120 if (IS_ERR(uprobe))
1121 return PTR_ERR(uprobe);
1122
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001123 /*
1124 * We can race with uprobe_unregister()->delete_uprobe().
1125 * Check uprobe_is_active() and retry if it is false.
1126 */
1127 down_write(&uprobe->register_rwsem);
1128 ret = -EAGAIN;
1129 if (likely(uprobe_is_active(uprobe))) {
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301130 consumer_add(uprobe, uc);
1131 ret = register_for_each_vma(uprobe, uc);
Oleg Nesterov9a98e032012-11-23 20:15:17 +01001132 if (ret)
Oleg Nesterov04aab9b2012-11-23 19:43:50 +01001133 __uprobe_unregister(uprobe, uc);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301134 }
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001135 up_write(&uprobe->register_rwsem);
1136 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301137
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001138 if (unlikely(ret == -EAGAIN))
1139 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301140 return ret;
1141}
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301142
1143int uprobe_register(struct inode *inode, loff_t offset,
1144 struct uprobe_consumer *uc)
1145{
Ravi Bangoria1cc33162018-08-20 10:12:47 +05301146 return __uprobe_register(inode, offset, 0, uc);
Ravi Bangoria38e967a2018-08-09 09:48:51 +05301147}
Josh Stonee8440c12013-01-13 19:03:34 +01001148EXPORT_SYMBOL_GPL(uprobe_register);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301149
Ravi Bangoria1cc33162018-08-20 10:12:47 +05301150int uprobe_register_refctr(struct inode *inode, loff_t offset,
1151 loff_t ref_ctr_offset, struct uprobe_consumer *uc)
1152{
1153 return __uprobe_register(inode, offset, ref_ctr_offset, uc);
1154}
1155EXPORT_SYMBOL_GPL(uprobe_register_refctr);
1156
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301157/*
Tobias Tefke788faab2018-07-09 12:57:15 +02001158 * uprobe_apply - unregister an already registered probe.
Oleg Nesterovbdf86472013-02-03 19:21:12 +01001159 * @inode: the file in which the probe has to be removed.
1160 * @offset: offset from the start of the file.
1161 * @uc: consumer which wants to add more or remove some breakpoints
1162 * @add: add or remove the breakpoints
1163 */
1164int uprobe_apply(struct inode *inode, loff_t offset,
1165 struct uprobe_consumer *uc, bool add)
1166{
1167 struct uprobe *uprobe;
1168 struct uprobe_consumer *con;
1169 int ret = -ENOENT;
1170
1171 uprobe = find_uprobe(inode, offset);
Oleg Nesterov06d07132014-06-27 19:01:40 +02001172 if (WARN_ON(!uprobe))
Oleg Nesterovbdf86472013-02-03 19:21:12 +01001173 return ret;
1174
1175 down_write(&uprobe->register_rwsem);
1176 for (con = uprobe->consumers; con && con != uc ; con = con->next)
1177 ;
1178 if (con)
1179 ret = register_for_each_vma(uprobe, add ? uc : NULL);
1180 up_write(&uprobe->register_rwsem);
1181 put_uprobe(uprobe);
1182
1183 return ret;
1184}
1185
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001186static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
1187{
1188 struct vm_area_struct *vma;
1189 int err = 0;
1190
1191 down_read(&mm->mmap_sem);
1192 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1193 unsigned long vaddr;
1194 loff_t offset;
1195
1196 if (!valid_vma(vma, false) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +01001197 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001198 continue;
1199
1200 offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1201 if (uprobe->offset < offset ||
1202 uprobe->offset >= offset + vma->vm_end - vma->vm_start)
1203 continue;
1204
1205 vaddr = offset_to_vaddr(vma, uprobe->offset);
1206 err |= remove_breakpoint(uprobe, mm, vaddr);
1207 }
1208 up_read(&mm->mmap_sem);
1209
1210 return err;
1211}
1212
Oleg Nesterov891c3972012-07-29 20:22:40 +02001213static struct rb_node *
1214find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301215{
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301216 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301217
1218 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +02001219 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001220
Oleg Nesterov891c3972012-07-29 20:22:40 +02001221 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301222 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +02001223 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301224 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +02001225 } else {
1226 if (max < u->offset)
1227 n = n->rb_left;
1228 else if (min > u->offset)
1229 n = n->rb_right;
1230 else
1231 break;
1232 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301233 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001234
Oleg Nesterov891c3972012-07-29 20:22:40 +02001235 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301236}
1237
1238/*
Oleg Nesterov891c3972012-07-29 20:22:40 +02001239 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301240 */
Oleg Nesterov891c3972012-07-29 20:22:40 +02001241static void build_probe_list(struct inode *inode,
1242 struct vm_area_struct *vma,
1243 unsigned long start, unsigned long end,
1244 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301245{
Oleg Nesterov891c3972012-07-29 20:22:40 +02001246 loff_t min, max;
Oleg Nesterov891c3972012-07-29 20:22:40 +02001247 struct rb_node *n, *t;
1248 struct uprobe *u;
1249
1250 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001251 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001252 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301253
Oleg Nesterov6f47caa2012-08-18 17:01:57 +02001254 spin_lock(&uprobes_treelock);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001255 n = find_node_in_range(inode, min, max);
1256 if (n) {
1257 for (t = n; t; t = rb_prev(t)) {
1258 u = rb_entry(t, struct uprobe, rb_node);
1259 if (u->inode != inode || u->offset < min)
1260 break;
1261 list_add(&u->pending_list, head);
Oleg Nesterovf2317222015-07-21 15:40:03 +02001262 get_uprobe(u);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001263 }
1264 for (t = n; (t = rb_next(t)); ) {
1265 u = rb_entry(t, struct uprobe, rb_node);
1266 if (u->inode != inode || u->offset > max)
1267 break;
1268 list_add(&u->pending_list, head);
Oleg Nesterovf2317222015-07-21 15:40:03 +02001269 get_uprobe(u);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001270 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301271 }
Oleg Nesterov6f47caa2012-08-18 17:01:57 +02001272 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301273}
1274
Ravi Bangoria1cc33162018-08-20 10:12:47 +05301275/* @vma contains reference counter, not the probed instruction. */
1276static int delayed_ref_ctr_inc(struct vm_area_struct *vma)
1277{
1278 struct list_head *pos, *q;
1279 struct delayed_uprobe *du;
1280 unsigned long vaddr;
1281 int ret = 0, err = 0;
1282
1283 mutex_lock(&delayed_uprobe_lock);
1284 list_for_each_safe(pos, q, &delayed_uprobe_list) {
1285 du = list_entry(pos, struct delayed_uprobe, list);
1286
1287 if (du->mm != vma->vm_mm ||
1288 !valid_ref_ctr_vma(du->uprobe, vma))
1289 continue;
1290
1291 vaddr = offset_to_vaddr(vma, du->uprobe->ref_ctr_offset);
1292 ret = __update_ref_ctr(vma->vm_mm, vaddr, 1);
1293 if (ret) {
1294 update_ref_ctr_warn(du->uprobe, vma->vm_mm, 1);
1295 if (!err)
1296 err = ret;
1297 }
1298 delayed_uprobe_delete(du);
1299 }
1300 mutex_unlock(&delayed_uprobe_lock);
1301 return err;
1302}
1303
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301304/*
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001305 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301306 *
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001307 * Currently we ignore all errors and always return 0, the callers
1308 * can't handle the failure anyway.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301309 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001310int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301311{
1312 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +02001313 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301314 struct inode *inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301315
Ravi Bangoria1cc33162018-08-20 10:12:47 +05301316 if (no_uprobe_events())
1317 return 0;
1318
1319 if (vma->vm_file &&
1320 (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE &&
1321 test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags))
1322 delayed_ref_ctr_inc(vma);
1323
1324 if (!valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001325 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301326
Oleg Nesterovf2817692013-03-17 18:54:44 +01001327 inode = file_inode(vma->vm_file);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301328 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001329 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301330
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301331 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +02001332 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001333 /*
1334 * We can race with uprobe_unregister(), this uprobe can be already
1335 * removed. But in this case filter_chain() must return false, all
1336 * consumers have gone away.
1337 */
Oleg Nesterov665605a2012-07-29 20:22:29 +02001338 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001339 if (!fatal_signal_pending(current) &&
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +01001340 filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
Oleg Nesterov57683f72012-07-29 20:22:47 +02001341 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001342 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301343 }
1344 put_uprobe(uprobe);
1345 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301346 mutex_unlock(uprobes_mmap_hash(inode));
1347
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001348 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301349}
1350
Oleg Nesterov9f68f6722012-08-19 16:15:09 +02001351static bool
1352vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1353{
1354 loff_t min, max;
1355 struct inode *inode;
1356 struct rb_node *n;
1357
Oleg Nesterovf2817692013-03-17 18:54:44 +01001358 inode = file_inode(vma->vm_file);
Oleg Nesterov9f68f6722012-08-19 16:15:09 +02001359
1360 min = vaddr_to_offset(vma, start);
1361 max = min + (end - start) - 1;
1362
1363 spin_lock(&uprobes_treelock);
1364 n = find_node_in_range(inode, min, max);
1365 spin_unlock(&uprobes_treelock);
1366
1367 return !!n;
1368}
1369
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301370/*
1371 * Called in context of a munmap of a vma.
1372 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301373void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301374{
Oleg Nesterov441f1eb72012-11-25 19:54:29 +01001375 if (no_uprobe_events() || !valid_vma(vma, false))
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301376 return;
1377
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001378 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1379 return;
1380
Oleg Nesterov9f68f6722012-08-19 16:15:09 +02001381 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1382 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001383 return;
1384
Oleg Nesterov9f68f6722012-08-19 16:15:09 +02001385 if (vma_has_uprobes(vma, start, end))
1386 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301387}
1388
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301389/* Slot allocation for XOL */
Oleg Nesterov6441ec82013-10-13 21:18:35 +02001390static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301391{
Oleg Nesterov704bde32015-07-21 15:40:33 +02001392 struct vm_area_struct *vma;
1393 int ret;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301394
Michal Hocko598fdc12016-05-23 16:26:08 -07001395 if (down_write_killable(&mm->mmap_sem))
1396 return -EINTR;
1397
Oleg Nesterov704bde32015-07-21 15:40:33 +02001398 if (mm->uprobes_state.xol_area) {
1399 ret = -EALREADY;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301400 goto fail;
Oleg Nesterov704bde32015-07-21 15:40:33 +02001401 }
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301402
Oleg Nesterovaf0d95a2013-10-13 21:18:38 +02001403 if (!area->vaddr) {
1404 /* Try to map as high as possible, this is only a hint. */
1405 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
1406 PAGE_SIZE, 0, 0);
1407 if (area->vaddr & ~PAGE_MASK) {
1408 ret = area->vaddr;
1409 goto fail;
1410 }
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301411 }
1412
Oleg Nesterov704bde32015-07-21 15:40:33 +02001413 vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1414 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
1415 &area->xol_mapping);
1416 if (IS_ERR(vma)) {
1417 ret = PTR_ERR(vma);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301418 goto fail;
Oleg Nesterov704bde32015-07-21 15:40:33 +02001419 }
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301420
Oleg Nesterov704bde32015-07-21 15:40:33 +02001421 ret = 0;
Paul E. McKenney5c6338b2017-10-09 11:08:53 -07001422 /* pairs with get_xol_area() */
1423 smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001424 fail:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301425 up_write(&mm->mmap_sem);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301426
1427 return ret;
1428}
1429
Oleg Nesterovaf0d95a2013-10-13 21:18:38 +02001430static struct xol_area *__create_xol_area(unsigned long vaddr)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301431{
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001432 struct mm_struct *mm = current->mm;
Anton Arapove78aebf2013-04-03 18:00:32 +02001433 uprobe_opcode_t insn = UPROBE_SWBP_INSN;
Oleg Nesterov6441ec82013-10-13 21:18:35 +02001434 struct xol_area *area;
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001435
Oleg Nesterovaf0d95a2013-10-13 21:18:38 +02001436 area = kmalloc(sizeof(*area), GFP_KERNEL);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301437 if (unlikely(!area))
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001438 goto out;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301439
Kees Cook6396bb22018-06-12 14:03:40 -07001440 area->bitmap = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long),
1441 GFP_KERNEL);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301442 if (!area->bitmap)
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001443 goto free_area;
1444
Oleg Nesterov704bde32015-07-21 15:40:33 +02001445 area->xol_mapping.name = "[uprobes]";
Oleg Nesterov869ae762016-02-27 23:11:28 +01001446 area->xol_mapping.fault = NULL;
Oleg Nesterov704bde32015-07-21 15:40:33 +02001447 area->xol_mapping.pages = area->pages;
Oleg Nesterovf58bea22015-07-21 15:40:31 +02001448 area->pages[0] = alloc_page(GFP_HIGHUSER);
1449 if (!area->pages[0])
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001450 goto free_bitmap;
Oleg Nesterovf58bea22015-07-21 15:40:31 +02001451 area->pages[1] = NULL;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301452
Oleg Nesterovaf0d95a2013-10-13 21:18:38 +02001453 area->vaddr = vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301454 init_waitqueue_head(&area->wq);
Oleg Nesterov6441ec82013-10-13 21:18:35 +02001455 /* Reserve the 1st slot for get_trampoline_vaddr() */
1456 set_bit(0, area->bitmap);
1457 atomic_set(&area->slot_count, 1);
Marcin Nowakowski297e7652016-12-13 11:40:57 +01001458 arch_uprobe_copy_ixol(area->pages[0], 0, &insn, UPROBE_SWBP_INSN_SIZE);
Anton Arapove78aebf2013-04-03 18:00:32 +02001459
Oleg Nesterov6441ec82013-10-13 21:18:35 +02001460 if (!xol_add_vma(mm, area))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301461 return area;
1462
Oleg Nesterovf58bea22015-07-21 15:40:31 +02001463 __free_page(area->pages[0]);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001464 free_bitmap:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301465 kfree(area->bitmap);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001466 free_area:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301467 kfree(area);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001468 out:
Oleg Nesterov6441ec82013-10-13 21:18:35 +02001469 return NULL;
1470}
1471
1472/*
1473 * get_xol_area - Allocate process's xol_area if necessary.
1474 * This area will be used for storing instructions for execution out of line.
1475 *
1476 * Returns the allocated area or NULL.
1477 */
1478static struct xol_area *get_xol_area(void)
1479{
1480 struct mm_struct *mm = current->mm;
1481 struct xol_area *area;
1482
1483 if (!mm->uprobes_state.xol_area)
Oleg Nesterovaf0d95a2013-10-13 21:18:38 +02001484 __create_xol_area(0);
Oleg Nesterov6441ec82013-10-13 21:18:35 +02001485
Paul E. McKenney5c6338b2017-10-09 11:08:53 -07001486 /* Pairs with xol_add_vma() smp_store_release() */
1487 area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001488 return area;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301489}
1490
1491/*
1492 * uprobe_clear_state - Free the area allocated for slots.
1493 */
1494void uprobe_clear_state(struct mm_struct *mm)
1495{
1496 struct xol_area *area = mm->uprobes_state.xol_area;
1497
Ravi Bangoria1cc33162018-08-20 10:12:47 +05301498 mutex_lock(&delayed_uprobe_lock);
1499 delayed_uprobe_remove(NULL, mm);
1500 mutex_unlock(&delayed_uprobe_lock);
1501
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301502 if (!area)
1503 return;
1504
Oleg Nesterovf58bea22015-07-21 15:40:31 +02001505 put_page(area->pages[0]);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301506 kfree(area->bitmap);
1507 kfree(area);
1508}
1509
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001510void uprobe_start_dup_mmap(void)
1511{
1512 percpu_down_read(&dup_mmap_sem);
1513}
1514
1515void uprobe_end_dup_mmap(void)
1516{
1517 percpu_up_read(&dup_mmap_sem);
1518}
1519
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001520void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1521{
Oleg Nesterov9f68f6722012-08-19 16:15:09 +02001522 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001523 set_bit(MMF_HAS_UPROBES, &newmm->flags);
Oleg Nesterov9f68f6722012-08-19 16:15:09 +02001524 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1525 set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1526 }
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001527}
1528
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301529/*
1530 * - search for a free slot.
1531 */
1532static unsigned long xol_take_insn_slot(struct xol_area *area)
1533{
1534 unsigned long slot_addr;
1535 int slot_nr;
1536
1537 do {
1538 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1539 if (slot_nr < UINSNS_PER_PAGE) {
1540 if (!test_and_set_bit(slot_nr, area->bitmap))
1541 break;
1542
1543 slot_nr = UINSNS_PER_PAGE;
1544 continue;
1545 }
1546 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1547 } while (slot_nr >= UINSNS_PER_PAGE);
1548
1549 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1550 atomic_inc(&area->slot_count);
1551
1552 return slot_addr;
1553}
1554
1555/*
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001556 * xol_get_insn_slot - allocate a slot for xol.
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301557 * Returns the allocated slot address or 0.
1558 */
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001559static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301560{
1561 struct xol_area *area;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001562 unsigned long xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301563
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001564 area = get_xol_area();
1565 if (!area)
1566 return 0;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301567
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001568 xol_vaddr = xol_take_insn_slot(area);
1569 if (unlikely(!xol_vaddr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301570 return 0;
1571
Oleg Nesterovf58bea22015-07-21 15:40:31 +02001572 arch_uprobe_copy_ixol(area->pages[0], xol_vaddr,
Victor Kamensky72e6ae22014-04-29 04:20:52 +01001573 &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301574
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001575 return xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301576}
1577
1578/*
1579 * xol_free_insn_slot - If slot was earlier allocated by
1580 * @xol_get_insn_slot(), make the slot available for
1581 * subsequent requests.
1582 */
1583static void xol_free_insn_slot(struct task_struct *tsk)
1584{
1585 struct xol_area *area;
1586 unsigned long vma_end;
1587 unsigned long slot_addr;
1588
1589 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1590 return;
1591
1592 slot_addr = tsk->utask->xol_vaddr;
Oleg Nesterovaf4355e2012-12-31 18:37:11 +01001593 if (unlikely(!slot_addr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301594 return;
1595
1596 area = tsk->mm->uprobes_state.xol_area;
1597 vma_end = area->vaddr + PAGE_SIZE;
1598 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1599 unsigned long offset;
1600 int slot_nr;
1601
1602 offset = slot_addr - area->vaddr;
1603 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1604 if (slot_nr >= UINSNS_PER_PAGE)
1605 return;
1606
1607 clear_bit(slot_nr, area->bitmap);
1608 atomic_dec(&area->slot_count);
Oleg Nesterov2a742ce2015-07-21 15:40:36 +02001609 smp_mb__after_atomic(); /* pairs with prepare_to_wait() */
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301610 if (waitqueue_active(&area->wq))
1611 wake_up(&area->wq);
1612
1613 tsk->utask->xol_vaddr = 0;
1614 }
1615}
1616
Victor Kamensky72e6ae22014-04-29 04:20:52 +01001617void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
1618 void *src, unsigned long len)
1619{
1620 /* Initialize the slot */
1621 copy_to_page(page, vaddr, src, len);
1622
1623 /*
1624 * We probably need flush_icache_user_range() but it needs vma.
1625 * This should work on most of architectures by default. If
1626 * architecture needs to do something different it can define
1627 * its own version of the function.
1628 */
1629 flush_dcache_page(page);
1630}
1631
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301632/**
1633 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1634 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1635 * instruction.
1636 * Return the address of the breakpoint instruction.
1637 */
1638unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1639{
1640 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1641}
1642
Oleg Nesterovb02ef202014-05-12 18:24:45 +02001643unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
1644{
1645 struct uprobe_task *utask = current->utask;
1646
1647 if (unlikely(utask && utask->active_uprobe))
1648 return utask->vaddr;
1649
1650 return instruction_pointer(regs);
1651}
1652
Oleg Nesterov2bb5e842015-07-21 15:40:06 +02001653static struct return_instance *free_ret_instance(struct return_instance *ri)
1654{
1655 struct return_instance *next = ri->next;
1656 put_uprobe(ri->uprobe);
1657 kfree(ri);
1658 return next;
1659}
1660
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301661/*
1662 * Called with no locks held.
Tobias Tefke788faab2018-07-09 12:57:15 +02001663 * Called in context of an exiting or an exec-ing thread.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301664 */
1665void uprobe_free_utask(struct task_struct *t)
1666{
1667 struct uprobe_task *utask = t->utask;
Oleg Nesterov2bb5e842015-07-21 15:40:06 +02001668 struct return_instance *ri;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301669
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301670 if (!utask)
1671 return;
1672
1673 if (utask->active_uprobe)
1674 put_uprobe(utask->active_uprobe);
1675
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001676 ri = utask->return_instances;
Oleg Nesterov2bb5e842015-07-21 15:40:06 +02001677 while (ri)
1678 ri = free_ret_instance(ri);
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001679
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301680 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301681 kfree(utask);
1682 t->utask = NULL;
1683}
1684
1685/*
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001686 * Allocate a uprobe_task object for the task if if necessary.
1687 * Called when the thread hits a breakpoint.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301688 *
1689 * Returns:
1690 * - pointer to new uprobe_task on success
1691 * - NULL otherwise
1692 */
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001693static struct uprobe_task *get_utask(void)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301694{
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001695 if (!current->utask)
1696 current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
1697 return current->utask;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301698}
1699
Oleg Nesterov248d3a72013-10-13 21:18:41 +02001700static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
1701{
1702 struct uprobe_task *n_utask;
1703 struct return_instance **p, *o, *n;
1704
1705 n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
1706 if (!n_utask)
1707 return -ENOMEM;
1708 t->utask = n_utask;
1709
1710 p = &n_utask->return_instances;
1711 for (o = o_utask->return_instances; o; o = o->next) {
1712 n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
1713 if (!n)
1714 return -ENOMEM;
1715
1716 *n = *o;
Oleg Nesterovf2317222015-07-21 15:40:03 +02001717 get_uprobe(n->uprobe);
Oleg Nesterov248d3a72013-10-13 21:18:41 +02001718 n->next = NULL;
1719
1720 *p = n;
1721 p = &n->next;
1722 n_utask->depth++;
1723 }
1724
1725 return 0;
1726}
1727
1728static void uprobe_warn(struct task_struct *t, const char *msg)
1729{
1730 pr_warn("uprobe: %s:%d failed to %s\n",
1731 current->comm, current->pid, msg);
1732}
1733
Oleg Nesterovaa59c532013-10-13 21:18:44 +02001734static void dup_xol_work(struct callback_head *work)
1735{
Oleg Nesterovaa59c532013-10-13 21:18:44 +02001736 if (current->flags & PF_EXITING)
1737 return;
1738
Michal Hocko598fdc12016-05-23 16:26:08 -07001739 if (!__create_xol_area(current->utask->dup_xol_addr) &&
1740 !fatal_signal_pending(current))
Oleg Nesterovaa59c532013-10-13 21:18:44 +02001741 uprobe_warn(current, "dup xol area");
1742}
1743
Anton Arapove78aebf2013-04-03 18:00:32 +02001744/*
Oleg Nesterovb68e0742013-10-13 21:18:31 +02001745 * Called in context of a new clone/fork from copy_process.
1746 */
Oleg Nesterov3ab67962013-10-16 19:39:37 +02001747void uprobe_copy_process(struct task_struct *t, unsigned long flags)
Oleg Nesterovb68e0742013-10-13 21:18:31 +02001748{
Oleg Nesterov248d3a72013-10-13 21:18:41 +02001749 struct uprobe_task *utask = current->utask;
1750 struct mm_struct *mm = current->mm;
Oleg Nesterovaa59c532013-10-13 21:18:44 +02001751 struct xol_area *area;
Oleg Nesterov248d3a72013-10-13 21:18:41 +02001752
Oleg Nesterovb68e0742013-10-13 21:18:31 +02001753 t->utask = NULL;
Oleg Nesterov248d3a72013-10-13 21:18:41 +02001754
Oleg Nesterov3ab67962013-10-16 19:39:37 +02001755 if (!utask || !utask->return_instances)
1756 return;
1757
1758 if (mm == t->mm && !(flags & CLONE_VFORK))
Oleg Nesterov248d3a72013-10-13 21:18:41 +02001759 return;
1760
1761 if (dup_utask(t, utask))
1762 return uprobe_warn(t, "dup ret instances");
Oleg Nesterovaa59c532013-10-13 21:18:44 +02001763
1764 /* The task can fork() after dup_xol_work() fails */
1765 area = mm->uprobes_state.xol_area;
1766 if (!area)
1767 return uprobe_warn(t, "dup xol area");
1768
Oleg Nesterov3ab67962013-10-16 19:39:37 +02001769 if (mm == t->mm)
1770 return;
1771
Oleg Nesterov32473432013-11-08 18:52:21 +01001772 t->utask->dup_xol_addr = area->vaddr;
1773 init_task_work(&t->utask->dup_xol_work, dup_xol_work);
1774 task_work_add(t, &t->utask->dup_xol_work, true);
Oleg Nesterovb68e0742013-10-13 21:18:31 +02001775}
1776
1777/*
Anton Arapove78aebf2013-04-03 18:00:32 +02001778 * Current area->vaddr notion assume the trampoline address is always
1779 * equal area->vaddr.
1780 *
1781 * Returns -1 in case the xol_area is not allocated.
1782 */
1783static unsigned long get_trampoline_vaddr(void)
1784{
1785 struct xol_area *area;
1786 unsigned long trampoline_vaddr = -1;
1787
Paul E. McKenney5c6338b2017-10-09 11:08:53 -07001788 /* Pairs with xol_add_vma() smp_store_release() */
1789 area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */
Anton Arapove78aebf2013-04-03 18:00:32 +02001790 if (area)
1791 trampoline_vaddr = area->vaddr;
1792
1793 return trampoline_vaddr;
1794}
1795
Oleg Nesterovdb087ef2015-07-21 15:40:28 +02001796static void cleanup_return_instances(struct uprobe_task *utask, bool chained,
1797 struct pt_regs *regs)
Oleg Nesterova5b7e1a2015-07-21 15:40:23 +02001798{
1799 struct return_instance *ri = utask->return_instances;
Oleg Nesterovdb087ef2015-07-21 15:40:28 +02001800 enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL;
Oleg Nesterov86dcb702015-07-21 15:40:26 +02001801
1802 while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) {
Oleg Nesterova5b7e1a2015-07-21 15:40:23 +02001803 ri = free_ret_instance(ri);
1804 utask->depth--;
1805 }
1806 utask->return_instances = ri;
1807}
1808
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001809static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
1810{
1811 struct return_instance *ri;
1812 struct uprobe_task *utask;
1813 unsigned long orig_ret_vaddr, trampoline_vaddr;
Oleg Nesterovdb087ef2015-07-21 15:40:28 +02001814 bool chained;
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001815
1816 if (!get_xol_area())
1817 return;
1818
1819 utask = get_utask();
1820 if (!utask)
1821 return;
1822
Anton Arapovded49c52013-04-03 18:00:37 +02001823 if (utask->depth >= MAX_URETPROBE_DEPTH) {
1824 printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
1825 " nestedness limit pid/tgid=%d/%d\n",
1826 current->pid, current->tgid);
1827 return;
1828 }
1829
Oleg Nesterov6c58d0e2015-07-21 15:40:10 +02001830 ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001831 if (!ri)
Oleg Nesterov6c58d0e2015-07-21 15:40:10 +02001832 return;
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001833
1834 trampoline_vaddr = get_trampoline_vaddr();
1835 orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
1836 if (orig_ret_vaddr == -1)
1837 goto fail;
1838
Oleg Nesterova5b7e1a2015-07-21 15:40:23 +02001839 /* drop the entries invalidated by longjmp() */
Oleg Nesterovdb087ef2015-07-21 15:40:28 +02001840 chained = (orig_ret_vaddr == trampoline_vaddr);
1841 cleanup_return_instances(utask, chained, regs);
Oleg Nesterova5b7e1a2015-07-21 15:40:23 +02001842
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001843 /*
1844 * We don't want to keep trampoline address in stack, rather keep the
1845 * original return address of first caller thru all the consequent
1846 * instances. This also makes breakpoint unwrapping easier.
1847 */
Oleg Nesterovdb087ef2015-07-21 15:40:28 +02001848 if (chained) {
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001849 if (!utask->return_instances) {
1850 /*
1851 * This situation is not possible. Likely we have an
1852 * attack from user-space.
1853 */
Oleg Nesterov6c58d0e2015-07-21 15:40:10 +02001854 uprobe_warn(current, "handle tail call");
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001855 goto fail;
1856 }
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001857 orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
1858 }
1859
Oleg Nesterovf2317222015-07-21 15:40:03 +02001860 ri->uprobe = get_uprobe(uprobe);
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001861 ri->func = instruction_pointer(regs);
Oleg Nesterov7b868e42015-07-21 15:40:18 +02001862 ri->stack = user_stack_pointer(regs);
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001863 ri->orig_ret_vaddr = orig_ret_vaddr;
1864 ri->chained = chained;
1865
Anton Arapovded49c52013-04-03 18:00:37 +02001866 utask->depth++;
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001867 ri->next = utask->return_instances;
1868 utask->return_instances = ri;
1869
1870 return;
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001871 fail:
1872 kfree(ri);
1873}
1874
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301875/* Prepare to single-step probed instruction out of line. */
1876static int
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001877pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301878{
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001879 struct uprobe_task *utask;
1880 unsigned long xol_vaddr;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001881 int err;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301882
Oleg Nesterov608e7422012-12-31 18:20:42 +01001883 utask = get_utask();
1884 if (!utask)
1885 return -ENOMEM;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001886
1887 xol_vaddr = xol_get_insn_slot(uprobe);
1888 if (!xol_vaddr)
1889 return -ENOMEM;
1890
1891 utask->xol_vaddr = xol_vaddr;
1892 utask->vaddr = bp_vaddr;
1893
Oleg Nesterovaba51022012-12-31 18:12:48 +01001894 err = arch_uprobe_pre_xol(&uprobe->arch, regs);
1895 if (unlikely(err)) {
1896 xol_free_insn_slot(current);
1897 return err;
1898 }
1899
Oleg Nesterov608e7422012-12-31 18:20:42 +01001900 utask->active_uprobe = uprobe;
1901 utask->state = UTASK_SSTEP;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001902 return 0;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301903}
1904
1905/*
1906 * If we are singlestepping, then ensure this thread is not connected to
1907 * non-fatal signals until completion of singlestep. When xol insn itself
1908 * triggers the signal, restart the original insn even if the task is
1909 * already SIGKILL'ed (since coredump should report the correct ip). This
1910 * is even more important if the task has a handler for SIGSEGV/etc, The
1911 * _same_ instruction should be repeated again after return from the signal
1912 * handler, and SSTEP can never finish in this case.
1913 */
1914bool uprobe_deny_signal(void)
1915{
1916 struct task_struct *t = current;
1917 struct uprobe_task *utask = t->utask;
1918
1919 if (likely(!utask || !utask->active_uprobe))
1920 return false;
1921
1922 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1923
1924 if (signal_pending(t)) {
1925 spin_lock_irq(&t->sighand->siglock);
1926 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1927 spin_unlock_irq(&t->sighand->siglock);
1928
1929 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1930 utask->state = UTASK_SSTEP_TRAPPED;
1931 set_tsk_thread_flag(t, TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301932 }
1933 }
1934
1935 return true;
1936}
1937
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001938static void mmf_recalc_uprobes(struct mm_struct *mm)
1939{
1940 struct vm_area_struct *vma;
1941
1942 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1943 if (!valid_vma(vma, false))
1944 continue;
1945 /*
1946 * This is not strictly accurate, we can race with
1947 * uprobe_unregister() and see the already removed
1948 * uprobe if delete_uprobe() was not yet called.
Oleg Nesterov63633cb2012-11-22 18:30:15 +01001949 * Or this uprobe can be filtered out.
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001950 */
1951 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
1952 return;
1953 }
1954
1955 clear_bit(MMF_HAS_UPROBES, &mm->flags);
1956}
1957
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301958static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001959{
1960 struct page *page;
1961 uprobe_opcode_t opcode;
1962 int result;
1963
1964 pagefault_disable();
Linus Torvaldsbd28b142016-05-22 17:21:27 -07001965 result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001966 pagefault_enable();
1967
1968 if (likely(result == 0))
1969 goto out;
1970
Dave Hansen1e987792016-02-12 13:01:54 -08001971 /*
1972 * The NULL 'tsk' here ensures that any faults that occur here
1973 * will not be accounted to the task. 'mm' *is* current->mm,
1974 * but we treat this as a 'remote' access since it is
1975 * essentially a kernel access to the memory.
1976 */
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001977 result = get_user_pages_remote(NULL, mm, vaddr, 1, FOLL_FORCE, &page,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001978 NULL, NULL);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001979 if (result < 0)
1980 return result;
1981
Oleg Nesterovab0d8052013-03-24 18:24:37 +01001982 copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001983 put_page(page);
1984 out:
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301985 /* This needs to return true for any variant of the trap insn */
1986 return is_trap_insn(&opcode);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001987}
1988
Oleg Nesterovd790d342012-05-29 21:29:14 +02001989static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001990{
1991 struct mm_struct *mm = current->mm;
1992 struct uprobe *uprobe = NULL;
1993 struct vm_area_struct *vma;
1994
1995 down_read(&mm->mmap_sem);
1996 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001997 if (vma && vma->vm_start <= bp_vaddr) {
1998 if (valid_vma(vma, false)) {
Oleg Nesterovf2817692013-03-17 18:54:44 +01001999 struct inode *inode = file_inode(vma->vm_file);
Oleg Nesterovcb113b42012-07-29 20:22:42 +02002000 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02002001
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02002002 uprobe = find_uprobe(inode, offset);
2003 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02002004
2005 if (!uprobe)
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05302006 *is_swbp = is_trap_at_addr(mm, bp_vaddr);
Oleg Nesterovd790d342012-05-29 21:29:14 +02002007 } else {
2008 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02002009 }
Oleg Nesterov499a4f32012-08-19 17:41:34 +02002010
2011 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
2012 mmf_recalc_uprobes(mm);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02002013 up_read(&mm->mmap_sem);
2014
2015 return uprobe;
2016}
2017
Oleg Nesterovda1816b2012-12-29 17:49:11 +01002018static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
2019{
2020 struct uprobe_consumer *uc;
2021 int remove = UPROBE_HANDLER_REMOVE;
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02002022 bool need_prep = false; /* prepare return uprobe, when needed */
Oleg Nesterovda1816b2012-12-29 17:49:11 +01002023
2024 down_read(&uprobe->register_rwsem);
2025 for (uc = uprobe->consumers; uc; uc = uc->next) {
Anton Arapovea024872013-04-03 18:00:31 +02002026 int rc = 0;
Oleg Nesterovda1816b2012-12-29 17:49:11 +01002027
Anton Arapovea024872013-04-03 18:00:31 +02002028 if (uc->handler) {
2029 rc = uc->handler(uc, regs);
2030 WARN(rc & ~UPROBE_HANDLER_MASK,
2031 "bad rc=0x%x from %pf()\n", rc, uc->handler);
2032 }
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02002033
2034 if (uc->ret_handler)
2035 need_prep = true;
2036
Oleg Nesterovda1816b2012-12-29 17:49:11 +01002037 remove &= rc;
2038 }
2039
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02002040 if (need_prep && !remove)
2041 prepare_uretprobe(uprobe, regs); /* put bp at return */
2042
Oleg Nesterovda1816b2012-12-29 17:49:11 +01002043 if (remove && uprobe->consumers) {
2044 WARN_ON(!uprobe_is_active(uprobe));
2045 unapply_uprobe(uprobe, current->mm);
2046 }
2047 up_read(&uprobe->register_rwsem);
2048}
2049
Anton Arapovfec88982013-04-03 18:00:36 +02002050static void
2051handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
2052{
2053 struct uprobe *uprobe = ri->uprobe;
2054 struct uprobe_consumer *uc;
2055
2056 down_read(&uprobe->register_rwsem);
2057 for (uc = uprobe->consumers; uc; uc = uc->next) {
2058 if (uc->ret_handler)
2059 uc->ret_handler(uc, ri->func, regs);
2060 }
2061 up_read(&uprobe->register_rwsem);
2062}
2063
Oleg Nesterova83cfeb2015-07-21 15:40:13 +02002064static struct return_instance *find_next_ret_chain(struct return_instance *ri)
2065{
2066 bool chained;
2067
2068 do {
2069 chained = ri->chained;
2070 ri = ri->next; /* can't be NULL if chained */
2071 } while (chained);
2072
2073 return ri;
2074}
2075
Oleg Nesterov0b5256c2015-07-21 15:40:08 +02002076static void handle_trampoline(struct pt_regs *regs)
Anton Arapovfec88982013-04-03 18:00:36 +02002077{
2078 struct uprobe_task *utask;
Oleg Nesterova83cfeb2015-07-21 15:40:13 +02002079 struct return_instance *ri, *next;
Oleg Nesterov5eeb50d2015-07-21 15:40:21 +02002080 bool valid;
Anton Arapovfec88982013-04-03 18:00:36 +02002081
2082 utask = current->utask;
2083 if (!utask)
Oleg Nesterov0b5256c2015-07-21 15:40:08 +02002084 goto sigill;
Anton Arapovfec88982013-04-03 18:00:36 +02002085
2086 ri = utask->return_instances;
2087 if (!ri)
Oleg Nesterov0b5256c2015-07-21 15:40:08 +02002088 goto sigill;
Anton Arapovfec88982013-04-03 18:00:36 +02002089
Oleg Nesterova83cfeb2015-07-21 15:40:13 +02002090 do {
Oleg Nesterov5eeb50d2015-07-21 15:40:21 +02002091 /*
2092 * We should throw out the frames invalidated by longjmp().
2093 * If this chain is valid, then the next one should be alive
2094 * or NULL; the latter case means that nobody but ri->func
2095 * could hit this trampoline on return. TODO: sigaltstack().
2096 */
2097 next = find_next_ret_chain(ri);
Oleg Nesterov86dcb702015-07-21 15:40:26 +02002098 valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs);
Oleg Nesterov5eeb50d2015-07-21 15:40:21 +02002099
2100 instruction_pointer_set(regs, ri->orig_ret_vaddr);
2101 do {
2102 if (valid)
2103 handle_uretprobe_chain(ri, regs);
2104 ri = free_ret_instance(ri);
2105 utask->depth--;
2106 } while (ri != next);
2107 } while (!valid);
Anton Arapovfec88982013-04-03 18:00:36 +02002108
2109 utask->return_instances = ri;
Oleg Nesterov0b5256c2015-07-21 15:40:08 +02002110 return;
Anton Arapovfec88982013-04-03 18:00:36 +02002111
Oleg Nesterov0b5256c2015-07-21 15:40:08 +02002112 sigill:
2113 uprobe_warn(current, "handle uretprobe, sending SIGILL.");
Eric W. Biederman55a32352018-07-19 20:33:53 -05002114 force_sig(SIGILL, current);
Oleg Nesterov0b5256c2015-07-21 15:40:08 +02002115
Anton Arapovfec88982013-04-03 18:00:36 +02002116}
2117
David A. Long6fe50a22014-02-03 14:25:49 -05002118bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs)
2119{
2120 return false;
2121}
2122
Oleg Nesterov86dcb702015-07-21 15:40:26 +02002123bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
2124 struct pt_regs *regs)
Oleg Nesterov97da8972015-07-21 15:40:16 +02002125{
2126 return true;
2127}
2128
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302129/*
2130 * Run handler and ask thread to singlestep.
2131 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
2132 */
2133static void handle_swbp(struct pt_regs *regs)
2134{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302135 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302136 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02002137 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302138
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302139 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterov0b5256c2015-07-21 15:40:08 +02002140 if (bp_vaddr == get_trampoline_vaddr())
2141 return handle_trampoline(regs);
Anton Arapovfec88982013-04-03 18:00:36 +02002142
2143 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302144 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02002145 if (is_swbp > 0) {
2146 /* No matching uprobe; signal SIGTRAP. */
2147 send_sig(SIGTRAP, current, 0);
2148 } else {
2149 /*
2150 * Either we raced with uprobe_unregister() or we can't
2151 * access this memory. The latter is only possible if
2152 * another thread plays with our ->mm. In both cases
2153 * we can simply restart. If this vma was unmapped we
2154 * can pretend this insn was not executed yet and get
2155 * the (correct) SIGSEGV after restart.
2156 */
2157 instruction_pointer_set(regs, bp_vaddr);
2158 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302159 return;
2160 }
Oleg Nesterov74e59df2012-12-30 15:54:08 +01002161
2162 /* change it in advance for ->handler() and restart */
2163 instruction_pointer_set(regs, bp_vaddr);
2164
Oleg Nesterov142b18d2012-09-29 21:56:57 +02002165 /*
2166 * TODO: move copy_insn/etc into _register and remove this hack.
2167 * After we hit the bp, _unregister + _register can install the
2168 * new and not-yet-analyzed uprobe at the same address, restart.
2169 */
Oleg Nesterov71434f22012-09-30 21:12:44 +02002170 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
Oleg Nesterov74e59df2012-12-30 15:54:08 +01002171 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302172
Andrea Parri09d3f012018-11-22 17:10:31 +01002173 /*
2174 * Pairs with the smp_wmb() in prepare_uprobe().
2175 *
2176 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
2177 * we must also see the stores to &uprobe->arch performed by the
2178 * prepare_uprobe() call.
2179 */
2180 smp_rmb();
2181
Oleg Nesterov72fd2932013-11-26 09:35:25 +09002182 /* Tracing handlers use ->utask to communicate with fetch methods */
2183 if (!get_utask())
2184 goto out;
2185
David A. Long6fe50a22014-02-03 14:25:49 -05002186 if (arch_uprobe_ignore(&uprobe->arch, regs))
2187 goto out;
2188
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302189 handler_chain(uprobe, regs);
David A. Long6fe50a22014-02-03 14:25:49 -05002190
Oleg Nesterov8a6b1732014-03-30 18:56:22 +02002191 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
Oleg Nesterov0578a972012-09-14 18:31:23 +02002192 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302193
Oleg Nesterov608e7422012-12-31 18:20:42 +01002194 if (!pre_ssout(uprobe, regs, bp_vaddr))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302195 return;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302196
Oleg Nesterov8a6b1732014-03-30 18:56:22 +02002197 /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */
Oleg Nesterov0578a972012-09-14 18:31:23 +02002198out:
Sebastian Andrzej Siewior8bd87442012-08-07 18:12:30 +02002199 put_uprobe(uprobe);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302200}
2201
2202/*
2203 * Perform required fix-ups and disable singlestep.
2204 * Allow pending signals to take effect.
2205 */
2206static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
2207{
2208 struct uprobe *uprobe;
Oleg Nesterov014940b2014-04-03 20:20:10 +02002209 int err = 0;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302210
2211 uprobe = utask->active_uprobe;
2212 if (utask->state == UTASK_SSTEP_ACK)
Oleg Nesterov014940b2014-04-03 20:20:10 +02002213 err = arch_uprobe_post_xol(&uprobe->arch, regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302214 else if (utask->state == UTASK_SSTEP_TRAPPED)
2215 arch_uprobe_abort_xol(&uprobe->arch, regs);
2216 else
2217 WARN_ON_ONCE(1);
2218
2219 put_uprobe(uprobe);
2220 utask->active_uprobe = NULL;
2221 utask->state = UTASK_RUNNING;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05302222 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302223
2224 spin_lock_irq(&current->sighand->siglock);
2225 recalc_sigpending(); /* see uprobe_deny_signal() */
2226 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterov014940b2014-04-03 20:20:10 +02002227
2228 if (unlikely(err)) {
2229 uprobe_warn(current, "execute the probed insn, sending SIGILL.");
Eric W. Biederman55a32352018-07-19 20:33:53 -05002230 force_sig(SIGILL, current);
Oleg Nesterov014940b2014-04-03 20:20:10 +02002231 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302232}
2233
2234/*
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02002235 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
2236 * allows the thread to return from interrupt. After that handle_swbp()
2237 * sets utask->active_uprobe.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302238 *
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02002239 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
2240 * and allows the thread to return from interrupt.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302241 *
2242 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
2243 * uprobe_notify_resume().
2244 */
2245void uprobe_notify_resume(struct pt_regs *regs)
2246{
2247 struct uprobe_task *utask;
2248
Oleg Nesterovdb023ea2012-09-14 19:05:46 +02002249 clear_thread_flag(TIF_UPROBE);
2250
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302251 utask = current->utask;
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02002252 if (utask && utask->active_uprobe)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302253 handle_singlestep(utask, regs);
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02002254 else
2255 handle_swbp(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302256}
2257
2258/*
2259 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
2260 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
2261 */
2262int uprobe_pre_sstep_notifier(struct pt_regs *regs)
2263{
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02002264 if (!current->mm)
2265 return 0;
2266
2267 if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
2268 (!current->utask || !current->utask->return_instances))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302269 return 0;
2270
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302271 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302272 return 1;
2273}
2274
2275/*
2276 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
2277 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
2278 */
2279int uprobe_post_sstep_notifier(struct pt_regs *regs)
2280{
2281 struct uprobe_task *utask = current->utask;
2282
2283 if (!current->mm || !utask || !utask->active_uprobe)
2284 /* task is currently not uprobed */
2285 return 0;
2286
2287 utask->state = UTASK_SSTEP_ACK;
2288 set_thread_flag(TIF_UPROBE);
2289 return 1;
2290}
2291
2292static struct notifier_block uprobe_exception_nb = {
2293 .notifier_call = arch_uprobe_exception_notify,
2294 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
2295};
2296
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302297static int __init init_uprobes(void)
2298{
2299 int i;
2300
Oleg Nesterov66d06df2012-11-25 22:48:37 +01002301 for (i = 0; i < UPROBES_HASH_SZ; i++)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302302 mutex_init(&uprobes_mmap_mutex[i]);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302303
Oleg Nesterov32cdba12012-11-14 19:03:42 +01002304 if (percpu_init_rwsem(&dup_mmap_sem))
2305 return -ENOMEM;
2306
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302307 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302308}
Oleg Nesterov736e89d2013-10-31 19:28:22 +01002309__initcall(init_uprobes);