Skip to contentSkip to conversation

af_packet/tcp: fix late hardware timestamp handling

PATCH In review Kohei Enju 4 participants 11 messages Apr 29 – May 1

Subsystems (5)
lore
Share & tools
lore

Kohei Enju

author
#0 lore raw

cover af_packet/tcp: fix late hardware timestamp handling

Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may carry netdev_data instead of a resolved hwtstamp. AF_PACKET and TCP could still read that storage as a ktime_t and report bogus hardware timestamps to userspace.

This series factors the late timestamp resolution logic into a common helper, then switches AF_PACKET and TCP to use it.

Notes on SOF_TIMESTAMPING_BIND_PHC:
The generic socket receive timestamping path honors it, but the AF_PACKET and TCP receive paths touched here haven't implemented that behavior.
This series doesn't change that; those paths always resolve timestamps with cycles == false and preserve their timestamp-domain semantics.

Kohei Enju (3):
net: introduce helper to resolve hardware timestamps from skb
af_packet: use skb_get_hwtstamp() for hardware timestamps
tcp: use skb_get_hwtstamp() for hardware timestamps

 include/linux/skbuff.h | 11 +++++++++++
 include/net/tcp.h      |  2 +-
 net/core/skbuff.c      | 27 +++++++++++++++++++++++++++
 net/ipv4/tcp_input.c   |  3 ++-
 net/ipv4/tcp_ipv4.c    |  6 ++++--
 net/ipv6/tcp_ipv6.c    |  3 ++-
 net/packet/af_packet.c |  2 +-
 net/socket.c           | 27 +++------------------------
 8 files changed, 51 insertions(+), 30 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/3 net: introduce helper to resolve hardware timestamps from skb

Move the logic that resolves a hardware timestamp from an skb, including late timestamp resolution via netdev_get_tstamp(), from net/socket.c to a common helper.

Let's allow other networking code to reuse the same resolution path.

---
 include/linux/skbuff.h | 11 +++++++++++
 net/core/skbuff.c      | 27 +++++++++++++++++++++++++++
 net/socket.c           | 27 +++------------------------
 3 files changed, 41 insertions(+), 24 deletions(-)
include/linux/skbuff.h +11 -0
index 2bcf78a4de7b..651a5ae8b11c 100644
@@ -4731,6 +4731,17 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb,
 void skb_tstamp_tx(struct sk_buff *orig_skb,
 		   struct skb_shared_hwtstamps *hwtstamps);
 
