Re: [PATCH v3 2.6.39-rc1-tip 5/26] 5: uprobes: Adding and removea uprobe in a rb tree.

From: Peter Zijlstra
Date: Mon Apr 18 2011 - 08:21:55 EST


On Fri, 2011-04-01 at 20:03 +0530, Srikar Dronamraju wrote:
> +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;
> +}

Here you break out the match functions for some reason.

> +/*
> + * 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;
> +}

And here you open-code the match functions..

Why not have something like:

static int match_probe(struct uprobe *l, struct uprobe *r)
{
if (l->inode < r->inode)
return -1;
else if (l->inode > r->inode)
return 1;
else {
if (l->offset < r->offset)
return -1;
else if (l->offset > r->offset)
return 1;
}

return 0;
}

And use that as:

static struct uprobe *
__find_uprobe(struct inode *inode, loff_t offset)
{
struct uprobe r = { .inode = inode, .offset = offset };
struct rb_node *n = uprobes_tree.rb_node;
struct uprobe *uprobe;
int match;

while (n) {
uprobe = rb_node(n, struct uprobe, rb_node);
match = match_probe(uprobe, &r);
if (!match) {
atomic_inc(&uprobe->ref);
return uprobe;
}
if (match < 0)
n = n->rb_left;
else
n = n->rb_right;
}

return NULL;
}

static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
{
struct rb_node **p = &uprobes_tree.rb_node;
struct rn_node *parent = NULL;
struct uprobe *u;
int match;

while (*p) {
parent = *p;
u = rb_entry(parent, struct uprobe, rb_node);
match = match_uprobe(u, uprobe);
if (!match) {
atomic_inc(&u->ref);
return u;
}
if (match < 0)
p = &parent->rb_left;
else
p = &parent->rb_right;
}

atomic_set(&uprobe->ref, 2);
rb_link_node(&uprobe->rb_node, parent, p);
rb_insert_color(&uprobe->rb_node, &uprobes_tree);
return uprobe;
}

Isn't that much nicer?
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/