hashtab.c 37 KB

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