Skip to contentSkip to conversation

drm/virtio: introduce F_BLOB_ALIGNMENT support

PATCH Superseded Sergio Lopez 2 participants 12 messages Nov 10, 2025 – Nov 13, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover drm/virtio: introduce F_BLOB_ALIGNMENT support

There's an incresing number of machines supporting multiple page sizes and on these machines the host and a guest can be running, each one, with a different page size.

For what pertains to virtio-gpu, this is not a problem if the page size of the guest happens to be bigger or equal than the host, but will potentially lead to failures in memory allocations and/or mappings otherwise.

To deal with this, the virtio-spec was extended to introduce with the VIRTIO_GPU_F_BLOB_ALIGNMENT feature [1]. If this feature is negotiated, we must use the "blob_alignment" field in the config to ensure every CREATE_BLOB and MAP_BLOB is properly aligned before sending it to the device.

We also introduce the VIRTGPU_PARAM_BLOB_ALIGNMENT parameter to allow userspace to query the alignment restrictions for blobs.

This supersedes "drm/virtio: introduce the HOST_PAGE_SIZE feature" [2].

[1] https://github.com/oasis-tcs/virtio-spec/commit/f9abfd55cb663837dda1153b826216dcf4d25b84
[2] https://lkml.org/lkml/2024/7/23/438

Sergio Lopez (3):
drm/virtio: support VIRTIO_GPU_F_BLOB_ALIGNMENT drm/virtio: honor blob_alignment requirements drm/virtio: add VIRTGPU_PARAM_BLOB_ALIGNMENT to params

 drivers/gpu/drm/virtio/virtgpu_drv.c    |  1 +
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  4 +++-
 drivers/gpu/drm/virtio/virtgpu_ioctl.c  |  5 +++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    | 14 +++++++++++---
 drivers/gpu/drm/virtio/virtgpu_object.c |  6 ++++--
 drivers/gpu/drm/virtio/virtgpu_prime.c  |  7 +++++--
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 12 +++++++++++-
 drivers/gpu/drm/virtio/virtgpu_vram.c   | 10 ++++++++--
 include/uapi/drm/virtgpu_drm.h          |  1 +
 include/uapi/linux/virtio_gpu.h         |  9 +++++++++
 10 files changed, 58 insertions(+), 11 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/3 drm/virtio: support VIRTIO_GPU_F_BLOB_ALIGNMENT

Support VIRTIO_GPU_F_BLOB_ALIGNMENT, a feature that indicates the device provides a valid blob_alignment field in its configuration, and that both RESOURCE_CREATE_BLOB and RESOURCE_MAP_BLOB requests must be aligned to that value.

---
 drivers/gpu/drm/virtio/virtgpu_drv.c |  1 +
 drivers/gpu/drm/virtio/virtgpu_drv.h |  2 ++
 drivers/gpu/drm/virtio/virtgpu_kms.c | 14 +++++++++++---
 include/uapi/linux/virtio_gpu.h      |  9 +++++++++
 4 files changed, 23 insertions(+), 3 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +1 -0
index 71c6ccad4b99..ad675ee18b01 100644
@@ -162,6 +162,7 @@ static unsigned int features[] = {
 	VIRTIO_GPU_F_RESOURCE_UUID,
 	VIRTIO_GPU_F_RESOURCE_BLOB,
 	VIRTIO_GPU_F_CONTEXT_INIT,
+	VIRTIO_GPU_F_BLOB_ALIGNMENT,
 };
 static struct virtio_driver virtio_gpu_driver = {
 	.feature_table = features,
drivers/gpu/drm/virtio/virtgpu_drv.h +2 -0
index f17660a71a3e..d1fa386a5a99 100644
@@ -257,6 +257,7 @@ struct virtio_gpu_device {
 	bool has_resource_blob;
 	bool has_host_visible;
 	bool has_context_init;
+	bool has_blob_alignment;
 	struct virtio_shm_region host_visible_region;
 	struct drm_mm host_visible_mm;
 
@@ -270,6 +271,7 @@ struct virtio_gpu_device {
 	uint32_t num_capsets;
 	uint64_t capset_id_mask;
 	struct list_head cap_cache;
+	uint32_t blob_alignment;
 
 	/* protects uuid state when exporting */
 	spinlock_t resource_export_lock;
drivers/gpu/drm/virtio/virtgpu_kms.c +11 -3
index 1c15cbf326b7..a20b62041f03 100644
@@ -123,7 +123,7 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 	struct virtio_gpu_device *vgdev;
 	/* this will expand later */
 	struct virtqueue *vqs[2];
-	u32 num_scanouts, num_capsets;
+	u32 num_scanouts, num_capsets, blob_alignment;
 	int ret = 0;
 
 	if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
@@ -197,14 +197,22 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_CONTEXT_INIT))
 		vgdev->has_context_init = true;
 
+	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_BLOB_ALIGNMENT)) {
+		vgdev->has_blob_alignment = true;
+		virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
+				blob_alignment, &blob_alignment);
+		vgdev->blob_alignment = blob_alignment;
+	}
+
 	DRM_INFO("features: %cvirgl %cedid %cresource_blob %chost_visible",
 		 vgdev->has_virgl_3d    ? '+' : '-',
 		 vgdev->has_edid        ? '+' : '-',
 		 vgdev->has_resource_blob ? '+' : '-',
 		 vgdev->has_host_visible ? '+' : '-');
 
