[PATCH 10/15] bpf: Make tracing_map use kmalloc/kfree_notrace()

From: Tom Zanussi
Date: Mon Mar 02 2015 - 11:05:42 EST


We need to prevent any kmallocs that could be invoked from within a
tracepoint handler from being traced, in order to prevent recursion.

For tracing maps, this means the allocation of the map elements (maps
themselves along with their associated buckets, etc, are never
allocated in the context of a tracepoint handler, so we don't need to
worry about them).

We also want the matching kfrees to remain untraced as well.

With this, we should be able to use maps when tracing kmalloc, which
otherwise would lock up the machine.

Signed-off-by: Tom Zanussi <tom.zanussi@xxxxxxxxxxxxxxx>
---
kernel/bpf/hashtab.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 6f349ad..308ef47 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -200,6 +200,15 @@ find_first_elem:
return -ENOENT;
}

+/* Use this instead of kfree_rcu() for notrace-kfreeing
+ * kmalloc_notrace()'d elements, to avoid creating a
+ * kfree_rcu_notrace() */
+static void kfree_htab_elem(struct rcu_head *head)
+{
+ struct htab_elem *elem = container_of(head, struct htab_elem, rcu);
+ kfree_notrace(elem);
+}
+
/* Called from syscall or from eBPF program */
static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
u64 map_flags)
@@ -218,7 +227,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
WARN_ON_ONCE(!rcu_read_lock_held());

/* allocate new element outside of lock */
- l_new = kmalloc(htab->elem_size, GFP_ATOMIC);
+ l_new = kmalloc_notrace(htab->elem_size, GFP_ATOMIC);
if (!l_new)
return -ENOMEM;

@@ -262,7 +271,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
hlist_add_head_rcu(&l_new->hash_node, head);
if (l_old) {
hlist_del_rcu(&l_old->hash_node);
- kfree_rcu(l_old, rcu);
+ call_rcu(&l_old->rcu, kfree_htab_elem);
} else {
htab->count++;
}
@@ -271,7 +280,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
return 0;
err:
spin_unlock_irqrestore(&htab->lock, flags);
- kfree(l_new);
+ kfree_notrace(l_new);
return ret;
}

@@ -311,7 +320,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key)
free_client_elem(htab, l);
hlist_del_rcu(&l->hash_node);
htab->count--;
- kfree_rcu(l, rcu);
+ call_rcu(&l->rcu, kfree_htab_elem);
ret = 0;
}

@@ -332,7 +341,7 @@ static void delete_all_elements(struct bpf_htab *htab)
free_client_elem(htab, l);
hlist_del_rcu(&l->hash_node);
htab->count--;
- kfree(l);
+ kfree_notrace(l);
}
}
}
--
1.9.3

--
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/