From: Gustavo Padovan <[email protected]>
Inform userspace how many fences are in the sync_fence_info field.
Signed-off-by: Gustavo Padovan <[email protected]>
Reviewed-by: Maarten Lankhorst <[email protected]>
---
drivers/staging/android/sync.c | 2 ++
drivers/staging/android/uapi/sync.h | 2 ++
2 files changed, 4 insertions(+)
diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c
index 3a8f210..31aa462 100644
--- a/drivers/staging/android/sync.c
+++ b/drivers/staging/android/sync.c
@@ -525,6 +525,8 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
if (info->status >= 0)
info->status = !info->status;
+ info->num_fences = sync_file->num_fences;
+
len = sizeof(struct sync_file_info);
for (i = 0; i < sync_file->num_fences; ++i) {
diff --git a/drivers/staging/android/uapi/sync.h b/drivers/staging/android/uapi/sync.h
index a0cf357..4ffb7cc 100644
--- a/drivers/staging/android/uapi/sync.h
+++ b/drivers/staging/android/uapi/sync.h
@@ -47,12 +47,14 @@ struct sync_fence_info {
* userspace including pt_info.
* @name: name of fence
* @status: status of fence. 1: signaled 0:active <0:error
+ * @num_fences number of fences in the sync_file
* @sync_fence_info: array of sync_fence_info for every fence in the sync_file
*/
struct sync_file_info {
__u32 len;
char name[32];
__s32 status;
+ __u32 num_fences;
__u8 sync_fence_info[0];
};
--
2.5.0
From: Gustavo Padovan <[email protected]>
We don't use the 'fence' name to refer to sync_file anymore. So rename it
to SYNC_IOC_FILE_INFO.
Signed-off-by: Gustavo Padovan <[email protected]>
Reviewed-by: Maarten Lankhorst <[email protected]>
---
drivers/staging/android/sync.c | 2 +-
drivers/staging/android/uapi/sync.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c
index 31aa462..dc5f382 100644
--- a/drivers/staging/android/sync.c
+++ b/drivers/staging/android/sync.c
@@ -562,7 +562,7 @@ static long sync_file_ioctl(struct file *file, unsigned int cmd,
case SYNC_IOC_MERGE:
return sync_file_ioctl_merge(sync_file, arg);
- case SYNC_IOC_FENCE_INFO:
+ case SYNC_IOC_FILE_INFO:
return sync_file_ioctl_fence_info(sync_file, arg);
default:
diff --git a/drivers/staging/android/uapi/sync.h b/drivers/staging/android/uapi/sync.h
index 4ffb7cc..dd0dd84 100644
--- a/drivers/staging/android/uapi/sync.h
+++ b/drivers/staging/android/uapi/sync.h
@@ -81,6 +81,6 @@ struct sync_file_info {
* pt_info is a buffer containing sync_pt_infos for every sync_pt in the fence.
* To iterate over the sync_pt_infos, use the sync_pt_info.len field.
*/
-#define SYNC_IOC_FENCE_INFO _IOWR(SYNC_IOC_MAGIC, 2, struct sync_file_info)
+#define SYNC_IOC_FILE_INFO _IOWR(SYNC_IOC_MAGIC, 2, struct sync_file_info)
#endif /* _UAPI_LINUX_SYNC_H */
--
2.5.0
From: Gustavo Padovan <[email protected]>
Change SYNC_IOC_FILE_INFO behaviour to avoid future API breaks and
optimize buffer
Now num_fences can be filled by the caller to inform how many fences it
wants to retrieve from the kernel. If the num_fences passed is greater
than zero info->sync_fence_info should point to a buffer with enough space
to fit all fences.
However if num_fences passed to the kernel is 0, the kernel will reply
with number of fences of the sync_file.
Sending first an ioctl with num_fences = 0 can optimize buffer allocation,
in a first call with num_fences = 0 userspace will receive the actual
number of fences in the num_fences filed.
Then it can allocate a buffer with the correct size on sync_fence_info and
call SYNC_IOC_FILE_INFO again, but now with the actual value of num_fences
in the sync_file.
Also, info->sync_fence_info was converted to __u64 pointer to prevent
32bit compatibility issues.
An example userspace code for the later would be:
struct sync_file_info *info;
int err, size, num_fences;
info = malloc(sizeof(*info));
info.flags = 0;
err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
num_fences = info->num_fences;
if (num_fences) {
info.flags = 0;
size = sizeof(struct sync_fence_info) * num_fences;
info->num_fences = num_fences;
info->sync_fence_info = (uint64_t) calloc(num_fences,
sizeof(struct sync_fence_info));
err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
}
Signed-off-by: Gustavo Padovan <[email protected]>
Reviewed-by: Maarten Lankhorst <[email protected]>
---
v2: fix fence_info memory leak
v3: Comments from Emil Velikov
- improve commit message
- remove __u64 cast
- remove check for output fields in file_info
- clean up sync_fill_fence_info()
Comments from Maarten Lankhorst
- remove in.num_fences && !in.sync_fence_info check
- remove info->len and use only num_fences to calculate size
Comments from Dan Carpenter
- fix info->sync_fence_info documentation
v4: remove allocated struct sync_file_info (comment from Maarten)
---
drivers/staging/android/sync.c | 70 +++++++++++++++++--------------------
drivers/staging/android/uapi/sync.h | 9 ++---
2 files changed, 36 insertions(+), 43 deletions(-)
diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c
index dc5f382..48ee175 100644
--- a/drivers/staging/android/sync.c
+++ b/drivers/staging/android/sync.c
@@ -479,13 +479,9 @@ err_put_fd:
return err;
}
-static int sync_fill_fence_info(struct fence *fence, void *data, int size)
+static void sync_fill_fence_info(struct fence *fence,
+ struct sync_fence_info *info)
{
- struct sync_fence_info *info = data;
-
- if (size < sizeof(*info))
- return -ENOMEM;
-
strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
sizeof(info->obj_name));
strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
@@ -495,60 +491,60 @@ static int sync_fill_fence_info(struct fence *fence, void *data, int size)
else
info->status = 0;
info->timestamp_ns = ktime_to_ns(fence->timestamp);
-
- return sizeof(*info);
}
static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
unsigned long arg)
{
- struct sync_file_info *info;
+ struct sync_file_info info;
+ struct sync_fence_info *fence_info = NULL;
__u32 size;
- __u32 len = 0;
int ret, i;
- if (copy_from_user(&size, (void __user *)arg, sizeof(size)))
+ if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
return -EFAULT;
- if (size < sizeof(struct sync_file_info))
- return -EINVAL;
+ /*
+ * Passing num_fences = 0 means that userspace doesn't want to
+ * retrieve any sync_fence_info. If num_fences = 0 we skip filling
+ * sync_fence_info and return the actual number of fences on
+ * info->num_fences.
+ */
+ if (!info.num_fences)
+ goto no_fences;
- if (size > 4096)
- size = 4096;
+ if (info.num_fences < sync_file->num_fences)
+ return -EINVAL;
- info = kzalloc(size, GFP_KERNEL);
- if (!info)
+ size = sync_file->num_fences * sizeof(*fence_info);
+ fence_info = kzalloc(size, GFP_KERNEL);
+ if (!fence_info)
return -ENOMEM;
- strlcpy(info->name, sync_file->name, sizeof(info->name));
- info->status = atomic_read(&sync_file->status);
- if (info->status >= 0)
- info->status = !info->status;
+ for (i = 0; i < sync_file->num_fences; ++i)
+ sync_fill_fence_info(sync_file->cbs[i].fence, &fence_info[i]);
- info->num_fences = sync_file->num_fences;
-
- len = sizeof(struct sync_file_info);
-
- for (i = 0; i < sync_file->num_fences; ++i) {
- struct fence *fence = sync_file->cbs[i].fence;
-
- ret = sync_fill_fence_info(fence, (u8 *)info + len, size - len);
-
- if (ret < 0)
- goto out;
-
- len += ret;
+ if (copy_to_user((void __user *)info.sync_fence_info, fence_info,
+ size)) {
+ ret = -EFAULT;
+ goto out;
}
- info->len = len;
+no_fences:
+ strlcpy(info.name, sync_file->name, sizeof(info.name));
+ info.status = atomic_read(&sync_file->status);
+ if (info.status >= 0)
+ info.status = !info.status;
+
+ info.num_fences = sync_file->num_fences;
- if (copy_to_user((void __user *)arg, info, len))
+ if (copy_to_user((void __user *)arg, &info, sizeof(info)))
ret = -EFAULT;
else
ret = 0;
out:
- kfree(info);
+ kfree(fence_info);
return ret;
}
diff --git a/drivers/staging/android/uapi/sync.h b/drivers/staging/android/uapi/sync.h
index f0b41ce..a122bb5 100644
--- a/drivers/staging/android/uapi/sync.h
+++ b/drivers/staging/android/uapi/sync.h
@@ -42,21 +42,18 @@ struct sync_fence_info {
/**
* struct sync_file_info - data returned from fence info ioctl
- * @len: ioctl caller writes the size of the buffer its passing in.
- * ioctl returns length of sync_file_info returned to
- * userspace including pt_info.
* @name: name of fence
* @status: status of fence. 1: signaled 0:active <0:error
* @num_fences number of fences in the sync_file
- * @sync_fence_info: array of sync_fence_info for every fence in the sync_file
+ * @sync_fence_info: pointer to array of structs sync_fence_info with all
+ * fences in the sync_file
*/
struct sync_file_info {
- __u32 len;
char name[32];
__s32 status;
__u32 num_fences;
- __u8 sync_fence_info[0];
+ __u64 sync_fence_info;
};
#define SYNC_IOC_MAGIC '>'
--
2.5.0
From: Gustavo Padovan <[email protected]>
struct sync_merge_data already have documentation on top of the
struct definition. No need to duplicate it.
Signed-off-by: Gustavo Padovan <[email protected]>
Reviewed-by: Maarten Lankhorst <[email protected]>
---
drivers/staging/android/uapi/sync.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/staging/android/uapi/sync.h b/drivers/staging/android/uapi/sync.h
index dd0dd84..f0b41ce 100644
--- a/drivers/staging/android/uapi/sync.h
+++ b/drivers/staging/android/uapi/sync.h
@@ -21,9 +21,9 @@
* @fence: returns the fd of the new fence to userspace
*/
struct sync_merge_data {
- __s32 fd2; /* fd of second fence */
- char name[32]; /* name of new fence */
- __s32 fence; /* fd on newly created fence */
+ __s32 fd2;
+ char name[32];
+ __s32 fence;
};
/**
--
2.5.0
From: Gustavo Padovan <[email protected]>
Play safe and add flags member to all structs. So we don't need to
break API or create new IOCTL in the future if new features that requires
flags arises.
Signed-off-by: Gustavo Padovan <[email protected]>
Reviewed-by: Maarten Lankhorst <[email protected]>
---
v2: check if flags are valid (zero, in this case)
v3: return -EINVAL if flags are not zero'ed
v4: add padding for 64-bit alignment
v5: rebase to use only stacked sync_file_info
---
drivers/staging/android/sync.c | 8 ++++++++
drivers/staging/android/uapi/sync.h | 14 ++++++++++++--
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c
index 48ee175..ae81c95 100644
--- a/drivers/staging/android/sync.c
+++ b/drivers/staging/android/sync.c
@@ -445,6 +445,11 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
goto err_put_fd;
}
+ if (data.flags || data.pad) {
+ err = -EINVAL;
+ goto err_put_fd;
+ }
+
fence2 = sync_file_fdget(data.fd2);
if (!fence2) {
err = -ENOENT;
@@ -504,6 +509,9 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
return -EFAULT;
+ if (info.flags || info.pad)
+ return -EINVAL;
+
/*
* Passing num_fences = 0 means that userspace doesn't want to
* retrieve any sync_fence_info. If num_fences = 0 we skip filling
diff --git a/drivers/staging/android/uapi/sync.h b/drivers/staging/android/uapi/sync.h
index a122bb5..859977c 100644
--- a/drivers/staging/android/uapi/sync.h
+++ b/drivers/staging/android/uapi/sync.h
@@ -16,14 +16,18 @@
/**
* struct sync_merge_data - data passed to merge ioctl
- * @fd2: file descriptor of second fence
* @name: name of new fence
+ * @fd2: file descriptor of second fence
* @fence: returns the fd of the new fence to userspace
+ * @flags: merge_data flags
+ * @pad: padding for 64-bit alignment, should always be zero
*/
struct sync_merge_data {
- __s32 fd2;
char name[32];
+ __s32 fd2;
__s32 fence;
+ __u32 flags;
+ __u32 pad;
};
/**
@@ -31,12 +35,14 @@ struct sync_merge_data {
* @obj_name: name of parent sync_timeline
* @driver_name: name of driver implementing the parent
* @status: status of the fence 0:active 1:signaled <0:error
+ * @flags: fence_info flags
* @timestamp_ns: timestamp of status change in nanoseconds
*/
struct sync_fence_info {
char obj_name[32];
char driver_name[32];
__s32 status;
+ __u32 flags;
__u64 timestamp_ns;
};
@@ -44,14 +50,18 @@ struct sync_fence_info {
* struct sync_file_info - data returned from fence info ioctl
* @name: name of fence
* @status: status of fence. 1: signaled 0:active <0:error
+ * @flags: sync_file_info flags
* @num_fences number of fences in the sync_file
+ * @pad: padding for 64-bit alignment, should always be zero
* @sync_fence_info: pointer to array of structs sync_fence_info with all
* fences in the sync_file
*/
struct sync_file_info {
char name[32];
__s32 status;
+ __u32 flags;
__u32 num_fences;
+ __u32 pad;
__u64 sync_fence_info;
};
--
2.5.0
From: Gustavo Padovan <[email protected]>
Burn the old opcode to avoid any potential old userspace running the old
API to get weird errors. Changing the opcodes will make them fail right
away.
This is just a precaution, there no upstream users of these interfaces
yet and the only user is Android, but we don't expect anyone trying to
run android userspace and all it dependencies on top of upstream kernels.
Moreover Android should be converted to use upstream sync_files.
Suggested-by: Rob Clark <[email protected]>
Signed-off-by: Gustavo Padovan <[email protected]>
---
drivers/staging/android/uapi/sync.h | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/staging/android/uapi/sync.h b/drivers/staging/android/uapi/sync.h
index 859977c..fbadb8a 100644
--- a/drivers/staging/android/uapi/sync.h
+++ b/drivers/staging/android/uapi/sync.h
@@ -69,13 +69,20 @@ struct sync_file_info {
#define SYNC_IOC_MAGIC '>'
/**
+ * Opcodes 0, 1 and 2 were burned during a API change to avoid users of the
+ * old API to get weird errors when trying to handling sync_files. The API
+ * change happened during the de-stage of the Sync Framework when there was
+ * no upstream users available.
+ */
+
+/**
* DOC: SYNC_IOC_MERGE - merge two fences
*
* Takes a struct sync_merge_data. Creates a new fence containing copies of
* the sync_pts in both the calling fd and sync_merge_data.fd2. Returns the
* new fence's fd in sync_merge_data.fence
*/
-#define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 1, struct sync_merge_data)
+#define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
/**
* DOC: SYNC_IOC_FENCE_INFO - get detailed information on a fence
@@ -88,6 +95,6 @@ struct sync_file_info {
* pt_info is a buffer containing sync_pt_infos for every sync_pt in the fence.
* To iterate over the sync_pt_infos, use the sync_pt_info.len field.
*/
-#define SYNC_IOC_FILE_INFO _IOWR(SYNC_IOC_MAGIC, 2, struct sync_file_info)
+#define SYNC_IOC_FILE_INFO _IOWR(SYNC_IOC_MAGIC, 4, struct sync_file_info)
#endif /* _UAPI_LINUX_SYNC_H */
--
2.5.0
On 03/03/2016 11:40 AM, Gustavo Padovan wrote:
> From: Gustavo Padovan <[email protected]>
>
> Inform userspace how many fences are in the sync_fence_info field.
>
> Signed-off-by: Gustavo Padovan <[email protected]>
> Reviewed-by: Maarten Lankhorst <[email protected]>
> ---
> drivers/staging/android/sync.c | 2 ++
> drivers/staging/android/uapi/sync.h | 2 ++
> 2 files changed, 4 insertions(+)
>
> diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c
> index 3a8f210..31aa462 100644
> --- a/drivers/staging/android/sync.c
> +++ b/drivers/staging/android/sync.c
> @@ -525,6 +525,8 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
> if (info->status >= 0)
> info->status = !info->status;
>
> + info->num_fences = sync_file->num_fences;
> +
> len = sizeof(struct sync_file_info);
>
> for (i = 0; i < sync_file->num_fences; ++i) {
> diff --git a/drivers/staging/android/uapi/sync.h b/drivers/staging/android/uapi/sync.h
> index a0cf357..4ffb7cc 100644
> --- a/drivers/staging/android/uapi/sync.h
> +++ b/drivers/staging/android/uapi/sync.h
> @@ -47,12 +47,14 @@ struct sync_fence_info {
> * userspace including pt_info.
> * @name: name of fence
> * @status: status of fence. 1: signaled 0:active <0:error
> + * @num_fences number of fences in the sync_file
> * @sync_fence_info: array of sync_fence_info for every fence in the sync_file
> */
> struct sync_file_info {
> __u32 len;
> char name[32];
> __s32 status;
> + __u32 num_fences;
>
> __u8 sync_fence_info[0];
> };
>
Greg, this is ultimately your call. But to echo what I said before, I'm
OK with the ABI break here if that's what's needed to get sync moved out
of staging.
Pragmatically speaking, this won't break ordinary Android apps, because
they don't get direct access to sync fence fds to begin with. Some
privileged system processes do, but they go through libsync, which can
be changed to deal with the ABI differences.
Anyway, for the whole series:
Acked-by: Greg Hackmann <[email protected]>
On 03/03/2016 02:42 PM, Gustavo Padovan wrote:
> From: Gustavo Padovan <[email protected]>
>
> Burn the old opcode to avoid any potential old userspace running the old
> API to get weird errors. Changing the opcodes will make them fail right
> away.
>
> This is just a precaution, there no upstream users of these interfaces
> yet and the only user is Android, but we don't expect anyone trying to
> run android userspace and all it dependencies on top of upstream kernels.
>
> Moreover Android should be converted to use upstream sync_files.
>
> Suggested-by: Rob Clark <[email protected]>
> Signed-off-by: Gustavo Padovan <[email protected]>
Acked-by: Greg Hackmann <[email protected]>
On Thu, Mar 03, 2016 at 07:42:43PM -0300, Gustavo Padovan wrote:
> From: Gustavo Padovan <[email protected]>
>
> Burn the old opcode to avoid any potential old userspace running the old
> API to get weird errors. Changing the opcodes will make them fail right
> away.
>
> This is just a precaution, there no upstream users of these interfaces
> yet and the only user is Android, but we don't expect anyone trying to
> run android userspace and all it dependencies on top of upstream kernels.
>
> Moreover Android should be converted to use upstream sync_files.
>
> Suggested-by: Rob Clark <[email protected]>
> Signed-off-by: Gustavo Padovan <[email protected]>
> ---
> drivers/staging/android/uapi/sync.h | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
Where does this belong in this patch series? Before it? After it? In
the middle?
Please resend the whole series, gather up all of the reviewed and
signed-off-by markings from everyone involved, and I'll be glad to apply
them.
thanks,
greg k-h
On Fri, Mar 11, 2016 at 5:00 PM, Greg Kroah-Hartman
<[email protected]> wrote:
> On Thu, Mar 03, 2016 at 07:42:43PM -0300, Gustavo Padovan wrote:
>> From: Gustavo Padovan <[email protected]>
>>
>> Burn the old opcode to avoid any potential old userspace running the old
>> API to get weird errors. Changing the opcodes will make them fail right
>> away.
>>
>> This is just a precaution, there no upstream users of these interfaces
>> yet and the only user is Android, but we don't expect anyone trying to
>> run android userspace and all it dependencies on top of upstream kernels.
>>
>> Moreover Android should be converted to use upstream sync_files.
>>
>> Suggested-by: Rob Clark <[email protected]>
>> Signed-off-by: Gustavo Padovan <[email protected]>
>> ---
>> drivers/staging/android/uapi/sync.h | 11 +++++++++--
>> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> Where does this belong in this patch series? Before it? After it? In
> the middle?
Not sure if overkill, but if we wanted to be pedantic about
bisectability put all the uabi struct changes plus ioctl # changes
into a single patch (with the following patches starting to use the
new fields)?
Either way, I think the only two people in the world effected by this
(ie. who are playing with AOSP on an upstream (plus a few patches)
kernel) are Rob Herring and John Stultz. (Adding them to CC so they
are aware).
BR,
-R
> Please resend the whole series, gather up all of the reviewed and
> signed-off-by markings from everyone involved, and I'll be glad to apply
> them.
>
> thanks,
>
> greg k-h
On Thu, Mar 03, 2016 at 04:40:42PM -0300, Gustavo Padovan wrote:
> From: Gustavo Padovan <[email protected]>
<snip>
Gustavo, can you resend both series of your android patches so I know I
have the latest ones to work with? Please also collect the acks that
people have provided so far.
thanks,
greg k-h
Hi Greg,
2016-03-30 Greg Kroah-Hartman <[email protected]>:
> On Thu, Mar 03, 2016 at 04:40:42PM -0300, Gustavo Padovan wrote:
> > From: Gustavo Padovan <[email protected]>
>
> <snip>
>
> Gustavo, can you resend both series of your android patches so I know I
> have the latest ones to work with? Please also collect the acks that
> people have provided so far.
I have resent it already. The lastest patches on this series is v10,
it contain the acks
https://lkml.org/lkml/2016/3/18/298
Gustavo
On Wed, Mar 30, 2016 at 11:53:38PM -0300, Gustavo Padovan wrote:
> Hi Greg,
>
> 2016-03-30 Greg Kroah-Hartman <[email protected]>:
>
> > On Thu, Mar 03, 2016 at 04:40:42PM -0300, Gustavo Padovan wrote:
> > > From: Gustavo Padovan <[email protected]>
> >
> > <snip>
> >
> > Gustavo, can you resend both series of your android patches so I know I
> > have the latest ones to work with? Please also collect the acks that
> > people have provided so far.
>
> I have resent it already. The lastest patches on this series is v10,
> it contain the acks
>
> https://lkml.org/lkml/2016/3/18/298
Ok, I'll review those, thanks.
greg k-h