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