-	DRM_INFO("features: %ccontext_init\n",
-		 vgdev->has_context_init ? '+' : '-');
+	DRM_INFO("features: %ccontext_init %cblob_alignment\n",
+		 vgdev->has_context_init ? '+' : '-',
+		 vgdev->has_blob_alignment ? '+' : '-');
 
 	ret = virtio_find_vqs(vgdev->vdev, 2, vqs, vqs_info, NULL);
 	if (ret) {
include/uapi/linux/virtio_gpu.h +9 -0
index be109777d10d..4f530d90058c 100644
@@ -64,6 +64,14 @@
  * context_init and multiple timelines
  */
 #define VIRTIO_GPU_F_CONTEXT_INIT        4
+/*
+ * The device provides a valid blob_alignment
+ * field in its configuration and both
+ * VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB and
+ * VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB requests
+ * must be aligned to that value.
+ */
+#define VIRTIO_GPU_F_BLOB_ALIGNMENT      5
 
 enum virtio_gpu_ctrl_type {
 	VIRTIO_GPU_UNDEFINED = 0,
@@ -365,6 +373,7 @@ struct virtio_gpu_config {
 	__le32 events_clear;
 	__le32 num_scanouts;
 	__le32 num_capsets;
+	__le32 blob_alignment;
 };
 
 /* simple formats for fbcon/X use */
Signature · 1 line
2.51.0
maintainer
#2 lore raw

Re: [PATCH 1/3] drm/virtio: support VIRTIO_GPU_F_BLOB_ALIGNMENT

drivers/gpu/drm/virtio/virtgpu_kms.c int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev) · line 204

Quoted 5 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_kms.c +5 -0
+	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_BLOB_ALIGNMENT)) {
+		vgdev->has_blob_alignment = true;
+		virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
+				blob_alignment, &blob_alignment);
+		vgdev->blob_alignment = blob_alignment;

Shouldn't blob_alignment be max(guest_alignment, host_alignment)?

Signature · 2 lines
Best regards,
Dmitry
#3 lore raw

Re: [PATCH 1/3] drm/virtio: support VIRTIO_GPU_F_BLOB_ALIGNMENT

Quoted 8 lines · Dmitry Osipenko

drivers/gpu/drm/virtio/virtgpu_kms.c int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev) · line 204

Quoted 5 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_kms.c +5 -0
+	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_BLOB_ALIGNMENT)) {
+		vgdev->has_blob_alignment = true;
+		virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
+				blob_alignment, &blob_alignment);
+		vgdev->blob_alignment = blob_alignment;

Shouldn't blob_alignment be max(guest_alignment, host_alignment)?

virtio_gpu_config is the minimum alignment required by the device/host. If the guest requires a higher alignment than the device/host, I would expect that to be found by a different mechanism, as it would happen on gpu drivers other than virtio-gpu.

Sergio.

maintainer
#4 lore raw

Re: [PATCH 1/3] drm/virtio: support VIRTIO_GPU_F_BLOB_ALIGNMENT

Quoted 11 lines · Sergio Lopez Pascual
Quoted 8 lines · Dmitry Osipenko

