fb_defio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * linux/drivers/video/fb_defio.c
  3. *
  4. * Copyright (C) 2006 Jaya Kumar
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/mm.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/fb.h>
  19. #include <linux/list.h>
  20. /* to support deferred IO */
  21. #include <linux/rmap.h>
  22. #include <linux/pagemap.h>
  23. struct page *fb_deferred_io_page(struct fb_info *info, unsigned long offs)
  24. {
  25. void *screen_base = (void __force *) info->screen_base;
  26. struct page *page;
  27. if (is_vmalloc_addr(screen_base + offs))
  28. page = vmalloc_to_page(screen_base + offs);
  29. else
  30. page = pfn_to_page((info->fix.smem_start + offs) >> PAGE_SHIFT);
  31. return page;
  32. }
  33. /* this is to find and return the vmalloc-ed fb pages */
  34. static int fb_deferred_io_fault(struct vm_area_struct *vma,
  35. struct vm_fault *vmf)
  36. {
  37. unsigned long offset;
  38. struct page *page;
  39. struct fb_info *info = vma->vm_private_data;
  40. offset = vmf->pgoff << PAGE_SHIFT;
  41. if (offset >= info->fix.smem_len)
  42. return VM_FAULT_SIGBUS;
  43. page = fb_deferred_io_page(info, offset);
  44. if (!page)
  45. return VM_FAULT_SIGBUS;
  46. get_page(page);
  47. if (vma->vm_file)
  48. page->mapping = vma->vm_file->f_mapping;
  49. else
  50. printk(KERN_ERR "no mapping available\n");
  51. BUG_ON(!page->mapping);
  52. page->index = vmf->pgoff;
  53. vmf->page = page;
  54. return 0;
  55. }
  56. int fb_deferred_io_fsync(struct file *file, struct dentry *dentry, int datasync)
  57. {
  58. struct fb_info *info = file->private_data;
  59. /* Skip if deferred io is compiled-in but disabled on this fbdev */
  60. if (!info->fbdefio)
  61. return 0;
  62. /* Kill off the delayed work */
  63. cancel_rearming_delayed_work(&info->deferred_work);
  64. /* Run it immediately */
  65. return schedule_delayed_work(&info->deferred_work, 0);
  66. }
  67. EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
  68. /* vm_ops->page_mkwrite handler */
  69. static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
  70. struct vm_fault *vmf)
  71. {
  72. struct page *page = vmf->page;
  73. struct fb_info *info = vma->vm_private_data;
  74. struct fb_deferred_io *fbdefio = info->fbdefio;
  75. struct page *cur;
  76. /* this is a callback we get when userspace first tries to
  77. write to the page. we schedule a workqueue. that workqueue
  78. will eventually mkclean the touched pages and execute the
  79. deferred framebuffer IO. then if userspace touches a page
  80. again, we repeat the same scheme */
  81. /* protect against the workqueue changing the page list */
  82. mutex_lock(&fbdefio->lock);
  83. /* we loop through the pagelist before adding in order
  84. to keep the pagelist sorted */
  85. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  86. /* this check is to catch the case where a new
  87. process could start writing to the same page
  88. through a new pte. this new access can cause the
  89. mkwrite even when the original ps's pte is marked
  90. writable */
  91. if (unlikely(cur == page))
  92. goto page_already_added;
  93. else if (cur->index > page->index)
  94. break;
  95. }
  96. list_add_tail(&page->lru, &cur->lru);
  97. page_already_added:
  98. mutex_unlock(&fbdefio->lock);
  99. /* come back after delay to process the deferred IO */
  100. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  101. return 0;
  102. }
  103. static const struct vm_operations_struct fb_deferred_io_vm_ops = {
  104. .fault = fb_deferred_io_fault,
  105. .page_mkwrite = fb_deferred_io_mkwrite,
  106. };
  107. static int fb_deferred_io_set_page_dirty(struct page *page)
  108. {
  109. if (!PageDirty(page))
  110. SetPageDirty(page);
  111. return 0;
  112. }
  113. static const struct address_space_operations fb_deferred_io_aops = {
  114. .set_page_dirty = fb_deferred_io_set_page_dirty,
  115. };
  116. static int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
  117. {
  118. vma->vm_ops = &fb_deferred_io_vm_ops;
  119. vma->vm_flags |= ( VM_RESERVED | VM_DONTEXPAND );
  120. if (!(info->flags & FBINFO_VIRTFB))
  121. vma->vm_flags |= VM_IO;
  122. vma->vm_private_data = info;
  123. return 0;
  124. }
  125. /* workqueue callback */
  126. static void fb_deferred_io_work(struct work_struct *work)
  127. {
  128. struct fb_info *info = container_of(work, struct fb_info,
  129. deferred_work.work);
  130. struct fb_deferred_io *fbdefio = info->fbdefio;
  131. struct page *page, *tmp_page;
  132. struct list_head *node, *tmp_node;
  133. struct list_head non_dirty;
  134. INIT_LIST_HEAD(&non_dirty);
  135. /* here we mkclean the pages, then do all deferred IO */
  136. mutex_lock(&fbdefio->lock);
  137. list_for_each_entry_safe(page, tmp_page, &fbdefio->pagelist, lru) {
  138. lock_page(page);
  139. /*
  140. * The workqueue callback can be triggered after a
  141. * ->page_mkwrite() call but before the PTE has been marked
  142. * dirty. In this case page_mkclean() won't "rearm" the page.
  143. *
  144. * To avoid this, remove those "non-dirty" pages from the
  145. * pagelist before calling the driver's callback, then add
  146. * them back to get processed on the next work iteration.
  147. * At that time, their PTEs will hopefully be dirty for real.
  148. */
  149. if (!page_mkclean(page))
  150. list_move_tail(&page->lru, &non_dirty);
  151. unlock_page(page);
  152. }
  153. /* driver's callback with pagelist */
  154. fbdefio->deferred_io(info, &fbdefio->pagelist);
  155. /* clear the list... */
  156. list_for_each_safe(node, tmp_node, &fbdefio->pagelist) {
  157. list_del(node);
  158. }
  159. /* ... and add back the "non-dirty" pages to the list */
  160. list_splice_tail(&non_dirty, &fbdefio->pagelist);
  161. mutex_unlock(&fbdefio->lock);
  162. }
  163. void fb_deferred_io_init(struct fb_info *info)
  164. {
  165. struct fb_deferred_io *fbdefio = info->fbdefio;
  166. BUG_ON(!fbdefio);
  167. mutex_init(&fbdefio->lock);
  168. info->fbops->fb_mmap = fb_deferred_io_mmap;
  169. INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
  170. INIT_LIST_HEAD(&fbdefio->pagelist);
  171. if (fbdefio->delay == 0) /* set a default of 1 s */
  172. fbdefio->delay = HZ;
  173. }
  174. EXPORT_SYMBOL_GPL(fb_deferred_io_init);
  175. void fb_deferred_io_open(struct fb_info *info,
  176. struct inode *inode,
  177. struct file *file)
  178. {
  179. file->f_mapping->a_ops = &fb_deferred_io_aops;
  180. }
  181. EXPORT_SYMBOL_GPL(fb_deferred_io_open);
  182. void fb_deferred_io_cleanup(struct fb_info *info)
  183. {
  184. struct fb_deferred_io *fbdefio = info->fbdefio;
  185. struct list_head *node, *tmp_node;
  186. struct page *page;
  187. int i;
  188. BUG_ON(!fbdefio);
  189. cancel_delayed_work(&info->deferred_work);
  190. flush_scheduled_work();
  191. /* the list may have still some non-dirty pages at this point */
  192. mutex_lock(&fbdefio->lock);
  193. list_for_each_safe(node, tmp_node, &fbdefio->pagelist) {
  194. list_del(node);
  195. }
  196. mutex_unlock(&fbdefio->lock);
  197. /* clear out the mapping that we setup */
  198. for (i = 0 ; i < info->fix.smem_len; i += PAGE_SIZE) {
  199. page = fb_deferred_io_page(info, i);
  200. page->mapping = NULL;
  201. }
  202. info->fbops->fb_mmap = NULL;
  203. mutex_destroy(&fbdefio->lock);
  204. }
  205. EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
  206. MODULE_LICENSE("GPL");