hashtab.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/bpf.h>
  14. #include <linux/jhash.h>
  15. #include <linux/filter.h>
  16. #include <linux/vmalloc.h>
  17. #include "percpu_freelist.h"
  18. #include "bpf_lru_list.h"
  19. struct bucket {
  20. struct hlist_head head;
  21. raw_spinlock_t lock;
  22. };
  23. struct bpf_htab {
  24. struct bpf_map map;
  25. struct bucket *buckets;
  26. void *elems;
  27. union {
  28. struct pcpu_freelist freelist;
  29. struct bpf_lru lru;
  30. };
  31. void __percpu *extra_elems;
  32. atomic_t count; /* number of elements in this hashtable */
  33. u32 n_buckets; /* number of hash buckets */
  34. u32 elem_size; /* size of each element in bytes */
  35. };
  36. enum extra_elem_state {
  37. HTAB_NOT_AN_EXTRA_ELEM = 0,
  38. HTAB_EXTRA_ELEM_FREE,
  39. HTAB_EXTRA_ELEM_USED
  40. };
  41. /* each htab element is struct htab_elem + key + value */
  42. struct htab_elem {
  43. union {
  44. struct hlist_node hash_node;
  45. struct bpf_htab *htab;
  46. struct pcpu_freelist_node fnode;
  47. };
  48. union {
  49. struct rcu_head rcu;
  50. enum extra_elem_state state;
  51. struct bpf_lru_node lru_node;
  52. };
  53. u32 hash;
  54. char key[0] __aligned(8);
  55. };
  56. static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
  57. static bool htab_is_lru(const struct bpf_htab *htab)
  58. {
  59. return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
  60. htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
  61. }
  62. static bool htab_is_percpu(const struct bpf_htab *htab)
  63. {
  64. return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  65. htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
  66. }
  67. static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
  68. void __percpu *pptr)
  69. {
  70. *(void __percpu **)(l->key + key_size) = pptr;
  71. }
  72. static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
  73. {
  74. return *(void __percpu **)(l->key + key_size);
  75. }
  76. static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
  77. {
  78. return (struct htab_elem *) (htab->elems + i * htab->elem_size);
  79. }
  80. static void htab_free_elems(struct bpf_htab *htab)
  81. {
  82. int i;
  83. if (!htab_is_percpu(htab))
  84. goto free_elems;
  85. for (i = 0; i < htab->map.max_entries; i++) {
  86. void __percpu *pptr;
  87. pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
  88. htab->map.key_size);
  89. free_percpu(pptr);
  90. }
  91. free_elems:
  92. vfree(htab->elems);
  93. }
  94. static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
  95. u32 hash)
  96. {
  97. struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
  98. struct htab_elem *l;
  99. if (node) {
  100. l = container_of(node, struct htab_elem, lru_node);
  101. memcpy(l->key, key, htab->map.key_size);
  102. return l;
  103. }
  104. return NULL;
  105. }
  106. static int prealloc_init(struct bpf_htab *htab)
  107. {
  108. int err = -ENOMEM, i;
  109. htab->elems = vzalloc(htab->elem_size * htab->map.max_entries);
  110. if (!htab->elems)
  111. return -ENOMEM;
  112. if (!htab_is_percpu(htab))
  113. goto skip_percpu_elems;
  114. for (i = 0; i < htab->map.max_entries; i++) {
  115. u32 size = round_up(htab->map.value_size, 8);
  116. void __percpu *pptr;
  117. pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
  118. if (!pptr)
  119. goto free_elems;
  120. htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
  121. pptr);
  122. }
  123. skip_percpu_elems:
  124. if (htab_is_lru(htab))
  125. err = bpf_lru_init(&htab->lru,
  126. htab->map.map_flags & BPF_F_NO_COMMON_LRU,
  127. offsetof(struct htab_elem, hash) -
  128. offsetof(struct htab_elem, lru_node),
  129. htab_lru_map_delete_node,
  130. htab);
  131. else
  132. err = pcpu_freelist_init(&htab->freelist);
  133. if (err)
  134. goto free_elems;
  135. if (htab_is_lru(htab))
  136. bpf_lru_populate(&htab->lru, htab->elems,
  137. offsetof(struct htab_elem, lru_node),
  138. htab->elem_size, htab->map.max_entries);
  139. else
  140. pcpu_freelist_populate(&htab->freelist, htab->elems,
  141. htab->elem_size, htab->map.max_entries);
  142. return 0;
  143. free_elems:
  144. htab_free_elems(htab);
  145. return err;
  146. }
  147. static void prealloc_destroy(struct bpf_htab *htab)
  148. {
  149. htab_free_elems(htab);
  150. if (htab_is_lru(htab))
  151. bpf_lru_destroy(&htab->lru);
  152. else
  153. pcpu_freelist_destroy(&htab->freelist);
  154. }
  155. static int alloc_extra_elems(struct bpf_htab *htab)
  156. {
  157. void __percpu *pptr;
  158. int cpu;
  159. pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN);
  160. if (!pptr)
  161. return -ENOMEM;
  162. for_each_possible_cpu(cpu) {
  163. ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state =
  164. HTAB_EXTRA_ELEM_FREE;
  165. }
  166. htab->extra_elems = pptr;
  167. return 0;
  168. }
  169. /* Called from syscall */
  170. static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
  171. {
  172. bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  173. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  174. bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
  175. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  176. /* percpu_lru means each cpu has its own LRU list.
  177. * it is different from BPF_MAP_TYPE_PERCPU_HASH where
  178. * the map's value itself is percpu. percpu_lru has
  179. * nothing to do with the map's value.
  180. */
  181. bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
  182. bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
  183. struct bpf_htab *htab;
  184. int err, i;
  185. u64 cost;
  186. if (lru && !capable(CAP_SYS_ADMIN))
  187. /* LRU implementation is much complicated than other
  188. * maps. Hence, limit to CAP_SYS_ADMIN for now.
  189. */
  190. return ERR_PTR(-EPERM);
  191. if (attr->map_flags & ~(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU))
  192. /* reserved bits should not be used */
  193. return ERR_PTR(-EINVAL);
  194. if (!lru && percpu_lru)
  195. return ERR_PTR(-EINVAL);
  196. if (lru && !prealloc)
  197. return ERR_PTR(-ENOTSUPP);
  198. htab = kzalloc(sizeof(*htab), GFP_USER);
  199. if (!htab)
  200. return ERR_PTR(-ENOMEM);
  201. /* mandatory map attributes */
  202. htab->map.map_type = attr->map_type;
  203. htab->map.key_size = attr->key_size;
  204. htab->map.value_size = attr->value_size;
  205. htab->map.max_entries = attr->max_entries;
  206. htab->map.map_flags = attr->map_flags;
  207. /* check sanity of attributes.
  208. * value_size == 0 may be allowed in the future to use map as a set
  209. */
  210. err = -EINVAL;
  211. if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
  212. htab->map.value_size == 0)
  213. goto free_htab;
  214. if (percpu_lru) {
  215. /* ensure each CPU's lru list has >=1 elements.
  216. * since we are at it, make each lru list has the same
  217. * number of elements.
  218. */
  219. htab->map.max_entries = roundup(attr->max_entries,
  220. num_possible_cpus());
  221. if (htab->map.max_entries < attr->max_entries)
  222. htab->map.max_entries = rounddown(attr->max_entries,
  223. num_possible_cpus());
  224. }
  225. /* hash table size must be power of 2 */
  226. htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
  227. err = -E2BIG;
  228. if (htab->map.key_size > MAX_BPF_STACK)
  229. /* eBPF programs initialize keys on stack, so they cannot be
  230. * larger than max stack size
  231. */
  232. goto free_htab;
  233. if (htab->map.value_size >= KMALLOC_MAX_SIZE -
  234. MAX_BPF_STACK - sizeof(struct htab_elem))
  235. /* if value_size is bigger, the user space won't be able to
  236. * access the elements via bpf syscall. This check also makes
  237. * sure that the elem_size doesn't overflow and it's
  238. * kmalloc-able later in htab_map_update_elem()
  239. */
  240. goto free_htab;
  241. if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
  242. /* make sure the size for pcpu_alloc() is reasonable */
  243. goto free_htab;
  244. htab->elem_size = sizeof(struct htab_elem) +
  245. round_up(htab->map.key_size, 8);
  246. if (percpu)
  247. htab->elem_size += sizeof(void *);
  248. else
  249. htab->elem_size += round_up(htab->map.value_size, 8);
  250. /* prevent zero size kmalloc and check for u32 overflow */
  251. if (htab->n_buckets == 0 ||
  252. htab->n_buckets > U32_MAX / sizeof(struct bucket))
  253. goto free_htab;
  254. cost = (u64) htab->n_buckets * sizeof(struct bucket) +
  255. (u64) htab->elem_size * htab->map.max_entries;
  256. if (percpu)
  257. cost += (u64) round_up(htab->map.value_size, 8) *
  258. num_possible_cpus() * htab->map.max_entries;
  259. else
  260. cost += (u64) htab->elem_size * num_possible_cpus();
  261. if (cost >= U32_MAX - PAGE_SIZE)
  262. /* make sure page count doesn't overflow */
  263. goto free_htab;
  264. htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  265. /* if map size is larger than memlock limit, reject it early */
  266. err = bpf_map_precharge_memlock(htab->map.pages);
  267. if (err)
  268. goto free_htab;
  269. err = -ENOMEM;
  270. htab->buckets = kmalloc_array(htab->n_buckets, sizeof(struct bucket),
  271. GFP_USER | __GFP_NOWARN);
  272. if (!htab->buckets) {
  273. htab->buckets = vmalloc(htab->n_buckets * sizeof(struct bucket));
  274. if (!htab->buckets)
  275. goto free_htab;
  276. }
  277. for (i = 0; i < htab->n_buckets; i++) {
  278. INIT_HLIST_HEAD(&htab->buckets[i].head);
  279. raw_spin_lock_init(&htab->buckets[i].lock);
  280. }
  281. if (!percpu && !lru) {
  282. /* lru itself can remove the least used element, so
  283. * there is no need for an extra elem during map_update.
  284. */
  285. err = alloc_extra_elems(htab);
  286. if (err)
  287. goto free_buckets;
  288. }
  289. if (prealloc) {
  290. err = prealloc_init(htab);
  291. if (err)
  292. goto free_extra_elems;
  293. }
  294. return &htab->map;
  295. free_extra_elems:
  296. free_percpu(htab->extra_elems);
  297. free_buckets:
  298. kvfree(htab->buckets);
  299. free_htab:
  300. kfree(htab);
  301. return ERR_PTR(err);
  302. }
  303. static inline u32 htab_map_hash(const void *key, u32 key_len)
  304. {
  305. return jhash(key, key_len, 0);
  306. }
  307. static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
  308. {
  309. return &htab->buckets[hash & (htab->n_buckets - 1)];
  310. }
  311. static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
  312. {
  313. return &__select_bucket(htab, hash)->head;
  314. }
  315. static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
  316. void *key, u32 key_size)
  317. {
  318. struct htab_elem *l;
  319. hlist_for_each_entry_rcu(l, head, hash_node)
  320. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  321. return l;
  322. return NULL;
  323. }
  324. /* Called from syscall or from eBPF program */
  325. static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
  326. {
  327. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  328. struct hlist_head *head;
  329. struct htab_elem *l;
  330. u32 hash, key_size;
  331. /* Must be called with rcu_read_lock. */
  332. WARN_ON_ONCE(!rcu_read_lock_held());
  333. key_size = map->key_size;
  334. hash = htab_map_hash(key, key_size);
  335. head = select_bucket(htab, hash);
  336. l = lookup_elem_raw(head, hash, key, key_size);
  337. return l;
  338. }
  339. static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
  340. {
  341. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  342. if (l)
  343. return l->key + round_up(map->key_size, 8);
  344. return NULL;
  345. }
  346. static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
  347. {
  348. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  349. if (l) {
  350. bpf_lru_node_set_ref(&l->lru_node);
  351. return l->key + round_up(map->key_size, 8);
  352. }
  353. return NULL;
  354. }
  355. /* It is called from the bpf_lru_list when the LRU needs to delete
  356. * older elements from the htab.
  357. */
  358. static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
  359. {
  360. struct bpf_htab *htab = (struct bpf_htab *)arg;
  361. struct htab_elem *l, *tgt_l;
  362. struct hlist_head *head;
  363. unsigned long flags;
  364. struct bucket *b;
  365. tgt_l = container_of(node, struct htab_elem, lru_node);
  366. b = __select_bucket(htab, tgt_l->hash);
  367. head = &b->head;
  368. raw_spin_lock_irqsave(&b->lock, flags);
  369. hlist_for_each_entry_rcu(l, head, hash_node)
  370. if (l == tgt_l) {
  371. hlist_del_rcu(&l->hash_node);
  372. break;
  373. }
  374. raw_spin_unlock_irqrestore(&b->lock, flags);
  375. return l == tgt_l;
  376. }
  377. /* Called from syscall */
  378. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  379. {
  380. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  381. struct hlist_head *head;
  382. struct htab_elem *l, *next_l;
  383. u32 hash, key_size;
  384. int i;
  385. WARN_ON_ONCE(!rcu_read_lock_held());
  386. key_size = map->key_size;
  387. hash = htab_map_hash(key, key_size);
  388. head = select_bucket(htab, hash);
  389. /* lookup the key */
  390. l = lookup_elem_raw(head, hash, key, key_size);
  391. if (!l) {
  392. i = 0;
  393. goto find_first_elem;
  394. }
  395. /* key was found, get next key in the same bucket */
  396. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
  397. struct htab_elem, hash_node);
  398. if (next_l) {
  399. /* if next elem in this hash list is non-zero, just return it */
  400. memcpy(next_key, next_l->key, key_size);
  401. return 0;
  402. }
  403. /* no more elements in this hash list, go to the next bucket */
  404. i = hash & (htab->n_buckets - 1);
  405. i++;
  406. find_first_elem:
  407. /* iterate over buckets */
  408. for (; i < htab->n_buckets; i++) {
  409. head = select_bucket(htab, i);
  410. /* pick first element in the bucket */
  411. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
  412. struct htab_elem, hash_node);
  413. if (next_l) {
  414. /* if it's not empty, just return it */
  415. memcpy(next_key, next_l->key, key_size);
  416. return 0;
  417. }
  418. }
  419. /* iterated over all buckets and all elements */
  420. return -ENOENT;
  421. }
  422. static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
  423. {
  424. if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
  425. free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
  426. kfree(l);
  427. }
  428. static void htab_elem_free_rcu(struct rcu_head *head)
  429. {
  430. struct htab_elem *l = container_of(head, struct htab_elem, rcu);
  431. struct bpf_htab *htab = l->htab;
  432. /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
  433. * we're calling kfree, otherwise deadlock is possible if kprobes
  434. * are placed somewhere inside of slub
  435. */
  436. preempt_disable();
  437. __this_cpu_inc(bpf_prog_active);
  438. htab_elem_free(htab, l);
  439. __this_cpu_dec(bpf_prog_active);
  440. preempt_enable();
  441. }
  442. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  443. {
  444. if (l->state == HTAB_EXTRA_ELEM_USED) {
  445. l->state = HTAB_EXTRA_ELEM_FREE;
  446. return;
  447. }
  448. if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
  449. pcpu_freelist_push(&htab->freelist, &l->fnode);
  450. } else {
  451. atomic_dec(&htab->count);
  452. l->htab = htab;
  453. call_rcu(&l->rcu, htab_elem_free_rcu);
  454. }
  455. }
  456. static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
  457. void *value, bool onallcpus)
  458. {
  459. if (!onallcpus) {
  460. /* copy true value_size bytes */
  461. memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
  462. } else {
  463. u32 size = round_up(htab->map.value_size, 8);
  464. int off = 0, cpu;
  465. for_each_possible_cpu(cpu) {
  466. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  467. value + off, size);
  468. off += size;
  469. }
  470. }
  471. }
  472. static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
  473. void *value, u32 key_size, u32 hash,
  474. bool percpu, bool onallcpus,
  475. bool old_elem_exists)
  476. {
  477. u32 size = htab->map.value_size;
  478. bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
  479. struct htab_elem *l_new;
  480. void __percpu *pptr;
  481. int err = 0;
  482. if (prealloc) {
  483. l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
  484. if (!l_new)
  485. err = -E2BIG;
  486. } else {
  487. if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
  488. atomic_dec(&htab->count);
  489. err = -E2BIG;
  490. } else {
  491. l_new = kmalloc(htab->elem_size,
  492. GFP_ATOMIC | __GFP_NOWARN);
  493. if (!l_new)
  494. return ERR_PTR(-ENOMEM);
  495. }
  496. }
  497. if (err) {
  498. if (!old_elem_exists)
  499. return ERR_PTR(err);
  500. /* if we're updating the existing element and the hash table
  501. * is full, use per-cpu extra elems
  502. */
  503. l_new = this_cpu_ptr(htab->extra_elems);
  504. if (l_new->state != HTAB_EXTRA_ELEM_FREE)
  505. return ERR_PTR(-E2BIG);
  506. l_new->state = HTAB_EXTRA_ELEM_USED;
  507. } else {
  508. l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
  509. }
  510. memcpy(l_new->key, key, key_size);
  511. if (percpu) {
  512. /* round up value_size to 8 bytes */
  513. size = round_up(size, 8);
  514. if (prealloc) {
  515. pptr = htab_elem_get_ptr(l_new, key_size);
  516. } else {
  517. /* alloc_percpu zero-fills */
  518. pptr = __alloc_percpu_gfp(size, 8,
  519. GFP_ATOMIC | __GFP_NOWARN);
  520. if (!pptr) {
  521. kfree(l_new);
  522. return ERR_PTR(-ENOMEM);
  523. }
  524. }
  525. pcpu_copy_value(htab, pptr, value, onallcpus);
  526. if (!prealloc)
  527. htab_elem_set_ptr(l_new, key_size, pptr);
  528. } else {
  529. memcpy(l_new->key + round_up(key_size, 8), value, size);
  530. }
  531. l_new->hash = hash;
  532. return l_new;
  533. }
  534. static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  535. u64 map_flags)
  536. {
  537. if (l_old && map_flags == BPF_NOEXIST)
  538. /* elem already exists */
  539. return -EEXIST;
  540. if (!l_old && map_flags == BPF_EXIST)
  541. /* elem doesn't exist, cannot update it */
  542. return -ENOENT;
  543. return 0;
  544. }
  545. /* Called from syscall or from eBPF program */
  546. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  547. u64 map_flags)
  548. {
  549. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  550. struct htab_elem *l_new = NULL, *l_old;
  551. struct hlist_head *head;
  552. unsigned long flags;
  553. struct bucket *b;
  554. u32 key_size, hash;
  555. int ret;
  556. if (unlikely(map_flags > BPF_EXIST))
  557. /* unknown flags */
  558. return -EINVAL;
  559. WARN_ON_ONCE(!rcu_read_lock_held());
  560. key_size = map->key_size;
  561. hash = htab_map_hash(key, key_size);
  562. b = __select_bucket(htab, hash);
  563. head = &b->head;
  564. /* bpf_map_update_elem() can be called in_irq() */
  565. raw_spin_lock_irqsave(&b->lock, flags);
  566. l_old = lookup_elem_raw(head, hash, key, key_size);
  567. ret = check_flags(htab, l_old, map_flags);
  568. if (ret)
  569. goto err;
  570. l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
  571. !!l_old);
  572. if (IS_ERR(l_new)) {
  573. /* all pre-allocated elements are in use or memory exhausted */
  574. ret = PTR_ERR(l_new);
  575. goto err;
  576. }
  577. /* add new element to the head of the list, so that
  578. * concurrent search will find it before old elem
  579. */
  580. hlist_add_head_rcu(&l_new->hash_node, head);
  581. if (l_old) {
  582. hlist_del_rcu(&l_old->hash_node);
  583. free_htab_elem(htab, l_old);
  584. }
  585. ret = 0;
  586. err:
  587. raw_spin_unlock_irqrestore(&b->lock, flags);
  588. return ret;
  589. }
  590. static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
  591. u64 map_flags)
  592. {
  593. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  594. struct htab_elem *l_new, *l_old = NULL;
  595. struct hlist_head *head;
  596. unsigned long flags;
  597. struct bucket *b;
  598. u32 key_size, hash;
  599. int ret;
  600. if (unlikely(map_flags > BPF_EXIST))
  601. /* unknown flags */
  602. return -EINVAL;
  603. WARN_ON_ONCE(!rcu_read_lock_held());
  604. key_size = map->key_size;
  605. hash = htab_map_hash(key, key_size);
  606. b = __select_bucket(htab, hash);
  607. head = &b->head;
  608. /* For LRU, we need to alloc before taking bucket's
  609. * spinlock because getting free nodes from LRU may need
  610. * to remove older elements from htab and this removal
  611. * operation will need a bucket lock.
  612. */
  613. l_new = prealloc_lru_pop(htab, key, hash);
  614. if (!l_new)
  615. return -ENOMEM;
  616. memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
  617. /* bpf_map_update_elem() can be called in_irq() */
  618. raw_spin_lock_irqsave(&b->lock, flags);
  619. l_old = lookup_elem_raw(head, hash, key, key_size);
  620. ret = check_flags(htab, l_old, map_flags);
  621. if (ret)
  622. goto err;
  623. /* add new element to the head of the list, so that
  624. * concurrent search will find it before old elem
  625. */
  626. hlist_add_head_rcu(&l_new->hash_node, head);
  627. if (l_old) {
  628. bpf_lru_node_set_ref(&l_new->lru_node);
  629. hlist_del_rcu(&l_old->hash_node);
  630. }
  631. ret = 0;
  632. err:
  633. raw_spin_unlock_irqrestore(&b->lock, flags);
  634. if (ret)
  635. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  636. else if (l_old)
  637. bpf_lru_push_free(&htab->lru, &l_old->lru_node);
  638. return ret;
  639. }
  640. static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  641. void *value, u64 map_flags,
  642. bool onallcpus)
  643. {
  644. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  645. struct htab_elem *l_new = NULL, *l_old;
  646. struct hlist_head *head;
  647. unsigned long flags;
  648. struct bucket *b;
  649. u32 key_size, hash;
  650. int ret;
  651. if (unlikely(map_flags > BPF_EXIST))
  652. /* unknown flags */
  653. return -EINVAL;
  654. WARN_ON_ONCE(!rcu_read_lock_held());
  655. key_size = map->key_size;
  656. hash = htab_map_hash(key, key_size);
  657. b = __select_bucket(htab, hash);
  658. head = &b->head;
  659. /* bpf_map_update_elem() can be called in_irq() */
  660. raw_spin_lock_irqsave(&b->lock, flags);
  661. l_old = lookup_elem_raw(head, hash, key, key_size);
  662. ret = check_flags(htab, l_old, map_flags);
  663. if (ret)
  664. goto err;
  665. if (l_old) {
  666. /* per-cpu hash map can update value in-place */
  667. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  668. value, onallcpus);
  669. } else {
  670. l_new = alloc_htab_elem(htab, key, value, key_size,
  671. hash, true, onallcpus, false);
  672. if (IS_ERR(l_new)) {
  673. ret = PTR_ERR(l_new);
  674. goto err;
  675. }
  676. hlist_add_head_rcu(&l_new->hash_node, head);
  677. }
  678. ret = 0;
  679. err:
  680. raw_spin_unlock_irqrestore(&b->lock, flags);
  681. return ret;
  682. }
  683. static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  684. void *value, u64 map_flags,
  685. bool onallcpus)
  686. {
  687. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  688. struct htab_elem *l_new = NULL, *l_old;
  689. struct hlist_head *head;
  690. unsigned long flags;
  691. struct bucket *b;
  692. u32 key_size, hash;
  693. int ret;
  694. if (unlikely(map_flags > BPF_EXIST))
  695. /* unknown flags */
  696. return -EINVAL;
  697. WARN_ON_ONCE(!rcu_read_lock_held());
  698. key_size = map->key_size;
  699. hash = htab_map_hash(key, key_size);
  700. b = __select_bucket(htab, hash);
  701. head = &b->head;
  702. /* For LRU, we need to alloc before taking bucket's
  703. * spinlock because LRU's elem alloc may need
  704. * to remove older elem from htab and this removal
  705. * operation will need a bucket lock.
  706. */
  707. if (map_flags != BPF_EXIST) {
  708. l_new = prealloc_lru_pop(htab, key, hash);
  709. if (!l_new)
  710. return -ENOMEM;
  711. }
  712. /* bpf_map_update_elem() can be called in_irq() */
  713. raw_spin_lock_irqsave(&b->lock, flags);
  714. l_old = lookup_elem_raw(head, hash, key, key_size);
  715. ret = check_flags(htab, l_old, map_flags);
  716. if (ret)
  717. goto err;
  718. if (l_old) {
  719. bpf_lru_node_set_ref(&l_old->lru_node);
  720. /* per-cpu hash map can update value in-place */
  721. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  722. value, onallcpus);
  723. } else {
  724. pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
  725. value, onallcpus);
  726. hlist_add_head_rcu(&l_new->hash_node, head);
  727. l_new = NULL;
  728. }
  729. ret = 0;
  730. err:
  731. raw_spin_unlock_irqrestore(&b->lock, flags);
  732. if (l_new)
  733. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  734. return ret;
  735. }
  736. static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  737. void *value, u64 map_flags)
  738. {
  739. return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
  740. }
  741. static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  742. void *value, u64 map_flags)
  743. {
  744. return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
  745. false);
  746. }
  747. /* Called from syscall or from eBPF program */
  748. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  749. {
  750. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  751. struct hlist_head *head;
  752. struct bucket *b;
  753. struct htab_elem *l;
  754. unsigned long flags;
  755. u32 hash, key_size;
  756. int ret = -ENOENT;
  757. WARN_ON_ONCE(!rcu_read_lock_held());
  758. key_size = map->key_size;
  759. hash = htab_map_hash(key, key_size);
  760. b = __select_bucket(htab, hash);
  761. head = &b->head;
  762. raw_spin_lock_irqsave(&b->lock, flags);
  763. l = lookup_elem_raw(head, hash, key, key_size);
  764. if (l) {
  765. hlist_del_rcu(&l->hash_node);
  766. free_htab_elem(htab, l);
  767. ret = 0;
  768. }
  769. raw_spin_unlock_irqrestore(&b->lock, flags);
  770. return ret;
  771. }
  772. static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
  773. {
  774. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  775. struct hlist_head *head;
  776. struct bucket *b;
  777. struct htab_elem *l;
  778. unsigned long flags;
  779. u32 hash, key_size;
  780. int ret = -ENOENT;
  781. WARN_ON_ONCE(!rcu_read_lock_held());
  782. key_size = map->key_size;
  783. hash = htab_map_hash(key, key_size);
  784. b = __select_bucket(htab, hash);
  785. head = &b->head;
  786. raw_spin_lock_irqsave(&b->lock, flags);
  787. l = lookup_elem_raw(head, hash, key, key_size);
  788. if (l) {
  789. hlist_del_rcu(&l->hash_node);
  790. ret = 0;
  791. }
  792. raw_spin_unlock_irqrestore(&b->lock, flags);
  793. if (l)
  794. bpf_lru_push_free(&htab->lru, &l->lru_node);
  795. return ret;
  796. }
  797. static void delete_all_elements(struct bpf_htab *htab)
  798. {
  799. int i;
  800. for (i = 0; i < htab->n_buckets; i++) {
  801. struct hlist_head *head = select_bucket(htab, i);
  802. struct hlist_node *n;
  803. struct htab_elem *l;
  804. hlist_for_each_entry_safe(l, n, head, hash_node) {
  805. hlist_del_rcu(&l->hash_node);
  806. if (l->state != HTAB_EXTRA_ELEM_USED)
  807. htab_elem_free(htab, l);
  808. }
  809. }
  810. }
  811. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  812. static void htab_map_free(struct bpf_map *map)
  813. {
  814. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  815. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  816. * so the programs (can be more than one that used this map) were
  817. * disconnected from events. Wait for outstanding critical sections in
  818. * these programs to complete
  819. */
  820. synchronize_rcu();
  821. /* some of free_htab_elem() callbacks for elements of this map may
  822. * not have executed. Wait for them.
  823. */
  824. rcu_barrier();
  825. if (htab->map.map_flags & BPF_F_NO_PREALLOC)
  826. delete_all_elements(htab);
  827. else
  828. prealloc_destroy(htab);
  829. free_percpu(htab->extra_elems);
  830. kvfree(htab->buckets);
  831. kfree(htab);
  832. }
  833. static const struct bpf_map_ops htab_ops = {
  834. .map_alloc = htab_map_alloc,
  835. .map_free = htab_map_free,
  836. .map_get_next_key = htab_map_get_next_key,
  837. .map_lookup_elem = htab_map_lookup_elem,
  838. .map_update_elem = htab_map_update_elem,
  839. .map_delete_elem = htab_map_delete_elem,
  840. };
  841. static struct bpf_map_type_list htab_type __read_mostly = {
  842. .ops = &htab_ops,
  843. .type = BPF_MAP_TYPE_HASH,
  844. };
  845. static const struct bpf_map_ops htab_lru_ops = {
  846. .map_alloc = htab_map_alloc,
  847. .map_free = htab_map_free,
  848. .map_get_next_key = htab_map_get_next_key,
  849. .map_lookup_elem = htab_lru_map_lookup_elem,
  850. .map_update_elem = htab_lru_map_update_elem,
  851. .map_delete_elem = htab_lru_map_delete_elem,
  852. };
  853. static struct bpf_map_type_list htab_lru_type __read_mostly = {
  854. .ops = &htab_lru_ops,
  855. .type = BPF_MAP_TYPE_LRU_HASH,
  856. };
  857. /* Called from eBPF program */
  858. static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  859. {
  860. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  861. if (l)
  862. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  863. else
  864. return NULL;
  865. }
  866. static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  867. {
  868. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  869. if (l) {
  870. bpf_lru_node_set_ref(&l->lru_node);
  871. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  872. }
  873. return NULL;
  874. }
  875. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
  876. {
  877. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  878. struct htab_elem *l;
  879. void __percpu *pptr;
  880. int ret = -ENOENT;
  881. int cpu, off = 0;
  882. u32 size;
  883. /* per_cpu areas are zero-filled and bpf programs can only
  884. * access 'value_size' of them, so copying rounded areas
  885. * will not leak any kernel data
  886. */
  887. size = round_up(map->value_size, 8);
  888. rcu_read_lock();
  889. l = __htab_map_lookup_elem(map, key);
  890. if (!l)
  891. goto out;
  892. if (htab_is_lru(htab))
  893. bpf_lru_node_set_ref(&l->lru_node);
  894. pptr = htab_elem_get_ptr(l, map->key_size);
  895. for_each_possible_cpu(cpu) {
  896. bpf_long_memcpy(value + off,
  897. per_cpu_ptr(pptr, cpu), size);
  898. off += size;
  899. }
  900. ret = 0;
  901. out:
  902. rcu_read_unlock();
  903. return ret;
  904. }
  905. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  906. u64 map_flags)
  907. {
  908. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  909. int ret;
  910. rcu_read_lock();
  911. if (htab_is_lru(htab))
  912. ret = __htab_lru_percpu_map_update_elem(map, key, value,
  913. map_flags, true);
  914. else
  915. ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
  916. true);
  917. rcu_read_unlock();
  918. return ret;
  919. }
  920. static const struct bpf_map_ops htab_percpu_ops = {
  921. .map_alloc = htab_map_alloc,
  922. .map_free = htab_map_free,
  923. .map_get_next_key = htab_map_get_next_key,
  924. .map_lookup_elem = htab_percpu_map_lookup_elem,
  925. .map_update_elem = htab_percpu_map_update_elem,
  926. .map_delete_elem = htab_map_delete_elem,
  927. };
  928. static struct bpf_map_type_list htab_percpu_type __read_mostly = {
  929. .ops = &htab_percpu_ops,
  930. .type = BPF_MAP_TYPE_PERCPU_HASH,
  931. };
  932. static const struct bpf_map_ops htab_lru_percpu_ops = {
  933. .map_alloc = htab_map_alloc,
  934. .map_free = htab_map_free,
  935. .map_get_next_key = htab_map_get_next_key,
  936. .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
  937. .map_update_elem = htab_lru_percpu_map_update_elem,
  938. .map_delete_elem = htab_lru_map_delete_elem,
  939. };
  940. static struct bpf_map_type_list htab_lru_percpu_type __read_mostly = {
  941. .ops = &htab_lru_percpu_ops,
  942. .type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
  943. };
  944. static int __init register_htab_map(void)
  945. {
  946. bpf_register_map_type(&htab_type);
  947. bpf_register_map_type(&htab_percpu_type);
  948. bpf_register_map_type(&htab_lru_type);
  949. bpf_register_map_type(&htab_lru_percpu_type);
  950. return 0;
  951. }
  952. late_initcall(register_htab_map);