Skip to contentSkip to conversation

0: Inode based uprobes

RFC PATCH Superseded Srikar Dronamraju 6 participants 91 messages Dec 16, 2010 – Jan 28, 2011

Subsystems (9)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover 0: Inode based uprobes

into any routine in a user space application and collect information non-disruptively.

For previous posting: please refer: http://lkml.org/lkml/2010/8/25/165
http://lkml.org/lkml/2010/7/27/121, http://lkml.org/lkml/2010/7/12/67,
http://lkml.org/lkml/2010/7/8/239, http://lkml.org/lkml/2010/6/29/299,
http://lkml.org/lkml/2010/6/14/41, http://lkml.org/lkml/2010/3/20/107
and http://lkml.org/lkml/2010/5/18/307

Uprobes Patches
Unlike the previous postings where a probe was specified as pid:vaddr, this patchset implements inode based uprobes which are specified as <file>:<offset> where offset is the offset from start of the map. The probehit overhead is around 3X times the previous patchset overhead.

This patchset is a rework based on suggestions from discussions on lkml in September, March and January 2010 (http://lkml.org/lkml/2010/1/11/92,
http://lkml.org/lkml/2010/1/27/19, http://lkml.org/lkml/2010/3/20/107
and http://lkml.org/lkml/2010/3/31/199 ). This implementation of uprobes doesnt depend on utrace.

When a uprobe is registered, Uprobes makes a copy of the probed instruction, replaces the first byte(s) of the probed instruction with a breakpoint instruction. (Uprobes uses background page replacement mechanism and ensures that the breakpoint affects only that process.)

When a CPU hits the breakpoint instruction, Uprobes gets notified of trap and finds the associated uprobe. It then executes the associated handler. Uprobes single-steps its copy of the probed instruction and resumes execution of the probed process at the instruction following the probepoint. Instruction copies to be single-stepped are stored in a per-mm "execution out of line (XOL) area". Currently XOL area is allocated as one page vma.

Advantages of uprobes over conventional debugging include:

1. Non-disruptive.
Unlike current ptrace based mechanisms, uprobes tracing wouldnt involve signals, stopping threads and context switching between the tracer and tracee.

2. Much better handling of multithreaded programs because of XOL. Current ptrace based mechanisms use single stepping inline, i.e they copy back the original instruction on hitting a breakpoint. In such mechanisms tracers have to stop all the threads on a breakpoint hit or tracers will not be able to handle all hits to the location of interest. Uprobes uses execution out of line, where the instruction to be traced is analysed at the time of breakpoint insertion and a copy of instruction is stored at a different location. On breakpoint hit, uprobes jumps to that copied location and singlesteps the same instruction and does the necessary fixups post singlestepping.

3. Multiple tracers for an application.
Multiple uprobes based tracer could work in unison to trace an application. There could one tracer that could be interested in generic events for a particular set of process. While there could be another tracer that is just interested in one specific event of a particular process thats part of the previous set of process.

4. Corelating events from kernels and userspace.
Uprobes could be used with other tools like kprobes, tracepoints or as part of higher level tools like perf to give a consolidated set of events from kernel and userspace. In future we could look at a single backtrace showing application, library and kernel calls.

Here is the list of TODO Items.

- Integrating perf probe with this patchset.
- Prefiltering (i.e filtering at the time of probe insertion) (Can be achieved if we can dynamically assign consumers at uprobe tracer enable time; Suggestions on how to do this are welcome)
- Signal handling.
- queueing non-uprobes based INT3 as SIGTRAPS.
- delaying signals from INT3 till post singlestep and queueing the delayed signals.
- Return probes.
- Support for other architectures.
- Uprobes booster.
- replace macro W with bits in inat table.
- Bulk registration/unregisteration.

To try please fetch using
git fetch \
git://git.kernel.org/pub/scm/linux/kernel/git/srikar/linux-uprobes.git \
tip_inode_uprobes_161210:tip_inode_uprobes

Please refer "[RFC] [PATCH 2.6.37-rc5-tip 20/20] 20: tracing: uprobes trace_event infrastructure" on how to use uprobe_tracer.

Please do provide your valuable comments.

Thanks in advance.
Srikar

Srikar Dronamraju(20)
0: Inode based uprobes
1: mm: Move replace_page() / write_protect_page() to mm/memory.c 2: X86 specific breakpoint definitions.
3: uprobes: Breakground page replacement.
4: uprobes: Adding and remove a uprobe in a rb tree.
5: Uprobes: register/unregister probes.
6: x86: analyze instruction and determine fixups.
7: uprobes: store/restore original instruction.
8: uprobes: mmap and fork hooks.
9: x86: architecture specific task information.
10: uprobes: task specific information.
11: uprobes: slot allocation for uprobes
12: uprobes: get the breakpoint address.
13: x86: x86 specific probe handling
14: uprobes: Handing int3 and singlestep exception.
15: x86: uprobes exception notifier for x86.
16: uprobes: register a notifier for uprobes.
17: uprobes: filter chain
18: uprobes: commonly used filters.
19: tracing: Extract out common code for kprobes/uprobes traceevents. 20: tracing: uprobes trace_event interface

 arch/Kconfig                       |    4 +
 arch/x86/Kconfig                   |    3 +
 arch/x86/include/asm/thread_info.h |    2 +
 arch/x86/include/asm/uprobes.h     |   55 ++
 arch/x86/kernel/Makefile           |    1 +
 arch/x86/kernel/signal.c           |   14 +
 arch/x86/kernel/uprobes.c          |  599 +++++++++++++++++
 include/linux/mm.h                 |    4 +
 include/linux/mm_types.h           |    9 +
 include/linux/sched.h              |    3 +
 include/linux/uprobes.h            |  186 ++++++
 kernel/Makefile                    |    1 +
 kernel/fork.c                      |   10 +
 kernel/trace/Kconfig               |   20 +
 kernel/trace/Makefile              |    2 +
 kernel/trace/trace.h               |    5 +
 kernel/trace/trace_kprobe.c        |  752 +---------------------
 kernel/trace/trace_probe.c         |  654 +++++++++++++++++++
 kernel/trace/trace_probe.h         |  157 +++++
 kernel/trace/trace_uprobe.c        |  753 ++++++++++++++++++++++
 kernel/uprobes.c                   | 1250 ++++++++++++++++++++++++++++++++++++
 mm/ksm.c                           |  114 ----
 mm/memory.c                        |  122 ++++
 mm/mmap.c                          |    2 +
 24 files changed, 3871 insertions(+), 851 deletions(-)
#1 lore raw

RFC PATCH 1/20 1: mm: Move replace_page() / write_protect_page() to mm/memory.c

User bkpt will use background page replacement approach to insert/delete breakpoints. Background page replacement approach will be based on replace_page and write_protect_page. Now replace_page() and write_protect_page loses their static attribute.

---
 include/linux/mm.h |    4 ++
 mm/ksm.c           |  114 -------------------------------------------------
 mm/memory.c        |  122 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 126 insertions(+), 114 deletions(-)
include/linux/mm.h +4 -0
index 721f451..24f8bb0 100644
@@ -874,6 +874,10 @@ void account_page_writeback(struct page *page);
 int set_page_dirty(struct page *page);
 int set_page_dirty_lock(struct page *page);
 int clear_page_dirty_for_io(struct page *page);
+int replace_page(struct vm_area_struct *vma, struct page *page,
+					struct page *kpage, pte_t orig_pte);
+int write_protect_page(struct vm_area_struct *vma, struct page *page,
+						      pte_t *orig_pte);
 
 /* Is the vma a continuation of the stack vma above it? */
 static inline int vma_stack_continue(struct vm_area_struct *vma, unsigned long addr)
mm/ksm.c +0 -114

120 lines not on this page · raw message

mm/memory.c +122 -0

128 lines not on this page · raw message

#2 lore raw

RFC PATCH 2/20 2: X86 specific breakpoint definitions.

In reply to Srikar Dronamraju

Provides definitions for the breakpoint instruction and x86 specific uprobe info structure.

---
 arch/x86/Kconfig               |    3 +++
 arch/x86/include/asm/uprobes.h |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/include/asm/uprobes.h
arch/x86/Kconfig +3 -0
index b6fccb0..bcbbc52 100644
@@ -239,6 +239,9 @@ config ARCH_CPU_PROBE_RELEASE
 	def_bool y
 	depends on HOTPLUG_CPU
 
+config ARCH_SUPPORTS_UPROBES
+	def_bool y
+
 source "init/Kconfig"
 source "kernel/Kconfig.freezer"
 
arch/x86/include/asm/uprobes.h new file +40 -0
new file mode 100644
index 0000000..0e8ad5d
@@ -0,0 +1,40 @@
+#ifndef _ASM_UPROBES_H
+#define _ASM_UPROBES_H
+/*
+ * Userspace Probes (UProbes) for x86
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) IBM Corporation, 2008-2010
+ * Authors:
+ *	Srikar Dronamraju
+ *	Jim Keniston
+ */
+
+typedef u8 uprobe_opcode_t;
+#define MAX_UINSN_BYTES 16
+#define UPROBES_XOL_SLOT_BYTES (MAX_UINSN_BYTES)
+
+#define UPROBES_BKPT_INSN 0xcc
+#define UPROBES_BKPT_INSN_SIZE 1
+
+#ifdef CONFIG_X86_64
+struct uprobe_arch_info {
+	unsigned long rip_rela_target_address;
+};
+#else
+struct uprobe_arch_info {};
+#endif
+#endif	/* _ASM_UPROBES_H */
#3 lore raw

RFC PATCH 3/20 3: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju

Provides Background page replacement using replace_page() routine. Also provides routines to read/write few bytes to vm and for verifying if a instruction is a breakpoint instruction.

---
 arch/Kconfig                   |   11 ++
 arch/x86/include/asm/uprobes.h |    2 
 include/linux/uprobes.h        |   76 ++++++++++++
 kernel/Makefile                |    1 
 kernel/uprobes.c               |  252 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 341 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/uprobes.h
 create mode 100644 kernel/uprobes.c
arch/Kconfig +11 -0
index f78c2be..6e8f26e 100644
@@ -61,6 +61,17 @@ config OPTPROBES
 	depends on KPROBES && HAVE_OPTPROBES
 	depends on !PREEMPT
 
+config UPROBES
+	bool "User-space probes (EXPERIMENTAL)"
+	depends on ARCH_SUPPORTS_UPROBES
+	depends on MMU
+	help
+	  Uprobes enables kernel subsystems to establish probepoints
+	  in user applications and execute handler functions when
+	  the probepoints are hit. For more information, refer to
+	  Documentation/uprobes.txt.
+	  If in doubt, say "N".
+
 config HAVE_EFFICIENT_UNALIGNED_ACCESS
 	bool
 	help
arch/x86/include/asm/uprobes.h +1 -1
index 0e8ad5d..5026359 100644
@@ -25,7 +25,7 @@
 
 typedef u8 uprobe_opcode_t;
 #define MAX_UINSN_BYTES 16
-#define UPROBES_XOL_SLOT_BYTES (MAX_UINSN_BYTES)
+#define UPROBES_XOL_SLOT_BYTES	128	/* to keep it cache aligned */
 
 #define UPROBES_BKPT_INSN 0xcc
 #define UPROBES_BKPT_INSN_SIZE 1
include/linux/uprobes.h new file +76 -0

76 lines not on this page · raw message

kernel/Makefile +1 -0
index 0b5ff08..e53c0c4 100644
@@ -106,6 +106,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_event.o
 obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
 obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o
 obj-$(CONFIG_PADATA) += padata.o
+obj-$(CONFIG_UPROBES) += uprobes.o
 
 ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y)
 # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer is
kernel/uprobes.c new file +252 -0

252 lines not on this page · raw message

#4 lore raw

RFC PATCH 4/20 4: uprobes: Adding and remove a uprobe in a rb tree.

In reply to Srikar Dronamraju

Provides interfaces to add and remove uprobes from the global rb tree. Also provides definitions for uprobe_consumer, interfaces to add and remove a consumer to a uprobe. There is a unique uprobe element in the rbtree for each unique inode:offset pair.

Uprobe gets added to the global rb tree when the first consumer for that uprobe gets registered. It gets removed from the tree only when all registered consumers are unregistered.

Multiple consumers can share the same probe. Each consumer provides a filter to limit the tasks on which the handler should run, a handler that runs on probe hit and a value which helps filter callback to limit the tasks on which the handler should run.

---
 include/linux/uprobes.h |   14 +++
 kernel/uprobes.c        |  209 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 223 insertions(+), 0 deletions(-)
include/linux/uprobes.h +14 -0
index 952e9d7..94557ff 100644
@@ -23,6 +23,7 @@
  *	Jim Keniston
  */
 
+#include <linux/rbtree.h>
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 #include <asm/uprobes.h>
 #else
@@ -56,6 +57,19 @@ extern unsigned long uprobes_write_vm(struct task_struct *tsk,
 			void __user *vaddr, const void *kbuf,
 			unsigned long nbytes);
 
+struct uprobe_consumer {
+	int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
+	/*
+	 * filter is optional; If a filter exists, handler is run
+	 * if and only if filter returns true.
+	 */
+	bool (*filter)(struct uprobe_consumer *self, struct task_struct *task);
+
+	struct uprobe_consumer *next;
+	void *fvalue;	/* filter value */
+};
+
+
 /*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
kernel/uprobes.c +209 -0

218 lines not on this page · raw message

maintainer
#5 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 1 line · Srikar Dronamraju

+/* Acquires uprobes_mutex */

Requires? afaict uprobes_mutex isn't actually used anywhere in this patch.

Its use is added in the next patch.

Quoted 28 lines
kernel/uprobes.c +28 -0
+static struct uprobe *uprobes_add(struct inode *inode,
+                                       unsigned long offset)
+{
+       struct uprobe *uprobe, *cur_uprobe;
+
+       __iget(inode);
+       uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
+
+       if (!uprobe) {
+               iput(inode);
+               return NULL;
+       }
+       uprobe->inode = inode;
+       uprobe->offset = offset;
+
+       /* add to uprobes_tree, sorted on inode:offset */
+       cur_uprobe = insert_uprobe_rb_node(uprobe);
+
+       /* a uprobe exists for this inode:offset combination*/
+       if (cur_uprobe) {
+               kfree(uprobe);
+               uprobe = cur_uprobe;
+               iput(inode);
+       } else
+               init_rwsem(&uprobe->consumer_rwsem);
+
+       return uprobe;
+} 
#6 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 8 lines · Peter Zijlstra
Quoted 1 line · Srikar Dronamraju

+/* Acquires uprobes_mutex */

Requires? afaict uprobes_mutex isn't actually used anywhere in this patch.

Its use is added in the next patch.

Right, Stale comment, will be removed in the next patch series.

maintainer
#7 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

In reply to Srikar Dronamraju

kernel/uprobes.c bool __weak is_bkpt_insn(struct uprobe *uprobe) · line 365

Quoted 6 lines · Srikar Dronamraju
kernel/uprobes.c +6 -0
+/* Should be called lock-less */
+static void put_uprobe(struct uprobe *uprobe)
+{
+       if (atomic_dec_and_test(&uprobe->ref))
+               kfree(uprobe);
+} 

Since this instantly frees the uprobe once ref hits 0, the atomic_inc_not_zero() in find_uprobe() doesn't really make sense does it?

#8 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 11 lines · Peter Zijlstra

kernel/uprobes.c bool __weak is_bkpt_insn(struct uprobe *uprobe) · line 365

Quoted 6 lines · Srikar Dronamraju
kernel/uprobes.c +6 -0
+/* Should be called lock-less */
+static void put_uprobe(struct uprobe *uprobe)
+{
+       if (atomic_dec_and_test(&uprobe->ref))
+               kfree(uprobe);
+} 

Since this instantly frees the uprobe once ref hits 0, the atomic_inc_not_zero() in find_uprobe() doesn't really make sense does it?

Okay, I can move the atomic_inc_not_zero() in find_uprobe() to atomic_inc().

Do you see any side-effects of using atomic_inc_not_zero?

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#9 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 14 lines · Srikar Dronamraju
Quoted 11 lines · Peter Zijlstra

kernel/uprobes.c bool __weak is_bkpt_insn(struct uprobe *uprobe) · line 365

Quoted 6 lines · Srikar Dronamraju
kernel/uprobes.c +6 -0
+/* Should be called lock-less */
+static void put_uprobe(struct uprobe *uprobe)
+{
+       if (atomic_dec_and_test(&uprobe->ref))
+               kfree(uprobe);
+} 

Since this instantly frees the uprobe once ref hits 0, the atomic_inc_not_zero() in find_uprobe() doesn't really make sense does it?

Quoted 4 lines

Okay, I can move the atomic_inc_not_zero() in find_uprobe() to atomic_inc().

Do you see any side-effects of using atomic_inc_not_zero?

No, its just slower, once you want to start doing RCU lookups in the probe tree you'll need it through.

maintainer
#10 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

