cpumap.c 21 KB

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