hashtab.c 30 KB

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