Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016 Facebook |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | #include <linux/bpf.h> |
| 14 | #include <linux/jhash.h> |
| 15 | #include <linux/filter.h> |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 16 | #include <linux/rculist_nulls.h> |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 17 | #include "percpu_freelist.h" |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 18 | #include "bpf_lru_list.h" |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 19 | #include "map_in_map.h" |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 20 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 21 | #define HTAB_CREATE_FLAG_MASK \ |
| 22 | (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \ |
| 23 | BPF_F_RDONLY | BPF_F_WRONLY) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 24 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 25 | struct bucket { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 26 | struct hlist_nulls_head head; |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 27 | raw_spinlock_t lock; |
| 28 | }; |
| 29 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 30 | struct bpf_htab { |
| 31 | struct bpf_map map; |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 32 | struct bucket *buckets; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 33 | void *elems; |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 34 | union { |
| 35 | struct pcpu_freelist freelist; |
| 36 | struct bpf_lru lru; |
| 37 | }; |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 38 | struct htab_elem *__percpu *extra_elems; |
tom.leiming@gmail.com | 6591f1e | 2015-12-29 22:40:25 +0800 | [diff] [blame] | 39 | atomic_t count; /* number of elements in this hashtable */ |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 40 | u32 n_buckets; /* number of hash buckets */ |
| 41 | u32 elem_size; /* size of each element in bytes */ |
| 42 | }; |
| 43 | |
| 44 | /* each htab element is struct htab_elem + key + value */ |
| 45 | struct htab_elem { |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 46 | union { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 47 | struct hlist_nulls_node hash_node; |
Alexei Starovoitov | 9f69154 | 2017-03-07 20:00:12 -0800 | [diff] [blame] | 48 | struct { |
| 49 | void *padding; |
| 50 | union { |
| 51 | struct bpf_htab *htab; |
| 52 | struct pcpu_freelist_node fnode; |
| 53 | }; |
| 54 | }; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 55 | }; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 56 | union { |
| 57 | struct rcu_head rcu; |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 58 | struct bpf_lru_node lru_node; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 59 | }; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 60 | u32 hash; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 61 | char key[0] __aligned(8); |
| 62 | }; |
| 63 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 64 | static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node); |
| 65 | |
| 66 | static bool htab_is_lru(const struct bpf_htab *htab) |
| 67 | { |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 68 | return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH || |
| 69 | htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH; |
| 70 | } |
| 71 | |
| 72 | static bool htab_is_percpu(const struct bpf_htab *htab) |
| 73 | { |
| 74 | return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 75 | htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH; |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 78 | static bool htab_is_prealloc(const struct bpf_htab *htab) |
| 79 | { |
| 80 | return !(htab->map.map_flags & BPF_F_NO_PREALLOC); |
| 81 | } |
| 82 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 83 | static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size, |
| 84 | void __percpu *pptr) |
| 85 | { |
| 86 | *(void __percpu **)(l->key + key_size) = pptr; |
| 87 | } |
| 88 | |
| 89 | static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size) |
| 90 | { |
| 91 | return *(void __percpu **)(l->key + key_size); |
| 92 | } |
| 93 | |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 94 | static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l) |
| 95 | { |
| 96 | return *(void **)(l->key + roundup(map->key_size, 8)); |
| 97 | } |
| 98 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 99 | static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i) |
| 100 | { |
| 101 | return (struct htab_elem *) (htab->elems + i * htab->elem_size); |
| 102 | } |
| 103 | |
| 104 | static void htab_free_elems(struct bpf_htab *htab) |
| 105 | { |
| 106 | int i; |
| 107 | |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 108 | if (!htab_is_percpu(htab)) |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 109 | goto free_elems; |
| 110 | |
| 111 | for (i = 0; i < htab->map.max_entries; i++) { |
| 112 | void __percpu *pptr; |
| 113 | |
| 114 | pptr = htab_elem_get_ptr(get_htab_elem(htab, i), |
| 115 | htab->map.key_size); |
| 116 | free_percpu(pptr); |
Eric Dumazet | 9147efc | 2017-12-12 14:22:39 -0800 | [diff] [blame] | 117 | cond_resched(); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 118 | } |
| 119 | free_elems: |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 120 | bpf_map_area_free(htab->elems); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 123 | static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key, |
| 124 | u32 hash) |
| 125 | { |
| 126 | struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash); |
| 127 | struct htab_elem *l; |
| 128 | |
| 129 | if (node) { |
| 130 | l = container_of(node, struct htab_elem, lru_node); |
| 131 | memcpy(l->key, key, htab->map.key_size); |
| 132 | return l; |
| 133 | } |
| 134 | |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | static int prealloc_init(struct bpf_htab *htab) |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 139 | { |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 140 | u32 num_entries = htab->map.max_entries; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 141 | int err = -ENOMEM, i; |
| 142 | |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 143 | if (!htab_is_percpu(htab) && !htab_is_lru(htab)) |
| 144 | num_entries += num_possible_cpus(); |
| 145 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 146 | htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries, |
| 147 | htab->map.numa_node); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 148 | if (!htab->elems) |
| 149 | return -ENOMEM; |
| 150 | |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 151 | if (!htab_is_percpu(htab)) |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 152 | goto skip_percpu_elems; |
| 153 | |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 154 | for (i = 0; i < num_entries; i++) { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 155 | u32 size = round_up(htab->map.value_size, 8); |
| 156 | void __percpu *pptr; |
| 157 | |
| 158 | pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN); |
| 159 | if (!pptr) |
| 160 | goto free_elems; |
| 161 | htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size, |
| 162 | pptr); |
Eric Dumazet | 9147efc | 2017-12-12 14:22:39 -0800 | [diff] [blame] | 163 | cond_resched(); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | skip_percpu_elems: |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 167 | if (htab_is_lru(htab)) |
| 168 | err = bpf_lru_init(&htab->lru, |
| 169 | htab->map.map_flags & BPF_F_NO_COMMON_LRU, |
| 170 | offsetof(struct htab_elem, hash) - |
| 171 | offsetof(struct htab_elem, lru_node), |
| 172 | htab_lru_map_delete_node, |
| 173 | htab); |
| 174 | else |
| 175 | err = pcpu_freelist_init(&htab->freelist); |
| 176 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 177 | if (err) |
| 178 | goto free_elems; |
| 179 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 180 | if (htab_is_lru(htab)) |
| 181 | bpf_lru_populate(&htab->lru, htab->elems, |
| 182 | offsetof(struct htab_elem, lru_node), |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 183 | htab->elem_size, num_entries); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 184 | else |
Alexei Starovoitov | 9f69154 | 2017-03-07 20:00:12 -0800 | [diff] [blame] | 185 | pcpu_freelist_populate(&htab->freelist, |
| 186 | htab->elems + offsetof(struct htab_elem, fnode), |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 187 | htab->elem_size, num_entries); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 188 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 189 | return 0; |
| 190 | |
| 191 | free_elems: |
| 192 | htab_free_elems(htab); |
| 193 | return err; |
| 194 | } |
| 195 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 196 | static void prealloc_destroy(struct bpf_htab *htab) |
| 197 | { |
| 198 | htab_free_elems(htab); |
| 199 | |
| 200 | if (htab_is_lru(htab)) |
| 201 | bpf_lru_destroy(&htab->lru); |
| 202 | else |
| 203 | pcpu_freelist_destroy(&htab->freelist); |
| 204 | } |
| 205 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 206 | static int alloc_extra_elems(struct bpf_htab *htab) |
| 207 | { |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 208 | struct htab_elem *__percpu *pptr, *l_new; |
| 209 | struct pcpu_freelist_node *l; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 210 | int cpu; |
| 211 | |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 212 | pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8, |
| 213 | GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 214 | if (!pptr) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | for_each_possible_cpu(cpu) { |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 218 | l = pcpu_freelist_pop(&htab->freelist); |
| 219 | /* pop will succeed, since prealloc_init() |
| 220 | * preallocated extra num_possible_cpus elements |
| 221 | */ |
| 222 | l_new = container_of(l, struct htab_elem, fnode); |
| 223 | *per_cpu_ptr(pptr, cpu) = l_new; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 224 | } |
| 225 | htab->extra_elems = pptr; |
| 226 | return 0; |
| 227 | } |
| 228 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 229 | /* Called from syscall */ |
| 230 | static struct bpf_map *htab_map_alloc(union bpf_attr *attr) |
| 231 | { |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 232 | bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 233 | attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH); |
| 234 | bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH || |
| 235 | attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 236 | /* percpu_lru means each cpu has its own LRU list. |
| 237 | * it is different from BPF_MAP_TYPE_PERCPU_HASH where |
| 238 | * the map's value itself is percpu. percpu_lru has |
| 239 | * nothing to do with the map's value. |
| 240 | */ |
| 241 | bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU); |
| 242 | bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC); |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 243 | int numa_node = bpf_map_attr_numa_node(attr); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 244 | struct bpf_htab *htab; |
| 245 | int err, i; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 246 | u64 cost; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 247 | |
Alexei Starovoitov | 9f69154 | 2017-03-07 20:00:12 -0800 | [diff] [blame] | 248 | BUILD_BUG_ON(offsetof(struct htab_elem, htab) != |
| 249 | offsetof(struct htab_elem, hash_node.pprev)); |
| 250 | BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) != |
| 251 | offsetof(struct htab_elem, hash_node.pprev)); |
| 252 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 253 | if (lru && !capable(CAP_SYS_ADMIN)) |
| 254 | /* LRU implementation is much complicated than other |
| 255 | * maps. Hence, limit to CAP_SYS_ADMIN for now. |
| 256 | */ |
| 257 | return ERR_PTR(-EPERM); |
| 258 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 259 | if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK) |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 260 | /* reserved bits should not be used */ |
| 261 | return ERR_PTR(-EINVAL); |
| 262 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 263 | if (!lru && percpu_lru) |
| 264 | return ERR_PTR(-EINVAL); |
| 265 | |
| 266 | if (lru && !prealloc) |
| 267 | return ERR_PTR(-ENOTSUPP); |
| 268 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 269 | if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru)) |
| 270 | return ERR_PTR(-EINVAL); |
| 271 | |
Jakub Kicinski | daffc5a | 2018-01-11 20:29:04 -0800 | [diff] [blame^] | 272 | /* check sanity of attributes. |
| 273 | * value_size == 0 may be allowed in the future to use map as a set |
| 274 | */ |
| 275 | if (attr->max_entries == 0 || attr->key_size == 0 || |
| 276 | attr->value_size == 0) |
| 277 | return ERR_PTR(-EINVAL); |
| 278 | |
| 279 | if (attr->key_size > MAX_BPF_STACK) |
| 280 | /* eBPF programs initialize keys on stack, so they cannot be |
| 281 | * larger than max stack size |
| 282 | */ |
| 283 | return ERR_PTR(-E2BIG); |
| 284 | |
| 285 | if (attr->value_size >= KMALLOC_MAX_SIZE - |
| 286 | MAX_BPF_STACK - sizeof(struct htab_elem)) |
| 287 | /* if value_size is bigger, the user space won't be able to |
| 288 | * access the elements via bpf syscall. This check also makes |
| 289 | * sure that the elem_size doesn't overflow and it's |
| 290 | * kmalloc-able later in htab_map_update_elem() |
| 291 | */ |
| 292 | return ERR_PTR(-E2BIG); |
| 293 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 294 | htab = kzalloc(sizeof(*htab), GFP_USER); |
| 295 | if (!htab) |
| 296 | return ERR_PTR(-ENOMEM); |
| 297 | |
| 298 | /* mandatory map attributes */ |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 299 | htab->map.map_type = attr->map_type; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 300 | htab->map.key_size = attr->key_size; |
| 301 | htab->map.value_size = attr->value_size; |
| 302 | htab->map.max_entries = attr->max_entries; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 303 | htab->map.map_flags = attr->map_flags; |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 304 | htab->map.numa_node = numa_node; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 305 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 306 | if (percpu_lru) { |
| 307 | /* ensure each CPU's lru list has >=1 elements. |
| 308 | * since we are at it, make each lru list has the same |
| 309 | * number of elements. |
| 310 | */ |
| 311 | htab->map.max_entries = roundup(attr->max_entries, |
| 312 | num_possible_cpus()); |
| 313 | if (htab->map.max_entries < attr->max_entries) |
| 314 | htab->map.max_entries = rounddown(attr->max_entries, |
| 315 | num_possible_cpus()); |
| 316 | } |
| 317 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 318 | /* hash table size must be power of 2 */ |
| 319 | htab->n_buckets = roundup_pow_of_two(htab->map.max_entries); |
| 320 | |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 321 | htab->elem_size = sizeof(struct htab_elem) + |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 322 | round_up(htab->map.key_size, 8); |
| 323 | if (percpu) |
| 324 | htab->elem_size += sizeof(void *); |
| 325 | else |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 326 | htab->elem_size += round_up(htab->map.value_size, 8); |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 327 | |
Jakub Kicinski | daffc5a | 2018-01-11 20:29:04 -0800 | [diff] [blame^] | 328 | err = -E2BIG; |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 329 | /* prevent zero size kmalloc and check for u32 overflow */ |
| 330 | if (htab->n_buckets == 0 || |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 331 | htab->n_buckets > U32_MAX / sizeof(struct bucket)) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 332 | goto free_htab; |
| 333 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 334 | cost = (u64) htab->n_buckets * sizeof(struct bucket) + |
| 335 | (u64) htab->elem_size * htab->map.max_entries; |
| 336 | |
| 337 | if (percpu) |
| 338 | cost += (u64) round_up(htab->map.value_size, 8) * |
| 339 | num_possible_cpus() * htab->map.max_entries; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 340 | else |
| 341 | cost += (u64) htab->elem_size * num_possible_cpus(); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 342 | |
| 343 | if (cost >= U32_MAX - PAGE_SIZE) |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 344 | /* make sure page count doesn't overflow */ |
| 345 | goto free_htab; |
| 346 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 347 | htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT; |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 348 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 349 | /* if map size is larger than memlock limit, reject it early */ |
| 350 | err = bpf_map_precharge_memlock(htab->map.pages); |
| 351 | if (err) |
| 352 | goto free_htab; |
| 353 | |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 354 | err = -ENOMEM; |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 355 | htab->buckets = bpf_map_area_alloc(htab->n_buckets * |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 356 | sizeof(struct bucket), |
| 357 | htab->map.numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 358 | if (!htab->buckets) |
| 359 | goto free_htab; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 360 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 361 | for (i = 0; i < htab->n_buckets; i++) { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 362 | INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i); |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 363 | raw_spin_lock_init(&htab->buckets[i].lock); |
| 364 | } |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 365 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 366 | if (prealloc) { |
| 367 | err = prealloc_init(htab); |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 368 | if (err) |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 369 | goto free_buckets; |
| 370 | |
| 371 | if (!percpu && !lru) { |
| 372 | /* lru itself can remove the least used element, so |
| 373 | * there is no need for an extra elem during map_update. |
| 374 | */ |
| 375 | err = alloc_extra_elems(htab); |
| 376 | if (err) |
| 377 | goto free_prealloc; |
| 378 | } |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 381 | return &htab->map; |
| 382 | |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 383 | free_prealloc: |
| 384 | prealloc_destroy(htab); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 385 | free_buckets: |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 386 | bpf_map_area_free(htab->buckets); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 387 | free_htab: |
| 388 | kfree(htab); |
| 389 | return ERR_PTR(err); |
| 390 | } |
| 391 | |
| 392 | static inline u32 htab_map_hash(const void *key, u32 key_len) |
| 393 | { |
| 394 | return jhash(key, key_len, 0); |
| 395 | } |
| 396 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 397 | static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 398 | { |
| 399 | return &htab->buckets[hash & (htab->n_buckets - 1)]; |
| 400 | } |
| 401 | |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 402 | static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash) |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 403 | { |
| 404 | return &__select_bucket(htab, hash)->head; |
| 405 | } |
| 406 | |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 407 | /* this lookup function can only be called with bucket lock taken */ |
| 408 | static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash, |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 409 | void *key, u32 key_size) |
| 410 | { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 411 | struct hlist_nulls_node *n; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 412 | struct htab_elem *l; |
| 413 | |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 414 | hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 415 | if (l->hash == hash && !memcmp(&l->key, key, key_size)) |
| 416 | return l; |
| 417 | |
| 418 | return NULL; |
| 419 | } |
| 420 | |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 421 | /* can be called without bucket lock. it will repeat the loop in |
| 422 | * the unlikely event when elements moved from one bucket into another |
| 423 | * while link list is being walked |
| 424 | */ |
| 425 | static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head, |
| 426 | u32 hash, void *key, |
| 427 | u32 key_size, u32 n_buckets) |
| 428 | { |
| 429 | struct hlist_nulls_node *n; |
| 430 | struct htab_elem *l; |
| 431 | |
| 432 | again: |
| 433 | hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) |
| 434 | if (l->hash == hash && !memcmp(&l->key, key, key_size)) |
| 435 | return l; |
| 436 | |
| 437 | if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1)))) |
| 438 | goto again; |
| 439 | |
| 440 | return NULL; |
| 441 | } |
| 442 | |
Alexei Starovoitov | 9015d2f | 2017-03-15 18:26:43 -0700 | [diff] [blame] | 443 | /* Called from syscall or from eBPF program directly, so |
| 444 | * arguments have to match bpf_map_lookup_elem() exactly. |
| 445 | * The return value is adjusted by BPF instructions |
| 446 | * in htab_map_gen_lookup(). |
| 447 | */ |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 448 | static void *__htab_map_lookup_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 449 | { |
| 450 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 451 | struct hlist_nulls_head *head; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 452 | struct htab_elem *l; |
| 453 | u32 hash, key_size; |
| 454 | |
| 455 | /* Must be called with rcu_read_lock. */ |
| 456 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 457 | |
| 458 | key_size = map->key_size; |
| 459 | |
| 460 | hash = htab_map_hash(key, key_size); |
| 461 | |
| 462 | head = select_bucket(htab, hash); |
| 463 | |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 464 | l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 465 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 466 | return l; |
| 467 | } |
| 468 | |
| 469 | static void *htab_map_lookup_elem(struct bpf_map *map, void *key) |
| 470 | { |
| 471 | struct htab_elem *l = __htab_map_lookup_elem(map, key); |
| 472 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 473 | if (l) |
| 474 | return l->key + round_up(map->key_size, 8); |
| 475 | |
| 476 | return NULL; |
| 477 | } |
| 478 | |
Alexei Starovoitov | 9015d2f | 2017-03-15 18:26:43 -0700 | [diff] [blame] | 479 | /* inline bpf_map_lookup_elem() call. |
| 480 | * Instead of: |
| 481 | * bpf_prog |
| 482 | * bpf_map_lookup_elem |
| 483 | * map->ops->map_lookup_elem |
| 484 | * htab_map_lookup_elem |
| 485 | * __htab_map_lookup_elem |
| 486 | * do: |
| 487 | * bpf_prog |
| 488 | * __htab_map_lookup_elem |
| 489 | */ |
| 490 | static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) |
| 491 | { |
| 492 | struct bpf_insn *insn = insn_buf; |
| 493 | const int ret = BPF_REG_0; |
| 494 | |
| 495 | *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem); |
| 496 | *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1); |
| 497 | *insn++ = BPF_ALU64_IMM(BPF_ADD, ret, |
| 498 | offsetof(struct htab_elem, key) + |
| 499 | round_up(map->key_size, 8)); |
| 500 | return insn - insn_buf; |
| 501 | } |
| 502 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 503 | static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key) |
| 504 | { |
| 505 | struct htab_elem *l = __htab_map_lookup_elem(map, key); |
| 506 | |
| 507 | if (l) { |
| 508 | bpf_lru_node_set_ref(&l->lru_node); |
| 509 | return l->key + round_up(map->key_size, 8); |
| 510 | } |
| 511 | |
| 512 | return NULL; |
| 513 | } |
| 514 | |
Martin KaFai Lau | cc55542 | 2017-08-31 23:27:12 -0700 | [diff] [blame] | 515 | static u32 htab_lru_map_gen_lookup(struct bpf_map *map, |
| 516 | struct bpf_insn *insn_buf) |
| 517 | { |
| 518 | struct bpf_insn *insn = insn_buf; |
| 519 | const int ret = BPF_REG_0; |
Martin KaFai Lau | bb9b9f8 | 2017-08-31 23:27:13 -0700 | [diff] [blame] | 520 | const int ref_reg = BPF_REG_1; |
Martin KaFai Lau | cc55542 | 2017-08-31 23:27:12 -0700 | [diff] [blame] | 521 | |
| 522 | *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem); |
Martin KaFai Lau | bb9b9f8 | 2017-08-31 23:27:13 -0700 | [diff] [blame] | 523 | *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4); |
| 524 | *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret, |
| 525 | offsetof(struct htab_elem, lru_node) + |
| 526 | offsetof(struct bpf_lru_node, ref)); |
| 527 | *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1); |
Martin KaFai Lau | cc55542 | 2017-08-31 23:27:12 -0700 | [diff] [blame] | 528 | *insn++ = BPF_ST_MEM(BPF_B, ret, |
| 529 | offsetof(struct htab_elem, lru_node) + |
| 530 | offsetof(struct bpf_lru_node, ref), |
| 531 | 1); |
| 532 | *insn++ = BPF_ALU64_IMM(BPF_ADD, ret, |
| 533 | offsetof(struct htab_elem, key) + |
| 534 | round_up(map->key_size, 8)); |
| 535 | return insn - insn_buf; |
| 536 | } |
| 537 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 538 | /* It is called from the bpf_lru_list when the LRU needs to delete |
| 539 | * older elements from the htab. |
| 540 | */ |
| 541 | static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node) |
| 542 | { |
| 543 | struct bpf_htab *htab = (struct bpf_htab *)arg; |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 544 | struct htab_elem *l = NULL, *tgt_l; |
| 545 | struct hlist_nulls_head *head; |
| 546 | struct hlist_nulls_node *n; |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 547 | unsigned long flags; |
| 548 | struct bucket *b; |
| 549 | |
| 550 | tgt_l = container_of(node, struct htab_elem, lru_node); |
| 551 | b = __select_bucket(htab, tgt_l->hash); |
| 552 | head = &b->head; |
| 553 | |
| 554 | raw_spin_lock_irqsave(&b->lock, flags); |
| 555 | |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 556 | hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 557 | if (l == tgt_l) { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 558 | hlist_nulls_del_rcu(&l->hash_node); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | |
| 562 | raw_spin_unlock_irqrestore(&b->lock, flags); |
| 563 | |
| 564 | return l == tgt_l; |
| 565 | } |
| 566 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 567 | /* Called from syscall */ |
| 568 | static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 569 | { |
| 570 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 571 | struct hlist_nulls_head *head; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 572 | struct htab_elem *l, *next_l; |
| 573 | u32 hash, key_size; |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 574 | int i = 0; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 575 | |
| 576 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 577 | |
| 578 | key_size = map->key_size; |
| 579 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 580 | if (!key) |
| 581 | goto find_first_elem; |
| 582 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 583 | hash = htab_map_hash(key, key_size); |
| 584 | |
| 585 | head = select_bucket(htab, hash); |
| 586 | |
| 587 | /* lookup the key */ |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 588 | l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 589 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 590 | if (!l) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 591 | goto find_first_elem; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 592 | |
| 593 | /* key was found, get next key in the same bucket */ |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 594 | next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)), |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 595 | struct htab_elem, hash_node); |
| 596 | |
| 597 | if (next_l) { |
| 598 | /* if next elem in this hash list is non-zero, just return it */ |
| 599 | memcpy(next_key, next_l->key, key_size); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* no more elements in this hash list, go to the next bucket */ |
| 604 | i = hash & (htab->n_buckets - 1); |
| 605 | i++; |
| 606 | |
| 607 | find_first_elem: |
| 608 | /* iterate over buckets */ |
| 609 | for (; i < htab->n_buckets; i++) { |
| 610 | head = select_bucket(htab, i); |
| 611 | |
| 612 | /* pick first element in the bucket */ |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 613 | next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)), |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 614 | struct htab_elem, hash_node); |
| 615 | if (next_l) { |
| 616 | /* if it's not empty, just return it */ |
| 617 | memcpy(next_key, next_l->key, key_size); |
| 618 | return 0; |
| 619 | } |
| 620 | } |
| 621 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 622 | /* iterated over all buckets and all elements */ |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 623 | return -ENOENT; |
| 624 | } |
| 625 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 626 | static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 627 | { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 628 | if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH) |
| 629 | free_percpu(htab_elem_get_ptr(l, htab->map.key_size)); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 630 | kfree(l); |
| 631 | } |
| 632 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 633 | static void htab_elem_free_rcu(struct rcu_head *head) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 634 | { |
| 635 | struct htab_elem *l = container_of(head, struct htab_elem, rcu); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 636 | struct bpf_htab *htab = l->htab; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 637 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 638 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering while |
| 639 | * we're calling kfree, otherwise deadlock is possible if kprobes |
| 640 | * are placed somewhere inside of slub |
| 641 | */ |
| 642 | preempt_disable(); |
| 643 | __this_cpu_inc(bpf_prog_active); |
| 644 | htab_elem_free(htab, l); |
| 645 | __this_cpu_dec(bpf_prog_active); |
| 646 | preempt_enable(); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 649 | static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 650 | { |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 651 | struct bpf_map *map = &htab->map; |
| 652 | |
| 653 | if (map->ops->map_fd_put_ptr) { |
| 654 | void *ptr = fd_htab_map_get_ptr(map, l); |
| 655 | |
| 656 | map->ops->map_fd_put_ptr(ptr); |
| 657 | } |
| 658 | |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 659 | if (htab_is_prealloc(htab)) { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 660 | pcpu_freelist_push(&htab->freelist, &l->fnode); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 661 | } else { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 662 | atomic_dec(&htab->count); |
| 663 | l->htab = htab; |
| 664 | call_rcu(&l->rcu, htab_elem_free_rcu); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Martin KaFai Lau | fd91de7 | 2016-11-11 10:55:08 -0800 | [diff] [blame] | 668 | static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr, |
| 669 | void *value, bool onallcpus) |
| 670 | { |
| 671 | if (!onallcpus) { |
| 672 | /* copy true value_size bytes */ |
| 673 | memcpy(this_cpu_ptr(pptr), value, htab->map.value_size); |
| 674 | } else { |
| 675 | u32 size = round_up(htab->map.value_size, 8); |
| 676 | int off = 0, cpu; |
| 677 | |
| 678 | for_each_possible_cpu(cpu) { |
| 679 | bpf_long_memcpy(per_cpu_ptr(pptr, cpu), |
| 680 | value + off, size); |
| 681 | off += size; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
Daniel Borkmann | cd36c3a | 2017-08-23 00:06:09 +0200 | [diff] [blame] | 686 | static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab) |
| 687 | { |
| 688 | return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS && |
| 689 | BITS_PER_LONG == 64; |
| 690 | } |
| 691 | |
| 692 | static u32 htab_size_value(const struct bpf_htab *htab, bool percpu) |
| 693 | { |
| 694 | u32 size = htab->map.value_size; |
| 695 | |
| 696 | if (percpu || fd_htab_map_needs_adjust(htab)) |
| 697 | size = round_up(size, 8); |
| 698 | return size; |
| 699 | } |
| 700 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 701 | static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, |
| 702 | void *value, u32 key_size, u32 hash, |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 703 | bool percpu, bool onallcpus, |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 704 | struct htab_elem *old_elem) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 705 | { |
Daniel Borkmann | cd36c3a | 2017-08-23 00:06:09 +0200 | [diff] [blame] | 706 | u32 size = htab_size_value(htab, percpu); |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 707 | bool prealloc = htab_is_prealloc(htab); |
| 708 | struct htab_elem *l_new, **pl_new; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 709 | void __percpu *pptr; |
| 710 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 711 | if (prealloc) { |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 712 | if (old_elem) { |
| 713 | /* if we're updating the existing element, |
| 714 | * use per-cpu extra elems to avoid freelist_pop/push |
| 715 | */ |
| 716 | pl_new = this_cpu_ptr(htab->extra_elems); |
| 717 | l_new = *pl_new; |
| 718 | *pl_new = old_elem; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 719 | } else { |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 720 | struct pcpu_freelist_node *l; |
| 721 | |
| 722 | l = pcpu_freelist_pop(&htab->freelist); |
| 723 | if (!l) |
| 724 | return ERR_PTR(-E2BIG); |
| 725 | l_new = container_of(l, struct htab_elem, fnode); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 726 | } |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 727 | } else { |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 728 | if (atomic_inc_return(&htab->count) > htab->map.max_entries) |
| 729 | if (!old_elem) { |
| 730 | /* when map is full and update() is replacing |
| 731 | * old element, it's ok to allocate, since |
| 732 | * old element will be freed immediately. |
| 733 | * Otherwise return an error |
| 734 | */ |
| 735 | atomic_dec(&htab->count); |
| 736 | return ERR_PTR(-E2BIG); |
| 737 | } |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 738 | l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN, |
| 739 | htab->map.numa_node); |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 740 | if (!l_new) |
| 741 | return ERR_PTR(-ENOMEM); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 742 | } |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 743 | |
| 744 | memcpy(l_new->key, key, key_size); |
| 745 | if (percpu) { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 746 | if (prealloc) { |
| 747 | pptr = htab_elem_get_ptr(l_new, key_size); |
| 748 | } else { |
| 749 | /* alloc_percpu zero-fills */ |
| 750 | pptr = __alloc_percpu_gfp(size, 8, |
| 751 | GFP_ATOMIC | __GFP_NOWARN); |
| 752 | if (!pptr) { |
| 753 | kfree(l_new); |
| 754 | return ERR_PTR(-ENOMEM); |
| 755 | } |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Martin KaFai Lau | fd91de7 | 2016-11-11 10:55:08 -0800 | [diff] [blame] | 758 | pcpu_copy_value(htab, pptr, value, onallcpus); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 759 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 760 | if (!prealloc) |
| 761 | htab_elem_set_ptr(l_new, key_size, pptr); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 762 | } else { |
| 763 | memcpy(l_new->key + round_up(key_size, 8), value, size); |
| 764 | } |
| 765 | |
| 766 | l_new->hash = hash; |
| 767 | return l_new; |
| 768 | } |
| 769 | |
| 770 | static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old, |
| 771 | u64 map_flags) |
| 772 | { |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 773 | if (l_old && map_flags == BPF_NOEXIST) |
| 774 | /* elem already exists */ |
| 775 | return -EEXIST; |
| 776 | |
| 777 | if (!l_old && map_flags == BPF_EXIST) |
| 778 | /* elem doesn't exist, cannot update it */ |
| 779 | return -ENOENT; |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 784 | /* Called from syscall or from eBPF program */ |
| 785 | static int htab_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 786 | u64 map_flags) |
| 787 | { |
| 788 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 789 | struct htab_elem *l_new = NULL, *l_old; |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 790 | struct hlist_nulls_head *head; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 791 | unsigned long flags; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 792 | struct bucket *b; |
| 793 | u32 key_size, hash; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 794 | int ret; |
| 795 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 796 | if (unlikely(map_flags > BPF_EXIST)) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 797 | /* unknown flags */ |
| 798 | return -EINVAL; |
| 799 | |
| 800 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 801 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 802 | key_size = map->key_size; |
| 803 | |
| 804 | hash = htab_map_hash(key, key_size); |
| 805 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 806 | b = __select_bucket(htab, hash); |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 807 | head = &b->head; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 808 | |
| 809 | /* bpf_map_update_elem() can be called in_irq() */ |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 810 | raw_spin_lock_irqsave(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 811 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 812 | l_old = lookup_elem_raw(head, hash, key, key_size); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 813 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 814 | ret = check_flags(htab, l_old, map_flags); |
| 815 | if (ret) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 816 | goto err; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 817 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 818 | l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false, |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 819 | l_old); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 820 | if (IS_ERR(l_new)) { |
| 821 | /* all pre-allocated elements are in use or memory exhausted */ |
| 822 | ret = PTR_ERR(l_new); |
| 823 | goto err; |
| 824 | } |
| 825 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 826 | /* add new element to the head of the list, so that |
| 827 | * concurrent search will find it before old elem |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 828 | */ |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 829 | hlist_nulls_add_head_rcu(&l_new->hash_node, head); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 830 | if (l_old) { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 831 | hlist_nulls_del_rcu(&l_old->hash_node); |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 832 | if (!htab_is_prealloc(htab)) |
| 833 | free_htab_elem(htab, l_old); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 834 | } |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 835 | ret = 0; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 836 | err: |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 837 | raw_spin_unlock_irqrestore(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 838 | return ret; |
| 839 | } |
| 840 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 841 | static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 842 | u64 map_flags) |
| 843 | { |
| 844 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 845 | struct htab_elem *l_new, *l_old = NULL; |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 846 | struct hlist_nulls_head *head; |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 847 | unsigned long flags; |
| 848 | struct bucket *b; |
| 849 | u32 key_size, hash; |
| 850 | int ret; |
| 851 | |
| 852 | if (unlikely(map_flags > BPF_EXIST)) |
| 853 | /* unknown flags */ |
| 854 | return -EINVAL; |
| 855 | |
| 856 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 857 | |
| 858 | key_size = map->key_size; |
| 859 | |
| 860 | hash = htab_map_hash(key, key_size); |
| 861 | |
| 862 | b = __select_bucket(htab, hash); |
| 863 | head = &b->head; |
| 864 | |
| 865 | /* For LRU, we need to alloc before taking bucket's |
| 866 | * spinlock because getting free nodes from LRU may need |
| 867 | * to remove older elements from htab and this removal |
| 868 | * operation will need a bucket lock. |
| 869 | */ |
| 870 | l_new = prealloc_lru_pop(htab, key, hash); |
| 871 | if (!l_new) |
| 872 | return -ENOMEM; |
| 873 | memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size); |
| 874 | |
| 875 | /* bpf_map_update_elem() can be called in_irq() */ |
| 876 | raw_spin_lock_irqsave(&b->lock, flags); |
| 877 | |
| 878 | l_old = lookup_elem_raw(head, hash, key, key_size); |
| 879 | |
| 880 | ret = check_flags(htab, l_old, map_flags); |
| 881 | if (ret) |
| 882 | goto err; |
| 883 | |
| 884 | /* add new element to the head of the list, so that |
| 885 | * concurrent search will find it before old elem |
| 886 | */ |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 887 | hlist_nulls_add_head_rcu(&l_new->hash_node, head); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 888 | if (l_old) { |
| 889 | bpf_lru_node_set_ref(&l_new->lru_node); |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 890 | hlist_nulls_del_rcu(&l_old->hash_node); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 891 | } |
| 892 | ret = 0; |
| 893 | |
| 894 | err: |
| 895 | raw_spin_unlock_irqrestore(&b->lock, flags); |
| 896 | |
| 897 | if (ret) |
| 898 | bpf_lru_push_free(&htab->lru, &l_new->lru_node); |
| 899 | else if (l_old) |
| 900 | bpf_lru_push_free(&htab->lru, &l_old->lru_node); |
| 901 | |
| 902 | return ret; |
| 903 | } |
| 904 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 905 | static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key, |
| 906 | void *value, u64 map_flags, |
| 907 | bool onallcpus) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 908 | { |
| 909 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 910 | struct htab_elem *l_new = NULL, *l_old; |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 911 | struct hlist_nulls_head *head; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 912 | unsigned long flags; |
| 913 | struct bucket *b; |
| 914 | u32 key_size, hash; |
| 915 | int ret; |
| 916 | |
| 917 | if (unlikely(map_flags > BPF_EXIST)) |
| 918 | /* unknown flags */ |
| 919 | return -EINVAL; |
| 920 | |
| 921 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 922 | |
| 923 | key_size = map->key_size; |
| 924 | |
| 925 | hash = htab_map_hash(key, key_size); |
| 926 | |
| 927 | b = __select_bucket(htab, hash); |
| 928 | head = &b->head; |
| 929 | |
| 930 | /* bpf_map_update_elem() can be called in_irq() */ |
| 931 | raw_spin_lock_irqsave(&b->lock, flags); |
| 932 | |
| 933 | l_old = lookup_elem_raw(head, hash, key, key_size); |
| 934 | |
| 935 | ret = check_flags(htab, l_old, map_flags); |
| 936 | if (ret) |
| 937 | goto err; |
| 938 | |
| 939 | if (l_old) { |
| 940 | /* per-cpu hash map can update value in-place */ |
Martin KaFai Lau | fd91de7 | 2016-11-11 10:55:08 -0800 | [diff] [blame] | 941 | pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), |
| 942 | value, onallcpus); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 943 | } else { |
| 944 | l_new = alloc_htab_elem(htab, key, value, key_size, |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 945 | hash, true, onallcpus, NULL); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 946 | if (IS_ERR(l_new)) { |
| 947 | ret = PTR_ERR(l_new); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 948 | goto err; |
| 949 | } |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 950 | hlist_nulls_add_head_rcu(&l_new->hash_node, head); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 951 | } |
| 952 | ret = 0; |
| 953 | err: |
| 954 | raw_spin_unlock_irqrestore(&b->lock, flags); |
| 955 | return ret; |
| 956 | } |
| 957 | |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 958 | static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key, |
| 959 | void *value, u64 map_flags, |
| 960 | bool onallcpus) |
| 961 | { |
| 962 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 963 | struct htab_elem *l_new = NULL, *l_old; |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 964 | struct hlist_nulls_head *head; |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 965 | unsigned long flags; |
| 966 | struct bucket *b; |
| 967 | u32 key_size, hash; |
| 968 | int ret; |
| 969 | |
| 970 | if (unlikely(map_flags > BPF_EXIST)) |
| 971 | /* unknown flags */ |
| 972 | return -EINVAL; |
| 973 | |
| 974 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 975 | |
| 976 | key_size = map->key_size; |
| 977 | |
| 978 | hash = htab_map_hash(key, key_size); |
| 979 | |
| 980 | b = __select_bucket(htab, hash); |
| 981 | head = &b->head; |
| 982 | |
| 983 | /* For LRU, we need to alloc before taking bucket's |
| 984 | * spinlock because LRU's elem alloc may need |
| 985 | * to remove older elem from htab and this removal |
| 986 | * operation will need a bucket lock. |
| 987 | */ |
| 988 | if (map_flags != BPF_EXIST) { |
| 989 | l_new = prealloc_lru_pop(htab, key, hash); |
| 990 | if (!l_new) |
| 991 | return -ENOMEM; |
| 992 | } |
| 993 | |
| 994 | /* bpf_map_update_elem() can be called in_irq() */ |
| 995 | raw_spin_lock_irqsave(&b->lock, flags); |
| 996 | |
| 997 | l_old = lookup_elem_raw(head, hash, key, key_size); |
| 998 | |
| 999 | ret = check_flags(htab, l_old, map_flags); |
| 1000 | if (ret) |
| 1001 | goto err; |
| 1002 | |
| 1003 | if (l_old) { |
| 1004 | bpf_lru_node_set_ref(&l_old->lru_node); |
| 1005 | |
| 1006 | /* per-cpu hash map can update value in-place */ |
| 1007 | pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), |
| 1008 | value, onallcpus); |
| 1009 | } else { |
| 1010 | pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size), |
| 1011 | value, onallcpus); |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 1012 | hlist_nulls_add_head_rcu(&l_new->hash_node, head); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1013 | l_new = NULL; |
| 1014 | } |
| 1015 | ret = 0; |
| 1016 | err: |
| 1017 | raw_spin_unlock_irqrestore(&b->lock, flags); |
| 1018 | if (l_new) |
| 1019 | bpf_lru_push_free(&htab->lru, &l_new->lru_node); |
| 1020 | return ret; |
| 1021 | } |
| 1022 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1023 | static int htab_percpu_map_update_elem(struct bpf_map *map, void *key, |
| 1024 | void *value, u64 map_flags) |
| 1025 | { |
| 1026 | return __htab_percpu_map_update_elem(map, key, value, map_flags, false); |
| 1027 | } |
| 1028 | |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1029 | static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key, |
| 1030 | void *value, u64 map_flags) |
| 1031 | { |
| 1032 | return __htab_lru_percpu_map_update_elem(map, key, value, map_flags, |
| 1033 | false); |
| 1034 | } |
| 1035 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1036 | /* Called from syscall or from eBPF program */ |
| 1037 | static int htab_map_delete_elem(struct bpf_map *map, void *key) |
| 1038 | { |
| 1039 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 1040 | struct hlist_nulls_head *head; |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 1041 | struct bucket *b; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1042 | struct htab_elem *l; |
| 1043 | unsigned long flags; |
| 1044 | u32 hash, key_size; |
| 1045 | int ret = -ENOENT; |
| 1046 | |
| 1047 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1048 | |
| 1049 | key_size = map->key_size; |
| 1050 | |
| 1051 | hash = htab_map_hash(key, key_size); |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 1052 | b = __select_bucket(htab, hash); |
| 1053 | head = &b->head; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1054 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 1055 | raw_spin_lock_irqsave(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1056 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1057 | l = lookup_elem_raw(head, hash, key, key_size); |
| 1058 | |
| 1059 | if (l) { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 1060 | hlist_nulls_del_rcu(&l->hash_node); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 1061 | free_htab_elem(htab, l); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1062 | ret = 0; |
| 1063 | } |
| 1064 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 1065 | raw_spin_unlock_irqrestore(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1066 | return ret; |
| 1067 | } |
| 1068 | |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 1069 | static int htab_lru_map_delete_elem(struct bpf_map *map, void *key) |
| 1070 | { |
| 1071 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 1072 | struct hlist_nulls_head *head; |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 1073 | struct bucket *b; |
| 1074 | struct htab_elem *l; |
| 1075 | unsigned long flags; |
| 1076 | u32 hash, key_size; |
| 1077 | int ret = -ENOENT; |
| 1078 | |
| 1079 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1080 | |
| 1081 | key_size = map->key_size; |
| 1082 | |
| 1083 | hash = htab_map_hash(key, key_size); |
| 1084 | b = __select_bucket(htab, hash); |
| 1085 | head = &b->head; |
| 1086 | |
| 1087 | raw_spin_lock_irqsave(&b->lock, flags); |
| 1088 | |
| 1089 | l = lookup_elem_raw(head, hash, key, key_size); |
| 1090 | |
| 1091 | if (l) { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 1092 | hlist_nulls_del_rcu(&l->hash_node); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 1093 | ret = 0; |
| 1094 | } |
| 1095 | |
| 1096 | raw_spin_unlock_irqrestore(&b->lock, flags); |
| 1097 | if (l) |
| 1098 | bpf_lru_push_free(&htab->lru, &l->lru_node); |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1102 | static void delete_all_elements(struct bpf_htab *htab) |
| 1103 | { |
| 1104 | int i; |
| 1105 | |
| 1106 | for (i = 0; i < htab->n_buckets; i++) { |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 1107 | struct hlist_nulls_head *head = select_bucket(htab, i); |
| 1108 | struct hlist_nulls_node *n; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1109 | struct htab_elem *l; |
| 1110 | |
Alexei Starovoitov | 4fe8435 | 2017-03-07 20:00:13 -0800 | [diff] [blame] | 1111 | hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { |
| 1112 | hlist_nulls_del_rcu(&l->hash_node); |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 1113 | htab_elem_free(htab, l); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | } |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1117 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1118 | /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ |
| 1119 | static void htab_map_free(struct bpf_map *map) |
| 1120 | { |
| 1121 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 1122 | |
| 1123 | /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, |
| 1124 | * so the programs (can be more than one that used this map) were |
| 1125 | * disconnected from events. Wait for outstanding critical sections in |
| 1126 | * these programs to complete |
| 1127 | */ |
| 1128 | synchronize_rcu(); |
| 1129 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 1130 | /* some of free_htab_elem() callbacks for elements of this map may |
| 1131 | * not have executed. Wait for them. |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1132 | */ |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 1133 | rcu_barrier(); |
Alexei Starovoitov | 8c290e6 | 2017-03-21 19:05:04 -0700 | [diff] [blame] | 1134 | if (!htab_is_prealloc(htab)) |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 1135 | delete_all_elements(htab); |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 1136 | else |
| 1137 | prealloc_destroy(htab); |
| 1138 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 1139 | free_percpu(htab->extra_elems); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 1140 | bpf_map_area_free(htab->buckets); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1141 | kfree(htab); |
| 1142 | } |
| 1143 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 1144 | const struct bpf_map_ops htab_map_ops = { |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1145 | .map_alloc = htab_map_alloc, |
| 1146 | .map_free = htab_map_free, |
| 1147 | .map_get_next_key = htab_map_get_next_key, |
| 1148 | .map_lookup_elem = htab_map_lookup_elem, |
| 1149 | .map_update_elem = htab_map_update_elem, |
| 1150 | .map_delete_elem = htab_map_delete_elem, |
Alexei Starovoitov | 9015d2f | 2017-03-15 18:26:43 -0700 | [diff] [blame] | 1151 | .map_gen_lookup = htab_map_gen_lookup, |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1152 | }; |
| 1153 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 1154 | const struct bpf_map_ops htab_lru_map_ops = { |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 1155 | .map_alloc = htab_map_alloc, |
| 1156 | .map_free = htab_map_free, |
| 1157 | .map_get_next_key = htab_map_get_next_key, |
| 1158 | .map_lookup_elem = htab_lru_map_lookup_elem, |
| 1159 | .map_update_elem = htab_lru_map_update_elem, |
| 1160 | .map_delete_elem = htab_lru_map_delete_elem, |
Martin KaFai Lau | cc55542 | 2017-08-31 23:27:12 -0700 | [diff] [blame] | 1161 | .map_gen_lookup = htab_lru_map_gen_lookup, |
Martin KaFai Lau | 29ba732 | 2016-11-11 10:55:09 -0800 | [diff] [blame] | 1162 | }; |
| 1163 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 1164 | /* Called from eBPF program */ |
| 1165 | static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key) |
| 1166 | { |
| 1167 | struct htab_elem *l = __htab_map_lookup_elem(map, key); |
| 1168 | |
| 1169 | if (l) |
| 1170 | return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size)); |
| 1171 | else |
| 1172 | return NULL; |
| 1173 | } |
| 1174 | |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1175 | static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key) |
| 1176 | { |
| 1177 | struct htab_elem *l = __htab_map_lookup_elem(map, key); |
| 1178 | |
| 1179 | if (l) { |
| 1180 | bpf_lru_node_set_ref(&l->lru_node); |
| 1181 | return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size)); |
| 1182 | } |
| 1183 | |
| 1184 | return NULL; |
| 1185 | } |
| 1186 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1187 | int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value) |
| 1188 | { |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1189 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1190 | struct htab_elem *l; |
| 1191 | void __percpu *pptr; |
| 1192 | int ret = -ENOENT; |
| 1193 | int cpu, off = 0; |
| 1194 | u32 size; |
| 1195 | |
| 1196 | /* per_cpu areas are zero-filled and bpf programs can only |
| 1197 | * access 'value_size' of them, so copying rounded areas |
| 1198 | * will not leak any kernel data |
| 1199 | */ |
| 1200 | size = round_up(map->value_size, 8); |
| 1201 | rcu_read_lock(); |
| 1202 | l = __htab_map_lookup_elem(map, key); |
| 1203 | if (!l) |
| 1204 | goto out; |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1205 | if (htab_is_lru(htab)) |
| 1206 | bpf_lru_node_set_ref(&l->lru_node); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1207 | pptr = htab_elem_get_ptr(l, map->key_size); |
| 1208 | for_each_possible_cpu(cpu) { |
| 1209 | bpf_long_memcpy(value + off, |
| 1210 | per_cpu_ptr(pptr, cpu), size); |
| 1211 | off += size; |
| 1212 | } |
| 1213 | ret = 0; |
| 1214 | out: |
| 1215 | rcu_read_unlock(); |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
| 1219 | int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, |
| 1220 | u64 map_flags) |
| 1221 | { |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1222 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Sasha Levin | 6bbd9a0 | 2016-02-19 13:53:10 -0500 | [diff] [blame] | 1223 | int ret; |
| 1224 | |
| 1225 | rcu_read_lock(); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1226 | if (htab_is_lru(htab)) |
| 1227 | ret = __htab_lru_percpu_map_update_elem(map, key, value, |
| 1228 | map_flags, true); |
| 1229 | else |
| 1230 | ret = __htab_percpu_map_update_elem(map, key, value, map_flags, |
| 1231 | true); |
Sasha Levin | 6bbd9a0 | 2016-02-19 13:53:10 -0500 | [diff] [blame] | 1232 | rcu_read_unlock(); |
| 1233 | |
| 1234 | return ret; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 1237 | const struct bpf_map_ops htab_percpu_map_ops = { |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 1238 | .map_alloc = htab_map_alloc, |
| 1239 | .map_free = htab_map_free, |
| 1240 | .map_get_next_key = htab_map_get_next_key, |
| 1241 | .map_lookup_elem = htab_percpu_map_lookup_elem, |
| 1242 | .map_update_elem = htab_percpu_map_update_elem, |
| 1243 | .map_delete_elem = htab_map_delete_elem, |
| 1244 | }; |
| 1245 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 1246 | const struct bpf_map_ops htab_lru_percpu_map_ops = { |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1247 | .map_alloc = htab_map_alloc, |
| 1248 | .map_free = htab_map_free, |
| 1249 | .map_get_next_key = htab_map_get_next_key, |
| 1250 | .map_lookup_elem = htab_lru_percpu_map_lookup_elem, |
| 1251 | .map_update_elem = htab_lru_percpu_map_update_elem, |
| 1252 | .map_delete_elem = htab_lru_map_delete_elem, |
| 1253 | }; |
| 1254 | |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1255 | static struct bpf_map *fd_htab_map_alloc(union bpf_attr *attr) |
| 1256 | { |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1257 | if (attr->value_size != sizeof(u32)) |
| 1258 | return ERR_PTR(-EINVAL); |
Daniel Borkmann | cd36c3a | 2017-08-23 00:06:09 +0200 | [diff] [blame] | 1259 | return htab_map_alloc(attr); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static void fd_htab_map_free(struct bpf_map *map) |
| 1263 | { |
| 1264 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 1265 | struct hlist_nulls_node *n; |
| 1266 | struct hlist_nulls_head *head; |
| 1267 | struct htab_elem *l; |
| 1268 | int i; |
| 1269 | |
| 1270 | for (i = 0; i < htab->n_buckets; i++) { |
| 1271 | head = select_bucket(htab, i); |
| 1272 | |
| 1273 | hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { |
| 1274 | void *ptr = fd_htab_map_get_ptr(map, l); |
| 1275 | |
| 1276 | map->ops->map_fd_put_ptr(ptr); |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | htab_map_free(map); |
| 1281 | } |
| 1282 | |
| 1283 | /* only called from syscall */ |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 1284 | int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value) |
| 1285 | { |
| 1286 | void **ptr; |
| 1287 | int ret = 0; |
| 1288 | |
| 1289 | if (!map->ops->map_fd_sys_lookup_elem) |
| 1290 | return -ENOTSUPP; |
| 1291 | |
| 1292 | rcu_read_lock(); |
| 1293 | ptr = htab_map_lookup_elem(map, key); |
| 1294 | if (ptr) |
| 1295 | *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr)); |
| 1296 | else |
| 1297 | ret = -ENOENT; |
| 1298 | rcu_read_unlock(); |
| 1299 | |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
| 1303 | /* only called from syscall */ |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1304 | int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file, |
| 1305 | void *key, void *value, u64 map_flags) |
| 1306 | { |
| 1307 | void *ptr; |
| 1308 | int ret; |
| 1309 | u32 ufd = *(u32 *)value; |
| 1310 | |
| 1311 | ptr = map->ops->map_fd_get_ptr(map, map_file, ufd); |
| 1312 | if (IS_ERR(ptr)) |
| 1313 | return PTR_ERR(ptr); |
| 1314 | |
| 1315 | ret = htab_map_update_elem(map, key, &ptr, map_flags); |
| 1316 | if (ret) |
| 1317 | map->ops->map_fd_put_ptr(ptr); |
| 1318 | |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
| 1322 | static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr) |
| 1323 | { |
| 1324 | struct bpf_map *map, *inner_map_meta; |
| 1325 | |
| 1326 | inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd); |
| 1327 | if (IS_ERR(inner_map_meta)) |
| 1328 | return inner_map_meta; |
| 1329 | |
| 1330 | map = fd_htab_map_alloc(attr); |
| 1331 | if (IS_ERR(map)) { |
| 1332 | bpf_map_meta_free(inner_map_meta); |
| 1333 | return map; |
| 1334 | } |
| 1335 | |
| 1336 | map->inner_map_meta = inner_map_meta; |
| 1337 | |
| 1338 | return map; |
| 1339 | } |
| 1340 | |
| 1341 | static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key) |
| 1342 | { |
| 1343 | struct bpf_map **inner_map = htab_map_lookup_elem(map, key); |
| 1344 | |
| 1345 | if (!inner_map) |
| 1346 | return NULL; |
| 1347 | |
| 1348 | return READ_ONCE(*inner_map); |
| 1349 | } |
| 1350 | |
Daniel Borkmann | 7b0c2a0 | 2017-08-19 03:12:46 +0200 | [diff] [blame] | 1351 | static u32 htab_of_map_gen_lookup(struct bpf_map *map, |
| 1352 | struct bpf_insn *insn_buf) |
| 1353 | { |
| 1354 | struct bpf_insn *insn = insn_buf; |
| 1355 | const int ret = BPF_REG_0; |
| 1356 | |
| 1357 | *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem); |
| 1358 | *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2); |
| 1359 | *insn++ = BPF_ALU64_IMM(BPF_ADD, ret, |
| 1360 | offsetof(struct htab_elem, key) + |
| 1361 | round_up(map->key_size, 8)); |
| 1362 | *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0); |
| 1363 | |
| 1364 | return insn - insn_buf; |
| 1365 | } |
| 1366 | |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1367 | static void htab_of_map_free(struct bpf_map *map) |
| 1368 | { |
| 1369 | bpf_map_meta_free(map->inner_map_meta); |
| 1370 | fd_htab_map_free(map); |
| 1371 | } |
| 1372 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 1373 | const struct bpf_map_ops htab_of_maps_map_ops = { |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1374 | .map_alloc = htab_of_map_alloc, |
| 1375 | .map_free = htab_of_map_free, |
| 1376 | .map_get_next_key = htab_map_get_next_key, |
| 1377 | .map_lookup_elem = htab_of_map_lookup_elem, |
| 1378 | .map_delete_elem = htab_map_delete_elem, |
| 1379 | .map_fd_get_ptr = bpf_map_fd_get_ptr, |
| 1380 | .map_fd_put_ptr = bpf_map_fd_put_ptr, |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 1381 | .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem, |
Daniel Borkmann | 7b0c2a0 | 2017-08-19 03:12:46 +0200 | [diff] [blame] | 1382 | .map_gen_lookup = htab_of_map_gen_lookup, |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1383 | }; |