All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefano Stabellini <sstabellini@kernel.org>
To: Ian Jackson <ian.jackson@citrix.com>
Cc: Juergen Gross <jgross@suse.com>,
	Stefano Stabellini <stefanos@xilinx.com>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Wei Liu <wei.liu2@citrix.com>,
	Andrew Cooper <Andrew.Cooper3@citrix.com>,
	Julien Grall <julien.grall@gmail.com>,
	Julien Grall <julien.grall@arm.com>,
	Jan Beulich <JBeulich@suse.com>,
	Stewart Hildebrand <Stewart.Hildebrand@dornerworks.com>,
	xen-devel <xen-devel@lists.xenproject.org>
Subject: Re: [PATCH v6 1/4] xen: introduce SYMBOL
Date: Thu, 7 Feb 2019 10:18:00 -0800 (PST)	[thread overview]
Message-ID: <alpine.DEB.2.10.1902070954530.28697@sstabellini-ThinkPad-X260> (raw)
In-Reply-To: <23644.6915.12943.72736@mariner.uk.xensource.com>

On Thu, 7 Feb 2019, Ian Jackson wrote:
> Stefano Stabellini writes ("Re: [Xen-devel] [PATCH v6 1/4] xen: introduce SYMBOL"):
> > I am OK with this approach. Maybe not the best IMO, but good enough. It
> > should also satisfy the MISRAC guys, as they wrote "ideally cast to
> > uintptr_t only once": here we wouldn't be casting only once, but at
> > least we would do it inside a single well-defined macro.
> 
> Right.  I think it meets the goals of MISRA-C, probably better than
> most other approaches.
> 
> FAOD, I think you should expect people to declare the linker symbols
> either as I suggested:
> 
>      extern const struct wombat _wombats_start[];
>      extern const struct abstract_symbol _wombats_end[];
> 
> (or along the lines of Jan's suggestion, but frankly I think that is
> going to be too hard to sort out now.)

Yes, they are already declared this way, I would prefer to avoid
changing the declaration as part of this series.


> > +/*
> > + * Performs x - y, returns the original pointer type. To be used when
> > + * either x or y or both are linker symbols.
> > + */
> > +#define SYMBOLS_SUBTRACT(x, y) ({                                             \
> > +    __typeof__(*(y)) *ptr_;                                                   \
> > +    ptr_ = (typeof(ptr_)) (((uintptr_t)(x) - (uintptr_t)(y)) / sizeof(*(y))); \
> > +    ptr_;                                                                     \
> > +})
> 
> This is type-incoherent.  The difference between two pointers is a
> scalar, not another pointer.

I am glad you highlighted this. The vast majority of changes in this
series are subtractions or comparisons.  So, if subtractions (and also
comparisons as you wrote below) need to return a scalar, then we might
as well return uintptr_t or ptrdiff_t from the two macros. It makes a
lot of sense to me.


> Also "the original pointer type" is
> ambiguous.  It should refer explicitly to y.  IMO this function should
> contain a typecheck which assures that x is of the right type.
> 
> How about something like this:
> 
>   /*
>    * Calculate (end - start), where start and end are linker symbols,
>    * giving a ptrdiff_t.  The size is in units of start's referent.
>    * end must be a `struct abstract_symbol*'.
>    */
>   #define SYMBOLS_ARRAY_LEN(start,end) ({
>      ((end) == (struct abstract_symbol*)0);                               
>      (ptrdiff_t)((uintptr_t)(end) - (uintptr_t)(start)) / sizeof(*start);
>   })

Sounds good, but the issue is that we might have to use this macro with:

- start is a linker symbol and end as a normal pointer
- start is a normal pointer and end as a linker symbol
- both are linker symbols

If so, do we need three slightly different variations of this macro?


>   /*
>    * Given two pointers A,B of arbitrary types, gives the difference
>    * B-A in bytes.  Can be used for comparisons:
>    *   If A<B, gives a negative number
>    *   if A==B, gives zero
>    *   If A>B, gives a positive number
>    * Legal even if the pointers are to different objects.
>    */
>   #define POINTER_CMP(a,b) ({
>      ((a) == (void*)0);
>      ((b) == (void*)0);
>      (ptrdiff_t)((uintptr_t)(end) - (uintptr_t)(start));
>   })
> 
> The application of these two your two examples is complex because your
> examples seem wrong to me.

