* [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations
@ 2023-08-29 20:19 Devaraj Rangasamy
2023-08-29 20:19 ` [PATCH v2 2/2] tee: amdtee: add support for CMA buffer allocations Devaraj Rangasamy
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Devaraj Rangasamy @ 2023-08-29 20:19 UTC (permalink / raw)
To: Jonathan Corbet, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H . Peter Anvin, Jens Wiklander, Sumit Garg,
Paul E . McKenney, Catalin Marinas, Randy Dunlap, Peter Zijlstra,
Steven Rostedt, Daniel Sneddon, Rijo Thomas, Devaraj Rangasamy,
SivaSangeetha SK, Josh Poimboeuf, Juergen Gross, Ard Biesheuvel,
Ross Lagerwall, Yuntao Wang, Sean Christopherson, Jarkko Nikula,
Herbert Xu, Tom Lendacky, Mario Limonciello, linux-doc,
linux-kernel, op-tee
Cc: Mythri PK, Nimesh Easow
Use page_alloc_exact() to get buffers, instead of
get_free_pages(), so as to avoid wastage of memory.
Currently get_free_pages() is allocating at next order,
while page_alloc_exact() will free the unused pages.
Signed-off-by: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
---
v2:
* Replaced __get_free_pages() with alloc_pages_exact().
drivers/tee/amdtee/shm_pool.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/drivers/tee/amdtee/shm_pool.c b/drivers/tee/amdtee/shm_pool.c
index f0303126f199..156e8a6f631f 100644
--- a/drivers/tee/amdtee/shm_pool.c
+++ b/drivers/tee/amdtee/shm_pool.c
@@ -4,6 +4,7 @@
*/
#include <linux/slab.h>
+#include <linux/mm.h>
#include <linux/tee_drv.h>
#include <linux/psp.h>
#include "amdtee_private.h"
@@ -11,26 +12,23 @@
static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
size_t size, size_t align)
{
- unsigned int order = get_order(size);
- unsigned long va;
+ void *va;
int rc;
- /*
- * Ignore alignment since this is already going to be page aligned
- * and there's no need for any larger alignment.
- */
- va = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
+ size = PAGE_ALIGN(size);
+
+ va = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
if (!va)
return -ENOMEM;
shm->kaddr = (void *)va;
shm->paddr = __psp_pa((void *)va);
- shm->size = PAGE_SIZE << order;
+ shm->size = size;
/* Map the allocated memory in to TEE */
rc = amdtee_map_shmem(shm);
if (rc) {
- free_pages(va, order);
+ free_pages_exact(va, size);
shm->kaddr = NULL;
return rc;
}
@@ -42,7 +40,7 @@ static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
{
/* Unmap the shared memory from TEE */
amdtee_unmap_shmem(shm);
- free_pages((unsigned long)shm->kaddr, get_order(shm->size));
+ free_pages_exact(shm->kaddr, shm->size);
shm->kaddr = NULL;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] tee: amdtee: add support for CMA buffer allocations
2023-08-29 20:19 [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations Devaraj Rangasamy
@ 2023-08-29 20:19 ` Devaraj Rangasamy
2023-10-04 10:59 ` [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations Rijo Thomas
2023-10-04 19:09 ` Jeff Johnson
2 siblings, 0 replies; 5+ messages in thread
From: Devaraj Rangasamy @ 2023-08-29 20:19 UTC (permalink / raw)
To: Jonathan Corbet, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H . Peter Anvin, Jens Wiklander, Sumit Garg,
Paul E . McKenney, Catalin Marinas, Randy Dunlap, Peter Zijlstra,
Steven Rostedt, Daniel Sneddon, Rijo Thomas, Devaraj Rangasamy,
SivaSangeetha SK, Josh Poimboeuf, Juergen Gross, Ard Biesheuvel,
Ross Lagerwall, Yuntao Wang, Sean Christopherson, Jarkko Nikula,
Herbert Xu, Tom Lendacky, Mario Limonciello, linux-doc,
linux-kernel, op-tee
Cc: Mythri PK, Nimesh Easow
amdtee driver shall use CMA region for contiguous
buffer allocation, if CMA is available.
since CMA and DMA contiguous APIs are not exported,
this support is enabled only when amdtee is built
as a builtin driver.
Signed-off-by: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
Signed-off-by: SivaSangeetha SK <SivaSangeetha.SK@amd.com>
Reviewed-by: Rijo Thomas <Rijo-john.Thomas@amd.com>
---
v2:
* Switched to generic CMA pool.
* Replaced __get_free_pages() with alloc_pages_exact().
* Compacted patch with simplified changes within single file.
drivers/tee/amdtee/shm_pool.c | 51 +++++++++++++++++++++++++++++++++--
1 file changed, 49 insertions(+), 2 deletions(-)
diff --git a/drivers/tee/amdtee/shm_pool.c b/drivers/tee/amdtee/shm_pool.c
index 156e8a6f631f..d504d9749114 100644
--- a/drivers/tee/amdtee/shm_pool.c
+++ b/drivers/tee/amdtee/shm_pool.c
@@ -5,10 +5,50 @@
#include <linux/slab.h>
#include <linux/mm.h>
+#include <linux/dma-map-ops.h>
#include <linux/tee_drv.h>
#include <linux/psp.h>
#include "amdtee_private.h"
+#if IS_BUILTIN(CONFIG_AMDTEE) && IS_ENABLED(CONFIG_DMA_CMA)
+static void *alloc_from_cma(size_t size)
+{
+
+ int nr_pages = size >> PAGE_SHIFT;
+ struct page *page;
+
+ page = dma_alloc_from_contiguous(NULL, nr_pages, 0, false);
+ if (page)
+ return page_to_virt(page);
+
+ return NULL;
+}
+
+static bool free_from_cma(struct tee_shm *shm)
+{
+
+ int nr_pages;
+ struct page *page;
+
+ if (!dev_get_cma_area(NULL))
+ return false;
+
+ nr_pages = shm->size >> PAGE_SHIFT;
+ page = virt_to_page(shm->kaddr);
+ return dma_release_from_contiguous(NULL, page, nr_pages);
+}
+#else
+static void *alloc_from_cma(size_t size)
+{
+ return NULL;
+}
+
+static bool free_from_cma(struct tee_shm *shm)
+{
+ return false;
+}
+#endif
+
static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
size_t size, size_t align)
{
@@ -17,7 +57,11 @@ static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
size = PAGE_ALIGN(size);
- va = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
+ va = alloc_from_cma(size);
+
+ if (!va)
+ va = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
+
if (!va)
return -ENOMEM;
@@ -40,7 +84,10 @@ static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
{
/* Unmap the shared memory from TEE */
amdtee_unmap_shmem(shm);
- free_pages_exact(shm->kaddr, shm->size);
+
+ if (!free_from_cma(shm))
+ free_pages_exact(shm->kaddr, shm->size);
+
shm->kaddr = NULL;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations
2023-08-29 20:19 [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations Devaraj Rangasamy
2023-08-29 20:19 ` [PATCH v2 2/2] tee: amdtee: add support for CMA buffer allocations Devaraj Rangasamy
@ 2023-10-04 10:59 ` Rijo Thomas
2023-10-04 19:09 ` Jeff Johnson
2 siblings, 0 replies; 5+ messages in thread
From: Rijo Thomas @ 2023-10-04 10:59 UTC (permalink / raw)
To: Devaraj Rangasamy, Jonathan Corbet, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H . Peter Anvin,
Jens Wiklander, Sumit Garg, Paul E . McKenney, Catalin Marinas,
Randy Dunlap, Peter Zijlstra, Steven Rostedt, Daniel Sneddon,
SivaSangeetha SK, Josh Poimboeuf, Juergen Gross, Ard Biesheuvel,
Ross Lagerwall, Yuntao Wang, Sean Christopherson, Jarkko Nikula,
Herbert Xu, Tom Lendacky, Mario Limonciello, linux-doc,
linux-kernel, op-tee
Cc: Mythri PK, Nimesh Easow
On 8/30/2023 1:49 AM, Devaraj Rangasamy wrote:
> Use page_alloc_exact() to get buffers, instead of
> get_free_pages(), so as to avoid wastage of memory.
> Currently get_free_pages() is allocating at next order,
> while page_alloc_exact() will free the unused pages.
>
> Signed-off-by: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
> ---
> v2:
> * Replaced __get_free_pages() with alloc_pages_exact().
>
Reviewed-by: Rijo Thomas <Rijo-john.Thomas@amd.com>
Thanks,
Rijo
> drivers/tee/amdtee/shm_pool.c | 18 ++++++++----------
> 1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/tee/amdtee/shm_pool.c b/drivers/tee/amdtee/shm_pool.c
> index f0303126f199..156e8a6f631f 100644
> --- a/drivers/tee/amdtee/shm_pool.c
> +++ b/drivers/tee/amdtee/shm_pool.c
> @@ -4,6 +4,7 @@
> */
>
> #include <linux/slab.h>
> +#include <linux/mm.h>
> #include <linux/tee_drv.h>
> #include <linux/psp.h>
> #include "amdtee_private.h"
> @@ -11,26 +12,23 @@
> static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
> size_t size, size_t align)
> {
> - unsigned int order = get_order(size);
> - unsigned long va;
> + void *va;
> int rc;
>
> - /*
> - * Ignore alignment since this is already going to be page aligned
> - * and there's no need for any larger alignment.
> - */
> - va = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
> + size = PAGE_ALIGN(size);
> +
> + va = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
> if (!va)
> return -ENOMEM;
>
> shm->kaddr = (void *)va;
> shm->paddr = __psp_pa((void *)va);
> - shm->size = PAGE_SIZE << order;
> + shm->size = size;
>
> /* Map the allocated memory in to TEE */
> rc = amdtee_map_shmem(shm);
> if (rc) {
> - free_pages(va, order);
> + free_pages_exact(va, size);
> shm->kaddr = NULL;
> return rc;
> }
> @@ -42,7 +40,7 @@ static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
> {
> /* Unmap the shared memory from TEE */
> amdtee_unmap_shmem(shm);
> - free_pages((unsigned long)shm->kaddr, get_order(shm->size));
> + free_pages_exact(shm->kaddr, shm->size);
> shm->kaddr = NULL;
> }
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations
2023-08-29 20:19 [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations Devaraj Rangasamy
2023-08-29 20:19 ` [PATCH v2 2/2] tee: amdtee: add support for CMA buffer allocations Devaraj Rangasamy
2023-10-04 10:59 ` [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations Rijo Thomas
@ 2023-10-04 19:09 ` Jeff Johnson
2023-10-23 14:01 ` Devaraj Rangasamy
2 siblings, 1 reply; 5+ messages in thread
From: Jeff Johnson @ 2023-10-04 19:09 UTC (permalink / raw)
To: Devaraj Rangasamy, Jonathan Corbet, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H . Peter Anvin,
Jens Wiklander, Sumit Garg, Paul E . McKenney, Catalin Marinas,
Randy Dunlap, Peter Zijlstra, Steven Rostedt, Daniel Sneddon,
Rijo Thomas, SivaSangeetha SK, Josh Poimboeuf, Juergen Gross,
Ard Biesheuvel, Ross Lagerwall, Yuntao Wang, Sean Christopherson,
Jarkko Nikula, Herbert Xu, Tom Lendacky, Mario Limonciello,
linux-doc, linux-kernel, op-tee
Cc: Mythri PK, Nimesh Easow
On 8/29/2023 1:19 PM, Devaraj Rangasamy wrote:> Use page_alloc_exact()
to get buffers, instead of
> get_free_pages(), so as to avoid wastage of memory.
> Currently get_free_pages() is allocating at next order,
> while page_alloc_exact() will free the unused pages.
s/page_alloc_exact()/alloc_pages_exact()/ everywhere including subject
to match the actual code change?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations
2023-10-04 19:09 ` Jeff Johnson
@ 2023-10-23 14:01 ` Devaraj Rangasamy
0 siblings, 0 replies; 5+ messages in thread
From: Devaraj Rangasamy @ 2023-10-23 14:01 UTC (permalink / raw)
To: Jeff Johnson, Jonathan Corbet, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H . Peter Anvin,
Jens Wiklander, Sumit Garg, Paul E . McKenney, Catalin Marinas,
Randy Dunlap, Peter Zijlstra, Steven Rostedt, Daniel Sneddon,
Rijo Thomas, SivaSangeetha SK, Josh Poimboeuf, Juergen Gross,
Ard Biesheuvel, Ross Lagerwall, Yuntao Wang, Sean Christopherson,
Jarkko Nikula, Herbert Xu, Tom Lendacky, Mario Limonciello,
linux-doc, linux-kernel, op-tee
Cc: Mythri PK, Nimesh Easow
On 10/5/2023 12:39 AM, Jeff Johnson wrote:
> On 8/29/2023 1:19 PM, Devaraj Rangasamy wrote:> Use page_alloc_exact()
> to get buffers, instead of
>> get_free_pages(), so as to avoid wastage of memory.
>> Currently get_free_pages() is allocating at next order,
>> while page_alloc_exact() will free the unused pages.
>
> s/page_alloc_exact()/alloc_pages_exact()/ everywhere including subject
> to match the actual code change?
Ack. Thanks for the catching this.
While I could send an updated patch taking care of this, we are working
on a rearchitected patch, due to a new requirement.
Kindly abandon this patch series, and we will be posting new support
soon to the community.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-10-23 14:02 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-29 20:19 [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations Devaraj Rangasamy
2023-08-29 20:19 ` [PATCH v2 2/2] tee: amdtee: add support for CMA buffer allocations Devaraj Rangasamy
2023-10-04 10:59 ` [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations Rijo Thomas
2023-10-04 19:09 ` Jeff Johnson
2023-10-23 14:01 ` Devaraj Rangasamy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox