Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Thierry Escande

On 30/11/2018 14:46, Arnd Bergmann wrote:

On Fri, Nov 30, 2018 at 2:20 PM Thierry Escande
 wrote:

On 30/11/2018 13:58, Arnd Bergmann wrote:

On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
 wrote:


From: Thierry Escande 

This patch adds support for compat ioctl from 32 bits userland to
Qualcomm fastrpc driver.

Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.

Most of the work is derived from various downstream Qualcomm kernels.
Credits to various Qualcomm authors who have contributed to this code.
Specially Tharun Kumar Merugu 

Signed-off-by: Thierry Escande 
Signed-off-by: Srinivas Kandagatla 


Can't you just define the native ioctls so that you don't need this.


There are long time defined structures that are passed as argument to
these ioctls and their sizes vary between 32 and 64 bits userlands, so
the ioctl command values.


Where? I don't see them in linux-4.19.


Unless I'm missing something here this also has the advantage not to be
compiled if CONFIG_COMPAT is not set.


You can normally just set .compat_ioctl() to the same function as
.unlocked_ioctl(), and get no overhead either way.


As you suggested for the other patches in this series, we will rework 
these ioctl data structures and then use the same function.


Regards,
Thierry


Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Thierry Escande

On 30/11/2018 14:46, Arnd Bergmann wrote:

On Fri, Nov 30, 2018 at 2:20 PM Thierry Escande
 wrote:

On 30/11/2018 13:58, Arnd Bergmann wrote:

On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
 wrote:


From: Thierry Escande 

This patch adds support for compat ioctl from 32 bits userland to
Qualcomm fastrpc driver.

Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.

Most of the work is derived from various downstream Qualcomm kernels.
Credits to various Qualcomm authors who have contributed to this code.
Specially Tharun Kumar Merugu 

Signed-off-by: Thierry Escande 
Signed-off-by: Srinivas Kandagatla 


Can't you just define the native ioctls so that you don't need this.


There are long time defined structures that are passed as argument to
these ioctls and their sizes vary between 32 and 64 bits userlands, so
the ioctl command values.


Where? I don't see them in linux-4.19.


Unless I'm missing something here this also has the advantage not to be
compiled if CONFIG_COMPAT is not set.


You can normally just set .compat_ioctl() to the same function as
.unlocked_ioctl(), and get no overhead either way.


As you suggested for the other patches in this series, we will rework 
these ioctl data structures and then use the same function.


Regards,
Thierry


Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Arnd Bergmann
On Fri, Nov 30, 2018 at 2:20 PM Thierry Escande
 wrote:
> On 30/11/2018 13:58, Arnd Bergmann wrote:
> > On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
> >  wrote:
> >>
> >> From: Thierry Escande 
> >>
> >> This patch adds support for compat ioctl from 32 bits userland to
> >> Qualcomm fastrpc driver.
> >>
> >> Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.
> >>
> >> Most of the work is derived from various downstream Qualcomm kernels.
> >> Credits to various Qualcomm authors who have contributed to this code.
> >> Specially Tharun Kumar Merugu 
> >>
> >> Signed-off-by: Thierry Escande 
> >> Signed-off-by: Srinivas Kandagatla 
> >
> > Can't you just define the native ioctls so that you don't need this.
>
> There are long time defined structures that are passed as argument to
> these ioctls and their sizes vary between 32 and 64 bits userlands, so
> the ioctl command values.

Where? I don't see them in linux-4.19.

> Unless I'm missing something here this also has the advantage not to be
> compiled if CONFIG_COMPAT is not set.

You can normally just set .compat_ioctl() to the same function as
.unlocked_ioctl(), and get no overhead either way.

Arnd


Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Arnd Bergmann
On Fri, Nov 30, 2018 at 2:20 PM Thierry Escande
 wrote:
> On 30/11/2018 13:58, Arnd Bergmann wrote:
> > On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
> >  wrote:
> >>
> >> From: Thierry Escande 
> >>
> >> This patch adds support for compat ioctl from 32 bits userland to
> >> Qualcomm fastrpc driver.
> >>
> >> Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.
> >>
> >> Most of the work is derived from various downstream Qualcomm kernels.
> >> Credits to various Qualcomm authors who have contributed to this code.
> >> Specially Tharun Kumar Merugu 
> >>
> >> Signed-off-by: Thierry Escande 
> >> Signed-off-by: Srinivas Kandagatla 
> >
> > Can't you just define the native ioctls so that you don't need this.
>
> There are long time defined structures that are passed as argument to
> these ioctls and their sizes vary between 32 and 64 bits userlands, so
> the ioctl command values.

Where? I don't see them in linux-4.19.

> Unless I'm missing something here this also has the advantage not to be
> compiled if CONFIG_COMPAT is not set.

You can normally just set .compat_ioctl() to the same function as
.unlocked_ioctl(), and get no overhead either way.

Arnd


Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Thierry Escande

Hi Arnd,

On 30/11/2018 13:58, Arnd Bergmann wrote:

On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
 wrote:


From: Thierry Escande 

This patch adds support for compat ioctl from 32 bits userland to
Qualcomm fastrpc driver.

Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.

Most of the work is derived from various downstream Qualcomm kernels.
Credits to various Qualcomm authors who have contributed to this code.
Specially Tharun Kumar Merugu 

Signed-off-by: Thierry Escande 
Signed-off-by: Srinivas Kandagatla 


Can't you just define the native ioctls so that you don't need this.


There are long time defined structures that are passed as argument to 
these ioctls and their sizes vary between 32 and 64 bits userlands, so 
the ioctl command values.


Unless I'm missing something here this also has the advantage not to be 
compiled if CONFIG_COMPAT is not set.


Regards,
Thierry


Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Thierry Escande

Hi Arnd,

On 30/11/2018 13:58, Arnd Bergmann wrote:

On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
 wrote:


From: Thierry Escande 

This patch adds support for compat ioctl from 32 bits userland to
Qualcomm fastrpc driver.

Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.

Most of the work is derived from various downstream Qualcomm kernels.
Credits to various Qualcomm authors who have contributed to this code.
Specially Tharun Kumar Merugu 

Signed-off-by: Thierry Escande 
Signed-off-by: Srinivas Kandagatla 


Can't you just define the native ioctls so that you don't need this.


There are long time defined structures that are passed as argument to 
these ioctls and their sizes vary between 32 and 64 bits userlands, so 
the ioctl command values.


Unless I'm missing something here this also has the advantage not to be 
compiled if CONFIG_COMPAT is not set.


Regards,
Thierry


Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Arnd Bergmann
On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
 wrote:
>
> From: Thierry Escande 
>
> This patch adds support for compat ioctl from 32 bits userland to
> Qualcomm fastrpc driver.
>
> Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.
>
> Most of the work is derived from various downstream Qualcomm kernels.
> Credits to various Qualcomm authors who have contributed to this code.
> Specially Tharun Kumar Merugu 
>
> Signed-off-by: Thierry Escande 
> Signed-off-by: Srinivas Kandagatla 

Can't you just define the native ioctls so that you don't need this.

 Arnd


