lkml.org 
[lkml]   [2015]   [Mar]   [2]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH 05/15] bpf: Export a map-clearing function
Date
Add a new map_clear() function to bpf_map_ops along with a
tracing_map_clear() export for external users.

Map implementations that it makes sense for should implement it,
otherwise it's not required. The bpf hashtab implementation does
implement a clear operation, but since it doesn't make sense for bpf
arraymaps, the arraymap implementation doesn't.

Signed-off-by: Tom Zanussi <tom.zanussi@linux.intel.com>
---
include/linux/bpf.h | 2 ++
kernel/bpf/hashtab.c | 8 ++++++++
kernel/bpf/syscall.c | 17 +++++++++++++++++
3 files changed, 27 insertions(+)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 900405bf..f7f95d7 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -18,6 +18,7 @@ struct bpf_map_ops {
/* funcs callable from userspace (via syscall) */
struct bpf_map *(*map_alloc)(union bpf_attr *attr);
void (*map_free)(struct bpf_map *);
+ void (*map_clear)(struct bpf_map *);
int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);

/* funcs callable from userspace and from eBPF programs */
@@ -144,6 +145,7 @@ extern struct bpf_func_proto bpf_map_delete_elem_proto;

struct bpf_map *tracing_map_create(union bpf_attr *attr);
void tracing_map_destroy(struct bpf_map *map);
+void tracing_map_clear(struct bpf_map *map);
int tracing_map_update_elem(struct bpf_map *map, void *key, void *value,
union bpf_attr *attr);
int tracing_map_lookup_elem(struct bpf_map *map, void *key, void *uvalue);
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index b3ba436..addf3a8 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -325,6 +325,13 @@ static void delete_all_elements(struct bpf_htab *htab)
}
}

+static void htab_map_clear(struct bpf_map *map)
+{
+ struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+
+ delete_all_elements(htab);
+}
+
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
static void htab_map_free(struct bpf_map *map)
{
@@ -348,6 +355,7 @@ static void htab_map_free(struct bpf_map *map)
static struct bpf_map_ops htab_ops = {
.map_alloc = htab_map_alloc,
.map_free = htab_map_free,
+ .map_clear = htab_map_clear,
.map_get_next_key = htab_map_get_next_key,
.map_lookup_elem = htab_map_lookup_elem,
.map_update_elem = htab_map_update_elem,
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index cac8df6..0f28904 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -362,6 +362,23 @@ int tracing_map_delete_elem(struct bpf_map *map, void *key)
}
EXPORT_SYMBOL_GPL(tracing_map_delete_elem);

+/**
+ * tracing_map_clear - Clear a bpf_map
+ * @map: The bpf_map to clear
+ *
+ * Clear the bpf_map.
+ *
+ * Return: nothing, map clearing always succeeds
+ */
+void tracing_map_clear(struct bpf_map *map)
+{
+ rcu_read_lock();
+ if (map->ops->map_clear)
+ map->ops->map_clear(map);
+ rcu_read_unlock();
+}
+EXPORT_SYMBOL_GPL(tracing_map_clear);
+
#define BPF_MAP_DELETE_ELEM_LAST_FIELD key

static int map_delete_elem(union bpf_attr *attr)
--
1.9.3


\
 
 \ /
  Last update: 2015-03-02 17:21    [W:0.993 / U:0.108 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site