diff mbox series

[v2,1/6] xen/arm: optee: impose limit on shared buffer size

Message ID 20190918185041.22738-2-volodymyr_babchuk@epam.com (mailing list archive)
State Superseded
Headers show
Series arch/arm: optee: fix TODOs and change status to "Tech Preview" | expand

Commit Message

Volodymyr Babchuk Sept. 18, 2019, 6:50 p.m. UTC
We want to limit number of calls to lookup_and_pin_guest_ram_addr()
per one request. There are two ways to do this: either preempt
translate_noncontig() or limit size of one shared buffer size.

It is quite hard to preempt translate_noncontig(), because it is deep
nested. So we chose the second option. We will allow 129 pages per one
shared buffer. This corresponds to the GP standard, as it requires
that size limit for shared buffer should be at least 512kB. One extra
page (129th) is needed to cope with the fact that user's buffer is not
necessary aligned with page boundary.

Also, with this limitation OP-TEE still passes own "xtest" test suite,
so this is okay for now.

Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@epam.com>

---

Changes from v1:
 - Added comment before BUILD_BUG_ON(PAGE_SIZE != 4096);
 - Fixed typo in the commit message
 - Decreased MAX_SHM_BUFFER_PG to 129
---
 xen/arch/arm/tee/optee.c | 44 +++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 12 deletions(-)

Comments

Julien Grall Sept. 23, 2019, 9:29 a.m. UTC | #1
Hi Volodymyr,