Yeah, I realize it wasn't really possible to understand my examples
unless one was very familiar with past versions of the series. I'll add
more context below.


> > +/*
> > + * Performs x - y, returns uintptr_t. To be used when either x or y or
> 
> This is wrong.  Comparisons should give a signed output.
> 
> > + * both are linker symbols.
> 
> In neither of your example below are the things in question linker
> symbols so your examples violate your own preconditions...
> 
> 
> > Examples:
> > 
> > +    new_ptr = SYMBOLS_SUBTRACT(func->old_addr, _start) + vmap_of_xen_text;
> 
> This is punning wildly between pointers and integers.  I infer that
> old_addr is a pointer of some kind and vmap_of_xen_text is an integer.
> I also infer that sizeof(*old_addr) is 1 because otherwise you
> multiply vmap_of_xen_text by the size which is clearly entirely wrong.
> Ie this code is just entirely wrong.
> 
> This is presumably some kind of relocation.  I don't think it makes
> much sense to macro this.  Instead, it is better to make
> vmap_of_xen_text a pointer and do this:
> 
>   +    /* Relocation.  We need to calculate the offset of the address
>   +     * from _start, and apply that to our own map, to find where we
>   +     * have this mapped.  Doing these kind of games directly with
>   +     * pointers is contrary to the C rules for what pointers may be
>   +     * compared and computed.  So we do the offset calculation with
>   +     * integers, which is always legal.  The subsequent addition of
>   +     * the offset to the vmap_of_xen_text pointer is legal because
>   +     * the computed pointer is indeed a valid part of the object
>   +     * referred to by vmap_of_xen_text - namely, the byte array
>   +     * of our mapping of the Xen text. */
>   +    new_ptr = ((uintptr_t)func->old_addr - (uintptr_t)_start) + vmap_of_xen_text;
> 
> Note that, unfortunately, any deviation from completely normal pointer
> handling *must* be accompanied by this kind of a proof, to explain why
> it is OK.

OK. Most of the call sites only do things like (_end - _start) or (p >
_end). I wanted to bring up cases that are not trivial.

We have a couple of cases where we are "punning wildly between pointers
and integers", for instance:

xen/arch/arm/arm64/livepatch.c:arch_livepatch_apply
xen/arch/arm/setup.c:start_xen  line 772
xen/arch/x86/setup.c:__start_xen  line 1382

I think it is OK to manually cast to (uintptr_t) in those cases as you
suggest.


> > and:
> > 
> > +    for ( alt = region->begin;
> > +          SYMBOLS_COMPARE(alt, region->end) < 0;
> > +          alt++ )
> 
> region->begin and ->end aren't linker symbols, are they ?

I made this example because this is a common pattern that we have in the
hypervisor. A better example using your suggested macro would be:

+    for ( call = __initcall_start;
+          POINTER_CMP(call, __presmp_initcall_end) < 0;
+          call++ )
 
Where __initcall_start and __presmp_initcall_end are linker symbols.
(Above region->begin and region->end are initialized to two linker
symbols.)


> So the
> wrong assumption by the compiler (which is at the root of this thread)
> that different linker symbols are necessarily different objects
> (resulting from the need to declare them in C as if they were) does
> not arise.  I think you mean maybe something like _region_start and
> _region_end.  So with my proposed macro:
> 
> > We could also define a third macro such as:
> >   #define SYMBOLS_SUBTRACT_INT(x, y)  SYMBOLS_COMPARE((x), (y))
> > because we have many places where we need the result of SYMBOLS_SUBTRACT
> > converted to an integer type. For instance:
> >   paddr_t xen_size = (uintptr_t)SYMBOLS_SUBTRAC(_end, _start);
> 
> This need arises because the difference between two pointers is indeed
> an integer and not another pointer.

Yes, I get it.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  reply	other threads:[~2019-02-07 18:18 UTC|newest]