Re: [RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Arnd Bergmann
On Fri, Nov 30, 2018 at 11:48 AM Srinivas Kandagatla
 wrote:
>
> From: Thierry Escande 
>
> This patch adds support for compat ioctl from 32 bits userland to
> Qualcomm fastrpc driver.
>
> Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.
>
> Most of the work is derived from various downstream Qualcomm kernels.
> Credits to various Qualcomm authors who have contributed to this code.
> Specially Tharun Kumar Merugu 
>
> Signed-off-by: Thierry Escande 
> Signed-off-by: Srinivas Kandagatla 

Can't you just define the native ioctls so that you don't need this.

 Arnd


[RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Srinivas Kandagatla
From: Thierry Escande 

This patch adds support for compat ioctl from 32 bits userland to
Qualcomm fastrpc driver.

Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.

Most of the work is derived from various downstream Qualcomm kernels.
Credits to various Qualcomm authors who have contributed to this code.
Specially Tharun Kumar Merugu 

Signed-off-by: Thierry Escande 
Signed-off-by: Srinivas Kandagatla 
---
 drivers/char/fastrpc.c | 279 +
 1 file changed, 279 insertions(+)

diff --git a/drivers/char/fastrpc.c b/drivers/char/fastrpc.c
index 3c52502eae9f..0b99a13109ea 100644
--- a/drivers/char/fastrpc.c
+++ b/drivers/char/fastrpc.c
@@ -3,6 +3,7 @@
 // Copyright (c) 2018, Linaro Limited
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1207,10 +1208,288 @@ static long fastrpc_device_ioctl(struct file *file, 
unsigned int cmd,
return err;
 }
 
+#ifdef CONFIG_COMPAT
+
+#define FASTRPC_COMPAT_IOCTL_ALLOC_DMA_BUFF \
+   _IOWR('R', 1, struct fastrpc_compat_ioctl_alloc_dma_buf)
+#define FASTRPC_COMPAT_IOCTL_FREE_DMA_BUFF \
+   _IOWR('R', 2, uint32_t)
+#define FASTRPC_COMPAT_IOCTL_INVOKE \
+   _IOWR('R', 3, struct fastrpc_compat_ioctl_invoke)
+#define FASTRPC_COMPAT_IOCTL_INIT \
+   _IOWR('R', 4, struct fastrpc_compat_ioctl_init)
+
+struct compat_remote_buf {
+   compat_uptr_t pv;   /* buffer pointer */
+   compat_size_t len;  /* length of buffer */
+};
+
+union compat_remote_arg {
+   struct compat_remote_buf buf;
+   compat_uint_t h;
+};
+
+struct fastrpc_compat_ioctl_alloc_dma_buf {
+   compat_int_t   fd;
+   compat_ssize_t size;
+   compat_uint_t  flags;
+};
+
+struct fastrpc_compat_ioctl_invoke {
+   compat_uint_t handle;   /* remote handle */
+   compat_uint_t sc;   /* scalars describing the data */
+   compat_uptr_t pra;  /* remote arguments list */
+   compat_uptr_t fds;  /* fd list */
+   compat_uptr_t attrs;/* attribute list */
+   compat_uptr_t crc;  /* crc list */
+};
+
+struct fastrpc_compat_ioctl_init {
+   compat_uint_t flags;/* one of FASTRPC_INIT_* macros */
+   compat_uptr_t file; /* pointer to elf file */
+   compat_int_t filelen;   /* elf file length */
+   compat_int_t filefd;/* dmabuf fd for the file */
+   compat_uptr_t mem;  /* mem for the PD */
+   compat_int_t memlen;/* mem length */
+   compat_int_t memfd; /* dmabuf fd for the mem */
+   compat_int_t attrs; /* attributes to init process */
+   compat_int_t siglen;/* test signature file length */
+};
+
+static int fastrpc_compat_get_ioctl_alloc_dma_buf(
+   struct fastrpc_compat_ioctl_alloc_dma_buf __user *buf32,
+   struct fastrpc_ioctl_alloc_dma_buf __user *buf)
+{
+   compat_size_t size;
+   compat_uint_t flags;
+   int err;
+
+   err = put_user(0, >fd);
+   err |= get_user(size, >size);
+   err |= put_user(size, >size);
+   err |= get_user(flags, >flags);
+   err |= put_user(flags, >flags);
+
+   return err;
+}
+
+static int fastrpc_compat_put_ioctl_alloc_dma_buf(
+   struct fastrpc_compat_ioctl_alloc_dma_buf __user *buf32,
+   struct fastrpc_ioctl_alloc_dma_buf __user *buf)
+{
+   compat_int_t fd;
+   int err;
+
+   err = get_user(fd, >fd);
+   err |= put_user(fd, >fd);
+
+   return err;
+}
+
+static int compat_get_fastrpc_ioctl_invoke(
+   struct fastrpc_compat_ioctl_invoke __user *inv32,
+   struct fastrpc_ioctl_invoke __user **inva)
+{
+   compat_uint_t u, sc;
+   compat_size_t s;
+   compat_uptr_t p;
+   struct fastrpc_ioctl_invoke *inv;
+   union compat_remote_arg *pra32;
+   union remote_arg *pra;
+   int err, len, j;
+
+   err = get_user(sc, >sc);
+   if (err)
+   return err;
+
+   len = REMOTE_SCALARS_LENGTH(sc);
+   inv = compat_alloc_user_space(sizeof(*inv) + len * sizeof(*pra));
+   if (!inv)
+   return -EFAULT;
+
+   pra = (union remote_arg *)(inv + 1);
+   err = put_user(pra, >pra);
+   err |= put_user(sc, >sc);
+   err |= get_user(u, >handle);
+   err |= put_user(u, >handle);
+   err |= get_user(p, >pra);
+   if (err)
+   return err;
+
+   pra32 = compat_ptr(p);
+   pra = (union remote_arg *)(inv + 1);
+   for (j = 0; j < len; j++) {
+   err |= get_user(p, [j].buf.pv);
+   err |= put_user(p, (uintptr_t *)[j].buf.pv);
+   err |= get_user(s, [j].buf.len);
+   err |= put_user(s, [j].buf.len);
+   }
+
+   err |= put_user(NULL, >fds);
+   if (inv32->fds) {
+   err |= get_user(p, >fds);
+   err |= put_user(p, (compat_uptr_t *)>fds);
+   }
+
+   err |= put_user(NULL, 

[RFC PATCH 6/6] char: fastrpc: Add support for compat ioctls

2018-11-30 Thread Srinivas Kandagatla
From: Thierry Escande 

This patch adds support for compat ioctl from 32 bits userland to
Qualcomm fastrpc driver.

Supported ioctls in this change are INIT, INVOKE, and ALLOC/FREE_DMA.

Most of the work is derived from various downstream Qualcomm kernels.
Credits to various Qualcomm authors who have contributed to this code.
Specially Tharun Kumar Merugu 

Signed-off-by: Thierry Escande 
Signed-off-by: Srinivas Kandagatla 
---
 drivers/char/fastrpc.c | 279 +
 1 file changed, 279 insertions(+)

diff --git a/drivers/char/fastrpc.c b/drivers/char/fastrpc.c
index 3c52502eae9f..0b99a13109ea 100644
--- a/drivers/char/fastrpc.c
+++ b/drivers/char/fastrpc.c
@@ -3,6 +3,7 @@
 // Copyright (c) 2018, Linaro Limited
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1207,10 +1208,288 @@ static long fastrpc_device_ioctl(struct file *file, 
unsigned int cmd,
return err;
 }
 
+#ifdef CONFIG_COMPAT
+
+#define FASTRPC_COMPAT_IOCTL_ALLOC_DMA_BUFF \
+   _IOWR('R', 1, struct fastrpc_compat_ioctl_alloc_dma_buf)
+#define FASTRPC_COMPAT_IOCTL_FREE_DMA_BUFF \
+   _IOWR('R', 2, uint32_t)
+#define FASTRPC_COMPAT_IOCTL_INVOKE \
+   _IOWR('R', 3, struct fastrpc_compat_ioctl_invoke)
+#define FASTRPC_COMPAT_IOCTL_INIT \
+   _IOWR('R', 4, struct fastrpc_compat_ioctl_init)
+
+struct compat_remote_buf {
+   compat_uptr_t pv;   /* buffer pointer */
+   compat_size_t len;  /* length of buffer */
+};
+
+union compat_remote_arg {
+   struct compat_remote_buf buf;
+   compat_uint_t h;
+};
+
+struct fastrpc_compat_ioctl_alloc_dma_buf {
+   compat_int_t   fd;
+   compat_ssize_t size;
+   compat_uint_t  flags;
+};
+
+struct fastrpc_compat_ioctl_invoke {
+   compat_uint_t handle;   /* remote handle */
+   compat_uint_t sc;   /* scalars describing the data */
+   compat_uptr_t pra;  /* remote arguments list */
+   compat_uptr_t fds;  /* fd list */
+   compat_uptr_t attrs;/* attribute list */
+   compat_uptr_t crc;  /* crc list */
+};
+
+struct fastrpc_compat_ioctl_init {
+   compat_uint_t flags;/* one of FASTRPC_INIT_* macros */
+   compat_uptr_t file; /* pointer to elf file */
+   compat_int_t filelen;   /* elf file length */
+   compat_int_t filefd;/* dmabuf fd for the file */
+   compat_uptr_t mem;  /* mem for the PD */
+   compat_int_t memlen;/* mem length */
+   compat_int_t memfd; /* dmabuf fd for the mem */
+   compat_int_t attrs; /* attributes to init process */
+   compat_int_t siglen;/* test signature file length */
+};
+
+static int fastrpc_compat_get_ioctl_alloc_dma_buf(
+   struct fastrpc_compat_ioctl_alloc_dma_buf __user *buf32,
+   struct fastrpc_ioctl_alloc_dma_buf __user *buf)
+{
+   compat_size_t size;
+   compat_uint_t flags;
+   int err;
+
+   err = put_user(0, >fd);
+   err |= get_user(size, >size);
+   err |= put_user(size, >size);
+   err |= get_user(flags, >flags);
+   err |= put_user(flags, >flags);
+
+   return err;
+}
+
+static int fastrpc_compat_put_ioctl_alloc_dma_buf(
+   struct fastrpc_compat_ioctl_alloc_dma_buf __user *buf32,
+   struct fastrpc_ioctl_alloc_dma_buf __user *buf)
+{
+   compat_int_t fd;
+   int err;
+
+   err = get_user(fd, >fd);
+   err |= put_user(fd, >fd);
+
+   return err;
+}
+
+static int compat_get_fastrpc_ioctl_invoke(
+   struct fastrpc_compat_ioctl_invoke __user *inv32,
+   struct fastrpc_ioctl_invoke __user **inva)
+{
+   compat_uint_t u, sc;
+   compat_size_t s;
+   compat_uptr_t p;
+   struct fastrpc_ioctl_invoke *inv;
+   union compat_remote_arg *pra32;
+   union remote_arg *pra;
+   int err, len, j;
+
+   err = get_user(sc, >sc);
+   if (err)
+   return err;
+
+   len = REMOTE_SCALARS_LENGTH(sc);
+   inv = compat_alloc_user_space(sizeof(*inv) + len * sizeof(*pra));
+   if (!inv)
+   return -EFAULT;
+
+   pra = (union remote_arg *)(inv + 1);
+   err = put_user(pra, >pra);
+   err |= put_user(sc, >sc);
+   err |= get_user(u, >handle);
+   err |= put_user(u, >handle);
+   err |= get_user(p, >pra);
+   if (err)
+   return err;
+
+   pra32 = compat_ptr(p);
+   pra = (union remote_arg *)(inv + 1);
+   for (j = 0; j < len; j++) {
+   err |= get_user(p, [j].buf.pv);
+   err |= put_user(p, (uintptr_t *)[j].buf.pv);
+   err |= get_user(s, [j].buf.len);
+   err |= put_user(s, [j].buf.len);
+   }
+
+   err |= put_user(NULL, >fds);
+   if (inv32->fds) {
+   err |= get_user(p, >fds);
+   err |= put_user(p, (compat_uptr_t *)>fds);
+   }
+
+   err |= put_user(NULL,