2017-10-29 01:48:53 -04:00
|
|
|
diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c
|
2017-10-29 22:14:37 -04:00
|
|
|
index ee1c2f3..e99ea9a 100755
|
2017-10-29 01:48:53 -04:00
|
|
|
--- a/drivers/staging/android/ion/ion.c
|
|
|
|
+++ b/drivers/staging/android/ion/ion.c
|
2017-10-29 22:14:37 -04:00
|
|
|
@@ -116,6 +116,7 @@
|
2017-10-29 01:48:53 -04:00
|
|
|
*/
|
|
|
|
struct ion_handle {
|
|
|
|
struct kref ref;
|
|
|
|
+ unsigned int user_ref_count;
|
|
|
|
struct ion_client *client;
|
|
|
|
struct ion_buffer *buffer;
|
|
|
|
struct rb_node node;
|
2017-10-29 22:14:37 -04:00
|
|
|
@@ -429,6 +430,50 @@
|
2017-10-29 01:48:53 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
+/* Must hold the client lock */
|
|
|
|
+static void user_ion_handle_get(struct ion_handle *handle)
|
|
|
|
+{
|
2017-10-29 22:14:37 -04:00
|
|
|
+ if (handle->user_ref_count++ == 0) {
|
2017-10-29 01:48:53 -04:00
|
|
|
+ kref_get(&handle->ref);
|
2017-10-29 22:14:37 -04:00
|
|
|
+ }
|
2017-10-29 01:48:53 -04:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Must hold the client lock */
|
2017-10-29 22:14:37 -04:00
|
|
|
+static struct ion_handle* user_ion_handle_get_check_overflow(struct ion_handle *handle)
|
2017-10-29 01:48:53 -04:00
|
|
|
+{
|
|
|
|
+ if (handle->user_ref_count + 1 == 0)
|
|
|
|
+ return ERR_PTR(-EOVERFLOW);
|
|
|
|
+ user_ion_handle_get(handle);
|
|
|
|
+ return handle;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* passes a kref to the user ref count.
|
|
|
|
+ * We know we're holding a kref to the object before and
|
|
|
|
+ * after this call, so no need to reverify handle. */
|
2017-10-29 22:14:37 -04:00
|
|
|
+static struct ion_handle* pass_to_user(struct ion_handle *handle)
|
2017-10-29 01:48:53 -04:00
|
|
|
+{
|
|
|
|
+ struct ion_client *client = handle->client;
|
|
|
|
+ struct ion_handle *ret;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&client->lock);
|
|
|
|
+ ret = user_ion_handle_get_check_overflow(handle);
|
|
|
|
+ ion_handle_put_nolock(handle);
|
|
|
|
+ mutex_unlock(&client->lock);
|
|
|
|
+ return ret;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Must hold the client lock */
|
|
|
|
+static int user_ion_handle_put_nolock(struct ion_handle *handle)
|
|
|
|
+{
|
|
|
|
+ int ret;
|
|
|
|
+
|
2017-10-29 22:14:37 -04:00
|
|
|
+ if (--handle->user_ref_count == 0) {
|
2017-10-29 01:48:53 -04:00
|
|
|
+ ret = ion_handle_put_nolock(handle);
|
2017-10-29 22:14:37 -04:00
|
|
|
+ }
|
2017-10-29 01:48:53 -04:00
|
|
|
+
|
|
|
|
+ return ret;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
static struct ion_handle *ion_handle_lookup(struct ion_client *client,
|
|
|
|
struct ion_buffer *buffer)
|
|
|
|
{
|
2017-10-29 22:14:37 -04:00
|
|
|
@@ -645,6 +690,24 @@
|
2017-10-29 01:48:53 -04:00
|
|
|
ion_handle_put_nolock(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
+static void user_ion_free_nolock(struct ion_client *client, struct ion_handle *handle)
|
|
|
|
+{
|
|
|
|
+ bool valid_handle;
|
|
|
|
+
|
|
|
|
+ BUG_ON(client != handle->client);
|
|
|
|
+
|
|
|
|
+ valid_handle = ion_handle_validate(client, handle);
|
|
|
|
+ if (!valid_handle) {
|
|
|
|
+ WARN(1, "%s: invalid handle passed to free.\n", __func__);
|
|
|
|
+ return;
|
|
|
|
+ }
|
2017-10-29 22:14:37 -04:00
|
|
|
+ if (!handle->user_ref_count > 0) {
|
2017-10-29 01:48:53 -04:00
|
|
|
+ WARN(1, "%s: User does not have access!\n", __func__);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ user_ion_handle_put_nolock(handle);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
void ion_free(struct ion_client *client, struct ion_handle *handle)
|
|
|
|
{
|
|
|
|
BUG_ON(client != handle->client);
|
2017-10-29 22:14:37 -04:00
|
|
|
@@ -1439,7 +1502,7 @@
|
2017-10-29 01:48:53 -04:00
|
|
|
data.allocation.flags, true);
|
|
|
|
if (IS_ERR(handle))
|
|
|
|
return PTR_ERR(handle);
|
|
|
|
-
|
|
|
|
+ pass_to_user(handle);
|
|
|
|
data.allocation.handle = handle->id;
|
|
|
|
|
|
|
|
cleanup_handle = handle;
|
2017-10-29 22:14:37 -04:00
|
|
|
@@ -1455,7 +1518,7 @@
|
2017-10-29 01:48:53 -04:00
|
|
|
mutex_unlock(&client->lock);
|
|
|
|
return PTR_ERR(handle);
|
|
|
|
}
|
|
|
|
- ion_free_nolock(client, handle);
|
|
|
|
+ user_ion_free_nolock(client, handle);
|
|
|
|
ion_handle_put_nolock(handle);
|
|
|
|
mutex_unlock(&client->lock);
|
|
|
|
break;
|
2017-10-29 22:14:37 -04:00
|
|
|
@@ -1478,10 +1541,15 @@
|
|
|
|
{
|
2017-10-29 01:48:53 -04:00
|
|
|
struct ion_handle *handle;
|
|
|
|
handle = ion_import_dma_buf(client, data.fd.fd);
|
|
|
|
- if (IS_ERR(handle))
|
|
|
|
+ if (IS_ERR(handle)) {
|
|
|
|
ret = PTR_ERR(handle);
|
|
|
|
- else
|
|
|
|
- data.handle.handle = handle->id;
|
|
|
|
+ } else {
|
|
|
|
+ handle = pass_to_user(handle);
|
|
|
|
+ if (IS_ERR(handle))
|
|
|
|
+ ret = PTR_ERR(handle);
|
|
|
|
+ else
|
|
|
|
+ data.handle.handle = handle->id;
|
|
|
|
+ }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ION_IOC_SYNC:
|
2017-10-29 22:14:37 -04:00
|
|
|
@@ -1518,8 +1586,10 @@
|
2017-10-29 01:48:53 -04:00
|
|
|
if (dir & _IOC_READ) {
|
|
|
|
if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) {
|
|
|
|
if (cleanup_handle) {
|
|
|
|
- ion_free(client, cleanup_handle);
|
|
|
|
- ion_handle_put(cleanup_handle);
|
|
|
|
+ mutex_lock(&client->lock);
|
|
|
|
+ user_ion_free_nolock(client, cleanup_handle);
|
|
|
|
+ ion_handle_put_nolock(cleanup_handle);
|
|
|
|
+ mutex_unlock(&client->lock);
|
|
|
|
}
|
|
|
|
return -EFAULT;
|
|
|
|
}
|