Thread overview: 102+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-09 23:41 [PATCH v6 0/4] misc safety certification fixes Stefano Stabellini
2019-01-09 23:42 ` [PATCH v6 1/4] xen: introduce SYMBOL Stefano Stabellini
2019-01-10  2:40   ` Julien Grall
2019-01-10  8:24     ` Jan Beulich
2019-01-10 17:29       ` Stefano Stabellini
2019-01-10 18:46         ` Stewart Hildebrand
2019-01-10 19:03           ` Stefano Stabellini
2019-01-11 10:35           ` Jan Beulich
2019-01-11 17:01             ` Stefano Stabellini
2019-01-10 19:24         ` Julien Grall
2019-01-10 21:36           ` Stefano Stabellini
2019-01-10 23:31             ` Julien Grall
2019-01-11  2:14               ` Stefano Stabellini
2019-01-11  6:52                 ` Juergen Gross
2019-01-11 16:52                   ` Stefano Stabellini
2019-01-11 10:48                 ` Jan Beulich
2019-01-11 18:04                   ` Stefano Stabellini
2019-01-11 18:53                     ` Stewart Hildebrand
2019-01-11 20:35                       ` Julien Grall
2019-01-11 20:46                         ` Stewart Hildebrand
2019-01-11 21:37                           ` Stefano Stabellini
2019-01-14  3:45                             ` Stewart Hildebrand
2019-01-14 10:26                               ` Jan Beulich
2019-01-14 21:18                                 ` Stefano Stabellini
     [not found]                                   ` <1CACC1FB020000D800417A66@prv1-mh.provo.novell.com>
2019-01-15  8:21                                     ` Jan Beulich
2019-01-15 11:51                                       ` Julien Grall
     [not found]                                         ` <AB1DA25B020000B95C475325@prv1-mh.provo.novell.com>
2019-01-15 12:04                                           ` Jan Beulich
2019-01-15 12:23                                             ` Julien Grall
     [not found]                                               ` <BAE986750200003A5C475325@prv1-mh.provo.novell.com>
2019-01-15 12:44                                                 ` Jan Beulich
2019-01-15 20:03                                       ` Stewart Hildebrand
2019-01-16  6:01                                         ` Juergen Gross
2019-01-16 10:19                                         ` Jan Beulich
2019-01-17  0:37                                           ` Stefano Stabellini
     [not found]                                             ` <B4D3ABC30200003B88BF86FB@prv1-mh.provo.novell.com>
     [not found]                                               ` <529ED2F90200004D00417A66@prv1-mh.provo.novell.com>
2019-01-17 11:45                                                 ` Jan Beulich
2019-01-18  1:24                                                   ` Stefano Stabellini
     [not found]                                                     ` <76A2DEED0200005600417A66@prv1-mh.provo.novell.com>
2019-01-18  9:54                                                       ` Jan Beulich
2019-01-18 10:48                                                         ` Julien Grall
     [not found]                                                           ` <9F511FC70200005E5C475325@prv1-mh.provo.novell.com>
2019-01-18 11:09                                                             ` Jan Beulich
2019-01-18 15:22                                                               ` Julien Grall
     [not found]                                                                 ` <3A8206D8020000035C475325@prv1-mh.provo.novell.com>
2019-01-21  9:39                                                                   ` Jan Beulich
2019-01-21  9:34                                                             ` Jan Beulich
2019-01-21 10:22                                                               ` Julien Grall
     [not found]                                                                 ` <E16AB350020000435C475325@prv1-mh.provo.novell.com>
2019-01-21 10:31                                                                   ` Jan Beulich
2019-01-21 23:15                                                                     ` Stefano Stabellini
     [not found]                                                                       ` <5EA2B4FA0200008000417A66@prv1-mh.provo.novell.com>
2019-01-22  9:06                                                                         ` Jan Beulich
2019-01-18 23:05                                                         ` Stefano Stabellini
2019-01-21  5:24                                                           ` Stewart Hildebrand
     [not found]                                                           ` <5A96F2FD0200008D00417A66@prv1-mh.provo.novell.com>
2019-01-21  9:50                                                             ` Jan Beulich
2019-01-21 23:41                                                               ` Stefano Stabellini
2019-01-22  6:08                                                                 ` Juergen Gross
     [not found]                                                                 ` <42A2C4FA0200009000417A66@prv1-mh.provo.novell.com>
2019-01-22  9:16                                                                   ` Jan Beulich
2019-02-01 18:52                                                                     ` George Dunlap
2019-02-01 20:53                                                                       ` Stefano Stabellini
     [not found]                                                             ` <58377FAD0200004688BF86FB@prv1-mh.provo.novell.com>
2019-01-21 10:06                                                               ` Jan Beulich
2019-02-06 15:41                                                                 ` Ian Jackson
     [not found]                                           ` <C8F95655020000CAB8D7C7D4@prv1-mh.provo.novell.com>
     [not found]                                             ` <5867EFE6020000DB00417A66@prv1-mh.provo.novell.com>
     [not found]                                               ` <DACE7A5F020000B1B8D7C7D4@prv1-mh.provo.novell.com>
