Skip to contentSkip to conversation

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

PATCH Superseded Yangbo Lu 3 participants 4 messages Nov 26, 2019 – Nov 27, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yangbo Lu

author
#0 lore raw

PATCH 1/1 net: mscc: ocelot: fix potential issues accessing skbs list

Fix two prtential issues accessing skbs list.
- Protect skbs list in case of competing for accessing.
- Break the matching loop when find the matching skb to avoid consuming more skbs incorrectly. The ID is only from 0 to 3, but the FIFO supports 128 timestamps at most.

---
 drivers/net/ethernet/mscc/ocelot.c | 11 +++++++++++
 include/soc/mscc/ocelot.h          |  2 ++
 2 files changed, 13 insertions(+)
drivers/net/ethernet/mscc/ocelot.c +11 -0
index 0e96ffa..5d842d8 100644
@@ -580,6 +580,7 @@ int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
 {
 	struct skb_shared_info *shinfo = skb_shinfo(skb);
 	struct ocelot *ocelot = ocelot_port->ocelot;
+	unsigned long flags;
 
 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
@@ -594,7 +595,9 @@ int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
 		oskb->skb = skb;
 		oskb->id = ocelot_port->ts_id % 4;
 
+		spin_lock_irqsave(&ocelot_port->skbs_lock, flags);
 		list_add_tail(&oskb->head, &ocelot_port->skbs);
+		spin_unlock_irqrestore(&ocelot_port->skbs_lock, flags);
 		return 0;
 	}
 	return -ENODATA;
