Skip to contentSkip to conversation

net: fix softnet_stat

PATCH Superseded Changli Gao 2 participants 10 messages Apr 15, 2010

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: fix softnet_stat

fix softnet_stat

Per cpu variable softnet_data.total was shared between IRQ and SoftIRQ context without any protection. And enqueue_to_backlog should update the netdev_rx_stat of the target CPU.

This patch changes the meaning of softnet_data.total to the number of packets processed, not includes dropped, in order to avoid it sharing between IRQ and SoftIRQ. And softnet_data.dropped is protected in the corresponding input_pkt_queue.lock, if RPS is enabled.

----

 net/core/dev.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/core/dev.c +2 -3
index a10a216..b38a991 100644
@@ -2336,7 +2336,6 @@ static int enqueue_to_backlog(struct sk_buff *skb, int cpu)
 	queue = &per_cpu(softnet_data, cpu);
 
 	local_irq_save(flags);
-	__get_cpu_var(netdev_rx_stat).total++;
 
 	rps_lock(queue);
 	if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
@@ -2366,9 +2365,9 @@ enqueue:
 		goto enqueue;
 	}
 
+	per_cpu(netdev_rx_stat, cpu).dropped++;
 	rps_unlock(queue);
 
-	__get_cpu_var(netdev_rx_stat).dropped++;
 	local_irq_restore(flags);
 
 	kfree_skb(skb);
@@ -3565,7 +3564,7 @@ static int softnet_seq_show(struct seq_file *seq, void *v)
 	struct netif_rx_stats *s = v;
 
 	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
-		   s->total, s->dropped, s->time_squeeze, 0,
+		   s->total + s->dropped, s->dropped, s->time_squeeze, 0,
 		   0, 0, 0, 0, /* was fastroute */
 		   s->cpu_collision, s->received_rps);
 	return 0;
#1 lore raw
Quoted 11 lines · Changli Gao

fix softnet_stat

Per cpu variable softnet_data.total was shared between IRQ and SoftIRQ context without any protection. And enqueue_to_backlog should update the netdev_rx_stat of the target CPU.

This patch changes the meaning of softnet_data.total to the number of packets processed, not includes dropped, in order to avoid it sharing between IRQ and SoftIRQ. And softnet_data.dropped is protected in the corresponding input_pkt_queue.lock, if RPS is enabled.

I forget a comment:

This patch also fixed a bug: packets received by enqueue_to_backlog() was counted twice: one in enqueue_to_backlog(), and one in __netif_receive_skb(), although they may not on the same CPUs.

Signature · 2 lines
Regards,
Changli Gao([email protected])
#2 lore raw
In reply to Changli Gao
Quoted 20 lines · Changli Gao

fix softnet_stat

Per cpu variable softnet_data.total was shared between IRQ and SoftIRQ context without any protection. And enqueue_to_backlog should update the netdev_rx_stat of the target CPU.

This patch changes the meaning of softnet_data.total to the number of packets processed, not includes dropped, in order to avoid it sharing between IRQ and SoftIRQ. And softnet_data.dropped is protected in the corresponding input_pkt_queue.lock, if RPS is enabled.

----

 net/core/dev.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/core/dev.c +0 -0
index a10a216..b38a991 100644

net/core/dev.c static int enqueue_to_backlog(struct sk_buff *skb, int cpu) · line 2339

Quoted 4 lines
net/core/dev.c +0 -1
@@ -2336,7 +2336,6 @@ static int enqueue_to_backlog(struct sk_buff *skb, int cpu)
 	queue = &per_cpu(softnet_data, cpu);
 
 	local_irq_save(flags);
-	__get_cpu_var(netdev_rx_stat).total++;

I think this was nice, because we can see number of frames received by this cpu, before giving them to another cpu.

Maybe we want a new counter ?

	__get_cpu_var(netdev_rx_stat).inpackets++;

net/core/dev.c enqueue: · line 2368

