hashtab.c 66 KB

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