Skip to contentSkip to conversation

net: mscc: ocelot: fix potential issues accessing skbs list

PATCH In review Yangbo Lu 2 participants 4 messages Nov 27, 2019

Subsystems (3)
lore
Share & tools
lore

Yangbo Lu

author
#0 lore raw

v2 cover net: mscc: ocelot: fix potential issues accessing skbs list

Fix two prtential issues accessing skbs list.
- Break the matching loop when find the matching skb to avoid consuming more skbs incorrectly. The timestamp ID is only from 0 to 3 while the FIFO supports 128 timestamps at most.
- Convert to use skb queue instead of the list of skbs to provide protect with lock.

---
Changes for v2:
- Split into two patches.
- Converted to use skb queue.

Yangbo Lu (2):
net: mscc: ocelot: avoid incorrect consuming in skbs list
net: mscc: ocelot: use skb queue instead of skbs list

 drivers/net/ethernet/mscc/ocelot.c | 55 +++++++++++++-------------------------
 include/soc/mscc/ocelot.h          |  9 +------
 2 files changed, 20 insertions(+), 44 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 1/2 net: mscc: ocelot: avoid incorrect consuming in skbs list

Break the matching loop when find the matching skb for TX timestamp. This is to avoid consuming more skbs incorrectly. The timestamp ID is from 0 to 3 while the FIFO could support 128 timestamps at most.

---
Changes for v2:
- Split a single patch for this fix-up.

---
 drivers/net/ethernet/mscc/ocelot.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mscc/ocelot.c +1 -0
index 0e96ffa..6dc9de3 100644
@@ -736,6 +736,7 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
 
 			list_del(pos);
 			kfree(entry);
+			break;
 		}
 
 		/* Next ts */
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH v2 2/2 net: mscc: ocelot: use skb queue instead of skbs list

In reply to Yangbo Lu

Convert to use skb queue instead of the list of skbs. The skb queue could provide protection with lock.

---
Changes for v2:
- Converted to use skb queue.

---
 drivers/net/ethernet/mscc/ocelot.c | 54 +++++++++++++-------------------------
 include/soc/mscc/ocelot.h          |  9 +------
 2 files changed, 19 insertions(+), 44 deletions(-)
drivers/net/ethernet/mscc/ocelot.c +18 -36
index 6dc9de3..2cccadc 100644
@@ -583,18 +583,10 @@ int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
 
 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-		struct ocelot_skb *oskb =
-			kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
-
-		if (unlikely(!oskb))
-			return -ENOMEM;
-
 		shinfo->tx_flags |= SKBTX_IN_PROGRESS;
-
-		oskb->skb = skb;
-		oskb->id = ocelot_port->ts_id % 4;
-
-		list_add_tail(&oskb->head, &ocelot_port->skbs);
+		/* Store timestamp ID in cb[0] of sk_buff */
+		skb->cb[0] = ocelot_port->ts_id % 4;
+		skb_queue_tail(&ocelot_port->tx_skbs, skb);
 		return 0;
 	}
 	return -ENODATA;
@@ -704,12 +696,11 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
 	int budget = OCELOT_PTP_QUEUE_SZ;
 
 	while (budget--) {
+		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
 		struct skb_shared_hwtstamps shhwtstamps;
-		struct list_head *pos, *tmp;
-		struct sk_buff *skb = NULL;
-		struct ocelot_skb *entry;
 		struct ocelot_port *port;
 		struct timespec64 ts;
+		unsigned long flags;
 		u32 val, id, txport;
 
 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
@@ -727,22 +718,22 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
 		/* Retrieve its associated skb */
 		port = ocelot->ports[txport];
 
-		list_for_each_safe(pos, tmp, &port->skbs) {
-			entry = list_entry(pos, struct ocelot_skb, head);
-			if (entry->id != id)
-				continue;
+		spin_lock_irqsave(&port->tx_skbs.lock, flags);
 
-			skb = entry->skb;
-
-			list_del(pos);
-			kfree(entry);
+		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
+			if (skb->cb[0] != id)
+				continue;
+			__skb_unlink(skb, &port->tx_skbs);
+			skb_match = skb;
 			break;
 		}
 
+		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
+
 		/* Next ts */
 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
 
-		if (unlikely(!skb))
+		if (unlikely(!skb_match))
 			continue;
 
 		/* Get the h/w timestamp */
@@ -751,9 +742,9 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
 		/* Set the timestamp into the skb */
 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
-		skb_tstamp_tx(skb, &shhwtstamps);
+		skb_tstamp_tx(skb_match, &shhwtstamps);
 
-		dev_kfree_skb_any(skb);
+		dev_kfree_skb_any(skb_match);
 	}
 }
 EXPORT_SYMBOL(ocelot_get_txtstamp);
@@ -2206,7 +2197,7 @@ void ocelot_init_port(struct ocelot *ocelot, int port)
 {
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
-	INIT_LIST_HEAD(&ocelot_port->skbs);
+	skb_queue_head_init(&ocelot_port->tx_skbs);
 
 	/* Basic L2 initialization */
 
@@ -2491,9 +2482,7 @@ EXPORT_SYMBOL(ocelot_init);
 
 void ocelot_deinit(struct ocelot *ocelot)
 {
-	struct list_head *pos, *tmp;
 	struct ocelot_port *port;
-	struct ocelot_skb *entry;
 	int i;
 
 	cancel_delayed_work(&ocelot->stats_work);
@@ -2503,14 +2492,7 @@ void ocelot_deinit(struct ocelot *ocelot)
 
 	for (i = 0; i < ocelot->num_phys_ports; i++) {
 		port = ocelot->ports[i];
-
-		list_for_each_safe(pos, tmp, &port->skbs) {
-			entry = list_entry(pos, struct ocelot_skb, head);
-
-			list_del(pos);
-			dev_kfree_skb_any(entry->skb);
-			kfree(entry);
-		}
+		skb_queue_purge(&port->tx_skbs);
 	}
 }
 EXPORT_SYMBOL(ocelot_deinit);
include/soc/mscc/ocelot.h +1 -8
index e1108a5..64cbbbe 100644
@@ -406,13 +406,6 @@ struct ocelot_ops {
 	int (*reset)(struct ocelot *ocelot);
 };
 
-struct ocelot_skb {
-	struct list_head head;
-	struct sk_buff *skb;
-	u8 id;
-};
-
-
 struct ocelot_port {
 	struct ocelot			*ocelot;
 
@@ -425,7 +418,7 @@ struct ocelot_port {
 	u16				vid;
 
 	u8				ptp_cmd;
-	struct list_head		skbs;
+	struct sk_buff_head		tx_skbs;
 	u8				ts_id;
 };
 
Signature · 1 line
2.7.4
maintainer
#3 lore raw
In reply to Yangbo Lu

From: Yangbo Lu <[email protected]>
Date: Wed, 27 Nov 2019 15:27:55 +0800

Quoted 11 lines

Fix two prtential issues accessing skbs list.
- Break the matching loop when find the matching skb to avoid consuming more skbs incorrectly. The timestamp ID is only from 0 to 3 while the FIFO supports 128 timestamps at most.
- Convert to use skb queue instead of the list of skbs to provide protect with lock.

---
Changes for v2:
- Split into two patches.
- Converted to use skb queue.

Series applied, thanks.