Skip to contentSkip to conversation

net: skb content must be visible for lockless skb_peek() and its variations

PATCH In review Kirill Tkhai 3 participants 8 messages Jul 31, 2022 – Aug 7, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: skb content must be visible for lockless skb_peek() and its variations

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...
skb->xxx = var1; skb = READ_ONCE(prev_skb->next);
<no barrier> <no barrier>
WRITE_ONCE(prev_skb->next, skb); var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

---
Hi, David, Eric and other developers,

picking unix sockets code I found this problem, and for me it looks like it exists. If there are arguments that everything is OK and it's expected, please, explain.

Best wishes,
Kirill

 include/linux/skbuff.h |    9 ++++++---
 net/core/skbuff.c      |    6 ++++++
 2 files changed, 12 insertions(+), 3 deletions(-)
include/linux/skbuff.h +6 -3
index ca8afa382bf2..2939a5dc0ad7 100644
@@ -2018,7 +2018,8 @@ static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
  */
 static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
 {
-	struct sk_buff *skb = list_->next;
+	/* Pairs with mb in skb_queue_tail() and variations */
+	struct sk_buff *skb = smp_load_acquire(&list_->next);
 
 	if (skb == (struct sk_buff *)list_)
 		skb = NULL;
@@ -2048,7 +2049,8 @@ static inline struct sk_buff *__skb_peek(const struct sk_buff_head *list_)
 static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
 		const struct sk_buff_head *list_)
 {
-	struct sk_buff *next = skb->next;
+	/* Pairs with mb in skb_queue_tail() and variations */
+	struct sk_buff *next = smp_load_acquire(&skb->next);
 
 	if (next == (struct sk_buff *)list_)
 		next = NULL;
@@ -2070,7 +2072,8 @@ static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
  */
 static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
 {
-	struct sk_buff *skb = READ_ONCE(list_->prev);
+	/* Pairs with mb in skb_queue_tail() and variations */
+	struct sk_buff *skb = smp_load_acquire(&list_->prev);
 
 	if (skb == (struct sk_buff *)list_)
 		skb = NULL;
net/core/skbuff.c +6 -0
index 974bbbbe7138..1de46eb91405 100644
@@ -3373,6 +3373,8 @@ void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
 	unsigned long flags;
 
 	spin_lock_irqsave(&list->lock, flags);
+	/* Pairs with mb in skb_peek() and variations */
+	smp_mb__after_spinlock();
 	__skb_queue_head(list, newsk);
 	spin_unlock_irqrestore(&list->lock, flags);
 }
@@ -3394,6 +3396,8 @@ void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
 	unsigned long flags;
 
 	spin_lock_irqsave(&list->lock, flags);
+	/* Pairs with mb in skb_peek() and variations */
+	smp_mb__after_spinlock();
 	__skb_queue_tail(list, newsk);
 	spin_unlock_irqrestore(&list->lock, flags);
 }
@@ -3434,6 +3438,8 @@ void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head
 	unsigned long flags;
 
 	spin_lock_irqsave(&list->lock, flags);
+	/* Pairs with mb in skb_peek() and variations */
+	smp_mb__after_spinlock();
 	__skb_queue_after(list, old, newsk);
 	spin_unlock_irqrestore(&list->lock, flags);
 }
maintainer
#1 lore raw
Quoted 16 lines · Kirill Tkhai

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...
skb->xxx = var1; skb = READ_ONCE(prev_skb->next);
<no barrier> <no barrier>
WRITE_ONCE(prev_skb->next, skb); var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

Quoted 5 lines

---
Hi, David, Eric and other developers,

picking unix sockets code I found this problem,

Could you please report exactly how/where the problem maifests (e.g. the involved call paths/time sequence)?

Quoted 2 lines

and for me it looks like it exists. If there
are arguments that everything is OK and it's expected, please, explain.

I don't see why such barriers are needed for the locked peek/tail variants, as the spin_lock pair implies a full memory barrier.

Cheers,

Paolo

#2 lore raw
Quoted 17 lines · Paolo Abeni
Quoted 16 lines · Kirill Tkhai

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...
skb->xxx = var1; skb = READ_ONCE(prev_skb->next);
<no barrier> <no barrier>
WRITE_ONCE(prev_skb->next, skb); var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

Quoted 8 lines
Quoted 5 lines

---
Hi, David, Eric and other developers,

picking unix sockets code I found this problem,

Could you please report exactly how/where the problem maifests (e.g. the involved call paths/time sequence)?

