Skip to contentSkip to conversation

optee: Suppress false positive kmemleak report in optee_handle_rpc()

PATCH In review Xiaolei Wang 6 participants 27 messages Dec 6, 2021 – Dec 16, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 optee: Suppress false positive kmemleak report in optee_handle_rpc()

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

---
 drivers/tee/optee/smc_abi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tee/optee/smc_abi.c +2 -0
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
 #include "optee_private.h"
 #include "optee_smc.h"
 #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
 #define CREATE_TRACE_POINTS
 #include "optee_trace.h"
 
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
 			param->a4 = 0;
 			param->a5 = 0;
 		}
+		kmemleak_not_leak(shm);
 		break;
 	case OPTEE_SMC_RPC_FUNC_FREE:
 		shm = reg_pair_to_ptr(param->a1, param->a2);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 2 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world? An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

-Sumit

Quoted 29 lines
---
 drivers/tee/optee/smc_abi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tee/optee/smc_abi.c +2 -1
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
 #include "optee_private.h"
 #include "optee_smc.h"
 #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
 #define CREATE_TRACE_POINTS
 #include "optee_trace.h"
 
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
                        param->a4 = 0;
                        param->a5 = 0;
                }
+               kmemleak_not_leak(shm);
                break;
        case OPTEE_SMC_RPC_FUNC_FREE:
                shm = reg_pair_to_ptr(param->a1, param->a2);
--

2.25.1

#2 lore raw

________________________________
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:40 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org <[email protected]>; op-tee(a)lists.trustedfirmware.org <[email protected]>; linux-kernel(a)vger.kernel.org <[email protected]>
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 28 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

Quoted 8 lines

How about if it's actually a memory leak caused by the secure world? An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit

You mean we need to check whether there is a real memory leak, if being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and until the end, there is no free it via OPTEE_SMC_RPC_FUNC_FREE, then we should judge it as a memory leak.

Quoted 31 lines

-Sumit

---
 drivers/tee/optee/smc_abi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tee/optee/smc_abi.c +2 -1
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
 #include "optee_private.h"
 #include "optee_smc.h"
 #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
 #define CREATE_TRACE_POINTS
 #include "optee_trace.h"
 
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
                        param->a4 = 0;
                        param->a5 = 0;
                }
+               kmemleak_not_leak(shm);
                break;
        case OPTEE_SMC_RPC_FUNC_FREE:
                shm = reg_pair_to_ptr(param->a1, param->a2);
--

2.25.1

#3 lore raw
In reply to Sumit Garg

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Thanks
Xiaolei

Quoted 31 lines

-Sumit

---
 drivers/tee/optee/smc_abi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tee/optee/smc_abi.c +2 -1
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
 #include "optee_private.h"
 #include "optee_smc.h"
 #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
 #define CREATE_TRACE_POINTS
 #include "optee_trace.h"
 
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
                        param->a4 = 0;
                        param->a5 = 0;
                }
+               kmemleak_not_leak(shm);
                break;
        case OPTEE_SMC_RPC_FUNC_FREE:
                shm = reg_pair_to_ptr(param->a1, param->a2);
--

2.25.1

#4 lore raw
Quoted 44 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 30 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

Quoted 7 lines
Quoted 1 line

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

-Sumit

Quoted 36 lines

Thanks
Xiaolei

Quoted 31 lines

-Sumit

---
 drivers/tee/optee/smc_abi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tee/optee/smc_abi.c +2 -1
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
 #include "optee_private.h"
 #include "optee_smc.h"
 #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
 #define CREATE_TRACE_POINTS
 #include "optee_trace.h"
 
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
                        param->a4 = 0;
                        param->a5 = 0;
                }
+               kmemleak_not_leak(shm);
                break;
        case OPTEE_SMC_RPC_FUNC_FREE:
                shm = reg_pair_to_ptr(param->a1, param->a2);
--

2.25.1

#5 lore raw

+CC Jens, Etienne

Quoted 47 lines · Sumit Garg
Quoted 46 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

Quoted 8 lines
Quoted 3 lines

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

Signature · 1 line
Jerome
#6 lore raw

Hello all,

Quoted 60 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 47 lines · Sumit Garg
Quoted 46 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

Quoted 8 lines
Quoted 3 lines

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

Quoted 6 lines

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

It's been a while since OP-TEE caches some shm buffers to prevent re-allocting them on and on.
OP-TEE does so for 1 shm buffer per "tee threads" OP-TEE has provisioned. Each thread can cache a shm reference.
Note that used RPCs from optee to linux/u-boot/ree do not require such message buffer (IMO).

The main issue is the shm buffer are allocated per optee thread (thread context assigned to client invocation request when entreing optee).
Therefore, if an optee thread caches a shm buffer, it makes the caller tee session to have a shm reference with a refcount held, until Optee thread releases its cached shm reference.

There are ugly side effects. Linux must disable the cache to release all resources.
We recently saw some tee sessions may be left open because of such shm refcount held.
It can lead to few misbehaviour of the TA service (restarting a service, releasing a resource)

Config switch CFG_PREALLOC_RPC_CACHE was introduced [pr4896] to disable the feature at boot time.
There are means to not use it, or to explicitly enable/disable it at run time (already used optee smc services for that). Would maybe be a better default config.
Note this discussion thread ending at his comment [issue1918]:

Comments are welcome. I may have missed something in the description (or understanding :).

[pr4896] https://github.com/OP-TEE/optee_os/pull/4896
[issue1918] https://github.com/OP-TEE/optee_os/issues/1918#issuecomment-968747738

Best regards,
etienne

Quoted 2 lines

--
Jerome

#7 lore raw