On 18/09/2019 19:50, Volodymyr Babchuk wrote:
> We want to limit number of calls to lookup_and_pin_guest_ram_addr()
> per one request. There are two ways to do this: either preempt
> translate_noncontig() or limit size of one shared buffer size.
> 
> It is quite hard to preempt translate_noncontig(), because it is deep
> nested. So we chose the second option. We will allow 129 pages per one
> shared buffer. This corresponds to the GP standard, as it requires
> that size limit for shared buffer should be at least 512kB. One extra
> page (129th) is needed to cope with the fact that user's buffer is not
> necessary aligned with page boundary.
> 
> Also, with this limitation OP-TEE still passes own "xtest" test suite,
> so this is okay for now.
> 
> Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@epam.com>
> 
> ---
> 
> Changes from v1:
>   - Added comment before BUILD_BUG_ON(PAGE_SIZE != 4096);
>   - Fixed typo in the commit message
>   - Decreased MAX_SHM_BUFFER_PG to 129
> ---
>   xen/arch/arm/tee/optee.c | 44 +++++++++++++++++++++++++++++-----------
>   1 file changed, 32 insertions(+), 12 deletions(-)
> 
> diff --git a/xen/arch/arm/tee/optee.c b/xen/arch/arm/tee/optee.c
> index ec5402e89b..d64e9c3b85 100644
> --- a/xen/arch/arm/tee/optee.c
> +++ b/xen/arch/arm/tee/optee.c
> @@ -72,6 +72,19 @@
>    */
>   #define MAX_TOTAL_SMH_BUF_PG    16384
>   
> +/*
> + * Limit for shared buffer size. Please note that this define limits
> + * number of pages. But user buffer can be not aligned to a page
> + * boundary. So it is possible that user would not be able to share
> + * exactly MAX_SHM_BUFFER_PG * PAGE_SIZE bytes with OP-TEE.
> + *
> + * Global Platform specification for TEE requires that any TEE
> + * implementation should allow to share buffers with size of at least
> + * 512KB, which equals to 128 4KB pages. Due to align issue mentioned
> + * above, we need to increase this value to 129.
> + */
> +#define MAX_SHM_BUFFER_PG       129
> +
>   #define OPTEE_KNOWN_NSEC_CAPS OPTEE_SMC_NSEC_CAP_UNIPROCESSOR
>   #define OPTEE_KNOWN_SEC_CAPS (OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM | \
>                                 OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM | \
> @@ -697,16 +710,29 @@ static int translate_noncontig(struct optee_domain *ctx,
>       size = ROUNDUP(param->u.tmem.size + offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
>   
>       pg_count = DIV_ROUND_UP(size, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
> +    if ( pg_count > MAX_SHM_BUFFER_PG )
> +        return -ENOMEM;
> +
>       order = get_order_from_bytes(get_pages_list_size(pg_count));
>   
>       /*
> -     * In the worst case we will want to allocate 33 pages, which is
> -     * MAX_TOTAL_SMH_BUF_PG/511 rounded up. This gives order 6 or at
> -     * most 64 pages allocated. This buffer will be freed right after
> -     * the end of the call and there can be no more than
> +     * In the worst case we will want to allocate 1 page, which is
> +     * MAX_SHM_BUFFER_PG/511 rounded up. This buffer will be freed
> +     * right after the end of the call and there can be no more than
>        * max_optee_threads calls simultaneously. So in the worst case
> -     * guest can trick us to allocate 64 * max_optee_threads pages in
> +     * guest can trick us to allocate 1 * max_optee_threads pages in
>        * total.
> +     *
> +     * It may seem strange to have such complex calculations if we
> +     * always will allocate exactly one page. Those calculations exist
> +     * in the first place because earlier there were bigger limit for
> +     * shared buffer size, so there were cases, when we needed more
> +     * that one page there. Right now this is not true, but this code
> +     * remains for two reasons:
> +     * - Users can change MAX_SHM_BUFFER_PG to a higher value, in which
> +     *   case they will need this code.
> +     * - There is a plan to implement preemption in the code below, which
> +     *   will allow use to increase default MAX_SHM_BUFFER_PG value.
>        */
>       xen_pgs = alloc_domheap_pages(current->domain, order, 0);
>       if ( !xen_pgs )
> @@ -747,13 +773,7 @@ static int translate_noncontig(struct optee_domain *ctx,
>               xen_data = __map_domain_page(xen_pgs);
>           }
>   
> -        /*
> -         * TODO: That function can pin up to 64MB of guest memory by
> -         * calling lookup_and_pin_guest_ram_addr() 16384 times
> -         * (assuming that PAGE_SIZE equals to 4096).
> -         * This should be addressed before declaring OP-TEE security
> -         * supported.
> -         */
> +        /* Only 4k pages are supported right now */

NIT: s/4k/4KB/ to stay consistent.

>           BUILD_BUG_ON(PAGE_SIZE != 4096);
>           page = get_domain_ram_page(gaddr_to_gfn(guest_data->pages_list[idx]));
>           if ( !page )
> 

Cheers,
diff mbox series

Patch

diff --git a/xen/arch/arm/tee/optee.c b/xen/arch/arm/tee/optee.c
index ec5402e89b..d64e9c3b85 100644
--- a/xen/arch/arm/tee/optee.c
+++ b/xen/arch/arm/tee/optee.c
@@ -72,6 +72,19 @@ 
  */
 #define MAX_TOTAL_SMH_BUF_PG    16384
 
+/*
+ * Limit for shared buffer size. Please note that this define limits
+ * number of pages. But user buffer can be not aligned to a page
+ * boundary. So it is possible that user would not be able to share
+ * exactly MAX_SHM_BUFFER_PG * PAGE_SIZE bytes with OP-TEE.
+ *
+ * Global Platform specification for TEE requires that any TEE
+ * implementation should allow to share buffers with size of at least
+ * 512KB, which equals to 128 4KB pages. Due to align issue mentioned
+ * above, we need to increase this value to 129.
+ */
+#define MAX_SHM_BUFFER_PG       129
+
 #define OPTEE_KNOWN_NSEC_CAPS OPTEE_SMC_NSEC_CAP_UNIPROCESSOR
 #define OPTEE_KNOWN_SEC_CAPS (OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM | \
                               OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM | \
@@ -697,16 +710,29 @@  static int translate_noncontig(struct optee_domain *ctx,
     size = ROUNDUP(param->u.tmem.size + offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
 
     pg_count = DIV_ROUND_UP(size, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
+    if ( pg_count > MAX_SHM_BUFFER_PG )
+        return -ENOMEM;
+
     order = get_order_from_bytes(get_pages_list_size(pg_count));
 
     /*
-     * In the worst case we will want to allocate 33 pages, which is
-     * MAX_TOTAL_SMH_BUF_PG/511 rounded up. This gives order 6 or at
-     * most 64 pages allocated. This buffer will be freed right after
-     * the end of the call and there can be no more than
+     * In the worst case we will want to allocate 1 page, which is
+     * MAX_SHM_BUFFER_PG/511 rounded up. This buffer will be freed
+     * right after the end of the call and there can be no more than
      * max_optee_threads calls simultaneously. So in the worst case
-     * guest can trick us to allocate 64 * max_optee_threads pages in
+     * guest can trick us to allocate 1 * max_optee_threads pages in
      * total.
+     *
+     * It may seem strange to have such complex calculations if we
+     * always will allocate exactly one page. Those calculations exist
+     * in the first place because earlier there were bigger limit for
+     * shared buffer size, so there were cases, when we needed more
+     * that one page there. Right now this is not true, but this code
+     * remains for two reasons:
+     * - Users can change MAX_SHM_BUFFER_PG to a higher value, in which
+     *   case they will need this code.
+     * - There is a plan to implement preemption in the code below, which
+     *   will allow use to increase default MAX_SHM_BUFFER_PG value.
      */
     xen_pgs = alloc_domheap_pages(current->domain, order, 0);
     if ( !xen_pgs )
@@ -747,13 +773,7 @@  static int translate_noncontig(struct optee_domain *ctx,
             xen_data = __map_domain_page(xen_pgs);
         }
 
-        /*
-         * TODO: That function can pin up to 64MB of guest memory by
-         * calling lookup_and_pin_guest_ram_addr() 16384 times
-         * (assuming that PAGE_SIZE equals to 4096).
-         * This should be addressed before declaring OP-TEE security
-         * supported.
-         */
+        /* Only 4k pages are supported right now */
         BUILD_BUG_ON(PAGE_SIZE != 4096);
         page = get_domain_ram_page(gaddr_to_gfn(guest_data->pages_list[idx]));
         if ( !page )