In reply to Srikar Dronamraju
Quoted 73 lines · Srikar Dronamraju
kernel/uprobes.c +73 -0
+static int match_inode(struct uprobe *uprobe, struct inode *inode,
+                                               struct rb_node **p)
+{
+       struct rb_node *n = *p;
+
+       if (inode < uprobe->inode)
+               *p = n->rb_left;
+       else if (inode > uprobe->inode)
+               *p = n->rb_right;
+       else
+               return 1;
+       return 0;
+}
+
+static int match_offset(struct uprobe *uprobe, unsigned long offset,
+                                               struct rb_node **p)
+{
+       struct rb_node *n = *p;
+
+       if (offset < uprobe->offset)
+               *p = n->rb_left;
+       else if (offset > uprobe->offset)
+               *p = n->rb_right;
+       else
+               return 1;
+       return 0;
+}
+
+/*
+ * Find a uprobe corresponding to a given inode:offset
+ * Acquires treelock
+ */
+static struct uprobe *find_uprobe(struct inode * inode,
+                                        unsigned long offset)
+{
+       struct rb_node *n = uprobes_tree.rb_node;
+       struct uprobe *uprobe, *u = NULL;
+       unsigned long flags;
+
+       spin_lock_irqsave(&treelock, flags);
+       while (n) {
+               uprobe = rb_entry(n, struct uprobe, rb_node);
+
+               if (match_inode(uprobe, inode, &n)) {
+                       if (match_offset(uprobe, offset, &n)) {
+                               if (atomic_inc_not_zero(&uprobe->ref))
+                                       u = uprobe;
+                               break;
+                       }
+               }
+       }
+       spin_unlock_irqrestore(&treelock, flags);
+       return u;
+}
+
+/*
+ * Check if a uprobe is already inserted;
+ *     If it does; return refcount incremented uprobe
+ *     else add the current uprobe and return NULL
+ * Acquires treelock.
+ */
+static struct uprobe *insert_uprobe_rb_node(struct uprobe *uprobe)
+{
+       struct rb_node **p = &uprobes_tree.rb_node;
+       struct rb_node *parent = NULL;
+       struct uprobe *u;
+       unsigned long flags;
+
+       spin_lock_irqsave(&treelock, flags);
+       while (*p) {
+               parent = *p;
+               u = rb_entry(parent, struct uprobe, rb_node);
+               if (u->inode > uprobe->inode)

kernel/uprobes.c bool __weak is_bkpt_insn(struct uprobe *uprobe) · line 345

Quoted 10 lines
kernel/uprobes.c +10 -0
+                       p = &(*p)->rb_left;
+               else if (u->inode < uprobe->inode)
+                       p = &(*p)->rb_right;
+               else {
+                       if (u->offset > uprobe->offset)
+                               p = &(*p)->rb_left;
+                       else if (u->offset < uprobe->offset)
+                               p = &(*p)->rb_right;
+                       else {
+                               atomic_inc(&u->ref);

If the lookup can find a 'dead' entry, then why can't we here?

kernel/uprobes.c bool __weak is_bkpt_insn(struct uprobe *uprobe) · line 358

Quoted 13 lines
kernel/uprobes.c +13 -0
+                               goto unlock_return;
+                       }
+               }
+       }
+       u = NULL;
+       rb_link_node(&uprobe->rb_node, parent, p);
+       rb_insert_color(&uprobe->rb_node, &uprobes_tree);
+       atomic_set(&uprobe->ref, 2);
+
+unlock_return:
+       spin_unlock_irqrestore(&treelock, flags);
+       return u;
+} 

It would be nice if you could merge the find and 'acquire' thing, the lookup is basically the same in both cases.

Also, I'm not quite sure on the name of that last function, its not a strict insert and what's the trailing _rb_node about? That lookup isn't called find_uprobe_rb_node() either is it?

#11 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 16 lines
Quoted 5 lines
kernel/uprobes.c +5 -0
+       spin_lock_irqsave(&treelock, flags);
+       while (*p) {
+               parent = *p;
+               u = rb_entry(parent, struct uprobe, rb_node);
+               if (u->inode > uprobe->inode)

kernel/uprobes.c bool __weak is_bkpt_insn(struct uprobe *uprobe) · line 345

Quoted 10 lines
kernel/uprobes.c +10 -0
+                       p = &(*p)->rb_left;
+               else if (u->inode < uprobe->inode)
+                       p = &(*p)->rb_right;
+               else {
+                       if (u->offset > uprobe->offset)
+                               p = &(*p)->rb_left;
+                       else if (u->offset < uprobe->offset)
+                               p = &(*p)->rb_right;
+                       else {
+                               atomic_inc(&u->ref);
Quoted 2 lines

If the lookup can find a 'dead' entry, then why can't we here?

If a new user of a uprobe comes up as when the last registered user was removing the uprobe, we keep the uprobe entry till the new user loses interest in that uprobe.

Quoted 17 lines

kernel/uprobes.c bool __weak is_bkpt_insn(struct uprobe *uprobe) · line 358

Quoted 13 lines
kernel/uprobes.c +13 -0
+                               goto unlock_return;
+                       }
+               }
+       }
+       u = NULL;
+       rb_link_node(&uprobe->rb_node, parent, p);
+       rb_insert_color(&uprobe->rb_node, &uprobes_tree);
+       atomic_set(&uprobe->ref, 2);
+
+unlock_return:
+       spin_unlock_irqrestore(&treelock, flags);
+       return u;
+} 

It would be nice if you could merge the find and 'acquire' thing, the lookup is basically the same in both cases.

Quoted 3 lines

Also, I'm not quite sure on the name of that last function, its not a strict insert and what's the trailing _rb_node about? That lookup isn't called find_uprobe_rb_node() either is it?

Since we already have a install_uprobe, register_uprobe, I thought insert_uprobe_rb_node would give context to that function that it was only inserting an rb_node but not installing the actual breakpoint. I am okay to rename it to insert_uprobe().

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#12 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

In reply to Srikar Dronamraju

include/linux/uprobes.h extern unsigned long uprobes_write_vm(struct task_struct *tsk, · line 70

Quoted 11 lines · Srikar Dronamraju
include/linux/uprobes.h +11 -0
+struct uprobe_consumer {
+       int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
+       /*
+        * filter is optional; If a filter exists, handler is run
+        * if and only if filter returns true.
+        */
+       bool (*filter)(struct uprobe_consumer *self, struct task_struct *task);
+
+       struct uprobe_consumer *next;
+       void *fvalue;   /* filter value */
+}; 

Since you pass in a pointer to this structure at register_uprobe() its user allocated, hence you can remove the fvalue thing and let the user embed this in a larger struct if needed, the filter functions can then use container_of() to get at the larger data structure.

#13 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 17 lines · Peter Zijlstra

include/linux/uprobes.h extern unsigned long uprobes_write_vm(struct task_struct *tsk, · line 70

Quoted 11 lines · Srikar Dronamraju
include/linux/uprobes.h +11 -0
+struct uprobe_consumer {
+       int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
+       /*
+        * filter is optional; If a filter exists, handler is run
+        * if and only if filter returns true.
+        */
+       bool (*filter)(struct uprobe_consumer *self, struct task_struct *task);
+
+       struct uprobe_consumer *next;
+       void *fvalue;   /* filter value */
+}; 

Since you pass in a pointer to this structure at register_uprobe() its user allocated, hence you can remove the fvalue thing and let the user embed this in a larger struct if needed, the filter functions can then use container_of() to get at the larger data structure.

Okay, Will do, but Is there a reason for moving the fvalue out of the uprobe_consumer? Except for reducing the size of the structure, I am unable to see advantage.

maintainer
#14 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 5 lines · Srikar Dronamraju

Okay, Will do, but Is there a reason for moving the fvalue out of the uprobe_consumer? Except for reducing the size of the structure, I am unable to see advantage.

That's about it, and its the normal way to do such things in kernel space.

#15 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Srikar Dronamraju

Okay, Will do, but Is there a reason for moving the fvalue out of the uprobe_consumer? Except for reducing the size of the structure, I am unable to see advantage.

That's about it, and its the normal way to do such things in kernel space.

But the disadvantage would be we wont be able to share the filter functions. Currently i had one patch that implemented the common filter functions that tracers could reuse.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#16 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 9 lines · Srikar Dronamraju
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Srikar Dronamraju

Okay, Will do, but Is there a reason for moving the fvalue out of the uprobe_consumer? Except for reducing the size of the structure, I am unable to see advantage.

Quoted 6 lines
Quoted 2 lines

That's about it, and its the normal way to do such things in kernel space.

But the disadvantage would be we wont be able to share the filter functions. Currently i had one patch that implemented the common filter functions that tracers could reuse.

But you could still do that, just make then use something like:

struct uprobe_simple_consumer {
	struct uprobe_consumer consumer;
	unsigned long value;
};
#17 lore raw

RFC PATCH 5/20 5: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju

A probe is specified by a file:offset. While registering, a breakpoint is inserted for the first consumer, On subsequent probes, the consumer gets appended to the existing consumers. While unregistering a breakpoint is removed if the consumer happens to be the last consumer. All other unregisterations, the consumer is deleted from the list of consumers.

Probe specifications are maintained in a rb tree. A probe specification is converted into a uprobe before store in a rb tree. A uprobe can be shared by many consumers.

Given a inode, we get a list of mm's that have mapped the inode. However we want to limit the probes to certain processes/threads. The filtering should be at thread level. To limit the probes to a certain processes/threads, we would want to walk through the list of threads whose mm member refer to a given mm.

Here are the options that I thought of:
1. Use mm->owner and walk thro the thread_group of mm->owner, siblings of mm->owner, siblings of parent of mm->owner. This should be good list to traverse. Not sure if this is an exhaustive enough list that all tasks that have a mm set to this mm_struct are walked through.

2. Install probes on all mm's that have mapped the probes and filter only at probe hit time.

3. walk thro do_each_thread; while_each_thread; I think this will catch all tasks that have a mm set to the given mm. However this might be too heavy esp if mm corresponds to a library.

4. add a list_head element to the mm struct and update the list whenever the task->mm thread gets updated. This could mean extending the current mm->owner. However there is some maintainance overhead.

Currently we use the second approach, i.e probe all mm's that have mapped the probes and filter only at probe hit.

Also would be interested to know if there are ways to call replace_page without having to take mmap_sem.

---
 include/linux/mm_types.h |    5 +
 include/linux/uprobes.h  |   32 +++++++++
 kernel/uprobes.c         |  161 +++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 187 insertions(+), 11 deletions(-)
include/linux/mm_types.h +5 -0
index bb7288a..af2b55d 100644
@@ -312,6 +312,11 @@ struct mm_struct {
 #endif
 	/* How many tasks sharing this mm are OOM_DISABLE */
 	atomic_t oom_disable_count;
+#ifdef CONFIG_UPROBES
+	unsigned long uprobes_vaddr;
+	struct list_head uprobes_list;
+	atomic_t uprobes_count;
+#endif
 };
 
 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
include/linux/uprobes.h +32 -0
index 94557ff..f62c7b0 100644
@@ -31,6 +31,7 @@
  * ARCH_SUPPORTS_UPROBES has not be defined.
  */
 typedef u8 uprobe_opcode_t;
+struct uprobe_arch_info	{};		/* arch specific info*/
 
 /* Post-execution fixups.  Some architectures may define others. */
 #endif /* CONFIG_ARCH_SUPPORTS_UPROBES */
@@ -69,6 +70,19 @@ struct uprobe_consumer {
 	void *fvalue;	/* filter value */
 };
 
+struct uprobe {
+	struct rb_node		rb_node;	/* node in the rb tree */
+	atomic_t		ref;
+	struct rw_semaphore	consumer_rwsem;
+	struct uprobe_arch_info	arch_info;	/* arch specific info if any */
+	struct uprobe_consumer	*consumers;
+	struct inode		*inode;		/* Also hold a ref to inode */
+	unsigned long		offset;
+	uprobe_opcode_t		opcode;
+	u16			fixups;
+	int			copy;
+	u8			insn[MAX_UINSN_BYTES];	/* orig instruction */
+};
 
 /*
  * Most architectures can use the default versions of @read_opcode(),
@@ -87,4 +101,22 @@ struct uprobe_consumer {
  *	You may modify @user_bkpt->insn (e.g., the x86_64 port does this
  *	for rip-relative instructions).
  */
+
+#ifdef CONFIG_UPROBES
+extern int register_uprobe(struct inode *inode, unsigned long offset,
+				struct uprobe_consumer *consumer);
+extern void unregister_uprobe(struct inode *inode, unsigned long offset,
+				struct uprobe_consumer *consumer);
+#else /* CONFIG_UPROBES is not defined */
+static inline int register_uprobe(struct inode *inode, unsigned long offset,
+				struct uprobe_consumer *consumer)
+{
+	return -ENOSYS;
+}
+static inline void unregister_uprobe(struct inode *inode, unsigned long offset,
+				struct uprobe_consumer *consumer)
+{
+}
+
+#endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/uprobes.c +150 -11

170 lines not on this page · raw message

maintainer
#18 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 52 lines · Srikar Dronamraju
kernel/uprobes.c +52 -0
+void unregister_uprobe(struct inode *inode, unsigned long offset,
+                               struct uprobe_consumer *consumer)
+{
+       struct prio_tree_iter iter;
+       struct list_head tmp_list;
+       struct address_space *mapping;
+       struct mm_struct *mm, *tmpmm;
+       struct vm_area_struct *vma;
+       struct uprobe *uprobe;
+
+       if (!inode || !consumer)
+               return;
+
+       uprobe = find_uprobe(inode, offset);
+       if (!uprobe) {
+               printk(KERN_ERR "No uprobe found with inode:offset %p %lu\n",
+                               inode, offset);
+               return;
+       }
+
+       if (!del_consumer(uprobe, consumer)) {
+               printk(KERN_ERR "No uprobe found with consumer %p\n",
+                               consumer);
+               return;
+       }
+
+       INIT_LIST_HEAD(&tmp_list);
+
+       mapping = inode->i_mapping;
+
+       mutex_lock(&uprobes_mutex);
+       if (uprobe->consumers)
+               goto put_unlock;
+
+       spin_lock(&mapping->i_mmap_lock);
+       vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+               if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+                       continue;
+
+               mm = vma->vm_mm;
+
+               if (!atomic_read(&mm->uprobes_count)) {
+                       mmput(mm);
+                       continue;
+               }
+
+               if (valid_vma(vma)) {
+                       list_add(&mm->uprobes_list, &tmp_list);
+                       mm->uprobes_vaddr = vma->vm_start + offset;
+               } else
+                       mmput(mm);
+       }

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 592

Quoted 10 lines
kernel/uprobes.c +10 -0
+       spin_unlock(&mapping->i_mmap_lock);
+       list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
+               remove_uprobe(mm, uprobe);
+               list_del(&mm->uprobes_list);
+               mmput(mm);
+       }
+
+       if (atomic_read(&uprobe->ref) == 1) {
+               synchronize_sched();
+               rb_erase(&uprobe->rb_node, &uprobes_tree);

How is that safe without holding the treelock?

Quoted 7 lines
kernel/uprobes.c +7 -0
+               iput(uprobe->inode);
+       }
+
+put_unlock:
+       mutex_unlock(&uprobes_mutex);
+       put_uprobe(uprobe);
+} 
#19 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 6 lines · Peter Zijlstra

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 592

