Skip to contentSkip to conversation

optee: fix OOM seen due to tee_shm_free()

PATCH Superseded Allen Pais 4 participants 10 messages Feb 17, 2021 – Feb 24, 2021

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Allen Pais

author
#0 lore raw

cover optee: fix OOM seen due to tee_shm_free()

From: Allen Pais <[email protected]>

The following out of memory errors are seen on kexec reboot from the optee core.

[    0.368428] tee_bnxt_fw optee-clnt0: tee_shm_alloc failed
[    0.368461] tee_bnxt_fw: probe of optee-clnt0 failed with error -22

tee_shm_release() is not invoked on dma shm buffer.

Implement .shutdown() in optee core as well as bnxt firmware driver to handle the release of the buffers correctly.

More info:
https://github.com/OP-TEE/optee_os/issues/3637

Allen Pais (2):
optee: fix tee out of memory failure seen during kexec reboot
firmware: tee_bnxt: implement shutdown method to handle kexec reboots

 drivers/firmware/broadcom/tee_bnxt_fw.c |  9 ++++
 drivers/tee/optee/core.c                | 69 ++++++++++++++++++-------
 2 files changed, 58 insertions(+), 20 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/2 optee: fix tee out of memory failure seen during kexec reboot

From: Allen Pais <[email protected]>

The following out of memory errors are seen on kexec reboot from the optee core.

[    0.368428] tee_bnxt_fw optee-clnt0: tee_shm_alloc failed
[    0.368461] tee_bnxt_fw: probe of optee-clnt0 failed with error -22

tee_shm_release() is not invoked on dma shm buffer.

Implement .shutdown() method to handle the release of the buffers correctly.

More info:
https://github.com/OP-TEE/optee_os/issues/3637

---
 drivers/tee/optee/core.c | 69 ++++++++++++++++++++++++++++------------
 1 file changed, 49 insertions(+), 20 deletions(-)
drivers/tee/optee/core.c +49 -20
index cf4718c6d35d..b402e5eace7b 100644
@@ -582,36 +582,64 @@ static optee_invoke_fn *get_invoke_func(struct device *dev)
 	return ERR_PTR(-EINVAL);
 }
 
-static int optee_remove(struct platform_device *pdev)
+static int __optee_shutoff(struct platform_device *pdev, bool shutdown)
 {
 	struct optee *optee = platform_get_drvdata(pdev);
 
-	/*
-	 * Ask OP-TEE to free all cached shared memory objects to decrease
-	 * reference counters and also avoid wild pointers in secure world
-	 * into the old shared memory range.
-	 */
-	optee_disable_shm_cache(optee);
+	if (shutdown) {
+		optee_disable_shm_cache(optee);
+	} else {
+		/*
+		 * Ask OP-TEE to free all cached shared memory
+		 * objects to decrease reference counters and
+		 * also avoid wild pointers in secure world
+		 * into the old shared memory range.
+		 */
+		optee_disable_shm_cache(optee);
 
-	/*
-	 * The two devices have to be unregistered before we can free the
-	 * other resources.
-	 */
-	tee_device_unregister(optee->supp_teedev);
-	tee_device_unregister(optee->teedev);
+		/*
+		 * The two devices have to be unregistered before
+		 * we can free the other resources.
+		 */
+		tee_device_unregister(optee->supp_teedev);
+		tee_device_unregister(optee->teedev);
 
-	tee_shm_pool_free(optee->pool);
-	if (optee->memremaped_shm)
-		memunmap(optee->memremaped_shm);
-	optee_wait_queue_exit(&optee->wait_queue);
-	optee_supp_uninit(&optee->supp);
-	mutex_destroy(&optee->call_queue.mutex);
+		tee_shm_pool_free(optee->pool);
+		if (optee->memremaped_shm)
+			memunmap(optee->memremaped_shm);
+		optee_wait_queue_exit(&optee->wait_queue);
+		optee_supp_uninit(&optee->supp);
+		mutex_destroy(&optee->call_queue.mutex);
 
-	kfree(optee);
+		kfree(optee);
+	}
 
 	return 0;
 }
 
