* [PATCH] virtio: disable notification hardening by default
@ 2022-06-17 7:26 ` Jason Wang
0 siblings, 0 replies; 10+ messages in thread
From: Jason Wang @ 2022-06-17 7:26 UTC (permalink / raw)
To: mst, jasowang, virtualization, linux-kernel
We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
harden vring IRQ"). It works with the assumption that the driver or
core can properly call virtio_device_ready() at the right
place. Unfortunately, this seems to be not true and uncover various
bugs of the existing drivers, mainly the issue of using
virtio_device_ready() incorrectly.
So let's having a Kconfig option and disable it by default. It gives
us a breath to fix the drivers and then we can consider to enable it
by default.
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
drivers/virtio/Kconfig | 6 ++++++
drivers/virtio/virtio.c | 2 ++
drivers/virtio/virtio_ring.c | 12 ++++++++++++
include/linux/virtio_config.h | 2 ++
4 files changed, 22 insertions(+)
diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index b5adf6abd241..b059c39af3d4 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
if VIRTIO_MENU
+config VIRTIO_HARDEN_NOTIFICATION
+ bool "Harden virtio notification"
+ help
+ Enable to this to harden the device notifications and
+ supress the ones that are illegal.
+
config VIRTIO_PCI
tristate "PCI driver for virtio devices"
depends on PCI
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index ef04a96942bf..21dc08d2f32d 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
* */
void virtio_reset_device(struct virtio_device *dev)
{
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
/*
* The below virtio_synchronize_cbs() guarantees that any
* interrupt for this line arriving after
@@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
*/
virtio_break_device(dev);
virtio_synchronize_cbs(dev);
+#endif
dev->config->reset(dev);
}
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 13a7348cedff..d9d3b6e201fb 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
vq->we_own_ring = true;
vq->notify = notify;
vq->weak_barriers = weak_barriers;
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
vq->broken = true;
+#else
+ vq->broken = false;
+#endif
vq->last_used_idx = 0;
vq->event_triggered = false;
vq->num_added = 0;
@@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
}
if (unlikely(vq->broken)) {
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
dev_warn_once(&vq->vq.vdev->dev,
"virtio vring IRQ raised before DRIVER_OK");
return IRQ_NONE;
+#else
+ return IRQ_HANDLED;
+#endif
}
/* Just a hint for performance: so it's ok that this can be racy! */
@@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
vq->we_own_ring = false;
vq->notify = notify;
vq->weak_barriers = weak_barriers;
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
vq->broken = true;
+#else
+ vq->broken = false;
+#endif
vq->last_used_idx = 0;
vq->event_triggered = false;
vq->num_added = 0;
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
index 9a36051ceb76..d15c3cdda2d2 100644
--- a/include/linux/virtio_config.h
+++ b/include/linux/virtio_config.h
@@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
/*
* The virtio_synchronize_cbs() makes sure vring_interrupt()
* will see the driver specific setup if it sees vq->broken
@@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
*/
virtio_synchronize_cbs(dev);
__virtio_unbreak_device(dev);
+#endif
/*
* The transport should ensure the visibility of vq->broken
* before setting DRIVER_OK. See the comments for the transport
--
2.25.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH] virtio: disable notification hardening by default
@ 2022-06-17 7:26 ` Jason Wang
0 siblings, 0 replies; 10+ messages in thread
From: Jason Wang @ 2022-06-17 7:26 UTC (permalink / raw)
To: mst, jasowang, virtualization, linux-kernel
We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
harden vring IRQ"). It works with the assumption that the driver or
core can properly call virtio_device_ready() at the right
place. Unfortunately, this seems to be not true and uncover various
bugs of the existing drivers, mainly the issue of using
virtio_device_ready() incorrectly.
So let's having a Kconfig option and disable it by default. It gives
us a breath to fix the drivers and then we can consider to enable it
by default.
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
drivers/virtio/Kconfig | 6 ++++++
drivers/virtio/virtio.c | 2 ++
drivers/virtio/virtio_ring.c | 12 ++++++++++++
include/linux/virtio_config.h | 2 ++
4 files changed, 22 insertions(+)
diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index b5adf6abd241..b059c39af3d4 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
if VIRTIO_MENU
+config VIRTIO_HARDEN_NOTIFICATION
+ bool "Harden virtio notification"
+ help
+ Enable to this to harden the device notifications and
+ supress the ones that are illegal.
+
config VIRTIO_PCI
tristate "PCI driver for virtio devices"
depends on PCI
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index ef04a96942bf..21dc08d2f32d 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
* */
void virtio_reset_device(struct virtio_device *dev)
{
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
/*
* The below virtio_synchronize_cbs() guarantees that any
* interrupt for this line arriving after
@@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
*/
virtio_break_device(dev);
virtio_synchronize_cbs(dev);
+#endif
dev->config->reset(dev);
}
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 13a7348cedff..d9d3b6e201fb 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
vq->we_own_ring = true;
vq->notify = notify;
vq->weak_barriers = weak_barriers;
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
vq->broken = true;
+#else
+ vq->broken = false;
+#endif
vq->last_used_idx = 0;
vq->event_triggered = false;
vq->num_added = 0;
@@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
}
if (unlikely(vq->broken)) {
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
dev_warn_once(&vq->vq.vdev->dev,
"virtio vring IRQ raised before DRIVER_OK");
return IRQ_NONE;
+#else
+ return IRQ_HANDLED;
+#endif
}
/* Just a hint for performance: so it's ok that this can be racy! */
@@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
vq->we_own_ring = false;
vq->notify = notify;
vq->weak_barriers = weak_barriers;
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
vq->broken = true;
+#else
+ vq->broken = false;
+#endif
vq->last_used_idx = 0;
vq->event_triggered = false;
vq->num_added = 0;
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
index 9a36051ceb76..d15c3cdda2d2 100644
--- a/include/linux/virtio_config.h
+++ b/include/linux/virtio_config.h
@@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
+#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
/*
* The virtio_synchronize_cbs() makes sure vring_interrupt()
* will see the driver specific setup if it sees vq->broken
@@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
*/
virtio_synchronize_cbs(dev);
__virtio_unbreak_device(dev);
+#endif
/*
* The transport should ensure the visibility of vq->broken
* before setting DRIVER_OK. See the comments for the transport
--
2.25.1
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
2022-06-17 7:26 ` Jason Wang
@ 2022-06-17 10:01 ` Michael S. Tsirkin
-1 siblings, 0 replies; 10+ messages in thread
From: Michael S. Tsirkin @ 2022-06-17 10:01 UTC (permalink / raw)
To: Jason Wang; +Cc: virtualization, linux-kernel
On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> harden vring IRQ"). It works with the assumption that the driver or
> core can properly call virtio_device_ready() at the right
> place. Unfortunately, this seems to be not true and uncover various
> bugs of the existing drivers, mainly the issue of using
> virtio_device_ready() incorrectly.
>
> So let's having a Kconfig option and disable it by default. It gives
> us a breath to fix the drivers and then we can consider to enable it
> by default.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
Do you want to drop irq_lock lock/unlock in ccw too?
> ---
> drivers/virtio/Kconfig | 6 ++++++
> drivers/virtio/virtio.c | 2 ++
> drivers/virtio/virtio_ring.c | 12 ++++++++++++
> include/linux/virtio_config.h | 2 ++
> 4 files changed, 22 insertions(+)
>
> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> index b5adf6abd241..b059c39af3d4 100644
> --- a/drivers/virtio/Kconfig
> +++ b/drivers/virtio/Kconfig
> @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
>
> if VIRTIO_MENU
>
> +config VIRTIO_HARDEN_NOTIFICATION
> + bool "Harden virtio notification"
> + help
> + Enable to this to harden the device notifications and
> + supress the ones that are illegal.
> +
> config VIRTIO_PCI
> tristate "PCI driver for virtio devices"
> depends on PCI
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index ef04a96942bf..21dc08d2f32d 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> * */
> void virtio_reset_device(struct virtio_device *dev)
> {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The below virtio_synchronize_cbs() guarantees that any
> * interrupt for this line arriving after
> @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> */
> virtio_break_device(dev);
> virtio_synchronize_cbs(dev);
> +#endif
>
> dev->config->reset(dev);
> }
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 13a7348cedff..d9d3b6e201fb 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> vq->we_own_ring = true;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> }
>
> if (unlikely(vq->broken)) {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> dev_warn_once(&vq->vq.vdev->dev,
> "virtio vring IRQ raised before DRIVER_OK");
> return IRQ_NONE;
> +#else
> + return IRQ_HANDLED;
> +#endif
> }
>
> /* Just a hint for performance: so it's ok that this can be racy! */
> @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> vq->we_own_ring = false;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> index 9a36051ceb76..d15c3cdda2d2 100644
> --- a/include/linux/virtio_config.h
> +++ b/include/linux/virtio_config.h
> @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
>
> WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
>
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The virtio_synchronize_cbs() makes sure vring_interrupt()
> * will see the driver specific setup if it sees vq->broken
> @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> */
> virtio_synchronize_cbs(dev);
> __virtio_unbreak_device(dev);
> +#endif
> /*
> * The transport should ensure the visibility of vq->broken
> * before setting DRIVER_OK. See the comments for the transport
> --
> 2.25.1
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
@ 2022-06-17 10:01 ` Michael S. Tsirkin
0 siblings, 0 replies; 10+ messages in thread
From: Michael S. Tsirkin @ 2022-06-17 10:01 UTC (permalink / raw)
To: Jason Wang; +Cc: linux-kernel, virtualization
On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> harden vring IRQ"). It works with the assumption that the driver or
> core can properly call virtio_device_ready() at the right
> place. Unfortunately, this seems to be not true and uncover various
> bugs of the existing drivers, mainly the issue of using
> virtio_device_ready() incorrectly.
>
> So let's having a Kconfig option and disable it by default. It gives
> us a breath to fix the drivers and then we can consider to enable it
> by default.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
Do you want to drop irq_lock lock/unlock in ccw too?
> ---
> drivers/virtio/Kconfig | 6 ++++++
> drivers/virtio/virtio.c | 2 ++
> drivers/virtio/virtio_ring.c | 12 ++++++++++++
> include/linux/virtio_config.h | 2 ++
> 4 files changed, 22 insertions(+)
>
> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> index b5adf6abd241..b059c39af3d4 100644
> --- a/drivers/virtio/Kconfig
> +++ b/drivers/virtio/Kconfig
> @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
>
> if VIRTIO_MENU
>
> +config VIRTIO_HARDEN_NOTIFICATION
> + bool "Harden virtio notification"
> + help
> + Enable to this to harden the device notifications and
> + supress the ones that are illegal.
> +
> config VIRTIO_PCI
> tristate "PCI driver for virtio devices"
> depends on PCI
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index ef04a96942bf..21dc08d2f32d 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> * */
> void virtio_reset_device(struct virtio_device *dev)
> {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The below virtio_synchronize_cbs() guarantees that any
> * interrupt for this line arriving after
> @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> */
> virtio_break_device(dev);
> virtio_synchronize_cbs(dev);
> +#endif
>
> dev->config->reset(dev);
> }
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 13a7348cedff..d9d3b6e201fb 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> vq->we_own_ring = true;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> }
>
> if (unlikely(vq->broken)) {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> dev_warn_once(&vq->vq.vdev->dev,
> "virtio vring IRQ raised before DRIVER_OK");
> return IRQ_NONE;
> +#else
> + return IRQ_HANDLED;
> +#endif
> }
>
> /* Just a hint for performance: so it's ok that this can be racy! */
> @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> vq->we_own_ring = false;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> index 9a36051ceb76..d15c3cdda2d2 100644
> --- a/include/linux/virtio_config.h
> +++ b/include/linux/virtio_config.h
> @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
>
> WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
>
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The virtio_synchronize_cbs() makes sure vring_interrupt()
> * will see the driver specific setup if it sees vq->broken
> @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> */
> virtio_synchronize_cbs(dev);
> __virtio_unbreak_device(dev);
> +#endif
> /*
> * The transport should ensure the visibility of vq->broken
> * before setting DRIVER_OK. See the comments for the transport
> --
> 2.25.1
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
2022-06-17 10:01 ` Michael S. Tsirkin
@ 2022-06-17 11:47 ` Jason Wang
-1 siblings, 0 replies; 10+ messages in thread
From: Jason Wang @ 2022-06-17 11:47 UTC (permalink / raw)
To: Michael S. Tsirkin; +Cc: virtualization, linux-kernel
On Fri, Jun 17, 2022 at 6:01 PM Michael S. Tsirkin <mst@redhat.com> wrote:
>
> On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> > We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> > harden vring IRQ"). It works with the assumption that the driver or
> > core can properly call virtio_device_ready() at the right
> > place. Unfortunately, this seems to be not true and uncover various
> > bugs of the existing drivers, mainly the issue of using
> > virtio_device_ready() incorrectly.
> >
> > So let's having a Kconfig option and disable it by default. It gives
> > us a breath to fix the drivers and then we can consider to enable it
> > by default.
> >
> > Signed-off-by: Jason Wang <jasowang@redhat.com>
>
>
> Do you want to drop irq_lock lock/unlock in ccw too?
Not a must I think. But if you insist, I can remove them.
Thanks
>
> > ---
> > drivers/virtio/Kconfig | 6 ++++++
> > drivers/virtio/virtio.c | 2 ++
> > drivers/virtio/virtio_ring.c | 12 ++++++++++++
> > include/linux/virtio_config.h | 2 ++
> > 4 files changed, 22 insertions(+)
> >
> > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> > index b5adf6abd241..b059c39af3d4 100644
> > --- a/drivers/virtio/Kconfig
> > +++ b/drivers/virtio/Kconfig
> > @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
> >
> > if VIRTIO_MENU
> >
> > +config VIRTIO_HARDEN_NOTIFICATION
> > + bool "Harden virtio notification"
> > + help
> > + Enable to this to harden the device notifications and
> > + supress the ones that are illegal.
> > +
> > config VIRTIO_PCI
> > tristate "PCI driver for virtio devices"
> > depends on PCI
> > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> > index ef04a96942bf..21dc08d2f32d 100644
> > --- a/drivers/virtio/virtio.c
> > +++ b/drivers/virtio/virtio.c
> > @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> > * */
> > void virtio_reset_device(struct virtio_device *dev)
> > {
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > /*
> > * The below virtio_synchronize_cbs() guarantees that any
> > * interrupt for this line arriving after
> > @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> > */
> > virtio_break_device(dev);
> > virtio_synchronize_cbs(dev);
> > +#endif
> >
> > dev->config->reset(dev);
> > }
> > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > index 13a7348cedff..d9d3b6e201fb 100644
> > --- a/drivers/virtio/virtio_ring.c
> > +++ b/drivers/virtio/virtio_ring.c
> > @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > vq->we_own_ring = true;
> > vq->notify = notify;
> > vq->weak_barriers = weak_barriers;
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > vq->broken = true;
> > +#else
> > + vq->broken = false;
> > +#endif
> > vq->last_used_idx = 0;
> > vq->event_triggered = false;
> > vq->num_added = 0;
> > @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> > }
> >
> > if (unlikely(vq->broken)) {
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > dev_warn_once(&vq->vq.vdev->dev,
> > "virtio vring IRQ raised before DRIVER_OK");
> > return IRQ_NONE;
> > +#else
> > + return IRQ_HANDLED;
> > +#endif
> > }
> >
> > /* Just a hint for performance: so it's ok that this can be racy! */
> > @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> > vq->we_own_ring = false;
> > vq->notify = notify;
> > vq->weak_barriers = weak_barriers;
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > vq->broken = true;
> > +#else
> > + vq->broken = false;
> > +#endif
> > vq->last_used_idx = 0;
> > vq->event_triggered = false;
> > vq->num_added = 0;
> > diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> > index 9a36051ceb76..d15c3cdda2d2 100644
> > --- a/include/linux/virtio_config.h
> > +++ b/include/linux/virtio_config.h
> > @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
> >
> > WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
> >
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > /*
> > * The virtio_synchronize_cbs() makes sure vring_interrupt()
> > * will see the driver specific setup if it sees vq->broken
> > @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> > */
> > virtio_synchronize_cbs(dev);
> > __virtio_unbreak_device(dev);
> > +#endif
> > /*
> > * The transport should ensure the visibility of vq->broken
> > * before setting DRIVER_OK. See the comments for the transport
> > --
> > 2.25.1
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
@ 2022-06-17 11:47 ` Jason Wang
0 siblings, 0 replies; 10+ messages in thread
From: Jason Wang @ 2022-06-17 11:47 UTC (permalink / raw)
To: Michael S. Tsirkin; +Cc: linux-kernel, virtualization
On Fri, Jun 17, 2022 at 6:01 PM Michael S. Tsirkin <mst@redhat.com> wrote:
>
> On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> > We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> > harden vring IRQ"). It works with the assumption that the driver or
> > core can properly call virtio_device_ready() at the right
> > place. Unfortunately, this seems to be not true and uncover various
> > bugs of the existing drivers, mainly the issue of using
> > virtio_device_ready() incorrectly.
> >
> > So let's having a Kconfig option and disable it by default. It gives
> > us a breath to fix the drivers and then we can consider to enable it
> > by default.
> >
> > Signed-off-by: Jason Wang <jasowang@redhat.com>
>
>
> Do you want to drop irq_lock lock/unlock in ccw too?
Not a must I think. But if you insist, I can remove them.
Thanks
>
> > ---
> > drivers/virtio/Kconfig | 6 ++++++
> > drivers/virtio/virtio.c | 2 ++
> > drivers/virtio/virtio_ring.c | 12 ++++++++++++
> > include/linux/virtio_config.h | 2 ++
> > 4 files changed, 22 insertions(+)
> >
> > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> > index b5adf6abd241..b059c39af3d4 100644
> > --- a/drivers/virtio/Kconfig
> > +++ b/drivers/virtio/Kconfig
> > @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
> >
> > if VIRTIO_MENU
> >
> > +config VIRTIO_HARDEN_NOTIFICATION
> > + bool "Harden virtio notification"
> > + help
> > + Enable to this to harden the device notifications and
> > + supress the ones that are illegal.
> > +
> > config VIRTIO_PCI
> > tristate "PCI driver for virtio devices"
> > depends on PCI
> > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> > index ef04a96942bf..21dc08d2f32d 100644
> > --- a/drivers/virtio/virtio.c
> > +++ b/drivers/virtio/virtio.c
> > @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> > * */
> > void virtio_reset_device(struct virtio_device *dev)
> > {
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > /*
> > * The below virtio_synchronize_cbs() guarantees that any
> > * interrupt for this line arriving after
> > @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> > */
> > virtio_break_device(dev);
> > virtio_synchronize_cbs(dev);
> > +#endif
> >
> > dev->config->reset(dev);
> > }
> > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > index 13a7348cedff..d9d3b6e201fb 100644
> > --- a/drivers/virtio/virtio_ring.c
> > +++ b/drivers/virtio/virtio_ring.c
> > @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > vq->we_own_ring = true;
> > vq->notify = notify;
> > vq->weak_barriers = weak_barriers;
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > vq->broken = true;
> > +#else
> > + vq->broken = false;
> > +#endif
> > vq->last_used_idx = 0;
> > vq->event_triggered = false;
> > vq->num_added = 0;
> > @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> > }
> >
> > if (unlikely(vq->broken)) {
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > dev_warn_once(&vq->vq.vdev->dev,
> > "virtio vring IRQ raised before DRIVER_OK");
> > return IRQ_NONE;
> > +#else
> > + return IRQ_HANDLED;
> > +#endif
> > }
> >
> > /* Just a hint for performance: so it's ok that this can be racy! */
> > @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> > vq->we_own_ring = false;
> > vq->notify = notify;
> > vq->weak_barriers = weak_barriers;
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > vq->broken = true;
> > +#else
> > + vq->broken = false;
> > +#endif
> > vq->last_used_idx = 0;
> > vq->event_triggered = false;
> > vq->num_added = 0;
> > diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> > index 9a36051ceb76..d15c3cdda2d2 100644
> > --- a/include/linux/virtio_config.h
> > +++ b/include/linux/virtio_config.h
> > @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
> >
> > WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
> >
> > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > /*
> > * The virtio_synchronize_cbs() makes sure vring_interrupt()
> > * will see the driver specific setup if it sees vq->broken
> > @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> > */
> > virtio_synchronize_cbs(dev);
> > __virtio_unbreak_device(dev);
> > +#endif
> > /*
> > * The transport should ensure the visibility of vq->broken
> > * before setting DRIVER_OK. See the comments for the transport
> > --
> > 2.25.1
>
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
2022-06-17 11:47 ` Jason Wang
@ 2022-06-17 12:34 ` Michael S. Tsirkin
-1 siblings, 0 replies; 10+ messages in thread
From: Michael S. Tsirkin @ 2022-06-17 12:34 UTC (permalink / raw)
To: Jason Wang; +Cc: virtualization, linux-kernel
On Fri, Jun 17, 2022 at 07:47:21PM +0800, Jason Wang wrote:
> On Fri, Jun 17, 2022 at 6:01 PM Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> > > We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> > > harden vring IRQ"). It works with the assumption that the driver or
> > > core can properly call virtio_device_ready() at the right
> > > place. Unfortunately, this seems to be not true and uncover various
> > > bugs of the existing drivers, mainly the issue of using
> > > virtio_device_ready() incorrectly.
> > >
> > > So let's having a Kconfig option and disable it by default. It gives
> > > us a breath to fix the drivers and then we can consider to enable it
> > > by default.
> > >
> > > Signed-off-by: Jason Wang <jasowang@redhat.com>
> >
> >
> > Do you want to drop irq_lock lock/unlock in ccw too?
>
> Not a must I think. But if you insist, I can remove them.
>
> Thanks
With the ifdef. Seems safer ...
> >
> > > ---
> > > drivers/virtio/Kconfig | 6 ++++++
> > > drivers/virtio/virtio.c | 2 ++
> > > drivers/virtio/virtio_ring.c | 12 ++++++++++++
> > > include/linux/virtio_config.h | 2 ++
> > > 4 files changed, 22 insertions(+)
> > >
> > > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> > > index b5adf6abd241..b059c39af3d4 100644
> > > --- a/drivers/virtio/Kconfig
> > > +++ b/drivers/virtio/Kconfig
> > > @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
> > >
> > > if VIRTIO_MENU
> > >
> > > +config VIRTIO_HARDEN_NOTIFICATION
> > > + bool "Harden virtio notification"
> > > + help
> > > + Enable to this to harden the device notifications and
> > > + supress the ones that are illegal.
> > > +
> > > config VIRTIO_PCI
> > > tristate "PCI driver for virtio devices"
> > > depends on PCI
> > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> > > index ef04a96942bf..21dc08d2f32d 100644
> > > --- a/drivers/virtio/virtio.c
> > > +++ b/drivers/virtio/virtio.c
> > > @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> > > * */
> > > void virtio_reset_device(struct virtio_device *dev)
> > > {
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > /*
> > > * The below virtio_synchronize_cbs() guarantees that any
> > > * interrupt for this line arriving after
> > > @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> > > */
> > > virtio_break_device(dev);
> > > virtio_synchronize_cbs(dev);
> > > +#endif
> > >
> > > dev->config->reset(dev);
> > > }
> > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > > index 13a7348cedff..d9d3b6e201fb 100644
> > > --- a/drivers/virtio/virtio_ring.c
> > > +++ b/drivers/virtio/virtio_ring.c
> > > @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > > vq->we_own_ring = true;
> > > vq->notify = notify;
> > > vq->weak_barriers = weak_barriers;
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > vq->broken = true;
> > > +#else
> > > + vq->broken = false;
> > > +#endif
> > > vq->last_used_idx = 0;
> > > vq->event_triggered = false;
> > > vq->num_added = 0;
> > > @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> > > }
> > >
> > > if (unlikely(vq->broken)) {
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > dev_warn_once(&vq->vq.vdev->dev,
> > > "virtio vring IRQ raised before DRIVER_OK");
> > > return IRQ_NONE;
> > > +#else
> > > + return IRQ_HANDLED;
> > > +#endif
> > > }
> > >
> > > /* Just a hint for performance: so it's ok that this can be racy! */
> > > @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> > > vq->we_own_ring = false;
> > > vq->notify = notify;
> > > vq->weak_barriers = weak_barriers;
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > vq->broken = true;
> > > +#else
> > > + vq->broken = false;
> > > +#endif
> > > vq->last_used_idx = 0;
> > > vq->event_triggered = false;
> > > vq->num_added = 0;
> > > diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> > > index 9a36051ceb76..d15c3cdda2d2 100644
> > > --- a/include/linux/virtio_config.h
> > > +++ b/include/linux/virtio_config.h
> > > @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
> > >
> > > WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
> > >
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > /*
> > > * The virtio_synchronize_cbs() makes sure vring_interrupt()
> > > * will see the driver specific setup if it sees vq->broken
> > > @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> > > */
> > > virtio_synchronize_cbs(dev);
> > > __virtio_unbreak_device(dev);
> > > +#endif
> > > /*
> > > * The transport should ensure the visibility of vq->broken
> > > * before setting DRIVER_OK. See the comments for the transport
> > > --
> > > 2.25.1
> >
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
@ 2022-06-17 12:34 ` Michael S. Tsirkin
0 siblings, 0 replies; 10+ messages in thread
From: Michael S. Tsirkin @ 2022-06-17 12:34 UTC (permalink / raw)
To: Jason Wang; +Cc: linux-kernel, virtualization
On Fri, Jun 17, 2022 at 07:47:21PM +0800, Jason Wang wrote:
> On Fri, Jun 17, 2022 at 6:01 PM Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> > > We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> > > harden vring IRQ"). It works with the assumption that the driver or
> > > core can properly call virtio_device_ready() at the right
> > > place. Unfortunately, this seems to be not true and uncover various
> > > bugs of the existing drivers, mainly the issue of using
> > > virtio_device_ready() incorrectly.
> > >
> > > So let's having a Kconfig option and disable it by default. It gives
> > > us a breath to fix the drivers and then we can consider to enable it
> > > by default.
> > >
> > > Signed-off-by: Jason Wang <jasowang@redhat.com>
> >
> >
> > Do you want to drop irq_lock lock/unlock in ccw too?
>
> Not a must I think. But if you insist, I can remove them.
>
> Thanks
With the ifdef. Seems safer ...
> >
> > > ---
> > > drivers/virtio/Kconfig | 6 ++++++
> > > drivers/virtio/virtio.c | 2 ++
> > > drivers/virtio/virtio_ring.c | 12 ++++++++++++
> > > include/linux/virtio_config.h | 2 ++
> > > 4 files changed, 22 insertions(+)
> > >
> > > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> > > index b5adf6abd241..b059c39af3d4 100644
> > > --- a/drivers/virtio/Kconfig
> > > +++ b/drivers/virtio/Kconfig
> > > @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
> > >
> > > if VIRTIO_MENU
> > >
> > > +config VIRTIO_HARDEN_NOTIFICATION
> > > + bool "Harden virtio notification"
> > > + help
> > > + Enable to this to harden the device notifications and
> > > + supress the ones that are illegal.
> > > +
> > > config VIRTIO_PCI
> > > tristate "PCI driver for virtio devices"
> > > depends on PCI
> > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> > > index ef04a96942bf..21dc08d2f32d 100644
> > > --- a/drivers/virtio/virtio.c
> > > +++ b/drivers/virtio/virtio.c
> > > @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> > > * */
> > > void virtio_reset_device(struct virtio_device *dev)
> > > {
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > /*
> > > * The below virtio_synchronize_cbs() guarantees that any
> > > * interrupt for this line arriving after
> > > @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> > > */
> > > virtio_break_device(dev);
> > > virtio_synchronize_cbs(dev);
> > > +#endif
> > >
> > > dev->config->reset(dev);
> > > }
> > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > > index 13a7348cedff..d9d3b6e201fb 100644
> > > --- a/drivers/virtio/virtio_ring.c
> > > +++ b/drivers/virtio/virtio_ring.c
> > > @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > > vq->we_own_ring = true;
> > > vq->notify = notify;
> > > vq->weak_barriers = weak_barriers;
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > vq->broken = true;
> > > +#else
> > > + vq->broken = false;
> > > +#endif
> > > vq->last_used_idx = 0;
> > > vq->event_triggered = false;
> > > vq->num_added = 0;
> > > @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> > > }
> > >
> > > if (unlikely(vq->broken)) {
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > dev_warn_once(&vq->vq.vdev->dev,
> > > "virtio vring IRQ raised before DRIVER_OK");
> > > return IRQ_NONE;
> > > +#else
> > > + return IRQ_HANDLED;
> > > +#endif
> > > }
> > >
> > > /* Just a hint for performance: so it's ok that this can be racy! */
> > > @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> > > vq->we_own_ring = false;
> > > vq->notify = notify;
> > > vq->weak_barriers = weak_barriers;
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > vq->broken = true;
> > > +#else
> > > + vq->broken = false;
> > > +#endif
> > > vq->last_used_idx = 0;
> > > vq->event_triggered = false;
> > > vq->num_added = 0;
> > > diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> > > index 9a36051ceb76..d15c3cdda2d2 100644
> > > --- a/include/linux/virtio_config.h
> > > +++ b/include/linux/virtio_config.h
> > > @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
> > >
> > > WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
> > >
> > > +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> > > /*
> > > * The virtio_synchronize_cbs() makes sure vring_interrupt()
> > > * will see the driver specific setup if it sees vq->broken
> > > @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> > > */
> > > virtio_synchronize_cbs(dev);
> > > __virtio_unbreak_device(dev);
> > > +#endif
> > > /*
> > > * The transport should ensure the visibility of vq->broken
> > > * before setting DRIVER_OK. See the comments for the transport
> > > --
> > > 2.25.1
> >
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
2022-06-17 7:26 ` Jason Wang
@ 2022-06-17 12:36 ` Michael S. Tsirkin
-1 siblings, 0 replies; 10+ messages in thread
From: Michael S. Tsirkin @ 2022-06-17 12:36 UTC (permalink / raw)
To: Jason Wang; +Cc: virtualization, linux-kernel
On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> harden vring IRQ"). It works with the assumption that the driver or
> core can properly call virtio_device_ready() at the right
> place. Unfortunately, this seems to be not true and uncover various
> bugs of the existing drivers, mainly the issue of using
> virtio_device_ready() incorrectly.
>
> So let's having a Kconfig option and disable it by default. It gives
> us a breath to fix the drivers and then we can consider to enable it
> by default.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
> drivers/virtio/Kconfig | 6 ++++++
> drivers/virtio/virtio.c | 2 ++
> drivers/virtio/virtio_ring.c | 12 ++++++++++++
> include/linux/virtio_config.h | 2 ++
> 4 files changed, 22 insertions(+)
>
> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> index b5adf6abd241..b059c39af3d4 100644
> --- a/drivers/virtio/Kconfig
> +++ b/drivers/virtio/Kconfig
> @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
>
> if VIRTIO_MENU
>
> +config VIRTIO_HARDEN_NOTIFICATION
> + bool "Harden virtio notification"
> + help
> + Enable to this
Enable this
> to harden the
handling of
> device notifications and
> + supress the ones that are illegal.
Experimental: not all drivers handle this correctly at this point.
If unsure, say N.
> +
> config VIRTIO_PCI
> tristate "PCI driver for virtio devices"
> depends on PCI
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index ef04a96942bf..21dc08d2f32d 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> * */
> void virtio_reset_device(struct virtio_device *dev)
> {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The below virtio_synchronize_cbs() guarantees that any
> * interrupt for this line arriving after
> @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> */
> virtio_break_device(dev);
> virtio_synchronize_cbs(dev);
> +#endif
>
> dev->config->reset(dev);
> }
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 13a7348cedff..d9d3b6e201fb 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> vq->we_own_ring = true;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> }
>
> if (unlikely(vq->broken)) {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> dev_warn_once(&vq->vq.vdev->dev,
> "virtio vring IRQ raised before DRIVER_OK");
> return IRQ_NONE;
> +#else
> + return IRQ_HANDLED;
> +#endif
> }
>
> /* Just a hint for performance: so it's ok that this can be racy! */
> @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> vq->we_own_ring = false;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> index 9a36051ceb76..d15c3cdda2d2 100644
> --- a/include/linux/virtio_config.h
> +++ b/include/linux/virtio_config.h
> @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
>
> WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
>
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The virtio_synchronize_cbs() makes sure vring_interrupt()
> * will see the driver specific setup if it sees vq->broken
> @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> */
> virtio_synchronize_cbs(dev);
> __virtio_unbreak_device(dev);
> +#endif
> /*
> * The transport should ensure the visibility of vq->broken
> * before setting DRIVER_OK. See the comments for the transport
> --
> 2.25.1
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] virtio: disable notification hardening by default
@ 2022-06-17 12:36 ` Michael S. Tsirkin
0 siblings, 0 replies; 10+ messages in thread
From: Michael S. Tsirkin @ 2022-06-17 12:36 UTC (permalink / raw)
To: Jason Wang; +Cc: linux-kernel, virtualization
On Fri, Jun 17, 2022 at 03:26:26PM +0800, Jason Wang wrote:
> We try to harden virtio device notifications in 8b4ec69d7e09 ("virtio:
> harden vring IRQ"). It works with the assumption that the driver or
> core can properly call virtio_device_ready() at the right
> place. Unfortunately, this seems to be not true and uncover various
> bugs of the existing drivers, mainly the issue of using
> virtio_device_ready() incorrectly.
>
> So let's having a Kconfig option and disable it by default. It gives
> us a breath to fix the drivers and then we can consider to enable it
> by default.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
> drivers/virtio/Kconfig | 6 ++++++
> drivers/virtio/virtio.c | 2 ++
> drivers/virtio/virtio_ring.c | 12 ++++++++++++
> include/linux/virtio_config.h | 2 ++
> 4 files changed, 22 insertions(+)
>
> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> index b5adf6abd241..b059c39af3d4 100644
> --- a/drivers/virtio/Kconfig
> +++ b/drivers/virtio/Kconfig
> @@ -35,6 +35,12 @@ menuconfig VIRTIO_MENU
>
> if VIRTIO_MENU
>
> +config VIRTIO_HARDEN_NOTIFICATION
> + bool "Harden virtio notification"
> + help
> + Enable to this
Enable this
> to harden the
handling of
> device notifications and
> + supress the ones that are illegal.
Experimental: not all drivers handle this correctly at this point.
If unsure, say N.
> +
> config VIRTIO_PCI
> tristate "PCI driver for virtio devices"
> depends on PCI
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index ef04a96942bf..21dc08d2f32d 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -220,6 +220,7 @@ static int virtio_features_ok(struct virtio_device *dev)
> * */
> void virtio_reset_device(struct virtio_device *dev)
> {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The below virtio_synchronize_cbs() guarantees that any
> * interrupt for this line arriving after
> @@ -228,6 +229,7 @@ void virtio_reset_device(struct virtio_device *dev)
> */
> virtio_break_device(dev);
> virtio_synchronize_cbs(dev);
> +#endif
>
> dev->config->reset(dev);
> }
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 13a7348cedff..d9d3b6e201fb 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1688,7 +1688,11 @@ static struct virtqueue *vring_create_virtqueue_packed(
> vq->we_own_ring = true;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> @@ -2135,9 +2139,13 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
> }
>
> if (unlikely(vq->broken)) {
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> dev_warn_once(&vq->vq.vdev->dev,
> "virtio vring IRQ raised before DRIVER_OK");
> return IRQ_NONE;
> +#else
> + return IRQ_HANDLED;
> +#endif
> }
>
> /* Just a hint for performance: so it's ok that this can be racy! */
> @@ -2180,7 +2188,11 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> vq->we_own_ring = false;
> vq->notify = notify;
> vq->weak_barriers = weak_barriers;
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> vq->broken = true;
> +#else
> + vq->broken = false;
> +#endif
> vq->last_used_idx = 0;
> vq->event_triggered = false;
> vq->num_added = 0;
> diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> index 9a36051ceb76..d15c3cdda2d2 100644
> --- a/include/linux/virtio_config.h
> +++ b/include/linux/virtio_config.h
> @@ -257,6 +257,7 @@ void virtio_device_ready(struct virtio_device *dev)
>
> WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
>
> +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
> /*
> * The virtio_synchronize_cbs() makes sure vring_interrupt()
> * will see the driver specific setup if it sees vq->broken
> @@ -264,6 +265,7 @@ void virtio_device_ready(struct virtio_device *dev)
> */
> virtio_synchronize_cbs(dev);
> __virtio_unbreak_device(dev);
> +#endif
> /*
> * The transport should ensure the visibility of vq->broken
> * before setting DRIVER_OK. See the comments for the transport
> --
> 2.25.1
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2022-06-17 12:36 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-17 7:26 [PATCH] virtio: disable notification hardening by default Jason Wang
2022-06-17 7:26 ` Jason Wang
2022-06-17 10:01 ` Michael S. Tsirkin
2022-06-17 10:01 ` Michael S. Tsirkin
2022-06-17 11:47 ` Jason Wang
2022-06-17 11:47 ` Jason Wang
2022-06-17 12:34 ` Michael S. Tsirkin
2022-06-17 12:34 ` Michael S. Tsirkin
2022-06-17 12:36 ` Michael S. Tsirkin
2022-06-17 12:36 ` Michael S. Tsirkin
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.