Quoted 4 lines
kernel/uprobes.c +4 -0
+
+       if (atomic_read(&uprobe->ref) == 1) {
+               synchronize_sched();
+               rb_erase(&uprobe->rb_node, &uprobes_tree);

How is that safe without holding the treelock?

Right,
Something like this should be good enuf right?

if (atomic_read(&uprobe->ref) == 1) {
	synchronize_sched();
	spin_lock_irqsave(&treelock, flags);
	rb_erase(&uprobe->rb_node, &uprobes_tree);
	spin_lock_irqrestore(&treelock, flags);
	iput(uprobe->inode);
}
Signature · 12 lines
Thanks and Regards
Srikar

PS: Last time I had goofed up with Linux-mm mailing alias. 
Hopefully this time it goes to the right list.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#20 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 12 lines · Srikar Dronamraju
Quoted 6 lines · Peter Zijlstra

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 592

Quoted 4 lines
kernel/uprobes.c +4 -0
+
+       if (atomic_read(&uprobe->ref) == 1) {
+               synchronize_sched();
+               rb_erase(&uprobe->rb_node, &uprobes_tree);

How is that safe without holding the treelock?

Right,
Something like this should be good enuf right?

Quoted 8 lines
if (atomic_read(&uprobe->ref) == 1) {
	synchronize_sched();
	spin_lock_irqsave(&treelock, flags);
	rb_erase(&uprobe->rb_node, &uprobes_tree);
	spin_lock_irqrestore(&treelock, flags);
	iput(uprobe->inode);
}

How is the atomic_read() not racy with a future increment, and what is that synchronize_sched() thing for?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#21 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 30 lines · Srikar Dronamraju
kernel/uprobes.c +30 -0
+/* Returns 0 if it can install one probe */
+int register_uprobe(struct inode *inode, unsigned long offset,
+                               struct uprobe_consumer *consumer)
+{
+       struct prio_tree_iter iter;
+       struct list_head tmp_list;
+       struct address_space *mapping;
+       struct mm_struct *mm, *tmpmm;
+       struct vm_area_struct *vma;
+       struct uprobe *uprobe;
+       int ret = -1;
+
+       if (!inode || !consumer || consumer->next)
+               return -EINVAL;
+       uprobe = uprobes_add(inode, offset);
+       INIT_LIST_HEAD(&tmp_list);
+
+       mapping = inode->i_mapping;
+
+       mutex_lock(&uprobes_mutex);
+       if (uprobe->consumers) {
+               ret = 0;
+               goto consumers_add;
+       }
+
+       spin_lock(&mapping->i_mmap_lock);
+       vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+               if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+                       continue;
+

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 511

Quoted 10 lines
kernel/uprobes.c +10 -0
+               mm = vma->vm_mm;
+               if (!valid_vma(vma)) {
+                       mmput(mm);
+                       continue;
+               }
+
+               list_add(&mm->uprobes_list, &tmp_list);
+               mm->uprobes_vaddr = vma->vm_start + offset;
+       }
+       spin_unlock(&mapping->i_mmap_lock);

Both this and unregister are racy, what is to say:
- the vma didn't get removed from the mm
- no new matching vma got added

Quoted 18 lines
kernel/uprobes.c +18 -0
+       if (list_empty(&tmp_list)) {
+               ret = 0;
+               goto consumers_add;
+       }
+       list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
+               if (!install_uprobe(mm, uprobe))
+                       ret = 0;
+               list_del(&mm->uprobes_list);
+               mmput(mm);
+       }
+
+consumers_add:
+       add_consumer(uprobe, consumer);
+       mutex_unlock(&uprobes_mutex);
+       put_uprobe(uprobe);
+       return ret;
+}
+ 
#22 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 10 lines

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 511

Quoted 5 lines
kernel/uprobes.c +5 -0
+
+               list_add(&mm->uprobes_list, &tmp_list);
+               mm->uprobes_vaddr = vma->vm_start + offset;
+       }
+       spin_unlock(&mapping->i_mmap_lock);

Both this and unregister are racy, what is to say:
- the vma didn't get removed from the mm
- no new matching vma got added

register_uprobe, unregister_uprobe, uprobe_mmap are all synchronized by uprobes_mutex. So I dont see one unregister_uprobe getting thro when another register_uprobe is working with a vma.

If I am missing something elementary, please explain a bit more.

Quoted 18 lines
Quoted 18 lines
kernel/uprobes.c +18 -0
+       if (list_empty(&tmp_list)) {
+               ret = 0;
+               goto consumers_add;
+       }
+       list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
+               if (!install_uprobe(mm, uprobe))
+                       ret = 0;
+               list_del(&mm->uprobes_list);
+               mmput(mm);
+       }
+
+consumers_add:
+       add_consumer(uprobe, consumer);
+       mutex_unlock(&uprobes_mutex);
+       put_uprobe(uprobe);
+       return ret;
+}
+ 
Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#23 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 12 lines · Srikar Dronamraju
Quoted 11 lines

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 511

Quoted 6 lines
kernel/uprobes.c +5 -0
+
+               list_add(&mm->uprobes_list, &tmp_list);
+               mm->uprobes_vaddr = vma->vm_start + offset;
+       }
+       spin_unlock(&mapping->i_mmap_lock);

Both this and unregister are racy, what is to say:
- the vma didn't get removed from the mm
- no new matching vma got added

Quoted 5 lines

register_uprobe, unregister_uprobe, uprobe_mmap are all synchronized by uprobes_mutex. So I dont see one unregister_uprobe getting thro when another register_uprobe is working with a vma.

If I am missing something elementary, please explain a bit more.

afaict you're not holding the mmap_sem, so userspace can simply unmap the vma.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#24 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Srikar Dronamraju
Quoted 11 lines

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 511

Quoted 6 lines
kernel/uprobes.c +5 -0
+
+               list_add(&mm->uprobes_list, &tmp_list);
+               mm->uprobes_vaddr = vma->vm_start + offset;
+       }
+       spin_unlock(&mapping->i_mmap_lock);

Both this and unregister are racy, what is to say:
- the vma didn't get removed from the mm
- no new matching vma got added

Quoted 8 lines
Quoted 5 lines

register_uprobe, unregister_uprobe, uprobe_mmap are all synchronized by uprobes_mutex. So I dont see one unregister_uprobe getting thro when another register_uprobe is working with a vma.

If I am missing something elementary, please explain a bit more.

afaict you're not holding the mmap_sem, so userspace can simply unmap the vma.

When we do the actual insert/remove of the breakpoint we hold the mmap_sem. During the actual insertion/removal, if the vma for the specific inode is not found, we just come out without doing the actual insertion/deletion.

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#25 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 21 lines · Srikar Dronamraju
Quoted 19 lines · Peter Zijlstra
Quoted 17 lines · Srikar Dronamraju
Quoted 11 lines

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 511

Quoted 6 lines
kernel/uprobes.c +5 -0
+
+               list_add(&mm->uprobes_list, &tmp_list);
+               mm->uprobes_vaddr = vma->vm_start + offset;
+       }
+       spin_unlock(&mapping->i_mmap_lock);

Both this and unregister are racy, what is to say:
- the vma didn't get removed from the mm
- no new matching vma got added

register_uprobe, unregister_uprobe, uprobe_mmap are all synchronized by uprobes_mutex. So I dont see one unregister_uprobe getting thro when another register_uprobe is working with a vma.

If I am missing something elementary, please explain a bit more.

Quoted 7 lines
Quoted 2 lines

afaict you're not holding the mmap_sem, so userspace can simply unmap the vma.

When we do the actual insert/remove of the breakpoint we hold the mmap_sem. During the actual insertion/removal, if the vma for the specific inode is not found, we just come out without doing the actual insertion/deletion.

Right, but then install_uprobe() should:

- lookup the vma relating to the address you stored,
- validate that the vma is indeed a map of the right inode
- validate that the offset of the probe corresponds with the stored
address

Otherwise you can race with unmap/map and end up installing the probe in a random location.

Also, I think the whole thing goes funny if someone maps the same text twice ;-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#26 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Srikar Dronamraju
Quoted 21 lines · Peter Zijlstra
Quoted 12 lines · Srikar Dronamraju
Quoted 11 lines

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 511

Quoted 6 lines
kernel/uprobes.c +5 -0
+
+               list_add(&mm->uprobes_list, &tmp_list);
+               mm->uprobes_vaddr = vma->vm_start + offset;
+       }
+       spin_unlock(&mapping->i_mmap_lock);

Both this and unregister are racy, what is to say:
- the vma didn't get removed from the mm
- no new matching vma got added

Quoted 5 lines

register_uprobe, unregister_uprobe, uprobe_mmap are all synchronized by uprobes_mutex. So I dont see one unregister_uprobe getting thro when another register_uprobe is working with a vma.

If I am missing something elementary, please explain a bit more.

afaict you're not holding the mmap_sem, so userspace can simply unmap the vma.

Quoted 8 lines
Quoted 4 lines

When we do the actual insert/remove of the breakpoint we hold the mmap_sem. During the actual insertion/removal, if the vma for the specific inode is not found, we just come out without doing the actual insertion/deletion.

Right, but then install_uprobe() should:

- lookup the vma relating to the address you stored,

We already do this thro get_user_pages in write_opcode().

Quoted 1 line

- validate that the vma is indeed a map of the right inode

We can add a check in write_opcode( we need to pass the inode to write_opcode).

Quoted 2 lines

- validate that the offset of the probe corresponds with the stored
address

I am not clear on this. We would have derived the address from the offset. So is that we check for
(vaddr == vma->vm_start + uprobe->offset)

Quoted 6 lines

Otherwise you can race with unmap/map and end up installing the probe in a random location.

Also, I think the whole thing goes funny if someone maps the same text twice ;-)

I am not sure if we can map the same text twice. If something like this is possible then we would have 2 addresses for each function. So how does the linker know which address to jump to out of the 2 or multiple matching addresses. What would be the usecases for same text being mapped multiple times and both being executable?

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#27 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 3 lines · Srikar Dronamraju
Quoted 1 line

- lookup the vma relating to the address you stored,

We already do this thro get_user_pages in write_opcode().

Ah, I didn't read that far..

Quoted 4 lines
Quoted 1 line

- validate that the vma is indeed a map of the right inode

We can add a check in write_opcode( we need to pass the inode to write_opcode).

sure..

Quoted 6 lines
Quoted 2 lines

- validate that the offset of the probe corresponds with the stored
address

I am not clear on this. We would have derived the address from the offset. So is that we check for
(vaddr == vma->vm_start + uprobe->offset)

Sure, but the vma might have changed since you computed the offset -)

Quoted 4 lines
Quoted 4 lines

Otherwise you can race with unmap/map and end up installing the probe in a random location.

Quoted 8 lines
Quoted 2 lines

Also, I think the whole thing goes funny if someone maps the same text twice ;-)

I am not sure if we can map the same text twice. If something like this is possible then we would have 2 addresses for each function. So how does the linker know which address to jump to out of the 2 or multiple matching addresses. What would be the usecases for same text being mapped multiple times and both being executable?

You can, if only to wreck your thing, you can call mmap() as often as you like (until your virtual memory space runs out) and get many many mapping of the same file.

It doesn't need to make sense to the linker, all it needs to do is confuse your code ;-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#28 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 15 lines · Peter Zijlstra
Quoted 3 lines · Srikar Dronamraju
Quoted 1 line

- lookup the vma relating to the address you stored,

We already do this thro get_user_pages in write_opcode().

Ah, I didn't read that far..

Quoted 4 lines
Quoted 1 line

- validate that the vma is indeed a map of the right inode

We can add a check in write_opcode( we need to pass the inode to write_opcode).

sure..

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

- validate that the offset of the probe corresponds with the stored
address

I am not clear on this. We would have derived the address from the offset. So is that we check for
(vaddr == vma->vm_start + uprobe->offset)

Sure, but the vma might have changed since you computed the offset -)

If the vma has changed then it would fail the 2nd validation i.e vma corresponds to the uprobe inode right. If the vma was unmapped and mapped back at the same place, then I guess we are okay to probe.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines

Otherwise you can race with unmap/map and end up installing the probe in a random location.

Quoted 8 lines
Quoted 2 lines

Also, I think the whole thing goes funny if someone maps the same text twice ;-)

I am not sure if we can map the same text twice. If something like this is possible then we would have 2 addresses for each function. So how does the linker know which address to jump to out of the 2 or multiple matching addresses. What would be the usecases for same text being mapped multiple times and both being executable?

Quoted 6 lines

You can, if only to wreck your thing, you can call mmap() as often as you like (until your virtual memory space runs out) and get many many mapping of the same file.

It doesn't need to make sense to the linker, all it needs to do is confuse your code ;-)

Currently if there are multiple mappings of the same executable code, only one mapped area would have the breakpoint inserted.

If the code were to execute from some other mapping, then it would work as if there are no probes. However if the code from the mapping that had the breakpoint executes then we would see the probes.

If we want to insert breakpoints in each of the maps then we would have to extend mm->uprobes_vaddr.

Do you have any other ideas to tackle this? Infact do you think we should be handling this case?

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#29 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 14 lines · Srikar Dronamraju
Quoted 14 lines
Quoted 4 lines
Quoted 1 line

- validate that the vma is indeed a map of the right inode

We can add a check in write_opcode( we need to pass the inode to write_opcode).

sure..

Quoted 6 lines
Quoted 2 lines

- validate that the offset of the probe corresponds with the stored
address

I am not clear on this. We would have derived the address from the offset. So is that we check for
(vaddr == vma->vm_start + uprobe->offset)

Quoted 5 lines
Quoted 1 line

Sure, but the vma might have changed since you computed the offset -)

If the vma has changed then it would fail the 2nd validation i.e vma corresponds to the uprobe inode right. If the vma was unmapped and mapped back at the same place, then I guess we are okay to probe.

It can be unmapped and mapped back slightly different. A map of the same file doesn't need to mean its in the exact same location or has the exact same pgoffset.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#30 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Srikar Dronamraju
Quoted 15 lines
Quoted 4 lines
Quoted 1 line

- validate that the vma is indeed a map of the right inode

We can add a check in write_opcode( we need to pass the inode to write_opcode).

sure..

Quoted 6 lines
Quoted 2 lines

- validate that the offset of the probe corresponds with the stored
address

I am not clear on this. We would have derived the address from the offset. So is that we check for
(vaddr == vma->vm_start + uprobe->offset)

Sure, but the vma might have changed since you computed the offset -)

Quoted 9 lines
Quoted 3 lines

If the vma has changed then it would fail the 2nd validation i.e vma corresponds to the uprobe inode right. If the vma was unmapped and mapped back at the same place, then I guess we are okay to probe.

It can be unmapped and mapped back slightly different. A map of the same file doesn't need to mean its in the exact same location or has the exact same pgoffset.

If its not at the exact same location, then our third validation of checking that (vaddr == vma->vm_start + uprobe->offset) should fail right?

Also should it be (vaddr == uprobe->offset + vma->vm_start - vma->pgoff << PAGE_SHIFT) ?

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#31 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

Quoted 8 lines · Srikar Dronamraju

If its not at the exact same location, then our third validation of checking that (vaddr == vma->vm_start + uprobe->offset) should fail right?

Also should it be (vaddr == uprobe->offset + vma->vm_start - vma->pgoff << PAGE_SHIFT) ?

Yeah, although I just realized that ->offset should be a u64, since pgoff is a unsigned long, we can have files up to 44 bit (assuming the page-size is 12bits).

But yes, this matches the validation I mentioned.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#32 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 5/20] 5: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 10 lines · Srikar Dronamraju
Quoted 7 lines

You can, if only to wreck your thing, you can call mmap() as often as you like (until your virtual memory space runs out) and get many many mapping of the same file.

It doesn't need to make sense to the linker, all it needs to do is confuse your code ;-)

Currently if there are multiple mappings of the same executable code, only one mapped area would have the breakpoint inserted.

Right, so you could use it to make debugging harder..

Quoted 9 lines

If the code were to execute from some other mapping, then it would work as if there are no probes. However if the code from the mapping that had the breakpoint executes then we would see the probes.

If we want to insert breakpoints in each of the maps then we would have to extend mm->uprobes_vaddr.

Do you have any other ideas to tackle this?

Supposing I can get my preemptible mmu patches anywhere.. you could simply call install_uprobe() while holding the i_mmap_mutex ;-)

Quoted 1 line

Infact do you think we should be handling this case?

I'm really not sure how often this would happen, but dealing with it sure makes me feel better..

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#33 lore raw

RFC PATCH 6/20 6: x86: analyze instruction and determine fixups.

In reply to Srikar Dronamraju

The instruction analysis is based on x86 instruction decoder and determines if an instruction can be probed and determines the necessary fixups after singlestep. Instruction analysis is done at probe insertion time so that we avoid having to repeat the same analysis every time a probe is hit.

---
 arch/x86/include/asm/uprobes.h |    2 
 arch/x86/kernel/Makefile       |    1 
 arch/x86/kernel/uprobes.c      |  415 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 418 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/kernel/uprobes.c
arch/x86/include/asm/uprobes.h +2 -0
index 5026359..0063207 100644
@@ -37,4 +37,6 @@ struct uprobe_arch_info {
 #else
 struct uprobe_arch_info {};
 #endif
+struct uprobe;
+extern int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe);
 #endif	/* _ASM_UPROBES_H */
arch/x86/kernel/Makefile +1 -0
index f60153d..2146b87 100644
@@ -108,6 +108,7 @@ obj-$(CONFIG_MICROCODE)			+= microcode.o
 obj-$(CONFIG_X86_CHECK_BIOS_CORRUPTION) += check.o
 
 obj-$(CONFIG_SWIOTLB)			+= pci-swiotlb.o
+obj-$(CONFIG_UPROBES)			+= uprobes.o
 
 ###
 # 64 bit specific files
arch/x86/kernel/uprobes.c new file +415 -0

415 lines not on this page · raw message

#34 lore raw

RFC PATCH 7/20 7: uprobes: store/restore original instruction.

In reply to Srikar Dronamraju

On the first probe insertion, copy the original instruction and opcode. If multiple vmas map the same text area corresponding to an inode, we only need to copy the instruction just once.
The copied instruction is further copied to a designated slot on probe hit. Its also used at the time of probe removal to restore the original instruction.
opcode is used to analyze the instruction and determine the fixups. Determining fixups at probe hit time would result in doing the same operation on every probe hit. Hence Instruction analysis using the opcode is done at probe insertion time.

---
 arch/Kconfig     |    1 +
 kernel/uprobes.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 57 insertions(+), 5 deletions(-)
