Skip to contentSkip to conversation

optee: Fix multi page dynamic shm pool alloc

PATCH Superseded Sumit Garg 2 participants 3 messages Nov 19, 2019 – Dec 17, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sumit Garg

author
#0 lore raw

PATCH 1/1 optee: Fix multi page dynamic shm pool alloc

optee_shm_register() expected pages to be passed as an array of page pointers rather than as an array of contiguous pages. So fix that via correctly passing pages as per expectation.

Fixes a249dd200d03 ("tee: optee: Fix dynamic shm pool allocations")
---
 drivers/tee/optee/shm_pool.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/tee/optee/shm_pool.c +13 -1
index 0332a53..85aa5bb 100644
@@ -28,8 +28,20 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
 	shm->size = PAGE_SIZE << order;
 
 	if (shm->flags & TEE_SHM_DMA_BUF) {
+		unsigned int nr_pages = 1 << order, i;
+		struct page **pages;
+
+		pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL);
+		if (!pages)
+			return -ENOMEM;
+
+		for (i = 0; i < nr_pages; i++) {
+			pages[i] = page;
+			page++;
+		}
+
 		shm->flags |= TEE_SHM_REGISTER;
-		rc = optee_shm_register(shm->ctx, shm, &page, 1 << order,
+		rc = optee_shm_register(shm->ctx, shm, pages, nr_pages,
 					(unsigned long)shm->kaddr);
 	}
 
Signature · 1 line
2.7.4
#1 lore raw

Hi Sumit,

Quoted 27 lines · Sumit Garg

optee_shm_register() expected pages to be passed as an array of page pointers rather than as an array of contiguous pages. So fix that via correctly passing pages as per expectation.

Fixes a249dd200d03 ("tee: optee: Fix dynamic shm pool allocations")
---
 drivers/tee/optee/shm_pool.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/tee/optee/shm_pool.c +7 -0
index 0332a53..85aa5bb 100644
@@ -28,8 +28,20 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
 	shm->size = PAGE_SIZE << order;
 
 	if (shm->flags & TEE_SHM_DMA_BUF) {
+		unsigned int nr_pages = 1 << order, i;
+		struct page **pages;
+
+		pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL);
+		if (!pages)
+			return -ENOMEM;
+

drivers/tee/optee/shm_pool.c static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, · line 46

Quoted 10 lines
drivers/tee/optee/shm_pool.c +6 -1
+		for (i = 0; i < nr_pages; i++) {
+			pages[i] = page;
+			page++;
+		}
+
 		shm->flags |= TEE_SHM_REGISTER;
-		rc = optee_shm_register(shm->ctx, shm, &page, 1 << order,
+		rc = optee_shm_register(shm->ctx, shm, pages, nr_pages,
 					(unsigned long)shm->kaddr);
 	}

Apoligies for the later reply. It seems that this will leak memory. The pointer pages isn't freed after the call to optee_shm_register().

Thanks,
Jens

author
#2 lore raw

Hi Jens,

Quoted 35 lines · Jens Wiklander

Hi Sumit,

Quoted 31 lines · Sumit Garg

optee_shm_register() expected pages to be passed as an array of page pointers rather than as an array of contiguous pages. So fix that via correctly passing pages as per expectation.

Fixes a249dd200d03 ("tee: optee: Fix dynamic shm pool allocations")
---
 drivers/tee/optee/shm_pool.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/tee/optee/shm_pool.c +11 -0
index 0332a53..85aa5bb 100644
@@ -28,8 +28,20 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
      shm->size = PAGE_SIZE << order;
 
      if (shm->flags & TEE_SHM_DMA_BUF) {
+             unsigned int nr_pages = 1 << order, i;
+             struct page **pages;
+
+             pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL);
+             if (!pages)
+                     return -ENOMEM;
+
+             for (i = 0; i < nr_pages; i++) {
+                     pages[i] = page;
+                     page++;
+             }
Quoted 8 lines

drivers/tee/optee/shm_pool.c static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, · line 46

Quoted 6 lines
drivers/tee/optee/shm_pool.c +2 -1
+
              shm->flags |= TEE_SHM_REGISTER;
-             rc = optee_shm_register(shm->ctx, shm, &page, 1 << order,
+             rc = optee_shm_register(shm->ctx, shm, pages, nr_pages,
                                      (unsigned long)shm->kaddr);
      }

Apoligies for the later reply.

No worries.

Quoted 3 lines

It seems that this will leak memory.
The pointer pages isn't freed after the call to optee_shm_register().

Will fix it in v2.

-Sumit

Quoted 2 lines

Thanks,
Jens