@@ -53,6 +53,7 @@ struct bpf_dtab_netdev {
5353struct bpf_dtab {
5454 struct bpf_map map ;
5555 struct bpf_dtab_netdev * * netdev_map ;
56+ unsigned long int __percpu * flush_needed ;
5657};
5758
5859static struct bpf_map * dev_map_alloc (union bpf_attr * attr )
@@ -87,6 +88,7 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
8788
8889 /* make sure page count doesn't overflow */
8990 cost = (u64 ) dtab -> map .max_entries * sizeof (struct bpf_dtab_netdev * );
91+ cost += BITS_TO_LONGS (attr -> max_entries ) * sizeof (unsigned long );
9092 if (cost >= U32_MAX - PAGE_SIZE )
9193 goto free_dtab ;
9294
@@ -97,6 +99,14 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
9799 if (err )
98100 goto free_dtab ;
99101
102+ /* A per cpu bitfield with a bit per possible net device */
103+ dtab -> flush_needed = __alloc_percpu (
104+ BITS_TO_LONGS (attr -> max_entries ) *
105+ sizeof (unsigned long ),
106+ __alignof__(unsigned long ));
107+ if (!dtab -> flush_needed )
108+ goto free_dtab ;
109+
100110 dtab -> netdev_map = bpf_map_area_alloc (dtab -> map .max_entries *
101111 sizeof (struct bpf_dtab_netdev * ));
102112 if (!dtab -> netdev_map )
@@ -105,14 +115,15 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
105115 return & dtab -> map ;
106116
107117free_dtab :
118+ free_percpu (dtab -> flush_needed );
108119 kfree (dtab );
109120 return ERR_PTR (err );
110121}
111122
112123static void dev_map_free (struct bpf_map * map )
113124{
114125 struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
115- int i ;
126+ int i , cpu ;
116127
117128 /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
118129 * so the programs (can be more than one that used this map) were
@@ -123,6 +134,18 @@ static void dev_map_free(struct bpf_map *map)
123134 */
124135 synchronize_rcu ();
125136
137+ /* To ensure all pending flush operations have completed wait for flush
138+ * bitmap to indicate all flush_needed bits to be zero on _all_ cpus.
139+ * Because the above synchronize_rcu() ensures the map is disconnected
140+ * from the program we can assume no new bits will be set.
141+ */
142+ for_each_online_cpu (cpu ) {
143+ unsigned long * bitmap = per_cpu_ptr (dtab -> flush_needed , cpu );
144+
145+ while (!bitmap_empty (bitmap , dtab -> map .max_entries ))
146+ cpu_relax ();
147+ }
148+
126149 for (i = 0 ; i < dtab -> map .max_entries ; i ++ ) {
127150 struct bpf_dtab_netdev * dev ;
128151
@@ -137,6 +160,7 @@ static void dev_map_free(struct bpf_map *map)
137160 /* At this point bpf program is detached and all pending operations
138161 * _must_ be complete
139162 */
163+ free_percpu (dtab -> flush_needed );
140164 bpf_map_area_free (dtab -> netdev_map );
141165 kfree (dtab );
142166}
@@ -159,6 +183,14 @@ static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
159183 return 0 ;
160184}
161185
186+ void __dev_map_insert_ctx (struct bpf_map * map , u32 key )
187+ {
188+ struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
189+ unsigned long * bitmap = this_cpu_ptr (dtab -> flush_needed );
190+
191+ __set_bit (key , bitmap );
192+ }
193+
162194struct net_device * __dev_map_lookup_elem (struct bpf_map * map , u32 key )
163195{
164196 struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
@@ -171,6 +203,39 @@ struct net_device *__dev_map_lookup_elem(struct bpf_map *map, u32 key)
171203 return dev ? dev -> dev : NULL ;
172204}
173205
206+ /* __dev_map_flush is called from xdp_do_flush_map() which _must_ be signaled
207+ * from the driver before returning from its napi->poll() routine. The poll()
208+ * routine is called either from busy_poll context or net_rx_action signaled
209+ * from NET_RX_SOFTIRQ. Either way the poll routine must complete before the
210+ * net device can be torn down. On devmap tear down we ensure the ctx bitmap
211+ * is zeroed before completing to ensure all flush operations have completed.
212+ */
213+ void __dev_map_flush (struct bpf_map * map )
214+ {
215+ struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
216+ unsigned long * bitmap = this_cpu_ptr (dtab -> flush_needed );
217+ u32 bit ;
218+
219+ for_each_set_bit (bit , bitmap , map -> max_entries ) {
220+ struct bpf_dtab_netdev * dev = READ_ONCE (dtab -> netdev_map [bit ]);
221+ struct net_device * netdev ;
222+
223+ /* This is possible if the dev entry is removed by user space
224+ * between xdp redirect and flush op.
225+ */
226+ if (unlikely (!dev ))
227+ continue ;
228+
229+ netdev = dev -> dev ;
230+
231+ __clear_bit (bit , bitmap );
232+ if (unlikely (!netdev || !netdev -> netdev_ops -> ndo_xdp_flush ))
233+ continue ;
234+
235+ netdev -> netdev_ops -> ndo_xdp_flush (netdev );
236+ }
237+ }
238+
174239/* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
175240 * update happens in parallel here a dev_put wont happen until after reading the
176241 * ifindex.
@@ -188,11 +253,28 @@ static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
188253 return dev ? & dev -> dev -> ifindex : NULL ;
189254}
190255
256+ static void dev_map_flush_old (struct bpf_dtab_netdev * old_dev )
257+ {
258+ if (old_dev -> dev -> netdev_ops -> ndo_xdp_flush ) {
259+ struct net_device * fl = old_dev -> dev ;
260+ unsigned long * bitmap ;
261+ int cpu ;
262+
263+ for_each_online_cpu (cpu ) {
264+ bitmap = per_cpu_ptr (old_dev -> dtab -> flush_needed , cpu );
265+ __clear_bit (old_dev -> key , bitmap );
266+
267+ fl -> netdev_ops -> ndo_xdp_flush (old_dev -> dev );
268+ }
269+ }
270+ }
271+
191272static void __dev_map_entry_free (struct rcu_head * rcu )
192273{
193274 struct bpf_dtab_netdev * old_dev ;
194275
195276 old_dev = container_of (rcu , struct bpf_dtab_netdev , rcu );
277+ dev_map_flush_old (old_dev );
196278 dev_put (old_dev -> dev );
197279 kfree (old_dev );
198280}
0 commit comments