Quoted 8 lines
net/core/dev.c +1 -0
 	rps_lock(queue);
 	if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
@@ -2366,9 +2365,9 @@ enqueue:
 		goto enqueue;
 	}
 
+	per_cpu(netdev_rx_stat, cpu).dropped++;

This is a bit expensive, and could be a queue->rx_stat.dropped++ if netdev_rx_stat is moved inside queue structure.

net/core/dev.c static int softnet_seq_show(struct seq_file *seq, void *v) · line 3567

Quoted 12 lines
net/core/dev.c +1 -2
 	rps_unlock(queue);
 
-	__get_cpu_var(netdev_rx_stat).dropped++;
 	local_irq_restore(flags);
 
 	kfree_skb(skb);
@@ -3565,7 +3564,7 @@ static int softnet_seq_show(struct seq_file *seq, void *v)
 	struct netif_rx_stats *s = v;
 
 	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
-		   s->total, s->dropped, s->time_squeeze, 0,
+		   s->total + s->dropped, s->dropped, s->time_squeeze, 0,

This is wrong I believe... I prefer to add a new column, s->inpackets ?

Quoted 4 lines
net/core/dev.c +0 -1
 		   0, 0, 0, 0, /* was fastroute */
 		   s->cpu_collision, s->received_rps);
 	return 0;
--
#3 lore raw
Quoted 5 lines · Eric Dumazet

net/core/dev.c enqueue: · line 2368

Quoted 2 lines
net/core/dev.c +1 -0
+	per_cpu(netdev_rx_stat, cpu).dropped++;

This is a bit expensive, and could be a queue->rx_stat.dropped++ if netdev_rx_stat is moved inside queue structure.

Hmm, this is not really needed, this event is the slow path and should almost never happen. This part of your fix is fine.

#7 lore raw
In reply to Eric Dumazet
Quoted 10 lines · Eric Dumazet
Quoted 10 lines

----
 net/core/dev.c |    5 ++---

 1 file changed, 2 insertions(+), 3 deletions(-)
net/core/dev.c +0 -0
index a10a216..b38a991 100644
@@ -2336,7 +2336,6 @@ static int enqueue_to_backlog(struct sk_buff *skb, int cpu)
       queue = &per_cpu(softnet_data, cpu);
 
Quoted 6 lines

net/core/dev.c static int enqueue_to_backlog(struct sk_buff *skb, int cpu) · line 2339

Quoted 2 lines
net/core/dev.c +0 -1
       local_irq_save(flags);
-     __get_cpu_var(netdev_rx_stat).total++;

I think this was nice, because we can see number of frames received by this cpu, before giving them to another cpu.

:( Packets will be counted again in __net_receive_skb(). Now the meaning of total is confusing.

Quoted 4 lines

Maybe we want a new counter ?

       __get_cpu_var(netdev_rx_stat).inpackets++;

How about replacing total with received?

Quoted 7 lines
Quoted 7 lines
net/core/dev.c +0 -0
       rps_lock(queue);
       if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
@@ -2366,9 +2365,9 @@ enqueue:
               goto enqueue;
       }
 
Quoted 5 lines

net/core/dev.c enqueue: · line 2368

Quoted 1 line
net/core/dev.c +1 -0
+     per_cpu(netdev_rx_stat, cpu).dropped++;

This is a bit expensive, and could be a queue->rx_stat.dropped++ if netdev_rx_stat is moved inside queue structure.

It will make softnet_data larger.?

Quoted 10 lines
Quoted 10 lines
net/core/dev.c +0 -1
       rps_unlock(queue);
 
-     __get_cpu_var(netdev_rx_stat).dropped++;
       local_irq_restore(flags);
 
       kfree_skb(skb);
@@ -3565,7 +3564,7 @@ static int softnet_seq_show(struct seq_file *seq, void *v)
       struct netif_rx_stats *s = v;
 
Quoted 5 lines

net/core/dev.c static int softnet_seq_show(struct seq_file *seq, void *v) · line 3567

