hashtab.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  3. * Copyright (c) 2016 Facebook
  4. */
  5. #include <linux/bpf.h>
  6. #include <linux/btf.h>
  7. #include <linux/jhash.h>
  8. #include <linux/filter.h>
  9. #include <linux/rculist_nulls.h>
  10. #include <linux/random.h>
  11. #include <uapi/linux/btf.h>
  12. #include <linux/rcupdate_trace.h>
  13. #include "percpu_freelist.h"
  14. #include "bpf_lru_list.h"
  15. #include "map_in_map.h"
  16. #define HTAB_CREATE_FLAG_MASK \
  17. (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
  18. BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
  19. #define BATCH_OPS(_name) \
  20. .map_lookup_batch = \
  21. _name##_map_lookup_batch, \
  22. .map_lookup_and_delete_batch = \
  23. _name##_map_lookup_and_delete_batch, \
  24. .map_update_batch = \
  25. generic_map_update_batch, \
  26. .map_delete_batch = \
  27. generic_map_delete_batch
  28. /*
  29. * The bucket lock has two protection scopes:
  30. *
  31. * 1) Serializing concurrent operations from BPF programs on differrent
  32. * CPUs
  33. *
  34. * 2) Serializing concurrent operations from BPF programs and sys_bpf()
  35. *
  36. * BPF programs can execute in any context including perf, kprobes and
  37. * tracing. As there are almost no limits where perf, kprobes and tracing
  38. * can be invoked from the lock operations need to be protected against
  39. * deadlocks. Deadlocks can be caused by recursion and by an invocation in
  40. * the lock held section when functions which acquire this lock are invoked
  41. * from sys_bpf(). BPF recursion is prevented by incrementing the per CPU
  42. * variable bpf_prog_active, which prevents BPF programs attached to perf
  43. * events, kprobes and tracing to be invoked before the prior invocation
  44. * from one of these contexts completed. sys_bpf() uses the same mechanism
  45. * by pinning the task to the current CPU and incrementing the recursion
  46. * protection accross the map operation.
  47. *
  48. * This has subtle implications on PREEMPT_RT. PREEMPT_RT forbids certain
  49. * operations like memory allocations (even with GFP_ATOMIC) from atomic
  50. * contexts. This is required because even with GFP_ATOMIC the memory
  51. * allocator calls into code pathes which acquire locks with long held lock
  52. * sections. To ensure the deterministic behaviour these locks are regular
  53. * spinlocks, which are converted to 'sleepable' spinlocks on RT. The only
  54. * true atomic contexts on an RT kernel are the low level hardware
  55. * handling, scheduling, low level interrupt handling, NMIs etc. None of
  56. * these contexts should ever do memory allocations.
  57. *
  58. * As regular device interrupt handlers and soft interrupts are forced into
  59. * thread context, the existing code which does
  60. * spin_lock*(); alloc(GPF_ATOMIC); spin_unlock*();
  61. * just works.
  62. *
  63. * In theory the BPF locks could be converted to regular spinlocks as well,
  64. * but the bucket locks and percpu_freelist locks can be taken from
  65. * arbitrary contexts (perf, kprobes, tracepoints) which are required to be
  66. * atomic contexts even on RT. These mechanisms require preallocated maps,
  67. * so there is no need to invoke memory allocations within the lock held
  68. * sections.
  69. *
  70. * BPF maps which need dynamic allocation are only used from (forced)
  71. * thread context on RT and can therefore use regular spinlocks which in
  72. * turn allows to invoke memory allocations from the lock held section.
  73. *
  74. * On a non RT kernel this distinction is neither possible nor required.
  75. * spinlock maps to raw_spinlock and the extra code is optimized out by the
  76. * compiler.
  77. */
  78. struct bucket {
  79. struct hlist_nulls_head head;
  80. union {
  81. raw_spinlock_t raw_lock;
  82. spinlock_t lock;
  83. };
  84. };
  85. struct bpf_htab {
  86. struct bpf_map map;
  87. struct bucket *buckets;
  88. void *elems;
  89. union {
  90. struct pcpu_freelist freelist;
  91. struct bpf_lru lru;
  92. };
  93. struct htab_elem *__percpu *extra_elems;
  94. atomic_t count; /* number of elements in this hashtable */
  95. u32 n_buckets; /* number of hash buckets */
  96. u32 elem_size; /* size of each element in bytes */
  97. u32 hashrnd;
  98. };
  99. /* each htab element is struct htab_elem + key + value */
  100. struct htab_elem {
  101. union {
  102. struct hlist_nulls_node hash_node;
  103. struct {
  104. void *padding;
  105. union {
  106. struct bpf_htab *htab;
  107. struct pcpu_freelist_node fnode;
  108. struct htab_elem *batch_flink;
  109. };
  110. };
  111. };
  112. union {
  113. struct rcu_head rcu;
  114. struct bpf_lru_node lru_node;
  115. };
  116. u32 hash;
  117. char key[] __aligned(8);
  118. };
  119. static inline bool htab_is_prealloc(const struct bpf_htab *htab)
  120. {
  121. return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
  122. }
  123. static inline bool htab_use_raw_lock(const struct bpf_htab *htab)
  124. {
  125. return (!IS_ENABLED(CONFIG_PREEMPT_RT) || htab_is_prealloc(htab));
  126. }
  127. static void htab_init_buckets(struct bpf_htab *htab)
  128. {
  129. unsigned i;
  130. for (i = 0; i < htab->n_buckets; i++) {
  131. INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
  132. if (htab_use_raw_lock(htab))
  133. raw_spin_lock_init(&htab->buckets[i].raw_lock);
  134. else
  135. spin_lock_init(&htab->buckets[i].lock);
  136. }
  137. }
  138. static inline unsigned long htab_lock_bucket(const struct bpf_htab *htab,
  139. struct bucket *b)
  140. {
  141. unsigned long flags;
  142. if (htab_use_raw_lock(htab))
  143. raw_spin_lock_irqsave(&b->raw_lock, flags);
  144. else
  145. spin_lock_irqsave(&b->lock, flags);
  146. return flags;
  147. }
  148. static inline void htab_unlock_bucket(const struct bpf_htab *htab,
  149. struct bucket *b,
  150. unsigned long flags)
  151. {
  152. if (htab_use_raw_lock(htab))
  153. raw_spin_unlock_irqrestore(&b->raw_lock, flags);
  154. else
  155. spin_unlock_irqrestore(&b->lock, flags);
  156. }
  157. static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
  158. static bool htab_is_lru(const struct bpf_htab *htab)
  159. {
  160. return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
  161. htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
  162. }
  163. static bool htab_is_percpu(const struct bpf_htab *htab)
  164. {
  165. return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  166. htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
  167. }
  168. static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
  169. void __percpu *pptr)
  170. {
  171. *(void __percpu **)(l->key + key_size) = pptr;
  172. }
  173. static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
  174. {
  175. return *(void __percpu **)(l->key + key_size);
  176. }
  177. static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
  178. {
  179. return *(void **)(l->key + roundup(map->key_size, 8));
  180. }
  181. static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
  182. {
  183. return (struct htab_elem *) (htab->elems + i * htab->elem_size);
  184. }
  185. static void htab_free_elems(struct bpf_htab *htab)
  186. {
  187. int i;
  188. if (!htab_is_percpu(htab))
  189. goto free_elems;
  190. for (i = 0; i < htab->map.max_entries; i++) {
  191. void __percpu *pptr;
  192. pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
  193. htab->map.key_size);
  194. free_percpu(pptr);
  195. cond_resched();
  196. }
  197. free_elems:
  198. bpf_map_area_free(htab->elems);
  199. }
  200. /* The LRU list has a lock (lru_lock). Each htab bucket has a lock
  201. * (bucket_lock). If both locks need to be acquired together, the lock
  202. * order is always lru_lock -> bucket_lock and this only happens in
  203. * bpf_lru_list.c logic. For example, certain code path of
  204. * bpf_lru_pop_free(), which is called by function prealloc_lru_pop(),
  205. * will acquire lru_lock first followed by acquiring bucket_lock.
  206. *
  207. * In hashtab.c, to avoid deadlock, lock acquisition of
  208. * bucket_lock followed by lru_lock is not allowed. In such cases,
  209. * bucket_lock needs to be released first before acquiring lru_lock.
  210. */
  211. static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
  212. u32 hash)
  213. {
  214. struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
  215. struct htab_elem *l;
  216. if (node) {
  217. l = container_of(node, struct htab_elem, lru_node);
  218. memcpy(l->key, key, htab->map.key_size);
  219. return l;
  220. }
  221. return NULL;
  222. }
  223. static int prealloc_init(struct bpf_htab *htab)
  224. {
  225. u32 num_entries = htab->map.max_entries;
  226. int err = -ENOMEM, i;
  227. if (!htab_is_percpu(htab) && !htab_is_lru(htab))
  228. num_entries += num_possible_cpus();
  229. htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
  230. htab->map.numa_node);
  231. if (!htab->elems)
  232. return -ENOMEM;
  233. if (!htab_is_percpu(htab))
  234. goto skip_percpu_elems;
  235. for (i = 0; i < num_entries; i++) {
  236. u32 size = round_up(htab->map.value_size, 8);
  237. void __percpu *pptr;
  238. pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
  239. if (!pptr)
  240. goto free_elems;
  241. htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
  242. pptr);
  243. cond_resched();
  244. }
  245. skip_percpu_elems:
  246. if (htab_is_lru(htab))
  247. err = bpf_lru_init(&htab->lru,
  248. htab->map.map_flags & BPF_F_NO_COMMON_LRU,
  249. offsetof(struct htab_elem, hash) -
  250. offsetof(struct htab_elem, lru_node),
  251. htab_lru_map_delete_node,
  252. htab);
  253. else
  254. err = pcpu_freelist_init(&htab->freelist);
  255. if (err)
  256. goto free_elems;
  257. if (htab_is_lru(htab))
  258. bpf_lru_populate(&htab->lru, htab->elems,
  259. offsetof(struct htab_elem, lru_node),
  260. htab->elem_size, num_entries);
  261. else
  262. pcpu_freelist_populate(&htab->freelist,
  263. htab->elems + offsetof(struct htab_elem, fnode),
  264. htab->elem_size, num_entries);
  265. return 0;
  266. free_elems:
  267. htab_free_elems(htab);
  268. return err;
  269. }
  270. static void prealloc_destroy(struct bpf_htab *htab)
  271. {
  272. htab_free_elems(htab);
  273. if (htab_is_lru(htab))
  274. bpf_lru_destroy(&htab->lru);
  275. else
  276. pcpu_freelist_destroy(&htab->freelist);
  277. }
  278. static int alloc_extra_elems(struct bpf_htab *htab)
  279. {
  280. struct htab_elem *__percpu *pptr, *l_new;
  281. struct pcpu_freelist_node *l;
  282. int cpu;
  283. pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
  284. GFP_USER | __GFP_NOWARN);
  285. if (!pptr)
  286. return -ENOMEM;
  287. for_each_possible_cpu(cpu) {
  288. l = pcpu_freelist_pop(&htab->freelist);
  289. /* pop will succeed, since prealloc_init()
  290. * preallocated extra num_possible_cpus elements
  291. */
  292. l_new = container_of(l, struct htab_elem, fnode);
  293. *per_cpu_ptr(pptr, cpu) = l_new;
  294. }
  295. htab->extra_elems = pptr;
  296. return 0;
  297. }
  298. /* Called from syscall */
  299. static int htab_map_alloc_check(union bpf_attr *attr)
  300. {
  301. bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  302. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  303. bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
  304. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  305. /* percpu_lru means each cpu has its own LRU list.
  306. * it is different from BPF_MAP_TYPE_PERCPU_HASH where
  307. * the map's value itself is percpu. percpu_lru has
  308. * nothing to do with the map's value.
  309. */
  310. bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
  311. bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
  312. bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
  313. int numa_node = bpf_map_attr_numa_node(attr);
  314. BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
  315. offsetof(struct htab_elem, hash_node.pprev));
  316. BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
  317. offsetof(struct htab_elem, hash_node.pprev));
  318. if (lru && !bpf_capable())
  319. /* LRU implementation is much complicated than other
  320. * maps. Hence, limit to CAP_BPF.
  321. */
  322. return -EPERM;
  323. if (zero_seed && !capable(CAP_SYS_ADMIN))
  324. /* Guard against local DoS, and discourage production use. */
  325. return -EPERM;
  326. if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
  327. !bpf_map_flags_access_ok(attr->map_flags))
  328. return -EINVAL;
  329. if (!lru && percpu_lru)
  330. return -EINVAL;
  331. if (lru && !prealloc)
  332. return -ENOTSUPP;
  333. if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
  334. return -EINVAL;
  335. /* check sanity of attributes.
  336. * value_size == 0 may be allowed in the future to use map as a set
  337. */
  338. if (attr->max_entries == 0 || attr->key_size == 0 ||
  339. attr->value_size == 0)
  340. return -EINVAL;
  341. if (attr->key_size > MAX_BPF_STACK)
  342. /* eBPF programs initialize keys on stack, so they cannot be
  343. * larger than max stack size
  344. */
  345. return -E2BIG;
  346. if (attr->value_size >= KMALLOC_MAX_SIZE -
  347. MAX_BPF_STACK - sizeof(struct htab_elem))
  348. /* if value_size is bigger, the user space won't be able to
  349. * access the elements via bpf syscall. This check also makes
  350. * sure that the elem_size doesn't overflow and it's
  351. * kmalloc-able later in htab_map_update_elem()
  352. */
  353. return -E2BIG;
  354. return 0;
  355. }
  356. static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
  357. {
  358. bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  359. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  360. bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
  361. attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
  362. /* percpu_lru means each cpu has its own LRU list.
  363. * it is different from BPF_MAP_TYPE_PERCPU_HASH where
  364. * the map's value itself is percpu. percpu_lru has
  365. * nothing to do with the map's value.
  366. */
  367. bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
  368. bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
  369. struct bpf_htab *htab;
  370. u64 cost;
  371. int err;
  372. htab = kzalloc(sizeof(*htab), GFP_USER);
  373. if (!htab)
  374. return ERR_PTR(-ENOMEM);
  375. bpf_map_init_from_attr(&htab->map, attr);
  376. if (percpu_lru) {
  377. /* ensure each CPU's lru list has >=1 elements.
  378. * since we are at it, make each lru list has the same
  379. * number of elements.
  380. */
  381. htab->map.max_entries = roundup(attr->max_entries,
  382. num_possible_cpus());
  383. if (htab->map.max_entries < attr->max_entries)
  384. htab->map.max_entries = rounddown(attr->max_entries,
  385. num_possible_cpus());
  386. }
  387. /* hash table size must be power of 2 */
  388. htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
  389. htab->elem_size = sizeof(struct htab_elem) +
  390. round_up(htab->map.key_size, 8);
  391. if (percpu)
  392. htab->elem_size += sizeof(void *);
  393. else
  394. htab->elem_size += round_up(htab->map.value_size, 8);
  395. err = -E2BIG;
  396. /* prevent zero size kmalloc and check for u32 overflow */
  397. if (htab->n_buckets == 0 ||
  398. htab->n_buckets > U32_MAX / sizeof(struct bucket))
  399. goto free_htab;
  400. cost = (u64) htab->n_buckets * sizeof(struct bucket) +
  401. (u64) htab->elem_size * htab->map.max_entries;
  402. if (percpu)
  403. cost += (u64) round_up(htab->map.value_size, 8) *
  404. num_possible_cpus() * htab->map.max_entries;
  405. else
  406. cost += (u64) htab->elem_size * num_possible_cpus();
  407. /* if map size is larger than memlock limit, reject it */
  408. err = bpf_map_charge_init(&htab->map.memory, cost);
  409. if (err)
  410. goto free_htab;
  411. err = -ENOMEM;
  412. htab->buckets = bpf_map_area_alloc(htab->n_buckets *
  413. sizeof(struct bucket),
  414. htab->map.numa_node);
  415. if (!htab->buckets)
  416. goto free_charge;
  417. if (htab->map.map_flags & BPF_F_ZERO_SEED)
  418. htab->hashrnd = 0;
  419. else
  420. htab->hashrnd = get_random_int();
  421. htab_init_buckets(htab);
  422. if (prealloc) {
  423. err = prealloc_init(htab);
  424. if (err)
  425. goto free_buckets;
  426. if (!percpu && !lru) {
  427. /* lru itself can remove the least used element, so
  428. * there is no need for an extra elem during map_update.
  429. */
  430. err = alloc_extra_elems(htab);
  431. if (err)
  432. goto free_prealloc;
  433. }
  434. }
  435. return &htab->map;
  436. free_prealloc:
  437. prealloc_destroy(htab);
  438. free_buckets:
  439. bpf_map_area_free(htab->buckets);
  440. free_charge:
  441. bpf_map_charge_finish(&htab->map.memory);
  442. free_htab:
  443. kfree(htab);
  444. return ERR_PTR(err);
  445. }
  446. static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
  447. {
  448. return jhash(key, key_len, hashrnd);
  449. }
  450. static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
  451. {
  452. return &htab->buckets[hash & (htab->n_buckets - 1)];
  453. }
  454. static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
  455. {
  456. return &__select_bucket(htab, hash)->head;
  457. }
  458. /* this lookup function can only be called with bucket lock taken */
  459. static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
  460. void *key, u32 key_size)
  461. {
  462. struct hlist_nulls_node *n;
  463. struct htab_elem *l;
  464. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  465. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  466. return l;
  467. return NULL;
  468. }
  469. /* can be called without bucket lock. it will repeat the loop in
  470. * the unlikely event when elements moved from one bucket into another
  471. * while link list is being walked
  472. */
  473. static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
  474. u32 hash, void *key,
  475. u32 key_size, u32 n_buckets)
  476. {
  477. struct hlist_nulls_node *n;
  478. struct htab_elem *l;
  479. again:
  480. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  481. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  482. return l;
  483. if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
  484. goto again;
  485. return NULL;
  486. }
  487. /* Called from syscall or from eBPF program directly, so
  488. * arguments have to match bpf_map_lookup_elem() exactly.
  489. * The return value is adjusted by BPF instructions
  490. * in htab_map_gen_lookup().
  491. */
  492. static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
  493. {
  494. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  495. struct hlist_nulls_head *head;
  496. struct htab_elem *l;
  497. u32 hash, key_size;
  498. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
  499. key_size = map->key_size;
  500. hash = htab_map_hash(key, key_size, htab->hashrnd);
  501. head = select_bucket(htab, hash);
  502. l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
  503. return l;
  504. }
  505. static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
  506. {
  507. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  508. if (l)
  509. return l->key + round_up(map->key_size, 8);
  510. return NULL;
  511. }
  512. /* inline bpf_map_lookup_elem() call.
  513. * Instead of:
  514. * bpf_prog
  515. * bpf_map_lookup_elem
  516. * map->ops->map_lookup_elem
  517. * htab_map_lookup_elem
  518. * __htab_map_lookup_elem
  519. * do:
  520. * bpf_prog
  521. * __htab_map_lookup_elem
  522. */
  523. static int htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
  524. {
  525. struct bpf_insn *insn = insn_buf;
  526. const int ret = BPF_REG_0;
  527. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  528. (void *(*)(struct bpf_map *map, void *key))NULL));
  529. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  530. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
  531. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  532. offsetof(struct htab_elem, key) +
  533. round_up(map->key_size, 8));
  534. return insn - insn_buf;
  535. }
  536. static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
  537. void *key, const bool mark)
  538. {
  539. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  540. if (l) {
  541. if (mark)
  542. bpf_lru_node_set_ref(&l->lru_node);
  543. return l->key + round_up(map->key_size, 8);
  544. }
  545. return NULL;
  546. }
  547. static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
  548. {
  549. return __htab_lru_map_lookup_elem(map, key, true);
  550. }
  551. static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
  552. {
  553. return __htab_lru_map_lookup_elem(map, key, false);
  554. }
  555. static int htab_lru_map_gen_lookup(struct bpf_map *map,
  556. struct bpf_insn *insn_buf)
  557. {
  558. struct bpf_insn *insn = insn_buf;
  559. const int ret = BPF_REG_0;
  560. const int ref_reg = BPF_REG_1;
  561. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  562. (void *(*)(struct bpf_map *map, void *key))NULL));
  563. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  564. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
  565. *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
  566. offsetof(struct htab_elem, lru_node) +
  567. offsetof(struct bpf_lru_node, ref));
  568. *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
  569. *insn++ = BPF_ST_MEM(BPF_B, ret,
  570. offsetof(struct htab_elem, lru_node) +
  571. offsetof(struct bpf_lru_node, ref),
  572. 1);
  573. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  574. offsetof(struct htab_elem, key) +
  575. round_up(map->key_size, 8));
  576. return insn - insn_buf;
  577. }
  578. /* It is called from the bpf_lru_list when the LRU needs to delete
  579. * older elements from the htab.
  580. */
  581. static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
  582. {
  583. struct bpf_htab *htab = (struct bpf_htab *)arg;
  584. struct htab_elem *l = NULL, *tgt_l;
  585. struct hlist_nulls_head *head;
  586. struct hlist_nulls_node *n;
  587. unsigned long flags;
  588. struct bucket *b;
  589. tgt_l = container_of(node, struct htab_elem, lru_node);
  590. b = __select_bucket(htab, tgt_l->hash);
  591. head = &b->head;
  592. flags = htab_lock_bucket(htab, b);
  593. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  594. if (l == tgt_l) {
  595. hlist_nulls_del_rcu(&l->hash_node);
  596. break;
  597. }
  598. htab_unlock_bucket(htab, b, flags);
  599. return l == tgt_l;
  600. }
  601. /* Called from syscall */
  602. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  603. {
  604. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  605. struct hlist_nulls_head *head;
  606. struct htab_elem *l, *next_l;
  607. u32 hash, key_size;
  608. int i = 0;
  609. WARN_ON_ONCE(!rcu_read_lock_held());
  610. key_size = map->key_size;
  611. if (!key)
  612. goto find_first_elem;
  613. hash = htab_map_hash(key, key_size, htab->hashrnd);
  614. head = select_bucket(htab, hash);
  615. /* lookup the key */
  616. l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
  617. if (!l)
  618. goto find_first_elem;
  619. /* key was found, get next key in the same bucket */
  620. next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
  621. struct htab_elem, hash_node);
  622. if (next_l) {
  623. /* if next elem in this hash list is non-zero, just return it */
  624. memcpy(next_key, next_l->key, key_size);
  625. return 0;
  626. }
  627. /* no more elements in this hash list, go to the next bucket */
  628. i = hash & (htab->n_buckets - 1);
  629. i++;
  630. find_first_elem:
  631. /* iterate over buckets */
  632. for (; i < htab->n_buckets; i++) {
  633. head = select_bucket(htab, i);
  634. /* pick first element in the bucket */
  635. next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
  636. struct htab_elem, hash_node);
  637. if (next_l) {
  638. /* if it's not empty, just return it */
  639. memcpy(next_key, next_l->key, key_size);
  640. return 0;
  641. }
  642. }
  643. /* iterated over all buckets and all elements */
  644. return -ENOENT;
  645. }
  646. static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
  647. {
  648. if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
  649. free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
  650. kfree(l);
  651. }
  652. static void htab_elem_free_rcu(struct rcu_head *head)
  653. {
  654. struct htab_elem *l = container_of(head, struct htab_elem, rcu);
  655. struct bpf_htab *htab = l->htab;
  656. htab_elem_free(htab, l);
  657. }
  658. static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l)
  659. {
  660. struct bpf_map *map = &htab->map;
  661. void *ptr;
  662. if (map->ops->map_fd_put_ptr) {
  663. ptr = fd_htab_map_get_ptr(map, l);
  664. map->ops->map_fd_put_ptr(ptr);
  665. }
  666. }
  667. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  668. {
  669. htab_put_fd_value(htab, l);
  670. if (htab_is_prealloc(htab)) {
  671. __pcpu_freelist_push(&htab->freelist, &l->fnode);
  672. } else {
  673. atomic_dec(&htab->count);
  674. l->htab = htab;
  675. call_rcu(&l->rcu, htab_elem_free_rcu);
  676. }
  677. }
  678. static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
  679. void *value, bool onallcpus)
  680. {
  681. if (!onallcpus) {
  682. /* copy true value_size bytes */
  683. memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
  684. } else {
  685. u32 size = round_up(htab->map.value_size, 8);
  686. int off = 0, cpu;
  687. for_each_possible_cpu(cpu) {
  688. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  689. value + off, size);
  690. off += size;
  691. }
  692. }
  693. }
  694. static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr,
  695. void *value, bool onallcpus)
  696. {
  697. /* When using prealloc and not setting the initial value on all cpus,
  698. * zero-fill element values for other cpus (just as what happens when
  699. * not using prealloc). Otherwise, bpf program has no way to ensure
  700. * known initial values for cpus other than current one
  701. * (onallcpus=false always when coming from bpf prog).
  702. */
  703. if (htab_is_prealloc(htab) && !onallcpus) {
  704. u32 size = round_up(htab->map.value_size, 8);
  705. int current_cpu = raw_smp_processor_id();
  706. int cpu;
  707. for_each_possible_cpu(cpu) {
  708. if (cpu == current_cpu)
  709. bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value,
  710. size);
  711. else
  712. memset(per_cpu_ptr(pptr, cpu), 0, size);
  713. }
  714. } else {
  715. pcpu_copy_value(htab, pptr, value, onallcpus);
  716. }
  717. }
  718. static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
  719. {
  720. return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
  721. BITS_PER_LONG == 64;
  722. }
  723. static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
  724. void *value, u32 key_size, u32 hash,
  725. bool percpu, bool onallcpus,
  726. struct htab_elem *old_elem)
  727. {
  728. u32 size = htab->map.value_size;
  729. bool prealloc = htab_is_prealloc(htab);
  730. struct htab_elem *l_new, **pl_new;
  731. void __percpu *pptr;
  732. if (prealloc) {
  733. if (old_elem) {
  734. /* if we're updating the existing element,
  735. * use per-cpu extra elems to avoid freelist_pop/push
  736. */
  737. pl_new = this_cpu_ptr(htab->extra_elems);
  738. l_new = *pl_new;
  739. htab_put_fd_value(htab, old_elem);
  740. *pl_new = old_elem;
  741. } else {
  742. struct pcpu_freelist_node *l;
  743. l = __pcpu_freelist_pop(&htab->freelist);
  744. if (!l)
  745. return ERR_PTR(-E2BIG);
  746. l_new = container_of(l, struct htab_elem, fnode);
  747. }
  748. } else {
  749. if (atomic_inc_return(&htab->count) > htab->map.max_entries)
  750. if (!old_elem) {
  751. /* when map is full and update() is replacing
  752. * old element, it's ok to allocate, since
  753. * old element will be freed immediately.
  754. * Otherwise return an error
  755. */
  756. l_new = ERR_PTR(-E2BIG);
  757. goto dec_count;
  758. }
  759. l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
  760. htab->map.numa_node);
  761. if (!l_new) {
  762. l_new = ERR_PTR(-ENOMEM);
  763. goto dec_count;
  764. }
  765. check_and_init_map_lock(&htab->map,
  766. l_new->key + round_up(key_size, 8));
  767. }
  768. memcpy(l_new->key, key, key_size);
  769. if (percpu) {
  770. size = round_up(size, 8);
  771. if (prealloc) {
  772. pptr = htab_elem_get_ptr(l_new, key_size);
  773. } else {
  774. /* alloc_percpu zero-fills */
  775. pptr = __alloc_percpu_gfp(size, 8,
  776. GFP_ATOMIC | __GFP_NOWARN);
  777. if (!pptr) {
  778. kfree(l_new);
  779. l_new = ERR_PTR(-ENOMEM);
  780. goto dec_count;
  781. }
  782. }
  783. pcpu_init_value(htab, pptr, value, onallcpus);
  784. if (!prealloc)
  785. htab_elem_set_ptr(l_new, key_size, pptr);
  786. } else if (fd_htab_map_needs_adjust(htab)) {
  787. size = round_up(size, 8);
  788. memcpy(l_new->key + round_up(key_size, 8), value, size);
  789. } else {
  790. copy_map_value(&htab->map,
  791. l_new->key + round_up(key_size, 8),
  792. value);
  793. }
  794. l_new->hash = hash;
  795. return l_new;
  796. dec_count:
  797. atomic_dec(&htab->count);
  798. return l_new;
  799. }
  800. static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  801. u64 map_flags)
  802. {
  803. if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
  804. /* elem already exists */
  805. return -EEXIST;
  806. if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
  807. /* elem doesn't exist, cannot update it */
  808. return -ENOENT;
  809. return 0;
  810. }
  811. /* Called from syscall or from eBPF program */
  812. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  813. u64 map_flags)
  814. {
  815. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  816. struct htab_elem *l_new = NULL, *l_old;
  817. struct hlist_nulls_head *head;
  818. unsigned long flags;
  819. struct bucket *b;
  820. u32 key_size, hash;
  821. int ret;
  822. if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
  823. /* unknown flags */
  824. return -EINVAL;
  825. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
  826. key_size = map->key_size;
  827. hash = htab_map_hash(key, key_size, htab->hashrnd);
  828. b = __select_bucket(htab, hash);
  829. head = &b->head;
  830. if (unlikely(map_flags & BPF_F_LOCK)) {
  831. if (unlikely(!map_value_has_spin_lock(map)))
  832. return -EINVAL;
  833. /* find an element without taking the bucket lock */
  834. l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
  835. htab->n_buckets);
  836. ret = check_flags(htab, l_old, map_flags);
  837. if (ret)
  838. return ret;
  839. if (l_old) {
  840. /* grab the element lock and update value in place */
  841. copy_map_value_locked(map,
  842. l_old->key + round_up(key_size, 8),
  843. value, false);
  844. return 0;
  845. }
  846. /* fall through, grab the bucket lock and lookup again.
  847. * 99.9% chance that the element won't be found,
  848. * but second lookup under lock has to be done.
  849. */
  850. }
  851. flags = htab_lock_bucket(htab, b);
  852. l_old = lookup_elem_raw(head, hash, key, key_size);
  853. ret = check_flags(htab, l_old, map_flags);
  854. if (ret)
  855. goto err;
  856. if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
  857. /* first lookup without the bucket lock didn't find the element,
  858. * but second lookup with the bucket lock found it.
  859. * This case is highly unlikely, but has to be dealt with:
  860. * grab the element lock in addition to the bucket lock
  861. * and update element in place
  862. */
  863. copy_map_value_locked(map,
  864. l_old->key + round_up(key_size, 8),
  865. value, false);
  866. ret = 0;
  867. goto err;
  868. }
  869. l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
  870. l_old);
  871. if (IS_ERR(l_new)) {
  872. /* all pre-allocated elements are in use or memory exhausted */
  873. ret = PTR_ERR(l_new);
  874. goto err;
  875. }
  876. /* add new element to the head of the list, so that
  877. * concurrent search will find it before old elem
  878. */
  879. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  880. if (l_old) {
  881. hlist_nulls_del_rcu(&l_old->hash_node);
  882. if (!htab_is_prealloc(htab))
  883. free_htab_elem(htab, l_old);
  884. }
  885. ret = 0;
  886. err:
  887. htab_unlock_bucket(htab, b, flags);
  888. return ret;
  889. }
  890. static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
  891. u64 map_flags)
  892. {
  893. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  894. struct htab_elem *l_new, *l_old = NULL;
  895. struct hlist_nulls_head *head;
  896. unsigned long flags;
  897. struct bucket *b;
  898. u32 key_size, hash;
  899. int ret;
  900. if (unlikely(map_flags > BPF_EXIST))
  901. /* unknown flags */
  902. return -EINVAL;
  903. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
  904. key_size = map->key_size;
  905. hash = htab_map_hash(key, key_size, htab->hashrnd);
  906. b = __select_bucket(htab, hash);
  907. head = &b->head;
  908. /* For LRU, we need to alloc before taking bucket's
  909. * spinlock because getting free nodes from LRU may need
  910. * to remove older elements from htab and this removal
  911. * operation will need a bucket lock.
  912. */
  913. l_new = prealloc_lru_pop(htab, key, hash);
  914. if (!l_new)
  915. return -ENOMEM;
  916. memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
  917. flags = htab_lock_bucket(htab, b);
  918. l_old = lookup_elem_raw(head, hash, key, key_size);
  919. ret = check_flags(htab, l_old, map_flags);
  920. if (ret)
  921. goto err;
  922. /* add new element to the head of the list, so that
  923. * concurrent search will find it before old elem
  924. */
  925. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  926. if (l_old) {
  927. bpf_lru_node_set_ref(&l_new->lru_node);
  928. hlist_nulls_del_rcu(&l_old->hash_node);
  929. }
  930. ret = 0;
  931. err:
  932. htab_unlock_bucket(htab, b, flags);
  933. if (ret)
  934. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  935. else if (l_old)
  936. bpf_lru_push_free(&htab->lru, &l_old->lru_node);
  937. return ret;
  938. }
  939. static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  940. void *value, u64 map_flags,
  941. bool onallcpus)
  942. {
  943. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  944. struct htab_elem *l_new = NULL, *l_old;
  945. struct hlist_nulls_head *head;
  946. unsigned long flags;
  947. struct bucket *b;
  948. u32 key_size, hash;
  949. int ret;
  950. if (unlikely(map_flags > BPF_EXIST))
  951. /* unknown flags */
  952. return -EINVAL;
  953. WARN_ON_ONCE(!rcu_read_lock_held());
  954. key_size = map->key_size;
  955. hash = htab_map_hash(key, key_size, htab->hashrnd);
  956. b = __select_bucket(htab, hash);
  957. head = &b->head;
  958. flags = htab_lock_bucket(htab, b);
  959. l_old = lookup_elem_raw(head, hash, key, key_size);
  960. ret = check_flags(htab, l_old, map_flags);
  961. if (ret)
  962. goto err;
  963. if (l_old) {
  964. /* per-cpu hash map can update value in-place */
  965. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  966. value, onallcpus);
  967. } else {
  968. l_new = alloc_htab_elem(htab, key, value, key_size,
  969. hash, true, onallcpus, NULL);
  970. if (IS_ERR(l_new)) {
  971. ret = PTR_ERR(l_new);
  972. goto err;
  973. }
  974. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  975. }
  976. ret = 0;
  977. err:
  978. htab_unlock_bucket(htab, b, flags);
  979. return ret;
  980. }
  981. static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  982. void *value, u64 map_flags,
  983. bool onallcpus)
  984. {
  985. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  986. struct htab_elem *l_new = NULL, *l_old;
  987. struct hlist_nulls_head *head;
  988. unsigned long flags;
  989. struct bucket *b;
  990. u32 key_size, hash;
  991. int ret;
  992. if (unlikely(map_flags > BPF_EXIST))
  993. /* unknown flags */
  994. return -EINVAL;
  995. WARN_ON_ONCE(!rcu_read_lock_held());
  996. key_size = map->key_size;
  997. hash = htab_map_hash(key, key_size, htab->hashrnd);
  998. b = __select_bucket(htab, hash);
  999. head = &b->head;
  1000. /* For LRU, we need to alloc before taking bucket's
  1001. * spinlock because LRU's elem alloc may need
  1002. * to remove older elem from htab and this removal
  1003. * operation will need a bucket lock.
  1004. */
  1005. if (map_flags != BPF_EXIST) {
  1006. l_new = prealloc_lru_pop(htab, key, hash);
  1007. if (!l_new)
  1008. return -ENOMEM;
  1009. }
  1010. flags = htab_lock_bucket(htab, b);
  1011. l_old = lookup_elem_raw(head, hash, key, key_size);
  1012. ret = check_flags(htab, l_old, map_flags);
  1013. if (ret)
  1014. goto err;
  1015. if (l_old) {
  1016. bpf_lru_node_set_ref(&l_old->lru_node);
  1017. /* per-cpu hash map can update value in-place */
  1018. pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
  1019. value, onallcpus);
  1020. } else {
  1021. pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size),
  1022. value, onallcpus);
  1023. hlist_nulls_add_head_rcu(&l_new->hash_node, head);
  1024. l_new = NULL;
  1025. }
  1026. ret = 0;
  1027. err:
  1028. htab_unlock_bucket(htab, b, flags);
  1029. if (l_new)
  1030. bpf_lru_push_free(&htab->lru, &l_new->lru_node);
  1031. return ret;
  1032. }
  1033. static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  1034. void *value, u64 map_flags)
  1035. {
  1036. return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
  1037. }
  1038. static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
  1039. void *value, u64 map_flags)
  1040. {
  1041. return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
  1042. false);
  1043. }
  1044. /* Called from syscall or from eBPF program */
  1045. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  1046. {
  1047. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1048. struct hlist_nulls_head *head;
  1049. struct bucket *b;
  1050. struct htab_elem *l;
  1051. unsigned long flags;
  1052. u32 hash, key_size;
  1053. int ret = -ENOENT;
  1054. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
  1055. key_size = map->key_size;
  1056. hash = htab_map_hash(key, key_size, htab->hashrnd);
  1057. b = __select_bucket(htab, hash);
  1058. head = &b->head;
  1059. flags = htab_lock_bucket(htab, b);
  1060. l = lookup_elem_raw(head, hash, key, key_size);
  1061. if (l) {
  1062. hlist_nulls_del_rcu(&l->hash_node);
  1063. free_htab_elem(htab, l);
  1064. ret = 0;
  1065. }
  1066. htab_unlock_bucket(htab, b, flags);
  1067. return ret;
  1068. }
  1069. static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
  1070. {
  1071. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1072. struct hlist_nulls_head *head;
  1073. struct bucket *b;
  1074. struct htab_elem *l;
  1075. unsigned long flags;
  1076. u32 hash, key_size;
  1077. int ret = -ENOENT;
  1078. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
  1079. key_size = map->key_size;
  1080. hash = htab_map_hash(key, key_size, htab->hashrnd);
  1081. b = __select_bucket(htab, hash);
  1082. head = &b->head;
  1083. flags = htab_lock_bucket(htab, b);
  1084. l = lookup_elem_raw(head, hash, key, key_size);
  1085. if (l) {
  1086. hlist_nulls_del_rcu(&l->hash_node);
  1087. ret = 0;
  1088. }
  1089. htab_unlock_bucket(htab, b, flags);
  1090. if (l)
  1091. bpf_lru_push_free(&htab->lru, &l->lru_node);
  1092. return ret;
  1093. }
  1094. static void delete_all_elements(struct bpf_htab *htab)
  1095. {
  1096. int i;
  1097. for (i = 0; i < htab->n_buckets; i++) {
  1098. struct hlist_nulls_head *head = select_bucket(htab, i);
  1099. struct hlist_nulls_node *n;
  1100. struct htab_elem *l;
  1101. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  1102. hlist_nulls_del_rcu(&l->hash_node);
  1103. htab_elem_free(htab, l);
  1104. }
  1105. }
  1106. }
  1107. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  1108. static void htab_map_free(struct bpf_map *map)
  1109. {
  1110. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1111. /* bpf_free_used_maps() or close(map_fd) will trigger this map_free callback.
  1112. * bpf_free_used_maps() is called after bpf prog is no longer executing.
  1113. * There is no need to synchronize_rcu() here to protect map elements.
  1114. */
  1115. /* some of free_htab_elem() callbacks for elements of this map may
  1116. * not have executed. Wait for them.
  1117. */
  1118. rcu_barrier();
  1119. if (!htab_is_prealloc(htab))
  1120. delete_all_elements(htab);
  1121. else
  1122. prealloc_destroy(htab);
  1123. free_percpu(htab->extra_elems);
  1124. bpf_map_area_free(htab->buckets);
  1125. kfree(htab);
  1126. }
  1127. static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
  1128. struct seq_file *m)
  1129. {
  1130. void *value;
  1131. rcu_read_lock();
  1132. value = htab_map_lookup_elem(map, key);
  1133. if (!value) {
  1134. rcu_read_unlock();
  1135. return;
  1136. }
  1137. btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
  1138. seq_puts(m, ": ");
  1139. btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
  1140. seq_puts(m, "\n");
  1141. rcu_read_unlock();
  1142. }
  1143. static int
  1144. __htab_map_lookup_and_delete_batch(struct bpf_map *map,
  1145. const union bpf_attr *attr,
  1146. union bpf_attr __user *uattr,
  1147. bool do_delete, bool is_lru_map,
  1148. bool is_percpu)
  1149. {
  1150. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1151. u32 bucket_cnt, total, key_size, value_size, roundup_key_size;
  1152. void *keys = NULL, *values = NULL, *value, *dst_key, *dst_val;
  1153. void __user *uvalues = u64_to_user_ptr(attr->batch.values);
  1154. void __user *ukeys = u64_to_user_ptr(attr->batch.keys);
  1155. void *ubatch = u64_to_user_ptr(attr->batch.in_batch);
  1156. u32 batch, max_count, size, bucket_size;
  1157. struct htab_elem *node_to_free = NULL;
  1158. u64 elem_map_flags, map_flags;
  1159. struct hlist_nulls_head *head;
  1160. struct hlist_nulls_node *n;
  1161. unsigned long flags = 0;
  1162. bool locked = false;
  1163. struct htab_elem *l;
  1164. struct bucket *b;
  1165. int ret = 0;
  1166. elem_map_flags = attr->batch.elem_flags;
  1167. if ((elem_map_flags & ~BPF_F_LOCK) ||
  1168. ((elem_map_flags & BPF_F_LOCK) && !map_value_has_spin_lock(map)))
  1169. return -EINVAL;
  1170. map_flags = attr->batch.flags;
  1171. if (map_flags)
  1172. return -EINVAL;
  1173. max_count = attr->batch.count;
  1174. if (!max_count)
  1175. return 0;
  1176. if (put_user(0, &uattr->batch.count))
  1177. return -EFAULT;
  1178. batch = 0;
  1179. if (ubatch && copy_from_user(&batch, ubatch, sizeof(batch)))
  1180. return -EFAULT;
  1181. if (batch >= htab->n_buckets)
  1182. return -ENOENT;
  1183. key_size = htab->map.key_size;
  1184. roundup_key_size = round_up(htab->map.key_size, 8);
  1185. value_size = htab->map.value_size;
  1186. size = round_up(value_size, 8);
  1187. if (is_percpu)
  1188. value_size = size * num_possible_cpus();
  1189. total = 0;
  1190. /* while experimenting with hash tables with sizes ranging from 10 to
  1191. * 1000, it was observed that a bucket can have upto 5 entries.
  1192. */
  1193. bucket_size = 5;
  1194. alloc:
  1195. /* We cannot do copy_from_user or copy_to_user inside
  1196. * the rcu_read_lock. Allocate enough space here.
  1197. */
  1198. keys = kvmalloc_array(key_size, bucket_size, GFP_USER | __GFP_NOWARN);
  1199. values = kvmalloc_array(value_size, bucket_size, GFP_USER | __GFP_NOWARN);
  1200. if (!keys || !values) {
  1201. ret = -ENOMEM;
  1202. goto after_loop;
  1203. }
  1204. again:
  1205. bpf_disable_instrumentation();
  1206. rcu_read_lock();
  1207. again_nocopy:
  1208. dst_key = keys;
  1209. dst_val = values;
  1210. b = &htab->buckets[batch];
  1211. head = &b->head;
  1212. /* do not grab the lock unless need it (bucket_cnt > 0). */
  1213. if (locked)
  1214. flags = htab_lock_bucket(htab, b);
  1215. bucket_cnt = 0;
  1216. hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
  1217. bucket_cnt++;
  1218. if (bucket_cnt && !locked) {
  1219. locked = true;
  1220. goto again_nocopy;
  1221. }
  1222. if (bucket_cnt > (max_count - total)) {
  1223. if (total == 0)
  1224. ret = -ENOSPC;
  1225. /* Note that since bucket_cnt > 0 here, it is implicit
  1226. * that the locked was grabbed, so release it.
  1227. */
  1228. htab_unlock_bucket(htab, b, flags);
  1229. rcu_read_unlock();
  1230. bpf_enable_instrumentation();
  1231. goto after_loop;
  1232. }
  1233. if (bucket_cnt > bucket_size) {
  1234. bucket_size = bucket_cnt;
  1235. /* Note that since bucket_cnt > 0 here, it is implicit
  1236. * that the locked was grabbed, so release it.
  1237. */
  1238. htab_unlock_bucket(htab, b, flags);
  1239. rcu_read_unlock();
  1240. bpf_enable_instrumentation();
  1241. kvfree(keys);
  1242. kvfree(values);
  1243. goto alloc;
  1244. }
  1245. /* Next block is only safe to run if you have grabbed the lock */
  1246. if (!locked)
  1247. goto next_batch;
  1248. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  1249. memcpy(dst_key, l->key, key_size);
  1250. if (is_percpu) {
  1251. int off = 0, cpu;
  1252. void __percpu *pptr;
  1253. pptr = htab_elem_get_ptr(l, map->key_size);
  1254. for_each_possible_cpu(cpu) {
  1255. bpf_long_memcpy(dst_val + off,
  1256. per_cpu_ptr(pptr, cpu), size);
  1257. off += size;
  1258. }
  1259. } else {
  1260. value = l->key + roundup_key_size;
  1261. if (elem_map_flags & BPF_F_LOCK)
  1262. copy_map_value_locked(map, dst_val, value,
  1263. true);
  1264. else
  1265. copy_map_value(map, dst_val, value);
  1266. check_and_init_map_lock(map, dst_val);
  1267. }
  1268. if (do_delete) {
  1269. hlist_nulls_del_rcu(&l->hash_node);
  1270. /* bpf_lru_push_free() will acquire lru_lock, which
  1271. * may cause deadlock. See comments in function
  1272. * prealloc_lru_pop(). Let us do bpf_lru_push_free()
  1273. * after releasing the bucket lock.
  1274. */
  1275. if (is_lru_map) {
  1276. l->batch_flink = node_to_free;
  1277. node_to_free = l;
  1278. } else {
  1279. free_htab_elem(htab, l);
  1280. }
  1281. }
  1282. dst_key += key_size;
  1283. dst_val += value_size;
  1284. }
  1285. htab_unlock_bucket(htab, b, flags);
  1286. locked = false;
  1287. while (node_to_free) {
  1288. l = node_to_free;
  1289. node_to_free = node_to_free->batch_flink;
  1290. bpf_lru_push_free(&htab->lru, &l->lru_node);
  1291. }
  1292. next_batch:
  1293. /* If we are not copying data, we can go to next bucket and avoid
  1294. * unlocking the rcu.
  1295. */
  1296. if (!bucket_cnt && (batch + 1 < htab->n_buckets)) {
  1297. batch++;
  1298. goto again_nocopy;
  1299. }
  1300. rcu_read_unlock();
  1301. bpf_enable_instrumentation();
  1302. if (bucket_cnt && (copy_to_user(ukeys + total * key_size, keys,
  1303. key_size * bucket_cnt) ||
  1304. copy_to_user(uvalues + total * value_size, values,
  1305. value_size * bucket_cnt))) {
  1306. ret = -EFAULT;
  1307. goto after_loop;
  1308. }
  1309. total += bucket_cnt;
  1310. batch++;
  1311. if (batch >= htab->n_buckets) {
  1312. ret = -ENOENT;
  1313. goto after_loop;
  1314. }
  1315. goto again;
  1316. after_loop:
  1317. if (ret == -EFAULT)
  1318. goto out;
  1319. /* copy # of entries and next batch */
  1320. ubatch = u64_to_user_ptr(attr->batch.out_batch);
  1321. if (copy_to_user(ubatch, &batch, sizeof(batch)) ||
  1322. put_user(total, &uattr->batch.count))
  1323. ret = -EFAULT;
  1324. out:
  1325. kvfree(keys);
  1326. kvfree(values);
  1327. return ret;
  1328. }
  1329. static int
  1330. htab_percpu_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
  1331. union bpf_attr __user *uattr)
  1332. {
  1333. return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
  1334. false, true);
  1335. }
  1336. static int
  1337. htab_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
  1338. const union bpf_attr *attr,
  1339. union bpf_attr __user *uattr)
  1340. {
  1341. return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
  1342. false, true);
  1343. }
  1344. static int
  1345. htab_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
  1346. union bpf_attr __user *uattr)
  1347. {
  1348. return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
  1349. false, false);
  1350. }
  1351. static int
  1352. htab_map_lookup_and_delete_batch(struct bpf_map *map,
  1353. const union bpf_attr *attr,
  1354. union bpf_attr __user *uattr)
  1355. {
  1356. return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
  1357. false, false);
  1358. }
  1359. static int
  1360. htab_lru_percpu_map_lookup_batch(struct bpf_map *map,
  1361. const union bpf_attr *attr,
  1362. union bpf_attr __user *uattr)
  1363. {
  1364. return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
  1365. true, true);
  1366. }
  1367. static int
  1368. htab_lru_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
  1369. const union bpf_attr *attr,
  1370. union bpf_attr __user *uattr)
  1371. {
  1372. return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
  1373. true, true);
  1374. }
  1375. static int
  1376. htab_lru_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
  1377. union bpf_attr __user *uattr)
  1378. {
  1379. return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
  1380. true, false);
  1381. }
  1382. static int
  1383. htab_lru_map_lookup_and_delete_batch(struct bpf_map *map,
  1384. const union bpf_attr *attr,
  1385. union bpf_attr __user *uattr)
  1386. {
  1387. return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
  1388. true, false);
  1389. }
  1390. struct bpf_iter_seq_hash_map_info {
  1391. struct bpf_map *map;
  1392. struct bpf_htab *htab;
  1393. void *percpu_value_buf; // non-zero means percpu hash
  1394. u32 bucket_id;
  1395. u32 skip_elems;
  1396. };
  1397. static struct htab_elem *
  1398. bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info *info,
  1399. struct htab_elem *prev_elem)
  1400. {
  1401. const struct bpf_htab *htab = info->htab;
  1402. u32 skip_elems = info->skip_elems;
  1403. u32 bucket_id = info->bucket_id;
  1404. struct hlist_nulls_head *head;
  1405. struct hlist_nulls_node *n;
  1406. struct htab_elem *elem;
  1407. struct bucket *b;
  1408. u32 i, count;
  1409. if (bucket_id >= htab->n_buckets)
  1410. return NULL;
  1411. /* try to find next elem in the same bucket */
  1412. if (prev_elem) {
  1413. /* no update/deletion on this bucket, prev_elem should be still valid
  1414. * and we won't skip elements.
  1415. */
  1416. n = rcu_dereference_raw(hlist_nulls_next_rcu(&prev_elem->hash_node));
  1417. elem = hlist_nulls_entry_safe(n, struct htab_elem, hash_node);
  1418. if (elem)
  1419. return elem;
  1420. /* not found, unlock and go to the next bucket */
  1421. b = &htab->buckets[bucket_id++];
  1422. rcu_read_unlock();
  1423. skip_elems = 0;
  1424. }
  1425. for (i = bucket_id; i < htab->n_buckets; i++) {
  1426. b = &htab->buckets[i];
  1427. rcu_read_lock();
  1428. count = 0;
  1429. head = &b->head;
  1430. hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
  1431. if (count >= skip_elems) {
  1432. info->bucket_id = i;
  1433. info->skip_elems = count;
  1434. return elem;
  1435. }
  1436. count++;
  1437. }
  1438. rcu_read_unlock();
  1439. skip_elems = 0;
  1440. }
  1441. info->bucket_id = i;
  1442. info->skip_elems = 0;
  1443. return NULL;
  1444. }
  1445. static void *bpf_hash_map_seq_start(struct seq_file *seq, loff_t *pos)
  1446. {
  1447. struct bpf_iter_seq_hash_map_info *info = seq->private;
  1448. struct htab_elem *elem;
  1449. elem = bpf_hash_map_seq_find_next(info, NULL);
  1450. if (!elem)
  1451. return NULL;
  1452. if (*pos == 0)
  1453. ++*pos;
  1454. return elem;
  1455. }
  1456. static void *bpf_hash_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1457. {
  1458. struct bpf_iter_seq_hash_map_info *info = seq->private;
  1459. ++*pos;
  1460. ++info->skip_elems;
  1461. return bpf_hash_map_seq_find_next(info, v);
  1462. }
  1463. static int __bpf_hash_map_seq_show(struct seq_file *seq, struct htab_elem *elem)
  1464. {
  1465. struct bpf_iter_seq_hash_map_info *info = seq->private;
  1466. u32 roundup_key_size, roundup_value_size;
  1467. struct bpf_iter__bpf_map_elem ctx = {};
  1468. struct bpf_map *map = info->map;
  1469. struct bpf_iter_meta meta;
  1470. int ret = 0, off = 0, cpu;
  1471. struct bpf_prog *prog;
  1472. void __percpu *pptr;
  1473. meta.seq = seq;
  1474. prog = bpf_iter_get_info(&meta, elem == NULL);
  1475. if (prog) {
  1476. ctx.meta = &meta;
  1477. ctx.map = info->map;
  1478. if (elem) {
  1479. roundup_key_size = round_up(map->key_size, 8);
  1480. ctx.key = elem->key;
  1481. if (!info->percpu_value_buf) {
  1482. ctx.value = elem->key + roundup_key_size;
  1483. } else {
  1484. roundup_value_size = round_up(map->value_size, 8);
  1485. pptr = htab_elem_get_ptr(elem, map->key_size);
  1486. for_each_possible_cpu(cpu) {
  1487. bpf_long_memcpy(info->percpu_value_buf + off,
  1488. per_cpu_ptr(pptr, cpu),
  1489. roundup_value_size);
  1490. off += roundup_value_size;
  1491. }
  1492. ctx.value = info->percpu_value_buf;
  1493. }
  1494. }
  1495. ret = bpf_iter_run_prog(prog, &ctx);
  1496. }
  1497. return ret;
  1498. }
  1499. static int bpf_hash_map_seq_show(struct seq_file *seq, void *v)
  1500. {
  1501. return __bpf_hash_map_seq_show(seq, v);
  1502. }
  1503. static void bpf_hash_map_seq_stop(struct seq_file *seq, void *v)
  1504. {
  1505. if (!v)
  1506. (void)__bpf_hash_map_seq_show(seq, NULL);
  1507. else
  1508. rcu_read_unlock();
  1509. }
  1510. static int bpf_iter_init_hash_map(void *priv_data,
  1511. struct bpf_iter_aux_info *aux)
  1512. {
  1513. struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
  1514. struct bpf_map *map = aux->map;
  1515. void *value_buf;
  1516. u32 buf_size;
  1517. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  1518. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  1519. buf_size = round_up(map->value_size, 8) * num_possible_cpus();
  1520. value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN);
  1521. if (!value_buf)
  1522. return -ENOMEM;
  1523. seq_info->percpu_value_buf = value_buf;
  1524. }
  1525. seq_info->map = map;
  1526. seq_info->htab = container_of(map, struct bpf_htab, map);
  1527. return 0;
  1528. }
  1529. static void bpf_iter_fini_hash_map(void *priv_data)
  1530. {
  1531. struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
  1532. kfree(seq_info->percpu_value_buf);
  1533. }
  1534. static const struct seq_operations bpf_hash_map_seq_ops = {
  1535. .start = bpf_hash_map_seq_start,
  1536. .next = bpf_hash_map_seq_next,
  1537. .stop = bpf_hash_map_seq_stop,
  1538. .show = bpf_hash_map_seq_show,
  1539. };
  1540. static const struct bpf_iter_seq_info iter_seq_info = {
  1541. .seq_ops = &bpf_hash_map_seq_ops,
  1542. .init_seq_private = bpf_iter_init_hash_map,
  1543. .fini_seq_private = bpf_iter_fini_hash_map,
  1544. .seq_priv_size = sizeof(struct bpf_iter_seq_hash_map_info),
  1545. };
  1546. static int htab_map_btf_id;
  1547. const struct bpf_map_ops htab_map_ops = {
  1548. .map_meta_equal = bpf_map_meta_equal,
  1549. .map_alloc_check = htab_map_alloc_check,
  1550. .map_alloc = htab_map_alloc,
  1551. .map_free = htab_map_free,
  1552. .map_get_next_key = htab_map_get_next_key,
  1553. .map_lookup_elem = htab_map_lookup_elem,
  1554. .map_update_elem = htab_map_update_elem,
  1555. .map_delete_elem = htab_map_delete_elem,
  1556. .map_gen_lookup = htab_map_gen_lookup,
  1557. .map_seq_show_elem = htab_map_seq_show_elem,
  1558. BATCH_OPS(htab),
  1559. .map_btf_name = "bpf_htab",
  1560. .map_btf_id = &htab_map_btf_id,
  1561. .iter_seq_info = &iter_seq_info,
  1562. };
  1563. static int htab_lru_map_btf_id;
  1564. const struct bpf_map_ops htab_lru_map_ops = {
  1565. .map_meta_equal = bpf_map_meta_equal,
  1566. .map_alloc_check = htab_map_alloc_check,
  1567. .map_alloc = htab_map_alloc,
  1568. .map_free = htab_map_free,
  1569. .map_get_next_key = htab_map_get_next_key,
  1570. .map_lookup_elem = htab_lru_map_lookup_elem,
  1571. .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
  1572. .map_update_elem = htab_lru_map_update_elem,
  1573. .map_delete_elem = htab_lru_map_delete_elem,
  1574. .map_gen_lookup = htab_lru_map_gen_lookup,
  1575. .map_seq_show_elem = htab_map_seq_show_elem,
  1576. BATCH_OPS(htab_lru),
  1577. .map_btf_name = "bpf_htab",
  1578. .map_btf_id = &htab_lru_map_btf_id,
  1579. .iter_seq_info = &iter_seq_info,
  1580. };
  1581. /* Called from eBPF program */
  1582. static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  1583. {
  1584. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  1585. if (l)
  1586. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  1587. else
  1588. return NULL;
  1589. }
  1590. static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  1591. {
  1592. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  1593. if (l) {
  1594. bpf_lru_node_set_ref(&l->lru_node);
  1595. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  1596. }
  1597. return NULL;
  1598. }
  1599. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
  1600. {
  1601. struct htab_elem *l;
  1602. void __percpu *pptr;
  1603. int ret = -ENOENT;
  1604. int cpu, off = 0;
  1605. u32 size;
  1606. /* per_cpu areas are zero-filled and bpf programs can only
  1607. * access 'value_size' of them, so copying rounded areas
  1608. * will not leak any kernel data
  1609. */
  1610. size = round_up(map->value_size, 8);
  1611. rcu_read_lock();
  1612. l = __htab_map_lookup_elem(map, key);
  1613. if (!l)
  1614. goto out;
  1615. /* We do not mark LRU map element here in order to not mess up
  1616. * eviction heuristics when user space does a map walk.
  1617. */
  1618. pptr = htab_elem_get_ptr(l, map->key_size);
  1619. for_each_possible_cpu(cpu) {
  1620. bpf_long_memcpy(value + off,
  1621. per_cpu_ptr(pptr, cpu), size);
  1622. off += size;
  1623. }
  1624. ret = 0;
  1625. out:
  1626. rcu_read_unlock();
  1627. return ret;
  1628. }
  1629. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  1630. u64 map_flags)
  1631. {
  1632. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1633. int ret;
  1634. rcu_read_lock();
  1635. if (htab_is_lru(htab))
  1636. ret = __htab_lru_percpu_map_update_elem(map, key, value,
  1637. map_flags, true);
  1638. else
  1639. ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
  1640. true);
  1641. rcu_read_unlock();
  1642. return ret;
  1643. }
  1644. static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
  1645. struct seq_file *m)
  1646. {
  1647. struct htab_elem *l;
  1648. void __percpu *pptr;
  1649. int cpu;
  1650. rcu_read_lock();
  1651. l = __htab_map_lookup_elem(map, key);
  1652. if (!l) {
  1653. rcu_read_unlock();
  1654. return;
  1655. }
  1656. btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
  1657. seq_puts(m, ": {\n");
  1658. pptr = htab_elem_get_ptr(l, map->key_size);
  1659. for_each_possible_cpu(cpu) {
  1660. seq_printf(m, "\tcpu%d: ", cpu);
  1661. btf_type_seq_show(map->btf, map->btf_value_type_id,
  1662. per_cpu_ptr(pptr, cpu), m);
  1663. seq_puts(m, "\n");
  1664. }
  1665. seq_puts(m, "}\n");
  1666. rcu_read_unlock();
  1667. }
  1668. static int htab_percpu_map_btf_id;
  1669. const struct bpf_map_ops htab_percpu_map_ops = {
  1670. .map_meta_equal = bpf_map_meta_equal,
  1671. .map_alloc_check = htab_map_alloc_check,
  1672. .map_alloc = htab_map_alloc,
  1673. .map_free = htab_map_free,
  1674. .map_get_next_key = htab_map_get_next_key,
  1675. .map_lookup_elem = htab_percpu_map_lookup_elem,
  1676. .map_update_elem = htab_percpu_map_update_elem,
  1677. .map_delete_elem = htab_map_delete_elem,
  1678. .map_seq_show_elem = htab_percpu_map_seq_show_elem,
  1679. BATCH_OPS(htab_percpu),
  1680. .map_btf_name = "bpf_htab",
  1681. .map_btf_id = &htab_percpu_map_btf_id,
  1682. .iter_seq_info = &iter_seq_info,
  1683. };
  1684. static int htab_lru_percpu_map_btf_id;
  1685. const struct bpf_map_ops htab_lru_percpu_map_ops = {
  1686. .map_meta_equal = bpf_map_meta_equal,
  1687. .map_alloc_check = htab_map_alloc_check,
  1688. .map_alloc = htab_map_alloc,
  1689. .map_free = htab_map_free,
  1690. .map_get_next_key = htab_map_get_next_key,
  1691. .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
  1692. .map_update_elem = htab_lru_percpu_map_update_elem,
  1693. .map_delete_elem = htab_lru_map_delete_elem,
  1694. .map_seq_show_elem = htab_percpu_map_seq_show_elem,
  1695. BATCH_OPS(htab_lru_percpu),
  1696. .map_btf_name = "bpf_htab",
  1697. .map_btf_id = &htab_lru_percpu_map_btf_id,
  1698. .iter_seq_info = &iter_seq_info,
  1699. };
  1700. static int fd_htab_map_alloc_check(union bpf_attr *attr)
  1701. {
  1702. if (attr->value_size != sizeof(u32))
  1703. return -EINVAL;
  1704. return htab_map_alloc_check(attr);
  1705. }
  1706. static void fd_htab_map_free(struct bpf_map *map)
  1707. {
  1708. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1709. struct hlist_nulls_node *n;
  1710. struct hlist_nulls_head *head;
  1711. struct htab_elem *l;
  1712. int i;
  1713. for (i = 0; i < htab->n_buckets; i++) {
  1714. head = select_bucket(htab, i);
  1715. hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
  1716. void *ptr = fd_htab_map_get_ptr(map, l);
  1717. map->ops->map_fd_put_ptr(ptr);
  1718. }
  1719. }
  1720. htab_map_free(map);
  1721. }
  1722. /* only called from syscall */
  1723. int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
  1724. {
  1725. void **ptr;
  1726. int ret = 0;
  1727. if (!map->ops->map_fd_sys_lookup_elem)
  1728. return -ENOTSUPP;
  1729. rcu_read_lock();
  1730. ptr = htab_map_lookup_elem(map, key);
  1731. if (ptr)
  1732. *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
  1733. else
  1734. ret = -ENOENT;
  1735. rcu_read_unlock();
  1736. return ret;
  1737. }
  1738. /* only called from syscall */
  1739. int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
  1740. void *key, void *value, u64 map_flags)
  1741. {
  1742. void *ptr;
  1743. int ret;
  1744. u32 ufd = *(u32 *)value;
  1745. ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  1746. if (IS_ERR(ptr))
  1747. return PTR_ERR(ptr);
  1748. ret = htab_map_update_elem(map, key, &ptr, map_flags);
  1749. if (ret)
  1750. map->ops->map_fd_put_ptr(ptr);
  1751. return ret;
  1752. }
  1753. static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
  1754. {
  1755. struct bpf_map *map, *inner_map_meta;
  1756. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  1757. if (IS_ERR(inner_map_meta))
  1758. return inner_map_meta;
  1759. map = htab_map_alloc(attr);
  1760. if (IS_ERR(map)) {
  1761. bpf_map_meta_free(inner_map_meta);
  1762. return map;
  1763. }
  1764. map->inner_map_meta = inner_map_meta;
  1765. return map;
  1766. }
  1767. static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
  1768. {
  1769. struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
  1770. if (!inner_map)
  1771. return NULL;
  1772. return READ_ONCE(*inner_map);
  1773. }
  1774. static int htab_of_map_gen_lookup(struct bpf_map *map,
  1775. struct bpf_insn *insn_buf)
  1776. {
  1777. struct bpf_insn *insn = insn_buf;
  1778. const int ret = BPF_REG_0;
  1779. BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
  1780. (void *(*)(struct bpf_map *map, void *key))NULL));
  1781. *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
  1782. *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
  1783. *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
  1784. offsetof(struct htab_elem, key) +
  1785. round_up(map->key_size, 8));
  1786. *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
  1787. return insn - insn_buf;
  1788. }
  1789. static void htab_of_map_free(struct bpf_map *map)
  1790. {
  1791. bpf_map_meta_free(map->inner_map_meta);
  1792. fd_htab_map_free(map);
  1793. }
  1794. static int htab_of_maps_map_btf_id;
  1795. const struct bpf_map_ops htab_of_maps_map_ops = {
  1796. .map_alloc_check = fd_htab_map_alloc_check,
  1797. .map_alloc = htab_of_map_alloc,
  1798. .map_free = htab_of_map_free,
  1799. .map_get_next_key = htab_map_get_next_key,
  1800. .map_lookup_elem = htab_of_map_lookup_elem,
  1801. .map_delete_elem = htab_map_delete_elem,
  1802. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  1803. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  1804. .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
  1805. .map_gen_lookup = htab_of_map_gen_lookup,
  1806. .map_check_btf = map_check_no_btf,
  1807. .map_btf_name = "bpf_htab",
  1808. .map_btf_id = &htab_of_maps_map_btf_id,
  1809. };