hashtab.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  421. (void *(*)(struct bpf_map *map, void *key))NULL));
  422. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  423. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
  424. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  425. offsetof(struct htab_elem, key) +
  426. round_up(map->key_size, 8));
  427. return insn - insn_buf;
  428. }
  429. static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
  430. {
  431. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  432. if (l) {
  433. bpf_lru_node_set_ref(&l->lru_node);
  434. return l->key + round_up(map->key_size, 8);
  435. }
  436. return NULL;
  437. }
  438. static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
  439. struct bpf_insn *insn_buf)
  440. {
  441. struct bpf_insn *insn = insn_buf;
  442. const int ret = BPF_REG_0;
  443. const int ref_reg = BPF_REG_1;
  444. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  445. (void *(*)(struct bpf_map *map, void *key))NULL));
  446. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  447. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
  448. *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
  449. offsetof(struct htab_elem, lru_node) +
  450. offsetof(struct bpf_lru_node, ref));
  451. *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
  452. *insn++ = BPF_ST_MEM(BPF_B, ret,
  453. offsetof(struct htab_elem, lru_node) +
  454. offsetof(struct bpf_lru_node, ref),
  455. 1);
  456. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  457. offsetof(struct htab_elem, key) +
  458. round_up(map->key_size, 8));
  459. return insn - insn_buf;
  460. }
  461. /* It is called from the bpf_lru_list when the LRU needs to delete
  462. * older elements from the htab.
  463. */
  464. static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
  465. {
  466. struct bpf_htab *htab = (struct bpf_htab *)arg;
  467. struct htab_elem *l = NULL, *tgt_l;
  468. struct hlist_nulls_head *head;
  469. struct hlist_nulls_node *n;
  470. unsigned long flags;
  471. struct bucket *b;
  472. tgt_l = container_of(node, struct htab_elem, lru_node);
  473. b = __select_bucket(htab, tgt_l->hash);
  474. head = &b->head;
  475. raw_spin_lock_irqsave(&b->lock, flags);
  476. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  477. if (l == tgt_l) {
  478. hlist_nulls_del_rcu(&l->hash_node);
  479. break;
  480. }
  481. raw_spin_unlock_irqrestore(&b->lock, flags);
  482. return l == tgt_l;
  483. }
  484. /* Called from syscall */
  485. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  486. {
  487. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  488. struct hlist_nulls_head *head;
  489. struct htab_elem *l, *next_l;
  490. u32 hash, key_size;
  491. int i = 0;
  492. WARN_ON_ONCE(!rcu_read_lock_held());
  493. key_size = map->key_size;
  494. if (!key)
  495. goto find_first_elem;
  496. hash = htab_map_hash(key, key_size);
  497. head = select_bucket(htab, hash);
  498. /* lookup the key */
  499. l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
  500. if (!l)
  501. goto find_first_elem;
  502. /* key was found, get next key in the same bucket */
  503. next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
  504. struct htab_elem, hash_node);
  505. if (next_l) {
  506. /* if next elem in this hash list is non-zero, just return it */
  507. memcpy(next_key, next_l->key, key_size);
  508. return 0;
  509. }
  510. /* no more elements in this hash list, go to the next bucket */
  511. i = hash & (htab->n_buckets - 1);
  512. i++;
  513. find_first_elem:
  514. /* iterate over buckets */
  515. for (; i < htab->n_buckets; i++) {
  516. head = select_bucket(htab, i);
  517. /* pick first element in the bucket */
  518. next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
  519. struct htab_elem, hash_node);
  520. if (next_l) {
  521. /* if it's not empty, just return it */
  522. memcpy(next_key, next_l->key, key_size);
  523. return 0;
  524. }
  525. }
  526. /* iterated over all buckets and all elements */
  527. return -ENOENT;
  528. }
  529. static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
  530. {
  531. if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
  532. free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
  533. kfree(l);
  534. }
  535. static void htab_elem_free_rcu(struct rcu_head *head)
  536. {
  537. struct htab_elem *l = container_of(head, struct htab_elem, rcu);
  538. struct bpf_htab *htab = l->htab;
  539. /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
  540. * we're calling kfree, otherwise deadlock is possible if kprobes
  541. * are placed somewhere inside of slub
  542. */
  543. preempt_disable();
  544. __this_cpu_inc(bpf_prog_active);
  545. htab_elem_free(htab, l);
  546. __this_cpu_dec(bpf_prog_active);
  547. preempt_enable();
  548. }
  549. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  550. {
  551. struct bpf_map *map = &htab->map;
  552. if (map->ops->map_fd_put_ptr) {
  553. void *ptr = fd_htab_map_get_ptr(map, l);
  554. map->ops->map_fd_put_ptr(ptr);
  555. }
  556. if (htab_is_prealloc(htab)) {
  557. pcpu_freelist_push(&htab->freelist, &l->fnode);
  558. } else {
  559. atomic_dec(&htab->count);
  560. l->htab = htab;
  561. call_rcu(&l->rcu, htab_elem_free_rcu);
  562. }
  563. }
  564. static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
  565. void *value, bool onallcpus)
  566. {
  567. if (!onallcpus) {
  568. /* copy true value_size bytes */
  569. memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
  570. } else {
  571. u32 size = round_up(htab->map.value_size, 8);
  572. int off = 0, cpu;
  573. for_each_possible_cpu(cpu) {
  574. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  575. value + off, size);
  576. off += size;
  577. }
  578. }
  579. }
  580. static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
  581. {
  582. return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
  583. BITS_PER_LONG == 64;
  584. }
  585. static u32 htab_size_value(const struct bpf_htab *htab, bool percpu)
  586. {
  587. u32 size = htab->map.value_size;
  588. if (percpu || fd_htab_map_needs_adjust(htab))
  589. size = round_up(size, 8);
  590. return size;
  591. }
  592. static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
  593. void *value, u32 key_size, u32 hash,
  594. bool percpu, bool onallcpus,
  595. struct htab_elem *old_elem)
  596. {
  597. u32 size = htab_size_value(htab, percpu);
  598. bool prealloc = htab_is_prealloc(htab);
  599. struct htab_elem *l_new, **pl_new;
  600. void __percpu *pptr;
  601. if (prealloc) {
  602. if (old_elem) {
  603. /* if we're updating the existing element,
  604. * use per-cpu extra elems to avoid freelist_pop/push
  605. */
  606. pl_new = this_cpu_ptr(htab->extra_elems);
  607. l_new = *pl_new;
  608. *pl_new = old_elem;
  609. } else {
  610. struct pcpu_freelist_node *l;
  611. l = pcpu_freelist_pop(&htab->freelist);
  612. if (!l)
  613. return ERR_PTR(-E2BIG);
  614. l_new = container_of(l, struct htab_elem, fnode);
  615. }
  616. } else {
  617. if (atomic_inc_return(&htab->count) > htab->map.max_entries)
  618. if (!old_elem) {
  619. /* when map is full and update() is replacing
  620. * old element, it's ok to allocate, since
  621. * old element will be freed immediately.
  622. * Otherwise return an error
  623. */
  624. atomic_dec(&htab->count);
  625. return ERR_PTR(-E2BIG);
  626. }
  627. l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
  628. htab->map.numa_node);
  629. if (!l_new)
  630. return ERR_PTR(-ENOMEM);
  631. }
  632. memcpy(l_new->key, key, key_size);
  633. if (percpu) {
  634. if (prealloc) {
  635. pptr = htab_elem_get_ptr(l_new, key_size);
  636. } else {
  637. /* alloc_percpu zero-fills */
  638. pptr = __alloc_percpu_gfp(size, 8,
  639. GFP_ATOMIC | __GFP_NOWARN);
  640. if (!pptr) {
  641. kfree(l_new);
  642. return ERR_PTR(-ENOMEM);
  643. }
  644. }
  645. pcpu_copy_value(htab, pptr, value, onallcpus);
  646. if (!prealloc)
  647. htab_elem_set_ptr(l_new, key_size, pptr);
  648. } else {
  649. memcpy(l_new->key + round_up(key_size, 8), value, size);
  650. }
  651. l_new->hash = hash;
  652. return l_new;
  653. }
  654. static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  655. u64 map_flags)
  656. {
  657. if (l_old && map_flags == BPF_NOEXIST)
  658. /* elem already exists */
  659. return -EEXIST;
  660. if (!l_old && map_flags == BPF_EXIST)
  661. /* elem doesn't exist, cannot update it */
  662. return -ENOENT;
  663. return 0;
  664. }
  665. /* Called from syscall or from eBPF program */
  666. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  667. u64 map_flags)
  668. {
  669. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  670. struct htab_elem *l_new = NULL, *l_old;
  671. struct hlist_nulls_head *head;
  672. unsigned long flags;
  673. struct bucket *b;
  674. u32 key_size, hash;
  675. int ret;
  676. if (unlikely(map_flags > BPF_EXIST))
  677. /* unknown flags */
  678. return -EINVAL;
  679. WARN_ON_ONCE(!rcu_read_lock_held());
  680. key_size = map->key_size;
  681. hash = htab_map_hash(key, key_size);
  682. b = __select_bucket(htab, hash);
  683. head = &b->head;
  684. /* bpf_map_update_elem() can be called in_irq() */
  685. raw_spin_lock_irqsave(&b->lock, flags);
  686. l_old = lookup_elem_raw(head, hash, key, key_size);
  687. ret = check_flags(htab, l_old, map_flags);
  688. if (ret)
  689. goto err;
  690. l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
  691. l_old);
  692. if (IS_ERR(l_new)) {
  693. /* all pre-allocated elements are in use or memory exhausted */
  694. ret = PTR_ERR(l_new);
  695. goto err;
  696. }
  697. /* add new element to the head of the list, so that
  698. * concurrent search will find it before old elem
  699. */
  700. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  701. if (l_old) {
  702. hlist_nulls_del_rcu(&l_old->hash_node);
  703. if (!htab_is_prealloc(htab))
  704. free_htab_elem(htab, l_old);
  705. }
  706. ret = 0;
  707. err:
  708. raw_spin_unlock_irqrestore(&b->lock, flags);
  709. return ret;
  710. }
  711. static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
  712. u64 map_flags)
  713. {
  714. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  715. struct htab_elem *l_new, *l_old = NULL;
  716. struct hlist_nulls_head *head;
  717. unsigned long flags;
  718. struct bucket *b;
  719. u32 key_size, hash;
  720. int ret;
  721. if (unlikely(map_flags > BPF_EXIST))
  722. /* unknown flags */
  723. return -EINVAL;
  724. WARN_ON_ONCE(!rcu_read_lock_held());
  725. key_size = map->key_size;
  726. hash = htab_map_hash(key, key_size);
  727. b = __select_bucket(htab, hash);
  728. head = &b->head;
  729. /* For LRU, we need to alloc before taking bucket's
  730. * spinlock because getting free nodes from LRU may need
  731. * to remove older elements from htab and this removal
  732. * operation will need a bucket lock.
  733. */
  734. l_new = prealloc_lru_pop(htab, key, hash);
  735. if (!l_new)
  736. return -ENOMEM;
  737. memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
  738. /* bpf_map_update_elem() can be called in_irq() */
  739. raw_spin_lock_irqsave(&b->lock, flags);
  740. l_old = lookup_elem_raw(head, hash, key, key_size);
  741. ret = check_flags(htab, l_old, map_flags);
  742. if (ret)
  743. goto err;
  744. /* add new element to the head of the list, so that
  745. * concurrent search will find it before old elem
  746. */
  747. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  748. if (l_old) {
  749. bpf_lru_node_set_ref(&l_new->lru_node);
  750. hlist_nulls_del_rcu(&l_old->hash_node);
  751. }
  752. ret = 0;
  753. err:
  754. raw_spin_unlock_irqrestore(&b->lock, flags);
  755. if (ret)
  756. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  757. else if (l_old)
  758. bpf_lru_push_free(&htab->lru, &l_old->lru_node);
  759. return ret;
  760. }
  761. static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  762. void *value, u64 map_flags,
  763. bool onallcpus)
  764. {
  765. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  766. struct htab_elem *l_new = NULL, *l_old;
  767. struct hlist_nulls_head *head;
  768. unsigned long flags;
  769. struct bucket *b;
  770. u32 key_size, hash;
  771. int ret;
  772. if (unlikely(map_flags > BPF_EXIST))
  773. /* unknown flags */
  774. return -EINVAL;
  775. WARN_ON_ONCE(!rcu_read_lock_held());
  776. key_size = map->key_size;
  777. hash = htab_map_hash(key, key_size);
  778. b = __select_bucket(htab, hash);
  779. head = &b->head;
  780. /* bpf_map_update_elem() can be called in_irq() */
  781. raw_spin_lock_irqsave(&b->lock, flags);
  782. l_old = lookup_elem_raw(head, hash, key, key_size);
  783. ret = check_flags(htab, l_old, map_flags);
  784. if (ret)
  785. goto err;
  786. if (l_old) {
  787. /* per-cpu hash map can update value in-place */
  788. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  789. value, onallcpus);
  790. } else {
  791. l_new = alloc_htab_elem(htab, key, value, key_size,
  792. hash, true, onallcpus, NULL);
  793. if (IS_ERR(l_new)) {
  794. ret = PTR_ERR(l_new);
  795. goto err;
  796. }
  797. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  798. }
  799. ret = 0;
  800. err:
  801. raw_spin_unlock_irqrestore(&b->lock, flags);
  802. return ret;
  803. }
  804. static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  805. void *value, u64 map_flags,
  806. bool onallcpus)
  807. {
  808. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  809. struct htab_elem *l_new = NULL, *l_old;
  810. struct hlist_nulls_head *head;
  811. unsigned long flags;
  812. struct bucket *b;
  813. u32 key_size, hash;
  814. int ret;
  815. if (unlikely(map_flags > BPF_EXIST))
  816. /* unknown flags */
  817. return -EINVAL;
  818. WARN_ON_ONCE(!rcu_read_lock_held());
  819. key_size = map->key_size;
  820. hash = htab_map_hash(key, key_size);
  821. b = __select_bucket(htab, hash);
  822. head = &b->head;
  823. /* For LRU, we need to alloc before taking bucket's
  824. * spinlock because LRU's elem alloc may need
  825. * to remove older elem from htab and this removal
  826. * operation will need a bucket lock.
  827. */
  828. if (map_flags != BPF_EXIST) {
  829. l_new = prealloc_lru_pop(htab, key, hash);
  830. if (!l_new)
  831. return -ENOMEM;
  832. }
  833. /* bpf_map_update_elem() can be called in_irq() */
  834. raw_spin_lock_irqsave(&b->lock, flags);
  835. l_old = lookup_elem_raw(head, hash, key, key_size);
  836. ret = check_flags(htab, l_old, map_flags);
  837. if (ret)
  838. goto err;
  839. if (l_old) {
  840. bpf_lru_node_set_ref(&l_old->lru_node);
  841. /* per-cpu hash map can update value in-place */
  842. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  843. value, onallcpus);
  844. } else {
  845. pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
  846. value, onallcpus);
  847. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  848. l_new = NULL;
  849. }
  850. ret = 0;
  851. err:
  852. raw_spin_unlock_irqrestore(&b->lock, flags);
  853. if (l_new)
  854. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  855. return ret;
  856. }
  857. static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  858. void *value, u64 map_flags)
  859. {
  860. return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
  861. }
  862. static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  863. void *value, u64 map_flags)
  864. {
  865. return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
  866. false);
  867. }
  868. /* Called from syscall or from eBPF program */
  869. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  870. {
  871. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  872. struct hlist_nulls_head *head;
  873. struct bucket *b;
  874. struct htab_elem *l;
  875. unsigned long flags;
  876. u32 hash, key_size;
  877. int ret = -ENOENT;
  878. WARN_ON_ONCE(!rcu_read_lock_held());
  879. key_size = map->key_size;
  880. hash = htab_map_hash(key, key_size);
  881. b = __select_bucket(htab, hash);
  882. head = &b->head;
  883. raw_spin_lock_irqsave(&b->lock, flags);
  884. l = lookup_elem_raw(head, hash, key, key_size);
  885. if (l) {
  886. hlist_nulls_del_rcu(&l->hash_node);
  887. free_htab_elem(htab, l);
  888. ret = 0;
  889. }
  890. raw_spin_unlock_irqrestore(&b->lock, flags);
  891. return ret;
  892. }
  893. static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
  894. {
  895. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  896. struct hlist_nulls_head *head;
  897. struct bucket *b;
  898. struct htab_elem *l;
  899. unsigned long flags;
  900. u32 hash, key_size;
  901. int ret = -ENOENT;
  902. WARN_ON_ONCE(!rcu_read_lock_held());
  903. key_size = map->key_size;
  904. hash = htab_map_hash(key, key_size);
  905. b = __select_bucket(htab, hash);
  906. head = &b->head;
  907. raw_spin_lock_irqsave(&b->lock, flags);
  908. l = lookup_elem_raw(head, hash, key, key_size);
  909. if (l) {
  910. hlist_nulls_del_rcu(&l->hash_node);
  911. ret = 0;
  912. }
  913. raw_spin_unlock_irqrestore(&b->lock, flags);
  914. if (l)
  915. bpf_lru_push_free(&htab->lru, &l->lru_node);
  916. return ret;
  917. }
  918. static void delete_all_elements(struct bpf_htab *htab)
  919. {
  920. int i;
  921. for (i = 0; i < htab->n_buckets; i++) {
  922. struct hlist_nulls_head *head = select_bucket(htab, i);
  923. struct hlist_nulls_node *n;
  924. struct htab_elem *l;
  925. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  926. hlist_nulls_del_rcu(&l->hash_node);
  927. htab_elem_free(htab, l);
  928. }
  929. }
  930. }
  931. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  932. static void htab_map_free(struct bpf_map *map)
  933. {
  934. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  935. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  936. * so the programs (can be more than one that used this map) were
  937. * disconnected from events. Wait for outstanding critical sections in
  938. * these programs to complete
  939. */
  940. synchronize_rcu();
  941. /* some of free_htab_elem() callbacks for elements of this map may
  942. * not have executed. Wait for them.
  943. */
  944. rcu_barrier();
  945. if (!htab_is_prealloc(htab))
  946. delete_all_elements(htab);
  947. else
  948. prealloc_destroy(htab);
  949. free_percpu(htab->extra_elems);
  950. bpf_map_area_free(htab->buckets);
  951. kfree(htab);
  952. }
  953. const struct bpf_map_ops htab_map_ops = {
  954. .map_alloc_check = htab_map_alloc_check,
  955. .map_alloc = htab_map_alloc,
  956. .map_free = htab_map_free,
  957. .map_get_next_key = htab_map_get_next_key,
  958. .map_lookup_elem = htab_map_lookup_elem,
  959. .map_update_elem = htab_map_update_elem,
  960. .map_delete_elem = htab_map_delete_elem,
  961. .map_gen_lookup = htab_map_gen_lookup,
  962. };
  963. const struct bpf_map_ops htab_lru_map_ops = {
  964. .map_alloc_check = htab_map_alloc_check,
  965. .map_alloc = htab_map_alloc,
  966. .map_free = htab_map_free,
  967. .map_get_next_key = htab_map_get_next_key,
  968. .map_lookup_elem = htab_lru_map_lookup_elem,
  969. .map_update_elem = htab_lru_map_update_elem,
  970. .map_delete_elem = htab_lru_map_delete_elem,
  971. .map_gen_lookup = htab_lru_map_gen_lookup,
  972. };
  973. /* Called from eBPF program */
  974. static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  975. {
  976. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  977. if (l)
  978. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  979. else
  980. return NULL;
  981. }
  982. static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  983. {
  984. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  985. if (l) {
  986. bpf_lru_node_set_ref(&l->lru_node);
  987. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  988. }
  989. return NULL;
  990. }
  991. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
  992. {
  993. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  994. struct htab_elem *l;
  995. void __percpu *pptr;
  996. int ret = -ENOENT;
  997. int cpu, off = 0;
  998. u32 size;
  999. /* per_cpu areas are zero-filled and bpf programs can only
  1000. * access 'value_size' of them, so copying rounded areas
  1001. * will not leak any kernel data
  1002. */
  1003. size = round_up(map->value_size, 8);
  1004. rcu_read_lock();
  1005. l = __htab_map_lookup_elem(map, key);
  1006. if (!l)
  1007. goto out;
  1008. if (htab_is_lru(htab))
  1009. bpf_lru_node_set_ref(&l->lru_node);
  1010. pptr = htab_elem_get_ptr(l, map->key_size);
  1011. for_each_possible_cpu(cpu) {
  1012. bpf_long_memcpy(value + off,
  1013. per_cpu_ptr(pptr, cpu), size);
  1014. off += size;
  1015. }
  1016. ret = 0;
  1017. out:
  1018. rcu_read_unlock();
  1019. return ret;
  1020. }
  1021. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  1022. u64 map_flags)
  1023. {
  1024. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1025. int ret;
  1026. rcu_read_lock();
  1027. if (htab_is_lru(htab))
  1028. ret = __htab_lru_percpu_map_update_elem(map, key, value,
  1029. map_flags, true);
  1030. else
  1031. ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
  1032. true);
  1033. rcu_read_unlock();
  1034. return ret;
  1035. }
  1036. const struct bpf_map_ops htab_percpu_map_ops = {
  1037. .map_alloc_check = htab_map_alloc_check,
  1038. .map_alloc = htab_map_alloc,
  1039. .map_free = htab_map_free,
  1040. .map_get_next_key = htab_map_get_next_key,
  1041. .map_lookup_elem = htab_percpu_map_lookup_elem,
  1042. .map_update_elem = htab_percpu_map_update_elem,
  1043. .map_delete_elem = htab_map_delete_elem,
  1044. };
  1045. const struct bpf_map_ops htab_lru_percpu_map_ops = {
  1046. .map_alloc_check = htab_map_alloc_check,
  1047. .map_alloc = htab_map_alloc,
  1048. .map_free = htab_map_free,
  1049. .map_get_next_key = htab_map_get_next_key,
  1050. .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
  1051. .map_update_elem = htab_lru_percpu_map_update_elem,
  1052. .map_delete_elem = htab_lru_map_delete_elem,
  1053. };
  1054. static int fd_htab_map_alloc_check(union bpf_attr *attr)
  1055. {
  1056. if (attr->value_size != sizeof(u32))
  1057. return -EINVAL;
  1058. return htab_map_alloc_check(attr);
  1059. }
  1060. static void fd_htab_map_free(struct bpf_map *map)
  1061. {
  1062. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1063. struct hlist_nulls_node *n;
  1064. struct hlist_nulls_head *head;
  1065. struct htab_elem *l;
  1066. int i;
  1067. for (i = 0; i < htab->n_buckets; i++) {
  1068. head = select_bucket(htab, i);
  1069. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  1070. void *ptr = fd_htab_map_get_ptr(map, l);
  1071. map->ops->map_fd_put_ptr(ptr);
  1072. }
  1073. }
  1074. htab_map_free(map);
  1075. }
  1076. /* only called from syscall */
  1077. int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
  1078. {
  1079. void **ptr;
  1080. int ret = 0;
  1081. if (!map->ops->map_fd_sys_lookup_elem)
  1082. return -ENOTSUPP;
  1083. rcu_read_lock();
  1084. ptr = htab_map_lookup_elem(map, key);
  1085. if (ptr)
  1086. *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
  1087. else
  1088. ret = -ENOENT;
  1089. rcu_read_unlock();
  1090. return ret;
  1091. }
  1092. /* only called from syscall */
  1093. int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
  1094. void *key, void *value, u64 map_flags)
  1095. {
  1096. void *ptr;
  1097. int ret;
  1098. u32 ufd = *(u32 *)value;
  1099. ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  1100. if (IS_ERR(ptr))
  1101. return PTR_ERR(ptr);
  1102. ret = htab_map_update_elem(map, key, &ptr, map_flags);
  1103. if (ret)
  1104. map->ops->map_fd_put_ptr(ptr);
  1105. return ret;
  1106. }
  1107. static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
  1108. {
  1109. struct bpf_map *map, *inner_map_meta;
  1110. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  1111. if (IS_ERR(inner_map_meta))
  1112. return inner_map_meta;
  1113. map = htab_map_alloc(attr);
  1114. if (IS_ERR(map)) {
  1115. bpf_map_meta_free(inner_map_meta);
  1116. return map;
  1117. }
  1118. map->inner_map_meta = inner_map_meta;
  1119. return map;
  1120. }
  1121. static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
  1122. {
  1123. struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
  1124. if (!inner_map)
  1125. return NULL;
  1126. return READ_ONCE(*inner_map);
  1127. }
  1128. static u32 htab_of_map_gen_lookup(struct bpf_map *map,
  1129. struct bpf_insn *insn_buf)
  1130. {
  1131. struct bpf_insn *insn = insn_buf;
  1132. const int ret = BPF_REG_0;
  1133. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  1134. (void *(*)(struct bpf_map *map, void *key))NULL));
  1135. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  1136. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
  1137. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  1138. offsetof(struct htab_elem, key) +
  1139. round_up(map->key_size, 8));
  1140. *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
  1141. return insn - insn_buf;
  1142. }
  1143. static void htab_of_map_free(struct bpf_map *map)
  1144. {
  1145. bpf_map_meta_free(map->inner_map_meta);
  1146. fd_htab_map_free(map);
  1147. }
  1148. const struct bpf_map_ops htab_of_maps_map_ops = {
  1149. .map_alloc_check = fd_htab_map_alloc_check,
  1150. .map_alloc = htab_of_map_alloc,
  1151. .map_free = htab_of_map_free,
  1152. .map_get_next_key = htab_map_get_next_key,
  1153. .map_lookup_elem = htab_of_map_lookup_elem,
  1154. .map_delete_elem = htab_map_delete_elem,
  1155. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  1156. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  1157. .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
  1158. .map_gen_lookup = htab_of_map_gen_lookup,
  1159. };