arch/Kconfig +1 -0
index 6e8f26e..bba8108 100644
@@ -65,6 +65,7 @@ config UPROBES
 	bool "User-space probes (EXPERIMENTAL)"
 	depends on ARCH_SUPPORTS_UPROBES
 	depends on MMU
+	select MM_OWNER
 	help
 	  Uprobes enables kernel subsystems to establish probepoints
 	  in user applications and execute handler functions when
kernel/uprobes.c +56 -5

77 lines not on this page · raw message

maintainer
#35 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 7/20] 7: uprobes: store/restore original instruction.

Quoted 53 lines · Srikar Dronamraju

On the first probe insertion, copy the original instruction and opcode. If multiple vmas map the same text area corresponding to an inode, we only need to copy the instruction just once.
The copied instruction is further copied to a designated slot on probe hit. Its also used at the time of probe removal to restore the original instruction.
opcode is used to analyze the instruction and determine the fixups. Determining fixups at probe hit time would result in doing the same operation on every probe hit. Hence Instruction analysis using the opcode is done at probe insertion time.

---
 arch/Kconfig     |    1 +
 kernel/uprobes.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 57 insertions(+), 5 deletions(-)
arch/Kconfig +1 -0
index 6e8f26e..bba8108 100644
@@ -65,6 +65,7 @@ config UPROBES
 	bool "User-space probes (EXPERIMENTAL)"
 	depends on ARCH_SUPPORTS_UPROBES
 	depends on MMU
+	select MM_OWNER
 	help
 	  Uprobes enables kernel subsystems to establish probepoints
 	  in user applications and execute handler functions when
kernel/uprobes.c +16 -0
index 8a5da38..858ddb1 100644
@@ -448,21 +448,72 @@ static int del_consumer(struct uprobe *uprobe,
 	return ret;
 }
 
+static int copy_insn(struct task_struct *tsk, unsigned long vaddr,
+						struct uprobe *uprobe)
+{
+	int len;
+
+	len = uprobes_read_vm(tsk, (void __user *)vaddr, uprobe->insn,
+						MAX_UINSN_BYTES);
+	if (len < uprobe_opcode_sz) {
+		print_insert_fail(tsk, vaddr,
+				"error reading original instruction");
+		return -EINVAL;
+	}
+	memcpy(&uprobe->opcode, uprobe->insn, uprobe_opcode_sz);
+	if (is_bkpt_insn(uprobe)) {
+		print_insert_fail(tsk, vaddr,
+				"breakpoint instruction already exists");

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 476

Quoted 10 lines
kernel/uprobes.c +10 -0
+		return -EEXIST;
+	}
+	if (analyze_insn(tsk, uprobe)) {
+		print_insert_fail(tsk, vaddr,
+					"instruction type cannot be probed");
+		return -EINVAL;
+	}
+	uprobe->copy = 1;
+	return 0;
+}

Since you actually have the inode, you could read it from the page-cache. Also, why do you have the whole opcode/insn thing, that looks like its data duplication.

Quoted 8 lines
kernel/uprobes.c +2 -3
 static int install_uprobe(struct mm_struct *mm, struct uprobe *uprobe)
 {
-	int ret = 0;
+	struct task_struct *tsk;
+	int ret = -EINVAL;
 
-	/*TODO: install breakpoint */
-	if (!ret)

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 492

Quoted 10 lines
kernel/uprobes.c +10 -0
+	get_task_struct(mm->owner);
+	tsk = mm->owner;
+	if (!tsk)
+		return ret;
+
+	if (!uprobe->copy) {
+		ret = copy_insn(tsk, mm->uprobes_vaddr, uprobe);
+		if (ret)
+			goto put_return;
+	}

So you do know that uprobes_vaddr can point to some random piece of memory by now, right? :-)

Quoted 17 lines
kernel/uprobes.c +10 -1
+	ret = set_bkpt(tsk, mm->uprobes_vaddr);
+	if (ret < 0)
+		print_insert_fail(tsk, mm->uprobes_vaddr,
+					"failed to insert bkpt instruction");
+	else
 		atomic_inc(&mm->uprobes_count);
+
+put_return:
+	put_task_struct(tsk);
 	return ret;
 }
 
 static int remove_uprobe(struct mm_struct *mm, struct uprobe *uprobe)
 {
-	int ret = 0;
+	struct task_struct *tsk;
+	int ret;

kernel/uprobes.c static int del_consumer(struct uprobe *uprobe, · line 518

Quoted 10 lines
kernel/uprobes.c +6 -1
+
+	get_task_struct(mm->owner);
+	tsk = mm->owner;
+	if (!tsk)
+		return -EINVAL;
 
-	/*TODO: remove breakpoint */
+	ret = set_orig_insn(tsk, mm->uprobes_vaddr, true, uprobe);
 	if (!ret)
 		atomic_dec(&mm->uprobes_count);

Same here, there is no guarantee vaddr is even still mapped.

#36 lore raw

RFC PATCH 8/20 8: uprobes: mmap and fork hooks.

In reply to Srikar Dronamraju

Provides hooks in mmap and fork.

On fork, after the new mm is created, we need to set the count of uprobes. On mmap, check if the mmap region is an executable page and if its a executable page, walk through the rbtree and insert actual breakpoints for already registered probes corresponding to this inode.

---
 include/linux/uprobes.h |   11 +++++
 kernel/fork.c           |    2 +
 kernel/uprobes.c        |   96 +++++++++++++++++++++++++++++++++++++++++++++++
 mm/mmap.c               |    2 +
 4 files changed, 110 insertions(+), 1 deletions(-)
include/linux/uprobes.h +10 -1
index f62c7b0..0d4f5e3 100644
@@ -73,6 +73,7 @@ struct uprobe_consumer {
 struct uprobe {
 	struct rb_node		rb_node;	/* node in the rb tree */
 	atomic_t		ref;
+	struct list_head	pending_list;
 	struct rw_semaphore	consumer_rwsem;
 	struct uprobe_arch_info	arch_info;	/* arch specific info if any */
 	struct uprobe_consumer	*consumers;
@@ -107,6 +108,10 @@ extern int register_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer);
 extern void unregister_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer);
+
+struct vm_area_struct;
+extern void uprobe_mmap(struct vm_area_struct *vma);
+extern void uprobe_dup_mmap(struct mm_struct *old_mm, struct mm_struct *mm);
 #else /* CONFIG_UPROBES is not defined */
 static inline int register_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer)
@@ -117,6 +122,10 @@ static inline void unregister_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer)
 {
 }
-
+static inline void uprobe_dup_mmap(struct mm_struct *old_mm,
+		struct mm_struct *mm)
+{
+}
+static inline void uprobe_mmap(struct vm_area_struct *vma) { }
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/fork.c +2 -0
index 70ea75f..b135d1b 100644
@@ -66,6 +66,7 @@
 #include <linux/posix-timers.h>
 #include <linux/user-return-notifier.h>
 #include <linux/oom.h>
+#include <linux/uprobes.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -415,6 +416,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
 	}
 	/* a new mm has just been created */
 	arch_dup_mmap(oldmm, mm);
+	uprobe_dup_mmap(oldmm, mm);
 	retval = 0;
 out:
 	up_write(&mm->mmap_sem);
kernel/uprobes.c +96 -0

105 lines not on this page · raw message

mm/mmap.c +2 -0
index b179abb..df7307f 100644
@@ -29,6 +29,7 @@
 #include <linux/mmu_notifier.h>
 #include <linux/perf_event.h>
 #include <linux/audit.h>
+#include <linux/uprobes.h>
 
 #include <asm/uaccess.h>
 #include <asm/cacheflush.h>
@@ -1353,6 +1354,7 @@ out:
 			mm->locked_vm += (len >> PAGE_SHIFT);
 	} else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
 		make_pages_present(addr, addr + len);
+	uprobe_mmap(vma);
 	return addr;
 
 unmap_and_free_vma:
maintainer
#37 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 10 lines · Srikar Dronamraju
kernel/uprobes.c +10 -0
+void uprobe_mmap(struct vm_area_struct *vma)
+{
+       struct list_head tmp_list;
+       struct uprobe *uprobe, *u;
+       struct mm_struct *mm;
+       struct inode *inode;
+
+       if (!valid_vma(vma))
+               return;
+

kernel/uprobes.c put_unlock: · line 733

Quoted 10 lines
kernel/uprobes.c +10 -0
+       INIT_LIST_HEAD(&tmp_list);
+
+       /*
+        * The vma was just allocated and this routine gets called
+        * while holding write lock for mmap_sem.  Function called
+        * in context of a thread that has a reference to mm.
+        * Hence no need to take a reference to mm
+        */
+       mm = vma->vm_mm;
+       up_write(&mm->mmap_sem);

Are you very very sure its a good thing to simply drop the mmap_sem here? Also, why?

kernel/uprobes.c put_unlock: · line 736

Quoted 3 lines
kernel/uprobes.c +3 -0
+       mutex_lock(&uprobes_mutex);
+
+       inode = vma->vm_file->f_mapping->host;

Since you just dropped the mmap_sem, what's keeping that vma from going away?

Quoted 10 lines
kernel/uprobes.c +10 -0
+       add_to_temp_list(vma, inode, &tmp_list);
+
+       list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
+               mm->uprobes_vaddr = vma->vm_start + uprobe->offset;
+               install_uprobe(mm, uprobe);
+               list_del(&uprobe->pending_list);
+       }
+       mutex_unlock(&uprobes_mutex);
+       down_write(&mm->mmap_sem);
+} 
#38 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 16 lines · Srikar Dronamraju
Quoted 15 lines
kernel/uprobes.c +15 -0
+void uprobe_mmap(struct vm_area_struct *vma)
+{
+       struct list_head tmp_list;
+       struct uprobe *uprobe, *u;
+       struct mm_struct *mm;
+       struct inode *inode;
+
+       if (!valid_vma(vma))
+               return;
+
+       INIT_LIST_HEAD(&tmp_list);
+
+       /*
+        * The vma was just allocated and this routine gets called
+        * while holding write lock for mmap_sem.  Function called
Quoted 9 lines

kernel/uprobes.c put_unlock: · line 733

Quoted 5 lines
kernel/uprobes.c +5 -0
+        * in context of a thread that has a reference to mm.
+        * Hence no need to take a reference to mm
+        */
+       mm = vma->vm_mm;
+       up_write(&mm->mmap_sem);

Are you very very sure its a good thing to simply drop the mmap_sem here? Also, why?

I actually dont like to release the write_lock and then reacquire it. write_opcode, which is called thro install_uprobe, i.e to insert the actual breakpoint instruction takes a read lock on the mmap_sem. Hence uprobe_mmap gets called in context with write lock on mmap_sem held, I had to release it before calling install_uprobe.

Another solution, I thought of was to pass a context to write_opcode to say that map-sem is already acquired by us. But I am not sure that idea is good enuf.

Quoted 7 lines

kernel/uprobes.c put_unlock: · line 736

Quoted 3 lines
kernel/uprobes.c +3 -0
+       mutex_lock(&uprobes_mutex);
+
+       inode = vma->vm_file->f_mapping->host;

Since you just dropped the mmap_sem, what's keeping that vma from going away?

How about dropping the mmap_sem after add_to_temp_list and cachng the vma->vm_start value before calling add_to_temp_list?

Or if you have better ideas, then that would be great.

Quoted 12 lines

kernel/uprobes.c put_unlock: · line 746

Quoted 10 lines
kernel/uprobes.c +10 -0
+       add_to_temp_list(vma, inode, &tmp_list);
+
+       list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
+               mm->uprobes_vaddr = vma->vm_start + uprobe->offset;
+               install_uprobe(mm, uprobe);
+               list_del(&uprobe->pending_list);
+       }
+       mutex_unlock(&uprobes_mutex);
+       down_write(&mm->mmap_sem);
+} 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#39 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 7 lines · Srikar Dronamraju

I actually dont like to release the write_lock and then reacquire it. write_opcode, which is called thro install_uprobe, i.e to insert the actual breakpoint instruction takes a read lock on the mmap_sem. Hence uprobe_mmap gets called in context with write lock on mmap_sem held, I had to release it before calling install_uprobe.

Ah, right, so that's going to give you a head-ache ;-)

The moment you release this mmap_sem, the map you're going to install the probe point in can go away.

The only way to make this work seems to start by holding the mmap_sem for writing and make a breakpoint install function that assumes its taken and doesn't try to acquire it again.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#40 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Srikar Dronamraju

I actually dont like to release the write_lock and then reacquire it. write_opcode, which is called thro install_uprobe, i.e to insert the actual breakpoint instruction takes a read lock on the mmap_sem. Hence uprobe_mmap gets called in context with write lock on mmap_sem held, I had to release it before calling install_uprobe.

Quoted 9 lines

Ah, right, so that's going to give you a head-ache ;-)

The moment you release this mmap_sem, the map you're going to install the probe point in can go away.

The only way to make this work seems to start by holding the mmap_sem for writing and make a breakpoint install function that assumes its taken and doesn't try to acquire it again.

Yes, this can be done.
I would have to do something like this in register_uprobe().

list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
		down_read(&mm->map_sem);
                if (!install_uprobe(mm, uprobe))
                        ret = 0;
		up_read(&mm->map_sem);
                list_del(&mm->uprobes_list);
                mmput(mm);
}

Agree that this is much better than what we have now.

maintainer
#41 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 8 lines · Srikar Dronamraju
list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
                down_read(&mm->map_sem);
                if (!install_uprobe(mm, uprobe))
                        ret = 0;
                up_read(&mm->map_sem);
                list_del(&mm->uprobes_list);
                mmput(mm);
} 

and the tmp_list thing works because new mm's will hit the mmap callback and you cannot loose mm's due to the refcount, right?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#42 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Srikar Dronamraju
list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
                down_read(&mm->map_sem);
                if (!install_uprobe(mm, uprobe))
                        ret = 0;
                up_read(&mm->map_sem);
                list_del(&mm->uprobes_list);
                mmput(mm);
} 
Quoted 3 lines

and the tmp_list thing works because new mm's will hit the mmap callback and you cannot loose mm's due to the refcount, right?

Right, In other words, the tmp_list has all mm's that have already running and have this inode mapped as executable text. Those process that are yet to start or yet to map the inode as executable text will hit mmap and then we look at inserting the probes thro uprobes_mmap.

Signature · 2 lines
Thanks and Regards
Srikar
Quoted 1 line
maintainer
#43 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

In reply to Srikar Dronamraju
Quoted 39 lines · Srikar Dronamraju
kernel/uprobes.c +39 -0
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list);
+
+static void add_to_temp_list(struct vm_area_struct *vma, struct inode *inode,
+               struct list_head *tmp_list)
+{
+       struct uprobe *uprobe;
+       struct rb_node *n;
+       unsigned long flags;
+
+       n = uprobes_tree.rb_node;
+       spin_lock_irqsave(&treelock, flags);
+       while (n) {
+               uprobe = rb_entry(n, struct uprobe, rb_node);
+               if (match_inode(uprobe, inode, &n)) {
+                       list_add(&uprobe->pending_list, tmp_list);
+                       search_within_subtree(n, inode, tmp_list);
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&treelock, flags);
+}
+
+static void __search_within_subtree(struct rb_node *p, struct inode *inode,
+               struct list_head *tmp_list)
+{
+       struct uprobe *uprobe;
+
+       uprobe = rb_entry(p, struct uprobe, rb_node);
+       if (match_inode(uprobe, inode, &p)) {
+               list_add(&uprobe->pending_list, tmp_list);
+               search_within_subtree(p, inode, tmp_list);
+       }
+
+
+}
+
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list)

kernel/uprobes.c put_unlock: · line 702

Quoted 10 lines
kernel/uprobes.c +10 -0
+{
+       struct rb_node *p;
+
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+
+       p = n->rb_right;
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+} 

Whee recursion FTW!, you just blew your kernel stack :-)

Since you sort inode first, offset second, I think you can simply look for the first matching inode entry and simply rb_next() until you don't match.

maintainer
#44 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 17 lines · Peter Zijlstra

kernel/uprobes.c put_unlock: · line 702

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c +12 -0
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list)
+{
+       struct rb_node *p;
+
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+
+       p = n->rb_right;
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+} 

Whee recursion FTW!, you just blew your kernel stack :-)

Quoted 3 lines

Since you sort inode first, offset second, I think you can simply look for the first matching inode entry and simply rb_next() until you don't match.

Not to mention that p is uninitialized. Did this code ever work?

-- Steve

#45 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 18 lines · Steven Rostedt
Quoted 17 lines · Peter Zijlstra

kernel/uprobes.c put_unlock: · line 702

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c +12 -0
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list)
+{
+       struct rb_node *p;
+
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+
+       p = n->rb_right;
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+} 

Whee recursion FTW!, you just blew your kernel stack :-)

Quoted 5 lines
Quoted 3 lines

Since you sort inode first, offset second, I think you can simply look for the first matching inode entry and simply rb_next() until you don't match.

