The memory of an exported dma-buf, of an invoke context and of the init,
map and mmap buffers of a fastrpc_user is allocated against a compute-cb
device. Userspace may release it only after the channel has been removed,
e.g. when a fastrpc daemon closes its dma-bufs in response to the remote
processor crashing, but fastrpc_rpmsg_remove() destroys the compute-cb
devices right away. Once a device is deleted, iommu_deinit_device()
frees its IOMMU domain and clears its dma_iommu flag, so the later
dma_free_coherent() is handed a freed device:

  Unable to handle kernel paging request at virtual address 0000d4d5dddf6748
  ...
  CPU: 7 UID: 0 PID: 735 Comm: cdsprpcd ...
  pc : dma_free_attrs+0xbc/0xfc
  lr : dma_free_attrs+0x50/0xfc
  Call trace:
   dma_free_attrs+0xbc/0xfc (P)
   fastrpc_release+0x28/0x40 [fastrpc]
   dma_buf_release+0x40/0xa0
   dentry_kill+0xec/0x2d4
   finish_dput+0x1c/0x90
   dput+0x3c/0x40
   __fput+0x110/0x2f0
   fput_close_sync+0x40/0x100
   __arm64_sys_close+0x38/0x7c

A reference on the device would not help, as the IOMMU domain goes away
at device_del() regardless, and the buffers can't be freed at channel
removal either, as userspace may still have them mapped.

Keep the compute-cb devices for as long as the module is loaded instead.
Create them without a parent, so that they don't hang off the rpmsg
device, reuse them on a later probe of the channel, and only invalidate
the sessions on removal. Destroy them at module exit, by which time the
exported dma-bufs and open fastrpc files, which pin the module, are all
gone. A device that failed DMA/IOMMU configuration is still destroyed
straight away, as nothing can have used it.

The issue is seen on Nord Ride by crashing a CDSP while cdsprpcd holds
a fastrpc dma-buf.

Signed-off-by: Shawn Guo <[email protected]>
---
 drivers/misc/fastrpc.c | 71 +++++++++++++++++++++++++++++-------------
 1 file changed, 50 insertions(+), 21 deletions(-)

diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index 47cbfd4ba706..d387de21fd46 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -2431,9 +2431,9 @@ static const struct file_operations fastrpc_fops = {
        .compat_ioctl = fastrpc_device_ioctl,
 };
 
-static int fastrpc_cb_init(struct platform_device *pdev)
+static int fastrpc_cb_init(struct fastrpc_channel_ctx *cctx,
+                          struct platform_device *pdev)
 {
-       struct fastrpc_channel_ctx *cctx;
        struct fastrpc_session_ctx *sess;
        struct device *dev = &pdev->dev;
        unsigned long flags;
@@ -2441,10 +2441,6 @@ static int fastrpc_cb_init(struct platform_device *pdev)
        u32 sid = 0;
        int rc;
 
-       cctx = dev_get_drvdata(dev->parent);
-       if (!cctx)
-               return -EINVAL;
-
        if (of_property_read_u32(dev->of_node, "reg", &sid))
                dev_info(dev, "FastRPC Session ID not specified in DT\n");
 
@@ -2474,10 +2470,8 @@ static int fastrpc_cb_init(struct platform_device *pdev)
        return 0;
 }
 
-static void fastrpc_cb_devices_destroy(struct rpmsg_device *rpdev)
+static void fastrpc_cb_sessions_invalidate(struct fastrpc_channel_ctx *cctx)
 {
-       struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
-       struct device *rdev = &rpdev->dev;
        unsigned long flags;
        int i;
 
@@ -2485,12 +2479,21 @@ static void fastrpc_cb_devices_destroy(struct 
rpmsg_device *rpdev)
        for (i = 0; i < cctx->sesscount; i++)
                cctx->session[i].valid = false;
        spin_unlock_irqrestore(&cctx->lock, flags);
-
-       device_for_each_child_reverse(rdev, NULL, of_platform_device_destroy);
 }
 
