netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: Eric Dumazet <edumazet@google.com>,
	"David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org, Willem de Bruijn <willemb@google.com>,
	eric.dumazet@gmail.com, Eric Dumazet <edumazet@google.com>
Subject: RE: [PATCH net-next 1/9] net/packet: annotate accesses to po->xmit
Date: Thu, 16 Mar 2023 11:20:48 -0400	[thread overview]
Message-ID: <641333d07c6b0_3333c72088e@willemb.c.googlers.com.notmuch> (raw)
In-Reply-To: <20230316011014.992179-2-edumazet@google.com>

Eric Dumazet wrote:
> po->xmit can be set from setsockopt(PACKET_QDISC_BYPASS),
> while read locklessly.
> 
> Use READ_ONCE()/WRITE_ONCE() to avoid potential load/store
> tearing issues.
> 
> Fixes: d346a3fae3ff ("packet: introduce PACKET_QDISC_BYPASS socket option")
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> ---
>  net/packet/af_packet.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
> index d4e76e2ae153ea3b3c7d73ef6ac2f8c2742f790d..d25dd9f63cc4f11ad8197ab66d60180b6358132f 100644
> --- a/net/packet/af_packet.c
> +++ b/net/packet/af_packet.c
> @@ -307,7 +307,8 @@ static void packet_cached_dev_reset(struct packet_sock *po)
>  
>  static bool packet_use_direct_xmit(const struct packet_sock *po)
>  {
> -	return po->xmit == packet_direct_xmit;
> +	/* Paired with WRITE_ONCE() in packet_setsockopt() */
> +	return READ_ONCE(po->xmit) == packet_direct_xmit;
>  }
>  
>  static u16 packet_pick_tx_queue(struct sk_buff *skb)
> @@ -2867,7 +2868,8 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
>  		packet_inc_pending(&po->tx_ring);
>  
>  		status = TP_STATUS_SEND_REQUEST;
> -		err = po->xmit(skb);
> +		/* Paired with WRITE_ONCE() in packet_setsockopt() */
> +		err = READ_ONCE(po->xmit)(skb);
>  		if (unlikely(err != 0)) {
>  			if (err > 0)
>  				err = net_xmit_errno(err);
> @@ -3070,7 +3072,8 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
>  		virtio_net_hdr_set_proto(skb, &vnet_hdr);
>  	}
>  
> -	err = po->xmit(skb);
> +	/* Paired with WRITE_ONCE() in packet_setsockopt() */
> +	err = READ_ONCE(po->xmit)(skb);
>  	if (unlikely(err != 0)) {
>  		if (err > 0)
>  			err = net_xmit_errno(err);
> @@ -4007,7 +4010,8 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
>  		if (copy_from_sockptr(&val, optval, sizeof(val)))
>  			return -EFAULT;
>  
> -		po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
> +		/* Paired with all lockless reads of po->xmit */
> +		WRITE_ONCE(po->xmit, val ? packet_direct_xmit : dev_queue_xmit);

We could avoid the indirect function call with a branch on
packet_use_direct_xmit() and just store a bit?

>  		return 0;
>  	}
>  	default:
> -- 
> 2.40.0.rc2.332.ga46443480c-goog
> 



  reply	other threads:[~2023-03-16 15:20 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-16  1:10 [PATCH net-next 0/9] net/packet: KCSAN awareness Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 1/9] net/packet: annotate accesses to po->xmit Eric Dumazet
2023-03-16 15:20   ` Willem de Bruijn [this message]
2023-03-16 15:34     ` Eric Dumazet
2023-03-16 17:02       ` Willem de Bruijn
2023-03-16  1:10 ` [PATCH net-next 2/9] net/packet: convert po->origdev to an atomic flag Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 3/9] net/packet: convert po->auxdata " Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 4/9] net/packet: annotate accesses to po->tp_tstamp Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 5/9] net/packet: convert po->tp_tx_has_off to an atomic flag Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 6/9] net/packet: convert po->tp_loss " Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 7/9] net/packet: convert po->has_vnet_hdr " Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 8/9] net/packet: convert po->running " Eric Dumazet
2023-03-16  1:10 ` [PATCH net-next 9/9] net/packet: convert po->pressure " Eric Dumazet
2023-03-16 15:15 ` [PATCH net-next 0/9] net/packet: KCSAN awareness Willem de Bruijn
2023-03-17  9:00 ` 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=641333d07c6b0_3333c72088e@willemb.c.googlers.com.notmuch \
    --to=willemdebruijn.kernel@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eric.dumazet@gmail.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.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;
as well as URLs for NNTP newsgroup(s).