2019-02-07 14:51                                                 ` Jan Beulich
2019-01-15 23:36                                       ` Stefano Stabellini
2019-01-16  8:47                                         ` Juergen Gross
     [not found]                                         ` <2EA6D6FD0200001F00417A66@prv1-mh.provo.novell.com>
2019-01-16 10:25                                           ` Jan Beulich
2019-01-17  0:41                                             ` Stefano Stabellini
     [not found]                                               ` <4EA2F2F90200004D00417A66@prv1-mh.provo.novell.com>
2019-01-17 11:46                                                 ` Jan Beulich
     [not found]                                     ` <95DC675902000028AB59E961@prv1-mh.provo.novell.com>
2019-02-04  9:37                                       ` Jan Beulich
2019-02-04 19:08                                         ` Stefano Stabellini
2019-02-05  6:02                                           ` Juergen Gross
     [not found]                                           ` <2E9DDEFD0200007B00417A66@prv1-mh.provo.novell.com>
2019-02-05  7:53                                             ` Jan Beulich
2019-02-05 14:56                                         ` George Dunlap
     [not found]                                           ` <E730A9F90200001DAB59E961@prv1-mh.provo.novell.com>
2019-02-06 11:59                                             ` Jan Beulich
     [not found]                                 ` <7A8C0A4F020000EEB8D7C7D4@prv1-mh.provo.novell.com>
2019-02-06 16:21                                   ` Jan Beulich
2019-02-06 16:37                                     ` Ian Jackson
     [not found]                                       ` <08D440470200001BB8D7C7D4@prv1-mh.provo.novell.com>
2019-02-06 16:47                                         ` Jan Beulich
2019-02-06 16:52                                           ` Ian Jackson
2019-02-06 23:39                                             ` Stefano Stabellini
2019-02-07 11:48                                               ` Ian Jackson
2019-02-07 18:18                                                 ` Stefano Stabellini [this message]
2019-02-12 11:31                                                   ` Ian Jackson
2019-02-13  0:09                                                     ` Stefano Stabellini
2019-01-15 11:46                             ` Julien Grall
2019-01-15 12:23                               ` Julien Grall
2019-01-14 10:11                     ` Jan Beulich
2019-01-14 15:41                       ` Julien Grall
2019-01-14 15:52                         ` Jan Beulich
2019-01-14 16:26                           ` Stewart Hildebrand
2019-01-14 16:39                             ` Jan Beulich
2019-01-14 16:28                           ` Julien Grall
2019-01-14 16:44                             ` Jan Beulich
2019-01-14 17:24                               ` Julien Grall
2019-01-15  8:04                                 ` Jan Beulich
2019-01-10 17:22     ` Stefano Stabellini
2019-01-10  8:34   ` Jan Beulich
2019-01-10 18:09     ` Stefano Stabellini
2019-01-09 23:42 ` [PATCH v6 2/4] xen/arm: use SYMBOL when required Stefano Stabellini
2019-01-10  8:41   ` Jan Beulich
2019-01-10 17:44     ` Stefano Stabellini
2019-01-11 10:52       ` Jan Beulich
2019-01-11 16:58         ` Stefano Stabellini
2019-01-14  9:23           ` Jan Beulich
2019-01-09 23:42 ` [PATCH v6 3/4] xen/x86: " Stefano Stabellini
2019-01-10  8:43   ` Jan Beulich
2019-01-10 17:45     ` Stefano Stabellini
2019-01-09 23:42 ` [PATCH v6 4/4] xen/common: " Stefano Stabellini
2019-01-10  8:49   ` Jan Beulich
2019-01-10 17:48     ` Stefano Stabellini

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=alpine.DEB.2.10.1902070954530.28697@sstabellini-ThinkPad-X260 \
    --to=sstabellini@kernel.org \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=Stewart.Hildebrand@dornerworks.com \
    --cc=ian.jackson@citrix.com \
    --cc=jgross@suse.com \
    --cc=julien.grall@arm.com \
    --cc=julien.grall@gmail.com \
    --cc=stefanos@xilinx.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xenproject.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.