+/**
+ * skb_get_hwtstamp - resolve a hardware timestamp from an skb
+ * @skb:	skb carrying the timestamp
+ * @cycles:	true to request the free-running cycle-based timestamp
+ * @if_index:	optional return pointer for the originating netdev ifindex
+ *
+ * Return: resolved hardware timestamp, or the stored skb hwtstamp when no
+ * device-specific late timestamp resolution is needed.
+ */
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index);
+
 /**
  * skb_tx_timestamp() - Driver hook for transmit timestamping
  *
net/core/skbuff.c +27 -0
index 7dad68e3b518..d11f4e2e9391 100644
@@ -5729,6 +5729,33 @@ void skb_tstamp_tx(struct sk_buff *orig_skb,
 }
 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
 
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index)
+{
+	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
+	struct net_device *orig_dev;
+	ktime_t hwtstamp;
+
+	if (if_index)
+		*if_index = 0;
+
+	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV))
+		return shhwtstamps->hwtstamp;
+
+	rcu_read_lock();
+	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
+	if (orig_dev) {
+		if (if_index)
+			*if_index = orig_dev->ifindex;
+		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
+	} else {
+		hwtstamp = shhwtstamps->hwtstamp;
+	}
+	rcu_read_unlock();
+
+	return hwtstamp;
+}
+EXPORT_SYMBOL_GPL(skb_get_hwtstamp);
+
 #ifdef CONFIG_WIRELESS
 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
 {
net/socket.c +3 -24
index 22a412fdec07..95b21b16a0fc 100644
@@ -876,21 +876,7 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
 static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
 {
 	bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
-	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
-	struct net_device *orig_dev;
-	ktime_t hwtstamp;
-
-	rcu_read_lock();
-	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
-	if (orig_dev) {
-		*if_index = orig_dev->ifindex;
-		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
-	} else {
-		hwtstamp = shhwtstamps->hwtstamp;
-	}
-	rcu_read_unlock();
-
-	return hwtstamp;
+	return skb_get_hwtstamp(skb, cycles, if_index);
 }
 
 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
@@ -940,7 +926,6 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
 {
 	u32 tsflags = READ_ONCE(sk->sk_tsflags);
 	ktime_t hwtstamp;
-	int if_index = 0;
 
 	if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
 	    ktime_to_timespec64_cond(skb->tstamp, ts))
@@ -950,10 +935,7 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
 	    skb_is_swtx_tstamp(skb, false))
 		return -ENOENT;
 
-	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-		hwtstamp = get_timestamp(sk, skb, &if_index);
-	else
-		hwtstamp = skb_hwtstamps(skb)->hwtstamp;
+	hwtstamp = get_timestamp(sk, skb, NULL);
 
 	if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
 		hwtstamp = ptp_convert_timestamp(&hwtstamp,
@@ -1033,10 +1015,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 	      !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
 	    !skb_is_swtx_tstamp(skb, false_tstamp)) {
 		if_index = 0;
-		if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-			hwtstamp = get_timestamp(sk, skb, &if_index);
-		else
-			hwtstamp = shhwtstamps->hwtstamp;
+		hwtstamp = get_timestamp(sk, skb, &if_index);
 
 		if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
 			hwtstamp = ptp_convert_timestamp(&hwtstamp,
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb

Quoted 7 lines · Kohei Enju

Move the logic that resolves a hardware timestamp from an skb, including late timestamp resolution via netdev_get_tstamp(), from net/socket.c to a common helper.

Let's allow other networking code to reuse the same resolution path.

Thanks for the fix series.

Fixes require a Fixes tag. See also https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html.

I suggest merging this and the second patch, as this is not a standalone fix, and the other is a one-line change.

Quoted 81 lines
---
 include/linux/skbuff.h | 11 +++++++++++
 net/core/skbuff.c      | 27 +++++++++++++++++++++++++++
 net/socket.c           | 27 +++------------------------
 3 files changed, 41 insertions(+), 24 deletions(-)
include/linux/skbuff.h +11 -0
index 2bcf78a4de7b..651a5ae8b11c 100644
@@ -4731,6 +4731,17 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb,
 void skb_tstamp_tx(struct sk_buff *orig_skb,
 		   struct skb_shared_hwtstamps *hwtstamps);
 
+/**
+ * skb_get_hwtstamp - resolve a hardware timestamp from an skb
+ * @skb:	skb carrying the timestamp
+ * @cycles:	true to request the free-running cycle-based timestamp
+ * @if_index:	optional return pointer for the originating netdev ifindex
+ *
+ * Return: resolved hardware timestamp, or the stored skb hwtstamp when no
+ * device-specific late timestamp resolution is needed.
+ */
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index);
+
 /**
  * skb_tx_timestamp() - Driver hook for transmit timestamping
  *
net/core/skbuff.c +27 -0
index 7dad68e3b518..d11f4e2e9391 100644
@@ -5729,6 +5729,33 @@ void skb_tstamp_tx(struct sk_buff *orig_skb,
 }
 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
 
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index)
+{
+	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
+	struct net_device *orig_dev;
+	ktime_t hwtstamp;
+
+	if (if_index)
+		*if_index = 0;
+
+	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV))
+		return shhwtstamps->hwtstamp;
+
+	rcu_read_lock();
+	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
+	if (orig_dev) {
+		if (if_index)
+			*if_index = orig_dev->ifindex;
+		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
+	} else {
+		hwtstamp = shhwtstamps->hwtstamp;
+	}
+	rcu_read_unlock();
+
+	return hwtstamp;
+}
+EXPORT_SYMBOL_GPL(skb_get_hwtstamp);
+
 #ifdef CONFIG_WIRELESS
 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
 {
net/socket.c +0 -7
index 22a412fdec07..95b21b16a0fc 100644
@@ -876,21 +876,7 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
 static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
 {
 	bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
-	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
-	struct net_device *orig_dev;
-	ktime_t hwtstamp;
-
-	rcu_read_lock();
-	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
-	if (orig_dev) {

net/socket.c static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) · line 880

Quoted 10 lines
net/socket.c +1 -8
-		*if_index = orig_dev->ifindex;
-		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
-	} else {
-		hwtstamp = shhwtstamps->hwtstamp;
-	}
-	rcu_read_unlock();
-
-	return hwtstamp;
+	return skb_get_hwtstamp(skb, cycles, if_index);
 }

At this point simpler to remove get_timestamp entirely. It's an unnecessary layer of indirection.

Perhaps pass sk_tsflags rather than bool to skb_get_hwtstamp.

Quoted 36 lines
net/socket.c +2 -9
 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
@@ -940,7 +926,6 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
 {
 	u32 tsflags = READ_ONCE(sk->sk_tsflags);
 	ktime_t hwtstamp;
-	int if_index = 0;
 
 	if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
 	    ktime_to_timespec64_cond(skb->tstamp, ts))
@@ -950,10 +935,7 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
 	    skb_is_swtx_tstamp(skb, false))
 		return -ENOENT;
 
-	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-		hwtstamp = get_timestamp(sk, skb, &if_index);
-	else
-		hwtstamp = skb_hwtstamps(skb)->hwtstamp;
+	hwtstamp = get_timestamp(sk, skb, NULL);
 
 	if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
 		hwtstamp = ptp_convert_timestamp(&hwtstamp,
@@ -1033,10 +1015,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 	      !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
 	    !skb_is_swtx_tstamp(skb, false_tstamp)) {
 		if_index = 0;
-		if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-			hwtstamp = get_timestamp(sk, skb, &if_index);
-		else
-			hwtstamp = shhwtstamps->hwtstamp;
+		hwtstamp = get_timestamp(sk, skb, &if_index);
 
 		if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
 			hwtstamp = ptp_convert_timestamp(&hwtstamp,
Signature · 1 line
2.53.0
author
#3 lore raw

Re: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb

Quoted 7 lines · Willem de Bruijn
Quoted 6 lines · Kohei Enju

Move the logic that resolves a hardware timestamp from an skb, including late timestamp resolution via netdev_get_tstamp(), from net/socket.c to a common helper.

Let's allow other networking code to reuse the same resolution path.

Quoted 8 lines
Quoted 1 line

Thanks for the fix series.

Fixes require a Fixes tag. See also https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html.

I suggest merging this and the second patch, as this is not a standalone fix, and the other is a one-line change.

Thank you for the suggestion, and it looks good to me. I'll do so in v2.

Quoted 96 lines
Quoted 82 lines
---
 include/linux/skbuff.h | 11 +++++++++++
 net/core/skbuff.c      | 27 +++++++++++++++++++++++++++
 net/socket.c           | 27 +++------------------------
 3 files changed, 41 insertions(+), 24 deletions(-)
include/linux/skbuff.h +11 -0
index 2bcf78a4de7b..651a5ae8b11c 100644
@@ -4731,6 +4731,17 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb,
 void skb_tstamp_tx(struct sk_buff *orig_skb,
 		   struct skb_shared_hwtstamps *hwtstamps);
 
+/**
+ * skb_get_hwtstamp - resolve a hardware timestamp from an skb
+ * @skb:	skb carrying the timestamp
+ * @cycles:	true to request the free-running cycle-based timestamp
+ * @if_index:	optional return pointer for the originating netdev ifindex
+ *
+ * Return: resolved hardware timestamp, or the stored skb hwtstamp when no
+ * device-specific late timestamp resolution is needed.
+ */
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index);
+
 /**
  * skb_tx_timestamp() - Driver hook for transmit timestamping
  *
net/core/skbuff.c +27 -0
index 7dad68e3b518..d11f4e2e9391 100644
@@ -5729,6 +5729,33 @@ void skb_tstamp_tx(struct sk_buff *orig_skb,
 }
 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
 
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index)
+{
+	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
+	struct net_device *orig_dev;
+	ktime_t hwtstamp;
+
+	if (if_index)
+		*if_index = 0;
+
+	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV))
+		return shhwtstamps->hwtstamp;
+
+	rcu_read_lock();
+	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
+	if (orig_dev) {
+		if (if_index)
+			*if_index = orig_dev->ifindex;
+		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
+	} else {
+		hwtstamp = shhwtstamps->hwtstamp;
+	}
+	rcu_read_unlock();
+
+	return hwtstamp;
+}
+EXPORT_SYMBOL_GPL(skb_get_hwtstamp);
+
 #ifdef CONFIG_WIRELESS
 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
 {
net/socket.c +0 -7
index 22a412fdec07..95b21b16a0fc 100644
@@ -876,21 +876,7 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
 static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
 {
 	bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
-	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
-	struct net_device *orig_dev;
-	ktime_t hwtstamp;
-
-	rcu_read_lock();
-	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
-	if (orig_dev) {

net/socket.c static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) · line 880

Quoted 10 lines
net/socket.c +1 -8
-		*if_index = orig_dev->ifindex;
-		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
-	} else {
-		hwtstamp = shhwtstamps->hwtstamp;
-	}
-	rcu_read_unlock();
-
-	return hwtstamp;
+	return skb_get_hwtstamp(skb, cycles, if_index);
 }

At this point simpler to remove get_timestamp entirely. It's an unnecessary layer of indirection.

Quoted 1 line

Perhaps pass sk_tsflags rather than bool to skb_get_hwtstamp.

Good points. I'll rework this in v2.

Thanks,
Kohei

Quoted 39 lines
Quoted 37 lines
net/socket.c +2 -9
 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
@@ -940,7 +926,6 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
 {
 	u32 tsflags = READ_ONCE(sk->sk_tsflags);
 	ktime_t hwtstamp;
-	int if_index = 0;
 
 	if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
 	    ktime_to_timespec64_cond(skb->tstamp, ts))
@@ -950,10 +935,7 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
 	    skb_is_swtx_tstamp(skb, false))
 		return -ENOENT;
 
-	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-		hwtstamp = get_timestamp(sk, skb, &if_index);
-	else
-		hwtstamp = skb_hwtstamps(skb)->hwtstamp;
+	hwtstamp = get_timestamp(sk, skb, NULL);
 
 	if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
 		hwtstamp = ptp_convert_timestamp(&hwtstamp,
@@ -1033,10 +1015,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 	      !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
 	    !skb_is_swtx_tstamp(skb, false_tstamp)) {
 		if_index = 0;
-		if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-			hwtstamp = get_timestamp(sk, skb, &if_index);
-		else
-			hwtstamp = shhwtstamps->hwtstamp;
+		hwtstamp = get_timestamp(sk, skb, &if_index);
 
 		if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
 			hwtstamp = ptp_convert_timestamp(&hwtstamp,
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb

In reply to Kohei Enju
Quoted 41 lines · Kohei Enju

Move the logic that resolves a hardware timestamp from an skb, including late timestamp resolution via netdev_get_tstamp(), from net/socket.c to a common helper.

Let's allow other networking code to reuse the same resolution path.

---
  include/linux/skbuff.h | 11 +++++++++++
  net/core/skbuff.c      | 27 +++++++++++++++++++++++++++
  net/socket.c           | 27 +++------------------------
  3 files changed, 41 insertions(+), 24 deletions(-)
include/linux/skbuff.h +11 -0
index 2bcf78a4de7b..651a5ae8b11c 100644
@@ -4731,6 +4731,17 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb,
  void skb_tstamp_tx(struct sk_buff *orig_skb,
  		   struct skb_shared_hwtstamps *hwtstamps);
 
+/**
+ * skb_get_hwtstamp - resolve a hardware timestamp from an skb
+ * @skb:	skb carrying the timestamp
+ * @cycles:	true to request the free-running cycle-based timestamp
+ * @if_index:	optional return pointer for the originating netdev ifindex
+ *
+ * Return: resolved hardware timestamp, or the stored skb hwtstamp when no
+ * device-specific late timestamp resolution is needed.
+ */
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index);
+
  /**
   * skb_tx_timestamp() - Driver hook for transmit timestamping
   *
net/core/skbuff.c +0 -0
index 7dad68e3b518..d11f4e2e9391 100644
@@ -5729,6 +5729,33 @@ void skb_tstamp_tx(struct sk_buff *orig_skb,
  }

net/core/skbuff.c void skb_tstamp_tx(struct sk_buff *orig_skb, · line 5739

Quoted 10 lines
net/core/skbuff.c +8 -0
  EXPORT_SYMBOL_GPL(skb_tstamp_tx);
 
+ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index)
+{
+	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
+	struct net_device *orig_dev;
+	ktime_t hwtstamp;
+
+	if (if_index)
+		*if_index = 0;

if_index is set to 0 here the second time when called from __sock_recv_timestamp(). IMO you can remove these two lines, because in skb_get_tx_timestamp() if_index has been removed by your commit.

Quoted 50 lines
net/core/skbuff.c +19 -0
+
+	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV))
+		return shhwtstamps->hwtstamp;
+
+	rcu_read_lock();
+	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
+	if (orig_dev) {
+		if (if_index)
+			*if_index = orig_dev->ifindex;
+		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
+	} else {
+		hwtstamp = shhwtstamps->hwtstamp;
+	}
+	rcu_read_unlock();
+
+	return hwtstamp;
+}
+EXPORT_SYMBOL_GPL(skb_get_hwtstamp);
+
  #ifdef CONFIG_WIRELESS
  void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  {
net/socket.c +1 -15
index 22a412fdec07..95b21b16a0fc 100644
@@ -876,21 +876,7 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
  static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
  {
  	bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
-	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
-	struct net_device *orig_dev;
-	ktime_t hwtstamp;
-
-	rcu_read_lock();
-	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
-	if (orig_dev) {
-		*if_index = orig_dev->ifindex;
-		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
-	} else {
-		hwtstamp = shhwtstamps->hwtstamp;
-	}
-	rcu_read_unlock();
-
-	return hwtstamp;
+	return skb_get_hwtstamp(skb, cycles, if_index);
  }
 
  static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,

net/socket.c int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk, · line 943

Quoted 4 lines
net/socket.c +0 -1
@@ -940,7 +926,6 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
  {
  	u32 tsflags = READ_ONCE(sk->sk_tsflags);
  	ktime_t hwtstamp;
-	int if_index = 0;

if_index is useless in this function, so it can be removed. This was introduced with 2410251cde0b. Maybe removing if_index here could be a separate commit, as this clean up makes sense on its own.

Quoted 17 lines
net/socket.c +1 -4
  	if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
  	    ktime_to_timespec64_cond(skb->tstamp, ts))
@@ -950,10 +935,7 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
  	    skb_is_swtx_tstamp(skb, false))
  		return -ENOENT;
 
-	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-		hwtstamp = get_timestamp(sk, skb, &if_index);
-	else
-		hwtstamp = skb_hwtstamps(skb)->hwtstamp;
+	hwtstamp = get_timestamp(sk, skb, NULL);
 
  	if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
  		hwtstamp = ptp_convert_timestamp(&hwtstamp,
@@ -1033,10 +1015,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  	      !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&

net/socket.c void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, · line 1021

Quoted 10 lines
net/socket.c +1 -4
  	    !skb_is_swtx_tstamp(skb, false_tstamp)) {
  		if_index = 0;
-		if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
-			hwtstamp = get_timestamp(sk, skb, &if_index);
-		else
-			hwtstamp = shhwtstamps->hwtstamp;
+		hwtstamp = get_timestamp(sk, skb, &if_index);
 
  		if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
  			hwtstamp = ptp_convert_timestamp(&hwtstamp,

Gerhard

author
#5 lore raw

PATCH 2/3 af_packet: use skb_get_hwtstamp() for hardware timestamps

In reply to Kohei Enju

Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. tpacket_get_timestamp() unconditionally interprets skb_shared_hwtstamps as a resolved hardware timestamp, and can report bogus hardware timestamps to userspace.

Use skb_get_hwtstamp() instead of reading hwtstamp directly, so packet sockets follow the same hardware timestamp resolution path as the socket layer.

Note that skb_get_hwtstamp() is called with cycles == false, since AF_PACKET hasn't honored SOF_TIMESTAMPING_BIND_PHC so far, and this patch doesn't change that behavior.

Fixes 97dc7cd92ac6 ("ptp: Support late timestamp determination")
---
 net/packet/af_packet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/packet/af_packet.c +1 -1
index 8e6f3a734ba0..e88bc3f1156f 100644
@@ -458,7 +458,7 @@ static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec64 *ts,
 
 	if (shhwtstamps &&
 	    (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
-	    ktime_to_timespec64_cond(shhwtstamps->hwtstamp, ts))
+	    ktime_to_timespec64_cond(skb_get_hwtstamp(skb, false, NULL), ts))
 		return TP_STATUS_TS_RAW_HARDWARE;
 
 	if ((flags & SOF_TIMESTAMPING_SOFTWARE) &&
Signature · 1 line
2.53.0
author
#6 lore raw

PATCH 3/3 tcp: use skb_get_hwtstamp() for hardware timestamps

In reply to Kohei Enju

Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. TCP receive timestamping can then interpret the stored value as a ktime_t and report bogus hardware timestamps to userspace.

Use skb_get_hwtstamp() instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb. Additionally, recognize SKBTX_HW_TSTAMP_NETDEV as indicating a receive timestamp is present.

Note that skb_get_hwtstamp() is called with cycles == false, since TCP hasn't honored SOF_TIMESTAMPING_BIND_PHC so far, and this patch doesn't change that behavior.

Fixes 97dc7cd92ac6 ("ptp: Support late timestamp determination")
---
 include/net/tcp.h    | 2 +-
 net/ipv4/tcp_input.c | 3 ++-
 net/ipv4/tcp_ipv4.c  | 6 ++++--
 net/ipv6/tcp_ipv6.c  | 3 ++-
 4 files changed, 9 insertions(+), 5 deletions(-)
include/net/tcp.h +1 -1
index ecbadcb3a744..7b5fcee97079 100644
@@ -524,7 +524,7 @@ tcp_update_recv_tstamps(struct sk_buff *skb,
 			struct scm_timestamping_internal *tss)
 {
 	tss->ts[0] = skb->tstamp;
-	tss->ts[2] = skb_hwtstamps(skb)->hwtstamp;
+	tss->ts[2] = skb_get_hwtstamp(skb, false, NULL);
 }
 
 void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
net/ipv4/tcp_input.c +2 -1
index d5c9e65d9760..9fd473559b58 100644
@@ -5237,7 +5237,8 @@ static bool tcp_try_coalesce(struct sock *sk,
 	if (TCP_SKB_CB(from)->has_rxtstamp) {
 		TCP_SKB_CB(to)->has_rxtstamp = true;
 		to->tstamp = from->tstamp;
-		skb_hwtstamps(to)->hwtstamp = skb_hwtstamps(from)->hwtstamp;
+		skb_hwtstamps(to)->hwtstamp = skb_get_hwtstamp(from, false, NULL);
+		skb_shinfo(to)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV;
 	}
 
 	return true;
net/ipv4/tcp_ipv4.c +4 -2
index 8fc24c3743c5..c35d82317764 100644
@@ -1993,7 +1993,8 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
 		if (TCP_SKB_CB(skb)->has_rxtstamp) {
 			TCP_SKB_CB(tail)->has_rxtstamp = true;
 			tail->tstamp = skb->tstamp;
-			skb_hwtstamps(tail)->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
+			skb_hwtstamps(tail)->hwtstamp = skb_get_hwtstamp(skb, false, NULL);
+			skb_shinfo(tail)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV;
 		}
 
 		/* Not as strict as GRO. We only need to carry mss max value */
