mbox series

[RFC,0/6] xen/abi: On wide bitfields inside primitive types

Message ID 20241029181632.69600-1-alejandro.vallejo@cloud.com (mailing list archive)
Headers show
Series xen/abi: On wide bitfields inside primitive types | expand

Message

Alejandro Vallejo Oct. 29, 2024, 6:16 p.m. UTC
Non-boolean bitfields in the hypercall ABI make it fairly inconvenient to
create bindings for any language because (a) they are always ad-hoc and are
subject to restrictions regular fields are not (b) require boilerplate that
regular fields do not and (c) might not even be part of the core language,
forcing avoidable external libraries into any sort of generic library.

This patch (it's a series merely to split roughly by maintainer) is one such
case that I happened to spot while playing around. It's the grant_version
field, buried under an otherwise empty grant_opts.

The invariant I'd like to (slowly) introduce and discuss is that fields may
have bitflags (e.g: a packed array of booleans indexed by some enumerated
type), but not be mixed with wider fields in the same primitive type. This
ensures any field containing an integer of any kind can be referred by pointer
and treated the same way as any other with regards to sizeof() and the like.

I'd like to have a certain consensus about this general point before going
establishing this restriction in the IDL system I'm working on.

My preference would be to fold everything into a single patch if we decide to
follow through with this particular case. As I said before, the split is
artificial for review.

Alejandro Vallejo (6):
  xen/domctl: Refine grant_opts into grant_version
  tools: Rename grant_opts to grant_version
  tools/ocaml: Rename grant_opts to grant_version
  xen/arm: Rename grant_opts to grant_version
  xen/x86: Rename grant_opts to grant_version
  xen/common: Rename grant_opts to grant_version

 tools/helpers/init-xenstore-domain.c             |  2 +-
 tools/libs/light/libxl_create.c                  |  2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c              |  3 +--
 tools/python/xen/lowlevel/xc/xc.c                |  2 +-
 tools/tests/paging-mempool/test-paging-mempool.c |  2 +-
 tools/tests/resource/test-resource.c             |  6 +++---
 tools/tests/tsx/test-tsx.c                       |  4 ++--
 xen/arch/arm/dom0less-build.c                    |  4 ++--
 xen/arch/arm/domain_build.c                      |  2 +-
 xen/arch/x86/setup.c                             |  2 +-
 xen/common/domain.c                              |  6 +++---
 xen/common/grant_table.c                         |  3 +--
 xen/include/public/domctl.h                      | 15 +++++++++++----
 xen/include/xen/grant_table.h                    |  4 ++--
 14 files changed, 31 insertions(+), 26 deletions(-)

Comments

Alejandro Vallejo Oct. 29, 2024, 6:29 p.m. UTC | #1
On Tue Oct 29, 2024 at 6:16 PM GMT, Alejandro Vallejo wrote:
> Non-boolean bitfields in the hypercall ABI make it fairly inconvenient to
> create bindings for any language because (a) they are always ad-hoc and are
> subject to restrictions regular fields are not (b) require boilerplate that
> regular fields do not and (c) might not even be part of the core language,
> forcing avoidable external libraries into any sort of generic library.
>
> This patch (it's a series merely to split roughly by maintainer) is one such
> case that I happened to spot while playing around. It's the grant_version
> field, buried under an otherwise empty grant_opts.
>
> The invariant I'd like to (slowly) introduce and discuss is that fields may
> have bitflags (e.g: a packed array of booleans indexed by some enumerated
> type), but not be mixed with wider fields in the same primitive type. This
> ensures any field containing an integer of any kind can be referred by pointer
> and treated the same way as any other with regards to sizeof() and the like.
>
> I'd like to have a certain consensus about this general point before going
> establishing this restriction in the IDL system I'm working on.
>
> My preference would be to fold everything into a single patch if we decide to
> follow through with this particular case. As I said before, the split is
> artificial for review.
>
> Alejandro Vallejo (6):
>   xen/domctl: Refine grant_opts into grant_version
>   tools: Rename grant_opts to grant_version
>   tools/ocaml: Rename grant_opts to grant_version
>   xen/arm: Rename grant_opts to grant_version
>   xen/x86: Rename grant_opts to grant_version
>   xen/common: Rename grant_opts to grant_version
>
>  tools/helpers/init-xenstore-domain.c             |  2 +-
>  tools/libs/light/libxl_create.c                  |  2 +-
>  tools/ocaml/libs/xc/xenctrl_stubs.c              |  3 +--
>  tools/python/xen/lowlevel/xc/xc.c                |  2 +-
>  tools/tests/paging-mempool/test-paging-mempool.c |  2 +-
>  tools/tests/resource/test-resource.c             |  6 +++---
>  tools/tests/tsx/test-tsx.c                       |  4 ++--
>  xen/arch/arm/dom0less-build.c                    |  4 ++--
>  xen/arch/arm/domain_build.c                      |  2 +-
>  xen/arch/x86/setup.c                             |  2 +-
>  xen/common/domain.c                              |  6 +++---
>  xen/common/grant_table.c                         |  3 +--
>  xen/include/public/domctl.h                      | 15 +++++++++++----
>  xen/include/xen/grant_table.h                    |  4 ++--
>  14 files changed, 31 insertions(+), 26 deletions(-)

Bah. I sent it too early. The new field in patches 2-6 ought to be
max_grant_version. Regardless, the general point still holds, I hope.

Cheers,
Alejandro
Christian Lindig Oct. 30, 2024, 8:45 a.m. UTC | #2
> On 29 Oct 2024, at 18:16, Alejandro Vallejo <alejandro.vallejo@cloud.com> wrote:
> 
> 
> The invariant I'd like to (slowly) introduce and discuss is that fields may
> have bitflags (e.g: a packed array of booleans indexed by some enumerated
> type), but not be mixed with wider fields in the same primitive type. This
> ensures any field containing an integer of any kind can be referred by pointer
> and treated the same way as any other with regards to sizeof() and the like.

Acked-by: Christian Lindig <christian.lindig@cloud.com>


Fine with me but the OCaml part is not very exposed to this.

— C
Jan Beulich Oct. 30, 2024, 9:14 a.m. UTC | #3
On 29.10.2024 19:16, Alejandro Vallejo wrote:
> Non-boolean bitfields in the hypercall ABI make it fairly inconvenient to
> create bindings for any language because (a) they are always ad-hoc and are
> subject to restrictions regular fields are not (b) require boilerplate that
> regular fields do not and (c) might not even be part of the core language,
> forcing avoidable external libraries into any sort of generic library.
> 
> This patch (it's a series merely to split roughly by maintainer) is one such
> case that I happened to spot while playing around. It's the grant_version
> field, buried under an otherwise empty grant_opts.
> 
> The invariant I'd like to (slowly) introduce and discuss is that fields may
> have bitflags (e.g: a packed array of booleans indexed by some enumerated
> type), but not be mixed with wider fields in the same primitive type. This
> ensures any field containing an integer of any kind can be referred by pointer
> and treated the same way as any other with regards to sizeof() and the like.

While I don't strictly mind, I'm also not really seeing why taking addresses
or applying sizeof() would be commonly necessary. Can you perhaps provide a
concrete example of where the present way of dealing with grant max version
is getting in the way? After all your use of the term "bitfield" doesn't
really mean C's understanding of it, so especially (c) above escapes me to a
fair degree.

> I'd like to have a certain consensus about this general point before going
> establishing this restriction in the IDL system I'm working on.
> 
> My preference would be to fold everything into a single patch if we decide to
> follow through with this particular case. As I said before, the split is
> artificial for review.

That's not just a preference, but a requirement, or else the build will break
in the middle of the series (harming bisection at the very least).

Jan
Alejandro Vallejo Oct. 30, 2024, 2:02 p.m. UTC | #4
On Wed Oct 30, 2024 at 8:45 AM GMT, Christian Lindig wrote:
>
>
> > On 29 Oct 2024, at 18:16, Alejandro Vallejo <alejandro.vallejo@cloud.com> wrote:
> > 
> > 
> > The invariant I'd like to (slowly) introduce and discuss is that fields may
> > have bitflags (e.g: a packed array of booleans indexed by some enumerated
> > type), but not be mixed with wider fields in the same primitive type. This
> > ensures any field containing an integer of any kind can be referred by pointer
> > and treated the same way as any other with regards to sizeof() and the like.
>
> Acked-by: Christian Lindig <christian.lindig@cloud.com>

Thanks.

>
>
> Fine with me but the OCaml part is not very exposed to this.

Yeah, OCaml is pretty far from interacting with these details at all.
>
> — C

Cheers,
Alejandro
Alejandro Vallejo Oct. 30, 2024, 3:08 p.m. UTC | #5
In the course of preparing this answer I just noticed that altp2m_opts suffers
from the exact same annoyance, with the exact same fix. I just noticed while
rebasing my Rust branch.

On Wed Oct 30, 2024 at 9:14 AM GMT, Jan Beulich wrote:
> On 29.10.2024 19:16, Alejandro Vallejo wrote:
> > Non-boolean bitfields in the hypercall ABI make it fairly inconvenient to
> > create bindings for any language because (a) they are always ad-hoc and are
> > subject to restrictions regular fields are not (b) require boilerplate that
> > regular fields do not and (c) might not even be part of the core language,
> > forcing avoidable external libraries into any sort of generic library.
> > 
> > This patch (it's a series merely to split roughly by maintainer) is one such
> > case that I happened to spot while playing around. It's the grant_version
> > field, buried under an otherwise empty grant_opts.
> > 
> > The invariant I'd like to (slowly) introduce and discuss is that fields may
> > have bitflags (e.g: a packed array of booleans indexed by some enumerated
> > type), but not be mixed with wider fields in the same primitive type. This
> > ensures any field containing an integer of any kind can be referred by pointer
> > and treated the same way as any other with regards to sizeof() and the like.
>
> While I don't strictly mind, I'm also not really seeing why taking addresses
> or applying sizeof() would be commonly necessary. Can you perhaps provide a
> concrete example of where the present way of dealing with grant max version
> is getting in the way? After all your use of the term "bitfield" doesn't
> really mean C's understanding of it, so especially (c) above escapes me to a
> fair degree.

Wall of text ahead, but I'll try to stay on point. The rationale should become
a lot clearer after I send an RFC series with initial code to autogenerate some
hypercall payloads from markup. The biggest question is: Can I create a
definition language such that (a) it precisely represents the Xen ABI and (b)
is fully type-safe under modern strongly-typed languages?

I already have a backbone I can define the ABI in, so my options when I hit
some impedance mismatch are:

  1. Change the ABI so it matches better my means of defining it.
  2. Change the means to define so it captures the existing ABI better.

Most of the work I've done has moved in the (2) direction so far, but I found a
number of pain points when mapping the existing ABI to Rust that, while not
impossible to work around, are quite annoying for no clear benefit. If
possible, I'd like to simplify the cognitive load involved in defining, using
and updating hypercalls rather than bending over backwards to support a
construct that provides no real benefit. IOW: If I can define an ABI that is
_simpler_, it follows that it's also easier to not make mistakes and it's
easier to generate code for it.

The use of packed fields is one such case. Even in C, we create extra macros
for creating a field, modifying it, fetching it, etc. Patches 2-6 are strict
code removals. And even in the most extreme cases the space savings are largely
irrelevant because the hypercall has a fixed size. We do want to pack _flags_
as otherwise the payload size would explode pretty quickly on hypercalls with
tons of boolean options, but I'm not aware of that being problematic for wider
subfields (like the grant max version).

Now, being more concrete...

##################################################################
# IDL is simpler if the size is a property of the type
##################################################################

Consider the definition of the (new) max_grant_version type under the IDL I'm
working on (it's TOML, but I don't particularly care about which markup we end
up using).

  [[enums]]
  name = "xen_domaincreate_max_grant_version"
  description = "Content of the `max_grant_version` field of the domain creation hypercall."
  typ = { tag = "u8" }

  [[enums.variants]]
  name = "off"
  description = "Must be used with gnttab support compiled out"
  value = 0

  [[enums.variants]]
  name = "v1"
  description = "Allow the domain to use up to gnttab_v1"
  value = 1

  [[enums.variants]]
  name = "v2"
  description = "Allow the domain to use up to gnttab_v2"
  value = 2

Note that I can define a type being enumerated, can choose its specific
variants and its width is a property of the type itself. With bitfields you're
always in a weird position of the width not being part of the type that goes
into it.

Should I need it as a field somewhere, then...

  [[structs.fields]]
  name = "max_grant_version"
  description = "Maximum grant table version the domain may be bumped to"
  typ = { tag = "enum", args = "xen_domaincreate_max_grant_version" }

... at which point the size of the field is given by an intrinsic property of
the type (the typ property on the enums table) I previously defined. It's
extensible, composable and allows me to generate readable code in both C and
Rust.

Should I need to support full bitfields I would require a means of stating the
start and end bits of every field, which is very bad for the sanity of whoever
wants to maintain coherency in the ABI.

##################################################################
# Rust and Go don't like bitfields...
##################################################################

... and neither does C, even if for historic reasons they do exist in the
standard. On a slight tangent, neither Rust nor Go support bitfields in the
core language. This was a deliberate design decision of their respective
designers. I can't speak for Go as I'm not a Go developer, but Rust does have a
very well-known, well-supported and very common external crate ("bitflags")
that allows very ergonomic semantics for definition of packed booleans. As an
example here's the flags for domain create, as spitted out by the generator I
have.

(comments removed for brevity). This defines a bitmap indexed by the flags
type, represented by a 32bit primitive. It's type-safe from the PoV that I
can't just write 1 << 15 to a variable of this type and expect anything but
screams from the compiler.

  bitflags! {
      #[repr(C)]
      #[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
      pub struct XenDomaincreateFlags: u32 {
          const Hvm = 1 << 0;
          const Hap = 1 << 1;
          const S3Integrity = 1 << 2;
          const OosOff = 1 << 3;
          const XsDomain = 1 << 4;
          const Iommu = 1 << 5;
          const NestedVirt = 1 << 6;
          const Vpmu = 1 << 7;
      }
  }

This enables callers to have type-safe variables in a pretty ergonomic fashion:

  let flags = XenDomainCreateFlags::Hvm |
              XenDomainCreateFlags::Hap |
              XenDomainCreateFlags::Iommu;

and assignments to its related struct would follow regular assignment rules.

##################################################################
# IOW
##################################################################

Supporting general bitfields is annoying, even in C. Adding support in IDL for
them is a headache if we want the descriptions to help us catch mistakes and
language support is poor using this constructs (at best).

Can we please get rid of them?

>
> > I'd like to have a certain consensus about this general point before going
> > establishing this restriction in the IDL system I'm working on.
> > 
> > My preference would be to fold everything into a single patch if we decide to
> > follow through with this particular case. As I said before, the split is
> > artificial for review.
>
> That's not just a preference, but a requirement, or else the build will break
> in the middle of the series (harming bisection at the very least).
>
> Jan

Yes, indeed. As I said, I'm more interested in the discussion now rather than
the details of committing it.

Cheers,
Alejandro