GNU Linux-libre 4.9.314-gnu1
[releases.git] / drivers / staging / android / ion / ion-ioctl.c
1 /*
2  *
3  * Copyright (C) 2011 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/file.h>
18 #include <linux/fs.h>
19 #include <linux/uaccess.h>
20
21 #include "ion.h"
22 #include "ion_priv.h"
23 #include "compat_ion.h"
24
25 union ion_ioctl_arg {
26         struct ion_fd_data fd;
27         struct ion_allocation_data allocation;
28         struct ion_handle_data handle;
29         struct ion_custom_data custom;
30         struct ion_heap_query query;
31 };
32
33 /* Must hold the client lock */
34 static void user_ion_handle_get(struct ion_handle *handle)
35 {
36         if (handle->user_ref_count++ == 0)
37                 kref_get(&handle->ref);
38 }
39
40 /* Must hold the client lock */
41 static struct ion_handle *user_ion_handle_get_check_overflow(
42         struct ion_handle *handle)
43 {
44         if (handle->user_ref_count + 1 == 0)
45                 return ERR_PTR(-EOVERFLOW);
46         user_ion_handle_get(handle);
47         return handle;
48 }
49
50 /* passes a kref to the user ref count.
51  * We know we're holding a kref to the object before and
52  * after this call, so no need to reverify handle.
53  */
54 static struct ion_handle *pass_to_user(struct ion_handle *handle)
55 {
56         struct ion_client *client = handle->client;
57         struct ion_handle *ret;
58
59         mutex_lock(&client->lock);
60         ret = user_ion_handle_get_check_overflow(handle);
61         ion_handle_put_nolock(handle);
62         mutex_unlock(&client->lock);
63         return ret;
64 }
65
66 /* Must hold the client lock */
67 static int user_ion_handle_put_nolock(struct ion_handle *handle)
68 {
69         int ret;
70
71         if (--handle->user_ref_count == 0)
72                 ret = ion_handle_put_nolock(handle);
73
74         return ret;
75 }
76
77 static void user_ion_free_nolock(struct ion_client *client,
78                                  struct ion_handle *handle)
79 {
80         bool valid_handle;
81
82         WARN_ON(client != handle->client);
83
84         valid_handle = ion_handle_validate(client, handle);
85         if (!valid_handle) {
86                 WARN(1, "%s: invalid handle passed to free.\n", __func__);
87                 return;
88         }
89         if (handle->user_ref_count == 0) {
90                 WARN(1, "%s: User does not have access!\n", __func__);
91                 return;
92         }
93         user_ion_handle_put_nolock(handle);
94 }
95
96 static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg)
97 {
98         int ret = 0;
99
100         switch (cmd) {
101         case ION_IOC_HEAP_QUERY:
102                 ret = arg->query.reserved0 != 0;
103                 ret |= arg->query.reserved1 != 0;
104                 ret |= arg->query.reserved2 != 0;
105                 break;
106         default:
107                 break;
108         }
109
110         return ret ? -EINVAL : 0;
111 }
112
113 /* fix up the cases where the ioctl direction bits are incorrect */
114 static unsigned int ion_ioctl_dir(unsigned int cmd)
115 {
116         switch (cmd) {
117         case ION_IOC_SYNC:
118         case ION_IOC_FREE:
119         case ION_IOC_CUSTOM:
120                 return _IOC_WRITE;
121         default:
122                 return _IOC_DIR(cmd);
123         }
124 }
125
126 long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
127 {
128         struct ion_client *client = filp->private_data;
129         struct ion_device *dev = client->dev;
130         struct ion_handle *cleanup_handle = NULL;
131         int ret = 0;
132         unsigned int dir;
133         union ion_ioctl_arg data;
134
135         dir = ion_ioctl_dir(cmd);
136
137         if (_IOC_SIZE(cmd) > sizeof(data))
138                 return -EINVAL;
139
140         /*
141          * The copy_from_user is unconditional here for both read and write
142          * to do the validate. If there is no write for the ioctl, the
143          * buffer is cleared
144          */
145         if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
146                 return -EFAULT;
147
148         ret = validate_ioctl_arg(cmd, &data);
149         if (ret) {
150                 pr_warn_once("%s: ioctl validate failed\n", __func__);
151                 return ret;
152         }
153
154         if (!(dir & _IOC_WRITE))
155                 memset(&data, 0, sizeof(data));
156
157         switch (cmd) {
158         case ION_IOC_ALLOC:
159         {
160                 struct ion_handle *handle;
161
162                 handle = __ion_alloc(client, data.allocation.len,
163                                      data.allocation.align,
164                                      data.allocation.heap_id_mask,
165                                      data.allocation.flags, true);
166                 if (IS_ERR(handle))
167                         return PTR_ERR(handle);
168                 data.allocation.handle = handle->id;
169                 cleanup_handle = handle;
170                 pass_to_user(handle);
171                 break;
172         }
173         case ION_IOC_FREE:
174         {
175                 struct ion_handle *handle;
176
177                 mutex_lock(&client->lock);
178                 handle = ion_handle_get_by_id_nolock(client, data.handle.handle);
179                 if (IS_ERR(handle)) {
180                         mutex_unlock(&client->lock);
181                         return PTR_ERR(handle);
182                 }
183                 user_ion_free_nolock(client, handle);
184                 ion_handle_put_nolock(handle);
185                 mutex_unlock(&client->lock);
186                 break;
187         }
188         case ION_IOC_SHARE:
189         case ION_IOC_MAP:
190         {
191                 struct ion_handle *handle;
192
193                 mutex_lock(&client->lock);
194                 handle = ion_handle_get_by_id_nolock(client, data.handle.handle);
195                 if (IS_ERR(handle)) {
196                         mutex_unlock(&client->lock);
197                         return PTR_ERR(handle);
198                 }
199                 data.fd.fd = ion_share_dma_buf_fd_nolock(client, handle);
200                 ion_handle_put_nolock(handle);
201                 mutex_unlock(&client->lock);
202                 if (data.fd.fd < 0)
203                         ret = data.fd.fd;
204                 break;
205         }
206         case ION_IOC_IMPORT:
207         {
208                 struct ion_handle *handle;
209
210                 handle = ion_import_dma_buf_fd(client, data.fd.fd);
211                 if (IS_ERR(handle)) {
212                         ret = PTR_ERR(handle);
213                 } else {
214                         data.handle.handle = handle->id;
215                         handle = pass_to_user(handle);
216                         if (IS_ERR(handle)) {
217                                 ret = PTR_ERR(handle);
218                                 data.handle.handle = 0;
219                         }
220                 }
221                 break;
222         }
223         case ION_IOC_SYNC:
224         {
225                 ret = ion_sync_for_device(client, data.fd.fd);
226                 break;
227         }
228         case ION_IOC_CUSTOM:
229         {
230                 if (!dev->custom_ioctl)
231                         return -ENOTTY;
232                 ret = dev->custom_ioctl(client, data.custom.cmd,
233                                                 data.custom.arg);
234                 break;
235         }
236         case ION_IOC_HEAP_QUERY:
237                 ret = ion_query_heaps(client, &data.query);
238                 break;
239         default:
240                 return -ENOTTY;
241         }
242
243         if (dir & _IOC_READ) {
244                 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) {
245                         if (cleanup_handle) {
246                                 mutex_lock(&client->lock);
247                                 user_ion_free_nolock(client, cleanup_handle);
248                                 ion_handle_put_nolock(cleanup_handle);
249                                 mutex_unlock(&client->lock);
250                         }
251                         return -EFAULT;
252                 }
253         }
254         if (cleanup_handle)
255                 ion_handle_put(cleanup_handle);
256         return ret;
257 }