Skip to contentSkip to conversation

vfio: use __aligned_u64 for ioctl structs

PATCH Applied Stefan Hajnoczi 2 participants 5 messages Sep 18, 2023 – Sep 28, 2023

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v3 cover vfio: use __aligned_u64 for ioctl structs

v3:
- Remove the output struct sizing code that copied out zeroed fields at the end of the struct. Alex pointed out that new fields (or repurposing a field that was previously reserved) must be guarded by a flag and this means userspace won't access those fields when they are absent.
v2:
- Rebased onto https://github.com/awilliam/linux-vfio.git next to get the vfio_iommu_type1_info pad field [Kevin]
- Fixed min(minsz, sizeof(dmabuf)) -> min(dmabuf.argsz, sizeof(dmabuf)) [Jason, Kevin]
- Squashed Patch 3 (vfio_iommu_type1_info) into Patch 1 since it is trivial now that the padding field is already there.

Jason Gunthorpe <[email protected]> pointed out that u64 VFIO ioctl struct fields have architecture-dependent alignment. iommufd already uses __aligned_u64 to avoid this problem.

See the __aligned_u64 typedef in <uapi/linux/types.h> for details on why it is a good idea for kernel<->user interfaces.

This series modifies the VFIO ioctl structs to use __aligned_u64. Some of the changes preserve the existing memory layout on all architectures, so I put them together into the first patch. The remaining patches are for structs where explanation is necessary about why changing the memory layout does not break the uapi.

Stefan Hajnoczi (3):
vfio: trivially use __aligned_u64 for ioctl structs
vfio: use __aligned_u64 in struct vfio_device_gfx_plane_info
vfio: use __aligned_u64 in struct vfio_device_ioeventfd

 include/uapi/linux/vfio.h        | 26 ++++++++++++++------------
 drivers/gpu/drm/i915/gvt/kvmgt.c |  2 +-
 samples/vfio-mdev/mbochs.c       |  2 +-
 samples/vfio-mdev/mdpy.c         |  2 +-
 4 files changed, 17 insertions(+), 15 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v3 1/3 vfio: trivially use __aligned_u64 for ioctl structs

u64 alignment behaves differently depending on the architecture and so <uapi/linux/types.h> offers __aligned_u64 to achieve consistent behavior in kernel<->userspace ABIs.

There are structs in <uapi/linux/vfio.h> that can trivially be updated to __aligned_u64 because the struct sizes are multiples of 8 bytes. There is no change in memory layout on any CPU architecture and therefore this change is safe.

The commits that follow this one handle the trickier cases where explanation about ABI breakage is necessary.

---
 include/uapi/linux/vfio.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
include/uapi/linux/vfio.h +9 -9
index afc1369216d9..4dc0182c6bcb 100644
@@ -277,8 +277,8 @@ struct vfio_region_info {
 #define VFIO_REGION_INFO_FLAG_CAPS	(1 << 3) /* Info supports caps */
 	__u32	index;		/* Region index */
 	__u32	cap_offset;	/* Offset within info struct of first cap */
-	__u64	size;		/* Region size (bytes) */
-	__u64	offset;		/* Region offset from start of device fd */
+	__aligned_u64	size;	/* Region size (bytes) */
+	__aligned_u64	offset;	/* Region offset from start of device fd */
 };
 #define VFIO_DEVICE_GET_REGION_INFO	_IO(VFIO_TYPE, VFIO_BASE + 8)
 
@@ -294,8 +294,8 @@ struct vfio_region_info {
 #define VFIO_REGION_INFO_CAP_SPARSE_MMAP	1
 
 struct vfio_region_sparse_mmap_area {
-	__u64	offset;	/* Offset of mmap'able area within region */
-	__u64	size;	/* Size of mmap'able area */
+	__aligned_u64	offset;	/* Offset of mmap'able area within region */
+	__aligned_u64	size;	/* Size of mmap'able area */
 };
 
 struct vfio_region_info_cap_sparse_mmap {
@@ -450,9 +450,9 @@ struct vfio_device_migration_info {
 					     VFIO_DEVICE_STATE_V1_RESUMING)
 
 	__u32 reserved;
-	__u64 pending_bytes;
-	__u64 data_offset;
-	__u64 data_size;
+	__aligned_u64 pending_bytes;
+	__aligned_u64 data_offset;
+	__aligned_u64 data_size;
 };
 
 /*
@@ -476,7 +476,7 @@ struct vfio_device_migration_info {
 
 struct vfio_region_info_cap_nvlink2_ssatgt {
 	struct vfio_info_cap_header header;
-	__u64 tgt;
+	__aligned_u64 tgt;
 };
 
 /*
@@ -1449,7 +1449,7 @@ struct vfio_iommu_type1_info {
 	__u32	flags;
 #define VFIO_IOMMU_INFO_PGSIZES (1 << 0)	/* supported page sizes info */
 #define VFIO_IOMMU_INFO_CAPS	(1 << 1)	/* Info supports caps */
-	__u64	iova_pgsizes;	/* Bitmap of supported page sizes */
+	__aligned_u64	iova_pgsizes;		/* Bitmap of supported page sizes */
 	__u32   cap_offset;	/* Offset within info struct of first cap */
 	__u32   pad;
 };
