mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1 0/2] iommu/virtio: Harden event handling
@ 2026-07-14  2:59 weimin xiong
  2026-07-14  2:59 ` [PATCH v1 1/2] iommu/virtio: Set driver data before enabling virtqueues weimin xiong
  2026-07-14  2:59 ` [PATCH v1 2/2] iommu/virtio: Reject short event buffers weimin xiong
  0 siblings, 2 replies; 5+ messages in thread
From: weimin xiong @ 2026-07-14  2:59 UTC (permalink / raw)
  To: Jean-Philippe Brucker, Joerg Roedel, Will Deacon
  Cc: Robin Murphy, virtualization, iommu, linux-kernel, Xiong Weimin

From: Xiong Weimin <xiongweimin@kylinos.cn>

Hi,

This follow-up series hardens virtio-iommu event handling.

It is based on the probe error handling fixes sent earlier:

  [PATCH v1 0/2] iommu/virtio: Fix probe error handling
  Message-ID: <20260714024949.190014-1-15927021679@163.com>

The first patch makes sure vq->vdev->priv is initialized before
virtqueues can invoke callbacks. The second patch rejects short event
buffers before reading the event contents.

Xiong Weimin (2):
  iommu/virtio: Set driver data before enabling virtqueues
  iommu/virtio: Reject short event buffers

 drivers/iommu/virtio-iommu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1 1/2] iommu/virtio: Set driver data before enabling virtqueues
  2026-07-14  2:59 [PATCH v1 0/2] iommu/virtio: Harden event handling weimin xiong
@ 2026-07-14  2:59 ` weimin xiong
  2026-07-22 21:42   ` Will Deacon
  2026-07-14  2:59 ` [PATCH v1 2/2] iommu/virtio: Reject short event buffers weimin xiong
  1 sibling, 1 reply; 5+ messages in thread
From: weimin xiong @ 2026-07-14  2:59 UTC (permalink / raw)
  To: Jean-Philippe Brucker, Joerg Roedel, Will Deacon
  Cc: Robin Murphy, virtualization, iommu, linux-kernel, Xiong Weimin

From: Xiong Weimin <xiongweimin@kylinos.cn>

The event virtqueue callback retrieves the driver state through
vq->vdev->priv. viommu_probe() currently initializes that pointer only
after virtio_device_ready() and after the event queue is populated.

Store the driver data before creating the virtqueues so callbacks always
see initialized driver state once the device is made ready. Clear the
pointer again on probe failure.

Signed-off-by: Xiong Weimin <xiongweimin@kylinos.cn>
---
 drivers/iommu/virtio-iommu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
index 9118377d7..4c91a82d2 100644
--- a/drivers/iommu/virtio-iommu.c
+++ b/drivers/iommu/virtio-iommu.c
@@ -1173,11 +1173,12 @@ static int viommu_probe(struct virtio_device *vdev)
 	ida_init(&viommu->domain_ids);
 	viommu->dev = dev;
 	viommu->vdev = vdev;
+	vdev->priv = viommu;
 	INIT_LIST_HEAD(&viommu->requests);
 
 	ret = viommu_init_vqs(viommu);
 	if (ret)
-		return ret;
+		goto err_clear_priv;
 
 	virtio_cread_le(vdev, struct virtio_iommu_config, page_size_mask,
 			&viommu->pgsize_bitmap);
@@ -1238,8 +1239,6 @@ static int viommu_probe(struct virtio_device *vdev)
 	if (ret)
 		goto err_free_vqs;
 
-	vdev->priv = viommu;
-
 	ret = iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
 	if (ret)
 		goto err_remove_sysfs;
@@ -1254,6 +1253,7 @@ static int viommu_probe(struct virtio_device *vdev)
 	iommu_device_sysfs_remove(&viommu->iommu);
 err_free_vqs:
 	vdev->config->del_vqs(vdev);
+err_clear_priv:
 	vdev->priv = NULL;
 
 	return ret;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1 2/2] iommu/virtio: Reject short event buffers
  2026-07-14  2:59 [PATCH v1 0/2] iommu/virtio: Harden event handling weimin xiong
  2026-07-14  2:59 ` [PATCH v1 1/2] iommu/virtio: Set driver data before enabling virtqueues weimin xiong
@ 2026-07-14  2:59 ` weimin xiong
  2026-07-22 21:41   ` Will Deacon
  1 sibling, 1 reply; 5+ messages in thread
From: weimin xiong @ 2026-07-14  2:59 UTC (permalink / raw)
  To: Jean-Philippe Brucker, Joerg Roedel, Will Deacon
  Cc: Robin Murphy, virtualization, iommu, linux-kernel, Xiong Weimin

From: Xiong Weimin <xiongweimin@kylinos.cn>

viommu_event_handler() only rejects event buffers that are larger than
struct viommu_event. A short buffer is also invalid, but the handler
would still read evt->head and, for fault events, the rest of evt->fault.

Require the used length to match the event buffer size before looking at
the event contents.

Signed-off-by: Xiong Weimin <xiongweimin@kylinos.cn>
---
 drivers/iommu/virtio-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
