Skip to content

Commit bba1dc0

Browse files
author
Alexei Starovoitov
committed
bpf: Remove redundant synchronize_rcu.
bpf_free_used_maps() or close(map_fd) will trigger map_free callback. bpf_free_used_maps() is called after bpf prog is no longer executing: bpf_prog_put->call_rcu->bpf_prog_free->bpf_free_used_maps. Hence there is no need to call synchronize_rcu() to protect map elements. Note that hash_of_maps and array_of_maps update/delete inner maps via sys_bpf() that calls maybe_wait_bpf_programs() and synchronize_rcu(). Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Paul E. McKenney <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8c18311 commit bba1dc0

File tree

7 files changed

+3
-38
lines changed

7 files changed

+3
-38
lines changed

kernel/bpf/arraymap.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,6 @@ static void array_map_free(struct bpf_map *map)
386386
{
387387
struct bpf_array *array = container_of(map, struct bpf_array, map);
388388

389-
/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
390-
* so the programs (can be more than one that used this map) were
391-
* disconnected from events. Wait for outstanding programs to complete
392-
* and free the array
393-
*/
394-
synchronize_rcu();
395-
396389
if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
397390
bpf_array_free_percpu(array);
398391

@@ -546,8 +539,6 @@ static void fd_array_map_free(struct bpf_map *map)
546539
struct bpf_array *array = container_of(map, struct bpf_array, map);
547540
int i;
548541

549-
synchronize_rcu();
550-
551542
/* make sure it's empty */
552543
for (i = 0; i < array->map.max_entries; i++)
553544
BUG_ON(array->ptrs[i] != NULL);

kernel/bpf/hashtab.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,12 +1290,10 @@ static void htab_map_free(struct bpf_map *map)
12901290
{
12911291
struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
12921292

1293-
/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1294-
* so the programs (can be more than one that used this map) were
1295-
* disconnected from events. Wait for outstanding critical sections in
1296-
* these programs to complete
1293+
/* bpf_free_used_maps() or close(map_fd) will trigger this map_free callback.
1294+
* bpf_free_used_maps() is called after bpf prog is no longer executing.
1295+
* There is no need to synchronize_rcu() here to protect map elements.
12971296
*/
1298-
synchronize_rcu();
12991297

13001298
/* some of free_htab_elem() callbacks for elements of this map may
13011299
* not have executed. Wait for them.

kernel/bpf/lpm_trie.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,6 @@ static void trie_free(struct bpf_map *map)
589589
struct lpm_trie_node __rcu **slot;
590590
struct lpm_trie_node *node;
591591

592-
/* Wait for outstanding programs to complete
593-
* update/lookup/delete/get_next_key and free the trie.
594-
*/
595-
synchronize_rcu();
596-
597592
/* Always start at the root and walk down to a node that has no
598593
* children. Then free that node, nullify its reference in the parent
599594
* and start over.

kernel/bpf/queue_stack_maps.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ static void queue_stack_map_free(struct bpf_map *map)
101101
{
102102
struct bpf_queue_stack *qs = bpf_queue_stack(map);
103103

104-
/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
105-
* so the programs (can be more than one that used this map) were
106-
* disconnected from events. Wait for outstanding critical sections in
107-
* these programs to complete
108-
*/
109-
synchronize_rcu();
110-
111104
bpf_map_area_free(qs);
112105
}
113106

kernel/bpf/reuseport_array.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ static void reuseport_array_free(struct bpf_map *map)
9696
struct sock *sk;
9797
u32 i;
9898

99-
synchronize_rcu();
100-
10199
/*
102100
* ops->map_*_elem() will not be able to access this
103101
* array now. Hence, this function only races with

kernel/bpf/ringbuf.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,6 @@ static void ringbuf_map_free(struct bpf_map *map)
215215
{
216216
struct bpf_ringbuf_map *rb_map;
217217

218-
/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
219-
* so the programs (can be more than one that used this map) were
220-
* disconnected from events. Wait for outstanding critical sections in
221-
* these programs to complete
222-
*/
223-
synchronize_rcu();
224-
225218
rb_map = container_of(map, struct bpf_ringbuf_map, map);
226219
bpf_ringbuf_free(rb_map->rb);
227220
kfree(rb_map);

kernel/bpf/stackmap.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,6 @@ static void stack_map_free(struct bpf_map *map)
604604
{
605605
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
606606

607-
/* wait for bpf programs to complete before freeing stack map */
608-
synchronize_rcu();
609-
610607
bpf_map_area_free(smap->elems);
611608
pcpu_freelist_destroy(&smap->freelist);
612609
bpf_map_area_free(smap);

0 commit comments

Comments
 (0)