Netdev List
 help / color / mirror / Atom feed
From: Vadim Fedorenko <vadim.fedorenko@linux.dev>
To: Jiawen Wu <jiawenwu@trustnetic.com>, netdev@vger.kernel.org
Cc: Mengyuan Lou <mengyuanlou@net-swift.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Richard Cochran <richardcochran@gmail.com>
Subject: Re: [PATCH net v2] net: libwx: fix concurrent bitmap overwrite in PTP setup
Date: Tue, 18 Aug 2026 12:25:24 +0100	[thread overview]
Message-ID: <668c3127-1ab3-4dcf-a856-9f396cd57e58@linux.dev> (raw)
In-Reply-To: <6C7EC12D69217315+20260818074721.45536-1-jiawenwu@trustnetic.com>

On 18/08/2026 08:47, Jiawen Wu wrote:
> In wx_ptp_set_timestamp_mode(), the driver copies the global `wx->flags`
> bitmap to a local variable, modifies the PTP-related bits, and then writes
> the entire bitmap back using memcpy().
> 
> This Read-Copy-Update pattern is unsafe and introduces a critical race
> condition. Other asynchronous contexts (such as Tx timeout routines or
> GPIO IRQ handlers) update individual bits in `wx->flags` concurrently
> using atomic bitops like set_bit() or clear_bit(). The memcpy() write-back
> can silently overwrite and drop these concurrent changes, potentially
> causing the driver to miss critical module reset or PCIe recovery requests.
> 
> Fix this by removing the local bitmap copy. Instead, evaluate the intended
> PTP flag states locally and apply them directly to `wx->flags` using
> atomic set_bit() and clear_bit() operations only after the hardware is
> successfully configured.
> 
> Fixes: 06e75161b9d4 ("net: wangxun: Add support for PTP clock")
> Signed-off-by: Jiawen Wu <jiawenwu@trustnetic.com>
> ---
> Changes in v2:
> - Combine two variables that are always equal into one.
> - Link to v1: https://lore.kernel.org/all/F97867A3AA2696FD+20260817021708.37975-1-jiawenwu@trustnetic.com
> ---
>   drivers/net/ethernet/wangxun/libwx/wx_ptp.c | 18 ++++++------------
>   1 file changed, 6 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/net/ethernet/wangxun/libwx/wx_ptp.c b/drivers/net/ethernet/wangxun/libwx/wx_ptp.c
> index 44f3e6505246..1165518d5522 100644
> --- a/drivers/net/ethernet/wangxun/libwx/wx_ptp.c
> +++ b/drivers/net/ethernet/wangxun/libwx/wx_ptp.c
> @@ -555,13 +555,11 @@ static int wx_ptp_set_timestamp_mode(struct wx *wx,
>   {
>   	u32 tsync_tx_ctl = WX_TSC_1588_CTL_ENABLED;
>   	u32 tsync_rx_ctl = WX_PSR_1588_CTL_ENABLED;
> -	DECLARE_BITMAP(flags, WX_PF_FLAGS_NBITS);
>   	u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
> +	bool rx_tstamp = false;
>   	bool is_l2 = false;
>   	u32 regval;
>   
> -	memcpy(flags, wx->flags, sizeof(wx->flags));
> -
>   	switch (config->tx_type) {
>   	case HWTSTAMP_TX_OFF:
>   		tsync_tx_ctl = 0;
> @@ -576,20 +574,16 @@ static int wx_ptp_set_timestamp_mode(struct wx *wx,
>   	case HWTSTAMP_FILTER_NONE:
>   		tsync_rx_ctl = 0;
>   		tsync_rx_mtrl = 0;
> -		clear_bit(WX_FLAG_RX_HWTSTAMP_ENABLED, flags);
> -		clear_bit(WX_FLAG_RX_HWTSTAMP_IN_REGISTER, flags);
>   		break;
>   	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
>   		tsync_rx_ctl |= WX_PSR_1588_CTL_TYPE_L4_V1;
>   		tsync_rx_mtrl |= WX_PSR_1588_MSG_V1_SYNC;
> -		set_bit(WX_FLAG_RX_HWTSTAMP_ENABLED, flags);
> -		set_bit(WX_FLAG_RX_HWTSTAMP_IN_REGISTER, flags);
> +		rx_tstamp = true;
>   		break;
>   	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
>   		tsync_rx_ctl |= WX_PSR_1588_CTL_TYPE_L4_V1;
>   		tsync_rx_mtrl |= WX_PSR_1588_MSG_V1_DELAY_REQ;
> -		set_bit(WX_FLAG_RX_HWTSTAMP_ENABLED, flags);
> -		set_bit(WX_FLAG_RX_HWTSTAMP_IN_REGISTER, flags);
> +		rx_tstamp = true;
>   		break;
>   	case HWTSTAMP_FILTER_PTP_V2_EVENT:
>   	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
> @@ -602,9 +596,8 @@ static int wx_ptp_set_timestamp_mode(struct wx *wx,
>   	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
>   		tsync_rx_ctl |= WX_PSR_1588_CTL_TYPE_EVENT_V2;
>   		is_l2 = true;
> +		rx_tstamp = true;
>   		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
> -		set_bit(WX_FLAG_RX_HWTSTAMP_ENABLED, flags);
> -		set_bit(WX_FLAG_RX_HWTSTAMP_IN_REGISTER, flags);
>   		break;
>   	default:
>   		/* register PSR_1588_MSG must be set in order to do V1 packets,
> @@ -643,7 +636,8 @@ static int wx_ptp_set_timestamp_mode(struct wx *wx,
>   	WX_WRITE_FLUSH(wx);
>   
>   	/* configure adapter flags only when HW is actually configured */
> -	memcpy(wx->flags, flags, sizeof(wx->flags));
> +	assign_bit(WX_FLAG_RX_HWTSTAMP_ENABLED, wx->flags, rx_tstamp);
> +	assign_bit(WX_FLAG_RX_HWTSTAMP_IN_REGISTER, wx->flags, rx_tstamp);
>   
>   	/* clear TX/RX timestamp state, just to be sure */
>   	wx_ptp_clear_tx_timestamp(wx);

Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>

  reply	other threads:[~2026-08-18 11:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-18  7:47 [PATCH net v2] net: libwx: fix concurrent bitmap overwrite in PTP setup Jiawen Wu
2026-08-18 11:25 ` Vadim Fedorenko [this message]
2026-08-22 20:25 ` Jakub Kicinski
2026-08-22 20:30 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=668c3127-1ab3-4dcf-a856-9f396cd57e58@linux.dev \
    --to=vadim.fedorenko@linux.dev \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jiawenwu@trustnetic.com \
    --cc=kuba@kernel.org \
    --cc=mengyuanlou@net-swift.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=richardcochran@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox