Skip to content

Commit 34d9615

Browse files
Daniel Colascionegregkh
authored andcommitted
bpf: wait for running BPF programs when updating map-in-map
commit 1ae80cf upstream. The map-in-map frequently serves as a mechanism for atomic snapshotting of state that a BPF program might record. The current implementation is dangerous to use in this way, however, since userspace has no way of knowing when all programs that might have retrieved the "old" value of the map may have completed. This change ensures that map update operations on map-in-map map types always wait for all references to the old map to drop before returning to userspace. Signed-off-by: Daniel Colascione <dancol@google.com> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Chenbo Feng <fengc@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9a7a80f commit 34d9615

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎kernel/bpf/syscall.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,17 @@ static int map_lookup_elem(union bpf_attr *attr)
741741
return err;
742742
}
743743

744+
static void maybe_wait_bpf_programs(struct bpf_map *map)
745+
{
746+
/* Wait for any running BPF programs to complete so that
747+
* userspace, when we return to it, knows that all programs
748+
* that could be running use the new map value.
749+
*/
750+
if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
751+
map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
752+
synchronize_rcu();
753+
}
754+
744755
#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
745756

746757
static int map_update_elem(union bpf_attr *attr)
@@ -831,6 +842,7 @@ static int map_update_elem(union bpf_attr *attr)
831842
}
832843
__this_cpu_dec(bpf_prog_active);
833844
preempt_enable();
845+
maybe_wait_bpf_programs(map);
834846
out:
835847
free_value:
836848
kfree(value);
@@ -883,6 +895,7 @@ static int map_delete_elem(union bpf_attr *attr)
883895
rcu_read_unlock();
884896
__this_cpu_dec(bpf_prog_active);
885897
preempt_enable();
898+
maybe_wait_bpf_programs(map);
886899
out:
887900
kfree(key);
888901
err_put:

0 commit comments

Comments
 (0)