arraymap.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  3. * Copyright (c) 2016,2017 Facebook
  4. */
  5. #include <linux/bpf.h>
  6. #include <linux/btf.h>
  7. #include <linux/err.h>
  8. #include <linux/slab.h>
  9. #include <linux/mm.h>
  10. #include <linux/filter.h>
  11. #include <linux/perf_event.h>
  12. #include <uapi/linux/btf.h>
  13. #include <linux/rcupdate_trace.h>
  14. #include <linux/btf_ids.h>
  15. #include "map_in_map.h"
  16. #define ARRAY_CREATE_FLAG_MASK \
  17. (BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK | \
  18. BPF_F_PRESERVE_ELEMS | BPF_F_INNER_MAP)
  19. static void bpf_array_free_percpu(struct bpf_array *array)
  20. {
  21. int i;
  22. for (i = 0; i < array->map.max_entries; i++) {
  23. free_percpu(array->pptrs[i]);
  24. cond_resched();
  25. }
  26. }
  27. static int bpf_array_alloc_percpu(struct bpf_array *array)
  28. {
  29. void __percpu *ptr;
  30. int i;
  31. for (i = 0; i < array->map.max_entries; i++) {
  32. ptr = bpf_map_alloc_percpu(&array->map, array->elem_size, 8,
  33. GFP_USER | __GFP_NOWARN);
  34. if (!ptr) {
  35. bpf_array_free_percpu(array);
  36. return -ENOMEM;
  37. }
  38. array->pptrs[i] = ptr;
  39. cond_resched();
  40. }
  41. return 0;
  42. }
  43. /* Called from syscall */
  44. 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. !bpf_map_flags_access_ok(attr->map_flags) ||
  53. (percpu && numa_node != NUMA_NO_NODE))
  54. return -EINVAL;
  55. if (attr->map_type != BPF_MAP_TYPE_ARRAY &&
  56. attr->map_flags & (BPF_F_MMAPABLE | BPF_F_INNER_MAP))
  57. return -EINVAL;
  58. if (attr->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
  59. attr->map_flags & BPF_F_PRESERVE_ELEMS)
  60. return -EINVAL;
  61. /* avoid overflow on round_up(map->value_size) */
  62. if (attr->value_size > INT_MAX)
  63. return -E2BIG;
  64. /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
  65. if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
  66. return -E2BIG;
  67. return 0;
  68. }
  69. static struct bpf_map *array_map_alloc(union bpf_attr *attr)
  70. {
  71. bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
  72. int numa_node = bpf_map_attr_numa_node(attr);
  73. u32 elem_size, index_mask, max_entries;
  74. bool bypass_spec_v1 = bpf_bypass_spec_v1(NULL);
  75. u64 array_size, mask64;
  76. struct bpf_array *array;
  77. elem_size = round_up(attr->value_size, 8);
  78. max_entries = attr->max_entries;
  79. /* On 32 bit archs roundup_pow_of_two() with max_entries that has
  80. * upper most bit set in u32 space is undefined behavior due to
  81. * resulting 1U << 32, so do it manually here in u64 space.
  82. */
  83. mask64 = fls_long(max_entries - 1);
  84. mask64 = 1ULL << mask64;
  85. mask64 -= 1;
  86. index_mask = mask64;
  87. if (!bypass_spec_v1) {
  88. /* round up array size to nearest power of 2,
  89. * since cpu will speculate within index_mask limits
  90. */
  91. max_entries = index_mask + 1;
  92. /* Check for overflows. */
  93. if (max_entries < attr->max_entries)
  94. return ERR_PTR(-E2BIG);
  95. }
  96. array_size = sizeof(*array);
  97. if (percpu) {
  98. array_size += (u64) max_entries * sizeof(void *);
  99. } else {
  100. /* rely on vmalloc() to return page-aligned memory and
  101. * ensure array->value is exactly page-aligned
  102. */
  103. if (attr->map_flags & BPF_F_MMAPABLE) {
  104. array_size = PAGE_ALIGN(array_size);
  105. array_size += PAGE_ALIGN((u64) max_entries * elem_size);
  106. } else {
  107. array_size += (u64) max_entries * elem_size;
  108. }
  109. }
  110. /* allocate all map elements and zero-initialize them */
  111. if (attr->map_flags & BPF_F_MMAPABLE) {
  112. void *data;
  113. /* kmalloc'ed memory can't be mmap'ed, use explicit vmalloc */
  114. data = bpf_map_area_mmapable_alloc(array_size, numa_node);
  115. if (!data)
  116. return ERR_PTR(-ENOMEM);
  117. array = data + PAGE_ALIGN(sizeof(struct bpf_array))
  118. - offsetof(struct bpf_array, value);
  119. } else {
  120. array = bpf_map_area_alloc(array_size, numa_node);
  121. }
  122. if (!array)
  123. return ERR_PTR(-ENOMEM);
  124. array->index_mask = index_mask;
  125. array->map.bypass_spec_v1 = bypass_spec_v1;
  126. /* copy mandatory map attributes */
  127. bpf_map_init_from_attr(&array->map, attr);
  128. array->elem_size = elem_size;
  129. if (percpu && bpf_array_alloc_percpu(array)) {
  130. bpf_map_area_free(array);
  131. return ERR_PTR(-ENOMEM);
  132. }
  133. return &array->map;
  134. }
  135. static void *array_map_elem_ptr(struct bpf_array* array, u32 index)
  136. {
  137. return array->value + (u64)array->elem_size * index;
  138. }
  139. /* Called from syscall or from eBPF program */
  140. static void *array_map_lookup_elem(struct bpf_map *map, void *key)
  141. {
  142. struct bpf_array *array = container_of(map, struct bpf_array, map);
  143. u32 index = *(u32 *)key;
  144. if (unlikely(index >= array->map.max_entries))
  145. return NULL;
  146. return array->value + (u64)array->elem_size * (index & array->index_mask);
  147. }
  148. static int array_map_direct_value_addr(const struct bpf_map *map, u64 *imm,
  149. u32 off)
  150. {
  151. struct bpf_array *array = container_of(map, struct bpf_array, map);
  152. if (map->max_entries != 1)
  153. return -ENOTSUPP;
  154. if (off >= map->value_size)
  155. return -EINVAL;
  156. *imm = (unsigned long)array->value;
  157. return 0;
  158. }
  159. static int array_map_direct_value_meta(const struct bpf_map *map, u64 imm,
  160. u32 *off)
  161. {
  162. struct bpf_array *array = container_of(map, struct bpf_array, map);
  163. u64 base = (unsigned long)array->value;
  164. u64 range = array->elem_size;
  165. if (map->max_entries != 1)
  166. return -ENOTSUPP;
  167. if (imm < base || imm >= base + range)
  168. return -ENOENT;
  169. *off = imm - base;
  170. return 0;
  171. }
  172. /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
  173. static int array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
  174. {
  175. struct bpf_array *array = container_of(map, struct bpf_array, map);
  176. struct bpf_insn *insn = insn_buf;
  177. u32 elem_size = array->elem_size;
  178. const int ret = BPF_REG_0;
  179. const int map_ptr = BPF_REG_1;
  180. const int index = BPF_REG_2;
  181. if (map->map_flags & BPF_F_INNER_MAP)
  182. return -EOPNOTSUPP;
  183. *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
  184. *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
  185. if (!map->bypass_spec_v1) {
  186. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
  187. *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
  188. } else {
  189. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
  190. }
  191. if (is_power_of_2(elem_size)) {
  192. *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
  193. } else {
  194. *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
  195. }
  196. *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
  197. *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
  198. *insn++ = BPF_MOV64_IMM(ret, 0);
  199. return insn - insn_buf;
  200. }
  201. /* Called from eBPF program */
  202. static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
  203. {
  204. struct bpf_array *array = container_of(map, struct bpf_array, map);
  205. u32 index = *(u32 *)key;
  206. if (unlikely(index >= array->map.max_entries))
  207. return NULL;
  208. return this_cpu_ptr(array->pptrs[index & array->index_mask]);
  209. }
  210. /* emit BPF instructions equivalent to C code of percpu_array_map_lookup_elem() */
  211. static int percpu_array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
  212. {
  213. struct bpf_array *array = container_of(map, struct bpf_array, map);
  214. struct bpf_insn *insn = insn_buf;
  215. if (!bpf_jit_supports_percpu_insn())
  216. return -EOPNOTSUPP;
  217. if (map->map_flags & BPF_F_INNER_MAP)
  218. return -EOPNOTSUPP;
  219. BUILD_BUG_ON(offsetof(struct bpf_array, map) != 0);
  220. *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, offsetof(struct bpf_array, pptrs));
  221. *insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0);
  222. if (!map->bypass_spec_v1) {
  223. *insn++ = BPF_JMP_IMM(BPF_JGE, BPF_REG_0, map->max_entries, 6);
  224. *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_0, array->index_mask);
  225. } else {
  226. *insn++ = BPF_JMP_IMM(BPF_JGE, BPF_REG_0, map->max_entries, 5);
  227. }
  228. *insn++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3);
  229. *insn++ = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1);
  230. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
  231. *insn++ = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
  232. *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
  233. *insn++ = BPF_MOV64_IMM(BPF_REG_0, 0);
  234. return insn - insn_buf;
  235. }
  236. static void *percpu_array_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu)
  237. {
  238. struct bpf_array *array = container_of(map, struct bpf_array, map);
  239. u32 index = *(u32 *)key;
  240. if (cpu >= nr_cpu_ids)
  241. return NULL;
  242. if (unlikely(index >= array->map.max_entries))
  243. return NULL;
  244. return per_cpu_ptr(array->pptrs[index & array->index_mask], cpu);
  245. }
  246. int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
  247. {
  248. struct bpf_array *array = container_of(map, struct bpf_array, map);
  249. u32 index = *(u32 *)key;
  250. void __percpu *pptr;
  251. int cpu, off = 0;
  252. u32 size;
  253. if (unlikely(index >= array->map.max_entries))
  254. return -ENOENT;
  255. /* per_cpu areas are zero-filled and bpf programs can only
  256. * access 'value_size' of them, so copying rounded areas
  257. * will not leak any kernel data
  258. */
  259. size = array->elem_size;
  260. rcu_read_lock();
  261. pptr = array->pptrs[index & array->index_mask];
  262. for_each_possible_cpu(cpu) {
  263. copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
  264. check_and_init_map_value(map, value + off);
  265. off += size;
  266. }
  267. rcu_read_unlock();
  268. return 0;
  269. }
  270. /* Called from syscall */
  271. static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  272. {
  273. struct bpf_array *array = container_of(map, struct bpf_array, map);
  274. u32 index = key ? *(u32 *)key : U32_MAX;
  275. u32 *next = (u32 *)next_key;
  276. if (index >= array->map.max_entries) {
  277. *next = 0;
  278. return 0;
  279. }
  280. if (index == array->map.max_entries - 1)
  281. return -ENOENT;
  282. *next = index + 1;
  283. return 0;
  284. }
  285. /* Called from syscall or from eBPF program */
  286. static long array_map_update_elem(struct bpf_map *map, void *key, void *value,
  287. u64 map_flags)
  288. {
  289. struct bpf_array *array = container_of(map, struct bpf_array, map);
  290. u32 index = *(u32 *)key;
  291. char *val;
  292. if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
  293. /* unknown flags */
  294. return -EINVAL;
  295. if (unlikely(index >= array->map.max_entries))
  296. /* all elements were pre-allocated, cannot insert a new one */
  297. return -E2BIG;
  298. if (unlikely(map_flags & BPF_NOEXIST))
  299. /* all elements already exist */
  300. return -EEXIST;
  301. if (unlikely((map_flags & BPF_F_LOCK) &&
  302. !btf_record_has_field(map->record, BPF_SPIN_LOCK)))
  303. return -EINVAL;
  304. if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  305. val = this_cpu_ptr(array->pptrs[index & array->index_mask]);
  306. copy_map_value(map, val, value);
  307. bpf_obj_free_fields(array->map.record, val);
  308. } else {
  309. val = array->value +
  310. (u64)array->elem_size * (index & array->index_mask);
  311. if (map_flags & BPF_F_LOCK)
  312. copy_map_value_locked(map, val, value, false);
  313. else
  314. copy_map_value(map, val, value);
  315. bpf_obj_free_fields(array->map.record, val);
  316. }
  317. return 0;
  318. }
  319. int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
  320. u64 map_flags)
  321. {
  322. struct bpf_array *array = container_of(map, struct bpf_array, map);
  323. u32 index = *(u32 *)key;
  324. void __percpu *pptr;
  325. int cpu, off = 0;
  326. u32 size;
  327. if (unlikely(map_flags > BPF_EXIST))
  328. /* unknown flags */
  329. return -EINVAL;
  330. if (unlikely(index >= array->map.max_entries))
  331. /* all elements were pre-allocated, cannot insert a new one */
  332. return -E2BIG;
  333. if (unlikely(map_flags == BPF_NOEXIST))
  334. /* all elements already exist */
  335. return -EEXIST;
  336. /* the user space will provide round_up(value_size, 8) bytes that
  337. * will be copied into per-cpu area. bpf programs can only access
  338. * value_size of it. During lookup the same extra bytes will be
  339. * returned or zeros which were zero-filled by percpu_alloc,
  340. * so no kernel data leaks possible
  341. */
  342. size = array->elem_size;
  343. rcu_read_lock();
  344. pptr = array->pptrs[index & array->index_mask];
  345. for_each_possible_cpu(cpu) {
  346. copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value + off);
  347. bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu));
  348. off += size;
  349. }
  350. rcu_read_unlock();
  351. return 0;
  352. }
  353. /* Called from syscall or from eBPF program */
  354. static long array_map_delete_elem(struct bpf_map *map, void *key)
  355. {
  356. return -EINVAL;
  357. }
  358. static void *array_map_vmalloc_addr(struct bpf_array *array)
  359. {
  360. return (void *)round_down((unsigned long)array, PAGE_SIZE);
  361. }
  362. static void array_map_free_timers_wq(struct bpf_map *map)
  363. {
  364. struct bpf_array *array = container_of(map, struct bpf_array, map);
  365. int i;
  366. /* We don't reset or free fields other than timer and workqueue
  367. * on uref dropping to zero.
  368. */
  369. if (btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE)) {
  370. for (i = 0; i < array->map.max_entries; i++) {
  371. if (btf_record_has_field(map->record, BPF_TIMER))
  372. bpf_obj_free_timer(map->record, array_map_elem_ptr(array, i));
  373. if (btf_record_has_field(map->record, BPF_WORKQUEUE))
  374. bpf_obj_free_workqueue(map->record, array_map_elem_ptr(array, i));
  375. }
  376. }
  377. }
  378. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  379. static void array_map_free(struct bpf_map *map)
  380. {
  381. struct bpf_array *array = container_of(map, struct bpf_array, map);
  382. int i;
  383. if (!IS_ERR_OR_NULL(map->record)) {
  384. if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  385. for (i = 0; i < array->map.max_entries; i++) {
  386. void __percpu *pptr = array->pptrs[i & array->index_mask];
  387. int cpu;
  388. for_each_possible_cpu(cpu) {
  389. bpf_obj_free_fields(map->record, per_cpu_ptr(pptr, cpu));
  390. cond_resched();
  391. }
  392. }
  393. } else {
  394. for (i = 0; i < array->map.max_entries; i++)
  395. bpf_obj_free_fields(map->record, array_map_elem_ptr(array, i));
  396. }
  397. }
  398. if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  399. bpf_array_free_percpu(array);
  400. if (array->map.map_flags & BPF_F_MMAPABLE)
  401. bpf_map_area_free(array_map_vmalloc_addr(array));
  402. else
  403. bpf_map_area_free(array);
  404. }
  405. static void array_map_seq_show_elem(struct bpf_map *map, void *key,
  406. struct seq_file *m)
  407. {
  408. void *value;
  409. rcu_read_lock();
  410. value = array_map_lookup_elem(map, key);
  411. if (!value) {
  412. rcu_read_unlock();
  413. return;
  414. }
  415. if (map->btf_key_type_id)
  416. seq_printf(m, "%u: ", *(u32 *)key);
  417. btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
  418. seq_putc(m, '\n');
  419. rcu_read_unlock();
  420. }
  421. static void percpu_array_map_seq_show_elem(struct bpf_map *map, void *key,
  422. struct seq_file *m)
  423. {
  424. struct bpf_array *array = container_of(map, struct bpf_array, map);
  425. u32 index = *(u32 *)key;
  426. void __percpu *pptr;
  427. int cpu;
  428. rcu_read_lock();
  429. seq_printf(m, "%u: {\n", *(u32 *)key);
  430. pptr = array->pptrs[index & array->index_mask];
  431. for_each_possible_cpu(cpu) {
  432. seq_printf(m, "\tcpu%d: ", cpu);
  433. btf_type_seq_show(map->btf, map->btf_value_type_id,
  434. per_cpu_ptr(pptr, cpu), m);
  435. seq_putc(m, '\n');
  436. }
  437. seq_puts(m, "}\n");
  438. rcu_read_unlock();
  439. }
  440. static int array_map_check_btf(const struct bpf_map *map,
  441. const struct btf *btf,
  442. const struct btf_type *key_type,
  443. const struct btf_type *value_type)
  444. {
  445. u32 int_data;
  446. /* One exception for keyless BTF: .bss/.data/.rodata map */
  447. if (btf_type_is_void(key_type)) {
  448. if (map->map_type != BPF_MAP_TYPE_ARRAY ||
  449. map->max_entries != 1)
  450. return -EINVAL;
  451. if (BTF_INFO_KIND(value_type->info) != BTF_KIND_DATASEC)
  452. return -EINVAL;
  453. return 0;
  454. }
  455. if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
  456. return -EINVAL;
  457. int_data = *(u32 *)(key_type + 1);
  458. /* bpf array can only take a u32 key. This check makes sure
  459. * that the btf matches the attr used during map_create.
  460. */
  461. if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
  462. return -EINVAL;
  463. return 0;
  464. }
  465. static int array_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
  466. {
  467. struct bpf_array *array = container_of(map, struct bpf_array, map);
  468. pgoff_t pgoff = PAGE_ALIGN(sizeof(*array)) >> PAGE_SHIFT;
  469. if (!(map->map_flags & BPF_F_MMAPABLE))
  470. return -EINVAL;
  471. if (vma->vm_pgoff * PAGE_SIZE + (vma->vm_end - vma->vm_start) >
  472. PAGE_ALIGN((u64)array->map.max_entries * array->elem_size))
  473. return -EINVAL;
  474. return remap_vmalloc_range(vma, array_map_vmalloc_addr(array),
  475. vma->vm_pgoff + pgoff);
  476. }
  477. static bool array_map_meta_equal(const struct bpf_map *meta0,
  478. const struct bpf_map *meta1)
  479. {
  480. if (!bpf_map_meta_equal(meta0, meta1))
  481. return false;
  482. return meta0->map_flags & BPF_F_INNER_MAP ? true :
  483. meta0->max_entries == meta1->max_entries;
  484. }
  485. struct bpf_iter_seq_array_map_info {
  486. struct bpf_map *map;
  487. void *percpu_value_buf;
  488. u32 index;
  489. };
  490. static void *bpf_array_map_seq_start(struct seq_file *seq, loff_t *pos)
  491. {
  492. struct bpf_iter_seq_array_map_info *info = seq->private;
  493. struct bpf_map *map = info->map;
  494. struct bpf_array *array;
  495. u32 index;
  496. if (info->index >= map->max_entries)
  497. return NULL;
  498. if (*pos == 0)
  499. ++*pos;
  500. array = container_of(map, struct bpf_array, map);
  501. index = info->index & array->index_mask;
  502. if (info->percpu_value_buf)
  503. return (void *)(uintptr_t)array->pptrs[index];
  504. return array_map_elem_ptr(array, index);
  505. }
  506. static void *bpf_array_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  507. {
  508. struct bpf_iter_seq_array_map_info *info = seq->private;
  509. struct bpf_map *map = info->map;
  510. struct bpf_array *array;
  511. u32 index;
  512. ++*pos;
  513. ++info->index;
  514. if (info->index >= map->max_entries)
  515. return NULL;
  516. array = container_of(map, struct bpf_array, map);
  517. index = info->index & array->index_mask;
  518. if (info->percpu_value_buf)
  519. return (void *)(uintptr_t)array->pptrs[index];
  520. return array_map_elem_ptr(array, index);
  521. }
  522. static int __bpf_array_map_seq_show(struct seq_file *seq, void *v)
  523. {
  524. struct bpf_iter_seq_array_map_info *info = seq->private;
  525. struct bpf_iter__bpf_map_elem ctx = {};
  526. struct bpf_map *map = info->map;
  527. struct bpf_array *array = container_of(map, struct bpf_array, map);
  528. struct bpf_iter_meta meta;
  529. struct bpf_prog *prog;
  530. int off = 0, cpu = 0;
  531. void __percpu *pptr;
  532. u32 size;
  533. meta.seq = seq;
  534. prog = bpf_iter_get_info(&meta, v == NULL);
  535. if (!prog)
  536. return 0;
  537. ctx.meta = &meta;
  538. ctx.map = info->map;
  539. if (v) {
  540. ctx.key = &info->index;
  541. if (!info->percpu_value_buf) {
  542. ctx.value = v;
  543. } else {
  544. pptr = (void __percpu *)(uintptr_t)v;
  545. size = array->elem_size;
  546. for_each_possible_cpu(cpu) {
  547. copy_map_value_long(map, info->percpu_value_buf + off,
  548. per_cpu_ptr(pptr, cpu));
  549. check_and_init_map_value(map, info->percpu_value_buf + off);
  550. off += size;
  551. }
  552. ctx.value = info->percpu_value_buf;
  553. }
  554. }
  555. return bpf_iter_run_prog(prog, &ctx);
  556. }
  557. static int bpf_array_map_seq_show(struct seq_file *seq, void *v)
  558. {
  559. return __bpf_array_map_seq_show(seq, v);
  560. }
  561. static void bpf_array_map_seq_stop(struct seq_file *seq, void *v)
  562. {
  563. if (!v)
  564. (void)__bpf_array_map_seq_show(seq, NULL);
  565. }
  566. static int bpf_iter_init_array_map(void *priv_data,
  567. struct bpf_iter_aux_info *aux)
  568. {
  569. struct bpf_iter_seq_array_map_info *seq_info = priv_data;
  570. struct bpf_map *map = aux->map;
  571. struct bpf_array *array = container_of(map, struct bpf_array, map);
  572. void *value_buf;
  573. u32 buf_size;
  574. if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  575. buf_size = array->elem_size * num_possible_cpus();
  576. value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN);
  577. if (!value_buf)
  578. return -ENOMEM;
  579. seq_info->percpu_value_buf = value_buf;
  580. }
  581. /* bpf_iter_attach_map() acquires a map uref, and the uref may be
  582. * released before or in the middle of iterating map elements, so
  583. * acquire an extra map uref for iterator.
  584. */
  585. bpf_map_inc_with_uref(map);
  586. seq_info->map = map;
  587. return 0;
  588. }
  589. static void bpf_iter_fini_array_map(void *priv_data)
  590. {
  591. struct bpf_iter_seq_array_map_info *seq_info = priv_data;
  592. bpf_map_put_with_uref(seq_info->map);
  593. kfree(seq_info->percpu_value_buf);
  594. }
  595. static const struct seq_operations bpf_array_map_seq_ops = {
  596. .start = bpf_array_map_seq_start,
  597. .next = bpf_array_map_seq_next,
  598. .stop = bpf_array_map_seq_stop,
  599. .show = bpf_array_map_seq_show,
  600. };
  601. static const struct bpf_iter_seq_info iter_seq_info = {
  602. .seq_ops = &bpf_array_map_seq_ops,
  603. .init_seq_private = bpf_iter_init_array_map,
  604. .fini_seq_private = bpf_iter_fini_array_map,
  605. .seq_priv_size = sizeof(struct bpf_iter_seq_array_map_info),
  606. };
  607. static long bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_fn,
  608. void *callback_ctx, u64 flags)
  609. {
  610. u32 i, key, num_elems = 0;
  611. struct bpf_array *array;
  612. bool is_percpu;
  613. u64 ret = 0;
  614. void *val;
  615. if (flags != 0)
  616. return -EINVAL;
  617. is_percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
  618. array = container_of(map, struct bpf_array, map);
  619. if (is_percpu)
  620. migrate_disable();
  621. for (i = 0; i < map->max_entries; i++) {
  622. if (is_percpu)
  623. val = this_cpu_ptr(array->pptrs[i]);
  624. else
  625. val = array_map_elem_ptr(array, i);
  626. num_elems++;
  627. key = i;
  628. ret = callback_fn((u64)(long)map, (u64)(long)&key,
  629. (u64)(long)val, (u64)(long)callback_ctx, 0);
  630. /* return value: 0 - continue, 1 - stop and return */
  631. if (ret)
  632. break;
  633. }
  634. if (is_percpu)
  635. migrate_enable();
  636. return num_elems;
  637. }
  638. static u64 array_map_mem_usage(const struct bpf_map *map)
  639. {
  640. struct bpf_array *array = container_of(map, struct bpf_array, map);
  641. bool percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
  642. u32 elem_size = array->elem_size;
  643. u64 entries = map->max_entries;
  644. u64 usage = sizeof(*array);
  645. if (percpu) {
  646. usage += entries * sizeof(void *);
  647. usage += entries * elem_size * num_possible_cpus();
  648. } else {
  649. if (map->map_flags & BPF_F_MMAPABLE) {
  650. usage = PAGE_ALIGN(usage);
  651. usage += PAGE_ALIGN(entries * elem_size);
  652. } else {
  653. usage += entries * elem_size;
  654. }
  655. }
  656. return usage;
  657. }
  658. BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)
  659. const struct bpf_map_ops array_map_ops = {
  660. .map_meta_equal = array_map_meta_equal,
  661. .map_alloc_check = array_map_alloc_check,
  662. .map_alloc = array_map_alloc,
  663. .map_free = array_map_free,
  664. .map_get_next_key = array_map_get_next_key,
  665. .map_release_uref = array_map_free_timers_wq,
  666. .map_lookup_elem = array_map_lookup_elem,
  667. .map_update_elem = array_map_update_elem,
  668. .map_delete_elem = array_map_delete_elem,
  669. .map_gen_lookup = array_map_gen_lookup,
  670. .map_direct_value_addr = array_map_direct_value_addr,
  671. .map_direct_value_meta = array_map_direct_value_meta,
  672. .map_mmap = array_map_mmap,
  673. .map_seq_show_elem = array_map_seq_show_elem,
  674. .map_check_btf = array_map_check_btf,
  675. .map_lookup_batch = generic_map_lookup_batch,
  676. .map_update_batch = generic_map_update_batch,
  677. .map_set_for_each_callback_args = map_set_for_each_callback_args,
  678. .map_for_each_callback = bpf_for_each_array_elem,
  679. .map_mem_usage = array_map_mem_usage,
  680. .map_btf_id = &array_map_btf_ids[0],
  681. .iter_seq_info = &iter_seq_info,
  682. };
  683. const struct bpf_map_ops percpu_array_map_ops = {
  684. .map_meta_equal = bpf_map_meta_equal,
  685. .map_alloc_check = array_map_alloc_check,
  686. .map_alloc = array_map_alloc,
  687. .map_free = array_map_free,
  688. .map_get_next_key = array_map_get_next_key,
  689. .map_lookup_elem = percpu_array_map_lookup_elem,
  690. .map_gen_lookup = percpu_array_map_gen_lookup,
  691. .map_update_elem = array_map_update_elem,
  692. .map_delete_elem = array_map_delete_elem,
  693. .map_lookup_percpu_elem = percpu_array_map_lookup_percpu_elem,
  694. .map_seq_show_elem = percpu_array_map_seq_show_elem,
  695. .map_check_btf = array_map_check_btf,
  696. .map_lookup_batch = generic_map_lookup_batch,
  697. .map_update_batch = generic_map_update_batch,
  698. .map_set_for_each_callback_args = map_set_for_each_callback_args,
  699. .map_for_each_callback = bpf_for_each_array_elem,
  700. .map_mem_usage = array_map_mem_usage,
  701. .map_btf_id = &array_map_btf_ids[0],
  702. .iter_seq_info = &iter_seq_info,
  703. };
  704. static int fd_array_map_alloc_check(union bpf_attr *attr)
  705. {
  706. /* only file descriptors can be stored in this type of map */
  707. if (attr->value_size != sizeof(u32))
  708. return -EINVAL;
  709. /* Program read-only/write-only not supported for special maps yet. */
  710. if (attr->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG))
  711. return -EINVAL;
  712. return array_map_alloc_check(attr);
  713. }
  714. static void fd_array_map_free(struct bpf_map *map)
  715. {
  716. struct bpf_array *array = container_of(map, struct bpf_array, map);
  717. int i;
  718. /* make sure it's empty */
  719. for (i = 0; i < array->map.max_entries; i++)
  720. BUG_ON(array->ptrs[i] != NULL);
  721. bpf_map_area_free(array);
  722. }
  723. static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
  724. {
  725. return ERR_PTR(-EOPNOTSUPP);
  726. }
  727. /* only called from syscall */
  728. int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
  729. {
  730. void **elem, *ptr;
  731. int ret = 0;
  732. if (!map->ops->map_fd_sys_lookup_elem)
  733. return -ENOTSUPP;
  734. rcu_read_lock();
  735. elem = array_map_lookup_elem(map, key);
  736. if (elem && (ptr = READ_ONCE(*elem)))
  737. *value = map->ops->map_fd_sys_lookup_elem(ptr);
  738. else
  739. ret = -ENOENT;
  740. rcu_read_unlock();
  741. return ret;
  742. }
  743. /* only called from syscall */
  744. int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
  745. void *key, void *value, u64 map_flags)
  746. {
  747. struct bpf_array *array = container_of(map, struct bpf_array, map);
  748. void *new_ptr, *old_ptr;
  749. u32 index = *(u32 *)key, ufd;
  750. if (map_flags != BPF_ANY)
  751. return -EINVAL;
  752. if (index >= array->map.max_entries)
  753. return -E2BIG;
  754. ufd = *(u32 *)value;
  755. new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  756. if (IS_ERR(new_ptr))
  757. return PTR_ERR(new_ptr);
  758. if (map->ops->map_poke_run) {
  759. mutex_lock(&array->aux->poke_mutex);
  760. old_ptr = xchg(array->ptrs + index, new_ptr);
  761. map->ops->map_poke_run(map, index, old_ptr, new_ptr);
  762. mutex_unlock(&array->aux->poke_mutex);
  763. } else {
  764. old_ptr = xchg(array->ptrs + index, new_ptr);
  765. }
  766. if (old_ptr)
  767. map->ops->map_fd_put_ptr(map, old_ptr, true);
  768. return 0;
  769. }
  770. static long __fd_array_map_delete_elem(struct bpf_map *map, void *key, bool need_defer)
  771. {
  772. struct bpf_array *array = container_of(map, struct bpf_array, map);
  773. void *old_ptr;
  774. u32 index = *(u32 *)key;
  775. if (index >= array->map.max_entries)
  776. return -E2BIG;
  777. if (map->ops->map_poke_run) {
  778. mutex_lock(&array->aux->poke_mutex);
  779. old_ptr = xchg(array->ptrs + index, NULL);
  780. map->ops->map_poke_run(map, index, old_ptr, NULL);
  781. mutex_unlock(&array->aux->poke_mutex);
  782. } else {
  783. old_ptr = xchg(array->ptrs + index, NULL);
  784. }
  785. if (old_ptr) {
  786. map->ops->map_fd_put_ptr(map, old_ptr, need_defer);
  787. return 0;
  788. } else {
  789. return -ENOENT;
  790. }
  791. }
  792. static long fd_array_map_delete_elem(struct bpf_map *map, void *key)
  793. {
  794. return __fd_array_map_delete_elem(map, key, true);
  795. }
  796. static void *prog_fd_array_get_ptr(struct bpf_map *map,
  797. struct file *map_file, int fd)
  798. {
  799. struct bpf_prog *prog = bpf_prog_get(fd);
  800. bool is_extended;
  801. if (IS_ERR(prog))
  802. return prog;
  803. if (prog->type == BPF_PROG_TYPE_EXT ||
  804. !bpf_prog_map_compatible(map, prog)) {
  805. bpf_prog_put(prog);
  806. return ERR_PTR(-EINVAL);
  807. }
  808. mutex_lock(&prog->aux->ext_mutex);
  809. is_extended = prog->aux->is_extended;
  810. if (!is_extended)
  811. prog->aux->prog_array_member_cnt++;
  812. mutex_unlock(&prog->aux->ext_mutex);
  813. if (is_extended) {
  814. /* Extended prog can not be tail callee. It's to prevent a
  815. * potential infinite loop like:
  816. * tail callee prog entry -> tail callee prog subprog ->
  817. * freplace prog entry --tailcall-> tail callee prog entry.
  818. */
  819. bpf_prog_put(prog);
  820. return ERR_PTR(-EBUSY);
  821. }
  822. return prog;
  823. }
  824. static void prog_fd_array_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
  825. {
  826. struct bpf_prog *prog = ptr;
  827. mutex_lock(&prog->aux->ext_mutex);
  828. prog->aux->prog_array_member_cnt--;
  829. mutex_unlock(&prog->aux->ext_mutex);
  830. /* bpf_prog is freed after one RCU or tasks trace grace period */
  831. bpf_prog_put(prog);
  832. }
  833. static u32 prog_fd_array_sys_lookup_elem(void *ptr)
  834. {
  835. return ((struct bpf_prog *)ptr)->aux->id;
  836. }
  837. /* decrement refcnt of all bpf_progs that are stored in this map */
  838. static void bpf_fd_array_map_clear(struct bpf_map *map, bool need_defer)
  839. {
  840. struct bpf_array *array = container_of(map, struct bpf_array, map);
  841. int i;
  842. for (i = 0; i < array->map.max_entries; i++)
  843. __fd_array_map_delete_elem(map, &i, need_defer);
  844. }
  845. static void prog_array_map_seq_show_elem(struct bpf_map *map, void *key,
  846. struct seq_file *m)
  847. {
  848. void **elem, *ptr;
  849. u32 prog_id;
  850. rcu_read_lock();
  851. elem = array_map_lookup_elem(map, key);
  852. if (elem) {
  853. ptr = READ_ONCE(*elem);
  854. if (ptr) {
  855. seq_printf(m, "%u: ", *(u32 *)key);
  856. prog_id = prog_fd_array_sys_lookup_elem(ptr);
  857. btf_type_seq_show(map->btf, map->btf_value_type_id,
  858. &prog_id, m);
  859. seq_putc(m, '\n');
  860. }
  861. }
  862. rcu_read_unlock();
  863. }
  864. struct prog_poke_elem {
  865. struct list_head list;
  866. struct bpf_prog_aux *aux;
  867. };
  868. static int prog_array_map_poke_track(struct bpf_map *map,
  869. struct bpf_prog_aux *prog_aux)
  870. {
  871. struct prog_poke_elem *elem;
  872. struct bpf_array_aux *aux;
  873. int ret = 0;
  874. aux = container_of(map, struct bpf_array, map)->aux;
  875. mutex_lock(&aux->poke_mutex);
  876. list_for_each_entry(elem, &aux->poke_progs, list) {
  877. if (elem->aux == prog_aux)
  878. goto out;
  879. }
  880. elem = kmalloc(sizeof(*elem), GFP_KERNEL);
  881. if (!elem) {
  882. ret = -ENOMEM;
  883. goto out;
  884. }
  885. INIT_LIST_HEAD(&elem->list);
  886. /* We must track the program's aux info at this point in time
  887. * since the program pointer itself may not be stable yet, see
  888. * also comment in prog_array_map_poke_run().
  889. */
  890. elem->aux = prog_aux;
  891. list_add_tail(&elem->list, &aux->poke_progs);
  892. out:
  893. mutex_unlock(&aux->poke_mutex);
  894. return ret;
  895. }
  896. static void prog_array_map_poke_untrack(struct bpf_map *map,
  897. struct bpf_prog_aux *prog_aux)
  898. {
  899. struct prog_poke_elem *elem, *tmp;
  900. struct bpf_array_aux *aux;
  901. aux = container_of(map, struct bpf_array, map)->aux;
  902. mutex_lock(&aux->poke_mutex);
  903. list_for_each_entry_safe(elem, tmp, &aux->poke_progs, list) {
  904. if (elem->aux == prog_aux) {
  905. list_del_init(&elem->list);
  906. kfree(elem);
  907. break;
  908. }
  909. }
  910. mutex_unlock(&aux->poke_mutex);
  911. }
  912. void __weak bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,
  913. struct bpf_prog *new, struct bpf_prog *old)
  914. {
  915. WARN_ON_ONCE(1);
  916. }
  917. static void prog_array_map_poke_run(struct bpf_map *map, u32 key,
  918. struct bpf_prog *old,
  919. struct bpf_prog *new)
  920. {
  921. struct prog_poke_elem *elem;
  922. struct bpf_array_aux *aux;
  923. aux = container_of(map, struct bpf_array, map)->aux;
  924. WARN_ON_ONCE(!mutex_is_locked(&aux->poke_mutex));
  925. list_for_each_entry(elem, &aux->poke_progs, list) {
  926. struct bpf_jit_poke_descriptor *poke;
  927. int i;
  928. for (i = 0; i < elem->aux->size_poke_tab; i++) {
  929. poke = &elem->aux->poke_tab[i];
  930. /* Few things to be aware of:
  931. *
  932. * 1) We can only ever access aux in this context, but
  933. * not aux->prog since it might not be stable yet and
  934. * there could be danger of use after free otherwise.
  935. * 2) Initially when we start tracking aux, the program
  936. * is not JITed yet and also does not have a kallsyms
  937. * entry. We skip these as poke->tailcall_target_stable
  938. * is not active yet. The JIT will do the final fixup
  939. * before setting it stable. The various
  940. * poke->tailcall_target_stable are successively
  941. * activated, so tail call updates can arrive from here
  942. * while JIT is still finishing its final fixup for
  943. * non-activated poke entries.
  944. * 3) Also programs reaching refcount of zero while patching
  945. * is in progress is okay since we're protected under
  946. * poke_mutex and untrack the programs before the JIT
  947. * buffer is freed.
  948. */
  949. if (!READ_ONCE(poke->tailcall_target_stable))
  950. continue;
  951. if (poke->reason != BPF_POKE_REASON_TAIL_CALL)
  952. continue;
  953. if (poke->tail_call.map != map ||
  954. poke->tail_call.key != key)
  955. continue;
  956. bpf_arch_poke_desc_update(poke, new, old);
  957. }
  958. }
  959. }
  960. static void prog_array_map_clear_deferred(struct work_struct *work)
  961. {
  962. struct bpf_map *map = container_of(work, struct bpf_array_aux,
  963. work)->map;
  964. bpf_fd_array_map_clear(map, true);
  965. bpf_map_put(map);
  966. }
  967. static void prog_array_map_clear(struct bpf_map *map)
  968. {
  969. struct bpf_array_aux *aux = container_of(map, struct bpf_array,
  970. map)->aux;
  971. bpf_map_inc(map);
  972. schedule_work(&aux->work);
  973. }
  974. static struct bpf_map *prog_array_map_alloc(union bpf_attr *attr)
  975. {
  976. struct bpf_array_aux *aux;
  977. struct bpf_map *map;
  978. aux = kzalloc(sizeof(*aux), GFP_KERNEL_ACCOUNT);
  979. if (!aux)
  980. return ERR_PTR(-ENOMEM);
  981. INIT_WORK(&aux->work, prog_array_map_clear_deferred);
  982. INIT_LIST_HEAD(&aux->poke_progs);
  983. mutex_init(&aux->poke_mutex);
  984. map = array_map_alloc(attr);
  985. if (IS_ERR(map)) {
  986. kfree(aux);
  987. return map;
  988. }
  989. container_of(map, struct bpf_array, map)->aux = aux;
  990. aux->map = map;
  991. return map;
  992. }
  993. static void prog_array_map_free(struct bpf_map *map)
  994. {
  995. struct prog_poke_elem *elem, *tmp;
  996. struct bpf_array_aux *aux;
  997. aux = container_of(map, struct bpf_array, map)->aux;
  998. list_for_each_entry_safe(elem, tmp, &aux->poke_progs, list) {
  999. list_del_init(&elem->list);
  1000. kfree(elem);
  1001. }
  1002. kfree(aux);
  1003. fd_array_map_free(map);
  1004. }
  1005. /* prog_array->aux->{type,jited} is a runtime binding.
  1006. * Doing static check alone in the verifier is not enough.
  1007. * Thus, prog_array_map cannot be used as an inner_map
  1008. * and map_meta_equal is not implemented.
  1009. */
  1010. const struct bpf_map_ops prog_array_map_ops = {
  1011. .map_alloc_check = fd_array_map_alloc_check,
  1012. .map_alloc = prog_array_map_alloc,
  1013. .map_free = prog_array_map_free,
  1014. .map_poke_track = prog_array_map_poke_track,
  1015. .map_poke_untrack = prog_array_map_poke_untrack,
  1016. .map_poke_run = prog_array_map_poke_run,
  1017. .map_get_next_key = array_map_get_next_key,
  1018. .map_lookup_elem = fd_array_map_lookup_elem,
  1019. .map_delete_elem = fd_array_map_delete_elem,
  1020. .map_fd_get_ptr = prog_fd_array_get_ptr,
  1021. .map_fd_put_ptr = prog_fd_array_put_ptr,
  1022. .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
  1023. .map_release_uref = prog_array_map_clear,
  1024. .map_seq_show_elem = prog_array_map_seq_show_elem,
  1025. .map_mem_usage = array_map_mem_usage,
  1026. .map_btf_id = &array_map_btf_ids[0],
  1027. };
  1028. static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
  1029. struct file *map_file)
  1030. {
  1031. struct bpf_event_entry *ee;
  1032. ee = kzalloc(sizeof(*ee), GFP_KERNEL);
  1033. if (ee) {
  1034. ee->event = perf_file->private_data;
  1035. ee->perf_file = perf_file;
  1036. ee->map_file = map_file;
  1037. }
  1038. return ee;
  1039. }
  1040. static void __bpf_event_entry_free(struct rcu_head *rcu)
  1041. {
  1042. struct bpf_event_entry *ee;
  1043. ee = container_of(rcu, struct bpf_event_entry, rcu);
  1044. fput(ee->perf_file);
  1045. kfree(ee);
  1046. }
  1047. static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
  1048. {
  1049. call_rcu(&ee->rcu, __bpf_event_entry_free);
  1050. }
  1051. static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
  1052. struct file *map_file, int fd)
  1053. {
  1054. struct bpf_event_entry *ee;
  1055. struct perf_event *event;
  1056. struct file *perf_file;
  1057. u64 value;
  1058. perf_file = perf_event_get(fd);
  1059. if (IS_ERR(perf_file))
  1060. return perf_file;
  1061. ee = ERR_PTR(-EOPNOTSUPP);
  1062. event = perf_file->private_data;
  1063. if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
  1064. goto err_out;
  1065. ee = bpf_event_entry_gen(perf_file, map_file);
  1066. if (ee)
  1067. return ee;
  1068. ee = ERR_PTR(-ENOMEM);
  1069. err_out:
  1070. fput(perf_file);
  1071. return ee;
  1072. }
  1073. static void perf_event_fd_array_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
  1074. {
  1075. /* bpf_perf_event is freed after one RCU grace period */
  1076. bpf_event_entry_free_rcu(ptr);
  1077. }
  1078. static void perf_event_fd_array_release(struct bpf_map *map,
  1079. struct file *map_file)
  1080. {
  1081. struct bpf_array *array = container_of(map, struct bpf_array, map);
  1082. struct bpf_event_entry *ee;
  1083. int i;
  1084. if (map->map_flags & BPF_F_PRESERVE_ELEMS)
  1085. return;
  1086. rcu_read_lock();
  1087. for (i = 0; i < array->map.max_entries; i++) {
  1088. ee = READ_ONCE(array->ptrs[i]);
  1089. if (ee && ee->map_file == map_file)
  1090. __fd_array_map_delete_elem(map, &i, true);
  1091. }
  1092. rcu_read_unlock();
  1093. }
  1094. static void perf_event_fd_array_map_free(struct bpf_map *map)
  1095. {
  1096. if (map->map_flags & BPF_F_PRESERVE_ELEMS)
  1097. bpf_fd_array_map_clear(map, false);
  1098. fd_array_map_free(map);
  1099. }
  1100. const struct bpf_map_ops perf_event_array_map_ops = {
  1101. .map_meta_equal = bpf_map_meta_equal,
  1102. .map_alloc_check = fd_array_map_alloc_check,
  1103. .map_alloc = array_map_alloc,
  1104. .map_free = perf_event_fd_array_map_free,
  1105. .map_get_next_key = array_map_get_next_key,
  1106. .map_lookup_elem = fd_array_map_lookup_elem,
  1107. .map_delete_elem = fd_array_map_delete_elem,
  1108. .map_fd_get_ptr = perf_event_fd_array_get_ptr,
  1109. .map_fd_put_ptr = perf_event_fd_array_put_ptr,
  1110. .map_release = perf_event_fd_array_release,
  1111. .map_check_btf = map_check_no_btf,
  1112. .map_mem_usage = array_map_mem_usage,
  1113. .map_btf_id = &array_map_btf_ids[0],
  1114. };
  1115. #ifdef CONFIG_CGROUPS
  1116. static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
  1117. struct file *map_file /* not used */,
  1118. int fd)
  1119. {
  1120. return cgroup_get_from_fd(fd);
  1121. }
  1122. static void cgroup_fd_array_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
  1123. {
  1124. /* cgroup_put free cgrp after a rcu grace period */
  1125. cgroup_put(ptr);
  1126. }
  1127. static void cgroup_fd_array_free(struct bpf_map *map)
  1128. {
  1129. bpf_fd_array_map_clear(map, false);
  1130. fd_array_map_free(map);
  1131. }
  1132. const struct bpf_map_ops cgroup_array_map_ops = {
  1133. .map_meta_equal = bpf_map_meta_equal,
  1134. .map_alloc_check = fd_array_map_alloc_check,
  1135. .map_alloc = array_map_alloc,
  1136. .map_free = cgroup_fd_array_free,
  1137. .map_get_next_key = array_map_get_next_key,
  1138. .map_lookup_elem = fd_array_map_lookup_elem,
  1139. .map_delete_elem = fd_array_map_delete_elem,
  1140. .map_fd_get_ptr = cgroup_fd_array_get_ptr,
  1141. .map_fd_put_ptr = cgroup_fd_array_put_ptr,
  1142. .map_check_btf = map_check_no_btf,
  1143. .map_mem_usage = array_map_mem_usage,
  1144. .map_btf_id = &array_map_btf_ids[0],
  1145. };
  1146. #endif
  1147. static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
  1148. {
  1149. struct bpf_map *map, *inner_map_meta;
  1150. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  1151. if (IS_ERR(inner_map_meta))
  1152. return inner_map_meta;
  1153. map = array_map_alloc(attr);
  1154. if (IS_ERR(map)) {
  1155. bpf_map_meta_free(inner_map_meta);
  1156. return map;
  1157. }
  1158. map->inner_map_meta = inner_map_meta;
  1159. return map;
  1160. }
  1161. static void array_of_map_free(struct bpf_map *map)
  1162. {
  1163. /* map->inner_map_meta is only accessed by syscall which
  1164. * is protected by fdget/fdput.
  1165. */
  1166. bpf_map_meta_free(map->inner_map_meta);
  1167. bpf_fd_array_map_clear(map, false);
  1168. fd_array_map_free(map);
  1169. }
  1170. static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
  1171. {
  1172. struct bpf_map **inner_map = array_map_lookup_elem(map, key);
  1173. if (!inner_map)
  1174. return NULL;
  1175. return READ_ONCE(*inner_map);
  1176. }
  1177. static int array_of_map_gen_lookup(struct bpf_map *map,
  1178. struct bpf_insn *insn_buf)
  1179. {
  1180. struct bpf_array *array = container_of(map, struct bpf_array, map);
  1181. u32 elem_size = array->elem_size;
  1182. struct bpf_insn *insn = insn_buf;
  1183. const int ret = BPF_REG_0;
  1184. const int map_ptr = BPF_REG_1;
  1185. const int index = BPF_REG_2;
  1186. *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
  1187. *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
  1188. if (!map->bypass_spec_v1) {
  1189. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
  1190. *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
  1191. } else {
  1192. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
  1193. }
  1194. if (is_power_of_2(elem_size))
  1195. *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
  1196. else
  1197. *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
  1198. *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
  1199. *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
  1200. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
  1201. *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
  1202. *insn++ = BPF_MOV64_IMM(ret, 0);
  1203. return insn - insn_buf;
  1204. }
  1205. const struct bpf_map_ops array_of_maps_map_ops = {
  1206. .map_alloc_check = fd_array_map_alloc_check,
  1207. .map_alloc = array_of_map_alloc,
  1208. .map_free = array_of_map_free,
  1209. .map_get_next_key = array_map_get_next_key,
  1210. .map_lookup_elem = array_of_map_lookup_elem,
  1211. .map_delete_elem = fd_array_map_delete_elem,
  1212. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  1213. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  1214. .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
  1215. .map_gen_lookup = array_of_map_gen_lookup,
  1216. .map_lookup_batch = generic_map_lookup_batch,
  1217. .map_update_batch = generic_map_update_batch,
  1218. .map_check_btf = map_check_no_btf,
  1219. .map_mem_usage = array_map_mem_usage,
  1220. .map_btf_id = &array_map_btf_ids[0],
  1221. };