netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: Nicolai Stange <nstange@suse.de>
Cc: "David S. Miller" <davem@davemloft.net>,
	Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>,
	Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>,
	Mohamed Ghannam <simo.ghannam@gmail.com>,
	Michal Kubecek <mkubecek@suse.cz>,
	Miroslav Benes <mbenes@suse.cz>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] net: ipv4: emulate READ_ONCE() on ->hdrincl bit-field in raw_sendmsg()
Date: Wed, 3 Jan 2018 11:37:00 +0100	[thread overview]
Message-ID: <20180103113700.049f7558@elisabeth> (raw)
In-Reply-To: <87vagjjnpn.fsf@suse.de>

Hi Nicolai,

On Wed, 03 Jan 2018 10:28:20 +0100
Nicolai Stange <nstange@suse.de> wrote:

> Hi Stefano,
> 
> Stefano Brivio <sbrivio@redhat.com> writes:
> 
> > On Tue,  2 Jan 2018 17:30:20 +0100
> > Nicolai Stange <nstange@suse.de> wrote:
> >  
> >> [...]
> >>
> >> diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
> >> index 5b9bd5c33d9d..e84290c28c0c 100644
> >> --- a/net/ipv4/raw.c
> >> +++ b/net/ipv4/raw.c
> >> @@ -513,16 +513,18 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
> >>  	int err;
> >>  	struct ip_options_data opt_copy;
> >>  	struct raw_frag_vec rfv;
> >> -	int hdrincl;
> >> +	int hdrincl, __hdrincl;
> >>  
> >>  	err = -EMSGSIZE;
> >>  	if (len > 0xFFFF)
> >>  		goto out;
> >>  
> >>  	/* hdrincl should be READ_ONCE(inet->hdrincl)
> >> -	 * but READ_ONCE() doesn't work with bit fields
> >> +	 * but READ_ONCE() doesn't work with bit fields.
> >> +	 * Emulate it by doing the READ_ONCE() from an intermediate int.
> >>  	 */
> >> -	hdrincl = inet->hdrincl;
> >> +	__hdrincl = inet->hdrincl;
> >> +	hdrincl = READ_ONCE(__hdrincl);  
> >
> > I guess you don't actually need to use a third variable. What about
> > doing READ_ONCE() on hdrincl itself after the first assignment?
> >
> > Perhaps something like the patch below -- applies to net.git, yields
> > same binary output as your version with gcc 6, looks IMHO more
> > straightforward:
> >
> > diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
> > index 125c1eab3eaa..8c2f783a95fc 100644
> > --- a/net/ipv4/raw.c
> > +++ b/net/ipv4/raw.c
> > @@ -519,10 +519,12 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
> >  	if (len > 0xFFFF)
> >  		goto out;
> >  
> > -	/* hdrincl should be READ_ONCE(inet->hdrincl)
> > -	 * but READ_ONCE() doesn't work with bit fields
> > +	/* hdrincl should be READ_ONCE(inet->hdrincl) but READ_ONCE() doesn't
> > +	 * work with bit fields. Emulate it by adding a further sequence point.
> >  	 */
> >  	hdrincl = inet->hdrincl;
> > +	hdrincl = READ_ONCE(hdrincl);
> > +  
> 
> Yes, this does also work. In fact, after having been lowered into SSA
> form, it should be equivalent to what I posted.
> 
> So, it's a matter of preference/style and I'd leave the decision on
> this to the maintainers -- for me, either way is fine.
> 
> I don't like the "sequence point" wording in the comment above though:
> AFAICS, if taken in the meaning of C99, it's not any sequence point but
> the volatile access in READ_ONCE() which ensures that there won't be any
> reloads from ->hdrincl. If you don't mind, I'll adjust that comment if
> asked to resend with your solution.

Well, by "by adding a further sequence point" I refer to what we have
to do to emulate READ_ONCE(), not to the reason why we need READ_ONCE().

However, this is a likely sign that my comment isn't that clear either.
So unless you have better ideas, I would go with:

+	/* hdrincl should be READ_ONCE(inet->hdrincl) but READ_ONCE() doesn't
+	 * work with bit fields. Doing this indirectly yields the same result.

but I really hope you have a better idea. :)

-- 
Stefano

  reply	other threads:[~2018-01-03 10:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-02 16:30 [PATCH] net: ipv4: emulate READ_ONCE() on ->hdrincl bit-field in raw_sendmsg() Nicolai Stange
2018-01-02 21:12 ` Stefano Brivio
2018-01-03  9:28   ` Nicolai Stange
2018-01-03 10:37     ` Stefano Brivio [this message]
2018-01-08 14:54       ` [PATCH v2] " Nicolai Stange
2018-01-08 15:11         ` Stefano Brivio
2018-01-09 16:59         ` David Miller

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=20180103113700.049f7558@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=davem@davemloft.net \
    --cc=kuznet@ms2.inr.ac.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=mkubecek@suse.cz \
    --cc=netdev@vger.kernel.org \
    --cc=nstange@suse.de \
    --cc=simo.ghannam@gmail.com \
    --cc=yoshfuji@linux-ipv6.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).