Not to mention that p is uninitialized. Did this code ever work?

I think the original patch that I sent had p initialized. I think it got dropped off by Peter when he replied. Please do confirm.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#46 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 10 lines · Srikar Dronamraju
Quoted 1 line

Not to mention that p is uninitialized. Did this code ever work?

I think the original patch that I sent had p initialized. I think it got dropped off by Peter when he replied. Please do confirm.

kernel/uprobes.c +3 -0
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list)
+{

kernel/uprobes.c put_unlock: · line 703

Quoted 10 lines
kernel/uprobes.c +10 -0
+       struct rb_node *p;
+
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+
+       p = n->rb_right;
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+}
+

The above is from the original patch. 'p' does not look initialized to me.

-- Steve

#47 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 26 lines · Steven Rostedt
Quoted 10 lines · Srikar Dronamraju
Quoted 1 line

Not to mention that p is uninitialized. Did this code ever work?

I think the original patch that I sent had p initialized. I think it got dropped off by Peter when he replied. Please do confirm.

kernel/uprobes.c +3 -0
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list)
+{

kernel/uprobes.c put_unlock: · line 703

Quoted 10 lines
kernel/uprobes.c +10 -0
+       struct rb_node *p;
+
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+
+       p = n->rb_right;
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+}
+

The above is from the original patch. 'p' does not look initialized to me.

Quoted 3 lines

-- Steve

Here is the extract from the original patch at
https://lkml.org/lkml/2010/12/16/74 that I sent to LKML and I dont see p being uninitialized.

+
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+ struct list_head *tmp_list)
+{
+ struct rb_node *p;
+
+ p = n->rb_left;
+ if (p)
+ __search_within_subtree(p, inode, tmp_list);
+
+ p = n->rb_right;
+ if (p)
+ __search_within_subtree(p, inode, tmp_list);
+}
+

However I have already agreed to remove this recursion and replace it with a rb_next() logic.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#48 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 3 lines · Srikar Dronamraju

Here is the extract from the original patch at
https://lkml.org/lkml/2010/12/16/74 that I sent to LKML and I dont see p being uninitialized.

I very much apologize. Seems that Evolution was playing tricks on me.

http://rostedt.homelinux.com/private/evolution-wtf.png

That's a screen shot of your email in the preview window. Where 'p' is definitely not initialized. WTF happened to my email?

But all the replies to that email does not have it initialized either ?? I was not the only one affected by this.

-- Steve

maintainer
#49 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 8 lines · Steven Rostedt
Quoted 3 lines · Srikar Dronamraju

Here is the extract from the original patch at
https://lkml.org/lkml/2010/12/16/74 that I sent to LKML and I dont see p being uninitialized.

I very much apologize. Seems that Evolution was playing tricks on me.

Quoted 8 lines

http://rostedt.homelinux.com/private/evolution-wtf.png

That's a screen shot of your email in the preview window. Where 'p' is definitely not initialized. WTF happened to my email?

But all the replies to that email does not have it initialized either ?? I was not the only one affected by this.

Seems to be a bug in Evolution :-p As both Peter and I tested out this email in mutt, and that line appears ?? And Peter tried it out in raw mode (^u) and it shows up there in Evolution.

I know, I know, GUI email clients suck. And for good reason too. I guess I just spent too many years in the corporate environment to know any better.

-- Steve

#50 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 18 lines · Steven Rostedt
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · Srikar Dronamraju

Here is the extract from the original patch at
https://lkml.org/lkml/2010/12/16/74 that I sent to LKML and I dont see p being uninitialized.

I very much apologize. Seems that Evolution was playing tricks on me.

Quoted 8 lines

http://rostedt.homelinux.com/private/evolution-wtf.png

That's a screen shot of your email in the preview window. Where 'p' is definitely not initialized. WTF happened to my email?

But all the replies to that email does not have it initialized either ?? I was not the only one affected by this.

Quoted 8 lines

Seems to be a bug in Evolution :-p As both Peter and I tested out this email in mutt, and that line appears ?? And Peter tried it out in raw mode (^u) and it shows up there in Evolution.

I know, I know, GUI email clients suck. And for good reason too. I guess I just spent too many years in the corporate environment to know any better.

Interesting, Nothing you can do if your mail client decides to delete one arbitrary line. Its Good that you told what you saw and hence found a bug in evolution.

Signature · 2 lines
Thanks and Regards
Srikar
Quoted 3 lines

-- Steve

maintainer
#51 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 4 lines · Srikar Dronamraju

Interesting, Nothing you can do if your mail client decides to delete one arbitrary line. Its Good that you told what you saw and hence found a bug in evolution.

Yep, I'm filling out a BZ report right now.

Thanks,

-- Steve

#52 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

In reply to Peter Zijlstra
Quoted 53 lines · Peter Zijlstra
Quoted 39 lines · Srikar Dronamraju
kernel/uprobes.c +39 -0
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list);
+
+static void add_to_temp_list(struct vm_area_struct *vma, struct inode *inode,
+               struct list_head *tmp_list)
+{
+       struct uprobe *uprobe;
+       struct rb_node *n;
+       unsigned long flags;
+
+       n = uprobes_tree.rb_node;
+       spin_lock_irqsave(&treelock, flags);
+       while (n) {
+               uprobe = rb_entry(n, struct uprobe, rb_node);
+               if (match_inode(uprobe, inode, &n)) {
+                       list_add(&uprobe->pending_list, tmp_list);
+                       search_within_subtree(n, inode, tmp_list);
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&treelock, flags);
+}
+
+static void __search_within_subtree(struct rb_node *p, struct inode *inode,
+               struct list_head *tmp_list)
+{
+       struct uprobe *uprobe;
+
+       uprobe = rb_entry(p, struct uprobe, rb_node);
+       if (match_inode(uprobe, inode, &p)) {
+               list_add(&uprobe->pending_list, tmp_list);
+               search_within_subtree(p, inode, tmp_list);
+       }
+
+
+}
+
+static void search_within_subtree(struct rb_node *n, struct inode *inode,
+               struct list_head *tmp_list)

kernel/uprobes.c put_unlock: · line 702

Quoted 10 lines
kernel/uprobes.c +10 -0
+{
+       struct rb_node *p;
+
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+
+       p = n->rb_right;
+       if (p)
+               __search_within_subtree(p, inode, tmp_list);
+} 

Whee recursion FTW!, you just blew your kernel stack :-)

Quoted 3 lines

Since you sort inode first, offset second, I think you can simply look for the first matching inode entry and simply rb_next() until you don't match.

Agree that we should get rid of recursion.

I dont think we can simply use rb_next() once we have the first matching function. There could be a matching inode but a smaller offset in left that will be missed by rb_next(). (Unless I have misunderstood rb_next() !!!)

Here are the ways I think we can workaround.
A. change the match_inode() logic to use rb_first/rb_next. This would make negate the benefit we get from rb_trees because we have to match every node. Also match_offset might get a little tricky.

B. use the current match_inode but change the search_within_subtree logic. search_within_subtree() would first find the leftmode node within the subtree that still has the same inode. Thereafter it will use rb_next().

Do you have any other ideas?

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#53 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 8/20] 8: uprobes: mmap and fork hooks.

Quoted 7 lines · Srikar Dronamraju

B. use the current match_inode but change the search_within_subtree logic. search_within_subtree() would first find the leftmode node within the subtree that still has the same inode. Thereafter it will use rb_next().

Do you have any other ideas?

Look for the right inode but with offset 0, that should get you the leftmost matching inode, or the entry left of that (depending on how you build the tree), after that you should be able to iterate all probes of that inode by using rb_next.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#54 lore raw

RFC PATCH 9/20 9: x86: architecture specific task information.

In reply to Srikar Dronamraju

On X86_64, we need to support rip relative instructions. Rip relative instructions are handled by saving the scratch register on probe hit and then retrieving the previously saved scratch register after single-step. This value stored at probe hit is specific to each task. Hence this is implemented as part of uprobe_task_arch_info.

Since x86_32 has no support for rip relative instructions, we dont need to bother for x86_32.

---
 arch/x86/include/asm/uprobes.h |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
arch/x86/include/asm/uprobes.h +5 -0
index 0063207..e38950f 100644
@@ -34,8 +34,13 @@ typedef u8 uprobe_opcode_t;
 struct uprobe_arch_info {
 	unsigned long rip_rela_target_address;
 };
+
+struct uprobe_task_arch_info {
+	unsigned long saved_scratch_register;
+};
 #else
 struct uprobe_arch_info {};
+struct uprobe_task_arch_info {};
 #endif
 struct uprobe;
 extern int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe);
#55 lore raw

RFC PATCH 10/20 10: uprobes: task specific information.

In reply to Srikar Dronamraju

Uprobes needs to maintain some task specific information include if a task is currently uprobed, the currently handing uprobe, any arch specific information (for example to handle rip relative instructions), the per-task slot where the original instruction is copied to before single-stepping.

Provides routines to create/manage and free the task specific information.

---
 include/linux/sched.h   |    3 +++
 include/linux/uprobes.h |   25 +++++++++++++++++++++++++
 kernel/fork.c           |    4 ++++
 kernel/uprobes.c        |   37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 0 deletions(-)
include/linux/sched.h +3 -0
index f4e90b6..5a3ebea 100644
@@ -1523,6 +1523,9 @@ struct task_struct {
 		unsigned long memsw_bytes; /* uncharged mem+swap usage */
 	} memcg_batch;
 #endif
+#ifdef CONFIG_UPROBES
+	struct uprobe_task *utask;
+#endif
 };
 
 /* Future-safe accessor for struct task_struct's cpus_allowed. */
include/linux/uprobes.h +25 -0
index 0d4f5e3..14a4fce 100644
@@ -26,12 +26,14 @@
 #include <linux/rbtree.h>
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 #include <asm/uprobes.h>
+struct uprobe_task_arch_info;	/* arch specific task info */
 #else
 /*
  * ARCH_SUPPORTS_UPROBES has not be defined.
  */
 typedef u8 uprobe_opcode_t;
 struct uprobe_arch_info	{};		/* arch specific info*/
+struct uprobe_task_arch_info {};	/* arch specific task info */
 
 /* Post-execution fixups.  Some architectures may define others. */
 #endif /* CONFIG_ARCH_SUPPORTS_UPROBES */
@@ -85,6 +87,27 @@ struct uprobe {
 	u8			insn[MAX_UINSN_BYTES];	/* orig instruction */
 };
 
+enum uprobe_task_state {
+	UTASK_RUNNING,
+	UTASK_BP_HIT,
+	UTASK_SSTEP
+};
+
+/*
+ * uprobe_utask -- not a user-visible struct.
+ * Corresponds to a thread in a probed process.
+ * Guarded by uproc->mutex.
+ */
+struct uprobe_task {
+	unsigned long xol_vaddr;
+	unsigned long vaddr;
+
+	enum uprobe_task_state state;
+	struct uprobe_task_arch_info tskinfo;
+
+	struct uprobe *active_uprobe;
+};
+
 /*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
@@ -108,6 +131,7 @@ extern int register_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer);
 extern void unregister_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer);
+extern void uprobe_free_utask(struct task_struct *tsk);
 
 struct vm_area_struct;
 extern void uprobe_mmap(struct vm_area_struct *vma);
@@ -126,6 +150,7 @@ static inline void uprobe_dup_mmap(struct mm_struct *old_mm,
 		struct mm_struct *mm)
 {
 }
+static inline void uprobe_free_utask(struct task_struct *tsk) {}
 static inline void uprobe_mmap(struct vm_area_struct *vma) { }
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/fork.c +4 -0
index b135d1b..ae99239 100644
@@ -191,6 +191,7 @@ void __put_task_struct(struct task_struct *tsk)
 	delayacct_tsk_free(tsk);
 	put_signal_struct(tsk->signal);
 
+	uprobe_free_utask(tsk);
 	if (!profile_handoff_task(tsk))
 		free_task(tsk);
 }
@@ -1202,6 +1203,9 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	INIT_LIST_HEAD(&p->pi_state_list);
 	p->pi_state_cache = NULL;
 #endif
+#ifdef CONFIG_UPROBES
+	p->utask = NULL;
+#endif
 	/*
 	 * sigaltstack should be cleared when sharing the same VM
 	 */
kernel/uprobes.c +37 -0
index 31867a6..f182fe6 100644
@@ -745,3 +745,40 @@ void uprobe_mmap(struct vm_area_struct *vma)
 	down_write(&mm->mmap_sem);
 }
 
+/*
+ * Called with no locks held.
+ * Called in context of a exiting or a exec-ing thread.
+ */
+void uprobe_free_utask(struct task_struct *tsk)
+{
+	struct uprobe_task *utask = tsk->utask;
+
+	if (!utask)
+		return;
+
+	if (utask->active_uprobe)
+		put_uprobe(utask->active_uprobe);
+	kfree(utask);
+	tsk->utask = NULL;
+}
+
+/*
+ * Allocate a uprobe_task object for the task.
+ * Called when the thread hits a breakpoint for the first time.
+ *
+ * Returns:
+ * - pointer to new uprobe_task on success
+ * - negative errno otherwise
+ */
+static struct uprobe_task *add_utask(void)
+{
+	struct uprobe_task *utask;
+
+	utask = kzalloc(sizeof *utask, GFP_KERNEL);
+	if (unlikely(utask == NULL))
+		return ERR_PTR(-ENOMEM);
+
+	utask->active_uprobe = NULL;
+	current->utask = utask;
+	return utask;
+}
maintainer
#56 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 10/20] 10: uprobes: task specific information.

Quoted 5 lines · Srikar Dronamraju

Uprobes needs to maintain some task specific information include if a task is currently uprobed, the currently handing uprobe, any arch specific information (for example to handle rip relative instructions), the per-task slot where the original instruction is copied to before single-stepping.

This can go away once you have per-task xol slots and boosted probes, because then you can write the complete replacement sequence on trap and never need to come back until you hit another probe, right?

Quoted 4 lines
include/linux/uprobes.h +4 -0
+/*
+ * uprobe_utask -- not a user-visible struct.
+ * Corresponds to a thread in a probed process.
+ * Guarded by uproc->mutex.

include/linux/uprobes.h struct uprobe { · line 109

Quoted 10 lines
include/linux/uprobes.h +10 -0
+ */
+struct uprobe_task {
+	unsigned long xol_vaddr;
+	unsigned long vaddr;
+
+	enum uprobe_task_state state;
+	struct uprobe_task_arch_info tskinfo;
+
+	struct uprobe *active_uprobe;
+};

So xol_vaddr is the start of the xol slot,
vaddr is the trap address, we store those so that you still have the state during the single-step things?

I guess you could obtain the xol slot information from the IP during single-step, but since you have storage anyway, this might be cheaper.

And the active_probe is again due to single-step, right? Why exactly do you need that? If you trap, acquire a new slot, write the replacement sequence, single step through it, and release the slot once you're back to the original code stream. I'm not quite seeing where you need the probe during stepping.

Ah, I think I found it while reading patch 13, you need the pre/post_xol callbacks, can't you simply synthesize their effect into the replacement sequence?

push %rax
mov $vaddr, %rax
$INSN
pop %rax
jmp $next_insn

like replacements would obviate the need for the pre/post callbacks and allow you to run straight through.

It doesn't look too hard to create simple sequences for each UPROBE_FIX_* thingy:

pre:
push %rax; mov $vaddr, %rax && UPROBE_FIX_RIP_AX push %rcx; mov $vaddr, %rcx && UPROBE_FIX_RIP_CX

INSN

post:
pop %rax && UPROBE_FIX_RIP_AX
pop %rcx && UPROBE_FIX_RIP_CX
add $correction, $offset(%rsp) && UPROBE_FIX_CALL jmp $next_insn

you already have all the logic of computing the various constants there. And your slots are 128bytes long, which should fit sequences like that just fine I think.

It would also remove the whole single-step need since they're proper boosted probes.

#57 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 10/20] 10: uprobes: task specific information.

Quoted 4 lines · Peter Zijlstra

Ah, I think I found it while reading patch 13, you need the pre/post_xol callbacks, can't you simply synthesize their effect into the replacement sequence?

Quoted 8 lines

push %rax
mov $vaddr, %rax
$INSN
pop %rax
jmp $next_insn

like replacements would obviate the need for the pre/post callbacks and allow you to run straight through.

For this particular example, you'd better be sure that $INSN doesn't need %rsp intact.

Control flow in general also makes this challenging. If $INSN is a call, then any inline fixups won't get a chance until after return. If $INSN is a jump, then its target must be modified so that both taken and not-taken paths land in respective fixup locations. I'm sure there are more cases that I'm not thinking of.

Quoted 2 lines

It would also remove the whole single-step need since they're proper boosted probes.

Kprobes has boosting, but it doesn't apply to all opcodes. I would guess that the same could be done for uprobes, where certain opcodes get a fixup sequence like you suggest, but the pre/post_xol mechanism is still needed in general.

Josh

#58 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 10/20] 10: uprobes: task specific information.