@@ -2062,7 +2063,8 @@ static void tcp_v4_fill_cb(struct sk_buff *skb, const struct iphdr *iph,
 	TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
 	TCP_SKB_CB(skb)->sacked	 = 0;
 	TCP_SKB_CB(skb)->has_rxtstamp =
-			skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
+			skb->tstamp || skb_hwtstamps(skb)->hwtstamp ||
+			(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV);
 }
 
 /*
net/ipv6/tcp_ipv6.c +2 -1
index 2c3f7a739709..3361ed7f94de 100644
@@ -1704,7 +1704,8 @@ static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,
 	TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
 	TCP_SKB_CB(skb)->sacked = 0;
 	TCP_SKB_CB(skb)->has_rxtstamp =
-			skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
+			skb->tstamp || skb_hwtstamps(skb)->hwtstamp ||
+			(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV);
 }
 
 INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
Signature · 1 line
2.53.0
maintainer
#7 lore raw

Re: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps

Quoted 9 lines · Kohei Enju

Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. TCP receive timestamping can then interpret the stored value as a ktime_t and report bogus hardware timestamps to userspace.

Use skb_get_hwtstamp() instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.

Why? Does this preclude supporting SOF_TIMESTAMPING_BIND_PHC for such sockets at a later time?

It is just as easy to coalesce the cookie as the htwtstamp.

That also avoids the need to mask out SKBTX_HW_TSTAMP_NETDEV.

Quoted 2 lines

Additionally, recognize SKBTX_HW_TSTAMP_NETDEV as
indicating a receive timestamp is present.

What is the reason for this extra condition?

Quoted 81 lines

Note that skb_get_hwtstamp() is called with cycles == false, since TCP hasn't honored SOF_TIMESTAMPING_BIND_PHC so far, and this patch doesn't change that behavior.

Fixes 97dc7cd92ac6 ("ptp: Support late timestamp determination")
---
 include/net/tcp.h    | 2 +-
 net/ipv4/tcp_input.c | 3 ++-
 net/ipv4/tcp_ipv4.c  | 6 ++++--
 net/ipv6/tcp_ipv6.c  | 3 ++-
 4 files changed, 9 insertions(+), 5 deletions(-)
include/net/tcp.h +1 -1
index ecbadcb3a744..7b5fcee97079 100644
@@ -524,7 +524,7 @@ tcp_update_recv_tstamps(struct sk_buff *skb,
 			struct scm_timestamping_internal *tss)
 {
 	tss->ts[0] = skb->tstamp;
-	tss->ts[2] = skb_hwtstamps(skb)->hwtstamp;
+	tss->ts[2] = skb_get_hwtstamp(skb, false, NULL);
 }
 
 void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
net/ipv4/tcp_input.c +2 -1
index d5c9e65d9760..9fd473559b58 100644
@@ -5237,7 +5237,8 @@ static bool tcp_try_coalesce(struct sock *sk,
 	if (TCP_SKB_CB(from)->has_rxtstamp) {
 		TCP_SKB_CB(to)->has_rxtstamp = true;
 		to->tstamp = from->tstamp;
-		skb_hwtstamps(to)->hwtstamp = skb_hwtstamps(from)->hwtstamp;
+		skb_hwtstamps(to)->hwtstamp = skb_get_hwtstamp(from, false, NULL);
+		skb_shinfo(to)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV;
 	}
 
 	return true;
net/ipv4/tcp_ipv4.c +4 -2
index 8fc24c3743c5..c35d82317764 100644
@@ -1993,7 +1993,8 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
 		if (TCP_SKB_CB(skb)->has_rxtstamp) {
 			TCP_SKB_CB(tail)->has_rxtstamp = true;
 			tail->tstamp = skb->tstamp;
-			skb_hwtstamps(tail)->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
+			skb_hwtstamps(tail)->hwtstamp = skb_get_hwtstamp(skb, false, NULL);
+			skb_shinfo(tail)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV;
 		}
 
 		/* Not as strict as GRO. We only need to carry mss max value */
