Skip to contentSkip to conversation

New Ion query ioctl

PATCH In review Laura Abbott 2 participants 6 messages Sep 7, 2016 – Sep 8, 2016 Staging

lore
Share & tools
lore
#0 lore raw

v3 cover New Ion query ioctl

Hi,

This is v3 of the previous series. The scope continues to shrink. The ABI ioctl was dropped after discussion about how it creates more problems than it actually solves. This is mostly a rebase to staging-next with some refactoring from not having the ABI ioctl. There was some discussion about ion_dummy cleanup but I've decided to have that be a separate patch.

Laura Abbott (2):
staging: android: ion: Pull out ion ioctls to a separate file
staging: android: ion: Add ioctl to query available heaps

 drivers/staging/android/ion/Makefile    |   3 +-
 drivers/staging/android/ion/ion-ioctl.c | 177 +++++++++++++++++++++++++
 drivers/staging/android/ion/ion.c       | 227 ++++++--------------------------
 drivers/staging/android/ion/ion_priv.h  |  94 +++++++++++++
 drivers/staging/android/uapi/ion.h      |  39 ++++++
 5 files changed, 349 insertions(+), 191 deletions(-)
 create mode 100644 drivers/staging/android/ion/ion-ioctl.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v3 1/2 staging: android: ion: Pull out ion ioctls to a separate file

The number of Ion ioctls may continue to grow along with necessary validation. Pull it out into a separate file for easier management and review.

---
v3: Rebase to staging-next

---
 drivers/staging/android/ion/Makefile    |   3 +-
 drivers/staging/android/ion/ion-ioctl.c | 144 ++++++++++++++++++++++
 drivers/staging/android/ion/ion.c       | 212 ++------------------------------
 drivers/staging/android/ion/ion_priv.h  |  91 ++++++++++++++
 4 files changed, 245 insertions(+), 205 deletions(-)
 create mode 100644 drivers/staging/android/ion/ion-ioctl.c
drivers/staging/android/ion/Makefile +2 -1
index 18cc2aa..376c2b2 100644
@@ -1,4 +1,5 @@
-obj-$(CONFIG_ION) +=	ion.o ion_heap.o ion_page_pool.o ion_system_heap.o \
+obj-$(CONFIG_ION) +=	ion.o ion-ioctl.o ion_heap.o \
+			ion_page_pool.o ion_system_heap.o \
 			ion_carveout_heap.o ion_chunk_heap.o ion_cma_heap.o
 obj-$(CONFIG_ION_TEST) += ion_test.o
 ifdef CONFIG_COMPAT
drivers/staging/android/ion/ion-ioctl.c new file +144 -0

144 lines not on this page · raw message

drivers/staging/android/ion/ion.c +8 -204

258 lines not on this page · raw message

drivers/staging/android/ion/ion_priv.h +91 -0

107 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

PATCH v3 2/2 staging: android: ion: Add ioctl to query available heaps

In reply to Laura Abbott

Ion clients currently lack a good method to determine what heaps are available and what ids they map to. This leads to tight coupling between user and kernel space and headaches. Add a query ioctl to let userspace know the availability of heaps.

---
v3: Include some refactoring that was previously part of the dropped ABI ioctl. Use u64_to_user_ptr to avoid warnings and ugly double casts. General rebase due to dropped ABI ioctl.

---
 drivers/staging/android/ion/ion-ioctl.c | 53 ++++++++++++++++++++++++++-------
 drivers/staging/android/ion/ion.c       | 43 ++++++++++++++++++++++++++
 drivers/staging/android/ion/ion_priv.h  |  3 ++
 drivers/staging/android/uapi/ion.h      | 39 ++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 10 deletions(-)
drivers/staging/android/ion/ion-ioctl.c +43 -10
index 341ba7d..7e7431d 100644
@@ -22,6 +22,31 @@
 #include "ion_priv.h"
 #include "compat_ion.h"
 
+union ion_ioctl_arg {
+	struct ion_fd_data fd;
+	struct ion_allocation_data allocation;
+	struct ion_handle_data handle;
+	struct ion_custom_data custom;
+	struct ion_heap_query query;
+};
+
+static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg)
+{
+	int ret = 0;
+
+	switch (cmd) {
+	case ION_IOC_HEAP_QUERY:
+		ret = arg->query.reserved0 != 0;
+		ret |= arg->query.reserved1 != 0;
+		ret |= arg->query.reserved2 != 0;
+		break;
+	default:
+		break;
+	}
+
+	return ret ? -EINVAL : 0;
+}
+
 /* fix up the cases where the ioctl direction bits are incorrect */
 static unsigned int ion_ioctl_dir(unsigned int cmd)
 {
@@ -42,22 +67,27 @@ long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	struct ion_handle *cleanup_handle = NULL;
 	int ret = 0;
 	unsigned int dir;
-
-	union {
-		struct ion_fd_data fd;
-		struct ion_allocation_data allocation;
-		struct ion_handle_data handle;
-		struct ion_custom_data custom;
-	} data;
+	union ion_ioctl_arg data;
 
 	dir = ion_ioctl_dir(cmd);
 
 	if (_IOC_SIZE(cmd) > sizeof(data))
 		return -EINVAL;
 
-	if (dir & _IOC_WRITE)
-		if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
-			return -EFAULT;
+	/*
+	 * The copy_from_user is unconditional here for both read and write
+	 * to do the validate. If there is no write for the ioctl, the
+	 * buffer is cleared
+	 */
+	if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
+		return -EFAULT;
+
+	ret = validate_ioctl_arg(cmd, &data);
+	if (WARN_ON_ONCE(ret))
+		return ret;
+
+	if (!(dir & _IOC_WRITE))
+		memset(&data, 0, sizeof(data));
 
 	switch (cmd) {
 	case ION_IOC_ALLOC:
@@ -129,6 +159,9 @@ long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 						data.custom.arg);
 		break;
 	}
