netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <edumazet@google.com>
Cc: <davem@davemloft.net>, <kuba@kernel.org>, <kuni1840@gmail.com>,
	<kuniyu@amazon.com>, <netdev@vger.kernel.org>,
	<pabeni@redhat.com>
Subject: Re: [PATCH v3 net 05/17] ratelimit: Fix data-races in ___ratelimit().
Date: Mon, 22 Aug 2022 12:14:45 -0700	[thread overview]
Message-ID: <20220822191445.21807-1-kuniyu@amazon.com> (raw)
In-Reply-To: <CANn89i+amoiCtNuGO6e1dx=9vfdfQSe09MZ7iRKQ+sdo6K=uzA@mail.gmail.com>

From:   Eric Dumazet <edumazet@google.com>
Date:   Mon, 22 Aug 2022 12:00:11 -0700
> On Thu, Aug 18, 2022 at 11:29 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> >
> > While reading rs->interval and rs->burst, they can be changed
> > concurrently.  Thus, we need to add READ_ONCE() to their readers.
> >
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> > ---
> >  lib/ratelimit.c | 8 +++++---
> >  1 file changed, 5 insertions(+), 3 deletions(-)
> >
> > diff --git a/lib/ratelimit.c b/lib/ratelimit.c
> > index e01a93f46f83..b59a1d3d0cc3 100644
> > --- a/lib/ratelimit.c
> > +++ b/lib/ratelimit.c
> > @@ -26,10 +26,12 @@
> >   */
> >  int ___ratelimit(struct ratelimit_state *rs, const char *func)
> >  {
> > +       int interval = READ_ONCE(rs->interval);
> > +       int burst = READ_ONCE(rs->burst);
> 
> I thought rs->interval and rs->burst were constants...
> 
> Can you point to the part where they are changed ?
> 
> Ideally such a patch should also add corresponding WRITE_ONCE(), and
> comments to pair them,
>  this would really help reviewing it.

In this case, &net_ratelimit_state.(burst|interval) are directly
passed to proc_handlers, and exactly the relation is unclear.

As Jakub pointed out, two reads can be inconsistent, so I'll add
a spin lock in struct ratelimit_state and two dedicated proc
handlers for each member.  Then, I'll add few more comments to
make that relation clear.

Thanks for feedback!


> >         unsigned long flags;
> >         int ret;
> >
> > -       if (!rs->interval)
> > +       if (!interval)
> >                 return 1;
> >
> >         /*
> > @@ -44,7 +46,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
> >         if (!rs->begin)
> >                 rs->begin = jiffies;
> >
> > -       if (time_is_before_jiffies(rs->begin + rs->interval)) {
> > +       if (time_is_before_jiffies(rs->begin + interval)) {
> >                 if (rs->missed) {
> >                         if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
> >                                 printk_deferred(KERN_WARNING
> > @@ -56,7 +58,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
> >                 rs->begin   = jiffies;
> >                 rs->printed = 0;
> >         }
> > -       if (rs->burst && rs->burst > rs->printed) {
> > +       if (burst && burst > rs->printed) {
> >                 rs->printed++;
> >                 ret = 1;
> >         } else {
> > --
> > 2.30.2
> >

  reply	other threads:[~2022-08-22 19:16 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-18 18:26 [PATCH v3 net 00/17] net: sysctl: Fix data-races around net.core.XXX Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 01/17] net: Fix data-races around sysctl_[rw]mem_(max|default) Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 02/17] net: Fix data-races around weight_p and dev_weight_[rt]x_bias Kuniyuki Iwashima
2022-08-20  0:03   ` Jakub Kicinski
2022-08-22 18:30     ` Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 03/17] net: Fix data-races around netdev_max_backlog Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 04/17] net: Fix data-races around netdev_tstamp_prequeue Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 05/17] ratelimit: Fix data-races in ___ratelimit() Kuniyuki Iwashima
2022-08-20  0:04   ` Jakub Kicinski
2022-08-22 18:32     ` Kuniyuki Iwashima
2022-08-22 19:00   ` Eric Dumazet
2022-08-22 19:14     ` Kuniyuki Iwashima [this message]
2022-08-22 19:22       ` Eric Dumazet
2022-08-22 19:49         ` Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 06/17] net: Fix data-races around sysctl_optmem_max Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 07/17] net: Fix a data-race around sysctl_tstamp_allow_data Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 08/17] net: Fix a data-race around sysctl_net_busy_poll Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 09/17] net: Fix a data-race around sysctl_net_busy_read Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 10/17] net: Fix a data-race around netdev_budget Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 11/17] net: Fix data-races around sysctl_max_skb_frags Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 12/17] net: Fix a data-race around netdev_budget_usecs Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 13/17] net: Fix data-races around sysctl_fb_tunnels_only_for_init_net Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 14/17] net: Fix data-races around sysctl_devconf_inherit_init_net Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 15/17] net: Fix a data-race around gro_normal_batch Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 16/17] net: Fix a data-race around netdev_unregister_timeout_secs Kuniyuki Iwashima
2022-08-18 18:26 ` [PATCH v3 net 17/17] net: Fix a data-race around sysctl_somaxconn Kuniyuki Iwashima
2022-08-18 19:00 ` [PATCH v3 net 00/17] net: sysctl: Fix data-races around net.core.XXX Jakub Kicinski
2022-08-18 19:09   ` Kuniyuki Iwashima

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=20220822191445.21807-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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).