@@ -702,6 +705,7 @@ static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
 void ocelot_get_txtstamp(struct ocelot *ocelot)
 {
 	int budget = OCELOT_PTP_QUEUE_SZ;
+	unsigned long flags;
 
 	while (budget--) {
 		struct skb_shared_hwtstamps shhwtstamps;
@@ -727,6 +731,7 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
 		/* Retrieve its associated skb */
 		port = ocelot->ports[txport];
 
+		spin_lock_irqsave(&port->skbs_lock, flags);
 		list_for_each_safe(pos, tmp, &port->skbs) {
 			entry = list_entry(pos, struct ocelot_skb, head);
 			if (entry->id != id)
@@ -736,7 +741,9 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
 
 			list_del(pos);
 			kfree(entry);
+			break;
 		}
+		spin_unlock_irqrestore(&port->skbs_lock, flags);
 
 		/* Next ts */
 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
@@ -2205,6 +2212,7 @@ void ocelot_init_port(struct ocelot *ocelot, int port)
 {
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
+	spin_lock_init(&ocelot_port->skbs_lock);
 	INIT_LIST_HEAD(&ocelot_port->skbs);
 
 	/* Basic L2 initialization */
@@ -2493,6 +2501,7 @@ void ocelot_deinit(struct ocelot *ocelot)
 	struct list_head *pos, *tmp;
 	struct ocelot_port *port;
 	struct ocelot_skb *entry;
+	unsigned long flags;
 	int i;
 
 	cancel_delayed_work(&ocelot->stats_work);
@@ -2503,6 +2512,7 @@ void ocelot_deinit(struct ocelot *ocelot)
 	for (i = 0; i < ocelot->num_phys_ports; i++) {
 		port = ocelot->ports[i];
 
+		spin_lock_irqsave(&port->skbs_lock, flags);
 		list_for_each_safe(pos, tmp, &port->skbs) {
 			entry = list_entry(pos, struct ocelot_skb, head);
 
@@ -2510,6 +2520,7 @@ void ocelot_deinit(struct ocelot *ocelot)
 			dev_kfree_skb_any(entry->skb);
 			kfree(entry);
 		}
+		spin_unlock_irqrestore(&port->skbs_lock, flags);
 	}
 }
 EXPORT_SYMBOL(ocelot_deinit);
include/soc/mscc/ocelot.h +2 -0
index e1108a5..7973458 100644
@@ -426,6 +426,8 @@ struct ocelot_port {
 
 	u8				ptp_cmd;
 	struct list_head		skbs;
+	/* Protects the skbs list */
+	spinlock_t			skbs_lock;
 	u8				ts_id;
 };
 
Signature · 1 line
2.7.4
#1 lore raw

Hi Yangbo,

Quoted 9 lines · Yangbo Lu

Fix two prtential issues accessing skbs list.
- Protect skbs list in case of competing for accessing.
- Break the matching loop when find the matching skb to avoid consuming more skbs incorrectly. The ID is only from 0 to 3, but the FIFO supports 128 timestamps at most.

---

Don't you want to convert struct ocelot_skb to a proper struct sk_buff_head skb_queue, which has its own lock when calling skb_dequeue? The timestamp ID can be held in skb->cb.

Quoted 43 lines
 drivers/net/ethernet/mscc/ocelot.c | 11 +++++++++++
 include/soc/mscc/ocelot.h          |  2 ++
 2 files changed, 13 insertions(+)
drivers/net/ethernet/mscc/ocelot.c +5 -0
index 0e96ffa..5d842d8 100644
@@ -580,6 +580,7 @@ int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
 {
        struct skb_shared_info *shinfo = skb_shinfo(skb);
        struct ocelot *ocelot = ocelot_port->ocelot;
+       unsigned long flags;
 
        if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
            ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
@@ -594,7 +595,9 @@ int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
                oskb->skb = skb;
                oskb->id = ocelot_port->ts_id % 4;
 
+               spin_lock_irqsave(&ocelot_port->skbs_lock, flags);
                list_add_tail(&oskb->head, &ocelot_port->skbs);
+               spin_unlock_irqrestore(&ocelot_port->skbs_lock, flags);
                return 0;
        }
        return -ENODATA;
@@ -702,6 +705,7 @@ static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
 void ocelot_get_txtstamp(struct ocelot *ocelot)
 {
        int budget = OCELOT_PTP_QUEUE_SZ;
+       unsigned long flags;
 
        while (budget--) {
                struct skb_shared_hwtstamps shhwtstamps;
@@ -727,6 +731,7 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
                /* Retrieve its associated skb */
                port = ocelot->ports[txport];
 
+               spin_lock_irqsave(&port->skbs_lock, flags);
                list_for_each_safe(pos, tmp, &port->skbs) {
                        entry = list_entry(pos, struct ocelot_skb, head);
                        if (entry->id != id)

drivers/net/ethernet/mscc/ocelot.c void ocelot_get_txtstamp(struct ocelot *ocelot) · line 744

Quoted 4 lines
drivers/net/ethernet/mscc/ocelot.c +1 -0
@@ -736,7 +741,9 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
 
                        list_del(pos);
                        kfree(entry);
+                       break;

I think this should be a separate patch, since it is unrelated to locking.

Quoted 51 lines
drivers/net/ethernet/mscc/ocelot.c +5 -0
                }
+               spin_unlock_irqrestore(&port->skbs_lock, flags);
 
                /* Next ts */
                ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
@@ -2205,6 +2212,7 @@ void ocelot_init_port(struct ocelot *ocelot, int port)
 {
        struct ocelot_port *ocelot_port = ocelot->ports[port];
 
+       spin_lock_init(&ocelot_port->skbs_lock);
        INIT_LIST_HEAD(&ocelot_port->skbs);
 
        /* Basic L2 initialization */
@@ -2493,6 +2501,7 @@ void ocelot_deinit(struct ocelot *ocelot)
        struct list_head *pos, *tmp;
        struct ocelot_port *port;
        struct ocelot_skb *entry;
+       unsigned long flags;
        int i;
 
        cancel_delayed_work(&ocelot->stats_work);
@@ -2503,6 +2512,7 @@ void ocelot_deinit(struct ocelot *ocelot)
        for (i = 0; i < ocelot->num_phys_ports; i++) {
                port = ocelot->ports[i];
 
+               spin_lock_irqsave(&port->skbs_lock, flags);
                list_for_each_safe(pos, tmp, &port->skbs) {
                        entry = list_entry(pos, struct ocelot_skb, head);
 
@@ -2510,6 +2520,7 @@ void ocelot_deinit(struct ocelot *ocelot)
                        dev_kfree_skb_any(entry->skb);
                        kfree(entry);
                }
+               spin_unlock_irqrestore(&port->skbs_lock, flags);
        }
 }
 EXPORT_SYMBOL(ocelot_deinit);
include/soc/mscc/ocelot.h +2 -1
index e1108a5..7973458 100644
@@ -426,6 +426,8 @@ struct ocelot_port {
 
        u8                              ptp_cmd;
        struct list_head                skbs;
+       /* Protects the skbs list */
+       spinlock_t                      skbs_lock;
        u8                              ts_id;
 };
 
--
Quoted 2 lines

2.7.4

Regards,
-Vladimir

author
#2 lore raw

Thanks Vladimir and David.
I hadn't known such skb queue before. Sent out v2.

Best regards,
Yangbo Lu

Quoted 136 lines

-----Original Message-----
From: Vladimir Oltean <[email protected]>
Sent: Tuesday, November 26, 2019 8:32 PM
To: Y.b. Lu <[email protected]>
Cc: netdev <[email protected]>; Alexandre Belloni <[email protected]>; Microchip Linux Driver Support <[email protected]>; David S . Miller <[email protected]>
Subject: Re: [PATCH] net: mscc: ocelot: fix potential issues accessing skbs list

Hi Yangbo,

Quoted 9 lines · Yangbo Lu

Fix two prtential issues accessing skbs list.
- Protect skbs list in case of competing for accessing.
- Break the matching loop when find the matching skb to avoid consuming more skbs incorrectly. The ID is only from 0 to 3, but the FIFO supports 128 timestamps at most.

---

Don't you want to convert struct ocelot_skb to a proper struct sk_buff_head skb_queue, which has its own lock when calling skb_dequeue? The timestamp ID can be held in skb->cb.

Quoted 10 lines
 drivers/net/ethernet/mscc/ocelot.c | 11 +++++++++++
 include/soc/mscc/ocelot.h          |  2 ++
 2 files changed, 13 insertions(+)
drivers/net/ethernet/mscc/ocelot.c +0 -0
index 0e96ffa..5d842d8 100644

drivers/net/ethernet/mscc/ocelot.c int ocelot_port_add_txtstamp_skb(struct · line 586

Quoted 7 lines
drivers/net/ethernet/mscc/ocelot.c +1 -0
@@ -580,6 +580,7 @@ int ocelot_port_add_txtstamp_skb(struct
 ocelot_port *ocelot_port,  {
        struct skb_shared_info *shinfo = skb_shinfo(skb);
        struct ocelot *ocelot = ocelot_port->ocelot;
+       unsigned long flags;
 
        if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
            ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)

{ @@

Quoted 1 line

-594,7 +595,9 @@ int ocelot_port_add_txtstamp_skb(struct ocelot_port

*ocelot_port,

Quoted 27 lines
                oskb->skb = skb;
                oskb->id = ocelot_port->ts_id % 4;
drivers/net/ethernet/mscc/ocelot.c +6 -0
+               spin_lock_irqsave(&ocelot_port->skbs_lock, flags);
                list_add_tail(&oskb->head, &ocelot_port->skbs);
+               spin_unlock_irqrestore(&ocelot_port->skbs_lock,
+ flags);
                return 0;
        }
        return -ENODATA;
@@ -702,6 +705,7 @@ static void ocelot_get_hwtimestamp(struct ocelot
 *ocelot,  void ocelot_get_txtstamp(struct ocelot *ocelot)  {
        int budget = OCELOT_PTP_QUEUE_SZ;
+       unsigned long flags;
 
        while (budget--) {
                struct skb_shared_hwtstamps shhwtstamps; @@ -727,6
+731,7 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
                /* Retrieve its associated skb */
                port = ocelot->ports[txport];
 
+               spin_lock_irqsave(&port->skbs_lock, flags);
                list_for_each_safe(pos, tmp, &port->skbs) {
                        entry = list_entry(pos, struct ocelot_skb, head);
                        if (entry->id != id) @@ -736,7 +741,9 @@ void

ocelot_get_txtstamp(struct ocelot *ocelot)

Quoted 3 lines
                        list_del(pos);
                        kfree(entry);

+ break;

I think this should be a separate patch, since it is unrelated to locking.

Quoted 52 lines
                }

+ spin_unlock_irqrestore(&port->skbs_lock, flags);

                /* Next ts */
                ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT,
SYS_PTP_NXT); @@ -2205,6 +2212,7 @@ void ocelot_init_port(struct
ocelot *ocelot, int port)  {
        struct ocelot_port *ocelot_port = ocelot->ports[port];
drivers/net/ethernet/mscc/ocelot.c +4 -0
+       spin_lock_init(&ocelot_port->skbs_lock);
        INIT_LIST_HEAD(&ocelot_port->skbs);
 
        /* Basic L2 initialization */
@@ -2493,6 +2501,7 @@ void ocelot_deinit(struct ocelot *ocelot)
        struct list_head *pos, *tmp;
        struct ocelot_port *port;
        struct ocelot_skb *entry;
+       unsigned long flags;
        int i;
 
        cancel_delayed_work(&ocelot->stats_work);
@@ -2503,6 +2512,7 @@ void ocelot_deinit(struct ocelot *ocelot)
        for (i = 0; i < ocelot->num_phys_ports; i++) {
                port = ocelot->ports[i];
 
+               spin_lock_irqsave(&port->skbs_lock, flags);
                list_for_each_safe(pos, tmp, &port->skbs) {
                        entry = list_entry(pos, struct ocelot_skb,
 head);
@@ -2510,6 +2520,7 @@ void ocelot_deinit(struct ocelot *ocelot)
                        dev_kfree_skb_any(entry->skb);
                        kfree(entry);
                }
+               spin_unlock_irqrestore(&port->skbs_lock, flags);
        }
 }
 EXPORT_SYMBOL(ocelot_deinit);
include/soc/mscc/ocelot.h +2 -1
index e1108a5..7973458 100644
@@ -426,6 +426,8 @@ struct ocelot_port {
 
        u8                              ptp_cmd;
        struct list_head                skbs;
+       /* Protects the skbs list */
+       spinlock_t                      skbs_lock;
        u8                              ts_id;
 };
 
--
Quoted 2 lines

2.7.4

Regards,
-Vladimir

maintainer
#3 lore raw
In reply to Yangbo Lu

From: Yangbo Lu <[email protected]>
Date: Tue, 26 Nov 2019 18:44:03 +0800

Quoted 7 lines

Fix two prtential issues accessing skbs list.
- Protect skbs list in case of competing for accessing.
- Break the matching loop when find the matching skb to avoid consuming more skbs incorrectly. The ID is only from 0 to 3, but the FIFO supports 128 timestamps at most.

Seriously, use skb_queue_head and associated helpers, we have all of the infrastructure and arrangements for this.