[PATCH v6 5/6] staging/android: refactor SYNC_IOC_FILE_INFO

Maarten Lankhorst maarten.lankhorst at linux.intel.com
Thu Mar 3 09:29:01 UTC 2016


Op 02-03-16 om 20:52 schreef Gustavo Padovan:
> From: Gustavo Padovan <gustavo.padovan at collabora.co.uk>
>
> 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);
> 	}
>
> 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
>
> Signed-off-by: Gustavo Padovan <gustavo.padovan at collabora.co.uk>
> ---
>  drivers/staging/android/sync.c      | 64 ++++++++++++++++++++-----------------
>  drivers/staging/android/uapi/sync.h |  9 ++----
>  2 files changed, 38 insertions(+), 35 deletions(-)
>
> diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c
> index dc5f382..3604e453 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,28 +491,20 @@ 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 in, *info;
Why put one copy on the stack, and allocate a second?
With sync_file_info now being a fixed size just put 1 copy on the stack from userspace,
run some sanity checks first, put in the new values and copy it back to userspace without additional memory allocation.

The rest looks good now. :)

~Maarten


More information about the dri-devel mailing list