hashtab.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. const struct bpf_map_ops htab_percpu_map_ops = {
  1066. .map_alloc_check = htab_map_alloc_check,
  1067. .map_alloc = htab_map_alloc,
  1068. .map_free = htab_map_free,
  1069. .map_get_next_key = htab_map_get_next_key,
  1070. .map_lookup_elem = htab_percpu_map_lookup_elem,
  1071. .map_update_elem = htab_percpu_map_update_elem,
  1072. .map_delete_elem = htab_map_delete_elem,
  1073. };
  1074. const struct bpf_map_ops htab_lru_percpu_map_ops = {
  1075. .map_alloc_check = htab_map_alloc_check,
  1076. .map_alloc = htab_map_alloc,
  1077. .map_free = htab_map_free,
  1078. .map_get_next_key = htab_map_get_next_key,
  1079. .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
  1080. .map_update_elem = htab_lru_percpu_map_update_elem,
  1081. .map_delete_elem = htab_lru_map_delete_elem,
  1082. };
  1083. static int fd_htab_map_alloc_check(union bpf_attr *attr)
  1084. {
  1085. if (attr->value_size != sizeof(u32))
  1086. return -EINVAL;
  1087. return htab_map_alloc_check(attr);
  1088. }
  1089. static void fd_htab_map_free(struct bpf_map *map)
  1090. {
  1091. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1092. struct hlist_nulls_node *n;
  1093. struct hlist_nulls_head *head;
  1094. struct htab_elem *l;
  1095. int i;
  1096. for (i = 0; i < htab->n_buckets; i++) {
  1097. head = select_bucket(htab, i);
  1098. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  1099. void *ptr = fd_htab_map_get_ptr(map, l);
  1100. map->ops->map_fd_put_ptr(ptr);
  1101. }
  1102. }
  1103. htab_map_free(map);
  1104. }
  1105. /* only called from syscall */
  1106. int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
  1107. {
  1108. void **ptr;
  1109. int ret = 0;
  1110. if (!map->ops->map_fd_sys_lookup_elem)
  1111. return -ENOTSUPP;
  1112. rcu_read_lock();
  1113. ptr = htab_map_lookup_elem(map, key);
  1114. if (ptr)
  1115. *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
  1116. else
  1117. ret = -ENOENT;
  1118. rcu_read_unlock();
  1119. return ret;
  1120. }
  1121. /* only called from syscall */
  1122. int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
  1123. void *key, void *value, u64 map_flags)
  1124. {
  1125. void *ptr;
  1126. int ret;
  1127. u32 ufd = *(u32 *)value;
  1128. ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  1129. if (IS_ERR(ptr))
  1130. return PTR_ERR(ptr);
  1131. ret = htab_map_update_elem(map, key, &ptr, map_flags);
  1132. if (ret)
  1133. map->ops->map_fd_put_ptr(ptr);
  1134. return ret;
  1135. }
  1136. static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
  1137. {
  1138. struct bpf_map *map, *inner_map_meta;
  1139. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  1140. if (IS_ERR(inner_map_meta))
  1141. return inner_map_meta;
  1142. map = htab_map_alloc(attr);
  1143. if (IS_ERR(map)) {
  1144. bpf_map_meta_free(inner_map_meta);
  1145. return map;
  1146. }
  1147. map->inner_map_meta = inner_map_meta;
  1148. return map;
  1149. }
  1150. static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
  1151. {
  1152. struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
  1153. if (!inner_map)
  1154. return NULL;
  1155. return READ_ONCE(*inner_map);
  1156. }
  1157. static u32 htab_of_map_gen_lookup(struct bpf_map *map,
  1158. struct bpf_insn *insn_buf)
  1159. {
  1160. struct bpf_insn *insn = insn_buf;
  1161. const int ret = BPF_REG_0;
  1162. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  1163. (void *(*)(struct bpf_map *map, void *key))NULL));
  1164. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  1165. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
  1166. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  1167. offsetof(struct htab_elem, key) +
  1168. round_up(map->key_size, 8));
  1169. *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
  1170. return insn - insn_buf;
  1171. }
  1172. static void htab_of_map_free(struct bpf_map *map)
  1173. {
  1174. bpf_map_meta_free(map->inner_map_meta);
  1175. fd_htab_map_free(map);
  1176. }
  1177. const struct bpf_map_ops htab_of_maps_map_ops = {
  1178. .map_alloc_check = fd_htab_map_alloc_check,
  1179. .map_alloc = htab_of_map_alloc,
  1180. .map_free = htab_of_map_free,
  1181. .map_get_next_key = htab_map_get_next_key,
  1182. .map_lookup_elem = htab_of_map_lookup_elem,
  1183. .map_delete_elem = htab_map_delete_elem,
  1184. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  1185. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  1186. .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
  1187. .map_gen_lookup = htab_of_map_gen_lookup,
  1188. .map_check_btf = map_check_no_btf,
  1189. };