4848 * calls will fail at this point.
4949 */
5050#include <linux/bpf.h>
51- #include <linux/jhash.h>
5251#include <linux/filter.h>
53- #include <linux/rculist_nulls.h>
54- #include "percpu_freelist.h"
55- #include "bpf_lru_list.h"
56- #include "map_in_map.h"
5752
5853struct bpf_dtab_netdev {
5954 struct net_device * dev ;
60- int key ;
61- struct rcu_head rcu ;
6255 struct bpf_dtab * dtab ;
56+ unsigned int bit ;
57+ struct rcu_head rcu ;
6358};
6459
6560struct bpf_dtab {
6661 struct bpf_map map ;
6762 struct bpf_dtab_netdev * * netdev_map ;
68- unsigned long int __percpu * flush_needed ;
63+ unsigned long __percpu * flush_needed ;
6964 struct list_head list ;
7065};
7166
7267static DEFINE_SPINLOCK (dev_map_lock );
7368static LIST_HEAD (dev_map_list );
7469
70+ static u64 dev_map_bitmap_size (const union bpf_attr * attr )
71+ {
72+ return BITS_TO_LONGS (attr -> max_entries ) * sizeof (unsigned long );
73+ }
74+
7575static struct bpf_map * dev_map_alloc (union bpf_attr * attr )
7676{
7777 struct bpf_dtab * dtab ;
@@ -95,11 +95,9 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
9595 dtab -> map .map_flags = attr -> map_flags ;
9696 dtab -> map .numa_node = bpf_map_attr_numa_node (attr );
9797
98- err = - ENOMEM ;
99-
10098 /* make sure page count doesn't overflow */
10199 cost = (u64 ) dtab -> map .max_entries * sizeof (struct bpf_dtab_netdev * );
102- cost += BITS_TO_LONGS (attr -> max_entries ) * sizeof ( unsigned long );
100+ cost += dev_map_bitmap_size (attr ) * num_possible_cpus ( );
103101 if (cost >= U32_MAX - PAGE_SIZE )
104102 goto free_dtab ;
105103
@@ -110,12 +108,9 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
110108 if (err )
111109 goto free_dtab ;
112110
113- err = - ENOMEM ;
114111 /* A per cpu bitfield with a bit per possible net device */
115- dtab -> flush_needed = __alloc_percpu (
116- BITS_TO_LONGS (attr -> max_entries ) *
117- sizeof (unsigned long ),
118- __alignof__(unsigned long ));
112+ dtab -> flush_needed = __alloc_percpu (dev_map_bitmap_size (attr ),
113+ __alignof__(unsigned long ));
119114 if (!dtab -> flush_needed )
120115 goto free_dtab ;
121116
@@ -128,12 +123,12 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
128123 spin_lock (& dev_map_lock );
129124 list_add_tail_rcu (& dtab -> list , & dev_map_list );
130125 spin_unlock (& dev_map_lock );
131- return & dtab -> map ;
132126
127+ return & dtab -> map ;
133128free_dtab :
134129 free_percpu (dtab -> flush_needed );
135130 kfree (dtab );
136- return ERR_PTR (err );
131+ return ERR_PTR (- ENOMEM );
137132}
138133
139134static void dev_map_free (struct bpf_map * map )
@@ -178,9 +173,6 @@ static void dev_map_free(struct bpf_map *map)
178173 kfree (dev );
179174 }
180175
181- /* At this point bpf program is detached and all pending operations
182- * _must_ be complete
183- */
184176 free_percpu (dtab -> flush_needed );
185177 bpf_map_area_free (dtab -> netdev_map );
186178 kfree (dtab );
@@ -190,7 +182,7 @@ static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
190182{
191183 struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
192184 u32 index = key ? * (u32 * )key : U32_MAX ;
193- u32 * next = ( u32 * ) next_key ;
185+ u32 * next = next_key ;
194186
195187 if (index >= dtab -> map .max_entries ) {
196188 * next = 0 ;
@@ -199,29 +191,16 @@ static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
199191
200192 if (index == dtab -> map .max_entries - 1 )
201193 return - ENOENT ;
202-
203194 * next = index + 1 ;
204195 return 0 ;
205196}
206197
207- void __dev_map_insert_ctx (struct bpf_map * map , u32 key )
198+ void __dev_map_insert_ctx (struct bpf_map * map , u32 bit )
208199{
209200 struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
210201 unsigned long * bitmap = this_cpu_ptr (dtab -> flush_needed );
211202
212- __set_bit (key , bitmap );
213- }
214-
215- struct net_device * __dev_map_lookup_elem (struct bpf_map * map , u32 key )
216- {
217- struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
218- struct bpf_dtab_netdev * dev ;
219-
220- if (key >= map -> max_entries )
221- return NULL ;
222-
223- dev = READ_ONCE (dtab -> netdev_map [key ]);
224- return dev ? dev -> dev : NULL ;
203+ __set_bit (bit , bitmap );
225204}
226205
227206/* __dev_map_flush is called from xdp_do_flush_map() which _must_ be signaled
@@ -248,7 +227,6 @@ void __dev_map_flush(struct bpf_map *map)
248227 continue ;
249228
250229 netdev = dev -> dev ;
251-
252230 __clear_bit (bit , bitmap );
253231 if (unlikely (!netdev || !netdev -> netdev_ops -> ndo_xdp_flush ))
254232 continue ;
@@ -261,43 +239,49 @@ void __dev_map_flush(struct bpf_map *map)
261239 * update happens in parallel here a dev_put wont happen until after reading the
262240 * ifindex.
263241 */
264- static void * dev_map_lookup_elem (struct bpf_map * map , void * key )
242+ struct net_device * __dev_map_lookup_elem (struct bpf_map * map , u32 key )
265243{
266244 struct bpf_dtab * dtab = container_of (map , struct bpf_dtab , map );
267245 struct bpf_dtab_netdev * dev ;
268- u32 i = * (u32 * )key ;
269246
270- if (i >= map -> max_entries )
247+ if (key >= map -> max_entries )
271248 return NULL ;
272249
273- dev = READ_ONCE (dtab -> netdev_map [i ]);
274- return dev ? & dev -> dev -> ifindex : NULL ;
250+ dev = READ_ONCE (dtab -> netdev_map [key ]);
251+ return dev ? dev -> dev : NULL ;
275252}
276253
277- static void dev_map_flush_old (struct bpf_dtab_netdev * old_dev )
254+ static void * dev_map_lookup_elem (struct bpf_map * map , void * key )
255+ {
256+ struct net_device * dev = __dev_map_lookup_elem (map , * (u32 * )key );
257+
258+ return dev ? & dev -> ifindex : NULL ;
259+ }
260+
261+ static void dev_map_flush_old (struct bpf_dtab_netdev * dev )
278262{
279- if (old_dev -> dev -> netdev_ops -> ndo_xdp_flush ) {
280- struct net_device * fl = old_dev -> dev ;
263+ if (dev -> dev -> netdev_ops -> ndo_xdp_flush ) {
264+ struct net_device * fl = dev -> dev ;
281265 unsigned long * bitmap ;
282266 int cpu ;
283267
284268 for_each_online_cpu (cpu ) {
285- bitmap = per_cpu_ptr (old_dev -> dtab -> flush_needed , cpu );
286- __clear_bit (old_dev -> key , bitmap );
269+ bitmap = per_cpu_ptr (dev -> dtab -> flush_needed , cpu );
270+ __clear_bit (dev -> bit , bitmap );
287271
288- fl -> netdev_ops -> ndo_xdp_flush (old_dev -> dev );
272+ fl -> netdev_ops -> ndo_xdp_flush (dev -> dev );
289273 }
290274 }
291275}
292276
293277static void __dev_map_entry_free (struct rcu_head * rcu )
294278{
295- struct bpf_dtab_netdev * old_dev ;
279+ struct bpf_dtab_netdev * dev ;
296280
297- old_dev = container_of (rcu , struct bpf_dtab_netdev , rcu );
298- dev_map_flush_old (old_dev );
299- dev_put (old_dev -> dev );
300- kfree (old_dev );
281+ dev = container_of (rcu , struct bpf_dtab_netdev , rcu );
282+ dev_map_flush_old (dev );
283+ dev_put (dev -> dev );
284+ kfree (dev );
301285}
302286
303287static int dev_map_delete_elem (struct bpf_map * map , void * key )
@@ -309,8 +293,8 @@ static int dev_map_delete_elem(struct bpf_map *map, void *key)
309293 if (k >= map -> max_entries )
310294 return - EINVAL ;
311295
312- /* Use synchronize_rcu () here to ensure any rcu critical sections
313- * have completed, but this does not guarantee a flush has happened
296+ /* Use call_rcu () here to ensure any rcu critical sections have
297+ * completed, but this does not guarantee a flush has happened
314298 * yet. Because driver side rcu_read_lock/unlock only protects the
315299 * running XDP program. However, for pending flush operations the
316300 * dev and ctx are stored in another per cpu map. And additionally,
@@ -334,10 +318,8 @@ static int dev_map_update_elem(struct bpf_map *map, void *key, void *value,
334318
335319 if (unlikely (map_flags > BPF_EXIST ))
336320 return - EINVAL ;
337-
338321 if (unlikely (i >= dtab -> map .max_entries ))
339322 return - E2BIG ;
340-
341323 if (unlikely (map_flags == BPF_NOEXIST ))
342324 return - EEXIST ;
343325
@@ -355,7 +337,7 @@ static int dev_map_update_elem(struct bpf_map *map, void *key, void *value,
355337 return - EINVAL ;
356338 }
357339
358- dev -> key = i ;
340+ dev -> bit = i ;
359341 dev -> dtab = dtab ;
360342 }
361343
0 commit comments