diff mbox series

[net] net: macb: fix PTP TX timestamp failure due to packet padding

Message ID 20230116214133.1834364-1-robert.hancock@calian.com (mailing list archive)
State Accepted
Commit 7b90f5a665acd46efbbfa677a3a3a18d01ad6487
Delegated to: Netdev Maintainers
Headers show
Series [net] net: macb: fix PTP TX timestamp failure due to packet padding | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 21 this patch: 21
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 21 this patch: 21
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 27 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Robert Hancock Jan. 16, 2023, 9:41 p.m. UTC
PTP TX timestamp handling was observed to be broken with this driver
when using the raw Layer 2 PTP encapsulation. ptp4l was not receiving
the expected TX timestamp after transmitting a packet, causing it to
enter a failure state.

The problem appears to be due to the way that the driver pads packets
which are smaller than the Ethernet minimum of 60 bytes. If headroom
space was available in the SKB, this caused the driver to move the data
back to utilize it. However, this appears to cause other data references
in the SKB to become inconsistent. In particular, this caused the
ptp_one_step_sync function to later (in the TX completion path) falsely
detect the packet as a one-step SYNC packet, even when it was not, which
caused the TX timestamp to not be processed when it should be.

Using the headroom for this purpose seems like an unnecessary complexity
as this is not a hot path in the driver, and in most cases it appears
that there is sufficient tailroom to not require using the headroom
anyway. Remove this usage of headroom to prevent this inconsistency from
occurring and causing other problems.

Fixes: 653e92a9175e ("net: macb: add support for padding and fcs computation")
Signed-off-by: Robert Hancock <robert.hancock@calian.com>
---
 drivers/net/ethernet/cadence/macb_main.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

Comments

Jacob Keller Jan. 18, 2023, 12:07 a.m. UTC | #1
On 1/16/2023 1:41 PM, Robert Hancock wrote:
> PTP TX timestamp handling was observed to be broken with this driver
> when using the raw Layer 2 PTP encapsulation. ptp4l was not receiving
> the expected TX timestamp after transmitting a packet, causing it to
> enter a failure state.
> 
> The problem appears to be due to the way that the driver pads packets
> which are smaller than the Ethernet minimum of 60 bytes. If headroom
> space was available in the SKB, this caused the driver to move the data
> back to utilize it. However, this appears to cause other data references
> in the SKB to become inconsistent. In particular, this caused the
> ptp_one_step_sync function to later (in the TX completion path) falsely
> detect the packet as a one-step SYNC packet, even when it was not, which
> caused the TX timestamp to not be processed when it should be.
> 
> Using the headroom for this purpose seems like an unnecessary complexity
> as this is not a hot path in the driver, and in most cases it appears
> that there is sufficient tailroom to not require using the headroom
> anyway. Remove this usage of headroom to prevent this inconsistency from
> occurring and causing other problems.
> 
> Fixes: 653e92a9175e ("net: macb: add support for padding and fcs computation")
> Signed-off-by: Robert Hancock <robert.hancock@calian.com>
> ---

Makes sense and is a nice cleanup.

Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>

