hashtab.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016 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/jhash.h>
  15. #include <linux/filter.h>
  16. #include <linux/rculist_nulls.h>
  17. #include "percpu_freelist.h"
  18. #include "bpf_lru_list.h"
  19. #include "map_in_map.h"
  20. struct bucket {
  21. struct hlist_nulls_head head;
  22. raw_spinlock_t lock;
  23. };
  24. struct bpf_htab {
  25. struct bpf_map map;
  26. struct bucket *buckets;
  27. void *elems;
  28. union {
  29. struct pcpu_freelist freelist;
  30. struct bpf_lru lru;
  31. };
  32. struct htab_elem *__percpu *extra_elems;
  33. atomic_t count; /* number of elements in this hashtable */
  34. u32 n_buckets; /* number of hash buckets */
  35. u32 elem_size; /* size of each element in bytes */
  36. };
  37. /* each htab element is struct htab_elem + key + value */
  38. struct htab_elem {
  39. union {
  40. struct hlist_nulls_node hash_node;
  41. struct {
  42. void *padding;
  43. union {
  44. struct bpf_htab *htab;
  45. struct pcpu_freelist_node fnode;
  46. };
  47. };
  48. };
  49. union {
  50. struct rcu_head rcu;
  51. struct bpf_lru_node lru_node;
  52. };
  53. u32 hash;
  54. char key[0] __aligned(8);
  55. };
  56. static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
  57. static bool htab_is_lru(const struct bpf_htab *htab)
  58. {
  59. return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
  60. htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
  61. }
  62. static bool htab_is_percpu(const struct bpf_htab *htab)
  63. {
  64. return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  65. htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
  66. }
  67. static bool htab_is_prealloc(const struct bpf_htab *htab)
  68. {
  69. return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
  70. }
  71. static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
  72. void __percpu *pptr)
  73. {
  74. *(void __percpu **)(l->key + key_size) = pptr;
  75. }
  76. static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
  77. {
  78. return *(void __percpu **)(l->key + key_size);
  79. }
  80. static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
  81. {
  82. return *(void **)(l->key + roundup(map->key_size, 8));
  83. }
  84. static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
  85. {
  86. return (struct htab_elem *) (htab->elems + i * htab->elem_size);
  87. }
  88. static void htab_free_elems(struct bpf_htab *htab)
  89. {
  90. int i;
  91. if (!htab_is_percpu(htab))
  92. goto free_elems;
  93. for (i = 0; i < htab->map.max_entries; i++) {
  94. void __percpu *pptr;
  95. pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
  96. htab->map.key_size);
  97. free_percpu(pptr);
  98. }
  99. free_elems:
  100. bpf_map_area_free(htab->elems);
  101. }
  102. static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
  103. u32 hash)
  104. {
  105. struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
  106. struct htab_elem *l;
  107. if (node) {
  108. l = container_of(node, struct htab_elem, lru_node);
  109. memcpy(l->key, key, htab->map.key_size);
  110. return l;
  111. }
  112. return NULL;
  113. }
  114. static int prealloc_init(struct bpf_htab *htab)
  115. {
  116. u32 num_entries = htab->map.max_entries;
  117. int err = -ENOMEM, i;
  118. if (!htab_is_percpu(htab) && !htab_is_lru(htab))
  119. num_entries += num_possible_cpus();
  120. htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries);
  121. if (!htab->elems)
  122. return -ENOMEM;
  123. if (!htab_is_percpu(htab))
  124. goto skip_percpu_elems;
  125. for (i = 0; i < num_entries; i++) {
  126. u32 size = round_up(htab->map.value_size, 8);
  127. void __percpu *pptr;
  128. pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
  129. if (!pptr)
  130. goto free_elems;
  131. htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
  132. pptr);
  133. }
  134. skip_percpu_elems:
  135. if (htab_is_lru(htab))
  136. err = bpf_lru_init(&htab->lru,
  137. htab->map.map_flags & BPF_F_NO_COMMON_LRU,
  138. offsetof(struct htab_elem, hash) -
  139. offsetof(struct htab_elem, lru_node),
  140. htab_lru_map_delete_node,
  141. htab);
  142. else
  143. err = pcpu_freelist_init(&htab->freelist);
  144. if (err)
  145. goto free_elems;
  146. if (htab_is_lru(htab))
  147. bpf_lru_populate(&htab->lru, htab->elems,
  148. offsetof(struct htab_elem, lru_node),
  149. htab->elem_size, num_entries);
  150. else
  151. pcpu_freelist_populate(&htab->freelist,
  152. htab->elems + offsetof(struct htab_elem, fnode),
  153. htab->elem_size, num_entries);
  154. return 0;
  155. free_elems:
  156. htab_free_elems(htab);
  157. return err;
  158. }
  159. static void prealloc_destroy(struct bpf_htab *htab)
  160. {
  161. htab_free_elems(htab);
  162. if (htab_is_lru(htab))
  163. bpf_lru_destroy(&htab->lru);
  164. else
  165. pcpu_freelist_destroy(&htab->freelist);
  166. }
  167. static int alloc_extra_elems(struct bpf_htab *htab)
  168. {
  169. struct htab_elem *__percpu *pptr, *l_new;
  170. struct pcpu_freelist_node *l;
  171. int cpu;
  172. pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
  173. GFP_USER | __GFP_NOWARN);
  174. if (!pptr)
  175. return -ENOMEM;
  176. for_each_possible_cpu(cpu) {
  177. l = pcpu_freelist_pop(&htab->freelist);
  178. /* pop will succeed, since prealloc_init()
  179. * preallocated extra num_possible_cpus elements
  180. */
  181. l_new = container_of(l, struct htab_elem, fnode);
  182. *per_cpu_ptr(pptr, cpu) = l_new;
  183. }
  184. htab->extra_elems = pptr;
  185. return 0;
  186. }
  187. /* Called from syscall */
  188. static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
  189. {
  190. bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  191. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  192. bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
  193. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  194. /* percpu_lru means each cpu has its own LRU list.
  195. * it is different from BPF_MAP_TYPE_PERCPU_HASH where
  196. * the map's value itself is percpu. percpu_lru has
  197. * nothing to do with the map's value.
  198. */
  199. bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
  200. bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
  201. struct bpf_htab *htab;
  202. int err, i;
  203. u64 cost;
  204. BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
  205. offsetof(struct htab_elem, hash_node.pprev));
  206. BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
  207. offsetof(struct htab_elem, hash_node.pprev));
  208. if (lru && !capable(CAP_SYS_ADMIN))
  209. /* LRU implementation is much complicated than other
  210. * maps. Hence, limit to CAP_SYS_ADMIN for now.
  211. */
  212. return ERR_PTR(-EPERM);
  213. if (attr->map_flags & ~(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU))
  214. /* reserved bits should not be used */
  215. return ERR_PTR(-EINVAL);
  216. if (!lru && percpu_lru)
  217. return ERR_PTR(-EINVAL);
  218. if (lru && !prealloc)
  219. return ERR_PTR(-ENOTSUPP);
  220. htab = kzalloc(sizeof(*htab), GFP_USER);
  221. if (!htab)
  222. return ERR_PTR(-ENOMEM);
  223. /* mandatory map attributes */
  224. htab->map.map_type = attr->map_type;
  225. htab->map.key_size = attr->key_size;
  226. htab->map.value_size = attr->value_size;
  227. htab->map.max_entries = attr->max_entries;
  228. htab->map.map_flags = attr->map_flags;
  229. /* check sanity of attributes.
  230. * value_size == 0 may be allowed in the future to use map as a set
  231. */
  232. err = -EINVAL;
  233. if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
  234. htab->map.value_size == 0)
  235. goto free_htab;
  236. if (percpu_lru) {
  237. /* ensure each CPU's lru list has >=1 elements.
  238. * since we are at it, make each lru list has the same
  239. * number of elements.
  240. */
  241. htab->map.max_entries = roundup(attr->max_entries,
  242. num_possible_cpus());
  243. if (htab->map.max_entries < attr->max_entries)
  244. htab->map.max_entries = rounddown(attr->max_entries,
  245. num_possible_cpus());
  246. }
  247. /* hash table size must be power of 2 */
  248. htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
  249. err = -E2BIG;
  250. if (htab->map.key_size > MAX_BPF_STACK)
  251. /* eBPF programs initialize keys on stack, so they cannot be
  252. * larger than max stack size
  253. */
  254. goto free_htab;
  255. if (htab->map.value_size >= KMALLOC_MAX_SIZE -
  256. MAX_BPF_STACK - sizeof(struct htab_elem))
  257. /* if value_size is bigger, the user space won't be able to
  258. * access the elements via bpf syscall. This check also makes
  259. * sure that the elem_size doesn't overflow and it's
  260. * kmalloc-able later in htab_map_update_elem()
  261. */
  262. goto free_htab;
  263. if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
  264. /* make sure the size for pcpu_alloc() is reasonable */
  265. goto free_htab;
  266. htab->elem_size = sizeof(struct htab_elem) +
  267. round_up(htab->map.key_size, 8);
  268. if (percpu)
  269. htab->elem_size += sizeof(void *);
  270. else
  271. htab->elem_size += round_up(htab->map.value_size, 8);
  272. /* prevent zero size kmalloc and check for u32 overflow */
  273. if (htab->n_buckets == 0 ||
  274. htab->n_buckets > U32_MAX / sizeof(struct bucket))
  275. goto free_htab;
  276. cost = (u64) htab->n_buckets * sizeof(struct bucket) +
  277. (u64) htab->elem_size * htab->map.max_entries;
  278. if (percpu)
  279. cost += (u64) round_up(htab->map.value_size, 8) *
  280. num_possible_cpus() * htab->map.max_entries;
  281. else
  282. cost += (u64) htab->elem_size * num_possible_cpus();
  283. if (cost >= U32_MAX - PAGE_SIZE)
  284. /* make sure page count doesn't overflow */
  285. goto free_htab;
  286. htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  287. /* if map size is larger than memlock limit, reject it early */
  288. err = bpf_map_precharge_memlock(htab->map.pages);
  289. if (err)
  290. goto free_htab;
  291. err = -ENOMEM;
  292. htab->buckets = bpf_map_area_alloc(htab->n_buckets *
  293. sizeof(struct bucket));
  294. if (!htab->buckets)
  295. goto free_htab;
  296. for (i = 0; i < htab->n_buckets; i++) {
  297. INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
  298. raw_spin_lock_init(&htab->buckets[i].lock);
  299. }
  300. if (prealloc) {
  301. err = prealloc_init(htab);
  302. if (err)
  303. goto free_buckets;
  304. if (!percpu && !lru) {
  305. /* lru itself can remove the least used element, so
  306. * there is no need for an extra elem during map_update.
  307. */
  308. err = alloc_extra_elems(htab);
  309. if (err)
  310. goto free_prealloc;
  311. }
  312. }
  313. return &htab->map;
  314. free_prealloc:
  315. prealloc_destroy(htab);
  316. free_buckets:
  317. bpf_map_area_free(htab->buckets);
  318. free_htab:
  319. kfree(htab);
  320. return ERR_PTR(err);
  321. }
  322. static inline u32 htab_map_hash(const void *key, u32 key_len)
  323. {
  324. return jhash(key, key_len, 0);
  325. }
  326. static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
  327. {
  328. return &htab->buckets[hash & (htab->n_buckets - 1)];
  329. }
  330. static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
  331. {
  332. return &__select_bucket(htab, hash)->head;
  333. }
  334. /* this lookup function can only be called with bucket lock taken */
  335. static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
  336. void *key, u32 key_size)
  337. {
  338. struct hlist_nulls_node *n;
  339. struct htab_elem *l;
  340. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  341. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  342. return l;
  343. return NULL;
  344. }
  345. /* can be called without bucket lock. it will repeat the loop in
  346. * the unlikely event when elements moved from one bucket into another
  347. * while link list is being walked
  348. */
  349. static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
  350. u32 hash, void *key,
  351. u32 key_size, u32 n_buckets)
  352. {
  353. struct hlist_nulls_node *n;
  354. struct htab_elem *l;
  355. again:
  356. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  357. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  358. return l;
  359. if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
  360. goto again;
  361. return NULL;
  362. }
  363. /* Called from syscall or from eBPF program directly, so
  364. * arguments have to match bpf_map_lookup_elem() exactly.
  365. * The return value is adjusted by BPF instructions
  366. * in htab_map_gen_lookup().
  367. */
  368. static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
  369. {
  370. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  371. struct hlist_nulls_head *head;
  372. struct htab_elem *l;
  373. u32 hash, key_size;
  374. /* Must be called with rcu_read_lock. */
  375. WARN_ON_ONCE(!rcu_read_lock_held());
  376. key_size = map->key_size;
  377. hash = htab_map_hash(key, key_size);
  378. head = select_bucket(htab, hash);
  379. l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
  380. return l;
  381. }
  382. static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
  383. {
  384. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  385. if (l)
  386. return l->key + round_up(map->key_size, 8);
  387. return NULL;
  388. }
  389. /* inline bpf_map_lookup_elem() call.
  390. * Instead of:
  391. * bpf_prog
  392. * bpf_map_lookup_elem
  393. * map->ops->map_lookup_elem
  394. * htab_map_lookup_elem
  395. * __htab_map_lookup_elem
  396. * do:
  397. * bpf_prog
  398. * __htab_map_lookup_elem
  399. */
  400. static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
  401. {
  402. struct bpf_insn *insn = insn_buf;
  403. const int ret = BPF_REG_0;
  404. *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem);
  405. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
  406. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  407. offsetof(struct htab_elem, key) +
  408. round_up(map->key_size, 8));
  409. return insn - insn_buf;
  410. }
  411. static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
  412. {
  413. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  414. if (l) {
  415. bpf_lru_node_set_ref(&l->lru_node);
  416. return l->key + round_up(map->key_size, 8);
  417. }
  418. return NULL;
  419. }
  420. /* It is called from the bpf_lru_list when the LRU needs to delete
  421. * older elements from the htab.
  422. */
  423. static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
  424. {
  425. struct bpf_htab *htab = (struct bpf_htab *)arg;
  426. struct htab_elem *l = NULL, *tgt_l;
  427. struct hlist_nulls_head *head;
  428. struct hlist_nulls_node *n;
  429. unsigned long flags;
  430. struct bucket *b;
  431. tgt_l = container_of(node, struct htab_elem, lru_node);
  432. b = __select_bucket(htab, tgt_l->hash);
  433. head = &b->head;
  434. raw_spin_lock_irqsave(&b->lock, flags);
  435. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  436. if (l == tgt_l) {
  437. hlist_nulls_del_rcu(&l->hash_node);
  438. break;
  439. }
  440. raw_spin_unlock_irqrestore(&b->lock, flags);
  441. return l == tgt_l;
  442. }
  443. /* Called from syscall */
  444. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  445. {
  446. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  447. struct hlist_nulls_head *head;
  448. struct htab_elem *l, *next_l;
  449. u32 hash, key_size;
  450. int i = 0;
  451. WARN_ON_ONCE(!rcu_read_lock_held());
  452. key_size = map->key_size;
  453. if (!key)
  454. goto find_first_elem;
  455. hash = htab_map_hash(key, key_size);
  456. head = select_bucket(htab, hash);
  457. /* lookup the key */
  458. l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
  459. if (!l)
  460. goto find_first_elem;
  461. /* key was found, get next key in the same bucket */
  462. next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
  463. struct htab_elem, hash_node);
  464. if (next_l) {
  465. /* if next elem in this hash list is non-zero, just return it */
  466. memcpy(next_key, next_l->key, key_size);
  467. return 0;
  468. }
  469. /* no more elements in this hash list, go to the next bucket */
  470. i = hash & (htab->n_buckets - 1);
  471. i++;
  472. find_first_elem:
  473. /* iterate over buckets */
  474. for (; i < htab->n_buckets; i++) {
  475. head = select_bucket(htab, i);
  476. /* pick first element in the bucket */
  477. next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
  478. struct htab_elem, hash_node);
  479. if (next_l) {
  480. /* if it's not empty, just return it */
  481. memcpy(next_key, next_l->key, key_size);
  482. return 0;
  483. }
  484. }
  485. /* iterated over all buckets and all elements */
  486. return -ENOENT;
  487. }
  488. static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
  489. {
  490. if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
  491. free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
  492. kfree(l);
  493. }
  494. static void htab_elem_free_rcu(struct rcu_head *head)
  495. {
  496. struct htab_elem *l = container_of(head, struct htab_elem, rcu);
  497. struct bpf_htab *htab = l->htab;
  498. /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
  499. * we're calling kfree, otherwise deadlock is possible if kprobes
  500. * are placed somewhere inside of slub
  501. */
  502. preempt_disable();
  503. __this_cpu_inc(bpf_prog_active);
  504. htab_elem_free(htab, l);
  505. __this_cpu_dec(bpf_prog_active);
  506. preempt_enable();
  507. }
  508. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  509. {
  510. struct bpf_map *map = &htab->map;
  511. if (map->ops->map_fd_put_ptr) {
  512. void *ptr = fd_htab_map_get_ptr(map, l);
  513. map->ops->map_fd_put_ptr(ptr);
  514. }
  515. if (htab_is_prealloc(htab)) {
  516. pcpu_freelist_push(&htab->freelist, &l->fnode);
  517. } else {
  518. atomic_dec(&htab->count);
  519. l->htab = htab;
  520. call_rcu(&l->rcu, htab_elem_free_rcu);
  521. }
  522. }
  523. static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
  524. void *value, bool onallcpus)
  525. {
  526. if (!onallcpus) {
  527. /* copy true value_size bytes */
  528. memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
  529. } else {
  530. u32 size = round_up(htab->map.value_size, 8);
  531. int off = 0, cpu;
  532. for_each_possible_cpu(cpu) {
  533. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  534. value + off, size);
  535. off += size;
  536. }
  537. }
  538. }
  539. static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
  540. void *value, u32 key_size, u32 hash,
  541. bool percpu, bool onallcpus,
  542. struct htab_elem *old_elem)
  543. {
  544. u32 size = htab->map.value_size;
  545. bool prealloc = htab_is_prealloc(htab);
  546. struct htab_elem *l_new, **pl_new;
  547. void __percpu *pptr;
  548. if (prealloc) {
  549. if (old_elem) {
  550. /* if we're updating the existing element,
  551. * use per-cpu extra elems to avoid freelist_pop/push
  552. */
  553. pl_new = this_cpu_ptr(htab->extra_elems);
  554. l_new = *pl_new;
  555. *pl_new = old_elem;
  556. } else {
  557. struct pcpu_freelist_node *l;
  558. l = pcpu_freelist_pop(&htab->freelist);
  559. if (!l)
  560. return ERR_PTR(-E2BIG);
  561. l_new = container_of(l, struct htab_elem, fnode);
  562. }
  563. } else {
  564. if (atomic_inc_return(&htab->count) > htab->map.max_entries)
  565. if (!old_elem) {
  566. /* when map is full and update() is replacing
  567. * old element, it's ok to allocate, since
  568. * old element will be freed immediately.
  569. * Otherwise return an error
  570. */
  571. atomic_dec(&htab->count);
  572. return ERR_PTR(-E2BIG);
  573. }
  574. l_new = kmalloc(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN);
  575. if (!l_new)
  576. return ERR_PTR(-ENOMEM);
  577. }
  578. memcpy(l_new->key, key, key_size);
  579. if (percpu) {
  580. /* round up value_size to 8 bytes */
  581. size = round_up(size, 8);
  582. if (prealloc) {
  583. pptr = htab_elem_get_ptr(l_new, key_size);
  584. } else {
  585. /* alloc_percpu zero-fills */
  586. pptr = __alloc_percpu_gfp(size, 8,
  587. GFP_ATOMIC | __GFP_NOWARN);
  588. if (!pptr) {
  589. kfree(l_new);
  590. return ERR_PTR(-ENOMEM);
  591. }
  592. }
  593. pcpu_copy_value(htab, pptr, value, onallcpus);
  594. if (!prealloc)
  595. htab_elem_set_ptr(l_new, key_size, pptr);
  596. } else {
  597. memcpy(l_new->key + round_up(key_size, 8), value, size);
  598. }
  599. l_new->hash = hash;
  600. return l_new;
  601. }
  602. static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  603. u64 map_flags)
  604. {
  605. if (l_old && map_flags == BPF_NOEXIST)
  606. /* elem already exists */
  607. return -EEXIST;
  608. if (!l_old && map_flags == BPF_EXIST)
  609. /* elem doesn't exist, cannot update it */
  610. return -ENOENT;
  611. return 0;
  612. }
  613. /* Called from syscall or from eBPF program */
  614. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  615. u64 map_flags)
  616. {
  617. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  618. struct htab_elem *l_new = NULL, *l_old;
  619. struct hlist_nulls_head *head;
  620. unsigned long flags;
  621. struct bucket *b;
  622. u32 key_size, hash;
  623. int ret;
  624. if (unlikely(map_flags > BPF_EXIST))
  625. /* unknown flags */
  626. return -EINVAL;
  627. WARN_ON_ONCE(!rcu_read_lock_held());
  628. key_size = map->key_size;
  629. hash = htab_map_hash(key, key_size);
  630. b = __select_bucket(htab, hash);
  631. head = &b->head;
  632. /* bpf_map_update_elem() can be called in_irq() */
  633. raw_spin_lock_irqsave(&b->lock, flags);
  634. l_old = lookup_elem_raw(head, hash, key, key_size);
  635. ret = check_flags(htab, l_old, map_flags);
  636. if (ret)
  637. goto err;
  638. l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
  639. l_old);
  640. if (IS_ERR(l_new)) {
  641. /* all pre-allocated elements are in use or memory exhausted */
  642. ret = PTR_ERR(l_new);
  643. goto err;
  644. }
  645. /* add new element to the head of the list, so that
  646. * concurrent search will find it before old elem
  647. */
  648. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  649. if (l_old) {
  650. hlist_nulls_del_rcu(&l_old->hash_node);
  651. if (!htab_is_prealloc(htab))
  652. free_htab_elem(htab, l_old);
  653. }
  654. ret = 0;
  655. err:
  656. raw_spin_unlock_irqrestore(&b->lock, flags);
  657. return ret;
  658. }
  659. static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
  660. u64 map_flags)
  661. {
  662. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  663. struct htab_elem *l_new, *l_old = NULL;
  664. struct hlist_nulls_head *head;
  665. unsigned long flags;
  666. struct bucket *b;
  667. u32 key_size, hash;
  668. int ret;
  669. if (unlikely(map_flags > BPF_EXIST))
  670. /* unknown flags */
  671. return -EINVAL;
  672. WARN_ON_ONCE(!rcu_read_lock_held());
  673. key_size = map->key_size;
  674. hash = htab_map_hash(key, key_size);
  675. b = __select_bucket(htab, hash);
  676. head = &b->head;
  677. /* For LRU, we need to alloc before taking bucket's
  678. * spinlock because getting free nodes from LRU may need
  679. * to remove older elements from htab and this removal
  680. * operation will need a bucket lock.
  681. */
  682. l_new = prealloc_lru_pop(htab, key, hash);
  683. if (!l_new)
  684. return -ENOMEM;
  685. memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
  686. /* bpf_map_update_elem() can be called in_irq() */
  687. raw_spin_lock_irqsave(&b->lock, flags);
  688. l_old = lookup_elem_raw(head, hash, key, key_size);
  689. ret = check_flags(htab, l_old, map_flags);
  690. if (ret)
  691. goto err;
  692. /* add new element to the head of the list, so that
  693. * concurrent search will find it before old elem
  694. */
  695. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  696. if (l_old) {
  697. bpf_lru_node_set_ref(&l_new->lru_node);
  698. hlist_nulls_del_rcu(&l_old->hash_node);
  699. }
  700. ret = 0;
  701. err:
  702. raw_spin_unlock_irqrestore(&b->lock, flags);
  703. if (ret)
  704. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  705. else if (l_old)
  706. bpf_lru_push_free(&htab->lru, &l_old->lru_node);
  707. return ret;
  708. }
  709. static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  710. void *value, u64 map_flags,
  711. bool onallcpus)
  712. {
  713. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  714. struct htab_elem *l_new = NULL, *l_old;
  715. struct hlist_nulls_head *head;
  716. unsigned long flags;
  717. struct bucket *b;
  718. u32 key_size, hash;
  719. int ret;
  720. if (unlikely(map_flags > BPF_EXIST))
  721. /* unknown flags */
  722. return -EINVAL;
  723. WARN_ON_ONCE(!rcu_read_lock_held());
  724. key_size = map->key_size;
  725. hash = htab_map_hash(key, key_size);
  726. b = __select_bucket(htab, hash);
  727. head = &b->head;
  728. /* bpf_map_update_elem() can be called in_irq() */
  729. raw_spin_lock_irqsave(&b->lock, flags);
  730. l_old = lookup_elem_raw(head, hash, key, key_size);
  731. ret = check_flags(htab, l_old, map_flags);
  732. if (ret)
  733. goto err;
  734. if (l_old) {
  735. /* per-cpu hash map can update value in-place */
  736. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  737. value, onallcpus);
  738. } else {
  739. l_new = alloc_htab_elem(htab, key, value, key_size,
  740. hash, true, onallcpus, NULL);
  741. if (IS_ERR(l_new)) {
  742. ret = PTR_ERR(l_new);
  743. goto err;
  744. }
  745. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  746. }
  747. ret = 0;
  748. err:
  749. raw_spin_unlock_irqrestore(&b->lock, flags);
  750. return ret;
  751. }
  752. static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  753. void *value, u64 map_flags,
  754. bool onallcpus)
  755. {
  756. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  757. struct htab_elem *l_new = NULL, *l_old;
  758. struct hlist_nulls_head *head;
  759. unsigned long flags;
  760. struct bucket *b;
  761. u32 key_size, hash;
  762. int ret;
  763. if (unlikely(map_flags > BPF_EXIST))
  764. /* unknown flags */
  765. return -EINVAL;
  766. WARN_ON_ONCE(!rcu_read_lock_held());
  767. key_size = map->key_size;
  768. hash = htab_map_hash(key, key_size);
  769. b = __select_bucket(htab, hash);
  770. head = &b->head;
  771. /* For LRU, we need to alloc before taking bucket's
  772. * spinlock because LRU's elem alloc may need
  773. * to remove older elem from htab and this removal
  774. * operation will need a bucket lock.
  775. */
  776. if (map_flags != BPF_EXIST) {
  777. l_new = prealloc_lru_pop(htab, key, hash);
  778. if (!l_new)
  779. return -ENOMEM;
  780. }
  781. /* bpf_map_update_elem() can be called in_irq() */
  782. raw_spin_lock_irqsave(&b->lock, flags);
  783. l_old = lookup_elem_raw(head, hash, key, key_size);
  784. ret = check_flags(htab, l_old, map_flags);
  785. if (ret)
  786. goto err;
  787. if (l_old) {
  788. bpf_lru_node_set_ref(&l_old->lru_node);
  789. /* per-cpu hash map can update value in-place */
  790. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  791. value, onallcpus);
  792. } else {
  793. pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
  794. value, onallcpus);
  795. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  796. l_new = NULL;
  797. }
  798. ret = 0;
  799. err:
  800. raw_spin_unlock_irqrestore(&b->lock, flags);
  801. if (l_new)
  802. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  803. return ret;
  804. }
  805. static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  806. void *value, u64 map_flags)
  807. {
  808. return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
  809. }
  810. static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  811. void *value, u64 map_flags)
  812. {
  813. return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
  814. false);
  815. }
  816. /* Called from syscall or from eBPF program */
  817. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  818. {
  819. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  820. struct hlist_nulls_head *head;
  821. struct bucket *b;
  822. struct htab_elem *l;
  823. unsigned long flags;
  824. u32 hash, key_size;
  825. int ret = -ENOENT;
  826. WARN_ON_ONCE(!rcu_read_lock_held());
  827. key_size = map->key_size;
  828. hash = htab_map_hash(key, key_size);
  829. b = __select_bucket(htab, hash);
  830. head = &b->head;
  831. raw_spin_lock_irqsave(&b->lock, flags);
  832. l = lookup_elem_raw(head, hash, key, key_size);
  833. if (l) {
  834. hlist_nulls_del_rcu(&l->hash_node);
  835. free_htab_elem(htab, l);
  836. ret = 0;
  837. }
  838. raw_spin_unlock_irqrestore(&b->lock, flags);
  839. return ret;
  840. }
  841. static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
  842. {
  843. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  844. struct hlist_nulls_head *head;
  845. struct bucket *b;
  846. struct htab_elem *l;
  847. unsigned long flags;
  848. u32 hash, key_size;
  849. int ret = -ENOENT;
  850. WARN_ON_ONCE(!rcu_read_lock_held());
  851. key_size = map->key_size;
  852. hash = htab_map_hash(key, key_size);
  853. b = __select_bucket(htab, hash);
  854. head = &b->head;
  855. raw_spin_lock_irqsave(&b->lock, flags);
  856. l = lookup_elem_raw(head, hash, key, key_size);
  857. if (l) {
  858. hlist_nulls_del_rcu(&l->hash_node);
  859. ret = 0;
  860. }
  861. raw_spin_unlock_irqrestore(&b->lock, flags);
  862. if (l)
  863. bpf_lru_push_free(&htab->lru, &l->lru_node);
  864. return ret;
  865. }
  866. static void delete_all_elements(struct bpf_htab *htab)
  867. {
  868. int i;
  869. for (i = 0; i < htab->n_buckets; i++) {
  870. struct hlist_nulls_head *head = select_bucket(htab, i);
  871. struct hlist_nulls_node *n;
  872. struct htab_elem *l;
  873. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  874. hlist_nulls_del_rcu(&l->hash_node);
  875. htab_elem_free(htab, l);
  876. }
  877. }
  878. }
  879. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  880. static void htab_map_free(struct bpf_map *map)
  881. {
  882. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  883. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  884. * so the programs (can be more than one that used this map) were
  885. * disconnected from events. Wait for outstanding critical sections in
  886. * these programs to complete
  887. */
  888. synchronize_rcu();
  889. /* some of free_htab_elem() callbacks for elements of this map may
  890. * not have executed. Wait for them.
  891. */
  892. rcu_barrier();
  893. if (!htab_is_prealloc(htab))
  894. delete_all_elements(htab);
  895. else
  896. prealloc_destroy(htab);
  897. free_percpu(htab->extra_elems);
  898. bpf_map_area_free(htab->buckets);
  899. kfree(htab);
  900. }
  901. const struct bpf_map_ops htab_map_ops = {
  902. .map_alloc = htab_map_alloc,
  903. .map_free = htab_map_free,
  904. .map_get_next_key = htab_map_get_next_key,
  905. .map_lookup_elem = htab_map_lookup_elem,
  906. .map_update_elem = htab_map_update_elem,
  907. .map_delete_elem = htab_map_delete_elem,
  908. .map_gen_lookup = htab_map_gen_lookup,
  909. };
  910. const struct bpf_map_ops htab_lru_map_ops = {
  911. .map_alloc = htab_map_alloc,
  912. .map_free = htab_map_free,
  913. .map_get_next_key = htab_map_get_next_key,
  914. .map_lookup_elem = htab_lru_map_lookup_elem,
  915. .map_update_elem = htab_lru_map_update_elem,
  916. .map_delete_elem = htab_lru_map_delete_elem,
  917. };
  918. /* Called from eBPF program */
  919. static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  920. {
  921. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  922. if (l)
  923. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  924. else
  925. return NULL;
  926. }
  927. static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  928. {
  929. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  930. if (l) {
  931. bpf_lru_node_set_ref(&l->lru_node);
  932. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  933. }
  934. return NULL;
  935. }
  936. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
  937. {
  938. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  939. struct htab_elem *l;
  940. void __percpu *pptr;
  941. int ret = -ENOENT;
  942. int cpu, off = 0;
  943. u32 size;
  944. /* per_cpu areas are zero-filled and bpf programs can only
  945. * access 'value_size' of them, so copying rounded areas
  946. * will not leak any kernel data
  947. */
  948. size = round_up(map->value_size, 8);
  949. rcu_read_lock();
  950. l = __htab_map_lookup_elem(map, key);
  951. if (!l)
  952. goto out;
  953. if (htab_is_lru(htab))
  954. bpf_lru_node_set_ref(&l->lru_node);
  955. pptr = htab_elem_get_ptr(l, map->key_size);
  956. for_each_possible_cpu(cpu) {
  957. bpf_long_memcpy(value + off,
  958. per_cpu_ptr(pptr, cpu), size);
  959. off += size;
  960. }
  961. ret = 0;
  962. out:
  963. rcu_read_unlock();
  964. return ret;
  965. }
  966. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  967. u64 map_flags)
  968. {
  969. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  970. int ret;
  971. rcu_read_lock();
  972. if (htab_is_lru(htab))
  973. ret = __htab_lru_percpu_map_update_elem(map, key, value,
  974. map_flags, true);
  975. else
  976. ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
  977. true);
  978. rcu_read_unlock();
  979. return ret;
  980. }
  981. const struct bpf_map_ops htab_percpu_map_ops = {
  982. .map_alloc = htab_map_alloc,
  983. .map_free = htab_map_free,
  984. .map_get_next_key = htab_map_get_next_key,
  985. .map_lookup_elem = htab_percpu_map_lookup_elem,
  986. .map_update_elem = htab_percpu_map_update_elem,
  987. .map_delete_elem = htab_map_delete_elem,
  988. };
  989. const struct bpf_map_ops htab_lru_percpu_map_ops = {
  990. .map_alloc = htab_map_alloc,
  991. .map_free = htab_map_free,
  992. .map_get_next_key = htab_map_get_next_key,
  993. .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
  994. .map_update_elem = htab_lru_percpu_map_update_elem,
  995. .map_delete_elem = htab_lru_map_delete_elem,
  996. };
  997. static struct bpf_map *fd_htab_map_alloc(union bpf_attr *attr)
  998. {
  999. struct bpf_map *map;
  1000. if (attr->value_size != sizeof(u32))
  1001. return ERR_PTR(-EINVAL);
  1002. /* pointer is stored internally */
  1003. attr->value_size = sizeof(void *);
  1004. map = htab_map_alloc(attr);
  1005. attr->value_size = sizeof(u32);
  1006. return map;
  1007. }
  1008. static void fd_htab_map_free(struct bpf_map *map)
  1009. {
  1010. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1011. struct hlist_nulls_node *n;
  1012. struct hlist_nulls_head *head;
  1013. struct htab_elem *l;
  1014. int i;
  1015. for (i = 0; i < htab->n_buckets; i++) {
  1016. head = select_bucket(htab, i);
  1017. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  1018. void *ptr = fd_htab_map_get_ptr(map, l);
  1019. map->ops->map_fd_put_ptr(ptr);
  1020. }
  1021. }
  1022. htab_map_free(map);
  1023. }
  1024. /* only called from syscall */
  1025. int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
  1026. void *key, void *value, u64 map_flags)
  1027. {
  1028. void *ptr;
  1029. int ret;
  1030. u32 ufd = *(u32 *)value;
  1031. ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  1032. if (IS_ERR(ptr))
  1033. return PTR_ERR(ptr);
  1034. ret = htab_map_update_elem(map, key, &ptr, map_flags);
  1035. if (ret)
  1036. map->ops->map_fd_put_ptr(ptr);
  1037. return ret;
  1038. }
  1039. static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
  1040. {
  1041. struct bpf_map *map, *inner_map_meta;
  1042. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  1043. if (IS_ERR(inner_map_meta))
  1044. return inner_map_meta;
  1045. map = fd_htab_map_alloc(attr);
  1046. if (IS_ERR(map)) {
  1047. bpf_map_meta_free(inner_map_meta);
  1048. return map;
  1049. }
  1050. map->inner_map_meta = inner_map_meta;
  1051. return map;
  1052. }
  1053. static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
  1054. {
  1055. struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
  1056. if (!inner_map)
  1057. return NULL;
  1058. return READ_ONCE(*inner_map);
  1059. }
  1060. static void htab_of_map_free(struct bpf_map *map)
  1061. {
  1062. bpf_map_meta_free(map->inner_map_meta);
  1063. fd_htab_map_free(map);
  1064. }
  1065. const struct bpf_map_ops htab_of_maps_map_ops = {
  1066. .map_alloc = htab_of_map_alloc,
  1067. .map_free = htab_of_map_free,
  1068. .map_get_next_key = htab_map_get_next_key,
  1069. .map_lookup_elem = htab_of_map_lookup_elem,
  1070. .map_delete_elem = htab_map_delete_elem,
  1071. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  1072. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  1073. };