Quoted 11 lines · Peter Zijlstra
Quoted 10 lines

Ah, I think I found it while reading patch 13, you need the pre/post_xol callbacks, can't you simply synthesize their effect into the replacement sequence?

push %rax
mov $vaddr, %rax
$INSN
pop %rax
jmp $next_insn

Quoted 5 lines
Quoted 2 lines

like replacements would obviate the need for the pre/post callbacks and allow you to run straight through.

For this particular example, you'd better be sure that $INSN doesn't need %rsp intact.

In general it is quite bad form to touch the user's stack at all for instrumentation purposes. Unexpected stack usage might be what you are trying to debug, after all.

On x86-64 in particular, it is strictly verboten to touch the user's stack immediately below the SP. In the x86-64 ABI, the 128 bytes below %rsp are a scratch area for leaf functions that normal compiled user code will use to store data that must not be clobbered. (Normal signal handler frames start 128 bytes below %rsp for this reason.)

That's aside from the more obvious issues Josh mentioned, where the instruction itself is a push/pop/call/ret or uses an addressing mode relative to %rsp.

Thanks,
Roland

maintainer
#59 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 10/20] 10: uprobes: task specific information.

In reply to Josh Stone
Quoted 11 lines · Josh Stone
Quoted 10 lines · Peter Zijlstra

Ah, I think I found it while reading patch 13, you need the pre/post_xol callbacks, can't you simply synthesize their effect into the replacement sequence?

push %rax
mov $vaddr, %rax
$INSN
pop %rax
jmp $next_insn

Quoted 5 lines
Quoted 2 lines

like replacements would obviate the need for the pre/post callbacks and allow you to run straight through.

For this particular example, you'd better be sure that $INSN doesn't need %rsp intact.

Well, either that of fix up the %rsp offset, but yes I had not considered this.

Quoted 5 lines

Control flow in general also makes this challenging. If $INSN is a call, then any inline fixups won't get a chance until after return. If $INSN is a jump, then its target must be modified so that both taken and not-taken paths land in respective fixup locations. I'm sure there are more cases that I'm not thinking of.

Right.

Quoted 7 lines
Quoted 2 lines

It would also remove the whole single-step need since they're proper boosted probes.

Kprobes has boosting, but it doesn't apply to all opcodes. I would guess that the same could be done for uprobes, where certain opcodes get a fixup sequence like you suggest, but the pre/post_xol mechanism is still needed in general.

Bummer..

#60 lore raw

RFC PATCH 11/20 11: uprobes: slot allocation for uprobes

In reply to Srikar Dronamraju

Every task is allocated a fixed slot. When a probe is hit, the original instruction corresponding to the probe hit is copied to per-task fixed slot. Currently we allocate one page of slots for each mm. Bitmaps are used to know which slots are free. Each slot is made of 128 bytes so that its cache aligned.

TODO: On massively threaded processes (or if a huge number of processes share the same mm), there is a possiblilty of running out of slots. One alternative could be to extend the slots as when slots are required.

---
 include/linux/mm_types.h |    4 +
 include/linux/uprobes.h  |   21 ++++
 kernel/fork.c            |    4 +
 kernel/uprobes.c         |  233 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 262 insertions(+), 0 deletions(-)
include/linux/mm_types.h +4 -0
index af2b55d..2bc97cc 100644
@@ -12,6 +12,9 @@
 #include <linux/completion.h>
 #include <linux/cpumask.h>
 #include <linux/page-debug-flags.h>
+#ifdef CONFIG_UPROBES
+#include <linux/uprobes.h>
+#endif
 #include <asm/page.h>
 #include <asm/mmu.h>
 
@@ -316,6 +319,7 @@ struct mm_struct {
 	unsigned long uprobes_vaddr;
 	struct list_head uprobes_list;
 	atomic_t uprobes_count;
+	struct uprobes_xol_area *uprobes_xol_area;
 #endif
 };
 
include/linux/uprobes.h +21 -0
index 14a4fce..a631c42 100644
@@ -109,6 +109,25 @@ struct uprobe_task {
 };
 
 /*
+ * Every thread gets its own slot.  Once it's assigned a slot, it
+ * keeps that slot until the thread exits. Only definite number
+ * of slots are allocated.
+ */
+
+struct uprobes_xol_area {
+	spinlock_t slot_lock;	/* protects bitmap and slot (de)allocation*/
+	unsigned long *bitmap;	/* 0 = free slot */
+	struct page *page;
+
+	/*
+	 * We keep the vma's vm_start rather than a pointer to the vma
+	 * itself.  The probed process or a naughty kernel module could make
+	 * the vma go away, and we must handle that reasonably gracefully.
+	 */
+	unsigned long vaddr;		/* Page(s) of instruction slots */
+};
+
+/*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
  *
@@ -136,6 +155,7 @@ extern void uprobe_free_utask(struct task_struct *tsk);
 struct vm_area_struct;
 extern void uprobe_mmap(struct vm_area_struct *vma);
 extern void uprobe_dup_mmap(struct mm_struct *old_mm, struct mm_struct *mm);
+extern void uprobes_free_xol_area(struct mm_struct *mm);
 #else /* CONFIG_UPROBES is not defined */
 static inline int register_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer)
@@ -152,5 +172,6 @@ static inline void uprobe_dup_mmap(struct mm_struct *old_mm,
 }
 static inline void uprobe_free_utask(struct task_struct *tsk) {}
 static inline void uprobe_mmap(struct vm_area_struct *vma) { }
+static inline void uprobes_free_xol_area(struct mm_struct *mm) {}
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/fork.c +4 -0
index ae99239..76538fd 100644
@@ -545,6 +545,7 @@ void mmput(struct mm_struct *mm)
 	might_sleep();
 
 	if (atomic_dec_and_test(&mm->mm_users)) {
+		uprobes_free_xol_area(mm);
 		exit_aio(mm);
 		ksm_exit(mm);
 		exit_mmap(mm);
@@ -670,6 +671,9 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
 	memcpy(mm, oldmm, sizeof(*mm));
 
 	/* Initializing for Swap token stuff */
+#ifdef CONFIG_UPROBES
+	mm->uprobes_xol_area = NULL;
+#endif
 	mm->token_priority = 0;
 	mm->last_interval = 0;
 
kernel/uprobes.c +233 -0

262 lines not on this page · raw message

maintainer
#61 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 11/20] 11: uprobes: slot allocation for uprobes

include/linux/uprobes.h struct uprobe_task { · line 127

Quoted 6 lines · Srikar Dronamraju
include/linux/uprobes.h +6 -0
+       /*
+        * We keep the vma's vm_start rather than a pointer to the vma
+        * itself.  The probed process or a naughty kernel module could make
+        * the vma go away, and we must handle that reasonably gracefully.
+        */
+       unsigned long vaddr;            /* Page(s) of instruction slots */ 

You could simply refuse to let the user unmap that area and rogue kernel modules aren't something you can do anything about anyway.

But yeah..

#62 lore raw

RFC PATCH 12/20 12: uprobes: get the breakpoint address.

In reply to Srikar Dronamraju

On a breakpoint hit, perform a architecture specific calculation to return the address where the breakpoint was hit.

---
 include/linux/uprobes.h |    5 +++++
 kernel/uprobes.c        |   11 +++++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)
include/linux/uprobes.h +5 -0
index a631c42..ee12b2e 100644
@@ -154,6 +154,7 @@ extern void uprobe_free_utask(struct task_struct *tsk);
 
 struct vm_area_struct;
 extern void uprobe_mmap(struct vm_area_struct *vma);
+extern unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs);
 extern void uprobe_dup_mmap(struct mm_struct *old_mm, struct mm_struct *mm);
 extern void uprobes_free_xol_area(struct mm_struct *mm);
 #else /* CONFIG_UPROBES is not defined */
@@ -173,5 +174,9 @@ static inline void uprobe_dup_mmap(struct mm_struct *old_mm,
 static inline void uprobe_free_utask(struct task_struct *tsk) {}
 static inline void uprobe_mmap(struct vm_area_struct *vma) { }
 static inline void uprobes_free_xol_area(struct mm_struct *mm) {}
+static inline unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs)
+{
+	return 0;
+}
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/uprobes.c +11 -0
index 09e36f6..f486c4f 100644
@@ -976,6 +976,17 @@ static void xol_free_insn_slot(struct task_struct *tsk, unsigned long slot_addr)
 						__func__, slot_addr);
 }
 
+/**
+ * uprobes_get_bkpt_addr - compute address of bkpt given post-bkpt regs
+ * @regs: Reflects the saved state of the task after it has hit a breakpoint
+ * instruction.
+ * Return the address of the breakpoint instruction.
+ */
+unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs)
+{
+	return instruction_pointer(regs) - UPROBES_BKPT_INSN_SIZE;
+}
+
 /*
  * Called with no locks held.
  * Called in context of a exiting or a exec-ing thread.
maintainer
#63 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 12/20] 12: uprobes: get the breakpoint address.

Quoted 2 lines · Srikar Dronamraju

On a breakpoint hit, perform a architecture specific calculation to return the address where the breakpoint was hit.

And yet all the code added is generic ;-)

Quoted 51 lines
---
 include/linux/uprobes.h |    5 +++++
 kernel/uprobes.c        |   11 +++++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)
include/linux/uprobes.h +5 -0
index a631c42..ee12b2e 100644
@@ -154,6 +154,7 @@ extern void uprobe_free_utask(struct task_struct *tsk);
 
 struct vm_area_struct;
 extern void uprobe_mmap(struct vm_area_struct *vma);
+extern unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs);
 extern void uprobe_dup_mmap(struct mm_struct *old_mm, struct mm_struct *mm);
 extern void uprobes_free_xol_area(struct mm_struct *mm);
 #else /* CONFIG_UPROBES is not defined */
@@ -173,5 +174,9 @@ static inline void uprobe_dup_mmap(struct mm_struct *old_mm,
 static inline void uprobe_free_utask(struct task_struct *tsk) {}
 static inline void uprobe_mmap(struct vm_area_struct *vma) { }
 static inline void uprobes_free_xol_area(struct mm_struct *mm) {}
+static inline unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs)
+{
+	return 0;
+}
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/uprobes.c +11 -0
index 09e36f6..f486c4f 100644
@@ -976,6 +976,17 @@ static void xol_free_insn_slot(struct task_struct *tsk, unsigned long slot_addr)
 						__func__, slot_addr);
 }
 
+/**
+ * uprobes_get_bkpt_addr - compute address of bkpt given post-bkpt regs
+ * @regs: Reflects the saved state of the task after it has hit a breakpoint
+ * instruction.
+ * Return the address of the breakpoint instruction.
+ */
+unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs)
+{
+	return instruction_pointer(regs) - UPROBES_BKPT_INSN_SIZE;
+}
+
 /*
  * Called with no locks held.
  * Called in context of a exiting or a exec-ing thread.
#64 lore raw

RFC PATCH 13/20 13: x86: x86 specific probe handling

In reply to Srikar Dronamraju

Provides x86 specific implementations for setting the current instruction pointer, pre single-step and post-singlestep handling, enabling and disabling singlestep.

This patch also introduces TIF_UPROBE which is set by uprobes notifier code. TIF_UPROBE indicates that there is pending work that needs to be done at do_notify_resume time.

---
 arch/x86/include/asm/thread_info.h |    2 
 arch/x86/include/asm/uprobes.h     |    5 +
 arch/x86/kernel/uprobes.c          |  155 ++++++++++++++++++++++++++++++++++++
 3 files changed, 162 insertions(+), 0 deletions(-)
arch/x86/include/asm/thread_info.h +2 -0
index f0b6e5d..5b9c9f0 100644
@@ -84,6 +84,7 @@ struct thread_info {
 #define TIF_SECCOMP		8	/* secure computing */
 #define TIF_MCE_NOTIFY		10	/* notify userspace of an MCE */
 #define TIF_USER_RETURN_NOTIFY	11	/* notify kernel of userspace return */
+#define TIF_UPROBE		12	/* breakpointed or singlestepping */
 #define TIF_NOTSC		16	/* TSC is not accessible in userland */
 #define TIF_IA32		17	/* 32bit process */
 #define TIF_FORK		18	/* ret_from_fork */
@@ -107,6 +108,7 @@ struct thread_info {
 #define _TIF_SECCOMP		(1 << TIF_SECCOMP)
 #define _TIF_MCE_NOTIFY		(1 << TIF_MCE_NOTIFY)
 #define _TIF_USER_RETURN_NOTIFY	(1 << TIF_USER_RETURN_NOTIFY)
+#define _TIF_UPROBE		(1 << TIF_UPROBE)
 #define _TIF_NOTSC		(1 << TIF_NOTSC)
 #define _TIF_IA32		(1 << TIF_IA32)
 #define _TIF_FORK		(1 << TIF_FORK)
arch/x86/include/asm/uprobes.h +5 -0
index e38950f..0c9c8b6 100644
@@ -44,4 +44,9 @@ struct uprobe_task_arch_info {};
 #endif
 struct uprobe;
 extern int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe);
+extern void set_ip(struct pt_regs *regs, unsigned long vaddr);
+extern int pre_xol(struct uprobe *uprobe, struct pt_regs *regs);
+extern int post_xol(struct uprobe *uprobe, struct pt_regs *regs);
+extern void arch_uprobe_enable_sstep(struct pt_regs *regs);
+extern void arch_uprobe_disable_sstep(struct pt_regs *regs);
 #endif	/* _ASM_UPROBES_H */
arch/x86/kernel/uprobes.c +155 -0

158 lines not on this page · raw message

maintainer
#65 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Quoted 12 lines · Srikar Dronamraju
arch/x86/kernel/uprobes.c +11 -0
+void arch_uprobe_enable_sstep(struct pt_regs *regs)
+{
+       /*
+        * Enable single-stepping by
+        * - Set TF on stack
+        * - Set TIF_SINGLESTEP: Guarantees that TF is set when
+        *      returning to user mode.
+        *  - Indicate that TF is set by us.
+        */
+       regs->flags |= X86_EFLAGS_TF;
+       set_thread_flag(TIF_SINGLESTEP);

arch/x86/kernel/uprobes.c int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe) · line 570

Quoted 10 lines
arch/x86/kernel/uprobes.c +10 -0
+       set_thread_flag(TIF_FORCED_TF);
+}
+
+void arch_uprobe_disable_sstep(struct pt_regs *regs)
+{
+       /* Disable single-stepping by clearing what we set */
+       clear_thread_flag(TIF_SINGLESTEP);
+       clear_thread_flag(TIF_FORCED_TF);
+       regs->flags &= ~X86_EFLAGS_TF;
+} 

Why not use the code from arch/x86/kernel/step.c?

#66 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Srikar Dronamraju
arch/x86/kernel/uprobes.c +15 -0
+void arch_uprobe_enable_sstep(struct pt_regs *regs)
+{
+       /*
+        * Enable single-stepping by
+        * - Set TF on stack
+        * - Set TIF_SINGLESTEP: Guarantees that TF is set when
+        *      returning to user mode.
+        *  - Indicate that TF is set by us.
+        */
+       regs->flags |= X86_EFLAGS_TF;
+       set_thread_flag(TIF_SINGLESTEP);
+       set_thread_flag(TIF_FORCED_TF);
+}
+
+void arch_uprobe_disable_sstep(struct pt_regs *regs)
Quoted 8 lines

arch/x86/kernel/uprobes.c int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe) · line 570

Quoted 6 lines
arch/x86/kernel/uprobes.c +6 -0
+{
+       /* Disable single-stepping by clearing what we set */
+       clear_thread_flag(TIF_SINGLESTEP);
+       clear_thread_flag(TIF_FORCED_TF);
+       regs->flags &= ~X86_EFLAGS_TF;
+} 

Why not use the code from arch/x86/kernel/step.c?

user_enable_single_step and user_disable_single_step that are defined in arch/x86/kernel/step.c cant be called in interrupt context.

Initially we were looking at enabling/disabling singlestep in interrupt context. Even now we disable singlestep in post notifier in interrupt context.

Though arch/x86/kernel/step.c has a static function enable_single_step which is identical to arch_uprobe_enable_sstep; there is no equivalent function for arch_uprobe_disable_sstep.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#67 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Quoted 28 lines · Srikar Dronamraju
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Srikar Dronamraju
arch/x86/kernel/uprobes.c +15 -0
+void arch_uprobe_enable_sstep(struct pt_regs *regs)
+{
+       /*
+        * Enable single-stepping by
+        * - Set TF on stack
+        * - Set TIF_SINGLESTEP: Guarantees that TF is set when
+        *      returning to user mode.
+        *  - Indicate that TF is set by us.
+        */
+       regs->flags |= X86_EFLAGS_TF;
+       set_thread_flag(TIF_SINGLESTEP);
+       set_thread_flag(TIF_FORCED_TF);
+}
+
+void arch_uprobe_disable_sstep(struct pt_regs *regs)
Quoted 8 lines

arch/x86/kernel/uprobes.c int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe) · line 570