>  drivers/net/ethernet/cadence/macb_main.c | 9 +--------
>  1 file changed, 1 insertion(+), 8 deletions(-)
> 
> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
> index 95667b979fab..72e42820713d 100644
> --- a/drivers/net/ethernet/cadence/macb_main.c
> +++ b/drivers/net/ethernet/cadence/macb_main.c
> @@ -2187,7 +2187,6 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>  	bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
>  		      skb_is_nonlinear(*skb);
>  	int padlen = ETH_ZLEN - (*skb)->len;
> -	int headroom = skb_headroom(*skb);
>  	int tailroom = skb_tailroom(*skb);
>  	struct sk_buff *nskb;
>  	u32 fcs;
> @@ -2201,9 +2200,6 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>  		/* FCS could be appeded to tailroom. */
>  		if (tailroom >= ETH_FCS_LEN)
>  			goto add_fcs;
> -		/* FCS could be appeded by moving data to headroom. */
> -		else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
> -			padlen = 0;
>  		/* No room for FCS, need to reallocate skb. */
>  		else
>  			padlen = ETH_FCS_LEN;
> @@ -2212,10 +2208,7 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>  		padlen += ETH_FCS_LEN;
>  	}
>  
> -	if (!cloned && headroom + tailroom >= padlen) {
> -		(*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
> -		skb_set_tail_pointer(*skb, (*skb)->len);
> -	} else {
> +	if (cloned || tailroom < padlen) {
>  		nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
>  		if (!nskb)
>  			return -ENOMEM;
Claudiu Beznea Jan. 18, 2023, 9:39 a.m. UTC | #2
On 16.01.2023 23:41, Robert Hancock wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> PTP TX timestamp handling was observed to be broken with this driver
> when using the raw Layer 2 PTP encapsulation. ptp4l was not receiving
> the expected TX timestamp after transmitting a packet, causing it to
> enter a failure state.
> 
> The problem appears to be due to the way that the driver pads packets
> which are smaller than the Ethernet minimum of 60 bytes. If headroom
> space was available in the SKB, this caused the driver to move the data
> back to utilize it. However, this appears to cause other data references
> in the SKB to become inconsistent. In particular, this caused the
> ptp_one_step_sync function to later (in the TX completion path) falsely
> detect the packet as a one-step SYNC packet, even when it was not, which
> caused the TX timestamp to not be processed when it should be.
> 
> Using the headroom for this purpose seems like an unnecessary complexity
> as this is not a hot path in the driver, and in most cases it appears
> that there is sufficient tailroom to not require using the headroom
> anyway. Remove this usage of headroom to prevent this inconsistency from
> occurring and causing other problems.
> 
> Fixes: 653e92a9175e ("net: macb: add support for padding and fcs computation")
> Signed-off-by: Robert Hancock <robert.hancock@calian.com>

Tested-by: Claudiu Beznea <claudiu.beznea@microchip.com> # on SAMA7G5

> ---
>  drivers/net/ethernet/cadence/macb_main.c | 9 +--------
>  1 file changed, 1 insertion(+), 8 deletions(-)
> 
> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
> index 95667b979fab..72e42820713d 100644
> --- a/drivers/net/ethernet/cadence/macb_main.c
> +++ b/drivers/net/ethernet/cadence/macb_main.c
> @@ -2187,7 +2187,6 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>         bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
>                       skb_is_nonlinear(*skb);
>         int padlen = ETH_ZLEN - (*skb)->len;
> -       int headroom = skb_headroom(*skb);
>         int tailroom = skb_tailroom(*skb);
>         struct sk_buff *nskb;
>         u32 fcs;
> @@ -2201,9 +2200,6 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>                 /* FCS could be appeded to tailroom. */
>                 if (tailroom >= ETH_FCS_LEN)
>                         goto add_fcs;
> -               /* FCS could be appeded by moving data to headroom. */
> -               else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
> -                       padlen = 0;
>                 /* No room for FCS, need to reallocate skb. */
>                 else
>                         padlen = ETH_FCS_LEN;
> @@ -2212,10 +2208,7 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>                 padlen += ETH_FCS_LEN;
>         }
> 
> -       if (!cloned && headroom + tailroom >= padlen) {
> -               (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
> -               skb_set_tail_pointer(*skb, (*skb)->len);
> -       } else {
> +       if (cloned || tailroom < padlen) {
>                 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
>                 if (!nskb)
>                         return -ENOMEM;
> --
> 2.39.0
>
Claudiu Beznea Jan. 18, 2023, 9:39 a.m. UTC | #3
On 16.01.2023 23:41, Robert Hancock wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> PTP TX timestamp handling was observed to be broken with this driver
> when using the raw Layer 2 PTP encapsulation. ptp4l was not receiving
> the expected TX timestamp after transmitting a packet, causing it to
> enter a failure state.
> 
> The problem appears to be due to the way that the driver pads packets
> which are smaller than the Ethernet minimum of 60 bytes. If headroom
> space was available in the SKB, this caused the driver to move the data
> back to utilize it. However, this appears to cause other data references
> in the SKB to become inconsistent. In particular, this caused the
> ptp_one_step_sync function to later (in the TX completion path) falsely
> detect the packet as a one-step SYNC packet, even when it was not, which
> caused the TX timestamp to not be processed when it should be.
> 
> Using the headroom for this purpose seems like an unnecessary complexity
> as this is not a hot path in the driver, and in most cases it appears
> that there is sufficient tailroom to not require using the headroom
> anyway. Remove this usage of headroom to prevent this inconsistency from
> occurring and causing other problems.
> 
> Fixes: 653e92a9175e ("net: macb: add support for padding and fcs computation")
> Signed-off-by: Robert Hancock <robert.hancock@calian.com>

Reviewed-by: Claudiu Beznea <claudiu.beznea@microchip.com>


> ---
>  drivers/net/ethernet/cadence/macb_main.c | 9 +--------
>  1 file changed, 1 insertion(+), 8 deletions(-)
> 
> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
> index 95667b979fab..72e42820713d 100644
> --- a/drivers/net/ethernet/cadence/macb_main.c
> +++ b/drivers/net/ethernet/cadence/macb_main.c
> @@ -2187,7 +2187,6 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>         bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
>                       skb_is_nonlinear(*skb);
>         int padlen = ETH_ZLEN - (*skb)->len;
> -       int headroom = skb_headroom(*skb);
>         int tailroom = skb_tailroom(*skb);
>         struct sk_buff *nskb;
>         u32 fcs;
> @@ -2201,9 +2200,6 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>                 /* FCS could be appeded to tailroom. */
>                 if (tailroom >= ETH_FCS_LEN)
>                         goto add_fcs;
> -               /* FCS could be appeded by moving data to headroom. */
> -               else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
> -                       padlen = 0;
>                 /* No room for FCS, need to reallocate skb. */
>                 else
>                         padlen = ETH_FCS_LEN;
> @@ -2212,10 +2208,7 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
>                 padlen += ETH_FCS_LEN;
>         }
> 
> -       if (!cloned && headroom + tailroom >= padlen) {
> -               (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
> -               skb_set_tail_pointer(*skb, (*skb)->len);
> -       } else {
> +       if (cloned || tailroom < padlen) {
>                 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
>                 if (!nskb)
>                         return -ENOMEM;
> --
> 2.39.0
>
patchwork-bot+netdevbpf@kernel.org Jan. 18, 2023, 2:30 p.m. UTC | #4
Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Mon, 16 Jan 2023 15:41:33 -0600 you wrote:
> PTP TX timestamp handling was observed to be broken with this driver
> when using the raw Layer 2 PTP encapsulation. ptp4l was not receiving
> the expected TX timestamp after transmitting a packet, causing it to
> enter a failure state.
> 
> The problem appears to be due to the way that the driver pads packets
> which are smaller than the Ethernet minimum of 60 bytes. If headroom
> space was available in the SKB, this caused the driver to move the data
> back to utilize it. However, this appears to cause other data references
> in the SKB to become inconsistent. In particular, this caused the
> ptp_one_step_sync function to later (in the TX completion path) falsely
> detect the packet as a one-step SYNC packet, even when it was not, which
> caused the TX timestamp to not be processed when it should be.
> 
> [...]

Here is the summary with links:
  - [net] net: macb: fix PTP TX timestamp failure due to packet padding
    https://git.kernel.org/netdev/net/c/7b90f5a665ac

You are awesome, thank you!
diff mbox series

Patch

diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index 95667b979fab..72e42820713d 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -2187,7 +2187,6 @@  static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
 	bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
 		      skb_is_nonlinear(*skb);
 	int padlen = ETH_ZLEN - (*skb)->len;
-	int headroom = skb_headroom(*skb);
 	int tailroom = skb_tailroom(*skb);
 	struct sk_buff *nskb;
 	u32 fcs;
@@ -2201,9 +2200,6 @@  static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
 		/* FCS could be appeded to tailroom. */
 		if (tailroom >= ETH_FCS_LEN)
 			goto add_fcs;
-		/* FCS could be appeded by moving data to headroom. */
-		else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
-			padlen = 0;
 		/* No room for FCS, need to reallocate skb. */
 		else
 			padlen = ETH_FCS_LEN;
@@ -2212,10 +2208,7 @@  static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
 		padlen += ETH_FCS_LEN;
 	}
 
-	if (!cloned && headroom + tailroom >= padlen) {
-		(*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
-		skb_set_tail_pointer(*skb, (*skb)->len);
-	} else {
+	if (cloned || tailroom < padlen) {
 		nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
 		if (!nskb)
 			return -ENOMEM;