@@ -2062,7 +2063,8 @@ static void tcp_v4_fill_cb(struct sk_buff *skb, const struct iphdr *iph,
 	TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
 	TCP_SKB_CB(skb)->sacked	 = 0;
 	TCP_SKB_CB(skb)->has_rxtstamp =
-			skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
+			skb->tstamp || skb_hwtstamps(skb)->hwtstamp ||
+			(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV);
 }
 
 /*
net/ipv6/tcp_ipv6.c +2 -1
index 2c3f7a739709..3361ed7f94de 100644
@@ -1704,7 +1704,8 @@ static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,
 	TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
 	TCP_SKB_CB(skb)->sacked = 0;
 	TCP_SKB_CB(skb)->has_rxtstamp =
-			skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
+			skb->tstamp || skb_hwtstamps(skb)->hwtstamp ||
+			(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV);
 }
 
 INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
Signature · 1 line
2.53.0
author
#8 lore raw

Re: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps

Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Kohei Enju

Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. TCP receive timestamping can then interpret the stored value as a ktime_t and report bogus hardware timestamps to userspace.

Quoted 7 lines
Quoted 4 lines

Use skb_get_hwtstamp() instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.

Why? Does this preclude supporting SOF_TIMESTAMPING_BIND_PHC for such sockets at a later time?

You're right. If we want to keep the door open for SOF_TIMESTAMPING_BIND_PHC on coalesced skbs, resolving the timestamp at coalescing time is the wrong approach.

Quoted 4 lines

It is just as easy to coalesce the cookie as the htwtstamp.

That also avoids the need to mask out SKBTX_HW_TSTAMP_NETDEV.

Indeed. We should also carry the matching @napi_id, since skb_get_hwtstamp() uses it for late resolution.

I'll update this in v2.

Quoted 5 lines
Quoted 3 lines

Additionally, recognize SKBTX_HW_TSTAMP_NETDEV as
indicating a receive timestamp is present.

What is the reason for this extra condition?

With late timestamp determination, skb_shared_hwtstamps may hold netdev_data instead of a resolved hwtstamp, and 0 can be a valid cookie value. So checking only skb_hwtstamps(skb)->hwtstamp is not enough to detect the presence of an RX hardware timestamp.

Quoted 8 lines
Quoted 8 lines

Note that skb_get_hwtstamp() is called with cycles == false, since TCP hasn't honored SOF_TIMESTAMPING_BIND_PHC so far, and this patch doesn't change that behavior.

Fixes 97dc7cd92ac6 ("ptp: Support late timestamp determination")

---

[...]

#9 lore raw

Re: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps

Quoted 21 lines · Kohei Enju
Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Kohei Enju

Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. TCP receive timestamping can then interpret the stored value as a ktime_t and report bogus hardware timestamps to userspace.

Quoted 7 lines
Quoted 4 lines

Use skb_get_hwtstamp() instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.

Why? Does this preclude supporting SOF_TIMESTAMPING_BIND_PHC for such sockets at a later time?

You're right. If we want to keep the door open for SOF_TIMESTAMPING_BIND_PHC on coalesced skbs, resolving the timestamp at coalescing time is the wrong approach.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

It is just as easy to coalesce the cookie as the htwtstamp.

That also avoids the need to mask out SKBTX_HW_TSTAMP_NETDEV.

Indeed. We should also carry the matching @napi_id, since skb_get_hwtstamp() uses it for late resolution.

I'll update this in v2.

Do you plan adding a selftest ?

I am confused by your series, a test would really help clarify the issue at hand.

author
#10 lore raw

Re: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps

Quoted 29 lines · Eric Dumazet
Quoted 28 lines · Kohei Enju
Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Kohei Enju

Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. TCP receive timestamping can then interpret the stored value as a ktime_t and report bogus hardware timestamps to userspace.

Quoted 7 lines
Quoted 4 lines

Use skb_get_hwtstamp() instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.

Why? Does this preclude supporting SOF_TIMESTAMPING_BIND_PHC for such sockets at a later time?

You're right. If we want to keep the door open for SOF_TIMESTAMPING_BIND_PHC on coalesced skbs, resolving the timestamp at coalescing time is the wrong approach.

Quoted 4 lines

It is just as easy to coalesce the cookie as the htwtstamp.

That also avoids the need to mask out SKBTX_HW_TSTAMP_NETDEV.

Indeed. We should also carry the matching @napi_id, since skb_get_hwtstamp() uses it for late resolution.

Quoted 6 lines
Quoted 1 line

I'll update this in v2.

Do you plan adding a selftest ?

I am confused by your series, a test would really help clarify the issue at hand.

Yes, I agree a selftest would help.

Since this issue is only triggered with drivers that use late RX timestamp determination (SKBTX_HW_TSTAMP_NETDEV / ndo_get_tstamp), it would likely need to live under tools/testing/selftests/drivers/net/hw/ rather than as a generic net selftest. I'm looking into whether I can add such a test.

Either way, I'll expand the cover letter to explain the issue in more detail. Sorry for the confusion.