All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: "'Anton Blanchard'" <anton@samba.org>,
	"behanw@converseincode.com" <behanw@converseincode.com>,
	"ying.huang@intel.com" <ying.huang@intel.com>,
	"a.p.zijlstra@chello.nl" <a.p.zijlstra@chello.nl>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	"oleg@redhat.com" <oleg@redhat.com>,
	Segher Boessenkool <segher@kernel.crashing.org>,
	"mingo@elte.hu" <mingo@elte.hu>
Cc: "linuxppc-dev@lists.ozlabs.org" <linuxppc-dev@lists.ozlabs.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: llist code relies on undefined behaviour, upsets llvm/clang
Date: Mon, 16 Jan 2017 14:34:43 +0000	[thread overview]
Message-ID: <063D6719AE5E284EB5DD2968C1650D6DB0264AA5@AcuExch.aculab.com> (raw)
In-Reply-To: <20170116083600.47175073@kryten>

From: Anton Blanchard
> Sent: 15 January 2017 21:36
> I was debugging a hang on a ppc64le kernel built with clang, and it
> looks to be undefined behaviour with pointer wrapping in the llist code.
> 
> A test case is below. llist_for_each_entry() does container_of() on a
> NULL pointer, which wraps our pointer negative, then adds the same
> offset back in and expects to get back to NULL. Unfortunately clang
> decides that this can never be NULL and optimises it into an infinite
> loop.
...
> #define llist_for_each_entry(pos, node, member)                         \
>         for ((pos) = llist_entry((node), typeof(*(pos)), member);       \
>              &(pos)->member != NULL;                                    \
>              (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))

Maybe the above could be rewritten as (untested):
		for ((pos) = NULL; (!(pos) ? (node) : ((pos)->member.next) || (pos) = 0) && \
			(((pos) = !(pos) ? llist_entry((node), typeof(*(pos)), member) \
					: llist_entry((pos)->member.next, typeof(*(pos)), member)),1); )
Provided the compiler assumes that the loop body is never executed with 'pos == 0'
it should generate the same code.

	David

WARNING: multiple messages have this Message-ID (diff)
From: David Laight <David.Laight@ACULAB.COM>
To: 'Anton Blanchard' <anton@samba.org>,
	"behanw@converseincode.com" <behanw@converseincode.com>,
	"ying.huang@intel.com" <ying.huang@intel.com>,
	"a.p.zijlstra@chello.nl" <a.p.zijlstra@chello.nl>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	"oleg@redhat.com" <oleg@redhat.com>,
	Segher Boessenkool <segher@kernel.crashing.org>,
	"mingo@elte.hu" <mingo@elte.hu>
Cc: "linuxppc-dev@lists.ozlabs.org" <linuxppc-dev@lists.ozlabs.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: llist code relies on undefined behaviour, upsets llvm/clang
Date: Mon, 16 Jan 2017 14:34:43 +0000	[thread overview]
Message-ID: <063D6719AE5E284EB5DD2968C1650D6DB0264AA5@AcuExch.aculab.com> (raw)
In-Reply-To: <20170116083600.47175073@kryten>

From: Anton Blanchard
> Sent: 15 January 2017 21:36
> I was debugging a hang on a ppc64le kernel built with clang, and it
> looks to be undefined behaviour with pointer wrapping in the llist code.
>=20
> A test case is below. llist_for_each_entry() does container_of() on a
> NULL pointer, which wraps our pointer negative, then adds the same
> offset back in and expects to get back to NULL. Unfortunately clang
> decides that this can never be NULL and optimises it into an infinite
> loop.
...
> #define llist_for_each_entry(pos, node, member)                         \
>         for ((pos) =3D llist_entry((node), typeof(*(pos)), member);      =
 \
>              &(pos)->member !=3D NULL;                                   =
 \
>              (pos) =3D llist_entry((pos)->member.next, typeof(*(pos)), me=
mber))

Maybe the above could be rewritten as (untested):
		for ((pos) =3D NULL; (!(pos) ? (node) : ((pos)->member.next) || (pos) =3D=
 0) && \
			(((pos) =3D !(pos) ? llist_entry((node), typeof(*(pos)), member) \
					: llist_entry((pos)->member.next, typeof(*(pos)), member)),1); )
Provided the compiler assumes that the loop body is never executed with 'po=
s =3D=3D 0'
it should generate the same code.

	David

  parent reply	other threads:[~2017-01-16 14:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-15 21:36 llist code relies on undefined behaviour, upsets llvm/clang Anton Blanchard
2017-01-16  9:05 ` Peter Zijlstra
2017-01-16 11:42   ` Anton Blanchard
2017-01-16 12:53     ` Peter Zijlstra
2017-01-16 13:09       ` Andrey Ryabinin
2017-01-16 14:34 ` David Laight [this message]
2017-01-16 14:34   ` David Laight
2017-01-16 16:25   ` Peter Zijlstra

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=063D6719AE5E284EB5DD2968C1650D6DB0264AA5@AcuExch.aculab.com \
    --to=david.laight@aculab.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=anton@samba.org \
    --cc=behanw@converseincode.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mingo@elte.hu \
    --cc=oleg@redhat.com \
    --cc=segher@kernel.crashing.org \
    --cc=ying.huang@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.