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 
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/mm.h>
16 #include <linux/vmalloc.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/fb.h>
20 #include <linux/list.h>
21 
22 /* to support deferred IO */
23 #include <linux/rmap.h>
24 #include <linux/pagemap.h>
25 
fb_deferred_io_page(struct fb_info * info,unsigned long offs)26 static struct page *fb_deferred_io_page(struct fb_info *info, unsigned long offs)
27 {
28 	void *screen_base = (void __force *) info->screen_base;
29 	struct page *page;
30 
31 	if (is_vmalloc_addr(screen_base + offs))
32 		page = vmalloc_to_page(screen_base + offs);
33 	else
34 		page = pfn_to_page((info->fix.smem_start + offs) >> PAGE_SHIFT);
35 
36 	return page;
37 }
38 
fb_deferred_io_pageref_get(struct fb_info * info,unsigned long offset,struct page * page)39 static struct fb_deferred_io_pageref *fb_deferred_io_pageref_get(struct fb_info *info,
40 								 unsigned long offset,
41 								 struct page *page)
42 {
43 	struct fb_deferred_io *fbdefio = info->fbdefio;
44 	struct list_head *pos = &fbdefio->pagereflist;
45 	unsigned long pgoff = offset >> PAGE_SHIFT;
46 	struct fb_deferred_io_pageref *pageref, *cur;
47 
48 	if (WARN_ON_ONCE(pgoff >= info->npagerefs))
49 		return NULL; /* incorrect allocation size */
50 
51 	/* 1:1 mapping between pageref and page offset */
52 	pageref = &info->pagerefs[pgoff];
53 
54 	/*
55 	 * This check is to catch the case where a new process could start
56 	 * writing to the same page through a new PTE. This new access
57 	 * can cause a call to .page_mkwrite even if the original process'
58 	 * PTE is marked writable.
59 	 */
60 	if (!list_empty(&pageref->list))
61 		goto pageref_already_added;
62 
63 	pageref->page = page;
64 	pageref->offset = pgoff << PAGE_SHIFT;
65 
66 	if (unlikely(fbdefio->sort_pagereflist)) {
67 		/*
68 		 * We loop through the list of pagerefs before adding in
69 		 * order to keep the pagerefs sorted. This has significant
70 		 * overhead of O(n^2) with n being the number of written
71 		 * pages. If possible, drivers should try to work with
72 		 * unsorted page lists instead.
73 		 */
74 		list_for_each_entry(cur, &fbdefio->pagereflist, list) {
75 			if (cur->offset > pageref->offset)
76 				break;
77 		}
78 		pos = &cur->list;
79 	}
80 
81 	list_add_tail(&pageref->list, pos);
82 
83 pageref_already_added:
84 	return pageref;
85 }
86 
fb_deferred_io_pageref_put(struct fb_deferred_io_pageref * pageref,struct fb_info * info)87 static void fb_deferred_io_pageref_put(struct fb_deferred_io_pageref *pageref,
88 				       struct fb_info *info)
89 {
90 	list_del_init(&pageref->list);
91 }
92 
93 /* this is to find and return the vmalloc-ed fb pages */
fb_deferred_io_fault(struct vm_fault * vmf)94 static vm_fault_t fb_deferred_io_fault(struct vm_fault *vmf)
95 {
96 	unsigned long offset;
97 	struct page *page;
98 	struct fb_info *info = vmf->vma->vm_private_data;
99 
100 	offset = vmf->pgoff << PAGE_SHIFT;
101 	if (offset >= info->fix.smem_len)
102 		return VM_FAULT_SIGBUS;
103 
104 	page = fb_deferred_io_page(info, offset);
105 	if (!page)
106 		return VM_FAULT_SIGBUS;
107 
108 	get_page(page);
109 
110 	if (vmf->vma->vm_file)
111 		page->mapping = vmf->vma->vm_file->f_mapping;
112 	else
113 		printk(KERN_ERR "no mapping available\n");
114 
115 	BUG_ON(!page->mapping);
116 	page->index = vmf->pgoff; /* for page_mkclean() */
117 
118 	vmf->page = page;
119 	return 0;
120 }
121 
fb_deferred_io_fsync(struct file * file,loff_t start,loff_t end,int datasync)122 int fb_deferred_io_fsync(struct file *file, loff_t start, loff_t end, int datasync)
123 {
124 	struct fb_info *info = file->private_data;
125 	struct inode *inode = file_inode(file);
126 	int err = file_write_and_wait_range(file, start, end);
127 	if (err)
128 		return err;
129 
130 	/* Skip if deferred io is compiled-in but disabled on this fbdev */
131 	if (!info->fbdefio)
132 		return 0;
133 
134 	inode_lock(inode);
135 	flush_delayed_work(&info->deferred_work);
136 	inode_unlock(inode);
137 
138 	return 0;
139 }
140 EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
141 
142 /*
143  * Adds a page to the dirty list. Call this from struct
144  * vm_operations_struct.page_mkwrite.
145  */
fb_deferred_io_track_page(struct fb_info * info,unsigned long offset,struct page * page)146 static vm_fault_t fb_deferred_io_track_page(struct fb_info *info, unsigned long offset,
147 					    struct page *page)
148 {
149 	struct fb_deferred_io *fbdefio = info->fbdefio;
150 	struct fb_deferred_io_pageref *pageref;
151 	vm_fault_t ret;
152 
153 	/* protect against the workqueue changing the page list */
154 	mutex_lock(&fbdefio->lock);
155 
156 	pageref = fb_deferred_io_pageref_get(info, offset, page);
157 	if (WARN_ON_ONCE(!pageref)) {
158 		ret = VM_FAULT_OOM;
159 		goto err_mutex_unlock;
160 	}
161 
162 	/*
163 	 * We want the page to remain locked from ->page_mkwrite until
164 	 * the PTE is marked dirty to avoid page_mkclean() being called
165 	 * before the PTE is updated, which would leave the page ignored
166 	 * by defio.
167 	 * Do this by locking the page here and informing the caller
168 	 * about it with VM_FAULT_LOCKED.
169 	 */
170 	lock_page(pageref->page);
171 
172 	mutex_unlock(&fbdefio->lock);
173 
174 	/* come back after delay to process the deferred IO */
175 	schedule_delayed_work(&info->deferred_work, fbdefio->delay);
176 	return VM_FAULT_LOCKED;
177 
178 err_mutex_unlock:
179 	mutex_unlock(&fbdefio->lock);
180 	return ret;
181 }
182 
183 /*
184  * fb_deferred_io_page_mkwrite - Mark a page as written for deferred I/O
185  * @fb_info: The fbdev info structure
186  * @vmf: The VM fault
187  *
188  * This is a callback we get when userspace first tries to
189  * write to the page. We schedule a workqueue. That workqueue
190  * will eventually mkclean the touched pages and execute the
191  * deferred framebuffer IO. Then if userspace touches a page
192  * again, we repeat the same scheme.
193  *
194  * Returns:
195  * VM_FAULT_LOCKED on success, or a VM_FAULT error otherwise.
196  */
fb_deferred_io_page_mkwrite(struct fb_info * info,struct vm_fault * vmf)197 static vm_fault_t fb_deferred_io_page_mkwrite(struct fb_info *info, struct vm_fault *vmf)
198 {
199 	unsigned long offset = vmf->pgoff << PAGE_SHIFT;
200 	struct page *page = vmf->page;
201 
202 	file_update_time(vmf->vma->vm_file);
203 
204 	return fb_deferred_io_track_page(info, offset, page);
205 }
206 
207 /* vm_ops->page_mkwrite handler */
fb_deferred_io_mkwrite(struct vm_fault * vmf)208 static vm_fault_t fb_deferred_io_mkwrite(struct vm_fault *vmf)
209 {
210 	struct fb_info *info = vmf->vma->vm_private_data;
211 
212 	return fb_deferred_io_page_mkwrite(info, vmf);
213 }
214 
215 static const struct vm_operations_struct fb_deferred_io_vm_ops = {
216 	.fault		= fb_deferred_io_fault,
217 	.page_mkwrite	= fb_deferred_io_mkwrite,
218 };
219 
220 static const struct address_space_operations fb_deferred_io_aops = {
221 	.dirty_folio	= noop_dirty_folio,
222 };
223 
fb_deferred_io_mmap(struct fb_info * info,struct vm_area_struct * vma)224 int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
225 {
226 	vma->vm_ops = &fb_deferred_io_vm_ops;
227 	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
228 	if (!(info->flags & FBINFO_VIRTFB))
229 		vm_flags_set(vma, VM_IO);
230 	vma->vm_private_data = info;
231 	return 0;
232 }
233 EXPORT_SYMBOL_GPL(fb_deferred_io_mmap);
234 
235 /* workqueue callback */
fb_deferred_io_work(struct work_struct * work)236 static void fb_deferred_io_work(struct work_struct *work)
237 {
238 	struct fb_info *info = container_of(work, struct fb_info, deferred_work.work);
239 	struct fb_deferred_io_pageref *pageref, *next;
240 	struct fb_deferred_io *fbdefio = info->fbdefio;
241 
242 	/* here we mkclean the pages, then do all deferred IO */
243 	mutex_lock(&fbdefio->lock);
244 	list_for_each_entry(pageref, &fbdefio->pagereflist, list) {
245 		struct page *cur = pageref->page;
246 		lock_page(cur);
247 		page_mkclean(cur);
248 		unlock_page(cur);
249 	}
250 
251 	/* driver's callback with pagereflist */
252 	fbdefio->deferred_io(info, &fbdefio->pagereflist);
253 
254 	/* clear the list */
255 	list_for_each_entry_safe(pageref, next, &fbdefio->pagereflist, list)
256 		fb_deferred_io_pageref_put(pageref, info);
257 
258 	mutex_unlock(&fbdefio->lock);
259 }
260 
fb_deferred_io_init(struct fb_info * info)261 int fb_deferred_io_init(struct fb_info *info)
262 {
263 	struct fb_deferred_io *fbdefio = info->fbdefio;
264 	struct fb_deferred_io_pageref *pagerefs;
265 	unsigned long npagerefs, i;
266 	int ret;
267 
268 	BUG_ON(!fbdefio);
269 
270 	if (WARN_ON(!info->fix.smem_len))
271 		return -EINVAL;
272 
273 	mutex_init(&fbdefio->lock);
274 	INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
275 	INIT_LIST_HEAD(&fbdefio->pagereflist);
276 	if (fbdefio->delay == 0) /* set a default of 1 s */
277 		fbdefio->delay = HZ;
278 
279 	npagerefs = DIV_ROUND_UP(info->fix.smem_len, PAGE_SIZE);
280 
281 	/* alloc a page ref for each page of the display memory */
282 	pagerefs = kvcalloc(npagerefs, sizeof(*pagerefs), GFP_KERNEL);
283 	if (!pagerefs) {
284 		ret = -ENOMEM;
285 		goto err;
286 	}
287 	for (i = 0; i < npagerefs; ++i)
288 		INIT_LIST_HEAD(&pagerefs[i].list);
289 	info->npagerefs = npagerefs;
290 	info->pagerefs = pagerefs;
291 
292 	return 0;
293 
294 err:
295 	mutex_destroy(&fbdefio->lock);
296 	return ret;
297 }
298 EXPORT_SYMBOL_GPL(fb_deferred_io_init);
299 
fb_deferred_io_open(struct fb_info * info,struct inode * inode,struct file * file)300 void fb_deferred_io_open(struct fb_info *info,
301 			 struct inode *inode,
302 			 struct file *file)
303 {
304 	struct fb_deferred_io *fbdefio = info->fbdefio;
305 
306 	file->f_mapping->a_ops = &fb_deferred_io_aops;
307 	fbdefio->open_count++;
308 }
309 EXPORT_SYMBOL_GPL(fb_deferred_io_open);
310 
fb_deferred_io_lastclose(struct fb_info * info)311 static void fb_deferred_io_lastclose(struct fb_info *info)
312 {
313 	struct page *page;
314 	int i;
315 
316 	flush_delayed_work(&info->deferred_work);
317 
318 	/* clear out the mapping that we setup */
319 	for (i = 0 ; i < info->fix.smem_len; i += PAGE_SIZE) {
320 		page = fb_deferred_io_page(info, i);
321 		page->mapping = NULL;
322 	}
323 }
324 
fb_deferred_io_release(struct fb_info * info)325 void fb_deferred_io_release(struct fb_info *info)
326 {
327 	struct fb_deferred_io *fbdefio = info->fbdefio;
328 
329 	if (!--fbdefio->open_count)
330 		fb_deferred_io_lastclose(info);
331 }
332 EXPORT_SYMBOL_GPL(fb_deferred_io_release);
333 
fb_deferred_io_cleanup(struct fb_info * info)334 void fb_deferred_io_cleanup(struct fb_info *info)
335 {
336 	struct fb_deferred_io *fbdefio = info->fbdefio;
337 
338 	fb_deferred_io_lastclose(info);
339 
340 	kvfree(info->pagerefs);
341 	mutex_destroy(&fbdefio->lock);
342 }
343 EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
344