arraymap.c 18 KB

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