Quoted 3 lines
net/core/dev.c +1 -1
       seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
-                s->total, s->dropped, s->time_squeeze, 0,
+                s->total + s->dropped, s->dropped, s->time_squeeze, 0,

This is wrong I believe... I prefer to add a new column, s->inpackets ?

If I rename total to received, is it still confusing?

Signature · 2 lines
Regards,
Changli Gao([email protected])
#4 lore raw
In reply to Eric Dumazet
Quoted 10 lines · Eric Dumazet
Quoted 5 lines · Eric Dumazet
Quoted 2 lines

+   per_cpu(netdev_rx_stat, cpu).dropped++;

This is a bit expensive, and could be a queue->rx_stat.dropped++ if netdev_rx_stat is moved inside queue structure.

Hmm, this is not really needed, this event is the slow path and should almost never happen. This part of your fix is fine.

It there any document about /proc/net/softnet_stat?

Signature · 2 lines
Regards,
Changli Gao([email protected])
#9 lore raw
In reply to Eric Dumazet
Quoted 31 lines · Eric Dumazet

I think this was nice, because we can see number of frames received by this cpu, before giving them to another cpu.

Maybe we want a new counter ?

       __get_cpu_var(netdev_rx_stat).inpackets++;

net/core/dev.c enqueue: · line 2368

Quoted 8 lines

      rps_lock(queue);
      if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {

net/core/dev.c +1 -0
@@ -2366,9 +2365,9 @@ enqueue:
               goto enqueue;
       }
 
+     per_cpu(netdev_rx_stat, cpu).dropped++;

This is a bit expensive, and could be a queue->rx_stat.dropped++ if netdev_rx_stat is moved inside queue structure.

Quoted 9 lines
net/core/dev.c +0 -1
       rps_unlock(queue);
 
-     __get_cpu_var(netdev_rx_stat).dropped++;
       local_irq_restore(flags);
 
       kfree_skb(skb);
@@ -3565,7 +3564,7 @@ static int softnet_seq_show(struct seq_file *seq, void *v)
       struct netif_rx_stats *s = v;
 
Quoted 6 lines

net/core/dev.c static int softnet_seq_show(struct seq_file *seq, void *v) · line 3567

Quoted 3 lines
net/core/dev.c +1 -1
       seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
-                s->total, s->dropped, s->time_squeeze, 0,
+                s->total + s->dropped, s->dropped, s->time_squeeze, 0,

This is wrong I believe... I prefer to add a new column, s->inpackets ?

the first column total should be the number of the packets, which are processed by this CPU before RPS involved. If RPS is enabled, it should keep the old meaning. If new statistics data is needed by RPS, we would add it in another patch.

Signature · 2 lines
Regards,
Changli Gao([email protected])
#8 lore raw
In reply to Changli Gao
Quoted 18 lines · Changli Gao
Quoted 10 lines · Eric Dumazet
Quoted 10 lines

----

 net/core/dev.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/core/dev.c +0 -0
index a10a216..b38a991 100644
@@ -2336,7 +2336,6 @@ static int enqueue_to_backlog(struct sk_buff *skb, int cpu)
      queue = &per_cpu(softnet_data, cpu);
 
Quoted 6 lines

net/core/dev.c static int enqueue_to_backlog(struct sk_buff *skb, int cpu) · line 2339

Quoted 2 lines
net/core/dev.c +0 -1
      local_irq_save(flags);
-     __get_cpu_var(netdev_rx_stat).total++;

I think this was nice, because we can see number of frames received by this cpu, before giving them to another cpu.

Quoted 2 lines

:( Packets will be counted again in __net_receive_skb(). Now the meaning of total is confusing.

Yes, this should be documented somewhere :)

Quoted 15 lines
Quoted 5 lines

Maybe we want a new counter ?

       __get_cpu_var(netdev_rx_stat).inpackets++;

How about replacing total with received?