Signature · 1 line
2.41.0
#2 lore raw

PATCH v3 2/3 vfio: use __aligned_u64 in struct vfio_device_gfx_plane_info

In reply to Stefan Hajnoczi

The memory layout of struct vfio_device_gfx_plane_info is architecture-dependent due to a u64 field and a struct size that is not a multiple of 8 bytes:
- On x86_64 the struct size is padded to a multiple of 8 bytes.
- On x32 the struct size is only a multiple of 4 bytes, not 8.
- Other architectures may vary.

Use __aligned_u64 to make memory layout consistent. This reduces the chance of 32-bit userspace on a 64-bit kernel breakage.

This patch increases the struct size on x32 but this is safe because of the struct's argsz field. The kernel may grow the struct as long as it still supports smaller argsz values from userspace (e.g. applications compiled against older kernel headers).

---
 include/uapi/linux/vfio.h        | 3 ++-
 drivers/gpu/drm/i915/gvt/kvmgt.c | 2 +-
 samples/vfio-mdev/mbochs.c       | 2 +-
 samples/vfio-mdev/mdpy.c         | 2 +-
 4 files changed, 5 insertions(+), 4 deletions(-)
include/uapi/linux/vfio.h +2 -1
index 4dc0182c6bcb..ee98b6d4a112 100644
@@ -816,7 +816,7 @@ struct vfio_device_gfx_plane_info {
 	__u32 drm_plane_type;	/* type of plane: DRM_PLANE_TYPE_* */
 	/* out */
 	__u32 drm_format;	/* drm format of plane */
-	__u64 drm_format_mod;   /* tiled mode */
+	__aligned_u64 drm_format_mod;   /* tiled mode */
 	__u32 width;	/* width of plane */
 	__u32 height;	/* height of plane */
 	__u32 stride;	/* stride of plane */
@@ -829,6 +829,7 @@ struct vfio_device_gfx_plane_info {
 		__u32 region_index;	/* region index */
 		__u32 dmabuf_id;	/* dma-buf id */
 	};
+	__u32 reserved;
 };
 
 #define VFIO_DEVICE_QUERY_GFX_PLANE _IO(VFIO_TYPE, VFIO_BASE + 14)
