• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* mm/ashmem.c
3  *
4  * Anonymous Shared Memory Subsystem, ashmem
5  *
6  * Copyright (C) 2008 Google, Inc.
7  *
8  * Robert Love <rlove@google.com>
9  */
10 
11 #define pr_fmt(fmt) "ashmem: " fmt
12 
13 #include <linux/init.h>
14 #include <linux/export.h>
15 #include <linux/file.h>
16 #include <linux/fs.h>
17 #include <linux/falloc.h>
18 #include <linux/miscdevice.h>
19 #include <linux/security.h>
20 #include <linux/mm.h>
21 #include <linux/mman.h>
22 #include <linux/uaccess.h>
23 #include <linux/personality.h>
24 #include <linux/bitops.h>
25 #include <linux/mutex.h>
26 #include <linux/shmem_fs.h>
27 #include <linux/memcheck.h>
28 #include "ashmem.h"
29 
30 #define ASHMEM_NAME_PREFIX "dev/ashmem/"
31 #define ASHMEM_NAME_PREFIX_LEN (sizeof(ASHMEM_NAME_PREFIX) - 1)
32 #define ASHMEM_FULL_NAME_LEN (ASHMEM_NAME_LEN + ASHMEM_NAME_PREFIX_LEN)
33 
34 /**
35  * struct ashmem_area - The anonymous shared memory area
36  * @name:		The optional name in /proc/pid/maps
37  * @unpinned_list:	The list of all ashmem areas
38  * @file:		The shmem-based backing file
39  * @size:		The size of the mapping, in bytes
40  * @prot_mask:		The allowed protection bits, as vm_flags
41  *
42  * The lifecycle of this structure is from our parent file's open() until
43  * its release(). It is also protected by 'ashmem_mutex'
44  *
45  * Warning: Mappings do NOT pin this structure; It dies on close()
46  */
47 struct ashmem_area {
48 	char name[ASHMEM_FULL_NAME_LEN];
49 	struct list_head unpinned_list;
50 	struct file *file;
51 	size_t size;
52 	unsigned long prot_mask;
53 };
54 
55 /**
56  * struct ashmem_range - A range of unpinned/evictable pages
57  * @lru:	         The entry in the LRU list
58  * @unpinned:	         The entry in its area's unpinned list
59  * @asma:	         The associated anonymous shared memory area.
60  * @pgstart:	         The starting page (inclusive)
61  * @pgend:	         The ending page (inclusive)
62  * @purged:	         The purge status (ASHMEM_NOT or ASHMEM_WAS_PURGED)
63  *
64  * The lifecycle of this structure is from unpin to pin.
65  * It is protected by 'ashmem_mutex'
66  */
67 struct ashmem_range {
68 	struct list_head lru;
69 	struct list_head unpinned;
70 	struct ashmem_area *asma;
71 	size_t pgstart;
72 	size_t pgend;
73 	unsigned int purged;
74 };
75 
76 /* LRU list of unpinned pages, protected by ashmem_mutex */
77 static LIST_HEAD(ashmem_lru_list);
78 
79 static atomic_t ashmem_shrink_inflight = ATOMIC_INIT(0);
80 static DECLARE_WAIT_QUEUE_HEAD(ashmem_shrink_wait);
81 
82 /*
83  * long lru_count - The count of pages on our LRU list.
84  *
85  * This is protected by ashmem_mutex.
86  */
87 static unsigned long lru_count;
88 
89 /*
90  * ashmem_mutex - protects the list of and each individual ashmem_area
91  *
92  * Lock Ordering: ashmex_mutex -> i_mutex -> i_alloc_sem
93  */
94 static DEFINE_MUTEX(ashmem_mutex);
95 
96 static struct kmem_cache *ashmem_area_cachep __read_mostly;
97 static struct kmem_cache *ashmem_range_cachep __read_mostly;
98 
99 /*
100  * A separate lockdep class for the backing shmem inodes to resolve the lockdep
101  * warning about the race between kswapd taking fs_reclaim before inode_lock
102  * and write syscall taking inode_lock and then fs_reclaim.
103  * Note that such race is impossible because ashmem does not support write
104  * syscalls operating on the backing shmem.
105  */
106 static struct lock_class_key backing_shmem_inode_class;
107 
ashmem_mutex_lock(void)108 void ashmem_mutex_lock(void)
109 {
110 	mutex_lock(&ashmem_mutex);
111 }
112 
ashmem_mutex_unlock(void)113 void ashmem_mutex_unlock(void)
114 {
115 	mutex_unlock(&ashmem_mutex);
116 }
117 
range_size(struct ashmem_range * range)118 static inline unsigned long range_size(struct ashmem_range *range)
119 {
120 	return range->pgend - range->pgstart + 1;
121 }
122 
range_on_lru(struct ashmem_range * range)123 static inline bool range_on_lru(struct ashmem_range *range)
124 {
125 	return range->purged == ASHMEM_NOT_PURGED;
126 }
127 
page_range_subsumes_range(struct ashmem_range * range,size_t start,size_t end)128 static inline bool page_range_subsumes_range(struct ashmem_range *range,
129 					     size_t start, size_t end)
130 {
131 	return (range->pgstart >= start) && (range->pgend <= end);
132 }
133 
page_range_subsumed_by_range(struct ashmem_range * range,size_t start,size_t end)134 static inline bool page_range_subsumed_by_range(struct ashmem_range *range,
135 						size_t start, size_t end)
136 {
137 	return (range->pgstart <= start) && (range->pgend >= end);
138 }
139 
page_in_range(struct ashmem_range * range,size_t page)140 static inline bool page_in_range(struct ashmem_range *range, size_t page)
141 {
142 	return (range->pgstart <= page) && (range->pgend >= page);
143 }
144 
page_range_in_range(struct ashmem_range * range,size_t start,size_t end)145 static inline bool page_range_in_range(struct ashmem_range *range,
146 				       size_t start, size_t end)
147 {
148 	return page_in_range(range, start) || page_in_range(range, end) ||
149 		page_range_subsumes_range(range, start, end);
150 }
151 
range_before_page(struct ashmem_range * range,size_t page)152 static inline bool range_before_page(struct ashmem_range *range,
153 				     size_t page)
154 {
155 	return range->pgend < page;
156 }
157 
158 #define PROT_MASK		(PROT_EXEC | PROT_READ | PROT_WRITE)
159 
160 /**
161  * lru_add() - Adds a range of memory to the LRU list
162  * @range:     The memory range being added.
163  *
164  * The range is first added to the end (tail) of the LRU list.
165  * After this, the size of the range is added to @lru_count
166  */
lru_add(struct ashmem_range * range)167 static inline void lru_add(struct ashmem_range *range)
168 {
169 	list_add_tail(&range->lru, &ashmem_lru_list);
170 	lru_count += range_size(range);
171 }
172 
173 /**
174  * lru_del() - Removes a range of memory from the LRU list
175  * @range:     The memory range being removed
176  *
177  * The range is first deleted from the LRU list.
178  * After this, the size of the range is removed from @lru_count
179  */
lru_del(struct ashmem_range * range)180 static inline void lru_del(struct ashmem_range *range)
181 {
182 	list_del(&range->lru);
183 	lru_count -= range_size(range);
184 }
185 
186 /**
187  * range_alloc() - Allocates and initializes a new ashmem_range structure
188  * @asma:	   The associated ashmem_area
189  * @prev_range:	   The previous ashmem_range in the sorted asma->unpinned list
190  * @purged:	   Initial purge status (ASMEM_NOT_PURGED or ASHMEM_WAS_PURGED)
191  * @start:	   The starting page (inclusive)
192  * @end:	   The ending page (inclusive)
193  *
194  * This function is protected by ashmem_mutex.
195  */
range_alloc(struct ashmem_area * asma,struct ashmem_range * prev_range,unsigned int purged,size_t start,size_t end,struct ashmem_range ** new_range)196 static void range_alloc(struct ashmem_area *asma,
197 			struct ashmem_range *prev_range, unsigned int purged,
198 			size_t start, size_t end,
199 			struct ashmem_range **new_range)
200 {
201 	struct ashmem_range *range = *new_range;
202 
203 	*new_range = NULL;
204 	range->asma = asma;
205 	range->pgstart = start;
206 	range->pgend = end;
207 	range->purged = purged;
208 
209 	list_add_tail(&range->unpinned, &prev_range->unpinned);
210 
211 	if (range_on_lru(range))
212 		lru_add(range);
213 }
214 
215 /**
216  * range_del() - Deletes and deallocates an ashmem_range structure
217  * @range:	 The associated ashmem_range that has previously been allocated
218  */
range_del(struct ashmem_range * range)219 static void range_del(struct ashmem_range *range)
220 {
221 	list_del(&range->unpinned);
222 	if (range_on_lru(range))
223 		lru_del(range);
224 	kmem_cache_free(ashmem_range_cachep, range);
225 }
226 
227 /**
228  * range_shrink() - Shrinks an ashmem_range
229  * @range:	    The associated ashmem_range being shrunk
230  * @start:	    The starting byte of the new range
231  * @end:	    The ending byte of the new range
232  *
233  * This does not modify the data inside the existing range in any way - It
234  * simply shrinks the boundaries of the range.
235  *
236  * Theoretically, with a little tweaking, this could eventually be changed
237  * to range_resize, and expand the lru_count if the new range is larger.
238  */
range_shrink(struct ashmem_range * range,size_t start,size_t end)239 static inline void range_shrink(struct ashmem_range *range,
240 				size_t start, size_t end)
241 {
242 	size_t pre = range_size(range);
243 
244 	range->pgstart = start;
245 	range->pgend = end;
246 
247 	if (range_on_lru(range))
248 		lru_count -= pre - range_size(range);
249 }
250 
251 /**
252  * ashmem_open() - Opens an Anonymous Shared Memory structure
253  * @inode:	   The backing file's index node(?)
254  * @file:	   The backing file
255  *
256  * Please note that the ashmem_area is not returned by this function - It is
257  * instead written to "file->private_data".
258  *
259  * Return: 0 if successful, or another code if unsuccessful.
260  */
ashmem_open(struct inode * inode,struct file * file)261 static int ashmem_open(struct inode *inode, struct file *file)
262 {
263 	struct ashmem_area *asma;
264 	int ret;
265 
266 	ret = generic_file_open(inode, file);
267 	if (ret)
268 		return ret;
269 
270 	asma = kmem_cache_zalloc(ashmem_area_cachep, GFP_KERNEL);
271 	if (!asma)
272 		return -ENOMEM;
273 
274 	INIT_LIST_HEAD(&asma->unpinned_list);
275 	memcpy(asma->name, ASHMEM_NAME_PREFIX, ASHMEM_NAME_PREFIX_LEN);
276 	asma->prot_mask = PROT_MASK;
277 	file->private_data = asma;
278 
279 	return 0;
280 }
281 
282 /**
283  * ashmem_release() - Releases an Anonymous Shared Memory structure
284  * @ignored:	      The backing file's Index Node(?) - It is ignored here.
285  * @file:	      The backing file
286  *
287  * Return: 0 if successful. If it is anything else, go have a coffee and
288  * try again.
289  */
ashmem_release(struct inode * ignored,struct file * file)290 static int ashmem_release(struct inode *ignored, struct file *file)
291 {
292 	struct ashmem_area *asma = file->private_data;
293 	struct ashmem_range *range, *next;
294 
295 	mutex_lock(&ashmem_mutex);
296 	list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned)
297 		range_del(range);
298 	mutex_unlock(&ashmem_mutex);
299 
300 	if (asma->file)
301 		fput(asma->file);
302 	kmem_cache_free(ashmem_area_cachep, asma);
303 
304 	return 0;
305 }
306 
ashmem_read_iter(struct kiocb * iocb,struct iov_iter * iter)307 static ssize_t ashmem_read_iter(struct kiocb *iocb, struct iov_iter *iter)
308 {
309 	struct ashmem_area *asma = iocb->ki_filp->private_data;
310 	int ret = 0;
311 
312 	mutex_lock(&ashmem_mutex);
313 
314 	/* If size is not set, or set to 0, always return EOF. */
315 	if (asma->size == 0)
316 		goto out_unlock;
317 
318 	if (!asma->file) {
319 		ret = -EBADF;
320 		goto out_unlock;
321 	}
322 
323 	/*
324 	 * asma and asma->file are used outside the lock here.  We assume
325 	 * once asma->file is set it will never be changed, and will not
326 	 * be destroyed until all references to the file are dropped and
327 	 * ashmem_release is called.
328 	 */
329 	mutex_unlock(&ashmem_mutex);
330 	ret = vfs_iter_read(asma->file, iter, &iocb->ki_pos, 0);
331 	mutex_lock(&ashmem_mutex);
332 	if (ret > 0)
333 		asma->file->f_pos = iocb->ki_pos;
334 out_unlock:
335 	mutex_unlock(&ashmem_mutex);
336 	return ret;
337 }
338 
ashmem_llseek(struct file * file,loff_t offset,int origin)339 static loff_t ashmem_llseek(struct file *file, loff_t offset, int origin)
340 {
341 	struct ashmem_area *asma = file->private_data;
342 	loff_t ret;
343 
344 	mutex_lock(&ashmem_mutex);
345 
346 	if (asma->size == 0) {
347 		mutex_unlock(&ashmem_mutex);
348 		return -EINVAL;
349 	}
350 
351 	if (!asma->file) {
352 		mutex_unlock(&ashmem_mutex);
353 		return -EBADF;
354 	}
355 
356 	mutex_unlock(&ashmem_mutex);
357 
358 	ret = vfs_llseek(asma->file, offset, origin);
359 	if (ret < 0)
360 		return ret;
361 
362 	/** Copy f_pos from backing file, since f_ops->llseek() sets it */
363 	file->f_pos = asma->file->f_pos;
364 	return ret;
365 }
366 
calc_vm_may_flags(unsigned long prot)367 static inline vm_flags_t calc_vm_may_flags(unsigned long prot)
368 {
369 	return _calc_vm_trans(prot, PROT_READ,  VM_MAYREAD) |
370 	       _calc_vm_trans(prot, PROT_WRITE, VM_MAYWRITE) |
371 	       _calc_vm_trans(prot, PROT_EXEC,  VM_MAYEXEC);
372 }
373 
ashmem_vmfile_mmap(struct file * file,struct vm_area_struct * vma)374 static int ashmem_vmfile_mmap(struct file *file, struct vm_area_struct *vma)
375 {
376 	/* do not allow to mmap ashmem backing shmem file directly */
377 	return -EPERM;
378 }
379 
380 static unsigned long
ashmem_vmfile_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)381 ashmem_vmfile_get_unmapped_area(struct file *file, unsigned long addr,
382 				unsigned long len, unsigned long pgoff,
383 				unsigned long flags)
384 {
385 	return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
386 }
387 
ashmem_mmap(struct file * file,struct vm_area_struct * vma)388 static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
389 {
390 	static struct file_operations vmfile_fops;
391 	struct ashmem_area *asma = file->private_data;
392 	int ret = 0;
393 
394 	mutex_lock(&ashmem_mutex);
395 
396 	/* user needs to SET_SIZE before mapping */
397 	if (!asma->size) {
398 		ret = -EINVAL;
399 		goto out;
400 	}
401 
402 	/* requested mapping size larger than object size */
403 	if (vma->vm_end - vma->vm_start > PAGE_ALIGN(asma->size)) {
404 		ret = -EINVAL;
405 		goto out;
406 	}
407 
408 	/* requested protection bits must match our allowed protection mask */
409 	if ((vma->vm_flags & ~calc_vm_prot_bits(asma->prot_mask, 0)) &
410 	    calc_vm_prot_bits(PROT_MASK, 0)) {
411 		ret = -EPERM;
412 		goto out;
413 	}
414 	vma->vm_flags &= ~calc_vm_may_flags(~asma->prot_mask);
415 
416 	if (!asma->file) {
417 		char *name = ASHMEM_NAME_DEF;
418 		struct file *vmfile;
419 		struct inode *inode;
420 
421 		if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0')
422 			name = asma->name;
423 
424 		/* ... and allocate the backing shmem file */
425 		vmfile = shmem_file_setup(name, asma->size, vma->vm_flags);
426 		if (IS_ERR(vmfile)) {
427 			ret = PTR_ERR(vmfile);
428 			goto out;
429 		}
430 		vmfile->f_mode |= FMODE_LSEEK;
431 		inode = file_inode(vmfile);
432 		lockdep_set_class(&inode->i_rwsem, &backing_shmem_inode_class);
433 		asma->file = vmfile;
434 		/*
435 		 * override mmap operation of the vmfile so that it can't be
436 		 * remapped which would lead to creation of a new vma with no
437 		 * asma permission checks. Have to override get_unmapped_area
438 		 * as well to prevent VM_BUG_ON check for f_ops modification.
439 		 */
440 		if (!vmfile_fops.mmap) {
441 			vmfile_fops = *vmfile->f_op;
442 			vmfile_fops.mmap = ashmem_vmfile_mmap;
443 			vmfile_fops.get_unmapped_area =
444 					ashmem_vmfile_get_unmapped_area;
445 		}
446 		vmfile->f_op = &vmfile_fops;
447 	}
448 	get_file(asma->file);
449 
450 	/*
451 	 * XXX - Reworked to use shmem_zero_setup() instead of
452 	 * shmem_set_file while we're in staging. -jstultz
453 	 */
454 	if (vma->vm_flags & VM_SHARED) {
455 		ret = shmem_zero_setup(vma);
456 		if (ret) {
457 			fput(asma->file);
458 			goto out;
459 		}
460 	} else {
461 		vma_set_anonymous(vma);
462 	}
463 
464 	if (vma->vm_file)
465 		fput(vma->vm_file);
466 	vma->vm_file = asma->file;
467 
468 out:
469 	mutex_unlock(&ashmem_mutex);
470 	return ret;
471 }
472 
473 /*
474  * ashmem_shrink - our cache shrinker, called from mm/vmscan.c
475  *
476  * 'nr_to_scan' is the number of objects to scan for freeing.
477  *
478  * 'gfp_mask' is the mask of the allocation that got us into this mess.
479  *
480  * Return value is the number of objects freed or -1 if we cannot
481  * proceed without risk of deadlock (due to gfp_mask).
482  *
483  * We approximate LRU via least-recently-unpinned, jettisoning unpinned partial
484  * chunks of ashmem regions LRU-wise one-at-a-time until we hit 'nr_to_scan'
485  * pages freed.
486  */
487 static unsigned long
ashmem_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)488 ashmem_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
489 {
490 	unsigned long freed = 0;
491 
492 	/* We might recurse into filesystem code, so bail out if necessary */
493 	if (!(sc->gfp_mask & __GFP_FS))
494 		return SHRINK_STOP;
495 
496 	if (!mutex_trylock(&ashmem_mutex))
497 		return -1;
498 
499 	while (!list_empty(&ashmem_lru_list)) {
500 		struct ashmem_range *range =
501 			list_first_entry(&ashmem_lru_list, typeof(*range), lru);
502 		loff_t start = range->pgstart * PAGE_SIZE;
503 		loff_t end = (range->pgend + 1) * PAGE_SIZE;
504 		struct file *f = range->asma->file;
505 
506 		get_file(f);
507 		atomic_inc(&ashmem_shrink_inflight);
508 		range->purged = ASHMEM_WAS_PURGED;
509 		lru_del(range);
510 
511 		freed += range_size(range);
512 		mutex_unlock(&ashmem_mutex);
513 		f->f_op->fallocate(f,
514 				   FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
515 				   start, end - start);
516 		fput(f);
517 		if (atomic_dec_and_test(&ashmem_shrink_inflight))
518 			wake_up_all(&ashmem_shrink_wait);
519 		if (!mutex_trylock(&ashmem_mutex))
520 			goto out;
521 		if (--sc->nr_to_scan <= 0)
522 			break;
523 	}
524 	mutex_unlock(&ashmem_mutex);
525 out:
526 	return freed;
527 }
528 
529 static unsigned long
ashmem_shrink_count(struct shrinker * shrink,struct shrink_control * sc)530 ashmem_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
531 {
532 	/*
533 	 * note that lru_count is count of pages on the lru, not a count of
534 	 * objects on the list. This means the scan function needs to return the
535 	 * number of pages freed, not the number of objects scanned.
536 	 */
537 	return lru_count;
538 }
539 
540 static struct shrinker ashmem_shrinker = {
541 	.count_objects = ashmem_shrink_count,
542 	.scan_objects = ashmem_shrink_scan,
543 	/*
544 	 * XXX (dchinner): I wish people would comment on why they need on
545 	 * significant changes to the default value here
546 	 */
547 	.seeks = DEFAULT_SEEKS * 4,
548 };
549 
set_prot_mask(struct ashmem_area * asma,unsigned long prot)550 static int set_prot_mask(struct ashmem_area *asma, unsigned long prot)
551 {
552 	int ret = 0;
553 
554 	mutex_lock(&ashmem_mutex);
555 
556 	/* the user can only remove, not add, protection bits */
557 	if ((asma->prot_mask & prot) != prot) {
558 		ret = -EINVAL;
559 		goto out;
560 	}
561 
562 	/* does the application expect PROT_READ to imply PROT_EXEC? */
563 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
564 		prot |= PROT_EXEC;
565 
566 	asma->prot_mask = prot;
567 
568 out:
569 	mutex_unlock(&ashmem_mutex);
570 	return ret;
571 }
572 
set_name(struct ashmem_area * asma,void __user * name)573 static int set_name(struct ashmem_area *asma, void __user *name)
574 {
575 	int len;
576 	int ret = 0;
577 	char local_name[ASHMEM_NAME_LEN];
578 
579 	/*
580 	 * Holding the ashmem_mutex while doing a copy_from_user might cause
581 	 * an data abort which would try to access mmap_lock. If another
582 	 * thread has invoked ashmem_mmap then it will be holding the
583 	 * semaphore and will be waiting for ashmem_mutex, there by leading to
584 	 * deadlock. We'll release the mutex and take the name to a local
585 	 * variable that does not need protection and later copy the local
586 	 * variable to the structure member with lock held.
587 	 */
588 	len = strncpy_from_user(local_name, name, ASHMEM_NAME_LEN);
589 	if (len < 0)
590 		return len;
591 
592 	mutex_lock(&ashmem_mutex);
593 	/* cannot change an existing mapping's name */
594 	if (asma->file)
595 		ret = -EINVAL;
596 	else
597 		strscpy(asma->name + ASHMEM_NAME_PREFIX_LEN, local_name,
598 			ASHMEM_NAME_LEN);
599 
600 	mutex_unlock(&ashmem_mutex);
601 	return ret;
602 }
603 
get_name(struct ashmem_area * asma,void __user * name)604 static int get_name(struct ashmem_area *asma, void __user *name)
605 {
606 	int ret = 0;
607 	size_t len;
608 	/*
609 	 * Have a local variable to which we'll copy the content
610 	 * from asma with the lock held. Later we can copy this to the user
611 	 * space safely without holding any locks. So even if we proceed to
612 	 * wait for mmap_lock, it won't lead to deadlock.
613 	 */
614 	char local_name[ASHMEM_NAME_LEN];
615 
616 	mutex_lock(&ashmem_mutex);
617 	if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') {
618 		/*
619 		 * Copying only `len', instead of ASHMEM_NAME_LEN, bytes
620 		 * prevents us from revealing one user's stack to another.
621 		 */
622 		len = strlen(asma->name + ASHMEM_NAME_PREFIX_LEN) + 1;
623 		memcpy(local_name, asma->name + ASHMEM_NAME_PREFIX_LEN, len);
624 	} else {
625 		len = sizeof(ASHMEM_NAME_DEF);
626 		memcpy(local_name, ASHMEM_NAME_DEF, len);
627 	}
628 	mutex_unlock(&ashmem_mutex);
629 
630 	/*
631 	 * Now we are just copying from the stack variable to userland
632 	 * No lock held
633 	 */
634 	if (copy_to_user(name, local_name, len))
635 		ret = -EFAULT;
636 	return ret;
637 }
638 
639 /*
640  * ashmem_pin - pin the given ashmem region, returning whether it was
641  * previously purged (ASHMEM_WAS_PURGED) or not (ASHMEM_NOT_PURGED).
642  *
643  * Caller must hold ashmem_mutex.
644  */
ashmem_pin(struct ashmem_area * asma,size_t pgstart,size_t pgend,struct ashmem_range ** new_range)645 static int ashmem_pin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
646 		      struct ashmem_range **new_range)
647 {
648 	struct ashmem_range *range, *next;
649 	int ret = ASHMEM_NOT_PURGED;
650 
651 	list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
652 		/* moved past last applicable page; we can short circuit */
653 		if (range_before_page(range, pgstart))
654 			break;
655 
656 		/*
657 		 * The user can ask us to pin pages that span multiple ranges,
658 		 * or to pin pages that aren't even unpinned, so this is messy.
659 		 *
660 		 * Four cases:
661 		 * 1. The requested range subsumes an existing range, so we
662 		 *    just remove the entire matching range.
663 		 * 2. The requested range overlaps the start of an existing
664 		 *    range, so we just update that range.
665 		 * 3. The requested range overlaps the end of an existing
666 		 *    range, so we just update that range.
667 		 * 4. The requested range punches a hole in an existing range,
668 		 *    so we have to update one side of the range and then
669 		 *    create a new range for the other side.
670 		 */
671 		if (page_range_in_range(range, pgstart, pgend)) {
672 			ret |= range->purged;
673 
674 			/* Case #1: Easy. Just nuke the whole thing. */
675 			if (page_range_subsumes_range(range, pgstart, pgend)) {
676 				range_del(range);
677 				continue;
678 			}
679 
680 			/* Case #2: We overlap from the start, so adjust it */
681 			if (range->pgstart >= pgstart) {
682 				range_shrink(range, pgend + 1, range->pgend);
683 				continue;
684 			}
685 
686 			/* Case #3: We overlap from the rear, so adjust it */
687 			if (range->pgend <= pgend) {
688 				range_shrink(range, range->pgstart,
689 					     pgstart - 1);
690 				continue;
691 			}
692 
693 			/*
694 			 * Case #4: We eat a chunk out of the middle. A bit
695 			 * more complicated, we allocate a new range for the
696 			 * second half and adjust the first chunk's endpoint.
697 			 */
698 			range_alloc(asma, range, range->purged,
699 				    pgend + 1, range->pgend, new_range);
700 			range_shrink(range, range->pgstart, pgstart - 1);
701 			break;
702 		}
703 	}
704 
705 	return ret;
706 }
707 
708 /*
709  * ashmem_unpin - unpin the given range of pages. Returns zero on success.
710  *
711  * Caller must hold ashmem_mutex.
712  */
ashmem_unpin(struct ashmem_area * asma,size_t pgstart,size_t pgend,struct ashmem_range ** new_range)713 static int ashmem_unpin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
714 			struct ashmem_range **new_range)
715 {
716 	struct ashmem_range *range, *next;
717 	unsigned int purged = ASHMEM_NOT_PURGED;
718 
719 restart:
720 	list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
721 		/* short circuit: this is our insertion point */
722 		if (range_before_page(range, pgstart))
723 			break;
724 
725 		/*
726 		 * The user can ask us to unpin pages that are already entirely
727 		 * or partially pinned. We handle those two cases here.
728 		 */
729 		if (page_range_subsumed_by_range(range, pgstart, pgend))
730 			return 0;
731 		if (page_range_in_range(range, pgstart, pgend)) {
732 			pgstart = min(range->pgstart, pgstart);
733 			pgend = max(range->pgend, pgend);
734 			purged |= range->purged;
735 			range_del(range);
736 			goto restart;
737 		}
738 	}
739 
740 	range_alloc(asma, range, purged, pgstart, pgend, new_range);
741 	return 0;
742 }
743 
744 /*
745  * ashmem_get_pin_status - Returns ASHMEM_IS_UNPINNED if _any_ pages in the
746  * given interval are unpinned and ASHMEM_IS_PINNED otherwise.
747  *
748  * Caller must hold ashmem_mutex.
749  */
ashmem_get_pin_status(struct ashmem_area * asma,size_t pgstart,size_t pgend)750 static int ashmem_get_pin_status(struct ashmem_area *asma, size_t pgstart,
751 				 size_t pgend)
752 {
753 	struct ashmem_range *range;
754 	int ret = ASHMEM_IS_PINNED;
755 
756 	list_for_each_entry(range, &asma->unpinned_list, unpinned) {
757 		if (range_before_page(range, pgstart))
758 			break;
759 		if (page_range_in_range(range, pgstart, pgend)) {
760 			ret = ASHMEM_IS_UNPINNED;
761 			break;
762 		}
763 	}
764 
765 	return ret;
766 }
767 
ashmem_pin_unpin(struct ashmem_area * asma,unsigned long cmd,void __user * p)768 static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
769 			    void __user *p)
770 {
771 	struct ashmem_pin pin;
772 	size_t pgstart, pgend;
773 	int ret = -EINVAL;
774 	struct ashmem_range *range = NULL;
775 
776 	if (copy_from_user(&pin, p, sizeof(pin)))
777 		return -EFAULT;
778 
779 	if (cmd == ASHMEM_PIN || cmd == ASHMEM_UNPIN) {
780 		range = kmem_cache_zalloc(ashmem_range_cachep, GFP_KERNEL);
781 		if (!range)
782 			return -ENOMEM;
783 	}
784 
785 	mutex_lock(&ashmem_mutex);
786 	wait_event(ashmem_shrink_wait, !atomic_read(&ashmem_shrink_inflight));
787 
788 	if (!asma->file)
789 		goto out_unlock;
790 
791 	/* per custom, you can pass zero for len to mean "everything onward" */
792 	if (!pin.len)
793 		pin.len = PAGE_ALIGN(asma->size) - pin.offset;
794 
795 	if ((pin.offset | pin.len) & ~PAGE_MASK)
796 		goto out_unlock;
797 
798 	if (((__u32)-1) - pin.offset < pin.len)
799 		goto out_unlock;
800 
801 	if (PAGE_ALIGN(asma->size) < pin.offset + pin.len)
802 		goto out_unlock;
803 
804 	pgstart = pin.offset / PAGE_SIZE;
805 	pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
806 
807 	switch (cmd) {
808 	case ASHMEM_PIN:
809 		ret = ashmem_pin(asma, pgstart, pgend, &range);
810 		break;
811 	case ASHMEM_UNPIN:
812 		ret = ashmem_unpin(asma, pgstart, pgend, &range);
813 		break;
814 	case ASHMEM_GET_PIN_STATUS:
815 		ret = ashmem_get_pin_status(asma, pgstart, pgend);
816 		break;
817 	}
818 
819 out_unlock:
820 	mutex_unlock(&ashmem_mutex);
821 	if (range)
822 		kmem_cache_free(ashmem_range_cachep, range);
823 
824 	return ret;
825 }
826 
ashmem_ioctl(struct file * file,unsigned int cmd,unsigned long arg)827 static long ashmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
828 {
829 	struct ashmem_area *asma = file->private_data;
830 	long ret = -ENOTTY;
831 
832 	switch (cmd) {
833 	case ASHMEM_SET_NAME:
834 		ret = set_name(asma, (void __user *)arg);
835 		break;
836 	case ASHMEM_GET_NAME:
837 		ret = get_name(asma, (void __user *)arg);
838 		break;
839 	case ASHMEM_SET_SIZE:
840 		ret = -EINVAL;
841 		mutex_lock(&ashmem_mutex);
842 		if (!asma->file) {
843 			ret = 0;
844 			asma->size = (size_t)arg;
845 		}
846 		mutex_unlock(&ashmem_mutex);
847 		break;
848 	case ASHMEM_GET_SIZE:
849 		ret = asma->size;
850 		break;
851 	case ASHMEM_SET_PROT_MASK:
852 		ret = set_prot_mask(asma, arg);
853 		break;
854 	case ASHMEM_GET_PROT_MASK:
855 		ret = asma->prot_mask;
856 		break;
857 	case ASHMEM_PIN:
858 	case ASHMEM_UNPIN:
859 	case ASHMEM_GET_PIN_STATUS:
860 		ret = ashmem_pin_unpin(asma, cmd, (void __user *)arg);
861 		break;
862 	case ASHMEM_PURGE_ALL_CACHES:
863 		ret = -EPERM;
864 		if (capable(CAP_SYS_ADMIN)) {
865 			struct shrink_control sc = {
866 				.gfp_mask = GFP_KERNEL,
867 				.nr_to_scan = LONG_MAX,
868 			};
869 			ret = ashmem_shrink_count(&ashmem_shrinker, &sc);
870 			ashmem_shrink_scan(&ashmem_shrinker, &sc);
871 		}
872 		break;
873 	}
874 
875 	return ret;
876 }
877 
878 /* support of 32bit userspace on 64bit platforms */
879 #ifdef CONFIG_COMPAT
compat_ashmem_ioctl(struct file * file,unsigned int cmd,unsigned long arg)880 static long compat_ashmem_ioctl(struct file *file, unsigned int cmd,
881 				unsigned long arg)
882 {
883 	switch (cmd) {
884 	case COMPAT_ASHMEM_SET_SIZE:
885 		cmd = ASHMEM_SET_SIZE;
886 		break;
887 	case COMPAT_ASHMEM_SET_PROT_MASK:
888 		cmd = ASHMEM_SET_PROT_MASK;
889 		break;
890 	}
891 	return ashmem_ioctl(file, cmd, arg);
892 }
893 #endif
894 #ifdef CONFIG_PROC_FS
ashmem_show_fdinfo(struct seq_file * m,struct file * file)895 static void ashmem_show_fdinfo(struct seq_file *m, struct file *file)
896 {
897 	struct ashmem_area *asma = file->private_data;
898 
899 	mutex_lock(&ashmem_mutex);
900 
901 	if (asma->file)
902 		seq_printf(m, "inode:\t%ld\n", file_inode(asma->file)->i_ino);
903 
904 	if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0')
905 		seq_printf(m, "name:\t%s\n",
906 			   asma->name + ASHMEM_NAME_PREFIX_LEN);
907 
908 	mutex_unlock(&ashmem_mutex);
909 }
910 #endif
911 static const struct file_operations ashmem_fops = {
912 	.owner = THIS_MODULE,
913 	.open = ashmem_open,
914 	.release = ashmem_release,
915 	.read_iter = ashmem_read_iter,
916 	.llseek = ashmem_llseek,
917 	.mmap = ashmem_mmap,
918 	.unlocked_ioctl = ashmem_ioctl,
919 #ifdef CONFIG_COMPAT
920 	.compat_ioctl = compat_ashmem_ioctl,
921 #endif
922 #ifdef CONFIG_PROC_FS
923 	.show_fdinfo = ashmem_show_fdinfo,
924 #endif
925 };
926 
is_ashmem_file(struct file * file)927 int is_ashmem_file(struct file *file)
928 {
929 	return file->f_op == &ashmem_fops;
930 }
931 
932 static struct miscdevice ashmem_misc = {
933 	.minor = MISC_DYNAMIC_MINOR,
934 	.name = "ashmem",
935 	.fops = &ashmem_fops,
936 };
937 
get_ashmem_size_by_file(struct file * f)938 size_t get_ashmem_size_by_file(struct file *f)
939 {
940 	struct ashmem_area *asma = f->private_data;
941 
942 	if (asma)
943 		return asma->size;
944 	return 0;
945 }
946 
get_ashmem_name_by_file(struct file * f)947 char *get_ashmem_name_by_file(struct file *f)
948 {
949 	struct ashmem_area *asma = f->private_data;
950 
951 	if (asma)
952 		return asma->name;
953 	return NULL;
954 }
955 
ashmem_init(void)956 static int __init ashmem_init(void)
957 {
958 	int ret = -ENOMEM;
959 
960 	ashmem_area_cachep = kmem_cache_create("ashmem_area_cache",
961 					       sizeof(struct ashmem_area),
962 					       0, 0, NULL);
963 	if (!ashmem_area_cachep) {
964 		pr_err("failed to create slab cache\n");
965 		goto out;
966 	}
967 
968 	ashmem_range_cachep = kmem_cache_create("ashmem_range_cache",
969 						sizeof(struct ashmem_range),
970 						0, 0, NULL);
971 	if (!ashmem_range_cachep) {
972 		pr_err("failed to create slab cache\n");
973 		goto out_free1;
974 	}
975 
976 	ret = misc_register(&ashmem_misc);
977 	if (ret) {
978 		pr_err("failed to register misc device!\n");
979 		goto out_free2;
980 	}
981 
982 	ret = register_shrinker(&ashmem_shrinker);
983 	if (ret) {
984 		pr_err("failed to register shrinker!\n");
985 		goto out_demisc;
986 	}
987 	init_ashmem_process_info();
988 	pr_info("initialized\n");
989 
990 	return 0;
991 
992 out_demisc:
993 	misc_deregister(&ashmem_misc);
994 out_free2:
995 	kmem_cache_destroy(ashmem_range_cachep);
996 out_free1:
997 	kmem_cache_destroy(ashmem_area_cachep);
998 out:
999 	return ret;
1000 }
1001 device_initcall(ashmem_init);
1002