Skip to contentSkip to conversation

net: 8390: Fix possible data races in __ei_get_stats

PATCH In review Jia-Ju Bai 2 participants 7 messages May 7, 2018 – May 8, 2018

Subsystems (3)
lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 net: 8390: Fix possible data races in __ei_get_stats

The write operations to "dev->stats" are protected by the spinlock on line 862-864, but the read operations to this data on line 858 and 867 are not protected by the spinlock. Thus, there may exist data races for "dev->stats".

To fix the data races, the read operations to "dev->stats" are protected by the spinlock, and a local variable is used for return.

---
 drivers/net/ethernet/8390/lib8390.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/net/ethernet/8390/lib8390.c +10 -4
index c9c55c9eab9f..198952247d30 100644
@@ -852,19 +852,25 @@ static struct net_device_stats *__ei_get_stats(struct net_device *dev)
 	unsigned long ioaddr = dev->base_addr;
 	struct ei_device *ei_local = netdev_priv(dev);
 	unsigned long flags;
+	struct net_device_stats *stats;
+
+	spin_lock_irqsave(&ei_local->page_lock, flags);
 
 	/* If the card is stopped, just return the present stats. */
-	if (!netif_running(dev))
-		return &dev->stats;
+	if (!netif_running(dev)) {
+		stats = &dev->stats;
+		spin_unlock_irqrestore(&ei_local->page_lock, flags);
+		return stats;
+	}
 
-	spin_lock_irqsave(&ei_local->page_lock, flags);
 	/* Read the counter registers, assuming we are in page 0. */
 	dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0);
 	dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1);
 	dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);
+	stats = &dev->stats;
 	spin_unlock_irqrestore(&ei_local->page_lock, flags);
 
-	return &dev->stats;
+	return stats;
 }
 
 /*
Signature · 1 line
2.17.0
#1 lore raw
Quoted 38 lines · Jia-Ju Bai

The write operations to "dev->stats" are protected by the spinlock on line 862-864, but the read operations to this data on line 858 and 867 are not protected by the spinlock. Thus, there may exist data races for "dev->stats".

To fix the data races, the read operations to "dev->stats" are protected by the spinlock, and a local variable is used for return.

---
 drivers/net/ethernet/8390/lib8390.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/net/ethernet/8390/lib8390.c +8 -3
index c9c55c9eab9f..198952247d30 100644
@@ -852,19 +852,25 @@ static struct net_device_stats *__ei_get_stats(struct net_device *dev)
 	unsigned long ioaddr = dev->base_addr;
 	struct ei_device *ei_local = netdev_priv(dev);
 	unsigned long flags;
+	struct net_device_stats *stats;
+
+	spin_lock_irqsave(&ei_local->page_lock, flags);
 
 	/* If the card is stopped, just return the present stats. */
-	if (!netif_running(dev))
-		return &dev->stats;
+	if (!netif_running(dev)) {
+		stats = &dev->stats;
+		spin_unlock_irqrestore(&ei_local->page_lock, flags);
+		return stats;
+	}
 
-	spin_lock_irqsave(&ei_local->page_lock, flags);
 	/* Read the counter registers, assuming we are in page 0. */
 	dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0);

drivers/net/ethernet/8390/lib8390.c static struct net_device_stats *__ei_get_stats(struct net_device *dev) · line 876

Quoted 10 lines
drivers/net/ethernet/8390/lib8390.c +2 -1
 	dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1);
 	dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);
+	stats = &dev->stats;
 	spin_unlock_irqrestore(&ei_local->page_lock, flags);
 
-	return &dev->stats;
+	return stats;
 }
 
 /*

dev->stats is not a pointer, it is an array embedded in the struct net_device

So this patch is not needed, since dev->stats can not change.

author
#2 lore raw
Quoted 48 lines · Eric Dumazet
Quoted 46 lines · Jia-Ju Bai

The write operations to "dev->stats" are protected by the spinlock on line 862-864, but the read operations to this data on line 858 and 867 are not protected by the spinlock. Thus, there may exist data races for "dev->stats".

To fix the data races, the read operations to "dev->stats" are protected by the spinlock, and a local variable is used for return.

---
  drivers/net/ethernet/8390/lib8390.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
drivers/net/ethernet/8390/lib8390.c +10 -4
index c9c55c9eab9f..198952247d30 100644
@@ -852,19 +852,25 @@ static struct net_device_stats *__ei_get_stats(struct net_device *dev)
  	unsigned long ioaddr = dev->base_addr;
  	struct ei_device *ei_local = netdev_priv(dev);
  	unsigned long flags;
+	struct net_device_stats *stats;
+
+	spin_lock_irqsave(&ei_local->page_lock, flags);
 
  	/* If the card is stopped, just return the present stats. */