Quoted 6 lines
arch/x86/kernel/uprobes.c +6 -0
+{
+       /* Disable single-stepping by clearing what we set */
+       clear_thread_flag(TIF_SINGLESTEP);
+       clear_thread_flag(TIF_FORCED_TF);
+       regs->flags &= ~X86_EFLAGS_TF;
+} 

Why not use the code from arch/x86/kernel/step.c?

Quoted 2 lines

user_enable_single_step and user_disable_single_step that are defined in arch/x86/kernel/step.c cant be called in interrupt context.

Right, because of is_setting_trap_flag()..

Quoted 7 lines

Initially we were looking at enabling/disabling singlestep in interrupt context. Even now we disable singlestep in post notifier in interrupt context.

Though arch/x86/kernel/step.c has a static function enable_single_step which is identical to arch_uprobe_enable_sstep; there is no equivalent function for arch_uprobe_disable_sstep.

Its not even close to identical, its very careful to deal with user-mode already doing single step.

But I'll leave this to the x86 people who actually know the intricacies of the single step cruft, I was just wondering why you weren't using (or extending) the existing code.

#68 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Quoted 3 lines

But I'll leave this to the x86 people who actually know the intricacies of the single step cruft, I was just wondering why you weren't using (or extending) the existing code.

The hairy aspects of the step.c code are hairy (and not usable at interrupt level) because they do some instruction analysis. Since uprobes already does its own instruction analysis, reusing step.c's separate hacks makes less sense to me than integrating knowledge of the single-step vs pushf/popf issues into the uprobes instruction analysis.

That said, there is further nontriviality just to do with the block-step support and with not clobbering user-visible usage of TF in eflags, which uprobes needs to handle as well. It makes sense to share that code rather than repeating it, even if that entails changes to the step.c code.

Thanks,
Roland

#69 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Hi Roland,

Quoted 10 lines
Quoted 3 lines

But I'll leave this to the x86 people who actually know the intricacies of the single step cruft, I was just wondering why you weren't using (or extending) the existing code.

The hairy aspects of the step.c code are hairy (and not usable at interrupt level) because they do some instruction analysis. Since uprobes already does its own instruction analysis, reusing step.c's separate hacks makes less sense to me than integrating knowledge of the single-step vs pushf/popf issues into the uprobes instruction analysis.

Quoted 5 lines

That said, there is further nontriviality just to do with the block-step support and with not clobbering user-visible usage of TF in eflags, which uprobes needs to handle as well. It makes sense to share that code rather than repeating it, even if that entails changes to the step.c code.

Uprobes doesn't request/handle block-step for now. So can we postpone your suggested changes till uprobes needs to handle block-step?

Signature · 2 lines
Thanks and Regards
Srikar
#70 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Quoted 2 lines

Uprobes doesn't request/handle block-step for now. So can we postpone your suggested changes till uprobes needs to handle block-step?

That's not the issue. The way the hardware works is that if the bit is set in the MSR, then the TF eflags bit means block-step instead of single-step. So if PTRACE_SINGLEBLOCK has been used (i.e. user_enable_block_step), then this can interfere with your use of single-step. You need to do the work in the else branch of step.c:enable_step to ensure that the hardware is not left in the state where it will do block-step instead of single-step when uprobes wants a single-step done.

Thanks,
Roland

maintainer
#71 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Quoted 10 lines · Roland McGrath
Quoted 2 lines

Uprobes doesn't request/handle block-step for now. So can we postpone your suggested changes till uprobes needs to handle block-step?

That's not the issue. The way the hardware works is that if the bit is set in the MSR, then the TF eflags bit means block-step instead of single-step. So if PTRACE_SINGLEBLOCK has been used (i.e. user_enable_block_step), then this can interfere with your use of single-step. You need to do the work in the else branch of step.c:enable_step to ensure that the hardware is not left in the state where it will do block-step instead of single-step when uprobes wants a single-step done.

And reset the hardware back to block step when done, and provide the actual break blockstep would have.

Suppose you hit a breakpoint on the return path while the user it debugging in blockstep mode, that should all just work.

So there you trap on the return, switch to single step to execute the return out of line, when done you need to actually break to userspace since its the end of a block, as well as reset block mode.

#72 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 13/20] 13: x86: x86 specific probe handling

Quoted 2 lines

And reset the hardware back to block step when done, and provide the actual break blockstep would have.

Oh, sure, that too. If you're that ambitious, then the place to start first is with plain single-step working right. When TF was already set (either via user_enable_single_step, so TIF_SINGLESTEP is set, or just from user mode, so it and TIF_FORCED_TF are not set, but TF is in the user state's eflags) and you hit a uprobe, then after servicing the uprobe and stepping over the copied original instruction and restoring the PC to where it should be, you should let the trap turn into a SIGTRAP as normal rather than swallowing it.

To support block-step correctly, you have to do something more clever. If block-step was enabled (TIF_BLOCKSTEP set), then you need to figure out which of two things is the right one to do. If the copied original instruction uprobes just single-stepped over is one that would trigger block-step, then you should treat it as if plain single-step were enabled, i.e. let that SIGTRAP go as above. If not, then you should swallow the signal, re-enable block-step and set TF (i.e. do the work of user_enable_block_step) before resuming. You have to decide which case it is based on instruction analysis. If it's a control-flow instruction (including the syscall instructions), then it would trigger block-step. IIRC a conditional branch instruction triggers it only if the branch is taken (check the book), so you have to notice that too.

Thanks,
Roland

#73 lore raw

RFC PATCH 14/20 14: uprobes: Handing int3 and singlestep exception.

In reply to Srikar Dronamraju

On int3, set the TIF_UPROBE flag and if a task specific info is available, indicate the task state as breakpoint hit. Setting the TIF_UPROBE flag results in uprobe_notify_resume being called. uprobe_notify_resume walks thro the list of vmas and then matches the inode and offset corresponding to the instruction pointer to enteries in rbtree. Once a matcing uprobes is found, run the handlers for all the consumers that have registered.

On singlestep exception, perform the necessary fixups and allow the process to continue. The necessary fixups are determined at instruction analysis time.

TODO: If there is no matching uprobe, signal a trap to the process.

---
 include/linux/uprobes.h |    4 +
 kernel/uprobes.c        |  144 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 148 insertions(+), 0 deletions(-)
include/linux/uprobes.h +4 -0
index ee12b2e..a91ff42 100644
@@ -157,6 +157,9 @@ extern void uprobe_mmap(struct vm_area_struct *vma);
 extern unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs);
 extern void uprobe_dup_mmap(struct mm_struct *old_mm, struct mm_struct *mm);
 extern void uprobes_free_xol_area(struct mm_struct *mm);
+extern int uprobe_post_notifier(struct pt_regs *regs);
+extern int uprobe_bkpt_notifier(struct pt_regs *regs);
+extern void uprobe_notify_resume(struct pt_regs *regs);
 #else /* CONFIG_UPROBES is not defined */
 static inline int register_uprobe(struct inode *inode, unsigned long offset,
 				struct uprobe_consumer *consumer)
@@ -174,6 +177,7 @@ static inline void uprobe_dup_mmap(struct mm_struct *old_mm,
 static inline void uprobe_free_utask(struct task_struct *tsk) {}
 static inline void uprobe_mmap(struct vm_area_struct *vma) { }
 static inline void uprobes_free_xol_area(struct mm_struct *mm) {}
+static inline void uprobe_notify_resume(struct pt_regs *regs) {}
 static inline unsigned long uprobes_get_bkpt_addr(struct pt_regs *regs)
 {
 	return 0;
kernel/uprobes.c +144 -0

147 lines not on this page · raw message

maintainer
#74 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 14/20] 14: uprobes: Handing int3 and singlestep exception.

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1083

Quoted 5 lines · Srikar Dronamraju
kernel/uprobes.c +5 -0
+       if (unlikely(!utask)) {
+               utask = add_utask();
+
+               /* Failed to allocate utask for the current task. */
+               BUG_ON(!utask);

That's not really sane...

Quoted 2 lines
(quoted diff) +2 -0
+               utask->state = UTASK_BP_HIT;
+       } 
maintainer
#75 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 14/20] 14: uprobes: Handing int3 and singlestep exception.

In reply to Srikar Dronamraju

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1099

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c +12 -0
+               down_read(&mm->mmap_sem);
+               for (vma = mm->mmap; vma; vma = vma->vm_next) {
+                       if (!valid_vma(vma))
+                               continue;
+                       if (probept < vma->vm_start || probept > vma->vm_end)
+                               continue;
+                       u = find_uprobe(vma->vm_file->f_mapping->host,
+                                       probept - vma->vm_start);
+                       if (u)
+                               break;
+               }
+               up_read(&mm->mmap_sem); 

One has to ask, what's wrong with find_vma() ?

#76 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 14/20] 14: uprobes: Handing int3 and singlestep exception.

Quoted 14 lines · Peter Zijlstra

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1099

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c +12 -0
+               down_read(&mm->mmap_sem);
+               for (vma = mm->mmap; vma; vma = vma->vm_next) {
+                       if (!valid_vma(vma))
+                               continue;
+                       if (probept < vma->vm_start || probept > vma->vm_end)
+                               continue;
+                       u = find_uprobe(vma->vm_file->f_mapping->host,
+                                       probept - vma->vm_start);
+                       if (u)
+                               break;
+               }
+               up_read(&mm->mmap_sem); 
Quoted 1 line

One has to ask, what's wrong with find_vma() ?

Are you looking for something like this.

       down_read(&mm->mmap_sem);
	for (vma = find_vma(mm, probept); ; vma = vma->vm_next) {
	       if (!valid_vma(vma))
		       continue;
	       u = find_uprobe(vma->vm_file->f_mapping->host,
			       probept - vma->vm_start);
	       if (u)
		       break;
       }
       up_read(&mm->mmap_sem); 
maintainer
#77 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 14/20] 14: uprobes: Handing int3 and singlestep exception.

Quoted 20 lines · Srikar Dronamraju
Quoted 14 lines · Peter Zijlstra

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1099

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c +12 -0
+               down_read(&mm->mmap_sem);
+               for (vma = mm->mmap; vma; vma = vma->vm_next) {
+                       if (!valid_vma(vma))
+                               continue;
+                       if (probept < vma->vm_start || probept > vma->vm_end)
+                               continue;
+                       u = find_uprobe(vma->vm_file->f_mapping->host,
+                                       probept - vma->vm_start);
+                       if (u)
+                               break;
+               }
+               up_read(&mm->mmap_sem); 
Quoted 1 line

One has to ask, what's wrong with find_vma() ?

Are you looking for something like this.

Quoted 10 lines
       down_read(&mm->mmap_sem);
	for (vma = find_vma(mm, probept); ; vma = vma->vm_next) {
	       if (!valid_vma(vma))
		       continue;
	       u = find_uprobe(vma->vm_file->f_mapping->host,
			       probept - vma->vm_start);
	       if (u)
		       break;
       }
       up_read(&mm->mmap_sem); 

How could you ever need to iterate here? There is only a single vma that covers the probe point, if that doesn't find a uprobe, there isn't any.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#78 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 14/20] 14: uprobes: Handing int3 and singlestep exception.

Quoted 32 lines · Peter Zijlstra
Quoted 20 lines · Srikar Dronamraju
Quoted 14 lines · Peter Zijlstra

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1099

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c +12 -0
+               down_read(&mm->mmap_sem);
+               for (vma = mm->mmap; vma; vma = vma->vm_next) {
+                       if (!valid_vma(vma))
+                               continue;
+                       if (probept < vma->vm_start || probept > vma->vm_end)
+                               continue;
+                       u = find_uprobe(vma->vm_file->f_mapping->host,
+                                       probept - vma->vm_start);
+                       if (u)
+                               break;
+               }
+               up_read(&mm->mmap_sem); 
Quoted 1 line

One has to ask, what's wrong with find_vma() ?

Are you looking for something like this.

Quoted 10 lines
       down_read(&mm->mmap_sem);
	for (vma = find_vma(mm, probept); ; vma = vma->vm_next) {
	       if (!valid_vma(vma))
		       continue;
	       u = find_uprobe(vma->vm_file->f_mapping->host,
			       probept - vma->vm_start);
	       if (u)
		       break;
       }
       up_read(&mm->mmap_sem); 
Quoted 2 lines

How could you ever need to iterate here? There is only a single vma that covers the probe point, if that doesn't find a uprobe, there isn't any.

Agree.
So it simplifies to

        down_read(&mm->mmap_sem);
 	vma = find_vma(mm, probept);
        if (valid_vma(vma)) {
 	       u = find_uprobe(vma->vm_file->f_mapping->host,
 			       probept - vma->vm_start);
        }
        up_read(&mm->mmap_sem); 
Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#79 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 14/20] 14: uprobes: Handing int3 and singlestep exception.

Quoted 9 lines · Srikar Dronamraju

So it simplifies to

        down_read(&mm->mmap_sem);
        vma = find_vma(mm, probept);
        if (valid_vma(vma)) {
               u = find_uprobe(vma->vm_file->f_mapping->host,
                               probept - vma->vm_start);
        }
        up_read(&mm->mmap_sem); 

Almost, the offset within a file is something like:

(address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#80 lore raw

RFC PATCH 15/20 15: x86: uprobes exception notifier for x86.

In reply to Srikar Dronamraju

Provides a uprobes exception notifier for x86. This uprobes_exception notifier gets called in interrupt context and routes int3 and singlestep exception when a uprobed process encounters a INT3 or a singlestep exception.

---
 arch/x86/include/asm/uprobes.h |    3 +++
 arch/x86/kernel/signal.c       |   14 ++++++++++++++
 arch/x86/kernel/uprobes.c      |   29 +++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 0 deletions(-)
arch/x86/include/asm/uprobes.h +3 -0
index 0c9c8b6..bb5c03c 100644
@@ -22,6 +22,7 @@
  *	Srikar Dronamraju
  *	Jim Keniston
  */
+#include <linux/notifier.h>
 
 typedef u8 uprobe_opcode_t;
 #define MAX_UINSN_BYTES 16
@@ -49,4 +50,6 @@ extern int pre_xol(struct uprobe *uprobe, struct pt_regs *regs);
 extern int post_xol(struct uprobe *uprobe, struct pt_regs *regs);
 extern void arch_uprobe_enable_sstep(struct pt_regs *regs);
 extern void arch_uprobe_disable_sstep(struct pt_regs *regs);
+extern int uprobes_exception_notify(struct notifier_block *self,
+				       unsigned long val, void *data);
 #endif	/* _ASM_UPROBES_H */
arch/x86/kernel/signal.c +14 -0
index 4fd173c..0b06b94 100644
@@ -20,6 +20,7 @@
 #include <linux/personality.h>
 #include <linux/uaccess.h>
 #include <linux/user-return-notifier.h>
+#include <linux/uprobes.h>
 
 #include <asm/processor.h>
 #include <asm/ucontext.h>
@@ -848,6 +849,19 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
 	if (thread_info_flags & _TIF_SIGPENDING)
 		do_signal(regs);
 
+	if (thread_info_flags & _TIF_UPROBE) {
+		clear_thread_flag(TIF_UPROBE);
+#ifdef CONFIG_X86_32
+		/*
+		 * On x86_32, do_notify_resume() gets called with
+		 * interrupts disabled. Hence enable interrupts if they
+		 * are still disabled.
+		 */
+		local_irq_enable();
+#endif
+		uprobe_notify_resume(regs);
+	}
+
 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 		clear_thread_flag(TIF_NOTIFY_RESUME);
 		tracehook_notify_resume(regs);
arch/x86/kernel/uprobes.c +29 -0
index 9a0b8a9..c4239b3 100644
@@ -568,3 +568,32 @@ void arch_uprobe_disable_sstep(struct pt_regs *regs)
 	clear_thread_flag(TIF_FORCED_TF);
 	regs->flags &= ~X86_EFLAGS_TF;
 }
+
+/*
+ * Wrapper routine for handling exceptions.
+ */
+int uprobes_exception_notify(struct notifier_block *self,
+				       unsigned long val, void *data)
+{
+	struct die_args *args = data;
+	struct pt_regs *regs = args->regs;
+	int ret = NOTIFY_DONE;
+
+	/* We are only interested in userspace traps */
+	if (regs && !user_mode_vm(regs))
+		return NOTIFY_DONE;
+
+	switch (val) {
+	case DIE_INT3:
+		/* Run your handler here */
+		if (uprobe_bkpt_notifier(regs))
+			ret = NOTIFY_STOP;
+		break;
+	case DIE_DEBUG:
+		if (uprobe_post_notifier(regs))
+			ret = NOTIFY_STOP;
+	default:
+		break;
+	}
+	return ret;
+}
#81 lore raw

RFC PATCH 16/20 16: uprobes: register a notifier for uprobes.

In reply to Srikar Dronamraju

Uprobe needs to be intimated on int3 and singlestep exceptions. Hence uprobes registers a die notifier so that its notified of the events.

---
 kernel/uprobes.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/uprobes.c +19 -0
index 3d21d8f..93a3118 100644
@@ -34,6 +34,7 @@
 #include <linux/rmap.h> /* needed for anon_vma_prepare */
 #include <linux/mman.h>	/* needed for PROT_EXEC, MAP_PRIVATE */
 #include <linux/file.h> /* needed for fput() */
+#include <linux/kdebug.h> /* needed for notifier mechanism */
 
 #define UINSNS_PER_PAGE	(PAGE_SIZE/UPROBES_XOL_SLOT_BYTES)
 #define MAX_UPROBES_XOL_SLOTS UINSNS_PER_PAGE
@@ -1170,3 +1171,21 @@ int uprobe_post_notifier(struct pt_regs *regs)
 	}
 	return 0;
 }
