All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Campbell <Ian.Campbell@citrix.com>
To: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Cc: Keir Fraser <keir@xen.org>,
	Ian Jackson <Ian.Jackson@eu.citrix.com>,
	"Tim (Xen.org)" <tim@xen.org>,
	"xen-devel@lists.xen.org" <xen-devel@lists.xen.org>,
	Julien Grall <julien.grall@citrix.com>,
	Jan Beulich <JBeulich@suse.com>
Subject: Re: [PATCH 4/4] xen/arm: correctly handle an empty array of platform descs.
Date: Tue, 14 May 2013 10:32:55 +0100	[thread overview]
Message-ID: <1368523975.14264.22.camel@zakaz.uk.xensource.com> (raw)
In-Reply-To: <alpine.DEB.2.02.1305131748510.4799@kaball.uk.xensource.com>

On Mon, 2013-05-13 at 17:48 +0100, Stefano Stabellini wrote:
> On Mon, 13 May 2013, Ian Campbell wrote:
> > This bit me midway through a bisect and the platform array shouldn't be
> > empty anymore but lets be safe.
> > 
> > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> 
> Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>

Sadly this turns out to be a bit more complicated :-( CCing Jan and Keir
since this was wider implications (this construct is used elsewhere) and
Ian J because he pointed this out.

TL;DR: There is probably a problem here, but I'm not sure it is one
worth worrying about, at least not yet/for 4.3.

The problem is that the compiler is allowed to assume that:
        extern const struct platform_desc _splatform[], _eplatform[];
Defines two *distinct* arrays, which therefore can never be equal. Hence
it optimises 
    for ( platform = _splatform; platform != _eplatform; platform++ )
with the assumption that there must always be at least one iteration.

The proposed fix
    for ( platform = _splatform; platform < _eplatform; platform++ )
is also not correct because due to the same optimisation it always does
at least one iteration.

I expect (although I have not tried) that the compiler would also
optimise an if (_splatform == _eplatform) away too.

We use similar constructs elsewhere (just cherry picking some likely
candidates from a grep for []):
xen/include/asm-x86/percpu.h:extern char __per_cpu_start[], __per_cpu_data_end[];
xen/include/asm/uaccess.h:extern struct exception_table_entry __start___ex_table[];
xen/include/asm/uaccess.h:extern struct exception_table_entry __stop___ex_table[];
xen/include/asm/uaccess.h:extern struct exception_table_entry __start___pre_ex_table[];
xen/include/asm/uaccess.h:extern struct exception_table_entry __stop___pre_ex_table[];
xen/include/asm-x86/config.h:extern char trampoline_start[], trampoline_end[];
xen/include/xen/kernel.h:extern char _start[], _end[];
xen/include/xen/kernel.h:extern char _stext[], _etext[];
xen/include/xen/kernel.h:extern const char _srodata[], _erodata[];
xen/include/xen/kernel.h:extern char _sinittext[], _einittext[];
xen/arch/x86/setup.c:extern char __init_begin[], __init_end[], __bss_start[];
xen/arch/x86/tboot.c:extern char __init_begin[], __bss_start[];
xen/common/lib.c:extern const ctor_func_t __ctors_start[], __ctors_end[];

I suspect that the code is actually OK (if not strictly
legal/well-defined) if there are actually entries in each table, since
then the two arrays are distinct like the compiler assumes and we
guarantee that the end array is after the start array via the linker
script.

Or maybe we are sitting on a ticking time bomb, waiting for the gcc guys
to get more maliciou^Wclever but at least it is the same bomb as Linux
(which uses this construct extensively too).

FWIW this seems to work:
    for ( platform = &_splatform[0]; platform < &_eplatform[0]; platform++ )
But I'm not sure it is strictly any less undefined than the original
code and it might just be obscuring things enough that today's optimiser
misses a trick.

Ian J also suggested __attribute__(may_alias) which must be applied to
the struct, or making __foo_start a real variable defined in an ASM
file.

I expect that none of these arrays will ever be empty in the natural
course of things and so the code works. I was just unlucky to trip over
the point in a bisection where some infrastructure had been introduced
but not yet used, but it is now too late to fix that. Probably the right
thing to do is not fret about this for 4.3.

Ian.

  reply	other threads:[~2013-05-14  9:32 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-13 16:22 [PATCH 0/4] xen/arm: fixes for 64 bit Ian Campbell
2013-05-13 16:22 ` [PATCH 1/4] xen/arm: Add emacs magic blocks to asm files Ian Campbell
2013-05-13 16:44   ` Stefano Stabellini
2013-05-13 17:35     ` Tim Deegan
2013-05-13 16:23 ` [PATCH 2/4] xen/arm64: do not clobber callee saved register in early_putch Ian Campbell
2013-05-13 16:53   ` Stefano Stabellini
2013-05-13 16:23 ` [PATCH 3/4] xen/arm: support "arm, armv8-timer" DTS compatibility Ian Campbell
2013-05-13 16:46   ` Stefano Stabellini
2013-05-13 16:23 ` [PATCH 4/4] xen/arm: correctly handle an empty array of platform descs Ian Campbell
2013-05-13 16:48   ` Stefano Stabellini
2013-05-14  9:32     ` Ian Campbell [this message]
2013-05-14  9:48       ` Jan Beulich
2013-05-14 10:21         ` Ian Campbell
2013-05-14 14:29           ` Jan Beulich
2013-05-14 14:37             ` Jan Beulich
2013-05-14 14:54             ` Ian Campbell
2013-05-14 14:56               ` Ian Campbell
2013-05-14 15:07                 ` Ian Campbell
2013-05-14 15:46                   ` Jan Beulich
2013-05-14 16:41                     ` Ian Campbell
2013-05-15  7:12                       ` Jan Beulich
2013-05-15  9:47                         ` Ian Campbell
2013-05-15 12:19                           ` Jan Beulich
2013-05-15 13:47                             ` Ian Campbell
2013-05-16 10:17                               ` Jan Beulich
2013-05-17 10:07                                 ` Ian Campbell
2013-05-17 10:39                                   ` Jan Beulich
2013-05-17 14:34                                     ` Ian Campbell
2013-05-17 15:34                                       ` Jan Beulich
2013-05-17 17:03                                         ` Ian Campbell
2013-05-21  7:54                                           ` Jan Beulich
2013-05-14 15:11               ` Jan Beulich
2013-05-14 15:14                 ` Ian Campbell
2013-05-14  9:36 ` [PATCH 0/4] xen/arm: fixes for 64 bit Ian Campbell

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=1368523975.14264.22.camel@zakaz.uk.xensource.com \
    --to=ian.campbell@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=JBeulich@suse.com \
    --cc=julien.grall@citrix.com \
    --cc=keir@xen.org \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xen.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 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.