drm/prime: resolve race for drm_gem_prime_handle_to_fd()
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / gpu / drm / drm_prime.c
1 /*
2  * Copyright © 2012 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Dave Airlie <airlied@redhat.com>
25  *      Rob Clark <rob.clark@linaro.org>
26  *
27  */
28
29 #include <linux/export.h>
30 #include <linux/dma-buf.h>
31 #include <drm/drmP.h>
32
33 /*
34  * DMA-BUF/GEM Object references and lifetime overview:
35  *
36  * On the export the dma_buf holds a reference to the exporting GEM
37  * object. It takes this reference in handle_to_fd_ioctl, when it
38  * first calls .prime_export and stores the exporting GEM object in
39  * the dma_buf priv. This reference is released when the dma_buf
40  * object goes away in the driver .release function.
41  *
42  * On the import the importing GEM object holds a reference to the
43  * dma_buf (which in turn holds a ref to the exporting GEM object).
44  * It takes that reference in the fd_to_handle ioctl.
45  * It calls dma_buf_get, creates an attachment to it and stores the
46  * attachment in the GEM object. When this attachment is destroyed
47  * when the imported object is destroyed, we remove the attachment
48  * and drop the reference to the dma_buf.
49  *
50  * Thus the chain of references always flows in one direction
51  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
52  *
53  * Self-importing: if userspace is using PRIME as a replacement for flink
54  * then it will get a fd->handle request for a GEM object that it created.
55  * Drivers should detect this situation and return back the gem object
56  * from the dma-buf private.  Prime will do this automatically for drivers that
57  * use the drm_gem_prime_{import,export} helpers.
58  */
59
60 struct drm_prime_member {
61         struct list_head entry;
62         struct dma_buf *dma_buf;
63         uint32_t handle;
64 };
65
66 bool drm_prime_check_dmabuf_valid(struct dma_buf *dmabuf)
67 {
68         struct file *file = dmabuf->file;
69
70         /*
71          * There could be a race when drm_gem_prime_handle_to_fd() is requested
72          * during __fput() is executed with same dmabuf.
73          * Let's suppose that there are process A and B. A creates a gem obj
74          * and exports it to dmabuf, B opens gem name which comes from A.
75          * And A is closing dmabuf and B tries to import dmabuf at that time.
76          * In this case, the dmabuf is in obj->export_dma_buf cache, because
77          * gem obj is still valid, and B could get dmabuf from this cache,
78          * but this dmabuf is invalid already, so B has to get a new dmabuf
79          * directly.
80          */
81         if (!atomic_long_read(&file->f_count))
82                 return false;
83
84         return true;
85 }
86 EXPORT_SYMBOL(drm_prime_check_dmabuf_valid);
87
88 static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
89                 enum dma_data_direction dir)
90 {
91         struct drm_gem_object *obj = attach->dmabuf->priv;
92         struct sg_table *sgt;
93
94         mutex_lock(&obj->dev->struct_mutex);
95
96         sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
97
98         if (!IS_ERR_OR_NULL(sgt))
99                 dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
100
101         mutex_unlock(&obj->dev->struct_mutex);
102         return sgt;
103 }
104
105 static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
106                 struct sg_table *sgt, enum dma_data_direction dir)
107 {
108         dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
109         sg_free_table(sgt);
110         kfree(sgt);
111 }
112
113 static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
114 {
115         struct drm_gem_object *obj = dma_buf->priv;
116
117         if (obj->export_dma_buf == dma_buf) {
118                 /* drop the reference on the export fd holds */
119                 obj->export_dma_buf = NULL;
120                 drm_gem_object_unreference_unlocked(obj);
121         }
122 }
123
124 static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
125 {
126         struct drm_gem_object *obj = dma_buf->priv;
127         struct drm_device *dev = obj->dev;
128
129         return dev->driver->gem_prime_vmap(obj);
130 }
131
132 static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
133 {
134         struct drm_gem_object *obj = dma_buf->priv;
135         struct drm_device *dev = obj->dev;
136
137         dev->driver->gem_prime_vunmap(obj, vaddr);
138 }
139
140 static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
141                 unsigned long page_num)
142 {
143         return NULL;
144 }
145
146 static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
147                 unsigned long page_num, void *addr)
148 {
149
150 }
151 static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
152                 unsigned long page_num)
153 {
154         return NULL;
155 }
156
157 static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
158                 unsigned long page_num, void *addr)
159 {
160
161 }
162
163 static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
164                 struct vm_area_struct *vma)
165 {
166         return -EINVAL;
167 }
168
169 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
170         .map_dma_buf = drm_gem_map_dma_buf,
171         .unmap_dma_buf = drm_gem_unmap_dma_buf,
172         .release = drm_gem_dmabuf_release,
173         .kmap = drm_gem_dmabuf_kmap,
174         .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
175         .kunmap = drm_gem_dmabuf_kunmap,
176         .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
177         .mmap = drm_gem_dmabuf_mmap,
178         .vmap = drm_gem_dmabuf_vmap,
179         .vunmap = drm_gem_dmabuf_vunmap,
180 };
181
182 /**
183  * DOC: PRIME Helpers
184  *
185  * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
186  * simpler APIs by using the helper functions @drm_gem_prime_export and
187  * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
188  * five lower-level driver callbacks:
189  *
190  * Export callbacks:
191  *
192  *  - @gem_prime_pin (optional): prepare a GEM object for exporting
193  *
194  *  - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
195  *
196  *  - @gem_prime_vmap: vmap a buffer exported by your driver
197  *
198  *  - @gem_prime_vunmap: vunmap a buffer exported by your driver
199  *
200  * Import callback:
201  *
202  *  - @gem_prime_import_sg_table (import): produce a GEM object from another
203  *    driver's scatter/gather table
204  */
205
206 struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
207                                      struct drm_gem_object *obj, int flags)
208 {
209         if (dev->driver->gem_prime_pin) {
210                 int ret = dev->driver->gem_prime_pin(obj);
211                 if (ret)
212                         return ERR_PTR(ret);
213         }
214         return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
215 }
216 EXPORT_SYMBOL(drm_gem_prime_export);
217
218 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
219                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
220                 int *prime_fd)
221 {
222         struct drm_gem_object *obj;
223         void *buf;
224         int ret = 0;
225         struct dma_buf *dmabuf;
226
227         obj = drm_gem_object_lookup(dev, file_priv, handle);
228         if (!obj)
229                 return -ENOENT;
230
231         mutex_lock(&file_priv->prime.lock);
232         /* re-export the original imported object */
233         if (obj->import_attach) {
234                 dmabuf = obj->import_attach->dmabuf;
235                 goto out_have_obj;
236         }
237
238         if (obj->export_dma_buf) {
239                 dmabuf = obj->export_dma_buf;
240                 goto out_have_obj;
241         }
242
243         buf = dev->driver->gem_prime_export(dev, obj, flags);
244         if (IS_ERR(buf)) {
245                 /* normally the created dma-buf takes ownership of the ref,
246                  * but if that fails then drop the ref
247                  */
248                 ret = PTR_ERR(buf);
249                 goto out;
250         }
251         obj->export_dma_buf = buf;
252
253         /* if we've exported this buffer the cheat and add it to the import list
254          * so we get the correct handle back
255          */
256         ret = drm_prime_add_buf_handle(&file_priv->prime,
257                                        obj->export_dma_buf, handle);
258         if (ret)
259                 goto out;
260
261         *prime_fd = dma_buf_fd(buf, flags);
262         mutex_unlock(&file_priv->prime.lock);
263         return 0;
264
265 out_have_obj:
266         get_dma_buf(dmabuf);
267         *prime_fd = dma_buf_fd(dmabuf, flags);
268 out:
269         drm_gem_object_unreference_unlocked(obj);
270         mutex_unlock(&file_priv->prime.lock);
271         return ret;
272 }
273 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
274
275 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
276                                             struct dma_buf *dma_buf)
277 {
278         struct dma_buf_attachment *attach;
279         struct sg_table *sgt;
280         struct drm_gem_object *obj;
281         int ret;
282
283         if (!dev->driver->gem_prime_import_sg_table)
284                 return ERR_PTR(-EINVAL);
285
286         if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
287                 obj = dma_buf->priv;
288                 if (obj->dev == dev) {
289                         /*
290                          * Importing dmabuf exported from out own gem increases
291                          * refcount on gem itself instead of f_count of dmabuf.
292                          */
293                         drm_gem_object_reference(obj);
294                         return obj;
295                 }
296         }
297
298         attach = dma_buf_attach(dma_buf, dev->dev);
299         if (IS_ERR(attach))
300                 return ERR_PTR(PTR_ERR(attach));
301
302         get_dma_buf(dma_buf);
303
304         sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
305         if (IS_ERR_OR_NULL(sgt)) {
306                 ret = PTR_ERR(sgt);
307                 goto fail_detach;
308         }
309
310         obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
311         if (IS_ERR(obj)) {
312                 ret = PTR_ERR(obj);
313                 goto fail_unmap;
314         }
315
316         obj->import_attach = attach;
317
318         return obj;
319
320 fail_unmap:
321         dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
322 fail_detach:
323         dma_buf_detach(dma_buf, attach);
324         dma_buf_put(dma_buf);
325
326         return ERR_PTR(ret);
327 }
328 EXPORT_SYMBOL(drm_gem_prime_import);
329
330 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
331                 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
332 {
333         struct dma_buf *dma_buf;
334         struct drm_gem_object *obj;
335         int ret;
336
337         dma_buf = dma_buf_get(prime_fd);
338         if (IS_ERR(dma_buf))
339                 return PTR_ERR(dma_buf);
340
341         mutex_lock(&file_priv->prime.lock);
342
343         ret = drm_prime_lookup_buf_handle(&file_priv->prime,
344                         dma_buf, handle);
345         if (!ret) {
346                 ret = 0;
347                 goto out_put;
348         }
349
350         /* never seen this one, need to import */
351         obj = dev->driver->gem_prime_import(dev, dma_buf);
352         if (IS_ERR(obj)) {
353                 ret = PTR_ERR(obj);
354                 goto out_put;
355         }
356
357         ret = drm_gem_handle_create(file_priv, obj, handle);
358         drm_gem_object_unreference_unlocked(obj);
359         if (ret)
360                 goto out_put;
361
362         ret = drm_prime_add_buf_handle(&file_priv->prime,
363                         dma_buf, *handle);
364         if (ret)
365                 goto fail;
366
367         mutex_unlock(&file_priv->prime.lock);
368
369         dma_buf_put(dma_buf);
370
371         return 0;
372
373 fail:
374         /* hmm, if driver attached, we are relying on the free-object path
375          * to detach.. which seems ok..
376          */
377         drm_gem_object_handle_unreference_unlocked(obj);
378 out_put:
379         dma_buf_put(dma_buf);
380         mutex_unlock(&file_priv->prime.lock);
381         return ret;
382 }
383 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
384
385 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
386                                  struct drm_file *file_priv)
387 {
388         struct drm_prime_handle *args = data;
389         uint32_t flags;
390
391         if (!drm_core_check_feature(dev, DRIVER_PRIME))
392                 return -EINVAL;
393
394         if (!dev->driver->prime_handle_to_fd)
395                 return -ENOSYS;
396
397         /* check flags are valid */
398         if (args->flags & ~DRM_CLOEXEC)
399                 return -EINVAL;
400
401         /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
402         flags = args->flags & DRM_CLOEXEC;
403
404         return dev->driver->prime_handle_to_fd(dev, file_priv,
405                         args->handle, flags, &args->fd);
406 }
407
408 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
409                                  struct drm_file *file_priv)
410 {
411         struct drm_prime_handle *args = data;
412
413         if (!drm_core_check_feature(dev, DRIVER_PRIME))
414                 return -EINVAL;
415
416         if (!dev->driver->prime_fd_to_handle)
417                 return -ENOSYS;
418
419         return dev->driver->prime_fd_to_handle(dev, file_priv,
420                         args->fd, &args->handle);
421 }
422
423 /*
424  * drm_prime_pages_to_sg
425  *
426  * this helper creates an sg table object from a set of pages
427  * the driver is responsible for mapping the pages into the
428  * importers address space
429  */
430 struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
431 {
432         struct sg_table *sg = NULL;
433         int ret;
434
435         sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
436         if (!sg)
437                 goto out;
438
439         ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
440                                 nr_pages << PAGE_SHIFT, GFP_KERNEL);
441         if (ret)
442                 goto out;
443
444         return sg;
445 out:
446         kfree(sg);
447         return NULL;
448 }
449 EXPORT_SYMBOL(drm_prime_pages_to_sg);
450
451 /* export an sg table into an array of pages and addresses
452    this is currently required by the TTM driver in order to do correct fault
453    handling */
454 int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
455                                      dma_addr_t *addrs, int max_pages)
456 {
457         unsigned count;
458         struct scatterlist *sg;
459         struct page *page;
460         u32 len, offset;
461         int pg_index;
462         dma_addr_t addr;
463
464         pg_index = 0;
465         for_each_sg(sgt->sgl, sg, sgt->nents, count) {
466                 len = sg->length;
467                 offset = sg->offset;
468                 page = sg_page(sg);
469                 addr = sg_dma_address(sg);
470
471                 while (len > 0) {
472                         if (WARN_ON(pg_index >= max_pages))
473                                 return -1;
474                         pages[pg_index] = page;
475                         if (addrs)
476                                 addrs[pg_index] = addr;
477
478                         page++;
479                         addr += PAGE_SIZE;
480                         len -= PAGE_SIZE;
481                         pg_index++;
482                 }
483         }
484         return 0;
485 }
486 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
487 /* helper function to cleanup a GEM/prime object */
488 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
489 {
490         struct dma_buf_attachment *attach;
491         struct dma_buf *dma_buf;
492         attach = obj->import_attach;
493         if (sg)
494                 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
495         dma_buf = attach->dmabuf;
496         dma_buf_detach(attach->dmabuf, attach);
497         /* remove the reference */
498         dma_buf_put(dma_buf);
499 }
500 EXPORT_SYMBOL(drm_prime_gem_destroy);
501
502 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
503 {
504         INIT_LIST_HEAD(&prime_fpriv->head);
505         mutex_init(&prime_fpriv->lock);
506 }
507 EXPORT_SYMBOL(drm_prime_init_file_private);
508
509 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
510 {
511         /* by now drm_gem_release should've made sure the list is empty */
512         WARN_ON(!list_empty(&prime_fpriv->head));
513 }
514 EXPORT_SYMBOL(drm_prime_destroy_file_private);
515
516 int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
517 {
518         struct drm_prime_member *member;
519
520         member = kmalloc(sizeof(*member), GFP_KERNEL);
521         if (!member)
522                 return -ENOMEM;
523
524         get_dma_buf(dma_buf);
525         member->dma_buf = dma_buf;
526         member->handle = handle;
527         list_add(&member->entry, &prime_fpriv->head);
528         return 0;
529 }
530 EXPORT_SYMBOL(drm_prime_add_buf_handle);
531
532 int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
533 {
534         struct drm_prime_member *member;
535
536         list_for_each_entry(member, &prime_fpriv->head, entry) {
537                 if (member->dma_buf == dma_buf) {
538                         *handle = member->handle;
539                         return 0;
540                 }
541         }
542         return -ENOENT;
543 }
544 EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
545
546 void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
547 {
548         struct drm_prime_member *member, *safe;
549
550         mutex_lock(&prime_fpriv->lock);
551         list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
552                 if (member->dma_buf == dma_buf) {
553                         dma_buf_put(dma_buf);
554                         list_del(&member->entry);
555                         kfree(member);
556                 }
557         }
558         mutex_unlock(&prime_fpriv->lock);
559 }
560 EXPORT_SYMBOL(drm_prime_remove_buf_handle);