Platform drivers invoke rproc_vq_interrupt() from hard-IRQ handlers,
threaded handlers, and work items. Because rpmsg callbacks may sleep,
the virtio core's synchronize_rcu() fallback does not synchronize with
callbacks across all these contexts. A device reset can therefore
complete while a callback is still running.

Add an SRCU domain per rproc. Protect both the queue lookup and
vring_interrupt() with it, and synchronize the domain in the new hook.

__rproc_virtio_del_vqs() can race with rproc_vq_interrupt() too. Clear
all queue pointers and synchronize the SRCU domain before freeing the
queues, so callers that already found a queue can finish using it.
Read rvring->vq once to avoid a second load after deletion starts.

The SRCU domain has the same lifetime as struct rproc. Its cleanup can
sleep, so document that rproc_free() and rproc_put() must not drop the
last reference from atomic context.

Assisted-by: LLM
Signed-off-by: Karl Mehltretter <[email protected]>
---
 drivers/remoteproc/remoteproc_core.c   | 12 ++++++++
 drivers/remoteproc/remoteproc_virtio.c | 37 +++++++++++++++++++++-----
 include/linux/remoteproc.h             |  3 +++
 3 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/drivers/remoteproc/remoteproc_core.c 
b/drivers/remoteproc/remoteproc_core.c
index f003be006b1b..6756f2fe4ec5 100644
--- a/drivers/remoteproc/remoteproc_core.c
+++ b/drivers/remoteproc/remoteproc_core.c
@@ -2367,6 +2367,7 @@ static void rproc_type_release(struct device *dev)
 
        dev_info(&rproc->dev, "releasing %s\n", rproc->name);
 
+       cleanup_srcu_struct(&rproc->vq_srcu);
        idr_destroy(&rproc->notifyids);
 
        if (rproc->index >= 0)
@@ -2464,6 +2465,11 @@ struct rproc *rproc_alloc(struct device *dev, const char 
*name,
        if (!rproc)
                return NULL;
 
+       if (init_srcu_struct(&rproc->vq_srcu)) {
+               kfree(rproc);
+               return NULL;
+       }
+
        rproc->priv = &rproc[1];
        rproc->auto_boot = true;
        rproc->elf_class = ELFCLASSNONE;
@@ -2526,6 +2532,9 @@ EXPORT_SYMBOL(rproc_alloc);
  *
  * If no one holds any reference to rproc anymore, then its refcount would
  * now drop to zero, and it would be freed.
+ *
+ * Context: Any context, but the last reference must not be dropped from
+ * atomic context.
  */
 void rproc_free(struct rproc *rproc)
 {
@@ -2541,6 +2550,9 @@ EXPORT_SYMBOL(rproc_free);
  *
  * If no one holds any reference to rproc anymore, then its refcount would
  * now drop to zero, and it would be freed.
+ *
+ * Context: Any context, but the last reference must not be dropped from
+ * atomic context.
  */
 void rproc_put(struct rproc *rproc)
 {
diff --git a/drivers/remoteproc/remoteproc_virtio.c 
b/drivers/remoteproc/remoteproc_virtio.c
index d5e9ff045a28..7fefb4bd7adc 100644
--- a/drivers/remoteproc/remoteproc_virtio.c
+++ b/drivers/remoteproc/remoteproc_virtio.c
@@ -23,6 +23,7 @@
 #include <linux/err.h>
 #include <linux/kref.h>
 #include <linux/slab.h>
+#include <linux/srcu.h>
 
 #include "remoteproc_internal.h"
 
@@ -88,15 +89,23 @@ 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);
 
+       idx = srcu_read_lock(&rproc->vq_srcu);
+
        rvring = idr_find(&rproc->notifyids, notifyid);
-       if (!rvring || !rvring->vq)
-               return IRQ_NONE;
+       vq = rvring ? READ_ONCE(rvring->vq) : NULL;
+       if (vq)
+               ret = vring_interrupt(0, vq);
 
-       return vring_interrupt(0, rvring->vq);
+       srcu_read_unlock(&rproc->vq_srcu, idx);
+
+       return ret;
 }
 EXPORT_SYMBOL(rproc_vq_interrupt);
 
@@ -153,7 +162,7 @@ static struct virtqueue *rp_find_vq(struct virtio_device 
*vdev,
 
        vq->num_max = num;
 
-       rvring->vq = vq;
+       WRITE_ONCE(rvring->vq, vq);
        vq->priv = rvring;
 
        /* Update vring in resource table */
@@ -165,14 +174,20 @@ static struct virtqueue *rp_find_vq(struct virtio_device 
*vdev,
 
 static void __rproc_virtio_del_vqs(struct virtio_device *vdev)
 {
+       struct rproc *rproc = vdev_to_rproc(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);
+               WRITE_ONCE(rvring->vq, NULL);
        }
+
+       /* Synchronize with rproc_vq_interrupt() callers that found a queue. */
+       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)
@@ -242,6 +257,13 @@ static void rproc_virtio_reset(struct virtio_device *vdev)
        dev_dbg(&vdev->dev, "reset !\n");
 }
 
+static void rproc_virtio_synchronize_cbs(struct virtio_device *vdev)
+{
+       struct rproc *rproc = vdev_to_rproc(vdev);
+
+       synchronize_srcu(&rproc->vq_srcu);
+}
+
 /* provide the vdev features as retrieved from the firmware */
 static u64 rproc_virtio_get_features(struct virtio_device *vdev)
 {
@@ -330,6 +352,7 @@ static const struct virtio_config_ops 
rproc_virtio_config_ops = {
        .find_vqs       = rproc_virtio_find_vqs,
        .del_vqs        = rproc_virtio_del_vqs,
        .reset          = rproc_virtio_reset,
+       .synchronize_cbs = rproc_virtio_synchronize_cbs,
        .set_status     = rproc_virtio_set_status,
        .get_status     = rproc_virtio_get_status,
        .get            = rproc_virtio_get,
diff --git a/include/linux/remoteproc.h b/include/linux/remoteproc.h
index 7c1546d48008..93a182b1868a 100644
--- a/include/linux/remoteproc.h
+++ b/include/linux/remoteproc.h
@@ -41,6 +41,7 @@
 #include <linux/cdev.h>
 #include <linux/completion.h>
 #include <linux/idr.h>
+#include <linux/srcu.h>
 #include <linux/of.h>
 #include <linux/rsc_table.h>
 
@@ -256,6 +257,7 @@ enum rproc_features {
  * @mappings: list of iommu mappings we initiated, needed on shutdown
  * @bootaddr: address of first instruction to boot rproc with (optional)
  * @rvdevs: list of remote virtio devices
+ * @vq_srcu: SRCU domain for the virtqueue callbacks of @rvdevs
  * @subdevs: list of subdevices, to following the running state
  * @notifyids: idr for dynamically assigning rproc-wide unique notify ids
  * @index: index of this rproc device
@@ -298,6 +300,7 @@ struct rproc {
        struct list_head mappings;
        u64 bootaddr;
        struct list_head rvdevs;
+       struct srcu_struct vq_srcu;
        struct list_head subdevs;
        struct idr notifyids;
        int index;
-- 
2.39.5 (Apple Git-154)

Reply via email to