Skip to contentSkip to conversation

lwt: disable BH too in run_lwt_bpf()

PATCH Superseded Cong Wang 3 participants 12 messages Dec 1, 2020 – Dec 5, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 lwt: disable BH too in run_lwt_bpf()

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Update the comments above the code too, as call_rcu() is now BH friendly.

---
 net/core/lwt_bpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/lwt_bpf.c +4 -4
index 7d3438215f32..4f3cb7c15ddf 100644
@@ -39,12 +39,11 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 {
 	int ret;
 
-	/* Preempt disable is needed to protect per-cpu redirect_info between
-	 * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
-	 * access to maps strictly require a rcu_read_lock() for protection,
-	 * mixing with BH RCU lock doesn't work.
+	/* Preempt disable and BH disable are needed to protect per-cpu
+	 * redirect_info between BPF prog and skb_do_redirect().
 	 */
 	preempt_disable();
+	local_bh_disable();
 	bpf_compute_data_pointers(skb);
 	ret = bpf_prog_run_save_cb(lwt->prog, skb);
 
@@ -78,6 +77,7 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 		break;
 	}
 
+	local_bh_enable();
 	preempt_enable();
 
 	return ret;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 16 lines · On Tue, 1 Dec 2020 11:44:38 -0800 Cong Wang wrote:

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Quoted 6 lines

Update the comments above the code too, as call_rcu() is now BH friendly.

Fixes?

maintainer
#2 lore raw
Quoted 17 lines · On Tue, 1 Dec 2020 16:16:33 -0800 Jakub Kicinski wrote:
Quoted 16 lines · On Tue, 1 Dec 2020 11:44:38 -0800 Cong Wang wrote:

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Quoted 8 lines
Quoted 6 lines

Update the comments above the code too, as call_rcu() is now BH friendly.

Fixes?

Ah, should have read the commit message first. Okay.

maintainer
#3 lore raw
In reply to Cong Wang
Quoted 33 lines · On Tue, 1 Dec 2020 11:44:38 -0800 Cong Wang wrote:

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Update the comments above the code too, as call_rcu() is now BH friendly.

---
 net/core/lwt_bpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/lwt_bpf.c +0 -0
index 7d3438215f32..4f3cb7c15ddf 100644
@@ -39,12 +39,11 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 {
 	int ret;

net/core/lwt_bpf.c static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, · line 46

Quoted 10 lines
net/core/lwt_bpf.c +3 -4
 
-	/* Preempt disable is needed to protect per-cpu redirect_info between
-	 * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
-	 * access to maps strictly require a rcu_read_lock() for protection,
-	 * mixing with BH RCU lock doesn't work.
+	/* Preempt disable and BH disable are needed to protect per-cpu
+	 * redirect_info between BPF prog and skb_do_redirect().
 	 */
 	preempt_disable();
+	local_bh_disable();

Why not remove the preempt_disable()? Disabling BH must also disable preemption AFAIK.

Quoted 11 lines
net/core/lwt_bpf.c +1 -0
 	bpf_compute_data_pointers(skb);
 	ret = bpf_prog_run_save_cb(lwt->prog, skb);
 
@@ -78,6 +77,7 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 		break;
 	}
 
+	local_bh_enable();
 	preempt_enable();
 
 	return ret;
author
#4 lore raw
Quoted 36 lines · Jakub Kicinski
Quoted 34 lines · On Tue, 1 Dec 2020 11:44:38 -0800 Cong Wang wrote:

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Update the comments above the code too, as call_rcu() is now BH friendly.

---
 net/core/lwt_bpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/lwt_bpf.c +0 -0
