Re: [PATCH v2 1/2] tee: amdtee: use page_alloc_exact() for memory allocations
From: Rijo Thomas
Date: Wed Oct 04 2023 - 07:00:11 EST
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@xxxxxxx>
> ---
> v2:
> * Replaced __get_free_pages() with alloc_pages_exact().
>
Reviewed-by: Rijo Thomas <Rijo-john.Thomas@xxxxxxx>
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;
> }
>