hashtab.c 36 KB

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