fixing typos :(

Quoted 17 lines · Etienne Carriere
Quoted 10 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 7 lines · Sumit Garg
Quoted 2 lines · Xiaolei

From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

Quoted 5 lines

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

Quoted 8 lines

(snip)

It's been a while since OP-TEE caches some shm buffers to prevent re-allocting them on and on.
OP-TEE does so for 1 shm buffer per "tee threads" OP-TEE has provisioned. Each thread can cache a shm reference.
Note that used RPCs from optee to linux/u-boot/ree do not require such message buffer (IMO).

I meant: "Note that **most of the** used RPCs from ..."

br,
etienne

#8 lore raw
In reply to Etienne Carriere
Quoted 92 lines · Etienne Carriere

Hello all,

Quoted 60 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 47 lines · Sumit Garg
Quoted 46 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

Quoted 8 lines
Quoted 3 lines

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

Quoted 6 lines

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

It's been a while since OP-TEE caches some shm buffers to prevent re-allocting them on and on.
OP-TEE does so for 1 shm buffer per "tee threads" OP-TEE has provisioned. Each thread can cache a shm reference.
Note that used RPCs from optee to linux/u-boot/ree do not require such message buffer (IMO).

The main issue is the shm buffer are allocated per optee thread (thread context assigned to client invocation request when entreing optee).
Therefore, if an optee thread caches a shm buffer, it makes the caller tee session to have a shm reference with a refcount held, until Optee thread releases its cached shm reference.

There are ugly side effects. Linux must disable the cache to release all resources.
We recently saw some tee sessions may be left open because of such shm refcount held.
It can lead to few misbehaviour of the TA service (restarting a service, releasing a resource)

Quoted 7 lines

Config switch CFG_PREALLOC_RPC_CACHE was introduced [pr4896] to disable the feature at boot time.
There are means to not use it, or to explicitly enable/disable it at run time (already used optee smc services for that). Would maybe be a better default config.
Note this discussion thread ending at his comment [issue1918]:

Thanks etienne for the detailed description and references. Although, we can set CFG_PREALLOC_RPC_CACHE=n by default but it feels like we would miss a valuable optimization.

How about we just allocate a shared memory page during the OP-TEE driver probe and share it with optee-os to use for RPC arguments? And later it can be freed during OP-TEE driver removal. This would avoid any refconting of this special memory to be associated with TA sessions.

-Sumit

Quoted 13 lines

Comments are welcome. I may have missed something in the description (or understanding :).

[pr4896] https://github.com/OP-TEE/optee_os/pull/4896
[issue1918] https://github.com/OP-TEE/optee_os/issues/1918#issuecomment-968747738

Best regards,
etienne

Quoted 2 lines

--
Jerome

#9 lore raw
Quoted 107 lines · Sumit Garg
Quoted 92 lines · Etienne Carriere

Hello all,

Quoted 60 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 47 lines · Sumit Garg
Quoted 46 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

Quoted 8 lines
Quoted 3 lines

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

Quoted 6 lines

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

It's been a while since OP-TEE caches some shm buffers to prevent re-allocting them on and on.
OP-TEE does so for 1 shm buffer per "tee threads" OP-TEE has provisioned. Each thread can cache a shm reference.
Note that used RPCs from optee to linux/u-boot/ree do not require such message buffer (IMO).

The main issue is the shm buffer are allocated per optee thread (thread context assigned to client invocation request when entreing optee).
Therefore, if an optee thread caches a shm buffer, it makes the caller tee session to have a shm reference with a refcount held, until Optee thread releases its cached shm reference.

There are ugly side effects. Linux must disable the cache to release all resources.
We recently saw some tee sessions may be left open because of such shm refcount held.
It can lead to few misbehaviour of the TA service (restarting a service, releasing a resource)

Quoted 7 lines

Config switch CFG_PREALLOC_RPC_CACHE was introduced [pr4896] to disable the feature at boot time.
There are means to not use it, or to explicitly enable/disable it at run time (already used optee smc services for that). Would maybe be a better default config.
Note this discussion thread ending at his comment [issue1918]:

Thanks etienne for the detailed description and references. Although, we can set CFG_PREALLOC_RPC_CACHE=n by default but it feels like we would miss a valuable optimization.

Quoted 5 lines

How about we just allocate a shared memory page during the OP-TEE driver probe and share it with optee-os to use for RPC arguments? And later it can be freed during OP-TEE driver removal. This would avoid any refconting of this special memory to be associated with TA sessions.

True. The driver currently invokes OPTEE_SMC_ENABLE_SHM_CACHE to start caching some shm allocations. The optee_os part of that command could be changed to preallocate the required small rpc message buffer per provisioned tee thread.

Existing OPTEE_SMC_DISABLE_SHM_CACHE should behave accordingly.

etienne

Quoted 16 lines

-Sumit

Quoted 13 lines

Comments are welcome. I may have missed something in the description (or understanding :).

[pr4896] https://github.com/OP-TEE/optee_os/pull/4896
[issue1918] https://github.com/OP-TEE/optee_os/issues/1918#issuecomment-968747738

Best regards,
etienne

Quoted 2 lines

--
Jerome

#10 lore raw
In reply to Sumit Garg

Hi,

Quoted 107 lines · Sumit Garg
Quoted 92 lines · Etienne Carriere

Hello all,

Quoted 60 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 47 lines · Sumit Garg
Quoted 46 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

Quoted 8 lines
Quoted 3 lines

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

Quoted 6 lines

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

It's been a while since OP-TEE caches some shm buffers to prevent re-allocting them on and on.
OP-TEE does so for 1 shm buffer per "tee threads" OP-TEE has provisioned. Each thread can cache a shm reference.
Note that used RPCs from optee to linux/u-boot/ree do not require such message buffer (IMO).

The main issue is the shm buffer are allocated per optee thread (thread context assigned to client invocation request when entreing optee).
Therefore, if an optee thread caches a shm buffer, it makes the caller tee session to have a shm reference with a refcount held, until Optee thread releases its cached shm reference.

There are ugly side effects. Linux must disable the cache to release all resources.
We recently saw some tee sessions may be left open because of such shm refcount held.
It can lead to few misbehaviour of the TA service (restarting a service, releasing a resource)

Quoted 7 lines

Config switch CFG_PREALLOC_RPC_CACHE was introduced [pr4896] to disable the feature at boot time.
There are means to not use it, or to explicitly enable/disable it at run time (already used optee smc services for that). Would maybe be a better default config.
Note this discussion thread ending at his comment [issue1918]:

Thanks etienne for the detailed description and references. Although, we can set CFG_PREALLOC_RPC_CACHE=n by default but it feels like we would miss a valuable optimization.

Quoted 5 lines

How about we just allocate a shared memory page during the OP-TEE driver probe and share it with optee-os to use for RPC arguments? And later it can be freed during OP-TEE driver removal. This would avoid any refconting of this special memory to be associated with TA sessions.

The FF-A ABI part of the driver avoids this problem. I've started on a similar solution for the SMC based ABI, but it's not ready to post yet.

Cheers,
Jens

Quoted 16 lines

-Sumit

Quoted 13 lines

Comments are welcome. I may have missed something in the description (or understanding :).

[pr4896] https://github.com/OP-TEE/optee_os/pull/4896
[issue1918] https://github.com/OP-TEE/optee_os/issues/1918#issuecomment-968747738

Best regards,
etienne

Quoted 2 lines

--
Jerome

#11 lore raw
In reply to Jerome Forissier
Quoted 55 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 51 lines · Sumit Garg
Quoted 49 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 7 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Xiaolei,

Can you once test with CFG_PREALLOC_RPC_CACHE=n while compiling optee-os and see if the observed memory leak disappears or not?

-Sumit

Quoted 6 lines

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

--
Jerome

#12 lore raw
Quoted 64 lines · Sumit Garg
Quoted 55 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 51 lines · Sumit Garg
Quoted 49 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 7 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Quoted 6 lines

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

IIUC this patch adds kmemleak_not_leak() at (pretty much) the last possible point before *ownership* of the SHM block is passed from kernel to OP-TEE.

Sure, after we change ownership it could still be leaked... but it can no longer be leaked by the kernel because the kernel no longer owns it! More importantly, it makes no sense to run the kernel memory detector on the buffer because it simply can't work.

After the RPC completes, doesn't it become impossible for kmemleak to scan to see if the pointer is lost[1]? kmemleak is essentially a tracing garbage collector and needs to be able to scan all memory that could hold a pointer to leakable memory. After the RPC completes the only copy of the pointer will be stored in a memory region that the kernel is prohibited from reading. How could kmemleak possibly give you a useful answer in this circumstance?

In other words if there's nothing kmemleak could do to fix this situation then marking the memory as kmemleak_not_leak() seems entirely appropriate (although it should be prefaced with a big comment explaining the change of ownerhship and why kmemleak cannot work).

Daniel.

[1] Everything I've said here hinges on this being true... so if I've made a mistake about where OP-TEE stores this pointer then most of the rest of this post is junk ;-)

