Skip to contentSkip to conversation

staging/android: add num_fences field to struct sync_file_info

PATCH Superseded Gustavo Padovan 2 participants 7 messages Mar 1, 2016 – Mar 2, 2016 Staging

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/5 staging/android: add num_fences field to struct sync_file_info

From: Gustavo Padovan <[email protected]>

Inform userspace how many fences are in the sync_fence_info field.

---
 drivers/staging/android/sync.c      | 2 ++
 drivers/staging/android/uapi/sync.h | 2 ++
 2 files changed, 4 insertions(+)
drivers/staging/android/sync.c +2 -0
index 3a8f210..31aa462 100644
@@ -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) {
drivers/staging/android/uapi/sync.h +2 -0
index a0cf357..4ffb7cc 100644
@@ -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];
 };
Signature · 6 lines
2.5.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH v5 2/5 staging/android: rename SYNC_IOC_FENCE_INFO

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.

---
 drivers/staging/android/sync.c      | 2 +-
 drivers/staging/android/uapi/sync.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/staging/android/sync.c +1 -1
index 31aa462..dc5f382 100644
@@ -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:
drivers/staging/android/uapi/sync.h +1 -1
index 4ffb7cc..dd0dd84 100644
@@ -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 */
Signature · 6 lines
2.5.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

PATCH v5 3/5 staging/android: remove redundant comments on sync_merge_data

In reply to Gustavo Padovan

From: Gustavo Padovan <[email protected]>

struct sync_merge_data already have documentation on top of the struct definition. No need to duplicate it.

---
 drivers/staging/android/uapi/sync.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/android/uapi/sync.h +3 -3
