remoteproc: virtio: Synchronize virtqueue teardown with rproc_vq_interrupt()
Subsystems (2)
PATCH v2 1/1 remoteproc: virtio: Synchronize virtqueue teardown with rproc_vq_interrupt()
rproc_vq_interrupt() looks up rvring->vq and passes it to vring_interrupt() without synchronizing against __rproc_virtio_del_vqs(), which clears rvring->vq and frees the virtqueue. rproc_stop() and __rproc_detach() stop the subdevices, and so delete the virtqueues, before the remote processor is stopped or detached, so the remote can still signal a vring meanwhile. The interrupt path can then pass a NULL or already freed virtqueue to vring_interrupt().
Protect rvring->vq with SRCU: rproc_vq_interrupt() uses the virtqueue inside a read-side section, and __rproc_virtio_del_vqs() clears the pointers and waits for readers before freeing the virtqueues. Plain RCU is not usable because rproc_vq_interrupt() is also called from process context, where vring callbacks may sleep.
---
Changes in v2:
- Rework the fix: v1 only loaded rvring->vq once, which closes the NULL window but not the use-after-free once the virtqueue is freed after the load (Mathieu Poirier). Use SRCU so __rproc_virtio_del_vqs() waits for rproc_vq_interrupt() before freeing the virtqueues.
- Update the subject accordingly.
v1: https://lore.kernel.org/all/[email protected]/
Compile-tested only (arm64 and x86_64 defconfig + REMOTEPROC/RPMSG_VIRTIO, W=1, sparse).
drivers/remoteproc/remoteproc_virtio.c | 35 +++++++++++++++++++++----- include/linux/remoteproc.h | 2 +- 2 files changed, 30 insertions(+), 7 deletions(-)
drivers/remoteproc/remoteproc_virtio.c +29 -6
@@ -16,6 +16,7 @@#include <linux/of_reserved_mem.h> #include <linux/platform_device.h> #include <linux/remoteproc.h> +#include <linux/srcu.h> #include <linux/virtio.h> #include <linux/virtio_config.h> #include <linux/virtio_ids.h>
@@ -26,6 +27,14 @@#include "remoteproc_internal.h" +/* +* Protects rproc_vring->vq: rproc_vq_interrupt() uses the virtqueue inside a +* read-side section, and __rproc_virtio_del_vqs() waits for those sections to +* finish before freeing it. Sleepable RCU is used because vring callbacks may +* sleep when rproc_vq_interrupt() is called from process context. +*/ +DEFINE_STATIC_SRCU (rproc_vq_srcu); +static int copy_dma_range_map (struct device *to,struct device *from) {const struct bus_dma_region *map = from->dma_range_map, *new_map, *r;
@@ -88,15 +97,24 @@ static bool rproc_virtio_notify(struct virtqueue *vq)*/ irqreturn_t rproc_vq_interrupt (struct rproc *rproc,int notifyid) { +irqreturn_t ret =IRQ_NONE ;struct rproc_vring *rvring; +struct virtqueue *vq; +int idx;dev_dbg (&rproc->dev,"vq index %d is interrupted\n" , notifyid); rvring =idr_find (&rproc->notifyids, notifyid); -if (!rvring || !rvring->vq) +if (!rvring)return IRQ_NONE ; -return vring_interrupt (0 , rvring->vq); + idx =srcu_read_lock (&rproc_vq_srcu); + vq =srcu_dereference (rvring->vq, &rproc_vq_srcu); +if (vq) + ret =vring_interrupt (0 , vq); +srcu_read_unlock (&rproc_vq_srcu, idx); + +return ret; }EXPORT_SYMBOL (rproc_vq_interrupt);
@@ -153,8 +171,8 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev, vq->num_max = num; - rvring->vq = vq; vq->priv = rvring; +rcu_assign_pointer (rvring->vq, vq);/* Update vring in resource table */ rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
@@ -168,11 +186,16 @@ static void __rproc_virtio_del_vqs(struct virtio_device *vdev)struct virtqueue *vq, *n;struct rproc_vring *rvring; -list_for_each_entry_safe (vq, n, &vdev->vqs, list) { +list_for_each_entry (vq, &vdev->vqs, list) { rvring = vq->priv; - rvring->vq =NULL ; -vring_del_virtqueue (vq); +RCU_INIT_POINTER (rvring->vq,NULL ); } + +/* Wait for rproc_vq_interrupt() callers still using the virtqueues */ +synchronize_srcu (&rproc_vq_srcu); + +list_for_each_entry_safe (vq, n, &vdev->vqs, list) +vring_del_virtqueue (vq); }static void rproc_virtio_del_vqs (struct virtio_device *vdev)
include/linux/remoteproc.h +1 -1
@@ -340,7 +340,7 @@ struct rproc_vring {u32 align;int notifyid;struct rproc_vdev *rvdev; -struct virtqueue *vq; +struct virtqueue __rcu *vq; };/**
base-commit: aa98230e410f0ed212b6788c46b1e4d49e0ff7ca
Signature · 1 line
2.43.0
Mathieu Poirier
maintainerQuoted 99 lines · Yuho Choi
rproc_vq_interrupt()looks uprvring->vqand passes it tovring_interrupt()without synchronizing against__rproc_virtio_del_vqs(), which clearsrvring->vqand frees the virtqueue.rproc_stop()and__rproc_detach()stop the subdevices, and so delete the virtqueues, before the remote processor is stopped or detached, so the remote can still signal a vring meanwhile. The interrupt path can then pass a NULL or already freed virtqueue tovring_interrupt().Protect
rvring->vqwith SRCU:rproc_vq_interrupt()uses the virtqueue inside a read-side section, and__rproc_virtio_del_vqs()clears the pointers and waits for readers before freeing the virtqueues. Plain RCU is not usable becauserproc_vq_interrupt()is also called from process context, where vring callbacks may sleep.Fixes 7a186941626d ("remoteproc: remove the single rpmsg vdev limitation")Signed-off-by Yuho Choi <[email protected]>---
Changes in v2:
- Rework the fix: v1 only loadedrvring->vqonce, which closes the NULL window but not the use-after-free once the virtqueue is freed after the load (Mathieu Poirier). Use SRCU so__rproc_virtio_del_vqs()waits forrproc_vq_interrupt()before freeing the virtqueues.
- Update the subject accordingly.v1: https://lore.kernel.org/all/[email protected]/
Compile-tested only (arm64 and
x86_64defconfig + REMOTEPROC/RPMSG_VIRTIO, W=1, sparse).drivers/remoteproc/remoteproc_virtio.c | 35 +++++++++++++++++++++----- include/linux/remoteproc.h | 2 +- 2 files changed, 30 insertions(+), 7 deletions(-)drivers/remoteproc/remoteproc_virtio.c +21
-3@@ -16,6 +16,7 @@#include <linux/of_reserved_mem.h> #include <linux/platform_device.h> #include <linux/remoteproc.h> +#include <linux/srcu.h> #include <linux/virtio.h> #include <linux/virtio_config.h> #include <linux/virtio_ids.h> @@ -26,6 +27,14 @@#include "remoteproc_internal.h" +/* +* Protects rproc_vring->vq: rproc_vq_interrupt() uses the virtqueue inside a +* read-side section, and __rproc_virtio_del_vqs() waits for those sections to +* finish before freeing it. Sleepable RCU is used because vring callbacks may +* sleep when rproc_vq_interrupt() is called from process context. +*/ +DEFINE_STATIC_SRCU (rproc_vq_srcu); +static int copy_dma_range_map (struct device *to,struct device *from) {const struct bus_dma_region *map = from->dma_range_map, *new_map, *r;@@ -88,15 +97,24 @@ static bool rproc_virtio_notify(struct virtqueue *vq)*/ irqreturn_t rproc_vq_interrupt (struct rproc *rproc,int notifyid) { +irqreturn_t ret =IRQ_NONE ;struct rproc_vring *rvring; +struct virtqueue *vq; +int idx;dev_dbg (&rproc->dev,"vq index %d is interrupted\n" , notifyid); rvring =idr_find (&rproc->notifyids, notifyid); -if (!rvring || !rvring->vq) +if (!rvring)return IRQ_NONE ; -return vring_interrupt (0 , rvring->vq); + idx =srcu_read_lock (&rproc_vq_srcu); + vq =srcu_dereference (rvring->vq, &rproc_vq_srcu); +if (vq) + ret =vring_interrupt (0 , vq); +srcu_read_unlock (&rproc_vq_srcu, idx); + +return ret; }EXPORT_SYMBOL (rproc_vq_interrupt);@@ -153,8 +171,8 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev, vq->num_max = num; - rvring->vq = vq; vq->priv = rvring; +rcu_assign_pointer (rvring->vq, vq);/* Update vring in resource table */ rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
drivers/remoteproc/remoteproc_virtio.c static void __rproc_virtio_del_vqs(struct virtio_device *vdev) · line 189
Quoted 5 lines
drivers/remoteproc/remoteproc_virtio.c +1
-1@@ -168,11 +186,16 @@ static void __rproc_virtio_del_vqs(struct virtio_device *vdev)struct virtqueue *vq, *n;struct rproc_vring *rvring; -list_for_each_entry_safe (vq, n, &vdev->vqs, list) { +list_for_each_entry (vq, &vdev->vqs, list) {
How is list_for_each_entry_safe() no longer needed?
Quoted 32 lines
drivers/remoteproc/remoteproc_virtio.c +7
-2rvring = vq->priv; - rvring->vq =NULL ; -vring_del_virtqueue (vq); +RCU_INIT_POINTER (rvring->vq,NULL ); } + +/* Wait for rproc_vq_interrupt() callers still using the virtqueues */ +synchronize_srcu (&rproc_vq_srcu); + +list_for_each_entry_safe (vq, n, &vdev->vqs, list) +vring_del_virtqueue (vq); }static void rproc_virtio_del_vqs (struct virtio_device *vdev)include/linux/remoteproc.h +1
-1@@ -340,7 +340,7 @@ struct rproc_vring {u32 align;int notifyid;struct rproc_vdev *rvdev; -struct virtqueue *vq; +struct virtqueue __rcu *vq; };/** base-commit:
aa98230e410f0ed212b6788c46b1e4d49e0ff7caSignature · 1 line
2.43.0
Quoted 5 lines · Mathieu Poirier
drivers/remoteproc/remoteproc_virtio.c static void __rproc_virtio_del_vqs(struct virtio_device *vdev) · line 189
Quoted 3 lines
drivers/remoteproc/remoteproc_virtio.c +1
-1-list_for_each_entry_safe (vq, n, &vdev->vqs, list) { +list_for_each_entry (vq, &vdev->vqs, list) {How is
list_for_each_entry_safe()no longer needed?
The first loop only clears the pointers with RCU_INIT_POINTER(). It does not modify the list, so the 'safe' variant is not needed there. synchronize_srcu() is
then called once, after that loop, to wait for in-flight readers. The actual removal
still happens in the second loop, which keeps list_for_each_entry_safe().
Mathieu Poirier
maintainerQuoted 5 lines · Yuho Choi
Quoted 4 lines · Mathieu Poirier
Quoted 3 lines
drivers/remoteproc/remoteproc_virtio.c +1
-1-list_for_each_entry_safe (vq, n, &vdev->vqs, list) { +list_for_each_entry (vq, &vdev->vqs, list) {
Quoted 8 lines
Quoted 1 line
How is
list_for_each_entry_safe()no longer needed?The first loop only clears the pointers with
RCU_INIT_POINTER(). It does not modify the list, so the 'safe' variant is not needed there.synchronize_srcu()is
then called once, after that loop, to wait for in-flight readers. The actual removal
still happens in the second loop, which keepslist_for_each_entry_safe().
I agree with your assessment. I will merge this patch patch when 7.4-rc1 comes out.
Thanks,
Mathieu
Mathieu Poirier
maintainerHi,
This patch doesn't apply on rproc-next - please rebase and resent.
Thanks,
Mathieu
Quoted 136 lines · Yuho Choi
rproc_vq_interrupt()looks uprvring->vqand passes it tovring_interrupt()without synchronizing against__rproc_virtio_del_vqs(), which clearsrvring->vqand frees the virtqueue.rproc_stop()and__rproc_detach()stop the subdevices, and so delete the virtqueues, before the remote processor is stopped or detached, so the remote can still signal a vring meanwhile. The interrupt path can then pass a NULL or already freed virtqueue tovring_interrupt().Protect
rvring->vqwith SRCU:rproc_vq_interrupt()uses the virtqueue inside a read-side section, and__rproc_virtio_del_vqs()clears the pointers and waits for readers before freeing the virtqueues. Plain RCU is not usable becauserproc_vq_interrupt()is also called from process context, where vring callbacks may sleep.Fixes 7a186941626d ("remoteproc: remove the single rpmsg vdev limitation")Signed-off-by Yuho Choi <[email protected]>---
Changes in v2:
- Rework the fix: v1 only loadedrvring->vqonce, which closes the NULL window but not the use-after-free once the virtqueue is freed after the load (Mathieu Poirier). Use SRCU so__rproc_virtio_del_vqs()waits forrproc_vq_interrupt()before freeing the virtqueues.
- Update the subject accordingly.v1: https://lore.kernel.org/all/[email protected]/
Compile-tested only (arm64 and
x86_64defconfig + REMOTEPROC/RPMSG_VIRTIO, W=1, sparse).drivers/remoteproc/remoteproc_virtio.c | 35 +++++++++++++++++++++----- include/linux/remoteproc.h | 2 +- 2 files changed, 30 insertions(+), 7 deletions(-)drivers/remoteproc/remoteproc_virtio.c +29
-6@@ -16,6 +16,7 @@#include <linux/of_reserved_mem.h> #include <linux/platform_device.h> #include <linux/remoteproc.h> +#include <linux/srcu.h> #include <linux/virtio.h> #include <linux/virtio_config.h> #include <linux/virtio_ids.h> @@ -26,6 +27,14 @@#include "remoteproc_internal.h" +/* +* Protects rproc_vring->vq: rproc_vq_interrupt() uses the virtqueue inside a +* read-side section, and __rproc_virtio_del_vqs() waits for those sections to +* finish before freeing it. Sleepable RCU is used because vring callbacks may +* sleep when rproc_vq_interrupt() is called from process context. +*/ +DEFINE_STATIC_SRCU (rproc_vq_srcu); +static int copy_dma_range_map (struct device *to,struct device *from) {const struct bus_dma_region *map = from->dma_range_map, *new_map, *r;@@ -88,15 +97,24 @@ static bool rproc_virtio_notify(struct virtqueue *vq)*/ irqreturn_t rproc_vq_interrupt (struct rproc *rproc,int notifyid) { +irqreturn_t ret =IRQ_NONE ;struct rproc_vring *rvring; +struct virtqueue *vq; +int idx;dev_dbg (&rproc->dev,"vq index %d is interrupted\n" , notifyid); rvring =idr_find (&rproc->notifyids, notifyid); -if (!rvring || !rvring->vq) +if (!rvring)return IRQ_NONE ; -return vring_interrupt (0 , rvring->vq); + idx =srcu_read_lock (&rproc_vq_srcu); + vq =srcu_dereference (rvring->vq, &rproc_vq_srcu); +if (vq) + ret =vring_interrupt (0 , vq); +srcu_read_unlock (&rproc_vq_srcu, idx); + +return ret; }EXPORT_SYMBOL (rproc_vq_interrupt);@@ -153,8 +171,8 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev, vq->num_max = num; - rvring->vq = vq; vq->priv = rvring; +rcu_assign_pointer (rvring->vq, vq);/* Update vring in resource table */ rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;@@ -168,11 +186,16 @@ static void __rproc_virtio_del_vqs(struct virtio_device *vdev)struct virtqueue *vq, *n;struct rproc_vring *rvring; -list_for_each_entry_safe (vq, n, &vdev->vqs, list) { +list_for_each_entry (vq, &vdev->vqs, list) { rvring = vq->priv; - rvring->vq =NULL ; -vring_del_virtqueue (vq); +RCU_INIT_POINTER (rvring->vq,NULL ); } + +/* Wait for rproc_vq_interrupt() callers still using the virtqueues */ +synchronize_srcu (&rproc_vq_srcu); + +list_for_each_entry_safe (vq, n, &vdev->vqs, list) +vring_del_virtqueue (vq); }static void rproc_virtio_del_vqs (struct virtio_device *vdev)include/linux/remoteproc.h +1
-1@@ -340,7 +340,7 @@ struct rproc_vring {u32 align;int notifyid;struct rproc_vdev *rvdev; -struct virtqueue *vq; +struct virtqueue __rcu *vq; };/** base-commit:
aa98230e410f0ed212b6788c46b1e4d49e0ff7caSignature · 1 line
2.43.0
Quoted 1 line · Mathieu Poirier
This patch doesn't apply on rproc-next - please rebase and resent.
Thanks for checking, Mathieu. It no longer applies because the same race has already been fixed by commit ffe14d610cb0 ("remoteproc:
implement synchronize_cbs() for virtio devices"), which is in v7.4-rc1. Therefore, this patch should be dropped.
Thanks,
Yuho