virtio: force spec specified alignment on types
Subsystems (5)
Michael S. Tsirkin
maintainerPATCH v9 1/1 virtio: force spec specified alignment on types
The ring element addresses are passed between components with different alignments assumptions. Thus, if guest/userspace selects a pointer and host then gets and dereferences it, we might need to decrease the compiler-selected alignment to prevent compiler on the host from assuming pointer is aligned.
This actually triggers on ARM with -mabi=apcs-gnu - which is a deprecated configuration, but it seems safer to handle this generally.
Note that userspace that allocates the memory is actually OK and does not need to be fixed, but userspace that gets it from guest or another process does need to be fixed. The later doesn't generally talk to the kernel so while it might be buggy it's not talking to the kernel in the buggy way - it's just using the header in the buggy way - so fixing header and asking userspace to recompile is the best we can do.
I verified that the produced kernel binary on x86 is exactly identical before and after the change.
---
I am still not sure this is even needed. Does compiler make optimizations based on ABI alignment assumptions?
Changes from v8:
- better commit log
- go back to vring in UAPI
drivers/vhost/vhost.h | 6 ++--- include/uapi/linux/virtio_ring.h | 38 +++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-)
drivers/vhost/vhost.h +3 -3
@@ -67,9 +67,9 @@ struct vhost_virtqueue {/* The actual ring of buffers. */ struct mutex mutex;unsigned int num; -struct vring_desc __user *desc; -struct vring_avail __user *avail; -struct vring_used __user *used; +vring_desc_t __user *desc; +vring_avail_t __user *avail; +vring_used_t __user *used;const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS ];struct file *kick;struct eventfd_ctx *call_ctx;
include/uapi/linux/virtio_ring.h +28 -10
@@ -118,16 +118,6 @@ struct vring_used {struct vring_used_elem ring[]; }; -struct vring { -unsigned int num; - -struct vring_desc *desc; - -struct vring_avail *avail; - -struct vring_used *used; -}; -/* Alignment requirements for vring elements. * When using pre-virtio 1.0 layout, these fall out naturally. */
@@ -135,6 +125,34 @@ struct vring {#define VRING_USED_ALIGN_SIZE 4#define VRING_DESC_ALIGN_SIZE 16 +/* +* The ring element addresses are passed between components with different +* alignments assumptions. Thus, we might need to decrease the compiler-selected +* alignment, and so must use a typedef to make sure the __aligned attribute +* actually takes hold: +* +* https://gcc.gnu.org/onlinedocs//gcc/Common-Type-Attributes.html#Common-Type-Attributes +* +* When used on a struct, or struct member, the aligned attribute can only +* increase the alignment; in order to decrease it, the packed attribute must +* be specified as well. When used as part of a typedef, the aligned attribute +* can both increase and decrease alignment, and specifying the packed +* attribute generates a warning. +*/ +typedef struct vring_desc __aligned(VRING_DESC_ALIGN_SIZE) vring_desc_t ; +typedef struct vring_avail __aligned(VRING_AVAIL_ALIGN_SIZE) vring_avail_t ; +typedef struct vring_used __aligned(VRING_USED_ALIGN_SIZE) vring_used_t ; + +struct vring { +unsigned int num; + +vring_desc_t *desc; + +vring_avail_t *avail; + +vring_used_t *used; +}; +#ifndef VIRTIO_RING_NO_LEGACY /* The standard layout for the ring is a continuous chunk of memory which looks
Signature · 1 line
MST
kbuild test robot
build botI love your patch! Yet something to improve:
Hi "Michael,
I love your patch! Yet something to improve:
[auto build test ERROR on vhost/linux-next]
[cannot apply to linus/master linux/master v5.7-rc2 next-20200422] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Michael-S-Tsirkin/virtio-force-spec-specified-alignment-on-types/20200420-100135 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next config: i386-randconfig-c001-20200422 (attached as .config) compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0 reproduce: # save the attached .config to linux build tree make ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from ./usr/include/linux/vhost_types.h:17:0,
from ./usr/include/linux/vhost.h:14,
from <command-line>:32:
>> ./usr/include/linux/virtio_ring.h:124:31: error: expected declaration specifiers or '...' before numeric constant
#define VRING_DESC_ALIGN_SIZE 16
^
./usr/include/linux/virtio_ring.h:140:37: note: in expansion of macro 'VRING_DESC_ALIGN_SIZE'
typedef struct vring_desc __aligned(VRING_DESC_ALIGN_SIZE) vring_desc_t;
^~~~~~~~~~~~~~~~~~~~~
./usr/include/linux/virtio_ring.h:122:32: error: expected declaration specifiers or '...' before numeric constant
#define VRING_AVAIL_ALIGN_SIZE 2
^
./usr/include/linux/virtio_ring.h:141:38: note: in expansion of macro 'VRING_AVAIL_ALIGN_SIZE'
typedef struct vring_avail __aligned(VRING_AVAIL_ALIGN_SIZE) vring_avail_t;
^~~~~~~~~~~~~~~~~~~~~~
./usr/include/linux/virtio_ring.h:123:31: error: expected declaration specifiers or '...' before numeric constant
#define VRING_USED_ALIGN_SIZE 4
^
./usr/include/linux/virtio_ring.h:142:37: note: in expansion of macro 'VRING_USED_ALIGN_SIZE'
typedef struct vring_used __aligned(VRING_USED_ALIGN_SIZE) vring_used_t;
^~~~~~~~~~~~~~~~~~~~~
>> ./usr/include/linux/virtio_ring.h:147:2: error: unknown type name 'vring_desc_t'
vring_desc_t *desc;
^~~~~~~~~~~~
./usr/include/linux/virtio_ring.h:149:2: error: unknown type name 'vring_avail_t'
vring_avail_t *avail;
^~~~~~~~~~~~~
>> ./usr/include/linux/virtio_ring.h:151:2: error: unknown type name 'vring_used_t'
vring_used_t *used;
^~~~~~~~~~~~
./usr/include/linux/virtio_ring.h: In function 'vring_init':
./usr/include/linux/virtio_ring.h:190:12: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
vr->avail = (struct vring_avail *)((char *)p + num * sizeof(struct vring_desc));
^
>> ./usr/include/linux/virtio_ring.h:191:44: error: request for member 'ring' in something not a structure or union
vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16)
^~---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org