index 4c91a82d2..4b7f0dcfa 100644
--- a/drivers/iommu/virtio-iommu.c
+++ b/drivers/iommu/virtio-iommu.c
@@ -635,7 +635,7 @@ static void viommu_event_handler(struct virtqueue *vq)
 	struct viommu_dev *viommu = vq->vdev->priv;
 
 	while ((evt = virtqueue_get_buf(vq, &len)) != NULL) {
-		if (len > sizeof(*evt)) {
+		if (len != sizeof(*evt)) {
 			dev_err(viommu->dev,
 				"invalid event buffer (len %u != %zu)\n",
 				len, sizeof(*evt));
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 2/2] iommu/virtio: Reject short event buffers
  2026-07-14  2:59 ` [PATCH v1 2/2] iommu/virtio: Reject short event buffers weimin xiong
@ 2026-07-22 21:41   ` Will Deacon
  0 siblings, 0 replies; 5+ messages in thread
From: Will Deacon @ 2026-07-22 21:41 UTC (permalink / raw)
  To: weimin xiong
  Cc: Jean-Philippe Brucker, Joerg Roedel, Robin Murphy,
	virtualization, iommu, linux-kernel, Xiong Weimin

On Tue, Jul 14, 2026 at 10:59:14AM +0800, weimin xiong wrote:
> From: Xiong Weimin <xiongweimin@kylinos.cn>
> 
> viommu_event_handler() only rejects event buffers that are larger than
> struct viommu_event. A short buffer is also invalid, but the handler
> would still read evt->head and, for fault events, the rest of evt->fault.
> 
> Require the used length to match the event buffer size before looking at
> the event contents.
> 
> Signed-off-by: Xiong Weimin <xiongweimin@kylinos.cn>
> ---
>  drivers/iommu/virtio-iommu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
> index 4c91a82d2..4b7f0dcfa 100644
> --- a/drivers/iommu/virtio-iommu.c
> +++ b/drivers/iommu/virtio-iommu.c
> @@ -635,7 +635,7 @@ static void viommu_event_handler(struct virtqueue *vq)
>  	struct viommu_dev *viommu = vq->vdev->priv;
>  
>  	while ((evt = virtqueue_get_buf(vq, &len)) != NULL) {
> -		if (len > sizeof(*evt)) {
> +		if (len != sizeof(*evt)) {
>  			dev_err(viommu->dev,
>  				"invalid event buffer (len %u != %zu)\n",
>  				len, sizeof(*evt));

See Jean-Phillipe's previous reply to an identical patch:

https://lore.kernel.org/all/6800BE0812AF0897+20260706083148.788991-1-raoxu@uniontech.com/

Will

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 1/2] iommu/virtio: Set driver data before enabling virtqueues
  2026-07-14  2:59 ` [PATCH v1 1/2] iommu/virtio: Set driver data before enabling virtqueues weimin xiong
@ 2026-07-22 21:42   ` Will Deacon
  0 siblings, 0 replies; 5+ messages in thread
From: Will Deacon @ 2026-07-22 21:42 UTC (permalink / raw)
  To: weimin xiong
  Cc: Jean-Philippe Brucker, Joerg Roedel, Robin Murphy,
	virtualization, iommu, linux-kernel, Xiong Weimin

On Tue, Jul 14, 2026 at 10:59:13AM +0800, weimin xiong wrote:
> From: Xiong Weimin <xiongweimin@kylinos.cn>
> 
> The event virtqueue callback retrieves the driver state through
> vq->vdev->priv. viommu_probe() currently initializes that pointer only
> after virtio_device_ready() and after the event queue is populated.
> 
> Store the driver data before creating the virtqueues so callbacks always
> see initialized driver state once the device is made ready. Clear the
> pointer again on probe failure.
> 
> Signed-off-by: Xiong Weimin <xiongweimin@kylinos.cn>
> ---
>  drivers/iommu/virtio-iommu.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
> index 9118377d7..4c91a82d2 100644
> --- a/drivers/iommu/virtio-iommu.c
> +++ b/drivers/iommu/virtio-iommu.c
> @@ -1173,11 +1173,12 @@ static int viommu_probe(struct virtio_device *vdev)
>  	ida_init(&viommu->domain_ids);
>  	viommu->dev = dev;
>  	viommu->vdev = vdev;
> +	vdev->priv = viommu;
>  	INIT_LIST_HEAD(&viommu->requests);
>  
>  	ret = viommu_init_vqs(viommu);
>  	if (ret)
> -		return ret;
> +		goto err_clear_priv;

I guess it would make sense to pass the 'vdev' instead of the 'viommu'
pointer to viommu_init_vqs(), now that you have linked them together.

Will

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-22 21:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-14  2:59 [PATCH v1 0/2] iommu/virtio: Harden event handling weimin xiong
2026-07-14  2:59 ` [PATCH v1 1/2] iommu/virtio: Set driver data before enabling virtqueues weimin xiong
2026-07-22 21:42   ` Will Deacon
2026-07-14  2:59 ` [PATCH v1 2/2] iommu/virtio: Reject short event buffers weimin xiong
2026-07-22 21:41   ` Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®