I didn't get why call paths in the patch description are not enough for you. Please, explain what you want.

Quoted 5 lines
Quoted 2 lines

and for me it looks like it exists. If there
are arguments that everything is OK and it's expected, please, explain.

I don't see why such barriers are needed for the locked peek/tail variants, as the spin_lock pair implies a full memory barrier.

This is for lockless skb_peek() calls and the patch is called in that way :). For locked skb_peek() this is not needed. I'm not sure we need separate skb_peek() and skb_peek_lockless(). Do we?

#3 lore raw
Quoted 31 lines · Kirill Tkhai
Quoted 17 lines · Paolo Abeni
Quoted 16 lines · Kirill Tkhai

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...

skb->xxx = var1;                     skb = READ_ONCE(prev_skb->next);
<no barrier>                         <no barrier>
WRITE_ONCE(prev_skb->next, skb);     var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

Quoted 8 lines
Quoted 5 lines

---
Hi, David, Eric and other developers,

picking unix sockets code I found this problem,

Could you please report exactly how/where the problem maifests (e.g. the involved call paths/time sequence)?

I didn't get why call paths in the patch description are not enough for you. Please, explain what you want.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

and for me it looks like it exists. If there
are arguments that everything is OK and it's expected, please, explain.

I don't see why such barriers are needed for the locked peek/tail variants, as the spin_lock pair implies a full memory barrier.

This is for lockless skb_peek() calls and the patch is called in that way :). For locked skb_peek() this is not needed. I'm not sure we need separate skb_peek() and skb_peek_lockless(). Do we?

We prefer explicit _lockless variants to document the precise points they are needed.

A new helper (and its initial usage) will clearly point to the problem you saw in af_unix.

BTW, smp_mb__after_spinlock() in your patch does not really make sense to me. Please add in your changelog the precise issue you are seeing.

#4 lore raw
Quoted 38 lines · Eric Dumazet
Quoted 37 lines · Kirill Tkhai
Quoted 17 lines · Paolo Abeni
Quoted 16 lines · Kirill Tkhai

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...

skb->xxx = var1;                     skb = READ_ONCE(prev_skb->next);
<no barrier>                         <no barrier>
WRITE_ONCE(prev_skb->next, skb);     var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

Quoted 8 lines
Quoted 5 lines

---
Hi, David, Eric and other developers,

picking unix sockets code I found this problem,

Could you please report exactly how/where the problem maifests (e.g. the involved call paths/time sequence)?

I didn't get why call paths in the patch description are not enough for you. Please, explain what you want.

Quoted 5 lines
Quoted 2 lines

and for me it looks like it exists. If there
are arguments that everything is OK and it's expected, please, explain.

I don't see why such barriers are needed for the locked peek/tail variants, as the spin_lock pair implies a full memory barrier.

Quoted 8 lines
Quoted 2 lines

This is for lockless skb_peek() calls and the patch is called in that way :). For locked skb_peek() this is not needed. I'm not sure we need separate skb_peek() and skb_peek_lockless(). Do we?

We prefer explicit _lockless variants to document the precise points they are needed.

A new helper (and its initial usage) will clearly point to the problem you saw in af_unix.

The problem is:

unix_stream_sendmsg() unix_stream_read_generic()
skb->len = size; skb = skb_peek();
skb_queue_tail(skb); unix_skb_len(skb); <- here we read wrong len

Quoted 2 lines

BTW, smp_mb__after_spinlock() in your patch does not really make sense to me. Please add in your changelog the precise issue you are seeing.

What is about queue part, do you recommend to have a separate helper for that? skb_queue_tail_for_lockless()?

#5 lore raw
Quoted 45 lines · Kirill Tkhai
Quoted 44 lines · Eric Dumazet
Quoted 31 lines · Kirill Tkhai
Quoted 17 lines · Paolo Abeni
Quoted 16 lines · Kirill Tkhai

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...

skb->xxx = var1;                     skb = READ_ONCE(prev_skb->next);
<no barrier>                         <no barrier>
WRITE_ONCE(prev_skb->next, skb);     var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

Quoted 8 lines
Quoted 5 lines

---
Hi, David, Eric and other developers,

picking unix sockets code I found this problem,

Could you please report exactly how/where the problem maifests (e.g. the involved call paths/time sequence)?

I didn't get why call paths in the patch description are not enough for you. Please, explain what you want.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

and for me it looks like it exists. If there
are arguments that everything is OK and it's expected, please, explain.

