3 ** Anonymous Shared Memory Subsystem, ashmem
5 ** Copyright (C) 2008 Google, Inc.
7 ** Robert Love <rlove@google.com>
9 ** This software is licensed under the terms of the GNU General Public
10 ** License version 2, as published by the Free Software Foundation, and
11 ** may be copied, distributed, and modified under those terms.
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/file.h>
22 #include <linux/miscdevice.h>
23 #include <linux/security.h>
25 #include <linux/mman.h>
26 #include <linux/uaccess.h>
27 #include <linux/personality.h>
28 #include <linux/bitops.h>
29 #include <linux/mutex.h>
30 #include <linux/shmem_fs.h>
31 #include <linux/ashmem.h>
33 #define ASHMEM_NAME_PREFIX "dev/ashmem/"
34 #define ASHMEM_NAME_PREFIX_LEN (sizeof(ASHMEM_NAME_PREFIX) - 1)
35 #define ASHMEM_FULL_NAME_LEN (ASHMEM_NAME_LEN + ASHMEM_NAME_PREFIX_LEN)
38 * ashmem_area - anonymous shared memory area
39 * Lifecycle: From our parent file's open() until its release()
40 * Locking: Protected by `ashmem_mutex'
41 * Big Note: Mappings do NOT pin this structure; it dies on close()
44 char name[ASHMEM_FULL_NAME_LEN];/* optional name for /proc/pid/maps */
45 struct list_head unpinned_list; /* list of all ashmem areas */
46 struct file *file; /* the shmem-based backing file */
47 size_t size; /* size of the mapping, in bytes */
48 unsigned long prot_mask; /* allowed prot bits, as vm_flags */
52 * ashmem_range - represents an interval of unpinned (evictable) pages
53 * Lifecycle: From unpin to pin
54 * Locking: Protected by `ashmem_mutex'
57 struct list_head lru; /* entry in LRU list */
58 struct list_head unpinned; /* entry in its area's unpinned list */
59 struct ashmem_area *asma; /* associated area */
60 size_t pgstart; /* starting page, inclusive */
61 size_t pgend; /* ending page, inclusive */
62 unsigned int purged; /* ASHMEM_NOT or ASHMEM_WAS_PURGED */
65 /* LRU list of unpinned pages, protected by ashmem_mutex */
66 static LIST_HEAD(ashmem_lru_list);
68 /* Count of pages on our LRU list, protected by ashmem_mutex */
69 static unsigned long lru_count;
72 * ashmem_mutex - protects the list of and each individual ashmem_area
74 * Lock Ordering: ashmex_mutex -> i_mutex -> i_alloc_sem
76 static DEFINE_MUTEX(ashmem_mutex);
78 static struct kmem_cache *ashmem_area_cachep __read_mostly;
79 static struct kmem_cache *ashmem_range_cachep __read_mostly;
81 #define range_size(range) \
82 ((range)->pgend - (range)->pgstart + 1)
84 #define range_on_lru(range) \
85 ((range)->purged == ASHMEM_NOT_PURGED)
87 #define page_range_subsumes_range(range, start, end) \
88 (((range)->pgstart >= (start)) && ((range)->pgend <= (end)))
90 #define page_range_subsumed_by_range(range, start, end) \
91 (((range)->pgstart <= (start)) && ((range)->pgend >= (end)))
93 #define page_in_range(range, page) \
94 (((range)->pgstart <= (page)) && ((range)->pgend >= (page)))
96 #define page_range_in_range(range, start, end) \
97 (page_in_range(range, start) || page_in_range(range, end) || \
98 page_range_subsumes_range(range, start, end))
100 #define range_before_page(range, page) \
101 ((range)->pgend < (page))
103 #define PROT_MASK (PROT_EXEC | PROT_READ | PROT_WRITE)
105 static inline void lru_add(struct ashmem_range *range)
107 list_add_tail(&range->lru, &ashmem_lru_list);
108 lru_count += range_size(range);
111 static inline void lru_del(struct ashmem_range *range)
113 list_del(&range->lru);
114 lru_count -= range_size(range);
118 * range_alloc - allocate and initialize a new ashmem_range structure
120 * 'asma' - associated ashmem_area
121 * 'prev_range' - the previous ashmem_range in the sorted asma->unpinned list
122 * 'purged' - initial purge value (ASMEM_NOT_PURGED or ASHMEM_WAS_PURGED)
123 * 'start' - starting page, inclusive
124 * 'end' - ending page, inclusive
126 * Caller must hold ashmem_mutex.
128 static int range_alloc(struct ashmem_area *asma,
129 struct ashmem_range *prev_range, unsigned int purged,
130 size_t start, size_t end)
132 struct ashmem_range *range;
134 range = kmem_cache_zalloc(ashmem_range_cachep, GFP_KERNEL);
135 if (unlikely(!range))
139 range->pgstart = start;
141 range->purged = purged;
143 list_add_tail(&range->unpinned, &prev_range->unpinned);
145 if (range_on_lru(range))
151 static void range_del(struct ashmem_range *range)
153 list_del(&range->unpinned);
154 if (range_on_lru(range))
156 kmem_cache_free(ashmem_range_cachep, range);
160 * range_shrink - shrinks a range
162 * Caller must hold ashmem_mutex.
164 static inline void range_shrink(struct ashmem_range *range,
165 size_t start, size_t end)
167 size_t pre = range_size(range);
169 range->pgstart = start;
172 if (range_on_lru(range))
173 lru_count -= pre - range_size(range);
176 static int ashmem_open(struct inode *inode, struct file *file)
178 struct ashmem_area *asma;
181 ret = nonseekable_open(inode, file);
185 asma = kmem_cache_zalloc(ashmem_area_cachep, GFP_KERNEL);
189 INIT_LIST_HEAD(&asma->unpinned_list);
190 memcpy(asma->name, ASHMEM_NAME_PREFIX, ASHMEM_NAME_PREFIX_LEN);
191 asma->prot_mask = PROT_MASK;
192 file->private_data = asma;
197 static int ashmem_release(struct inode *ignored, struct file *file)
199 struct ashmem_area *asma = file->private_data;
200 struct ashmem_range *range, *next;
202 mutex_lock(&ashmem_mutex);
203 list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned)
205 mutex_unlock(&ashmem_mutex);
209 kmem_cache_free(ashmem_area_cachep, asma);
214 static ssize_t ashmem_read(struct file *file, char __user *buf,
215 size_t len, loff_t *pos)
217 struct ashmem_area *asma = file->private_data;
220 mutex_lock(&ashmem_mutex);
222 /* If size is not set, or set to 0, always return EOF. */
223 if (asma->size == 0) {
232 ret = asma->file->f_op->read(asma->file, buf, len, pos);
235 mutex_unlock(&ashmem_mutex);
239 static inline unsigned long
240 calc_vm_may_flags(unsigned long prot)
242 return _calc_vm_trans(prot, PROT_READ, VM_MAYREAD ) |
243 _calc_vm_trans(prot, PROT_WRITE, VM_MAYWRITE) |
244 _calc_vm_trans(prot, PROT_EXEC, VM_MAYEXEC);
247 static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
249 struct ashmem_area *asma = file->private_data;
252 mutex_lock(&ashmem_mutex);
254 /* user needs to SET_SIZE before mapping */
255 if (unlikely(!asma->size)) {
260 /* requested protection bits must match our allowed protection mask */
261 if (unlikely((vma->vm_flags & ~calc_vm_prot_bits(asma->prot_mask)) &
262 calc_vm_prot_bits(PROT_MASK))) {
266 vma->vm_flags &= ~calc_vm_may_flags(~asma->prot_mask);
269 char *name = ASHMEM_NAME_DEF;
272 if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0')
275 /* ... and allocate the backing shmem file */
276 vmfile = shmem_file_setup(name, asma->size, vma->vm_flags);
277 if (unlikely(IS_ERR(vmfile))) {
278 ret = PTR_ERR(vmfile);
283 get_file(asma->file);
285 if (vma->vm_flags & VM_SHARED)
286 shmem_set_file(vma, asma->file);
290 vma->vm_file = asma->file;
292 vma->vm_flags |= VM_CAN_NONLINEAR;
295 mutex_unlock(&ashmem_mutex);
300 * ashmem_shrink - our cache shrinker, called from mm/vmscan.c :: shrink_slab
302 * 'nr_to_scan' is the number of objects (pages) to prune, or 0 to query how
303 * many objects (pages) we have in total.
305 * 'gfp_mask' is the mask of the allocation that got us into this mess.
307 * Return value is the number of objects (pages) remaining, or -1 if we cannot
308 * proceed without risk of deadlock (due to gfp_mask).
310 * We approximate LRU via least-recently-unpinned, jettisoning unpinned partial
311 * chunks of ashmem regions LRU-wise one-at-a-time until we hit 'nr_to_scan'
314 static int ashmem_shrink(int nr_to_scan, gfp_t gfp_mask)
316 struct ashmem_range *range, *next;
318 /* We might recurse into filesystem code, so bail out if necessary */
319 if (nr_to_scan && !(gfp_mask & __GFP_FS))
324 mutex_lock(&ashmem_mutex);
325 list_for_each_entry_safe(range, next, &ashmem_lru_list, lru) {
326 struct inode *inode = range->asma->file->f_dentry->d_inode;
327 loff_t start = range->pgstart * PAGE_SIZE;
328 loff_t end = (range->pgend + 1) * PAGE_SIZE - 1;
330 vmtruncate_range(inode, start, end);
331 range->purged = ASHMEM_WAS_PURGED;
334 nr_to_scan -= range_size(range);
338 mutex_unlock(&ashmem_mutex);
343 static struct shrinker ashmem_shrinker = {
344 .shrink = ashmem_shrink,
345 .seeks = DEFAULT_SEEKS * 4,
348 static int set_prot_mask(struct ashmem_area *asma, unsigned long prot)
352 mutex_lock(&ashmem_mutex);
354 /* the user can only remove, not add, protection bits */
355 if (unlikely((asma->prot_mask & prot) != prot)) {
360 /* does the application expect PROT_READ to imply PROT_EXEC? */
361 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
364 asma->prot_mask = prot;
367 mutex_unlock(&ashmem_mutex);
371 static int set_name(struct ashmem_area *asma, void __user *name)
375 mutex_lock(&ashmem_mutex);
377 /* cannot change an existing mapping's name */
378 if (unlikely(asma->file)) {
383 if (unlikely(copy_from_user(asma->name + ASHMEM_NAME_PREFIX_LEN,
384 name, ASHMEM_NAME_LEN)))
386 asma->name[ASHMEM_FULL_NAME_LEN-1] = '\0';
389 mutex_unlock(&ashmem_mutex);
394 static int get_name(struct ashmem_area *asma, void __user *name)
398 mutex_lock(&ashmem_mutex);
399 if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') {
403 * Copying only `len', instead of ASHMEM_NAME_LEN, bytes
404 * prevents us from revealing one user's stack to another.
406 len = strlen(asma->name + ASHMEM_NAME_PREFIX_LEN) + 1;
407 if (unlikely(copy_to_user(name,
408 asma->name + ASHMEM_NAME_PREFIX_LEN, len)))
411 if (unlikely(copy_to_user(name, ASHMEM_NAME_DEF,
412 sizeof(ASHMEM_NAME_DEF))))
415 mutex_unlock(&ashmem_mutex);
421 * ashmem_pin - pin the given ashmem region, returning whether it was
422 * previously purged (ASHMEM_WAS_PURGED) or not (ASHMEM_NOT_PURGED).
424 * Caller must hold ashmem_mutex.
426 static int ashmem_pin(struct ashmem_area *asma, size_t pgstart, size_t pgend)
428 struct ashmem_range *range, *next;
429 int ret = ASHMEM_NOT_PURGED;
431 list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
432 /* moved past last applicable page; we can short circuit */
433 if (range_before_page(range, pgstart))
437 * The user can ask us to pin pages that span multiple ranges,
438 * or to pin pages that aren't even unpinned, so this is messy.
441 * 1. The requested range subsumes an existing range, so we
442 * just remove the entire matching range.
443 * 2. The requested range overlaps the start of an existing
444 * range, so we just update that range.
445 * 3. The requested range overlaps the end of an existing
446 * range, so we just update that range.
447 * 4. The requested range punches a hole in an existing range,
448 * so we have to update one side of the range and then
449 * create a new range for the other side.
451 if (page_range_in_range(range, pgstart, pgend)) {
452 ret |= range->purged;
454 /* Case #1: Easy. Just nuke the whole thing. */
455 if (page_range_subsumes_range(range, pgstart, pgend)) {
460 /* Case #2: We overlap from the start, so adjust it */
461 if (range->pgstart >= pgstart) {
462 range_shrink(range, pgend + 1, range->pgend);
466 /* Case #3: We overlap from the rear, so adjust it */
467 if (range->pgend <= pgend) {
468 range_shrink(range, range->pgstart, pgstart-1);
473 * Case #4: We eat a chunk out of the middle. A bit
474 * more complicated, we allocate a new range for the
475 * second half and adjust the first chunk's endpoint.
477 range_alloc(asma, range, range->purged,
478 pgend + 1, range->pgend);
479 range_shrink(range, range->pgstart, pgstart - 1);
488 * ashmem_unpin - unpin the given range of pages. Returns zero on success.
490 * Caller must hold ashmem_mutex.
492 static int ashmem_unpin(struct ashmem_area *asma, size_t pgstart, size_t pgend)
494 struct ashmem_range *range, *next;
495 unsigned int purged = ASHMEM_NOT_PURGED;
498 list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
499 /* short circuit: this is our insertion point */
500 if (range_before_page(range, pgstart))
504 * The user can ask us to unpin pages that are already entirely
505 * or partially pinned. We handle those two cases here.
507 if (page_range_subsumed_by_range(range, pgstart, pgend))
509 if (page_range_in_range(range, pgstart, pgend)) {
510 pgstart = min_t(size_t, range->pgstart, pgstart),
511 pgend = max_t(size_t, range->pgend, pgend);
512 purged |= range->purged;
518 return range_alloc(asma, range, purged, pgstart, pgend);
522 * ashmem_get_pin_status - Returns ASHMEM_IS_UNPINNED if _any_ pages in the
523 * given interval are unpinned and ASHMEM_IS_PINNED otherwise.
525 * Caller must hold ashmem_mutex.
527 static int ashmem_get_pin_status(struct ashmem_area *asma, size_t pgstart,
530 struct ashmem_range *range;
531 int ret = ASHMEM_IS_PINNED;
533 list_for_each_entry(range, &asma->unpinned_list, unpinned) {
534 if (range_before_page(range, pgstart))
536 if (page_range_in_range(range, pgstart, pgend)) {
537 ret = ASHMEM_IS_UNPINNED;
545 static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
548 struct ashmem_pin pin;
549 size_t pgstart, pgend;
552 if (unlikely(!asma->file))
555 if (unlikely(copy_from_user(&pin, p, sizeof(pin))))
558 /* per custom, you can pass zero for len to mean "everything onward" */
560 pin.len = PAGE_ALIGN(asma->size) - pin.offset;
562 if (unlikely((pin.offset | pin.len) & ~PAGE_MASK))
565 if (unlikely(((__u32) -1) - pin.offset < pin.len))
568 if (unlikely(PAGE_ALIGN(asma->size) < pin.offset + pin.len))
571 pgstart = pin.offset / PAGE_SIZE;
572 pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
574 mutex_lock(&ashmem_mutex);
578 ret = ashmem_pin(asma, pgstart, pgend);
581 ret = ashmem_unpin(asma, pgstart, pgend);
583 case ASHMEM_GET_PIN_STATUS:
584 ret = ashmem_get_pin_status(asma, pgstart, pgend);
588 mutex_unlock(&ashmem_mutex);
593 static long ashmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
595 struct ashmem_area *asma = file->private_data;
599 case ASHMEM_SET_NAME:
600 ret = set_name(asma, (void __user *) arg);
602 case ASHMEM_GET_NAME:
603 ret = get_name(asma, (void __user *) arg);
605 case ASHMEM_SET_SIZE:
609 asma->size = (size_t) arg;
612 case ASHMEM_GET_SIZE:
615 case ASHMEM_SET_PROT_MASK:
616 ret = set_prot_mask(asma, arg);
618 case ASHMEM_GET_PROT_MASK:
619 ret = asma->prot_mask;
623 case ASHMEM_GET_PIN_STATUS:
624 ret = ashmem_pin_unpin(asma, cmd, (void __user *) arg);
626 case ASHMEM_PURGE_ALL_CACHES:
628 if (capable(CAP_SYS_ADMIN)) {
629 ret = ashmem_shrink(0, GFP_KERNEL);
630 ashmem_shrink(ret, GFP_KERNEL);
638 static struct file_operations ashmem_fops = {
639 .owner = THIS_MODULE,
641 .release = ashmem_release,
644 .unlocked_ioctl = ashmem_ioctl,
645 .compat_ioctl = ashmem_ioctl,
648 static struct miscdevice ashmem_misc = {
649 .minor = MISC_DYNAMIC_MINOR,
651 .fops = &ashmem_fops,
654 static int __init ashmem_init(void)
658 ashmem_area_cachep = kmem_cache_create("ashmem_area_cache",
659 sizeof(struct ashmem_area),
661 if (unlikely(!ashmem_area_cachep)) {
662 printk(KERN_ERR "ashmem: failed to create slab cache\n");
666 ashmem_range_cachep = kmem_cache_create("ashmem_range_cache",
667 sizeof(struct ashmem_range),
669 if (unlikely(!ashmem_range_cachep)) {
670 printk(KERN_ERR "ashmem: failed to create slab cache\n");
674 ret = misc_register(&ashmem_misc);
676 printk(KERN_ERR "ashmem: failed to register misc device!\n");
680 register_shrinker(&ashmem_shrinker);
682 printk(KERN_INFO "ashmem: initialized\n");
687 static void __exit ashmem_exit(void)
691 unregister_shrinker(&ashmem_shrinker);
693 ret = misc_deregister(&ashmem_misc);
695 printk(KERN_ERR "ashmem: failed to unregister misc device!\n");
697 kmem_cache_destroy(ashmem_range_cachep);
698 kmem_cache_destroy(ashmem_area_cachep);
700 printk(KERN_INFO "ashmem: unloaded\n");
703 module_init(ashmem_init);
704 module_exit(ashmem_exit);
706 MODULE_LICENSE("GPL");