-	if (!netif_running(dev))
-		return &dev->stats;
+	if (!netif_running(dev)) {
+		stats = &dev->stats;
+		spin_unlock_irqrestore(&ei_local->page_lock, flags);
+		return stats;
+	}
 
-	spin_lock_irqsave(&ei_local->page_lock, flags);
  	/* Read the counter registers, assuming we are in page 0. */
  	dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0);
  	dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1);
  	dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);
+	stats = &dev->stats;
  	spin_unlock_irqrestore(&ei_local->page_lock, flags);
 
-	return &dev->stats;
+	return stats;
  }
 
Quoted 6 lines

drivers/net/ethernet/8390/lib8390.c static struct net_device_stats *__ei_get_stats(struct net_device *dev) · line 876

Quoted 2 lines
drivers/net/ethernet/8390/lib8390.c +0 -0
  /*

dev->stats is not a pointer, it is an array embedded in the struct net_device

So this patch is not needed, since dev->stats can not change.

Thanks for your reply :)

I do not understand that why "dev->stats can not change". Its data is indeed changed by the code:

     dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0);
     dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1);
     dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);

So I think a data race may occur when returning "dev->stats" without lock protection.

By the way, I find this possible data race is similar to the previous commit 7b31b4deda76 for the tg3 driver.

Best wishes,
Jia-Ju Bai

#3 lore raw
Quoted 54 lines · Jia-Ju Bai
Quoted 49 lines · Eric Dumazet
Quoted 34 lines · Jia-Ju Bai

The write operations to "dev->stats" are protected by the spinlock on line 862-864, but the read operations to this data on line 858 and 867 are not protected by the spinlock. Thus, there may exist data races for "dev->stats".

To fix the data races, the read operations to "dev->stats" are protected by the spinlock, and a local variable is used for return.

---
  drivers/net/ethernet/8390/lib8390.c | 14 ++++++++++----

  1 file changed, 10 insertions(+), 4 deletions(-)
drivers/net/ethernet/8390/lib8390.c +8 -2
index c9c55c9eab9f..198952247d30 100644
@@ -852,19 +852,25 @@ static struct net_device_stats *__ei_get_stats(struct net_device *dev)
       unsigned long ioaddr = dev->base_addr;
       struct ei_device *ei_local = netdev_priv(dev);
       unsigned long flags;
+    struct net_device_stats *stats;
+
+    spin_lock_irqsave(&ei_local->page_lock, flags);
         /* If the card is stopped, just return the present stats. */
-    if (!netif_running(dev))
-        return &dev->stats;
+    if (!netif_running(dev)) {
+        stats = &dev->stats;
+        spin_unlock_irqrestore(&ei_local->page_lock, flags);
+        return stats;
+    }
   -    spin_lock_irqsave(&ei_local->page_lock, flags);
       /* Read the counter registers, assuming we are in page 0. */

drivers/net/ethernet/8390/lib8390.c static struct net_device_stats *__ei_get_stats(struct net_device *dev) · line 875

Quoted 10 lines
drivers/net/ethernet/8390/lib8390.c +2 -0
       dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0);
       dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1);
       dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);
+    stats = &dev->stats;
       spin_unlock_irqrestore(&ei_local->page_lock, flags);
   -    return &dev->stats;