I don't see why such barriers are needed for the locked peek/tail variants, as the spin_lock pair implies a full memory barrier.

This is for lockless skb_peek() calls and the patch is called in that way :). For locked skb_peek() this is not needed. I'm not sure we need separate skb_peek() and skb_peek_lockless(). Do we?

We prefer explicit _lockless variants to document the precise points they are needed.

Quoted 8 lines
Quoted 2 lines

A new helper (and its initial usage) will clearly point to the problem you saw in af_unix.

The problem is:

unix_stream_sendmsg() unix_stream_read_generic()
skb->len = size; skb = skb_peek();
skb_queue_tail(skb); unix_skb_len(skb); <- here we read wrong len

Oh, there are unix_state_lock(). Please, ignore this patch...

maintainer
#6 lore raw
In reply to Kirill Tkhai
Quoted 22 lines · Kirill Tkhai
Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Kirill Tkhai

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...
skb->xxx = var1; skb = READ_ONCE(prev_skb->next);
<no barrier> <no barrier>
WRITE_ONCE(prev_skb->next, skb); var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

---
Hi, David, Eric and other developers,

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

picking unix sockets code I found this problem,

Could you please report exactly how/where the problem maifests (e.g. the involved call paths/time sequence)?

I didn't get why call paths in the patch description are not enough for you. Please, explain what you want.

You mentioned the unix socket, so I expect to see something alike (I'm totally making up the symbols lists just to give an example):

CPU0 CPU1
unix_stream_read_generic() unix_stream_sendmsg()
skb_peek() skb_queue_tail(other->sk_receive_queue)

plus some wording on how the critical race is reached, if not completely obvious.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

and for me it looks like it exists. If there
are arguments that everything is OK and it's expected, please, explain.

I don't see why such barriers are needed for the locked peek/tail variants, as the spin_lock pair implies a full memory barrier.

This is for lockless skb_peek() calls and the patch is called in that way :). For locked skb_peek() this is not needed.

But you are also unconditioanlly adding barriers to the locked append/enqueue functions - which would possibly make sense only when the latters are paired with lockless read access.

As Eric said, if any new barrier is needed, we want to apply it only where needed, and not to every skb_queue*()/skb_peek() user, so very likely a new helper (o a new pair of helpers) will be needed.

Thanks!

Paolo

#7 lore raw
Quoted 34 lines · Paolo Abeni
Quoted 22 lines · Kirill Tkhai
Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Kirill Tkhai

From: Kirill Tkhai <[email protected]>

Currently, there are no barriers, and skb->xxx update may become invisible on cpu2. In the below example var2 may point to intial_val0 instead of expected var1:

[cpu1] [cpu2]
skb->xxx = initial_val0;
...
skb->xxx = var1; skb = READ_ONCE(prev_skb->next);
<no barrier> <no barrier>
WRITE_ONCE(prev_skb->next, skb); var2 = skb->xxx;

This patch adds barriers and fixes the problem. Note, that __skb_peek() is not patched, since it's a lowlevel function, and a caller has to understand the things it does (and also __skb_peek() is used under queue lock in some places).

---
Hi, David, Eric and other developers,

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

picking unix sockets code I found this problem,

Could you please report exactly how/where the problem maifests (e.g. the involved call paths/time sequence)?

I didn't get why call paths in the patch description are not enough for you. Please, explain what you want.

You mentioned the unix socket, so I expect to see something alike (I'm totally making up the symbols lists just to give an example):

Quoted 6 lines

CPU0 CPU1
unix_stream_read_generic() unix_stream_sendmsg()
skb_peek() skb_queue_tail(other->sk_receive_queue)

plus some wording on how the critical race is reached, if not completely obvious.

Ah, you mean specific functions. Yes, this example and the rest of places, skb_peek{,tail} are used without queue lock, e.g., unix_stream_data_wait().

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

and for me it looks like it exists. If there
are arguments that everything is OK and it's expected, please, explain.

I don't see why such barriers are needed for the locked peek/tail variants, as the spin_lock pair implies a full memory barrier.

This is for lockless skb_peek() calls and the patch is called in that way :). For locked skb_peek() this is not needed.

Quoted 7 lines

But you are also unconditioanlly adding barriers to the locked append/enqueue functions - which would possibly make sense only when the latters are paired with lockless read access.

As Eric said, if any new barrier is needed, we want to apply it only where needed, and not to every skb_queue*()/skb_peek() user, so very likely a new helper (o a new pair of helpers) will be needed.

Ok, thanks, Paolo.

Kirill