History

xdp: Make cpumap flush_list common for all map instances

The cpumap flush list is used to track entries that need to flushed from via the xdp_do_flush_map() function. This list used to be per-map, but there is really no reason for that. Instead make the flush list global for all devmaps, which simplifies __cpu_map_flush() and cpu_map_alloc().

Björn Töpel committed 6 years ago

Commit cdfafe91 parent 96360003 files · +21 −21Browse files

Unified · Split

3 files
  1. include/linux/bpf.h +2 −2
  2. kernel/bpf/cpumap.c +18 −18
  3. net/core/filter.c +1 −1
include/linux/bpf.h+2−2
@@ -966,7 +966,7 @@ int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb, · 965 unmodified lines
966966 struct bpf_prog *xdp_prog);
967967
968968 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
969−void __cpu_map_flush(struct bpf_map *map);
969+void __cpu_map_flush(void);
970970 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
971971 struct net_device *dev_rx);
972972
@@ -1097,7 +1097,7 @@ struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key) · 124 unmodified lines
10971097 return NULL;
10981098 }
10991099
1100−static inline void __cpu_map_flush(struct bpf_map *map)
1100+static inline void __cpu_map_flush(void)
11011101 {
11021102 }
11031103
kernel/bpf/cpumap.c+18−18
@@ -72,17 +72,18 @@ struct bpf_cpu_map { · 71 unmodified lines
7272 struct bpf_map map;
7373 /* Below members specific for map type */
7474 struct bpf_cpu_map_entry **cpu_map;
75− struct list_head __percpu *flush_list;
7675 };
7776
77+static DEFINE_PER_CPU(struct list_head, cpu_map_flush_list);
78+
7879 static int bq_flush_to_queue(struct xdp_bulk_queue *bq);
7980
8081 static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
8182 {
8283 struct bpf_cpu_map *cmap;
8384 int err = -ENOMEM;
84− int ret, cpu;
8585 u64 cost;
86+ int ret;
8687
8788 if (!capable(CAP_SYS_ADMIN))
8889 return ERR_PTR(-EPERM);
@@ -106,7 +107,6 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr) · 17 unmodified lines
106107
107108 /* make sure page count doesn't overflow */
108109 cost = (u64) cmap->map.max_entries * sizeof(struct bpf_cpu_map_entry *);
109− cost += sizeof(struct list_head) * num_possible_cpus();
110110
111111 /* Notice returns -EPERM on if map size is larger than memlock limit */
112112 ret = bpf_map_charge_init(&cmap->map.memory, cost);
@@ -115,23 +115,14 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr) · 2 unmodified lines
115115 goto free_cmap;
116116 }
117117
118− cmap->flush_list = alloc_percpu(struct list_head);
119− if (!cmap->flush_list)
120− goto free_charge;
121−
122− for_each_possible_cpu(cpu)
123− INIT_LIST_HEAD(per_cpu_ptr(cmap->flush_list, cpu));
124−
125118 /* Alloc array for possible remote "destination" CPUs */
126119 cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
127120 sizeof(struct bpf_cpu_map_entry *),
128121 cmap->map.numa_node);
129122 if (!cmap->cpu_map)
130− goto free_percpu;
123+ goto free_charge;
131124
132125 return &cmap->map;
133−free_percpu:
134− free_percpu(cmap->flush_list);
135126 free_charge:
136127 bpf_map_charge_finish(&cmap->map.memory);
137128 free_cmap:
@@ -526,7 +517,6 @@ static void cpu_map_free(struct bpf_map *map) · 388 unmodified lines
526517 /* bq flush and cleanup happens after RCU grace-period */
527518 __cpu_map_entry_replace(cmap, i, NULL); /* call_rcu */
528519 }
529− free_percpu(cmap->flush_list);
530520 bpf_map_area_free(cmap->cpu_map);
531521 kfree(cmap);
532522 }
@@ -618,7 +608,7 @@ static int bq_flush_to_queue(struct xdp_bulk_queue *bq) · 85 unmodified lines
618608 */
619609 static int bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf)
620610 {
621− struct list_head *flush_list = this_cpu_ptr(rcpu->cmap->flush_list);
611+ struct list_head *flush_list = this_cpu_ptr(&cpu_map_flush_list);
622612 struct xdp_bulk_queue *bq = this_cpu_ptr(rcpu->bulkq);
623613
624614 if (unlikely(bq->count == CPU_MAP_BULK_SIZE))
@@ -657,10 +647,9 @@ int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp, · 32 unmodified lines
657647 return 0;
658648 }
659649
660−void __cpu_map_flush(struct bpf_map *map)
650+void __cpu_map_flush(void)
661651 {
662− struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
663− struct list_head *flush_list = this_cpu_ptr(cmap->flush_list);
652+ struct list_head *flush_list = this_cpu_ptr(&cpu_map_flush_list);
664653 struct xdp_bulk_queue *bq, *tmp;
665654
666655 list_for_each_entry_safe(bq, tmp, flush_list, flush_node) {
@@ -670,3 +659,14 @@ void __cpu_map_flush(struct bpf_map *map) · 3 unmodified lines
670659 wake_up_process(bq->obj->kthread);
671660 }
672661 }
662+
663+static int __init cpu_map_init(void)
664+{
665+ int cpu;
666+
667+ for_each_possible_cpu(cpu)
668+ INIT_LIST_HEAD(&per_cpu(cpu_map_flush_list, cpu));
669+ return 0;
670+}
671+
672+subsys_initcall(cpu_map_init);
net/core/filter.c+1−1
@@ -3558,7 +3558,7 @@ void xdp_do_flush_map(void) · 3557 unmodified lines
35583558 __dev_map_flush();
35593559 break;
35603560 case BPF_MAP_TYPE_CPUMAP:
3561− __cpu_map_flush(map);
3561+ __cpu_map_flush();
35623562 break;
35633563 case BPF_MAP_TYPE_XSKMAP:
35643564 __xsk_map_flush();