hashtab.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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. /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
  555. * we're calling kfree, otherwise deadlock is possible if kprobes
  556. * are placed somewhere inside of slub
  557. */
  558. preempt_disable();
  559. __this_cpu_inc(bpf_prog_active);
  560. htab_elem_free(htab, l);
  561. __this_cpu_dec(bpf_prog_active);
  562. preempt_enable();
  563. }
  564. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  565. {
  566. struct bpf_map *map = &htab->map;
  567. if (map->ops->map_fd_put_ptr) {
  568. void *ptr = fd_htab_map_get_ptr(map, l);
  569. map->ops->map_fd_put_ptr(ptr);
  570. }
  571. if (htab_is_prealloc(htab)) {
  572. __pcpu_freelist_push(&htab->freelist, &l->fnode);
  573. } else {
  574. atomic_dec(&htab->count);
  575. l->htab = htab;
  576. call_rcu(&l->rcu, htab_elem_free_rcu);
  577. }
  578. }
  579. static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
  580. void *value, bool onallcpus)
  581. {
  582. if (!onallcpus) {
  583. /* copy true value_size bytes */
  584. memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
  585. } else {
  586. u32 size = round_up(htab->map.value_size, 8);
  587. int off = 0, cpu;
  588. for_each_possible_cpu(cpu) {
  589. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  590. value + off, size);
  591. off += size;
  592. }
  593. }
  594. }
  595. static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
  596. {
  597. return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
  598. BITS_PER_LONG == 64;
  599. }
  600. static u32 htab_size_value(const struct bpf_htab *htab, bool percpu)
  601. {
  602. u32 size = htab->map.value_size;
  603. if (percpu || fd_htab_map_needs_adjust(htab))
  604. size = round_up(size, 8);
  605. return size;
  606. }
  607. static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
  608. void *value, u32 key_size, u32 hash,
  609. bool percpu, bool onallcpus,
  610. struct htab_elem *old_elem)
  611. {
  612. u32 size = htab_size_value(htab, percpu);
  613. bool prealloc = htab_is_prealloc(htab);
  614. struct htab_elem *l_new, **pl_new;
  615. void __percpu *pptr;
  616. if (prealloc) {
  617. if (old_elem) {
  618. /* if we're updating the existing element,
  619. * use per-cpu extra elems to avoid freelist_pop/push
  620. */
  621. pl_new = this_cpu_ptr(htab->extra_elems);
  622. l_new = *pl_new;
  623. *pl_new = old_elem;
  624. } else {
  625. struct pcpu_freelist_node *l;
  626. l = __pcpu_freelist_pop(&htab->freelist);
  627. if (!l)
  628. return ERR_PTR(-E2BIG);
  629. l_new = container_of(l, struct htab_elem, fnode);
  630. }
  631. } else {
  632. if (atomic_inc_return(&htab->count) > htab->map.max_entries)
  633. if (!old_elem) {
  634. /* when map is full and update() is replacing
  635. * old element, it's ok to allocate, since
  636. * old element will be freed immediately.
  637. * Otherwise return an error
  638. */
  639. l_new = ERR_PTR(-E2BIG);
  640. goto dec_count;
  641. }
  642. l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
  643. htab->map.numa_node);
  644. if (!l_new) {
  645. l_new = ERR_PTR(-ENOMEM);
  646. goto dec_count;
  647. }
  648. }
  649. memcpy(l_new->key, key, key_size);
  650. if (percpu) {
  651. if (prealloc) {
  652. pptr = htab_elem_get_ptr(l_new, key_size);
  653. } else {
  654. /* alloc_percpu zero-fills */
  655. pptr = __alloc_percpu_gfp(size, 8,
  656. GFP_ATOMIC | __GFP_NOWARN);
  657. if (!pptr) {
  658. kfree(l_new);
  659. l_new = ERR_PTR(-ENOMEM);
  660. goto dec_count;
  661. }
  662. }
  663. pcpu_copy_value(htab, pptr, value, onallcpus);
  664. if (!prealloc)
  665. htab_elem_set_ptr(l_new, key_size, pptr);
  666. } else {
  667. memcpy(l_new->key + round_up(key_size, 8), value, size);
  668. }
  669. l_new->hash = hash;
  670. return l_new;
  671. dec_count:
  672. atomic_dec(&htab->count);
  673. return l_new;
  674. }
  675. static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  676. u64 map_flags)
  677. {
  678. if (l_old && map_flags == BPF_NOEXIST)
  679. /* elem already exists */
  680. return -EEXIST;
  681. if (!l_old && map_flags == BPF_EXIST)
  682. /* elem doesn't exist, cannot update it */
  683. return -ENOENT;
  684. return 0;
  685. }
  686. /* Called from syscall or from eBPF program */
  687. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  688. u64 map_flags)
  689. {
  690. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  691. struct htab_elem *l_new = NULL, *l_old;
  692. struct hlist_nulls_head *head;
  693. unsigned long flags;
  694. struct bucket *b;
  695. u32 key_size, hash;
  696. int ret;
  697. if (unlikely(map_flags > BPF_EXIST))
  698. /* unknown flags */
  699. return -EINVAL;
  700. WARN_ON_ONCE(!rcu_read_lock_held());
  701. key_size = map->key_size;
  702. hash = htab_map_hash(key, key_size, htab->hashrnd);
  703. b = __select_bucket(htab, hash);
  704. head = &b->head;
  705. /* bpf_map_update_elem() can be called in_irq() */
  706. raw_spin_lock_irqsave(&b->lock, flags);
  707. l_old = lookup_elem_raw(head, hash, key, key_size);
  708. ret = check_flags(htab, l_old, map_flags);
  709. if (ret)
  710. goto err;
  711. l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
  712. l_old);
  713. if (IS_ERR(l_new)) {
  714. /* all pre-allocated elements are in use or memory exhausted */
  715. ret = PTR_ERR(l_new);
  716. goto err;
  717. }
  718. /* add new element to the head of the list, so that
  719. * concurrent search will find it before old elem
  720. */
  721. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  722. if (l_old) {
  723. hlist_nulls_del_rcu(&l_old->hash_node);
  724. if (!htab_is_prealloc(htab))
  725. free_htab_elem(htab, l_old);
  726. }
  727. ret = 0;
  728. err:
  729. raw_spin_unlock_irqrestore(&b->lock, flags);
  730. return ret;
  731. }
  732. static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
  733. u64 map_flags)
  734. {
  735. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  736. struct htab_elem *l_new, *l_old = NULL;
  737. struct hlist_nulls_head *head;
  738. unsigned long flags;
  739. struct bucket *b;
  740. u32 key_size, hash;
  741. int ret;
  742. if (unlikely(map_flags > BPF_EXIST))
  743. /* unknown flags */
  744. return -EINVAL;
  745. WARN_ON_ONCE(!rcu_read_lock_held());
  746. key_size = map->key_size;
  747. hash = htab_map_hash(key, key_size, htab->hashrnd);
  748. b = __select_bucket(htab, hash);
  749. head = &b->head;
  750. /* For LRU, we need to alloc before taking bucket's
  751. * spinlock because getting free nodes from LRU may need
  752. * to remove older elements from htab and this removal
  753. * operation will need a bucket lock.
  754. */
  755. l_new = prealloc_lru_pop(htab, key, hash);
  756. if (!l_new)
  757. return -ENOMEM;
  758. memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
  759. /* bpf_map_update_elem() can be called in_irq() */
  760. raw_spin_lock_irqsave(&b->lock, flags);
  761. l_old = lookup_elem_raw(head, hash, key, key_size);
  762. ret = check_flags(htab, l_old, map_flags);
  763. if (ret)
  764. goto err;
  765. /* add new element to the head of the list, so that
  766. * concurrent search will find it before old elem
  767. */
  768. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  769. if (l_old) {
  770. bpf_lru_node_set_ref(&l_new->lru_node);
  771. hlist_nulls_del_rcu(&l_old->hash_node);
  772. }
  773. ret = 0;
  774. err:
  775. raw_spin_unlock_irqrestore(&b->lock, flags);
  776. if (ret)
  777. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  778. else if (l_old)
  779. bpf_lru_push_free(&htab->lru, &l_old->lru_node);
  780. return ret;
  781. }
  782. static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  783. void *value, u64 map_flags,
  784. bool onallcpus)
  785. {
  786. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  787. struct htab_elem *l_new = NULL, *l_old;
  788. struct hlist_nulls_head *head;
  789. unsigned long flags;
  790. struct bucket *b;
  791. u32 key_size, hash;
  792. int ret;
  793. if (unlikely(map_flags > BPF_EXIST))
  794. /* unknown flags */
  795. return -EINVAL;
  796. WARN_ON_ONCE(!rcu_read_lock_held());
  797. key_size = map->key_size;
  798. hash = htab_map_hash(key, key_size, htab->hashrnd);
  799. b = __select_bucket(htab, hash);
  800. head = &b->head;
  801. /* bpf_map_update_elem() can be called in_irq() */
  802. raw_spin_lock_irqsave(&b->lock, flags);
  803. l_old = lookup_elem_raw(head, hash, key, key_size);
  804. ret = check_flags(htab, l_old, map_flags);
  805. if (ret)
  806. goto err;
  807. if (l_old) {
  808. /* per-cpu hash map can update value in-place */
  809. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  810. value, onallcpus);
  811. } else {
  812. l_new = alloc_htab_elem(htab, key, value, key_size,
  813. hash, true, onallcpus, NULL);
  814. if (IS_ERR(l_new)) {
  815. ret = PTR_ERR(l_new);
  816. goto err;
  817. }
  818. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  819. }
  820. ret = 0;
  821. err:
  822. raw_spin_unlock_irqrestore(&b->lock, flags);
  823. return ret;
  824. }
  825. static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  826. void *value, u64 map_flags,
  827. bool onallcpus)
  828. {
  829. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  830. struct htab_elem *l_new = NULL, *l_old;
  831. struct hlist_nulls_head *head;
  832. unsigned long flags;
  833. struct bucket *b;
  834. u32 key_size, hash;
  835. int ret;
  836. if (unlikely(map_flags > BPF_EXIST))
  837. /* unknown flags */
  838. return -EINVAL;
  839. WARN_ON_ONCE(!rcu_read_lock_held());
  840. key_size = map->key_size;
  841. hash = htab_map_hash(key, key_size, htab->hashrnd);
  842. b = __select_bucket(htab, hash);
  843. head = &b->head;
  844. /* For LRU, we need to alloc before taking bucket's
  845. * spinlock because LRU's elem alloc may need
  846. * to remove older elem from htab and this removal
  847. * operation will need a bucket lock.
  848. */
  849. if (map_flags != BPF_EXIST) {
  850. l_new = prealloc_lru_pop(htab, key, hash);
  851. if (!l_new)
  852. return -ENOMEM;
  853. }
  854. /* bpf_map_update_elem() can be called in_irq() */
  855. raw_spin_lock_irqsave(&b->lock, flags);
  856. l_old = lookup_elem_raw(head, hash, key, key_size);
  857. ret = check_flags(htab, l_old, map_flags);
  858. if (ret)
  859. goto err;
  860. if (l_old) {
  861. bpf_lru_node_set_ref(&l_old->lru_node);
  862. /* per-cpu hash map can update value in-place */
  863. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  864. value, onallcpus);
  865. } else {
  866. pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
  867. value, onallcpus);
  868. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  869. l_new = NULL;
  870. }
  871. ret = 0;
  872. err:
  873. raw_spin_unlock_irqrestore(&b->lock, flags);
  874. if (l_new)
  875. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  876. return ret;
  877. }
  878. static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  879. void *value, u64 map_flags)
  880. {
  881. return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
  882. }
  883. static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  884. void *value, u64 map_flags)
  885. {
  886. return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
  887. false);
  888. }
  889. /* Called from syscall or from eBPF program */
  890. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  891. {
  892. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  893. struct hlist_nulls_head *head;
  894. struct bucket *b;
  895. struct htab_elem *l;
  896. unsigned long flags;
  897. u32 hash, key_size;
  898. int ret = -ENOENT;
  899. WARN_ON_ONCE(!rcu_read_lock_held());
  900. key_size = map->key_size;
  901. hash = htab_map_hash(key, key_size, htab->hashrnd);
  902. b = __select_bucket(htab, hash);
  903. head = &b->head;
  904. raw_spin_lock_irqsave(&b->lock, flags);
  905. l = lookup_elem_raw(head, hash, key, key_size);
  906. if (l) {
  907. hlist_nulls_del_rcu(&l->hash_node);
  908. free_htab_elem(htab, l);
  909. ret = 0;
  910. }
  911. raw_spin_unlock_irqrestore(&b->lock, flags);
  912. return ret;
  913. }
  914. static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
  915. {
  916. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  917. struct hlist_nulls_head *head;
  918. struct bucket *b;
  919. struct htab_elem *l;
  920. unsigned long flags;
  921. u32 hash, key_size;
  922. int ret = -ENOENT;
  923. WARN_ON_ONCE(!rcu_read_lock_held());
  924. key_size = map->key_size;
  925. hash = htab_map_hash(key, key_size, htab->hashrnd);
  926. b = __select_bucket(htab, hash);
  927. head = &b->head;
  928. raw_spin_lock_irqsave(&b->lock, flags);
  929. l = lookup_elem_raw(head, hash, key, key_size);
  930. if (l) {
  931. hlist_nulls_del_rcu(&l->hash_node);
  932. ret = 0;
  933. }
  934. raw_spin_unlock_irqrestore(&b->lock, flags);
  935. if (l)
  936. bpf_lru_push_free(&htab->lru, &l->lru_node);
  937. return ret;
  938. }
  939. static void delete_all_elements(struct bpf_htab *htab)
  940. {
  941. int i;
  942. for (i = 0; i < htab->n_buckets; i++) {
  943. struct hlist_nulls_head *head = select_bucket(htab, i);
  944. struct hlist_nulls_node *n;
  945. struct htab_elem *l;
  946. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  947. hlist_nulls_del_rcu(&l->hash_node);
  948. htab_elem_free(htab, l);
  949. }
  950. }
  951. }
  952. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  953. static void htab_map_free(struct bpf_map *map)
  954. {
  955. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  956. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  957. * so the programs (can be more than one that used this map) were
  958. * disconnected from events. Wait for outstanding critical sections in
  959. * these programs to complete
  960. */
  961. synchronize_rcu();
  962. /* some of free_htab_elem() callbacks for elements of this map may
  963. * not have executed. Wait for them.
  964. */
  965. rcu_barrier();
  966. if (!htab_is_prealloc(htab))
  967. delete_all_elements(htab);
  968. else
  969. prealloc_destroy(htab);
  970. free_percpu(htab->extra_elems);
  971. bpf_map_area_free(htab->buckets);
  972. kfree(htab);
  973. }
  974. static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
  975. struct seq_file *m)
  976. {
  977. void *value;
  978. rcu_read_lock();
  979. value = htab_map_lookup_elem(map, key);
  980. if (!value) {
  981. rcu_read_unlock();
  982. return;
  983. }
  984. btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
  985. seq_puts(m, ": ");
  986. btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
  987. seq_puts(m, "\n");
  988. rcu_read_unlock();
  989. }
  990. const struct bpf_map_ops htab_map_ops = {
  991. .map_alloc_check = htab_map_alloc_check,
  992. .map_alloc = htab_map_alloc,
  993. .map_free = htab_map_free,
  994. .map_get_next_key = htab_map_get_next_key,
  995. .map_lookup_elem = htab_map_lookup_elem,
  996. .map_update_elem = htab_map_update_elem,
  997. .map_delete_elem = htab_map_delete_elem,
  998. .map_gen_lookup = htab_map_gen_lookup,
  999. .map_seq_show_elem = htab_map_seq_show_elem,
  1000. };
  1001. const struct bpf_map_ops htab_lru_map_ops = {
  1002. .map_alloc_check = htab_map_alloc_check,
  1003. .map_alloc = htab_map_alloc,
  1004. .map_free = htab_map_free,
  1005. .map_get_next_key = htab_map_get_next_key,
  1006. .map_lookup_elem = htab_lru_map_lookup_elem,
  1007. .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
  1008. .map_update_elem = htab_lru_map_update_elem,
  1009. .map_delete_elem = htab_lru_map_delete_elem,
  1010. .map_gen_lookup = htab_lru_map_gen_lookup,
  1011. .map_seq_show_elem = htab_map_seq_show_elem,
  1012. };
  1013. /* Called from eBPF program */
  1014. static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  1015. {
  1016. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  1017. if (l)
  1018. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  1019. else
  1020. return NULL;
  1021. }
  1022. static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  1023. {
  1024. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  1025. if (l) {
  1026. bpf_lru_node_set_ref(&l->lru_node);
  1027. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  1028. }
  1029. return NULL;
  1030. }
  1031. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
  1032. {
  1033. struct htab_elem *l;
  1034. void __percpu *pptr;
  1035. int ret = -ENOENT;
  1036. int cpu, off = 0;
  1037. u32 size;
  1038. /* per_cpu areas are zero-filled and bpf programs can only
  1039. * access 'value_size' of them, so copying rounded areas
  1040. * will not leak any kernel data
  1041. */
  1042. size = round_up(map->value_size, 8);
  1043. rcu_read_lock();
  1044. l = __htab_map_lookup_elem(map, key);
  1045. if (!l)
  1046. goto out;
  1047. /* We do not mark LRU map element here in order to not mess up
  1048. * eviction heuristics when user space does a map walk.
  1049. */
  1050. pptr = htab_elem_get_ptr(l, map->key_size);
  1051. for_each_possible_cpu(cpu) {
  1052. bpf_long_memcpy(value + off,
  1053. per_cpu_ptr(pptr, cpu), size);
  1054. off += size;
  1055. }
  1056. ret = 0;
  1057. out:
  1058. rcu_read_unlock();
  1059. return ret;
  1060. }
  1061. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  1062. u64 map_flags)
  1063. {
  1064. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1065. int ret;
  1066. rcu_read_lock();
  1067. if (htab_is_lru(htab))
  1068. ret = __htab_lru_percpu_map_update_elem(map, key, value,
  1069. map_flags, true);
  1070. else
  1071. ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
  1072. true);
  1073. rcu_read_unlock();
  1074. return ret;
  1075. }
  1076. const struct bpf_map_ops htab_percpu_map_ops = {
  1077. .map_alloc_check = htab_map_alloc_check,
  1078. .map_alloc = htab_map_alloc,
  1079. .map_free = htab_map_free,
  1080. .map_get_next_key = htab_map_get_next_key,
  1081. .map_lookup_elem = htab_percpu_map_lookup_elem,
  1082. .map_update_elem = htab_percpu_map_update_elem,
  1083. .map_delete_elem = htab_map_delete_elem,
  1084. };
  1085. const struct bpf_map_ops htab_lru_percpu_map_ops = {
  1086. .map_alloc_check = htab_map_alloc_check,
  1087. .map_alloc = htab_map_alloc,
  1088. .map_free = htab_map_free,
  1089. .map_get_next_key = htab_map_get_next_key,
  1090. .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
  1091. .map_update_elem = htab_lru_percpu_map_update_elem,
  1092. .map_delete_elem = htab_lru_map_delete_elem,
  1093. };
  1094. static int fd_htab_map_alloc_check(union bpf_attr *attr)
  1095. {
  1096. if (attr->value_size != sizeof(u32))
  1097. return -EINVAL;
  1098. return htab_map_alloc_check(attr);
  1099. }
  1100. static void fd_htab_map_free(struct bpf_map *map)
  1101. {
  1102. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1103. struct hlist_nulls_node *n;
  1104. struct hlist_nulls_head *head;
  1105. struct htab_elem *l;
  1106. int i;
  1107. for (i = 0; i < htab->n_buckets; i++) {
  1108. head = select_bucket(htab, i);
  1109. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  1110. void *ptr = fd_htab_map_get_ptr(map, l);
  1111. map->ops->map_fd_put_ptr(ptr);
  1112. }
  1113. }
  1114. htab_map_free(map);
  1115. }
  1116. /* only called from syscall */
  1117. int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
  1118. {
  1119. void **ptr;
  1120. int ret = 0;
  1121. if (!map->ops->map_fd_sys_lookup_elem)
  1122. return -ENOTSUPP;
  1123. rcu_read_lock();
  1124. ptr = htab_map_lookup_elem(map, key);
  1125. if (ptr)
  1126. *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
  1127. else
  1128. ret = -ENOENT;
  1129. rcu_read_unlock();
  1130. return ret;
  1131. }
  1132. /* only called from syscall */
  1133. int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
  1134. void *key, void *value, u64 map_flags)
  1135. {
  1136. void *ptr;
  1137. int ret;
  1138. u32 ufd = *(u32 *)value;
  1139. ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  1140. if (IS_ERR(ptr))
  1141. return PTR_ERR(ptr);
  1142. ret = htab_map_update_elem(map, key, &ptr, map_flags);
  1143. if (ret)
  1144. map->ops->map_fd_put_ptr(ptr);
  1145. return ret;
  1146. }
  1147. static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
  1148. {
  1149. struct bpf_map *map, *inner_map_meta;
  1150. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  1151. if (IS_ERR(inner_map_meta))
  1152. return inner_map_meta;
  1153. map = htab_map_alloc(attr);
  1154. if (IS_ERR(map)) {
  1155. bpf_map_meta_free(inner_map_meta);
  1156. return map;
  1157. }
  1158. map->inner_map_meta = inner_map_meta;
  1159. return map;
  1160. }
  1161. static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
  1162. {
  1163. struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
  1164. if (!inner_map)
  1165. return NULL;
  1166. return READ_ONCE(*inner_map);
  1167. }
  1168. static u32 htab_of_map_gen_lookup(struct bpf_map *map,
  1169. struct bpf_insn *insn_buf)
  1170. {
  1171. struct bpf_insn *insn = insn_buf;
  1172. const int ret = BPF_REG_0;
  1173. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  1174. (void *(*)(struct bpf_map *map, void *key))NULL));
  1175. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  1176. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
  1177. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  1178. offsetof(struct htab_elem, key) +
  1179. round_up(map->key_size, 8));
  1180. *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
  1181. return insn - insn_buf;
  1182. }
  1183. static void htab_of_map_free(struct bpf_map *map)
  1184. {
  1185. bpf_map_meta_free(map->inner_map_meta);
  1186. fd_htab_map_free(map);
  1187. }
  1188. const struct bpf_map_ops htab_of_maps_map_ops = {
  1189. .map_alloc_check = fd_htab_map_alloc_check,
  1190. .map_alloc = htab_of_map_alloc,
  1191. .map_free = htab_of_map_free,
  1192. .map_get_next_key = htab_map_get_next_key,
  1193. .map_lookup_elem = htab_of_map_lookup_elem,
  1194. .map_delete_elem = htab_map_delete_elem,
  1195. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  1196. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  1197. .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
  1198. .map_gen_lookup = htab_of_map_gen_lookup,
  1199. .map_check_btf = map_check_no_btf,
  1200. };