videobuf2-vmalloc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * videobuf2-vmalloc.c - vmalloc memory allocator for videobuf2
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <media/videobuf2-core.h>
  18. #include <media/videobuf2-memops.h>
  19. struct vb2_vmalloc_buf {
  20. void *vaddr;
  21. struct page **pages;
  22. int write;
  23. unsigned long size;
  24. unsigned int n_pages;
  25. atomic_t refcount;
  26. struct vb2_vmarea_handler handler;
  27. };
  28. static void vb2_vmalloc_put(void *buf_priv);
  29. static void *vb2_vmalloc_alloc(void *alloc_ctx, unsigned long size)
  30. {
  31. struct vb2_vmalloc_buf *buf;
  32. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  33. if (!buf)
  34. return NULL;
  35. buf->size = size;
  36. buf->vaddr = vmalloc_user(buf->size);
  37. buf->handler.refcount = &buf->refcount;
  38. buf->handler.put = vb2_vmalloc_put;
  39. buf->handler.arg = buf;
  40. if (!buf->vaddr) {
  41. pr_debug("vmalloc of size %ld failed\n", buf->size);
  42. kfree(buf);
  43. return NULL;
  44. }
  45. atomic_inc(&buf->refcount);
  46. return buf;
  47. }
  48. static void vb2_vmalloc_put(void *buf_priv)
  49. {
  50. struct vb2_vmalloc_buf *buf = buf_priv;
  51. if (atomic_dec_and_test(&buf->refcount)) {
  52. vfree(buf->vaddr);
  53. kfree(buf);
  54. }
  55. }
  56. static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
  57. unsigned long size, int write)
  58. {
  59. struct vb2_vmalloc_buf *buf;
  60. unsigned long first, last;
  61. int n_pages, offset;
  62. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  63. if (!buf)
  64. return NULL;
  65. buf->write = write;
  66. offset = vaddr & ~PAGE_MASK;
  67. buf->size = size;
  68. first = vaddr >> PAGE_SHIFT;
  69. last = (vaddr + size - 1) >> PAGE_SHIFT;
  70. buf->n_pages = last - first + 1;
  71. buf->pages = kzalloc(buf->n_pages * sizeof(struct page *), GFP_KERNEL);
  72. if (!buf->pages)
  73. goto fail_pages_array_alloc;
  74. /* current->mm->mmap_sem is taken by videobuf2 core */
  75. n_pages = get_user_pages(current, current->mm, vaddr & PAGE_MASK,
  76. buf->n_pages, write, 1, /* force */
  77. buf->pages, NULL);
  78. if (n_pages != buf->n_pages)
  79. goto fail_get_user_pages;
  80. buf->vaddr = vm_map_ram(buf->pages, buf->n_pages, -1, PAGE_KERNEL);
  81. if (!buf->vaddr)
  82. goto fail_get_user_pages;
  83. buf->vaddr += offset;
  84. return buf;
  85. fail_get_user_pages:
  86. pr_debug("get_user_pages requested/got: %d/%d]\n", n_pages,
  87. buf->n_pages);
  88. while (--n_pages >= 0)
  89. put_page(buf->pages[n_pages]);
  90. kfree(buf->pages);
  91. fail_pages_array_alloc:
  92. kfree(buf);
  93. return NULL;
  94. }
  95. static void vb2_vmalloc_put_userptr(void *buf_priv)
  96. {
  97. struct vb2_vmalloc_buf *buf = buf_priv;
  98. unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
  99. unsigned int i;
  100. if (vaddr)
  101. vm_unmap_ram((void *)vaddr, buf->n_pages);
  102. for (i = 0; i < buf->n_pages; ++i) {
  103. if (buf->write)
  104. set_page_dirty_lock(buf->pages[i]);
  105. put_page(buf->pages[i]);
  106. }
  107. kfree(buf->pages);
  108. kfree(buf);
  109. }
  110. static void *vb2_vmalloc_vaddr(void *buf_priv)
  111. {
  112. struct vb2_vmalloc_buf *buf = buf_priv;
  113. if (!buf->vaddr) {
  114. pr_err("Address of an unallocated plane requested "
  115. "or cannot map user pointer\n");
  116. return NULL;
  117. }
  118. return buf->vaddr;
  119. }
  120. static unsigned int vb2_vmalloc_num_users(void *buf_priv)
  121. {
  122. struct vb2_vmalloc_buf *buf = buf_priv;
  123. return atomic_read(&buf->refcount);
  124. }
  125. static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
  126. {
  127. struct vb2_vmalloc_buf *buf = buf_priv;
  128. int ret;
  129. if (!buf) {
  130. pr_err("No memory to map\n");
  131. return -EINVAL;
  132. }
  133. ret = remap_vmalloc_range(vma, buf->vaddr, 0);
  134. if (ret) {
  135. pr_err("Remapping vmalloc memory, error: %d\n", ret);
  136. return ret;
  137. }
  138. /*
  139. * Make sure that vm_areas for 2 buffers won't be merged together
  140. */
  141. vma->vm_flags |= VM_DONTEXPAND;
  142. /*
  143. * Use common vm_area operations to track buffer refcount.
  144. */
  145. vma->vm_private_data = &buf->handler;
  146. vma->vm_ops = &vb2_common_vm_ops;
  147. vma->vm_ops->open(vma);
  148. return 0;
  149. }
  150. const struct vb2_mem_ops vb2_vmalloc_memops = {
  151. .alloc = vb2_vmalloc_alloc,
  152. .put = vb2_vmalloc_put,
  153. .get_userptr = vb2_vmalloc_get_userptr,
  154. .put_userptr = vb2_vmalloc_put_userptr,
  155. .vaddr = vb2_vmalloc_vaddr,
  156. .mmap = vb2_vmalloc_mmap,
  157. .num_users = vb2_vmalloc_num_users,
  158. };
  159. EXPORT_SYMBOL_GPL(vb2_vmalloc_memops);
  160. MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
  161. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  162. MODULE_LICENSE("GPL");