+    return stats;
   }
     /*
 

dev->stats is not a pointer, it is an array embedded in the struct net_device

Quoted 1 line

So this patch is not needed, since dev->stats can not change.

Quoted 7 lines

Thanks for your reply :)

I do not understand that why "dev->stats can not change". Its data is indeed changed by the code:
     dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0); dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1); dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);

So ?

Quoted 2 lines

So I think a data race may occur when returning "dev->stats" without lock protection.

&dev->stats is a stable value.

It wont change over the lifetime of net_device object.

Adding a barrier before or after getting &dev->stats is useless, confusing and really not needed.

Quoted 2 lines

By the way, I find this possible data race is similar to the previous commit 7b31b4deda76 for the tg3 driver.

Very different things really.

This does a copy of the whole stats, not the pointer :

*stats = tp->net_stats_prev;

I guess you are confusing simple C semantics about returning the address of a structure, instead of returning a whole structure.

If __ei_get_stats(struct net_device *dev) prototype was :

struct net_device_stats __ei_get_stats(struct net_device *dev)

instead of :

struct net_device_stats *__ei_get_stats(struct net_device *dev)

Then sure, your patch might been needed.

author
#4 lore raw
Quoted 60 lines · Eric Dumazet
Quoted 50 lines · Jia-Ju Bai
Quoted 48 lines · Eric Dumazet
Quoted 34 lines · Jia-Ju Bai

The write operations to "dev->stats" are protected by the spinlock on line 862-864, but the read operations to this data on line 858 and 867 are not protected by the spinlock. Thus, there may exist data races for "dev->stats".

To fix the data races, the read operations to "dev->stats" are protected by the spinlock, and a local variable is used for return.

---
   drivers/net/ethernet/8390/lib8390.c | 14 ++++++++++----
   1 file changed, 10 insertions(+), 4 deletions(-)
drivers/net/ethernet/8390/lib8390.c +8 -2
index c9c55c9eab9f..198952247d30 100644
@@ -852,19 +852,25 @@ static struct net_device_stats *__ei_get_stats(struct net_device *dev)
       unsigned long ioaddr = dev->base_addr;
       struct ei_device *ei_local = netdev_priv(dev);
       unsigned long flags;
+    struct net_device_stats *stats;
+
+    spin_lock_irqsave(&ei_local->page_lock, flags);
         /* If the card is stopped, just return the present stats. */
-    if (!netif_running(dev))
-        return &dev->stats;
+    if (!netif_running(dev)) {
+        stats = &dev->stats;
+        spin_unlock_irqrestore(&ei_local->page_lock, flags);
+        return stats;
+    }
   -    spin_lock_irqsave(&ei_local->page_lock, flags);
       /* Read the counter registers, assuming we are in page 0. */

drivers/net/ethernet/8390/lib8390.c static struct net_device_stats *__ei_get_stats(struct net_device *dev) · line 875

Quoted 10 lines
drivers/net/ethernet/8390/lib8390.c +2 -0
       dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0);
       dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1);
       dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);
+    stats = &dev->stats;
       spin_unlock_irqrestore(&ei_local->page_lock, flags);
   -    return &dev->stats;
+    return stats;
   }
     /*
 

dev->stats is not a pointer, it is an array embedded in the struct net_device

Quoted 8 lines
Quoted 1 line

So this patch is not needed, since dev->stats can not change.

Thanks for your reply :)

I do not understand that why "dev->stats can not change". Its data is indeed changed by the code:

      dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0);
      dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1);
      dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);
Quoted 9 lines

So ?

Quoted 1 line

So I think a data race may occur when returning "dev->stats" without lock protection.

&dev->stats is a stable value.

It wont change over the lifetime of net_device object.

Adding a barrier before or after getting &dev->stats is useless, confusing and really not needed.

Yes, "&dev->stats" will not change, because it is a fixed address.
But the field data in "dev->stats" is changed (rx_frame_errors, rx_crc_errors and rx_missed_errors).
So if the driver returns "&dev->stats" without lock protection (like on line 858), the field data value of this return value can be the changed field data value or unchanged field data value.

Best wishes,
Jia-Ju Bai

#5 lore raw
Quoted 3 lines · Jia-Ju Bai

Yes, "&dev->stats" will not change, because it is a fixed address.
But the field data in "dev->stats" is changed (rx_frame_errors, rx_crc_errors and rx_missed_errors).
So if the driver returns "&dev->stats" without lock protection (like on line 858), the field data value of this return value can be the changed field data value or unchanged field data value.

We do not care.

This function can be called by multiple cpus at the same time.

As soon as one cpu returns from it, another cpu can happily modify dev->stats.ANYFIELD.

Your patch fixes nothing at all.

author
#6 lore raw
Quoted 7 lines · Eric Dumazet
Quoted 3 lines · Jia-Ju Bai

Yes, "&dev->stats" will not change, because it is a fixed address.
But the field data in "dev->stats" is changed (rx_frame_errors, rx_crc_errors and rx_missed_errors).
So if the driver returns "&dev->stats" without lock protection (like on line 858), the field data value of this return value can be the changed field data value or unchanged field data value.

Quoted 8 lines

We do not care.

This function can be called by multiple cpus at the same time.

As soon as one cpu returns from it, another cpu can happily modify dev->stats.ANYFIELD.

Your patch fixes nothing at all.

Okay, thanks.
I also find that my patch does not work...

Best wishes,
Jia-Ju Bai