drivers/gpu/drm/i915/gvt/kvmgt.c +1 -1
index 42ce20e72db7..faf21be724c3 100644
@@ -1379,7 +1379,7 @@ static long intel_vgpu_ioctl(struct vfio_device *vfio_dev, unsigned int cmd,
 		intel_gvt_reset_vgpu(vgpu);
 		return 0;
 	} else if (cmd == VFIO_DEVICE_QUERY_GFX_PLANE) {
-		struct vfio_device_gfx_plane_info dmabuf;
+		struct vfio_device_gfx_plane_info dmabuf = {};
 		int ret = 0;
 
 		minsz = offsetofend(struct vfio_device_gfx_plane_info,
samples/vfio-mdev/mbochs.c +1 -1
index 3764d1911b51..93405264ff23 100644
@@ -1262,7 +1262,7 @@ static long mbochs_ioctl(struct vfio_device *vdev, unsigned int cmd,
 
 	case VFIO_DEVICE_QUERY_GFX_PLANE:
 	{
-		struct vfio_device_gfx_plane_info plane;
+		struct vfio_device_gfx_plane_info plane = {};
 
 		minsz = offsetofend(struct vfio_device_gfx_plane_info,
 				    region_index);
samples/vfio-mdev/mdpy.c +1 -1
index 064e1c0a7aa8..72ea5832c927 100644
@@ -591,7 +591,7 @@ static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd,
 
 	case VFIO_DEVICE_QUERY_GFX_PLANE:
 	{
-		struct vfio_device_gfx_plane_info plane;
+		struct vfio_device_gfx_plane_info plane = {};
 
 		minsz = offsetofend(struct vfio_device_gfx_plane_info,
 				    region_index);
Signature · 1 line
2.41.0
#3 lore raw

PATCH v3 3/3 vfio: use __aligned_u64 in struct vfio_device_ioeventfd

In reply to Stefan Hajnoczi

The memory layout of struct vfio_device_ioeventfd is architecture-dependent due to a u64 field and a struct size that is not a multiple of 8 bytes:
- On x86_64 the struct size is padded to a multiple of 8 bytes.
- On x32 the struct size is only a multiple of 4 bytes, not 8.
- Other architectures may vary.

Use __aligned_u64 to make memory layout consistent. This reduces the chance that 32-bit userspace on a 64-bit kernel breakage.

This patch increases the struct size on x32 but this is safe because of the struct's argsz field. The kernel may grow the struct as long as it still supports smaller argsz values from userspace (e.g. applications compiled against older kernel headers).

The code that uses struct vfio_device_ioeventfd already works correctly when the struct size grows, so only the struct definition needs to be changed.

---
 include/uapi/linux/vfio.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/uapi/linux/vfio.h +3 -2
index ee98b6d4a112..049ce8065a32 100644
@@ -864,9 +864,10 @@ struct vfio_device_ioeventfd {
 #define VFIO_DEVICE_IOEVENTFD_32	(1 << 2) /* 4-byte write */
 #define VFIO_DEVICE_IOEVENTFD_64	(1 << 3) /* 8-byte write */
 #define VFIO_DEVICE_IOEVENTFD_SIZE_MASK	(0xf)
-	__u64	offset;			/* device fd offset of write */
-	__u64	data;			/* data to be written */
+	__aligned_u64	offset;		/* device fd offset of write */
+	__aligned_u64	data;		/* data to be written */
 	__s32	fd;			/* -1 for de-assignment */
+	__u32	reserved;
 };
 
 #define VFIO_DEVICE_IOEVENTFD		_IO(VFIO_TYPE, VFIO_BASE + 16)
Signature · 1 line
2.41.0
#4 lore raw
In reply to Stefan Hajnoczi
Quoted 30 lines · On Mon, 18 Sep 2023 16:56:14 -0400 Stefan Hajnoczi <[email protected]> wrote:

v3:
- Remove the output struct sizing code that copied out zeroed fields at the end of the struct. Alex pointed out that new fields (or repurposing a field that was previously reserved) must be guarded by a flag and this means userspace won't access those fields when they are absent.
v2:
- Rebased onto https://github.com/awilliam/linux-vfio.git next to get the vfio_iommu_type1_info pad field [Kevin]
- Fixed min(minsz, sizeof(dmabuf)) -> min(dmabuf.argsz, sizeof(dmabuf)) [Jason, Kevin]
- Squashed Patch 3 (vfio_iommu_type1_info) into Patch 1 since it is trivial now that the padding field is already there.

Jason Gunthorpe <[email protected]> pointed out that u64 VFIO ioctl struct fields have architecture-dependent alignment. iommufd already uses __aligned_u64 to avoid this problem.

See the __aligned_u64 typedef in <uapi/linux/types.h> for details on why it is a good idea for kernel<->user interfaces.

This series modifies the VFIO ioctl structs to use __aligned_u64. Some of the changes preserve the existing memory layout on all architectures, so I put them together into the first patch. The remaining patches are for structs where explanation is necessary about why changing the memory layout does not break the uapi.

Stefan Hajnoczi (3):
vfio: trivially use __aligned_u64 for ioctl structs
vfio: use __aligned_u64 in struct vfio_device_gfx_plane_info
vfio: use __aligned_u64 in struct vfio_device_ioeventfd

Quoted 6 lines
 include/uapi/linux/vfio.h        | 26 ++++++++++++++------------
 drivers/gpu/drm/i915/gvt/kvmgt.c |  2 +-
 samples/vfio-mdev/mbochs.c       |  2 +-
 samples/vfio-mdev/mdpy.c         |  2 +-
 4 files changed, 17 insertions(+), 15 deletions(-)

Applied to vfio next branch for v6.7. Thanks,

Alex