bpf: add helper for copying attrs to struct bpf_map
authorJakub Kicinski <jakub.kicinski@netronome.com>
Fri, 12 Jan 2018 04:29:06 +0000 (20:29 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Sun, 14 Jan 2018 22:36:29 +0000 (23:36 +0100)
All map types reimplement the field-by-field copy of union bpf_attr
members into struct bpf_map.  Add a helper to perform this operation.

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
include/linux/bpf.h
kernel/bpf/cpumap.c
kernel/bpf/devmap.c
kernel/bpf/hashtab.c
kernel/bpf/lpm_trie.c
kernel/bpf/sockmap.c
kernel/bpf/stackmap.c
kernel/bpf/syscall.c

index 263598619f908f9fb04d939f51d3ce26ebb15743..c60ddfb34d41f5b3c40c0cb4ae6f9555107cb09b 100644 (file)
@@ -378,6 +378,7 @@ void bpf_map_put(struct bpf_map *map);
 int bpf_map_precharge_memlock(u32 pages);
 void *bpf_map_area_alloc(size_t size, int numa_node);
 void bpf_map_area_free(void *base);
+void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
 
 extern int sysctl_unprivileged_bpf_disabled;
 
index ce5b669003b279640313aaf9dbe9a91256070e8b..192151ec9d1263698e2aba2179b5b3b47218cb38 100644 (file)
@@ -94,13 +94,7 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
        if (!cmap)
                return ERR_PTR(-ENOMEM);
 
-       /* mandatory map attributes */
-       cmap->map.map_type = attr->map_type;
-       cmap->map.key_size = attr->key_size;
-       cmap->map.value_size = attr->value_size;
-       cmap->map.max_entries = attr->max_entries;
-       cmap->map.map_flags = attr->map_flags;
-       cmap->map.numa_node = bpf_map_attr_numa_node(attr);
+       bpf_map_init_from_attr(&cmap->map, attr);
 
        /* Pre-limit array size based on NR_CPUS, not final CPU check */
        if (cmap->map.max_entries > NR_CPUS) {
index ebdef54bf7df2b898e7d03a5ddc3b0c8a7554bbb..565f9ece911519274370d9c043137d74d56d1a37 100644 (file)
@@ -93,13 +93,7 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
        if (!dtab)
                return ERR_PTR(-ENOMEM);
 
-       /* mandatory map attributes */
-       dtab->map.map_type = attr->map_type;
-       dtab->map.key_size = attr->key_size;
-       dtab->map.value_size = attr->value_size;
-       dtab->map.max_entries = attr->max_entries;
-       dtab->map.map_flags = attr->map_flags;
-       dtab->map.numa_node = bpf_map_attr_numa_node(attr);
+       bpf_map_init_from_attr(&dtab->map, attr);
 
        /* make sure page count doesn't overflow */
        cost = (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *);
index 7fd6519444d396df142de2c453b1d524ab4c9a14..b76828f23b49a06c6329e0265652da5c1a6999fc 100644 (file)
@@ -304,7 +304,6 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
         */
        bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
        bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
-       int numa_node = bpf_map_attr_numa_node(attr);
        struct bpf_htab *htab;
        int err, i;
        u64 cost;
@@ -313,13 +312,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
        if (!htab)
                return ERR_PTR(-ENOMEM);
 
-       /* mandatory map attributes */
-       htab->map.map_type = attr->map_type;
-       htab->map.key_size = attr->key_size;
-       htab->map.value_size = attr->value_size;
-       htab->map.max_entries = attr->max_entries;
-       htab->map.map_flags = attr->map_flags;
-       htab->map.numa_node = numa_node;
+       bpf_map_init_from_attr(&htab->map, attr);
 
        if (percpu_lru) {
                /* ensure each CPU's lru list has >=1 elements.
index 885e45479680508a11681290c0a52bc25c849c3b..584e02227671bc6d26423a07338fabd556f761d0 100644 (file)
@@ -522,12 +522,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)
                return ERR_PTR(-ENOMEM);
 
        /* copy mandatory map attributes */
-       trie->map.map_type = attr->map_type;
-       trie->map.key_size = attr->key_size;
-       trie->map.value_size = attr->value_size;
-       trie->map.max_entries = attr->max_entries;
-       trie->map.map_flags = attr->map_flags;
-       trie->map.numa_node = bpf_map_attr_numa_node(attr);
+       bpf_map_init_from_attr(&trie->map, attr);
        trie->data_size = attr->key_size -
                          offsetof(struct bpf_lpm_trie_key, data);
        trie->max_prefixlen = trie->data_size * 8;
index 079968680bc36231aa8181b13d282829ff1b0d3d..0314d1783d77adf823beedd0b0934f8e6de0680a 100644 (file)
@@ -513,13 +513,7 @@ static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
        if (!stab)
                return ERR_PTR(-ENOMEM);
 
-       /* mandatory map attributes */
-       stab->map.map_type = attr->map_type;
-       stab->map.key_size = attr->key_size;
-       stab->map.value_size = attr->value_size;
-       stab->map.max_entries = attr->max_entries;
-       stab->map.map_flags = attr->map_flags;
-       stab->map.numa_node = bpf_map_attr_numa_node(attr);
+       bpf_map_init_from_attr(&stab->map, attr);
 
        /* make sure page count doesn't overflow */
        cost = (u64) stab->map.max_entries * sizeof(struct sock *);
index 6c63c2222ea8abc8dc423db41b69c58764c326f3..b0ecf43f5894d12de9a20f4399e79e0b6d2979b8 100644 (file)
@@ -88,14 +88,10 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
        if (cost >= U32_MAX - PAGE_SIZE)
                goto free_smap;
 
-       smap->map.map_type = attr->map_type;
-       smap->map.key_size = attr->key_size;
+       bpf_map_init_from_attr(&smap->map, attr);
        smap->map.value_size = value_size;
-       smap->map.max_entries = attr->max_entries;
-       smap->map.map_flags = attr->map_flags;
        smap->n_buckets = n_buckets;
        smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
-       smap->map.numa_node = bpf_map_attr_numa_node(attr);
 
        err = bpf_map_precharge_memlock(smap->map.pages);
        if (err)
index c0ac03a04880f85b2010dfc92381c113cf7855ed..a3f726bb42eacfd25f4d7d583bf9f5fb43a909d0 100644 (file)
@@ -143,6 +143,16 @@ void bpf_map_area_free(void *area)
        kvfree(area);
 }
 
+void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
+{
+       map->map_type = attr->map_type;
+       map->key_size = attr->key_size;
+       map->value_size = attr->value_size;
+       map->max_entries = attr->max_entries;
+       map->map_flags = attr->map_flags;
+       map->numa_node = bpf_map_attr_numa_node(attr);
+}
+
 int bpf_map_precharge_memlock(u32 pages)
 {
        struct user_struct *user = get_current_user();