hashtab.c 35 KB

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