From: Paolo Abeni <pabeni@redhat.com>
To: David Laight <David.Laight@ACULAB.COM>,
'Jakub Kicinski' <kuba@kernel.org>,
"davem@davemloft.net" <davem@davemloft.net>
Cc: "netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"edumazet@google.com" <edumazet@google.com>,
Jesse Brandeburg <jesse.brandeburg@intel.com>,
"michael.chan@broadcom.com" <michael.chan@broadcom.com>
Subject: Re: [PATCH net-next v2 2/3] bnxt: use READ_ONCE/WRITE_ONCE for ring indexes
Date: Thu, 13 Apr 2023 13:38:08 +0200 [thread overview]
Message-ID: <78cea5774de414fa3bcbd6ef02e436ae6b5706c1.camel@redhat.com> (raw)
In-Reply-To: <f6c134852244441a88eef8c1774bb67f@AcuMS.aculab.com>
On Wed, 2023-04-12 at 08:15 +0000, David Laight wrote:
> From: Jakub Kicinski
> > Sent: 12 April 2023 02:51
> >
> > Eric points out that we should make sure that ring index updates
> > are wrapped in the appropriate READ_ONCE/WRITE_ONCE macros.
> >
> ...
> > -static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
> > +static inline u32 bnxt_tx_avail(struct bnxt *bp,
> > + const struct bnxt_tx_ring_info *txr)
> > {
> > - /* Tell compiler to fetch tx indices from memory. */
> > - barrier();
> > + u32 used = READ_ONCE(txr->tx_prod) - READ_ONCE(txr->tx_cons);
> >
> > - return bp->tx_ring_size -
> > - ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
> > + return bp->tx_ring_size - (used & bp->tx_ring_mask);
> > }
>
> Doesn't that function only make sense if only one of
> the ring index can be changing?
> In this case I think this is being used in the transmit path
> so that 'tx_prod' is constant and is either already read
> or need not be read again.
>
> Having written a lot of 'ring access' functions over the years
> if the ring size is a power of 2 I'd mask the 'tx_prod' value
> when it is being used rather than on the increment.
> (So the value just wraps modulo 2**32.)
> This tends to make the code safer - especially since the
> 'ring full' and 'ring empty' conditions are different.
>
> Also that code is masking with bp->tx_ring_mask, but the
> increments (in hunks I've chopped) use NEXT_TX(prod).
> If that is masking with bp->tx_ring_mask then 'bp' should
> be a parameter.
AFAICS bnxt_tx_avail() is also used in TX interrupt, outside tx path/tx
lock.
I think all the above consideration are more suited for a driver
refactor, while the current patch specifically address potential data
race issues.
Cheers,
Paolo
next prev parent reply other threads:[~2023-04-13 11:38 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-12 1:50 [PATCH net-next v2 0/3] net: use READ_ONCE/WRITE_ONCE for ring index accesses Jakub Kicinski
2023-04-12 1:50 ` [PATCH net-next v2 1/3] net: docs: update the sample code in driver.rst Jakub Kicinski
2023-04-12 1:50 ` [PATCH net-next v2 2/3] bnxt: use READ_ONCE/WRITE_ONCE for ring indexes Jakub Kicinski
2023-04-12 1:59 ` Michael Chan
2023-04-12 8:15 ` David Laight
2023-04-13 11:38 ` Paolo Abeni [this message]
2023-04-13 14:20 ` David Laight
2023-04-12 1:50 ` [PATCH net-next v2 3/3] mlx4: " Jakub Kicinski
2023-04-12 6:07 ` Tariq Toukan
2023-04-13 11:50 ` [PATCH net-next v2 0/3] net: use READ_ONCE/WRITE_ONCE for ring index accesses 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=78cea5774de414fa3bcbd6ef02e436ae6b5706c1.camel@redhat.com \
--to=pabeni@redhat.com \
--cc=David.Laight@ACULAB.COM \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=jesse.brandeburg@intel.com \
--cc=kuba@kernel.org \
--cc=michael.chan@broadcom.com \
--cc=netdev@vger.kernel.org \
/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).