#13 lore raw
Quoted 70 lines · Daniel Thompson
Quoted 68 lines · Sumit Garg
Quoted 55 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 51 lines · Sumit Garg
Quoted 49 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE.

IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 7 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

Quoted 6 lines
Quoted 2 lines

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

IIUC this patch adds kmemleak_not_leak() at (pretty much) the last possible point before *ownership* of the SHM block is passed from kernel to OP-TEE.

I wouldn't say it's a transfer of ownership from kernel to OP-TEE but rather a way for OP-TEE to access kernel's memory in order to pass info or execute further RPC commands.

Quoted 8 lines

Sure, after we change ownership it could still be leaked... but it can no longer be leaked by the kernel because the kernel no longer owns it! More importantly, it makes no sense to run the kernel memory detector on the buffer because it simply can't work.

After the RPC completes, doesn't it become impossible for kmemleak to scan to see if the pointer is lost[1]?

Apart from the special OP-TEE prealloc SHM cache stuff, I can't think of any scenario where an OP-TEE thread should hold off kernel's memory pointers for more than 5 seconds before being passed onto kernel for further RPC commands or RPC free action. So the kmemleak should be able to detect if a pointer is lost.

/*
* Kmemleak configuration and common defines.
*/
<snip>
#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
<snip>

Quoted 7 lines

kmemleak is essentially a tracing
garbage collector and needs to be able to scan all memory that could hold a pointer to leakable memory. After the RPC completes the only copy of the pointer will be stored in a memory region that the kernel is prohibited from reading. How could kmemleak possibly give you a useful answer in this circumstance?

There is another aspect of kmemleak being the minimum age of an object to be reported as a memory leak as described above. Also, this case resembles where a pointer is stored on the CPU stack (see struct optee_rpc_param param = { };).

In most of the scenarios apart from special prealloc shm cache case, the flow should be as follows:

1) Alloc kernel memory via RPC
2) OP-TEE passes references to kernel memory for RPC action commands
3) Free kernel memory via RPC

kmemleak should be useful in case the 3rd step is skipped due to incorrect behaviour of a particular OP-TEE thread. And I can't think of any appropriate way in OP-TEE OS to detect this type of kernel memory leak caused by one of its threads.

-Sumit

Quoted 12 lines

In other words if there's nothing kmemleak could do to fix this situation then marking the memory as kmemleak_not_leak() seems entirely appropriate (although it should be prefaced with a big comment explaining the change of ownerhship and why kmemleak cannot work).

Daniel.

[1] Everything I've said here hinges on this being true... so if I've made a mistake about where OP-TEE stores this pointer then most of the rest of this post is junk ;-)

#14 lore raw
Quoted 21 lines · Sumit Garg
Quoted 19 lines · Daniel Thompson
Quoted 17 lines · Sumit Garg
Quoted 9 lines · Jerome Forissier
Quoted 5 lines · Sumit Garg

On Fri, 10 Dec 2021 at 09:42, Wang, Xiaolei <[email protected]> wrote:
Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Quoted 7 lines
Quoted 3 lines

IIUC this patch adds kmemleak_not_leak() at (pretty much) the last possible point before *ownership* of the SHM block is passed from kernel to OP-TEE.

I wouldn't say it's a transfer of ownership from kernel to OP-TEE but rather a way for OP-TEE to access kernel's memory in order to pass info or execute further RPC commands.

The RPC handler allocates a pointer (e.g. now the RPC handler owns the allocated memory). The RPC handler then passes that pointer to OP-TEE and forgets what it's value was.

That is a transfer of ownership: the RPC handler does not hold any pointer to the memory and is incapable of freeing it. Moreover this situation is what kmemleak_no_leak() is for! Its job it to inform kmemleak that the pointer is owned/stored somewhere that is does not scan.

Quoted 5 lines
Quoted 5 lines

Sure, after we change ownership it could still be leaked... but it can no longer be leaked by the kernel because the kernel no longer owns it! More importantly, it makes no sense to run the kernel memory detector on the buffer because it simply can't work.

Quoted 8 lines
Quoted 2 lines

After the RPC completes, doesn't it become impossible for kmemleak to scan to see if the pointer is lost[1]?

Apart from the special OP-TEE prealloc SHM cache stuff, I can't think of any scenario where an OP-TEE thread should hold off kernel's memory pointers for more than 5 seconds before being passed onto kernel for further RPC commands or RPC free action. So the kmemleak should be able to detect if a pointer is lost.

Or putting this a different way: there is known to be firmware in the field that allocates pointers for more then five seconds!

Quoted 15 lines

/*
* Kmemleak configuration and common defines.
*/
<snip>
#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
<snip>

Quoted 7 lines

kmemleak is essentially a tracing
garbage collector and needs to be able to scan all memory that could hold a pointer to leakable memory. After the RPC completes the only copy of the pointer will be stored in a memory region that the kernel is prohibited from reading. How could kmemleak possibly give you a useful answer in this circumstance?

Quoted 4 lines

There is another aspect of kmemleak being the minimum age of an object to be reported as a memory leak as described above. Also, this case resembles where a pointer is stored on the CPU stack (see struct optee_rpc_param param = { };).

I can't see how this resembles pointers stored on the stack.

Firstly, stack memory is scanned by kmemleak meaning a thread is permitted to own memory for more than five seconds without provoking a warning. OP-TEE memory cannot be scanned like this.

Secondly, stacks don't have any concept of sessions. It is *really* buggy behaviour for a TA to allocate SHM memory during a session open so it can avoid critical path RPC round trips when operational?

Quoted 11 lines

In most of the scenarios apart from special prealloc shm cache case, the flow should be as follows:

1) Alloc kernel memory via RPC
2) OP-TEE passes references to kernel memory for RPC action commands
3) Free kernel memory via RPC

kmemleak should be useful in case the 3rd step is skipped due to incorrect behaviour of a particular OP-TEE thread. And I can't think of any appropriate way in OP-TEE OS to detect this type of kernel memory leak caused by one of its threads.

If OP-TEE is the only place the pointer is held and you can't think of any way for OP-TEE OS to detect if it has leaked the pointer then how can you expect the kernel to give a correct verdict when it has even less visibility than OP-TEE OS.

Note that, if you think OP-TEE routinely leaks memory, then there are ways that the corresponding kernel driver could track what memory it has handed to OP-TEE. However this should be described as a list of *allocations* rather than a list of *leaks* because the driver cannot distinguish the two.

Daniel.

#15 lore raw
Quoted 31 lines · Daniel Thompson
Quoted 21 lines · Sumit Garg
Quoted 19 lines · Daniel Thompson
Quoted 17 lines · Sumit Garg
Quoted 9 lines · Jerome Forissier
Quoted 5 lines · Sumit Garg

On Fri, 10 Dec 2021 at 09:42, Wang, Xiaolei <[email protected]> wrote:
Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Quoted 7 lines
Quoted 3 lines

IIUC this patch adds kmemleak_not_leak() at (pretty much) the last possible point before *ownership* of the SHM block is passed from kernel to OP-TEE.

I wouldn't say it's a transfer of ownership from kernel to OP-TEE but rather a way for OP-TEE to access kernel's memory in order to pass info or execute further RPC commands.

Quoted 9 lines

The RPC handler allocates a pointer (e.g. now the RPC handler owns the allocated memory). The RPC handler then passes that pointer to OP-TEE and forgets what it's value was.