index 7d3438215f32..4f3cb7c15ddf 100644
@@ -39,12 +39,11 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 {
      int ret;
 
Quoted 12 lines

net/core/lwt_bpf.c static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, · line 46

Quoted 9 lines
net/core/lwt_bpf.c +3 -4
-     /* Preempt disable is needed to protect per-cpu redirect_info between
-      * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
-      * access to maps strictly require a rcu_read_lock() for protection,
-      * mixing with BH RCU lock doesn't work.
+     /* Preempt disable and BH disable are needed to protect per-cpu
+      * redirect_info between BPF prog and skb_do_redirect().
       */
      preempt_disable();
+     local_bh_disable();

Why not remove the preempt_disable()? Disabling BH must also disable preemption AFAIK.

It seems RT kernel still needs preempt disable:
https://www.spinics.net/lists/kernel/msg3710124.html
but my RT knowledge is not sufficient to tell. So I just follow the same pattern
in x86 FPU (as of today):

static inline void fpregs_lock(void)

{
        preempt_disable();
        local_bh_disable();
}

static inline void fpregs_unlock(void)

{
        local_bh_enable();
        preempt_enable();
}

There are other similar patterns in the current code base, so if this needs a clean up, RT people can clean up them all together.

Thanks.

maintainer
#5 lore raw
Quoted 62 lines · On Wed, 2 Dec 2020 17:29:53 -0800 Cong Wang wrote:
Quoted 36 lines · Jakub Kicinski
Quoted 34 lines · On Tue, 1 Dec 2020 11:44:38 -0800 Cong Wang wrote:

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Update the comments above the code too, as call_rcu() is now BH friendly.

---
 net/core/lwt_bpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/lwt_bpf.c +0 -0
index 7d3438215f32..4f3cb7c15ddf 100644
@@ -39,12 +39,11 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 {
      int ret;
 
Quoted 12 lines

net/core/lwt_bpf.c static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, · line 46

Quoted 9 lines
net/core/lwt_bpf.c +3 -4
-     /* Preempt disable is needed to protect per-cpu redirect_info between
-      * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
-      * access to maps strictly require a rcu_read_lock() for protection,
-      * mixing with BH RCU lock doesn't work.
+     /* Preempt disable and BH disable are needed to protect per-cpu
+      * redirect_info between BPF prog and skb_do_redirect().
       */
      preempt_disable();
+     local_bh_disable();  

Why not remove the preempt_disable()? Disabling BH must also disable preemption AFAIK.

It seems RT kernel still needs preempt disable:
https://www.spinics.net/lists/kernel/msg3710124.html
but my RT knowledge is not sufficient to tell. So I just follow the same pattern
in x86 FPU (as of today):

static inline void fpregs_lock(void)

{
        preempt_disable();
        local_bh_disable();
}
Quoted 8 lines

static inline void fpregs_unlock(void)

{
        local_bh_enable();
        preempt_enable();
}

There are other similar patterns in the current code base, so if this needs a clean up, RT people can clean up them all together.

I see. GTK.

The patch seem good but it's probably best suited to the bpf tree, let me reassign it in patchwork.

#6 lore raw
In reply to Cong Wang
Quoted 51 lines · Cong Wang
Quoted 36 lines · Jakub Kicinski
Quoted 34 lines · On Tue, 1 Dec 2020 11:44:38 -0800 Cong Wang wrote:

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Update the comments above the code too, as call_rcu() is now BH friendly.

---
 net/core/lwt_bpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/lwt_bpf.c +0 -0
index 7d3438215f32..4f3cb7c15ddf 100644
@@ -39,12 +39,11 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 {
      int ret;
 
Quoted 12 lines

net/core/lwt_bpf.c static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, · line 46

Quoted 9 lines
net/core/lwt_bpf.c +3 -4
-     /* Preempt disable is needed to protect per-cpu redirect_info between
-      * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
-      * access to maps strictly require a rcu_read_lock() for protection,
-      * mixing with BH RCU lock doesn't work.
+     /* Preempt disable and BH disable are needed to protect per-cpu
+      * redirect_info between BPF prog and skb_do_redirect().
       */
      preempt_disable();
+     local_bh_disable();

Why not remove the preempt_disable()? Disabling BH must also disable preemption AFAIK.

Quoted 1 line

It seems RT kernel still needs preempt disable:

No. It's the opposite. When we did RT+bpf changes we missed this function. It should be migrate_disable here instead of preempt_disable. I don't know what local_bh_disable() maps to in RT.
Since it's used in many other places it's fine to use it here to prevent this race.

#7 lore raw
Quoted 53 lines · Alexei Starovoitov
Quoted 51 lines · Cong Wang
Quoted 48 lines · Jakub Kicinski
Quoted 33 lines · On Tue, 1 Dec 2020 11:44:38 -0800 Cong Wang wrote:

From: Dongdong Wang <[email protected]>

The per-cpu bpf_redirect_info is shared among all skb_do_redirect() and BPF redirect helpers. Callers on RX path are all in BH context, disabling preemption is not sufficient to prevent BH interruption.

In production, we observed strange packet drops because of the race condition between LWT xmit and TC ingress, and we verified this issue is fixed after we disable BH.

Although this bug was technically introduced from the beginning, that is commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel infrastructure"), at that time call_rcu() had to be call_rcu_bh() to match the RCU context. So this patch may not work well before RCU flavor consolidation has been completed around v5.0.

Update the comments above the code too, as call_rcu() is now BH friendly.

---
 net/core/lwt_bpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/lwt_bpf.c +0 -0
index 7d3438215f32..4f3cb7c15ddf 100644
@@ -39,12 +39,11 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 {
      int ret;

net/core/lwt_bpf.c static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, · line 46

Quoted 10 lines
net/core/lwt_bpf.c +3 -4
 
-     /* Preempt disable is needed to protect per-cpu redirect_info between
-      * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
-      * access to maps strictly require a rcu_read_lock() for protection,
-      * mixing with BH RCU lock doesn't work.
+     /* Preempt disable and BH disable are needed to protect per-cpu
+      * redirect_info between BPF prog and skb_do_redirect().
       */
      preempt_disable();
+     local_bh_disable();

Why not remove the preempt_disable()? Disabling BH must also disable preemption AFAIK.

Quoted 7 lines
Quoted 1 line

It seems RT kernel still needs preempt disable:

No. It's the opposite. When we did RT+bpf changes we missed this function. It should be migrate_disable here instead of preempt_disable. I don't know what local_bh_disable() maps to in RT.
Since it's used in many other places it's fine to use it here to prevent this race.

I guess my previous comment could be misinterpreted. Cong,
please respin with changing preempt_disable to migrate_disable and adding local_bh_disable.

author
#8 lore raw
Quoted 5 lines · Alexei Starovoitov

I guess my previous comment could be misinterpreted. Cong,
please respin with changing preempt_disable to migrate_disable and adding local_bh_disable.

I have no objection, just want to point out migrate_disable() may not exist if we backport this further to -stable, this helper was introduced in Feb 2020.

Thanks.

#9 lore raw
Quoted 4 lines · Cong Wang
Quoted 1 line · Alexei Starovoitov
Quoted 8 lines
Quoted 4 lines

I guess my previous comment could be misinterpreted. Cong,
please respin with changing preempt_disable to migrate_disable and adding local_bh_disable.

I have no objection, just want to point out migrate_disable() may not exist if we backport this further to -stable, this helper was introduced in Feb 2020.

I see. Then please split it into two patches for ease of backporting.

author
#10 lore raw
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Cong Wang
Quoted 5 lines · Alexei Starovoitov

I guess my previous comment could be misinterpreted. Cong,
please respin with changing preempt_disable to migrate_disable and adding local_bh_disable.

Quoted 5 lines
Quoted 3 lines

I have no objection, just want to point out migrate_disable() may not exist if we backport this further to -stable, this helper was introduced in Feb 2020.

I see. Then please split it into two patches for ease of backporting.

You mean the first patch does the same as this patch and the second patch replaces preempt_disable() with migrate_disable(). Right?

Thanks.

#11 lore raw
Quoted 14 lines · Cong Wang
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Cong Wang
Quoted 5 lines · Alexei Starovoitov

I guess my previous comment could be misinterpreted. Cong,
please respin with changing preempt_disable to migrate_disable and adding local_bh_disable.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I have no objection, just want to point out migrate_disable() may not exist if we backport this further to -stable, this helper was introduced in Feb 2020.

I see. Then please split it into two patches for ease of backporting.

You mean the first patch does the same as this patch and the second patch replaces preempt_disable() with migrate_disable(). Right?

Right. Just be mindful of changing the comment.