]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
firmware: qcom: scm: initialize tzmem before marking SCM as available
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 30 Jun 2025 12:12:04 +0000 (14:12 +0200)
committerBjorn Andersson <andersson@kernel.org>
Thu, 17 Jul 2025 03:50:55 +0000 (22:50 -0500)
Now that qcom_scm_shm_bridge_enable() uses the struct device passed to
it as argument to make the QCOM_SCM_MP_SHM_BRIDGE_ENABLE SCM call, we
can move the TZMem initialization before the assignment of the __scm
pointer in the SCM driver (which marks SCM as ready to users) thus
fixing the potential race between consumer calls and the memory pool
initialization.

Reported-by: Johan Hovold <johan+linaro@kernel.org>
Closes: https://lore.kernel.org/all/20250120151000.13870-1-johan+linaro@kernel.org/
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Link: https://lore.kernel.org/r/20250630-qcom-scm-race-v2-3-fa3851c98611@linaro.org
Signed-off-by: Bjorn Andersson <andersson@kernel.org>
drivers/firmware/qcom/qcom_scm.c

index 09b698b9021644660468d59bef496cd7859aec7f..b6e0420bb2b72247e4d772b66314d81f336f5d9d 100644 (file)
@@ -2256,7 +2256,32 @@ static int qcom_scm_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
-       /* Paired with smp_load_acquire() in qcom_scm_is_available(). */
+       ret = of_reserved_mem_device_init(scm->dev);
+       if (ret && ret != -ENODEV)
+               return dev_err_probe(scm->dev, ret,
+                                    "Failed to setup the reserved memory region for TZ mem\n");
+
+       ret = qcom_tzmem_enable(scm->dev);
+       if (ret)
+               return dev_err_probe(scm->dev, ret,
+                                    "Failed to enable the TrustZone memory allocator\n");
+
+       memset(&pool_config, 0, sizeof(pool_config));
+       pool_config.initial_size = 0;
+       pool_config.policy = QCOM_TZMEM_POLICY_ON_DEMAND;
+       pool_config.max_size = SZ_256K;
+
+       scm->mempool = devm_qcom_tzmem_pool_new(scm->dev, &pool_config);
+       if (IS_ERR(scm->mempool))
+               return dev_err_probe(scm->dev, PTR_ERR(scm->mempool),
+                                    "Failed to create the SCM memory pool\n");
+
+       /*
+        * Paired with smp_load_acquire() in qcom_scm_is_available().
+        *
+        * This marks the SCM API as ready to accept user calls and can only
+        * be called after the TrustZone memory pool is initialized.
+        */
        smp_store_release(&__scm, scm);
 
        irq = platform_get_irq_optional(pdev, 0);
@@ -2289,32 +2314,6 @@ static int qcom_scm_probe(struct platform_device *pdev)
        if (of_property_read_bool(pdev->dev.of_node, "qcom,sdi-enabled") || !download_mode)
                qcom_scm_disable_sdi();
 
-       ret = of_reserved_mem_device_init(__scm->dev);
-       if (ret && ret != -ENODEV) {
-               dev_err_probe(__scm->dev, ret,
-                             "Failed to setup the reserved memory region for TZ mem\n");
-               goto err;
-       }
-
-       ret = qcom_tzmem_enable(__scm->dev);
-       if (ret) {
-               dev_err_probe(__scm->dev, ret,
-                             "Failed to enable the TrustZone memory allocator\n");
-               goto err;
-       }
-
-       memset(&pool_config, 0, sizeof(pool_config));
-       pool_config.initial_size = 0;
-       pool_config.policy = QCOM_TZMEM_POLICY_ON_DEMAND;
-       pool_config.max_size = SZ_256K;
-
-       __scm->mempool = devm_qcom_tzmem_pool_new(__scm->dev, &pool_config);
-       if (IS_ERR(__scm->mempool)) {
-               ret = dev_err_probe(__scm->dev, PTR_ERR(__scm->mempool),
-                                   "Failed to create the SCM memory pool\n");
-               goto err;
-       }
-
        /*
         * Initialize the QSEECOM interface.
         *