cpumap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* bpf/cpumap.c
  3. *
  4. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  5. */
  6. /* The 'cpumap' is primarily used as a backend map for XDP BPF helper
  7. * call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'.
  8. *
  9. * Unlike devmap which redirects XDP frames out another NIC device,
  10. * this map type redirects raw XDP frames to another CPU. The remote
  11. * CPU will do SKB-allocation and call the normal network stack.
  12. *
  13. * This is a scalability and isolation mechanism, that allow
  14. * separating the early driver network XDP layer, from the rest of the
  15. * netstack, and assigning dedicated CPUs for this stage. This
  16. * basically allows for 10G wirespeed pre-filtering via bpf.
  17. */
  18. #include <linux/bpf.h>
  19. #include <linux/filter.h>
  20. #include <linux/ptr_ring.h>
  21. #include <net/xdp.h>
  22. #include <linux/sched.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/kthread.h>
  25. #include <linux/capability.h>
  26. #include <trace/events/xdp.h>
  27. #include <linux/netdevice.h> /* netif_receive_skb_core */
  28. #include <linux/etherdevice.h> /* eth_type_trans */
  29. /* General idea: XDP packets getting XDP redirected to another CPU,
  30. * will maximum be stored/queued for one driver ->poll() call. It is
  31. * guaranteed that queueing the frame and the flush operation happen on
  32. * same CPU. Thus, cpu_map_flush operation can deduct via this_cpu_ptr()
  33. * which queue in bpf_cpu_map_entry contains packets.
  34. */
  35. #define CPU_MAP_BULK_SIZE 8 /* 8 == one cacheline on 64-bit archs */
  36. struct bpf_cpu_map_entry;
  37. struct bpf_cpu_map;
  38. struct xdp_bulk_queue {
  39. void *q[CPU_MAP_BULK_SIZE];
  40. struct list_head flush_node;
  41. struct bpf_cpu_map_entry *obj;
  42. unsigned int count;
  43. };
  44. /* Struct for every remote "destination" CPU in map */
  45. struct bpf_cpu_map_entry {
  46. u32 cpu; /* kthread CPU and map index */
  47. int map_id; /* Back reference to map */
  48. /* XDP can run multiple RX-ring queues, need __percpu enqueue store */
  49. struct xdp_bulk_queue __percpu *bulkq;
  50. struct bpf_cpu_map *cmap;
  51. /* Queue with potential multi-producers, and single-consumer kthread */
  52. struct ptr_ring *queue;
  53. struct task_struct *kthread;
  54. struct bpf_cpumap_val value;
  55. struct bpf_prog *prog;
  56. atomic_t refcnt; /* Control when this struct can be free'ed */
  57. struct rcu_head rcu;
  58. struct work_struct kthread_stop_wq;
  59. };
  60. struct bpf_cpu_map {
  61. struct bpf_map map;
  62. /* Below members specific for map type */
  63. struct bpf_cpu_map_entry **cpu_map;
  64. };
  65. static DEFINE_PER_CPU(struct list_head, cpu_map_flush_list);
  66. static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
  67. {
  68. u32 value_size = attr->value_size;
  69. struct bpf_cpu_map *cmap;
  70. int err = -ENOMEM;
  71. u64 cost;
  72. int ret;
  73. if (!bpf_capable())
  74. return ERR_PTR(-EPERM);
  75. /* check sanity of attributes */
  76. if (attr->max_entries == 0 || attr->key_size != 4 ||
  77. (value_size != offsetofend(struct bpf_cpumap_val, qsize) &&
  78. value_size != offsetofend(struct bpf_cpumap_val, bpf_prog.fd)) ||
  79. attr->map_flags & ~BPF_F_NUMA_NODE)
  80. return ERR_PTR(-EINVAL);
  81. cmap = kzalloc(sizeof(*cmap), GFP_USER);
  82. if (!cmap)
  83. return ERR_PTR(-ENOMEM);
  84. bpf_map_init_from_attr(&cmap->map, attr);
  85. /* Pre-limit array size based on NR_CPUS, not final CPU check */
  86. if (cmap->map.max_entries > NR_CPUS) {
  87. err = -E2BIG;
  88. goto free_cmap;
  89. }
  90. /* make sure page count doesn't overflow */
  91. cost = (u64) cmap->map.max_entries * sizeof(struct bpf_cpu_map_entry *);
  92. /* Notice returns -EPERM on if map size is larger than memlock limit */
  93. ret = bpf_map_charge_init(&cmap->map.memory, cost);
  94. if (ret) {
  95. err = ret;
  96. goto free_cmap;
  97. }
  98. /* Alloc array for possible remote "destination" CPUs */
  99. cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
  100. sizeof(struct bpf_cpu_map_entry *),
  101. cmap->map.numa_node);
  102. if (!cmap->cpu_map)
  103. goto free_charge;
  104. return &cmap->map;
  105. free_charge:
  106. bpf_map_charge_finish(&cmap->map.memory);
  107. free_cmap:
  108. kfree(cmap);
  109. return ERR_PTR(err);
  110. }
  111. static void get_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
  112. {
  113. atomic_inc(&rcpu->refcnt);
  114. }
  115. /* called from workqueue, to workaround syscall using preempt_disable */
  116. static void cpu_map_kthread_stop(struct work_struct *work)
  117. {
  118. struct bpf_cpu_map_entry *rcpu;
  119. rcpu = container_of(work, struct bpf_cpu_map_entry, kthread_stop_wq);
  120. /* Wait for flush in __cpu_map_entry_free(), via full RCU barrier,
  121. * as it waits until all in-flight call_rcu() callbacks complete.
  122. */
  123. rcu_barrier();
  124. /* kthread_stop will wake_up_process and wait for it to complete */
  125. kthread_stop(rcpu->kthread);
  126. }
  127. static struct sk_buff *cpu_map_build_skb(struct xdp_frame *xdpf,
  128. struct sk_buff *skb)
  129. {
  130. unsigned int hard_start_headroom;
  131. unsigned int frame_size;
  132. void *pkt_data_start;
  133. /* Part of headroom was reserved to xdpf */
  134. hard_start_headroom = sizeof(struct xdp_frame) + xdpf->headroom;
  135. /* Memory size backing xdp_frame data already have reserved
  136. * room for build_skb to place skb_shared_info in tailroom.
  137. */
  138. frame_size = xdpf->frame_sz;
  139. pkt_data_start = xdpf->data - hard_start_headroom;
  140. skb = build_skb_around(skb, pkt_data_start, frame_size);
  141. if (unlikely(!skb))
  142. return NULL;
  143. skb_reserve(skb, hard_start_headroom);
  144. __skb_put(skb, xdpf->len);
  145. if (xdpf->metasize)
  146. skb_metadata_set(skb, xdpf->metasize);
  147. /* Essential SKB info: protocol and skb->dev */
  148. skb->protocol = eth_type_trans(skb, xdpf->dev_rx);
  149. /* Optional SKB info, currently missing:
  150. * - HW checksum info (skb->ip_summed)
  151. * - HW RX hash (skb_set_hash)
  152. * - RX ring dev queue index (skb_record_rx_queue)
  153. */
  154. /* Until page_pool get SKB return path, release DMA here */
  155. xdp_release_frame(xdpf);
  156. /* Allow SKB to reuse area used by xdp_frame */
  157. xdp_scrub_frame(xdpf);
  158. return skb;
  159. }
  160. static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
  161. {
  162. /* The tear-down procedure should have made sure that queue is
  163. * empty. See __cpu_map_entry_replace() and work-queue
  164. * invoked cpu_map_kthread_stop(). Catch any broken behaviour
  165. * gracefully and warn once.
  166. */
  167. struct xdp_frame *xdpf;
  168. while ((xdpf = ptr_ring_consume(ring)))
  169. if (WARN_ON_ONCE(xdpf))
  170. xdp_return_frame(xdpf);
  171. }
  172. static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
  173. {
  174. if (atomic_dec_and_test(&rcpu->refcnt)) {
  175. if (rcpu->prog)
  176. bpf_prog_put(rcpu->prog);
  177. /* The queue should be empty at this point */
  178. __cpu_map_ring_cleanup(rcpu->queue);
  179. ptr_ring_cleanup(rcpu->queue, NULL);
  180. kfree(rcpu->queue);
  181. kfree(rcpu);
  182. }
  183. }
  184. static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu,
  185. void **frames, int n,
  186. struct xdp_cpumap_stats *stats)
  187. {
  188. struct xdp_rxq_info rxq;
  189. struct xdp_buff xdp;
  190. int i, nframes = 0;
  191. if (!rcpu->prog)
  192. return n;
  193. rcu_read_lock_bh();
  194. xdp_set_return_frame_no_direct();
  195. xdp.rxq = &rxq;
  196. for (i = 0; i < n; i++) {
  197. struct xdp_frame *xdpf = frames[i];
  198. u32 act;
  199. int err;
  200. rxq.dev = xdpf->dev_rx;
  201. rxq.mem = xdpf->mem;
  202. /* TODO: report queue_index to xdp_rxq_info */
  203. xdp_convert_frame_to_buff(xdpf, &xdp);
  204. act = bpf_prog_run_xdp(rcpu->prog, &xdp);
  205. switch (act) {
  206. case XDP_PASS:
  207. err = xdp_update_frame_from_buff(&xdp, xdpf);
  208. if (err < 0) {
  209. xdp_return_frame(xdpf);
  210. stats->drop++;
  211. } else {
  212. frames[nframes++] = xdpf;
  213. stats->pass++;
  214. }
  215. break;
  216. case XDP_REDIRECT:
  217. err = xdp_do_redirect(xdpf->dev_rx, &xdp,
  218. rcpu->prog);
  219. if (unlikely(err)) {
  220. xdp_return_frame(xdpf);
  221. stats->drop++;
  222. } else {
  223. stats->redirect++;
  224. }
  225. break;
  226. default:
  227. bpf_warn_invalid_xdp_action(act);
  228. fallthrough;
  229. case XDP_DROP:
  230. xdp_return_frame(xdpf);
  231. stats->drop++;
  232. break;
  233. }
  234. }
  235. if (stats->redirect)
  236. xdp_do_flush_map();
  237. xdp_clear_return_frame_no_direct();
  238. rcu_read_unlock_bh(); /* resched point, may call do_softirq() */
  239. return nframes;
  240. }
  241. #define CPUMAP_BATCH 8
  242. static int cpu_map_kthread_run(void *data)
  243. {
  244. struct bpf_cpu_map_entry *rcpu = data;
  245. set_current_state(TASK_INTERRUPTIBLE);
  246. /* When kthread gives stop order, then rcpu have been disconnected
  247. * from map, thus no new packets can enter. Remaining in-flight
  248. * per CPU stored packets are flushed to this queue. Wait honoring
  249. * kthread_stop signal until queue is empty.
  250. */
  251. while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
  252. struct xdp_cpumap_stats stats = {}; /* zero stats */
  253. gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
  254. unsigned int drops = 0, sched = 0;
  255. void *frames[CPUMAP_BATCH];
  256. void *skbs[CPUMAP_BATCH];
  257. int i, n, m, nframes;
  258. /* Release CPU reschedule checks */
  259. if (__ptr_ring_empty(rcpu->queue)) {
  260. set_current_state(TASK_INTERRUPTIBLE);
  261. /* Recheck to avoid lost wake-up */
  262. if (__ptr_ring_empty(rcpu->queue)) {
  263. schedule();
  264. sched = 1;
  265. } else {
  266. __set_current_state(TASK_RUNNING);
  267. }
  268. } else {
  269. sched = cond_resched();
  270. }
  271. /*
  272. * The bpf_cpu_map_entry is single consumer, with this
  273. * kthread CPU pinned. Lockless access to ptr_ring
  274. * consume side valid as no-resize allowed of queue.
  275. */
  276. n = __ptr_ring_consume_batched(rcpu->queue, frames,
  277. CPUMAP_BATCH);
  278. for (i = 0; i < n; i++) {
  279. void *f = frames[i];
  280. struct page *page = virt_to_page(f);
  281. /* Bring struct page memory area to curr CPU. Read by
  282. * build_skb_around via page_is_pfmemalloc(), and when
  283. * freed written by page_frag_free call.
  284. */
  285. prefetchw(page);
  286. }
  287. /* Support running another XDP prog on this CPU */
  288. nframes = cpu_map_bpf_prog_run_xdp(rcpu, frames, n, &stats);
  289. if (nframes) {
  290. m = kmem_cache_alloc_bulk(skbuff_head_cache, gfp, nframes, skbs);
  291. if (unlikely(m == 0)) {
  292. for (i = 0; i < nframes; i++)
  293. skbs[i] = NULL; /* effect: xdp_return_frame */
  294. drops += nframes;
  295. }
  296. }
  297. local_bh_disable();
  298. for (i = 0; i < nframes; i++) {
  299. struct xdp_frame *xdpf = frames[i];
  300. struct sk_buff *skb = skbs[i];
  301. int ret;
  302. skb = cpu_map_build_skb(xdpf, skb);
  303. if (!skb) {
  304. xdp_return_frame(xdpf);
  305. continue;
  306. }
  307. /* Inject into network stack */
  308. ret = netif_receive_skb_core(skb);
  309. if (ret == NET_RX_DROP)
  310. drops++;
  311. }
  312. /* Feedback loop via tracepoint */
  313. trace_xdp_cpumap_kthread(rcpu->map_id, n, drops, sched, &stats);
  314. local_bh_enable(); /* resched point, may call do_softirq() */
  315. }
  316. __set_current_state(TASK_RUNNING);
  317. put_cpu_map_entry(rcpu);
  318. return 0;
  319. }
  320. bool cpu_map_prog_allowed(struct bpf_map *map)
  321. {
  322. return map->map_type == BPF_MAP_TYPE_CPUMAP &&
  323. map->value_size != offsetofend(struct bpf_cpumap_val, qsize);
  324. }
  325. static int __cpu_map_load_bpf_program(struct bpf_cpu_map_entry *rcpu, int fd)
  326. {
  327. struct bpf_prog *prog;
  328. prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
  329. if (IS_ERR(prog))
  330. return PTR_ERR(prog);
  331. if (prog->expected_attach_type != BPF_XDP_CPUMAP) {
  332. bpf_prog_put(prog);
  333. return -EINVAL;
  334. }
  335. rcpu->value.bpf_prog.id = prog->aux->id;
  336. rcpu->prog = prog;
  337. return 0;
  338. }
  339. static struct bpf_cpu_map_entry *
  340. __cpu_map_entry_alloc(struct bpf_cpumap_val *value, u32 cpu, int map_id)
  341. {
  342. int numa, err, i, fd = value->bpf_prog.fd;
  343. gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
  344. struct bpf_cpu_map_entry *rcpu;
  345. struct xdp_bulk_queue *bq;
  346. /* Have map->numa_node, but choose node of redirect target CPU */
  347. numa = cpu_to_node(cpu);
  348. rcpu = kzalloc_node(sizeof(*rcpu), gfp, numa);
  349. if (!rcpu)
  350. return NULL;
  351. /* Alloc percpu bulkq */
  352. rcpu->bulkq = __alloc_percpu_gfp(sizeof(*rcpu->bulkq),
  353. sizeof(void *), gfp);
  354. if (!rcpu->bulkq)
  355. goto free_rcu;
  356. for_each_possible_cpu(i) {
  357. bq = per_cpu_ptr(rcpu->bulkq, i);
  358. bq->obj = rcpu;
  359. }
  360. /* Alloc queue */
  361. rcpu->queue = kzalloc_node(sizeof(*rcpu->queue), gfp, numa);
  362. if (!rcpu->queue)
  363. goto free_bulkq;
  364. err = ptr_ring_init(rcpu->queue, value->qsize, gfp);
  365. if (err)
  366. goto free_queue;
  367. rcpu->cpu = cpu;
  368. rcpu->map_id = map_id;
  369. rcpu->value.qsize = value->qsize;
  370. if (fd > 0 && __cpu_map_load_bpf_program(rcpu, fd))
  371. goto free_ptr_ring;
  372. /* Setup kthread */
  373. rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa,
  374. "cpumap/%d/map:%d", cpu, map_id);
  375. if (IS_ERR(rcpu->kthread))
  376. goto free_prog;
  377. get_cpu_map_entry(rcpu); /* 1-refcnt for being in cmap->cpu_map[] */
  378. get_cpu_map_entry(rcpu); /* 1-refcnt for kthread */
  379. /* Make sure kthread runs on a single CPU */
  380. kthread_bind(rcpu->kthread, cpu);
  381. wake_up_process(rcpu->kthread);
  382. return rcpu;
  383. free_prog:
  384. if (rcpu->prog)
  385. bpf_prog_put(rcpu->prog);
  386. free_ptr_ring:
  387. ptr_ring_cleanup(rcpu->queue, NULL);
  388. free_queue:
  389. kfree(rcpu->queue);
  390. free_bulkq:
  391. free_percpu(rcpu->bulkq);
  392. free_rcu:
  393. kfree(rcpu);
  394. return NULL;
  395. }
  396. static void __cpu_map_entry_free(struct rcu_head *rcu)
  397. {
  398. struct bpf_cpu_map_entry *rcpu;
  399. /* This cpu_map_entry have been disconnected from map and one
  400. * RCU grace-period have elapsed. Thus, XDP cannot queue any
  401. * new packets and cannot change/set flush_needed that can
  402. * find this entry.
  403. */
  404. rcpu = container_of(rcu, struct bpf_cpu_map_entry, rcu);
  405. free_percpu(rcpu->bulkq);
  406. /* Cannot kthread_stop() here, last put free rcpu resources */
  407. put_cpu_map_entry(rcpu);
  408. }
  409. /* After xchg pointer to bpf_cpu_map_entry, use the call_rcu() to
  410. * ensure any driver rcu critical sections have completed, but this
  411. * does not guarantee a flush has happened yet. Because driver side
  412. * rcu_read_lock/unlock only protects the running XDP program. The
  413. * atomic xchg and NULL-ptr check in __cpu_map_flush() makes sure a
  414. * pending flush op doesn't fail.
  415. *
  416. * The bpf_cpu_map_entry is still used by the kthread, and there can
  417. * still be pending packets (in queue and percpu bulkq). A refcnt
  418. * makes sure to last user (kthread_stop vs. call_rcu) free memory
  419. * resources.
  420. *
  421. * The rcu callback __cpu_map_entry_free flush remaining packets in
  422. * percpu bulkq to queue. Due to caller map_delete_elem() disable
  423. * preemption, cannot call kthread_stop() to make sure queue is empty.
  424. * Instead a work_queue is started for stopping kthread,
  425. * cpu_map_kthread_stop, which waits for an RCU grace period before
  426. * stopping kthread, emptying the queue.
  427. */
  428. static void __cpu_map_entry_replace(struct bpf_cpu_map *cmap,
  429. u32 key_cpu, struct bpf_cpu_map_entry *rcpu)
  430. {
  431. struct bpf_cpu_map_entry *old_rcpu;
  432. old_rcpu = xchg(&cmap->cpu_map[key_cpu], rcpu);
  433. if (old_rcpu) {
  434. call_rcu(&old_rcpu->rcu, __cpu_map_entry_free);
  435. INIT_WORK(&old_rcpu->kthread_stop_wq, cpu_map_kthread_stop);
  436. schedule_work(&old_rcpu->kthread_stop_wq);
  437. }
  438. }
  439. static int cpu_map_delete_elem(struct bpf_map *map, void *key)
  440. {
  441. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  442. u32 key_cpu = *(u32 *)key;
  443. if (key_cpu >= map->max_entries)
  444. return -EINVAL;
  445. /* notice caller map_delete_elem() use preempt_disable() */
  446. __cpu_map_entry_replace(cmap, key_cpu, NULL);
  447. return 0;
  448. }
  449. static int cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
  450. u64 map_flags)
  451. {
  452. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  453. struct bpf_cpumap_val cpumap_value = {};
  454. struct bpf_cpu_map_entry *rcpu;
  455. /* Array index key correspond to CPU number */
  456. u32 key_cpu = *(u32 *)key;
  457. memcpy(&cpumap_value, value, map->value_size);
  458. if (unlikely(map_flags > BPF_EXIST))
  459. return -EINVAL;
  460. if (unlikely(key_cpu >= cmap->map.max_entries))
  461. return -E2BIG;
  462. if (unlikely(map_flags == BPF_NOEXIST))
  463. return -EEXIST;
  464. if (unlikely(cpumap_value.qsize > 16384)) /* sanity limit on qsize */
  465. return -EOVERFLOW;
  466. /* Make sure CPU is a valid possible cpu */
  467. if (key_cpu >= nr_cpumask_bits || !cpu_possible(key_cpu))
  468. return -ENODEV;
  469. if (cpumap_value.qsize == 0) {
  470. rcpu = NULL; /* Same as deleting */
  471. } else {
  472. /* Updating qsize cause re-allocation of bpf_cpu_map_entry */
  473. rcpu = __cpu_map_entry_alloc(&cpumap_value, key_cpu, map->id);
  474. if (!rcpu)
  475. return -ENOMEM;
  476. rcpu->cmap = cmap;
  477. }
  478. rcu_read_lock();
  479. __cpu_map_entry_replace(cmap, key_cpu, rcpu);
  480. rcu_read_unlock();
  481. return 0;
  482. }
  483. static void cpu_map_free(struct bpf_map *map)
  484. {
  485. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  486. u32 i;
  487. /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  488. * so the bpf programs (can be more than one that used this map) were
  489. * disconnected from events. Wait for outstanding critical sections in
  490. * these programs to complete. The rcu critical section only guarantees
  491. * no further "XDP/bpf-side" reads against bpf_cpu_map->cpu_map.
  492. * It does __not__ ensure pending flush operations (if any) are
  493. * complete.
  494. */
  495. bpf_clear_redirect_map(map);
  496. synchronize_rcu();
  497. /* For cpu_map the remote CPUs can still be using the entries
  498. * (struct bpf_cpu_map_entry).
  499. */
  500. for (i = 0; i < cmap->map.max_entries; i++) {
  501. struct bpf_cpu_map_entry *rcpu;
  502. rcpu = READ_ONCE(cmap->cpu_map[i]);
  503. if (!rcpu)
  504. continue;
  505. /* bq flush and cleanup happens after RCU grace-period */
  506. __cpu_map_entry_replace(cmap, i, NULL); /* call_rcu */
  507. }
  508. bpf_map_area_free(cmap->cpu_map);
  509. kfree(cmap);
  510. }
  511. struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
  512. {
  513. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  514. struct bpf_cpu_map_entry *rcpu;
  515. if (key >= map->max_entries)
  516. return NULL;
  517. rcpu = READ_ONCE(cmap->cpu_map[key]);
  518. return rcpu;
  519. }
  520. static void *cpu_map_lookup_elem(struct bpf_map *map, void *key)
  521. {
  522. struct bpf_cpu_map_entry *rcpu =
  523. __cpu_map_lookup_elem(map, *(u32 *)key);
  524. return rcpu ? &rcpu->value : NULL;
  525. }
  526. static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  527. {
  528. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  529. u32 index = key ? *(u32 *)key : U32_MAX;
  530. u32 *next = next_key;
  531. if (index >= cmap->map.max_entries) {
  532. *next = 0;
  533. return 0;
  534. }
  535. if (index == cmap->map.max_entries - 1)
  536. return -ENOENT;
  537. *next = index + 1;
  538. return 0;
  539. }
  540. static int cpu_map_btf_id;
  541. const struct bpf_map_ops cpu_map_ops = {
  542. .map_meta_equal = bpf_map_meta_equal,
  543. .map_alloc = cpu_map_alloc,
  544. .map_free = cpu_map_free,
  545. .map_delete_elem = cpu_map_delete_elem,
  546. .map_update_elem = cpu_map_update_elem,
  547. .map_lookup_elem = cpu_map_lookup_elem,
  548. .map_get_next_key = cpu_map_get_next_key,
  549. .map_check_btf = map_check_no_btf,
  550. .map_btf_name = "bpf_cpu_map",
  551. .map_btf_id = &cpu_map_btf_id,
  552. };
  553. static void bq_flush_to_queue(struct xdp_bulk_queue *bq)
  554. {
  555. struct bpf_cpu_map_entry *rcpu = bq->obj;
  556. unsigned int processed = 0, drops = 0;
  557. const int to_cpu = rcpu->cpu;
  558. struct ptr_ring *q;
  559. int i;
  560. if (unlikely(!bq->count))
  561. return;
  562. q = rcpu->queue;
  563. spin_lock(&q->producer_lock);
  564. for (i = 0; i < bq->count; i++) {
  565. struct xdp_frame *xdpf = bq->q[i];
  566. int err;
  567. err = __ptr_ring_produce(q, xdpf);
  568. if (err) {
  569. drops++;
  570. xdp_return_frame_rx_napi(xdpf);
  571. }
  572. processed++;
  573. }
  574. bq->count = 0;
  575. spin_unlock(&q->producer_lock);
  576. __list_del_clearprev(&bq->flush_node);
  577. /* Feedback loop via tracepoints */
  578. trace_xdp_cpumap_enqueue(rcpu->map_id, processed, drops, to_cpu);
  579. }
  580. /* Runs under RCU-read-side, plus in softirq under NAPI protection.
  581. * Thus, safe percpu variable access.
  582. */
  583. static void bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf)
  584. {
  585. struct list_head *flush_list = this_cpu_ptr(&cpu_map_flush_list);
  586. struct xdp_bulk_queue *bq = this_cpu_ptr(rcpu->bulkq);
  587. if (unlikely(bq->count == CPU_MAP_BULK_SIZE))
  588. bq_flush_to_queue(bq);
  589. /* Notice, xdp_buff/page MUST be queued here, long enough for
  590. * driver to code invoking us to finished, due to driver
  591. * (e.g. ixgbe) recycle tricks based on page-refcnt.
  592. *
  593. * Thus, incoming xdp_frame is always queued here (else we race
  594. * with another CPU on page-refcnt and remaining driver code).
  595. * Queue time is very short, as driver will invoke flush
  596. * operation, when completing napi->poll call.
  597. */
  598. bq->q[bq->count++] = xdpf;
  599. if (!bq->flush_node.prev)
  600. list_add(&bq->flush_node, flush_list);
  601. }
  602. int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
  603. struct net_device *dev_rx)
  604. {
  605. struct xdp_frame *xdpf;
  606. xdpf = xdp_convert_buff_to_frame(xdp);
  607. if (unlikely(!xdpf))
  608. return -EOVERFLOW;
  609. /* Info needed when constructing SKB on remote CPU */
  610. xdpf->dev_rx = dev_rx;
  611. bq_enqueue(rcpu, xdpf);
  612. return 0;
  613. }
  614. void __cpu_map_flush(void)
  615. {
  616. struct list_head *flush_list = this_cpu_ptr(&cpu_map_flush_list);
  617. struct xdp_bulk_queue *bq, *tmp;
  618. list_for_each_entry_safe(bq, tmp, flush_list, flush_node) {
  619. bq_flush_to_queue(bq);
  620. /* If already running, costs spin_lock_irqsave + smb_mb */
  621. wake_up_process(bq->obj->kthread);
  622. }
  623. }
  624. static int __init cpu_map_init(void)
  625. {
  626. int cpu;
  627. for_each_possible_cpu(cpu)
  628. INIT_LIST_HEAD(&per_cpu(cpu_map_flush_list, cpu));
  629. return 0;
  630. }
  631. subsys_initcall(cpu_map_init);