+	case ION_IOC_HEAP_QUERY:
+		ret = ion_query_heaps(client, &data.query);
+		break;
 	default:
 		return -ENOTTY;
 	}
drivers/staging/android/ion/ion.c +43 -0

55 lines not on this page · raw message

drivers/staging/android/ion/ion_priv.h +3 -0
index 4a78fab..3c3b324 100644
@@ -102,6 +102,7 @@ struct ion_device {
 	struct dentry *debug_root;
 	struct dentry *heaps_debug_root;
 	struct dentry *clients_debug_root;
+	int heap_cnt;
 };
 
 /**
@@ -467,4 +468,6 @@ struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
 
 int ion_handle_put(struct ion_handle *handle);
 
+int ion_query_heaps(struct ion_client *client, struct ion_heap_query *query);
+
 #endif /* _ION_PRIV_H */
drivers/staging/android/uapi/ion.h +39 -0
index a9c4e8b..647f130 100644
@@ -128,6 +128,36 @@ struct ion_custom_data {
 	unsigned long arg;
 };
 
+#define MAX_HEAP_NAME			32
+
+/**
+ * struct ion_heap_data - data about a heap
+ * @name - first 32 characters of the heap name
+ * @type - heap type
+ * @heap_id - heap id for the heap
+ */
+struct ion_heap_data {
+	char name[MAX_HEAP_NAME];
+	__u32 type;
+	__u32 heap_id;
+	__u32 reserved0;
+	__u32 reserved1;
+	__u32 reserved2;
+};
+
+/**
+ * struct ion_heap_query - collection of data about all heaps
+ * @cnt - total number of heaps to be copied
+ * @heaps - buffer to copy heap data
+ */
+struct ion_heap_query {
+	__u32 cnt; /* Total number of heaps to be copied */
+	__u32 reserved0; /* align to 64bits */
+	__u64 heaps; /* buffer to be populated */
+	__u32 reserved1;
+	__u32 reserved2;
+};
+
 #define ION_IOC_MAGIC		'I'
 
 /**
@@ -194,4 +224,13 @@ struct ion_custom_data {
  */
 #define ION_IOC_CUSTOM		_IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
 
+/**
+ * DOC: ION_IOC_HEAP_QUERY - information about available heaps
+ *
+ * Takes an ion_heap_query structure and populates information about
+ * available Ion heaps.
+ */
+#define ION_IOC_HEAP_QUERY     _IOWR(ION_IOC_MAGIC, 8, \
+					struct ion_heap_query)
+
 #endif /* _UAPI_LINUX_ION_H */
Signature · 1 line
2.7.4
#4 lore raw

Re: [Linaro-mm-sig] [PATCHv3 2/2] staging: android: ion: Add ioctl to query available heaps

Quoted 4 lines · CEST Laura Abbott
drivers/staging/android/ion/ion-ioctl.c +1 -3
-	if (dir & _IOC_WRITE)
-		if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
-			return -EFAULT;
+	/*

drivers/staging/android/ion/ion-ioctl.c long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 87

Quoted 10 lines
drivers/staging/android/ion/ion-ioctl.c +10 -0
+	 * The copy_from_user is unconditional here for both read and write
+	 * to do the validate. If there is no write for the ioctl, the
+	 * buffer is cleared
+	 */
+	if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
+		return -EFAULT;
+
+	ret = validate_ioctl_arg(cmd, &data);
+	if (WARN_ON_ONCE(ret))
+		return ret;

I noticed that the WARN_ON_ONCE warns about invalid user input, but I think we tend to normally just use WARN_ON for things that go wrong inside of the kernel or in hardware.

Maybe better use printk_once() or printk_ratelimited.

Is there any noticeable overhead in always copying the structure? copy_from_user() can be a bit slow depending on debugging or security features, and it seems unnecessary if the validation is only done for one of the commands.

Otherwise the patch looks good to me.

Arnd

#5 lore raw

Re: [Linaro-mm-sig] [PATCHv3 2/2] staging: android: ion: Add ioctl to query available heaps

Quoted 21 lines · Arnd Bergmann
Quoted 4 lines · CEST Laura Abbott
drivers/staging/android/ion/ion-ioctl.c +1 -3
-	if (dir & _IOC_WRITE)
-		if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
-			return -EFAULT;
+	/*

drivers/staging/android/ion/ion-ioctl.c long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 87

Quoted 10 lines
drivers/staging/android/ion/ion-ioctl.c +10 -0
+	 * The copy_from_user is unconditional here for both read and write
+	 * to do the validate. If there is no write for the ioctl, the
+	 * buffer is cleared
+	 */
+	if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
+		return -EFAULT;
+
+	ret = validate_ioctl_arg(cmd, &data);
+	if (WARN_ON_ONCE(ret))
+		return ret;

I noticed that the WARN_ON_ONCE warns about invalid user input, but I think we tend to normally just use WARN_ON for things that go wrong inside of the kernel or in hardware.

Quoted 2 lines

Maybe better use printk_once() or printk_ratelimited.

Sure, the error code should hopefully be enough of a hint to userspace to maybe check the log.

Quoted 5 lines

Is there any noticeable overhead in always copying the structure? copy_from_user() can be a bit slow depending on debugging or security features, and it seems unnecessary if the validation is only done for one of the commands.

Good point. It made sense with some of the other ioctls (specifically the ABI) but isn't necessary now. We can evaluate later when other ioctls get added.

Quoted 4 lines

Otherwise the patch looks good to me.

Arnd

Thanks!

Laura