bpf: Synchronize dispatcher update with bpf_dispatcher_xdp_func
Jiri Olsa
reviewerPATCH 1/1 bpf: Synchronize dispatcher update with bpf_dispatcher_xdp_func
Hao Sun reported crash in dispatcher image [1].
Currently we don't have any sync between bpf_dispatcher_update and bpf_dispatcher_xdp_func, so following race is possible:
cpu 0: cpu 1:
bpf_prog_run_xdp
...
bpf_dispatcher_xdp_func
in image at offset 0x0
bpf_dispatcher_update
update image at offset 0x800
bpf_dispatcher_update
update image at offset 0x0
in image at offset 0x0 -> crashFixing this by synchronizing dispatcher image update (which is done in bpf_dispatcher_update function) with bpf_dispatcher_xdp_func that reads and execute the dispatcher image.
Calling synchronize_rcu after updating and installing new image ensures that readers leave old image before it's changed in the next dispatcher update. The update itself is locked with dispatcher's mutex.
The bpf_prog_run_xdp is called under local_bh_disable and synchronize_rcu will wait for it to leave [2].
[1] https://lore.kernel.org/bpf/Y5SFho7ZYXr9ifRn@krava/T/#m00c29ece654bc9f332a17df493bbca33e702896c
[2] https://lore.kernel.org/bpf/[email protected]/T/#mff43e2c003ae99f4a38f353c7969be4c7162e877
--- kernel/bpf/dispatcher.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/bpf/dispatcher.c +5 -0
@@ -125,6 +125,11 @@ static void bpf_dispatcher_update(struct bpf_dispatcher *d, int prev_num_progs)__BPF_DISPATCHER_UPDATE (d, new ?: (void *)&bpf_dispatcher_nop_func); +/* Make sure all the callers executing the previous/old half of the +* image leave it, so following update call can modify it safely. +*/ +synchronize_rcu (); +if (new) d->image_off = noff; }
Signature · 1 line
2.38.1
Quoted 27 lines · Jiri Olsa
Hao Sun reported crash in dispatcher image [1].
Currently we don't have any sync between
bpf_dispatcher_updateandbpf_dispatcher_xdp_func, so following race is possible:cpu 0: cpu 1:
bpf_prog_run_xdp... bpf_dispatcher_xdp_func in image at offset 0x0 bpf_dispatcher_update update image at offset 0x800 bpf_dispatcher_update update image at offset 0x0 in image at offset 0x0 -> crashFixing this by synchronizing dispatcher image update (which is done in
bpf_dispatcher_updatefunction) withbpf_dispatcher_xdp_functhat reads and execute the dispatcher image.Calling
synchronize_rcuafter updating and installing new image ensures that readers leave old image before it's changed in the next dispatcher update. The update itself is locked with dispatcher's mutex.
Quoted 8 lines
The
bpf_prog_run_xdpis called underlocal_bh_disableandsynchronize_rcuwill wait for it to leave [2].[1] https://lore.kernel.org/bpf/Y5SFho7ZYXr9ifRn@krava/T/#m00c29ece654bc9f332a17df493bbca33e702896c
[2] https://lore.kernel.org/bpf/[email protected]/T/#mff43e2c003ae99f4a38f353c7969be4c7162e877Reported-by Hao Sun <[email protected]>Signed-off-by Jiri Olsa <[email protected]>
Quoted 27 lines · Jiri Olsa
Hao Sun reported crash in dispatcher image [1].
Currently we don't have any sync between
bpf_dispatcher_updateandbpf_dispatcher_xdp_func, so following race is possible:cpu 0: cpu 1:
bpf_prog_run_xdp
...
bpf_dispatcher_xdp_funcin image at offset 0x0 bpf_dispatcher_update update image at offset 0x800 bpf_dispatcher_update update image at offset 0x0 in image at offset 0x0 -> crashFixing this by synchronizing dispatcher image update (which is done in
bpf_dispatcher_updatefunction) withbpf_dispatcher_xdp_functhat reads and execute the dispatcher image.Calling
synchronize_rcuafter updating and installing new image ensures that readers leave old image before it's changed in the next dispatcher update. The update itself is locked with dispatcher's mutex.
Quoted 8 lines
The
bpf_prog_run_xdpis called underlocal_bh_disableandsynchronize_rcuwill wait for it to leave [2].[1] https://lore.kernel.org/bpf/Y5SFho7ZYXr9ifRn@krava/T/#m00c29ece654bc9f332a17df493bbca33e702896c
[2] https://lore.kernel.org/bpf/[email protected]/T/#mff43e2c003ae99f4a38f353c7969be4c7162e877Reported-by Hao Sun <[email protected]>Signed-off-by Jiri Olsa <[email protected]>
From an RCU viewpoint:
Quoted 23 lines
--- kernel/bpf/dispatcher.c | 5 +++++ 1 file changed, 5 insertions(+)kernel/bpf/dispatcher.c +5
-0@@ -125,6 +125,11 @@ static void bpf_dispatcher_update(struct bpf_dispatcher *d, int prev_num_progs)__BPF_DISPATCHER_UPDATE (d, new ?: (void *)&bpf_dispatcher_nop_func); +/* Make sure all the callers executing the previous/old half of the +* image leave it, so following update call can modify it safely. +*/ +synchronize_rcu (); +if (new) d->image_off = noff; }Signature · 1 line
2.38.1
This patch was applied to bpf/bpf.git (master) by Martin KaFai Lau <[email protected]>:
Hello:
This patch was applied to bpf/bpf.git (master)
by Martin KaFai Lau <[email protected]>:
Quoted 8 lines · On Wed, 14 Dec 2022 13:35:42 +0100 you wrote:
Hao Sun reported crash in dispatcher image [1].
Currently we don't have any sync between
bpf_dispatcher_updateandbpf_dispatcher_xdp_func, so following race is possible:cpu 0: cpu 1:
[...]
Here is the summary with links:
- [bpf] bpf: Synchronize dispatcher update with bpf_dispatcher_xdp_func
https://git.kernel.org/bpf/bpf/c/4121d4481b72
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html