From: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
To: Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@elte.hu>
Cc: Steven Rostedt <rostedt@goodmis.org>,
Srikar Dronamraju <srikar@linux.vnet.ibm.com>,
Linux-mm <linux-mm@kvack.org>,
Arnaldo Carvalho de Melo <acme@infradead.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
Ananth N Mavinakayanahalli <ananth@in.ibm.com>,
Christoph Hellwig <hch@infradead.org>,
Andi Kleen <andi@firstfloor.org>,
Thomas Gleixner <tglx@linutronix.de>,
Jonathan Corbet <corbet@lwn.net>, Oleg Nesterov <oleg@redhat.com>,
LKML <linux-kernel@vger.kernel.org>,
Jim Keniston <jkenisto@linux.vnet.ibm.com>,
Roland McGrath <roland@hack.frob.com>,
SystemTap <systemtap@sources.redhat.com>,
Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH v3 2.6.39-rc1-tip 5/26] 5: uprobes: Adding and remove a uprobe in a rb tree.
Date: Fri, 01 Apr 2011 20:03:28 +0530 [thread overview]
Message-ID: <20110401143328.15455.19094.sendpatchset@localhost6.localdomain6> (raw)
In-Reply-To: <20110401143223.15455.19844.sendpatchset@localhost6.localdomain6>
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.
Signed-off-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
---
include/linux/uprobes.h | 12 ++
kernel/uprobes.c | 226 ++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 234 insertions(+), 4 deletions(-)
diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index ae134a5..bfe2e9e 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -23,6 +23,7 @@
* Jim Keniston
*/
+#include <linux/rbtree.h>
#ifdef CONFIG_ARCH_SUPPORTS_UPROBES
#include <asm/uprobes.h>
#else
@@ -50,6 +51,17 @@ typedef u8 uprobe_opcode_t;
/* Unexported functions & macros for use by arch-specific code */
#define uprobe_opcode_sz (sizeof(uprobe_opcode_t))
+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;
+};
+
/*
* Most architectures can use the default versions of @read_opcode(),
* @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
diff --git a/kernel/uprobes.c b/kernel/uprobes.c
index 9ef21a7..f37418b 100644
--- a/kernel/uprobes.c
+++ b/kernel/uprobes.c
@@ -33,10 +33,28 @@
#include <linux/rmap.h> /* needed for anon_vma_prepare */
struct uprobe {
+ struct rb_node rb_node; /* node in the rb tree */
+ atomic_t ref; /* lifetime muck */
+ struct rw_semaphore consumer_rwsem;
+ struct uprobe_consumer *consumers;
+ struct inode *inode; /* we hold a ref */
+ loff_t offset;
u8 insn[MAX_UINSN_BYTES];
u16 fixups;
};
+static bool valid_vma(struct vm_area_struct *vma)
+{
+ if (!vma->vm_file)
+ return false;
+
+ if ((vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)) ==
+ (VM_READ|VM_EXEC))
+ return true;
+
+ return false;
+}
+
/*
* NOTE:
* Expect the breakpoint instruction to be the smallest size instruction for
@@ -83,8 +101,7 @@ static int write_opcode(struct task_struct *tsk, struct uprobe * uprobe,
* adding probes in write mapped pages since the breakpoints
* might end up in the file copy.
*/
- if ((vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)) !=
- (VM_READ|VM_EXEC))
+ if (!valid_vma(vma))
goto put_out;
/* Allocate a page */
@@ -164,8 +181,7 @@ int __weak read_opcode(struct task_struct *tsk, unsigned long vaddr,
* adding probes in write mapped pages since the breakpoints
* might end up in the file copy.
*/
- if ((vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)) !=
- (VM_READ|VM_EXEC))
+ if (!valid_vma(vma))
goto put_out;
lock_page(page);
@@ -252,3 +268,205 @@ bool __weak is_bkpt_insn(u8 *insn)
memcpy(&opcode, insn, UPROBES_BKPT_INSN_SIZE);
return (opcode == UPROBES_BKPT_INSN);
}
+
+static struct rb_root uprobes_tree = RB_ROOT;
+static DEFINE_SPINLOCK(treelock);
+
+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, loff_t 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;
+}
+
+
+/* Called with treelock held */
+static struct uprobe *__find_uprobe(struct inode * inode,
+ loff_t offset, struct rb_node **near_match)
+{
+ struct rb_node *n = uprobes_tree.rb_node;
+ struct uprobe *uprobe, *u = NULL;
+
+ while (n) {
+ uprobe = rb_entry(n, struct uprobe, rb_node);
+ if (match_inode(uprobe, inode, &n)) {
+ if (near_match)
+ *near_match = n;
+ if (match_offset(uprobe, offset, &n)) {
+ /* get access ref */
+ atomic_inc(&uprobe->ref);
+ u = uprobe;
+ break;
+ }
+ }
+ }
+ return u;
+}
+
+/*
+ * Find a uprobe corresponding to a given inode:offset
+ * Acquires treelock
+ */
+static struct uprobe *find_uprobe(struct inode * inode, loff_t offset)
+{
+ struct uprobe *uprobe;
+ unsigned long flags;
+
+ spin_lock_irqsave(&treelock, flags);
+ uprobe = __find_uprobe(inode, offset, NULL);
+ spin_unlock_irqrestore(&treelock, flags);
+ return uprobe;
+}
+
+/*
+ * Acquires treelock.
+ * Matching uprobe already exists in rbtree;
+ * increment (access refcount) and return the matching uprobe.
+ *
+ * No matching uprobe; insert the uprobe in rb_tree;
+ * get a double refcount (access + creation) and return NULL.
+ */
+static struct uprobe *insert_uprobe(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)
+ 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 {
+ /* get access ref */
+ atomic_inc(&u->ref);
+ goto unlock_return;
+ }
+ }
+ }
+ u = NULL;
+ rb_link_node(&uprobe->rb_node, parent, p);
+ rb_insert_color(&uprobe->rb_node, &uprobes_tree);
+ /* get access + drop ref */
+ atomic_set(&uprobe->ref, 2);
+
+unlock_return:
+ spin_unlock_irqrestore(&treelock, flags);
+ return u;
+}
+
+static void put_uprobe(struct uprobe *uprobe)
+{
+ if (atomic_dec_and_test(&uprobe->ref))
+ kfree(uprobe);
+}
+
+static struct uprobe *uprobes_add(struct inode *inode, loff_t offset)
+{
+ struct uprobe *uprobe, *cur_uprobe;
+
+ uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
+ if (!uprobe)
+ return NULL;
+
+ __iget(inode);
+ uprobe->inode = inode;
+ uprobe->offset = offset;
+ init_rwsem(&uprobe->consumer_rwsem);
+
+ /* add to uprobes_tree, sorted on inode:offset */
+ cur_uprobe = insert_uprobe(uprobe);
+
+ /* a uprobe exists for this inode:offset combination*/
+ if (cur_uprobe) {
+ kfree(uprobe);
+ uprobe = cur_uprobe;
+ iput(inode);
+ }
+ return uprobe;
+}
+
+static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
+{
+ struct uprobe_consumer *consumer;
+
+ down_read(&uprobe->consumer_rwsem);
+ consumer = uprobe->consumers;
+ while (consumer) {
+ if (!consumer->filter || consumer->filter(consumer, current))
+ consumer->handler(consumer, regs);
+
+ consumer = consumer->next;
+ }
+ up_read(&uprobe->consumer_rwsem);
+}
+
+static void add_consumer(struct uprobe *uprobe,
+ struct uprobe_consumer *consumer)
+{
+ down_write(&uprobe->consumer_rwsem);
+ consumer->next = uprobe->consumers;
+ uprobe->consumers = consumer;
+ up_write(&uprobe->consumer_rwsem);
+}
+
+/*
+ * For uprobe @uprobe, delete the consumer @consumer.
+ * Return true if the @consumer is deleted successfully
+ * or return false.
+ */
+static bool del_consumer(struct uprobe *uprobe,
+ struct uprobe_consumer *consumer)
+{
+ struct uprobe_consumer *con;
+ bool ret = false;
+
+ down_write(&uprobe->consumer_rwsem);
+ con = uprobe->consumers;
+ if (consumer == con) {
+ uprobe->consumers = con->next;
+ if (!con->next)
+ put_uprobe(uprobe); /* drop creation ref */
+ ret = true;
+ } else {
+ for (; con; con = con->next) {
+ if (con->next == consumer) {
+ con->next = consumer->next;
+ ret = true;
+ break;
+ }
+ }
+ }
+ up_write(&uprobe->consumer_rwsem);
+ return ret;
+}
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2011-04-01 14:43 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-01 14:32 [PATCH v3 2.6.39-rc1-tip 0/26] 0: Uprobes patchset with perf probe support Srikar Dronamraju
2011-04-01 14:32 ` [PATCH v3 2.6.39-rc1-tip 1/26] 1: mm: replace_page() loses static attribute Srikar Dronamraju
2011-04-01 14:32 ` [PATCH v3 2.6.39-rc1-tip 2/26] 2: mm: Move replace_page() to mm/memory.c Srikar Dronamraju
2011-04-01 14:33 ` [PATCH v3 2.6.39-rc1-tip 3/26] 3: X86 specific breakpoint definitions Srikar Dronamraju
2011-04-01 14:33 ` [PATCH v3 2.6.39-rc1-tip 4/26] 4: uprobes: Breakground page replacement Srikar Dronamraju
2011-04-18 12:20 ` Peter Zijlstra
2011-04-01 14:33 ` Srikar Dronamraju [this message]
2011-04-18 12:20 ` [PATCH v3 2.6.39-rc1-tip 5/26] 5: uprobes: Adding and remove a uprobe in a rb tree Peter Zijlstra
2011-04-01 14:33 ` [PATCH v3 2.6.39-rc1-tip 6/26] 6: Uprobes: register/unregister probes Srikar Dronamraju
2011-04-02 0:26 ` Stephen Wilson
2011-04-02 0:53 ` Srikar Dronamraju
2011-04-02 1:29 ` Stephen Wilson
2011-04-18 12:20 ` Peter Zijlstra
2011-04-01 14:33 ` [PATCH v3 2.6.39-rc1-tip 7/26] 7: x86: analyze instruction and determine fixups Srikar Dronamraju
2011-04-19 13:29 ` Steven Rostedt
2011-04-21 17:31 ` Srikar Dronamraju
2011-04-21 17:52 ` Peter Zijlstra
2011-04-01 14:33 ` [PATCH v3 2.6.39-rc1-tip 8/26] 8: uprobes: store/restore original instruction Srikar Dronamraju
2011-04-18 16:12 ` Peter Zijlstra
2011-04-01 14:34 ` [PATCH v3 2.6.39-rc1-tip 9/26] 9: uprobes: mmap and fork hooks Srikar Dronamraju
2011-04-18 16:21 ` Peter Zijlstra
2011-04-18 16:29 ` Peter Zijlstra
2011-04-19 6:45 ` Srikar Dronamraju
2011-04-01 14:34 ` [PATCH v3 2.6.39-rc1-tip 10/26] 10: x86: architecture specific task information Srikar Dronamraju
2011-04-01 14:34 ` [PATCH v3 2.6.39-rc1-tip 11/26] 11: uprobes: task specific information Srikar Dronamraju
2011-04-01 14:34 ` [PATCH v3 2.6.39-rc1-tip 12/26] 12: uprobes: slot allocation for uprobes Srikar Dronamraju
2011-04-18 16:46 ` Peter Zijlstra
2011-04-19 6:26 ` Srikar Dronamraju
2011-04-19 9:02 ` Peter Zijlstra
2011-04-19 9:11 ` Peter Zijlstra
2011-04-20 13:40 ` Eric Paris
2011-04-20 14:51 ` Frank Ch. Eigler
2011-04-20 15:16 ` Stephen Smalley
2011-04-21 14:11 ` Srikar Dronamraju
2011-04-21 14:45 ` Eric Paris
2011-04-21 16:14 ` Roland McGrath
2011-04-21 16:59 ` Srikar Dronamraju
2011-04-01 14:35 ` [PATCH v3 2.6.39-rc1-tip 13/26] 13: uprobes: get the breakpoint address Srikar Dronamraju
2011-04-18 16:47 ` Peter Zijlstra
2011-04-01 14:35 ` [PATCH v3 2.6.39-rc1-tip 14/26] 14: x86: x86 specific probe handling Srikar Dronamraju
2011-04-18 16:55 ` Peter Zijlstra
2011-04-19 5:43 ` Srikar Dronamraju
2011-04-18 16:57 ` Peter Zijlstra
2011-04-01 14:35 ` [PATCH v3 2.6.39-rc1-tip 15/26] 15: uprobes: Handing int3 and singlestep exception Srikar Dronamraju
2011-04-19 13:03 ` Peter Zijlstra
2011-04-19 13:12 ` Steven Rostedt
2011-04-21 17:10 ` Srikar Dronamraju
2011-04-21 17:41 ` Peter Zijlstra
2011-04-19 13:39 ` Peter Zijlstra
2011-04-21 17:03 ` Srikar Dronamraju
2011-04-01 14:35 ` [PATCH v3 2.6.39-rc1-tip 16/26] 16: x86: uprobes exception notifier for x86 Srikar Dronamraju
2011-04-01 14:35 ` [PATCH v3 2.6.39-rc1-tip 17/26] 17: uprobes: register a notifier for uprobes Srikar Dronamraju
2011-04-19 13:54 ` Peter Zijlstra
2011-04-01 14:36 ` [PATCH v3 2.6.39-rc1-tip 18/26] 18: uprobes: commonly used filters Srikar Dronamraju
2011-04-05 1:26 ` Masami Hiramatsu
2011-04-19 13:57 ` Peter Zijlstra
2011-04-21 11:09 ` Srikar Dronamraju
2011-04-21 11:37 ` Peter Zijlstra
2011-04-21 11:49 ` Srikar Dronamraju
2011-04-01 14:36 ` [PATCH v3 2.6.39-rc1-tip 19/26] 19: tracing: Extract out common code for kprobes/uprobes traceevents Srikar Dronamraju
2011-04-01 14:36 ` [PATCH v3 2.6.39-rc1-tip 20/26] 20: tracing: uprobes trace_event interface Srikar Dronamraju
2011-04-01 14:36 ` [PATCH v3 2.6.39-rc1-tip 21/26] 21: Signed-off-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Srikar Dronamraju
2011-04-01 14:47 ` [RESEND] [PATCH v3 2.6.39-rc1-tip 21/26] 21: Uprobe tracer documentation " Srikar Dronamraju
2011-04-01 14:36 ` [PATCH v3 2.6.39-rc1-tip 22/26] 22: perf: rename target_module to target Srikar Dronamraju
2011-04-04 10:06 ` Masami Hiramatsu
2011-04-06 23:46 ` Srikar Dronamraju
2011-04-01 14:37 ` [PATCH v3 2.6.39-rc1-tip 23/26] 23: perf: show possible probes in a given executable file or library Srikar Dronamraju
2011-04-04 10:15 ` Masami Hiramatsu
2011-04-06 22:50 ` Srikar Dronamraju
2011-04-01 14:37 ` [PATCH v3 2.6.39-rc1-tip 24/26] 24: perf: perf interface for uprobes Srikar Dronamraju
2011-04-01 14:37 ` [PATCH v3 2.6.39-rc1-tip 25/26] 25: perf: Documentation for perf uprobes Srikar Dronamraju
2011-04-01 14:37 ` [PATCH v3 2.6.39-rc1-tip 26/26] 26: uprobes: filter chain Srikar Dronamraju
2011-04-05 1:27 ` Masami Hiramatsu
2011-04-06 22:41 ` Srikar Dronamraju
2011-04-07 3:19 ` Masami Hiramatsu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110401143328.15455.19094.sendpatchset@localhost6.localdomain6 \
--to=srikar@linux.vnet.ibm.com \
--cc=acme@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=ananth@in.ibm.com \
--cc=andi@firstfloor.org \
--cc=corbet@lwn.net \
--cc=hch@infradead.org \
--cc=jkenisto@linux.vnet.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=masami.hiramatsu.pt@hitachi.com \
--cc=mingo@elte.hu \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=roland@hack.frob.com \
--cc=rostedt@goodmis.org \
--cc=systemtap@sources.redhat.com \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).