This is the mail archive of the
systemtap@sources.redhat.com
mailing list for the systemtap project.
[RFC PATCH 1/6] kprobes: remove global kprobe_lock - base changes
- From: Ananth N Mavinakayanahalli <ananth at in dot ibm dot com>
- To: systemtap at sources dot redhat dot com
- Cc: ak at muc dot de, davem at davemloft dot net
- Date: Fri, 29 Jul 2005 14:53:13 -0400
- Subject: [RFC PATCH 1/6] kprobes: remove global kprobe_lock - base changes
- References: <20050729185150.GM1692@in.ibm.com>
- Reply-to: ananth at in dot ibm dot com
Base kprobes infrastructure changes to remove the global spinlock
Signed-off-by: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
include/linux/kprobes.h | 8 ++++----
kernel/kprobes.c | 47 ++++++++++++++++++++---------------------------
2 files changed, 24 insertions(+), 31 deletions(-)
Index: linux-2.6.13-rc3/include/linux/kprobes.h
===================================================================
--- linux-2.6.13-rc3.orig/include/linux/kprobes.h 2005-07-29 12:49:50.000000000 -0400
+++ linux-2.6.13-rc3/include/linux/kprobes.h 2005-07-29 12:50:39.000000000 -0400
@@ -33,6 +33,7 @@
#include <linux/list.h>
#include <linux/notifier.h>
#include <linux/smp.h>
+#include <linux/spinlock.h>
#include <asm/kprobes.h>
@@ -142,10 +143,9 @@ struct kretprobe_instance {
};
#ifdef CONFIG_KPROBES
-/* Locks kprobe: irq must be disabled */
-void lock_kprobes(void);
-void unlock_kprobes(void);
+extern rwlock_t kprobe_lock;
+extern spinlock_t kretprobe_lock;
extern int arch_prepare_kprobe(struct kprobe *p);
extern void arch_copy_kprobe(struct kprobe *p);
extern void arch_arm_kprobe(struct kprobe *p);
@@ -156,7 +156,7 @@ extern void show_registers(struct pt_reg
extern kprobe_opcode_t *get_insn_slot(void);
extern void free_insn_slot(kprobe_opcode_t *slot);
-/* Get the kprobe at this addr (if any). Must have called lock_kprobes */
+/* Get the kprobe at this addr. Call with "read" kprobe_lock held */
struct kprobe *get_kprobe(void *addr);
struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
Index: linux-2.6.13-rc3/kernel/kprobes.c
===================================================================
--- linux-2.6.13-rc3.orig/kernel/kprobes.c 2005-07-29 12:49:50.000000000 -0400
+++ linux-2.6.13-rc3/kernel/kprobes.c 2005-07-29 12:50:39.000000000 -0400
@@ -32,7 +32,6 @@
* <prasanna@in.ibm.com> added function-return probes.
*/
#include <linux/kprobes.h>
-#include <linux/spinlock.h>
#include <linux/hash.h>
#include <linux/init.h>
#include <linux/module.h>
@@ -48,8 +47,8 @@
static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
-unsigned int kprobe_cpu = NR_CPUS;
-static DEFINE_SPINLOCK(kprobe_lock);
+DEFINE_RWLOCK(kprobe_lock);
+DEFINE_SPINLOCK(kretprobe_lock);
static DEFINE_PER_CPU(struct kprobe *, curr_kprobe) = NULL;
/*
@@ -152,19 +151,6 @@ void __kprobes free_insn_slot(kprobe_opc
}
}
-/* Locks kprobe: irqs must be disabled */
-void __kprobes lock_kprobes(void)
-{
- spin_lock(&kprobe_lock);
- kprobe_cpu = smp_processor_id();
-}
-
-void __kprobes unlock_kprobes(void)
-{
- kprobe_cpu = NR_CPUS;
- spin_unlock(&kprobe_lock);
-}
-
/* We have preemption disabled.. so it is safe to use __ versions */
static inline void set_curr_kprobe(struct kprobe *kp)
{
@@ -254,6 +240,7 @@ static int __kprobes aggr_break_handler(
return ret;
}
+/* Called with kretprobe_lock held */
struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
{
struct hlist_node *node;
@@ -263,6 +250,7 @@ struct kretprobe_instance __kprobes *get
return NULL;
}
+/* Called with kretprobe_lock held */
static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
*rp)
{
@@ -273,6 +261,7 @@ static struct kretprobe_instance __kprob
return NULL;
}
+/* Called with kretprobe_lock held */
void __kprobes add_rp_inst(struct kretprobe_instance *ri)
{
/*
@@ -291,6 +280,7 @@ void __kprobes add_rp_inst(struct kretpr
hlist_add_head(&ri->uflist, &ri->rp->used_instances);
}
+/* Called with kretprobe_lock held */
void __kprobes recycle_rp_inst(struct kretprobe_instance *ri)
{
/* remove rp inst off the rprobe_inst_table */
@@ -324,13 +314,13 @@ void __kprobes kprobe_flush_task(struct
struct hlist_node *node, *tmp;
unsigned long flags = 0;
- spin_lock_irqsave(&kprobe_lock, flags);
+ spin_lock_irqsave(&kretprobe_lock, flags);
head = kretprobe_inst_table_head(current);
hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
if (ri->task == tk)
recycle_rp_inst(ri);
}
- spin_unlock_irqrestore(&kprobe_lock, flags);
+ spin_unlock_irqrestore(&kretprobe_lock, flags);
}
/*
@@ -341,9 +331,12 @@ static int __kprobes pre_handler_kretpro
struct pt_regs *regs)
{
struct kretprobe *rp = container_of(p, struct kretprobe, kp);
+ unsigned long flags = 0;
/*TODO: consider to only swap the RA after the last pre_handler fired */
+ spin_lock_irqsave(&kretprobe_lock, flags);
arch_prepare_kretprobe(rp, regs);
+ spin_unlock_irqrestore(&kretprobe_lock, flags);
return 0;
}
@@ -409,7 +402,7 @@ static inline void add_aggr_kprobe(struc
/*
* This is the second or subsequent kprobe at the address - handle
* the intricacies
- * TODO: Move kcalloc outside the spinlock
+ * TODO: Move kcalloc outside the write_lock
*/
static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
struct kprobe *p)
@@ -436,7 +429,7 @@ static inline void cleanup_kprobe(struct
{
arch_disarm_kprobe(p);
hlist_del(&p->hlist);
- spin_unlock_irqrestore(&kprobe_lock, flags);
+ write_unlock_irqrestore(&kprobe_lock, flags);
arch_remove_kprobe(p);
}
@@ -448,7 +441,7 @@ static inline void cleanup_aggr_kprobe(s
cleanup_kprobe(old_p, flags);
kfree(old_p);
} else
- spin_unlock_irqrestore(&kprobe_lock, flags);
+ write_unlock_irqrestore(&kprobe_lock, flags);
}
static int __kprobes in_kprobes_functions(unsigned long addr)
@@ -470,7 +463,7 @@ int __kprobes register_kprobe(struct kpr
if ((ret = arch_prepare_kprobe(p)) != 0)
goto rm_kprobe;
- spin_lock_irqsave(&kprobe_lock, flags);
+ write_lock_irqsave(&kprobe_lock, flags);
old_p = get_kprobe(p->addr);
p->nmissed = 0;
if (old_p) {
@@ -486,7 +479,7 @@ int __kprobes register_kprobe(struct kpr
arch_arm_kprobe(p);
out:
- spin_unlock_irqrestore(&kprobe_lock, flags);
+ write_unlock_irqrestore(&kprobe_lock, flags);
rm_kprobe:
if (ret == -EEXIST)
arch_remove_kprobe(p);
@@ -498,7 +491,7 @@ void __kprobes unregister_kprobe(struct
unsigned long flags;
struct kprobe *old_p;
- spin_lock_irqsave(&kprobe_lock, flags);
+ write_lock_irqsave(&kprobe_lock, flags);
old_p = get_kprobe(p->addr);
if (old_p) {
if (old_p->pre_handler == aggr_pre_handler)
@@ -506,7 +499,7 @@ void __kprobes unregister_kprobe(struct
else
cleanup_kprobe(p, flags);
} else
- spin_unlock_irqrestore(&kprobe_lock, flags);
+ write_unlock_irqrestore(&kprobe_lock, flags);
}
static struct notifier_block kprobe_exceptions_nb = {
@@ -581,13 +574,13 @@ void __kprobes unregister_kretprobe(stru
unregister_kprobe(&rp->kp);
/* No race here */
- spin_lock_irqsave(&kprobe_lock, flags);
+ spin_lock_irqsave(&kretprobe_lock, flags);
free_rp_inst(rp);
while ((ri = get_used_rp_inst(rp)) != NULL) {
ri->rp = NULL;
hlist_del(&ri->uflist);
}
- spin_unlock_irqrestore(&kprobe_lock, flags);
+ spin_unlock_irqrestore(&kretprobe_lock, flags);
}
static int __init init_kprobes(void)