+/* optee_remove - Device Removal Routine
+ * @pdev: platform device information struct
+ *
+ * optee_remove is called by platform subsystem to alter the driver
+ * that it should release the device
+ */
+static int optee_remove(struct platform_device *pdev)
+{
+	return __optee_shutoff(pdev, false);
+}
+
+/* optee_shutdown - Device Removal Routine
+ * @pdev: platform device information struct
+ *
+ * platform_shutdown is called by the platform subsystem to alter
+ * the driver that a shutdown/reboot(or kexec) is happening and
+ * device must be disabled.
+ */
+static void optee_shutdown(struct platform_device *pdev)
+{
+	__optee_shutoff(pdev, true);
+}
+
 static int optee_probe(struct platform_device *pdev)
 {
 	optee_invoke_fn *invoke_fn;
@@ -738,6 +766,7 @@ MODULE_DEVICE_TABLE(of, optee_dt_match);
 static struct platform_driver optee_driver = {
 	.probe  = optee_probe,
 	.remove = optee_remove,
+	.shutdown = optee_shutdown,
 	.driver = {
 		.name = "optee",
 		.of_match_table = optee_dt_match,
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/2] optee: fix tee out of memory failure seen during kexec reboot

Quoted 13 lines · Allen Pais
Quoted 11 lines
drivers/tee/optee/core.c +5 -6
-	/*
-	 * Ask OP-TEE to free all cached shared memory objects to decrease
-	 * reference counters and also avoid wild pointers in secure world
-	 * into the old shared memory range.
-	 */
-	optee_disable_shm_cache(optee);
+	if (shutdown) {
+		optee_disable_shm_cache(optee);
+	} else {
+		/*
+		 * Ask OP-TEE to free all cached shared memory
Quoted 9 lines

drivers/tee/optee/core.c static optee_invoke_fn *get_invoke_func(struct device *dev) · line 598

Quoted 5 lines
drivers/tee/optee/core.c +5 -0
+		 * objects to decrease reference counters and
+		 * also avoid wild pointers in secure world
+		 * into the old shared memory range.
+		 */
+		optee_disable_shm_cache(optee);

Calling optee_disable_shm_cache() in both if and else. It could be put in front of if().

Ideally, I could just use optee_remove for shutdown() too.
But it would not look good. Hence this approach.

- Allen

#3 lore raw

Re: [PATCH 1/2] optee: fix tee out of memory failure seen during kexec reboot

Quoted 22 lines · Allen Pais
Quoted 12 lines · Allen Pais
Quoted 10 lines
drivers/tee/optee/core.c +4 -6
-	/*
-	 * Ask OP-TEE to free all cached shared memory objects to decrease
-	 * reference counters and also avoid wild pointers in secure world
-	 * into the old shared memory range.
-	 */
-	optee_disable_shm_cache(optee);
+	if (shutdown) {
+		optee_disable_shm_cache(optee);
+	} else {
+		/*
Quoted 9 lines

drivers/tee/optee/core.c static optee_invoke_fn *get_invoke_func(struct device *dev) · line 598

Quoted 6 lines
drivers/tee/optee/core.c +6 -0
+		 * Ask OP-TEE to free all cached shared memory
+		 * objects to decrease reference counters and
+		 * also avoid wild pointers in secure world
+		 * into the old shared memory range.
+		 */
+		optee_disable_shm_cache(optee);

Calling optee_disable_shm_cache() in both if and else. It could be put in front of if().

Quoted 2 lines

Ideally, I could just use optee_remove for shutdown() too.
But it would not look good. Hence this approach.

What is the problem with using optee_remove() for shutdown()?

Cheers,
Jens

#4 lore raw

Re: [PATCH 1/2] optee: fix tee out of memory failure seen during kexec reboot

Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 8 lines
drivers/tee/optee/core.c +0 -6
-	/*
-	 * Ask OP-TEE to free all cached shared memory objects to decrease
-	 * reference counters and also avoid wild pointers in secure world
-	 * into the old shared memory range.
-	 */
-	optee_disable_shm_cache(optee);

drivers/tee/optee/core.c static optee_invoke_fn *get_invoke_func(struct device *dev) · line 598

Quoted 10 lines
drivers/tee/optee/core.c +10 -0
+	if (shutdown) {
+		optee_disable_shm_cache(optee);
+	} else {
+		/*
+		 * Ask OP-TEE to free all cached shared memory
+		 * objects to decrease reference counters and
+		 * also avoid wild pointers in secure world
+		 * into the old shared memory range.
+		 */
+		optee_disable_shm_cache(optee);

Calling optee_disable_shm_cache() in both if and else. It could be put in front of if().

Quoted 5 lines
Quoted 2 lines

Ideally, I could just use optee_remove for shutdown() too.
But it would not look good. Hence this approach.

What is the problem with using optee_remove() for shutdown()?

There is no problem, I just thought it would be more cleaner/readable
with this approach. If you'd like to keep it simple by just calling optee_remove() for shutdown() too, I could quickly send out V2.

Thanks for the review.

- Allen

#5 lore raw

Re: [PATCH 1/2] optee: fix tee out of memory failure seen during kexec reboot

Quoted 25 lines · Allen Pais
Quoted 25 lines
Quoted 24 lines
Quoted 12 lines
Quoted 12 lines
drivers/tee/optee/core.c +4 -6
-	/*
-	 * Ask OP-TEE to free all cached shared memory objects to decrease
-	 * reference counters and also avoid wild pointers in secure world
-	 * into the old shared memory range.
-	 */
-	optee_disable_shm_cache(optee);
+	if (shutdown) {
+		optee_disable_shm_cache(optee);
+	} else {
+		/*
Quoted 9 lines

drivers/tee/optee/core.c static optee_invoke_fn *get_invoke_func(struct device *dev) · line 598

Quoted 6 lines
drivers/tee/optee/core.c +6 -0
+		 * Ask OP-TEE to free all cached shared memory
+		 * objects to decrease reference counters and
+		 * also avoid wild pointers in secure world
+		 * into the old shared memory range.
+		 */
+		optee_disable_shm_cache(optee);

Calling optee_disable_shm_cache() in both if and else. It could be put in front of if().

Ideally, I could just use optee_remove for shutdown() too.
But it would not look good. Hence this approach.

Quoted 6 lines
Quoted 2 lines

What is the problem with using optee_remove() for shutdown()?

There is no problem, I just thought it would be more cleaner/readable
with this approach. If you'd like to keep it simple by just calling optee_remove() for shutdown() too, I could quickly send out V2.

In the patch you posted it looks like you'd like to call only optee_disable_shm_cache() in the case of shutdown. Like:

static void optee_shutdown(struct platform_device *pdev)

{
        optee_disable_shm_cache(platform_get_drvdata(pdev));
}

and optee_remove() kept as it was before this patch.

Cheers,
Jens

#6 lore raw

Re: [PATCH 1/2] optee: fix tee out of memory failure seen during kexec reboot

Quoted 34 lines
Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 11 lines
Quoted 11 lines
drivers/tee/optee/core.c +4 -6
-	/*
-	 * Ask OP-TEE to free all cached shared memory objects to decrease
-	 * reference counters and also avoid wild pointers in secure world
-	 * into the old shared memory range.
-	 */
-	optee_disable_shm_cache(optee);
+	if (shutdown) {
+		optee_disable_shm_cache(optee);
+	} else {
+		/*
Quoted 9 lines

drivers/tee/optee/core.c static optee_invoke_fn *get_invoke_func(struct device *dev) · line 598

Quoted 6 lines
drivers/tee/optee/core.c +6 -0
+		 * Ask OP-TEE to free all cached shared memory
+		 * objects to decrease reference counters and
+		 * also avoid wild pointers in secure world
+		 * into the old shared memory range.
+		 */
+		optee_disable_shm_cache(optee);

Calling optee_disable_shm_cache() in both if and else. It could be put in front of if().

Ideally, I could just use optee_remove for shutdown() too.
But it would not look good. Hence this approach.

Quoted 6 lines
Quoted 2 lines

What is the problem with using optee_remove() for shutdown()?

There is no problem, I just thought it would be more cleaner/readable
with this approach. If you'd like to keep it simple by just calling optee_remove() for shutdown() too, I could quickly send out V2.

In the patch you posted it looks like you'd like to call only optee_disable_shm_cache() in the case of shutdown. Like:

Quoted 7 lines

static void optee_shutdown(struct platform_device *pdev)

{
         optee_disable_shm_cache(platform_get_drvdata(pdev));
}

and optee_remove() kept as it was before this patch.

Sure, Will have it fixed and send out V2.

Thanks.

author
#7 lore raw

PATCH 2/2 firmware: tee_bnxt: implement shutdown method to handle kexec reboots

In reply to Allen Pais

From: Allen Pais <[email protected]>

On kexec reboot the firmware driver fails to deallocate
shm memory leading to a memory leak. Implement .shutdown() method to handle kexec reboots and to release shm buffers correctly.

---
 drivers/firmware/broadcom/tee_bnxt_fw.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/firmware/broadcom/tee_bnxt_fw.c +9 -0
index ed10da5313e8..4c62e044a99f 100644
@@ -242,6 +242,14 @@ static int tee_bnxt_fw_remove(struct device *dev)
 	return 0;
 }
 
+static void tee_bnxt_fw_shutdown(struct device *dev)
+{
+	tee_shm_free(pvt_data.fw_shm_pool);
+	tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
+	tee_client_close_context(pvt_data.ctx);
+	pvt_data.ctx = NULL;
+}
+
 static const struct tee_client_device_id tee_bnxt_fw_id_table[] = {
 	{UUID_INIT(0x6272636D, 0x2019, 0x0716,
 		    0x42, 0x43, 0x4D, 0x5F, 0x53, 0x43, 0x48, 0x49)},
@@ -257,6 +265,7 @@ static struct tee_client_driver tee_bnxt_fw_driver = {
 		.bus		= &tee_bus_type,
 		.probe		= tee_bnxt_fw_probe,
 		.remove		= tee_bnxt_fw_remove,
+		.shutdown	= tee_bnxt_fw_shutdown,
 	},
 };
 
Signature · 1 line
2.25.1
#8 lore raw

cover optee: fix OOM seen due to tee_shm_free()

In reply to Allen Pais

From: Allen Pais <[email protected]>

Quoted 6 lines · Allen Pais

The following out of memory errors are seen on kexec reboot from the optee core.

[    0.368428] tee_bnxt_fw optee-clnt0: tee_shm_alloc failed
[    0.368461] tee_bnxt_fw: probe of optee-clnt0 failed with error -22
Quoted 7 lines

tee_shm_release() is not invoked on dma shm buffer.

Implement .shutdown() in optee core as well as bnxt firmware driver to handle the release of the buffers correctly.

More info:
https://github.com/OP-TEE/optee_os/issues/3637

CC: linux-kernel(a)vger.kernel.org instead of linux-mips? TEE / TrustZone is ARM.

Also, for Broadcom specific -
CC: bcm-kernel-feedback-list(a)broadcom.com

#9 lore raw
In reply to Allen Pais
Quoted 8 lines

The following out of memory errors are seen on kexec reboot from the optee core.

[    0.368428] tee_bnxt_fw optee-clnt0: tee_shm_alloc failed
[    0.368461] tee_bnxt_fw: probe of optee-clnt0 failed with error -22
Quoted 7 lines

tee_shm_release() is not invoked on dma shm buffer.

Implement .shutdown() in optee core as well as bnxt firmware driver to handle the release of the buffers correctly.

More info:
https://github.com/OP-TEE/optee_os/issues/3637

Jens,

Could you please take sometime out and review the series.

Thanks.

Quoted 9 lines

Allen Pais (2):
optee: fix tee out of memory failure seen during kexec reboot
firmware: tee_bnxt: implement shutdown method to handle kexec reboots

  drivers/firmware/broadcom/tee_bnxt_fw.c |  9 ++++
  drivers/tee/optee/core.c                | 69 ++++++++++++++++++-------
  2 files changed, 58 insertions(+), 20 deletions(-)