hashtab.c 36 KB

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