arraymap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016,2017 Facebook
  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/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/mm.h>
  17. #include <linux/filter.h>
  18. #include <linux/perf_event.h>
  19. #include "map_in_map.h"
  20. #define ARRAY_CREATE_FLAG_MASK \
  21. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
  22. static void bpf_array_free_percpu(struct bpf_array *array)
  23. {
  24. int i;
  25. for (i = 0; i < array->map.max_entries; i++)
  26. free_percpu(array->pptrs[i]);
  27. }
  28. static int bpf_array_alloc_percpu(struct bpf_array *array)
  29. {
  30. void __percpu *ptr;
  31. int i;
  32. for (i = 0; i < array->map.max_entries; i++) {
  33. ptr = __alloc_percpu_gfp(array->elem_size, 8,
  34. GFP_USER | __GFP_NOWARN);
  35. if (!ptr) {
  36. bpf_array_free_percpu(array);
  37. return -ENOMEM;
  38. }
  39. array->pptrs[i] = ptr;
  40. }
  41. return 0;
  42. }
  43. /* Called from syscall */
  44. static int array_map_alloc_check(union bpf_attr *attr)
  45. {
  46. bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
  47. int numa_node = bpf_map_attr_numa_node(attr);
  48. /* check sanity of attributes */
  49. if (attr->max_entries == 0 || attr->key_size != 4 ||
  50. attr->value_size == 0 ||
  51. attr->map_flags & ~ARRAY_CREATE_FLAG_MASK ||
  52. (percpu && numa_node != NUMA_NO_NODE))
  53. return -EINVAL;
  54. if (attr->value_size > KMALLOC_MAX_SIZE)
  55. /* if value_size is bigger, the user space won't be able to
  56. * access the elements.
  57. */
  58. return -E2BIG;
  59. return 0;
  60. }
  61. static struct bpf_map *array_map_alloc(union bpf_attr *attr)
  62. {
  63. bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
  64. int numa_node = bpf_map_attr_numa_node(attr);
  65. u32 elem_size, index_mask, max_entries;
  66. bool unpriv = !capable(CAP_SYS_ADMIN);
  67. struct bpf_array *array;
  68. u64 array_size, mask64;
  69. elem_size = round_up(attr->value_size, 8);
  70. max_entries = attr->max_entries;
  71. /* On 32 bit archs roundup_pow_of_two() with max_entries that has
  72. * upper most bit set in u32 space is undefined behavior due to
  73. * resulting 1U << 32, so do it manually here in u64 space.
  74. */
  75. mask64 = fls_long(max_entries - 1);
  76. mask64 = 1ULL << mask64;
  77. mask64 -= 1;
  78. index_mask = mask64;
  79. if (unpriv) {
  80. /* round up array size to nearest power of 2,
  81. * since cpu will speculate within index_mask limits
  82. */
  83. max_entries = index_mask + 1;
  84. /* Check for overflows. */
  85. if (max_entries < attr->max_entries)
  86. return ERR_PTR(-E2BIG);
  87. }
  88. array_size = sizeof(*array);
  89. if (percpu)
  90. array_size += (u64) max_entries * sizeof(void *);
  91. else
  92. array_size += (u64) max_entries * elem_size;
  93. /* make sure there is no u32 overflow later in round_up() */
  94. if (array_size >= U32_MAX - PAGE_SIZE)
  95. return ERR_PTR(-ENOMEM);
  96. /* allocate all map elements and zero-initialize them */
  97. array = bpf_map_area_alloc(array_size, numa_node);
  98. if (!array)
  99. return ERR_PTR(-ENOMEM);
  100. array->index_mask = index_mask;
  101. array->map.unpriv_array = unpriv;
  102. /* copy mandatory map attributes */
  103. bpf_map_init_from_attr(&array->map, attr);
  104. array->elem_size = elem_size;
  105. if (!percpu)
  106. goto out;
  107. array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
  108. if (array_size >= U32_MAX - PAGE_SIZE ||
  109. bpf_array_alloc_percpu(array)) {
  110. bpf_map_area_free(array);
  111. return ERR_PTR(-ENOMEM);
  112. }
  113. out:
  114. array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
  115. return &array->map;
  116. }
  117. /* Called from syscall or from eBPF program */
  118. static void *array_map_lookup_elem(struct bpf_map *map, void *key)
  119. {
  120. struct bpf_array *array = container_of(map, struct bpf_array, map);
  121. u32 index = *(u32 *)key;
  122. if (unlikely(index >= array->map.max_entries))
  123. return NULL;
  124. return array->value + array->elem_size * (index & array->index_mask);
  125. }
  126. /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
  127. static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
  128. {
  129. struct bpf_array *array = container_of(map, struct bpf_array, map);
  130. struct bpf_insn *insn = insn_buf;
  131. u32 elem_size = round_up(map->value_size, 8);
  132. const int ret = BPF_REG_0;
  133. const int map_ptr = BPF_REG_1;
  134. const int index = BPF_REG_2;
  135. *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
  136. *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
  137. if (map->unpriv_array) {
  138. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
  139. *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
  140. } else {
  141. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
  142. }
  143. if (is_power_of_2(elem_size)) {
  144. *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
  145. } else {
  146. *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
  147. }
  148. *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
  149. *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
  150. *insn++ = BPF_MOV64_IMM(ret, 0);
  151. return insn - insn_buf;
  152. }
  153. /* Called from eBPF program */
  154. static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
  155. {
  156. struct bpf_array *array = container_of(map, struct bpf_array, map);
  157. u32 index = *(u32 *)key;
  158. if (unlikely(index >= array->map.max_entries))
  159. return NULL;
  160. return this_cpu_ptr(array->pptrs[index & array->index_mask]);
  161. }
  162. int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
  163. {
  164. struct bpf_array *array = container_of(map, struct bpf_array, map);
  165. u32 index = *(u32 *)key;
  166. void __percpu *pptr;
  167. int cpu, off = 0;
  168. u32 size;
  169. if (unlikely(index >= array->map.max_entries))
  170. return -ENOENT;
  171. /* per_cpu areas are zero-filled and bpf programs can only
  172. * access 'value_size' of them, so copying rounded areas
  173. * will not leak any kernel data
  174. */
  175. size = round_up(map->value_size, 8);
  176. rcu_read_lock();
  177. pptr = array->pptrs[index & array->index_mask];
  178. for_each_possible_cpu(cpu) {
  179. bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
  180. off += size;
  181. }
  182. rcu_read_unlock();
  183. return 0;
  184. }
  185. /* Called from syscall */
  186. static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  187. {
  188. struct bpf_array *array = container_of(map, struct bpf_array, map);
  189. u32 index = key ? *(u32 *)key : U32_MAX;
  190. u32 *next = (u32 *)next_key;
  191. if (index >= array->map.max_entries) {
  192. *next = 0;
  193. return 0;
  194. }
  195. if (index == array->map.max_entries - 1)
  196. return -ENOENT;
  197. *next = index + 1;
  198. return 0;
  199. }
  200. /* Called from syscall or from eBPF program */
  201. static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
  202. u64 map_flags)
  203. {
  204. struct bpf_array *array = container_of(map, struct bpf_array, map);
  205. u32 index = *(u32 *)key;
  206. if (unlikely(map_flags > BPF_EXIST))
  207. /* unknown flags */
  208. return -EINVAL;
  209. if (unlikely(index >= array->map.max_entries))
  210. /* all elements were pre-allocated, cannot insert a new one */
  211. return -E2BIG;
  212. if (unlikely(map_flags == BPF_NOEXIST))
  213. /* all elements already exist */
  214. return -EEXIST;
  215. if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  216. memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
  217. value, map->value_size);
  218. else
  219. memcpy(array->value +
  220. array->elem_size * (index & array->index_mask),
  221. value, map->value_size);
  222. return 0;
  223. }
  224. int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
  225. u64 map_flags)
  226. {
  227. struct bpf_array *array = container_of(map, struct bpf_array, map);
  228. u32 index = *(u32 *)key;
  229. void __percpu *pptr;
  230. int cpu, off = 0;
  231. u32 size;
  232. if (unlikely(map_flags > BPF_EXIST))
  233. /* unknown flags */
  234. return -EINVAL;
  235. if (unlikely(index >= array->map.max_entries))
  236. /* all elements were pre-allocated, cannot insert a new one */
  237. return -E2BIG;
  238. if (unlikely(map_flags == BPF_NOEXIST))
  239. /* all elements already exist */
  240. return -EEXIST;
  241. /* the user space will provide round_up(value_size, 8) bytes that
  242. * will be copied into per-cpu area. bpf programs can only access
  243. * value_size of it. During lookup the same extra bytes will be
  244. * returned or zeros which were zero-filled by percpu_alloc,
  245. * so no kernel data leaks possible
  246. */
  247. size = round_up(map->value_size, 8);
  248. rcu_read_lock();
  249. pptr = array->pptrs[index & array->index_mask];
  250. for_each_possible_cpu(cpu) {
  251. bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
  252. off += size;
  253. }
  254. rcu_read_unlock();
  255. return 0;
  256. }
  257. /* Called from syscall or from eBPF program */
  258. static int array_map_delete_elem(struct bpf_map *map, void *key)
  259. {
  260. return -EINVAL;
  261. }
  262. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  263. static void array_map_free(struct bpf_map *map)
  264. {
  265. struct bpf_array *array = container_of(map, struct bpf_array, map);
  266. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  267. * so the programs (can be more than one that used this map) were
  268. * disconnected from events. Wait for outstanding programs to complete
  269. * and free the array
  270. */
  271. synchronize_rcu();
  272. if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  273. bpf_array_free_percpu(array);
  274. bpf_map_area_free(array);
  275. }
  276. const struct bpf_map_ops array_map_ops = {
  277. .map_alloc_check = array_map_alloc_check,
  278. .map_alloc = array_map_alloc,
  279. .map_free = array_map_free,
  280. .map_get_next_key = array_map_get_next_key,
  281. .map_lookup_elem = array_map_lookup_elem,
  282. .map_update_elem = array_map_update_elem,
  283. .map_delete_elem = array_map_delete_elem,
  284. .map_gen_lookup = array_map_gen_lookup,
  285. };
  286. const struct bpf_map_ops percpu_array_map_ops = {
  287. .map_alloc_check = array_map_alloc_check,
  288. .map_alloc = array_map_alloc,
  289. .map_free = array_map_free,
  290. .map_get_next_key = array_map_get_next_key,
  291. .map_lookup_elem = percpu_array_map_lookup_elem,
  292. .map_update_elem = array_map_update_elem,
  293. .map_delete_elem = array_map_delete_elem,
  294. };
  295. static int fd_array_map_alloc_check(union bpf_attr *attr)
  296. {
  297. /* only file descriptors can be stored in this type of map */
  298. if (attr->value_size != sizeof(u32))
  299. return -EINVAL;
  300. return array_map_alloc_check(attr);
  301. }
  302. static void fd_array_map_free(struct bpf_map *map)
  303. {
  304. struct bpf_array *array = container_of(map, struct bpf_array, map);
  305. int i;
  306. synchronize_rcu();
  307. /* make sure it's empty */
  308. for (i = 0; i < array->map.max_entries; i++)
  309. BUG_ON(array->ptrs[i] != NULL);
  310. bpf_map_area_free(array);
  311. }
  312. static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
  313. {
  314. return NULL;
  315. }
  316. /* only called from syscall */
  317. int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
  318. {
  319. void **elem, *ptr;
  320. int ret = 0;
  321. if (!map->ops->map_fd_sys_lookup_elem)
  322. return -ENOTSUPP;
  323. rcu_read_lock();
  324. elem = array_map_lookup_elem(map, key);
  325. if (elem && (ptr = READ_ONCE(*elem)))
  326. *value = map->ops->map_fd_sys_lookup_elem(ptr);
  327. else
  328. ret = -ENOENT;
  329. rcu_read_unlock();
  330. return ret;
  331. }
  332. /* only called from syscall */
  333. int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
  334. void *key, void *value, u64 map_flags)
  335. {
  336. struct bpf_array *array = container_of(map, struct bpf_array, map);
  337. void *new_ptr, *old_ptr;
  338. u32 index = *(u32 *)key, ufd;
  339. if (map_flags != BPF_ANY)
  340. return -EINVAL;
  341. if (index >= array->map.max_entries)
  342. return -E2BIG;
  343. ufd = *(u32 *)value;
  344. new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  345. if (IS_ERR(new_ptr))
  346. return PTR_ERR(new_ptr);
  347. old_ptr = xchg(array->ptrs + index, new_ptr);
  348. if (old_ptr)
  349. map->ops->map_fd_put_ptr(old_ptr);
  350. return 0;
  351. }
  352. static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
  353. {
  354. struct bpf_array *array = container_of(map, struct bpf_array, map);
  355. void *old_ptr;
  356. u32 index = *(u32 *)key;
  357. if (index >= array->map.max_entries)
  358. return -E2BIG;
  359. old_ptr = xchg(array->ptrs + index, NULL);
  360. if (old_ptr) {
  361. map->ops->map_fd_put_ptr(old_ptr);
  362. return 0;
  363. } else {
  364. return -ENOENT;
  365. }
  366. }
  367. static void *prog_fd_array_get_ptr(struct bpf_map *map,
  368. struct file *map_file, int fd)
  369. {
  370. struct bpf_array *array = container_of(map, struct bpf_array, map);
  371. struct bpf_prog *prog = bpf_prog_get(fd);
  372. if (IS_ERR(prog))
  373. return prog;
  374. if (!bpf_prog_array_compatible(array, prog)) {
  375. bpf_prog_put(prog);
  376. return ERR_PTR(-EINVAL);
  377. }
  378. return prog;
  379. }
  380. static void prog_fd_array_put_ptr(void *ptr)
  381. {
  382. bpf_prog_put(ptr);
  383. }
  384. static u32 prog_fd_array_sys_lookup_elem(void *ptr)
  385. {
  386. return ((struct bpf_prog *)ptr)->aux->id;
  387. }
  388. /* decrement refcnt of all bpf_progs that are stored in this map */
  389. void bpf_fd_array_map_clear(struct bpf_map *map)
  390. {
  391. struct bpf_array *array = container_of(map, struct bpf_array, map);
  392. int i;
  393. for (i = 0; i < array->map.max_entries; i++)
  394. fd_array_map_delete_elem(map, &i);
  395. }
  396. const struct bpf_map_ops prog_array_map_ops = {
  397. .map_alloc_check = fd_array_map_alloc_check,
  398. .map_alloc = array_map_alloc,
  399. .map_free = fd_array_map_free,
  400. .map_get_next_key = array_map_get_next_key,
  401. .map_lookup_elem = fd_array_map_lookup_elem,
  402. .map_delete_elem = fd_array_map_delete_elem,
  403. .map_fd_get_ptr = prog_fd_array_get_ptr,
  404. .map_fd_put_ptr = prog_fd_array_put_ptr,
  405. .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
  406. };
  407. static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
  408. struct file *map_file)
  409. {
  410. struct bpf_event_entry *ee;
  411. ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
  412. if (ee) {
  413. ee->event = perf_file->private_data;
  414. ee->perf_file = perf_file;
  415. ee->map_file = map_file;
  416. }
  417. return ee;
  418. }
  419. static void __bpf_event_entry_free(struct rcu_head *rcu)
  420. {
  421. struct bpf_event_entry *ee;
  422. ee = container_of(rcu, struct bpf_event_entry, rcu);
  423. fput(ee->perf_file);
  424. kfree(ee);
  425. }
  426. static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
  427. {
  428. call_rcu(&ee->rcu, __bpf_event_entry_free);
  429. }
  430. static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
  431. struct file *map_file, int fd)
  432. {
  433. struct bpf_event_entry *ee;
  434. struct perf_event *event;
  435. struct file *perf_file;
  436. u64 value;
  437. perf_file = perf_event_get(fd);
  438. if (IS_ERR(perf_file))
  439. return perf_file;
  440. ee = ERR_PTR(-EOPNOTSUPP);
  441. event = perf_file->private_data;
  442. if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
  443. goto err_out;
  444. ee = bpf_event_entry_gen(perf_file, map_file);
  445. if (ee)
  446. return ee;
  447. ee = ERR_PTR(-ENOMEM);
  448. err_out:
  449. fput(perf_file);
  450. return ee;
  451. }
  452. static void perf_event_fd_array_put_ptr(void *ptr)
  453. {
  454. bpf_event_entry_free_rcu(ptr);
  455. }
  456. static void perf_event_fd_array_release(struct bpf_map *map,
  457. struct file *map_file)
  458. {
  459. struct bpf_array *array = container_of(map, struct bpf_array, map);
  460. struct bpf_event_entry *ee;
  461. int i;
  462. rcu_read_lock();
  463. for (i = 0; i < array->map.max_entries; i++) {
  464. ee = READ_ONCE(array->ptrs[i]);
  465. if (ee && ee->map_file == map_file)
  466. fd_array_map_delete_elem(map, &i);
  467. }
  468. rcu_read_unlock();
  469. }
  470. const struct bpf_map_ops perf_event_array_map_ops = {
  471. .map_alloc_check = fd_array_map_alloc_check,
  472. .map_alloc = array_map_alloc,
  473. .map_free = fd_array_map_free,
  474. .map_get_next_key = array_map_get_next_key,
  475. .map_lookup_elem = fd_array_map_lookup_elem,
  476. .map_delete_elem = fd_array_map_delete_elem,
  477. .map_fd_get_ptr = perf_event_fd_array_get_ptr,
  478. .map_fd_put_ptr = perf_event_fd_array_put_ptr,
  479. .map_release = perf_event_fd_array_release,
  480. };
  481. #ifdef CONFIG_CGROUPS
  482. static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
  483. struct file *map_file /* not used */,
  484. int fd)
  485. {
  486. return cgroup_get_from_fd(fd);
  487. }
  488. static void cgroup_fd_array_put_ptr(void *ptr)
  489. {
  490. /* cgroup_put free cgrp after a rcu grace period */
  491. cgroup_put(ptr);
  492. }
  493. static void cgroup_fd_array_free(struct bpf_map *map)
  494. {
  495. bpf_fd_array_map_clear(map);
  496. fd_array_map_free(map);
  497. }
  498. const struct bpf_map_ops cgroup_array_map_ops = {
  499. .map_alloc_check = fd_array_map_alloc_check,
  500. .map_alloc = array_map_alloc,
  501. .map_free = cgroup_fd_array_free,
  502. .map_get_next_key = array_map_get_next_key,
  503. .map_lookup_elem = fd_array_map_lookup_elem,
  504. .map_delete_elem = fd_array_map_delete_elem,
  505. .map_fd_get_ptr = cgroup_fd_array_get_ptr,
  506. .map_fd_put_ptr = cgroup_fd_array_put_ptr,
  507. };
  508. #endif
  509. static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
  510. {
  511. struct bpf_map *map, *inner_map_meta;
  512. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  513. if (IS_ERR(inner_map_meta))
  514. return inner_map_meta;
  515. map = array_map_alloc(attr);
  516. if (IS_ERR(map)) {
  517. bpf_map_meta_free(inner_map_meta);
  518. return map;
  519. }
  520. map->inner_map_meta = inner_map_meta;
  521. return map;
  522. }
  523. static void array_of_map_free(struct bpf_map *map)
  524. {
  525. /* map->inner_map_meta is only accessed by syscall which
  526. * is protected by fdget/fdput.
  527. */
  528. bpf_map_meta_free(map->inner_map_meta);
  529. bpf_fd_array_map_clear(map);
  530. fd_array_map_free(map);
  531. }
  532. static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
  533. {
  534. struct bpf_map **inner_map = array_map_lookup_elem(map, key);
  535. if (!inner_map)
  536. return NULL;
  537. return READ_ONCE(*inner_map);
  538. }
  539. static u32 array_of_map_gen_lookup(struct bpf_map *map,
  540. struct bpf_insn *insn_buf)
  541. {
  542. struct bpf_array *array = container_of(map, struct bpf_array, map);
  543. u32 elem_size = round_up(map->value_size, 8);
  544. struct bpf_insn *insn = insn_buf;
  545. const int ret = BPF_REG_0;
  546. const int map_ptr = BPF_REG_1;
  547. const int index = BPF_REG_2;
  548. *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
  549. *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
  550. if (map->unpriv_array) {
  551. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
  552. *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
  553. } else {
  554. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
  555. }
  556. if (is_power_of_2(elem_size))
  557. *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
  558. else
  559. *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
  560. *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
  561. *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
  562. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
  563. *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
  564. *insn++ = BPF_MOV64_IMM(ret, 0);
  565. return insn - insn_buf;
  566. }
  567. const struct bpf_map_ops array_of_maps_map_ops = {
  568. .map_alloc_check = fd_array_map_alloc_check,
  569. .map_alloc = array_of_map_alloc,
  570. .map_free = array_of_map_free,
  571. .map_get_next_key = array_map_get_next_key,
  572. .map_lookup_elem = array_of_map_lookup_elem,
  573. .map_delete_elem = fd_array_map_delete_elem,
  574. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  575. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  576. .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
  577. .map_gen_lookup = array_of_map_gen_lookup,
  578. };