Skip to contentSkip to conversation

net_tx_action race condition?

PATCH Angelo Rizzi 3 participants 4 messages Nov 24, 2014

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

Hi Daniel,
Here attached the patch file you required.
The problem i've found is on the declaration of 'struct softnet_data *sd' in function 'net_tx_action'
What happens to me (i have an embedded system based on FPGA and a NIOS2 cpu) is that, due to compiler optimization, the content of 'sd->completion_queue' is saved in a CPU register before interrupt disabling (when the instruction 'if (sd->completion_queue) {' is executed) and then the register contents is used for interrupt-disabled assignment ('clist = sd->completion_queue') instead of re-read the variable contents. This seems to lead to a race condition when an interrupt modifies the content of 'sd->completion_queue' between these two instructions.
What i have done to avoid this situation is to change the declaration of 'struct softnet_data *sd' to 'volatile struct softnet_data *sd' and now everything seems to be ok.
I hope this will help.

Regards,
Angelo

net/core/dev.c renamed from net/core/dev.c.orig +1 -1
@@ -2069,7 +2069,7 @@ EXPORT_SYMBOL(netif_rx_ni);
 
 static void net_tx_action(struct softirq_action *h)
 {
-	struct softnet_data *sd = &__get_cpu_var(softnet_data);
+	volatile struct softnet_data *sd = &__get_cpu_var(softnet_data);
 
 	if (sd->completion_queue) {
 		struct sk_buff *clist;
#1 lore raw
Quoted 11 lines · Angelo Rizzi

[-- text/plain, encoding 7bit, charset: iso-8859-15, 22 lines --]

Hi Daniel,
Here attached the patch file you required.
The problem i've found is on the declaration of 'struct softnet_data *sd' in function 'net_tx_action'
What happens to me (i have an embedded system based on FPGA and a NIOS2 cpu) is that, due to compiler optimization, the content of 'sd->completion_queue' is saved in a CPU register before interrupt disabling (when the instruction 'if (sd->completion_queue) {' is executed) and then the register contents is used for interrupt-disabled

Quoted 8 lines

assignment ('clist = sd->completion_queue') instead of re-read the variable contents.
This seems to lead to a race condition when an interrupt modifies the content of 'sd->completion_queue' between these two instructions. What i have done to avoid this situation is to change the declaration of 'struct softnet_data *sd' to 'volatile struct softnet_data *sd' and now everything seems to be ok.
I hope this will help.

local_irq_disable is supposed to contain a compiler barrier. So this is either a buggy arch or a buggy compiler.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
In reply to Angelo Rizzi
Quoted 18 lines · Angelo Rizzi

Hi Daniel,
Here attached the patch file you required.
The problem i've found is on the declaration of 'struct softnet_data *sd' in function 'net_tx_action'
What happens to me (i have an embedded system based on FPGA and a NIOS2 cpu) is that, due to compiler optimization, the content of 'sd->completion_queue' is saved in a CPU register before interrupt disabling (when the instruction 'if (sd->completion_queue) {' is executed) and then the register contents is used for interrupt-disabled assignment ('clist = sd->completion_queue') instead of re-read the variable contents.
This seems to lead to a race condition when an interrupt modifies the content of 'sd->completion_queue' between these two instructions. What i have done to avoid this situation is to change the declaration of 'struct softnet_data *sd' to 'volatile struct softnet_data *sd' and now everything seems to be ok.
I hope this will help.

Quoted 3 lines

Regards,
Angelo

Do not add volatile in the kernel, this is not how we solve this kind of problems. Documentation/memory-barriers.txt and

Documentation/00-INDEX:476:volatile-considered-harmful.txt
Documentation/00-INDEX:477: - Why the "volatile" type class should not be used

I am surprised this patch is needed. Many other 'bugs' would need similar fixes.

local_irq_disable() MUST have a memory barrier. This looks like a bug in one particular arch implementation.

On x86 for example, native_irq_disable() is really :

	asm volatile("cli": : :"memory");
net/core/dev.c +1 -1
index ac4836241a965a71952469ba054f87d8dfca2d32..fa73072e515aa07fa8ae1bc39174b7d59c7a31a5 100644
@@ -3406,7 +3406,7 @@ static void net_tx_action(struct softirq_action *h)
 		struct sk_buff *clist;
 
 		local_irq_disable();
-		clist = sd->completion_queue;
+		clist = ACCESS_ONCE(sd->completion_queue);
 		sd->completion_queue = NULL;
 		local_irq_enable();
 
#3 lore raw

Hi Eric,
Thanks for your reply
You are right, it seems a bug in the NIOS2 architecture port.
I will check how local_irq_disable()/local_irq_enable() is implemented on this kind of architecture.

Regards,
Angelo

Il 24/11/2014 16:33, Eric Dumazet ha scritto:

Quoted 57 lines · Angelo Rizzi
Quoted 18 lines

Hi Daniel,
Here attached the patch file you required.
The problem i've found is on the declaration of 'struct softnet_data *sd' in function 'net_tx_action'
What happens to me (i have an embedded system based on FPGA and a NIOS2 cpu) is that, due to compiler optimization, the content of 'sd->completion_queue' is saved in a CPU register before interrupt disabling (when the instruction 'if (sd->completion_queue) {' is executed) and then the register contents is used for interrupt-disabled assignment ('clist = sd->completion_queue') instead of re-read the variable contents.
This seems to lead to a race condition when an interrupt modifies the content of 'sd->completion_queue' between these two instructions. What i have done to avoid this situation is to change the declaration of 'struct softnet_data *sd' to 'volatile struct softnet_data *sd' and now everything seems to be ok.
I hope this will help.

Quoted 3 lines

Regards,
Angelo

Do not add volatile in the kernel, this is not how we solve this kind of problems. Documentation/memory-barriers.txt and

Documentation/00-INDEX:476:volatile-considered-harmful.txt
Documentation/00-INDEX:477: - Why the "volatile" type class should not be used

I am surprised this patch is needed. Many other 'bugs' would need similar fixes.

local_irq_disable() MUST have a memory barrier. This looks like a bug in one particular arch implementation.

On x86 for example, native_irq_disable() is really :

	asm volatile("cli": : :"memory");
net/core/dev.c +1 -1
index ac4836241a965a71952469ba054f87d8dfca2d32..fa73072e515aa07fa8ae1bc39174b7d59c7a31a5 100644
@@ -3406,7 +3406,7 @@ static void net_tx_action(struct softirq_action *h)
  		struct sk_buff *clist;
 
  		local_irq_disable();
-		clist = sd->completion_queue;
+		clist = ACCESS_ONCE(sd->completion_queue);
  		sd->completion_queue = NULL;
  		local_irq_enable();