+
+struct notifier_block uprobes_exception_nb = {
+	.notifier_call = uprobes_exception_notify,
+	.priority = 0x7ffffff0,
+};
+
+static int __init init_uprobes(void)
+{
+	register_die_notifier(&uprobes_exception_nb);
+	return 0;
+}
+
+static void __exit exit_uprobes(void)
+{
+}
+
+module_init(init_uprobes);
+module_exit(exit_uprobes);
maintainer
#82 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 16/20] 16: uprobes: register a notifier for uprobes.

Quoted 2 lines · Srikar Dronamraju

Uprobe needs to be intimated on int3 and singlestep exceptions. Hence uprobes registers a die notifier so that its notified of the events.

Why isn't this part of the previous patch? This splitup really doesn't make sense.

#83 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 16/20] 16: uprobes: register a notifier for uprobes.

Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Srikar Dronamraju

Uprobe needs to be intimated on int3 and singlestep exceptions. Hence uprobes registers a die notifier so that its notified of the events.

Why isn't this part of the previous patch? This splitup really doesn't make sense.

The die notifier which is introduced in patch 15 is arch dependent (i.e x86). Whereever possible I have kept the arch dependent parts separate from the arch independent parts so that porting to another architecture becomes easier and clear.

Signature · 2 lines
Thanks and Regards
Srikar
#84 lore raw

RFC PATCH 17/20 17: uprobes: filter chain

In reply to Srikar Dronamraju

Loops through the filters callbacks of currently registered consumers to see if any consumer is interested in tracing this task.

---
 kernel/uprobes.c |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
kernel/uprobes.c +18 -0
index 93a3118..b0d323c 100644
@@ -429,6 +429,24 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
 }
 
 /* Acquires uprobe->consumer_rwsem */
+static bool filter_chain(struct uprobe *uprobe, struct task_struct *t)
+{
+	struct uprobe_consumer *consumer;
+	bool ret = false;
+
+	down_read(&uprobe->consumer_rwsem);
+	for (consumer = uprobe->consumers; consumer;
+					consumer = consumer->next) {
+		if (!consumer->filter || consumer->filter(consumer, t)) {
+			ret = true;
+			break;
+		}
+	}
+	up_read(&uprobe->consumer_rwsem);
+	return ret;
+}
+
+/* Acquires uprobe->consumer_rwsem */
 static void add_consumer(struct uprobe *uprobe,
 				struct uprobe_consumer *consumer)
 {
#85 lore raw

RFC PATCH 18/20 18: uprobes: commonly used filters.

In reply to Srikar Dronamraju

Provides most commonly used filters. Prevents users from having to define their own filters. However this would be useful once we can dynamically associate a filter with a uprobe-event tracer.

---
 kernel/uprobes.c |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)
kernel/uprobes.c +41 -0
index b0d323c..44a7ff6 100644
@@ -1190,6 +1190,46 @@ int uprobe_post_notifier(struct pt_regs *regs)
 	return 0;
 }
 
+bool uprobes_pid_filter(struct uprobe_consumer *self, struct task_struct *t)
+{
+	if (t->tgid == (long)self->fvalue)
+		return true;
+	return false;
+}
+
+bool uprobes_tid_filter(struct uprobe_consumer *self, struct task_struct *t)
+{
+	if (t->pid == (long)self->fvalue)
+		return true;
+	return false;
+}
+
+bool uprobes_ppid_filter(struct uprobe_consumer *self, struct task_struct *t)
+{
+	pid_t pid;
+
+	rcu_read_lock();
+	pid = task_tgid_vnr(t->real_parent);
+	rcu_read_unlock();
+
+	if (pid == (long)self->fvalue)
+		return true;
+	return false;
+}
+
+bool uprobes_sid_filter(struct uprobe_consumer *self, struct task_struct *t)
+{
+	pid_t pid;
+
+	rcu_read_lock();
+	pid = pid_vnr(task_session(t));
+	rcu_read_unlock();
+
+	if (pid == (long)self->fvalue)
+		return true;
+	return false;
+}
+
 struct notifier_block uprobes_exception_nb = {
 	.notifier_call = uprobes_exception_notify,
 	.priority = 0x7ffffff0,
@@ -1207,3 +1247,4 @@ static void __exit exit_uprobes(void)
 
 module_init(init_uprobes);
 module_exit(exit_uprobes);
+
#86 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 18/20] 18: uprobes: commonly used filters.

Quoted 3 lines · Srikar Dronamraju

Provides most commonly used filters. Prevents users from having to define their own filters. However this would be useful once we can dynamically associate a filter with a uprobe-event tracer.

Unclear/awkward language here...

Did you mean "Prevents users from defining their own filters" or "Allows users to not have to define their own filters"?

#87 lore raw

Re: [RFC] [PATCH 2.6.37-rc5-tip 18/20] 18: uprobes: commonly used filters.

Quoted 10 lines · * [email protected] <[email protected]> [2010-12-17 14:32:59]:
Quoted 3 lines · Srikar Dronamraju

Provides most commonly used filters. Prevents users from having to define their own filters. However this would be useful once we can dynamically associate a filter with a uprobe-event tracer.

Unclear/awkward language here...

Did you mean "Prevents users from defining their own filters" or "Allows users to not have to define their own filters"?

The later one. From my perspective, users would prefer to filter based on tid, pid, sid. So by defining these common/generic filters within uprobes, we help users from not having to redine these filters. However it no way prevents users from defining their filters. Also users can still go ahead and add a filter for the same purpose for which we already have defined a filter.

If you think user would like to filter based on some other parameter that most users would be interested, then do let me know, I will try to add a filter for the same.

--
Thanks and Regards
Srikar

#88 lore raw

RFC PATCH 19/20 19: tracing: Extract out common code for kprobes/uprobes traceevents.

In reply to Srikar Dronamraju

Move parts of trace_kprobe.c that can be shared with upcoming trace_uprobe.c. Common code to kernel/trace/trace_probe.h and kernel/trace/trace_probe.c.

---
 kernel/trace/Kconfig        |    4 
 kernel/trace/Makefile       |    1 
 kernel/trace/trace_kprobe.c |  752 +------------------------------------------
 kernel/trace/trace_probe.c  |  648 +++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_probe.h  |  155 +++++++++
 5 files changed, 823 insertions(+), 737 deletions(-)
 create mode 100644 kernel/trace/trace_probe.c
 create mode 100644 kernel/trace/trace_probe.h
kernel/trace/Kconfig +4 -0
index ea37e2f..09ad930 100644
@@ -358,6 +358,7 @@ config KPROBE_EVENT
 	depends on HAVE_REGS_AND_STACK_ACCESS_API
 	bool "Enable kprobes-based dynamic events"
 	select TRACING
+	select PROBE_EVENTS
 	default y
 	help
 	  This allows the user to add tracing events (similar to tracepoints)
@@ -370,6 +371,9 @@ config KPROBE_EVENT
 	  This option is also required by perf-probe subcommand of perf tools.
 	  If you want to use perf tools, this option is strongly recommended.
 
+config PROBE_EVENTS
+	def_bool n
+
 config DYNAMIC_FTRACE
 	bool "enable/disable ftrace tracepoints dynamically"
 	depends on FUNCTION_TRACER
kernel/trace/Makefile +1 -0
index 53f3381..95d2043 100644
@@ -56,5 +56,6 @@ obj-$(CONFIG_EVENT_TRACING) += power-traces.o
 ifeq ($(CONFIG_TRACING),y)
 obj-$(CONFIG_KGDB_KDB) += trace_kdb.o
 endif
+obj-$(CONFIG_PROBE_EVENTS) +=trace_probe.o
 
 libftrace-y := ftrace.o
kernel/trace/trace_kprobe.c +15 -737

841 lines not on this page · raw message

kernel/trace/trace_probe.c new file +648 -0

648 lines not on this page · raw message

kernel/trace/trace_probe.h new file +155 -0

155 lines not on this page · raw message

#89 lore raw

RFC PATCH 20/20 20: tracing: uprobes trace_event interface

In reply to Srikar Dronamraju

Implements trace_event support for uprobes. In its current form it can be used to put probes at a specified offset in a file and dump the required registers when the code flow reaches the probed address.

The following example shows how to dump the instruction pointer and %ax a register at the probed text address. Here we are trying to probe zfree in /bin/zsh

# cd /sys/kernel/debug/tracing/
# cat /proc/`pgrep  zsh`/maps | grep /bin/zsh | grep r-xp
00400000-0048a000 r-xp 00000000 08:03 130904 /bin/zsh
# objdump -T /bin/zsh | grep -w zfree
0000000000446420 g    DF .text  0000000000000012  Base        zfree
# echo 'p /bin/zsh:0x46420 %ip %ax' > uprobe_events
# cat uprobe_events
p:uprobes/p_zsh_0x46420 /bin/zsh:0x0000000000046420
# echo 1 > events/uprobes/enable
# sleep 20
# echo 0 > events/uprobes/enable
# cat trace
# tracer: nop
#
#           TASK-PID    CPU#    TIMESTAMP  FUNCTION
#              | |       |          |         |
             zsh-24842 [006] 258544.995456: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [007] 258545.000270: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [002] 258545.043929: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [004] 258547.046129: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79

TODO: Documentation/trace/uprobetrace.txt
TODO: dynamically allocate a consumer at probe enable time and remove the
consumer from trace_uprobe structure.

---
 arch/Kconfig                |   10 -
 kernel/trace/Kconfig        |   16 +
 kernel/trace/Makefile       |    1 
 kernel/trace/trace.h        |    5 
 kernel/trace/trace_kprobe.c |    4 
 kernel/trace/trace_probe.c  |   14 +
 kernel/trace/trace_probe.h  |    6 
 kernel/trace/trace_uprobe.c |  753 +++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 792 insertions(+), 17 deletions(-)
 create mode 100644 kernel/trace/trace_uprobe.c
arch/Kconfig +1 -9
index bba8108..c4e9663 100644
@@ -62,16 +62,8 @@ config OPTPROBES
 	depends on !PREEMPT
 
 config UPROBES
-	bool "User-space probes (EXPERIMENTAL)"
-	depends on ARCH_SUPPORTS_UPROBES
-	depends on MMU
 	select MM_OWNER
-	help
-	  Uprobes enables kernel subsystems to establish probepoints
-	  in user applications and execute handler functions when
-	  the probepoints are hit. For more information, refer to
-	  Documentation/uprobes.txt.
-	  If in doubt, say "N".
+	def_bool n
 
 config HAVE_EFFICIENT_UNALIGNED_ACCESS
 	bool
kernel/trace/Kconfig +16 -0
index 09ad930..0030f1e 100644
@@ -371,6 +371,22 @@ config KPROBE_EVENT
 	  This option is also required by perf-probe subcommand of perf tools.
 	  If you want to use perf tools, this option is strongly recommended.
 
+config UPROBE_EVENT
+	bool "Enable uprobes-based dynamic events"
+	depends on ARCH_SUPPORTS_UPROBES
+	depends on MMU
+	select UPROBES
+	select PROBE_EVENTS
+	select TRACING
+	default n
+	help
+	  This allows the user to add tracing events on top of userspace dynamic
+	  events (similar to tracepoints) on the fly via the traceevents interface.
+	  Those events can be inserted wherever uprobes can probe, and record
+	  various registers.
+	  This option is required if you plan to use perf-probe subcommand of perf
+	  tools on user space applications.
+
 config PROBE_EVENTS
 	def_bool n
 
kernel/trace/Makefile +1 -0
index 95d2043..67a12e3 100644
@@ -57,5 +57,6 @@ ifeq ($(CONFIG_TRACING),y)
 obj-$(CONFIG_KGDB_KDB) += trace_kdb.o
 endif
 obj-$(CONFIG_PROBE_EVENTS) +=trace_probe.o
+obj-$(CONFIG_UPROBE_EVENT) += trace_uprobe.o
 
 libftrace-y := ftrace.o
kernel/trace/trace.h +5 -0
index 9021f8c..73ca49e 100644
@@ -97,6 +97,11 @@ struct kretprobe_trace_entry_head {
 	unsigned long		ret_ip;
 };
 
+struct uprobe_trace_entry_head {
+	struct trace_entry	ent;
+	unsigned long		ip;
+};
+
 /*
  * trace_flag_type is an enumeration that holds different
  * states when a trace occurs. These are:
kernel/trace/trace_kprobe.c +2 -2
index 1045ed7..bb116b6 100644
@@ -374,8 +374,8 @@ static int create_trace_probe(int argc, char **argv)
 		}
 
 		/* Parse fetch argument */
-		ret = traceprobe_parse_probe_arg(arg, &tp->size, &tp->args[i],
-								is_return);
+		ret = traceprobe_parse_probe_arg(arg, &tp->size,
+					&tp->args[i], is_return, true);
 		if (ret) {
 			pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
 			goto error;
kernel/trace/trace_probe.c +10 -4
index 4ca48c6..5123d1c 100644
@@ -443,13 +443,17 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
 
 /* Recursive argument parser */
 static int parse_probe_arg(char *arg, const struct fetch_type *t,
-		     struct fetch_param *f, bool is_return)
+		     struct fetch_param *f, bool is_return, bool is_kprobe)
 {
 	int ret = 0;
 	unsigned long param;
 	long offset;
 	char *tmp;
 
+	/* Until uprobe_events supports only reg arguments */
+	if (!is_kprobe && arg[0] != '%')
+		return -EINVAL;
+
 	switch (arg[0]) {
 	case '$':
 		ret = parse_probe_vars(arg + 1, t, f, is_return);
@@ -500,7 +504,8 @@ static int parse_probe_arg(char *arg, const struct fetch_type *t,
 			if (!dprm)
 				return -ENOMEM;
 			dprm->offset = offset;
-			ret = parse_probe_arg(arg, t2, &dprm->orig, is_return);
+			ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
+							is_kprobe);
 			if (ret)
 				kfree(dprm);
 			else {
@@ -520,7 +525,7 @@ static int parse_probe_arg(char *arg, const struct fetch_type *t,
 
 /* String length checking wrapper */
 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
-		struct probe_arg *parg, bool is_return)
+		struct probe_arg *parg, bool is_return, bool is_kprobe)
 {
 	const char *t;
 	int ret;
@@ -546,7 +551,8 @@ int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
 	}
 	parg->offset = *size;
 	*size += parg->type->size;
-	ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
+	ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return,
+							is_kprobe);
 
 	if (ret >= 0) {
 		parg->fetch_size.fn = get_fetch_size_function(parg->type,
kernel/trace/trace_probe.h +4 -2
index 5352fd2..6f9a75d 100644
@@ -48,6 +48,7 @@
 #define FIELD_STRING_IP "__probe_ip"
 #define FIELD_STRING_RETIP "__probe_ret_ip"
 #define FIELD_STRING_FUNC "__probe_func"
+#define FIELD_STRING_PID "__probe_pid"
 
 #undef DEFINE_FIELD
 #define DEFINE_FIELD(type, item, name, is_signed)			\
@@ -64,6 +65,7 @@
 /* Flags for trace_probe */
 #define TP_FLAG_TRACE	1
 #define TP_FLAG_PROFILE	2
+#define TP_FLAG_UPROBE	4
 
 
 /* data_rloc: data relative location, compatible with u32 */
@@ -129,7 +131,7 @@ static inline __kprobes void call_fetch(struct fetch_param *fprm,
 }
 
 /* Check the name is good for event/group/fields */
-static int is_good_name(const char *name)
+static inline int is_good_name(const char *name)
 {
 	if (!isalpha(*name) && *name != '_')
 		return 0;
@@ -141,7 +143,7 @@ static int is_good_name(const char *name)
 }
 
 extern int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
-		   struct probe_arg *parg, bool is_return);
+		   struct probe_arg *parg, bool is_return, bool is_kprobe);
 
 extern int traceprobe_conflict_field_name(const char *name,
 			       struct probe_arg *args, int narg);
kernel/trace/trace_uprobe.c new file +753 -0

753 lines not on this page · raw message

#90 lore raw
In reply to Srikar Dronamraju

Sorry, the first line got edited while sending, please read this as.

This patchset implements Uprobes which enables you to dynamically break

Quoted 3 lines

into any routine in a user space application and collect information non-disruptively.