index dd0dd84..f0b41ce 100644
@@ -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;
 };
 
 /**
Signature · 6 lines
2.5.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

PATCH v5 4/5 staging/android: refactor SYNC_IOC_FILE_INFO

In reply to Gustavo Padovan

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);
	}

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

---
 drivers/staging/android/sync.c      | 64 ++++++++++++++++++++-----------------
 drivers/staging/android/uapi/sync.h |  9 ++----
 2 files changed, 38 insertions(+), 35 deletions(-)
drivers/staging/android/sync.c +35 -29
index dc5f382..3604e453 100644
@@ -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;
+	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(&in, (void __user *)arg, sizeof(in)))
 		return -EFAULT;
 
-	if (size < sizeof(struct sync_file_info))
-		return -EINVAL;
-
-	if (size > 4096)
-		size = 4096;
-
-	info = kzalloc(size, GFP_KERNEL);
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
 	if (!info)
 		return -ENOMEM;
 
@@ -525,29 +513,47 @@ 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);
+	/*
+	 * 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 (!in.num_fences)
+		goto no_fences;
 
-	for (i = 0; i < sync_file->num_fences; ++i) {
-		struct fence *fence = sync_file->cbs[i].fence;
+	if (in.num_fences < sync_file->num_fences) {
+		ret = -EINVAL;
+		goto out;
+	}
 
-		ret = sync_fill_fence_info(fence, (u8 *)info + len, size - len);
+	size = sync_file->num_fences * sizeof(*fence_info);
+	fence_info = kzalloc(size, GFP_KERNEL);
+	if (!fence_info) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
-		if (ret < 0)
-			goto out;
+	for (i = 0; i < sync_file->num_fences; ++i)
+		sync_fill_fence_info(sync_file->cbs[i].fence, &fence_info[i]);
 
-		len += ret;
+	if (copy_to_user((void __user *)in.sync_fence_info, fence_info, size)) {
+		ret = -EFAULT;
+		goto out;
 	}
 
-	info->len = len;
+	info->sync_fence_info = in.sync_fence_info;
+
+no_fences:
+	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(fence_info);
 	kfree(info);
 
 	return ret;
drivers/staging/android/uapi/sync.h +3 -6
index f0b41ce..a122bb5 100644
@@ -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		'>'
Signature · 6 lines
2.5.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw

PATCH v5 5/5 staging/android: add flags member to sync ioctl structs

In reply to Gustavo Padovan

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.

v2: check if flags are valid (zero, in this case)

v3: return -EINVAL if flags are not zero'ed

---
 drivers/staging/android/sync.c      | 8 ++++++++
 drivers/staging/android/uapi/sync.h | 6 ++++++
 2 files changed, 14 insertions(+)
drivers/staging/android/sync.c +8 -0
index 3604e453..3c265ed 100644
@@ -445,6 +445,11 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
 		goto err_put_fd;
 	}
 
+	if (data.flags) {
+		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(&in, (void __user *)arg, sizeof(in)))
 		return -EFAULT;
 
+	if (in.flags)
+		return -EINVAL;
+
 	info = kzalloc(sizeof(*info), GFP_KERNEL);
 	if (!info)
 		return -ENOMEM;
drivers/staging/android/uapi/sync.h +6 -0
index a122bb5..11e2d28 100644
@@ -19,11 +19,13 @@
  * @fd2:	file descriptor of second fence
  * @name:	name of new fence
  * @fence:	returns the fd of the new fence to userspace
+ * @flags:	merge_data flags
  */
 struct sync_merge_data {
 	__s32	fd2;
 	char	name[32];
 	__s32	fence;
+	__u32	flags;
 };
 
 /**
@@ -31,12 +33,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,6 +48,7 @@ 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
  * @sync_fence_info: pointer to array of structs sync_fence_info with all
  *		 fences in the sync_file
@@ -51,6 +56,7 @@ struct sync_fence_info {
 struct sync_file_info {
 	char	name[32];
 	__s32	status;
+	__u32	flags;
 	__u32	num_fences;
 
 	__u64	sync_fence_info;
Signature · 6 lines
2.5.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

Re: [PATCH v5 5/5] staging/android: add flags member to sync ioctl structs

Quoted 47 lines · Gustavo Padovan

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.

v2: check if flags are valid (zero, in this case)

v3: return -EINVAL if flags are not zero'ed

---
 drivers/staging/android/sync.c      | 8 ++++++++
 drivers/staging/android/uapi/sync.h | 6 ++++++
 2 files changed, 14 insertions(+)
drivers/staging/android/sync.c +8 -0
index 3604e453..3c265ed 100644
@@ -445,6 +445,11 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
                goto err_put_fd;
        }
 
+       if (data.flags) {
+               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(&in, (void __user *)arg, sizeof(in)))
                return -EFAULT;
 
+       if (in.flags)
+               return -EINVAL;
+
        info = kzalloc(sizeof(*info), GFP_KERNEL);
        if (!info)
                return -ENOMEM;
drivers/staging/android/uapi/sync.h +0 -0
index a122bb5..11e2d28 100644

drivers/staging/android/uapi/sync.h line 28

Quoted 10 lines
drivers/staging/android/uapi/sync.h +2 -0
@@ -19,11 +19,13 @@
  * @fd2:       file descriptor of second fence
  * @name:      name of new fence
  * @fence:     returns the fd of the new fence to userspace
+ * @flags:     merge_data flags
  */
 struct sync_merge_data {
        __s32   fd2;
        char    name[32];
        __s32   fence;
+       __u32   flags;

The comment from last round still stands, struct size must be multiple of 64bits. As is the struct will be broken whenever/if we decide to extend it. See [1] for an alternative wording.

Quoted 27 lines
drivers/staging/android/uapi/sync.h +3 -0
 };
 
 /**
@@ -31,12 +33,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,6 +48,7 @@ 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
  * @sync_fence_info: pointer to array of structs sync_fence_info with all
  *              fences in the sync_file

drivers/staging/android/uapi/sync.h struct sync_fence_info { · line 62

Quoted 7 lines
drivers/staging/android/uapi/sync.h +1 -0
@@ -51,6 +56,7 @@ struct sync_fence_info {
 struct sync_file_info {
        char    name[32];
        __s32   status;
+       __u32   flags;
        __u32   num_fences;
 
        __u64   sync_fence_info;

Thanks for taking my suggestion and dropping len. Although I fear that we introduced a hole which we should be explicitly padded [2].

In both cases the pad should be checked for 0 and -EINVAL should be returned if that's not the case. This will allow us to potentially reuse in the future.

Other than that I believe the series looks pretty much spot on.

Cheers,
Emil

[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/ioctl/botching-up-ioctls.txt#n37
[2] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/ioctl/botching-up-ioctls.txt#n32
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw

Re: [PATCH v5 5/5] staging/android: add flags member to sync ioctl structs

Hi Emil,

2016-03-02 Emil Velikov <[email protected]>:

Quoted 99 lines · Gustavo Padovan
Quoted 47 lines

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.

v2: check if flags are valid (zero, in this case)

v3: return -EINVAL if flags are not zero'ed

---
 drivers/staging/android/sync.c      | 8 ++++++++
 drivers/staging/android/uapi/sync.h | 6 ++++++
 2 files changed, 14 insertions(+)
drivers/staging/android/sync.c +8 -0
index 3604e453..3c265ed 100644
@@ -445,6 +445,11 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
                goto err_put_fd;
        }
 
+       if (data.flags) {
+               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(&in, (void __user *)arg, sizeof(in)))
                return -EFAULT;
 
+       if (in.flags)
+               return -EINVAL;
+
        info = kzalloc(sizeof(*info), GFP_KERNEL);
        if (!info)
                return -ENOMEM;
drivers/staging/android/uapi/sync.h +0 -0
index a122bb5..11e2d28 100644

drivers/staging/android/uapi/sync.h line 28

Quoted 10 lines
drivers/staging/android/uapi/sync.h +2 -0
@@ -19,11 +19,13 @@
  * @fd2:       file descriptor of second fence
  * @name:      name of new fence
  * @fence:     returns the fd of the new fence to userspace
+ * @flags:     merge_data flags
  */
 struct sync_merge_data {
        __s32   fd2;
        char    name[32];
        __s32   fence;
+       __u32   flags;

The comment from last round still stands, struct size must be multiple of 64bits. As is the struct will be broken whenever/if we decide to extend it. See [1] for an alternative wording.

Quoted 27 lines
drivers/staging/android/uapi/sync.h +3 -0
 };
 
 /**
@@ -31,12 +33,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,6 +48,7 @@ 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
  * @sync_fence_info: pointer to array of structs sync_fence_info with all
  *              fences in the sync_file

drivers/staging/android/uapi/sync.h struct sync_fence_info { · line 62

Quoted 7 lines
drivers/staging/android/uapi/sync.h +1 -0
@@ -51,6 +56,7 @@ struct sync_fence_info {
 struct sync_file_info {
        char    name[32];
        __s32   status;
+       __u32   flags;
        __u32   num_fences;
 
        __u64   sync_fence_info;

Thanks for taking my suggestion and dropping len. Although I fear that we introduced a hole which we should be explicitly padded [2].

Quoted 5 lines

In both cases the pad should be checked for 0 and -EINVAL should be returned if that's not the case. This will allow us to potentially reuse in the future.

Other than that I believe the series looks pretty much spot on.

I agree with both suggestions, a new version of the patches is on the way.

Gustavo