Skip to content

Commit 6565243

Browse files
vladimirolteandavem330
authored andcommitted
net: mscc: ocelot: add locking for the port TX timestamp ID
The ocelot_port->ts_id is used to: (a) populate skb->cb[0] for matching the TX timestamp in the PTP IRQ with an skb. (b) populate the REW_OP from the injection header of the ongoing skb. Only then is ocelot_port->ts_id incremented. This is a problem because, at least theoretically, another timestampable skb might use the same ocelot_port->ts_id before that is incremented. Normally all transmit calls are serialized by the netdev transmit spinlock, but in this case, ocelot_port_add_txtstamp_skb() is also called by DSA, which has started declaring the NETIF_F_LLTX feature since commit 2b86cb8 ("net: dsa: declare lockless TX feature for slave ports"). So the logic of using and incrementing the timestamp id should be atomic per port. The solution is to use the global ocelot_port->ts_id only while protected by the associated ocelot_port->ts_id_lock. That's where we populate skb->cb[0]. Note that for ocelot, ocelot_port_add_txtstamp_skb is called for the actual skb, but for felix, it is called for the skb's clone. That is something which will also be changed in the future. Signed-off-by: Vladimir Oltean <[email protected]> Reviewed-by: Horatiu Vultur <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Tested-by: Alexandre Belloni <[email protected]> Reviewed-by: Alexandre Belloni <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 9dda66a commit 6565243

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

drivers/net/ethernet/mscc/ocelot.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,15 @@ int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
421421

422422
if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
423423
ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
424+
spin_lock(&ocelot_port->ts_id_lock);
425+
424426
shinfo->tx_flags |= SKBTX_IN_PROGRESS;
425427
/* Store timestamp ID in cb[0] of sk_buff */
426-
skb->cb[0] = ocelot_port->ts_id % 4;
428+
skb->cb[0] = ocelot_port->ts_id;
429+
ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
427430
skb_queue_tail(&ocelot_port->tx_skbs, skb);
431+
432+
spin_unlock(&ocelot_port->ts_id_lock);
428433
return 0;
429434
}
430435
return -ENODATA;
@@ -1300,6 +1305,7 @@ void ocelot_init_port(struct ocelot *ocelot, int port)
13001305
struct ocelot_port *ocelot_port = ocelot->ports[port];
13011306

13021307
skb_queue_head_init(&ocelot_port->tx_skbs);
1308+
spin_lock_init(&ocelot_port->ts_id_lock);
13031309

13041310
/* Basic L2 initialization */
13051311

drivers/net/ethernet/mscc/ocelot_net.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@ static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
349349

350350
if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
351351
info.rew_op = ocelot_port->ptp_cmd;
352-
if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
353-
info.rew_op |= (ocelot_port->ts_id % 4) << 3;
354-
ocelot_port->ts_id++;
355-
}
352+
if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
353+
info.rew_op |= skb->cb[0] << 3;
356354
}
357355

358356
ocelot_gen_ifh(ifh, &info);

include/soc/mscc/ocelot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ struct ocelot_port {
566566
u8 ptp_cmd;
567567
struct sk_buff_head tx_skbs;
568568
u8 ts_id;
569+
spinlock_t ts_id_lock;
569570

570571
phy_interface_t phy_mode;
571572

net/dsa/tag_ocelot.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,14 @@ static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
160160
packing(injection, &qos_class, 19, 17, OCELOT_TAG_LEN, PACK, 0);
161161

162162
if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
163+
struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
164+
163165
rew_op = ocelot_port->ptp_cmd;
164-
if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
165-
rew_op |= (ocelot_port->ts_id % 4) << 3;
166-
ocelot_port->ts_id++;
167-
}
166+
/* Retrieve timestamp ID populated inside skb->cb[0] of the
167+
* clone by ocelot_port_add_txtstamp_skb
168+
*/
169+
if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
170+
rew_op |= clone->cb[0] << 3;
168171

169172
packing(injection, &rew_op, 125, 117, OCELOT_TAG_LEN, PACK, 0);
170173
}

0 commit comments

Comments
 (0)