hashtab.c 71 KB

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