Quoted 7 lines
Quoted 7 lines
net/core/dev.c +0 -0
      rps_lock(queue);
      if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
@@ -2366,9 +2365,9 @@ enqueue:
              goto enqueue;
      }
 
Quoted 7 lines
Quoted 5 lines

net/core/dev.c enqueue: · line 2368

Quoted 1 line
net/core/dev.c +1 -0
+     per_cpu(netdev_rx_stat, cpu).dropped++;

This is a bit expensive, and could be a queue->rx_stat.dropped++ if netdev_rx_stat is moved inside queue structure.

It will make softnet_data larger.?

We move a per_cpu struture inside another one. No extra bytes needed.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
net/core/dev.c +0 -1
      rps_unlock(queue);
 
-     __get_cpu_var(netdev_rx_stat).dropped++;
      local_irq_restore(flags);
 
      kfree_skb(skb);
@@ -3565,7 +3564,7 @@ static int softnet_seq_show(struct seq_file *seq, void *v)
      struct netif_rx_stats *s = v;
 
Quoted 7 lines
Quoted 5 lines

net/core/dev.c static int softnet_seq_show(struct seq_file *seq, void *v) · line 3567

Quoted 3 lines
net/core/dev.c +1 -1
      seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
-                s->total, s->dropped, s->time_squeeze, 0,
+                s->total + s->dropped, s->dropped, s->time_squeeze, 0,

This is wrong I believe... I prefer to add a new column, s->inpackets ?

If I rename total to received, is it still confusing?

Keep in mind /proc/net/softnet_stat is an ABI, and first column had the meaning : number of packets processed by this cpu.

But this was pre RPS time. Now we have a two phases process.

This makes sense to :

let 'total' be the counter of packets processed in upper levels (IP/TCP stack). As /proc/net/softnet_stat has no header, the 'total' name is not important and could be changed.

add a new column (after the existing ones in /proc file to keep ABI compatibility) with the meaning : number of packets pre-processed before network stacks.

total : number of packets processed in upper layers (IP stack for example)
dropped: number of packets dropped because this cpu queue was full
time_squeeze: number of time squeeze of this cpu cpu_collision:...
received_rps: number of time this cpu received a RPS signal from another cpu
inpackets : number of packets processed in layer 1 by this cpu (RPS level)

#5 lore raw
In reply to Eric Dumazet
Quoted 7 lines · Eric Dumazet
Quoted 5 lines · Eric Dumazet

net/core/dev.c enqueue: · line 2368

Quoted 2 lines
net/core/dev.c +1 -0
+	per_cpu(netdev_rx_stat, cpu).dropped++;

This is a bit expensive, and could be a queue->rx_stat.dropped++ if netdev_rx_stat is moved inside queue structure.

Quoted 5 lines

Hmm, this is not really needed, this event is the slow path and should almost never happen. This part of your fix is fine.

-

Oh well, I revert this comment, we need a mutual exclusion between cpus if we dont want to miss some increments, even in a slow path.

__get_cpu_var(netdev_rx_stat).field++; is safe because updated only by this cpu.

Once you use per_cpu(netdev_rx_stat, cpu).field++; you need a synchronization since this is not atomic.

so queue->dropped++ is safer and faster too (inside the rps_lock section)

#6 lore raw
Quoted 11 lines · Eric Dumazet

Le jeudi 15 avril 2010 à 05:40 +0200, Eric Dumazet a écrit :

Oh well, I revert this comment, we need a mutual exclusion between cpus if we dont want to miss some increments, even in a slow path.

__get_cpu_var(netdev_rx_stat).field++; is safe because updated only by this cpu.

Once you use per_cpu(netdev_rx_stat, cpu).field++; you need a synchronization since this is not atomic.

rps_unlock() is a synchronization if needed.

Quoted 3 lines

so queue->dropped++ is safer and faster too (inside the rps_lock section)

It involves more lines of code, moving netdev_rx_stat into softdata.

Signature · 2 lines
Regards,
Changli Gao([email protected])