hashtab.c 35 KB

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