+/*
+ * Buffers allocated against a CB device, e.g. an exported dma-buf or the
+ * init memory of a fastrpc_user, can outlive the channel that created the
+ * device: userspace may keep them across a remoteproc restart and only
+ * release them afterwards. Freeing them takes the device's IOMMU domain,
+ * which is torn down once the device is deleted, so a CB device is never
+ * destroyed along with its channel. It is created by the first probe of
+ * the channel, without a parent so that it doesn't hang off the rpmsg
+ * device, reused by every later probe and destroyed at module exit only.
+ */
 static int fastrpc_cb_devices_create(struct rpmsg_device *rpdev)
 {
+       struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
        struct device *rdev = &rpdev->dev;
        struct platform_device *pdev;
        struct device_node *np;
@@ -2500,10 +2503,16 @@ static int fastrpc_cb_devices_create(struct 
rpmsg_device *rpdev)
                if (!of_device_is_compatible(np, "qcom,fastrpc-compute-cb"))
                        continue;
 
-               pdev = of_platform_device_create(np, NULL, rdev);
+               pdev = of_find_device_by_node(np);
+               if (pdev) {
+                       /* Still registered, so the reference isn't needed */
+                       put_device(&pdev->dev);
+                       goto init;
+               }
+
+               pdev = of_platform_device_create(np, NULL, NULL);
                if (!pdev) {
                        of_node_put(np);
-                       fastrpc_cb_devices_destroy(rpdev);
                        return -ENODEV;
                }
 
@@ -2521,22 +2530,20 @@ static int fastrpc_cb_devices_create(struct 
rpmsg_device *rpdev)
                    !device_iommu_mapped(&pdev->dev))
                        err = -ENODEV;
                if (err) {
+                       of_platform_device_destroy(&pdev->dev, NULL);
                        if (err == -EPROBE_DEFER) {
                                of_node_put(np);
-                               fastrpc_cb_devices_destroy(rpdev);
                                return err;
                        }
 
                        dev_err(rdev, "skipping %pOF: failed to configure 
DMA/IOMMU: %d\n",
                                np, err);
-                       of_platform_device_destroy(&pdev->dev, NULL);
                        continue;
                }
-
-               err = fastrpc_cb_init(pdev);
+init:
+               err = fastrpc_cb_init(cctx, pdev);
                if (err) {
                        of_node_put(np);
-                       fastrpc_cb_devices_destroy(rpdev);
                        return err;
                }
        }
@@ -2544,6 +2551,14 @@ static int fastrpc_cb_devices_create(struct rpmsg_device 
*rpdev)
        return 0;
 }
 
+static int fastrpc_cb_device_destroy(struct device *dev, void *data)
+{
+       if (of_device_is_compatible(dev->of_node, "qcom,fastrpc-compute-cb"))
+               of_platform_device_destroy(dev, NULL);
+
+       return 0;
+}
+
 static int fastrpc_device_register(struct device *dev, struct 
fastrpc_channel_ctx *cctx,
                                   bool is_secured, const char *domain)
 {
@@ -2911,7 +2926,7 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 
        err = fastrpc_cb_devices_create(rpdev);
        if (err)
-               goto err_deregister_fdev;
+               goto err_invalidate_sessions;
 
        if (data->domain_id == ADSP_DOMAIN_ID && data->sesscount > 0) {
                struct fastrpc_session_ctx *last_sess;
@@ -2930,6 +2945,8 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 
        return 0;
 
+err_invalidate_sessions:
+       fastrpc_cb_sessions_invalidate(data);
 err_deregister_fdev:
        if (data->fdevice)
                misc_deregister(&data->fdevice->miscdev);
@@ -2978,7 +2995,7 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device 
*rpdev)
 
        fastrpc_remote_heap_release(cctx, rpdev);
 
-       fastrpc_cb_devices_destroy(rpdev);
+       fastrpc_cb_sessions_invalidate(cctx);
 
        fastrpc_channel_ctx_put(cctx);
 }
@@ -3065,7 +3082,19 @@ static struct rpmsg_driver fastrpc_driver = {
        },
 };
 
-module_rpmsg_driver(fastrpc_driver);
+static int __init fastrpc_init(void)
+{
+       return register_rpmsg_driver(&fastrpc_driver);
+}
+module_init(fastrpc_init);
+
+static void __exit fastrpc_exit(void)
+{
+       unregister_rpmsg_driver(&fastrpc_driver);
+       /* Exported dma-bufs and open fastrpc files pin the module, none is 
left */
+       device_for_each_child_reverse(&platform_bus, NULL, 
fastrpc_cb_device_destroy);
+}
+module_exit(fastrpc_exit);
 
 MODULE_DESCRIPTION("Qualcomm FastRPC");
 MODULE_LICENSE("GPL v2");
-- 
2.43.0

Reply via email to