That is a transfer of ownership: the RPC handler does not hold any pointer to the memory and is incapable of freeing it. Moreover this situation is what kmemleak_no_leak() is for! Its job it to inform kmemleak that the pointer is owned/stored somewhere that is does not scan.

Let me put this another way. If the memory allocator belongs to the kernel then how does OP-TEE get to know which memory is currently allocated and it is to be scanned?

I think the complete solution would be to extend kmemleak to support OP-TEE memory scanning via OP-TEE invocation to check if it's holding any kernel memory references.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

Sure, after we change ownership it could still be leaked... but it can no longer be leaked by the kernel because the kernel no longer owns it! More importantly, it makes no sense to run the kernel memory detector on the buffer because it simply can't work.

After the RPC completes, doesn't it become impossible for kmemleak to scan to see if the pointer is lost[1]?

Quoted 9 lines
Quoted 5 lines

Apart from the special OP-TEE prealloc SHM cache stuff, I can't think of any scenario where an OP-TEE thread should hold off kernel's memory pointers for more than 5 seconds before being passed onto kernel for further RPC commands or RPC free action. So the kmemleak should be able to detect if a pointer is lost.

Or putting this a different way: there is known to be firmware in the field that allocates pointers for more then five seconds!

If it's known that upstream OP-TEE doesn't hold any kernel memory references for more than 5 seconds then IMO we should be fine to not disable kmemleak until we have a future kmemleak extension. Otherwise it would be very hard to keep track of kernel memory lost in this way.

Quoted 23 lines
Quoted 16 lines

/*
* Kmemleak configuration and common defines.
*/
<snip>
#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
<snip>

Quoted 7 lines

kmemleak is essentially a tracing
garbage collector and needs to be able to scan all memory that could hold a pointer to leakable memory. After the RPC completes the only copy of the pointer will be stored in a memory region that the kernel is prohibited from reading. How could kmemleak possibly give you a useful answer in this circumstance?

Quoted 4 lines

There is another aspect of kmemleak being the minimum age of an object to be reported as a memory leak as described above. Also, this case resembles where a pointer is stored on the CPU stack (see struct optee_rpc_param param = { };).

I can't see how this resembles pointers stored on the stack.

Quoted 8 lines

Firstly, stack memory is scanned by kmemleak meaning a thread is permitted to own memory for more than five seconds without provoking a warning. OP-TEE memory cannot be scanned like this.

Secondly, stacks don't have any concept of sessions. It is *really* buggy behaviour for a TA to allocate SHM memory during a session open so it can avoid critical path RPC round trips when operational?

That's one optimization case for prealloc SHM we have currently. Jens is already working on an update for SMC ABI to avoid such allocations.

BTW, that could be disabled with CFG_PREALLOC_RPC_CACHE=n in upstream OP-TEE.

Quoted 13 lines
Quoted 4 lines

In most of the scenarios apart from special prealloc shm cache case, the flow should be as follows:

Quoted 8 lines

1) Alloc kernel memory via RPC
2) OP-TEE passes references to kernel memory for RPC action commands
3) Free kernel memory via RPC

kmemleak should be useful in case the 3rd step is skipped due to incorrect behaviour of a particular OP-TEE thread. And I can't think of any appropriate way in OP-TEE OS to detect this type of kernel memory leak caused by one of its threads.

Quoted 4 lines

If OP-TEE is the only place the pointer is held and you can't think of any way for OP-TEE OS to detect if it has leaked the pointer then how can you expect the kernel to give a correct verdict when it has even less visibility than OP-TEE OS.

Let me try to elaborate here. Since the nature of shared memory under consideration here are:

1) Allocated by kernel.
2) OP-TEE holds reference to it.

In order to detect its leakage, the memory leakage detector should be invoked by the kernel and OP-TEE should support that detector to say if it holds any references to the memory being scanned.

Also, as you may know OP-TEE is a passive firmware entity without any threads of its own and is scheduled by the Linux kernel. So it won't be possible to have an independent memory leak detector thread within OP-TEE.

Quoted 2 lines

Note that, if you think OP-TEE routinely leaks memory,

How can we be sure about any piece of complex software (lots of platform specific code) that it will leak memory or not? I guess that is the reason why the kernel has this runtime kmemleak debugging tool.

-Sumit

Quoted 8 lines

then there are
ways that the corresponding kernel driver could track what memory it has handed to OP-TEE. However this should be described as a list of *allocations* rather than a list of *leaks* because the driver cannot distinguish the two.

Daniel.

#16 lore raw
Quoted 21 lines · Sumit Garg
Quoted 19 lines · Daniel Thompson
Quoted 5 lines · Sumit Garg
Quoted 3 lines · Daniel Thompson
Quoted 2 lines · Sumit Garg
Quoted 1 line · Jerome Forissier

On 12/10/21 06:00, Sumit Garg wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Xiaolei

IIUC this patch adds kmemleak_not_leak() at (pretty much) the last possible point before *ownership* of the SHM block is passed from kernel to OP-TEE.

I wouldn't say it's a transfer of ownership from kernel to OP-TEE but rather a way for OP-TEE to access kernel's memory in order to pass info or execute further RPC commands.

The RPC handler allocates a pointer (e.g. now the RPC handler owns the allocated memory). The RPC handler then passes that pointer to OP-TEE and forgets what it's value was.

Quoted 8 lines
Quoted 4 lines

That is a transfer of ownership: the RPC handler does not hold any pointer to the memory and is incapable of freeing it. Moreover this situation is what kmemleak_no_leak() is for! Its job it to inform kmemleak that the pointer is owned/stored somewhere that is does not scan.

Let me put this another way. If the memory allocator belongs to the kernel then how does OP-TEE get to know which memory is currently allocated and it is to be scanned?

OP-TEE explicitly requested that the be allocated and responsible for figuring out where to store the pointer. How could it *not* know this information? More specifically OP-TEE is perfectly capable of recording what memory it has allocated and where to scan to find out if it has been lost.

Quoted 3 lines

I think the complete solution would be to extend kmemleak to support OP-TEE memory scanning via OP-TEE invocation to check if it's holding any kernel memory references.

This is the part I get stuck on... and the reason I'm still posting on the thread.

I struggle to see any value in using kmemleak to identify this type of leak. That is the fundamental issue. False positives from kmemleak are damaging to user confidence in the tool and are especially harmful when it is complex and time consuming to verify that is actually is a false positive (which would certainly be the case for OP-TEE false positives). In short it is *not* always the case failure-to-detect is worse than false-positive.

As discussed already the firmware/kernel contract prevents kmemleak from working as it is designed to and I am unconvinced that relying on fragile timeouts is sufficient.

Extending kmemleak to support OP-TEE memory scanning is also, IMHO, pointless. The reason for this is that OP-TEE cannot perform any scan on behalf of kmemleak without first validating the information provided to it by the kernel (to avoid information leaks). However if OP-TEE tracks enough state to validate the kernel input than it already has enough state to do a scan for leaks independently anyway (apart from being donated an execution context). Therefore it follows that any OP-TEE extension to handle leaks should be independent of kmemleak because it would still be useful to be able to ask OP-TEE to run a self-consistency check even if kmemleak is disabled.

Or, in short, even if you implement improved leak detection for OP-TEE (whether that is based on timers or scanning) then kmemleak_not_leak() is still the right thing to do with pointers whose ownership we have transferred to OP-TEE.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 7 lines