drivers/gpu/drm/virtio/virtgpu_kms.c int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev) · line 204

Quoted 5 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_kms.c +5 -0
+	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_BLOB_ALIGNMENT)) {
+		vgdev->has_blob_alignment = true;
+		virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
+				blob_alignment, &blob_alignment);
+		vgdev->blob_alignment = blob_alignment;

Shouldn't blob_alignment be max(guest_alignment, host_alignment)?

Quoted 4 lines

virtio_gpu_config is the minimum alignment required by the device/host. If the guest requires a higher alignment than the device/host, I would expect that to be found by a different mechanism, as it would happen on gpu drivers other than virtio-gpu.

Alright, perhaps that will work.

Signature · 2 lines
Best regards,
Dmitry
#5 lore raw

PATCH 2/3 drm/virtio: honor blob_alignment requirements

In reply to Sergio Lopez

If VIRTIO_GPU_F_BLOB_ALIGNMENT has been negotiated, both CREATE_BLOB and MAP_BLOB requests must be aligned to blob_alignment.

Introduce checks to ensure we don't send invalid requests to the device.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  2 +-
 drivers/gpu/drm/virtio/virtgpu_object.c |  6 ++++--
 drivers/gpu/drm/virtio/virtgpu_prime.c  |  7 +++++--
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 12 +++++++++++-
 drivers/gpu/drm/virtio/virtgpu_vram.c   | 10 ++++++++--
 5 files changed, 29 insertions(+), 8 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -1