Sure, after we change ownership it could still be leaked... but it can no longer be leaked by the kernel because the kernel no longer owns it! More importantly, it makes no sense to run the kernel memory detector on the buffer because it simply can't work.

After the RPC completes, doesn't it become impossible for kmemleak to scan to see if the pointer is lost[1]?

Apart from the special OP-TEE prealloc SHM cache stuff, I can't think of any scenario where an OP-TEE thread should hold off kernel's memory pointers for more than 5 seconds before being passed onto kernel for further RPC commands or RPC free action. So the kmemleak should be able to detect if a pointer is lost.

Quoted 7 lines
Quoted 2 lines

Or putting this a different way: there is known to be firmware in the field that allocates pointers for more then five seconds!

If it's known that upstream OP-TEE doesn't hold any kernel memory references for more than 5 seconds then IMO we should be fine to not disable kmemleak until we have a future kmemleak extension. Otherwise it would be very hard to keep track of kernel memory lost in this way.

In essence I am arguing for using the right tool for the right job (and against turning down a correct patch because the right tool isn't yet implemented). A memory scanning leak detector is the wrong tool to search for leaks in memory that cannot be scanned.

For me having to rely on fragile implied contracts and undocumented assumptions about timing further reinforces my view that kmemleak is not the wrong tool. Especially so when we know that those assumptions are not met by existing firmware.

Daniel.

#17 lore raw
Quoted 102 lines · Daniel Thompson
Quoted 21 lines · Sumit Garg
Quoted 19 lines · Daniel Thompson
Quoted 5 lines · Sumit Garg
Quoted 3 lines · Daniel Thompson
Quoted 2 lines · Sumit Garg
Quoted 1 line · Jerome Forissier

On 12/10/21 06:00, Sumit Garg wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Xiaolei

IIUC this patch adds kmemleak_not_leak() at (pretty much) the last possible point before *ownership* of the SHM block is passed from kernel to OP-TEE.

I wouldn't say it's a transfer of ownership from kernel to OP-TEE but rather a way for OP-TEE to access kernel's memory in order to pass info or execute further RPC commands.

The RPC handler allocates a pointer (e.g. now the RPC handler owns the allocated memory). The RPC handler then passes that pointer to OP-TEE and forgets what it's value was.

Quoted 8 lines
Quoted 4 lines

That is a transfer of ownership: the RPC handler does not hold any pointer to the memory and is incapable of freeing it. Moreover this situation is what kmemleak_no_leak() is for! Its job it to inform kmemleak that the pointer is owned/stored somewhere that is does not scan.

Let me put this another way. If the memory allocator belongs to the kernel then how does OP-TEE get to know which memory is currently allocated and it is to be scanned?

OP-TEE explicitly requested that the be allocated and responsible for figuring out where to store the pointer. How could it *not* know this information? More specifically OP-TEE is perfectly capable of recording what memory it has allocated and where to scan to find out if it has been lost.

Quoted 3 lines

I think the complete solution would be to extend kmemleak to support OP-TEE memory scanning via OP-TEE invocation to check if it's holding any kernel memory references.

This is the part I get stuck on... and the reason I'm still posting on the thread.

I struggle to see any value in using kmemleak to identify this type of leak. That is the fundamental issue. False positives from kmemleak are damaging to user confidence in the tool and are especially harmful when it is complex and time consuming to verify that is actually is a false positive (which would certainly be the case for OP-TEE false positives). In short it is *not* always the case failure-to-detect is worse than false-positive.

As discussed already the firmware/kernel contract prevents kmemleak from working as it is designed to and I am unconvinced that relying on fragile timeouts is sufficient.

Extending kmemleak to support OP-TEE memory scanning is also, IMHO, pointless. The reason for this is that OP-TEE cannot perform any scan on behalf of kmemleak without first validating the information provided to it by the kernel (to avoid information leaks). However if OP-TEE tracks enough state to validate the kernel input than it already has enough state to do a scan for leaks independently anyway (apart from being donated an execution context). Therefore it follows that any OP-TEE extension to handle leaks should be independent of kmemleak because it would still be useful to be able to ask OP-TEE to run a self-consistency check even if kmemleak is disabled.

Or, in short, even if you implement improved leak detection for OP-TEE (whether that is based on timers or scanning) then kmemleak_not_leak() is still the right thing to do with pointers whose ownership we have transferred to OP-TEE.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 7 lines

Sure, after we change ownership it could still be leaked... but it can no longer be leaked by the kernel because the kernel no longer owns it! More importantly, it makes no sense to run the kernel memory detector on the buffer because it simply can't work.

After the RPC completes, doesn't it become impossible for kmemleak to scan to see if the pointer is lost[1]?

Apart from the special OP-TEE prealloc SHM cache stuff, I can't think of any scenario where an OP-TEE thread should hold off kernel's memory pointers for more than 5 seconds before being passed onto kernel for further RPC commands or RPC free action. So the kmemleak should be able to detect if a pointer is lost.

Quoted 7 lines
Quoted 2 lines

Or putting this a different way: there is known to be firmware in the field that allocates pointers for more then five seconds!

If it's known that upstream OP-TEE doesn't hold any kernel memory references for more than 5 seconds then IMO we should be fine to not disable kmemleak until we have a future kmemleak extension. Otherwise it would be very hard to keep track of kernel memory lost in this way.

In essence I am arguing for using the right tool for the right job (and against turning down a correct patch because the right tool isn't yet implemented). A memory scanning leak detector is the wrong tool to search for leaks in memory that cannot be scanned.

Quoted 4 lines

For me having to rely on fragile implied contracts and undocumented assumptions about timing further reinforces my view that kmemleak is not the wrong tool. Especially so when we know that those assumptions are not met by existing firmware.

I agree, this patch makes sense. It fixes a problem and I can't see a downside with that. In a not too distant future we may change the way this memory is passed to OP-TEE by keeping a reference in the driver, but until then this patch will fix a problem.

Cheers,
Jens

#18 lore raw
Quoted 110 lines · Jens Wiklander
Quoted 102 lines · Daniel Thompson
Quoted 21 lines · Sumit Garg
Quoted 19 lines · Daniel Thompson
Quoted 5 lines · Sumit Garg
Quoted 3 lines · Daniel Thompson
Quoted 2 lines · Sumit Garg
Quoted 1 line · Jerome Forissier

On 12/10/21 06:00, Sumit Garg wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Xiaolei

IIUC this patch adds kmemleak_not_leak() at (pretty much) the last possible point before *ownership* of the SHM block is passed from kernel to OP-TEE.

I wouldn't say it's a transfer of ownership from kernel to OP-TEE but rather a way for OP-TEE to access kernel's memory in order to pass info or execute further RPC commands.

The RPC handler allocates a pointer (e.g. now the RPC handler owns the allocated memory). The RPC handler then passes that pointer to OP-TEE and forgets what it's value was.

Quoted 8 lines
Quoted 4 lines

That is a transfer of ownership: the RPC handler does not hold any pointer to the memory and is incapable of freeing it. Moreover this situation is what kmemleak_no_leak() is for! Its job it to inform kmemleak that the pointer is owned/stored somewhere that is does not scan.

Let me put this another way. If the memory allocator belongs to the kernel then how does OP-TEE get to know which memory is currently allocated and it is to be scanned?

OP-TEE explicitly requested that the be allocated and responsible for figuring out where to store the pointer. How could it *not* know this information? More specifically OP-TEE is perfectly capable of recording what memory it has allocated and where to scan to find out if it has been lost.

Quoted 3 lines

I think the complete solution would be to extend kmemleak to support OP-TEE memory scanning via OP-TEE invocation to check if it's holding any kernel memory references.

This is the part I get stuck on... and the reason I'm still posting on the thread.

I struggle to see any value in using kmemleak to identify this type of leak. That is the fundamental issue. False positives from kmemleak are damaging to user confidence in the tool and are especially harmful when it is complex and time consuming to verify that is actually is a false positive (which would certainly be the case for OP-TEE false positives). In short it is *not* always the case failure-to-detect is worse than false-positive.

As discussed already the firmware/kernel contract prevents kmemleak from working as it is designed to and I am unconvinced that relying on fragile timeouts is sufficient.

Extending kmemleak to support OP-TEE memory scanning is also, IMHO, pointless. The reason for this is that OP-TEE cannot perform any scan on behalf of kmemleak without first validating the information provided to it by the kernel (to avoid information leaks). However if OP-TEE tracks enough state to validate the kernel input than it already has enough state to do a scan for leaks independently anyway (apart from being donated an execution context). Therefore it follows that any OP-TEE extension to handle leaks should be independent of kmemleak because it would still be useful to be able to ask OP-TEE to run a self-consistency check even if kmemleak is disabled.

Or, in short, even if you implement improved leak detection for OP-TEE (whether that is based on timers or scanning) then kmemleak_not_leak() is still the right thing to do with pointers whose ownership we have transferred to OP-TEE.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 7 lines

Sure, after we change ownership it could still be leaked... but it can no longer be leaked by the kernel because the kernel no longer owns it! More importantly, it makes no sense to run the kernel memory detector on the buffer because it simply can't work.

After the RPC completes, doesn't it become impossible for kmemleak to scan to see if the pointer is lost[1]?

Apart from the special OP-TEE prealloc SHM cache stuff, I can't think of any scenario where an OP-TEE thread should hold off kernel's memory pointers for more than 5 seconds before being passed onto kernel for further RPC commands or RPC free action. So the kmemleak should be able to detect if a pointer is lost.

Quoted 7 lines
Quoted 2 lines

Or putting this a different way: there is known to be firmware in the field that allocates pointers for more then five seconds!

If it's known that upstream OP-TEE doesn't hold any kernel memory references for more than 5 seconds then IMO we should be fine to not disable kmemleak until we have a future kmemleak extension. Otherwise it would be very hard to keep track of kernel memory lost in this way.

In essence I am arguing for using the right tool for the right job (and against turning down a correct patch because the right tool isn't yet implemented). A memory scanning leak detector is the wrong tool to search for leaks in memory that cannot be scanned.

Quoted 4 lines

For me having to rely on fragile implied contracts and undocumented assumptions about timing further reinforces my view that kmemleak is not the wrong tool. Especially so when we know that those assumptions are not met by existing firmware.

Quoted 4 lines

I agree, this patch makes sense. It fixes a problem and I can't see a downside with that. In a not too distant future we may change the way this memory is passed to OP-TEE by keeping a reference in the driver, but until then this patch will fix a problem.

Fair enough, I was just trying to be more optimistic about leveraging existing kmemleak infrastructure as shared memory bugs are catching on us.

-Sumit

Quoted 3 lines

Cheers,
Jens

#19 lore raw
In reply to Sumit Garg
Quoted 64 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 48 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 45 lines · Sumit Garg
Quoted 44 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 24 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):

   comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s)
   hex dump (first 32 bytes):
     00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
     60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
   backtrace:
     [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
     [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
     [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
     [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
     [<00000000c35884da>] optee_open_session+0x128/0x1ec
     [<000000001748f2ff>] tee_client_open_session+0x28/0x40
     [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
     [<000000003df18bf1>] optee_probe+0x674/0x6cc
     [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
     [<000000000c51ce7d>] really_probe+0xe4/0x4d0
     [<000000002f04c865>] driver_probe_device+0x58/0xc0
     [<00000000b485397d>] device_driver_attach+0xc0/0xd0
     [<00000000c835f0df>] __driver_attach+0x84/0x124
     [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
     [<000000001735e8a8>] driver_attach+0x24/0x30
     [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 5 lines

This is not a memory leak because we pass the share memory pointer to
secure world and would get it from secure world before releasing it.
How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE. IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 6 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Quoted 6 lines

Xiaolei,

Can you once test with CFG_PREALLOC_RPC_CACHE=n while compiling optee-os and see if the observed memory leak disappears or not?

-Sumit

Hi sumit

The version I am using has not increased the CFG_PREALLOC_RPC_CACHE

switch, I checked out to the latest version, but because of the need for

additional patches for the imx8 platform, I still have no way to test the

CFG_PREALLOC_RPC_CACHE=n situation

thanks

xiaolei

Quoted 6 lines
Quoted 6 lines

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

--
Jerome

#20 lore raw
Quoted 77 lines · wangxiaolei
Quoted 64 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 48 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 45 lines · Sumit Garg
Quoted 44 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 24 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):

   comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s)
   hex dump (first 32 bytes):
     00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
     60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
   backtrace:
     [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
     [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
     [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
     [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
     [<00000000c35884da>] optee_open_session+0x128/0x1ec
     [<000000001748f2ff>] tee_client_open_session+0x28/0x40
     [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
     [<000000003df18bf1>] optee_probe+0x674/0x6cc
     [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
     [<000000000c51ce7d>] really_probe+0xe4/0x4d0
     [<000000002f04c865>] driver_probe_device+0x58/0xc0
     [<00000000b485397d>] device_driver_attach+0xc0/0xd0
     [<00000000c835f0df>] __driver_attach+0x84/0x124
     [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
     [<000000001735e8a8>] driver_attach+0x24/0x30
     [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 5 lines

This is not a memory leak because we pass the share memory pointer to
secure world and would get it from secure world before releasing it.
How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE. IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 6 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Quoted 6 lines

Xiaolei,

Can you once test with CFG_PREALLOC_RPC_CACHE=n while compiling optee-os and see if the observed memory leak disappears or not?

-Sumit

Hi sumit

Quoted 8 lines

The version I am using has not increased the CFG_PREALLOC_RPC_CACHE

switch, I checked out to the latest version, but because of the need for

additional patches for the imx8 platform, I still have no way to test the

CFG_PREALLOC_RPC_CACHE=n situation

Can you just try to backport this [1] patch to your imx8 optee-os tree and test?

[1] https://github.com/OP-TEE/optee_os/commit/8887663248ad

-Sumit

Quoted 11 lines

thanks

xiaolei

Quoted 6 lines
Quoted 6 lines

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

--
Jerome

#21 lore raw
Quoted 82 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 79 lines · wangxiaolei
Quoted 64 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 48 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 45 lines · Sumit Garg
Quoted 44 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 24 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):

    comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s)
    hex dump (first 32 bytes):
      00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
      60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
    backtrace:
      [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
      [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
      [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
      [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
      [<00000000c35884da>] optee_open_session+0x128/0x1ec
      [<000000001748f2ff>] tee_client_open_session+0x28/0x40
      [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
      [<000000003df18bf1>] optee_probe+0x674/0x6cc
      [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
      [<000000000c51ce7d>] really_probe+0xe4/0x4d0
      [<000000002f04c865>] driver_probe_device+0x58/0xc0
      [<00000000b485397d>] device_driver_attach+0xc0/0xd0
      [<00000000c835f0df>] __driver_attach+0x84/0x124
      [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
      [<000000001735e8a8>] driver_attach+0x24/0x30
      [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 5 lines

This is not a memory leak because we pass the share memory pointer to
secure world and would get it from secure world before releasing it.
How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE. IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 6 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Quoted 6 lines

Xiaolei,

Can you once test with CFG_PREALLOC_RPC_CACHE=n while compiling optee-os and see if the observed memory leak disappears or not?

-Sumit

Hi sumit

The version I am using has not increased the CFG_PREALLOC_RPC_CACHE

switch, I checked out to the latest version, but because of the need for

Quoted 7 lines
Quoted 4 lines

additional patches for the imx8 platform, I still have no way to test the

CFG_PREALLOC_RPC_CACHE=n situation

Can you just try to backport this [1] patch to your imx8 optee-os tree and test?

[1] https://github.com/OP-TEE/optee_os/commit/8887663248ad

Hi sumit

I upgraded optee-os from version 3.2.0 to 3.13.0, and the kernel did not detect this problem.

I have not set CFG_PREALLOC_RPC_CACHE to n. This should be a problem that occurs when compatible

with lower versions.

thanks

xiaolei

Quoted 12 lines

-Sumit

Quoted 9 lines

thanks

xiaolei

Quoted 5 lines
Quoted 5 lines

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

--
Jerome

#22 lore raw
Quoted 89 lines · wangxiaolei
Quoted 86 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 83 lines · wangxiaolei
Quoted 64 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 48 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 45 lines · Sumit Garg
Quoted 44 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 24 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):

    comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s)
    hex dump (first 32 bytes):
      00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
      60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
    backtrace:
      [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
      [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
      [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
      [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
      [<00000000c35884da>] optee_open_session+0x128/0x1ec
      [<000000001748f2ff>] tee_client_open_session+0x28/0x40
      [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
      [<000000003df18bf1>] optee_probe+0x674/0x6cc
      [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
      [<000000000c51ce7d>] really_probe+0xe4/0x4d0
      [<000000002f04c865>] driver_probe_device+0x58/0xc0
      [<00000000b485397d>] device_driver_attach+0xc0/0xd0
      [<00000000c835f0df>] __driver_attach+0x84/0x124
      [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
      [<000000001735e8a8>] driver_attach+0x24/0x30
      [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 5 lines

This is not a memory leak because we pass the share memory pointer to
secure world and would get it from secure world before releasing it.
How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE. IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 6 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Quoted 6 lines

Xiaolei,

Can you once test with CFG_PREALLOC_RPC_CACHE=n while compiling optee-os and see if the observed memory leak disappears or not?

-Sumit

Hi sumit

The version I am using has not increased the CFG_PREALLOC_RPC_CACHE

switch, I checked out to the latest version, but because of the need for

additional patches for the imx8 platform, I still have no way to test the

CFG_PREALLOC_RPC_CACHE=n situation

Quoted 8 lines
Quoted 3 lines

Can you just try to backport this [1] patch to your imx8 optee-os tree and test?

[1] https://github.com/OP-TEE/optee_os/commit/8887663248ad

Hi sumit

I upgraded optee-os from version 3.2.0 to 3.13.0, and the kernel did not detect this problem.

Can you check if CFG_TEE_CORE_EMBED_INTERNAL_TESTS is enabled in optee-os version 3.13.0? As we would require atleast one RPC prealloc SHM invocation from OP-TEE for kmemleak to detect the problem.

-Sumit

Quoted 23 lines

I have not set CFG_PREALLOC_RPC_CACHE to n. This should be a problem that occurs when compatible

with lower versions.

thanks

xiaolei

Quoted 12 lines

-Sumit

Quoted 9 lines

thanks

xiaolei

Quoted 5 lines
Quoted 5 lines

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

--
Jerome

#23 lore raw
Quoted 92 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 89 lines · wangxiaolei
Quoted 87 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 74 lines · wangxiaolei
Quoted 64 lines · Sumit Garg

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 48 lines · Jerome Forissier

+CC Jens, Etienne

Quoted 45 lines · Sumit Garg
Quoted 44 lines · Xiaolei

-----Original Message-----
From: Sumit Garg <[email protected]>
Sent: Thursday, December 9, 2021 7:41 PM
To: Wang, Xiaolei <[email protected]>
Cc: jens.wiklander(a)linaro.org; op-tee(a)lists.trustedfirmware.org; linux-kernel(a)vger.kernel.org
Subject: Re: [PATCH] optee: Suppress false positive kmemleak report in optee_handle_rpc()

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 24 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):

     comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s)
     hex dump (first 32 bytes):
       00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
       60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
     backtrace:
       [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
       [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
       [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
       [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
       [<00000000c35884da>] optee_open_session+0x128/0x1ec
       [<000000001748f2ff>] tee_client_open_session+0x28/0x40
       [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
       [<000000003df18bf1>] optee_probe+0x674/0x6cc
       [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
       [<000000000c51ce7d>] really_probe+0xe4/0x4d0
       [<000000002f04c865>] driver_probe_device+0x58/0xc0
       [<00000000b485397d>] device_driver_attach+0xc0/0xd0
       [<00000000c835f0df>] __driver_attach+0x84/0x124
       [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
       [<000000001735e8a8>] driver_attach+0x24/0x30
       [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 5 lines

This is not a memory leak because we pass the share memory pointer to
secure world and would get it from secure world before releasing it.
How about if it's actually a memory leak caused by the secure world?
An example being secure world just allocates kernel memory via OPTEE_SMC_RPC_FUNC_ALLOC and doesn't free it via OPTEE_SMC_RPC_FUNC_FREE. IMO, we need to cross-check optee-os if it's responsible for leaking kernel memory.

Hi sumit,

You mean we need to check whether there is a real memleak,
If being secure world just allocate kernel memory via OPTEE_SMC_PRC_FUNC_ALLOC and until the end, there is no free It via OPTEE_SMC_PRC_FUNC_FREE, then we should judge it as a memory leak, wo need to judge whether it is caused by secure os?

Quoted 6 lines
Quoted 4 lines

Yes. AFAICT, optee-os should allocate shared memory to communicate with tee-supplicant. So once the communication is done, the underlying shared memory should be freed. I can't think of any scenario where optee-os should keep hold-off shared memory indefinitely.

I believe it can happen when OP-TEE's CFG_PREALLOC_RPC_CACHE is y. See the config file [1] and the commit which introduced this config [2].

Okay, I see the reasoning. So during the OP-TEE driver's lifetime, the RPC shared memory remains allocated. I guess that is done primarily for performance reasons.

But still it doesn't feel appropriate that we term all RPC shm allocations as not leaking memory as we might miss obvious ones.

Quoted 6 lines

Xiaolei,

Can you once test with CFG_PREALLOC_RPC_CACHE=n while compiling optee-os and see if the observed memory leak disappears or not?

-Sumit

Hi sumit

Quoted 8 lines

The version I am using has not increased the CFG_PREALLOC_RPC_CACHE

switch, I checked out to the latest version, but because of the need for

additional patches for the imx8 platform, I still have no way to test the

CFG_PREALLOC_RPC_CACHE=n situation

Can you just try to backport this [1] patch to your imx8 optee-os tree and test?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

[1] https://github.com/OP-TEE/optee_os/commit/8887663248ad

Hi sumit

I upgraded optee-os from version 3.2.0 to 3.13.0, and the kernel did not detect this problem.

Can you check if CFG_TEE_CORE_EMBED_INTERNAL_TESTS is enabled in optee-os version 3.13.0? As we would require atleast one RPC prealloc SHM invocation from OP-TEE for kmemleak to detect the problem.

Hi

CFG_TEE_CORE_EMBED_INTERNAL_TESTS is enabled
,I can see the "*.o" files compiled from the core/pta/tests directory

thanks
xiaolei

Quoted 24 lines

-Sumit

Quoted 21 lines

I have not set CFG_PREALLOC_RPC_CACHE to n. This should be a problem that occurs when compatible

with lower versions.

thanks

xiaolei

Quoted 11 lines

-Sumit

Quoted 9 lines

thanks

xiaolei

Quoted 5 lines
Quoted 5 lines

[1] https://github.com/OP-TEE/optee_os/blob/3.15.0/mk/config.mk#L709
[2] https://github.com/OP-TEE/optee_os/commit/8887663248ad

--
Jerome

#24 lore raw
In reply to Xiaolei Wang
Quoted 45 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

---
 drivers/tee/optee/smc_abi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tee/optee/smc_abi.c +1 -0
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
 #include "optee_private.h"
 #include "optee_smc.h"
 #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
 #define CREATE_TRACE_POINTS
 #include "optee_trace.h"
 

drivers/tee/optee/smc_abi.c static void optee_handle_rpc(struct tee_context *ctx, · line 787

Quoted 4 lines
drivers/tee/optee/smc_abi.c +1 -0
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
 			param->a4 = 0;
 			param->a5 = 0;
 		}
+		kmemleak_not_leak(shm);

Eventually this pointer will be freed below with the call to tee_shm_free(). I assume than once the memory is freed it's not execused from being a leak any longer. Is that correct?

Thanks,
Jens

Quoted 6 lines
drivers/tee/optee/smc_abi.c +0 -0
 		break;
 	case OPTEE_SMC_RPC_FUNC_FREE:
 		shm = reg_pair_to_ptr(param->a1, param->a2);
Signature · 1 line
2.25.1
#25 lore raw
Quoted 47 lines · Jens Wiklander

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 44 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):

   comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s)
   hex dump (first 32 bytes):
     00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
     60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
   backtrace:
     [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
     [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
     [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
     [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
     [<00000000c35884da>] optee_open_session+0x128/0x1ec
     [<000000001748f2ff>] tee_client_open_session+0x28/0x40
     [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
     [<000000003df18bf1>] optee_probe+0x674/0x6cc
     [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
     [<000000000c51ce7d>] really_probe+0xe4/0x4d0
     [<000000002f04c865>] driver_probe_device+0x58/0xc0
     [<00000000b485397d>] device_driver_attach+0xc0/0xd0
     [<00000000c835f0df>] __driver_attach+0x84/0x124
     [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
     [<000000001735e8a8>] driver_attach+0x24/0x30
     [<000000006d94b04f>] bus_add_driver+0x104/0x1ec

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

---
  drivers/tee/optee/smc_abi.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/tee/optee/smc_abi.c +1 -0
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
  #include "optee_private.h"
  #include "optee_smc.h"
  #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
  #define CREATE_TRACE_POINTS
  #include "optee_trace.h"
 
Quoted 8 lines

drivers/tee/optee/smc_abi.c static void optee_handle_rpc(struct tee_context *ctx, · line 787

Quoted 5 lines
drivers/tee/optee/smc_abi.c +1 -0
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
                       param->a4 = 0;
                       param->a5 = 0;
               }
+             kmemleak_not_leak(shm);

Eventually this pointer will be freed below with the call to tee_shm_free(). I assume than once the memory is freed it's not execused from being a leak any longer. Is that correct?

Yes, it is the correct way to release memory through tee_shm_free, but if a memory leak is detected by the kernel before free memory, it is obviously a false alarm.

thanks

xiaolei

Quoted 10 lines

Thanks,
Jens

Quoted 6 lines
drivers/tee/optee/smc_abi.c +0 -1
               break;
       case OPTEE_SMC_RPC_FUNC_FREE:
               shm = reg_pair_to_ptr(param->a1, param->a2);
--

2.25.1

#26 lore raw
In reply to Xiaolei Wang
Quoted 25 lines · Xiaolei Wang

We observed the following kmemleak report:
unreferenced object 0xffff000007904500 (size 128):
comm "swapper/0", pid 1, jiffies 4294892671 (age 44.036s) hex dump (first 32 bytes):

    00 47 90 07 00 00 ff ff 60 00 c0 ff 00 00 00 00  .G......`.......
    60 00 80 13 00 80 ff ff a0 00 00 00 00 00 00 00  `...............
  backtrace:
    [<000000004c12b1c7>] kmem_cache_alloc+0x1ac/0x2f4
    [<000000005d23eb4f>] tee_shm_alloc+0x78/0x230
    [<00000000794dd22c>] optee_handle_rpc+0x60/0x6f0
    [<00000000d9f7c52d>] optee_do_call_with_arg+0x17c/0x1dc
    [<00000000c35884da>] optee_open_session+0x128/0x1ec
    [<000000001748f2ff>] tee_client_open_session+0x28/0x40
    [<00000000aecb5389>] optee_enumerate_devices+0x84/0x2a0
    [<000000003df18bf1>] optee_probe+0x674/0x6cc
    [<000000003a4a534a>] platform_drv_probe+0x54/0xb0
    [<000000000c51ce7d>] really_probe+0xe4/0x4d0
    [<000000002f04c865>] driver_probe_device+0x58/0xc0
    [<00000000b485397d>] device_driver_attach+0xc0/0xd0
    [<00000000c835f0df>] __driver_attach+0x84/0x124
    [<000000008e5a429c>] bus_for_each_dev+0x70/0xc0
    [<000000001735e8a8>] driver_attach+0x24/0x30
    [<000000006d94b04f>] bus_add_driver+0x104/0x1ec
Quoted 7 lines

This is not a memory leak because we pass the share memory pointer to secure world and would get it from secure world before releasing it.

---
 drivers/tee/optee/smc_abi.c | 2 ++
 1 file changed, 2 insertions(+)

I'm picking up this.

Thanks,
Jens

Quoted 24 lines
drivers/tee/optee/smc_abi.c +2 -1
index 6196d7c3888f..cf2e3293567d 100644
@@ -23,6 +23,7 @@
 #include "optee_private.h"
 #include "optee_smc.h"
 #include "optee_rpc_cmd.h"
+#include <linux/kmemleak.h>
 #define CREATE_TRACE_POINTS
 #include "optee_trace.h"
 
@@ -783,6 +784,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
                        param->a4 = 0;
                        param->a5 = 0;
                }
+               kmemleak_not_leak(shm);
                break;
        case OPTEE_SMC_RPC_FUNC_FREE:
                shm = reg_pair_to_ptr(param->a1, param->a2);
--

2.25.1