index d1fa386a5a99..11a55d4ccd54 100644
@@ -433,7 +433,7 @@ int virtio_gpu_cmd_map(struct virtio_gpu_device *vgdev,
 void virtio_gpu_cmd_unmap(struct virtio_gpu_device *vgdev,
 			  struct virtio_gpu_object *bo);
 
-void
+int
 virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev,
 				    struct virtio_gpu_object *bo,
 				    struct virtio_gpu_object_params *params,
drivers/gpu/drm/virtio/virtgpu_object.c +4 -2
index e6363c887500..6118344bff84 100644
@@ -246,8 +246,10 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
 		if (params->blob_mem == VIRTGPU_BLOB_MEM_GUEST)
 			bo->guest_blob = true;
 
-		virtio_gpu_cmd_resource_create_blob(vgdev, bo, params,
-						    ents, nents);
+		ret = virtio_gpu_cmd_resource_create_blob(vgdev, bo, params,
+							  ents, nents);
+		if (ret)
+			goto err_put_objs;
 	} else if (params->virgl) {
 		virtio_gpu_cmd_resource_create_3d(vgdev, bo, params,
 						  objs, fence);
drivers/gpu/drm/virtio/virtgpu_prime.c +5 -2
index ce49282198cb..06593496c53f 100644
@@ -257,8 +257,11 @@ static int virtgpu_dma_buf_init_obj(struct drm_device *dev,
 	params.blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
 	params.size = attach->dmabuf->size;
 
-	virtio_gpu_cmd_resource_create_blob(vgdev, bo, &params,
-					    ents, nents);
+	ret = virtio_gpu_cmd_resource_create_blob(vgdev, bo, &params,
+						  ents, nents);
+	if (ret)
+		goto err_import;
+
 	bo->guest_blob = true;
 
 	dma_buf_unpin(attach);
drivers/gpu/drm/virtio/virtgpu_vq.c +11 -1
index 8181b22b9b46..d558ba2d213a 100644
@@ -1393,6 +1393,10 @@ int virtio_gpu_cmd_map(struct virtio_gpu_device *vgdev,
 	struct virtio_gpu_vbuffer *vbuf;
 	struct virtio_gpu_resp_map_info *resp_buf;
 
+	if (vgdev->has_blob_alignment &&
+	    !IS_ALIGNED(offset, vgdev->blob_alignment))
+		return -EINVAL;
+
 	resp_buf = kzalloc(sizeof(*resp_buf), GFP_KERNEL);
 	if (!resp_buf)
 		return -ENOMEM;
@@ -1426,7 +1430,7 @@ void virtio_gpu_cmd_unmap(struct virtio_gpu_device *vgdev,
 	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
 }
 
-void
+int
 virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev,
 				    struct virtio_gpu_object *bo,
 				    struct virtio_gpu_object_params *params,
@@ -1436,6 +1440,10 @@ virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev,
 	struct virtio_gpu_resource_create_blob *cmd_p;
 	struct virtio_gpu_vbuffer *vbuf;
 
+	if (vgdev->has_blob_alignment &&
+	    !IS_ALIGNED(params->size, vgdev->blob_alignment))
+		return -EINVAL;
+
 	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
 	memset(cmd_p, 0, sizeof(*cmd_p));
 
@@ -1456,6 +1464,8 @@ virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev,
 
 	if (nents)
 		bo->attached = true;
+
+	return 0;
 }
 
 void virtio_gpu_cmd_set_scanout_blob(struct virtio_gpu_device *vgdev,
drivers/gpu/drm/virtio/virtgpu_vram.c +8 -2
index 5ad3b7c6f73c..6c402eca5726 100644
@@ -216,8 +216,14 @@ int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev,
 		return ret;
 	}
 
-	virtio_gpu_cmd_resource_create_blob(vgdev, &vram->base, params, NULL,
-					    0);
+	ret = virtio_gpu_cmd_resource_create_blob(vgdev, &vram->base, params,
+						  NULL, 0);
+	if (ret) {
+		drm_gem_free_mmap_offset(obj);
+		kfree(vram);
+		return ret;
+	}
+
 	if (params->blob_flags & VIRTGPU_BLOB_FLAG_USE_MAPPABLE) {
 		ret = virtio_gpu_vram_map(&vram->base);
 		if (ret) {
Signature · 1 line
2.51.0
maintainer
#6 lore raw

Re: [PATCH 2/3] drm/virtio: honor blob_alignment requirements

Quoted 5 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_object.c +0 -0
index e6363c887500..6118344bff84 100644

drivers/gpu/drm/virtio/virtgpu_object.c int virtio_gpu_object_create(struct virtio_gpu_device *vgdev, · line 252

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_object.c +4 -2
@@ -246,8 +246,10 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
 		if (params->blob_mem == VIRTGPU_BLOB_MEM_GUEST)
 			bo->guest_blob = true;
 
-		virtio_gpu_cmd_resource_create_blob(vgdev, bo, params,
-						    ents, nents);
+		ret = virtio_gpu_cmd_resource_create_blob(vgdev, bo, params,
+							  ents, nents);
+		if (ret)
+			goto err_put_objs;

Isn't it impossible for host to import guest blob when HOST_PAGE_SIZE > GUEST_PAGE_SIZE?

Signature · 2 lines
Best regards,
Dmitry
maintainer
#7 lore raw

Re: [PATCH 2/3] drm/virtio: honor blob_alignment requirements

In reply to Sergio Lopez

drivers/gpu/drm/virtio/virtgpu_vram.c int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev, · line 225

Quoted 13 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_vram.c +7 -2
@@ -216,8 +216,14 @@ int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev,
 		return ret;
 	}
 
-	virtio_gpu_cmd_resource_create_blob(vgdev, &vram->base, params, NULL,
-					    0);
+	ret = virtio_gpu_cmd_resource_create_blob(vgdev, &vram->base, params,
+						  NULL, 0);
+	if (ret) {
+		drm_gem_free_mmap_offset(obj);
+		kfree(vram);
+		return ret;
+	}

virtio_gpu_cleanup_object() shall be used to free obj, otherwise resource_ida and etc is leaked

Signature · 2 lines
Best regards,
Dmitry
maintainer
#8 lore raw

Re: [PATCH 2/3] drm/virtio: honor blob_alignment requirements

In reply to Sergio Lopez

drivers/gpu/drm/virtio/virtgpu_vq.c int virtio_gpu_cmd_map(struct virtio_gpu_device *vgdev, · line 1398

Quoted 11 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_vq.c +3 -0
index 8181b22b9b46..d558ba2d213a 100644
@@ -1393,6 +1393,10 @@ int virtio_gpu_cmd_map(struct virtio_gpu_device *vgdev,
 	struct virtio_gpu_vbuffer *vbuf;
 	struct virtio_gpu_resp_map_info *resp_buf;
 
+	if (vgdev->has_blob_alignment &&
+	    !IS_ALIGNED(offset, vgdev->blob_alignment))
+		return -EINVAL;

The vram offset should have been validated when obj is created. Checking offset here on cmd_map() should be unnecessary.

Signature · 2 lines
Best regards,
Dmitry
maintainer
#9 lore raw

Re: [PATCH 2/3] drm/virtio: honor blob_alignment requirements

In reply to Sergio Lopez
Quoted 5 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_prime.c +0 -0
index ce49282198cb..06593496c53f 100644

drivers/gpu/drm/virtio/virtgpu_prime.c static int virtgpu_dma_buf_init_obj(struct drm_device *dev, · line 263

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +4 -2
@@ -257,8 +257,11 @@ static int virtgpu_dma_buf_init_obj(struct drm_device *dev,
 	params.blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
 	params.size = attach->dmabuf->size;
 
-	virtio_gpu_cmd_resource_create_blob(vgdev, bo, &params,
-					    ents, nents);
+	ret = virtio_gpu_cmd_resource_create_blob(vgdev, bo, &params,
+						  ents, nents);
+	if (ret)
+		goto err_import;

The error handling is incorrect, dma_buf_unmap_attachment() won't be invoked when it should be.

Signature · 2 lines
Best regards,
Dmitry
maintainer
#10 lore raw

Re: [PATCH 2/3] drm/virtio: honor blob_alignment requirements

In reply to Sergio Lopez

drivers/gpu/drm/virtio/virtgpu_drv.h virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev, · line 1445

Quoted 12 lines · Sergio Lopez
drivers/gpu/drm/virtio/virtgpu_drv.h +4 -1
-void
+int
 virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev,
 				    struct virtio_gpu_object *bo,
 				    struct virtio_gpu_object_params *params,
@@ -1436,6 +1440,10 @@ virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev,
 	struct virtio_gpu_resource_create_blob *cmd_p;
 	struct virtio_gpu_vbuffer *vbuf;
 
+	if (vgdev->has_blob_alignment &&
+	    !IS_ALIGNED(params->size, vgdev->blob_alignment))
+		return -EINVAL;

Perhaps will be better to validate params.size earlier, within verify_blob(). Then all the added errors handling should become unnecessary.

Signature · 2 lines
Best regards,
Dmitry
#11 lore raw

PATCH 3/3 drm/virtio: add VIRTGPU_PARAM_BLOB_ALIGNMENT to params

In reply to Sergio Lopez

Add VIRTGPU_PARAM_BLOB_ALIGNMENT as a param that can be read with VIRTGPU_GETPARAM by userspace applications running in the guest to obtain the host's page size and find out the right alignment to be used in shared memory allocations.

---
 drivers/gpu/drm/virtio/virtgpu_ioctl.c | 5 +++++
 include/uapi/drm/virtgpu_drm.h         | 1 +
 2 files changed, 6 insertions(+)
drivers/gpu/drm/virtio/virtgpu_ioctl.c +5 -0
index c33c057365f8..b7651f504f00 100644
@@ -117,6 +117,11 @@ static int virtio_gpu_getparam_ioctl(struct drm_device *dev, void *data,
 	case VIRTGPU_PARAM_EXPLICIT_DEBUG_NAME:
 		value = vgdev->has_context_init ? 1 : 0;
 		break;
+	case VIRTGPU_PARAM_BLOB_ALIGNMENT:
+		if (!vgdev->has_blob_alignment)
+			return -ENOENT;
+		value = vgdev->blob_alignment;
+		break;
 	default:
 		return -EINVAL;
 	}
include/uapi/drm/virtgpu_drm.h +1 -0
index 9debb320c34b..7d4e4884d942 100644
@@ -98,6 +98,7 @@ struct drm_virtgpu_execbuffer {
 #define VIRTGPU_PARAM_CONTEXT_INIT 6 /* DRM_VIRTGPU_CONTEXT_INIT */
 #define VIRTGPU_PARAM_SUPPORTED_CAPSET_IDs 7 /* Bitmask of supported capability set ids */
 #define VIRTGPU_PARAM_EXPLICIT_DEBUG_NAME 8 /* Ability to set debug name from userspace */
+#define VIRTGPU_PARAM_BLOB_ALIGNMENT 9 /* Device alignment requirements for blobs */
 
 struct drm_virtgpu_getparam {
 	__u64 param;
Signature · 1 line
2.51.0